UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
cp_aliencont.cpp
Go to the documentation of this file.
1 
8 /*
9 Copyright (C) 2002-2020 UFO: Alien Invasion.
10 
11 This program is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License
13 as published by the Free Software Foundation; either version 2
14 of the License, or (at your option) any later version.
15 
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 
20 See the GNU General Public License for more details.
21 
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, 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"
30 #include "cp_aliencont_callbacks.h"
31 #include "save/save_aliencont.h"
32 #include "aliencargo.h"
33 #include "aliencontainment.h"
34 
46 bool 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 
69 void 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 
128 int 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
141 
144 static 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 
178 void 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 
192 bool 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 
227 bool AC_ContainmentAllowed (const base_t* base)
228 {
229  return base->alienContainment != nullptr;
230 }
int capturedAliens
Definition: cp_statistics.h:39
Header file for menu callback functions used for alien containment menu.
int AL_CountAll(void)
Counts live aliens in all bases.
#define FOREACH_XMLNODE(var, node, name)
Definition: cp_save.h:54
static bool isLifeSupported(const teamDef_t *team)
Returns if storing a specific life form is supported by the containment.
bool AC_ContainmentAllowed(const base_t *base)
Returns true if the current base is able to handle captured aliens.
virtual bool add(const teamDef_t *team, int alive, int dead)
Add aliens to the cargo by teamDef.
Definition: aliencargo.cpp:38
#define SAVE_ALIENCONT_ALIEN
#define _(String)
Definition: cl_shared.h:43
#define CAP_Get(base, capacity)
Capacity macros.
Definition: cp_capacity.h:50
linkedList_t * list(void) const
Returns a copy of the cargo list.
Definition: aliencargo.cpp:150
class AlienContainment * alienContainment
Definition: cp_base.h:108
#define SAVE_ALIENCONT_AMOUNTDEAD
void AL_AddAliens(aircraft_t *aircraft)
Puts alien cargo into Alien Containment.
#define SAVE_ALIENCONT_AMOUNTALIVE
#define SAVE_ALIENCONT_CONT
char name[MAX_VAR]
Definition: cp_aircraft.h:120
#define SAVE_ALIENCONT_BASEIDX
Alien containment class.
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.
Definition: cp_messages.cpp:61
A base with all it's data.
Definition: cp_base.h:84
base_t * B_GetFoundedBaseByIDX(int baseIdx)
Array bound check for the base index.
Definition: cp_base.cpp:325
#define xmlNode_t
Definition: xml.h:24
alien cargo entry
Definition: aliencargo.h:32
bool AL_AddAlienTypeToAircraftCargo(aircraft_t *aircraft, const teamDef_t *teamDef, int amount, bool dead)
Adds an alientype to an aircraft cargo.
int killedAliens
Definition: cp_statistics.h:38
base_t * B_GetNext(base_t *lastBase)
Iterates through founded bases.
Definition: cp_base.cpp:285
const cgame_import_t * cgi
int idx
Definition: cp_base.h:85
XML tag constants for savegame.
ccs_t ccs
Definition: cp_campaign.cpp:62
stats_t campaignStats
Definition: cp_campaign.h:378
void CP_TriggerEvent(campaignTriggerEventType_t type, const void *userdata)
Triggers a campaign event with a special type.
Definition: cp_event.cpp:311
#define SAVE_ALIENCONT_TEAMID
Alien cargo class.
Definition: aliencargo.h:41
void AC_InitStartup(void)
Defines commands and cvars for the alien containment menu(s).
Alien containment class header.
struct base_s * homebase
Definition: cp_aircraft.h:149
virtual bool add(const teamDef_t *team, int alive, int dead)
Add aliens to the containment by teamDef.
Alien cargo class header.
#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
Header file for single player campaign control.
int getAlive(const teamDef_t *team) const
Return number of alive aliens of a type in the cargo.
Definition: aliencargo.cpp:100
void AC_InitCallbacks(void)
#define SAVE_ALIENCONT_ALIENCONT
An aircraft with all it's data.
Definition: cp_aircraft.h:114
#define MAX_BASES
Definition: cp_base.h:32
class AlienCargo * alienCargo
Definition: cp_aircraft.h:176
const char *IMPORT * Cmd_Argv(int n)
const char *IMPORT * XML_GetString(xmlNode_t *parent, const char *name)
xmlNode_t *IMPORT * XML_GetNode(xmlNode_t *parent, const char *name)
bool AC_LoadXML(xmlNode_t *parent)
Load callback for savin in XML Format.