/
date.c
338 lines (254 loc) · 8.11 KB
/
date.c
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
/*
* $Id: date.c,v 1.3 2007/09/21 11:00:26 devel Exp $ (c) 2001 D.Brockhaus
*
* Game time and date. Also calculates stuff like sunrise/sunset, solstice (longest/shortest
* day of the year), equinox (day and night of same length), the state of the moon and the
* daylight created by sun and moon. It updates a bunch of global variables which can be used
* to determine the current date etc.
*
* $Log: date.c,v $
* Revision 1.3 2007/09/21 11:00:26 devel
* spawn times to /time
*
* Revision 1.2 2007/07/24 18:55:19 devel
* dlight darker to work better withnew client lighting
*
*/
#include <time.h>
#include "server.h"
#include "talk.h"
#include "clan.h"
#include "date.h"
#define STARTTIME 978303600 // 01/01/2001
#define DAYLEN (60*60*2)
#define HOURLEN (DAYLEN/24)
#define MINLEN (HOURLEN/60)
#define WEEKLEN (DAYLEN*7)
#define MONTHLEN (DAYLEN*30)
#define YEARLEN (MONTHLEN*12)
#define MOONLEN (DAYLEN*28)
int dlight=0;
int newmoon=0;
int fullmoon=0;
int moonsize=0;
int solstice=0;
int equinox=0;
int summer_solstice=0;
int winter_solstice=0;
int spring_equinox=0;
int fall_equinox=0;
int sunrise=0;
int sunset=0;
int moonrise=0;
int moonset=0;
int moonlight=0;
int sunlight=0;
int minute=0; // 0...59
int hour=0; // 0...23
int mday=0; // 0...29
int wday=0; // 0...6
int yday=0; // 0...359
int week=0; // warning: weeks since beginning of time !
int month=0; // 0...11
int year=0; // 0...
int moon=0; // warning: moons since beginning of time !
int moonday=0; // 0...27
int realtime=0; // seconds realtime since beginning of our epoch
extern int dlight_override;
//float new_system=0.0f;
//float old_system=1.0f;
void tick_date(void)
{
int t,daytime;
t=(time_now-STARTTIME); // local time starts on 01/01/2001
/*{
int start=1054677600,stop=1062540000;
if (time_now>start) {
if (time_now<stop) {
new_system=(double)(time_now-start)/(double)(stop-start);
} else new_system=1.0;
} else new_system=0.0;
old_system=1.0f-new_system;
}*/
realtime=t;
minute=(t/MINLEN)%60;
hour=(t/HOURLEN)%24;
week=(t/WEEKLEN);
wday=(t-week*WEEKLEN)/DAYLEN;
month=t/MONTHLEN;
mday=(t-month*MONTHLEN)/DAYLEN;
month=month%12;
year=t/YEARLEN;
yday=(t-year*YEARLEN)/DAYLEN;
moon=t/MOONLEN;
moonday=(t-moon*MOONLEN)/DAYLEN;
if (yday==0) { // winter solstice
sunrise=(HOURLEN*6)+90*MINLEN;
sunset=(HOURLEN*18)-90*MINLEN;
solstice=1;
summer_solstice=0;
winter_solstice=1;
equinox=0;
spring_equinox=0;
fall_equinox=0;
} else if (yday<90) { // winter/spring
sunrise=(HOURLEN*6)+(90-yday)*MINLEN;
sunset=(HOURLEN*18)-(90-yday)*MINLEN;
solstice=0;
summer_solstice=0;
winter_solstice=0;
equinox=0;
spring_equinox=0;
fall_equinox=0;
} else if (yday==90) { // spring equinox
sunrise=(HOURLEN*6);
sunset=(HOURLEN*18);
solstice=0;
summer_solstice=0;
winter_solstice=0;
equinox=1;
spring_equinox=1;
fall_equinox=0;
} else if (yday<180) { // spring/summer
sunrise=(HOURLEN*6)-(yday-90)*MINLEN;
sunset=(HOURLEN*18)+(yday-90)*MINLEN;
solstice=0;
summer_solstice=0;
winter_solstice=0;
equinox=0;
spring_equinox=0;
fall_equinox=0;
} else if (yday==180) { // summer solstice
sunrise=(HOURLEN*6)-90*MINLEN;
sunset=(HOURLEN*18)+90*MINLEN;
solstice=1;
summer_solstice=1;
winter_solstice=0;
equinox=0;
spring_equinox=0;
fall_equinox=0;
} else if (yday<270) { // summer/fall
sunrise=(HOURLEN*6)-(270-yday)*MINLEN;
sunset=(HOURLEN*18)+(270-yday)*MINLEN;
solstice=0;
summer_solstice=0;
winter_solstice=0;
equinox=0;
spring_equinox=0;
fall_equinox=0;
} else if (yday==270) { // fall equinox
sunrise=(HOURLEN*6);
sunset=(HOURLEN*18);
solstice=0;
summer_solstice=0;
winter_solstice=0;
equinox=1;
spring_equinox=0;
fall_equinox=1;
} else { // fall/winter
sunrise=(HOURLEN*6)+(yday-270)*MINLEN;
sunset=(HOURLEN*18)-(yday-270)*MINLEN;
solstice=0;
summer_solstice=0;
winter_solstice=0;
equinox=0;
spring_equinox=0;
fall_equinox=0;
}
if (moonday==0) { // new moon
moonrise=6*HOURLEN;
moonset=(moonrise+HOURLEN*12)%DAYLEN;
moonsize=0;
newmoon=1;
fullmoon=0;
} else if (moonday<14) {
moonrise=6*HOURLEN+(moonday*HOURLEN*12/14);
moonset=(moonrise+HOURLEN*12)%DAYLEN;
moonsize=moonday;
newmoon=0;
fullmoon=0;
} else if (moonday==14) {
moonrise=18*HOURLEN;
moonset=(moonrise+HOURLEN*12)%DAYLEN;
moonsize=14;
newmoon=0;
fullmoon=1;
} else {
moonrise=(6*HOURLEN+(moonday*HOURLEN*12/14))%DAYLEN;
moonset=(moonrise+HOURLEN*12)%DAYLEN;
moonsize=28-moonday;
newmoon=0;
fullmoon=0;
}
daytime=t%DAYLEN;
if (daytime<sunrise) sunlight=0;
else if (daytime<sunrise+HOURLEN) sunlight=(daytime-sunrise)*255/HOURLEN;
else if (daytime<=sunset) sunlight=255;
else if (daytime>sunset+HOURLEN) sunlight=0;
else if (daytime>sunset) sunlight=255-(daytime-sunset)*255/HOURLEN;
if (moonrise<moonset) {
if (daytime<moonrise) moonlight=0;
else if (daytime<moonrise+HOURLEN) moonlight=(daytime-moonrise)*255/HOURLEN;
else if (daytime<=moonset) moonlight=255;
else if (daytime>moonset+HOURLEN) moonlight=0;
else if (daytime>moonset) moonlight=255-(daytime-moonset)*255/HOURLEN;
} else {
if (daytime<=moonset) moonlight=255;
else if (daytime<moonset+HOURLEN) moonlight=255-(daytime-moonset)*255/HOURLEN;
else if (daytime<moonrise) moonlight=0;
else if (daytime<moonrise+HOURLEN) moonlight=(daytime-moonrise)*255/HOURLEN;
else if (daytime>=moonrise) moonlight=255;
}
// moonlight 0...255, moonsize 0...14
moonlight=moonlight*moonsize/223;
switch(areaID) {
case 2: dlight=64; break;
case 23: dlight=16; break;
case 24: dlight=16; break;
default: dlight=max(sunlight,moonlight); break;
}
if (dlight_override) dlight=dlight_override;
}
void showtime(int cn)
{
int nr,idx,off1,off2,off3;
log_char(cn,LOG_SYSTEM,0,"It's %02d:%02d on the %d/%d/%d. Sunrise is at %02d:%02d, sunset at %02d:%02d. Moonrise is at %02d:%02d, moonset at %02d:%02d.",
hour,minute,month+1,mday+1,year,
sunrise/HOURLEN,(sunrise%HOURLEN)/MINLEN,
sunset/HOURLEN,(sunset%HOURLEN)/MINLEN,
moonrise/HOURLEN,(moonrise%HOURLEN)/MINLEN,
moonset/HOURLEN,(moonset%HOURLEN)/MINLEN);
if (!fullmoon && !newmoon) {
if (moonsize<3) log_char(cn,LOG_SYSTEM,0,"Quarter Moon.");
else if (moonsize<10) log_char(cn,LOG_SYSTEM,0,"Half Moon.");
else log_char(cn,LOG_SYSTEM,0,"Three Quarter Moon.");
}
if (newmoon) log_char(cn,LOG_SYSTEM,0,"Be careful, New Moon tonight!");
if (fullmoon) log_char(cn,LOG_SYSTEM,0,"It's a fine day, Full Moon tonight!");
if (summer_solstice) log_char(cn,LOG_SYSTEM,0,"It's a great day, it's Summer Solstice today!");
if (winter_solstice) log_char(cn,LOG_SYSTEM,0,"It's a scary day, it's Winter Solstice today!");
if (spring_equinox) log_char(cn,LOG_SYSTEM,0,"Everything is in balance, it's Spring Equinox today!");
if (fall_equinox) log_char(cn,LOG_SYSTEM,0,"Everything is in balance, it's Fall Equinox today!");
if (moonday<14) {
log_char(cn,LOG_SYSTEM,0,"Next full moon is in %d days.",14-moonday);
} else {
log_char(cn,LOG_SYSTEM,0,"Next new moon is in %d days.",28-moonday);
}
if (yday<90) {
log_char(cn,LOG_SYSTEM,0,"Spring Equinox will be in %d days.",90-yday);
} else if (yday<180) {
log_char(cn,LOG_SYSTEM,0,"Summer Solstice will be in %d days.",180-yday);
} else if (yday<270) {
log_char(cn,LOG_SYSTEM,0,"Fall Equinox will be in %d days.",270-yday);
} else {
log_char(cn,LOG_SYSTEM,0,"Winter Solstice will be in %d days.",360-yday);
}
nr=(realtime/(BIGSPAWNFREQ))*BIGSPAWNFREQ+BIGSPAWNFREQ-realtime;
idx=(realtime/BIGSPAWNFREQ)%3;
if (idx==0) { off1=nr; off2=nr+1*BIGSPAWNFREQ; off3=nr+2*BIGSPAWNFREQ; }
else if (idx==1) { off1=nr+2*BIGSPAWNFREQ; off2=nr; off3=nr+1*BIGSPAWNFREQ; }
else { off1=nr+1*BIGSPAWNFREQ; off2=nr+2*BIGSPAWNFREQ; off3=nr; }
log_char(cn,LOG_SYSTEM,0,"Next spawn for levels 5 to 38 in: %d:%02d:%02d",off1/60/60,(off1/60)%60,off1%60);
log_char(cn,LOG_SYSTEM,0,"Next spawn for levels 40 to 60 in: %d:%02d:%02d",off2/60/60,(off2/60)%60,off2%60);
log_char(cn,LOG_SYSTEM,0,"Next spawn for levels 64 to 200 in: %d:%02d:%02d",off3/60/60,(off3/60)%60,off3%60);
}