-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.nut
1312 lines (1237 loc) · 50.2 KB
/
main.nut
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
// Objectives
// 1. Maintain functionality but improve script performance
// 2. Extend script functionality into other 'post-map creation' initialization
// ex. drawing roads between towns?
// 3. Extend script to handle more uses cases -- don't hardcode cargo types
// 4. Reference appropriate documentation for game API calls
require("progress.nut");
class IndustryPlacer extends GSController {
town_industry_limit = 0;
town_radius = 0;
town_long_radius = 0;
cluster_radius = 0;
cluster_occ_pct = 0;
cluster_industry_limit = 0;
cluster_spacing = 0;
industry_spacing = 0;
industry_newgrf = 0; // change based on setting
large_town_cutoff = 0;
large_town_spacing = 0;
farm_spacing = 0;
raw_industry_min = 0;
proc_industry_min = 0;
tertiary_industry_min = 0;
debug_level = 0;
// End config set variables
company_id = 0;
build_limit = 0;
chunk_size = 256; // Change this if Valuate runs out of CPU time
town_industry_counts = GSTownList();
// Tile lists
land_tiles = GSTileList();
shore_tiles = GSTileList();
water_tiles = GSTileList();
nondesert_tiles = GSTileList();
nonsnow_tiles = GSTileList();
town_tiles = GSTileList();
outer_town_tiles = GSTileList();
core_town_tiles = GSTileList();
// Town eligibility lists: 1 for eligible in that category, 0 else
// A town is 'eligible' if any tiles in its influence are still available for industry construction
town_eligibility_default = GSTownList();
town_eligibility_water = GSTownList();
town_eligibility_shore = GSTownList();
town_eligibility_townbldg = GSTownList();
town_eligibility_neartown = GSTownList();
town_eligibility_nondesert = GSTownList();
town_eligibility_nonsnow = GSTownList();
town_eligibility_nonsnowdesert = GSTownList();
// Cluster map: tiles that are eligible for being the 'home' of a cluster
// We gradually drop tiles from this as we attempt to site clusters and build industries
cluster_eligibility_water = GSTileList();
cluster_eligibility_nondesert = GSTileList();
cluster_eligibility_nonsnow = GSTileList();
cluster_eligibility_nonsnowdesert = GSTileList();
cluster_eligibility_land = GSTileList();
farmindustry_list = [];
rawindustry_list = []; // array of raw industry type id's, set in industryconstructor.init.
rawindustry_list_count = 0; // count of primary industries, set in industryconstructor.init.
procindustry_list = []; // array of processor industry type id's, set in industryconstructor.init.
procindustry_list_count = 0; // count of secondary industries, set in industryconstructor.init.
tertiaryindustry_list = []; // array of tertiary industry type id's, set in industryconstructor.init.
tertiaryindustry_list_count = 0; // count of tertiary industries, set in industryconstructor.init.
industry_classes = GSIndustryTypeList(); // Stores the build-type of industries
industry_class_lookup = [
"Default",
"Water",
"Shore",
"TownBldg",
"NearTown",
"Nondesert",
"Nonsnow",
"Nonsnowdesert",
"Skip"];
constructor() {
this.town_industry_limit = GSController.GetSetting("town_industry_limit");
this.town_radius = GSController.GetSetting("town_radius");
this.town_long_radius = GSController.GetSetting("town_long_radius");
this.cluster_radius = GSController.GetSetting("cluster_radius");
this.cluster_occ_pct = GSController.GetSetting("cluster_occ_pct");
this.cluster_industry_limit = GSController.GetSetting("cluster_industry_limit");
this.cluster_spacing = GSController.GetSetting("cluster_spacing");
this.industry_spacing = GSController.GetSetting("industry_spacing");
this.industry_newgrf = GSController.GetSetting("industry_newgrf");
this.large_town_cutoff = GSController.GetSetting("large_town_cutoff");
this.large_town_spacing = GSController.GetSetting("large_town_spacing");
this.farm_spacing = GSController.GetSetting("farm_spacing");
this.raw_industry_min = GSController.GetSetting("raw_industry_min");
this.proc_industry_min = GSController.GetSetting("proc_industry_min");
this.tertiary_industry_min = GSController.GetSetting("tertiary_industry_min");
this.debug_level = GSController.GetSetting("debug_level");
}
}
// Save function
function IndustryPlacer::Save() {
return {};
}
// Load function
function IndustryPlacer::Load() {
}
// Program start function
function IndustryPlacer::Start() {
// We have to re-initialize this. This is due to the script state being saved in the scenario editor and interacting strangely in round trips between save files and the scenario editor.
industry_classes = GSIndustryTypeList();
this.Init();
}
function IndustryPlacer::InArray(item, array) {
for(local i = 0; i < array.len(); i++) {
if(array[i] == item) {
return true;
}
}
return false;
}
function IndustryPlacer::RegisterIndustryGRF(industry_newgrf) {
local name = "";
switch(industry_newgrf) {
case 9:
name = "XIS";
break;
case 8:
name = "FIRS 4 Steeltown";
break;
case 7:
name = "FIRS 3 Extreme";
break;
case 6:
name = "FIRS 3 In A Hot Country";
break;
case 5:
name = "FIRS 3 Steeltown";
break;
case 4:
name = "FIRS 3 Tropic Basic";
break;
case 3:
name = "FIRS 3 Arctic Basic";
break;
case 2:
name = "FIRS 3 Temperate Basic";
break;
case 1:
name = "North American FIRS";
break;
case 0:
name = "Default";
break;
}
Print("Registering " + name + " industries.", 0);
local water_based_industries = [];
local shore_based_industries = [];
local townbldg_based_industries = [];
local neartown_based_industries = [];
local nondesert_based_industries = [];
local nonsnow_based_industries = [];
local nonsnowdesert_based_industries = [];
local farm_industries = [];
local skip_industries = [];
// Overrides are for industries that we want to force into a tier or terrain type
// If the industry is in the right tier and has the right terrain type (check the first logs printed when a new map is created) then it doesn't need to be in here.
/*
* From the API docs:
* Industries might be neither raw nor processing. This is usually the
* case for industries which produce nothing (e.g. power plants), but
* also for weird industries like temperate banks and tropic lumber
* mills.
*/
local primary_override = [];
local secondary_override = [];
local tertiary_override = [];
local farm_override = [];
if(name == "Default") {
water_based_industries = [
"Oil Rig"
];
townbldg_based_industries = [
"Bank"
];
farm_override = [
"Farm"
];
}
if(name == "North American FIRS") {
water_based_industries = [
"Oil Rig",
"Fishing Grounds",
"Dredging Site"
];
shore_based_industries = [
"Bulk Terminal",
"Goods Port",
"Liquids Terminal"
];
townbldg_based_industries = [
"General Store",
"Grocery Store",
"Hardware Store",
"Smithy Forge"
];
neartown_based_industries = [
"Vehicle Dealer",
"Bank"
];
nondesert_based_industries = [
"Forestry"
];
nonsnow_based_industries = [
"Fruit Plantation"
];
nonsnowdesert_based_industries = [
"Arable Farm",
"Dairy Farm",
"Mixed Farm"
];
tertiary_override = [
"Mint",
"Smithy Forge"
];
farm_override = [
"Arable Farm",
"Mixed Farm"
];
}
if(name == "FIRS 3 Temperate Basic") {
water_based_industries = [
"Fishing Grounds",
"Dredging Site"
];
shore_based_industries = [
"Fishing Harbor",
"Port",
"Bulk Terminal"
];
nonsnowdesert_based_industries = [
"Orchard and Piggery"
];
}
if(name == "FIRS 3 Arctic Basic") {
water_based_industries = [
"Fishing Grounds"
];
shore_based_industries = [
"Bulk Terminal",
"Fishing Harbor",
"Port",
"Trading Post"
];
townbldg_based_industries = [
"General Store"
];
nondesert_based_industries = [
"Forest"
];
}
if(name == "FIRS 3 Tropic Basic") {
water_based_industries = [
"Fishing Grounds"
];
shore_based_industries = [
"Bulk Terminal",
"Fishing Harbor",
"Port"
];
townbldg_based_industries = [
"General Store"
];
nonsnowdesert_based_industries = [
"Arable Farm",
"Coffee Estate",
"Vineyard"
];
farm_override = [
"Arable Farm"
];
}
if(name == "FIRS 3 Steeltown") {
shore_based_industries = [
"Bulk Terminal",
"Port",
"Wharf"
];
townbldg_based_industries = [
"General Store"
];
neartown_based_industries = [
"Vehicle Dealer"
];
nonsnowdesert_based_industries = [
"Farm"
];
farm_override = [
"Farm"
];
skip_industries = [
"Beach"
];
}
if(name == "FIRS 3 In A Hot Country") {
shore_based_industries = [
"Bulk Terminal",
"Liquids Terminal",
"Port",
"Trading Post"
];
water_based_industries = [
"Oil Rig"
];
townbldg_based_industries = [
"General Store",
"Hardware Store"
];
nondesert_based_industries = [
"Forest"
];
nonsnow_based_industries = [
"Fruit Plantation"
];
nonsnowdesert_based_industries = [
"Arable Farm",
"Coffee Estate",
"Mixed Farm",
"Quarry",
"Rubber Plantation"
];
farm_override = [
"Arable Farm",
"Mixed Farm"
];
}
if(name == "FIRS 3 Extreme") {
shore_based_industries = [
"Bulk Terminal",
"Fishing Harbor",
"Port"
];
water_based_industries = [
"Oil Rig",
"Dredging Site",
"Fishing Grounds"
];
townbldg_based_industries = [
"Grocery Store",
"Hardware Store",
"Smithy Forge"
];
nondesert_based_industries = [
"Forest"
];
nonsnow_based_industries = [
"Fruit Plantation"
];
nonsnowdesert_based_industries = [
"Arable Farm",
"Dairy Farm",
"Mixed Farm"
];
farm_override = [
"Arable Farm",
"Mixed Farm"
];
tertiary_override = [
"Smithy Forge"
];
skip_industries = [
"Biorefinery",
"Oil Rig",
"Recycling Depot",
"Recycling Plant",
"Smithy Forge"
];
}
if(name == "FIRS 4 Steeltown") {
shore_based_industries = [
"Bulk Terminal",
"Wharf"
];
townbldg_based_industries = [
"General Store"
];
neartown_based_industries = [
"Builders Yard",
"Vehicle Distributor"
];
nonsnowdesert_based_industries = [
"Farm"
];
farm_override = [
"Farm"
];
skip_industries = [
"Beach"
];
}
if(name == "XIS") {
water_based_industries = [
"Dredging Site",
"Fishing Grounds",
"Oil Rig"
];
shore_based_industries = [
"Bulk Terminal",
"Fishing Harbour",
"Liquids Terminal",
"Port",
"Wharf"
];
townbldg_based_industries = [
"General Store",
"Grocer's Shop",
"Hardware Store"
];
neartown_based_industries = [
"Recycling Depot",
"Vehicle Dealer"
];
nondesert_based_industries = [
"Forest"
];
nonsnow_based_industries = [
"Fruit Plantation"
];
nonsnowdesert_based_industries = [
"Arable Farm",
"Dairy Farm",
"Mixed Farm",
"Rubber Plantation"
];
tertiary_override = [
"Recycling Depot",
"Fishing Harbour"
];
farm_override = [
"Arable Farm",
"Mixed Farm"
];
}
foreach(ind_id, value in industry_classes) {
local ind_name = GSIndustryType.GetName(ind_id);
if(InArray(ind_name, water_based_industries)) {
industry_classes.SetValue(ind_id, 1);
}
if(InArray(ind_name, shore_based_industries)) {
industry_classes.SetValue(ind_id, 2);
}
if(InArray(ind_name, townbldg_based_industries)) {
industry_classes.SetValue(ind_id, 3);
}
if(InArray(ind_name, neartown_based_industries)) {
industry_classes.SetValue(ind_id, 4);
}
if(InArray(ind_name, nondesert_based_industries)) {
industry_classes.SetValue(ind_id, 5);
}
if(InArray(ind_name, nonsnow_based_industries)) {
industry_classes.SetValue(ind_id, 6);
}
if(InArray(ind_name, nonsnowdesert_based_industries)) {
industry_classes.SetValue(ind_id, 7);
}
if(InArray(ind_name, skip_industries)) {
industry_classes.SetValue(ind_id, 8);
}
}
foreach(ind_id, value in GSIndustryTypeList()) {
local ind_name = GSIndustryType.GetName(ind_id);
// We have to descend down these if else statements in order
// Otherwise the overrides don't work
if(!InArray(ind_name, skip_industries)) {
if(InArray(ind_name, farm_override)) {
farmindustry_list.push(ind_id);
} else if(InArray(ind_name, primary_override)) {
rawindustry_list.push(ind_id);
} else if(InArray(ind_name, secondary_override)) {
procindustry_list.push(ind_id);
} else if(InArray(ind_name, tertiary_override)) {
tertiaryindustry_list.push(ind_id);
} else if(GSIndustryType.IsRawIndustry(ind_id)) {
rawindustry_list.push(ind_id);
} else if(GSIndustryType.IsProcessingIndustry(ind_id)) {
procindustry_list.push(ind_id);
} else {
tertiaryindustry_list.push(ind_id);
}
}
}
Print("-----Primary industries:-----", 0);
foreach(ind_id in rawindustry_list) {
Print(GSIndustryType.GetName(ind_id) + ": " + industry_class_lookup[industry_classes.GetValue(ind_id)], 0);
}
Print("-----Secondary industries:-----", 0);
foreach(ind_id in procindustry_list) {
Print(GSIndustryType.GetName(ind_id) + ": " + industry_class_lookup[industry_classes.GetValue(ind_id)], 0);
}
Print("-----Tertiary industries:-----", 0);
foreach(ind_id in tertiaryindustry_list) {
Print(GSIndustryType.GetName(ind_id) + ": " + industry_class_lookup[industry_classes.GetValue(ind_id)], 0);
}
Print("-----Farm industries:-----", 0);
foreach(ind_id in farmindustry_list) {
Print(GSIndustryType.GetName(ind_id) + ": " + industry_class_lookup[industry_classes.GetValue(ind_id)], 0);
}
Print("-----Registration done.-----", 0)
}
// Initialization function
function IndustryPlacer::Init() {
Sleep(1);
company_id = GSCompany.ResolveCompanyID(GSCompany.COMPANY_FIRST);
RegisterIndustryGRF(industry_newgrf);
MapPreprocess();
InitializeTowns();
Print("-----Building tertiary industry:-----", 0);
local build_counter = 0;
local exhausted_list = [];
while(build_counter < tertiary_industry_min &&
exhausted_list.len() != tertiaryindustry_list.len()) {
foreach(ind_id in tertiaryindustry_list) {
local build = 0;
while(build == 0) {
build = TownBuildMethod(ind_id);
}
if(build == -1) {
// This specific industry has been exhausted
// Add it to the skip list
exhausted_list.append(ind_id);
}
}
build_counter += 1;
}
InitializeClusterMap();
build_counter = 0;
exhausted_list = [];
Print("-----Building primary industry:-----", 0);
while(build_counter < raw_industry_min &&
exhausted_list.len() != rawindustry_list.len()) {
foreach(ind_id in rawindustry_list) {
local build = 0;
while(build == 0) {
build = ClusterBuildMethod(ind_id);
}
if(build == -1) {
exhausted_list.append(ind_id);
}
}
build_counter += 1;
}
build_counter = 0;
exhausted_list = [];
Print("-----Building secondary industry:-----", 0);
while(build_counter < proc_industry_min &&
exhausted_list.len() != procindustry_list.len()) {
foreach(ind_id in procindustry_list) {
local build = 0;
while(build == 0) {
build = ScatteredBuildMethod(ind_id);
}
if(build == -1) {
exhausted_list.append(ind_id);
}
}
build_counter += 1;
}
FillFarms();
Print("Done!", 0)
}
function IndustryPlacer::FillCash() {
if(GSCompany.GetBankBalance(company_id) < 100000000) {
GSCompany.ChangeBankBalance(company_id, 1500000000, GSCompany.EXPENSES_OTHER);
}
}
function IndustryPlacer::Build(industry_id, tile_index) {
FillCash();
local mode = GSCompanyMode(company_id);
local build_status = false;
// Shores are wierd. Industries are built from their top left corner; but a shore industry also has to touch land on one side
// Without better knowledge of geometry, we'll just spam build in a 6x6 region up and to the left of the desired tile
if(industry_classes.GetValue(industry_id) == 2) {
local top_corner = GSMap.GetTileIndex(max(GSMap.GetTileX(tile_index) - 6, 1),
max(GSMap.GetTileY(tile_index) - 6, 1));
local build_zone = GSTileList();
build_zone.AddRectangle(top_corner, tile_index);
foreach(tile_id, value in build_zone) {
build_status = GSIndustryType.BuildIndustry(industry_id, tile_id);
if(build_status) {
return build_status;
}
}
return false;
}
return GSIndustryType.BuildIndustry(industry_id, tile_index);
}
function IndustryPlacer::InitializeTowns() {
town_eligibility_default.Valuate(Id)
town_eligibility_water.Valuate(Id);
town_eligibility_shore.Valuate(Id);
town_eligibility_townbldg.Valuate(Id);
town_eligibility_neartown.Valuate(Id);
town_eligibility_nondesert.Valuate(Id);
town_eligibility_nonsnow.Valuate(Id);
town_eligibility_nonsnowdesert.Valuate(Id);
town_industry_counts.Valuate(Zero);
}
function IndustryPlacer::InitializeClusterMap() {
cluster_eligibility_water.AddList(water_tiles);
cluster_eligibility_water.KeepList(outer_town_tiles);
cluster_eligibility_nondesert.AddList(nondesert_tiles);
cluster_eligibility_nondesert.KeepList(outer_town_tiles);
cluster_eligibility_nonsnow.AddList(nonsnow_tiles);
cluster_eligibility_nonsnow.KeepList(outer_town_tiles);
cluster_eligibility_nonsnowdesert.AddList(nonsnow_tiles);
cluster_eligibility_nonsnowdesert.KeepList(outer_town_tiles);
cluster_eligibility_nonsnowdesert.KeepList(nondesert_tiles);
cluster_eligibility_land.AddList(land_tiles);
cluster_eligibility_land.KeepList(outer_town_tiles);
cluster_eligibility_water.Sort(GSList.SORT_BY_VALUE, GSList.SORT_ASCENDING);
cluster_eligibility_nondesert.Sort(GSList.SORT_BY_VALUE, GSList.SORT_ASCENDING);
cluster_eligibility_nonsnow.Sort(GSList.SORT_BY_VALUE, GSList.SORT_ASCENDING);
cluster_eligibility_nonsnowdesert.Sort(GSList.SORT_BY_VALUE, GSList.SORT_ASCENDING);
cluster_eligibility_land.Sort(GSList.SORT_BY_VALUE, GSList.SORT_ASCENDING);
}
// Map preprocessor
// Creates data for all tiles on the map
function IndustryPlacer::MapPreprocess() {
Print("Building map tile list.", 0);
local all_tiles = GSTileList();
all_tiles.AddRectangle(GSMap.GetTileIndex(1, 1),
GSMap.GetTileIndex(GSMap.GetMapSizeX() - 2,
GSMap.GetMapSizeY() - 2));
Print("Map list size: " + all_tiles.Count(), 0);
local chunks = (GSMap.GetMapSizeX() - 2) * (GSMap.GetMapSizeY() - 2) / (chunk_size * chunk_size);
Print("Loading " + chunks + " chunks:", 0);
// Hybrid approach:
// Break the map into chunk_size x chunk_size chunks and valuate on each of them
local progress = ProgressReport(chunks);
for(local y = 1; y < GSMap.GetMapSizeY() - 1; y += chunk_size) {
for(local x = 1; x < GSMap.GetMapSizeX() - 1; x += chunk_size) {
local chunk_land = GetChunk(x, y);
local chunk_shore = GetChunk(x, y);
local chunk_water = GetChunk(x, y);
local chunk_nondesert = GSTileList();
local chunk_nonsnow = GSTileList();
chunk_land.Valuate(GSTile.IsCoastTile);
chunk_land.KeepValue(0);
chunk_land.Valuate(GSTile.IsWaterTile);
chunk_land.KeepValue(0);
chunk_land.Valuate(IsFlatTile);
chunk_land.KeepValue(1);
chunk_land.Valuate(GSBase.RandItem);
chunk_shore.Valuate(GSTile.IsCoastTile);
chunk_shore.KeepValue(1);
chunk_shore.Valuate(GSBase.RandItem);
chunk_water.Valuate(GSTile.IsWaterTile);
chunk_water.KeepValue(1);
chunk_water.Valuate(IsFlatTile);
chunk_water.KeepValue(1);
chunk_water.Valuate(GSBase.RandItem);
chunk_nondesert.AddList(chunk_land);
chunk_nondesert.Valuate(GSTile.IsDesertTile);
chunk_nondesert.KeepValue(0);
chunk_nondesert.Valuate(GSBase.RandItem);
chunk_nonsnow.AddList(chunk_land);
chunk_nonsnow.Valuate(GSTile.IsSnowTile);
chunk_nonsnow.KeepValue(0);
chunk_nonsnow.Valuate(GSBase.RandItem);
land_tiles.AddList(chunk_land);
shore_tiles.AddList(chunk_shore);
water_tiles.AddList(chunk_water);
nondesert_tiles.AddList(chunk_nondesert);
nonsnow_tiles.AddList(chunk_nonsnow);
if(progress.Increment()) {
Print(progress, 0);
}
}
}
land_tiles.Sort(GSList.SORT_BY_VALUE, GSList.SORT_ASCENDING);
shore_tiles.Sort(GSList.SORT_BY_VALUE, GSList.SORT_ASCENDING);
water_tiles.Sort(GSList.SORT_BY_VALUE, GSList.SORT_ASCENDING);
nondesert_tiles.Sort(GSList.SORT_BY_VALUE, GSList.SORT_ASCENDING);
nonsnow_tiles.Sort(GSList.SORT_BY_VALUE, GSList.SORT_ASCENDING);
BuildEligibleTownTiles();
Print("Land tile list size: " + land_tiles.Count(), 0);
Print("Shore tile list size: " + shore_tiles.Count(), 0);
Print("Water tile list size: " + water_tiles.Count(), 0);
Print("Nondesert tile list size: " + nondesert_tiles.Count(), 0);
Print("Nonsnow tile list size: " + nonsnow_tiles.Count(), 0);
Print("Town tile list size: " + town_tiles.Count(), 0);
Print("Outer town tile list size: " + outer_town_tiles.Count(), 0);
}
function IndustryPlacer::IsFlatTile(tile_id) {
return GSTile.GetSlope(tile_id) == GSTile.SLOPE_FLAT;
}
// Returns the map chunk with x, y in the upper left corner
// i.e. GetChunk(1, 1) will give you (1, 1) to (257, 257)
function IndustryPlacer::GetChunk(x, y) {
local chunk = GSTileList();
chunk.AddRectangle(GSMap.GetTileIndex(x, y),
GSMap.GetTileIndex(min(x + 256, GSMap.GetMapSizeX() - 2),
min(y + 256, GSMap.GetMapSizeY() - 2)));
return chunk;
}
// Go through each town and identify every valid tile_id (do we have a way to ID the town of a tile?)
function IndustryPlacer::BuildEligibleTownTiles() {
/*
1. get every town
2. get every tile in every town
3. cull based on config parameters
*/
Print("Building town tile list.", 0);
local town_list = GSTownList();
town_list.Valuate(GSTown.GetLocation);
local progress = ProgressReport(town_list.Count());
foreach(town_id, tile_id in town_list) {
core_town_tiles.AddList(RectangleAroundTile(tile_id, 4));
local local_town_tiles = RectangleAroundTile(tile_id, town_radius);
local distant_town_tiles = RectangleAroundTile(tile_id, town_long_radius);
foreach(tile, value in distant_town_tiles) {
if(local_town_tiles.HasItem(tile)) {
outer_town_tiles.RemoveItem(tile);
if(!town_tiles.HasItem(tile)) {
town_tiles.AddItem(tile, value);
}
} else {
if(!outer_town_tiles.HasItem(tile) && !town_tiles.HasItem(tile)) {
outer_town_tiles.AddItem(tile, value);
}
}
}
if(progress.Increment()) {
Print(progress, 0);
}
}
// Cull all outer town tiles that 'splashed' into nearby towns
foreach(tile, value in outer_town_tiles) {
if(town_tiles.HasItem(tile)) {
outer_town_tiles.RemoveItem(tile);
}
}
}
// Paints on the map all tiles in a given list
function IndustryPlacer::DiagnosticTileMap(tilelist, persist = false) {
foreach(tile_id, value in tilelist) {
GSSign.BuildSign(tile_id, ".");
}
GSController.Sleep(1);
if(!persist) {
foreach(sign_id, value in GSSignList()) {
if(GSSign.GetName(sign_id) == ".") {
GSSign.RemoveSign(sign_id);
}
}
}
}
function IndustryPlacer::RectangleAroundTile(tile_id, radius) {
local tile_x = GSMap.GetTileX(tile_id);
local tile_y = GSMap.GetTileY(tile_id);
local from_x = min(max(tile_x - radius, 1), GSMap.GetMapSizeX() - 2);
local from_y = min(max(tile_y - radius, 1), GSMap.GetMapSizeY() - 2);
local from_tile = GSMap.GetTileIndex(from_x, from_y);
local to_x = min(max(tile_x + radius, 1), GSMap.GetMapSizeX() - 2);
local to_y = min(max(tile_y + radius, 1), GSMap.GetMapSizeY() - 2);
local to_tile = GSMap.GetTileIndex(to_x, to_y);
local tiles = GSTileList();
tiles.AddRectangle(from_tile, to_tile);
return tiles;
}
// Fetch eligible tiles belonging to the town with the given ID
function IndustryPlacer::GetEligibleTownTiles(town_id, terrain_class) {
local local_town_tiles = RectangleAroundTile(GSTown.GetLocation(town_id), town_radius);
// now do a comparison between local town tiles and the terrain lists
local local_eligible_tiles = GSTileList();
local terrain_tiles = GSTileList();
switch(terrain_class) {
case "Water":
terrain_tiles.AddList(water_tiles);
break;
case "Shore":
terrain_tiles.AddList(shore_tiles);
break;
case "TownBldg":
terrain_tiles.AddList(core_town_tiles);
break;
case "NearTown":
terrain_tiles.AddList(town_tiles);
break;
case "Nondesert":
terrain_tiles.AddList(nondesert_tiles);
break;
case "Nonsnow":
terrain_tiles.AddList(nonsnow_tiles);
break;
case "Nonsnowdesert":
terrain_tiles.AddList(nonsnow_tiles);
terrain_tiles.KeepList(nondesert_tiles);
break;
case "Default":
terrain_tiles.AddList(land_tiles);
foreach(tile_id, value in terrain_tiles){} // WTF IS THIS
break;
case "All":
return local_town_tiles;
}
foreach(tile_id, value in local_town_tiles) {
if(terrain_tiles.HasItem(tile_id)) {
local_eligible_tiles.AddItem(tile_id, value);
}
}
return local_eligible_tiles;
}
// Given a tile list, filter to only tiles of that terrain class
function IndustryPlacer::FilterToTerrain(tile_list, terrain_class) {
local filtered_list = GSTileList();
foreach(tile_id, value in tile_list) {
switch(terrain_class) {
case "Water":
if(water_tiles.HasItem(tile_id)) {
filtered_list.AddTile(tile_id);
};
break;
case "Shore":
if(shore_tiles.HasItem(tile_id)) {
filtered_list.AddTile(tile_id);
}
break;
case "TownBldg":
if(core_town_tiles.HasItem(tile_id)) {
filtered_list.AddTile(tile_id);
}
case "NearTown":
if(town_tiles.HasItem(tile_id)) {
filtered_list.AddTile(tile_id);
}
break;
case "Nondesert":
if(nondesert_tiles.HasItem(tile_id)) {
filtered_list.AddTile(tile_id);
}
break;
case "Nonsnow":
if(nonsnow_tiles.HasItem(tile_id)) {
filtered_list.AddTile(tile_id);
}
break;
case "Nonsnowdesert":
if(nonsnow_tiles.HasItem(tile_id) && nondesert_tiles.HasItem(tile_id)) {
filtered_list.AddTile(tile_id);
}
break;
case "Default":
if(land_tiles.HasItem(tile_id)) {
filtered_list.AddTile(tile_id);
}
break;
}
}
return filtered_list;
}
function IndustryPlacer::GetEligibleTowns(terrain_class) {
local town_list = GSTownList();
switch(terrain_class) {
case "Water":
town_list = town_eligibility_water;
break;
case "Shore":
town_list = town_eligibility_shore;
break;
case "TownBldg":
town_list = town_eligibility_townbldg;
break
case "NearTown":
town_list = town_eligibility_neartown;
break
case "Nondesert":
town_list = town_eligibility_nondesert;
break
case "Nonsnow":
town_list = town_eligibility_nonsnow;
break
case "Nonsnowdesert":
town_list = town_eligibility_nonsnowdesert;
break
case "Default":
town_list = town_eligibility_default;
break
}
town_list.KeepValue(1);
return town_list;
}
// Town build method function
// return 1 if built and 0 if not
// Big issue: town eligibility is really eligibility by class -- we can exhaust all the shore tiles of a town, but still be able to build industries on land near the town. How to handle?
function IndustryPlacer::TownBuildMethod(industry_id) {
local ind_name = GSIndustryType.GetName(industry_id);
local terrain_class = industry_class_lookup[industry_classes.GetValue(industry_id)];
local eligible_towns = GetEligibleTowns(terrain_class);
if(eligible_towns.IsEmpty() == true) {
Print("No more eligible " + terrain_class + " towns!", 2);
return -1;
}
local town_id = SampleGSList(eligible_towns);
local eligible_tiles = GetEligibleTownTiles(town_id, terrain_class);
eligible_tiles.Valuate(GSBase.RandItem);
eligible_tiles.Sort(GSList.SORT_BY_VALUE, GSList.SORT_ASCENDING);
Print("Attempting " + ind_name + " in " + GSTown.GetName(town_id), 3);
if(eligible_tiles.Count() == 0) {
Print("Exhausted " + terrain_class + " in " + GSTown.GetName(town_id), 2);
DropTown(town_id, terrain_class);
return 0;
}
// Exclude eligible tiles based on industry class:
//DiagnosticTileMap(eligible_tiles);
// For each tile in the town tile list, try to build in one of them randomly
// - Maintain spacing as given by config file
// - Once built, remove the tile ID from the global eligible tile list
// - Two checks at the end:
// - Check for town industry limit here and cull from eligible_towns if this puts it over the limit
// - Check if the town we just built in now no longer has any eligible tiles
while(eligible_tiles.Count() > 0) {
local attempt_tile = eligible_tiles.Begin();
eligible_tiles.RemoveTop(1);
ClearTile(attempt_tile);
local build_success = Build(industry_id, attempt_tile);
if(build_success) {
Print("Founded " + ind_name + " in " + GSTown.GetName(town_id), 3);
// Check town industry limit (TK) and remove town from global eligible town list if so
local town_current_industries = town_industry_counts.GetValue(town_id) + 1;
town_industry_counts.SetValue(town_id, town_current_industries);
if(town_current_industries == town_industry_limit) {
// Remove town from eligible list AND remove its tiles from the eligible tiles list
foreach(tile_id, value in GetEligibleTownTiles(town_id, "All")) {
ClearTile(tile_id);
}
eligible_towns.RemoveItem(town_id);
}
return 1;
}
}
Print(GSTown.GetName(town_id) + " exhausted.", 2);
// Tiles exhausted, return
return 0;
}
function IndustryPlacer::DropTown(town_id, terrain_class) {
switch(terrain_class) {
case "Water":
town_eligibility_water.SetValue(town_id, 0);
break;
case "Shore":
town_eligibility_shore.SetValue(town_id, 0);
break;
case "TownBldg":
town_eligibility_townbldg.SetValue(town_id, 0);
break;
case "NearTown":
town_eligibility_neartown.SetValue(town_id, 0);
break;
case "Nondesert":
town_eligibility_nondesert.SetValue(town_id, 0);
break;
case "Nonsnow":
town_eligibility_nonsnow.SetValue(town_id, 0);
break;