forked from miki151/keeperrl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inventory.cpp
148 lines (123 loc) · 4.57 KB
/
inventory.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
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
/* Copyright (C) 2013-2014 Michal Brzozowski ([email protected])
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 "inventory.h"
#include "item.h"
#include "minion_equipment.h"
#include "resource_id.h"
template <class Archive>
void Inventory::serialize(Archive& ar, const unsigned int version) {
ar& SVAR(items)
& SVAR(itemsCache);
}
Inventory::~Inventory() {}
SERIALIZABLE(Inventory);
SERIALIZATION_CONSTRUCTOR_IMPL(Inventory);
void Inventory::addItem(PItem item) {
itemsCache.push_back(item.get());
for (ItemIndex ind : ENUM_ALL(ItemIndex))
if (indexes[ind] && getIndexPredicate(ind)(item.get()))
indexes[ind]->push_back(item.get());
items.push_back(std::move(item));
}
void Inventory::addItems(vector<PItem> v) {
for (PItem& it : v)
addItem(std::move(it));
}
PItem Inventory::removeItem(Item* itemRef) {
int ind = -1;
for (int i : All(items))
if (items[i].get() == itemRef) {
ind = i;
break;
}
CHECK(ind > -1) << "Tried to remove unknown item.";
PItem item = std::move(items[ind]);
items.erase(items.begin() + ind);
removeElement(itemsCache, itemRef);
for (ItemIndex ind : ENUM_ALL(ItemIndex))
if (indexes[ind] && getIndexPredicate(ind)(item.get()))
removeElement(*indexes[ind], itemRef);
return item;
}
vector<PItem> Inventory::removeItems(vector<Item*> items) {
vector<PItem> ret;
for (Item* item : items)
ret.push_back(removeItem(item));
return ret;
}
void Inventory::clearIndex(ItemIndex ind) {
indexes[ind] = none;
}
vector<PItem> Inventory::removeAllItems() {
itemsCache.clear();
for (ItemIndex ind : ENUM_ALL(ItemIndex))
indexes[ind] = none;
return std::move(items);
}
vector<Item*> Inventory::getItems(function<bool (Item*)> predicate) const {
vector<Item*> ret;
for (const PItem& item : items)
if (predicate(item.get()))
ret.push_back(item.get());
return ret;
}
Item* Inventory::getItemById(UniqueEntity<Item>::Id id) {
Item* ret = nullptr;
for (PItem& item : items)
if (item->getUniqueId() == id)
ret = item.get();
return ret;
}
function<bool(const Item*)> Inventory::getIndexPredicate(ItemIndex index) {
switch (index) {
case ItemIndex::GOLD: return Item::classPredicate(ItemClass::GOLD);
case ItemIndex::WOOD: return [](const Item* it) {
return it->getResourceId() == CollectiveResourceId::WOOD; };
case ItemIndex::IRON: return [](const Item* it) {
return it->getResourceId() == CollectiveResourceId::IRON; };
case ItemIndex::STONE: return [](const Item* it) {
return it->getResourceId() == CollectiveResourceId::STONE; };
case ItemIndex::REVIVABLE_CORPSE: return [](const Item* it) {
return it->getClass() == ItemClass::CORPSE && it->getCorpseInfo()->canBeRevived; };
case ItemIndex::WEAPON: return [](const Item* it) {
return it->getClass() == ItemClass::WEAPON; };
case ItemIndex::TRAP: return [](const Item* it) { return !!it->getTrapType(); };
case ItemIndex::CORPSE: return [](const Item* it) {
return it->getClass() == ItemClass::CORPSE; };
case ItemIndex::MINION_EQUIPMENT: return [](const Item* it) {
return MinionEquipment::isItemUseful(it);};
case ItemIndex::RANGED_WEAPON: return [](const Item* it) {
return it->getClass() == ItemClass::RANGED_WEAPON;};
case ItemIndex::CAN_EQUIP: return [](const Item* it) {return it->canEquip();};
case ItemIndex::FOR_SALE: return [](const Item* it) {return !!it->getShopkeeper();};
}
}
const vector<Item*>& Inventory::getItems(ItemIndex index) const {
if (!indexes[index])
indexes[index] = getItems(getIndexPredicate(index));
return *indexes[index];
}
const vector<Item*>& Inventory::getItems() const {
return itemsCache;
}
bool Inventory::hasItem(const Item* itemRef) const {
for (const PItem& item : items)
if (item.get() == itemRef)
return true;
return false;
}
int Inventory::size() const {
return items.size();
}
bool Inventory::isEmpty() const {
return items.empty();
}