2 // nazghul - an old-school RPG engine
3 // Copyright (C) 2002, 2003 Gordon McNutt
5 // Thi program is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License as published by the Free
7 // Software Foundation; either version 2 of the License, or (at your option)
10 // This program is distributed in the hope that it will be useful, but WITHOUT
11 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 // You should have received a copy of the GNU General Public License along with
16 // this program; if not, write to the Free Foundation, Inc., 59 Temple Place,
17 // Suite 330, Boston, MA 02111-1307 USA
20 // gmcnutt@users.sourceforge.net
36 #define Clock (Session->clock)
63 int clock_minute(void)
73 unsigned int clock_time_of_day(void)
75 return (Clock.hour * 60 + Clock.min);
78 unsigned int clock_time(void)
80 return Clock.total_minutes;
83 void clock_advance(int ticks)
85 assert(Clock.tick < Clock.tick_to_change_time);
91 if (Clock.tick == Clock.tick_to_change_time) {
93 Clock.total_minutes++;
95 Clock.tick_to_change_time = CLOCK_TICKS_PER_MINUTE;
98 if (Clock.min == MINUTES_PER_HOUR) {
101 if (Clock.hour == HOURS_PER_DAY) {
104 if (Clock.day == DAYS_PER_WEEK) {
107 if (Clock.week == WEEKS_PER_MONTH) {
110 if (Clock.month == MONTHS_PER_YEAR) {
124 char *time_HHMM_as_string(void)
126 static char str[] = "HH:MMPM";
127 static int maxlen = strlen("HH:MMPM") + 1;
132 hr = (hr > 12) ? (hr - 12) : hr;
133 hr = (hr == 0) ? 12 : hr;
135 n = snprintf(str, maxlen, "%2d:%02d%2s",
136 hr, min, (Clock.hour >= 12) ? "PM" : "AM");
139 } // time_HHMM_as_string()
141 char *vague_time_as_string(void)
143 static char str[] = "late afternoon";
144 static int maxlen = strlen("late afternoon") + 1;
150 n = snprintf(str, maxlen, "night");
154 n = snprintf(str, maxlen, "early morning");
158 n = snprintf(str, maxlen, "morning");
162 n = snprintf(str, maxlen, "noon");
166 n = snprintf(str, maxlen, "afternoon");
170 n = snprintf(str, maxlen, "late afternoon");
174 n = snprintf(str, maxlen, "evening");
178 n = snprintf(str, maxlen, "night");
182 } // vague_time_as_string()
184 char *time_YYYY_MM_DD_as_string(void)
186 static char str[] = "YYYY/MM/DD";
187 static int maxlen = strlen("YYYY/MM/DD") + 1;
188 int n = snprintf(str, maxlen, "%04d/%02d/%02d",
189 Clock.year, Clock.month, Clock.day);
192 } // time_YYYY_MM_DD_as_string()
194 #ifdef OTHER_TIME_STRING_FUNCTIONS
195 char *time_YYYY_as_string(void)
197 static char str[] = "YYYY";
198 static int maxlen = strlen("YYYY") + 1;
199 int n = snprintf(str, maxlen, "%4d", Clock.year);
204 char *time_MM_as_string(void)
206 static char str[] = "MM";
207 static int maxlen = strlen("MM") + 1;
208 int n = snprintf(str, maxlen, "%2d", Clock.month);
213 char *time_DD_as_string(void)
215 static char str[] = "DD";
216 static int maxlen = strlen("DD") + 1;
217 int n = snprintf(str, maxlen, "%2d", Clock.day);
221 #endif // OTHER_TIME_STRING_FUNCTIONS
224 // A proper implementation of
225 // month_name(), week_name(), day_name()
226 // will wait until we have GhulScript
227 // for week and month names and such.
229 const char *month_name(void)
231 int month = Clock.month;
262 const char *week_name(void)
264 int week = Clock.week;
279 const char *day_name(void)
302 void clock_alarm_set(clock_alarm_t *alarm, unsigned int minutes)
304 *alarm = Clock.total_minutes + minutes;
307 int clock_alarm_is_expired(clock_alarm_t *alarm)
309 return (Clock.total_minutes >= *alarm);
312 int clock_alarm_remaining(clock_alarm_t *alarm)
314 if (Clock.total_minutes >= *alarm) return 0;
315 return (*alarm - Clock.total_minutes);
320 return (Clock.hour == 12 && Clock.min == 0);
323 int is_midnight(void)
325 return (Clock.hour == 0 && Clock.min == 0);
328 #ifdef INCLUDE_UNUSED_CLOCK_ROUTINES
329 void clock_reset(struct clock *clock)
331 memset(clock, 0, sizeof(*clock));
334 void clock_set_alarm(struct clock *clock, struct clock *offset)
337 /* Copy the current time */
338 memcpy(clock, &Clock, sizeof(*clock));
340 /* Set the alarm to the current time plus the offset */
341 clock->min += offset->min;
342 if (clock->min >= MINUTES_PER_HOUR) {
343 offset->hour += clock->min / MINUTES_PER_HOUR;
344 clock->min %= MINUTES_PER_HOUR;
347 clock->hour += offset->hour;
348 if (clock->hour >= HOURS_PER_DAY) {
349 offset->day += clock->hour / HOURS_PER_DAY;
350 clock->hour %= HOURS_PER_DAY;
353 clock->day += offset->day;
354 if (clock->day >= DAYS_PER_WEEK) {
355 offset->week += clock->day / DAYS_PER_WEEK;
356 clock->day %= DAYS_PER_WEEK;
359 clock->week += offset->week;
360 if (clock->week >= WEEKS_PER_MONTH) {
361 offset->month += clock->week / WEEKS_PER_MONTH;
362 clock->week %= WEEKS_PER_MONTH;
365 clock->month += offset->month;
366 if (clock->month >= MONTHS_PER_YEAR) {
367 offset->year += clock->month / MONTHS_PER_YEAR;
368 clock->month %= MONTHS_PER_YEAR;
371 clock->year += offset->year;
373 consolePrint("Set alarm for year %d, month %d, week %d, day %d, "
374 "hour %d, min %d\n", clock->year, clock->month,
375 clock->week, clock->day, clock->hour, clock->min);
378 int clock_alarm_expired(struct clock *clock)
382 total_minute = (clock->year - Clock.year) * MINUTES_PER_YEAR;
383 total_minute += (clock->month - Clock.month) * MINUTES_PER_MONTH;
384 total_minute += (clock->week - Clock.week) * MINUTES_PER_WEEK;
385 total_minute += (clock->day - Clock.day) * MINUTES_PER_DAY;
386 total_minute += (clock->hour - Clock.hour) * MINUTES_PER_HOUR;
387 total_minute += (clock->min - Clock.min);
389 return (total_minute <= 0);
391 #endif // INCLUDE_UNUSED_CLOCK_ROUTINES