-
Notifications
You must be signed in to change notification settings - Fork 37
/
aregion.cpp
4118 lines (3554 loc) · 94.4 KB
/
aregion.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
// START A3HEADER
//
// This source file is part of the Atlantis PBM game program.
// Copyright (C) 1995-1999 Geoff Dunbar
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program, in the file license.txt. If not, write
// to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
//
// See the Atlantis Project web page for details:
// http://www.prankster.com/project
//
// END A3HEADER
#include <stdio.h>
#include <string.h>
#include "game.h"
#include "gamedata.h"
#include "mapgen.h"
#include "namegen.h"
#include "indenter.hpp"
#include <vector>
#include <algorithm>
#include <random>
#include <ctime>
#include <cassert>
#include <unordered_set>
#include <queue>
using namespace std;
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
Location *GetUnit(AList *list, int n)
{
forlist(list) {
Location *l = (Location *) elem;
if (l->unit->num == n) return l;
}
return 0;
}
ARegionPtr *GetRegion(AList *l, int n)
{
forlist(l) {
ARegionPtr *p = (ARegionPtr *) elem;
if (p->ptr->num == n) return p;
}
return 0;
}
Farsight::Farsight()
{
faction = 0;
unit = 0;
level = 0;
observation = 0;
for (int i = 0; i < NDIRS; i++)
exits_used[i] = 0;
}
Farsight *GetFarsight(AList *l, Faction *fac)
{
forlist(l) {
Farsight *f = (Farsight *) elem;
if (f->faction == fac) return f;
}
return 0;
}
AString TownString(int i)
{
switch (i) {
case TOWN_VILLAGE:
return "village";
case TOWN_TOWN:
return "town";
case TOWN_CITY:
return "city";
}
return "huh?";
}
TownInfo::TownInfo()
{
name = 0;
pop = 0;
activity = 0;
hab = 0;
}
TownInfo::~TownInfo()
{
if (name) delete name;
}
void TownInfo::Readin(istream &f)
{
AString temp;
f >> ws >> temp;
name = new AString(temp);
f >> pop;
f >> hab;
}
void TownInfo::Writeout(ostream& f)
{
f << name->const_str() << '\n' << pop << '\n' << hab << '\n';
}
ARegion::ARegion()
{
name = new AString("Region");
xloc = 0;
yloc = 0;
buildingseq = 1;
gate = 0;
gatemonth = 0;
gateopen = 1;
town = 0;
development = 0;
maxdevelopment = 0;
habitat = 0;
immigrants = 0;
emigrants = 0;
improvement = 0;
clearskies = 0;
earthlore = 0;
phantasmal_entertainment = 0;
for (int i=0; i<NDIRS; i++)
neighbors[i] = 0;
visited = 0;
}
ARegion::~ARegion()
{
if (name) delete name;
if (town) delete town;
}
void ARegion::ZeroNeighbors()
{
for (int i=0; i<NDIRS; i++) {
neighbors[i] = 0;
}
}
void ARegion::SetName(char const *c)
{
if (name) delete name;
name = new AString(c);
}
int ARegion::produces_item(int item)
{
if(products.size() == 0) return 0;
auto p = find_if(products.begin(), products.end(), [item](Production *p) { return p->itemtype == item; });
return (p != products.end()) ? (*p)->amount : 0;
}
Production *ARegion::get_production_for_skill(int item, int skill) {
if (products.size() == 0) return nullptr;
auto p = find_if(
products.begin(),
products.end(),
[item, skill](Production *p) { return p->itemtype == item && p->skill == skill; }
);
return (p != products.end()) ? *p : nullptr;
}
int ARegion::IsNativeRace(int item)
{
TerrainType *typer = &(TerrainDefs[type]);
int coastal = sizeof(typer->coastal_races)/sizeof(typer->coastal_races[0]);
int noncoastal = sizeof(typer->races)/sizeof(typer->races[0]);
if (IsCoastal()) {
for (int i=0; i<coastal; i++) {
if (item == typer->coastal_races[i]) return 1;
}
}
for (int i=0; i<noncoastal; i++) {
if (item == typer->races[i]) return 1;
}
return 0;
}
int ARegion::GetNearestProd(int item)
{
AList regs, regs2;
AList *rptr = ®s;
AList *r2ptr = ®s2;
AList *temp;
ARegionPtr *p = new ARegionPtr;
p->ptr = this;
regs.Add(p);
for (int i=0; i<5; i++) {
forlist(rptr) {
ARegion *r = ((ARegionPtr *) elem)->ptr;
AString skname = ItemDefs[item].pSkill;
int sk = LookupSkill(&skname);
if (r->get_production_for_skill(item, sk)) {
regs.DeleteAll();
regs2.DeleteAll();
return i;
}
for (int j=0; j<NDIRS; j++) {
if (neighbors[j]) {
p = new ARegionPtr;
p->ptr = neighbors[j];
r2ptr->Add(p);
}
}
rptr->DeleteAll();
temp = rptr;
rptr = r2ptr;
r2ptr = temp;
}
}
regs.DeleteAll();
regs2.DeleteAll();
return 5;
}
std::vector<int> ARegion::GetPossibleLairs() {
std::vector<int> lairs;
TerrainType *tt = &TerrainDefs[type];
const int sz = sizeof(tt->lairs) / sizeof(tt->lairs[0]);
for (int i = 0; i < sz; i++) {
int index = tt->lairs[i];
if (index == -1) {
continue;
}
ObjectType& lair = ObjectDefs[index];
if (lair.flags & ObjectType::DISABLED) {
continue;
}
lairs.push_back(index);
}
return lairs;
}
void ARegion::LairCheck()
{
// No lair if town in region
if (town) return;
TerrainType *tt = &TerrainDefs[type];
if (!tt->lairChance) return;
int check = getrandom(100);
if (check >= tt->lairChance) return;
auto lairs = GetPossibleLairs();
if (lairs.empty()) {
return;
}
int lair = lairs[getrandom(lairs.size())];
MakeLair(lair);
}
void ARegion::MakeLair(int t)
{
Object *o = new Object(this);
o->num = buildingseq++;
o->name = new AString(AString(ObjectDefs[t].name) +
" [" + o->num + "]");
o->type = t;
o->incomplete = 0;
o->inner = -1;
objects.Add(o);
}
int ARegion::GetPoleDistance(int dir)
{
int ct = 1;
ARegion *nreg = neighbors[dir];
while (nreg) {
ct++;
nreg = nreg->neighbors[dir];
}
return ct;
}
void ARegion::Setup()
{
//
// type and location have been setup, do everything else
SetupProds(1);
SetupPop();
//
// Make the dummy object
//
Object *obj = new Object(this);
objects.Add(obj);
if (Globals->LAIR_MONSTERS_EXIST) LairCheck();
}
void ARegion::ManualSetup(const RegionSetup& settings) {
SetupProds(settings.prodWeight);
habitat = settings.habitat;
SetupHabitat(settings.terrain);
if (settings.addSettlement) {
AString* name = new AString(settings.settlementName);
AddTown(settings.settlementSize, name);
}
SetupEconomy();
objects.Add(new Object(this));
if (Globals->LAIR_MONSTERS_EXIST && settings.addLair) {
auto lairs = GetPossibleLairs();
if (!lairs.empty()) {
int lair = lairs[getrandom(lairs.size())];
MakeLair(lair);
}
}
}
int ARegion::TraceConnectedRoad(int dir, int sum, AList *con, int range, int dev)
{
ARegionPtr *rn = new ARegionPtr();
rn->ptr = this;
int isnew = 1;
forlist(con) {
ARegionPtr *reg = (ARegionPtr *) elem;
if ((reg) && (reg->ptr)) if (reg->ptr == this) isnew = 0;
}
if (isnew == 0) return sum;
con->Add(rn);
// Add bonus for connecting town
if (town) sum++;
// Add bonus if development is higher
if (development > dev + 9) sum++;
if (development * 2 > dev * 5) sum++;
// Check further along road
if (range > 0) {
for (int d=0; d<NDIRS; d++) {
if (!HasExitRoad(d)) continue;
ARegion *r = neighbors[d];
if (!r) continue;
if (dir == r->GetRealDirComp(d)) continue;
if (HasConnectingRoad(d)) sum = r->TraceConnectedRoad(d, sum, con, range-1, dev+2);
}
}
return sum;
}
int ARegion::RoadDevelopmentBonus(int range, int dev)
{
int bonus = 0;
AList *con = new AList();
ARegionPtr *rp = new ARegionPtr();
rp->ptr = this;
con->Add(rp);
for (int d=0; d<NDIRS; d++) {
if (!HasExitRoad(d)) continue;
ARegion *r = neighbors[d];
if (!r) continue;
if (HasConnectingRoad(d)) bonus = r->TraceConnectedRoad(d, bonus, con, range-1, dev);
}
return bonus;
}
// AS
void ARegion::DoDecayCheck(ARegionList *pRegs)
{
forlist (&objects) {
Object *o = (Object *) elem;
if (!(ObjectDefs[o->type].flags & ObjectType::NEVERDECAY)) {
DoDecayClicks(o, pRegs);
}
}
}
// AS
void ARegion::DoDecayClicks(Object *o, ARegionList *pRegs)
{
if (ObjectDefs[o->type].flags & ObjectType::NEVERDECAY) return;
int clicks = getrandom(GetMaxClicks());
clicks += PillageCheck();
if (clicks > ObjectDefs[o->type].maxMonthlyDecay)
clicks = ObjectDefs[o->type].maxMonthlyDecay;
o->incomplete += clicks;
if (o->incomplete > 0) {
// trigger decay event
RunDecayEvent(o, pRegs);
}
}
// AS
void ARegion::RunDecayEvent(Object *o, ARegionList *pRegs)
{
AList *pFactions;
pFactions = PresentFactions();
forlist (pFactions) {
Faction *f = ((FactionPtr *) elem)->ptr;
string tmp = string(GetDecayFlavor().const_str()) + " " + ObjectDefs[o->type].name +
" in " + ShortPrint().const_str() + ".";
f->event(tmp, "decay");
}
}
// AS
AString ARegion::GetDecayFlavor()
{
AString flavor;
int badWeather = 0;
if (weather != W_NORMAL && !clearskies) badWeather = 1;
if (!Globals->WEATHER_EXISTS) badWeather = 0;
switch (type) {
case R_PLAIN:
case R_ISLAND_PLAIN:
case R_CERAN_PLAIN1:
case R_CERAN_PLAIN2:
case R_CERAN_PLAIN3:
case R_CERAN_LAKE:
flavor = AString("Floods have damaged ");
break;
case R_DESERT:
case R_CERAN_DESERT1:
case R_CERAN_DESERT2:
case R_CERAN_DESERT3:
flavor = AString("Flashfloods have damaged ");
break;
case R_CERAN_WASTELAND:
case R_CERAN_WASTELAND1:
flavor = AString("Magical radiation has damaged ");
break;
case R_TUNDRA:
case R_CERAN_TUNDRA1:
case R_CERAN_TUNDRA2:
case R_CERAN_TUNDRA3:
if (badWeather) {
flavor = AString("Ground freezing has damaged ");
} else {
flavor = AString("Ground thaw has damaged ");
}
break;
case R_MOUNTAIN:
case R_ISLAND_MOUNTAIN:
case R_CERAN_MOUNTAIN1:
case R_CERAN_MOUNTAIN2:
case R_CERAN_MOUNTAIN3:
if (badWeather) {
flavor = AString("Avalanches have damaged ");
} else {
flavor = AString("Rockslides have damaged ");
}
break;
case R_CERAN_HILL:
case R_CERAN_HILL1:
case R_CERAN_HILL2:
flavor = AString("Quakes have damaged ");
break;
case R_FOREST:
case R_SWAMP:
case R_ISLAND_SWAMP:
case R_JUNGLE:
case R_CERAN_FOREST1:
case R_CERAN_FOREST2:
case R_CERAN_FOREST3:
case R_CERAN_MYSTFOREST:
case R_CERAN_MYSTFOREST1:
case R_CERAN_MYSTFOREST2:
case R_CERAN_SWAMP1:
case R_CERAN_SWAMP2:
case R_CERAN_SWAMP3:
case R_CERAN_JUNGLE1:
case R_CERAN_JUNGLE2:
case R_CERAN_JUNGLE3:
flavor = AString("Encroaching vegetation has damaged ");
break;
case R_CAVERN:
case R_UFOREST:
case R_TUNNELS:
case R_CERAN_CAVERN1:
case R_CERAN_CAVERN2:
case R_CERAN_CAVERN3:
case R_CERAN_UFOREST1:
case R_CERAN_UFOREST2:
case R_CERAN_UFOREST3:
case R_CERAN_TUNNELS1:
case R_CERAN_TUNNELS2:
case R_CHASM:
case R_CERAN_CHASM1:
case R_GROTTO:
case R_CERAN_GROTTO1:
case R_DFOREST:
case R_CERAN_DFOREST1:
if (badWeather) {
flavor = AString("Lava flows have damaged ");
} else {
flavor = AString("Quakes have damaged ");
}
break;
default:
flavor = AString("Unexplained phenomena have damaged ");
break;
}
return flavor;
}
// AS
int ARegion::GetMaxClicks()
{
int terrainAdd = 0;
int terrainMult = 1;
int weatherAdd = 0;
int badWeather = 0;
int maxClicks;
if (weather != W_NORMAL && !clearskies) badWeather = 1;
if (!Globals->WEATHER_EXISTS) badWeather = 0;
switch (type) {
case R_PLAIN:
case R_ISLAND_PLAIN:
case R_TUNDRA:
case R_CERAN_PLAIN1:
case R_CERAN_PLAIN2:
case R_CERAN_PLAIN3:
case R_CERAN_LAKE:
case R_CERAN_TUNDRA1:
case R_CERAN_TUNDRA2:
case R_CERAN_TUNDRA3:
terrainAdd = -1;
if (badWeather) weatherAdd = 4;
break;
case R_MOUNTAIN:
case R_ISLAND_MOUNTAIN:
case R_CERAN_MOUNTAIN1:
case R_CERAN_MOUNTAIN2:
case R_CERAN_MOUNTAIN3:
case R_CERAN_HILL:
case R_CERAN_HILL1:
case R_CERAN_HILL2:
terrainMult = 2;
if (badWeather) weatherAdd = 4;
break;
case R_FOREST:
case R_SWAMP:
case R_ISLAND_SWAMP:
case R_JUNGLE:
case R_CERAN_FOREST1:
case R_CERAN_FOREST2:
case R_CERAN_FOREST3:
case R_CERAN_MYSTFOREST:
case R_CERAN_MYSTFOREST1:
case R_CERAN_MYSTFOREST2:
case R_CERAN_SWAMP1:
case R_CERAN_SWAMP2:
case R_CERAN_SWAMP3:
case R_CERAN_JUNGLE1:
case R_CERAN_JUNGLE2:
case R_CERAN_JUNGLE3:
terrainAdd = -1;
terrainMult = 2;
if (badWeather) weatherAdd = 1;
break;
case R_DESERT:
case R_CERAN_DESERT1:
case R_CERAN_DESERT2:
case R_CERAN_DESERT3:
terrainAdd = -1;
if (badWeather) weatherAdd = 5;
case R_CAVERN:
case R_UFOREST:
case R_TUNNELS:
case R_CERAN_CAVERN1:
case R_CERAN_CAVERN2:
case R_CERAN_CAVERN3:
case R_CERAN_UFOREST1:
case R_CERAN_UFOREST2:
case R_CERAN_UFOREST3:
case R_CERAN_TUNNELS1:
case R_CERAN_TUNNELS2:
case R_CHASM:
case R_CERAN_CHASM1:
case R_GROTTO:
case R_CERAN_GROTTO1:
case R_DFOREST:
case R_CERAN_DFOREST1:
terrainAdd = 1;
terrainMult = 2;
if (badWeather) weatherAdd = 6;
break;
default:
if (badWeather) weatherAdd = 4;
break;
}
maxClicks = terrainMult * (terrainAdd + 2) + (weatherAdd + 1);
return maxClicks;
}
// AS
int ARegion::PillageCheck()
{
int pillageAdd = maxwages - wages;
if (pillageAdd > 0) return pillageAdd;
return 0;
}
// AS
int ARegion::HasRoad()
{
forlist (&objects) {
Object *o = (Object *) elem;
if (o->IsRoad() && o->incomplete < 1) return 1;
}
return 0;
}
// AS
int ARegion::HasExitRoad(int realDirection)
{
forlist (&objects) {
Object *o = (Object *) elem;
if (o->IsRoad() && o->incomplete < 1) {
if (o->type == GetRoadDirection(realDirection)) return 1;
}
}
return 0;
}
// AS
int ARegion::CountConnectingRoads()
{
int connections = 0;
for (int i = 0; i < NDIRS; i++) {
if (HasExitRoad(i) && neighbors[i] &&
HasConnectingRoad(i))
connections ++;
}
return connections;
}
// AS
int ARegion::HasConnectingRoad(int realDirection)
{
int opposite = GetRealDirComp(realDirection);
if (neighbors[realDirection] && neighbors[realDirection]->HasExitRoad(opposite)) {
return 1;
}
return 0;
}
// AS
int ARegion::GetRoadDirection(int realDirection)
{
int roadDirection = 0;
switch (realDirection) {
case D_NORTH:
roadDirection = O_ROADN;
break;
case D_NORTHEAST:
roadDirection = O_ROADNE;
break;
case D_NORTHWEST:
roadDirection = O_ROADNW;
break;
case D_SOUTH:
roadDirection = O_ROADS;
break;
case D_SOUTHEAST:
roadDirection = O_ROADSE;
break;
case D_SOUTHWEST:
roadDirection = O_ROADSW;
break;
}
return roadDirection;
}
// AS
int ARegion::GetRealDirComp(int realDirection)
{
int complementDirection = 0;
if (neighbors[realDirection]) {
ARegion *n = neighbors[realDirection];
for (int i = 0; i < NDIRS; i++)
if (n->neighbors[i] == this)
return i;
}
switch (realDirection) {
case D_NORTH:
complementDirection = D_SOUTH;
break;
case D_NORTHEAST:
complementDirection = D_SOUTHWEST;
break;
case D_NORTHWEST:
complementDirection = D_SOUTHEAST;
break;
case D_SOUTH:
complementDirection = D_NORTH;
break;
case D_SOUTHEAST:
complementDirection = D_NORTHWEST;
break;
case D_SOUTHWEST:
complementDirection = D_NORTHEAST;
break;
}
return complementDirection;
}
AString ARegion::ShortPrint()
{
AString temp = TerrainDefs[type].name;
temp += AString(" (") + xloc + "," + yloc;
ARegionArray *pArr = this->level;
if (pArr->strName) {
temp += ",";
if (Globals->EASIER_UNDERWORLD &&
(Globals->UNDERWORLD_LEVELS+Globals->UNDERDEEP_LEVELS > 1)) {
temp += AString("") + zloc + " <";
} else {
// add less explicit multilevel information about the underworld
if (zloc > 2 && zloc < Globals->UNDERWORLD_LEVELS+2) {
for (int i = zloc; i > 3; i--) {
temp += "very ";
}
temp += "deep ";
} else if ((zloc > Globals->UNDERWORLD_LEVELS+2) &&
(zloc < Globals->UNDERWORLD_LEVELS +
Globals->UNDERDEEP_LEVELS + 2)) {
for (int i = zloc; i > Globals->UNDERWORLD_LEVELS + 3; i--) {
temp += "very ";
}
temp += "deep ";
}
}
temp += *pArr->strName;
if (Globals->EASIER_UNDERWORLD &&
(Globals->UNDERWORLD_LEVELS+Globals->UNDERDEEP_LEVELS > 1)) {
temp += ">";
}
}
temp += ")";
temp += AString(" in ") + *name;
return temp;
}
AString ARegion::Print()
{
AString temp = ShortPrint();
if (town) {
temp += AString(", contains ") + *(town->name) + " [" +
TownString(town->TownType()) + "]";
}
return temp;
}
void ARegion::SetLoc(int x, int y, int z)
{
xloc = x;
yloc = y;
zloc = z;
}
void ARegion::SetGateStatus(int month)
{
if ((type == R_NEXUS) || (Globals->START_GATES_OPEN && IsStartingCity())) {
gateopen = 1;
return;
}
gateopen = 0;
for (int i = 0; i < Globals->GATES_NOT_PERENNIAL; i++) {
int dmon = gatemonth + i;
if (dmon > 11) dmon = dmon - 12;
if (dmon == month) gateopen = 1;
}
}
void ARegion::Kill(Unit *u)
{
Unit *first = 0;
forlist((&objects)) {
Object *obj = (Object *) elem;
if (obj) {
for (auto u1: obj->units) {
if (u1->faction->num == u->faction->num && u1 != u) {
first = u1;
break;
}
}
}
if (first) break;
}
if (first) {
// give u's stuff to first. Since this can modify the list via SetNum, copy thelist
ItemList itemCopy = u->items;
for(auto i: itemCopy) {
if (ItemDefs[i.type].type & IT_SHIP && first->items.GetNum(i.type) > 0) {
if (first->items.GetNum(i.type) > i.num)
first->items.SetNum(i.type, i.num);
continue;
}
if (!IsSoldier(i.type)) {
first->items.SetNum(i.type, first->items.GetNum(i.type) + i.num);
// If we're in ocean and not in a structure, make sure that
// the first unit can actually hold the stuff and not drown
// If the item would cause them to drown then they won't
// pick it up.
if (TerrainDefs[type].similar_type == R_OCEAN) {
if (first->object->type == O_DUMMY) {
if (!first->CanReallySwim()) {
first->items.SetNum(i.type, first->items.GetNum(i.type) - i.num);
}
}
}
}
u->items.SetNum(i.type, 0);
}
}
u->MoveUnit(0);
hell.push_back(u);
}
void ARegion::ClearHell()
{
hell.clear();
}
Object *ARegion::GetObject(int num)
{
forlist(&objects) {
Object *o = (Object *) elem;
if (o->num == num) return o;
}
return 0;
}
Object *ARegion::GetDummy()
{
forlist(&objects) {
Object *o = (Object *) elem;
if (o->type == O_DUMMY) return o;
}
return 0;
}
/* Checks all fleets to see if they are empty.
* Moves all units out of an empty fleet into the
* dummy object.
*/
void ARegion::CheckFleets()
{
forlist(&objects) {
Object *o = (Object *) elem;
if (o->IsFleet()) {
int bail = 0;
if (o->FleetCapacity() < 1) bail = 1;
int alive = 0;
for(auto unit: o->units) {
if (unit->IsAlive()) alive = 1;
if (bail > 0) unit->MoveUnit(GetDummy());
}
// don't remove fleets when no living units are
// aboard when they're not at sea.
if (TerrainDefs[type].similar_type != R_OCEAN) alive = 1;
if ((alive == 0) || (bail == 1)) {
objects.Remove(o);
delete o;
}
}
}
}
Unit *ARegion::GetUnit(int num)
{
forlist((&objects)) {
Object *obj = (Object *) elem;
Unit *u = obj->GetUnit(num);
if (u) {
return(u);
}
}
return 0;
}
Location *ARegion::GetLocation(UnitId *id, int faction)
{
Unit *retval = 0;
forlist(&objects) {
Object *o = (Object *) elem;
retval = o->get_unit_id(id, faction);
if (retval) {
Location *l = new Location;
l->region = this;
l->obj = o;
l->unit = retval;
return l;
}
}
return 0;
}
Unit *ARegion::GetUnitAlias(int alias, int faction)
{
forlist((&objects)) {
Object *obj = (Object *) elem;
Unit *u = obj->GetUnitAlias(alias, faction);
if (u) {
return(u);
}
}
return 0;
}
Unit *ARegion::get_unit_id(UnitId id, int faction)
{
Unit *retval = 0;
forlist(&objects) {
Object *o = (Object *) elem;
retval = o->get_unit_id(&id, faction);
if (retval) return retval;
}
return retval;
}
void ARegion::deduplicate_unit_list(std::list<UnitId>& list, int faction)
{
std::unordered_set<Unit *>seen;
for (auto id = list.begin(); id != list.end();) {
Unit *u = get_unit_id(*id, faction);
if (u && seen.find(u) != seen.end()) {
id = list.erase(id);
} else {
seen.insert(u);
++id;
}
}
}
Location *ARegionList::GetUnitId(UnitId *id, int faction, ARegion *cur)
{
Location *retval = NULL;
// Check current region first
retval = cur->GetLocation(id, faction);
if (retval) return retval;
// No? We must be looking for an existing unit.
if (!id->unitnum) return NULL;
return this->FindUnit(id->unitnum);
}
int ARegion::Present(Faction *f)
{
forlist((&objects)) {
Object *obj = (Object *) elem;
for(auto unit: obj->units)
if (unit->faction == f) return 1;
}
return 0;
}
AList *ARegion::PresentFactions()
{