1 | /* |
|
|
2 | * static char *rcsid_time_c = |
|
|
3 | * "$Id: time.C,v 1.2 2006/08/15 18:07:24 root Exp $"; |
|
|
4 | */ |
|
|
5 | |
|
|
6 | /* |
1 | /* |
7 | CrossFire, A Multiplayer game for X-windows |
2 | CrossFire, A Multiplayer game for X-windows |
8 | |
3 | |
9 | Copyright (C) 2002 Mark Wedel & Crossfire Development Team |
4 | Copyright (C) 2002 Mark Wedel & Crossfire Development Team |
10 | Copyright (C) 1992 Frank Tore Johansen |
5 | Copyright (C) 1992 Frank Tore Johansen |
… | |
… | |
21 | |
16 | |
22 | You should have received a copy of the GNU General Public License |
17 | You should have received a copy of the GNU General Public License |
23 | along with this program; if not, write to the Free Software |
18 | along with this program; if not, write to the Free Software |
24 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
25 | |
20 | |
26 | The authors can be reached via e-mail at crossfire-devel@real-time.com |
21 | The authors can be reached via e-mail at <crossfire@schmorp.de> |
27 | */ |
22 | */ |
28 | |
23 | |
29 | #include <global.h> |
24 | #include <global.h> |
30 | #include <funcpoint.h> |
25 | #include <funcpoint.h> |
31 | #include <tod.h> |
26 | #include <tod.h> |
32 | |
27 | |
33 | #ifndef WIN32 /* ---win32 exclude header */ |
|
|
34 | #include <stdio.h> |
28 | #include <stdio.h> |
35 | #include <sys/types.h> |
29 | #include <sys/types.h> |
36 | #include <sys/time.h> |
30 | #include <sys/time.h> |
37 | #endif /* win32 */ |
|
|
38 | |
31 | |
39 | /* |
32 | /* |
40 | * Gloabal variables: |
33 | * Gloabal variables: |
41 | */ |
34 | */ |
42 | long max_time = MAX_TIME; |
|
|
43 | struct timeval last_time; |
|
|
44 | |
35 | |
45 | #define PBUFLEN 100 |
|
|
46 | long process_utime_save[PBUFLEN]; |
|
|
47 | long psaveind; |
36 | long psaveind; |
48 | long process_max_utime = 0; |
|
|
49 | long process_min_utime = 999999999; |
|
|
50 | long process_tot_mtime; |
|
|
51 | long pticks; |
37 | long pticks; |
52 | long process_utime_long_count; |
|
|
53 | |
38 | |
54 | const char *season_name[] = |
39 | const char *season_name[] = { |
55 | { |
|
|
56 | "The Season of New Year", |
40 | "The Season of New Year", |
57 | "The Season of Growth", |
41 | "The Season of Growth", |
58 | "The Season of Harvest", |
42 | "The Season of Harvest", |
59 | "The Season of Decay", |
43 | "The Season of Decay", |
60 | "The Season of the Blizzard", |
44 | "The Season of the Blizzard", |
61 | "\n" |
45 | "\n" |
62 | }; |
46 | }; |
63 | |
47 | |
64 | const char *weekdays[DAYS_PER_WEEK] = { |
48 | const char *weekdays[DAYS_PER_WEEK] = { |
65 | "the Day of the Moon", |
49 | "the Day of the Moon", |
66 | "the Day of the Bull", |
50 | "the Day of the Bull", |
67 | "the Day of the Deception", |
51 | "the Day of the Deception", |
68 | "the Day of Thunder", |
52 | "the Day of Thunder", |
69 | "the Day of Freedom", |
53 | "the Day of Freedom", |
70 | "the Day of the Great Gods", |
54 | "the Day of the Great Gods", |
71 | "the Day of the Sun" |
55 | "the Day of the Sun" |
72 | }; |
56 | }; |
73 | |
57 | |
74 | const char *month_name[MONTHS_PER_YEAR] = { |
58 | const char *month_name[MONTHS_PER_YEAR] = { |
75 | "Month of Winter", /* 0 */ |
59 | "Month of Winter", /* 0 */ |
76 | "Month of the Ice Dragon", |
60 | "Month of the Ice Dragon", |
77 | "Month of the Frost Giant", |
61 | "Month of the Frost Giant", |
78 | "Month of Valriel", |
62 | "Month of Valriel", |
79 | "Month of Lythander", |
63 | "Month of Lythander", |
80 | "Month of the Harvest", |
64 | "Month of the Harvest", |
81 | "Month of Gaea", |
65 | "Month of Gaea", |
82 | "Month of Futility", |
66 | "Month of Futility", |
83 | "Month of the Dragon", |
67 | "Month of the Dragon", |
84 | "Month of the Sun", |
68 | "Month of the Sun", |
85 | "Month of the Great Infernus", |
69 | "Month of the Great Infernus", |
86 | "Month of Ruggilli", |
70 | "Month of Ruggilli", |
87 | "Month of the Dark Shades", |
71 | "Month of the Dark Shades", |
88 | "Month of the Devourers", |
72 | "Month of the Devourers", |
89 | "Month of Sorig", |
73 | "Month of Sorig", |
90 | "Month of the Ancient Darkness", |
74 | "Month of the Ancient Darkness", |
91 | "Month of Gorokh" |
75 | "Month of Gorokh" |
92 | }; |
76 | }; |
93 | |
|
|
94 | /* |
|
|
95 | * Initialise all variables used in the timing routines. |
|
|
96 | */ |
|
|
97 | |
|
|
98 | void |
|
|
99 | reset_sleep(void) |
|
|
100 | { |
|
|
101 | int i; |
|
|
102 | for(i = 0; i < PBUFLEN; i++) |
|
|
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 | |
|
|
110 | (void) GETTIMEOFDAY(&last_time); |
|
|
111 | } |
|
|
112 | |
|
|
113 | void |
|
|
114 | log_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 | |
|
|
132 | int |
|
|
133 | enough_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 | |
|
|
151 | void |
|
|
152 | set_max_time(long t) { |
|
|
153 | max_time = t; |
|
|
154 | } |
|
|
155 | |
77 | |
156 | extern unsigned long todtick; |
78 | extern unsigned long todtick; |
157 | |
79 | |
158 | void |
80 | void |
159 | get_tod(timeofday_t *tod) |
81 | get_tod (timeofday_t * tod) |
160 | { |
82 | { |
161 | tod->year = todtick/HOURS_PER_YEAR; |
83 | tod->year = todtick / HOURS_PER_YEAR; |
162 | tod->month = (todtick/HOURS_PER_MONTH)%MONTHS_PER_YEAR; |
84 | tod->month = (todtick / HOURS_PER_MONTH) % MONTHS_PER_YEAR; |
163 | tod->day = (todtick%HOURS_PER_MONTH)/DAYS_PER_MONTH; |
85 | tod->day = (todtick % HOURS_PER_MONTH) / DAYS_PER_MONTH; |
164 | tod->dayofweek = tod->day%DAYS_PER_WEEK; |
86 | tod->dayofweek = tod->day % DAYS_PER_WEEK; |
165 | tod->hour = todtick%HOURS_PER_DAY; |
87 | tod->hour = todtick % HOURS_PER_DAY; |
166 | tod->minute = (pticks%PTICKS_PER_CLOCK)/(PTICKS_PER_CLOCK/58); |
88 | tod->minute = (pticks % PTICKS_PER_CLOCK) / (PTICKS_PER_CLOCK / 58); |
167 | if (tod->minute > 58) |
89 | if (tod->minute > 58) |
168 | tod->minute = 58; /* it's imprecise at best anyhow */ |
90 | tod->minute = 58; /* it's imprecise at best anyhow */ |
169 | tod->weekofmonth = tod->day/WEEKS_PER_MONTH; |
91 | tod->weekofmonth = tod->day / WEEKS_PER_MONTH; |
170 | if (tod->month < 3) |
92 | if (tod->month < 3) |
171 | tod->season = 0; |
93 | tod->season = 0; |
172 | else if (tod->month < 6) |
94 | else if (tod->month < 6) |
173 | tod->season = 1; |
95 | tod->season = 1; |
174 | else if (tod->month < 9) |
96 | else if (tod->month < 9) |
… | |
… | |
178 | else |
100 | else |
179 | tod->season = 4; |
101 | tod->season = 4; |
180 | } |
102 | } |
181 | |
103 | |
182 | void |
104 | void |
183 | print_tod(object *op) |
105 | print_tod (object *op) |
184 | { |
106 | { |
185 | timeofday_t tod; |
107 | timeofday_t tod; |
186 | const char *suf; |
108 | const char *suf; |
187 | int day; |
109 | int day; |
188 | |
110 | |
189 | get_tod(&tod); |
111 | get_tod (&tod); |
190 | sprintf(errmsg, "It is %d minute%s past %d o'clock %s, on %s", |
112 | sprintf (errmsg, "It is %d minute%s past %d o'clock %s, on %s", |
191 | tod.minute+1, ((tod.minute+1 < 2) ? "" : "s"), |
113 | tod.minute + 1, ((tod.minute + 1 < 2) ? "" : "s"), |
192 | ((tod.hour % 14 == 0) ? 14 : ((tod.hour)%14)), |
114 | ((tod.hour % 14 == 0) ? 14 : ((tod.hour) % 14)), ((tod.hour >= 14) ? "pm" : "am"), weekdays[tod.dayofweek]); |
193 | ((tod.hour >= 14) ? "pm" : "am"), |
|
|
194 | weekdays[tod.dayofweek]); |
|
|
195 | new_draw_info(NDI_UNIQUE, 0,op,errmsg); |
115 | new_draw_info (NDI_UNIQUE, 0, op, errmsg); |
196 | |
116 | |
197 | day = tod.day + 1; |
117 | day = tod.day + 1; |
198 | if (day == 1 || ((day % 10) == 1 && day > 20)) |
118 | if (day == 1 || ((day % 10) == 1 && day > 20)) |
199 | suf = "st"; |
119 | suf = "st"; |
200 | else if (day == 2 || ((day % 10) == 2 && day > 20)) |
120 | else if (day == 2 || ((day % 10) == 2 && day > 20)) |
201 | suf = "nd"; |
121 | suf = "nd"; |
202 | else if (day == 3 || ((day % 10) == 3 && day > 20)) |
122 | else if (day == 3 || ((day % 10) == 3 && day > 20)) |
203 | suf = "rd"; |
123 | suf = "rd"; |
204 | else |
124 | else |
205 | suf = "th"; |
125 | suf = "th"; |
206 | sprintf(errmsg, "The %d%s Day of the %s, Year %d", day, suf, |
126 | sprintf (errmsg, "The %d%s Day of the %s, Year %d", day, suf, month_name[tod.month], tod.year + 1); |
207 | month_name[tod.month], tod.year+1); |
|
|
208 | new_draw_info(NDI_UNIQUE, 0,op,errmsg); |
127 | new_draw_info (NDI_UNIQUE, 0, op, errmsg); |
209 | |
128 | |
210 | sprintf(errmsg, "Time of Year: %s", season_name[tod.season]); |
129 | sprintf (errmsg, "Time of Year: %s", season_name[tod.season]); |
211 | new_draw_info(NDI_UNIQUE, 0,op,errmsg); |
|
|
212 | } |
|
|
213 | |
|
|
214 | void |
|
|
215 | time_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); |
130 | 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 | } |
131 | } |
255 | |
132 | |
256 | long |
133 | long |
257 | seconds(void) |
134 | seconds (void) |
258 | { |
135 | { |
259 | struct timeval now; |
136 | struct timeval now; |
260 | |
137 | |
261 | (void) GETTIMEOFDAY(&now); |
138 | (void) GETTIMEOFDAY (&now); |
262 | return now.tv_sec; |
139 | return now.tv_sec; |
263 | } |
140 | } |