forked from nedbrek/Atlantis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharegion.cpp
4264 lines (3593 loc) · 82.3 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 "aregion.h"
#include "game.h"
#include "gamedata.h"
#include "object.h"
#include "orders.h"
#include "unit.h"
#include "faction.h"
#include "fileio.h"
#include "gameio.h"
#include <stdio.h>
#include <string.h>
//----------------------------------------------------------------------------
class ARegionFlatArray
{
public:
ARegionFlatArray(int);
~ARegionFlatArray();
void SetRegion(int, ARegion *);
ARegion* GetRegion(int);
int size;
ARegion **regions;
};
//----------------------------------------------------------------------------
const char* weatherString(int w)
{
static const char *strs[] = {
"clear",
"winter",
"monsoon",
"blizzard"
};
if (w < 0 || w > W_MAX)
return "";
return strs[w];
}
//----------------------------------------------------------------------------
Location* GetUnit(AList *list, int n)
{
forlist(list)
{
Location *l = (Location*)elem;
if (l->unit->num == n)
return l;
}
return NULL;
}
ARegionPtr* GetRegion(AList *l, int n)
{
forlist(l)
{
ARegionPtr *p = (ARegionPtr*)elem;
if (p->ptr->num == n)
return p;
}
return NULL;
}
//----------------------------------------------------------------------------
Farsight::Farsight()
{
faction = NULL;
unit = NULL;
level = 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 NULL;
}
//----------------------------------------------------------------------------
static
const char* TownString(int i)
{
static const char *names[] = {
"village",
"town",
"city"
};
if (i < 0 || i >= NTOWNS)
return "huh?";
return names[i];
}
TownInfo::TownInfo()
{
name = NULL;
pop = 0;
basepop = 0;
activity = 0;
}
TownInfo::~TownInfo()
{
delete name;
}
void TownInfo::Readin(Ainfile *f, ATL_VER &)
{
name = f->GetStr();
pop = f->GetInt();
basepop = f->GetInt();
}
void TownInfo::Writeout(Aoutfile *f) const
{
f->PutStr(*name);
f->PutInt(pop);
f->PutInt(basepop);
}
int TownInfo::TownType() const
{
if (pop < Globals->CITY_POP/4) return TOWN_VILLAGE;
if (pop < Globals->CITY_POP/2) return TOWN_TOWN;
return TOWN_CITY;
}
//----------------------------------------------------------------------------
ARegion::ARegion()
{
name = new AString("Region");
xloc = 0;
yloc = 0;
buildingseq = 1;
gate = 0;
town = NULL;
clearskies = 0;
earthlore = 0;
type = -1;
race = -1;
wages = -1;
maxwages = -1;
ZeroNeighbors();
}
ARegion::~ARegion()
{
delete name;
delete town;
}
void ARegion::ZeroNeighbors()
{
for (int i = 0; i < NDIRS; ++i)
neighbors[i] = NULL;
}
void ARegion::SetName(const char *c)
{
delete name;
name = new AString(c);
}
int ARegion::Population() const
{
return population + (town ? town->pop : 0);
}
int ARegion::Wages()
{
int retval = wages;
if (town)
{
// hack, assumes that TownType + 1 = town wages
retval += town->TownType() + 1;
}
if (earthlore) retval++;
if (clearskies) retval++;
if (CountConnectingRoads() > 1) retval++;
// check Lake Wage Effect
if (Globals->LAKE_WAGE_EFFECT == GameDefs::NO_EFFECT)
return retval;
// count adjacent lakes
int adjlake = 0;
for (int d = 0; d < NDIRS; ++d)
{
ARegion *check = neighbors[d];
if (check && check->type == R_LAKE)
++adjlake;
}
if (adjlake <= 0)
return retval;
// if lakes affect everything around them
if (Globals->LAKE_WAGE_EFFECT & GameDefs::ALL)
return retval + 1;
// if lakes affect any town, even those in plains
if ((Globals->LAKE_WAGE_EFFECT & GameDefs::TOWNS) && town)
return retval + 1;
// other lake effects are only for non-plains
if (TerrainDefs[type].similar_type == R_PLAIN)
return retval;
// if lakes affect any non-plains terrain
if (Globals->LAKE_WAGE_EFFECT & GameDefs::NONPLAINS)
return retval + 1;
// if lakes affect only desert
if ((Globals->LAKE_WAGE_EFFECT & GameDefs::DESERT_ONLY) && TerrainDefs[type].similar_type == R_DESERT)
return retval + 1;
// if lakes affect towns, but only in non-plains
if ((Globals->LAKE_WAGE_EFFECT & GameDefs::NONPLAINS_TOWNS_ONLY) && town)
return retval + 1;
return retval;
}
AString ARegion::WagesForReport()
{
Production *p = products.GetProd(I_SILVER, -1);
if (p)
{
return AString("$") + p->productivity + " (Max: $" + p->amount + ")";
}
return AString("$") + 0;
}
void ARegion::SetupPop()
{
const TerrainType *const typer = &TerrainDefs[type];
const int pop = typer->pop;
if (pop == 0)
{
// wasteland or ocean
population = 0;
basepopulation = 0;
wages = 0;
maxwages = 0;
money = 0;
return;
}
const int noncoastalraces = sizeof(typer->races)/sizeof(int);
const int allraces = noncoastalraces + sizeof(typer->coastal_races)/sizeof(int);
// pick a race
race = -1;
while (race == -1 || (ItemDefs[race].flags & ItemType::DISABLED))
{
const int n = getrandom(IsCoastal() ? allraces : noncoastalraces);
if (n > noncoastalraces-1)
{
race = typer->coastal_races[n-noncoastalraces-1];
}
else
race = typer->races[n];
}
if (Globals->RANDOM_ECONOMY)
{
population = (pop + getrandom(pop)) / 2;
}
else
{
population = pop;
}
basepopulation = population;
// setup wages
int mw = typer->wages;
//--- adjust for variable maintenance cost
mw += Game::GetAvgMaintPerMan() - 10;
if (mw < 0) mw = 0;
if (Globals->RANDOM_ECONOMY)
{
mw += getrandom(5);
}
wages = mw;
maxwages = mw;
if (Globals->TOWNS_EXIST)
{
int adjacent = 0;
int prob = Globals->TOWN_PROBABILITY;
if (prob < 1) prob = 100;
int uprob = Globals->UNDERWORLD_TOWN_PROBABILITY;
if (uprob < 1) uprob = 100;
int townch = (int) 80000 / prob;
if (Globals->TOWNS_NOT_ADJACENT)
{
for (int d = 0; d < NDIRS; ++d)
{
ARegion *newregion = neighbors[d];
if (newregion && newregion->town)
++adjacent;
}
}
if (Globals->LESS_ARCTIC_TOWNS && zloc == 1)
{
const int arctic_zone_size = parentRegionArray->y / 10;
const int dnorth = GetPoleDistance(D_NORTH);
const int dsouth = GetPoleDistance(D_SOUTH);
if (dnorth < arctic_zone_size)
townch += 25 * (arctic_zone_size - dnorth) * (arctic_zone_size - dnorth) * Globals->LESS_ARCTIC_TOWNS;
if (dsouth < arctic_zone_size)
townch += 25 * (arctic_zone_size - dsouth) * (arctic_zone_size - dsouth) * Globals->LESS_ARCTIC_TOWNS;
}
int spread = Globals->TOWN_SPREAD;
if (spread > 100) spread = 100;
int townprob = (TerrainDefs[type].economy * 4 * (100 - spread) + 100 * spread) / 100;
if (zloc > 1)
townprob = (townprob * uprob) / 100;
if (adjacent > 0)
townprob = townprob * (100 - Globals->TOWNS_NOT_ADJACENT) / 100;
if (getrandom(townch) < townprob)
AddTown();
} // if town
// add wages to products
Production *p = new Production;
p->itemtype = I_SILVER;
money = Population() * (Wages() - Game::GetAvgMaintPerMan());
p->amount = money / Globals->WORK_FRACTION;
p->skill = -1;
p->level = 1;
p->productivity = Wages();
products.Add(p);
// add entertainment to products
p = new Production;
p->itemtype = I_SILVER;
p->amount = money / Globals->ENTERTAIN_FRACTION;
p->skill = S_ENTERTAINMENT;
p->level = 1;
p->productivity = Globals->ENTERTAIN_INCOME;
products.Add(p);
// add recruits to market
float ratio = ItemDefs[race].baseprice / (float)Globals->BASE_MAN_COST;
Market *m = new Market(M_BUY, race, (int)(Wages()*4*ratio),
Population()/5, 0, 10000, 0, 2000);
markets.Add(m);
// add leaders to market
if (Globals->LEADERS_EXIST)
{
ratio = ItemDefs[I_LEADERS].baseprice / (float)Globals->BASE_MAN_COST;
m = new Market(M_BUY, I_LEADERS, (int)(Wages()*4*ratio),
Population()/25, 0, 10000, 0, 400);
markets.Add(m);
}
}
int ARegion::GetNearestProd(int item)
{
AList regs, regs2; // double buffer of search windows
AList *rptr = ®s; // current list being searched
AList *r2ptr = ®s2; // next generation
// start with current region
regs.Add(new ARegionPtr(this));
// go out to 5 hex radius
for (int i = 0; i < 5; ++i)
{
//foreach region in search list
forlist(rptr)
{
ARegion *r = ((ARegionPtr*)elem)->ptr;
// if it produces the item of interest
if (r->products.GetProd(item, ItemDefs[item].pSkill))
{
// done
regs.DeleteAll();
regs2.DeleteAll();
return i;
}
// add neighbors to next generation
for (int j = 0; j < NDIRS; ++j)
{
if (neighbors[j])
{
ARegionPtr *p = new ARegionPtr;
p->ptr = neighbors[j];
r2ptr->Add(p);
}
}
// swap windows
rptr->DeleteAll();
AList *temp = rptr;
rptr = r2ptr;
r2ptr = temp;
}
}
regs.DeleteAll();
regs2.DeleteAll();
return 5;
}
void ARegion::SetupCityMarket()
{
const int citymax = Globals->CITY_POP;
int numtrade = 0;
int normalbuy = 0;
int normalsell = 0;
int advanced = 0;
int magic = 0;
//foreach item
for (int i = 0; i < NITEMS; ++i)
{
if (i == I_SILVER)
continue; // not in market
// skip if disabled or not in markets
if (ItemDefs[i].flags & ItemType::DISABLED) continue;
if (ItemDefs[i].flags & ItemType::NOMARKET) continue;
// trade items handled later
if (ItemDefs[i].type & IT_TRADE)
{
++numtrade;
continue;
}
// specific food items
if (i == I_GRAIN || i == I_LIVESTOCK || i == I_FISH)
{
if (ItemDefs[i].flags & ItemType::NOBUY) continue;
if (i == I_FISH && !IsCoastal())
continue;
int amt = Globals->CITY_MARKET_NORMAL_AMT;
int price = ItemDefs[i].baseprice;
if (Globals->RANDOM_ECONOMY)
{
amt += getrandom(amt);
price *= 100 + getrandom(50);
price /= 100;
}
int cap = (citymax * 3/4) - 1000;
if (cap < 0) cap = citymax / 2;
markets.Add(new Market(M_SELL, i, price, amt, population, population+cap, amt, amt*2));
continue;
}
// generic food
if (i == I_FOOD)
{
if (ItemDefs[i].flags & ItemType::NOSELL) continue;
int amt = Globals->CITY_MARKET_NORMAL_AMT;
int price = ItemDefs[i].baseprice;
if (Globals->RANDOM_ECONOMY)
{
amt += getrandom(amt);
price *= 120 + getrandom(80);
price /= 100;
}
int cap = citymax / 8;
if (cap < citymax) cap = citymax * 3/4;
markets.Add(new Market (M_BUY, i, price, amt, population+cap, population+6*cap, amt, amt*5));
continue;
}
if (ItemDefs[i].type & IT_NORMAL)
{
// raw material
if (ItemDefs[i].pInput[0].item == -1)
{
// check if the product can be produced in the region
bool canProduce = false;
for (unsigned c = 0;
c < (sizeof(TerrainDefs[type].prods)/sizeof(Product));
++c)
{
if (i == TerrainDefs[type].prods[c].product)
{
canProduce = true;
break;
}
}
if (canProduce)
{
if (ItemDefs[i].flags & ItemType::NOSELL) continue;
// item can be produced here
if (getrandom(2) == 0)
continue;
int amt = Globals->CITY_MARKET_NORMAL_AMT;
int price = ItemDefs[i].baseprice;
if (Globals->RANDOM_ECONOMY)
{
amt += getrandom(amt);
price *= 150 + getrandom(50);
price /= 100;
}
const int cap = citymax / 4;
// raw goods have a basic offset of 0
const int offset = (normalbuy++ * citymax * 3 / 40);
markets.Add(new Market (M_BUY, i, price, 0, population+cap+offset, population+citymax, 0, amt));
continue;
}
//else item cannot be produced in this region;
// perhaps it is in demand here?
if (!getrandom(6))
{
if (ItemDefs[i].flags & ItemType::NOBUY) continue;
int amt = Globals->CITY_MARKET_NORMAL_AMT;
int price = ItemDefs[i].baseprice;
if (Globals->RANDOM_ECONOMY)
{
amt += getrandom(amt);
price *= 100 + getrandom(50);
price /= 100;
}
const int cap = citymax / 4;
const int offset = - (citymax/20) + (normalsell++ * citymax * 3/40);
markets.Add(new Market (M_SELL, i, price, amt/6, population+cap+offset, population+citymax, 0, amt));
}
continue;
}
// finished goods
// chance to sell
if (getrandom(3) == 0)
{
if (ItemDefs[i].flags & ItemType::NOBUY) continue;
int amt = Globals->CITY_MARKET_NORMAL_AMT;
int price = ItemDefs[i].baseprice;
if (Globals->RANDOM_ECONOMY)
{
amt += getrandom(amt);
price *= 100 + getrandom(50);
price /= 100;
}
const int cap = citymax / 4;
const int offset = - (citymax/20) + (normalsell++ * citymax * 3/40);
markets.Add(new Market (M_SELL, i, price, amt/6, population+cap+offset, population+citymax, 0, amt));
continue;
}
if (ItemDefs[i].flags & ItemType::NOSELL) continue;
// chance to buy
if (getrandom(6) != 0)
continue;
int amt = Globals->CITY_MARKET_NORMAL_AMT;
int price = ItemDefs[i].baseprice;
if (Globals->RANDOM_ECONOMY)
{
amt += getrandom(amt);
price *= 150 + getrandom(50);
price /= 100;
}
const int cap = citymax / 4;
// finished goods have a higher offset than raw goods
// Offset added to maxamt too to reflect lesser production capacity
// the more items are on sale.
const int offset = (citymax/20) + (normalbuy++ * citymax * 3/40);
markets.Add(new Market(M_BUY, i, price, 0, population+cap+offset, population+citymax+offset, 0, amt));
continue;
}
// Everything else is SELL
if (ItemDefs[i].flags & ItemType::NOBUY) continue;
if (ItemDefs[i].type & IT_ADVANCED)
{
if (getrandom(4) != 2)
continue;
int amt = Globals->CITY_MARKET_ADVANCED_AMT;
int price = ItemDefs[i].baseprice;
if (Globals->RANDOM_ECONOMY)
{
amt += getrandom(amt);
price *= 100 + getrandom(50);
price /= 100;
}
int cap = (citymax * 3/4) - 1000;
if (cap < citymax/2) cap = citymax / 2;
const int offset = (citymax / 8) * advanced++;
if (cap+offset < citymax)
{
markets.Add(new Market(M_SELL, i, price, amt/6, population+cap+offset, population+citymax, 0, amt));
}
continue;
}
if ((ItemDefs[i].type & IT_MAGIC) == 0)
continue;
if (getrandom(8) != 2)
continue;
int amt = Globals->CITY_MARKET_MAGIC_AMT;
int price = ItemDefs[i].baseprice;
if (Globals->RANDOM_ECONOMY)
{
amt += getrandom(amt);
price *= 100 + getrandom(50);
price /= 100;
}
int cap = (citymax * 3/4) - 1000;
if (cap < citymax/2) cap = citymax / 2;
// TODO why is offset unused?
//int offset = (citymax / 20) + ((citymax / 5) * ((magic++) + 1));
magic++;
markets.Add(new Market (M_SELL, i, price, amt/6, population+cap, population+citymax, 0, amt));
}
// set up the trade items
int buy1 = getrandom(numtrade);
int buy2 = getrandom(numtrade);
int sell1 = getrandom(numtrade);
int sell2 = getrandom(numtrade);
buy1 = getrandom(numtrade); // TODO why twice?
while (buy1 == buy2)
buy2 = getrandom(numtrade);
while (sell1 == buy1 || sell1 == buy2)
sell1 = getrandom(numtrade);
while (sell2 == sell1 || sell2 == buy2 || sell2 == buy1)
sell2 = getrandom(numtrade);
int tradebuy = 0;
int tradesell = 0;
for (int i = 0; i < NITEMS; ++i)
{
if (ItemDefs[i].flags & ItemType::DISABLED) continue;
if (ItemDefs[i].flags & ItemType::NOMARKET) continue;
if ((ItemDefs[i].type & IT_TRADE) == 0)
continue;
int addbuy = 0;
int addsell = 0;
if (buy1 == 0 || buy2 == 0)
addbuy = 1;
buy1--;
buy2--;
if (sell1 == 0 || sell2 == 0)
addsell = 1;
sell1--;
sell2--;
if (ItemDefs[i].flags & ItemType::NOBUY) addbuy = 0;
if (ItemDefs[i].flags & ItemType::NOSELL) addsell = 0;
if (addbuy)
{
int amt = Globals->CITY_MARKET_TRADE_AMT;
int price = ItemDefs[i].baseprice;
if (Globals->RANDOM_ECONOMY)
{
amt += getrandom(amt);
if (Globals->MORE_PROFITABLE_TRADE_GOODS)
{
price *= 250 + getrandom(100);
}
else
{
price *= 150 + getrandom(50);
}
price /= 100;
}
const int cap = citymax / 2;
const int offset = - (citymax/20) + tradesell * ((tradesell + 1) * (tradesell + 1) * citymax/40);
tradesell++;
if (cap + offset < citymax)
{
markets.Add(new Market(M_SELL, i, price, amt/5, cap+population+offset, citymax+population, 0, amt));
}
}
if (addsell)
{
int amt = Globals->CITY_MARKET_TRADE_AMT;
int price = ItemDefs[i].baseprice;
{
if (Globals->RANDOM_ECONOMY)
{
amt += getrandom(amt);
if (Globals->MORE_PROFITABLE_TRADE_GOODS)
{
price *= 100 + getrandom(90);
}
else
{
price *= 100 + getrandom(50);
}
price /= 100;
}
const int cap = citymax / 2;
const int offset = tradebuy++ * (citymax/6);
if (cap+offset < citymax)
{
markets.Add(new Market (M_BUY, i, price, amt/6, cap+population+offset, citymax+population, 0, amt));
}
}
}
}
}
void ARegion::SetupProds()
{
const TerrainType *const typer = &TerrainDefs[type];
if (Globals->FOOD_ITEMS_EXIST && typer->economy)
{
if (getrandom(2) && !(ItemDefs[I_GRAIN].flags & ItemType::DISABLED))
{
products.Add(new Production(I_GRAIN, typer->economy));
}
else if (!(ItemDefs[I_LIVESTOCK].flags & ItemType::DISABLED))
{
products.Add(new Production(I_LIVESTOCK, typer->economy));
}
}
for (unsigned c = 0; c < sizeof(typer->prods)/sizeof(Product); ++c)
{
const int item = typer->prods[c].product;
if (item == -1 || (ItemDefs[item].flags & ItemType::DISABLED))
continue;
if (getrandom(100) < typer->prods[c].chance)
{
products.Add(new Production(item, typer->prods[c].amount));
}
}
}
void ARegion::AddTown()
{
town = new TownInfo;
town->name = new AString(AGetNameString(AGetName(1)));
town->pop = Globals->CITY_POP / 8;
town->activity = 0;
if (Globals->RANDOM_ECONOMY)
{
int popch = Globals->CITY_POP * 16/10;
// Underground is not affected by LESS_ARTIC_TOWNS
if (Globals->LESS_ARCTIC_TOWNS && zloc == 1)
{
const int arctic_zone_size = parentRegionArray->y / 10;
const int dnorth = GetPoleDistance(D_NORTH);
const int dsouth = GetPoleDistance(D_SOUTH);
// on small worlds, or the underworld levels, both distances
// could be less than 9, so choose the smallest
int dist = dnorth;
if (dsouth < dist) dist = dsouth;
if (dist < arctic_zone_size)
popch = popch - (arctic_zone_size - dist) * ((arctic_zone_size - dist) + 10) * 15;
}
town->pop += getrandom(popch);
}
town->basepop = town->pop;
SetupCityMarket();
}
void ARegion::LairCheck()
{
// no lair if town in region
if (town)
return;
const TerrainType *const tt = &TerrainDefs[type];
if (!tt->lairChance)
return;
// check for lair
const int check = getrandom(100);
if (check >= tt->lairChance)
return;
// pick a lairtype
int count = 0; // how many lairs are enabled
for (unsigned c = 0; c < sizeof(tt->lairs)/sizeof(int); ++c)
{
const int lair_type = tt->lairs[c];
if (lair_type == -1)
continue;
if (!(ObjectDefs[lair_type].flags & ObjectType::DISABLED))
{
++count;
}
}
// pick one
count = getrandom(count);
// find it
for (unsigned c = 0; c < sizeof(tt->lairs)/sizeof(int); ++c)
{
const int lair_type = tt->lairs[c];
if (lair_type == -1 || (ObjectDefs[lair_type].flags & ObjectType::DISABLED))
continue;
if (count)
{
--count;
continue;
}
// make it
MakeLair(lair_type);
return; // done
}
}
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;
const ARegion *nreg = neighbors[dir];
while (nreg)
{
ct++;
nreg = nreg->neighbors[dir];
}
return ct;
}
void ARegion::Setup(ARegionArray *pArr)
{
parentRegionArray = pArr;
// type and location have been setup, do everything else
SetupProds();
SetupPop();
// make the dummy object
objects.Add(new Object(this));
if (Globals->LAIR_MONSTERS_EXIST)
LairCheck();
}
void ARegion::UpdateTown()
{
if (!Globals->VARIABLE_ECONOMY)
return; // nothing to do
// check if we were a starting city and got taken over
if (!Globals->SAFE_START_CITIES && IsStartingCity() && !HasCityGuard())
{
// make sure we haven't already been modified
bool done = true;
forlist(&markets)
{
Market *m = (Market*)elem;
if (m->minamt == -1)
{
done = false;
break;
}
}
if (!done)
{
markets.DeleteAll();
SetupCityMarket();
const float ratio = ItemDefs[race].baseprice / (float)Globals->BASE_MAN_COST;
// setup recruiting
Market *m = new Market(M_BUY, race, (int)(Wages()*4*ratio),
Population()/5, 0, 10000, 0, 2000);
markets.Add(m);
if (Globals->LEADERS_EXIST)
{
const float ratio = ItemDefs[I_LEADERS].baseprice / (float)Globals->BASE_MAN_COST;