UFO: Alien Invasion
cp_installation_callbacks.cpp
Go to the documentation of this file.
1 
6 /*
7 Copyright (C) 2002-2022 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 
26 #include "../../DateTime.h"
27 #include "../../cl_shared.h"
28 #include "../../ui/ui_dataids.h"
29 #include "cp_campaign.h"
31 #include "cp_installation.h"
32 #include "cp_geoscape.h"
33 #include "cp_popup.h"
34 #include "cp_ufo.h"
36 
42 {
44  cgi->Cvar_Set("mn_installation_title", "%s #%i", (insTemp) ? _(insTemp->name) : _("Installation"), ccs.campaignStats.installationsBuilt + 1);
45  if (!insTemp || !Q_strvalid(insTemp->description))
46  cgi->UI_ResetData(TEXT_BUILDING_INFO);
47  else
48  cgi->UI_RegisterText(TEXT_BUILDING_INFO, _(insTemp->description));
49 }
50 
58 {
59  B_SetCurrentSelectedBase(nullptr);
60  const int timetobuild = std::max(0, installation->installationTemplate->buildTime - (ccs.date.getDateAsDays() - installation->buildStart));
61 
62  cgi->Com_DPrintf(DEBUG_CLIENT, "INS_SelectInstallation: select installation with id %i\n", installation->idx);
64  if (installation->installationStatus == INSTALLATION_WORKING) {
65  cgi->Cvar_Set("mn_installation_timetobuild", "-");
66  } else {
67  cgi->Cvar_Set("mn_installation_timetobuild", ngettext("%d day", "%d days", timetobuild), timetobuild);
68  }
70 
71  switch (installation->installationTemplate->type) {
73  cgi->UI_PushWindow("popup_ufoyards");
74  break;
76  cgi->UI_PushWindow("basedefence");
77  break;
78  default:
79  cgi->UI_PushWindow("popup_installationstatus");
80  break;
81  }
82 }
83 
87 static void INS_BuildInstallation_f (void)
88 {
89  const installationTemplate_t* installationTemplate;
90 
91  if (cgi->Cmd_Argc() < 1) {
92  cgi->Com_Printf("Usage: %s <installationType>\n", cgi->Cmd_Argv(0));
93  return;
94  }
95 
96  /* We shouldn't build more installations than the actual limit */
98  return;
99 
100  installationTemplate = INS_GetInstallationTemplateByID(cgi->Cmd_Argv(1));
101  if (!installationTemplate) {
102  cgi->Com_Printf("The installation type %s passed for %s is not valid.\n", cgi->Cmd_Argv(1), cgi->Cmd_Argv(0));
103  return;
104  }
105 
106  assert(installationTemplate->cost >= 0);
107 
108  if (ccs.credits - installationTemplate->cost > 0) {
109  /* set up the installation */
110  installation_t* installation = INS_Build(installationTemplate, ccs.newBasePos, cgi->Cvar_GetString("mn_installation_title"));
111 
112  CP_UpdateCredits(ccs.credits - installationTemplate->cost);
113  /* this cvar is used for disabling the installation build button on geoscape if MAX_INSTALLATIONS was reached */
114  cgi->Cvar_SetValue("mn_installation_count", INS_GetCount());
115 
116  const nation_t* nation = GEO_GetNation(installation->pos);
117  if (nation)
118  Com_sprintf(cp_messageBuffer, sizeof(cp_messageBuffer), _("A new installation has been built: %s (nation: %s)"), installation->name, _(nation->name));
119  else
120  Com_sprintf(cp_messageBuffer, sizeof(cp_messageBuffer), _("A new installation has been built: %s"), installation->name);
122  } else {
123  if (installationTemplate->type == INSTALLATION_RADAR) {
125  GEO_SetOverlay("radar", 1);
126  }
129 
130  CP_Popup(_("Notice"), _("Not enough credits to set up a new installation."));
131  }
133 }
134 
139 static void INS_SelectInstallation_f (void)
140 {
141  int installationID;
142  installation_t* installation;
143 
144  if (cgi->Cmd_Argc() < 2) {
145  cgi->Com_Printf("Usage: %s <installationID>\n", cgi->Cmd_Argv(0));
146  return;
147  }
148  installationID = atoi(cgi->Cmd_Argv(1));
149 
150  installation = INS_GetByIDX(installationID);
151  if (installation != nullptr)
152  INS_SelectInstallation(installation);
153 }
154 
161 {
163 
164  /* maybe called without installation initialized or active */
165  if (!installation)
166  return;
167 
168  Q_strncpyz(installation->name, cgi->Cvar_GetString("mn_installation_title"), sizeof(installation->name));
169 }
170 
175 static void INS_DestroyInstallation_f (void)
176 {
177  installation_t* installation;
178 
179  if (cgi->Cmd_Argc() < 2 || atoi(cgi->Cmd_Argv(1)) < 0) {
180  installation = INS_GetCurrentSelectedInstallation();
181  } else {
182  installation = INS_GetByIDX(atoi(cgi->Cmd_Argv(1)));
183  if (!installation) {
184  cgi->Com_DPrintf(DEBUG_CLIENT, "Installation not founded (idx %i)\n", atoi(cgi->Cmd_Argv(1)));
185  return;
186  }
187  }
188 
189  /* Ask 'Are you sure?' by default */
190  if (cgi->Cmd_Argc() < 3 || !atoi(cgi->Cmd_Argv(2))) {
191  char command[MAX_VAR];
192 
193  Com_sprintf(command, sizeof(command), "mn_installation_destroy %d 1; ui_pop;", installation->idx);
194  cgi->UI_PopupButton(_("Destroy Installation"), _("Do you really want to destroy this installation?"),
195  command, _("Destroy"), _("Destroy installation"),
196  "ui_pop;", _("Cancel"), _("Forget it"),
197  nullptr, nullptr, nullptr);
198  return;
199  }
200  INS_DestroyInstallation(installation);
201 }
202 
207 {
208  cgi->Cvar_SetValue("mn_installation_max", B_GetInstallationLimit());
209 }
210 
214 static void INS_FillUFOYardData_f (void)
215 {
216  installation_t* ins;
217 
218  cgi->UI_ExecuteConfunc("ufolist_clear");
219  if (cgi->Cmd_Argc() < 2 || atoi(cgi->Cmd_Argv(1)) < 0) {
221  if (!ins || ins->installationTemplate->type != INSTALLATION_UFOYARD)
222  ins = INS_GetFirstUFOYard(false);
223  } else {
224  ins = INS_GetByIDX(atoi(cgi->Cmd_Argv(1)));
225  if (!ins)
226  cgi->Com_DPrintf(DEBUG_CLIENT, "Installation not founded (idx %i)\n", atoi(cgi->Cmd_Argv(1)));
227  }
228 
229  if (ins) {
230  const nation_t* nat = GEO_GetNation(ins->pos);
231  const int timeToBuild = std::max(0, ins->installationTemplate->buildTime - (ccs.date.getDateAsDays() - ins->buildStart));
232  const char* buildTime = (timeToBuild > 0 && ins->installationStatus == INSTALLATION_UNDER_CONSTRUCTION) ? va(ngettext("%d day", "%d days", timeToBuild), timeToBuild) : "-";
233  const int freeCap = std::max(0, ins->ufoCapacity.max - ins->ufoCapacity.cur);
234  const char* nationName = nat ? _(nat->name) : "";
235 
236  cgi->UI_ExecuteConfunc("ufolist_addufoyard %d \"%s\" \"%s\" %d %d \"%s\"", ins->idx, ins->name, nationName, ins->ufoCapacity.max, freeCap, buildTime);
237 
238  US_Foreach(ufo) {
239  if (ufo->installation != ins)
240  continue;
241 
242  const char* ufoName = UFO_GetName(ufo->ufoTemplate);
243  const char* condition = va(_("Condition: %3.0f%%"), ufo->condition * 100);
244  const char* status = US_StoredUFOStatus(ufo);
245  cgi->UI_ExecuteConfunc("ufolist_addufo %d \"%s\" \"%s\" \"%s\" \"%s\"", ufo->idx, ufoName, condition, ufo->ufoTemplate->model, status);
246  }
247  }
248 }
249 
253 static void INS_FillTypes_f (void)
254 {
255  cgi->UI_ExecuteConfunc("installationtype_clear");
257  for (int i = 0; i < ccs.numInstallationTemplates; i++) {
259  if (tpl->once && INS_HasType(tpl->type, INSTALLATION_NOT_USED))
260  continue;
261  if (tpl->tech == nullptr || RS_IsResearched_ptr(tpl->tech)) {
262  cgi->UI_ExecuteConfunc("installationtype_add \"%s\" \"%s\" \"%s\" \"%d c\"", tpl->id, _(tpl->name),
263  (tpl->buildTime > 0) ? va(ngettext("%d day", "%d days", tpl->buildTime), tpl->buildTime) : "-", tpl->cost);
264  }
265  }
266  }
267 
269  if (B_GetCount() < MAX_BASES)
270  cgi->UI_ExecuteConfunc("installationtype_add base \"%s\" - \"%d c\"", _("Base"), ccs.curCampaign->basecost);
271 }
272 
276 static void INS_SelectType_f (void)
277 {
278  if (cgi->Cmd_Argc() < 2)
279  return;
280 
281  const char* id = cgi->Cmd_Argv(1);
282 
284  GEO_ResetAction();
285  return;
286  }
287 
289  if (!tpl) {
290  cgi->Com_Printf("Invalid installation template\n");
291  return;
292  }
293 
295  cgi->Com_Printf("Maximum number of installations reached\n");
296  return;
297  }
298 
299  if (tpl->tech != nullptr && !RS_IsResearched_ptr(tpl->tech)) {
300  cgi->Com_Printf("This type of installation is not yet researched\n");
301  return;
302  }
303 
304  if (tpl->once && INS_HasType(tpl->type, INSTALLATION_NOT_USED)) {
305  cgi->Com_Printf("Cannot build more of this installation\n");
306  return;
307  }
308 
310 
311  /* show radar overlay (if not already displayed) */
313  GEO_SetOverlay("radar", 1);
314 
316  cgi->Cvar_Set("mn_installation_type", "%s", tpl->id);
317  cgi->Cvar_Set("mn_installation_cost", "%d", tpl->cost);
318  cgi->Cvar_Set("mn_installation_timetobuild", "%d", tpl->buildTime);
319 }
320 
322  {"mn_installation_select", INS_SelectInstallation_f, "Parameter is the installation index. -1 will build a new one."},
323  {"mn_installation_build", INS_BuildInstallation_f, nullptr},
324  {"mn_installation_changename", INS_ChangeInstallationName_f, "Called after editing the cvar installation name"},
325  {"mn_installation_destroy", INS_DestroyInstallation_f, "Destroys an installation"},
326  {"mn_installation_update_max_count", INS_UpdateInstallationLimit_f, "Updates the installation count limit"},
327  {"ui_fill_installationtypes", INS_FillTypes_f, "Fills create installation / installation type selection popup"},
328  {"ui_build_installationtype", INS_SelectType_f, "Selects installation type to build"},
329  {"ui_fillufoyards", INS_FillUFOYardData_f, "Fills UFOYard UI with data"},
330  {nullptr, nullptr, nullptr}
331 };
332 void INS_InitCallbacks (void)
333 {
334  cgi->Cmd_TableAddList(installationCallbacks);
335 
336  cgi->Cvar_SetValue("mn_installation_count", INS_GetCount());
337  cgi->Cvar_Set("mn_installation_title", "");
338  cgi->Cvar_Set("mn_installation_type", "");
339  cgi->Cvar_Set("mn_installation_max", "");
340 }
341 
343 {
344  cgi->Cmd_TableRemoveList(installationCallbacks);
345 
346  cgi->Cvar_Delete("mn_installation_count");
347  cgi->Cvar_Delete("mn_installation_title");
348  cgi->Cvar_Delete("mn_installation_max");
349  cgi->Cvar_Delete("mn_installation_type");
350 }
Header file for menu related console command callbacks.
uiMessageListNodeMessage_t * MSO_CheckAddNewMessage(const notify_t messagecategory, const char *title, const char *text, messageType_t type, technology_t *pedia, bool popup)
Adds a new message to message stack. It uses message settings to verify whether sound should be playe...
const installationTemplate_t * INS_GetInstallationTemplateByType(installationType_t type)
Returns the installation Template for a given installation type.
static void INS_DestroyInstallation_f(void)
console function for destroying an installation
bool RS_IsResearched_ptr(const technology_t *tech)
Checks whether an item is already researched.
QGL_EXTERN GLint GLenum type
Definition: r_gl.h:94
static void INS_FillUFOYardData_f(void)
Fills the UI with ufo yard data.
cvar_t *IMPORT * Cvar_Set(const char *varName, const char *value,...) __attribute__((format(__printf__
const char * va(const char *format,...)
does a varargs printf into a temp buffer, so I don&#39;t need to have varargs versions of all text functi...
Definition: shared.cpp:410
static void INS_UpdateInstallationLimit_f(void)
updates the installation limit cvar for menus
bool INS_HasType(installationType_t type, installationStatus_t status)
Checks whether the given installation type is available.
installationStatus_t installationStatus
#define _(String)
Definition: cl_shared.h:44
static void INS_FillTypes_f(void)
Fills create installation / installation type selection popup.
bool Com_sprintf(char *dest, size_t size, const char *fmt,...)
copies formatted string with buffer-size checking
Definition: shared.cpp:494
Header for installation management related stuff.
void INS_SetCurrentSelectedInstallation(const installation_t *installation)
Sets the currently selected installation.
bool GEO_IsRadarOverlayActivated(void)
Definition: cp_geoscape.cpp:85
const char *IMPORT * Cvar_GetString(const char *varName)
const char *IMPORT * Cmd_Argv(int n)
const char * UFO_GetName(const aircraft_t *ufocraft)
Returns name of the UFO if UFO has been researched.
Definition: cp_ufo.cpp:243
Nation definition.
Definition: cp_nation.h:46
A installation with all it&#39;s data.
int INS_GetCount(void)
Get number of installations.
UFO recovery and storing callback header file.
void GEO_ResetAction(void)
No more special action on the geoscape.
#define Q_strvalid(string)
Definition: shared.h:141
int B_GetCount(void)
Returns the count of founded bases.
Definition: cp_base.cpp:277
installationType_t type
void Q_strncpyz(char *dest, const char *src, size_t destsize)
Safe strncpy that ensures a trailing zero.
Definition: shared.cpp:457
static void INS_SelectInstallation_f(void)
Called when an installation is opened or a new installation is created on geoscape. For a new installation the installationID is -1.
#define ngettext(x, y, cnt)
Definition: g_local.h:40
installationType_t
#define DEBUG_CLIENT
Definition: defines.h:59
void INS_InitCallbacks(void)
#define MAX_VAR
Definition: shared.h:36
#define US_Foreach(var)
void CP_Popup(const char *title, const char *text,...)
Wrapper around UI_Popup.
Definition: cp_popup.cpp:474
int getDateAsDays() const
Return the date part of the DateTime as days.
Definition: DateTime.cpp:46
capacities_t ufoCapacity
const cgame_import_t * cgi
static void INS_SetInstallationTitle(installationType_t type)
Sets the title of the installation to a cvar to prepare the rename menu.
void CP_UpdateCredits(int credits)
Sets credits and update mn_credits cvar.
ccs_t ccs
Definition: cp_campaign.cpp:63
static const cmdList_t installationCallbacks[]
Header for Geoscape management.
void B_SetCurrentSelectedBase(const base_t *base)
Sets the selected base.
Definition: cp_base.cpp:1553
installation_t * INS_Build(const installationTemplate_t *installationTemplate, const vec2_t pos, const char *name)
Build a new installation.
char cp_messageBuffer[MAX_MESSAGE_TEXT]
Definition: cp_messages.cpp:33
static void INS_BuildInstallation_f(void)
Constructs a new installation.
int B_GetInstallationLimit(void)
Counts the actual installation count limit.
Definition: cp_base.cpp:1153
campaign_t * curCampaign
Definition: cp_campaign.h:378
char name[MAX_VAR]
stats_t campaignStats
Definition: cp_campaign.h:379
QGL_EXTERN GLint i
Definition: r_gl.h:113
installation_t * INS_GetFirstUFOYard(bool free)
returns the first installation with (free) ufostoring capacity
vec2_t newBasePos
Definition: cp_campaign.h:262
class DateTime date
Definition: cp_campaign.h:246
Definition: cmd.h:86
struct technology_s * tech
int installationsBuilt
Definition: cp_statistics.h:34
installation_t * INS_GetCurrentSelectedInstallation(void)
Returns the current selected installation.
int numInstallationTemplates
Definition: cp_campaign.h:349
Header file for single player campaign control.
static void INS_SelectType_f(void)
Selects installation type to build.
void INS_SelectInstallation(installation_t *installation)
Select an installation when clicking on it on geoscape.
mapAction_t mapAction
Definition: cp_campaign.h:261
void INS_ShutdownCallbacks(void)
#define MAX_BASES
Definition: cp_base.h:32
void GEO_SetOverlay(const char *overlayID, int status)
Turn overlay on/off.
int credits
Definition: cp_campaign.h:243
installationTemplate_t installationTemplates[MAX_INSTALLATION_TEMPLATES]
Definition: cp_campaign.h:348
const installationTemplate_t * installationTemplate
const installationTemplate_t * INS_GetInstallationTemplateByID(const char *id)
Returns the installation Template for a given installation ID.
const char * name
Definition: cp_nation.h:48
nation_t * GEO_GetNation(const vec2_t pos)
Translate nation map color to nation.
static void INS_ChangeInstallationName_f(void)
Creates console command to change the name of a installation. Copies the value of the cvar mn_install...
void INS_DestroyInstallation(installation_t *installation)
Destroys an installation.
const char * US_StoredUFOStatus(const storedUFO_t *ufo)
Returns string representation of the stored UFO&#39;s status.
installation_t * INS_GetByIDX(int idx)
Get installation by it&#39;s index.