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