-
-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathlevel_builder.cpp
294 lines (245 loc) · 9.05 KB
/
level_builder.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
#include "stdafx.h"
#include "level_builder.h"
#include "progress_meter.h"
#include "square.h"
#include "creature.h"
#include "level_maker.h"
#include "collective_builder.h"
#include "view_object.h"
#include "item.h"
#include "furniture.h"
#include "furniture_factory.h"
#include "position.h"
#include "movement_set.h"
#include "content_factory.h"
#include "creature_name.h"
LevelBuilder::LevelBuilder(ProgressMeter* meter, RandomGen& r, ContentFactory* contentFactory, int width, int height,
bool allCovered, optional<double> defaultLight)
: squares(Rectangle(width, height)), unavailable(width, height, false),
heightMap(width, height, 0), covered(width, height, allCovered),
sunlight(width, height, defaultLight ? *defaultLight : (allCovered ? 0.0 : 1.0)),
attrib(width, height), items(width, height), furniture(Rectangle(width, height)),
progressMeter(meter), random(r), contentFactory(contentFactory),
mountainLevel(0, 0, 0) {
}
LevelBuilder::LevelBuilder(RandomGen& r, ContentFactory* contentFactory, int width, int height, bool covered)
: LevelBuilder(nullptr, r, contentFactory, width, height, covered) {
}
LevelBuilder::~LevelBuilder() {}
LevelBuilder::LevelBuilder(LevelBuilder&&) = default;
RandomGen& LevelBuilder::getRandom() {
return random;
}
ContentFactory* LevelBuilder::getContentFactory() const {
return contentFactory;
}
bool LevelBuilder::hasAttrib(Vec2 posT, SquareAttrib attr) {
Vec2 pos = transform(posT);
return attrib[pos].contains(attr);
}
void LevelBuilder::addAttrib(Vec2 pos, SquareAttrib attr) {
attrib[transform(pos)].insert(attr);
}
void LevelBuilder::removeAttrib(Vec2 pos, SquareAttrib attr) {
attrib[transform(pos)].erase(attr);
}
Square* LevelBuilder::modSquare(Vec2 pos) {
return squares.getWritable(transform(pos));
}
Rectangle LevelBuilder::toGlobalCoordinates(Rectangle area) {
return area.apply([this](Vec2 v) { return transform(v); });
}
vector<Vec2> LevelBuilder::toGlobalCoordinates(const vector<Vec2>& v) {
return v.transform([this](Vec2 v) { return transform(v); });
}
void LevelBuilder::addCollective(CollectiveBuilder* col) {
if (!collectives.contains(col))
collectives.push_back(col);
}
void LevelBuilder::setHeightMap(Vec2 pos, double h) {
heightMap[transform(pos)] = h;
}
double LevelBuilder::getHeightMap(Vec2 pos) {
return heightMap[transform(pos)];
}
void LevelBuilder::putCreature(Vec2 pos, PCreature creature) {
creatures.emplace_back(std::move(creature), transform(pos));
}
void LevelBuilder::putItems(Vec2 posT, vector<PItem> it) {
CHECK(canPutItems(posT));
Vec2 pos = transform(posT);
append(items[pos], std::move(it));
}
bool LevelBuilder::hasAnyItems(Vec2 posT) {
return !items[transform(posT)].empty();
}
bool LevelBuilder::canPutItems(Vec2 posT) {
return canNavigate(posT, {MovementTrait::WALK});
}
void LevelBuilder::putFurniture(Vec2 posT, FurnitureList& f, TribeId tribe, optional<SquareAttrib> attrib) {
if (auto res = f.getRandom(getRandom(), tribe))
putFurniture(posT, *res, attrib);
}
void LevelBuilder::putFurniture(Vec2 posT, FurnitureParams f, optional<SquareAttrib> attrib) {
auto layer = contentFactory->furniture.getData(f.type).getLayer();
if (getFurniture(posT, layer))
removeFurniture(posT, layer);
furniture.getBuilt(layer).putElem(transform(posT), f, [&](const FurnitureParams& t) {
return contentFactory->furniture.getFurniture(t.type, t.tribe); });
if (attrib)
addAttrib(posT, *attrib);
}
void LevelBuilder::putFurniture(Vec2 pos, FurnitureType type, optional<SquareAttrib> attrib) {
putFurniture(pos, {type, TribeId::getHostile()}, attrib);
}
void LevelBuilder::putFurniture(Vec2 pos, FurnitureType type, TribeId tribe, optional<SquareAttrib> attrib) {
putFurniture(pos, {type, tribe}, attrib);
}
void LevelBuilder::resetFurniture(Vec2 posT, FurnitureType type, optional<SquareAttrib> attrib) {
USER_CHECK(contentFactory->furniture.getData(type).getLayer() == FurnitureLayer::GROUND)
<< type.data() << " must have layer set to GROUND ";
removeAllFurniture(posT);
putFurniture(posT, type, attrib);
}
void LevelBuilder::resetFurniture(Vec2 posT, FurnitureParams params, optional<SquareAttrib> attrib) {
USER_CHECK(contentFactory->furniture.getData(params.type).getLayer() == FurnitureLayer::GROUND)
<< params.type.data() << " must have layer set to GROUND "; removeAllFurniture(posT);
putFurniture(posT, params, attrib);
}
bool LevelBuilder::canPutFurniture(Vec2 posT, FurnitureLayer layer) {
return !getFurniture(posT, layer);
}
void LevelBuilder::removeFurniture(Vec2 pos, FurnitureLayer layer) {
CHECK(getFurnitureType(pos, layer) != FurnitureType("DOWN_STAIRS"));
furniture.getBuilt(layer).clearElem(transform(pos));
}
void LevelBuilder::removeAllFurniture(Vec2 pos) {
for (auto layer : ENUM_ALL(FurnitureLayer))
removeFurniture(pos, layer);
}
optional<FurnitureType> LevelBuilder::getFurnitureType(Vec2 posT, FurnitureLayer layer) {
if (auto f = getFurniture(posT, layer))
return f->getType();
else
return none;
}
bool LevelBuilder::isFurnitureType(Vec2 pos, FurnitureType type) {
return getFurnitureType(pos, contentFactory->furniture.getData(type).getLayer()) == type;
}
const Furniture* LevelBuilder::getFurniture(Vec2 posT, FurnitureLayer layer) {
return furniture.getBuilt(layer).getReadonly(transform(posT));
}
void LevelBuilder::setLandingLink(Vec2 posT, StairKey key) {
Vec2 pos = transform(posT);
squares.getWritable(pos)->setLandingLink(key);
}
bool LevelBuilder::canPutCreature(Vec2 posT, Creature* c) {
Vec2 pos = transform(posT);
if (!canNavigate(posT, c->getMovementType().setForced(false)) || hasAttrib(posT, SquareAttrib::NO_CREATURES))
return false;
for (pair<PCreature, Vec2>& c : creatures) {
if (c.second == pos)
return false;
}
return true;
}
void LevelBuilder::setNoDiagonalPassing() {
noDiagonalPassing = true;
}
PLevel LevelBuilder::build(const ContentFactory* factory, Model* m, LevelMaker* maker, LevelId levelId) {
PROFILE;
CHECK(!!m);
CHECK(mapStack.empty());
maker->make(this, squares.getBounds());
for (Vec2 v : squares.getBounds())
if (!items[v].empty())
squares.getWritable(v)->dropItemsLevelGen(std::move(items[v]));
for (auto& elem : permanentGas)
squares.getWritable(elem.second)->addPermanentGas(elem.first, 1);
auto l = Level::create(std::move(squares), std::move(furniture), m, sunlight, levelId, covered, unavailable, factory);
for (pair<PCreature, Vec2>& c : creatures) {
Position pos(c.second, l.get());
/*CHECK(pos.canEnter(c.first.get())) << c.first->getName().bare();
pos.addCreature(std::move(c.first));*/
// Use landCreature instead, because it will try to put it in adjacent positions
if (!l->landCreature({pos}, c.first.get()))
throw LevelGenException();
m->addCreature(std::move(c.first));
}
for (CollectiveBuilder* c : collectives)
c->setLevel(l.get());
l->noDiagonalPassing = noDiagonalPassing;
l->wildlife = wildlife;
l->mountainLevel = mountainLevel;
return l;
}
static Vec2::LinearMap identity() {
return [](Vec2 v) { return v; };
}
static Vec2::LinearMap deg90(Rectangle bounds) {
return [bounds](Vec2 v) {
v -= bounds.topLeft();
return bounds.topLeft() + Vec2(v.y, v.x);
};
}
static Vec2::LinearMap deg180(Rectangle bounds) {
return [bounds](Vec2 v) {
return bounds.topLeft() - v + bounds.bottomRight() - Vec2(1, 1);
};
}
static Vec2::LinearMap deg270(Rectangle bounds) {
return [bounds](Vec2 v) {
v -= bounds.topRight() - Vec2(1, 0);
return bounds.topLeft() + Vec2(v.y, -v.x);
};
}
void LevelBuilder::pushMap(Rectangle bounds, Rot rot) {
switch (rot) {
case CW0: mapStack.push_back(identity()); break;
case CW1: mapStack.push_back(deg90(bounds)); break;
case CW2: mapStack.push_back(deg180(bounds)); break;
case CW3: mapStack.push_back(deg270(bounds)); break;
}
}
void LevelBuilder::popMap() {
mapStack.pop_back();
}
Vec2 LevelBuilder::transform(Vec2 v) {
for (auto m : mapStack.reverse()) {
v = m(v);
}
return v;
}
void LevelBuilder::setCovered(Vec2 posT, bool state) {
covered[transform(posT)] = state;
}
void LevelBuilder::setSunlight(Vec2 pos, double s) {
sunlight[pos] = s;
}
void LevelBuilder::addPermanentGas(TileGasType type, Vec2 posT) {
permanentGas.push_back({type, transform(posT)});
}
void LevelBuilder::setMountainLevel(Vec2 posT, int level) {
if (mountainLevel.getHeight() == 0)
mountainLevel = Table<int>(covered.getBounds(), 0);
mountainLevel[transform(posT)] = level;
}
void LevelBuilder::setUnavailable(Vec2 pos) {
unavailable[transform(pos)] = true;
}
bool LevelBuilder::canNavigate(Vec2 posT, const MovementType& movement) {
Vec2 pos = transform(posT);
bool isCovered = covered[pos] || isFurnitureType(posT, FurnitureType("FLOOR"));
if (unavailable[pos])
return false;
bool result = true;
for (auto layer : ENUM_ALL(FurnitureLayer))
if (auto f = furniture.getBuilt(layer).getReadonly(pos)) {
bool canEnter = f->getMovementSet().canEnter(movement, isCovered, false, none);
if (f->overridesMovement())
return canEnter;
else
result &= canEnter;
}
return result;
}