-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
dungeon.cc
7061 lines (6070 loc) · 211 KB
/
dungeon.cc
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file
* @brief Functions used when building new levels.
**/
#include "AppHdr.h"
#include "dungeon.h"
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <list>
#include <map>
#include <set>
#include <sstream>
#include "abyss.h"
#include "acquire.h"
#include "act-iter.h"
#include "artefact.h"
#include "attitude-change.h"
#include "branch.h"
#include "butcher.h"
#include "chardump.h"
#include "cloud.h"
#include "coordit.h"
#include "directn.h"
#include "dbg-maps.h"
#include "dbg-scan.h"
#include "dgn-delve.h"
#include "dgn-height.h"
#include "dgn-labyrinth.h"
#include "dgn-overview.h"
#include "dgn-shoals.h"
#include "end.h"
#include "english.h"
#include "files.h"
#include "flood-find.h"
#include "food.h"
#include "ghost.h"
#include "god-passive.h"
#include "item-name.h"
#include "item-prop.h"
#include "item-status-flag-type.h"
#include "items.h"
#include "lev-pand.h"
#include "libutil.h"
#include "mapmark.h"
#include "maps.h"
#include "mon-death.h"
#include "mon-pick.h"
#include "mon-place.h"
#include "mon-poly.h"
#include "nearby-danger.h"
#include "notes.h"
#include "place.h"
#include "randbook.h"
#include "random.h"
#include "religion.h"
#include "rot.h"
#include "show.h"
#include "spl-transloc.h"
#include "stairs.h"
#include "state.h"
#include "stringutil.h"
#include "tiledef-dngn.h"
#include "tilepick.h"
#include "tileview.h"
#include "timed-effects.h"
#include "traps.h"
#ifdef DEBUG_DIAGNOSTICS
#define DEBUG_TEMPLES
#endif
#ifdef WIZARD
#include "cio.h" // for cancellable_get_line()
#endif
// DUNGEON BUILDERS
static bool _build_level_vetoable(bool enable_random_maps,
dungeon_feature_type dest_stairs_type);
static void _build_dungeon_level(dungeon_feature_type dest_stairs_type);
static bool _valid_dungeon_level();
static bool _builder_by_type();
static bool _builder_normal();
static void _builder_items();
static void _builder_monsters();
static coord_def _place_specific_feature(dungeon_feature_type feat);
static void _place_specific_trap(const coord_def& where, trap_spec* spec,
int charges = 0, bool known = false);
static void _place_branch_entrances(bool use_vaults);
static void _place_extra_vaults();
static void _place_chance_vaults();
static void _place_minivaults();
static int _place_uniques();
static void _place_traps();
static void _prepare_water();
static void _check_doors();
static void _add_plant_clumps(int rarity, int clump_sparseness,
int clump_radius);
static void _pick_float_exits(vault_placement &place,
vector<coord_def> &targets);
static bool _feat_is_wall_floor_liquid(dungeon_feature_type);
static bool _connect_spotty(const coord_def& from,
bool (*overwriteable)(dungeon_feature_type) = nullptr);
static bool _connect_vault_exit(const coord_def& exit);
// VAULT FUNCTIONS
static const vault_placement *
_build_secondary_vault(const map_def *vault,
bool check_collisions = true,
bool make_no_exits = false,
const coord_def &where = coord_def(-1, -1));
static const vault_placement *_build_primary_vault(const map_def *vault);
static void _build_postvault_level(vault_placement &place);
static const vault_placement *
_build_vault_impl(const map_def *vault,
bool build_only = false,
bool check_collisions = false,
bool make_no_exits = false,
const coord_def &where = coord_def(-1, -1));
static void _vault_grid(vault_placement &,
int vgrid,
const coord_def& where,
keyed_mapspec *mapsp);
static void _vault_grid_mons(vault_placement &,
int vgrid,
const coord_def& where,
keyed_mapspec *mapsp);
static void _vault_grid_glyph(vault_placement &place, const coord_def& where,
int vgrid);
static void _vault_grid_mapspec(vault_placement &place, const coord_def& where,
keyed_mapspec& mapsp);
static dungeon_feature_type _vault_inspect(vault_placement &place,
int vgrid, keyed_mapspec *mapsp);
static dungeon_feature_type _vault_inspect_mapspec(vault_placement &place,
keyed_mapspec& mapsp);
static dungeon_feature_type _vault_inspect_glyph(vault_placement &place,
int vgrid);
static const map_def *_dgn_random_map_for_place(bool minivault);
static void _dgn_load_colour_grid();
static void _dgn_map_colour_fixup();
static void _dgn_unregister_vault(const map_def &map);
static void _remember_vault_placement(const vault_placement &place, bool extra);
// Returns true if the given square is okay for use by any character,
// but always false for squares in non-transparent vaults.
static bool _dgn_square_is_passable(const coord_def &c);
static coord_def _dgn_random_point_in_bounds(
dungeon_feature_type searchfeat,
uint32_t mapmask = MMT_VAULT,
dungeon_feature_type adjacent = DNGN_UNSEEN,
bool monster_free = false,
int tries = 1500);
// ALTAR FUNCTIONS
static int _setup_temple_altars(CrawlHashTable &temple);
static dungeon_feature_type _pick_temple_altar(vault_placement &place);
static dungeon_feature_type _pick_an_altar();
static vector<god_type> _temple_altar_list;
static CrawlHashTable* _current_temple_hash = nullptr; // XXX: hack!
// MISC FUNCTIONS
static void _dgn_set_floor_colours();
static bool _fixup_interlevel_connectivity();
static void _slime_connectivity_fixup();
static void _dgn_postprocess_level();
static void _calc_density();
static void _mark_solid_squares();
//////////////////////////////////////////////////////////////////////////
// Static data
// A mask of vaults and vault-specific flags.
vector<vault_placement> Temp_Vaults;
static FixedBitVector<NUM_MONSTERS> temp_unique_creatures;
static FixedVector<unique_item_status_type, MAX_UNRANDARTS> temp_unique_items;
const map_bitmask *Vault_Placement_Mask = nullptr;
static bool use_random_maps = true;
static bool dgn_check_connectivity = false;
static int dgn_zones = 0;
static vector<string> _you_vault_list;
#ifdef DEBUG_STATISTICS
static vector<string> _you_all_vault_list;
#endif
struct coloured_feature
{
dungeon_feature_type feature;
int colour;
coloured_feature() : feature(DNGN_UNSEEN), colour(BLACK) { }
coloured_feature(dungeon_feature_type f, int c)
: feature(f), colour(c)
{
}
};
struct dgn_colour_override_manager
{
dgn_colour_override_manager()
{
_dgn_load_colour_grid();
}
~dgn_colour_override_manager()
{
_dgn_map_colour_fixup();
}
};
typedef FixedArray< coloured_feature, GXM, GYM > dungeon_colour_grid;
static unique_ptr<dungeon_colour_grid> dgn_colour_grid;
static string branch_epilogues[NUM_BRANCHES];
static void _count_gold()
{
vector<item_def *> gold_piles;
vector<coord_def> gold_places;
int gold = 0;
for (rectangle_iterator ri(0); ri; ++ri)
{
for (stack_iterator j(*ri); j; ++j)
{
if (j->base_type == OBJ_GOLD)
{
gold += j->quantity;
gold_piles.push_back(&(*j));
gold_places.push_back(*ri);
}
}
}
if (!player_in_branch(BRANCH_ABYSS))
you.attribute[ATTR_GOLD_GENERATED] += gold;
if (have_passive(passive_t::detect_gold))
{
for (unsigned int i = 0; i < gold_places.size(); i++)
{
bool detected = false;
int dummy = gold_piles[i]->index();
coord_def &pos = gold_places[i];
unlink_item(dummy);
move_item_to_grid(&dummy, pos, true);
if (!env.map_knowledge(pos).item()
|| env.map_knowledge(pos).item()->base_type != OBJ_GOLD)
{
detected = true;
}
update_item_at(pos, true);
if (detected)
{
ASSERT(env.map_knowledge(pos).item());
env.map_knowledge(pos).flags |= MAP_DETECTED_ITEM;
}
}
}
}
/**********************************************************************
* builder() - kickoff for the dungeon generator.
*********************************************************************/
bool builder(bool enable_random_maps, dungeon_feature_type dest_stairs_type)
{
// Re-check whether we're in a valid place, it leads to obscure errors
// otherwise.
ASSERT_RANGE(you.where_are_you, 0, NUM_BRANCHES);
ASSERT_RANGE(you.depth, 0 + 1, brdepth[you.where_are_you] + 1);
const set<string> uniq_tags = you.uniq_map_tags;
const set<string> uniq_names = you.uniq_map_names;
// Save a copy of unique creatures for vetoes.
temp_unique_creatures = you.unique_creatures;
// And unrands
temp_unique_items = you.unique_items;
unwind_bool levelgen(crawl_state.generating_level, true);
// N tries to build the level, after which we bail with a capital B.
int tries = 50;
while (tries-- > 0)
{
// If we're getting low on available retries, disable random vaults
// and minivaults (special levels will still be placed).
if (tries < 5)
enable_random_maps = false;
try
{
if (_build_level_vetoable(enable_random_maps, dest_stairs_type))
{
for (monster_iterator mi; mi; ++mi)
gozag_set_bribe(*mi);
return true;
}
}
catch (map_load_exception &mload)
{
mprf(MSGCH_ERROR, "Failed to load map, reloading all maps (%s).",
mload.what());
reread_maps();
}
you.uniq_map_tags = uniq_tags;
you.uniq_map_names = uniq_names;
}
if (!crawl_state.map_stat_gen && !crawl_state.obj_stat_gen)
{
// Failed to build level, bail out.
if (crawl_state.need_save)
{
save_game(true,
make_stringf("Unable to generate level for '%s'!",
level_id::current().describe().c_str()).c_str());
}
else
{
die("Unable to generate level for '%s'!",
level_id::current().describe().c_str());
}
}
env.level_layout_types.clear();
return false;
}
static bool _build_level_vetoable(bool enable_random_maps,
dungeon_feature_type dest_stairs_type)
{
#ifdef DEBUG_STATISTICS
mapstat_report_map_build_start();
#endif
dgn_reset_level(enable_random_maps);
if (player_in_branch(BRANCH_TEMPLE))
_setup_temple_altars(you.props);
try
{
_build_dungeon_level(dest_stairs_type);
}
catch (dgn_veto_exception& e)
{
dprf(DIAG_DNGN, "<white>VETO</white>: %s: %s",
level_id::current().describe().c_str(), e.what());
#ifdef DEBUG_STATISTICS
mapstat_report_map_veto(e.what());
#endif
// try not to lose any ghosts that have been placed
save_ghosts(ghost_demon::find_ghosts(false), false, false);
return false;
}
_dgn_set_floor_colours();
if (crawl_state.game_standard_levelgen()
&& !_valid_dungeon_level())
{
return false;
}
#ifdef DEBUG_MONS_SCAN
// If debug_mons_scan() finds a problem while crawl_state.generating_level is
// still true then it will announce that a problem was caused
// during level generation.
debug_mons_scan();
#endif
if (!env.level_build_method.empty()
&& env.level_build_method[0] == ' ')
{
env.level_build_method = env.level_build_method.substr(1);
}
string level_layout_type = comma_separated_line(
env.level_layout_types.begin(),
env.level_layout_types.end(), ", ");
// Save information in the level's properties hash table
// so we can include it in crash reports.
env.properties[BUILD_METHOD_KEY] = env.level_build_method;
env.properties[LAYOUT_TYPE_KEY] = level_layout_type;
_dgn_postprocess_level();
env.level_layout_types.clear();
env.level_uniq_maps.clear();
env.level_uniq_map_tags.clear();
_dgn_map_colour_fixup();
// Call the branch epilogue, if any.
if (!branch_epilogues[you.where_are_you].empty())
if (!dlua.callfn(branch_epilogues[you.where_are_you].c_str(), 0, 0))
{
mprf(MSGCH_ERROR, "branch epilogue for %s failed: %s",
level_id::current().describe().c_str(),
dlua.error.c_str());
return false;
}
// Discard any Lua chunks we loaded.
strip_all_maps();
check_map_validity();
_count_gold();
if (!_you_vault_list.empty())
{
vector<string> &vec(you.vault_list[level_id::current()]);
vec.insert(vec.end(), _you_vault_list.begin(), _you_vault_list.end());
}
#ifdef DEBUG_STATISTICS
for (auto vault : _you_all_vault_list)
mapstat_report_map_success(vault);
#endif
return true;
}
// Things that are bugs where we want to assert rather than to sweep it under
// the rug with a veto.
static void _builder_assertions()
{
#ifdef ASSERTS
for (rectangle_iterator ri(0); ri; ++ri)
if (!in_bounds(*ri))
if (!feat_is_valid_border(grd(*ri)))
{
die("invalid map border at (%d,%d): %s", ri->x, ri->y,
dungeon_feature_name(grd(*ri)));
}
#endif
}
/**
* Place a transporter and set its destination.
*
* @param pos The position of the transporter
* @param dest The position the transporter destination.
**/
void dgn_place_transporter(const coord_def &pos, const coord_def &dest)
{
ASSERT(pos != dest);
env.markers.add(new map_position_marker(pos, DNGN_TRANSPORTER, dest));
env.markers.clear_need_activate();
dungeon_terrain_changed(pos, DNGN_TRANSPORTER, false, true);
dungeon_terrain_changed(dest, DNGN_TRANSPORTER_LANDING, false, true);
}
/**
* Create transporters on the current level based on transporter markers. This
* does checks for duplicate transporter destinations, transporters with no
* destinations, and unused transporter destinations.
*
* @returns True if no transporter placement errors were found, false
* otherwise.
**/
bool dgn_make_transporters_from_markers()
{
bool no_errors = true;
// Find transporter destination markers and ensure no duplicates.
const vector<map_marker*> dest_markers =
find_markers_by_prop(TRANSPORTER_DEST_NAME_PROP);
map<string, map_marker *> dest_map;
for (auto dm : dest_markers)
{
const string name = dm->property(TRANSPORTER_DEST_NAME_PROP);
if (dest_map.find(name) != dest_map.end())
{
mprf(MSGCH_ERROR, "Multiple locations with transporter "
"destination name %s.", name.c_str());
no_errors = false;
continue;
}
dest_map[name] = dm;
}
// Place transporters.
const vector<map_marker*> trans_markers =
find_markers_by_prop(TRANSPORTER_NAME_PROP);
map<string, bool> used_dest_map;
for (auto tm : trans_markers)
{
const string name = tm->property(TRANSPORTER_NAME_PROP);
if (dest_map.find(name) == dest_map.end())
{
mprf(MSGCH_ERROR, "Transporter with name %s has no corresponding "
"destination marker.", name.c_str());
no_errors = false;
continue;
}
dgn_place_transporter(tm->pos, dest_map[name]->pos);
env.markers.remove(tm);
used_dest_map[name] = true;
}
// Clean up any destination markers.
for (auto dm : dest_markers)
{
const string name = dm->property(TRANSPORTER_DEST_NAME_PROP);
if (used_dest_map[name])
env.markers.remove(dm);
else
{
mprf(MSGCH_ERROR, "Unused transporter destination with name %s.",
name.c_str());
no_errors = false;
}
}
return no_errors;
}
// Should be called after a level is constructed to perform any final
// fixups.
static void _dgn_postprocess_level()
{
shoals_postprocess_level();
_builder_assertions();
_calc_density();
_mark_solid_squares();
}
void dgn_clear_vault_placements()
{
env.level_vaults.clear();
}
// Removes vaults that are not referenced in the map index mask from
// the level_vaults array.
void dgn_erase_unused_vault_placements()
{
set<int> referenced_vault_indexes;
for (rectangle_iterator ri(MAPGEN_BORDER); ri; ++ri)
{
const int map_index = env.level_map_ids(*ri);
if (map_index != INVALID_MAP_INDEX)
referenced_vault_indexes.insert(map_index);
}
// Walk backwards and toss unused vaults.
map<int, int> new_vault_index_map;
const int nvaults = env.level_vaults.size();
for (int i = nvaults - 1; i >= 0; --i)
{
if (!referenced_vault_indexes.count(i))
{
{
auto &vp = env.level_vaults[i];
// Unreferenced vault, blow it away
dprf(DIAG_DNGN, "Removing references to unused map #%d)"
" '%s' (%d,%d) (%d,%d)",
i, vp->map.name.c_str(), vp->pos.x, vp->pos.y,
vp->size.x, vp->size.y);
if (!vp->seen)
{
dprf(DIAG_DNGN, "Unregistering unseen vault: %s",
vp->map.name.c_str());
_dgn_unregister_vault(vp->map);
}
}
env.level_vaults.erase(env.level_vaults.begin() + i);
// Fix new indexes for all higher indexed vaults that are
// still referenced.
for (int j = i + 1; j < nvaults; ++j)
if (int *newidx = map_find(new_vault_index_map, j))
--*newidx;
}
else
{
// Vault is still referenced, make a note of this index.
new_vault_index_map[i] = i;
}
}
// Finally, update the index map.
for (rectangle_iterator ri(MAPGEN_BORDER); ri; ++ri)
{
const int map_index = env.level_map_ids(*ri);
if (map_index != INVALID_MAP_INDEX)
{
if (int *newidx = map_find(new_vault_index_map, map_index))
env.level_map_ids(*ri) = *newidx;
}
}
#ifdef DEBUG_ABYSS
dprf(DIAG_DNGN, "Extant vaults on level: %d",
(int) env.level_vaults.size());
int i = 0;
for (auto &vp : env.level_vaults)
{
dprf(DIAG_DNGN, "%d) %s (%d,%d) size (%d,%d)",
i++, vp->map.name.c_str(), vp->pos.x, vp->pos.y,
vp->size.x, vp->size.y);
}
#endif
}
void level_clear_vault_memory()
{
dgn_clear_vault_placements();
Temp_Vaults.clear();
env.level_map_mask.init(0);
env.level_map_ids.init(INVALID_MAP_INDEX);
}
void dgn_flush_map_memory()
{
you.uniq_map_tags.clear();
you.uniq_map_names.clear();
}
static void _dgn_load_colour_grid()
{
dgn_colour_grid.reset(new dungeon_colour_grid);
dungeon_colour_grid &dcgrid(*dgn_colour_grid);
for (int y = Y_BOUND_1; y <= Y_BOUND_2; ++y)
for (int x = X_BOUND_1; x <= X_BOUND_2; ++x)
if (env.grid_colours[x][y] != BLACK)
{
dcgrid[x][y]
= coloured_feature(grd[x][y], env.grid_colours[x][y]);
}
}
static void _dgn_map_colour_fixup()
{
if (!dgn_colour_grid)
return;
// If the original coloured feature has been changed, reset the colour.
const dungeon_colour_grid &dcgrid(*dgn_colour_grid);
for (int y = Y_BOUND_1; y <= Y_BOUND_2; ++y)
for (int x = X_BOUND_1; x <= X_BOUND_2; ++x)
if (dcgrid[x][y].colour != BLACK
&& grd[x][y] != dcgrid[x][y].feature
&& (grd[x][y] != DNGN_UNDISCOVERED_TRAP
|| dcgrid[x][y].feature != DNGN_FLOOR))
{
env.grid_colours[x][y] = BLACK;
}
dgn_colour_grid.reset(nullptr);
}
void dgn_set_grid_colour_at(const coord_def &c, int colour)
{
if (colour != BLACK)
{
env.grid_colours(c) = colour;
if (!dgn_colour_grid)
dgn_colour_grid.reset(new dungeon_colour_grid);
(*dgn_colour_grid)(c) = coloured_feature(grd(c), colour);
}
}
static void _set_grd(const coord_def &c, dungeon_feature_type feat)
{
// It might be good to clear some pgrid flags as well.
env.tile_flv(c).feat = 0;
env.tile_flv(c).special = 0;
env.grid_colours(c) = 0;
grd(c) = feat;
}
static void _dgn_register_vault(const string &name, const string &spaced_tags)
{
if (spaced_tags.find(" allow_dup ") == string::npos)
you.uniq_map_names.insert(name);
if (spaced_tags.find(" luniq ") != string::npos)
env.level_uniq_maps.insert(name);
for (const string &tag : split_string(" ", spaced_tags))
{
if (starts_with(tag, "uniq_"))
you.uniq_map_tags.insert(tag);
else if (starts_with(tag, "luniq_"))
env.level_uniq_map_tags.insert(tag);
}
}
static void _dgn_unregister_vault(const map_def &map)
{
you.uniq_map_names.erase(map.name);
env.level_uniq_maps.erase(map.name);
for (const string &tag : split_string(" ", map.tags))
{
if (starts_with(tag, "uniq_"))
you.uniq_map_tags.erase(tag);
else if (starts_with(tag, "luniq_"))
env.level_uniq_map_tags.erase(tag);
}
for (const subvault_place &sub : map.subvault_places)
_dgn_unregister_vault(*sub.subvault);
}
bool dgn_square_travel_ok(const coord_def &c)
{
const dungeon_feature_type feat = grd(c);
if (feat_is_trap(feat))
{
const trap_def * const trap = trap_at(c);
return !(trap && trap->type == TRAP_TELEPORT_PERMANENT);
}
else
return feat_is_traversable(feat);
}
static bool _dgn_square_is_passable(const coord_def &c)
{
// [enne] Why does this function check MMT_OPAQUE?
//
// Don't peek inside MMT_OPAQUE vaults (all vaults are opaque by
// default) because vaults may choose to create isolated regions,
// or otherwise cause connectivity issues even if the map terrain
// is travel-passable.
return !(env.level_map_mask(c) & MMT_OPAQUE) && dgn_square_travel_ok(c);
}
static inline void _dgn_point_record_stub(const coord_def &) { }
template <class point_record>
static bool _dgn_fill_zone(
const coord_def &start, int zone,
point_record &record_point,
bool (*passable)(const coord_def &) = _dgn_square_is_passable,
bool (*iswanted)(const coord_def &) = nullptr)
{
bool ret = false;
list<coord_def> points[2];
int cur = 0;
// No bounds checks, assuming the level has at least one layer of
// rock border.
for (points[cur].push_back(start); !points[cur].empty();)
{
for (const auto &c : points[cur])
{
travel_point_distance[c.x][c.y] = zone;
if (iswanted && iswanted(c))
ret = true;
for (adjacent_iterator ai(c); ai; ++ai)
{
const coord_def& cp = *ai;
if (!map_bounds(cp)
|| travel_point_distance[cp.x][cp.y] || !passable(cp))
{
continue;
}
travel_point_distance[cp.x][cp.y] = zone;
record_point(cp);
points[!cur].push_back(cp);
}
}
points[cur].clear();
cur = !cur;
}
return ret;
}
static bool _is_perm_down_stair(const coord_def &c)
{
switch (grd(c))
{
case DNGN_STONE_STAIRS_DOWN_I:
case DNGN_STONE_STAIRS_DOWN_II:
case DNGN_STONE_STAIRS_DOWN_III:
case DNGN_EXIT_HELL:
case DNGN_EXIT_PANDEMONIUM:
case DNGN_TRANSIT_PANDEMONIUM:
case DNGN_EXIT_ABYSS:
case DNGN_ABYSSAL_STAIR:
return true;
default:
return false;
}
}
static bool _is_upwards_exit_stair(const coord_def &c)
{
// Is this a valid upwards or exit stair out of a branch? In general,
// ensure that each region has a stone stair up.
if (feature_mimic_at(c))
return false;
if (feat_is_stone_stair_up(grd(c))
|| feat_is_branch_exit(grd(c)))
{
return true;
}
switch (grd(c))
{
case DNGN_EXIT_PANDEMONIUM:
case DNGN_TRANSIT_PANDEMONIUM:
case DNGN_EXIT_ABYSS:
return true;
case DNGN_ENTER_HELL:
return parent_branch(you.where_are_you) == BRANCH_VESTIBULE;
default:
return false;
}
}
static bool _is_exit_stair(const coord_def &c)
{
if (feature_mimic_at(c))
return false;
// Branch entries, portals, and abyss entries are not considered exit
// stairs here, as they do not provide an exit (in a transitive sense) from
// the current level.
if (feat_is_stone_stair(grd(c))
|| feat_is_escape_hatch(grd(c))
|| feat_is_branch_exit(grd(c)))
{
return true;
}
switch (grd(c))
{
case DNGN_EXIT_PANDEMONIUM:
case DNGN_TRANSIT_PANDEMONIUM:
case DNGN_EXIT_ABYSS:
return true;
case DNGN_ENTER_HELL:
return parent_branch(you.where_are_you) == BRANCH_VESTIBULE;
default:
return false;
}
}
// Counts the number of mutually unreachable areas in the map,
// excluding isolated zones within vaults (we assume the vault author
// knows what she's doing). This is an easy way to check whether a map
// has isolated parts of the level that were not formerly isolated.
//
// All squares within vaults are treated as non-reachable, to simplify
// life, because vaults may change the level layout and isolate
// different areas without changing the number of isolated areas.
// Here's a before and after example of such a vault that would cause
// problems if we considered floor in the vault as non-isolating (the
// vault is represented as V for walls and o for floor squares in the
// vault).
//
// Before:
//
// xxxxx xxxxx
// x<..x x.2.x
// x.1.x xxxxx 3 isolated zones
// x>..x x.3.x
// xxxxx xxxxx
//
// After:
//
// xxxxx xxxxx
// x<1.x x.2.x
// VVVVVVVVVVoooV 3 isolated zones, but the isolated zones are different.
// x>3.x x...x
// xxxxx xxxxx
//
// If count_stairless is true, returns the number of regions that have no
// stairs in them.
//
// If fill is non-zero, it fills any disconnected regions with fill.
//
static int _process_disconnected_zones(int x1, int y1, int x2, int y2,
bool choose_stairless,
dungeon_feature_type fill)
{
memset(travel_point_distance, 0, sizeof(travel_distance_grid_t));
int nzones = 0;
int ngood = 0;
for (int y = y1; y <= y2 ; ++y)
{
for (int x = x1; x <= x2; ++x)
{
if (!map_bounds(x, y)
|| travel_point_distance[x][y]
|| !_dgn_square_is_passable(coord_def(x, y)))
{
continue;
}
const bool found_exit_stair =
_dgn_fill_zone(coord_def(x, y), ++nzones,
_dgn_point_record_stub,
_dgn_square_is_passable,
choose_stairless ? (at_branch_bottom() ?
_is_upwards_exit_stair :
_is_exit_stair) : nullptr);
// If we want only stairless zones, screen out zones that did
// have stairs.
if (choose_stairless && found_exit_stair)
++ngood;
else if (fill)
{
// Don't fill in areas connected to vaults.
// We want vaults to be accessible; if the area is disconneted
// from the rest of the level, this will cause the level to be
// vetoed later on.
bool veto = false;
vector<coord_def> coords;
for (int fy = y1; fy <= y2 ; ++fy)
{
for (int fx = x1; fx <= x2; ++fx)
{
if (travel_point_distance[fx][fy] == nzones)
{
if (map_masked(coord_def(fx, fy), MMT_VAULT))
{
veto = true;
break;
}
else
coords.emplace_back(fx, fy);
}
}
if (veto)
break;
}
if (!veto)
{
for (auto c : coords)
_set_grd(c, fill);
}
}
}
}
return nzones - ngood;
}
int dgn_count_disconnected_zones(bool choose_stairless,
dungeon_feature_type fill)
{
return _process_disconnected_zones(0, 0, GXM-1, GYM-1, choose_stairless,
fill);
}
static void _fixup_hell_stairs()
{
for (rectangle_iterator ri(1); ri; ++ri)
{
if (feat_is_stone_stair_up(grd(*ri))
|| grd(*ri) == DNGN_ESCAPE_HATCH_UP)
{
_set_grd(*ri, DNGN_ENTER_HELL);
}
}
}
static void _fixup_pandemonium_stairs()
{
for (rectangle_iterator ri(1); ri; ++ri)
{
if (feat_is_stone_stair_up(grd(*ri))
|| grd(*ri) == DNGN_ESCAPE_HATCH_UP)