UFO: Alien Invasion
Loading...
Searching...
No Matches
itemcargo.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 "itemcargo.h"
26
27#define SAVE_ITEMCARGO_ITEM "item"
28#define SAVE_ITEMCARGO_ITEMID "itemid"
29#define SAVE_ITEMCARGO_AMOUNT "amount"
30#define SAVE_ITEMCARGO_LOOSEAMOUNT "looseamount"
31
39bool ItemCargo::add(const objDef_t* od, int amount, int looseAmount = 0)
40{
41 if (!od)
42 return false;
43 if (amount == 0 && looseAmount == 0)
44 return true;
45
46 LIST_Foreach(this->cargo, itemCargo_t, item) {
47 if (item->objDef != od)
48 continue;
49
50 if (amount + item->amount < 0)
51 return false;
52 if (looseAmount + item->looseAmount < 0)
53 return false;
54
55 item->amount += amount;
56 item->looseAmount += looseAmount;
57 if (od->ammo > 0 && item->looseAmount >= od->ammo) {
58 const int magazine = item->looseAmount / od->ammo;
59 this->add(od, magazine, -magazine * od->ammo);
60 }
61
62 if (item->amount == 0 && item->looseAmount == 0)
63 cgi->LIST_Remove(&this->cargo, (void*)item);
64
65 return true;
66 }
67
68 if (amount < 0 || looseAmount < 0)
69 return false;
70
71 itemCargo_t cargoItem = { od, amount, looseAmount };
72
73 if (cgi->LIST_Add(&this->cargo, (const void*)&cargoItem, sizeof(cargoItem))) {
74 return true;
75 }
76
77 return false;
78}
79
86bool ItemCargo::add(const char* objDefId, int amount, int looseAmount = 0)
87{
88 if (!objDefId)
89 return false;
90 const objDef_t* od = INVSH_GetItemByIDSilent(objDefId);
91 if (!od)
92 return false;
93 return this->add(od, amount, looseAmount);
94}
95
100{
101 cgi->LIST_Delete(&(this->cargo));
102}
103
107bool ItemCargo::isEmpty (void) const
108{
109 return (this->cargo == nullptr);
110}
111
118{
119 LIST_Foreach(this->cargo, itemCargo_t, item) {
120 if (item->objDef == od)
121 return item;
122 }
123 return nullptr;
124}
125
131int ItemCargo::getAmount(const objDef_t* od) const
132{
133 const itemCargo_t* const item = this->get(od);
134 if (item == nullptr)
135 return 0;
136 return item->amount;
137}
138
145{
146 const itemCargo_t* const item = this->get(od);
147 if (item == nullptr)
148 return 0;
149 return item->looseAmount;
150}
151
157{
158 linkedList_t* listing = nullptr;
159
160 LIST_Foreach(this->cargo, itemCargo_t, item) {
161 if (!cgi->LIST_Add(&listing, (void*)item, sizeof(*item))) {
162 cgi->LIST_Delete(&listing);
163 return nullptr;
164 }
165 }
166 return listing;
167}
168
172int ItemCargo::count(void) const
173{
174 int count = 0;
175 LIST_Foreach(this->cargo, itemCargo_t, item) {
176 count += item->amount;
177 }
178 return count;
179}
180
184int ItemCargo::size(void) const
185{
186 int size = 0;
187 LIST_Foreach(this->cargo, itemCargo_t, item) {
188 size += item->amount * item->objDef->size;
189 }
190 return size;
191}
192
198{
199 if (!root)
200 return false;
201
202 for (xmlNode_t* itemNode = cgi->XML_GetNode(root, SAVE_ITEMCARGO_ITEM); itemNode;
203 itemNode = cgi->XML_GetNextNode(itemNode, root, SAVE_ITEMCARGO_ITEM))
204 {
205 const char* objDefId = cgi->XML_GetString(itemNode, SAVE_ITEMCARGO_ITEMID);
206 const int amount = cgi->XML_GetInt(itemNode, SAVE_ITEMCARGO_AMOUNT, 0);
207 const int looseAmount = cgi->XML_GetInt(itemNode, SAVE_ITEMCARGO_LOOSEAMOUNT, 0);
208 if (!add(objDefId, amount, looseAmount))
209 cgi->Com_Printf("ItemCargo::load: Could add items to cargo: %s, %d, %d\n", objDefId, amount, looseAmount);
210 }
211 return true;
212}
213
218bool ItemCargo::save(xmlNode_t* root) const
219{
220 if (!root)
221 return false;
222 LIST_Foreach(this->cargo, itemCargo_t, item) {
223 xmlNode_t* itemNode = cgi->XML_AddNode(root, SAVE_ITEMCARGO_ITEM);
224 if (!itemNode)
225 return false;
226 cgi->XML_AddString(itemNode, SAVE_ITEMCARGO_ITEMID, item->objDef->id);
227 cgi->XML_AddIntValue(itemNode, SAVE_ITEMCARGO_AMOUNT, item->amount);
228 cgi->XML_AddIntValue(itemNode, SAVE_ITEMCARGO_LOOSEAMOUNT, item->looseAmount);
229 }
230 return true;
231}
232
239
245{
246 linkedList_t* list = itemCargo.list();
247
248 LIST_Foreach(list, itemCargo_t, cargoItem) {
249 cgi->LIST_Add(&this->cargo, (void*)cargoItem, sizeof(*cargoItem));
250 }
251 cgi->LIST_Delete(&list);
252}
253
258{
259 cgi->LIST_Delete(&this->cargo);
260}
virtual ~ItemCargo(void)
Destroys ItemCargo with it's internal data.
int size(void) const
Calculate size of all items in the cargo.
linkedList_t * cargo
Definition itemcargo.h:43
void empty(void)
Empties the cargo.
Definition itemcargo.cpp:99
bool load(xmlNode_t *root)
Load item cargo from xml savegame.
ItemCargo(void)
Creates and initializes ItemCargo object.
bool save(xmlNode_t *root) const
Save item cargo to xml savegame.
bool isEmpty(void) const
Checks if the cargo is empty.
int count(void) const
Count all items in the cargo.
itemCargo_t * get(const objDef_t *od) const
Returns a cargo item by its object definition.
int getLooseAmount(const objDef_t *od) const
Returns amount of loose item in the cargo.
linkedList_t * list(void) const
Returns a copy of the cargo list.
virtual bool add(const objDef_t *od, int amount, int looseAmount)
Add items to the cargo.
Definition itemcargo.cpp:39
int getAmount(const objDef_t *od) const
Returns amount of an item in the cargo.
const cgame_import_t * cgi
#define nullptr
Definition cxx.h:53
const objDef_t * INVSH_GetItemByIDSilent(const char *id)
Returns the item that belongs to the given id or nullptr if it wasn't found.
#define SAVE_ITEMCARGO_ITEM
Definition itemcargo.cpp:27
#define SAVE_ITEMCARGO_LOOSEAMOUNT
Definition itemcargo.cpp:30
#define SAVE_ITEMCARGO_AMOUNT
Definition itemcargo.cpp:29
#define SAVE_ITEMCARGO_ITEMID
Definition itemcargo.cpp:28
Item 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
item cargo entry
Definition itemcargo.h:32
int looseAmount
Definition itemcargo.h:35
Defines all attributes of objects used in the inventory.
Definition inv_shared.h:264
#define xmlNode_t
Definition xml.h:24