UFO: Alien Invasion
Loading...
Searching...
No Matches
cp_time.cpp
Go to the documentation of this file.
1
5
6/*
7Copyright (C) 2002-2025 UFO: Alien Invasion.
8
9This program is free software; you can redistribute it and/or
10modify it under the terms of the GNU General Public License
11as published by the Free Software Foundation; either version 2
12of the License, or (at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17
18See the GNU General Public License for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
22Foundation, 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
30typedef struct gameLapse_s {
31 const char* name;
32 int scale;
34
35#define NUM_TIMELAPSE 8
36
37const int DAYS_PER_MONTH[DateTime::MONTHS_PER_YEAR] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
38
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
57const 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
73void 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. */
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
104void 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));
112 ccs.gameTimeScale = lapse[ccs.gameLapse].scale;
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
127{
128 /* don't allow time scale in tactical mode - only on the geoscape */
129 if (!cp_missiontest->integer && CP_OnGeoscape())
130 ccs.gameLapse = 0;
131
132 /* Make sure the new lapse state is updated and it (and the time) is show in the menu. */
134}
135
140{
141 return !ccs.gameLapse;
142}
143
147static 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 */
151 if (!ccs.campaignStats.basesBuilt)
152 return false;
153
154 return CP_OnGeoscape();
155}
156
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. */
168 }
169}
170
175{
176 /* don't allow time scale in tactical mode - only on the geoscape */
177 if (CP_AllowTimeScale()) {
178 if (ccs.gameLapse < NUM_TIMELAPSE)
179 ccs.gameLapse++;
180 /* Make sure the new lapse state is updated and it (and the time) is show in the menu. */
182 }
183}
184
189static 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 */
196 if (!ccs.campaignStats.basesBuilt)
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. */
206}
207
208
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
229{
231}
232
239DateTime 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
250const 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}
DateTime class definition.
Share stuff between the different cgame implementations.
#define _(String)
Definition cl_shared.h:44
#define N_(String)
Definition cl_shared.h:46
Class describing a point of time.
Definition DateTime.h:31
static const int SECONDS_PER_DAY
Definition DateTime.h:43
int getTimeAsSeconds() const
Return the time part of the DateTime as seconds.
Definition DateTime.cpp:54
static const short SECONDS_PER_MINUTE
Definition DateTime.h:34
static const short MONTHS_PER_YEAR
Definition DateTime.h:39
static const short SECONDS_PER_HOUR
Definition DateTime.h:42
static const int DAYS_PER_YEAR
Definition DateTime.h:37
int getDateAsDays() const
Return the date part of the DateTime as days.
Definition DateTime.cpp:46
bool CP_OnGeoscape(void)
Returns if we are currently on the Geoscape.
cvar_t * cp_missiontest
ccs_t ccs
Header file for single player campaign control.
const cgame_import_t * cgi
void CP_GameTimeFast(void)
Increase game time speed.
Definition cp_time.cpp:174
#define NUM_TIMELAPSE
Definition cp_time.cpp:35
const char * CP_SecondConvert(int second)
Converts a number of second into a char to display.
Definition cp_time.cpp:57
void CP_GameTimeStop(void)
Stop game time speed.
Definition cp_time.cpp:126
void CP_UpdateTime(void)
Updates date/time and timescale (=timelapse) on the geoscape menu.
Definition cp_time.cpp:104
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
bool CP_IsTimeStopped(void)
Check if time is stopped.
Definition cp_time.cpp:139
void CP_GameTimeSlow(void)
Decrease game time speed.
Definition cp_time.cpp:160
void CP_SetGameTime_f(void)
Set a new time game from id.
Definition cp_time.cpp:214
int Date_DateToSeconds(const DateTime &date)
Convert a date to seconds.
Definition cp_time.cpp:228
static const gameLapse_t lapse[NUM_TIMELAPSE]
The possible geoscape time intervalls.
Definition cp_time.cpp:40
const char * Date_GetMonthName(int month)
Returns the short monthame to the given month index.
Definition cp_time.cpp:250
static bool CP_AllowTimeScale(void)
Definition cp_time.cpp:147
static void CP_SetGameTime(int gameLapseValue)
Set game time speed.
Definition cp_time.cpp:189
const int DAYS_PER_MONTH[DateTime::MONTHS_PER_YEAR]
Definition cp_time.cpp:37
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
Campaign geoscape time header.
float frand(void)
Return random values between 0 and 1.
Definition mathlib.cpp:506
QGL_EXTERN GLint i
Definition r_gl.h:113
#define lengthof(x)
Definition shared.h:105
#define CASSERT(x)
Definition shared.h:107
bool Com_sprintf(char *dest, size_t size, const char *fmt,...)
copies formatted string with buffer-size checking
Definition shared.cpp:494
Human readable time information in the game.
Definition cp_time.h:36
byte hour
Definition cp_time.h:40
byte month
Definition cp_time.h:38
byte day
Definition cp_time.h:39
byte min
Definition cp_time.h:41
short year
Definition cp_time.h:37
byte sec
Definition cp_time.h:42
const char * name
Definition cp_time.cpp:31