-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathmap.cpp
More file actions
2982 lines (2700 loc) · 74.8 KB
/
map.cpp
File metadata and controls
2982 lines (2700 loc) · 74.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
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 "field.h"
#include "map.h"
#include "rng.h"
#include "globals.h"
#include "monster.h"
#include "game.h"
#include "attack.h"
#include "entity.h"
#include "enum.h"
#include "worldmap.h"
#include "files.h" // For SAVE_DIR
#include <fstream>
#include <sstream>
Furniture::Furniture()
{
type = NULL;
uid = -1;
}
Furniture::~Furniture()
{
}
void Furniture::set_type(Furniture_type* t)
{
type = t;
if (type) {
hp = type->hp;
}
}
void Furniture::set_uid(int id)
{
uid = id;
}
bool Furniture::is_real()
{
return (type);
}
int Furniture::get_uid()
{
return uid;
}
glyph Furniture::get_glyph()
{
if (!type) {
return glyph();
}
glyph ret = type->sym;
if (is_smashable() && hp > 0 && hp < type->hp) {
int percent = (100 * hp) / type->hp;
if (percent >= 80) {
ret = ret.hilite(c_green);
} else if (percent >= 40) {
ret = ret.hilite(c_brown);
} else {
ret = ret.hilite(c_red);
}
}
return ret;
}
int Furniture::move_cost()
{
if (!type) {
return 100;
}
return type->move_cost;
}
int Furniture::get_height()
{
if (!type) {
return 0;
}
return type->height;
}
int Furniture::get_weight()
{
if (!type) {
return 0;
}
return type->weight;
}
std::string Furniture::get_name()
{
if (!type) {
return "";
}
return type->get_name();
}
bool Furniture::has_flag(Terrain_flag flag)
{
return (type && type->has_flag(flag));
}
bool Furniture::is_smashable()
{
return (type && type->smashable);
}
std::string Furniture::smash(Damage_set dam)
{
if (!is_smashable()) { // This verifies that terrain != NULL
return "";
}
Terrain_smash smash = type->smash;
if (rng(1, 100) <= smash.ignore_chance) {
return smash.failure_sound; // Make our "saving throw"
}
if (damage(dam)) {
return smash.success_sound;
}
return smash.failure_sound;
}
// Roll all damage types, but only apply whichever is the best.
bool Furniture::damage(Damage_set dam)
{
if (!type || type->hp == 0) {
return false;
}
int best_dmg = 0;
for (int i = 0; i < DAMAGE_MAX; i++) {
Damage_type damtype = Damage_type(i);
int dmg = dam.get_damage(damtype) - type->smash.armor[damtype].roll();
if (dmg > best_dmg) {
best_dmg = dmg;
}
}
hp -= best_dmg;
if (hp <= 0) {
return true;
}
return false;
}
bool Furniture::damage(Damage_type damtype, int dam)
{
if (dam <= 0) {
return false;
}
if (!type || type->hp == 0) {
return false;
}
Dice armor = type->smash.armor[damtype];
dam -= armor.roll();
if (dam <= 0) {
return false;
}
hp -= dam;
if (hp <= 0) {
return true;
}
return false;
}
void Furniture::destroy()
{
type = NULL;
uid = -1;
}
std::string Furniture::save_data()
{
if (!type) {
return "Done";
}
std::stringstream ret;
ret << "Type: " << type->name << std::endl; // Name is a persistant unique ID
ret << "HP: " << hp << std::endl;
ret << "UID: " << uid << std::endl;
ret << "Done";
return ret.str();
}
bool Furniture::load_data(std::istream& data)
{
std::string ident, junk;
while (ident != "done" && !data.eof()) {
if ( ! (data >> ident) ) {
debugmsg("Couldn't read Furniture data.");
return false;
}
ident = no_caps( ident );
if (ident == "type:") {
std::string tmpname;
std::getline(data, tmpname);
tmpname = trim( tmpname );
type = FURNITURE_TYPES.lookup_name(tmpname);
if (!type) {
debugmsg("Unknown furniture '%s'", tmpname.c_str());
return false;
}
} else if (ident == "hp:") {
data >> hp;
std::getline(data, junk);
} else if (ident == "uid:") {
data >> uid;
std::getline(data, junk);
} else if (ident != "done") {
debugmsg("Unknown furniture identifier '%s'", ident.c_str());
return false;
}
}
return true;
}
Sight_map::Sight_map()
{
initialized = false;
}
Sight_map::~Sight_map()
{
}
void Sight_map::make_initialized()
{
initialized = true;
}
void Sight_map::add_point(Tripoint p)
{
for (int i = 0; i < seen.size(); i++) {
if (seen[i] == p) {
return;
}
if (seen[i] >= p) {
seen.insert( seen.begin() + i, p );
return;
}
}
seen.push_back( p );
}
bool Sight_map::is_initialized()
{
return initialized;
}
bool Sight_map::can_see(Tripoint p)
{
for (int i = 0; i < seen.size(); i++) {
if (seen[i] == p) {
return true;
} else if (seen[i] > p) {
return false; // Since it's sorted, we stop once we find one > p
}
}
return false; // Should only happen on an empty vector
}
void Tile::set_terrain(Terrain* ter)
{
if (!ter) {
debugmsg("Tile::set_terrain(NULL)!");
return;
}
terrain = ter;
hp = ter->hp;
}
void Tile::add_furniture(Furniture_type* type, int uid)
{
if (!type) {
debugmsg("Tile::add_furniture(NULL)!");
return;
}
furniture.set_type(type);
furniture.set_uid(uid);
}
void Tile::add_furniture(Furniture furn)
{
furniture = furn;
}
void Tile::remove_furniture()
{
furniture.set_type(NULL);
}
glyph Tile::top_glyph()
{
if (field.is_valid()) {
return field.top_glyph();
}
if (furniture.is_real()) {
return furniture.get_glyph();
}
if (!items.empty() && (!has_flag(TF_SEALED) || !has_flag(TF_OPAQUE))) {
if (terrain && !terrain->has_flag(TF_FLOOR)) {
return terrain->sym.hilite(c_blue);
}
glyph ret = items.back().top_glyph();
if (items.size() > 1) {
ret = ret.invert();
}
return ret;
}
if (!terrain) {
return glyph();
}
glyph ret = terrain->sym;
if (is_smashable() && terrain->hp > 0 && hp < terrain->hp) {
int percent = (100 * hp) / terrain->hp;
if (percent >= 80) {
ret = ret.hilite(c_green);
} else if (percent >= 40) {
ret = ret.hilite(c_brown);
} else {
ret = ret.hilite(c_red);
}
}
return ret;
}
int Tile::move_cost()
{
if (furniture.is_real()) {
return furniture.move_cost();
}
if (!terrain) {
return 0;
}
return (terrain->movecost);
}
int Tile::get_height()
{
int ret = (terrain ? terrain->height : 0);
if (furniture.is_real()) {
ret += furniture.get_height();
}
return ret;
}
std::string Tile::get_name()
{
std::stringstream ret;
if (furniture.is_real()) {
ret << furniture.get_name() << " on ";
}
ret << (terrain ? terrain->get_name() : "<c=red>BUG - Unknown<c=/>");
return ret.str();
}
std::string Tile::get_name_indefinite()
{
std::stringstream ret;
if (furniture.is_real()) {
ret << (furniture.has_flag(TF_PLURAL) ? "some" : "a") << " " <<
furniture.get_name() << " on ";
}
if (terrain) {
ret << (terrain->has_flag(TF_PLURAL) ? "some" : "a") << " " <<
terrain->get_name();
} else {
ret << "<c=red>BUG - Unknown<c=/>";
}
return ret.str();
}
bool Tile::blocks_sense(Sense_type sense, int z_value)
{
if (!terrain) {
return false;
}
switch (sense) {
case SENSE_NULL:
return true;
case SENSE_SIGHT:
if (field.is_valid() && field.has_flag(TF_OPAQUE)) {
return true;
} else if (has_flag(TF_OPAQUE) && z_value <= get_height()) {
return true;
}
return false;
case SENSE_SOUND:
return false;
case SENSE_ECHOLOCATION:
return (move_cost() == 0);
case SENSE_SMELL:
return (move_cost() == 0);
case SENSE_OMNISCIENT:
return false;
case SENSE_MAX:
return false;
}
return false;
}
bool Tile::has_flag(Terrain_flag flag)
{
if (field.is_valid() && field.has_flag(flag)) {
return true;
}
if (!terrain) {
return false;
}
return terrain->has_flag(flag);
}
bool Tile::has_field()
{
return field.is_valid();
}
bool Tile::has_furniture()
{
return furniture.is_real();
}
bool Tile::is_smashable()
{
if (furniture.is_real() && furniture.is_smashable()) {
return true;
}
return (terrain && terrain->can_smash());
}
std::string Tile::smash(Damage_set dam)
{
// First check furniture
if (furniture.is_real()) {
std::string sound = furniture.smash(dam);
if (furniture.hp <= 0) { // We destroyed the furniture!
// First, add all items in the furniture's type list
Item_group* furn_items = furniture.type->components;
if (furn_items) {
for (int i = 0; i < furn_items->item_types.size(); i++) {
Item it(furn_items->item_types[i].item);
for (int n = 0; n < furn_items->item_types[i].number; n++) {
items.push_back(it);
}
}
}
// Next, destroy the furniture
furniture.destroy();
}
return sound; // We smashed furniture, we don't get to smash terrain too!
}
if (!is_smashable()) { // This also verifies that terrain != NULL
return "";
}
Terrain_smash smash = terrain->smash;
if (rng(1, 100) <= smash.ignore_chance) {
return smash.failure_sound; // Make our "saving throw"
}
if (damage(dam)) {
return smash.success_sound;
}
return smash.failure_sound;
}
// Roll all damage types; but only actually use the very best one.
bool Tile::damage(Damage_set dam)
{
if (!terrain || terrain->hp == 0) {
return false;
}
int best_dmg = 0;
for (int i = 0; i < DAMAGE_MAX; i++) {
Damage_type damtype = Damage_type(i);
int dmg = dam.get_damage(damtype) - terrain->smash.armor[damtype].roll();
if (dmg > best_dmg) {
best_dmg = dmg;
}
}
hp -= best_dmg;
if (hp <= 0) {
destroy();
return true;
}
return false;
}
bool Tile::damage(Damage_type type, int dam)
{
if (dam <= 0) {
return false;
}
if (!terrain || terrain->hp == 0) {
return false;
}
Dice armor = terrain->smash.armor[type];
dam -= armor.roll();
if (dam <= 0) {
return false;
}
hp -= dam;
if (hp <= 0) {
destroy();
return true;
}
return false;
}
void Tile::destroy()
{
// If HP is negative, then we run damage *again* with the extra damage
int extra = 0 - hp;
Terrain* result = TERRAIN.lookup_name( terrain->destroy_result );
if (!result) {
debugmsg("Tried to destroy '%s' but couldn't look up result '%s'.",
get_name().c_str(), terrain->destroy_result.c_str());
} else {
set_terrain(result);
damage(DAMAGE_NULL, extra); // See above
}
}
bool Tile::signal_applies(std::string signal)
{
signal = no_caps(signal);
signal = trim(signal);
if (!terrain || terrain->signal_handlers.count(signal) == 0) {
return false;
}
return true;
}
bool Tile::apply_signal(std::string signal, Entity* user)
{
signal = no_caps(signal);
signal = trim(signal);
std::string user_name = "";
if (user) {
user_name = user->get_name_to_player();
}
if (!terrain || !signal_applies(signal)) {
if (user) {
GAME.add_msg("Nothing to %s there.", user_name.c_str(), signal.c_str());
}
return false;
}
Terrain_signal_handler handler = terrain->signal_handlers[signal];
int success = handler.success_rate;
// Apply bonuses, if the user exists
if (user) {
// Terrain bonuses - check the flags for the terrain the user is on
// Kind of weird to check GAME.map from a tile, but... eh
Tile* user_tile = GAME.map->get_tile(user->pos);
if (user_tile) {
for (std::list<Terrain_flag_bonus>::iterator it =
handler.terrain_flag_bonuses.begin();
it != handler.terrain_flag_bonuses.end();
it++) {
if (user_tile->has_flag( it->flag )) {
success += it->amount;
}
}
}
// Stat bonuses
for (std::list<Stat_bonus>::iterator it = handler.stat_bonuses.begin();
it != handler.stat_bonuses.end();
it++) {
int stat = 0;
switch (it->stat) {
case STAT_STRENGTH: stat = user->stats.strength; break;
case STAT_DEXTERITY: stat = user->stats.dexterity; break;
case STAT_INTELLIGENCE: stat = user->stats.intelligence; break;
case STAT_PERCEPTION: stat = user->stats.perception; break;
}
// Apply stat in different ways, depending on the operator used...
switch (it->op) {
case MATH_MULTIPLY:
success += stat * it->amount;
break;
case MATH_GREATER_THAN:
if (stat > it->amount) {
success += it->amount_static;
}
break;
case MATH_GREATER_THAN_OR_EQUAL_TO:
if (stat >= it->amount) {
success += it->amount_static;
}
break;
case MATH_LESS_THAN:
if (stat < it->amount) {
success += it->amount_static;
}
break;
case MATH_LESS_THAN_OR_EQUAL_TO:
if (stat <= it->amount) {
success += it->amount_static;
}
break;
case MATH_EQUAL_TO:
if (stat == it->amount) {
success += it->amount_static;
}
break;
default:
debugmsg("Tile::apply_signal encountered unknown operator");
return false;
} // switch (it->symbol)
} // Iterator over handler.bonuses
} // if (user)
// We've finalized our success rate; now roll against it
if (success <= 0) {
if (user) {
GAME.add_msg("<c=red>%s (0 percent success rate)<c=/>",
handler.failure_message.c_str());
}
return true; // True since we *tried*
} else if (rng(1, 100) <= success) {
// Success!
if (handler.success_message.empty()) {
if (user) {
GAME.add_msg("<c=ltred>%s %s the %s.<c=/>",
user_name.c_str(), signal.c_str(), get_name().c_str());
}
} else if (user) {
std::stringstream mes;
mes << "<c=ltred>" << handler.success_message << "<c=/>";
GAME.add_msg(mes.str());
}
Terrain* result = TERRAIN.lookup_name(handler.result);
if (!result) {
debugmsg("Tile::apply_signal couldn't find terrain '%s'! (%s)",
handler.result.c_str(), get_name().c_str());
return false;
}
terrain = result;
return true;
}
// Failure.
if (user) {
GAME.add_msg( handler.failure_message );
}
return true; // True cause we still *tried* to...
}
std::string Tile::save_data()
{
if (!terrain) {
return "Done";
}
std::stringstream ret;
ret << "Type: " << terrain->name << std::endl; // Name is a persistant UID
ret << "HP: " << hp << std::endl;
if (field.is_valid()) {
ret << "Field: " << field.save_data() << std::endl;
}
if (furniture.is_real()) {
ret << "Furniture: " << std::endl << furniture.save_data() << std::endl;
}
for (int i = 0; i < items.size(); i++) {
ret << "Item: " << std::endl << items[i].save_data() << std::endl;
}
ret << "Done";
return ret.str();
}
bool Tile::load_data(std::istream& data)
{
std::string ident, junk;
while (ident != "done" && !data.eof()) {
if ( ! (data >> ident) ) {
debugmsg("Couldn't read data (Tile).");
return false;
}
ident = no_caps(ident);
if (ident == "type:") {
std::string tmpname;
std::getline(data, tmpname);
tmpname = trim(tmpname);
terrain = TERRAIN.lookup_name(tmpname);
if (!terrain) {
debugmsg("Unknown Terrain '%s'", tmpname.c_str());
return false;
}
} else if (ident == "hp:") {
data >> hp;
std::getline(data, junk);
} else if (ident == "field:") {
if (!field.load_data(data)) {
field = Field();
}
} else if (ident == "furniture:") {
if (!furniture.load_data(data)) {
furniture = Furniture();
}
} else if (ident == "item:") {
Item tmpit;
if (tmpit.load_data(data)) {
items.push_back(tmpit);
}
} else if (ident != "done") {
debugmsg("Unknown Tile property '%s'", ident.c_str());
return false;
}
}
return true;
}
Submap::Submap()
{
spec_used = NULL;
rotation = DIR_NULL;
level = 0;
}
Submap::~Submap()
{
}
void Submap::generate_empty()
{
Terrain* grass = TERRAIN.lookup_name("grass");
Terrain* dirt = TERRAIN.lookup_name("dirt");
if (!grass || !dirt) {
debugmsg("Couldn't find terrain for generate_empty()");
return;
}
for (int x = 0; x < SUBMAP_SIZE; x++) {
for (int y = 0; y < SUBMAP_SIZE; y++) {
tiles[x][y].set_terrain(one_in(2) ? grass : dirt);
}
}
}
void Submap::generate_open()
{
Terrain* open = TERRAIN.lookup_name("empty");
if (!open) {
debugmsg("Couldn't find terrain 'empty'; Submap::generate_open()");
return;
}
for (int x = 0; x < SUBMAP_SIZE; x++) {
for (int y = 0; y < SUBMAP_SIZE; y++) {
tiles[x][y].set_terrain(open);
}
}
}
void Submap::generate(Worldmap* map, int posx, int posy, int posz)
{
if (!map) {
debugmsg("Submap::generate(NULL, %d, %d)", posx, posy);
generate_empty();
return;
}
Worldmap_tile *tile = map->get_tile(posx, posy);
if (!tile) {
generate_empty();
return;
}
World_terrain* ter[5];
ter[0] = tile->terrain;
// North
tile = map->get_tile(posx, posy - 1);
ter[1] = (tile ? tile->terrain : NULL);
// East
tile = map->get_tile(posx + 1, posy);
ter[2] = (tile ? tile->terrain : NULL);
// South
tile = map->get_tile(posx, posy + 1);
ter[3] = (tile ? tile->terrain : NULL);
// West
tile = map->get_tile(posx - 1, posy);
ter[4] = (tile ? tile->terrain : NULL);
generate(ter, posz);
}
void Submap::generate(World_terrain* terrain[5], int posz)
{
if (!terrain[0]) {
generate_empty();
} else {
std::vector<bool> neighbor;
Mapgen_spec* spec = NULL;
// We shouldn't ever hit this; Mapgen_pool handles above-ground. But safety!
if (posz > 0) {
generate_open();
} else if (terrain[0]->has_flag(WTF_RELATIONAL)) {
neighbor.push_back(false);
for (int i = 1; i < 5; i++) {
bool nb = (terrain[i] == terrain[0]);
for (int n = 0; !nb && n < terrain[0]->connectors.size(); n++) {
std::string conn = no_caps( terrain[0]->connectors[n] );
if ( no_caps( terrain[i]->get_data_name() ) == conn ) {
nb = true;
}
}
neighbor.push_back(nb);
}
spec = MAPGEN_SPECS.random_for_terrain(terrain[0], neighbor);
} else {
spec = MAPGEN_SPECS.random_for_terrain(terrain[0], "", 0);
}
if (!spec) {
int num_conn = 0;
for (int i = 0; i < neighbor.size(); i++) {
if (neighbor[i]) {
num_conn++;
}
}
debugmsg("Mapgen::generate() failed to find spec for %s [conn=%d, z=%d]",
terrain[0]->get_data_name().c_str(), num_conn, posz);
generate_empty();
return;
}
spec->prepare(terrain);
generate( spec );
}
// If we're above ground, DON'T do adjacency maps!
if (posz > 0) {
return;
}
// Now do adjacency maps
for (int i = 1; i < 5; i++) {
if (terrain[i] && terrain[i] != terrain[0]) {
Mapgen_spec* adj = MAPGEN_SPECS.random_adjacent_to(terrain[i],terrain[0]);
if (adj) {
adj->prepare(terrain);
adj->rotate( Direction(i) );
generate_adjacent( adj );
}
}
}
}
void Submap::generate(Mapgen_spec* spec)
{
if (!spec) {
debugmsg("Null spec in Submap::generate()!");
generate_empty();
return;
}
// Set our subname to the spec's subname (defaults to empty, only matters for
// multi-story buildings
// Ditto rotation.
spec_used = spec;
subname = spec->subname;
// Rotation gets set in Mapgen_spec::prepare(), so it should still be valid here
rotation = spec->rotation;
// First, set the terrain.
for (int x = 0; x < SUBMAP_SIZE; x++) {
for (int y = 0; y < SUBMAP_SIZE; y++) {
Terrain* ter = spec->pick_terrain(x, y);
if (!ter) {
debugmsg("Generating null terrain at [%d:%d] {%s} (%s)", x, y,
spec->get_letter(x, y).c_str(), spec->get_name().c_str());
spec->debug_output();
}
tiles[x][y].set_terrain(ter);
}
}
// Next, add any furniture that needs adding.
// The Game keeps track of furniture UIDs, but so do Mapgen_specs.
// So store a std::map of what each Mapgen UID should be translated to.
std::map<int,int> map_uid_to_game_uid;
for (int x = 0; x < SUBMAP_SIZE; x++) {
for (int y = 0; y < SUBMAP_SIZE; y++) {
Furniture_type* furniture = spec->pick_furniture(x, y);
if (furniture) {
int map_uid = spec->pick_furniture_uid(x, y);
if (map_uid_to_game_uid.count(map_uid) == 0) {
map_uid_to_game_uid[map_uid] = GAME.get_furniture_uid();
}
tiles[x][y].add_furniture(furniture, map_uid_to_game_uid[map_uid]);
}
}
}
// Next, add items.
for (std::map<char,Item_area>::iterator it = spec->item_defs.begin();
it != spec->item_defs.end();
it++) {
Item_area* area = &(it->second);
area->reset();
while (area && area->place_item()) {
Point p = area->pick_location();
Item item( area->pick_type(spec->get_name()) );
if (item.get_type()) {
item.prep_for_generation();
add_item(item, p.x, p.y);
}
}
}
// Item_group_amount_areas work similarly!
for (std::map<char,Item_group_amount_area>::iterator
it = spec->item_group_defs.begin();
it != spec->item_group_defs.end();
it++) {
Item_group_amount_area* area = &(it->second);
Item_group_amount group = area->pick_group();
int amount = group.amount.roll();
for (int i = 0; i < amount; i++) {
Point p = area->pick_location();
Item item( group.group->pick_type() );
item.prep_for_generation();
add_item(item, p.x, p.y);
}
}
// Item_amount_areas work the same as Item_group_amount_areas, more or less
for (std::map<char,Item_amount_area>::iterator
it = spec->item_amount_defs.begin();
it != spec->item_amount_defs.end();
it++) {
Item_amount_area* area = &(it->second);
Item_amount item_amt = area->pick_item();
int amount = item_amt.amount.roll();
for (int i = 0; i < amount; i++) {
Point p = area->pick_location();
Item item( item_amt.item );
item.prep_for_generation();
add_item(item, p.x, p.y);
}
}
}
void Submap::generate_adjacent(Mapgen_spec* spec)
{
if (spec == NULL) {
return;
}
// First, set the terrain.
for (int x = 0; x < SUBMAP_SIZE; x++) {
for (int y = 0; y < SUBMAP_SIZE; y++) {
Terrain* tmpter = spec->pick_terrain(x, y);
// TODO: Only overwrite terrain with the "ground" tag
if (tmpter &&
(!tiles[x][y].terrain || tiles[x][y].terrain->has_flag(TF_MUTABLE))) {
tiles[x][y].set_terrain(tmpter);
}
}
}
// Next, add items.
for (std::map<char,Item_area>::iterator it = spec->item_defs.begin();