-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassignstartingplots.lua
2270 lines (1938 loc) · 65.5 KB
/
assignstartingplots.lua
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 "MapEnums"
include "MapUtilities"
------------------------------------------------------------------------------
AssignStartingPlots = {};
------------------------------------------------------------------------------
function AssignStartingPlots.Create(args)
local instance = {
-- Core Process member methods
__InitStartingData = AssignStartingPlots.__InitStartingData,
__SetStartMajor = AssignStartingPlots.__SetStartMajor,
__SetStartMinor = AssignStartingPlots.__SetStartMinor,
__GetWaterCheck = AssignStartingPlots.__GetWaterCheck,
__GetValidAdjacent = AssignStartingPlots.__GetValidAdjacent,
__NaturalWonderBuffer = AssignStartingPlots.__NaturalWonderBuffer,
__BonusResource = AssignStartingPlots.__BonusResource,
__TryToRemoveBonusResource = AssignStartingPlots.__TryToRemoveBonusResource,
__LuxuryBuffer = AssignStartingPlots.__LuxuryBuffer,
__StrategicBuffer = AssignStartingPlots.__StrategicBuffer,
__CivilizationBuffer = AssignStartingPlots.__CivilizationBuffer,
__MajorCivBuffer = AssignStartingPlots.__MajorCivBuffer,
__MinorMajorCivBuffer = AssignStartingPlots.__MinorMajorCivBuffer,
__MinorMinorCivBuffer = AssignStartingPlots.__MinorMinorCivBuffer,
__BaseFertility = AssignStartingPlots.__BaseFertility,
__WeightedFertility = AssignStartingPlots.__WeightedFertility,
__AddBonusFoodProduction = AssignStartingPlots.__AddBonusFoodProduction,
__AddFood = AssignStartingPlots.__AddFood,
__AddProduction = AssignStartingPlots.__AddProduction,
__InitStartBias = AssignStartingPlots.__InitStartBias,
__StartBiasResources = AssignStartingPlots.__StartBiasResources,
__StartBiasFeatures = AssignStartingPlots.__StartBiasFeatures,
__StartBiasTerrains = AssignStartingPlots.__StartBiasTerrains,
__StartBiasRivers = AssignStartingPlots.__StartBiasRivers,
__StartBiasPlotRemoval = AssignStartingPlots.__StartBiasPlotRemoval,
__SortByArray = AssignStartingPlots.__SortByArray,
__ArraySize = AssignStartingPlots.__ArraySize,
__PreFertilitySort = AssignStartingPlots.__PreFertilitySort,
__SortByFertilityArray = AssignStartingPlots.__SortByFertilityArray,
__AddResourcesBalanced = AssignStartingPlots.__AddResourcesBalanced,
__AddResourcesLegendary = AssignStartingPlots.__AddResourcesLegendary,
__BalancedStrategic = AssignStartingPlots.__BalancedStrategic,
__FindSpecificStrategic = AssignStartingPlots.__FindSpecificStrategic,
__AddStrategic = AssignStartingPlots.__AddStrategic,
__AddLuxury = AssignStartingPlots.__AddLuxury,
__AddBonus = AssignStartingPlots.__AddBonus,
__IsContinentalDivide = AssignStartingPlots.__IsContinentalDivide,
-- Caps Lock methods
__WeightScore = AssignStartingPlots.__WeightScore,
iNumMajorCivs = 0,
iResourceEraModifier = 1;
iNumMinorCivs = 0,
iNumRegions = 0,
iDefaultNumberMajor = 0,
iDefaultNumberMinor = 0,
uiMinMajorCivFertility = args.MIN_MAJOR_CIV_FERTILITY or 0,
uiMinMinorCivFertility = args.MIN_MINOR_CIV_FERTILITY or 0,
uiStartMinY = args.START_MIN_Y or 0,
uiStartMaxY = args.START_MAX_Y or 0,
uiStartConfig = args.START_CONFIG or 2,
waterMap = args.WATER or false,
landMap = args.LAND or false,
majorStartPlots = {},
majorCopy = {},
minorStartPlots = {},
minorCopy = {},
majorList = {},
minorList = {},
player_ID_list = {},
playerstarts = {},
sortedArray = {},
sortedFertilityArray = {},
-- Team info variables (not used in the core process, but necessary to many Multiplayer map scripts)
}
instance:__InitStartingData()
return instance
end
------------------------------------------------------------------------------
function AssignStartingPlots:__WeightScore(score, weight, falseScore)
-- convert boolean "true" to 0 numeric score
if score == true then
return 0;
end
-- otherwise, convert false/nil to numeric score
return (score or falseScore or 1) * (weight or 1);
end
------------------------------------------------------------------------------
function AssignStartingPlots:__InitStartingData()
if(self.uiMinMajorCivFertility <= 0) then
self.uiMinMajorCivFertility = 5;
end
if(self.uiMinMinorCivFertility <= 0) then
self.uiMinMinorCivFertility = 5;
end
self.iNumMajorCivs = PlayerManager.GetAliveMajorsCount();
self.iNumMinorCivs = PlayerManager.GetAliveMinorsCount();
self.iNumRegions = self.iNumMajorCivs + self.iNumMinorCivs;
local iMinNumBarbarians = self.iNumMajorCivs / 2;
StartPositioner.DivideMapIntoMajorRegions(self.iNumMajorCivs, self.uiMinMajorCivFertility, self.uiMinMinorCivFertility);
local iMajorCivStartLocs = StartPositioner.GetNumMajorCivStarts();
--Find Default Number
MapSizeTypes = {};
for row in GameInfo.Maps() do
MapSizeTypes[row.RowId] = row.DefaultPlayers;
end
local sizekey = Map.GetMapSize() + 1;
local iDefaultNumberPlayers = MapSizeTypes[sizekey] or 8;
self.iDefaultNumberMajor = iDefaultNumberPlayers ;
self.iDefaultNumberMinor = math.floor(iDefaultNumberPlayers * 1.5);
self.iIndex = 0;
self.player_ID_list = {};
for i = 0, (self.iNumRegions) - 1 do
table.insert(self.player_ID_list, i);
end
self.majorList = {};
self.minorList = {};
self.majorList = PlayerManager.GetAliveMajorIDs();
self.minorList = PlayerManager.GetAliveMinorIDs();
-- Place the major civ start plots in an array
self.majorStartPlots = {};
local failed = 0;
for i = self.iNumMajorCivs - 1, 0, - 1 do
plots = StartPositioner.GetMajorCivStartPlots(i);
local startPlot = self:__SetStartMajor(plots, i);
if(startPlot ~= nil) then
StartPositioner.MarkMajorRegionUsed(i);
table.insert(self.majorStartPlots, startPlot);
info = StartPositioner.GetMajorCivStartInfo(i);
print ("ContinentType: " .. tostring(info.ContinentType));
print ("LandmassID: " .. tostring(info.LandmassID));
print ("Fertility: " .. tostring(info.Fertility));
print ("TotalPlots: " .. tostring(info.TotalPlots));
print ("WestEdge: " .. tostring(info.WestEdge));
print ("EastEdge: " .. tostring(info.EastEdge));
print ("NorthEdge: " .. tostring(info.NorthEdge));
print ("SouthEdge: " .. tostring(info.SouthEdge));
else
failed = failed + 1;
info = StartPositioner.GetMajorCivStartInfo(i);
print ("-- START FAILED MAJOR --");
print ("ContinentType: " .. tostring(info.ContinentType));
print ("LandmassID: " .. tostring(info.LandmassID));
print ("Fertility: " .. tostring(info.Fertility));
print ("TotalPlots: " .. tostring(info.TotalPlots));
print ("WestEdge: " .. tostring(info.WestEdge));
print ("EastEdge: " .. tostring(info.EastEdge));
print ("NorthEdge: " .. tostring(info.NorthEdge));
print ("SouthEdge: " .. tostring(info.SouthEdge));
print ("-- END FAILED MAJOR --");
end
end
for k, plot in ipairs(self.majorStartPlots) do
table.insert(self.majorCopy, plot);
end
--Begin Start Bias for major
self:__InitStartBias(false);
if(self.uiStartConfig == 1 ) then
self:__AddResourcesBalanced();
elseif(self.uiStartConfig == 3 ) then
self:__AddResourcesLegendary();
end
for i = 1, self.iNumMajorCivs do
local player = Players[self.majorList[i]]
if(player == nil) then
print("THIS PLAYER FAILED");
else
local hasPlot = false;
for k, v in pairs(self.playerStarts[i]) do
if(v~= nil and hasPlot == false) then
hasPlot = true;
player:SetStartingPlot(v);
print("Major Start X: ", v:GetX(), "Major Start Y: ", v:GetY());
end
end
end
end
StartPositioner.DivideMapIntoMinorRegions(self.iNumMinorCivs);
local iMinorCivStartLocs = StartPositioner.GetNumMinorCivStarts();
local i = 0;
local valid = 0;
while i <= iMinorCivStartLocs - 1 and valid < self.iNumMinorCivs do
plots = StartPositioner.GetMinorCivStartPlots(i);
local startPlot = self:__SetStartMinor(plots);
info = StartPositioner.GetMinorCivStartInfo(i);
if(startPlot ~= nil) then
table.insert(self.minorStartPlots, startPlot);
print ("Minor ContinentType: " .. tostring(info.ContinentType));
print ("Minor LandmassID: " .. tostring(info.LandmassID));
print ("Minor Fertility: " .. tostring(info.Fertility));
print ("Minor TotalPlots: " .. tostring(info.TotalPlots));
print ("Minor WestEdge: " .. tostring(info.WestEdge));
print ("Minor EastEdge: " .. tostring(info.EastEdge));
print ("Minor NorthEdge: " .. tostring(info.NorthEdge));
print ("Minor SouthEdge: " .. tostring(info.SouthEdge));
valid = valid + 1;
else
print ("-- START FAILED MINOR --");
print ("Minor ContinentType: " .. tostring(info.ContinentType));
print ("Minor LandmassID: " .. tostring(info.LandmassID));
print ("Minor Fertility: " .. tostring(info.Fertility));
print ("Minor TotalPlots: " .. tostring(info.TotalPlots));
print ("Minor WestEdge: " .. tostring(info.WestEdge));
print ("Minor EastEdge: " .. tostring(info.EastEdge));
print ("Minor NorthEdge: " .. tostring(info.NorthEdge));
print ("Minor SouthEdge: " .. tostring(info.SouthEdge));
print ("-- END FAILED MINOR --");
end
i = i + 1;
end
for k, plot in ipairs(self.minorStartPlots) do
table.insert(self.minorCopy, plot);
end
--Begin Start Bias for minor
self:__InitStartBias(true);
for i = 1, self.iNumMinorCivs do
local player = Players[self.minorList[i]]
if(player == nil) then
print("THIS PLAYER FAILED");
else
local hasPlot = false;
for k, v in pairs(self.playerStarts[i + self.iNumMajorCivs]) do
if(v~= nil and hasPlot == false) then
hasPlot = true;
player:SetStartingPlot(v);
print("Minor Start X: ", v:GetX(), "Minor Start Y: ", v:GetY());
end
end
end
end
-- XXX: debug
-- Test seed: -348648293 (standard)
-- Test seed: -355184159 (standard or huge)
-- Test seed: 1911259460 (standard, new age, crowded)
-- Test seed: -975958907 (standard, pangaea, new age, crowded)
--RevealAll(0); -- explore all
--RevealAll(1); -- reveal all
end
------------------------------------------------------------------------------
-- XXX: debug
function RevealAll(level:number)
for index = 0, Map.GetPlotCount()-1, 1 do
RevealIndex(index, level);
end
end
function RevealIndex(index:number, level:number)
local pVis = PlayersVisibility[Game.GetLocalPlayer()];
pVis:ChangeVisibilityCount(index, level or 1);
end
function RevealPlot(plot:table, level:number)
RevealIndex(plot:GetIndex(), level);
end
------------------------------------------------------------------------------
function AssignStartingPlots:__SetStartMajor(plots, iMajorIndex)
-- Sort by fertility of all the plots
-- eliminate them if they do not meet the following:
-- distance to another civilization
-- distance to a natural wonder
-- minimum production
-- minimum food
-- minimum luxuries
-- minimum strategic
-- XXX: debug
--RevealAll(1); -- reveal all
sortedPlots ={};
if plots == nil then
return;
end
local iSize = #plots;
local iContinentIndex = 1;
-- Nothing there? Just exit, returing nil
if iSize == 0 then
return;
end
for i, plot in ipairs(plots) do
row = {};
row.Plot = plot;
row.Fertility = self:__WeightedFertility(plot, iMajorIndex, true);
table.insert (sortedPlots, row);
-- XXX: debug
--RevealIndex(plot, 0);
end
if(self.uiStartConfig > 1 ) then
table.sort (sortedPlots, function(a, b) return a.Fertility > b.Fertility; end);
else
self.sortedFertilityArray = {};
sortedPlotsFertility = {};
sortedPlotsFertility = self:__PreFertilitySort(sortedPlots);
self:__SortByFertilityArray(sortedPlots, sortedPlotsFertility);
for k, v in pairs(sortedPlots) do
sortedPlots[k] = nil;
end
for i, newPlot in ipairs(self.sortedFertilityArray) do
row = {};
row.Plot = newPlot.Plot;
row.Fertility = newPlot.Fertility;
table.insert (sortedPlots, row);
end
end
local bValid = false;
local pFallback:table = nil;
local iCrunch = 0;
local iScore = 0;
while bValid == false and iSize >= iContinentIndex do
local score = 5;
bValid = true;
local NWMajor = 0;
pTempPlot = Map.GetPlotByIndex(sortedPlots[iContinentIndex].Plot);
iContinentIndex = iContinentIndex + 1;
--print("Fertility: ", sortedPlots[iContinentIndex].Fertility)
-- XXX: debug
--RevealPlot(pTempPlot);
-- Checks to see if the plot is impassable
if(pTempPlot:IsImpassable() == true) then
bValid = false;
score = score - 10;
end
-- Checks to see if the plot is water
if(pTempPlot:IsWater() == true) then
bValid = false;
score = score - 10;
end
-- Checks to see if there are any major civs in the given distance
local iMajorCivCheck = self:__WeightScore(self:__MajorCivBuffer(pTempPlot, true));
if(iMajorCivCheck ~= 0) then
bValid = false;
end
local crunch = iMajorCivCheck;
-- Checks to see if there are luxuries
if (math.ceil(self.iDefaultNumberMajor * 1.25) + self.iDefaultNumberMinor > self.iNumMinorCivs + self.iNumMajorCivs) then
local bLuxuryCheck = self:__LuxuryBuffer(pTempPlot);
if(bLuxuryCheck == false) then
bValid = false;
score = score - 1;
end
end
--Checks to see if there are strategics
-- local bStrategicCheck = self:__StrategicBuffer(pTempPlot);
-- if(bStrategicCheck == false) then
-- bValid = false;
-- end
-- Checks to see if there is fresh water or coast
local bWaterCheck = self:__GetWaterCheck(pTempPlot);
if(bWaterCheck == false) then
bValid = false;
score = score - 2;
end
local iValidAdjacentCheck = self:__WeightScore(self:__GetValidAdjacent(pTempPlot, 0, true), 1, 3);
if(iValidAdjacentCheck ~= 0) then
bValid = false;
score = score - iValidAdjacentCheck;
end
-- Checks to see if there are natural wonders in the given distance
local bNaturalWonderCheck = self:__NaturalWonderBuffer(pTempPlot, false);
if(bNaturalWonderCheck == false) then
bValid = false;
score = score - 1;
end
-- Checks to see if there are resources
if(pTempPlot:GetResourceCount() > 0) then
local bValidResource = self:__BonusResource(pTempPlot);
if(bValidResource == false) then
bValid = false;
score = score - 1;
end
end
-- Checks to see if there is an Oasis
local featureType = pTempPlot:GetFeatureType();
if(featureType == g_FEATURE_OASIS) then
bValid = false;
score = score - 1;
end
-- If the plots passes all the checks then the plot equals the temp plot
if(bValid == true) then
self:__TryToRemoveBonusResource(pTempPlot);
self:__AddBonusFoodProduction(pTempPlot);
print(string.format("major: %d:%d", pTempPlot:GetX(), pTempPlot:GetY()));
return pTempPlot;
end
-- Otherwise, remember this plot if it's the best seen so far
if iScore - iCrunch < score - crunch then
iCrunch = crunch;
iScore = score;
--print(string.format("maybe: %d:%d (%d-%d)", pTempPlot:GetX(), pTempPlot:GetY(), iScore, iCrunch));
pFallback = pTempPlot;
end
end
if pFallback then
self:__TryToRemoveBonusResource(pTempPlot);
self:__AddBonusFoodProduction(pTempPlot);
print(string.format("MAJOR: %d:%d (%d-%d)", pFallback:GetX(), pFallback:GetY(), iScore, iCrunch));
else
print(string.format("MAJOR FAILED"));
end
return pFallback;
end
------------------------------------------------------------------------------
function AssignStartingPlots:__SetStartMinor(plots)
-- Sort by fertility of all the plots
-- eliminate them if they do not meet the following:
-- distance to another civilization
-- distance to a natural wonder
-- minimum production
-- minimum food
-- XXX: debug
--RevealAll(1); -- reveal all
sortedPlots ={};
if plots == nil then
return;
end
local iSize = #plots;
-- Nothing there? Just exit, returing nil
if iSize == 0 then
return;
end
local iContinentIndex = 1;
for i, plot in ipairs(plots) do
row = {};
row.Plot = plot;
row.Fertility = self:__BaseFertility(plot);
table.insert (sortedPlots, row);
-- XXX: debug
--RevealIndex(plot, 0);
end
table.sort (sortedPlots, function(a, b) return a.Fertility > b.Fertility; end);
local bValid = false;
local pFallback:table = nil;
local iCrunch = 0;
local iScore = 0;
while bValid == false and iSize >= iContinentIndex do
local score = 3;
bValid = true;
local NWMinor = 2;
pTempPlot = Map.GetPlotByIndex(sortedPlots[iContinentIndex].Plot);
iContinentIndex = iContinentIndex + 1;
--print("Fertility: ", sortedPlots[iContinentIndex].Fertility)
-- XXX: debug
--RevealPlot(pTempPlot);
-- Checks to see if the plot is impassable
if(pTempPlot:IsImpassable() == true) then
bValid = false;
score = score - 10;
end
-- Checks to see if the plot is water
if(pTempPlot:IsWater() == true) then
bValid = false;
score = score - 10;
end
-- Checks to see if there are any major civs in the given distance
local iMajorCivCheck = self:__WeightScore(self:__MinorMajorCivBuffer(pTempPlot, true));
if(iMajorCivCheck ~= 0) then
bValid = false;
end
-- Checks to see if there are any minor civs in the given distance
local iMinorCivCheck = self:__WeightScore(self:__MinorMinorCivBuffer(pTempPlot, true));
if(iMinorCivCheck ~= 0) then
bValid = false;
end
local crunch = iMajorCivCheck + iMinorCivCheck;
local iValidAdjacentCheck = self:__WeightScore(self:__GetValidAdjacent(pTempPlot, 2, true), 1, 3);
if(iValidAdjacentCheck ~= 0) then
bValid = false;
score = score - iValidAdjacentCheck;
end
-- Checks to see if there are natural wonders in the given distance
local bNaturalWonderCheck = self:__NaturalWonderBuffer(pTempPlot, true);
if(bNaturalWonderCheck == false) then
bValid = false;
score = score - 1;
end
-- Checks to see if there are resources
if(pTempPlot:GetResourceCount() > 0) then
local bValidResource = self:__BonusResource(pTempPlot);
if(bValidResource == false) then
bValid = false;
score = score - 1;
end
end
-- Checks to see if there is an Oasis
local featureType = pTempPlot:GetFeatureType();
if(featureType == g_FEATURE_OASIS) then
bValid = false;
score = score - 1;
end
-- If the plots passes all the checks then the plot equals the temp plot
if(bValid == true) then
self:__TryToRemoveBonusResource(pTempPlot);
print(string.format("minor: %d:%d", pTempPlot:GetX(), pTempPlot:GetY()));
return pTempPlot;
end
-- Otherwise, remember this plot if it's the best seen so far
if iScore - iCrunch < score - crunch then
iCrunch = crunch;
iScore = score;
--print(string.format("maybe: %d:%d (%d-%d)", pTempPlot:GetX(), pTempPlot:GetY(), iScore, iCrunch));
pFallback = pTempPlot;
end
end
if pFallback then
self:__TryToRemoveBonusResource(pTempPlot);
print(string.format("MINOR: %d:%d (%d-%d)", pFallback:GetX(), pFallback:GetY(), iScore, iCrunch));
else
print(string.format("MINOR FAILED"));
end
return pFallback;
end
------------------------------------------------------------------------------
function AssignStartingPlots:__GetWaterCheck(plot)
--Checks to see if there is water: rivers, it is a coastal hex, or adjacent fresh water
local gridWidth, gridHeight = Map.GetGridSize();
if(plot:IsFreshWater() == true) then
return true;
elseif( plot:IsCoastalLand() == true) then
return true;
end
return false;
end
------------------------------------------------------------------------------
function AssignStartingPlots:__GetValidAdjacent(plot, minor, numeric)
local impassable = 0;
local food = 0;
local production = 0;
local water = 0;
local desert = 0;
local snow = 0;
local gridWidth, gridHeight = Map.GetGridSize();
local terrainType = plot:GetTerrainType();
-- Add to the Snow Desert counter if snow shows up
if(terrainType == g_TERRAIN_TYPE_SNOW or terrainType == g_TERRAIN_TYPE_SNOW_HILLS) then
snow = snow + 1;
end
-- Add to the Snow Desert counter if desert shows up
if(terrainType == g_TERRAIN_TYPE_DESERT or terrainType == g_TERRAIN_TYPE_DESERT_HILLS) then
desert = desert + 1;
end
local max = 0;
local min = 0;
if(minor == 0) then
max = math.ceil(gridHeight * self.uiStartMaxY / 100);
min = math.ceil(gridHeight * self.uiStartMinY / 100);
end
-- This biases the map away from the south pole, maybe an off-by-one error?
-- It matches the way the map scripts put snow on the map, however.
local polar = 0;
if(plot:GetY() <= min or plot:GetY() > gridHeight - max) then
-- distance out of bounds
local south = math.max(0, min - plot:GetY() + 1);
local north = math.max(0, plot:GetY() - (gridHeight - max));
-- penalty for being out of bounds
polar = 1 + south + north; -- minimum 2
end
for direction = 0, DirectionTypes.NUM_DIRECTION_TYPES - 1, 1 do
local adjacentPlot = Map.GetAdjacentPlot(plot:GetX(), plot:GetY(), direction);
if (adjacentPlot ~= nil) then
terrainType = adjacentPlot:GetTerrainType();
if(adjacentPlot:GetX() >= 0 and adjacentPlot:GetY() < gridHeight) then
-- Checks to see if the plot is impassable
if(adjacentPlot:IsImpassable() == true) then
impassable = impassable + 1;
end
-- Checks to see if the plot is water
if(adjacentPlot:IsWater() == true) then
water = water + 1;
end
-- Add to the Snow Desert counter if snow shows up
if(terrainType == g_TERRAIN_TYPE_SNOW or terrainType == g_TERRAIN_TYPE_SNOW_HILLS) then
snow = snow + 1;
end
-- Add to the Snow Desert counter if desert shows up
if(terrainType == g_TERRAIN_TYPE_DESERT or terrainType == g_TERRAIN_TYPE_DESERT_HILLS) then
desert = desert + 1;
end
food = food + adjacentPlot:GetYield(g_YIELD_FOOD);
production = production + adjacentPlot:GetYield(g_YIELD_PRODUCTION);
else
impassable = impassable + 1;
end
end
end
--if(minor == 0) then
-- print("X: ", plot:GetX(), " Y: ", plot:GetY(), " Food ", food, "Production: ", production);
--end
local balancedStart = 0;
if(self.uiStartConfig == 1 and minor == 0) then
balancedStart = 1;
end
local stuck = 0;
if((impassable >= 2 + minor - balancedStart or (self.landMap == true and impassable >= 2 + minor)) and self.waterMap == false) then
stuck = 1;
elseif(self.waterMap == true and impassable >= 2 + minor * 2 - balancedStart) then
stuck = 1;
elseif(water + impassable >= 4 + minor - balancedStart and self.waterMap == false) then
stuck = 1;
elseif(water >= 3 + minor - balancedStart) then
stuck = 1;
elseif(water >= 4 + minor and self.waterMap == true) then
stuck = 1;
end
local waste = 0;
if(minor == 0 and desert > 2 - balancedStart) then
waste = 1;
elseif(minor == 0 and snow > 1 - balancedStart) then
waste = 1;
elseif(minor > 0 and snow > 2) then
waste = 1;
end
-- TODO: handle special situations like all mountains & all snow
-- XXX: debug
if plot:IsImpassable() ~= true and impassable + water >= 6 then
print(string.format("STUCK: %d:%d impassable %d water %d", plot:GetX(), plot:GetY(), impassable, water));
end
local penalty = polar + stuck + waste;
return numeric and penalty or penalty == 0;
end
------------------------------------------------------------------------------
function AssignStartingPlots:__BaseFertility(plot)
-- Calculate the fertility of the starting plot
local pPlot = Map.GetPlotByIndex(plot);
local iFertility = StartPositioner.GetPlotFertility(pPlot:GetIndex(), -1);
return iFertility;
end
------------------------------------------------------------------------------
function AssignStartingPlots:__WeightedFertility(plot, iMajorIndex, bCheckOthers)
-- Calculate the fertility of the starting plot
local pPlot = Map.GetPlotByIndex(plot);
local iFertility = StartPositioner.GetPlotFertility(pPlot:GetIndex(), iMajorIndex, bCheckOthers);
return iFertility;
end
------------------------------------------------------------------------------
function AssignStartingPlots:__NaturalWonderBuffer(plot, minor)
-- Returns false if the player can start because there is a natural wonder too close.
-- If Start position config equals legendary you can start near Natural wonders
if(self.uiStartConfig == 3) then
return true;
end
local iMaxNW = 4;
if(minor == true) then
iMaxNW = GlobalParameters.START_DISTANCE_MINOR_NATURAL_WONDER or 3;
else
iMaxNW = GlobalParameters.START_DISTANCE_MAJOR_NATURAL_WONDER or 3;
end
local plotX = plot:GetX();
local plotY = plot:GetY();
for dx = -iMaxNW, iMaxNW do
for dy = -iMaxNW,iMaxNW do
local otherPlot = Map.GetPlotXY(plotX, plotY, dx, dy, iMaxNW);
if(otherPlot and otherPlot:IsNaturalWonder()) then
return false;
end
end
end
return true;
end
------------------------------------------------------------------------------
function AssignStartingPlots:__BonusResource(plot)
--Finds bonus resources
local iResourcesInDB = 0;
eResourceType = {};
eResourceClassType = {};
for row in GameInfo.Resources() do
eResourceType[iResourcesInDB] = row.Index;
eResourceClassType[iResourcesInDB] = row.ResourceClassType;
iResourcesInDB = iResourcesInDB + 1;
end
--Find bonus resource
for row = 0, iResourcesInDB do
if (eResourceClassType[row] == "RESOURCECLASS_BONUS") then
if(eResourceType[row] == plot:GetResourceType()) then
return true;
end
end
end
return false;
end
------------------------------------------------------------------------------
function AssignStartingPlots:__TryToRemoveBonusResource(plot)
--Removes Bonus Resources underneath starting players
--Remove bonus resource
local iResourcesInDB = 0;
eResourceType = {};
eResourceClassType = {};
for row in GameInfo.Resources() do
eResourceType[iResourcesInDB] = row.Index;
eResourceClassType[iResourcesInDB] = row.ResourceClassType;
iResourcesInDB = iResourcesInDB + 1;
end
for row = 0, iResourcesInDB do
if (eResourceClassType[row] == "RESOURCECLASS_BONUS") then
if(eResourceType[row] == plot:GetResourceType()) then
ResourceBuilder.SetResourceType(plot, -1);
end
end
end
end
------------------------------------------------------------------------------
function AssignStartingPlots:__LuxuryBuffer(plot)
-- Checks to see if there are luxuries in the given distance
local iResourcesInDB = 0;
eResourceType = {};
eResourceClassType = {};
for row in GameInfo.Resources() do
eResourceType[iResourcesInDB] = row.Index;
eResourceClassType[iResourcesInDB] = row.ResourceClassType;
iResourcesInDB = iResourcesInDB + 1;
end
local plotX = plot:GetX();
local plotY = plot:GetY();
for dx = -2, 2 do
for dy = -2,2 do
local otherPlot = Map.GetPlotXY(plotX, plotY, dx, dy, 2);
if(otherPlot) then
if(otherPlot:GetResourceCount() > 0) then
for row = 0, iResourcesInDB do
if (eResourceClassType[row]== "RESOURCECLASS_LUXURY") then
if(eResourceType[row] == otherPlot:GetResourceType()) then
return true;
end
end
end
end
end
end
end
return false;
end
------------------------------------------------------------------------------
function AssignStartingPlots:__StrategicBuffer(plot)
-- Checks to see if there are strategics in the given distance
local iResourcesInDB = 0;
eResourceType = {};
eResourceClassType = {};
for row in GameInfo.Resources() do
eResourceType[iResourcesInDB] = row.Index;
eResourceClassType[iResourcesInDB] = row.ResourceClassType;
iResourcesInDB = iResourcesInDB + 1;
end
local plotX = plot:GetX();
local plotY = plot:GetY();
for dx = -2, 2 do
for dy = -2,2 do
local otherPlot = Map.GetPlotXY(plotX, plotY, dx, dy, 2);
if(otherPlot) then
if(otherPlot:GetResourceCount() > 0) then
for row = 0, iResourcesInDB do
if (eResourceClassType[row]== "RESOURCECLASS_STRATEGIC") then
if(eResourceType[row] == otherPlot:GetResourceType()) then
return true;
end
end
end
end
end
end
end
return false;
end
------------------------------------------------------------------------------
function AssignStartingPlots:__MajorCivBuffer(plot, numeric)
-- Checks to see if there are major civs in the given distance for this major civ
local iMaxStart = GlobalParameters.START_DISTANCE_MAJOR_CIVILIZATION or 9;
iMaxStart = iMaxStart - GlobalParameters.START_DISTANCE_RANGE_MAJOR or 2;
local iSourceIndex = plot:GetIndex();
-- Measure distance to nearest major civs
local iCrunch = 0; -- total encroachment
for i, majorPlot in ipairs(self.majorStartPlots) do
local iDistance = Map.GetPlotDistance(iSourceIndex, majorPlot:GetIndex());
if iDistance <= iMaxStart then
--print(string.format("close major: %d/%d", iDistance, iMaxStart));
iCrunch = iCrunch + iMaxStart - iDistance + 1;
end
end
--print(string.format("crunch: %d/%d", iCrunch, iMaxStart));
return numeric and iCrunch or iCrunch == 0;
end
------------------------------------------------------------------------------
function AssignStartingPlots:__MinorMajorCivBuffer(plot, numeric)
-- Checks to see if there are majors in the given distance for this minor civ
local iMaxStart = GlobalParameters.START_DISTANCE_MINOR_MAJOR_CIVILIZATION or 7;
local iSourceIndex = plot:GetIndex();
if(self.waterMap == true) then
iMaxStart = iMaxStart - 1;
end
-- Measure distance to nearest major civs
local iCrunch = 0; -- total encroachment
for i, majorPlot in ipairs(self.majorCopy) do
local iDistance = Map.GetPlotDistance(iSourceIndex, majorPlot:GetIndex());
if iDistance <= iMaxStart then
--print(string.format("close major: %d/%d", iDistance, iMaxStart));
iCrunch = iCrunch + iMaxStart - iDistance + 1;
end
end
--print(string.format("crunch: %d/%d", iCrunch, iMaxStart));
return numeric and iCrunch or iCrunch == 0;
end
------------------------------------------------------------------------------
function AssignStartingPlots:__MinorMinorCivBuffer(plot, numeric)
-- Checks to see if there are minors in the given distance for this minor civ
local iMaxStart = GlobalParameters.START_DISTANCE_MINOR_CIVILIZATION_START or 5;
iMaxStart = iMaxStart - GlobalParameters.START_DISTANCE_RANGE_MINOR or 2;
local iSourceIndex = plot:GetIndex();
-- Measure distance to nearest minor civs
local iCrunch = 0; -- total encroachment
for i, minorPlot in ipairs(self.minorStartPlots) do
local iDistance = Map.GetPlotDistance(iSourceIndex, minorPlot:GetIndex());
if iDistance <= iMaxStart then
--print(string.format("close minor: %d/%d", iDistance, iMaxStart));
iCrunch = iCrunch + iMaxStart - iDistance + 1;
end
end
--print(string.format("crunch: %d/%d", iCrunch, iMaxStart));
return numeric and iCrunch or iCrunch == 0;
end
------------------------------------------------------------------------------
function AssignStartingPlots:__AddBonusFoodProduction(plot)
local food = 0;
local production = 0;
local maxFood = 0;
local maxProduction = 0;
local gridWidth, gridHeight = Map.GetGridSize();
local terrainType = plot:GetTerrainType();
for direction = 0, DirectionTypes.NUM_DIRECTION_TYPES - 1, 1 do
local adjacentPlot = Map.GetAdjacentPlot(plot:GetX(), plot:GetY(), direction);
if (adjacentPlot ~= nil) then
terrainType = adjacentPlot:GetTerrainType();
if(adjacentPlot:GetX() >= 0 and adjacentPlot:GetY() < gridHeight) then
-- Gets the food and productions
food = food + adjacentPlot:GetYield(g_YIELD_FOOD);
production = production + adjacentPlot:GetYield(g_YIELD_PRODUCTION);
--Checks the maxFood
if(maxFood <= adjacentPlot:GetYield(g_YIELD_FOOD)) then
maxFood = adjacentPlot:GetYield(g_YIELD_FOOD);
end
--Checks the maxProduction
if(maxProduction <= adjacentPlot:GetYield(g_YIELD_PRODUCTION)) then
maxProduction = adjacentPlot:GetYield(g_YIELD_PRODUCTION);
end
end
end
end
if(food < 7 or maxFood < 3) then
--print("X: ", plot:GetX(), " Y: ", plot:GetY(), " Food Time");
self:__AddFood(plot);
end
if(production < 5 or maxProduction < 2) then
--print("X: ", plot:GetX(), " Y: ", plot:GetY(), " Production Time");
self:__AddProduction(plot);
end
end