ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/time.C
(Generate patch)

Comparing deliantra/server/common/time.C (file contents):
Revision 1.2 by root, Tue Aug 15 18:07:24 2006 UTC vs.
Revision 1.10 by root, Sat Mar 17 22:11:22 2007 UTC

1/* 1/*
2 * static char *rcsid_time_c = 2 * CrossFire, A Multiplayer game for X-windows
3 * "$Id: time.C,v 1.2 2006/08/15 18:07:24 root Exp $"; 3 *
4 * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team
5 * Copyright (C) 2002 Mark Wedel & Crossfire Development Team
6 * Copyright (C) 1992 Frank Tore Johansen
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 *
22 * The authors can be reached via e-mail at <crossfire@schmorp.de>
4 */ 23 */
5
6/*
7 CrossFire, A Multiplayer game for X-windows
8
9 Copyright (C) 2002 Mark Wedel & Crossfire Development Team
10 Copyright (C) 1992 Frank Tore Johansen
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25
26 The authors can be reached via e-mail at crossfire-devel@real-time.com
27*/
28 24
29#include <global.h> 25#include <global.h>
30#include <funcpoint.h> 26#include <funcpoint.h>
31#include <tod.h> 27#include <tod.h>
32 28
33#ifndef WIN32 /* ---win32 exclude header */
34#include <stdio.h> 29#include <cstdio>
30
35#include <sys/types.h> 31#include <sys/types.h>
36#include <sys/time.h> 32#include <sys/time.h>
37#endif /* win32 */
38 33
39/* 34/*
40 * Gloabal variables: 35 * Gloabal variables:
41 */ 36 */
42long max_time = MAX_TIME; 37long pticks;
43struct timeval last_time;
44 38
45#define PBUFLEN 100
46long process_utime_save[PBUFLEN];
47long psaveind;
48long process_max_utime = 0;
49long process_min_utime = 999999999;
50long process_tot_mtime;
51long pticks;
52long process_utime_long_count;
53
54const char *season_name[] = 39static const char *season_name[5] = {
55{ 40 "New Year",
56 "The Season of New Year", 41 "Growth",
57 "The Season of Growth", 42 "Harvest",
58 "The Season of Harvest", 43 "Decay",
59 "The Season of Decay", 44 "the Blizzard",
60 "The Season of the Blizzard",
61 "\n"
62}; 45};
63 46
64const char *weekdays[DAYS_PER_WEEK] = { 47static const char *weekdays[DAYS_PER_WEEK] = {
65 "the Day of the Moon", 48 "the Moon",
66 "the Day of the Bull", 49 "the Bull",
67 "the Day of the Deception", 50 "the Deception",
68 "the Day of Thunder", 51 "Thunder",
69 "the Day of Freedom", 52 "Freedom",
70 "the Day of the Great Gods", 53 "the Great Gods",
71 "the Day of the Sun" 54 "the Sun"
72}; 55};
73 56
74const char *month_name[MONTHS_PER_YEAR] = { 57static const char *month_name[MONTHS_PER_YEAR] = {
75 "Month of Winter", /* 0 */ 58 "Winter", /* 0 */
76 "Month of the Ice Dragon", 59 "the Ice Dragon",
77 "Month of the Frost Giant", 60 "the Frost Giant",
78 "Month of Valriel", 61 "Valriel",
79 "Month of Lythander", 62 "Lythander",
80 "Month of the Harvest", 63 "the Harvest",
81 "Month of Gaea", 64 "Gaea",
82 "Month of Futility", 65 "Futility",
83 "Month of the Dragon", 66 "the Dragon",
84 "Month of the Sun", 67 "the Sun",
85 "Month of the Great Infernus", 68 "the Great Infernus",
86 "Month of Ruggilli", 69 "Ruggilli",
87 "Month of the Dark Shades", 70 "the Dark Shades",
88 "Month of the Devourers", 71 "the Devourers",
89 "Month of Sorig", 72 "Sorig",
90 "Month of the Ancient Darkness", 73 "the Ancient Darkness",
91 "Month of Gorokh" 74 "Gorokh"
92}; 75};
93 76
94/* 77void
95 * Initialise all variables used in the timing routines. 78get_tod (timeofday_t * tod)
96 */ 79{
80 unsigned int todtick = runtime * (1. / RUNTIME_PER_HOUR);
97 81
98void 82 tod->year = todtick / HOURS_PER_YEAR;
99reset_sleep(void) 83 tod->month = todtick / HOURS_PER_MONTH % MONTHS_PER_YEAR;
100{ 84 tod->day = todtick % HOURS_PER_MONTH / DAYS_PER_MONTH;
101 int i; 85 tod->hour = todtick % HOURS_PER_DAY;
102 for(i = 0; i < PBUFLEN; i++) 86 tod->minute = (runtime - todtick * RUNTIME_PER_HOUR) * (60. / RUNTIME_PER_HOUR);
103 process_utime_save[i] = 0;
104 psaveind = 0;
105 process_max_utime = 0;
106 process_min_utime = 999999999;
107 process_tot_mtime = 0;
108 pticks = 0;
109 87
110 (void) GETTIMEOFDAY(&last_time); 88 tod->dayofweek = tod->day % DAYS_PER_WEEK;
89 tod->weekofmonth = tod->day / WEEKS_PER_MONTH;
90 tod->season = min (4, tod->month / 3);
111} 91}
112 92
113void 93void
114log_time(long process_utime)
115{
116 pticks++;
117 if (++psaveind >= PBUFLEN)
118 psaveind = 0;
119 process_utime_save[psaveind] = process_utime;
120 if (process_utime > process_max_utime)
121 process_max_utime = process_utime;
122 if (process_utime < process_min_utime)
123 process_min_utime = process_utime;
124 process_tot_mtime += process_utime/1000;
125}
126
127/*
128 * enough_elapsed_time will return true if the time passed since
129 * last tick is more than max-time.
130 */
131
132int
133enough_elapsed_time(void)
134{
135 static struct timeval new_time;
136 long elapsed_utime;
137
138 (void) GETTIMEOFDAY(&new_time);
139
140 elapsed_utime = (new_time.tv_sec - last_time.tv_sec) * 1000000 +
141 new_time.tv_usec - last_time.tv_usec;
142 if (elapsed_utime > max_time) {
143 log_time(elapsed_utime);
144 last_time.tv_sec = new_time.tv_sec;
145 last_time.tv_usec = new_time.tv_usec;
146 return 1;
147 }
148 return 0;
149}
150
151void
152set_max_time(long t) {
153 max_time = t;
154}
155
156extern unsigned long todtick;
157
158void
159get_tod(timeofday_t *tod)
160{
161 tod->year = todtick/HOURS_PER_YEAR;
162 tod->month = (todtick/HOURS_PER_MONTH)%MONTHS_PER_YEAR;
163 tod->day = (todtick%HOURS_PER_MONTH)/DAYS_PER_MONTH;
164 tod->dayofweek = tod->day%DAYS_PER_WEEK;
165 tod->hour = todtick%HOURS_PER_DAY;
166 tod->minute = (pticks%PTICKS_PER_CLOCK)/(PTICKS_PER_CLOCK/58);
167 if (tod->minute > 58)
168 tod->minute = 58; /* it's imprecise at best anyhow */
169 tod->weekofmonth = tod->day/WEEKS_PER_MONTH;
170 if (tod->month < 3)
171 tod->season = 0;
172 else if (tod->month < 6)
173 tod->season = 1;
174 else if (tod->month < 9)
175 tod->season = 2;
176 else if (tod->month < 12)
177 tod->season = 3;
178 else
179 tod->season = 4;
180}
181
182void
183print_tod(object *op) 94print_tod (object *op)
184{ 95{
185 timeofday_t tod; 96 timeofday_t tod;
97
98 get_tod (&tod);
99
100 char todmsg [1024];
101
102 int day = tod.day + 1;
186 const char *suf; 103 const char *suf;
187 int day;
188
189 get_tod(&tod);
190 sprintf(errmsg, "It is %d minute%s past %d o'clock %s, on %s",
191 tod.minute+1, ((tod.minute+1 < 2) ? "" : "s"),
192 ((tod.hour % 14 == 0) ? 14 : ((tod.hour)%14)),
193 ((tod.hour >= 14) ? "pm" : "am"),
194 weekdays[tod.dayofweek]);
195 new_draw_info(NDI_UNIQUE, 0,op,errmsg);
196
197 day = tod.day + 1;
198 if (day == 1 || ((day % 10) == 1 && day > 20)) 104 if (day == 1 || ((day % 10) == 1 && day > 20))
199 suf = "st"; 105 suf = "st";
200 else if (day == 2 || ((day % 10) == 2 && day > 20)) 106 else if (day == 2 || ((day % 10) == 2 && day > 20))
201 suf = "nd"; 107 suf = "nd";
202 else if (day == 3 || ((day % 10) == 3 && day > 20)) 108 else if (day == 3 || ((day % 10) == 3 && day > 20))
203 suf = "rd"; 109 suf = "rd";
204 else 110 else
205 suf = "th"; 111 suf = "th";
206 sprintf(errmsg, "The %d%s Day of the %s, Year %d", day, suf,
207 month_name[tod.month], tod.year+1);
208 new_draw_info(NDI_UNIQUE, 0,op,errmsg);
209 112
210 sprintf(errmsg, "Time of Year: %s", season_name[tod.season]); 113 snprintf (todmsg, sizeof (todmsg),
114 "It is %d minute%s past %d o'clock %s, on the Day of %s,\n"
115 "the %d%s Day of the Month of %s, Year %d.\n"
116 "It is the Season of %s.",
117
118 tod.minute + 1,
119 (tod.minute + 1 < 2) ? "" : "s",
120 (tod.hour + 13) % 14 + 1,
121 (tod.hour >= 14 ? "pm" : "am"),
122 weekdays[tod.dayofweek],
123
124 day, suf, month_name[tod.month], tod.year + 1,
125
126 season_name[tod.season]);
127
211 new_draw_info(NDI_UNIQUE, 0,op,errmsg); 128 new_draw_info (NDI_UNIQUE, 0, op, todmsg);
212} 129}
213 130
214void
215time_info(object *op)
216{
217 int tot = 0, maxt = 0, mint = 99999999, long_count = 0, i;
218
219 print_tod(op);
220 if (!QUERY_FLAG(op,FLAG_WIZ))
221 return;
222
223 new_draw_info (NDI_UNIQUE, 0,op,"Total time:");
224 sprintf(errmsg,"ticks=%ld time=%ld.%2ld",
225 pticks, process_tot_mtime/1000, process_tot_mtime%1000);
226 new_draw_info (NDI_UNIQUE, 0,op,errmsg);
227 sprintf(errmsg,"avg time=%ldms max time=%ldms min time=%ldms",
228 process_tot_mtime/pticks, process_max_utime/1000,
229 process_min_utime/1000);
230 new_draw_info (NDI_UNIQUE, 0,op,errmsg);
231 sprintf(errmsg,"ticks longer than max time (%ldms) = %ld (%ld%%)",
232 max_time/1000,
233 process_utime_long_count, 100*process_utime_long_count/pticks);
234 new_draw_info (NDI_UNIQUE, 0,op,errmsg);
235
236 sprintf(errmsg,"Time last %ld ticks:", pticks > PBUFLEN ? PBUFLEN : pticks);
237 new_draw_info (NDI_UNIQUE, 0,op,errmsg);
238
239 for (i = 0; i < (pticks > PBUFLEN ? PBUFLEN : pticks); i++) {
240 tot += process_utime_save[i];
241 if (process_utime_save[i] > maxt) maxt = process_utime_save[i];
242 if (process_utime_save[i] < mint) mint = process_utime_save[i];
243 if (process_utime_save[i] > max_time) long_count++;
244 }
245
246 sprintf(errmsg,"avg time=%ldms max time=%dms min time=%dms",
247 tot/(pticks > PBUFLEN ? PBUFLEN : pticks)/1000, maxt/1000,
248 mint/1000);
249 new_draw_info (NDI_UNIQUE, 0,op,errmsg);
250 sprintf(errmsg,"ticks longer than max time (%ldms) = %d (%ld%%)",
251 max_time/1000, long_count,
252 100*long_count/(pticks > PBUFLEN ? PBUFLEN : pticks));
253 new_draw_info (NDI_UNIQUE, 0,op,errmsg);
254}
255
256long
257seconds(void)
258{
259 struct timeval now;
260
261 (void) GETTIMEOFDAY(&now);
262 return now.tv_sec;
263}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines