cp_time.c
Go to the documentation of this file.00001
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "../cl_shared.h"
00027 #include "cp_campaign.h"
00028 #include "cp_time.h"
00029
00030
00031 typedef struct gameLapse_s {
00032 const char *name;
00033 int scale;
00034 } gameLapse_t;
00035
00036 #define NUM_TIMELAPSE 8
00037
00039 static const gameLapse_t lapse[NUM_TIMELAPSE] = {
00040 {N_("stopped"), 0},
00041 {N_("5 sec"), 5},
00042 {N_("5 mins"), 5 * 60},
00043 {N_("20 mins"), SECONDS_PER_HOUR / 3},
00044 {N_("1 hour"), SECONDS_PER_HOUR},
00045 {N_("12 hours"), 12 * SECONDS_PER_HOUR},
00046 {N_("1 day"), 24 * SECONDS_PER_HOUR},
00047 {N_("5 days"), 5 * SECONDS_PER_DAY}
00048 };
00049 CASSERT(lengthof(lapse) == NUM_TIMELAPSE);
00050
00056 void CL_UpdateTime (void)
00057 {
00058 dateLong_t date;
00059 CL_DateConvertLong(&ccs.date, &date);
00060
00061
00062 if (ccs.gameLapse >= 0 && ccs.gameLapse < NUM_TIMELAPSE) {
00063 Cvar_Set("mn_timelapse", _(lapse[ccs.gameLapse].name));
00064 ccs.gameTimeScale = lapse[ccs.gameLapse].scale;
00065 Cvar_SetValue("mn_timelapse_id", ccs.gameLapse);
00066 }
00067
00068
00069 Com_sprintf(cp_messageBuffer, sizeof(cp_messageBuffer), _("%i %s %02i"), date.year, Date_GetMonthName(date.month - 1), date.day);
00070 Cvar_Set("mn_mapdate", cp_messageBuffer);
00071
00072
00073 Com_sprintf(cp_messageBuffer, sizeof(cp_messageBuffer), _("%02i:%02i"), date.hour, date.min);
00074 Cvar_Set("mn_maptime", cp_messageBuffer);
00075 }
00076
00080 void CL_GameTimeStop (void)
00081 {
00082
00083 if (!cp_missiontest->integer && CP_OnGeoscape())
00084 ccs.gameLapse = 0;
00085
00086
00087 CL_UpdateTime();
00088 }
00089
00093 qboolean CL_IsTimeStopped (void)
00094 {
00095 return !ccs.gameLapse;
00096 }
00097
00101 static qboolean CL_AllowTimeScale (void)
00102 {
00103
00104
00105 if (!ccs.campaignStats.basesBuilt)
00106 return qfalse;
00107
00108 return CP_OnGeoscape();
00109 }
00110
00114 void CL_GameTimeSlow (void)
00115 {
00116
00117 if (CL_AllowTimeScale()) {
00118 if (ccs.gameLapse > 0)
00119 ccs.gameLapse--;
00120
00121 CL_UpdateTime();
00122 }
00123 }
00124
00128 void CL_GameTimeFast (void)
00129 {
00130
00131 if (CL_AllowTimeScale()) {
00132 if (ccs.gameLapse < NUM_TIMELAPSE)
00133 ccs.gameLapse++;
00134
00135 CL_UpdateTime();
00136 }
00137 }
00138
00143 static void CL_SetGameTime (int gameLapseValue)
00144 {
00145 if (gameLapseValue == ccs.gameLapse)
00146 return;
00147
00148
00149
00150 if (!ccs.campaignStats.basesBuilt)
00151 return;
00152
00153 if (gameLapseValue < 0 || gameLapseValue >= NUM_TIMELAPSE)
00154 return;
00155
00156 ccs.gameLapse = gameLapseValue;
00157
00158
00159 CL_UpdateTime();
00160 }
00161
00162
00168 void CL_SetGameTime_f (void)
00169 {
00170 if (Cmd_Argc() < 2) {
00171 Com_Printf("Usage: %s <timeid>\n", Cmd_Argv(0));
00172 return;
00173 }
00174 CL_SetGameTime(atoi(Cmd_Argv(1)));
00175 }
00176
00183 qboolean Date_LaterThan (date_t now, date_t compare)
00184 {
00185 if (now.day > compare.day)
00186 return qtrue;
00187 if (now.day < compare.day)
00188 return qfalse;
00189 if (now.sec > compare.sec)
00190 return qtrue;
00191 return qfalse;
00192 }
00193
00200 date_t Date_Add (date_t a, date_t b)
00201 {
00202 a.sec += b.sec;
00203 a.day += (a.sec / SECONDS_PER_DAY) + b.day;
00204 a.sec %= SECONDS_PER_DAY;
00205 return a;
00206 }
00207
00214 date_t Date_Random (date_t minFrame, date_t maxFrame)
00215 {
00216 maxFrame.sec = max(minFrame.day * SECONDS_PER_DAY + minFrame.sec,
00217 (maxFrame.day * SECONDS_PER_DAY + maxFrame.sec) * frand());
00218
00219 maxFrame.day = maxFrame.sec / SECONDS_PER_DAY;
00220 maxFrame.sec = maxFrame.sec % SECONDS_PER_DAY;
00221 return maxFrame;
00222 }
00223
00229 const char *Date_GetMonthName (int month)
00230 {
00231 switch (month) {
00232 case 0:
00233 return _("Jan");
00234 case 1:
00235 return _("Feb");
00236 case 2:
00237 return _("Mar");
00238 case 3:
00239 return _("Apr");
00240 case 4:
00241 return _("May");
00242 case 5:
00243 return _("Jun");
00244 case 6:
00245 return _("Jul");
00246 case 7:
00247 return _("Aug");
00248 case 8:
00249 return _("Sep");
00250 case 9:
00251 return _("Oct");
00252 case 10:
00253 return _("Nov");
00254 case 11:
00255 return _("Dec");
00256 default:
00257 return "Error";
00258 }
00259 }