-
Notifications
You must be signed in to change notification settings - Fork 16
/
industry.nut
754 lines (640 loc) · 26.5 KB
/
industry.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
// As defined number of industries on a 256x256 map [funding, minimal, very low, low, normal, high]
::NumIndustries <- [0, 0, 10, 25, 55, 80];
function InitIndustryLists()
{
local industry_type_list = GSIndustryTypeList();
local list_raw = [];
local list_1 = [];
local list_2 = [];
local list_3 = [];
local list_4 = [];
foreach (industry, _ in industry_type_list) {
local cargo_list = GSIndustryType.GetAcceptedCargo(industry);
cargo_list.RemoveItem(0); // exclude PASS
cargo_list.RemoveItem(2); // exclude MAIL
switch (cargo_list.Count())
{
case 0:
break;
case 1:
if (GSIndustryType.IsRawIndustry(industry))
list_raw.append(industry);
else
list_1.append(industry);
break;
case 2:
if (GSIndustryType.IsRawIndustry(industry))
list_raw.append(industry);
else
list_2.append(industry);
break;
case 3:
list_3.append(industry);
break;
default:
list_4.append(industry);
}
}
::industries_raw <- list_raw;
::industries_1 <- list_1;
::industries_2 <- list_2;
::industries_3 <- list_3;
::industries_4 <- list_4;
DebugIndustryLists();
// Require at least one 1-cargo industry and at least two out of three [2-cargo, 3-cargo, 4+cargo] with 3 and more industry types
if ((::industries_raw.len() + ::industries_1.len() == 0) || ((::industries_2.len() < 3).tointeger() + (::industries_3.len() < 3).tointeger() + (::industries_4.len() < 3).tointeger() > 1))
return false;
// Initialized required global variables
::CargoIDList <- [];
for(local i = 0; i < 64; ++i) {
if (GSCargo.GetCargoLabel(i) == "LVPT") // CZTR ZBARVENI
::CargoIDList.append(null);
else if (GSCargo.GetCargoLabel(i) == null) {
local j = i + 1;
local list_end = true;
while (j < 64) {
if (GSCargo.GetCargoLabel(j) != null) {
list_end = false;
break;
}
j++;
}
if (list_end)
break;
else
::CargoIDList.append(GSCargo.GetCargoLabel(i));
}
else
::CargoIDList.append(GSCargo.GetCargoLabel(i));
}
::CargoLimiter <- [0, 2];
::CargoCatNum <- (list_4.len() > 0 || (list_3.len() > 6 && (list_1.len() + list_raw.len()) > 9)) ? 5 : 4;
::Economy <- DiscoverEconomyType();
if (::CargoCatNum == 4) {
::CargoMinPopDemand <- [0, 1000, 4000, 8000];
::CargoCatList <- [CatLabels.CATEGORY_I, CatLabels.CATEGORY_II, CatLabels.CATEGORY_III, CatLabels.CATEGORY_IV];
::CargoPermille <- [60, 10, 25, 40];
::CargoDecay <- [0.4, 0.1, 0.1, 0.1];
}
else {
::CargoMinPopDemand <- [0, 500, 1000, 4000, 8000];
::CargoCatList <- [CatLabels.CATEGORY_I, CatLabels.CATEGORY_II, CatLabels.CATEGORY_III, CatLabels.CATEGORY_IV, CatLabels.CATEGORY_V];
::CargoPermille <- [60, 10, 25, 40, 60];
::CargoDecay <- [0.4, 0.1, 0.1, 0.1, 0.1];
}
// Change cargo min pop demand if specified
for (local i = 0; i < ::CargoCatNum; i++) {
if (::SettingsTable.category_min_pop[i] >= 0) {
::CargoMinPopDemand[i] = ::SettingsTable.category_min_pop[i];
}
}
// Sort min pop demand, but not categories
for (local i = 0; i < ::CargoCatNum; i++) {
for (local j = 0; j < ::CargoCatNum - 1; j++) {
if (::CargoMinPopDemand[j] > ::CargoMinPopDemand[j+1]) {
local min_pop_demand = ::CargoMinPopDemand[j];
::CargoMinPopDemand[j] = ::CargoMinPopDemand[j+1];
::CargoMinPopDemand[j+1] = min_pop_demand;
}
}
}
return true;
}
function RandomizeIndustry(ascending, near_industries, near_industry_probability)
{
local categories = [];
local list_raw = clone ::industries_raw;
local list_1 = clone ::industries_1;
local list_2 = clone ::industries_2;
local list_3 = clone ::industries_3;
local list_4 = clone ::industries_4;
local industry_cat_text = "";
// 1. Category (PASS, MAIL)
// 2. Category, pick 1 cargo
if ((near_industries[0].len() != 0 || near_industries[1].len() != 0) &&
GSBase.Chance(near_industry_probability, 100)) { // Use near industry
if (near_industries[1].len() == 0) { // Use raw industry for Cat II
local rand = GSBase.RandRange(near_industries[0].len());
categories.append([near_industries[0][rand]]);
// Find and remove industry id from the other array
foreach (idx, industry in list_raw) {
if (industry == near_industries[0][rand]) {
list_raw.remove(idx);
break;
}
}
industry_cat_text += " Cat II: [" + GSIndustryType.GetName(near_industries[0][rand]) + "(NEAR)]";
near_industries[0].remove(rand);
}
else { // Use 1 input industry for Cat II
local rand = GSBase.RandRange(near_industries[1].len());
categories.append([near_industries[1][rand]]);
// Find and remove industry id from the other array
foreach (idx, industry in list_1) {
if (industry == near_industries[1][rand]) {
list_1.remove(idx);
break;
}
}
industry_cat_text += " Cat II: [" + GSIndustryType.GetName(near_industries[1][rand]) + "(NEAR)]";
near_industries[1].remove(rand);
}
}
else { // Use random industry
if (list_1.len() == 0) { // Use raw industry for Cat II
local rand = GSBase.RandRange(list_raw.len());
categories.append([list_raw[rand]]);
// Find and remove industry id from the other array
foreach (idx, industry in near_industries[0]) {
if (industry == list_raw[rand]) {
near_industries[0].remove(idx);
break;
}
}
industry_cat_text += " Cat II: [" + GSIndustryType.GetName(list_raw[rand]) + "(RAND)]";
list_raw.remove(rand);
}
else { // Use 1 input industry for Cat II
local rand = GSBase.RandRange(list_1.len());
categories.append([list_1[rand]]);
// Find and remove industry id from the other array
foreach (idx, industry in near_industries[1]) {
if (industry == list_1[rand]) {
near_industries[1].remove(idx);
break;
}
}
industry_cat_text += " Cat II: [" + GSIndustryType.GetName(list_1[rand]) + "(RAND)]";
list_1.remove(rand);
}
}
// 3. Category, pick 2 cargos
if (near_industries[2].len() != 0 && GSBase.Chance(near_industry_probability, 100)) { // Use near industry
// Use 2 input industry for Cat III
local rand = GSBase.RandRange(near_industries[2].len());
categories.append([near_industries[2][rand]]);
// Find and remove industry id from the other array
foreach (idx, industry in list_2) {
if (industry == near_industries[2][rand]) {
list_2.remove(idx);
break;
}
}
industry_cat_text += " Cat III: [" + GSIndustryType.GetName(near_industries[2][rand]) + "(NEAR)]";
near_industries[2].remove(rand);
}
else { // Use random industry
if (list_2.len() > 2 || (list_1.len() < 2 && list_2.len() > 0)) { // Use 2 input industry for Cat II
local rand = GSBase.RandRange(list_2.len());
categories.append([list_2[rand]]);
// Find and remove industry id from the other array
foreach (idx, industry in near_industries[2]) {
if (industry == list_2[rand]) {
near_industries[2].remove(idx);
break;
}
}
industry_cat_text += " Cat III: [" + GSIndustryType.GetName(list_2[rand]) + "(RAND)]";
list_2.remove(rand);
}
else {
// Prevent using only max 2 industries by giving chance to use 1+1
local rand = GSBase.RandRange(3);
if (rand < list_2.len()) {
categories.append([list_2[rand]]);
// Find and remove industry id from the other array
foreach (idx, industry in near_industries[2]) {
if (industry == list_2[rand]) {
near_industries[2].remove(idx);
break;
}
}
industry_cat_text += " Cat III: [" + GSIndustryType.GetName(list_2[rand]) + "(RAND)]";
list_2.remove(rand);
}
else {
if (list_raw.len() > 0) {
local rand_1 = GSBase.RandRange(list_1.len());
local rand_raw = GSBase.RandRange(list_raw.len());
categories.append([list_1[rand_1], list_raw[rand_raw]]);
// Find and remove industry id from the other array
foreach (idx, industry in near_industries[1]) {
if (industry == list_1[rand_1]) {
near_industries[1].remove(idx);
break;
}
}
foreach (idx, industry in near_industries[0]) {
if (industry == list_raw[rand_raw]) {
near_industries[0].remove(idx);
break;
}
}
industry_cat_text += " Cat III: [" + GSIndustryType.GetName(list_1[rand_1]) + ", " + GSIndustryType.GetName(list_raw[rand_raw]) + "(RAND)]";
list_1.remove(rand_1);
list_raw.remove(rand_raw);
}
else {
local rand_1 = GSBase.RandRange(list_1.len());
local rand_1_ind = list_1[rand_1];
list_1.remove(rand_1);
local rand_2 = GSBase.RandRange(list_1.len() - 1);
categories.append([rand_1_ind, list_1[rand_2]]);
// Find and remove industry id from the other array
foreach (idx, industry in near_industries[1]) {
if (industry == list_1[rand_2] || industry == rand_1_ind) {
near_industries[1].remove(idx);
}
}
industry_cat_text += " Cat III: [" + GSIndustryType.GetName(rand_1_ind) + ", " + GSIndustryType.GetName(list_1[rand_2]) + "(RAND)]";
list_1.remove(rand_2);
}
}
}
}
// 4. Category, pick 3 cargos
if (near_industries[3].len() != 0 && GSBase.Chance(near_industry_probability, 100)) { // Use near industry
// Use 3 input industry for Cat IV
local rand = GSBase.RandRange(near_industries[3].len());
categories.append([near_industries[3][rand]]);
// Find and remove industry id from the other array
foreach (idx, industry in list_3) {
if (industry == near_industries[3][rand]) {
list_3.remove(idx);
break;
}
}
industry_cat_text += " Cat IV: [" + GSIndustryType.GetName(near_industries[3][rand]) + "(NEAR)]";
near_industries[3].remove(rand);
}
else { // Use random industry
if (list_3.len() > 2 || ((list_2.len() == 0 || (list_1.len() + list_raw.len() == 0)) && list_3.len() > 0)) {
local rand = GSBase.RandRange(list_3.len());
categories.append([list_3[rand]]);
// Find and remove industry id from the other array
foreach (idx, industry in near_industries[3]) {
if (industry == list_3[rand]) {
near_industries[3].remove(idx);
break;
}
}
industry_cat_text += " Cat IV: [" + GSIndustryType.GetName(list_3[rand]) + "(RAND)]";
list_3.remove(rand);
}
else {
// Prevent using only max 2 industries by giving chance to use 1+2
local rand = GSBase.RandRange(3);
if (rand < list_3.len()) {
categories.append([list_3[rand]]);
// Find and remove industry id from the other array
foreach (idx, industry in near_industries[3]) {
if (industry == list_3[rand]) {
near_industries[3].remove(idx);
break;
}
}
industry_cat_text += " Cat IV: [" + GSIndustryType.GetName(list_3[rand]) + "(RAND)]";
list_3.remove(rand);
}
else {
local list = clone list_1;
list.extend(list_raw);
local rand_1 = GSBase.RandRange(list.len());
local rand_2 = GSBase.RandRange(list_2.len());
categories.append([list[rand_1], list_2[rand_2]]);
industry_cat_text += " Cat III: [" + GSIndustryType.GetName(list[rand_1]) + ", " + GSIndustryType.GetName(list_2[rand_2]) + "(RAND)]";
if (rand_1 < list_1.len()) {
// Find and remove industry id from the other array
foreach (idx, industry in near_industries[1]) {
if (industry == list[rand_1]) {
near_industries[1].remove(idx);
break;
}
}
list_1.remove(rand_1);
}
else {
// Find and remove industry id from the other array
foreach (idx, industry in near_industries[0]) {
if (industry == list[rand_1]) {
near_industries[0].remove(idx);
break;
}
}
list_raw.remove(rand_1 - list_1.len());
}
// Find and remove industry id from the other array
foreach (idx, industry in near_industries[2]) {
if (industry == list[rand_2]) {
near_industries[2].remove(idx);
break;
}
}
list_2.remove(rand_2);
}
}
}
// 5. Category, pick 4 or more cargos
// Dont need to remove used ids in the last category
if (near_industries[4].len() != 0 && GSBase.Chance(near_industry_probability, 100)) { // Use near industry
// Use 4+ input industry for Cat V
local rand = GSBase.RandRange(near_industries[4].len());
categories.append([near_industries[4][rand]]);
industry_cat_text += " Cat V: [" + GSIndustryType.GetName(near_industries[4][rand]) + "(NEAR)]";
}
else { // Use random industry
if (list_4.len() > 2 || ((list_3.len() < 5 || (list_1.len() + list_raw.len() < 5)) && list_4.len() > 0)) {
local rand = GSBase.RandRange(list_4.len());
categories.append([list_4[rand]]);
industry_cat_text += " Cat V: [" + GSIndustryType.GetName(list_4[rand]) + "(RAND)]";
}
else if (list_4.len() > 0 && (list_1.len() + list_raw.len() > 0) && list_3.len() > 0) {
local rand = GSBase.RandRange(3);
if (rand < list_4.len()) {
categories.append([list_4[rand]]);
industry_cat_text += " Cat V: [" + GSIndustryType.GetName(list_4[rand]) + "(RAND)]";
}
else {
local list = clone list_1;
list.extend(list_raw);
local rand_1 = GSBase.RandRange(list.len());
local rand_3 = GSBase.RandRange(list_3.len());
categories.append([list[rand_1], list_3[rand_3]]);
industry_cat_text += " Cat V: [" + GSIndustryType.GetName(list[rand_1]) + ", " + GSIndustryType.GetName(list_3[rand_3]) + "(RAND)]";
}
}
else if (list_3.len() > 4 && (list_1.len() + list_raw.len() > 4)) {
local list = clone list_1;
list.extend(list_raw);
local rand_1 = GSBase.RandRange(list.len());
local rand_3 = GSBase.RandRange(list_3.len());
categories.append([list[rand_1], list_3[rand_3]]);
industry_cat_text += " Cat V: [" + GSIndustryType.GetName(list[rand_1]) + ", " + GSIndustryType.GetName(list_3[rand_3]) + "(RAND)]";
}
}
Log.Info(GSTown.GetName(this.id) + ":" + industry_cat_text, Log.LVL_SUB_DECISIONS);
if (!ascending) {
local category_reverse = [];
for (local i = categories.len() - 1; i >= 0; --i) {
category_reverse.append(categories[i]);
}
return category_reverse;
}
return categories;
}
function GetCargoCatFromIndustryCat(industry_cat)
{
local cargo_cat = [[0, 2]];
foreach (cat_idx, cat in industry_cat) {
cargo_cat.append([]);
foreach (ind_idx, ind in cat) {
local cargo_list = GSIndustryType.GetAcceptedCargo(ind);
foreach (cargo, _ in cargo_list) {
// Ignore PASS and MAIL
if (cargo == 0 || cargo == 2)
continue;
local found = false;
foreach (c in cargo_cat.top()) {
if (cargo == c) {
found = true;
break;
}
}
if (!found)
cargo_cat[cat_idx+1].append(cargo);
}
}
}
return cargo_cat;
}
function GetIndustryHash(industry_cat)
{
local hash = 0;
local index = 0;
foreach (cat_idx, cat in industry_cat)
{
local new_cat = 0x01;
foreach (ind_idx, ind in cat)
{
local industry = (ind << 1 & 0xff) | new_cat; // | industry id 8 bit | new category flag 1 bit |
hash = hash | (industry << index);
index += 9;
new_cat = 0x00;
}
}
return hash;
}
function GetIndustryTable(hash)
{
local industry_cat = [];
local cat_idx = 0;
while (hash > 0) {
local industry = (hash & 0x01fe) >> 1;
local new_cat = hash & 0x01;
if (new_cat) {
industry_cat.append([industry]);
++cat_idx;
}
else {
industry_cat[cat_idx-1].append(industry);
}
hash = hash >> 9;
}
return industry_cat;
}
function ProspectRawIndustry()
{
// In multiplayer, pause level cannot be changed, so skip funding industries
local pause_level = GSGameSettings.GetValue("construction.command_pause_level");
if (GSGame.IsPaused() && GSGame.IsMultiplayer() && pause_level < 3)
return;
local target_count = GetRawIndustryTargetCount();
if (target_count == 0)
return;
local industry_list = GSIndustryList();
local industry_count = industry_list.Count();
industry_list.Valuate(IsRawIndustry);
industry_list.KeepValue(1);
if (target_count <= industry_list.Count())
return;
// If it is not set, temporarily set prospecting industry construction type
local construction_type = GSGameSettings.GetValue("construction.raw_industry_construction");
if (construction_type != 2)
GSGameSettings.SetValue("construction.raw_industry_construction", 2);
// If it is not set, temporarily allow all actions during pause (for prospecting)
if (pause_level < 3)
GSGameSettings.SetValue("construction.command_pause_level", 3);
local built_count = 0;
local ignore_list = GSList();
local raw_count = industry_list.Count();
local try_count = 0;
while (raw_count < target_count) {
if (try_count > 3) {
Log.Warning("Industry funding failed: " + GSError.GetLastErrorString(), Log.LVL_INFO);
break;
}
local industry_type = GetRawIndustryToProspect(ignore_list);
if (industry_type == null)
break;
Log.Info("Funding industry " + GSIndustryType.GetName(industry_type), Log.LVL_DEBUG);
if (GSIndustryType.ProspectIndustry(industry_type)) {
if (GSIndustryList().Count() == (industry_count + built_count + 1)) {
raw_count++;
try_count = 0;
built_count++;
}
else {
try_count++;
}
// If newGRF refuses to build the industry type, prospecting returns true,
// so the industry type must be removed from chosing
if (try_count > 3) {
Log.Warning("Cannot prospect " + GSIndustryType.GetName(industry_type) + ", will be skipped.", Log.LVL_INFO);
ignore_list.AddItem(industry_type, 0);
try_count = 0;
}
}
else
try_count++;
}
Log.Info("Prospected " + (raw_count - industry_list.Count()) + " raw industries up to total number of " + raw_count, Log.LVL_INFO);
// Reset to previous settings
GSGameSettings.SetValue("construction.raw_industry_construction", construction_type);
GSGameSettings.SetValue("construction.command_pause_level", pause_level);
}
function IsProcessingIndustry(industry)
{
return GSIndustryType.IsProcessingIndustry(GSIndustry.GetIndustryType(industry))
}
function IsRawIndustry(industry)
{
return GSIndustryType.IsRawIndustry(GSIndustry.GetIndustryType(industry))
}
function CanProspectIndustry(industry)
{
return GSIndustryType.CanProspectIndustry(GSIndustry.GetIndustryType(industry))
}
function GetRawIndustryTypeRatio()
{
local industry_type_list = GSIndustryTypeList();
local count = industry_type_list.Count().tofloat();
industry_type_list.Valuate(GSIndustryType.IsRawIndustry);
industry_type_list.KeepValue(1);
return industry_type_list.Count().tofloat() / count;
}
function GetRawIndustryTypeCount()
{
local industry_type_list = GSIndustryTypeList();
industry_type_list.Valuate(GSIndustryType.IsRawIndustry);
industry_type_list.KeepValue(1);
return industry_type_list.Count();
}
function GetRawIndustryTargetCount()
{
local industry_density = GSController.GetSetting("raw_industry_density");
if (industry_density == 0) // funding only
return 0;
else if (industry_density == 1) // minimal (one of each)
return GetRawIndustryTypeCount();
local ratio = GetRawIndustryTypeRatio();
return (::NumIndustries[industry_density] * ((GSMap.GetMapSizeX() / 256.0) * (GSMap.GetMapSizeY() / 256.0)) * ratio).tointeger();
}
function GetIndustryTypeCount(industry_type)
{
local industry_list = GSIndustryList();
industry_list.Valuate(GSIndustry.GetIndustryType);
industry_list.KeepValue(industry_type);
return industry_list.Count();
}
function GetRawIndustryToProspect(ignore_list)
{
local industry_type_list = GSIndustryTypeList();
industry_type_list.RemoveList(ignore_list);
industry_type_list.Valuate(GSIndustryType.IsRawIndustry);
industry_type_list.KeepValue(1);
industry_type_list.Valuate(GSIndustryType.CanProspectIndustry);
industry_type_list.KeepValue(1);
industry_type_list.Valuate(GetIndustryTypeCount);
industry_type_list.Sort(GSList.SORT_BY_VALUE, GSList.SORT_ASCENDING);
return industry_type_list.Count() ? industry_type_list.Begin() : null;
}
/**
* @brief Find closest town to each industry on the map.
* @return table containing {town_id, [industry_ids]}
*/
function GetNearbyIndustriesToTowns()
{
local industry_list = GSIndustryList();
local town_list = GSTownList();
// Initialize town industries table
local town_industries = {};
foreach (town_id, _ in town_list) {
town_industries[town_id] <- [];
}
// Find closest town to each industry
foreach (industry_id, _ in industry_list) {
local industry_location = GSIndustry.GetLocation(industry_id);
town_industries[GSTile.GetClosestTown(industry_location)].append(industry_id);
}
// Print town_industries
DebugTownIndustries(town_industries);
return town_industries;
}
function GetTownsNearbyIndustryPerCategory()
{
local town_industries = GetNearbyIndustriesToTowns();
local town_industry_category = {};
foreach (town_id, industry_ids in town_industries) {
town_industry_category[town_id] <- [];
for (local i = 0; i < 5; ++i) {
town_industry_category[town_id].append([]);
}
foreach (industry_id in industry_ids) {
local industry_type_id = GSIndustry.GetIndustryType(industry_id);
// Determine the category of this industry type
local cargo_list = GSIndustryType.GetAcceptedCargo(industry_type_id);
cargo_list.RemoveItem(0); // exclude PASS
cargo_list.RemoveItem(2); // exclude MAIL
local category = -1;
switch (cargo_list.Count())
{
case 0:
break;
case 1:
if (GSIndustryType.IsRawIndustry(industry_type_id))
category = 0;
else
category = 1;
break;
case 2:
if (GSIndustryType.IsRawIndustry(industry_type_id))
category = 0;
else
category = 2;
break;
case 3:
category = 3;
break;
default:
category = 4;
}
// No accepting cargo
if (category < 0)
continue;
// Check if it already exists in the list, if not, add it there
local found = false;
foreach (saved_industry_type in town_industry_category[town_id][category]) {
if (saved_industry_type == industry_type_id) {
found = true;
break;
}
}
if (!found) {
town_industry_category[town_id][category].append(industry_type_id);
}
}
}
// Print results
DebugNearTownIndustryTypes(town_industry_category);
return town_industry_category;
}