UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
aliencargo.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 #include "aliencargo.h"
26 
27 #define SAVE_ALIENCARGO_ITEM "cargo"
28 #define SAVE_ALIENCARGO_TEAMDEFID "teamdefid"
29 #define SAVE_ALIENCARGO_ALIVE "alive"
30 #define SAVE_ALIENCARGO_DEAD "dead"
31 
38 bool AlienCargo::add(const teamDef_t* team, int alive, int dead)
39 {
40  if (!team)
41  return false;
42  if (alive == 0 && dead == 0)
43  return true;
44 
45  LIST_Foreach(this->cargo, alienCargo_t, item) {
46  if (item->teamDef != team)
47  continue;
48 
49  if (alive + item->alive < 0)
50  return false;
51  if (dead + item->dead < 0)
52  return false;
53 
54  item->alive += alive;
55  item->dead += dead;
56 
57  this->sumAlive += alive;
58  this->sumDead += dead;
59 
60  if (item->alive == 0 && item->dead == 0)
61  cgi->LIST_Remove(&this->cargo, (void*)item);
62 
63  return true;
64  }
65 
66  if (alive < 0 || dead < 0)
67  return false;
68 
69  const alienCargo_t cargoItem = { team, alive, dead };
70 
71  if (cgi->LIST_Add(&this->cargo, (const void*)&cargoItem, sizeof(cargoItem))) {
72  this->sumAlive += alive;
73  this->sumDead += dead;
74  return true;
75  }
76 
77  return false;
78 }
79 
86 bool AlienCargo::add(const char* teamId, int alive, int dead)
87 {
88  if (!teamId)
89  return false;
90  const teamDef_t* team = cgi->Com_GetTeamDefinitionByID(teamId);
91  if (!team)
92  return false;
93  return this->add(team, alive, dead);
94 }
95 
100 int AlienCargo::getAlive(const teamDef_t* team) const
101 {
102  if (!team)
103  return -1;
104 
105  LIST_Foreach(this->cargo, alienCargo_t, item) {
106  if (item->teamDef != team)
107  continue;
108  return item->alive;
109  }
110  return 0;
111 }
112 
117 int AlienCargo::getDead(const teamDef_t* team) const
118 {
119  if (!team)
120  return -1;
121 
122  LIST_Foreach(this->cargo, alienCargo_t, item) {
123  if (item->teamDef != team)
124  continue;
125  return item->dead;
126  }
127  return 0;
128 }
129 
133 int AlienCargo::getAlive(void) const
134 {
135  return this->sumAlive;
136 }
137 
141 int AlienCargo::getDead(void) const
142 {
143  return this->sumDead;
144 }
145 
151 {
152  linkedList_t* listing = 0;
153 
154  LIST_Foreach(this->cargo, alienCargo_t, item) {
155  if (!cgi->LIST_Add(&listing, (void*)item, sizeof(*item))) {
156  cgi->LIST_Delete(&listing);
157  return 0;
158  }
159  }
160  return listing;
161 }
162 
168 {
169  if (!root)
170  return false;
171 
172  for (xmlNode_t* alienNode = cgi->XML_GetNode(root, SAVE_ALIENCARGO_ITEM); alienNode;
173  alienNode = cgi->XML_GetNextNode(alienNode, root, SAVE_ALIENCARGO_ITEM))
174  {
175  const char* teamId = cgi->XML_GetString(alienNode, SAVE_ALIENCARGO_TEAMDEFID);
176  const int alive = cgi->XML_GetInt(alienNode, SAVE_ALIENCARGO_ALIVE, 0);
177  const int dead = cgi->XML_GetInt(alienNode, SAVE_ALIENCARGO_DEAD, 0);
178  if (!add(teamId, alive, dead))
179  cgi->Com_Printf("AlienCargo::load: Could add aliens to cargo: %s, %d, %d\n", teamId, alive, dead);
180  }
181  return true;
182 }
183 
188 bool AlienCargo::save(xmlNode_t* root) const
189 {
190  if (!root)
191  return false;
192  LIST_Foreach(this->cargo, alienCargo_t, item) {
193  xmlNode_t* alienNode = cgi->XML_AddNode(root, SAVE_ALIENCARGO_ITEM);
194  if (!alienNode)
195  return false;
196  cgi->XML_AddString(alienNode, SAVE_ALIENCARGO_TEAMDEFID, item->teamDef->id);
197  cgi->XML_AddIntValue(alienNode, SAVE_ALIENCARGO_ALIVE, item->alive);
198  cgi->XML_AddIntValue(alienNode, SAVE_ALIENCARGO_DEAD, item->dead);
199  }
200  return true;
201 }
202 
206 AlienCargo::AlienCargo(void) : cargo (0), sumAlive (0), sumDead(0)
207 {
208 }
209 
214 AlienCargo::AlienCargo(AlienCargo& alienCargo) : cargo (0), sumAlive (0), sumDead(0)
215 {
216  linkedList_t* list = alienCargo.list();
217 
218  LIST_Foreach(list, alienCargo_t, cargoItem) {
219  if (cgi->LIST_Add(&this->cargo, (void*)cargoItem, sizeof(*cargoItem))) {
220  this->sumAlive += cargoItem->alive;
221  this->sumDead += cargoItem->dead;
222  }
223  }
224  cgi->LIST_Delete(&list);
225 }
226 
231 {
232  cgi->LIST_Delete(&this->cargo);
233 }
#define SAVE_ALIENCARGO_ITEM
Definition: aliencargo.cpp:27
virtual bool add(const teamDef_t *team, int alive, int dead)
Add aliens to the cargo by teamDef.
Definition: aliencargo.cpp:38
#define SAVE_ALIENCARGO_DEAD
Definition: aliencargo.cpp:30
int sumDead
Definition: aliencargo.h:45
int getDead(void) const
Return number of all dead bodies in the cargo.
Definition: aliencargo.cpp:141
#define SAVE_ALIENCARGO_ALIVE
Definition: aliencargo.cpp:29
linkedList_t * list(void) const
Returns a copy of the cargo list.
Definition: aliencargo.cpp:150
#define xmlNode_t
Definition: xml.h:24
linkedList_t * cargo
Definition: aliencargo.h:43
virtual ~AlienCargo(void)
Destroys AlienCargo with it's internal data.
Definition: aliencargo.cpp:230
int sumAlive
Definition: aliencargo.h:44
alien cargo entry
Definition: aliencargo.h:32
const cgame_import_t * cgi
Alien cargo class.
Definition: aliencargo.h:41
AlienCargo(void)
Creates and initializes AlienCargo object.
Definition: aliencargo.cpp:206
int getAlive(void) const
Return number of all alive aliens in the cargo.
Definition: aliencargo.cpp:133
#define SAVE_ALIENCARGO_TEAMDEFID
Definition: aliencargo.cpp:28
xmlNode_t *IMPORT * XML_GetNextNode(xmlNode_t *current, xmlNode_t *parent, const char *name)
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
bool load(xmlNode_t *root)
Load alien cargo from xml savegame.
Definition: aliencargo.cpp:167
xmlNode_t *IMPORT * XML_AddNode(xmlNode_t *parent, const char *name)
const teamDef_t *IMPORT * Com_GetTeamDefinitionByID(const char *team)
bool save(xmlNode_t *root) const
Save alien cargo to xml savegame.
Definition: aliencargo.cpp:188
linkedList_t *IMPORT * LIST_Add(linkedList_t **list, void const *data, size_t length)
const char *IMPORT * XML_GetString(xmlNode_t *parent, const char *name)
xmlNode_t *IMPORT * XML_GetNode(xmlNode_t *parent, const char *name)