forked from miki151/keeperrl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstruction_map.cpp
More file actions
237 lines (193 loc) · 6.97 KB
/
construction_map.cpp
File metadata and controls
237 lines (193 loc) · 6.97 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
#include "stdafx.h"
#include "construction_map.h"
#include "creature.h"
#include "tribe.h"
#include "furniture.h"
#include "furniture_factory.h"
#include "task.h"
#include "game.h"
#include "content_factory.h"
SERIALIZATION_CONSTRUCTOR_IMPL2(ConstructionMap::FurnitureInfo, FurnitureInfo);
SERIALIZE_DEF(ConstructionMap::FurnitureInfo, cost, type, task);
ConstructionMap::FurnitureInfo::FurnitureInfo(FurnitureType t, CostInfo c) : task(-1), cost(c), type(t) {
}
ConstructionMap::FurnitureInfo ConstructionMap::FurnitureInfo::getBuilt(FurnitureType type) {
return FurnitureInfo(type, CostInfo::noCost());
}
void ConstructionMap::FurnitureInfo::reset() {
task = -1;
}
void ConstructionMap::FurnitureInfo::setTask(UniqueEntity<Task>::Id id) {
task = id.getGenericId();
}
CostInfo ConstructionMap::FurnitureInfo::getCost() const {
return cost;
}
bool ConstructionMap::FurnitureInfo::isBuilt(Position pos) const {
return !!pos.getFurniture(type);
}
bool ConstructionMap::FurnitureInfo::isBuilt(Position pos, FurnitureLayer layer) const {
if (auto f = pos.getFurniture(layer))
return f->getType() == type;
return false;
}
bool ConstructionMap::FurnitureInfo::hasTask() const {
return task != -1;
}
UniqueEntity<Task>::Id ConstructionMap::FurnitureInfo::getTask() const {
return UniqueEntity<Task>::Id(task);
}
FurnitureType ConstructionMap::FurnitureInfo::getFurnitureType() const {
return type;
}
const Furniture* ConstructionMap::FurnitureInfo::getBuilt(Position pos) const {
return pos.getFurniture(type);
}
optional<const ConstructionMap::FurnitureInfo&> ConstructionMap::getFurniture(Position pos, FurnitureLayer layer) const {
return furniture[layer].getReferenceMaybe(pos);
}
void ConstructionMap::setTask(Position pos, FurnitureLayer layer, UniqueEntity<Task>::Id id) {
furniture[layer].getOrFail(pos).setTask(id);
}
void ConstructionMap::removeFurniturePlan(Position pos, FurnitureLayer layer) {
auto& info = furniture[layer].getOrFail(pos);
auto type = info.getFurnitureType();
--unbuiltCounts[type];
addDebt(-info.getCost());
furniture[layer].erase(pos);
allFurniture.removeElement({pos, layer});
pos.setNeedsRenderAndMemoryUpdate(true);
}
void ConstructionMap::addDebt(const CostInfo& cost) {
debt[cost.id] += cost.value;
//checkDebtConsistency();
CHECK(debt[cost.id] >= 0);
}
void ConstructionMap::onFurnitureDestroyed(Position pos, FurnitureLayer layer, FurnitureType type) {
PROFILE;
if (auto info = furniture[layer].getReferenceMaybe(pos))
if (info->getFurnitureType() == type) {
addDebt(info->getCost());
furniturePositions[info->getFurnitureType()].erase(pos);
info->reset();
}
}
void ConstructionMap::addFurniture(Position pos, const FurnitureInfo& info, FurnitureLayer layer) {
CHECK(!furniture[layer].contains(pos));
allFurniture.push_back({pos, layer});
furniture[layer].set(pos, info);
pos.setNeedsRenderAndMemoryUpdate(true);
if (info.isBuilt(pos, layer))
furniturePositions[info.getFurnitureType()].insert(pos);
else {
++unbuiltCounts[info.getFurnitureType()];
addDebt(info.getCost());
}
}
bool ConstructionMap::containsFurniture(Position pos, FurnitureLayer layer) const {
return furniture[layer].contains(pos);
}
int ConstructionMap::getBuiltCount(FurnitureType type) const {
return (int) getReferenceMaybe(furniturePositions, type).map([](const auto& v) { return v.size(); }).value_or(0);
}
int ConstructionMap::getTotalCount(FurnitureType type) const {
return getValueMaybe(unbuiltCounts, type).value_or(0) + getBuiltCount(type);
}
const PositionSet& ConstructionMap::getBuiltPositions(FurnitureType type) const {
static PositionSet empty;
if (auto ret = getReferenceMaybe(furniturePositions, type))
return *ret;
else
return empty;
}
const vector<pair<Position, FurnitureLayer>>& ConstructionMap::getAllFurniture() const {
return allFurniture;
}
void ConstructionMap::onConstructed(Position pos, FurnitureType type) {
auto layer = pos.getGame()->getContentFactory()->furniture.getData(type).getLayer();
if (!containsFurniture(pos, layer))
addFurniture(pos, FurnitureInfo::getBuilt(type), layer);
furniturePositions[type].insert(pos);
--unbuiltCounts[type];
if (furniture[layer].contains(pos)) { // why this if?
auto& info = furniture[layer].getOrInit(pos);
addDebt(-info.getCost());
}
}
void ConstructionMap::clearUnsupportedFurniturePlans() {
vector<pair<Position, FurnitureLayer>> toRemove;
for (auto& elem : getAllFurniture())
if (auto info = getFurniture(elem.first, elem.second))
if (!elem.first.getGame()->getContentFactory()->furniture.getData(info->getFurnitureType()).hasRequiredSupport(elem.first))
toRemove.push_back(elem);
for (auto& elem : toRemove)
removeFurniturePlan(elem.first, elem.second);
}
optional<const ConstructionMap::TrapInfo&> ConstructionMap::getTrap(Position pos) const {
return traps.getReferenceMaybe(pos);
}
optional<ConstructionMap::TrapInfo&> ConstructionMap::getTrap(Position pos) {
return traps.getReferenceMaybe(pos);
}
void ConstructionMap::removeTrap(Position pos) {
traps.erase(pos);
allTraps.removeElement(pos);
pos.setNeedsRenderAndMemoryUpdate(true);
}
void ConstructionMap::addTrap(Position pos, const TrapInfo& info) {
CHECK(!traps.contains(pos));
traps.set(pos, info);
allTraps.push_back(pos);
pos.setNeedsRenderAndMemoryUpdate(true);
}
const vector<Position>& ConstructionMap::getAllTraps() const {
return allTraps;
}
void ConstructionMap::TrapInfo::setArmed() {
armed = true;
marked = false;
}
void ConstructionMap::TrapInfo::reset() {
armed = false;
marked = false;
}
ConstructionMap::TrapInfo::TrapInfo(FurnitureType t) : type(t) {
}
bool ConstructionMap::TrapInfo::isMarked() const {
return marked;
}
bool ConstructionMap::TrapInfo::isArmed() const {
return armed;
}
FurnitureType ConstructionMap::TrapInfo::getType() const {
return type;
}
void ConstructionMap::TrapInfo::setMarked() {
marked = true;
}
int ConstructionMap::getDebt(CollectiveResourceId id) const {
return getValueMaybe(debt, id).value_or(0);
}
void ConstructionMap::checkDebtConsistency() {
unordered_map<CollectiveResourceId, int, CustomHash<CollectiveResourceId>> nowDebt;
for (auto& f : allFurniture) {
auto& info = furniture[f.second].getOrFail(f.first);
if (!info.isBuilt(f.first))
nowDebt[info.getCost().id] += info.getCost().value;
}
for (auto& elem : nowDebt)
CHECK(getValueMaybe(debt, elem.first).value_or(0) == elem.second);
for (auto& elem : debt)
CHECK(getValueMaybe(nowDebt, elem.first).value_or(0) == elem.second);
}
template <class Archive>
void ConstructionMap::TrapInfo::serialize(Archive& ar, const unsigned int version) {
ar(type, armed, marked);
}
SERIALIZABLE(ConstructionMap::TrapInfo);
SERIALIZATION_CONSTRUCTOR_IMPL2(ConstructionMap::TrapInfo, TrapInfo);
template <class Archive>
void ConstructionMap::serialize(Archive& ar, const unsigned int version) {
ar(debt, traps, furniture, furniturePositions, unbuiltCounts, allFurniture, allTraps);
}
SERIALIZABLE(ConstructionMap);