Adonthell
0.4
|
00001 /* 00002 $Id: gamedate.cc,v 1.8 2002/12/04 17:09:48 ksterker Exp $ 00003 00004 Copyright (C) 2002 Kai Sterker <kaisterker@linuxgames.com> 00005 Part of the Adonthell Project http://adonthell.linuxgames.com 00006 00007 This program is free software; you can redistribute it and/or modify 00008 it under the terms of the GNU General Public License. 00009 This program is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY. 00011 00012 See the COPYING file for more details. 00013 */ 00014 00015 /** 00016 * @file gamedate.cc 00017 * 00018 * @author Kai Sterker 00019 * @brief Implements the gamedate class. 00020 */ 00021 00022 #include <ctype.h> 00023 #include <stdlib.h> 00024 #include "gamedate.h" 00025 #include "gametime.h" 00026 #include "time_event.h" 00027 #include "event_handler.h" 00028 00029 // gametime minutes spent in the gameworld so far 00030 u_int32 gamedate::Time = 0; 00031 00032 // number of game cycles since the last gametime minute passed 00033 double gamedate::Ticks = 0.0; 00034 00035 // Increase gametime 00036 void gamedate::update () 00037 { 00038 static double tenth_minute = gametime::minute () / 10.0; 00039 00040 // fts contains the number of cycles that passed since the last 00041 // call to gamedate::update 00042 Ticks += gametime::frames_to_skip (); 00043 00044 // check whether an in-game minute has passed 00045 while (Ticks >= tenth_minute) 00046 { 00047 Ticks -= tenth_minute; 00048 Time++; 00049 00050 // raise time event 00051 time_event evt (Time); 00052 event_handler::raise_event (&evt); 00053 } 00054 } 00055 00056 // load state from disk 00057 bool gamedate::get_state (igzstream &in) 00058 { 00059 // read the current date as (gametime) minutes since start of the game 00060 Time << in; 00061 00062 return true; 00063 } 00064 00065 // save state to disk 00066 void gamedate::put_state (ogzstream &out) 00067 { 00068 // write the time to disk 00069 Time >> out; 00070 } 00071 00072 // calculate the current weekday 00073 u_int16 gamedate::weekday () 00074 { 00075 return day () % DAYS_PER_WEEK; 00076 } 00077 00078 // calculate the current day 00079 u_int16 gamedate::day () 00080 { 00081 // how many minutes make one day 00082 static u_int16 day_in_minutes = 600 * HOURS_PER_DAY; 00083 00084 return Time / day_in_minutes; 00085 } 00086 00087 // calculate the hour of the current day 00088 u_int16 gamedate::hour () 00089 { 00090 return (Time / 600) % HOURS_PER_DAY; 00091 } 00092 00093 // calculate minute of the hour 00094 u_int16 gamedate::minute () 00095 { 00096 return (Time / 10) % 60; 00097 } 00098 00099 // convert the time string to gametime minutes 00100 u_int32 gamedate::parse_time (const std::string & time) 00101 { 00102 u_int32 t_minutes = 0, number = 0; 00103 char num[2] = "0"; 00104 00105 for (u_int32 i = 0; i < time.length (); i++) 00106 { 00107 // got a number 00108 if (isdigit (time[i])) 00109 { 00110 num[0] = time[i]; 00111 number = 10 * number + atoi (num); 00112 } 00113 // got a letter 00114 else if (isalpha (time[i])) 00115 { 00116 switch (time[i]) 00117 { 00118 // weeks 00119 case 'w': 00120 { 00121 t_minutes += number * DAYS_PER_WEEK * HOURS_PER_DAY * 600; 00122 break; 00123 } 00124 // days 00125 case 'd': 00126 { 00127 t_minutes += number * HOURS_PER_DAY * 600; 00128 break; 00129 } 00130 // hours 00131 case 'h': 00132 { 00133 t_minutes += number * 600; 00134 break; 00135 } 00136 // minutes 00137 case 'm': 00138 { 00139 t_minutes += number * 10; 00140 break; 00141 } 00142 // 1/10 minutes 00143 case 't': 00144 { 00145 t_minutes += number; 00146 break; 00147 } 00148 // error 00149 default: 00150 { 00151 fprintf (stderr, "*** gamedate::parse_time: Unknown time specifier '%c'\n", time[i]); 00152 break; 00153 } 00154 } 00155 00156 number = 0; 00157 } 00158 } 00159 00160 return t_minutes; 00161 }