Repository navigation
Expand file tree
/
Copy pathBattleItem.cpp
More file actions
308 lines (277 loc) · 6.3 KB
/
Copy pathBattleItem.cpp
File metadata and controls
308 lines (277 loc) · 6.3 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
/*
* Copyright 2010 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 "../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) : _rules(rules), _position(-1, -1, -1), _owner(0), _previousOwner(0), _inventorySlot(0), _inventoryX(0), _inventoryY(0), _ammoItem(0), _explodeTurn(0), _ammoQuantity(0)
{
if (_rules->getBattleType() == BT_AMMO)
{
setAmmoQuantity(_rules->getClipSize());
}
}
/**
*
*/
BattleItem::~BattleItem()
{
}
/**
* Loads the item from a YAML file.
* @param node YAML node.
*/
void BattleItem::load(const YAML::Node &node)
{
node["X"] >> _position.x;
node["Y"] >> _position.y;
node["Z"] >> _position.z;
//node["inventoryslot"] >> _inventorySlot;
node["inventoryX"] >> _inventoryX;
node["inventoryY"] >> _inventoryY;
}
/**
* Saves the item to a YAML file.
* @param out YAML emitter.
*/
void BattleItem::save(YAML::Emitter &out) const
{
out << YAML::BeginMap;
out << YAML::Key << "type" << YAML::Value << _rules->getType();
out << YAML::Key << "X" << YAML::Value << _position.x;
out << YAML::Key << "Y" << YAML::Value << _position.y;
out << YAML::Key << "Z" << YAML::Value << _position.z;
if (_owner)
out << YAML::Key << "owner" << YAML::Value << _owner->getId();
out << YAML::Key << "inventoryslot" << YAML::Value << _inventorySlot->getId();
out << YAML::Key << "inventoryX" << YAML::Value << _inventoryX;
out << YAML::Key << "inventoryY" << YAML::Value << _inventoryY;
out << YAML::EndMap;
}
/**
* Returns the ruleset for the item's type.
* @return Pointer to ruleset.
*/
RuleItem *const 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;
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;
}
/**
* 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 (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;
}
}