/
minion_equipment.cpp
339 lines (308 loc) · 10.8 KB
/
minion_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
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
/* 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 "minion_equipment.h"
#include "item.h"
#include "creature.h"
#include "effect.h"
#include "equipment.h"
#include "creature_attributes.h"
#include "effect.h"
#include "body.h"
#include "item_class.h"
#include "corpse_info.h"
#include "weapon_info.h"
#include "minion_equipment_type.h"
#include "health_type.h"
#include "automaton_part.h"
#include "game.h"
template <class Archive>
void MinionEquipment::serialize(Archive& ar, const unsigned int) {
ar(owners, locked, myItems, lockedSlots);
}
SERIALIZABLE(MinionEquipment);
optional<int> MinionEquipment::getEquipmentLimit(MinionEquipmentType type) const {
switch (type) {
case MinionEquipmentType::COMBAT_ITEM:
case MinionEquipmentType::HEALING:
case MinionEquipmentType::MATERIALIZATION:
return 6;
case MinionEquipmentType::TORCH:
return 1;
default:
return none;
}
}
optional<MinionEquipmentType> MinionEquipment::getEquipmentType(const Item* it) {
if (it->canEquip())
return MinionEquipmentType::ARMOR;
if (auto& effect = it->getEffect())
return effect->getMinionEquipmentType();
if (it->hasOwnedEffect(LastingEffect::LIGHT_SOURCE))
return MinionEquipmentType::TORCH;
return none;
}
bool MinionEquipment::isItemUseful(const Item* it) {
return getEquipmentType(it) || it->canApply() || it->getClass() == ItemClass::GOLD
|| (it->getClass() == ItemClass::FOOD && !it->getCorpseInfo()) || it->getIngredientType()
|| it->getUpgradeInfo();
}
const static vector<WeakPointer<Item>> emptyItems;
bool MinionEquipment::canUseItemType(const Creature* c, MinionEquipmentType type, const Item* it) const {
switch (type) {
case MinionEquipmentType::TORCH:
return !c->isAffected(LastingEffect::NIGHT_VISION);
case MinionEquipmentType::HEALING:
return c->getBody().hasHealth(HealthType::FLESH, c->getGame()->getContentFactory());
case MinionEquipmentType::MATERIALIZATION:
return c->getBody().hasHealth(HealthType::SPIRIT, c->getGame()->getContentFactory());
case MinionEquipmentType::COMBAT_ITEM:
return true;
case MinionEquipmentType::ARMOR: {
if (!c->canEquipIfEmptySlot(it))
return false;
for (auto& item : copyOf(myItems.getOrElse(c, emptyItems)))
if (item && it->isConflictingEquipment(item.get()))
return false;
return true;
}
}
}
bool MinionEquipment::needsItem(const Creature* c, const Item* it, bool noLimit) const {
PROFILE;
if (optional<MinionEquipmentType> type = getEquipmentType(it)) {
if (!canUseItemType(c, *type, it))
return false;
if (!noLimit) {
auto itemValue = getItemValue(c, it);
if (auto limit = getEquipmentLimit(*type)) {
auto pred = [=](const Item* ownedItem) {
return getEquipmentType(ownedItem) == *type &&
(getItemValue(c, ownedItem) >= itemValue || isLocked(c, ownedItem->getUniqueId())) &&
ownedItem != it;
};
if (getItemsOwnedBy(c, pred).size() >= *limit)
return false;
}
if (it->canEquip()) {
auto slot = it->getEquipmentSlot();
if (isLocked(c, slot) && !isOwner(it, c))
return false;
int limit = c->getEquipment().getMaxItems(slot, c);
auto pred = [=](const Item* ownedItem) {
return ownedItem->canEquip() &&
ownedItem->getEquipmentSlot() == slot &&
(getItemValue(c, ownedItem) >= itemValue || isLocked(c, ownedItem->getUniqueId())) &&
ownedItem != it;
};
if (getItemsOwnedBy(c, pred).size() >= limit)
return false;
if (slot == EquipmentSlot::RINGS) {
for (auto e : it->getEquipedEffects()) {
auto satisfiedEffect = [&] {
for (auto other : getItemsOwnedBy(c))
if (other != it && other->getEquipedEffects().contains(e))
return true;
return false;
};
if (!satisfiedEffect())
return true;
}
if (!it->getEquipedEffects().empty())
return false;
}
}
}
return true;
} else
return false;
}
optional<Creature::Id> MinionEquipment::getOwner(const Item* it) const {
return owners.getMaybe(it);
}
bool MinionEquipment::isOwner(const Item* it, const Creature* c) const {
return getOwner(it) == c->getUniqueId();
}
void MinionEquipment::updateOwners(const vector<Creature*>& creatures) {
auto oldItemMap = myItems;
myItems.clear();
owners.clear();
for (auto c : creatures)
for (auto item : oldItemMap.getOrElse(c, emptyItems))
if (item) {
owners.set(item, c->getUniqueId());
myItems.getOrInit(c).push_back(item);
}
for (auto c : creatures)
for (auto item : getItemsOwnedBy(c))
if (!needsItem(c, item) && !isLocked(c, item->getUniqueId()))
discard(item);
}
void MinionEquipment::updateItems(const vector<Item*>& items) {
auto oldOwners = owners;
myItems.clear();
owners.clear();
for (auto it : items)
if (auto owner = oldOwners.getMaybe(it)) {
myItems.getOrInit(*owner).push_back(it);
owners.set(it->getUniqueId(), *owner);
}
}
vector<Item*> MinionEquipment::getItemsOwnedBy(const Creature* c, ItemPredicate predicate) const {
PROFILE;
vector<Item*> ret;
for (auto& item : myItems.getOrElse(c, emptyItems))
if (item)
if (!predicate || predicate(item.get()))
ret.push_back(item.get());
return ret;
}
void MinionEquipment::discard(const Item* it) {
discard(it->getUniqueId());
}
void MinionEquipment::discard(UniqueEntity<Item>::Id id) {
if (auto owner = owners.getMaybe(id)) {
locked.erase(make_pair(*owner, id));
owners.erase(id);
auto& items = myItems.getOrFail(*owner);
for (int i : All(items))
if (items[i])
if (items[i]->getUniqueId() == id) {
items.removeIndex(i);
break;
}
}
}
void MinionEquipment::sortByEquipmentValue(const Creature* c, vector<Item*>& items) const {
PROFILE;
vector<int> values;
vector<int> indexes;
for (auto& item : items) {
values.push_back(getItemValue(c, item));
indexes.push_back(indexes.size());
}
sort(indexes.begin(), indexes.end(), [&](int index1, int index2) {
int diff = values[index1] - values[index2];
if (diff == 0)
return items[index1]->getUniqueId() < items[index2]->getUniqueId();
else
return diff > 0;
});
vector<Item*> ret;
for (auto& index : indexes)
ret.push_back(items[index]);
items = ret;
}
bool MinionEquipment::tryToOwn(const Creature* c, Item* it) {
if (it->canEquip()) {
auto slot = it->getEquipmentSlot();
vector<Item*> contesting;
int slotSize = c->getEquipment().getMaxItems(slot, c);
if (slotSize > 0) {
for (auto& item : myItems.getOrElse(c, emptyItems))
if (item)
if (item->canEquip() && item->getEquipmentSlot() == slot) {
if (!isLocked(c, item->getUniqueId()))
contesting.push_back(item.get());
else if (--slotSize <= 0)
return false;
}
if (contesting.size() >= slotSize) {
sortByEquipmentValue(c, contesting);
for (int i = slotSize - 1; i < contesting.size(); ++i)
discard(contesting[i]);
}
}
for (auto& item : copyOf(myItems.getOrElse(c, emptyItems)))
if (item && it->isConflictingEquipment(item.get()))
discard(item.get());
}
discard(it);
owners.set(it, c->getUniqueId());
myItems.getOrInit(c).push_back(it);
return true;
}
Item* MinionEquipment::getWorstItem(const Creature* c, vector<Item*> items) const {
PROFILE;
Item* ret = nullptr;
for (Item* it : items)
if (!isLocked(c, it->getUniqueId()) &&
(!ret || getItemValue(c, it) < getItemValue(c, ret)))
ret = it;
return ret;
}
static bool canAutoAssignItem(const Item* item) {
if (auto& effect = item->getEffect())
if (!effect->canAutoAssignMinionEquipment())
return false;
for (auto effect : item->getWeaponInfo().attackerEffect)
if (!effect.canAutoAssignMinionEquipment())
return false;
return true;
}
void MinionEquipment::autoAssign(const Creature* creature, vector<Item*> possibleItems) {
PROFILE;
map<EquipmentSlot, vector<Item*>> slots;
for (Item* it : getItemsOwnedBy(creature))
if (it->canEquip()) {
EquipmentSlot slot = it->getEquipmentSlot();
slots[slot].push_back(it);
}
sortByEquipmentValue(creature, possibleItems);
for (Item* it : possibleItems)
if (!getOwner(it) && needsItem(creature, it) && canAutoAssignItem(it)) {
if (!it->canEquip()) {
CHECK(tryToOwn(creature, it));
continue;
}
auto slot = it->getEquipmentSlot();
Item* replacedItem = getWorstItem(creature, slots[slot]);
int slotSize = creature->getEquipment().getMaxItems(slot, creature);
int numInSlot = slots[slot].size();
if (numInSlot < slotSize ||
(replacedItem && getItemValue(creature, replacedItem) < getItemValue(creature, it))) {
if (numInSlot == slotSize) {
discard(replacedItem);
slots[slot].removeElement(replacedItem);
}
CHECK(tryToOwn(creature, it));
slots[slot].push_back(it);
//break;
}
}
}
int MinionEquipment::getItemValue(const Creature* c, const Item* it) const {
PROFILE;
int sum = 0;
for (auto& mod : it->getModifierValues()) {
sum += mod.second;
if (mod.first == it->getWeaponInfo().meleeAttackAttr)
sum += c->getRawAttr(mod.first, c->getCombatExperience(true, true));
}
return sum;
}
void MinionEquipment::toggleLocked(const Creature* c, UniqueEntity<Item>::Id it) {
auto elem = make_pair(c->getUniqueId(), it);
if (!locked.count(elem))
locked.insert(elem);
else
locked.erase(elem);
}
void MinionEquipment::toggleLocked(const Creature* c, EquipmentSlot slot) {
lockedSlots.getOrInit(c).toggle(slot);
}
bool MinionEquipment::isLocked(const Creature* c, UniqueEntity<Item>::Id it) const {
return locked.count(make_pair(c->getUniqueId(), it));
}
bool MinionEquipment::isLocked(const Creature* c, EquipmentSlot slot) const {
static EnumSet<EquipmentSlot> empty;
return lockedSlots.getOrElse(c, empty).contains(slot);
}