1
<HTML><HEAD><TITLE>Section 18: Daemons and the passing of time</TITLE></HEAD>
2
<BODY BGCOLOR="#FFFFFF">
4
<TR><TD Valign="top"><A HREF="contents.html">Contents</A><BR><A HREF="section17.html">Back</A><BR><A HREF="section19.html">Forward</A><TD bgcolor="#F5DEB3"><BLOCKQUOTE><H3>18. Daemons and the passing of time</H3></BLOCKQUOTE><TR><TD><TD>
8
Some, such as Sleep and Love, were never human. From this
9
class an individual daemon is allotted to each human being
10
as his 'witness and guardian' through life.
11
<P>...C. S. Lewis (<B>1898</B>--<B>1963</B>), <I>The Discarded Image</I></BLOCKQUOTE>
15
A great Daemon... Through him subsist all
16
divination, and the science of sacred things as it relates
17
to sacrifices, and expiations, and disenchantments, and
18
prophecy, and magic... he who is wise in the science
19
of this intercourse is supremely happy...\
20
<P>...Plato (c.<B>427</B>--<B>347</B> BC),
21
<I> The Symposium</I>, translated by Percy Bysshe Shelley
22
(<B>1792</B>--<B>1822</B>)</BLOCKQUOTE>
27
In medieval neo-Platonist philosophy, daemons are the intermediaries
28
of God, hovering invisibly over the world and interfering with it.
29
They may be guardian spirits of places or people. So, here, a daemon is
30
a meddling spirit, associated with a particular game object, which
31
gets a chance to interfere once per turn while it is 'active'.
32
The classic example is of the dwarves of 'Advent', who
33
appear in the cave from time to time: a daemon routine attached to
34
the dwarf object moves it about, throws knives at the player and
36
Each object can have a <TT>daemon</TT> routine of its own. This is set
37
going, and stopped again, by calling the (library) routines
43
Once active, the <TT>daemon</TT> property of the object is called as a routine
44
each turn. Daemons are often started by a game's <TT>Initialise</TT> routine
45
and sometimes remain active throughout. For instance, a lamp-battery
46
daemon might do something every turn, while others may hide for many
47
turns before pouncing: such as the daemon in 'Advent' which waits
48
until the player has found all the treasures.
51
<P><TR><TD Valign="top"><IMG SRC="icons/dbend.gif" ALT="/\"><TD bgcolor="#EEEEEE"><SMALL> In particular, a daemon doesn't stop running just because the player
52
has moved on to somewhere else. (Indeed, the library never stops a daemon
53
unless told to.) Actually this is very useful, as it means daemons can be
54
used for 'tidying-up operations', or for the consequences of the player's
55
actions to catch up with him.
59
<P><TR><TD Valign="top"><IMG SRC="icons/exercise.gif" ALT="??"><TD bgcolor="#FBB9AC"><A NAME="ex34"><B>EXERCISE 34:</B><BR>(link to <A HREF="answers1/answer34.html">the answer</A>)<TR><TD><TD> Many games contain 'wandering monsters', characters who walk
60
around the map. Use a daemon
61
to implement one who wanders as freely as the player, like the
62
gentleman thief in 'Zork'.
66
<P><TR><TD Valign="top"><IMG SRC="icons/dexercise.gif" ALT="??/\"><TD bgcolor="#FBB9AC"><A NAME="ex35"><B>EXERCISE 35:</B><BR>(link to <A HREF="answers1/answer35.html">the answer</A>)<TR><TD><TD>
67
Use a background daemon to implement a system of weights, so
68
that the player can only carry a certain weight before her strength gives
69
out and she is obliged to drop something. It should allow for feathers to
70
be lighter than lawn-mowers.
75
A 'timer' (these are traditionally called 'fuses') can also
76
be attached to an object. A timer is started with
78
StartTimer(object, time);
82
in which case it will 'go off', alarm clock-style, in the given number of
83
turns. This means that its <TT>time_out</TT>
85
property will be called, once and once only, when the time comes.
86
The timer can be deactivated (so that it will never go off) by calling
91
A timer is required to provide a <TT>time_left</TT> property, to hold the
92
amount of time left. (If it doesn't, an error message is printed
93
at run-time.) You can alter <TT>time_left</TT> yourself: a value of 0 means 'will
94
go off at the end of this turn', so setting <TT>time_left</TT> to 0 triggers
98
<P><TR><TD Valign="top"><IMG SRC="icons/dbend.gif" ALT="/\"><TD bgcolor="#EEEEEE"><SMALL> Normally, you can only have 32 timers or daemons active at the same
99
time as each other (plus any number of inactive ones). But this
100
limit is easily raised: just define the constant <TT>MAX_TIMERS</TT>
102
to some larger value, putting the definition in your code before the
103
<TT>Parser</TT> file is included.
109
There is yet a third form of timed event. If a room provides an <TT>each_turn</TT>
110
routine, then this will be called at the end of each turn while the player
111
is there; if an object provides <TT>each_turn</TT>, this is called while the object is
112
nearby. For instance, a radio might blare out music
113
whenever it is nearby; a sword might glow whenever monsters are nearby;
114
or a stream running through several forest locations might occasionally
118
'Each turn' is especially useful to run creatures which stay
119
in one room and are only active when the player is nearby.
120
An ogre with limited patience can therefore have an <TT>each_turn</TT> routine
121
which worries the player ("The ogre stamps his feet angrily!'', etc.)
122
while also having a timer set to go off when his patience runs out.
125
<P><TR><TD Valign="top"><IMG SRC="icons/dbend.gif" ALT="/\"><TD bgcolor="#EEEEEE"><SMALL> 'Nearby' actually means 'in scope', a term which will be properly
126
explained later. The idea is based on line of sight, which works well
131
<P><TR><TD Valign="top"><IMG SRC="icons/ddbend.gif" ALT="/\/\"><TD bgcolor="#EEEEEE"><SMALL> But it does mean that the radio will be inaudible when shut up
132
inside most containers -- which is arguably fair enough -- yet audible when
133
shut up inside transparent, say glass, ones. You can always change the
134
scope rules using an <TT>InScope</TT> routine to get around this. In case you
135
want to tell whether scope is being worked out for ordinary parsing reasons
136
or instead for <TT>each_turn</TT> processing, look at the <TT>scope_reason</TT> variable
137
(see <A HREF="section28.html">Section 28</A>). Powerful effects are available
138
this way -- you could put the radio in scope within all nearby rooms so as
139
to allow sound to travel. Or you could make a thief audible throughout
140
the maze he is wandering around in, as in 'Zork I'.
145
<P><TR><TD Valign="top"><IMG SRC="icons/exercise.gif" ALT="??"><TD bgcolor="#FBB9AC"><A NAME="ex36"><B>EXERCISE 36:</B><BR>(link to <A HREF="answers1/answer36.html">the answer</A>)<TR><TD><TD> (Why the 'Ruins' are claustrophobic.) Make
146
"the sound of scuttling claws'' approach the player in darkness and,
147
after 4 consecutive turns in darkness, kill him.
151
<P><TR><TD Valign="top"><IMG SRC="icons/dexercise.gif" ALT="??/\"><TD bgcolor="#FBB9AC"><A NAME="ex37"><B>EXERCISE 37:</B><BR>(link to <A HREF="answers1/answer37.html">the answer</A>)<TR><TD><TD> A little harder: implement the scuttling claws in a single
152
object definition, with no associated code anywhere else in the program
153
(not even a line in <TT>Initialise</TT>) and without running its daemon all the
159
The library also has the (limited) ability to keep track of time of day
160
as the game goes on. The current time is held in the variable <TT>the_time</TT>
161
and runs on a 24-hour clock: this variable holds minutes since midnight,
162
so it has values between 0 and 1439. The time can be set by
164
<TT>SetTime(</TT> 60$\times$<I><B><hours></B></I><I>+</I><I><B><minutes></B></I><TT>,</TT> <I><B><rate></B></I> <TT>);</TT>
167
The <TT>rate</TT> controls how rapidly time is moving: a <TT>rate</TT> of 0 means it is
168
standing still (that is, that the library doesn't change it: your routines
169
still can). A positive <TT>rate</TT> means that that many minutes pass between
170
each turn, while a negative <TT>rate</TT> means that many turns pass between each
171
minute. (It's usual for a timed game to start off the clock by calling
172
<TT>SetTime</TT> in its <TT>Initialise</TT> routine.)
173
The time only (usually) appears on the game's status line if you set
178
as a directive somewhere in your code.
181
<P><TR><TD Valign="top"><IMG SRC="icons/exercise.gif" ALT="??"><TD bgcolor="#FBB9AC"><A NAME="ex38"><B>EXERCISE 38:</B><BR>(link to <A HREF="answers1/answer38.html">the answer</A>)<TR><TD><TD> How could you make your game take notice of the time passing
182
midnight, so that the day of the week could be nudged
187
<P><TR><TD Valign="top"><IMG SRC="icons/dexercise.gif" ALT="??/\"><TD bgcolor="#FBB9AC"><A NAME="ex39"><B>EXERCISE 39:</B><BR>(link to <A HREF="answers1/answer39.html">the answer</A>)<TR><TD><TD> (Cf. Sam Hulick's vampire game, 'Knight of Ages'.)
188
Make the lighting throughout the game change at
193
<TR><TD><TD bgcolor="#EEEEEE"><SMALL>
194
<P><TR><TD Valign="top"><IMG SRC="icons/dbend.gif" ALT="/\"><TD bgcolor="#EEEEEE"><SMALL> Exactly what happens at the end of each turn is:
196
<P>1. -- The turns counter is incremented.
197
<P>2. -- The 24-clock is moved on.
198
<P>3. -- Daemons and timers are run (in no guaranteed order).
199
<P>4. -- <TT>each_turn</TT> takes place for the current room, and then for
200
everything nearby (that is, in scope).
201
<P>5. -- The game's global <TT>TimePasses</TT> routine is called.
202
<P>6. -- Light is re-considered (it may have changed as a result
203
of events since this time last turn).
205
The sequence is abandoned if at any stage the player
206
dies or wins.</SMALL><TR><TD><TD>
210
<P><TR><TD Valign="top"><IMG SRC="icons/dexercise.gif" ALT="??/\"><TD bgcolor="#FBB9AC"><A NAME="ex40"><B>EXERCISE 40:</B><BR>(link to <A HREF="answers1/answer40.html">the answer</A>)<TR><TD><TD> Suppose the player is magically suspended in mid-air,
211
but that anything let go of will fall out of sight. The natural way
212
to code this is to use a daemon which gets rid of anything it finds
213
on the floor (this is better than just trapping <TT>Drop</TT> actions because
214
objects might end up on the floor in many different ways). Why is
215
using <TT>each_turn</TT> better?
219
<P><TR><TD Valign="top"><IMG SRC="icons/exercise.gif" ALT="??"><TD bgcolor="#FBB9AC"><A NAME="ex41"><B>EXERCISE 41:</B><BR>(link to <A HREF="answers1/answer41.html">the answer</A>)<TR><TD><TD> How would a game work if it involved a month-long
220
archaeological dig, where anything from days to minutes pass between
221
successive game turns?
224
<P><TR><TD Valign="top"><IMG SRC="icons/refs.gif" ALT="*"><TD bgcolor="#EEEEEE"><B>REFERENCES:</B><BR><SMALL> Daemons abound in most games. 'Advent' uses them to run
225
down the lamp batteries, make the bear follow you, animate the
226
dwarves and the pirate and watch for the treasure all being found.
227
See also the flying tortoise from 'Balances' and the chiggers
228
from 'Adventureland'. For more ingenious uses of <TT>daemon</TT>,
229
see the helium balloon, the matchbook and (particularly cunning)
230
the pair of white gloves in 'Toyshop'.
232
Classic timers include the burning match and the hand grenade
233
from 'Toyshop', the endgame timer from 'Advent' and the
234
'Balances' cyclops (also employing <TT>each_turn</TT>).
236
'Adventureland' makes much use of <TT>each_turn</TT>: see the
237
golden fish, the mud, the dragon and the bees.
239
The library extension 'timewait.h' by Andrew Clover
240
thoroughly implements time of day, allowing the player to "wait until
241
quarter past three''.
243
<HR><A HREF="contents.html">Contents</A> / <A HREF="section17.html">Back</A> / <A HREF="section19.html">Forward</A> <BR>
244
<A HREF="chapter1.html">Chapter I</A> / <A HREF="chapter2.html">Chapter II</A> / <A HREF="chapter3.html">Chapter III</A> / <A HREF="chapter4.html">Chapter IV</A> / <A HREF="chapter5.html">Chapter V</A> / <A HREF="chapter6.html">Chapter VI</A> / <A HREF="chapterA.html">Appendix</A><HR><SMALL><I>Mechanically translated to HTML from third edition as revised 16 May 1997. Copyright © Graham Nelson 1993, 1994, 1995, 1996, 1997: all rights reserved.</I></SMALL></BODY></HTML>