forked from miki151/keeperrl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
equipment.cpp
95 lines (76 loc) · 2.71 KB
/
equipment.cpp
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
/* Copyright (C) 2013-2014 Michal Brzozowski (rusolis@poczta.fm)
This file is part of KeeperRL.
KeeperRL 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 2
of the License, or (at your option) any later version.
KeeperRL 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 this program.
If not, see http://www.gnu.org/licenses/ . */
#include "stdafx.h"
#include "equipment.h"
#include "item.h"
map<EquipmentSlot, string> Equipment::slotTitles = {
{EquipmentSlot::WEAPON, "Weapon"},
{EquipmentSlot::GLOVES, "Gloves"},
{EquipmentSlot::RANGED_WEAPON, "Ranged weapon"},
{EquipmentSlot::HELMET, "Helmet"},
{EquipmentSlot::BODY_ARMOR, "Body armor"},
{EquipmentSlot::BOOTS, "Boots"},
{EquipmentSlot::RINGS, "Rings"},
{EquipmentSlot::AMULET, "Amulet"}};
template <class Archive>
void Equipment::serialize(Archive& ar, const unsigned int version) {
ar & SUBCLASS(Inventory) & SVAR(items);
}
SERIALIZABLE(Equipment);
SERIALIZATION_CONSTRUCTOR_IMPL(Equipment);
vector<Item*> Equipment::getItem(EquipmentSlot slot) const {
if (items.count(slot) > 0)
return items.at(slot);
else
return {};
}
bool Equipment::isEquiped(const Item* item) const {
if (!item->canEquip())
return false;
EquipmentSlot slot = item->getEquipmentSlot();
return items.count(slot) && contains(items.at(slot), item);
}
int Equipment::getMaxItems(EquipmentSlot slot) const {
switch (slot) {
case EquipmentSlot::RINGS: return 2;
default: return 1;
}
}
bool Equipment::canEquip(const Item* item) const {
if (!item->canEquip() || isEquiped(item))
return false;
EquipmentSlot slot = item->getEquipmentSlot();
return !items.count(slot) || items.at(slot).size() < getMaxItems(slot);
}
void Equipment::equip(Item* item, EquipmentSlot slot) {
items[slot].push_back(item);
CHECK(hasItem(item));
}
void Equipment::unequip(const Item* item) {
EquipmentSlot slot = item->getEquipmentSlot();
CHECK(items.count(slot));
removeElement(items.at(slot), item);
}
PItem Equipment::removeItem(Item* item) {
if (isEquiped(item))
unequip(item);
return Inventory::removeItem(item);
}
vector<PItem> Equipment::removeItems(const vector<Item*>& items) {
vector<PItem> ret;
for (Item* it : items)
ret.push_back(removeItem(it));
return ret;
}
vector<PItem> Equipment::removeAllItems() {
items.clear();
return Inventory::removeAllItems();
}