-
Notifications
You must be signed in to change notification settings - Fork 11
/
ClockLib.aslx
387 lines (345 loc) · 12.2 KB
/
ClockLib.aslx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
<?xml version="1.0"?>
<library>
<!--
If you are viewing this on GitHub and want to download it, right click on the RAW button
just above, and select "Save link as..."
-->
<!--
ClockLib v3.3
Quest version: 5.6
Written by: The Pixie, 2011-2018
You must give the game object a Boolean attribute called "notarealturn" for the events to fire
and time to pass.
Tutorial and notes here:
https://github.com/ThePix/quest/wiki/Clock-Library
Version 3.1 adds functionality for displaying dates.
Version 3.2 displays noon as "12 noon" and midnight as "1 midnight"
Version 3.1a
This is now useable with NpcLib 1.5 (but not 1.4 - check which you have)
Note that you MUST use SetTime to set the initial time, as this will disable the
standard NpcLib turnscript.
As of version 3.2a
To customise (including use with other languages), set attributes of the game_clock object.
Version 3.3
This combines the versions above!
-->
<object name="game_clock">
<inherit name="editor_object" />
<time type="int">0</time>
<minutes type="int">0</minutes>
<hours24 type="int">0</hours24>
<days type="int">0</days>
<countdown type="int">0</countdown>
<increment type="int">1</increment>
<waittime type="int">15</waittime>
<midnight>12 midnight</midnight>
<midday>12 noon</midday>
<am>am</am>
<pm>pm</pm>
<testing type="boolean">false</testing>
<clockmsg>You take your fob watch from your pocket, and look at it. It's ###.</clockmsg>
<waitmsg>You wait ### minutes, but nothing happens.</waitmsg>
<changedtime type="script">
JS.eval("$('#clock').html('" + TimeAsString() + "');")
</changedtime>
<startyear type="int">2000</startyear>
</object>
<command name="clock">
<pattern>clock;time;watch</pattern>
<script>
P (Replace(game_clock.clockmsg, "###", TimeAsString()))
</script>
</command>
<command name="wait">
<pattern type="string">^wait$|^z$</pattern>
<script>
count = game_clock.waittime
game_clock.event = false
while (count > 0 and not game_clock.event) {
//msg("count = " + count)
//msg("game_clock.event = " + game_clock.event)
count = count - 1
IncTime()
}
if (not game_clock.event) {
P (Replace (game_clock.waitmsg, "###", "" + game_clock.waittime))
}
</script>
</command>
<turnscript name="ClockLibTurnScript">
<enabled type="boolean">true</enabled>
<script>
if (HasBoolean(game, "notarealturn")) {
if (not game.notarealturn) {
for (i, 1, game_clock.increment) {
IncTime ()
}
}
game_clock.increment = 1
game.notarealturn = false
}
else {
this.enabled = false
}
</script>
</turnscript>
<function name="TimeAsString" type="string"><![CDATA[
if (game_clock.minutes = 0 and game_clock.hours24 = 0) {
return (" " + game_clock.midnight)
}
if (game_clock.minutes = 0 and game_clock.hours24 = 12) {
return (" " + game_clock.midday)
}
hours = game_clock.hours24 % 12
if (hours = 0) {
hours = 12
}
s = " " + hours + ":" + DD(game_clock.minutes)
if (game_clock.hours24 < 12) {
return (s + " " + game_clock.am)
}
else {
return (s + " " + game_clock.pm)
}
]]></function>
<!--
Should only be called internally.
Moves time on 1 minute, and checks to see if any event is set for that minute.
-->
<function name="IncTime"><![CDATA[
on ready {
game_clock.time = game_clock.time + 1
//msg("game_clock.time = " + game_clock.time)
//msg("game_clock.event = " + game_clock.event)
game_clock.minutes = game_clock.time % 60
game_clock.hours24 = (game_clock.time / 60) % 24
game_clock.days = game_clock.time / (60 * 24)
name = "event_" + DD(game_clock.days) + "_" + DD(game_clock.hours24) + "_" + DD(game_clock.minutes)
game_clock.countdown = game_clock.countdown - 1
//msg("Looking for: " + name)
o = GetObject(name)
if (not o = null and HasScript(o, "look")) {
if (game_clock.testing) msg("Found: " + o + " (" + o.alias + ")")
// this is used by wait
game_clock.event = true
do(o, "look")
}
else if (game_clock.countdown < 0 and HasAttribute(game_clock, "nextstep")) {
if (game_clock.testing) msg("{colour:silver:Step found: " + game_clock.nextstep + " (" + game_clock.nextstep.alias + ")}")
// this is used by wait
game_clock.event = true
step = game_clock.nextstep
game_clock.nextstep = null
do(step, "look")
}
if (not GetObject("NpcTurnScript") = null) {
do(NpcTurnScript, "script")
}
EachTurn
game.clock = TimeAsString()
}
]]></function>
<!--
Can be overridden to have stuff happened each minute.
This will fire after any event scheduled for this time.
-->
<function name="EachTurn">
</function>
<!--
Set this during a command to have the given time expire during the command.
-->
<function name="SetInc" parameters="minutes">
game_clock.increment = minutes
</function>
<!--
Creates a new event for the given time, or as soon after that where there is a slot free.
-->
<function name="CreateEvent" parameters="time, alias, script">
time = game_clock.time + time
o = null
while (o = null) {
o = AttemptCreateEvent(time)
time = time + 1
}
o.look = script
o.alias = alias
</function>
<!--
Creates a new event for the given time, or as soon after that where there is a slot free.
-->
<function name="NextStep" parameters="step, delay">
game_clock.nextstep = step
game_clock.countdown = delay
//msg("delay=" + delay)
</function>
<!--
Will try to create an event at the given time, returning said event if successful.
If another event is set for that time, it will return null.
-->
<function name="AttemptCreateEvent" parameters="time" type="object">
minutes = time % 60
hours24 = (time / 60) % 24
days = time / (60 * 24)
name = "event_" + DD(days) + "_" + DD(hours24) + "_" + DD(minutes)
//msg("Setting for: " + name)
o = GetObject(name)
if (o = null) {
create(name)
return (GetObject(name))
}
else {
return (null)
}
</function>
<!--
Sets the game time. This can either be sent as an integer, the number of minutes since midnight on day 1,
or as a string, in this format: 01:14:35 (i.e, 2:35 pm on day 1).
-->
<function name="SetTime" parameters="minutes">
if (TypeOf(minutes) = "string") {
l = Split(minutes, ":")
if (not ListCount(l) = 3) error("SetTime failed to understand " + minutes)
game_clock.days = ToInt(StringListItem(l, 0))
game_clock.hours24 = ToInt(StringListItem(l, 1))
game_clock.minutes = ToInt(StringListItem(l, 2))
game_clock.time = (game_clock.days * 24 + game_clock.hours24) * 60 + game_clock.minutes
}
else {
game_clock.time = minutes
game_clock.minutes = game_clock.time % 60
game_clock.hours24 = (game_clock.time / 60) % 24
game_clock.days = game_clock.time / (60 * 24) + 1
}
if (not GetObject("NpcTurnScript") = null) {
DisableTurnScript (NpcTurnScript)
}
game.clock = TimeAsString()
</function>
<!--
Returns true if the game time is after the given time.
-->
<function name="IsAfter" parameters="time" type="boolean"><![CDATA[
if (TypeOf(time) = "string") {
l = Split(time, ":")
if (not ListCount(l) = 3) error("SetTime failed to understand " + minutes)
days = ToInt(StringListItem(l, 0))
hours24 = ToInt(StringListItem(l, 1))
minutes = ToInt(StringListItem(l, 2))
time = (days * 24 + hours24) * 60 + minutes
}
return (game_clock.time > time)
]]></function>
<!--
Returns true if the game time is after the given time. This version works for each day,
so IsAfterDaily("19:00") is true after 7 pm each day.
-->
<function name="IsAfterDaily" parameters="time" type="boolean"><![CDATA[
if (TypeOf(time) = "string") {
l = Split(time, ":")
if (not ListCount(l) = 2) error("SetTime failed to understand " + time)
days = game_clock.time / (60 * 24)
hours24 = ToInt(StringListItem(l, 0))
minutes = ToInt(StringListItem(l, 1))
time = (days * 24 + hours24) * 60 + minutes
}
return (game_clock.time > time)
]]></function>
<!--
Returns the number of minutes since midnight on day 1.
-->
<function name="GetTime" type="int">
return (game_clock.time)
</function>
<!--
Pads the given number (or string) is a single digit, it will be padded with an extra leading zero.
-->
<function name="DD" parameters="s" type="string"><![CDATA[
return (Pad (s, 2, "0"))
]]></function>
<!--
Pads the given number or string is returned, padded with extra characters to the right to make it up to the given length.
-->
<function name="Pad" parameters="string, number, pad" type="string"><![CDATA[
text = ""
string = ToString(string)
for (i, 1, number - LengthOf(string)) {
text = text + pad
}
return (text + string)
]]></function>
<!--
Call this from an event to have it happen in the background. What that means is that
if the WAIT command will not terminate at this event.
-->
<function name="Quiet"><![CDATA[
game_clock.event = false
]]></function>
<!--
Moves time forward by the given number of minutes, skipping any intervening events.
Note that turnscripts will be run once only.
Use with care!
-->
<function name="SkipTime" parameters="duration"><![CDATA[
game_clock.time = game_clock.time + duration
game_clock.minutes = game_clock.time % 60
game_clock.hours24 = (game_clock.time / 60) % 24
game_clock.days = game_clock.time / (60 * 24)
]]></function>
<!--
Displays the the date as a string. Dates are relative to the year game_clock.startyear, which is 2000 by default.
You can override DisplayDate to alter how the date is actually shown.
Day 1 is the first of January, 2000 (or whatever year you set game_clock.startyear to). This means you must set the day to
greater than zero in set time.
Note that this is an approximation to the Gregorian Calendar; it will handle simple leap years, but is not perfect.
It will consider 1900, for example, to be a leap year, however, it is fine for 1901 to 2099, which should cover most games.
This could be readily adapted for a fantasy calender.
-->
<function name="Date" type="string">
year = game_clock.startyear
day = game_clock.days
if (1 > day) error("Date function cannot cope with a day of zero or less")
// what year is it?
flag = true
while (flag) {
if (year % 4 = 0) {
days_this_year = 366
}
else {
days_this_year = 365
}
if (day > days_this_year) {
year = year + 1
day = day - days_this_year
}
else {
flag = false
}
}
// days in month
if (year % 4 = 0) {
days_in_months = Split("31;29;31;30;31;30;31;31;30;31;30;31", ";")
}
else {
days_in_months = Split("31;28;31;30;31;30;31;31;30;31;30;31", ";")
}
// what month is it?
month = 1
for (i, 0, 11) {
days_in_month = ToInt(StringListItem(days_in_months, i))
if (day > days_in_month) {
month = month + 1
day = day - days_in_month
}
else {
return (DisplayDate(year, month, day))
}
}
</function>
<!--
Shows the date, nicely formatted
-->
<function name="DisplayDate" parameters="year, month, day" type="string">
months = Split(";January;February;March;April;May;June;July;August;September;October;November;December", ";")
return ("" + day + "th of " + StringListItem(months, month) + ", " + year)
</function>
</library>