1 | /* |
1 | /* |
2 | * static char *rcsid_time_c = |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 | * "$Id: time.C,v 1.3 2006/08/24 13:13:48 root Exp $"; |
3 | * |
|
|
4 | * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
|
|
5 | * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team |
|
|
6 | * Copyright (©) 1992,2007 Frank Tore Johansen |
|
|
7 | * |
|
|
8 | * Deliantra is free software: you can redistribute it and/or modify it under |
|
|
9 | * the terms of the Affero GNU General Public License as published by the |
|
|
10 | * Free Software Foundation, either version 3 of the License, or (at your |
|
|
11 | * 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 Affero GNU General Public License |
|
|
19 | * and the GNU General Public License along with this program. If not, see |
|
|
20 | * <http://www.gnu.org/licenses/>. |
|
|
21 | * |
|
|
22 | * The authors can be reached via e-mail to <support@deliantra.net> |
4 | */ |
23 | */ |
5 | |
24 | |
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 | |
|
|
29 | #include <global.h> |
25 | #include <global.h> |
30 | #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 <cstdio> |
|
|
29 | |
35 | #include <sys/types.h> |
30 | #include <sys/types.h> |
36 | #include <sys/time.h> |
31 | #include <sys/time.h> |
37 | #endif /* win32 */ |
|
|
38 | |
32 | |
39 | /* |
33 | /* |
40 | * Gloabal variables: |
34 | * Gloabal variables: |
41 | */ |
35 | */ |
|
|
36 | tick_t pticks; |
42 | |
37 | |
43 | long psaveind; |
|
|
44 | long pticks; |
|
|
45 | |
|
|
46 | const char *season_name[] = |
38 | static const char *season_name[5] = { |
47 | { |
39 | "New Year", |
48 | "The Season of New Year", |
40 | "Growth", |
49 | "The Season of Growth", |
41 | "Harvest", |
50 | "The Season of Harvest", |
42 | "Decay", |
51 | "The Season of Decay", |
43 | "the Blizzard", |
52 | "The Season of the Blizzard", |
|
|
53 | "\n" |
|
|
54 | }; |
44 | }; |
55 | |
45 | |
56 | const char *weekdays[DAYS_PER_WEEK] = { |
46 | static const char *weekdays[DAYS_PER_WEEK] = { |
57 | "the Day of the Moon", |
47 | "the Moon", |
58 | "the Day of the Bull", |
48 | "the Bull", |
59 | "the Day of the Deception", |
49 | "the Deception", |
60 | "the Day of Thunder", |
50 | "Thunder", |
61 | "the Day of Freedom", |
51 | "Freedom", |
62 | "the Day of the Great Gods", |
52 | "the Great Gods", |
63 | "the Day of the Sun" |
53 | "the Sun" |
64 | }; |
54 | }; |
65 | |
55 | |
66 | const char *month_name[MONTHS_PER_YEAR] = { |
56 | static const char *month_name[MONTHS_PER_YEAR] = { |
67 | "Month of Winter", /* 0 */ |
57 | "Winter", /* 0 */ |
68 | "Month of the Ice Dragon", |
58 | "the Ice Dragon", |
69 | "Month of the Frost Giant", |
59 | "the Frost Giant", |
70 | "Month of Valriel", |
60 | "Valriel", |
71 | "Month of Lythander", |
61 | "Lythander", |
72 | "Month of the Harvest", |
62 | "the Harvest", |
73 | "Month of Gaea", |
63 | "Gaea", |
74 | "Month of Futility", |
64 | "Futility", |
75 | "Month of the Dragon", |
65 | "the Dragon", |
76 | "Month of the Sun", |
66 | "the Sun", |
77 | "Month of the Great Infernus", |
67 | "the Great Infernus", |
78 | "Month of Ruggilli", |
68 | "Ruggilli", |
79 | "Month of the Dark Shades", |
69 | "the Dark Shades", |
80 | "Month of the Devourers", |
70 | "the Devourers", |
81 | "Month of Sorig", |
71 | "Sorig", |
82 | "Month of the Ancient Darkness", |
72 | "the Ancient Darkness", |
83 | "Month of Gorokh" |
73 | "Gorokh" |
84 | }; |
74 | }; |
85 | |
75 | |
86 | extern unsigned long todtick; |
76 | void |
|
|
77 | get_tod (timeofday_t *tod) |
|
|
78 | { |
|
|
79 | unsigned int todtick = (unsigned int)(runtime * (1. / RUNTIME_PER_HOUR)); |
87 | |
80 | |
88 | void |
|
|
89 | get_tod(timeofday_t *tod) |
|
|
90 | { |
|
|
91 | tod->year = todtick/HOURS_PER_YEAR; |
81 | tod->year = todtick / HOURS_PER_YEAR + EPOCH; |
92 | tod->month = (todtick/HOURS_PER_MONTH)%MONTHS_PER_YEAR; |
82 | tod->month = todtick / HOURS_PER_MONTH % MONTHS_PER_YEAR; |
93 | tod->day = (todtick%HOURS_PER_MONTH)/DAYS_PER_MONTH; |
83 | tod->day = todtick % HOURS_PER_MONTH / DAYS_PER_MONTH; |
|
|
84 | tod->hour = todtick % HOURS_PER_DAY; |
|
|
85 | tod->minute = (int)((runtime - todtick * RUNTIME_PER_HOUR) * (60. / RUNTIME_PER_HOUR)); |
|
|
86 | |
94 | tod->dayofweek = tod->day%DAYS_PER_WEEK; |
87 | tod->dayofweek = tod->day % DAYS_PER_WEEK; |
95 | tod->hour = todtick%HOURS_PER_DAY; |
|
|
96 | tod->minute = (pticks%PTICKS_PER_CLOCK)/(PTICKS_PER_CLOCK/58); |
|
|
97 | if (tod->minute > 58) |
|
|
98 | tod->minute = 58; /* it's imprecise at best anyhow */ |
|
|
99 | tod->weekofmonth = tod->day/WEEKS_PER_MONTH; |
88 | tod->weekofmonth = tod->day / WEEKS_PER_MONTH; |
100 | if (tod->month < 3) |
89 | tod->season = min (4, tod->month / 4); |
101 | tod->season = 0; |
|
|
102 | else if (tod->month < 6) |
|
|
103 | tod->season = 1; |
|
|
104 | else if (tod->month < 9) |
|
|
105 | tod->season = 2; |
|
|
106 | else if (tod->month < 12) |
|
|
107 | tod->season = 3; |
|
|
108 | else |
|
|
109 | tod->season = 4; |
|
|
110 | } |
90 | } |
111 | |
91 | |
112 | void |
92 | char * |
113 | print_tod(object *op) |
93 | format_tod (char *buf, int len, timeofday_t *tod) |
114 | { |
94 | { |
115 | timeofday_t tod; |
95 | int day = tod->day + 1; |
|
|
96 | |
116 | const char *suf; |
97 | const char *suf; |
117 | int day; |
|
|
118 | |
98 | |
119 | get_tod(&tod); |
|
|
120 | sprintf(errmsg, "It is %d minute%s past %d o'clock %s, on %s", |
|
|
121 | tod.minute+1, ((tod.minute+1 < 2) ? "" : "s"), |
|
|
122 | ((tod.hour % 14 == 0) ? 14 : ((tod.hour)%14)), |
|
|
123 | ((tod.hour >= 14) ? "pm" : "am"), |
|
|
124 | weekdays[tod.dayofweek]); |
|
|
125 | new_draw_info(NDI_UNIQUE, 0,op,errmsg); |
|
|
126 | |
|
|
127 | day = tod.day + 1; |
|
|
128 | if (day == 1 || ((day % 10) == 1 && day > 20)) |
99 | if (day == 1 || ((day % 10) == 1 && day > 20)) |
129 | suf = "st"; |
100 | suf = "st"; |
130 | else if (day == 2 || ((day % 10) == 2 && day > 20)) |
101 | else if (day == 2 || ((day % 10) == 2 && day > 20)) |
131 | suf = "nd"; |
102 | suf = "nd"; |
132 | else if (day == 3 || ((day % 10) == 3 && day > 20)) |
103 | else if (day == 3 || ((day % 10) == 3 && day > 20)) |
133 | suf = "rd"; |
104 | suf = "rd"; |
134 | else |
105 | else |
135 | suf = "th"; |
106 | suf = "th"; |
136 | sprintf(errmsg, "The %d%s Day of the %s, Year %d", day, suf, |
|
|
137 | month_name[tod.month], tod.year+1); |
|
|
138 | new_draw_info(NDI_UNIQUE, 0,op,errmsg); |
|
|
139 | |
107 | |
140 | sprintf(errmsg, "Time of Year: %s", season_name[tod.season]); |
108 | snprintf (buf, len, |
141 | new_draw_info(NDI_UNIQUE, 0,op,errmsg); |
109 | "It is %d minute%s past %d o'clock %s, on the Day of %s,\n" |
|
|
110 | "the %d%s Day of the Month of %s, Year %d.\n" |
|
|
111 | "It is the Season of %s.", |
|
|
112 | |
|
|
113 | tod->minute ? tod->minute : 60, |
|
|
114 | tod->minute ? "s" : "", |
|
|
115 | (tod->hour + 13) % 14 + 1, |
|
|
116 | (tod->hour >= 14 ? "pm" : "am"), |
|
|
117 | weekdays[tod->dayofweek], |
|
|
118 | |
|
|
119 | day, suf, month_name[tod->month], tod->year + 1, |
|
|
120 | |
|
|
121 | season_name[tod->season]); |
|
|
122 | |
|
|
123 | return buf; |
142 | } |
124 | } |
143 | |
125 | |
144 | long |
126 | const char * |
145 | seconds(void) |
127 | get_current_tod_str () |
146 | { |
128 | { |
147 | struct timeval now; |
129 | timeofday_t tod; |
|
|
130 | get_tod (&tod); |
148 | |
131 | |
149 | (void) GETTIMEOFDAY(&now); |
132 | static char todbuf[512]; |
150 | return now.tv_sec; |
133 | format_tod (todbuf, sizeof (todbuf), &tod); |
|
|
134 | |
|
|
135 | return todbuf; |
151 | } |
136 | } |
|
|
137 | |
|
|
138 | void |
|
|
139 | print_tod (object *op) |
|
|
140 | { |
|
|
141 | new_draw_info (NDI_UNIQUE, 0, op, get_current_tod_str ()); |
|
|
142 | } |
|
|
143 | |