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
221 lines (177 loc) · 5.8 KB
/
construction_map.cpp
File metadata and controls
221 lines (177 loc) · 5.8 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
#include "stdafx.h"
#include "construction_map.h"
#include "creature.h"
#include "tribe.h"
#include "furniture.h"
#include "furniture_factory.h"
SERIALIZATION_CONSTRUCTOR_IMPL2(ConstructionMap::FurnitureInfo, FurnitureInfo);
SERIALIZE_DEF(ConstructionMap::FurnitureInfo, cost, built, type, task);
ConstructionMap::FurnitureInfo::FurnitureInfo(FurnitureType t, CostInfo c) : cost(c), type(t) {
}
ConstructionMap::FurnitureInfo ConstructionMap::FurnitureInfo::getBuilt(FurnitureType type) {
FurnitureInfo ret(type, CostInfo::noCost());
ret.setBuilt();
return ret;
}
void ConstructionMap::FurnitureInfo::setBuilt() {
built = true;
task = none;
}
void ConstructionMap::FurnitureInfo::reset() {
built = false;
task = none;
}
void ConstructionMap::FurnitureInfo::setTask(UniqueEntity<Task>::Id id) {
task = id;
}
CostInfo ConstructionMap::FurnitureInfo::getCost() const {
return cost;
}
bool ConstructionMap::FurnitureInfo::isBuilt() const {
return built;
}
bool ConstructionMap::FurnitureInfo::hasTask() const {
return !!task;
}
UniqueEntity<Task>::Id ConstructionMap::FurnitureInfo::getTask() const {
return *task;
}
FurnitureType ConstructionMap::FurnitureInfo::getFurnitureType() const {
return type;
}
FurnitureLayer ConstructionMap::FurnitureInfo::getLayer() const {
return Furniture::getLayer(type);
}
optional<const ConstructionMap::FurnitureInfo&> ConstructionMap::getFurniture(Position pos, FurnitureLayer layer) const {
return getReferenceMaybe(furniture[layer], pos);
}
void ConstructionMap::setTask(Position pos, FurnitureLayer layer, UniqueEntity<Task>::Id id) {
modFurniture(pos, layer)->setTask(id);
}
optional<ConstructionMap::FurnitureInfo&> ConstructionMap::modFurniture(Position pos, FurnitureLayer layer) {
return getReferenceMaybe(furniture[layer], pos);
}
void ConstructionMap::removeFurniture(Position pos, FurnitureLayer layer) {
auto& info = furniture[layer].at(pos);
auto type = info.getFurnitureType();
if (!info.isBuilt()) {
--unbuiltCounts[type];
addDebt(-info.getCost());
}
furniturePositions[type].erase(pos);
furniture[layer].erase(pos);
allFurniture.removeElement({pos, layer});
pos.setNeedsRenderUpdate(true);
}
void ConstructionMap::addDebt(const CostInfo& cost) {
debt[cost.id] += cost.value;
}
void ConstructionMap::onFurnitureDestroyed(Position pos, FurnitureLayer layer) {
if (auto info = modFurniture(pos, layer)) {
if (info->isBuilt())
addDebt(info->getCost());
info->reset();
}
for (auto pos2 : pos.neighbors8())
for (auto layer : ENUM_ALL(FurnitureLayer))
if (auto info = getFurniture(pos2, layer))
if (!FurnitureFactory::hasSupport(info->getFurnitureType(), pos2))
removeFurniture(pos2, layer);
}
void ConstructionMap::addFurniture(Position pos, const FurnitureInfo& info) {
auto layer = info.getLayer();
CHECK(!furniture[layer].count(pos));
allFurniture.push_back({pos, layer});
furniture[layer].emplace(pos, info);
pos.setNeedsRenderUpdate(true);
if (info.isBuilt())
furniturePositions[info.getFurnitureType()].insert(pos);
else {
++unbuiltCounts[info.getFurnitureType()];
addDebt(info.getCost());
}
}
bool ConstructionMap::containsFurniture(Position pos, FurnitureLayer layer) const {
return furniture[layer].count(pos);
}
int ConstructionMap::getBuiltCount(FurnitureType type) const {
return (int) furniturePositions[type].size();
}
int ConstructionMap::getTotalCount(FurnitureType type) const {
return unbuiltCounts[type] + getBuiltCount(type);
}
const set<Position>& ConstructionMap::getBuiltPositions(FurnitureType type) const {
return furniturePositions[type];
}
const vector<pair<Position, FurnitureLayer>>& ConstructionMap::getAllFurniture() const {
return allFurniture;
}
void ConstructionMap::onConstructed(Position pos, FurnitureType type) {
auto layer = Furniture::getLayer(type);
if (!containsFurniture(pos, layer))
addFurniture(pos, FurnitureInfo::getBuilt(type));
furniturePositions[type].insert(pos);
--unbuiltCounts[type];
if (furniture[layer].count(pos)) { // why this if?
auto& info = furniture[layer].at(pos);
info.setBuilt();
addDebt(-info.getCost());
}
}
const ConstructionMap::TrapInfo& ConstructionMap::getTrap(Position pos) const {
return traps.at(pos);
}
ConstructionMap::TrapInfo& ConstructionMap::getTrap(Position pos) {
return traps.at(pos);
}
void ConstructionMap::removeTrap(Position pos) {
traps.erase(pos);
pos.setNeedsRenderUpdate(true);
}
void ConstructionMap::addTrap(Position pos, const TrapInfo& info) {
CHECK(!containsTrap(pos));
traps.insert(make_pair(pos, info));
pos.setNeedsRenderUpdate(true);
}
bool ConstructionMap::containsTrap(Position pos) const {
return traps.count(pos);
}
const map<Position, ConstructionMap::TrapInfo>& ConstructionMap::getTraps() const {
return traps;
}
void ConstructionMap::TrapInfo::setArmed() {
armed = true;
marked = false;
}
void ConstructionMap::TrapInfo::reset() {
armed = false;
marked = false;
}
ConstructionMap::TrapInfo::TrapInfo(TrapType t) : type(t) {
}
bool ConstructionMap::TrapInfo::isMarked() const {
return marked;
}
bool ConstructionMap::TrapInfo::isArmed() const {
return armed;
}
TrapType ConstructionMap::TrapInfo::getType() const {
return type;
}
void ConstructionMap::TrapInfo::setMarked() {
marked = true;
}
int ConstructionMap::getDebt(CollectiveResourceId id) const {
return debt[id];
}
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);
}
SERIALIZABLE(ConstructionMap);