Repository navigation
Expand file tree
/
Copy pathBattleItem.cpp
More file actions
491 lines (442 loc) · 9.75 KB
/
Copy pathBattleItem.cpp
File metadata and controls
491 lines (442 loc) · 9.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*
* Copyright 2010-2012 OpenXcom Developers.
*
* This file is part of OpenXcom.
*
* OpenXcom is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenXcom is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenXcom. If not, see <http://www.gnu.org/licenses/>.
*/
#include "BattleItem.h"
#include "BattleUnit.h"
#include "Tile.h"
#include "../Ruleset/RuleItem.h"
#include "../Ruleset/RuleInventory.h"
namespace OpenXcom
{
/**
* Initializes a item of the specified type.
* @param rules Pointer to ruleset.
*/
BattleItem::BattleItem(RuleItem *rules, int *id) : _id(*id), _rules(rules), _owner(0), _previousOwner(0), _unit(0), _tile(0), _inventorySlot(0), _inventoryX(0), _inventoryY(0), _ammoItem(0), _explodeTurn(0), _ammoQuantity(0), _painKiller(0), _heal(0), _stimulant(0), _XCOMProperty(false)
{
if (_rules && _rules->getBattleType() == BT_AMMO)
{
setAmmoQuantity(_rules->getClipSize());
} else if (_rules && _rules->getBattleType() == BT_MEDIKIT)
{
setHealQuantity (_rules->getHealQuantity ());
setPainKillerQuantity (_rules->getPainKillerQuantity ());
setStimulantQuantity (_rules->getStimulantQuantity ());
}
(*id)++;
// weapon does not need ammo, ammo item points to weapon
if (_rules && _rules->getClipSize() == -1)
{
_ammoItem = this;
setAmmoQuantity(99999);
}
}
/**
*
*/
BattleItem::~BattleItem()
{
}
/**
* Loads the item from a YAML file.
* @param node YAML node.
*/
void BattleItem::load(const YAML::Node &node)
{
//node["inventoryslot"] >> _inventorySlot;
node["inventoryX"] >> _inventoryX;
node["inventoryY"] >> _inventoryY;
node["ammoqty"] >> _ammoQuantity;
node["painKiller"] >> _painKiller;
node["heal"] >> _heal;
node["stimulant"] >> _stimulant;
node["explodeTurn"] >> _explodeTurn;
}
/**
* Saves the item to a YAML file.
* @param out YAML emitter.
*/
void BattleItem::save(YAML::Emitter &out) const
{
out << YAML::BeginMap;
out << YAML::Key << "id" << YAML::Value << _id;
out << YAML::Key << "type" << YAML::Value << _rules->getType();
if (_owner)
{
out << YAML::Key << "owner" << YAML::Value << _owner->getId();
}
else
{
out << YAML::Key << "owner" << YAML::Value << -1;
}
if (_unit)
{
out << YAML::Key << "unit" << YAML::Value << _unit->getId();
}
else
{
out << YAML::Key << "unit" << YAML::Value << -1;
}
if (_inventorySlot)
{
out << YAML::Key << "inventoryslot" << YAML::Value << _inventorySlot->getId();
}
else
{
out << YAML::Key << "inventoryslot" << YAML::Value << "NULL";
}
out << YAML::Key << "inventoryX" << YAML::Value << _inventoryX;
out << YAML::Key << "inventoryY" << YAML::Value << _inventoryY;
out << YAML::Key << "position" << YAML::Value << YAML::Flow;
if (_tile)
{
out << YAML::BeginSeq << _tile->getPosition().x << _tile->getPosition().y << _tile->getPosition().z << YAML::EndSeq;
}
else
{
out << YAML::BeginSeq << -1 << -1 << -1 << YAML::EndSeq;
}
out << YAML::Key << "ammoqty" << YAML::Value << _ammoQuantity;
if (_ammoItem)
{
out << YAML::Key << "ammoItem" << YAML::Value << _ammoItem->getId();
}
else
{
out << YAML::Key << "ammoItem" << YAML::Value << -1;
}
out << YAML::Key << "painKiller" << YAML::Value << _painKiller;
out << YAML::Key << "heal" << YAML::Value << _heal;
out << YAML::Key << "stimulant" << YAML::Value << _stimulant;
out << YAML::Key << "explodeTurn" << YAML::Value << _explodeTurn;
out << YAML::EndMap;
}
/**
* Returns the ruleset for the item's type.
* @return Pointer to ruleset.
*/
RuleItem *BattleItem::getRules() const
{
return _rules;
}
/**
* Returns the turn to explode on. 0 = unprimed grenade
* @return Explode turn.
*/
int BattleItem::getExplodeTurn() const
{
return _explodeTurn;
}
/**
* Set the turn to explode on.
* @param turn Turn to explode on.
*/
void BattleItem::setExplodeTurn(int turn)
{
_explodeTurn = turn;
}
/**
* Returns the quantity of ammo in this item.
* @return Ammo quantity.
*/
int BattleItem::getAmmoQuantity() const
{
return _ammoQuantity;
}
/**
* Changes the quantity of ammo in this item.
* @param qty Ammo quantity.
*/
void BattleItem::setAmmoQuantity(int qty)
{
_ammoQuantity = qty;
}
/**
* Spends a bullet from the ammo in this item.
* @return bool Got bullets left?
*/
bool BattleItem::spendBullet()
{
_ammoQuantity--;
if (_ammoQuantity == 0)
return false;
else
return true;
}
/**
* Gets the item's owner.
* @return pointer to Battleunit
*/
BattleUnit *BattleItem::getOwner() const
{
return _owner;
}
/// Gets the item's previous owner.
BattleUnit *BattleItem::getPreviousOwner() const
{
return _previousOwner;
}
/**
* Sets the item's owner.
* @param owner pointer to Battleunit
*/
void BattleItem::setOwner(BattleUnit *owner)
{
_previousOwner = _owner;
_owner = owner;
}
/**
* Removes the item from previous owner and moves to new owner.
* @param owner pointer to Battleunit
*/
void BattleItem::moveToOwner(BattleUnit *owner)
{
_previousOwner = _owner ? _owner:owner;
_owner = owner;
if (_previousOwner != 0)
{
for (std::vector<BattleItem*>::iterator i = _previousOwner->getInventory()->begin(); i != _previousOwner->getInventory()->end(); ++i)
{
if ((*i) == this)
{
_previousOwner->getInventory()->erase(i);
break;
}
}
}
if (_owner != 0)
{
_owner->getInventory()->push_back(this);
}
}
/**
* Gets the item's inventory slot.
* @return slot id
*/
RuleInventory *BattleItem::getSlot() const
{
return _inventorySlot;
}
/**
* Sets the item's inventory slot.
* @param slot slot id
*/
void BattleItem::setSlot(RuleInventory *slot)
{
_inventorySlot = slot;
}
/**
* Gets the item's inventory X position.
* @return X position.
*/
int BattleItem::getSlotX() const
{
return _inventoryX;
}
/**
* Sets the item's inventory X position.
* @param x X position.
*/
void BattleItem::setSlotX(int x)
{
_inventoryX = x;
}
/**
* Gets the item's inventory Y position.
* @return Y position.
*/
int BattleItem::getSlotY() const
{
return _inventoryY;
}
/**
* Sets the item's inventory Y position.
* @param y Y position.
*/
void BattleItem::setSlotY(int y)
{
_inventoryY = y;
}
/**
* Checks if the item is covering certain inventory slot(s).
* @param x Slot X position.
* @param y Slot Y position.
* @param item Item to check for overlap, or NULL if none.
* @return Whether it's covering or not.
*/
bool BattleItem::occupiesSlot(int x, int y, BattleItem *item) const
{
if (item == this)
return false;
if (_inventorySlot->getType() == INV_HAND)
return true;
if (item == 0)
{
return (x >= _inventoryX && x < _inventoryX + _rules->getInventoryWidth() &&
y >= _inventoryY && y < _inventoryY + _rules->getInventoryHeight());
}
else
{
return !(x >= _inventoryX + _rules->getInventoryWidth() ||
x + item->getRules()->getInventoryWidth() <= _inventoryX ||
y >= _inventoryY + _rules->getInventoryHeight() ||
y + item->getRules()->getInventoryHeight() <= _inventoryY);
}
}
/**
* Gets the item's ammo item.
* @return BattleItem
*/
BattleItem *BattleItem::getAmmoItem()
{
return _ammoItem;
}
/**
* Gets the item's ammo item.
* @return BattleItem
*/
bool BattleItem::needsAmmo() const
{
return !(_ammoItem == this); // no ammo for this weapon is needed
}
/**
* Sets the item's ammo item.
* @param item BattleItem
* @return -2 when ammo doesn't fit, or -1 when weapon already contains ammo
*/
int BattleItem::setAmmoItem(BattleItem *item)
{
if (!needsAmmo()) return -2;
if (item == 0)
{
_ammoItem = 0;
return 0;
}
if (_ammoItem)
return -1;
for (std::vector<std::string>::iterator i = _rules->getCompatibleAmmo()->begin(); i != _rules->getCompatibleAmmo()->end(); ++i)
{
if (*i == item->getRules()->getType())
{
_ammoItem = item;
return 0;
}
}
return -2;
}
/**
* Gets the item's tile.
* @return Tile
*/
Tile *BattleItem::getTile() const
{
return _tile;
}
/**
* Sets the item's tile.
* @param Tile
*/
void BattleItem::setTile(Tile *tile)
{
_tile = tile;
}
int BattleItem::getId() const
{
return _id;
}
/**
* Gets the corpse's unit.
* @return BattleUnit
*/
BattleUnit *BattleItem::getUnit() const
{
return _unit;
}
/**
* Sets the corpse's unit.
* @param unit
*/
void BattleItem::setUnit(BattleUnit *unit)
{
_unit = unit;
}
/**
* Set the heal quantity of item
* @param heal the new heal quantity
*/
void BattleItem::setHealQuantity (int heal)
{
_heal = heal;
}
/**
* Get the heal quantity of item
* @return The new heal quantity
*/
int BattleItem::getHealQuantity () const
{
return _heal;
}
/**
* Set the pain killer quantity of item
* @param pk the new pain killer quantity
*/
void BattleItem::setPainKillerQuantity (int pk)
{
_painKiller = pk;
}
/**
* Get the pain killer quantity of item
* @return The new pain killer quantity
*/
int BattleItem::getPainKillerQuantity () const
{
return _painKiller;
}
/**
* Set the stimulant quantity of item
* @param stimulant the new stimulant quantity
*/
void BattleItem::setStimulantQuantity (int stimulant)
{
_stimulant = stimulant;
}
/**
* Get the stimulant quantity of item
* @return The new stimulant quantity
*/
int BattleItem::getStimulantQuantity () const
{
return _stimulant;
}
/**
* Set the XCOM property flag. This is to determine at debriefing what goes into base/craft
* @param bool
*/
void BattleItem::setXCOMProperty (bool flag)
{
_XCOMProperty = flag;
}
/**
* Get the XCOM property flag. This is to determine at debriefing what goes into base/craft
* @return bool
*/
bool BattleItem::getXCOMProperty () const
{
return _XCOMProperty;
}
}