forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapgen_functions.cpp
3271 lines (3028 loc) · 132 KB
/
mapgen_functions.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
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
#include "mapgen_functions.h"
#include <algorithm>
#include <array>
#include <cstdlib>
#include <initializer_list>
#include <iterator>
#include <map>
#include <unordered_set>
#include <utility>
#include <vector>
#include "calendar.h"
#include "character_id.h"
#include "debug.h"
#include "enums.h"
#include "field_type.h"
#include "flood_fill.h"
#include "game_constants.h"
#include "int_id.h"
#include "line.h"
#include "map.h"
#include "map_iterator.h"
#include "mapdata.h"
#include "mapgen.h"
#include "mapgendata.h"
#include "mapgenformat.h"
#include "omdata.h"
#include "overmap.h"
#include "point.h"
#include "regional_settings.h"
#include "rng.h"
#include "string_id.h"
#include "trap.h"
#include "vehicle_group.h"
#include "weighted_list.h"
static const itype_id itype_hat_hard( "hat_hard" );
static const itype_id itype_jackhammer( "jackhammer" );
static const itype_id itype_mask_dust( "mask_dust" );
static const mtype_id mon_ant_larva( "mon_ant_larva" );
static const mtype_id mon_ant_queen( "mon_ant_queen" );
static const mtype_id mon_bee( "mon_bee" );
static const mtype_id mon_beekeeper( "mon_beekeeper" );
static const mtype_id mon_zombie_jackson( "mon_zombie_jackson" );
static const mongroup_id GROUP_ZOMBIE( "GROUP_ZOMBIE" );
class npc_template;
tripoint rotate_point( const tripoint &p, int rotations )
{
if( p.x < 0 || p.x >= SEEX * 2 ||
p.y < 0 || p.y >= SEEY * 2 ) {
debugmsg( "Point out of range: %d,%d,%d", p.x, p.y, p.z );
// Mapgen is vulnerable, don't supply invalid points, debugmsg is enough
return tripoint( 0, 0, p.z );
}
rotations = rotations % 4;
tripoint ret = p;
switch( rotations ) {
case 0:
break;
case 1:
ret.x = p.y;
ret.y = SEEX * 2 - 1 - p.x;
break;
case 2:
ret.x = SEEX * 2 - 1 - p.x;
ret.y = SEEY * 2 - 1 - p.y;
break;
case 3:
ret.x = SEEY * 2 - 1 - p.y;
ret.y = p.x;
break;
}
return ret;
}
building_gen_pointer get_mapgen_cfunction( const std::string &ident )
{
static const std::map<std::string, building_gen_pointer> pointers = { {
{ "null", &mapgen_null },
{ "test", &mapgen_test },
{ "crater", &mapgen_crater },
{ "field", &mapgen_field },
{ "forest", &mapgen_forest },
{ "forest_trail_straight", &mapgen_forest_trail_straight },
{ "forest_trail_curved", &mapgen_forest_trail_curved },
// TODO: Add a dedicated dead-end function. For now it copies the straight section above.
{ "forest_trail_end", &mapgen_forest_trail_straight },
{ "forest_trail_tee", &mapgen_forest_trail_tee },
{ "forest_trail_four_way", &mapgen_forest_trail_four_way },
{ "hive", &mapgen_hive },
{ "road_straight", &mapgen_road },
{ "road_curved", &mapgen_road },
{ "road_end", &mapgen_road },
{ "road_tee", &mapgen_road },
{ "road_four_way", &mapgen_road },
{ "field", &mapgen_field },
{ "highway", &mapgen_highway },
{ "railroad_straight", &mapgen_railroad },
{ "railroad_curved", &mapgen_railroad },
{ "railroad_end", &mapgen_railroad },
{ "railroad_tee", &mapgen_railroad },
{ "railroad_four_way", &mapgen_railroad },
{ "railroad_bridge", &mapgen_railroad_bridge },
{ "river_center", &mapgen_river_center },
{ "river_curved_not", &mapgen_river_curved_not },
{ "river_straight", &mapgen_river_straight },
{ "river_curved", &mapgen_river_curved },
{ "parking_lot", &mapgen_parking_lot },
{ "cavern", &mapgen_cavern },
{ "open_air", &mapgen_open_air },
{ "rift", &mapgen_rift },
{ "hellmouth", &mapgen_hellmouth },
// New rock function - should be default, but isn't yet for compatibility reasons (old overmaps)
{ "empty_rock", &mapgen_rock },
// Old rock behavior, for compatibility and near caverns and slime pits
{ "rock", &mapgen_rock_partial },
{ "subway_straight", &mapgen_subway },
{ "subway_curved", &mapgen_subway },
// TODO: Add a dedicated dead-end function. For now it copies the straight section above.
{ "subway_end", &mapgen_subway },
{ "subway_tee", &mapgen_subway },
{ "subway_four_way", &mapgen_subway },
{ "sewer_straight", &mapgen_sewer_straight },
{ "sewer_curved", &mapgen_sewer_curved },
// TODO: Add a dedicated dead-end function. For now it copies the straight section above.
{ "sewer_end", &mapgen_sewer_straight },
{ "sewer_tee", &mapgen_sewer_tee },
{ "sewer_four_way", &mapgen_sewer_four_way },
{ "ants_straight", &mapgen_ants_straight },
{ "ants_curved", &mapgen_ants_curved },
// TODO: Add a dedicated dead-end function. For now it copies the straight section above.
{ "ants_end", &mapgen_ants_straight },
{ "ants_tee", &mapgen_ants_tee },
{ "ants_four_way", &mapgen_ants_four_way },
{ "ants_food", &mapgen_ants_food },
{ "ants_larvae", &mapgen_ants_larvae },
{ "ants_queen", &mapgen_ants_queen },
{ "tutorial", &mapgen_tutorial },
{ "lake_shore", &mapgen_lake_shore },
}
};
const auto iter = pointers.find( ident );
return iter == pointers.end() ? nullptr : iter->second;
}
ter_id grass_or_dirt()
{
if( one_in( 4 ) ) {
return t_grass;
}
return t_dirt;
}
ter_id clay_or_sand()
{
if( one_in( 16 ) ) {
return t_sand;
}
return t_clay;
}
void mapgen_rotate( map *m, oter_id terrain_type, bool north_is_down )
{
const auto dir = terrain_type->get_dir();
m->rotate( static_cast<int>( north_is_down ? om_direction::opposite( dir ) : dir ) );
}
/////////////////////////////////////////////////////////////////////////////////////////////////
///// builtin terrain-specific mapgen functions. big multi-overmap-tile terrains are located in
///// mapgen_functions_big.cpp
void mapgen_null( mapgendata &dat )
{
debugmsg( "Generating null terrain, please report this as a bug" );
for( int i = 0; i < SEEX * 2; i++ ) {
for( int j = 0; j < SEEY * 2; j++ ) {
dat.m.ter_set( point( i, j ), t_null );
dat.m.set_radiation( point( i, j ), 0 );
}
}
}
void mapgen_test( mapgendata &dat )
{
for( int i = 0; i < SEEX * 2; i++ ) {
for( int j = 0; j < SEEY * 2; j++ ) {
dat.m.ter_set( point( i, j ), t_grass );
dat.m.set_radiation( point( i, j ), 0 );
}
}
}
void mapgen_crater( mapgendata &dat )
{
map *const m = &dat.m;
for( int i = 0; i < 4; i++ ) {
if( dat.t_nesw[i] != "crater" ) {
dat.set_dir( i, 6 );
}
}
for( int i = 0; i < SEEX * 2; i++ ) {
for( int j = 0; j < SEEY * 2; j++ ) {
if( rng( 0, dat.w_fac ) <= i && rng( 0, dat.e_fac ) <= SEEX * 2 - 1 - i &&
rng( 0, dat.n_fac ) <= j && rng( 0, dat.s_fac ) <= SEEX * 2 - 1 - j ) {
m->ter_set( point( i, j ), t_dirt );
m->make_rubble( tripoint( i, j, m->get_abs_sub().z ), f_rubble_rock );
m->set_radiation( point( i, j ), rng( 0, 4 ) * rng( 0, 2 ) );
} else {
m->ter_set( point( i, j ), dat.groundcover() );
m->set_radiation( point( i, j ), rng( 0, 2 ) * rng( 0, 2 ) * rng( 0, 2 ) );
}
}
}
m->place_items( item_group_id( "wreckage" ), 83, point_zero, point( SEEX * 2 - 1, SEEY * 2 - 1 ),
true, dat.when() );
}
// TODO: make void map::ter_or_furn_set(const int x, const int y, const ter_furn_id & tfid);
static void ter_or_furn_set( map *m, point p, const ter_furn_id &tfid )
{
if( tfid.ter != t_null ) {
m->ter_set( p, tfid.ter );
} else if( tfid.furn != f_null ) {
m->furn_set( p, tfid.furn );
}
}
/*
* Default above ground non forested 'blank' area; typically a grassy field with a scattering of shrubs,
* but changes according to dat->region
*/
void mapgen_field( mapgendata &dat )
{
map *const m = &dat.m;
// random area of increased vegetation. Or lava / toxic sludge / etc
const bool boosted_vegetation = ( dat.region.field_coverage.boost_chance > rng( 0, 1000000 ) );
const int &mpercent_bush = ( boosted_vegetation ?
dat.region.field_coverage.boosted_mpercent_coverage :
dat.region.field_coverage.mpercent_coverage
);
// one dominant plant type ( for boosted_vegetation == true )
ter_furn_id altbush = dat.region.field_coverage.pick( true );
for( int i = 0; i < SEEX * 2; i++ ) {
for( int j = 0; j < SEEY * 2; j++ ) {
point p( i, j );
// default is
m->ter_set( p, dat.groundcover() );
// yay, a shrub ( or tombstone )
if( mpercent_bush > rng( 0, 1000000 ) ) {
if( boosted_vegetation && dat.region.field_coverage.boosted_other_mpercent > rng( 0, 1000000 ) ) {
// already chose the lucky terrain/furniture/plant/rock/etc
ter_or_furn_set( m, p, altbush );
} else {
// pick from weighted list
ter_or_furn_set( m, p, dat.region.field_coverage.pick( false ) );
}
}
}
}
// FIXME: take 'rock' out and add as regional biome setting
m->place_items( item_group_id( "field" ), 60, point_zero, point( SEEX * 2 - 1, SEEY * 2 - 1 ), true,
dat.when() );
}
void mapgen_hive( mapgendata &dat )
{
map *const m = &dat.m;
// Start with a basic forest pattern
for( int i = 0; i < SEEX * 2; i++ ) {
for( int j = 0; j < SEEY * 2; j++ ) {
int rn = rng( 0, 14 );
if( rn > 13 ) {
m->ter_set( point( i, j ), t_tree );
} else if( rn > 11 ) {
m->ter_set( point( i, j ), t_tree_young );
} else if( rn > 10 ) {
m->ter_set( point( i, j ), t_underbrush );
} else {
m->ter_set( point( i, j ), dat.groundcover() );
}
}
}
// j and i loop through appropriate hive-cell center squares
const bool is_center = dat.t_nesw[0] == "hive" && dat.t_nesw[1] == "hive" &&
dat.t_nesw[2] == "hive" && dat.t_nesw[3] == "hive";
for( int j = 5; j < SEEY * 2 - 5; j += 6 ) {
for( int i = ( j == 5 || j == 17 ? 3 : 6 ); i < SEEX * 2 - 5; i += 6 ) {
if( !one_in( 8 ) ) {
// Caps are always there
m->ter_set( point( i, j - 5 ), t_wax );
m->ter_set( point( i, j + 5 ), t_wax );
for( int k = -2; k <= 2; k++ ) {
for( int l = -1; l <= 1; l++ ) {
m->ter_set( point( i + k, j + l ), t_floor_wax );
}
}
m->add_spawn( mon_bee, 2, { i, j, m->get_abs_sub().z } );
m->add_spawn( mon_beekeeper, 1, { i, j, m->get_abs_sub().z } );
m->ter_set( point( i, j - 3 ), t_floor_wax );
m->ter_set( point( i, j + 3 ), t_floor_wax );
m->ter_set( point( i - 1, j - 2 ), t_floor_wax );
m->ter_set( point( i, j - 2 ), t_floor_wax );
m->ter_set( point( i + 1, j - 2 ), t_floor_wax );
m->ter_set( point( i - 1, j + 2 ), t_floor_wax );
m->ter_set( point( i, j + 2 ), t_floor_wax );
m->ter_set( point( i + 1, j + 2 ), t_floor_wax );
// Up to two of these get skipped; an entrance to the cell
int skip1 = rng( 0, SEEX * 2 - 1 );
int skip2 = rng( 0, SEEY * 2 - 1 );
m->ter_set( point( i - 1, j - 4 ), t_wax );
m->ter_set( point( i, j - 4 ), t_wax );
m->ter_set( point( i + 1, j - 4 ), t_wax );
m->ter_set( point( i - 2, j - 3 ), t_wax );
m->ter_set( point( i - 1, j - 3 ), t_wax );
m->ter_set( point( i + 1, j - 3 ), t_wax );
m->ter_set( point( i + 2, j - 3 ), t_wax );
m->ter_set( point( i - 3, j - 2 ), t_wax );
m->ter_set( point( i - 2, j - 2 ), t_wax );
m->ter_set( point( i + 2, j - 2 ), t_wax );
m->ter_set( point( i + 3, j - 2 ), t_wax );
m->ter_set( point( i - 3, j - 1 ), t_wax );
m->ter_set( point( i - 3, j ), t_wax );
m->ter_set( point( i - 3, j - 1 ), t_wax );
m->ter_set( point( i - 3, j + 1 ), t_wax );
m->ter_set( point( i - 3, j ), t_wax );
m->ter_set( point( i - 3, j + 1 ), t_wax );
m->ter_set( point( i - 2, j + 3 ), t_wax );
m->ter_set( point( i - 1, j + 3 ), t_wax );
m->ter_set( point( i + 1, j + 3 ), t_wax );
m->ter_set( point( i + 2, j + 3 ), t_wax );
m->ter_set( point( i - 1, j + 4 ), t_wax );
m->ter_set( point( i, j + 4 ), t_wax );
m->ter_set( point( i + 1, j + 4 ), t_wax );
if( skip1 == 0 || skip2 == 0 ) {
m->ter_set( point( i - 1, j - 4 ), t_floor_wax );
}
if( skip1 == 1 || skip2 == 1 ) {
m->ter_set( point( i, j - 4 ), t_floor_wax );
}
if( skip1 == 2 || skip2 == 2 ) {
m->ter_set( point( i + 1, j - 4 ), t_floor_wax );
}
if( skip1 == 3 || skip2 == 3 ) {
m->ter_set( point( i - 2, j - 3 ), t_floor_wax );
}
if( skip1 == 4 || skip2 == 4 ) {
m->ter_set( point( i - 1, j - 3 ), t_floor_wax );
}
if( skip1 == 5 || skip2 == 5 ) {
m->ter_set( point( i + 1, j - 3 ), t_floor_wax );
}
if( skip1 == 6 || skip2 == 6 ) {
m->ter_set( point( i + 2, j - 3 ), t_floor_wax );
}
if( skip1 == 7 || skip2 == 7 ) {
m->ter_set( point( i - 3, j - 2 ), t_floor_wax );
}
if( skip1 == 8 || skip2 == 8 ) {
m->ter_set( point( i - 2, j - 2 ), t_floor_wax );
}
if( skip1 == 9 || skip2 == 9 ) {
m->ter_set( point( i + 2, j - 2 ), t_floor_wax );
}
if( skip1 == 10 || skip2 == 10 ) {
m->ter_set( point( i + 3, j - 2 ), t_floor_wax );
}
if( skip1 == 11 || skip2 == 11 ) {
m->ter_set( point( i - 3, j - 1 ), t_floor_wax );
}
if( skip1 == 12 || skip2 == 12 ) {
m->ter_set( point( i - 3, j ), t_floor_wax );
}
if( skip1 == 13 || skip2 == 13 ) {
m->ter_set( point( i - 3, j - 1 ), t_floor_wax );
}
if( skip1 == 14 || skip2 == 14 ) {
m->ter_set( point( i - 3, j + 1 ), t_floor_wax );
}
if( skip1 == 15 || skip2 == 15 ) {
m->ter_set( point( i - 3, j ), t_floor_wax );
}
if( skip1 == 16 || skip2 == 16 ) {
m->ter_set( point( i - 3, j + 1 ), t_floor_wax );
}
if( skip1 == 17 || skip2 == 17 ) {
m->ter_set( point( i - 2, j + 3 ), t_floor_wax );
}
if( skip1 == 18 || skip2 == 18 ) {
m->ter_set( point( i - 1, j + 3 ), t_floor_wax );
}
if( skip1 == 19 || skip2 == 19 ) {
m->ter_set( point( i + 1, j + 3 ), t_floor_wax );
}
if( skip1 == 20 || skip2 == 20 ) {
m->ter_set( point( i + 2, j + 3 ), t_floor_wax );
}
if( skip1 == 21 || skip2 == 21 ) {
m->ter_set( point( i - 1, j + 4 ), t_floor_wax );
}
if( skip1 == 22 || skip2 == 22 ) {
m->ter_set( point( i, j + 4 ), t_floor_wax );
}
if( skip1 == 23 || skip2 == 23 ) {
m->ter_set( point( i + 1, j + 4 ), t_floor_wax );
}
if( is_center ) {
m->place_items( item_group_id( "hive_center" ), 90, point( i - 2, j - 2 ), point( i + 2, j + 2 ),
false,
dat.when() );
} else {
m->place_items( item_group_id( "hive" ), 80, point( i - 2, j - 2 ), point( i + 2, j + 2 ), false,
dat.when() );
}
}
}
}
if( is_center ) {
m->place_npc( point( SEEX, SEEY ), string_id<npc_template>( "apis" ) );
}
}
int terrain_type_to_nesw_array( oter_id terrain_type, bool array[4] )
{
// count and mark which directions the road goes
const auto &oter( *terrain_type );
int num_dirs = 0;
for( const auto dir : om_direction::all ) {
num_dirs += ( array[static_cast<int>( dir )] = oter.has_connection( dir ) );
}
return num_dirs;
}
// perform dist counterclockwise rotations on a nesw or neswx array
template<typename T>
void nesw_array_rotate( T *array, size_t len, size_t dist )
{
if( len == 4 ) {
while( dist-- ) {
T temp = array[0];
array[0] = array[1];
array[1] = array[2];
array[2] = array[3];
array[3] = temp;
}
} else {
while( dist-- ) {
// N E S W NE SE SW NW
T temp = array[0];
array[0] = array[4];
array[4] = array[1];
array[1] = array[5];
array[5] = array[2];
array[2] = array[6];
array[6] = array[3];
array[3] = array[7];
array[7] = temp;
}
}
}
// take x/y coordinates in a map and rotate them counterclockwise around the center
static void coord_rotate_cw( int &x, int &y, int rot )
{
for( ; rot--; ) {
int temp = y;
y = x;
x = ( SEEY * 2 - 1 ) - temp;
}
}
static bool compare_neswx( bool *a1, std::initializer_list<int> a2 )
{
return std::equal( std::begin( a2 ), std::end( a2 ), a1,
[]( int a, bool b ) {
return static_cast<bool>( a ) == b;
} );
}
// mapgen_road replaces previous mapgen_road_straight _end _curved _tee _four_way
void mapgen_road( mapgendata &dat )
{
map *const m = &dat.m;
// start by filling the whole map with grass/dirt/etc
dat.fill_groundcover();
// which and how many neighbors have sidewalks?
bool sidewalks_neswx[8] = {};
int neighbor_sidewalks = 0;
// N E S W NE SE SW NW
for( int dir = 0; dir < 8; dir++ ) {
sidewalks_neswx[dir] = dat.t_nesw[dir]->has_flag( has_sidewalk );
neighbor_sidewalks += sidewalks_neswx[dir];
}
// which of the cardinal directions get roads?
bool roads_nesw[4] = {};
int num_dirs = terrain_type_to_nesw_array( dat.terrain_type(), roads_nesw );
// if this is a dead end, extend past the middle of the tile
int dead_end_extension = ( num_dirs == 1 ? 8 : 0 );
// which way should our roads curve, based on neighbor roads?
int curvedir_nesw[4] = {};
// N E S W
for( int dir = 0; dir < 4; dir++ ) {
if( !roads_nesw[dir] || dat.t_nesw[dir]->get_type_id().str() != "road" ) {
continue;
}
// n_* contain details about the neighbor being considered
bool n_roads_nesw[4] = {};
// TODO: figure out how to call this function without creating a new oter_id object
int n_num_dirs = terrain_type_to_nesw_array( dat.t_nesw[dir], n_roads_nesw );
// if 2-way neighbor has a road facing us
if( n_num_dirs == 2 && n_roads_nesw[( dir + 2 ) % 4] ) {
// curve towards the direction the neighbor turns
// our road curves counterclockwise
if( n_roads_nesw[( dir - 1 + 4 ) % 4] ) {
curvedir_nesw[dir]--;
}
// our road curves clockwise
if( n_roads_nesw[( dir + 1 ) % 4] ) {
curvedir_nesw[dir]++;
}
}
}
// calculate how far to rotate the map so we can work with just one orientation
// also keep track of diagonal roads and plazas
int rot = 0;
bool diag = false;
int plaza_dir = -1;
bool fourways_neswx[8] = {};
// TODO: reduce amount of logical/conditional constructs here
// TODO: make plazas include adjacent tees
switch( num_dirs ) {
case 4:
// 4-way intersection
for( int dir = 0; dir < 8; dir++ ) {
fourways_neswx[dir] = ( dat.t_nesw[dir].id() == "road_nesw" ||
dat.t_nesw[dir].id() == "road_nesw_manhole" );
}
// is this the middle, or which side or corner, of a plaza?
plaza_dir = compare_neswx( fourways_neswx, {1, 1, 1, 1, 1, 1, 1, 1} ) ? 8 :
compare_neswx( fourways_neswx, {0, 1, 1, 0, 0, 1, 0, 0} ) ? 7 :
compare_neswx( fourways_neswx, {1, 1, 0, 0, 1, 0, 0, 0} ) ? 6 :
compare_neswx( fourways_neswx, {1, 0, 0, 1, 0, 0, 0, 1} ) ? 5 :
compare_neswx( fourways_neswx, {0, 0, 1, 1, 0, 0, 1, 0} ) ? 4 :
compare_neswx( fourways_neswx, {1, 1, 1, 0, 1, 1, 0, 0} ) ? 3 :
compare_neswx( fourways_neswx, {1, 1, 0, 1, 1, 0, 0, 1} ) ? 2 :
compare_neswx( fourways_neswx, {1, 0, 1, 1, 0, 0, 1, 1} ) ? 1 :
compare_neswx( fourways_neswx, {0, 1, 1, 1, 0, 1, 1, 0} ) ? 0 :
-1;
if( plaza_dir > -1 ) {
rot = plaza_dir % 4;
}
break;
case 3:
// tee
// E/S/W, rotate 180 degrees
if( !roads_nesw[0] ) {
rot = 2;
break;
}
// N/S/W, rotate 270 degrees
if( !roads_nesw[1] ) {
rot = 3;
break;
}
// N/E/S, rotate 90 degrees
if( !roads_nesw[3] ) {
rot = 1;
break;
}
// N/E/W, don't rotate
break;
case 2:
// straight or diagonal
// E/W, rotate 90 degrees
if( roads_nesw[1] && roads_nesw[3] ) {
rot = 1;
break;
}
// E/S, rotate 90 degrees
if( roads_nesw[1] && roads_nesw[2] ) {
rot = 1;
diag = true;
break;
}
// S/W, rotate 180 degrees
if( roads_nesw[2] && roads_nesw[3] ) {
rot = 2;
diag = true;
break;
}
// W/N, rotate 270 degrees
if( roads_nesw[3] && roads_nesw[0] ) {
rot = 3;
diag = true;
break;
}
// N/E, don't rotate
if( roads_nesw[0] && roads_nesw[1] ) {
diag = true;
break;
}
// N/S, don't rotate
break;
case 1:
// dead end
// E, rotate 90 degrees
if( roads_nesw[1] ) {
rot = 1;
break;
}
// S, rotate 180 degrees
if( roads_nesw[2] ) {
rot = 2;
break;
}
// W, rotate 270 degrees
if( roads_nesw[3] ) {
rot = 3;
break;
}
// N, don't rotate
break;
}
// rotate the arrays left by rot steps
nesw_array_rotate<bool>( sidewalks_neswx, 8, rot * 2 );
nesw_array_rotate<bool>( roads_nesw, 4, rot );
nesw_array_rotate<int> ( curvedir_nesw, 4, rot );
// now we have only these shapes: ' | '- -'- -|-
if( diag ) {
// diagonal roads get drawn differently from all other types
// draw sidewalks if a S/SW/W neighbor has_sidewalk
if( sidewalks_neswx[4] || sidewalks_neswx[5] || sidewalks_neswx[6] ) {
for( int y = 0; y < SEEY * 2; y++ ) {
for( int x = 0; x < SEEX * 2; x++ ) {
if( x > y - 4 && ( x < 4 || y > SEEY * 2 - 5 || y >= x ) ) {
m->ter_set( point( x, y ), t_sidewalk );
}
}
}
}
// draw diagonal road
for( int y = 0; y < SEEY * 2; y++ ) {
for( int x = 0; x < SEEX * 2; x++ ) {
if( x > y && // definitely only draw in the upper right half of the map
( ( x > 3 && y < ( SEEY * 2 - 4 ) ) || // middle, for both corners and diagonals
( x < 4 && curvedir_nesw[0] < 0 ) || // diagonal heading northwest
( y > ( SEEY * 2 - 5 ) && curvedir_nesw[1] > 0 ) ) ) { // diagonal heading southeast
if( ( x + rot / 2 ) % 4 && ( x - y == SEEX - 1 + ( 1 - ( rot / 2 ) ) ||
x - y == SEEX + ( 1 - ( rot / 2 ) ) ) ) {
m->ter_set( point( x, y ), t_pavement_y );
} else {
m->ter_set( point( x, y ), t_pavement );
}
}
}
}
} else { // normal road drawing
bool cul_de_sac = false;
// dead ends become cul de sacs, 1/3 of the time, if a neighbor has_sidewalk
if( num_dirs == 1 && one_in( 3 ) && neighbor_sidewalks ) {
cul_de_sac = true;
fill_background( m, t_sidewalk );
}
// draw normal sidewalks
for( int dir = 0; dir < 4; dir++ ) {
if( roads_nesw[dir] ) {
// sidewalk west of north road, etc
if( sidewalks_neswx[( dir + 3 ) % 4 ] || // has_sidewalk west?
sidewalks_neswx[( dir + 3 ) % 4 + 4 ] || // has_sidewalk northwest?
sidewalks_neswx[ dir ] ) { // has_sidewalk north?
int x1 = 0;
int y1 = 0;
int x2 = 3;
int y2 = SEEY - 1 + dead_end_extension;
coord_rotate_cw( x1, y1, dir );
coord_rotate_cw( x2, y2, dir );
square( m, t_sidewalk, point( x1, y1 ), point( x2, y2 ) );
}
// sidewalk east of north road, etc
if( sidewalks_neswx[( dir + 1 ) % 4 ] || // has_sidewalk east?
sidewalks_neswx[ dir + 4 ] || // has_sidewalk northeast?
sidewalks_neswx[ dir ] ) { // has_sidewalk north?
int x1 = SEEX * 2 - 5;
int y1 = 0;
int x2 = SEEX * 2 - 1;
int y2 = SEEY - 1 + dead_end_extension;
coord_rotate_cw( x1, y1, dir );
coord_rotate_cw( x2, y2, dir );
square( m, t_sidewalk, point( x1, y1 ), point( x2, y2 ) );
}
}
}
//draw dead end sidewalk
if( dead_end_extension > 0 && sidewalks_neswx[ 2 ] ) {
square( m, t_sidewalk, point( 0, SEEY + dead_end_extension ), point( SEEX * 2 - 1,
SEEY + dead_end_extension + 4 ) );
}
// draw 16-wide pavement from the middle to the edge in each road direction
// also corner pieces to curve towards diagonal neighbors
for( int dir = 0; dir < 4; dir++ ) {
if( roads_nesw[dir] ) {
int x1 = 4;
int y1 = 0;
int x2 = SEEX * 2 - 1 - 4;
int y2 = SEEY - 1 + dead_end_extension;
coord_rotate_cw( x1, y1, dir );
coord_rotate_cw( x2, y2, dir );
square( m, t_pavement, point( x1, y1 ), point( x2, y2 ) );
if( curvedir_nesw[dir] != 0 ) {
for( int x = 1; x < 4; x++ ) {
for( int y = 0; y < x; y++ ) {
int ty = y, tx = ( curvedir_nesw[dir] == -1 ? x : SEEX * 2 - 1 - x );
coord_rotate_cw( tx, ty, dir );
m->ter_set( point( tx, ty ), t_pavement );
}
}
}
}
}
// draw yellow dots on the pavement
for( int dir = 0; dir < 4; dir++ ) {
if( roads_nesw[dir] ) {
int max_y = SEEY;
if( num_dirs == 4 || ( num_dirs == 3 && dir == 0 ) ) {
// dots don't extend into some intersections
max_y = 4;
}
for( int x = SEEX - 1; x <= SEEX; x++ ) {
for( int y = 0; y < max_y; y++ ) {
if( ( y + ( ( dir + rot ) / 2 % 2 ) ) % 4 ) {
int xn = x;
int yn = y;
coord_rotate_cw( xn, yn, dir );
m->ter_set( point( xn, yn ), t_pavement_y );
}
}
}
}
}
// draw round pavement for cul de sac late, to overdraw the yellow dots
if( cul_de_sac ) {
circle( m, t_pavement, double( SEEX ) - 0.5, double( SEEY ) - 0.5, 11.0 );
}
// overwrite part of intersection with rotary/plaza
if( plaza_dir > -1 ) {
if( plaza_dir == 8 ) {
// plaza center
fill_background( m, t_sidewalk );
// TODO: something interesting here
} else if( plaza_dir < 4 ) {
// plaza side
square( m, t_pavement, point( 0, SEEY - 10 ), point( SEEX * 2 - 1, SEEY - 1 ) );
square( m, t_sidewalk, point( 0, SEEY - 2 ), point( SEEX * 2 - 1, SEEY * 2 - 1 ) );
if( one_in( 3 ) ) {
line( m, t_tree_young, point( 1, SEEY ), point( SEEX * 2 - 2, SEEY ) );
}
if( one_in( 3 ) ) {
line_furn( m, f_bench, point( 2, SEEY + 2 ), point( 5, SEEY + 2 ) );
line_furn( m, f_bench, point( 10, SEEY + 2 ), point( 13, SEEY + 2 ) );
line_furn( m, f_bench, point( 18, SEEY + 2 ), point( 21, SEEY + 2 ) );
}
} else { // plaza corner
circle( m, t_pavement, point( 0, SEEY * 2 - 1 ), 21 );
circle( m, t_sidewalk, point( 0, SEEY * 2 - 1 ), 13 );
if( one_in( 3 ) ) {
circle( m, t_tree_young, point( 0, SEEY * 2 - 1 ), 11 );
circle( m, t_sidewalk, point( 0, SEEY * 2 - 1 ), 10 );
}
if( one_in( 3 ) ) {
circle( m, t_water_sh, point( 4, SEEY * 2 - 5 ), 3 );
}
}
}
}
// spawn some vehicles
if( plaza_dir != 8 ) {
vspawn_id( neighbor_sidewalks ? "default_city" : "default_country" ).obj().apply(
*m,
num_dirs == 4 ? "road_four_way" :
num_dirs == 3 ? "road_tee" :
num_dirs == 1 ? "road_end" :
diag ? "road_curved" :
"road_straight"
);
}
// spawn some monsters
if( neighbor_sidewalks ) {
m->place_spawns( GROUP_ZOMBIE, 2, point_zero, point( SEEX * 2 - 1, SEEX * 2 - 1 ),
dat.monster_density() );
// 1 per 10 overmaps
if( one_in( 10000 ) ) {
m->add_spawn( mon_zombie_jackson, 1, { SEEX, SEEY, m->get_abs_sub().z } );
}
}
// add some items
bool plaza = ( plaza_dir > -1 );
m->place_items( item_group_id( plaza ? "trash" : "road" ), 5, point_zero, point( SEEX * 2 - 1,
SEEX * 2 - 1 ), plaza,
dat.when() );
// add a manhole if appropriate
if( dat.terrain_type() == "road_nesw_manhole" ) {
m->ter_set( point( rng( 6, SEEX * 2 - 6 ), rng( 6, SEEX * 2 - 6 ) ), t_manhole_cover );
}
// finally, unrotate the map
m->rotate( rot );
}
///////////////////
void mapgen_subway( mapgendata &dat )
{
map *const m = &dat.m;
// start by filling the whole map with grass/dirt/etc
dat.fill_groundcover();
// which of the cardinal directions get subway?
bool subway_nesw[4] = {};
int num_dirs = terrain_type_to_nesw_array( dat.terrain_type(), subway_nesw );
// N E S W
for( int dir = 0; dir < 4; dir++ ) {
if( dat.t_nesw[dir]->has_flag( subway_connection ) && !subway_nesw[dir] ) {
num_dirs++;
subway_nesw[dir] = true;
}
}
// which way should our subway curve, based on neighbor subway?
int curvedir_nesw[4] = {};
// N E S W
for( int dir = 0; dir < 4; dir++ ) {
if( !subway_nesw[dir] ) {
continue;
}
if( dat.t_nesw[dir]->get_type_id().str() != "subway" &&
!dat.t_nesw[dir]->has_flag( subway_connection ) ) {
continue;
}
// n_* contain details about the neighbor being considered
bool n_subway_nesw[4] = {};
// TODO: figure out how to call this function without creating a new oter_id object
int n_num_dirs = terrain_type_to_nesw_array( dat.t_nesw[dir], n_subway_nesw );
for( int dir = 0; dir < 4; dir++ ) {
if( dat.t_nesw[dir]->has_flag( subway_connection ) && !n_subway_nesw[dir] ) {
n_num_dirs++;
n_subway_nesw[dir] = true;
}
}
// if 2-way neighbor has a subway facing us
if( n_num_dirs == 2 && n_subway_nesw[( dir + 2 ) % 4] ) {
// curve towards the direction the neighbor turns
// our subway curves counterclockwise
if( n_subway_nesw[( dir - 1 + 4 ) % 4] ) {
curvedir_nesw[dir]--;
}
// our subway curves clockwise
if( n_subway_nesw[( dir + 1 ) % 4] ) {
curvedir_nesw[dir]++;
}
}
}
// calculate how far to rotate the map so we can work with just one orientation
// also keep track of diagonal subway
int rot = 0;
bool diag = false;
// TODO: reduce amount of logical/conditional constructs here
switch( num_dirs ) {
case 4:
// 4-way intersection
break;
case 3:
// tee
// E/S/W, rotate 180 degrees
if( !subway_nesw[0] ) {
rot = 2;
break;
}
// N/S/W, rotate 270 degrees
if( !subway_nesw[1] ) {
rot = 3;
break;
}
// N/E/S, rotate 90 degrees
if( !subway_nesw[3] ) {
rot = 1;
break;
}
// N/E/W, don't rotate
break;
case 2:
// straight or diagonal
// E/W, rotate 90 degrees
if( subway_nesw[1] && subway_nesw[3] ) {
rot = 1;
break;
}
// E/S, rotate 90 degrees
if( subway_nesw[1] && subway_nesw[2] ) {
rot = 1;
diag = true;
break;
}
// S/W, rotate 180 degrees
if( subway_nesw[2] && subway_nesw[3] ) {
rot = 2;
diag = true;
break;
}
// W/N, rotate 270 degrees
if( subway_nesw[3] && subway_nesw[0] ) {
rot = 3;
diag = true;
break;
}
// N/E, don't rotate
if( subway_nesw[0] && subway_nesw[1] ) {
diag = true;
break;
}
break; // N/S, don't rotate
case 1:
// dead end
// E, rotate 90 degrees
if( subway_nesw[1] ) {
rot = 1;
break;
}
// S, rotate 180 degrees
if( subway_nesw[2] ) {
rot = 2;
break;
}
// W, rotate 270 degrees
if( subway_nesw[3] ) {
rot = 3;
break;
}
// N, don't rotate
break;
}
// rotate the arrays left by rot steps
nesw_array_rotate<bool>( subway_nesw, 4, rot );
nesw_array_rotate<int> ( curvedir_nesw, 4, rot );
// now we have only these shapes: ' | '- -'- -|-
switch( num_dirs ) {
case 4:
// 4-way intersection
mapf::formatted_set_simple( m, point_zero,
"...^X^^^X^....^X^^^X^...\n"
"..^DX^^DX^....^XD^^XD^..\n"
".^D^X^D^X^....^X^D^X^D^.\n"
"^D^^XD^^X^^^^^^X^^DX^^D^\n"
"XXXXDXXXXXXXXXXXXXXDXXXX\n"
"^^^DX^^^X^^^^^^X^^^XD^^^\n"
"^^D^X^^^X^^^^^^X^^^X^D^^\n"
"^D^^X^^^X^^^^^^X^^^X^^D^\n"