-
Notifications
You must be signed in to change notification settings - Fork 26
/
room.cpp
511 lines (449 loc) · 14.8 KB
/
room.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
#include "dfhack_shared.h"
#include "room.h"
#include "ai.h"
#include "plan.h"
#include "modules/Maps.h"
#include "df/abstract_building.h"
#include "df/abstract_building_contents.h"
#include "df/building.h"
#include "df/tile_occupancy.h"
#include "df/unit.h"
#include "df/world.h"
#include "df/world_site.h"
REQUIRE_GLOBAL(ui);
REQUIRE_GLOBAL(world);
#define BEGIN_ENUM BEGIN_IMPLEMENT_ENUM
#define ENUM_ITEM IMPLEMENT_ENUM_ITEM
#define END_ENUM END_IMPLEMENT_ENUM
ROOM_ENUMS
#undef BEGIN_ENUM
#undef ENUM_ITEM
#undef END_ENUM
room::room(room_type::type type, df::coord mins, df::coord maxs, std::string comment) :
status(room_status::plan),
type(type),
corridor_type(),
farm_type(),
stockpile_type(),
nobleroom_type(),
outpost_type(),
location_type(),
cistern_type(),
workshop_type(),
furnace_type(),
raw_type(""),
comment(comment),
min(mins),
max(maxs),
accesspath(),
layout(),
owner(-1),
bld_id(-1),
squad_id(-1),
level(-1),
noblesuite(-1),
queue(0),
workshop(nullptr),
users(),
channel_enable(),
stock_disable(),
stock_specific1(false),
stock_specific2(false),
has_users(0),
furnished(false),
queue_dig(false),
temporary(false),
outdoor(false),
channeled(false),
build_when_accessible(false),
required_value(0),
data1(-1),
data2(-1)
{
channel_enable.clear();
if (min.x > max.x)
std::swap(min.x, max.x);
if (min.y > max.y)
std::swap(min.y, max.y);
if (min.z > max.z)
std::swap(min.z, max.z);
}
room::room(corridor_type::type subtype, df::coord mins, df::coord maxs, std::string comment) :
room::room(room_type::corridor, mins, maxs, comment)
{
corridor_type = subtype;
}
room::room(farm_type::type subtype, df::coord mins, df::coord maxs, std::string comment) :
room::room(room_type::farmplot, mins, maxs, comment)
{
farm_type = subtype;
}
room::room(stockpile_type::type subtype, df::coord mins, df::coord maxs, std::string comment) :
room::room(room_type::stockpile, mins, maxs, comment)
{
stockpile_type = subtype;
}
room::room(nobleroom_type::type subtype, df::coord mins, df::coord maxs, std::string comment) :
room::room(room_type::nobleroom, mins, maxs, comment)
{
nobleroom_type = subtype;
}
room::room(outpost_type::type subtype, df::coord mins, df::coord maxs, std::string comment) :
room::room(room_type::outpost, mins, maxs, comment)
{
outpost_type = subtype;
}
room::room(location_type::type subtype, df::coord mins, df::coord maxs, std::string comment) :
room::room(room_type::location, mins, maxs, comment)
{
location_type = subtype;
}
room::room(cistern_type::type subtype, df::coord mins, df::coord maxs, std::string comment) :
room::room(room_type::cistern, mins, maxs, comment)
{
cistern_type = subtype;
}
room::room(df::workshop_type subtype, df::coord mins, df::coord maxs, std::string comment) :
room::room(room_type::workshop, mins, maxs, comment)
{
workshop_type = subtype;
}
room::room(df::furnace_type subtype, df::coord mins, df::coord maxs, std::string comment) :
room::room(room_type::furnace, mins, maxs, comment)
{
furnace_type = subtype;
}
room::~room()
{
for (auto it = layout.begin(); it != layout.end(); it++)
{
delete *it;
}
}
void room::dig(bool plan, bool channel)
{
for (int16_t x = min.x; x <= max.x; x++)
{
for (int16_t y = min.y; y <= max.y; y++)
{
for (int16_t z = min.z; z <= max.z; z++)
{
df::coord t(x, y, z);
df::tiletype *tt = Maps::getTileType(t);
if (tt)
{
if (ENUM_ATTR(tiletype, material, *tt) == tiletype_material::CONSTRUCTION)
{
continue;
}
df::tile_dig_designation dm = channel ? tile_dig_designation::Channel : dig_mode(t);
if (((dm == tile_dig_designation::DownStair || dm == tile_dig_designation::Channel) && ENUM_ATTR(tiletype, shape, *tt) != tiletype_shape::STAIR_DOWN && ENUM_ATTR(tiletype_shape, basic_shape, ENUM_ATTR(tiletype, shape, *tt)) != tiletype_shape_basic::Open) || ENUM_ATTR(tiletype, shape, *tt) == tiletype_shape::WALL)
{
AI::dig_tile(t, dm);
if (plan)
{
Maps::getTileOccupancy(t)->bits.dig_marked = 1;
}
}
}
}
}
}
if (plan)
return;
for (auto it = layout.begin(); it != layout.end(); it++)
{
furniture *f = *it;
df::coord t = min + f->pos;
df::tiletype *tt = Maps::getTileType(t);
if (tt)
{
if (ENUM_ATTR(tiletype, material, *tt) == tiletype_material::CONSTRUCTION)
continue;
if (f->dig != tile_dig_designation::Default)
{
if (ENUM_ATTR(tiletype_shape, basic_shape, ENUM_ATTR(tiletype, shape, *tt)) == tiletype_shape_basic::Wall || (f->dig == tile_dig_designation::Channel && ENUM_ATTR(tiletype_shape, basic_shape, ENUM_ATTR(tiletype, shape, *tt)) != tiletype_shape_basic::Open))
{
AI::dig_tile(t, f->dig);
}
}
else
{
df::tile_dig_designation dm = dig_mode(t);
if ((dm == tile_dig_designation::DownStair && ENUM_ATTR(tiletype, shape, *tt) != tiletype_shape::STAIR_DOWN) || ENUM_ATTR(tiletype, shape, *tt) == tiletype_shape::WALL)
{
AI::dig_tile(t, dm);
}
}
}
}
}
bool room::include(df::coord t) const
{
return min.x <= t.x && max.x >= t.x && min.y <= t.y && max.y >= t.y && min.z <= t.z && max.z >= t.z;
}
bool room::safe_include(df::coord t) const
{
if (min.x - 1 <= t.x && max.x + 1 >= t.x && min.y - 1 <= t.y && max.y + 1 >= t.y && min.z <= t.z && max.z >= t.z)
return true;
for (auto it = layout.begin(); it != layout.end(); it++)
{
furniture *f = *it;
df::coord ft = min + f->pos;
if (ft.x - 1 <= t.x && ft.x + 1 >= t.x && ft.y - 1 <= t.y && ft.y + 1 >= t.y && ft.z == t.z)
return true;
}
return false;
}
df::tile_dig_designation room::dig_mode(df::coord t) const
{
for (auto f : layout)
{
if (min + f->pos == t && f->dig != tile_dig_designation::Default)
{
return f->dig;
}
}
if (type != room_type::corridor)
{
return tile_dig_designation::Default;
}
bool wantup = include(t + df::coord(0, 0, 1));
bool wantdown = include(t - df::coord(0, 0, 1));
// XXX
extern std::unique_ptr<AI> dwarfAI;
wantup = wantup || dwarfAI->plan.corridor_include_hack(this, t, t + df::coord(0, 0, 1));
wantdown = wantdown || dwarfAI->plan.corridor_include_hack(this, t, t - df::coord(0, 0, 1));
if (wantup)
return wantdown ? tile_dig_designation::UpDownStair : tile_dig_designation::UpStair;
else
return wantdown ? tile_dig_designation::DownStair : tile_dig_designation::Default;
}
bool room::is_dug(std::ostream & reason, df::tiletype_shape_basic want) const
{
std::set<df::coord> holes;
for (auto f : layout)
{
if (f->ignore)
continue;
df::coord ft = min + f->pos;
if (f->dig == tile_dig_designation::No)
{
holes.insert(ft);
continue;
}
auto tt = *Maps::getTileType(ft);
auto sb = ENUM_ATTR(tiletype_shape, basic_shape, ENUM_ATTR(tiletype, shape, tt));
switch (sb)
{
case tiletype_shape_basic::Wall:
if (f->dig == tile_dig_designation::Default)
{
reason << "interior tile at (" << f->pos.x << ", " << f->pos.y << ", " << f->pos.z << ") is " << enum_item_key(tt);
return false;
}
reason << enum_item_key(f->dig) << "-designated tile at (" << f->pos.x << ", " << f->pos.y << ", " << f->pos.z << ") is " << enum_item_key(tt);
return false;
case tiletype_shape_basic::Open:
break;
default:
if (f->dig == tile_dig_designation::Channel)
{
reason << "Channel-designated tile at (" << f->pos.x << ", " << f->pos.y << ", " << f->pos.z << ") is " << enum_item_key(tt);
return false;
}
break;
}
}
for (int16_t x = min.x; x <= max.x; x++)
{
for (int16_t y = min.y; y <= max.y; y++)
{
for (int16_t z = min.z; z <= max.z; z++)
{
if (holes.count(df::coord(x, y, z)))
{
continue;
}
auto tt = *Maps::getTileType(x, y, z);
df::tiletype_shape s = ENUM_ATTR(tiletype, shape, tt);
if (s == tiletype_shape::WALL)
{
reason << "interior tile at (" << (x - min.x) << ", " << (y - min.y) << ", " << (z - min.z) << ") is " << enum_item_key(tt);
return false;
}
if (want != tiletype_shape_basic::None)
{
df::tiletype_shape_basic sb = ENUM_ATTR(tiletype_shape, basic_shape, s);
if (want != sb)
{
reason << "tile at (" << (x - min.x) << ", " << (y - min.y) << ", " << (z - min.z) << ") is " << enum_item_key(tt) << " (" << enum_item_key(sb) << ") but want " << enum_item_key(want);
return false;
}
}
}
}
}
return true;
}
bool room::constructions_done(std::ostream & reason) const
{
for (auto it = layout.begin(); it != layout.end(); it++)
{
furniture *f = *it;
df::coord ft = min + f->pos;
auto tt = *Maps::getTileType(ft);
auto ts = ENUM_ATTR(tiletype, shape, tt);
df::tiletype_shape want;
switch (f->construction)
{
case construction_type::NONE:
continue;
case construction_type::Fortification:
want = tiletype_shape::FORTIFICATION;
break;
case construction_type::Wall:
want = tiletype_shape::WALL;
break;
case construction_type::Floor:
want = tiletype_shape::FLOOR;
break;
case construction_type::UpStair:
want = tiletype_shape::STAIR_UP;
break;
case construction_type::DownStair:
want = tiletype_shape::STAIR_DOWN;
break;
case construction_type::UpDownStair:
want = tiletype_shape::STAIR_UPDOWN;
break;
case construction_type::Ramp:
want = tiletype_shape::RAMP;
break;
default:
reason << "unknown construction type " << enum_item_key(f->construction) << " at (" << f->pos.x << ", " << f->pos.y << ", " << f->pos.z << ")";
return false;
}
if (want == tiletype_shape::FLOOR)
{
if (ts == tiletype_shape::PEBBLES || ts == tiletype_shape::BOULDER || ts == tiletype_shape::BROOK_TOP)
continue;
if (ts == tiletype_shape::SAPLING && type == room_type::pasture)
continue;
// shrubs are allowed here but not in fixup_opena. this means that
// shrubs don't prevent pastures from being built, but they also
// don't stick around in pastures forever, potentially using up
// all the space for grass.
if (ts == tiletype_shape::SHRUB && type == room_type::pasture)
continue;
}
if (ts != want)
{
reason << "want construction type " << enum_item_key(f->construction) << " at (" << f->pos.x << ", " << f->pos.y << ", " << f->pos.z << ") but have " << enum_item_key(tt) << " (" << enum_item_key(ts) << ") instead of " << enum_item_key(want);
return false;
}
}
return true;
}
df::building *room::dfbuilding() const
{
return df::building::find(bld_id);
}
int32_t room::compute_value() const
{
auto bld = dfbuilding();
if (!bld)
{
return -1;
}
if (type == room_type::location && bld->location_id != -1)
{
auto loc = binsearch_in_vector(ui->main.fortress_site->buildings, bld->location_id);
if (loc && loc->getContents())
{
return loc->getContents()->location_value;
}
}
auto u = df::unit::find(owner);
if (!u)
{
u = world->units.active[0];
}
return bld->getRoomValue(u);
}
int32_t room::distance_to(const room *other) const
{
if (this == other)
{
return 0;
}
std::map<const room *, int32_t> path_distance;
int32_t distance = 0;
std::vector<room *> current_level = accesspath;
std::vector<room *> next_level;
path_distance[this] = 0;
while (!current_level.empty())
{
distance++;
for (room *r : current_level)
{
if (!path_distance.count(r))
{
if (r == other)
{
return distance;
}
path_distance[r] = distance;
next_level.insert(next_level.end(), r->accesspath.begin(), r->accesspath.end());
}
}
current_level = std::move(next_level);
next_level.clear();
}
std::set<const room *> seen;
seen.insert(other);
int32_t max_distance = distance;
distance = 0;
current_level = other->accesspath;
while (!current_level.empty())
{
distance++;
for (room *r : current_level)
{
if (!seen.count(r))
{
if (path_distance.count(r))
{
return path_distance.at(r) + distance;
}
seen.insert(r);
next_level.insert(next_level.end(), r->accesspath.begin(), r->accesspath.end());
}
}
current_level = std::move(next_level);
next_level.clear();
}
// not connected
return 0x10000 + distance + max_distance;
}
bool room::low_grass() const
{
df::coord sz = size();
int32_t size_tiles = sz.x * sz.y;
int32_t grass_tiles = 0;
for (int16_t x = 0; x <= sz.x; x++)
{
for (int16_t y = 0; y < sz.y; y++)
{
if (auto tt = Maps::getTileType(min + df::coord(x, y, 0)))
{
auto tm = ENUM_ATTR(tiletype, material, *tt);
if (tm == tiletype_material::GRASS_LIGHT || tm == tiletype_material::GRASS_DARK)
{
grass_tiles++;
}
}
}
}
return grass_tiles < size_tiles / 5;
}