Adonthell  0.4
gamedate.cc
Go to the documentation of this file.
1 /*
2  $Id: gamedate.cc,v 1.8 2002/12/04 17:09:48 ksterker Exp $
3 
4  Copyright (C) 2002 Kai Sterker <kaisterker@linuxgames.com>
5  Part of the Adonthell Project http://adonthell.linuxgames.com
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /**
16  * @file gamedate.cc
17  *
18  * @author Kai Sterker
19  * @brief Implements the gamedate class.
20  */
21 
22 #include <ctype.h>
23 #include <stdlib.h>
24 #include "gamedate.h"
25 #include "gametime.h"
26 #include "time_event.h"
27 #include "event_handler.h"
28 
29 // gametime minutes spent in the gameworld so far
30 u_int32 gamedate::Time = 0;
31 
32 // number of game cycles since the last gametime minute passed
33 double gamedate::Ticks = 0.0;
34 
35 // Increase gametime
37 {
38  static double tenth_minute = gametime::minute () / 10.0;
39 
40  // fts contains the number of cycles that passed since the last
41  // call to gamedate::update
42  Ticks += gametime::frames_to_skip ();
43 
44  // check whether an in-game minute has passed
45  while (Ticks >= tenth_minute)
46  {
47  Ticks -= tenth_minute;
48  Time++;
49 
50  // raise time event
51  time_event evt (Time);
53  }
54 }
55 
56 // load state from disk
58 {
59  // read the current date as (gametime) minutes since start of the game
60  Time << in;
61 
62  return true;
63 }
64 
65 // save state to disk
67 {
68  // write the time to disk
69  Time >> out;
70 }
71 
72 // calculate the current weekday
74 {
75  return day () % DAYS_PER_WEEK;
76 }
77 
78 // calculate the current day
80 {
81  // how many minutes make one day
82  static u_int16 day_in_minutes = 600 * HOURS_PER_DAY;
83 
84  return Time / day_in_minutes;
85 }
86 
87 // calculate the hour of the current day
89 {
90  return (Time / 600) % HOURS_PER_DAY;
91 }
92 
93 // calculate minute of the hour
95 {
96  return (Time / 10) % 60;
97 }
98 
99 // convert the time string to gametime minutes
100 u_int32 gamedate::parse_time (const std::string & time)
101 {
102  u_int32 t_minutes = 0, number = 0;
103  char num[2] = "0";
104 
105  for (u_int32 i = 0; i < time.length (); i++)
106  {
107  // got a number
108  if (isdigit (time[i]))
109  {
110  num[0] = time[i];
111  number = 10 * number + atoi (num);
112  }
113  // got a letter
114  else if (isalpha (time[i]))
115  {
116  switch (time[i])
117  {
118  // weeks
119  case 'w':
120  {
121  t_minutes += number * DAYS_PER_WEEK * HOURS_PER_DAY * 600;
122  break;
123  }
124  // days
125  case 'd':
126  {
127  t_minutes += number * HOURS_PER_DAY * 600;
128  break;
129  }
130  // hours
131  case 'h':
132  {
133  t_minutes += number * 600;
134  break;
135  }
136  // minutes
137  case 'm':
138  {
139  t_minutes += number * 10;
140  break;
141  }
142  // 1/10 minutes
143  case 't':
144  {
145  t_minutes += number;
146  break;
147  }
148  // error
149  default:
150  {
151  fprintf (stderr, "*** gamedate::parse_time: Unknown time specifier '%c'\n", time[i]);
152  break;
153  }
154  }
155 
156  number = 0;
157  }
158  }
159 
160  return t_minutes;
161 }
static bool get_state(igzstream &in)
Load the state of the gamedate class from disk.
Definition: gamedate.cc:57
static void raise_event(const event *ev)
Check if an event corresponding to ev exists, and execute it.
Definition: event_handler.h:64
Class to write data from a Gzip compressed file.
Definition: fileops.h:223
static u_int16 weekday()
Get the current weekday.
Definition: gamedate.cc:73
Declares the event_handler class.
Class to read data from a Gzip compressed file.
Definition: fileops.h:131
#define u_int16
16 bits long unsigned integer
Definition: types.h:32
static double minute()
Return the in-game time that passed since the last call to this method.
Definition: gametime.h:78
static u_int16 hour()
Return the hour of the current day.
Definition: gamedate.cc:88
Declares the gametime class.
#define DAYS_PER_WEEK
The number of days that make one gameworld week.
Definition: gamedate.h:36
#define u_int32
32 bits long unsigned integer
Definition: types.h:35
static u_int16 day()
Returns the current day in the gameworld.
Definition: gamedate.cc:79
static u_int32 time()
Get the current gametime.
Definition: gamedate.h:60
The time event executes the attached script or callback at a certain point in game-time.
Definition: time_event.h:34
static u_int16 minute()
Return the minute of the current hour.
Definition: gamedate.cc:94
static u_int8 frames_to_skip()
Returns the number of updates to perform before drawing the next frame.
Definition: gametime.h:113
Declares the gamedate class.
#define HOURS_PER_DAY
The number of hours that make one gameworld day.
Definition: gamedate.h:31
static u_int32 parse_time(const std::string &time)
convert the time string to gametime minutes.
Definition: gamedate.cc:100
Declares the time_event class.
static void put_state(ogzstream &out)
Save the state of the gamedate class to disk.
Definition: gamedate.cc:66
static void update()
Update the game date.
Definition: gamedate.cc:36