UFO: Alien Invasion
Doxygen documentation generating
cp_time.cpp
Go to the documentation of this file.
1 
6 /*
7 Copyright (C) 2002-2023 UFO: Alien Invasion.
8 
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 
18 See the GNU General Public License for more details.
19 
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 */
24 
25 #include "cp_time.h"
26 #include "../../DateTime.h"
27 #include "../../cl_shared.h"
28 #include "cp_campaign.h"
29 
30 typedef struct gameLapse_s {
31  const char* name;
32  int scale;
33 } gameLapse_t;
34 
35 #define NUM_TIMELAPSE 8
36 
37 const int DAYS_PER_MONTH[DateTime::MONTHS_PER_YEAR] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
38 
40 static const gameLapse_t lapse[NUM_TIMELAPSE] = {
41  {N_("stopped"), 0},
42  {N_("5 sec"), 5},
43  {N_("5 mins"), 5 * DateTime::SECONDS_PER_MINUTE},
44  {N_("20 mins"), 20 * DateTime::SECONDS_PER_MINUTE},
45  {N_("1 hour"), DateTime::SECONDS_PER_HOUR},
46  {N_("12 hours"), 12 * DateTime::SECONDS_PER_HOUR},
47  {N_("1 day"), DateTime::SECONDS_PER_DAY},
48  {N_("5 days"), 5 * DateTime::SECONDS_PER_DAY}
49 };
51 
57 const char* CP_SecondConvert (int second)
58 {
59  static char buffer[6];
60  const int hour = second / DateTime::SECONDS_PER_HOUR;
61  const int min = (second - hour * DateTime::SECONDS_PER_HOUR) / 60;
62  Com_sprintf(buffer, sizeof(buffer), "%2i:%02i", hour, min);
63  return buffer;
64 }
65 
73 void CP_DateConvertLong (const DateTime& date, dateLong_t* dateLong)
74 {
75  /* Get the year */
76  dateLong->year = date.getDateAsDays() / DateTime::DAYS_PER_YEAR;
77 
78  /* Get the days in the year. */
79  int d = date.getDateAsDays() % DateTime::DAYS_PER_YEAR;
80 
81  /* Subtract days until no full month is left. */
82  byte i;
83  for (i = 0; i < DateTime::MONTHS_PER_YEAR; i++) {
84  if (d < DAYS_PER_MONTH[i])
85  break;
86  d -= DAYS_PER_MONTH[i];
87  }
88 
89  dateLong->day = d + 1;
90  dateLong->month = i + 1;
92  dateLong->min = (date.getTimeAsSeconds() - dateLong->hour * DateTime::SECONDS_PER_HOUR) / 60;
93  dateLong->sec = date.getTimeAsSeconds() - dateLong->hour * DateTime::SECONDS_PER_HOUR - dateLong->min * 60;
94 
95  assert(dateLong->month >= 1 && dateLong->month <= DateTime::MONTHS_PER_YEAR);
96  assert(dateLong->day >= 1 && dateLong->day <= DAYS_PER_MONTH[i]);
97 }
98 
104 void CP_UpdateTime (void)
105 {
106  dateLong_t date;
107  CP_DateConvertLong(ccs.date, &date);
108 
109  /* Update the timelapse text */
110  if (ccs.gameLapse >= 0 && ccs.gameLapse < NUM_TIMELAPSE) {
111  cgi->Cvar_Set("mn_timelapse", "%s", _(lapse[ccs.gameLapse].name));
113  cgi->Cvar_SetValue("mn_timelapse_id", ccs.gameLapse);
114  }
115 
116  /* Update the date */
117  cgi->Cvar_Set("mn_mapdate", _("%i %s %02i"), date.year, Date_GetMonthName(date.month - 1), date.day);
118 
119  /* Update the time. */
120  cgi->Cvar_Set("mn_maptime", _("%02i:%02i"), date.hour, date.min);
121 }
122 
126 void CP_GameTimeStop (void)
127 {
128  /* don't allow time scale in tactical mode - only on the geoscape */
130  ccs.gameLapse = 0;
131 
132  /* Make sure the new lapse state is updated and it (and the time) is show in the menu. */
133  CP_UpdateTime();
134 }
135 
139 bool CP_IsTimeStopped (void)
140 {
141  return !ccs.gameLapse;
142 }
143 
147 static bool CP_AllowTimeScale (void)
148 {
149  /* check the stats value - already build bases might have been destroyed
150  * so the B_GetCount() values is pointless here */
152  return false;
153 
154  return CP_OnGeoscape();
155 }
156 
160 void CP_GameTimeSlow (void)
161 {
162  /* don't allow time scale in tactical mode - only on the geoscape */
163  if (CP_AllowTimeScale()) {
164  if (ccs.gameLapse > 0)
165  ccs.gameLapse--;
166  /* Make sure the new lapse state is updated and it (and the time) is show in the menu. */
167  CP_UpdateTime();
168  }
169 }
170 
174 void CP_GameTimeFast (void)
175 {
176  /* don't allow time scale in tactical mode - only on the geoscape */
177  if (CP_AllowTimeScale()) {
179  ccs.gameLapse++;
180  /* Make sure the new lapse state is updated and it (and the time) is show in the menu. */
181  CP_UpdateTime();
182  }
183 }
184 
189 static void CP_SetGameTime (int gameLapseValue)
190 {
191  if (gameLapseValue == ccs.gameLapse)
192  return;
193 
194  /* check the stats value - already build bases might have been destroyed
195  * so the B_GetCount() values is pointless here */
197  return;
198 
199  if (gameLapseValue < 0 || gameLapseValue >= NUM_TIMELAPSE)
200  return;
201 
202  ccs.gameLapse = gameLapseValue;
203 
204  /* Make sure the new lapse state is updated and it (and the time) is show in the menu. */
205  CP_UpdateTime();
206 }
207 
208 
214 void CP_SetGameTime_f (void)
215 {
216  if (cgi->Cmd_Argc() < 2) {
217  cgi->Com_Printf("Usage: %s <timeid>\n", cgi->Cmd_Argv(0));
218  return;
219  }
220  CP_SetGameTime(atoi(cgi->Cmd_Argv(1)));
221 }
222 
228 int Date_DateToSeconds (const DateTime& date)
229 {
231 }
232 
239 DateTime Date_Random (const DateTime& minFrame, const DateTime& maxFrame)
240 {
241  const int random = (maxFrame.getDateAsDays() * DateTime::SECONDS_PER_DAY + maxFrame.getTimeAsSeconds()) * frand();
242  return DateTime(0, std::max(minFrame.getDateAsDays() * DateTime::SECONDS_PER_DAY + minFrame.getTimeAsSeconds(), random));
243 }
244 
250 const char* Date_GetMonthName (int month)
251 {
252  switch (month) {
253  case 0:
254  return _("Jan");
255  case 1:
256  return _("Feb");
257  case 2:
258  return _("Mar");
259  case 3:
260  return _("Apr");
261  case 4:
262  return _("May");
263  case 5:
264  return _("Jun");
265  case 6:
266  return _("Jul");
267  case 7:
268  return _("Aug");
269  case 8:
270  return _("Sep");
271  case 9:
272  return _("Oct");
273  case 10:
274  return _("Nov");
275  case 11:
276  return _("Dec");
277  default:
278  return "Error";
279  }
280 }
bool CP_IsTimeStopped(void)
Check if time is stopped.
Definition: cp_time.cpp:139
#define NUM_TIMELAPSE
Definition: cp_time.cpp:35
Class describing a point of time.
Definition: DateTime.h:30
byte min
Definition: cp_time.h:41
static const short MONTHS_PER_YEAR
Definition: DateTime.h:39
bool CP_OnGeoscape(void)
Returns if we are currently on the Geoscape.
const char * name
Definition: cp_time.cpp:31
CASSERT(lengthof(lapse)==NUM_TIMELAPSE)
cvar_t *IMPORT * Cvar_Set(const char *varName, const char *value,...) __attribute__((format(__printf__
void CP_SetGameTime_f(void)
Set a new time game from id.
Definition: cp_time.cpp:214
byte sec
Definition: cp_time.h:42
#define _(String)
Definition: cl_shared.h:44
int Date_DateToSeconds(const DateTime &date)
Convert a date to seconds.
Definition: cp_time.cpp:228
int gameTimeScale
Definition: cp_campaign.h:265
bool Com_sprintf(char *dest, size_t size, const char *fmt,...)
copies formatted string with buffer-size checking
Definition: shared.cpp:494
int integer
Definition: cvar.h:81
static void CP_SetGameTime(int gameLapseValue)
Set game time speed.
Definition: cp_time.cpp:189
const char *IMPORT * Cmd_Argv(int n)
byte day
Definition: cp_time.h:39
int basesBuilt
Definition: cp_statistics.h:32
void CP_GameTimeSlow(void)
Decrease game time speed.
Definition: cp_time.cpp:160
void CP_GameTimeStop(void)
Stop game time speed.
Definition: cp_time.cpp:126
const char * Date_GetMonthName(int month)
Returns the short monthame to the given month index.
Definition: cp_time.cpp:250
byte hour
Definition: cp_time.h:40
static const int SECONDS_PER_DAY
Definition: DateTime.h:43
void CP_UpdateTime(void)
Updates date/time and timescale (=timelapse) on the geoscape menu.
Definition: cp_time.cpp:104
static const int DAYS_PER_YEAR
Definition: DateTime.h:37
const cgame_import_t * cgi
Human readable time information in the game.
Definition: cp_time.h:36
static bool CP_AllowTimeScale(void)
Definition: cp_time.cpp:147
int getTimeAsSeconds() const
Return the time part of the DateTime as seconds.
Definition: DateTime.cpp:54
ccs_t ccs
Definition: cp_campaign.cpp:63
short year
Definition: cp_time.h:37
Campaign geoscape time header.
int getDateAsDays() const
Return the date part of the DateTime as days.
Definition: DateTime.cpp:46
stats_t campaignStats
Definition: cp_campaign.h:379
float frand(void)
Return random values between 0 and 1.
Definition: mathlib.cpp:506
QGL_EXTERN GLint i
Definition: r_gl.h:113
DateTime Date_Random(const DateTime &minFrame, const DateTime &maxFrame)
Return a random relative date which lies between a lower and upper limit.
Definition: cp_time.cpp:239
class DateTime date
Definition: cp_campaign.h:246
static const gameLapse_t lapse[NUM_TIMELAPSE]
The possible geoscape time intervalls.
Definition: cp_time.cpp:40
void CP_GameTimeFast(void)
Increase game time speed.
Definition: cp_time.cpp:174
static const short SECONDS_PER_MINUTE
Definition: DateTime.h:34
const int DAYS_PER_MONTH[DateTime::MONTHS_PER_YEAR]
Definition: cp_time.cpp:37
Header file for single player campaign control.
static const short SECONDS_PER_HOUR
Definition: DateTime.h:42
#define N_(String)
Definition: cl_shared.h:46
#define lengthof(x)
Definition: shared.h:105
void CP_DateConvertLong(const DateTime &date, dateLong_t *dateLong)
Converts a date from the engine in a (longer) human-readable format.
Definition: cp_time.cpp:73
int gameLapse
Definition: cp_campaign.h:266
uint8_t byte
Definition: ufotypes.h:34
cvar_t * cp_missiontest
Definition: cp_campaign.cpp:64
const char * CP_SecondConvert(int second)
Converts a number of second into a char to display.
Definition: cp_time.cpp:57
byte month
Definition: cp_time.h:38