Debian Codegraph @

common/time.c

raw symbols SCIP
1 /*
2 * Crossfire -- cooperative multi-player graphical RPG and adventure game
3 *
4 * Copyright (c) 1999-2014 Mark Wedel and the Crossfire Development Team
5 * Copyright (c) 1992 Frank Tore Johansen
6 *
7 * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8 * welcome to redistribute it under certain conditions. For details, please
9 * see COPYING and LICENSE.
10 *
11 * The authors can be reached via e-mail at <[email protected]>.
12 */
13
14 /**
15 * @file
16 * In-game time functions.
17 */
18
19 #include "global.h"
20
21 #include <assert.h>
22 #include <math.h>
23
24 #include "tod.h"
25
26 #ifndef WIN32 /* ---win32 exclude header */
27 #include <stdio.h>
28 #include <sys/types.h>
29 #include <sys/time.h>
30 #endif /* win32 */
31
32 /**
33 * Gloabal variables:
34 */
35 uint32_t max_time = MAX_TIME;
36 static struct timespec game_time;
37
38 /** Size of history buffer. */
39 #define PBUFLEN 100
40 static uint32_t process_utime_save[PBUFLEN]; /**< Historic data. */
41 static uint32_t psaveind; /**< Index in ::process_utime_save. */
42 static uint32_t process_max_utime = 0; /**< Longest cycle time. */
43 static uint32_t process_min_utime = 999999999; /**< Shortest cycle time. */
44 static uint32_t process_tot_mtime; /**< ? */
45 uint32_t pticks; /**< ? */
46 static uint32_t process_utime_long_count; /**< Number of times server couldn't keep up with game time (tried to sleep for a negative time) */
47
48 /** Ingame seasons. */
49 static const char *const season_name[SEASONS_PER_YEAR+1] = {
50 "The Season of New Year",
51 "The Season of Growth",
52 "The Season of Harvest",
53 "The Season of Decay",
54 "The Season of the Blizzard",
55 "\n"
56 };
57
58 /** Days of the week. */
59 static const char *const weekdays[DAYS_PER_WEEK] = {
60 "the Day of the Moon",
61 "the Day of the Bull",
62 "the Day of the Deception",
63 "the Day of Thunder",
64 "the Day of Freedom",
65 "the Day of the Great Gods",
66 "the Day of the Sun"
67 };
68
69 /** Months. */
70 static const char *const month_name[MONTHS_PER_YEAR] = {
71 "Month of Winter", /* 0 */
72 "Month of the Ice Dragon",
73 "Month of the Frost Giant",
74 "Month of Valriel",
75 "Month of Lythander",
76 "Month of the Harvest",
77 "Month of Gaea",
78 "Month of Futility",
79 "Month of the Dragon",
80 "Month of the Sun",
81 "Month of the Great Infernus",
82 "Month of Ruggilli",
83 "Month of the Dark Shades",
84 "Month of the Devourers",
85 "Month of Sorig",
86 "Month of the Ancient Darkness",
87 "Month of Gorokh"
88 };
89
90 static const char *const periodsofday[PERIODS_PER_DAY] = {
91 "Night",
92 "Dawn",
93 "Morning",
94 "Noon",
95 "Evening",
96 "Dusk"
97 };
98
99 /**
100 * give access to weekday names
101 */
102 const char *get_periodofday(const int index) {
103 return ((index >= 0) && (index < PERIODS_PER_DAY)) ? periodsofday[index] : NULL;
104 }
105
106 /**
107 * give access to month names
108 */
109 const char *get_month_name(const int index) {
110 return ((index >= 0) && (index < MONTHS_PER_YEAR)) ? month_name[index] : NULL;
111 }
112
113 /**
114 * give access to weekday names
115 */
116 const char *get_weekday(const int index) {
117 return ((index >= 0) && (index < DAYS_PER_WEEK)) ? weekdays[index] : NULL;
118 }
119
120 /**
121 * give access to season names
122 */
123 const char *get_season_name(const int index) {
124 return ((index >= 0) && (index < (SEASONS_PER_YEAR+1))) ? season_name[index] : NULL;
125 }
126
127 /**
128 * Initialise all variables used in the timing routines.
129 */
130 void reset_sleep(void) {
131 int i;
132
133 for (i = 0; i < PBUFLEN; i++)
134 process_utime_save[i] = 0;
135 psaveind = 0;
136 process_max_utime = 0;
137 process_min_utime = 999999999;
138 process_tot_mtime = 0;
139 pticks = 0;
140
141 clock_gettime(CLOCK_MONOTONIC, &game_time);
142 }
143
144 /**
145 * Adds time to our history list.
146 */
147 static void log_time(uint32_t process_utime) {
148 pticks++;
149 if (++psaveind >= PBUFLEN)
150 psaveind = 0;
151 process_utime_save[psaveind] = process_utime;
152 if (process_utime > process_max_utime)
153 process_max_utime = process_utime;
154 if (process_utime < process_min_utime)
155 process_min_utime = process_utime;
156 process_tot_mtime += process_utime/1000;
157 }
158
159 /**
160 * Return the difference between two timespec's in microseconds.
161 */
162 long timespec_diff(struct timespec *end, struct timespec *start) {
163 long long sec = (long long)end->tv_sec - (long long)start->tv_sec;
164 long nsec = end->tv_nsec - start->tv_nsec;
165 return sec * 1e6 + nsec / 1e3;
166 }
167
168 /**
169 * Add 'usec' microseconds to the given timespec.
170 */
171 static void timespec_add(struct timespec *time, long usec) {
172 long nsec_sum = time->tv_nsec + usec * 1e3;
173 time->tv_sec += nsec_sum / 1e9;
174 time->tv_nsec = nsec_sum % (long)1e9;
175 }
176
177 /*
178 * Add one tick length to the last tick time.
179 */
180 void tick_game_time() {
181 timespec_add(&game_time, max_time);
182 }
183
184 long get_sleep_remaining() {
185 struct timespec now;
186 clock_gettime(CLOCK_MONOTONIC, &now);
187 long time_since_last_sleep = timespec_diff(&now, &game_time);
188 log_time(time_since_last_sleep);
189 return max_time - time_since_last_sleep;
190 }
191
192 void jump_time() {
193 process_utime_long_count++;
194 clock_gettime(CLOCK_MONOTONIC, &game_time);
195 }
196
197 /**
198 * Sets the max speed. Can be called by a DM through the speed command.
199 *
200 * @param t
201 * new speed.
202 */
203 void set_max_time(long t) {
204 max_time = t;
205 }
206
207 extern unsigned long todtick;
208
209 /**
210 * Computes the ingame time of the day.
211 *
212 * @param[out] tod
213 * where to store information. Must not be NULL.
214 */
215 void get_tod(timeofday_t *tod) {
216 tod->year = todtick/HOURS_PER_YEAR;
217 tod->month = (todtick/HOURS_PER_MONTH)%MONTHS_PER_YEAR;
218 tod->day = (todtick%HOURS_PER_MONTH)/DAYS_PER_MONTH;
219 tod->dayofweek = tod->day%DAYS_PER_WEEK;
220 tod->hour = todtick%HOURS_PER_DAY;
221 tod->minute = (pticks%PTICKS_PER_CLOCK)/(PTICKS_PER_CLOCK/58);
222 if (tod->minute > 58)
223 tod->minute = 58; /* it's imprecise at best anyhow */
224 tod->weekofmonth = tod->day/WEEKS_PER_MONTH;
225 if (tod->month < 3)
226 tod->season = 0;
227 else if (tod->month < 6)
228 tod->season = 1;
229 else if (tod->month < 9)
230 tod->season = 2;
231 else if (tod->month < 12)
232 tod->season = 3;
233 else
234 tod->season = 4;
235
236 if (tod->hour < 5) /*until 4:59*/
237 tod->periodofday = 0;
238 else if (tod->hour < 8)
239 tod->periodofday = 1;
240 else if (tod->hour < 13)
241 tod->periodofday = 2;
242 else if (tod->hour < 15)
243 tod->periodofday = 3;
244 else if (tod->hour < 20)
245 tod->periodofday = 4;
246 else if (tod->hour < 23)
247 tod->periodofday = 5;
248 else /*back to night*/
249 tod->periodofday = 0;
250 }
251
252 /**
253 * Prints the time.
254 *
255 * @param op
256 * player who requested time.
257 */
258 static void print_tod(object *op) {
259 timeofday_t tod;
260 const char *suf;
261 int day;
262 char buf1[128];
263
264 get_tod(&tod);
265
266 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_INFO,
267 "It is %s, on %s", time_format_time(&tod, buf1, sizeof(buf1)), weekdays[tod.dayofweek]);
268
269 day = tod.day+1;
270 if (day == 1 || ((day%10) == 1 && day > 20))
271 suf = "st";
272 else if (day == 2 || ((day%10) == 2 && day > 20))
273 suf = "nd";
274 else if (day == 3 || ((day%10) == 3 && day > 20))
275 suf = "rd";
276 else
277 suf = "th";
278
279 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_INFO,
280 "The %d%s Day of the %s, Year %d",
281 day, suf, month_name[tod.month], tod.year+1);
282
283 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_INFO,
284 "Time of Year: %s",
285 season_name[tod.season]);
286 }
287
288 /**
289 * Players wants to know the time. Called through the 'time' command.
290 *
291 * @param op
292 * player who requested time.
293 */
294 void time_info(object *op) {
295 int tot = 0, long_count = 0;
296 uint32_t maxt = 0, mint = 99999999, i;
297
298 print_tod(op);
299
300 if (!QUERY_FLAG(op, FLAG_WIZ))
301 return;
302
303 draw_ext_info_format(
304 NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_DEBUG,
305 "Statistics for last %d ticks:\n\tmin/avg/max = %d/%d/%d ms per tick",
306 pticks, process_min_utime / 1000, process_tot_mtime / pticks,
307 process_max_utime / 1000);
308
309 draw_ext_info_format(
310 NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_DEBUG,
311 "\tticks longer than %d ms = %d (%d%%)", max_time / 1000,
312 process_utime_long_count, 100 * process_utime_long_count / pticks);
313
314 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_DEBUG,
315 "Time last %d ticks:",
316 MIN(pticks, PBUFLEN));
317
318 for (i = 0; i < MIN(pticks, PBUFLEN); i++) {
319 tot += process_utime_save[i];
320 if (process_utime_save[i] > maxt)
321 maxt = process_utime_save[i];
322 if (process_utime_save[i] < mint)
323 mint = process_utime_save[i];
324 if (process_utime_save[i] > max_time)
325 long_count++;
326 }
327
328 assert(pticks > 0);
329
330 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_DEBUG,
331 "avg time=%dms max time=%dms min time=%dms",
332 tot/MIN(pticks, PBUFLEN)/1000, maxt/1000,
333 mint/1000);
334
335 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_DEBUG,
336 "ticks longer than max time (%dms) = %d (%d%%)",
337 max_time/1000, long_count,
338 100*long_count/MIN(pticks, PBUFLEN));
339 }
340
341 /**
342 * Return wall clock time in seconds.
343 */
344 long seconds(void) {
345 struct timespec now;
346 clock_gettime(CLOCK_REALTIME, &now);
347 return now.tv_sec;
348 }
349
350 /**
351 * Formats a timestamp in Crossfire time.
352 *
353 * @param tod
354 * the timestamp to format
355 *
356 * @param buf
357 * the buffer to fill
358 *
359 * @param bufsize
360 * the size of buf in bytes
361 *
362 * @return
363 * buf
364 */
365 const char *time_format_time(const timeofday_t *tod, char *buf, size_t bufsize)
366 {
367 snprintf(buf, bufsize, "%d minute%s past %d o'clock %s",
368 tod->minute+1,
369 tod->minute+1 < 2 ? "" : "s",
370 tod->hour%14 == 0 ? 14 : tod->hour%14,
371 tod->hour >= 14 ? "pm" : "am");
372 return buf;
373 }
374
375 /**
376 * Calculate the number of ticks that correspond to real time.
377 */
378 unsigned int tick_length(float seconds) {
379 return (int)ceil(seconds * 1000000 / max_time);
380 }