UFO: Alien Invasion
Loading...
Searching...
No Matches
cp_aliencont.cpp
Go to the documentation of this file.
1
7
8/*
9Copyright (C) 2002-2025 UFO: Alien Invasion.
10
11This program is free software; you can redistribute it and/or
12modify it under the terms of the GNU General Public License
13as published by the Free Software Foundation; either version 2
14of the License, or (at your option) any later version.
15
16This program is distributed in the hope that it will be useful,
17but WITHOUT ANY WARRANTY; without even the implied warranty of
18MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19
20See the GNU General Public License for more details.
21
22You should have received a copy of the GNU General Public License
23along with this program; if not, write to the Free Software
24Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25*/
26
27#include "../../cl_shared.h"
28#include "cp_campaign.h"
29#include "cp_capacity.h"
31#include "save/save_aliencont.h"
32#include "aliencargo.h"
33#include "aliencontainment.h"
34
38
46bool AL_AddAlienTypeToAircraftCargo (aircraft_t* aircraft, const teamDef_t* teamDef, int amount, bool dead)
47{
48 if (aircraft->alienCargo == nullptr)
49 aircraft->alienCargo = new AlienCargo();
50 if (aircraft->alienCargo == nullptr)
51 return false;
52
53 if (dead)
54 return aircraft->alienCargo->add(teamDef, 0, amount);
55 return aircraft->alienCargo->add(teamDef, amount, 0);
56}
57
61
69void AL_AddAliens (aircraft_t* aircraft)
70{
71 if (!aircraft)
72 return;
73 if (!aircraft->alienCargo)
74 return;
75 if (!aircraft->homebase) {
76 cgi->Com_Printf("AL_AddAliens: Aircraft %s (idx: %d) has no base, alienCargo destroyed\n", aircraft->name, aircraft->idx);
77 delete aircraft->alienCargo;
78 aircraft->alienCargo = nullptr;
79 return;
80 }
81
82 if (aircraft->homebase->alienContainment == nullptr)
83 aircraft->homebase->alienContainment = new AlienContainment(CAP_Get(aircraft->homebase, CAP_ALIENS), nullptr);
84
85 AlienContainment* cont = aircraft->homebase->alienContainment;
86 if (!cont)
87 return;
88
89 bool messageSent = false;
90 linkedList_t* cargo = aircraft->alienCargo->list();
91 LIST_Foreach(cargo, alienCargo_t, item) {
92 const bool lifeSupported = AlienContainment::isLifeSupported(item->teamDef);
93
94 if (!lifeSupported) {
95 cont->add(item->teamDef, 0, item->alive + item->dead);
96 aircraft->alienCargo->add(item->teamDef, -item->alive, -item->dead);
97
98 ccs.campaignStats.killedAliens += item->dead + item->alive;
99 if (item->alive > 0) {
101 /* only once */
102 if (!messageSent) {
103 MS_AddNewMessage(_("Notice"), _("You can't hold live aliens yet. Aliens died."), MSG_DEATH);
104 messageSent = true;
105 }
106 }
107 } else {
108 cont->add(item->teamDef, item->alive, item->dead);
109 aircraft->alienCargo->add(item->teamDef, -item->alive, -item->dead);
110
111 ccs.campaignStats.killedAliens += item->dead;
112 ccs.campaignStats.capturedAliens += item->alive;
113 if (item->alive > 0) {
115 if (!messageSent) {
116 MS_AddNewMessage(_("Notice"), _("You've captured new aliens."));
117 messageSent = true;
118 }
119 }
120 }
121 }
122 cgi->LIST_Delete(&cargo);
123}
124
128int AL_CountAll (void)
129{
130 int amount = 0;
131 base_t* base = nullptr;
132
133 while ((base = B_GetNext(base)) != nullptr) {
134 if (base->alienContainment)
135 amount += base->alienContainment->getAlive();
136 }
137 return amount;
138}
139
140#ifdef DEBUG
144static void AC_AddOne_f (void)
145{
146 if (cgi->Cmd_Argc() < 3) {
147 cgi->Com_Printf("Usage: %s <baseIDX> <alientype> [dead:true|false]\n", cgi->Cmd_Argv(0));
148 return;
149 }
150 base_t* base = B_GetFoundedBaseByIDX(atoi(cgi->Cmd_Argv(1)));
151 if (!base) {
152 cgi->Com_Printf("%s: Invalid base idx: %s\n", cgi->Cmd_Argv(0), cgi->Cmd_Argv(1));
153 return;
154 }
155 if (!base->alienContainment) {
156 cgi->Com_Printf("%s: B base %d has no alien containment\n", cgi->Cmd_Argv(0), base->idx);
157 return;
158 }
159 const char* alienName = cgi->Cmd_Argv(2);
160 if (!alienName)
161 return;
162
163 bool updateAlive = true;
164 if (cgi->Cmd_Argc() == 4)
165 updateAlive = cgi->Com_ParseBoolean(cgi->Cmd_Argv(3));
166
167 if (updateAlive)
168 base->alienContainment->add(alienName, 1, 0);
169 else
170 base->alienContainment->add(alienName, 0, 1);
171}
172#endif
173
178void AC_InitStartup (void)
179{
180 /* add commands */
181#ifdef DEBUG
182 cgi->Cmd_AddCommand("debug_addalientocont", AC_AddOne_f, "Add one alien of a given type");
183#endif
185}
186
192bool AC_LoadXML (xmlNode_t* parent)
193{
194 xmlNode_t* aliencont = cgi->XML_GetNode(parent, SAVE_ALIENCONT_ALIENCONT);
195 if (!aliencont)
196 return true;
197 FOREACH_XMLNODE(contNode, aliencont, SAVE_ALIENCONT_CONT) {
198 const int baseIdx = cgi->XML_GetInt(contNode, SAVE_ALIENCONT_BASEIDX, MAX_BASES);
199 base_t* base = B_GetFoundedBaseByIDX(baseIdx);
200 if (!base) {
201 cgi->Com_Printf("AC_LoadXML: Invalid base idx '%i'\n", baseIdx);
202 continue;
203 }
204
205 FOREACH_XMLNODE(alienNode, contNode, SAVE_ALIENCONT_ALIEN) {
206 const char* teamId = cgi->XML_GetString(alienNode, SAVE_ALIENCONT_TEAMID);
207 const int alive = cgi->XML_GetInt(alienNode, SAVE_ALIENCONT_AMOUNTALIVE, 0);
208 const int dead = cgi->XML_GetInt(alienNode, SAVE_ALIENCONT_AMOUNTDEAD, 0);
209
210 if (alive == 0 && dead == 0)
211 continue;
212
213 if (!base->alienContainment)
214 base->alienContainment = new AlienContainment(CAP_Get(base, CAP_ALIENS), nullptr);
215
216 base->alienContainment->add(teamId, alive, dead);
217 }
218 }
219
220 return true;
221}
222
228{
229 return base->alienContainment != nullptr;
230}
Alien cargo class header.
Alien containment class header.
Share stuff between the different cgame implementations.
#define _(String)
Definition cl_shared.h:44
Alien cargo class.
Definition aliencargo.h:41
virtual bool add(const teamDef_t *team, int alive, int dead)
Add aliens to the cargo by teamDef.
linkedList_t * list(void) const
Returns a copy of the cargo list.
int getAlive(const teamDef_t *team) const
Return number of alive aliens of a type in the cargo.
Alien containment class.
static bool isLifeSupported(const teamDef_t *team)
Returns if storing a specific life form is supported by the containment.
virtual bool add(const teamDef_t *team, int alive, int dead)
Add aliens to the containment by teamDef.
void AL_AddAliens(aircraft_t *aircraft)
Puts alien cargo into Alien Containment.
bool AC_LoadXML(xmlNode_t *parent)
Load callback for savin in XML Format.
bool AC_ContainmentAllowed(const base_t *base)
Returns true if the current base is able to handle captured aliens.
void AC_InitStartup(void)
Defines commands and cvars for the alien containment menu(s).
int AL_CountAll(void)
Counts live aliens in all bases.
bool AL_AddAlienTypeToAircraftCargo(aircraft_t *aircraft, const teamDef_t *teamDef, int amount, bool dead)
Adds an alientype to an aircraft cargo.
void AC_InitCallbacks(void)
Header file for menu callback functions used for alien containment menu.
base_t * B_GetFoundedBaseByIDX(int baseIdx)
Array bound check for the base index.
Definition cp_base.cpp:326
base_t * B_GetNext(base_t *lastBase)
Iterates through founded bases.
Definition cp_base.cpp:286
#define MAX_BASES
Definition cp_base.h:32
ccs_t ccs
Header file for single player campaign control.
const cgame_import_t * cgi
@ CAP_ALIENS
Definition cp_capacity.h:28
#define CAP_Get(base, capacity)
Capacity macros.
Definition cp_capacity.h:50
void CP_TriggerEvent(campaignTriggerEventType_t type, const void *userdata)
Triggers a campaign event with a special type.
Definition cp_event.cpp:310
@ CAPTURED_ALIENS
Definition cp_event.h:82
@ CAPTURED_ALIENS_DIED
Definition cp_event.h:81
uiMessageListNodeMessage_t * MS_AddNewMessage(const char *title, const char *text, messageType_t type, technology_t *pedia, bool popup, bool playSound)
Adds a new message to message stack.
@ MSG_DEATH
Definition cp_messages.h:48
#define FOREACH_XMLNODE(var, node, name)
Definition cp_save.h:54
#define LIST_Foreach(list, type, var)
Iterates over a linked list, it's safe to delete the returned entry from the list while looping over ...
Definition list.h:41
XML tag constants for savegame.
#define SAVE_ALIENCONT_AMOUNTALIVE
#define SAVE_ALIENCONT_ALIENCONT
#define SAVE_ALIENCONT_CONT
#define SAVE_ALIENCONT_TEAMID
#define SAVE_ALIENCONT_BASEIDX
#define SAVE_ALIENCONT_AMOUNTDEAD
#define SAVE_ALIENCONT_ALIEN
An aircraft with all it's data.
class AlienCargo * alienCargo
struct base_s * homebase
char name[MAX_VAR]
alien cargo entry
Definition aliencargo.h:32
A base with all it's data.
Definition cp_base.h:84
class AlienContainment * alienContainment
Definition cp_base.h:108
int idx
Definition cp_base.h:85
#define xmlNode_t
Definition xml.h:24