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.5 by root, Thu Sep 14 22:34:00 2006 UTC vs.
Revision 1.13 by root, Wed Apr 25 18:47:49 2007 UTC

1/* 1/*
2 CrossFire, A Multiplayer game for X-windows 2 * CrossFire, A Multiplayer game for X-windows
3 3 *
4 * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team
4 Copyright (C) 2002 Mark Wedel & Crossfire Development Team 5 * Copyright (C) 2002 Mark Wedel & Crossfire Development Team
5 Copyright (C) 1992 Frank Tore Johansen 6 * Copyright (C) 1992 Frank Tore Johansen
6 7 *
7 This program is free software; you can redistribute it and/or modify 8 * This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by 9 * it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or 10 * the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version. 11 * (at your option) any later version.
11 12 *
12 This program is distributed in the hope that it will be useful, 13 * This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details. 16 * GNU General Public License for more details.
16 17 *
17 You should have received a copy of the GNU General Public License 18 * You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software 19 * along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 21 *
21 The authors can be reached via e-mail at <crossfire@schmorp.de> 22 * The authors can be reached via e-mail at <crossfire@schmorp.de>
22*/ 23 */
23 24
24#include <global.h> 25#include <global.h>
25#include <funcpoint.h> 26#include <funcpoint.h>
26#include <tod.h> 27#include <tod.h>
27 28
28#ifndef WIN32 /* ---win32 exclude header */
29# include <stdio.h> 29#include <cstdio>
30
30# include <sys/types.h> 31#include <sys/types.h>
31# include <sys/time.h> 32#include <sys/time.h>
32#endif /* win32 */
33 33
34/* 34/*
35 * Gloabal variables: 35 * Gloabal variables:
36 */ 36 */
37tick_t pticks;
37 38
38long psaveind;
39long pticks;
40
41const char *season_name[] = { 39static const char *season_name[5] = {
42 "The Season of New Year", 40 "New Year",
43 "The Season of Growth", 41 "Growth",
44 "The Season of Harvest", 42 "Harvest",
45 "The Season of Decay", 43 "Decay",
46 "The Season of the Blizzard", 44 "the Blizzard",
47 "\n"
48}; 45};
49 46
50const char *weekdays[DAYS_PER_WEEK] = { 47static const char *weekdays[DAYS_PER_WEEK] = {
51 "the Day of the Moon", 48 "the Moon",
52 "the Day of the Bull", 49 "the Bull",
53 "the Day of the Deception", 50 "the Deception",
54 "the Day of Thunder", 51 "Thunder",
55 "the Day of Freedom", 52 "Freedom",
56 "the Day of the Great Gods", 53 "the Great Gods",
57 "the Day of the Sun" 54 "the Sun"
58}; 55};
59 56
60const char *month_name[MONTHS_PER_YEAR] = { 57static const char *month_name[MONTHS_PER_YEAR] = {
61 "Month of Winter", /* 0 */ 58 "Winter", /* 0 */
62 "Month of the Ice Dragon", 59 "the Ice Dragon",
63 "Month of the Frost Giant", 60 "the Frost Giant",
64 "Month of Valriel", 61 "Valriel",
65 "Month of Lythander", 62 "Lythander",
66 "Month of the Harvest", 63 "the Harvest",
67 "Month of Gaea", 64 "Gaea",
68 "Month of Futility", 65 "Futility",
69 "Month of the Dragon", 66 "the Dragon",
70 "Month of the Sun", 67 "the Sun",
71 "Month of the Great Infernus", 68 "the Great Infernus",
72 "Month of Ruggilli", 69 "Ruggilli",
73 "Month of the Dark Shades", 70 "the Dark Shades",
74 "Month of the Devourers", 71 "the Devourers",
75 "Month of Sorig", 72 "Sorig",
76 "Month of the Ancient Darkness", 73 "the Ancient Darkness",
77 "Month of Gorokh" 74 "Gorokh"
78}; 75};
79
80extern unsigned long todtick;
81 76
82void 77void
83get_tod (timeofday_t * tod) 78get_tod (timeofday_t * tod)
84{ 79{
80 unsigned int todtick = (unsigned int)(runtime * (1. / RUNTIME_PER_HOUR));
81
85 tod->year = todtick / HOURS_PER_YEAR; 82 tod->year = todtick / HOURS_PER_YEAR;
86 tod->month = (todtick / HOURS_PER_MONTH) % MONTHS_PER_YEAR; 83 tod->month = todtick / HOURS_PER_MONTH % MONTHS_PER_YEAR;
87 tod->day = (todtick % HOURS_PER_MONTH) / DAYS_PER_MONTH; 84 tod->day = todtick % HOURS_PER_MONTH / DAYS_PER_MONTH;
85 tod->hour = todtick % HOURS_PER_DAY;
86 tod->minute = (int)((runtime - todtick * RUNTIME_PER_HOUR) * (60. / RUNTIME_PER_HOUR));
87
88 tod->dayofweek = tod->day % DAYS_PER_WEEK; 88 tod->dayofweek = tod->day % DAYS_PER_WEEK;
89 tod->hour = todtick % HOURS_PER_DAY;
90 tod->minute = (pticks % PTICKS_PER_CLOCK) / (PTICKS_PER_CLOCK / 58);
91 if (tod->minute > 58)
92 tod->minute = 58; /* it's imprecise at best anyhow */
93 tod->weekofmonth = tod->day / WEEKS_PER_MONTH; 89 tod->weekofmonth = tod->day / WEEKS_PER_MONTH;
94 if (tod->month < 3) 90 tod->season = min (4, tod->month / 3);
95 tod->season = 0;
96 else if (tod->month < 6)
97 tod->season = 1;
98 else if (tod->month < 9)
99 tod->season = 2;
100 else if (tod->month < 12)
101 tod->season = 3;
102 else
103 tod->season = 4;
104} 91}
105 92
106void 93void
107print_tod (object *op) 94print_tod (object *op)
108{ 95{
109 timeofday_t tod; 96 timeofday_t tod;
97 get_tod (&tod);
98
99 char todmsg [1024];
100
101 int day = tod.day + 1;
110 const char *suf; 102 const char *suf;
111 int day;
112
113 get_tod (&tod);
114 sprintf (errmsg, "It is %d minute%s past %d o'clock %s, on %s",
115 tod.minute + 1, ((tod.minute + 1 < 2) ? "" : "s"),
116 ((tod.hour % 14 == 0) ? 14 : ((tod.hour) % 14)), ((tod.hour >= 14) ? "pm" : "am"), weekdays[tod.dayofweek]);
117 new_draw_info (NDI_UNIQUE, 0, op, errmsg);
118
119 day = tod.day + 1;
120 if (day == 1 || ((day % 10) == 1 && day > 20)) 103 if (day == 1 || ((day % 10) == 1 && day > 20))
121 suf = "st"; 104 suf = "st";
122 else if (day == 2 || ((day % 10) == 2 && day > 20)) 105 else if (day == 2 || ((day % 10) == 2 && day > 20))
123 suf = "nd"; 106 suf = "nd";
124 else if (day == 3 || ((day % 10) == 3 && day > 20)) 107 else if (day == 3 || ((day % 10) == 3 && day > 20))
125 suf = "rd"; 108 suf = "rd";
126 else 109 else
127 suf = "th"; 110 suf = "th";
128 sprintf (errmsg, "The %d%s Day of the %s, Year %d", day, suf, month_name[tod.month], tod.year + 1);
129 new_draw_info (NDI_UNIQUE, 0, op, errmsg);
130 111
131 sprintf (errmsg, "Time of Year: %s", season_name[tod.season]); 112 snprintf (todmsg, sizeof (todmsg),
113 "It is %d minute%s past %d o'clock %s, on the Day of %s,\n"
114 "the %d%s Day of the Month of %s, Year %d.\n"
115 "It is the Season of %s.",
116
117 tod.minute + 1,
118 (tod.minute + 1 < 2) ? "" : "s",
119 (tod.hour + 13) % 14 + 1,
120 (tod.hour >= 14 ? "pm" : "am"),
121 weekdays[tod.dayofweek],
122
123 day, suf, month_name[tod.month], tod.year + 1,
124
125 season_name[tod.season]);
126
132 new_draw_info (NDI_UNIQUE, 0, op, errmsg); 127 new_draw_info (NDI_UNIQUE, 0, op, todmsg);
133} 128}
134 129
135long
136seconds (void)
137{
138 struct timeval now;
139
140 (void) GETTIMEOFDAY (&now);
141 return now.tv_sec;
142}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines