UFO: Alien Invasion
Loading...
Searching...
No Matches
aliencargo.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#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
38bool 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
86bool 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
100int 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
117int 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
133int AlienCargo::getAlive(void) const
134{
135 return this->sumAlive;
136}
137
141int 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
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
207{
208}
209
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_DEAD
#define SAVE_ALIENCARGO_ALIVE
#define SAVE_ALIENCARGO_TEAMDEFID
#define SAVE_ALIENCARGO_ITEM
Alien cargo class header.
int getAlive(void) const
Return number of all alive aliens in the cargo.
linkedList_t * cargo
Definition aliencargo.h:43
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.
virtual ~AlienCargo(void)
Destroys AlienCargo with it's internal data.
bool load(xmlNode_t *root)
Load alien cargo from xml savegame.
bool save(xmlNode_t *root) const
Save alien cargo to xml savegame.
AlienCargo(void)
Creates and initializes AlienCargo object.
int getDead(void) const
Return number of all dead bodies in the cargo.
const cgame_import_t * cgi
#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
alien cargo entry
Definition aliencargo.h:32
#define xmlNode_t
Definition xml.h:24