Fuel Runs Down
This demonstrates an engine which is started by a player action, and then slowly runs down over time until it stops. The generator on Goldspring Island uses code similar to this.
This requires four properties, all at the realm (Age-wide) level. There will be one more code property wherever the generator is located.
The logic is simple but the math is slightly hairy.
lightlevel is always an integer from 0 (no fuel) to 6 (completely full). We consider the generator to be running whenever
lightlevel is nonzero. (Any number of room descriptions can depend on
lastrefill is the timestamp of the last time the generator was refilled; we provide an initial value of (effectively) forever ago.
When the instance awakens (when someone links in), the
on_wake hook sets
measure_fuel to run every ten minutes. It also makes an immediate call to
measure_fuel so that the light level will be properly adjusted for the incoming player.
measure_fuel property checks the last refill time, and sets
lightlevel will be between 5 and 0 after this call. An incoming player will never see the 6 (completely full) state. Note that if
lightlevel becomes zero because of this, we display a message. (But not if it remains zero.)
measure_fuel divides the interval by 28 minutes, the
lightlevel will decrease by one every half-hour (roughly). The full value of 6 will run down to 0 in about three hours.
Note that this is not precise. The value can only change during a
measure_fuel call, which probably won't occur exactly 28 minutes after the button is pushed. But that check occurs every ten minutes, so the light shift will be no more than ten minutes late.
The filling action simply sets
lightlevel to 6, and
lastrefill to the present time. It looks complicated, but that's just to cover the various cases that the player might find it in (full, partially full, or empty).
measure_fuel sched(datetime.timedelta(minutes=10), measure_fuel, repeat=True)
if realm.lightlevel: realm.lightlevel = max(0, 5 - int((datetime.now - realm.lastrefill) / datetime.timedelta(minutes=28))) if not realm.lightlevel: eventloc('locname', 'The generator chokes, stutters, and goes silent.')
Then, in the generator location (
locname), add this action for refilling the generator:
if realm.lightlevel == 6: event('The generator doesn’t need any additional fuel right now.') elif realm.lightlevel: realm.lastrefill = datetime.now realm.lightlevel = 6 event('You top off the generator with fuel.', text('[$name] tops off the generator’s fuel level.')) else: realm.lastrefill = datetime.now realm.lightlevel = 6 event('You refill the generator with fuel. It hums to life.', text('[$name] refills the generator. It hums to life.'))
A room description that includes light might look like this:
The lights are [$if realm.lightlevel]on[$else]off[$end].