UFO: Alien Invasion
Loading...
Searching...
No Matches
cp_installation_callbacks.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
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{
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);
63 ccs.mapAction = MA_NONE;
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
87static 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 }
127 if (ccs.mapAction == MA_NEWINSTALLATION)
128 ccs.mapAction = MA_NONE;
129
130 CP_Popup(_("Notice"), _("Not enough credits to set up a new installation."));
131 }
132 ccs.mapAction = MA_NONE;
133}
134
139static 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
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
214static 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) {
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
253static void INS_FillTypes_f (void)
254{
255 cgi->UI_ExecuteConfunc("installationtype_clear");
257 for (int i = 0; i < ccs.numInstallationTemplates; i++) {
258 const installationTemplate_t* tpl = &ccs.installationTemplates[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
276static void INS_SelectType_f (void)
277{
278 if (cgi->Cmd_Argc() < 2)
279 return;
280
281 const char* id = cgi->Cmd_Argv(1);
282
283 if (ccs.mapAction == MA_NEWINSTALLATION) {
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
309 ccs.mapAction = MA_NEWINSTALLATION;
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};
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}
DateTime class definition.
Share stuff between the different cgame implementations.
#define _(String)
Definition cl_shared.h:44
void B_SetCurrentSelectedBase(const base_t *base)
Sets the selected base.
Definition cp_base.cpp:1553
int B_GetCount(void)
Returns the count of founded bases.
Definition cp_base.cpp:277
int B_GetInstallationLimit(void)
Counts the actual installation count limit.
Definition cp_base.cpp:1153
#define MAX_BASES
Definition cp_base.h:32
void CP_UpdateCredits(int credits)
Sets credits and update mn_credits cvar.
ccs_t ccs
Header file for single player campaign control.
const cgame_import_t * cgi
@ MA_NONE
@ MA_NEWINSTALLATION
nation_t * GEO_GetNation(const vec2_t pos)
Translate nation map color to nation.
void GEO_SetOverlay(const char *overlayID, int status)
Turn overlay on/off.
bool GEO_IsRadarOverlayActivated(void)
void GEO_ResetAction(void)
No more special action on the geoscape.
Header for Geoscape management.
void INS_DestroyInstallation(installation_t *installation)
Destroys an installation.
installation_t * INS_Build(const installationTemplate_t *installationTemplate, const vec2_t pos, const char *name)
Build a new installation.
const installationTemplate_t * INS_GetInstallationTemplateByType(installationType_t type)
Returns the installation Template for a given installation type.
const installationTemplate_t * INS_GetInstallationTemplateByID(const char *id)
Returns the installation Template for a given installation ID.
installation_t * INS_GetCurrentSelectedInstallation(void)
Returns the current selected installation.
void INS_SetCurrentSelectedInstallation(const installation_t *installation)
Sets the currently selected installation.
installation_t * INS_GetFirstUFOYard(bool free)
returns the first installation with (free) ufostoring capacity
int INS_GetCount(void)
Get number of installations.
installation_t * INS_GetByIDX(int idx)
Get installation by it's index.
bool INS_HasType(installationType_t type, installationStatus_t status)
Checks whether the given installation type is available.
Header for installation management related stuff.
installationType_t
@ INSTALLATION_UFOYARD
@ INSTALLATION_DEFENCE
@ INSTALLATION_RADAR
@ INSTALLATION_UNDER_CONSTRUCTION
@ INSTALLATION_NOT_USED
@ INSTALLATION_WORKING
static void INS_UpdateInstallationLimit_f(void)
updates the installation limit cvar for menus
static void INS_ChangeInstallationName_f(void)
Creates console command to change the name of a installation. Copies the value of the cvar mn_install...
static void INS_FillUFOYardData_f(void)
Fills the UI with ufo yard data.
static void INS_SelectInstallation_f(void)
Called when an installation is opened or a new installation is created on geoscape....
static const cmdList_t installationCallbacks[]
void INS_InitCallbacks(void)
static void INS_SetInstallationTitle(installationType_t type)
Sets the title of the installation to a cvar to prepare the rename menu.
void INS_SelectInstallation(installation_t *installation)
Select an installation when clicking on it on geoscape.
static void INS_FillTypes_f(void)
Fills create installation / installation type selection popup.
static void INS_DestroyInstallation_f(void)
console function for destroying an installation
static void INS_SelectType_f(void)
Selects installation type to build.
void INS_ShutdownCallbacks(void)
static void INS_BuildInstallation_f(void)
Constructs a new installation.
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...
@ NT_INSTALLATION_BUILDSTART
char cp_messageBuffer[MAX_MESSAGE_TEXT]
@ MSG_CONSTRUCTION
Definition cp_messages.h:39
void CP_Popup(const char *title, const char *text,...)
Wrapper around UI_Popup.
Definition cp_popup.cpp:474
bool RS_IsResearched_ptr(const technology_t *tech)
Checks whether an item is already researched.
const char * UFO_GetName(const aircraft_t *ufocraft)
Returns name of the UFO if UFO has been researched.
Definition cp_ufo.cpp:243
#define US_Foreach(var)
const char * US_StoredUFOStatus(const storedUFO_t *ufo)
Returns string representation of the stored UFO's status.
UFO recovery and storing callback header file.
#define DEBUG_CLIENT
Definition defines.h:59
#define ngettext(x, y, cnt)
Definition g_local.h:40
QGL_EXTERN GLint i
Definition r_gl.h:113
QGL_EXTERN GLint GLenum type
Definition r_gl.h:94
#define Q_strvalid(string)
Definition shared.h:141
#define MAX_VAR
Definition shared.h:36
void Q_strncpyz(char *dest, const char *src, size_t destsize)
Safe strncpy that ensures a trailing zero.
Definition shared.cpp:457
bool Com_sprintf(char *dest, size_t size, const char *fmt,...)
copies formatted string with buffer-size checking
Definition shared.cpp:494
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
A installation with all it's data.
char name[MAX_VAR]
const installationTemplate_t * installationTemplate
capacities_t ufoCapacity
installationStatus_t installationStatus
struct technology_s * tech
installationType_t type
Nation definition.
Definition cp_nation.h:46
const char * name
Definition cp_nation.h:48
@ TEXT_BUILDING_INFO
Definition ui_dataids.h:36