ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/time.C
Revision: 1.20
Committed: Sun Dec 28 06:59:26 2008 UTC (15 years, 4 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_80, rel-2_76, rel-2_77, rel-2_74, rel-2_75, rel-2_79, rel-2_78
Changes since 1.19: +1 -1 lines
Log Message:
smell, remove gcfclient support, material fixes, cfpod parse fixes, refactoring

File Contents

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