UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
cp_installation_callbacks.cpp
Go to the documentation of this file.
1 
6 /*
7 Copyright (C) 2002-2020 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 "../../cl_shared.h"
27 #include "../../ui/ui_dataids.h"
28 #include "cp_campaign.h"
30 #include "cp_installation.h"
31 #include "cp_geoscape.h"
32 #include "cp_popup.h"
33 #include "cp_ufo.h"
35 
41 {
43  cgi->Cvar_Set("mn_installation_title", "%s #%i", (insTemp) ? _(insTemp->name) : _("Installation"), ccs.campaignStats.installationsBuilt + 1);
44  if (!insTemp || !Q_strvalid(insTemp->description))
45  cgi->UI_ResetData(TEXT_BUILDING_INFO);
46  else
47  cgi->UI_RegisterText(TEXT_BUILDING_INFO, _(insTemp->description));
48 }
49 
57 {
58  B_SetCurrentSelectedBase(nullptr);
59  const int timetobuild = std::max(0, installation->installationTemplate->buildTime - (ccs.date.day - installation->buildStart));
60 
61  cgi->Com_DPrintf(DEBUG_CLIENT, "INS_SelectInstallation: select installation with id %i\n", installation->idx);
63  if (installation->installationStatus == INSTALLATION_WORKING) {
64  cgi->Cvar_Set("mn_installation_timetobuild", "-");
65  } else {
66  cgi->Cvar_Set("mn_installation_timetobuild", ngettext("%d day", "%d days", timetobuild), timetobuild);
67  }
69 
70  switch (installation->installationTemplate->type) {
72  cgi->UI_PushWindow("popup_ufoyards");
73  break;
75  cgi->UI_PushWindow("basedefence");
76  break;
77  default:
78  cgi->UI_PushWindow("popup_installationstatus");
79  break;
80  }
81 }
82 
86 static void INS_BuildInstallation_f (void)
87 {
88  const installationTemplate_t* installationTemplate;
89 
90  if (cgi->Cmd_Argc() < 1) {
91  cgi->Com_Printf("Usage: %s <installationType>\n", cgi->Cmd_Argv(0));
92  return;
93  }
94 
95  /* We shouldn't build more installations than the actual limit */
97  return;
98 
99  installationTemplate = INS_GetInstallationTemplateByID(cgi->Cmd_Argv(1));
100  if (!installationTemplate) {
101  cgi->Com_Printf("The installation type %s passed for %s is not valid.\n", cgi->Cmd_Argv(1), cgi->Cmd_Argv(0));
102  return;
103  }
104 
105  assert(installationTemplate->cost >= 0);
106 
107  if (ccs.credits - installationTemplate->cost > 0) {
108  /* set up the installation */
109  installation_t* installation = INS_Build(installationTemplate, ccs.newBasePos, cgi->Cvar_GetString("mn_installation_title"));
110 
111  CP_UpdateCredits(ccs.credits - installationTemplate->cost);
112  /* this cvar is used for disabling the installation build button on geoscape if MAX_INSTALLATIONS was reached */
113  cgi->Cvar_SetValue("mn_installation_count", INS_GetCount());
114 
115  const nation_t* nation = GEO_GetNation(installation->pos);
116  if (nation)
117  Com_sprintf(cp_messageBuffer, sizeof(cp_messageBuffer), _("A new installation has been built: %s (nation: %s)"), installation->name, _(nation->name));
118  else
119  Com_sprintf(cp_messageBuffer, sizeof(cp_messageBuffer), _("A new installation has been built: %s"), installation->name);
121  } else {
122  if (installationTemplate->type == INSTALLATION_RADAR) {
124  GEO_SetOverlay("radar", 1);
125  }
128 
129  CP_Popup(_("Notice"), _("Not enough credits to set up a new installation."));
130  }
132 }
133 
138 static void INS_SelectInstallation_f (void)
139 {
140  int installationID;
141  installation_t* installation;
142 
143  if (cgi->Cmd_Argc() < 2) {
144  cgi->Com_Printf("Usage: %s <installationID>\n", cgi->Cmd_Argv(0));
145  return;
146  }
147  installationID = atoi(cgi->Cmd_Argv(1));
148 
149  installation = INS_GetByIDX(installationID);
150  if (installation != nullptr)
151  INS_SelectInstallation(installation);
152 }
153 
160 {
162 
163  /* maybe called without installation initialized or active */
164  if (!installation)
165  return;
166 
167  Q_strncpyz(installation->name, cgi->Cvar_GetString("mn_installation_title"), sizeof(installation->name));
168 }
169 
174 static void INS_DestroyInstallation_f (void)
175 {
176  installation_t* installation;
177 
178  if (cgi->Cmd_Argc() < 2 || atoi(cgi->Cmd_Argv(1)) < 0) {
179  installation = INS_GetCurrentSelectedInstallation();
180  } else {
181  installation = INS_GetByIDX(atoi(cgi->Cmd_Argv(1)));
182  if (!installation) {
183  cgi->Com_DPrintf(DEBUG_CLIENT, "Installation not founded (idx %i)\n", atoi(cgi->Cmd_Argv(1)));
184  return;
185  }
186  }
187 
188  /* Ask 'Are you sure?' by default */
189  if (cgi->Cmd_Argc() < 3 || !atoi(cgi->Cmd_Argv(2))) {
190  char command[MAX_VAR];
191 
192  Com_sprintf(command, sizeof(command), "mn_installation_destroy %d 1; ui_pop;", installation->idx);
193  cgi->UI_PopupButton(_("Destroy Installation"), _("Do you really want to destroy this installation?"),
194  command, _("Destroy"), _("Destroy installation"),
195  "ui_pop;", _("Cancel"), _("Forget it"),
196  nullptr, nullptr, nullptr);
197  return;
198  }
199  INS_DestroyInstallation(installation);
200 }
201 
206 {
207  cgi->Cvar_SetValue("mn_installation_max", B_GetInstallationLimit());
208 }
209 
213 static void INS_FillUFOYardData_f (void)
214 {
215  installation_t* ins;
216 
217  cgi->UI_ExecuteConfunc("ufolist_clear");
218  if (cgi->Cmd_Argc() < 2 || atoi(cgi->Cmd_Argv(1)) < 0) {
220  if (!ins || ins->installationTemplate->type != INSTALLATION_UFOYARD)
221  ins = INS_GetFirstUFOYard(false);
222  } else {
223  ins = INS_GetByIDX(atoi(cgi->Cmd_Argv(1)));
224  if (!ins)
225  cgi->Com_DPrintf(DEBUG_CLIENT, "Installation not founded (idx %i)\n", atoi(cgi->Cmd_Argv(1)));
226  }
227 
228  if (ins) {
229  const nation_t* nat = GEO_GetNation(ins->pos);
230  const int timeToBuild = std::max(0, ins->installationTemplate->buildTime - (ccs.date.day - ins->buildStart));
231  const char* buildTime = (timeToBuild > 0 && ins->installationStatus == INSTALLATION_UNDER_CONSTRUCTION) ? va(ngettext("%d day", "%d days", timeToBuild), timeToBuild) : "-";
232  const int freeCap = std::max(0, ins->ufoCapacity.max - ins->ufoCapacity.cur);
233  const char* nationName = nat ? _(nat->name) : "";
234 
235  cgi->UI_ExecuteConfunc("ufolist_addufoyard %d \"%s\" \"%s\" %d %d \"%s\"", ins->idx, ins->name, nationName, ins->ufoCapacity.max, freeCap, buildTime);
236 
237  US_Foreach(ufo) {
238  if (ufo->installation != ins)
239  continue;
240 
241  const char* ufoName = UFO_GetName(ufo->ufoTemplate);
242  const char* condition = va(_("Condition: %3.0f%%"), ufo->condition * 100);
243  const char* status = US_StoredUFOStatus(ufo);
244  cgi->UI_ExecuteConfunc("ufolist_addufo %d \"%s\" \"%s\" \"%s\" \"%s\"", ufo->idx, ufoName, condition, ufo->ufoTemplate->model, status);
245  }
246  }
247 }
248 
252 static void INS_FillTypes_f (void)
253 {
254  cgi->UI_ExecuteConfunc("installationtype_clear");
256  for (int i = 0; i < ccs.numInstallationTemplates; i++) {
258  if (tpl->once && INS_HasType(tpl->type, INSTALLATION_NOT_USED))
259  continue;
260  if (tpl->tech == nullptr || RS_IsResearched_ptr(tpl->tech)) {
261  cgi->UI_ExecuteConfunc("installationtype_add \"%s\" \"%s\" \"%s\" \"%d c\"", tpl->id, _(tpl->name),
262  (tpl->buildTime > 0) ? va(ngettext("%d day", "%d days", tpl->buildTime), tpl->buildTime) : "-", tpl->cost);
263  }
264  }
265  }
266 
268  if (B_GetCount() < MAX_BASES)
269  cgi->UI_ExecuteConfunc("installationtype_add base \"%s\" - \"%d c\"", _("Base"), ccs.curCampaign->basecost);
270 }
271 
275 static void INS_SelectType_f (void)
276 {
277  if (cgi->Cmd_Argc() < 2)
278  return;
279 
280  const char* id = cgi->Cmd_Argv(1);
281 
283  GEO_ResetAction();
284  return;
285  }
286 
288  if (!tpl) {
289  cgi->Com_Printf("Invalid installation template\n");
290  return;
291  }
292 
294  cgi->Com_Printf("Maximum number of installations reached\n");
295  return;
296  }
297 
298  if (tpl->tech != nullptr && !RS_IsResearched_ptr(tpl->tech)) {
299  cgi->Com_Printf("This type of installation is not yet researched\n");
300  return;
301  }
302 
303  if (tpl->once && INS_HasType(tpl->type, INSTALLATION_NOT_USED)) {
304  cgi->Com_Printf("Cannot build more of this installation\n");
305  return;
306  }
307 
309 
310  /* show radar overlay (if not already displayed) */
312  GEO_SetOverlay("radar", 1);
313 
315  cgi->Cvar_Set("mn_installation_type", "%s", tpl->id);
316  cgi->Cvar_Set("mn_installation_cost", "%d", tpl->cost);
317  cgi->Cvar_Set("mn_installation_timetobuild", "%d", tpl->buildTime);
318 }
319 
321  {"mn_installation_select", INS_SelectInstallation_f, "Parameter is the installation index. -1 will build a new one."},
322  {"mn_installation_build", INS_BuildInstallation_f, nullptr},
323  {"mn_installation_changename", INS_ChangeInstallationName_f, "Called after editing the cvar installation name"},
324  {"mn_installation_destroy", INS_DestroyInstallation_f, "Destroys an installation"},
325  {"mn_installation_update_max_count", INS_UpdateInstallationLimit_f, "Updates the installation count limit"},
326  {"ui_fill_installationtypes", INS_FillTypes_f, "Fills create installation / installation type selection popup"},
327  {"ui_build_installationtype", INS_SelectType_f, "Selects installation type to build"},
328  {"ui_fillufoyards", INS_FillUFOYardData_f, "Fills UFOYard UI with data"},
329  {nullptr, nullptr, nullptr}
330 };
331 void INS_InitCallbacks (void)
332 {
333  cgi->Cmd_TableAddList(installationCallbacks);
334 
335  cgi->Cvar_SetValue("mn_installation_count", INS_GetCount());
336  cgi->Cvar_Set("mn_installation_title", "");
337  cgi->Cvar_Set("mn_installation_type", "");
338  cgi->Cvar_Set("mn_installation_max", "");
339 }
340 
342 {
343  cgi->Cmd_TableRemoveList(installationCallbacks);
344 
345  cgi->Cvar_Delete("mn_installation_count");
346  cgi->Cvar_Delete("mn_installation_title");
347  cgi->Cvar_Delete("mn_installation_max");
348  cgi->Cvar_Delete("mn_installation_type");
349 }
Header file for menu related console command callbacks.
installationStatus_t installationStatus
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.
Nation definition.
Definition: cp_nation.h:44
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.
A installation with all it's data.
QGL_EXTERN GLint GLenum type
Definition: r_gl.h:94
static void INS_FillUFOYardData_f(void)
Fills the UI with ufo yard data.
const char * va(const char *format,...)
does a varargs printf into a temp buffer, so I don'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.
#define _(String)
Definition: cl_shared.h:43
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
int numInstallationTemplates
Definition: cp_campaign.h:348
int credits
Definition: cp_campaign.h:242
Header for installation management related stuff.
installationTemplate_t installationTemplates[MAX_INSTALLATION_TEMPLATES]
Definition: cp_campaign.h:347
int day
Definition: common.h:291
date_t date
Definition: cp_campaign.h:245
void INS_SetCurrentSelectedInstallation(const installation_t *installation)
Sets the currently selected installation.
bool GEO_IsRadarOverlayActivated(void)
Definition: cp_geoscape.cpp:84
const char * name
Definition: cp_nation.h:46
const char * UFO_GetName(const aircraft_t *ufocraft)
Returns name of the UFO if UFO has been researched.
Definition: cp_ufo.cpp:243
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.
const char *IMPORT * Cvar_GetString(const char *varName)
#define Q_strvalid(string)
Definition: shared.h:141
int B_GetCount(void)
Returns the count of founded bases.
Definition: cp_base.cpp:276
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.
cvar_t *IMPORT * Cvar_Set(const char *varName, const char *value,...) __attribute__((format(__printf__
#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:473
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.
struct technology_s * tech
mapAction_t mapAction
Definition: cp_campaign.h:260
ccs_t ccs
Definition: cp_campaign.cpp:62
stats_t campaignStats
Definition: cp_campaign.h:378
capacities_t ufoCapacity
Definition: cmd.h:86
static const cmdList_t installationCallbacks[]
Header for Geoscape management.
int installationsBuilt
Definition: cp_statistics.h:34
const installationTemplate_t * installationTemplate
void B_SetCurrentSelectedBase(const base_t *base)
Sets the selected base.
Definition: cp_base.cpp:1553
char name[MAX_VAR]
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:31
installationType_t type
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
QGL_EXTERN GLint i
Definition: r_gl.h:113
installation_t * INS_GetFirstUFOYard(bool free)
returns the first installation with (free) ufostoring capacity
installation_t * INS_GetCurrentSelectedInstallation(void)
Returns the current selected installation.
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.
void INS_ShutdownCallbacks(void)
#define MAX_BASES
Definition: cp_base.h:32
campaign_t * curCampaign
Definition: cp_campaign.h:377
void GEO_SetOverlay(const char *overlayID, int status)
Turn overlay on/off.
const installationTemplate_t * INS_GetInstallationTemplateByID(const char *id)
Returns the installation Template for a given installation ID.
nation_t * GEO_GetNation(const vec2_t pos)
Translate nation map color to nation.
const char *IMPORT * Cmd_Argv(int n)
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's status.
vec2_t newBasePos
Definition: cp_campaign.h:261
installation_t * INS_GetByIDX(int idx)
Get installation by it's index.