File: ToolAI.pas

package info (click to toggle)
c-evo-dh 3.1-2
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 10,548 kB
  • sloc: pascal: 57,426; xml: 256; makefile: 114; sh: 4
file content (1568 lines) | stat: -rw-r--r-- 53,941 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
{$INCLUDE Switches.inc}
unit ToolAI;

interface

uses
  SysUtils, Math,
{$IFDEF DEBUG}Names,{$ENDIF}
  Protocol, CustomAI;

type
  TGroupTransportPlan = record
    LoadLoc, uixTransport, nLoad, TurnsEmpty, TurnsLoaded: integer;
    uixLoad: array[0..15] of integer;
  end;


  TToolAI = class(TCustomAI)
  protected
  {$IFDEF DEBUG}DebugMap: array[0..lxmax * lymax - 1] of integer;{$ENDIF}

    function CenterOfEmpire: integer;
    // tile that is in the middle of all own cities

    function CityTaxBalance(cix: integer; const CityReport: TCityReport): integer;
    // calculates exact difference of income and maintenance cost for a single city
    // positive result = income higher than maintenance
    // negative result = income lower than maintenance
    // respects production and food converted to gold
    // CityReport must have been prepared before
    procedure SumCities(TaxRate: integer; var TaxSum, ScienceSum: integer);
    // calculates exact total tax and science income
    // tax is reduced by maintenance (so might be negative)
    // luxury not supported

    procedure OptimizeCityTiles;
    // obsolete; use City_OptimizeTiles instead

    procedure GetCityProdPotential;
    // calculates potential collected production resources of a city
    // result: list for all cities in CityResult
    procedure GetCityTradePotential;
    // calculates potential collected trade resources of a city
    // result: list for all cities in CityResult

    procedure JobAssignment_Initialize;
    // initialization, must be called first of the JobAssignment functions
    procedure JobAssignment_AddJob(Loc, Job, Score: integer);
    // add job for settlers with certain score
    // jobs include founding cities!
    procedure JobAssignment_AddUnit(uix: integer);
    // add a settler unit to do jobs
    procedure JobAssignment_Go;
    // to be called after all jobs and the settlers for them have been added
    // assigns each job to one settler, moves the settlers and makes them work
    // settlers prefer jobs which are closer to their current location and jobs with a higher score
    // starting a job one turn earlier counts the same as 4 points of score
    // function does not cancel jobs that are already started
    function JobAssignment_GotJob(uix: integer): boolean;
    // can be called after JobAssignment_Go to find out whether
    // a certain settler has been assigned a job to

    procedure AnalyzeMap;
    // calculates formations and districts

    function CheckStep(MoveStyle, TimeBeforeStep, CrossCorner: integer;
      var TimeAfterStep, RecoverTurns: integer; FromTile, ToTile: integer;
      IsCapture: boolean): integer;
    // forecast single unit move between adjacent tiles
    // format of TimeBeforeStep and TimeAfterStep: $1000*number of turns + $800-MP left
    // RecoverTurns: number of turns needed to rest outside city in order to
    //   recover from damage taken in this move (rounded up)
    // FromTile and ToTile must be Map[FromLoc] and Map[ToLoc], no location codes
    // CrossCorner=1 for long moves that cross the tile corner, =0 for short ones that don't

    function GetMyMoveStyle(mix, Health: integer): integer;

    function Unit_MoveEx(uix, ToLoc: integer; Options: integer = 0): integer;

    procedure SeaTransport_BeginInitialize;
    procedure SeaTransport_EndInitialize;
    // sea transport, obligatory call order:
    // 1. BeginInitialize
    // [2. AddLoad/AddTransport/AddDestination]
    // 3. EndInitialize
    // [4. MakeGroupPlan, MakeGroupPlan, MakeGroupPlan...]
    // don't use Pile between BeginInitialize and EndInitialize
    // sea transport only works well if
    // - all transports have same speed
    // - all transports have same capacity
    // - no transport is damaged
    procedure SeaTransport_AddLoad(uix: integer);
    procedure SeaTransport_AddTransport(uix: integer);
    procedure SeaTransport_AddDestination(Loc: integer);
    function SeaTransport_MakeGroupPlan(var TransportPlan: TGroupTransportPlan): boolean;
    // make plan for group of units to transport from a single loading location by a single transport
    // the plan optimizes:
    // - time for the units to move to the loading location
    // - time for the transport to move to the loading location
    // - time for the transport to move to one of the destination locations
    // after the plan is made, units and transport are removed from the pool, so that
    //   subsequent calls to MakeGroupPlan result in plans that may be executed parallel
    // function returns false if no more transports are possible

    function CurrentMStrength(Domain: integer): integer;
  end;

const
  // no-formations
  nfUndiscovered = -1;
  nfPole = -2;
  nfPeace = -3;

  // return codes of CheckStep
  csOk = 0;
  // step is valid
  // TimeAfterMove has been calculated
  csForbiddenTile = 1;
  // unit can not move onto this tile
  // TimeAfterMove not calculated
  csForbiddenStep = 2;
  // (ZoC unit only) unit can not do this step because of ZoC violation
  // maybe tile can be reached using another way
  // TimeAfterMove not calculated
  csCheckTerritory = 3;
  // move within other nations's territory shortly after making peace
  // step is only possible if RO.Territory is the same for both tiles
  // TimeAfterMove has been calculated

  // Unit_MoveEx
  mxAdjacent = $00000001;

var
  nContinent, nOcean, nDistrict: integer;
  Formation: array[0..lxmax * lymax - 1] of integer;
  // water: ocean index, land: continent index, sorted by size
  // territory unpassable due to peace treaty divides a continent
  District: array[0..lxmax * lymax - 1] of integer;
  // index of coherent own territory, sorted by size
  CityResult: array[0..nCmax - 1] of integer;

  Advancedness: array[0..nAdv - 1] of integer;
// total number of prerequisites for each advance

implementation

uses
  Pile;

type
  pinteger = ^integer;

var
  // for JobAssignment
  MaxScore: integer;
  TileJob, TileJobScore: array[0..lxmax * lymax - 1] of byte;
  JobLocOfSettler: array[0..nUmax - 1] of integer; // ToAssign = find job

  // for Transport
  TransportMoveStyle, TransportCapacity, nTransportLoad: integer;
  InitComplete, HaveDestinations: boolean;
  uixTransportLoad, TransportAvailable: array[0..nUmax - 1] of integer;
  TurnsAfterLoad: array[0..lxmax * lymax - 1] of shortint;

procedure ReplaceD(Start, Stop: pinteger; Raider, Twix: integer);
begin
  while Start <> Stop do
  begin
    if Start^ = Raider then
      Start^ := Twix;
    Inc(Start);
  end;
end;

function NextZero(Start, Stop: pinteger; Mask: cardinal): pinteger;
begin
  while (Start <> Stop) and (Start^ and Mask <> 0) do
    Inc(Start);
  Result := Start;
end;

function TToolAI.CenterOfEmpire: integer;
var
  cix, Loc, x, y, sy, n: integer;
  a, su, sv: double;
begin
  n := 0;
  sy := 0;
  su := 0;
  sv := 0;
  for cix := 0 to RO.nCity - 1 do
  begin
    Loc := MyCity[cix].Loc;
    if Loc >= 0 then
    begin
      y := Loc div G.lx;
      x := Loc - y * G.lx;
      Inc(sy, y);
      a := 2 * pi * x / G.lx;
      su := su + cos(a);
      sv := sv + sin(a);
      Inc(n);
    end;
  end;
  a := arctan2(sv, su);
  x := round(G.lx * a / (2 * pi));
  while x >= G.lx do
    Dec(x, G.lx);
  while x < 0 do
    Inc(x, G.lx);
  Result := ((2 * sy + n) div (2 * n)) * G.lx + x;
end;

function TToolAI.CityTaxBalance(cix: integer; const CityReport: TCityReport): integer;
var
  i: integer;
begin
  Result := 0;
  if (CityReport.Working - CityReport.Happy <= MyCity[cix].Size shr 1) {no disorder} and
    (MyCity[cix].Flags and chCaptured = 0) then // not captured
  begin
    Inc(Result, CityReport.Tax);
    if (MyCity[cix].Project and (cpImp + cpIndex) = cpImp + imTrGoods) and
      (CityReport.ProdRep > CityReport.Support) then
      Inc(Result, CityReport.ProdRep - CityReport.Support);
    if ((RO.Government = gLybertarianism) or (MyCity[cix].Size >=
      NeedAqueductSize) and (CityReport.FoodRep < CityReport.Eaten + 2)) and
      (CityReport.FoodRep > CityReport.Eaten) then
      Inc(Result, CityReport.FoodRep - CityReport.Eaten);
  end;
  for i := nWonder to nImp - 1 do
    if MyCity[cix].Built[i] > 0 then
      Dec(Result, Imp[i].Maint);
end;

procedure TToolAI.SumCities(TaxRate: integer; var TaxSum, ScienceSum: integer);
var
  cix, p1: integer;
  CityReport: TCityReport;
begin
  TaxSum := 0;
  ScienceSum := 0;
  if RO.Government = gAnarchy then
    exit;
  for p1 := 0 to nPl - 1 do
    if RO.Tribute[p1] <= RO.TributePaid[p1] then
      // don't rely on tribute from bankrupt nations
      TaxSum := TaxSum + RO.Tribute[p1];
  for cix := 0 to RO.nCity - 1 do
    if MyCity[cix].Loc >= 0 then
    begin
      City_GetHypoReport(cix, -1, TaxRate, 0, CityReport);
      if (CityReport.Working - CityReport.Happy <= MyCity[cix].Size shr
        1) {no disorder} and (MyCity[cix].Flags and chCaptured = 0) then // not captured
        ScienceSum := ScienceSum + CityReport.Science;
      TaxSum := TaxSum + CityTaxBalance(cix, CityReport);
    end;
end;

//------------------------------------------------------------------------------
// City Tiles Processing

const
  pctOptimize = 0;
  pctGetProdPotential = 1;
  pctGetTradePotential = 2;

procedure TToolAI.OptimizeCityTiles;
var
  cix: integer;
begin
  for cix := 0 to RO.nCity - 1 do
    with MyCity[cix] do
      if Loc >= 0 then
        City_OptimizeTiles(cix);
end;

procedure TToolAI.GetCityProdPotential;
var
  cix: integer;
  Advice: TCityTileAdviceData;
begin
  for cix := 0 to RO.nCity - 1 do
    with MyCity[cix] do
      if Loc >= 0 then
      begin
        Advice.ResourceWeights := rwMaxProd;
        Server(sGetCityTileAdvice, me, cix, Advice);
        CityResult[cix] := Advice.CityReport.ProdRep; // considers factory, but shouldn't
      end;
end;

procedure TToolAI.GetCityTradePotential;
var
  cix: integer;
  Advice: TCityTileAdviceData;
begin
  for cix := 0 to RO.nCity - 1 do
    with MyCity[cix] do
      if Loc >= 0 then
      begin
        Advice.ResourceWeights := rwMaxScience;
        Server(sGetCityTileAdvice, me, cix, Advice);
        CityResult[cix] := Advice.CityReport.Trade;
      end;
end;

//------------------------------------------------------------------------------
// JobAssignment

const
  ToAssign = lxmax * lymax;

procedure TToolAI.JobAssignment_Initialize;
begin
  fillchar(JobLocOfSettler, RO.nUn * sizeof(integer), $FF); // -1
  fillchar(TileJob, MapSize, jNone);
  fillchar(TileJobScore, MapSize, 0);
  MaxScore := 0;
end;

procedure TToolAI.JobAssignment_AddJob(Loc, Job, Score: integer);
begin
  if Score > 255 then
    Score := 255;
  if Score > TileJobScore[Loc] then
  begin
    TileJob[Loc] := Job;
    TileJobScore[Loc] := Score;
    if Score > MaxScore then
      MaxScore := Score;
  end;
end;

procedure TToolAI.JobAssignment_AddUnit(uix: integer);
begin
  assert(MyModel[MyUnit[uix].mix].Kind in [mkSettler, mkSlaves]);
  JobLocOfSettler[uix] := ToAssign;
end;

function TToolAI.JobAssignment_GotJob(uix: integer): boolean;
begin
  Result := JobLocOfSettler[uix] >= 0;
end;

procedure TToolAI.JobAssignment_Go;
const
  DistanceScore = 4;
  StepSizeByTerrain: array[0..11] of integer =
    (0, 0, 1, 2, 1, 1, 0, 1, 0, 1, 1, 2);
  //Oc-Sh-Gr-De-Pr-Tu-Ar-Sw-XX-Fo-Hi-Mo
var
  uix, BestScore, BestCount, BestLoc, BestJob, BestDistance, TestLoc,
  NextLoc, TestDistance, V8, TestScore, StepSize, MoveResult: integer;
  UnitsToAssign: boolean;
  Adjacent: TVicinity8Loc;
  SettlerOfJobLoc, DistToLoc: array[0..lxmax * lymax - 1] of integer;
  // DistToLoc is only defined where SettlerOfJobLoc>=0
  TileChecked: array[0..lxmax * lymax - 1] of boolean;
begin
  fillDWord(SettlerOfJobLoc, MapSize, $FFFFFFFF); // -1
  fillDWord(DisttoLoc, MapSize, 0);

  BestLoc 		:= 0;
  BestDistance 	:= 0;
  BestCount 	:= 0;

  // keep up jobs that are already started
  for uix := 0 to RO.nUn - 1 do
    if (MyUnit[uix].Loc >= 0) and (MyUnit[uix].Job > jNone) then
    begin
      JobLocOfSettler[uix] := MyUnit[uix].Loc;
      SettlerOfJobLoc[MyUnit[uix].Loc] := uix;
      DistToLoc[MyUnit[uix].Loc] := 0;
    end;

  // assign remaining jobs to remaining settlers
  UnitsToAssign := True;
  while UnitsToAssign do
  begin
    UnitsToAssign := False;
    for uix := 0 to RO.nUn - 1 do
      if JobLocOfSettler[uix] = ToAssign then
      begin
        BestJob := jNone;
        BestScore := -999999;
        FillChar(TileChecked, MapSize * sizeof(boolean), False);
        Pile.Create(MapSize);
        Pile.Put(MyUnit[uix].Loc, 0); // start search for new job at current location
        while Pile.Get(TestLoc, TestDistance) do
        begin
          // add surrounding tiles to queue, but only if there's a chance to beat BestScore
          if MaxScore - DistanceScore * (TestDistance + 1) >= BestScore then
          begin
            V8_to_Loc(TestLoc, Adjacent);
            for V8 := 0 to 7 do
            begin
              NextLoc := Adjacent[V8];
              if (NextLoc >= 0) and not TileChecked[NextLoc] and
                (Map[NextLoc] and fTerrain <> fUNKNOWN) then
              begin
                StepSize := StepSizeByTerrain[Map[NextLoc] and fTerrain];
                if (StepSize > 0) // no water or arctic tile
                  and (Map[NextLoc] and (fUnit or fOwned) <> fUnit) // no foreign unit
                  and ((RO.Territory[NextLoc] < 0) or
                  (RO.Territory[NextLoc] = me)) // no foreign territory
                  and (Map[TestLoc] and Map[NextLoc] and fInEnemyZoC = 0) then
                  // move not prevented by ZoC
                  Pile.Put(NextLoc, TestDistance + StepSize);
                // simplification, only optimal for 150 mp units in land with no roads
              end;
            end;
          end;

          // check tile for job
          if (TileJob[TestLoc] > jNone) and
            ((MyModel[MyUnit[uix].mix].Kind <> mkSlaves) or
            (TileJob[TestLoc] <> jCity)) and
            ((SettlerOfJobLoc[TestLoc] < 0) or (DistToLoc[TestLoc] > TestDistance)) then
          begin
            TestScore := integer(TileJobScore[TestLoc]) - DistanceScore * TestDistance;
            if TestScore > BestScore then
              BestCount := 0;
            if TestScore >= BestScore then
            begin
              Inc(BestCount);
              if random(BestCount) = 0 then
              begin
                BestScore := TestScore;
                BestLoc := TestLoc;
                BestJob := TileJob[TestLoc];
                BestDistance := TestDistance;
              end;
            end;
          end;
          TileChecked[TestLoc] := True;
        end;
        Pile.Free;

        if BestJob > jNone then
        begin // new job found for this unit
          if SettlerOfJobLoc[BestLoc] >= 0 then
          begin // another unit was already assigned to this job, but is not as close -- reassign that unit!
            JobLocOfSettler[SettlerOfJobLoc[BestLoc]] := ToAssign;
            UnitsToAssign := True;
          end;
          JobLocOfSettler[uix] := BestLoc;
          SettlerOfJobLoc[BestLoc] := uix;
          DistToLoc[BestLoc] := BestDistance;
        end
        else
          JobLocOfSettler[uix] := -1; // no jobs for this settler
      end; // for uix
  end;

  // move settlers and start new jobs
  for uix := 0 to RO.nUn - 1 do
    with MyUnit[uix] do
      if (Loc >= 0) and (Job = jNone) and (JobLocOfSettler[uix] >= 0) then
      begin
        if Loc <> JobLocOfSettler[uix] then
          repeat
            MoveResult := Unit_Move(uix, JobLocOfSettler[uix])
          until (MoveResult < rExecuted) or (MoveResult and
              (rLocationReached or rMoreTurns or rUnitRemoved) <> 0);
        if (Loc = JobLocOfSettler[uix]) and (Movement >= 100) then
          Unit_StartJob(uix, TileJob[JobLocOfSettler[uix]]);
      end;
end;

//------------------------------------------------------------------------------
// Map Analysis

procedure TToolAI.AnalyzeMap;

type
  tIndexOfID = array[0..lxmax * lymax - 1] of integer;
  tIDOfIndex = array[0..lxmax * lymax div 2 - 1] of integer;

var
  i, j, Loc, Loc1, V8, Count, Kind, MostIndex: integer;
  Adjacent: TVicinity8Loc;
  IndexOfID: tIndexOfID;
  IDOfIndex: tIDOfIndex;

  // Clamp Loc+1 to MapSize-1, to avoid range check error
  function MaxLoc : integer;
  begin
    result := min ((MapSize - 1),(Loc+1));
  end;

begin
  fillchar(District, MapSize * 4, $FF);

  IndexOfID := Default (tIndexOfID);
  IDOfIndex := Default (tIDOfIndex);

  for Loc := 0 to MapSize - 1 do
    if Map[Loc] and fTerrain = fUNKNOWN then
      Formation[Loc] := nfUndiscovered
    else if Map[Loc] and fTerrain = fArctic then
      Formation[Loc] := nfPole
    else if Map[Loc] and fPeace <> 0 then
      Formation[Loc] := nfPeace
    else
    begin
      Formation[Loc] := Loc;
      V8_to_Loc(Loc, Adjacent);
      for V8 := 0 to 7 do
      begin
        Loc1 := Adjacent[V8];
        if (Loc1 < Loc) and (Loc1 >= 0) and (Formation[Loc1] >= 0) and
          ((Map[Loc1] and fTerrain >= fGrass) = (Map[Loc] and fTerrain >= fGrass)) then
          if Formation[Loc] = Loc then
            Formation[Loc] := Formation[Loc1]
          else if Formation[Loc] < Formation[Loc1] then
            ReplaceD(@Formation[Formation[Loc1]], @Formation[MaxLoc],
              Formation[Loc1], Formation[Loc])
          else if Formation[Loc] > Formation[Loc1] then
            ReplaceD(@Formation[Formation[Loc]], @Formation[MaxLoc],
              Formation[Loc], Formation[Loc1]);
      end;

      if (RO.Territory[Loc] = me) and (Map[Loc] and fTerrain >= fGrass) then
      begin
        District[Loc] := Loc;
        for V8 := 0 to 7 do
        begin
          Loc1 := Adjacent[V8];
          if (Loc1 < Loc) and (Loc1 >= 0) and (District[Loc1] >= 0) then
            if District[Loc] = Loc then
              District[Loc] := District[Loc1]
            else if District[Loc] < District[Loc1] then
              ReplaceD(@District[District[Loc1]], @District[MaxLoc],
                District[Loc1], District[Loc])
            else if District[Loc] > District[Loc1] then
              ReplaceD(@District[District[Loc]], @District[MaxLoc],
                District[Loc], District[Loc1]);
        end;
      end;
    end;

  // sort continents, oceans and districts by size
  for Kind := 0 to 2 do
  begin
    FillChar(IndexOfID, MapSize * 2, 0);
    case Kind of
      0: // continents
        for Loc := 0 to MapSize - 1 do
          if (Formation[Loc] >= 0) and (Map[Loc] and fTerrain >= fGrass) then
            Inc(IndexOfID[Formation[Loc]]);
      1: // oceans
        for Loc := 0 to MapSize - 1 do
          if (Formation[Loc] >= 0) and (Map[Loc] and fTerrain < fGrass) then
            Inc(IndexOfID[Formation[Loc]]);
      2: // districts
        for Loc := 0 to MapSize - 1 do
          if District[Loc] >= 0 then
            Inc(IndexOfID[District[Loc]]);
    end;

    Count := 0;
    for Loc := 0 to MapSize - 1 do
      if IndexOfID[Loc] > 0 then
      begin
        IDOfIndex[Count] := Loc;
        Inc(Count);
      end;
    for i := 0 to Count - 2 do
    begin
      MostIndex := i;
      for j := i + 1 to Count - 1 do
        if IndexOfID[IDOfIndex[j]] > IndexOfID[IDOfIndex[MostIndex]] then
          MostIndex := j;
      if MostIndex <> i then
      begin
        j := IDOfIndex[i];
        IDOfIndex[i] := IDOfIndex[MostIndex];
        IDOfIndex[MostIndex] := j;
      end;
    end;
    for i := 0 to Count - 1 do
      IndexOfID[IDOfIndex[i]] := i;

    case Kind of
      0: // continents
      begin
        nContinent := Count;
        for Loc := 0 to MapSize - 1 do
          if (Formation[Loc] >= 0) and (Map[Loc] and fTerrain >= fGrass) then
            Formation[Loc] := IndexOfID[Formation[Loc]];
      end;
      1: // oceans
      begin
        nOcean := Count;
        for Loc := 0 to MapSize - 1 do
          if (Formation[Loc] >= 0) and (Map[Loc] and fTerrain < fGrass) then
            Formation[Loc] := IndexOfID[Formation[Loc]];
      end;
      2: // districts
      begin
        nDistrict := Count;
        for Loc := 0 to MapSize - 1 do
          if District[Loc] >= 0 then
            District[Loc] := IndexOfID[District[Loc]];
      end;
    end;
  end;
end;

//------------------------------------------------------------------------------
// Path Finding

const
  // basic move styles
  msGround = $00000000;
  msNoGround = $10000000;
  msAlpine = $20000000;
  msOver = $40000000;
  msSpy = $50000000;

  // other
  msHostile = $08000000;

// bits:   |31|30|29|28|27|26 .. 16|15|14|13|12|11|10| 9| 8| 7| 6| 5| 4| 3| 2| 1| 0|
// ground: |   Basic   |Ho| Speed  |       HeavyCost       |        RailCost       |
// other:  |   Basic   | 0| Speed  |              X X X             | MaxTerrType  |

function TToolAI.GetMyMoveStyle(mix, Health: integer): integer;
begin
  with MyModel[mix] do
  begin
    Result := Speed shl 16;
    case Domain of
      dGround:
      begin
        Inc(Result, (50 + (Speed - 150) * 13 shr 7) shl 8); //HeavyCost
        if RO.Wonder[woShinkansen].EffectiveOwner <> me then
          Inc(Result, Speed * (4 * 1311) shr 17); // RailCost

        if (RO.Wonder[woGardens].EffectiveOwner <> me)
        and not ((Kind = mkSettler) and (Speed >= 200)) then // unit not an engineer
          Inc(Result, msHostile);

        if Kind = mkDiplomat then
          Inc(Result, msSpy)
        else if Cap[mcOver] > 0 then
          Inc(Result, msOver)
        else if Cap[mcAlpine] > 0 then
          Inc(Result, msAlpine)
        else
          Inc(Result, msGround);
      end;
      dSea:
      begin
        Result := Speed;
        if RO.Wonder[woMagellan].EffectiveOwner = me then
          Inc(Result, 200);
        if Health < 100 then
          Result := ((Result - 250) * Health div 5000) * 50 + 250;
        Result := Result shl 16;
        Inc(Result, msNoGround);
        if Cap[mcNav] > 0 then
          Inc(Result);
      end;
      dAir:
        Inc(Result, msNoGround + fUNKNOWN xor 1 - 1);
    end;
  end;
end;

function TToolAI.CheckStep(MoveStyle, TimeBeforeStep, CrossCorner: integer;
  var TimeAfterStep, RecoverTurns: integer; FromTile, ToTile: integer;
  IsCapture: boolean): integer;
var
  MoveCost, RecoverCost: integer;
begin
  //IsCapture:=true;
  assert(((FromTile and fTerrain <= fMountains) or (FromTile and
    fTerrain = fUNKNOWN)) and ((ToTile and fTerrain <= fMountains) or
    (ToTile and fTerrain = fUNKNOWN)));
  // do not pass location codes for FromTile and ToTile!
  RecoverTurns  := 0;
  TimeAfterStep := 0;
  if MoveStyle < msGround + $10000000 then
  begin // common ground units
    if (ToTile + 1) and fTerrain < fGrass + 1 then
      Result := csForbiddenTile
    else if (ToTile and not FromTile and fPeace = 0) and
      (ToTile and (fUnit or fOwned) <> fUnit) and
      (IsCapture or (ToTile and (fCity or fOwned) <> fCity)) then
      if (FromTile and fCity <> 0) or (ToTile and (fCity or fOwned) = fCity or fOwned) or
        (ToTile and FromTile and (fInEnemyZoc or fOwnZoCUnit) <> fInEnemyZoc) then
      begin // ZoC is ok
        if (ToTile and (fRR or fCity) = 0) or (FromTile and (fRR or fCity) = 0) then
        begin // no railroad
          if (ToTile and (fRoad or fRR or fCity) <> 0) and
            (FromTile and (fRoad or fRR or fCity) <> 0) or
            (FromTile and ToTile and (fRiver or fCanal) <> 0) then
            MoveCost := 20 //move along road, river or canal
          else
          begin
            case Terrain[ToTile and fTerrain].MoveCost of
              1: MoveCost := 50; // plain terrain
              2: MoveCost := MoveStyle shr 8 and $FF; // heavy terrain
              else // mountains
              begin
                if TimeBeforeStep and $FFF + MoveStyle shr 16 and $7FF <= $800 then
                  TimeAfterStep := TimeBeforeStep and $7FFFF000 + $1800
                else
                  TimeAfterStep := TimeBeforeStep and $7FFFF000 + $2800;
                // must wait for next turn
                if (MoveStyle and msHostile <> 0) and
                  ((FromTile and (fTerrain or fSpecial1) = fDesert) or
                  (FromTile and fTerrain = fArctic)) and
                  (FromTile and (fCity or fRiver or fCanal) = 0) then
                begin
                  RecoverCost := ($800 - TimeBeforeStep and $FFF) * 5 shr 1;
                  while RecoverCost > 0 do
                  begin
                    Inc(RecoverTurns);
                    Dec(RecoverCost, MoveStyle shr 16 and $7FF);
                  end;
                end;
                Result := csOk;
                if ToTile and fPeace <> 0 then
                  Result := csCheckTerritory;
                exit;
              end;
            end;
          end;
        end
        else
          MoveCost := MoveStyle and $FF; //move along railroad

        Inc(MoveCost, MoveCost shl CrossCorner);
        if (MoveStyle and msHostile = 0) or
          (ToTile and (fTerrain or fSpecial1) <> fDesert) and
          (ToTile and fTerrain <> fArctic) or (ToTile and
          (fCity or fRiver or fCanal) <> 0) or (ToTile and fTerImp = tiBase) then
          RecoverCost := 0
        else
          RecoverCost := (MoveCost * 5) shr 1;
        // damage from movement: MoveCost*DesertThurst/NoCityRecovery
        if (TimeBeforeStep and $FFF + MoveCost <= $800) and
          (TimeBeforeStep and $FFF < $800) then
          TimeAfterStep := TimeBeforeStep + MoveCost
        else
        begin
          TimeAfterStep := TimeBeforeStep and $7FFFF000 + $1800 -
            MoveStyle shr 16 and $7FF + MoveCost; // must wait for next turn
          if (MoveStyle and msHostile <> 0) and
            ((FromTile and (fTerrain or fSpecial1) = fDesert) or
            (FromTile and fTerrain = fArctic)) and
            (FromTile and (fCity or fRiver or fCanal) = 0) and
            (FromTile and fTerImp <> tiBase) then
            Inc(RecoverCost, ($800 - TimeBeforeStep and $FFF) * 5 shr 1);
        end;
        while RecoverCost > 0 do
        begin
          Inc(RecoverTurns);
          Dec(RecoverCost, MoveStyle shr 16 and $7FF);
        end;
        Result := csOk;
        if ToTile and fPeace <> 0 then
          Result := csCheckTerritory;
      end
      else
        Result := csForbiddenStep // ZoC violation
    else
      Result := csForbiddenTile;
  end

  else if MoveStyle < msNoGround + $10000000 then
  begin // ships and aircraft
    if ((ToTile and fTerrain xor 1 > MoveStyle and fTerrain) and
      (ToTile and (fCity or fCanal) = 0)) or (ToTile and not FromTile and fPeace <> 0) or
      (ToTile and (fUnit or fOwned) = fUnit) or (ToTile and
      (fCity or fOwned) = fCity) then
      Result := csForbiddenTile
    else
    begin
      MoveCost := 50 shl CrossCorner + 50;
      if TimeBeforeStep and $FFF + MoveCost <= $800 then
        TimeAfterStep := TimeBeforeStep + MoveCost
      else
        TimeAfterStep := TimeBeforeStep and $7FFFF000 + $1800 -
          MoveStyle shr 16 and $7FF + MoveCost;
      // must wait for next turn
      Result := csOk;
      if ToTile and fPeace <> 0 then
        Result := csCheckTerritory;
    end;
  end

  else if MoveStyle < msAlpine + $10000000 then
  begin // alpine
    if (ToTile + 1) and fTerrain < fGrass + 1 then
      Result := csForbiddenTile
    else if (ToTile and not FromTile and fPeace = 0) and
      (ToTile and (fUnit or fOwned) <> fUnit) and
      (IsCapture or (ToTile and (fCity or fOwned) <> fCity)) then
      if (FromTile and fCity <> 0) or (ToTile and (fCity or fOwned) = fCity or fOwned) or
        (ToTile and FromTile and (fInEnemyZoc or fOwnZoCUnit) <> fInEnemyZoc) then
      begin
        if (ToTile and (fRR or fCity) = 0) or (FromTile and (fRR or fCity) = 0) then
          MoveCost := 20 // no railroad
        else
          MoveCost := MoveStyle and $FF; //move along railroad
        Inc(MoveCost, MoveCost shl CrossCorner);
        if (TimeBeforeStep and $FFF + MoveCost <= $800) and
          (TimeBeforeStep and $FFF < $800) then
          TimeAfterStep := TimeBeforeStep + MoveCost
        else
          TimeAfterStep := TimeBeforeStep and $7FFFF000 + $1800 -
            MoveStyle shr 16 and $7FF + MoveCost;
        // must wait for next turn
        Result := csOk;
        if ToTile and fPeace <> 0 then
          Result := csCheckTerritory;
      end
      else
        Result := csForbiddenStep // ZoC violation
    else
      Result := csForbiddenTile;
  end

  else if MoveStyle < msOver + $10000000 then
  begin // overweight
    if (ToTile + 1) and fTerrain < fGrass + 1 then
      Result := csForbiddenTile
    else if (ToTile and not FromTile and fPeace = 0) and
      (ToTile and (fUnit or fOwned) <> fUnit) and
      (IsCapture or (ToTile and (fCity or fOwned) <> fCity)) then
      if (FromTile and fCity <> 0) or (ToTile and (fCity or fOwned) = fCity or fOwned) or
        (ToTile and FromTile and (fInEnemyZoc or fOwnZoCUnit) <> fInEnemyZoc) then
      begin
        if (ToTile and (fRR or fCity) = 0) or (FromTile and (fRR or fCity) = 0) then
        begin // no railroad
          if (ToTile and (fRoad or fRR or fCity) <> 0) and
            (FromTile and (fRoad or fRR or fCity) <> 0) or
            (FromTile and ToTile and (fRiver or fCanal) <> 0) then
            MoveCost := 40 //move along road, river or canal
          else
          begin
            Result := csForbiddenTile;
            exit;
          end;
        end
        else
          MoveCost := MoveStyle and $FF; //move along railroad
        Inc(MoveCost, MoveCost shl CrossCorner);
        if (TimeBeforeStep and $FFF + MoveCost <= $800) and
          (TimeBeforeStep and $FFF < $800) then
          TimeAfterStep := TimeBeforeStep + MoveCost
        else
          TimeAfterStep := TimeBeforeStep and $7FFFF000 + $1800 -
            MoveStyle shr 16 and $7FF + MoveCost;
        // must wait for next turn
        Result := csOk;
        if ToTile and fPeace <> 0 then
          Result := csCheckTerritory;
      end
      else
        Result := csForbiddenStep // ZoC violation
    else
      Result := csForbiddenTile;
  end

  else {if MoveStyle<msSpy+$10000000 then}
  begin // spies
    if (ToTile + 1) and fTerrain < fGrass + 1 then
      Result := csForbiddenTile
    else if (ToTile and (fUnit or fOwned) <> fUnit) and
      (IsCapture or (ToTile and (fCity or fOwned) <> fCity)) then
    begin
      if (ToTile and (fRR or fCity) = 0) or (FromTile and (fRR or fCity) = 0) then
      begin // no railroad
        if (ToTile and (fRoad or fRR or fCity) <> 0) and
          (FromTile and (fRoad or fRR or fCity) <> 0) or
          (FromTile and ToTile and (fRiver or fCanal) <> 0) then
          MoveCost := 20 //move along road, river or canal
        else
        begin
          case Terrain[ToTile and fTerrain].MoveCost of
            1: MoveCost := 50; // plain terrain
            2: MoveCost := MoveStyle shr 8 and $FF; // heavy terrain
            else // mountains
            begin
              if TimeBeforeStep and $FFF + MoveStyle shr 16 and $7FF <= $800 then
                TimeAfterStep := TimeBeforeStep and $7FFFF000 + $1800
              else
                TimeAfterStep := TimeBeforeStep and $7FFFF000 + $2800;
              // must wait for next turn
              Result := csOk;
              exit;
            end;
          end;
        end;
      end
      else
        MoveCost := MoveStyle and $FF; //move along railroad
      Inc(MoveCost, MoveCost shl CrossCorner);
      if (TimeBeforeStep and $FFF + MoveCost <= $800) and
        (TimeBeforeStep and $FFF < $800) then
        TimeAfterStep := TimeBeforeStep + MoveCost
      else
        TimeAfterStep := TimeBeforeStep and $7FFFF000 + $1800 -
          MoveStyle shr 16 and $7FF + MoveCost;
      // must wait for next turn
      Result := csOk;
    end
    else
      Result := csForbiddenTile;
  end;
end;

(*
-------- Pathfinding Reference Implementation --------
var
MoveStyle,V8,Loc,Time,NextLoc,NextTime,RecoverTurns: integer;
Adjacent: TVicinity8Loc;
Reached: array[0..lxmax*lymax-1] of boolean;
begin
fillchar(Reached, MapSize, false);
MoveStyle:=GetMyMoveStyle(MyUnit[uix].mix, MyUnit[uix].Health);
Pile.Create(MapSize);
Pile.Put(MyUnit[uix].Loc, $800-MyUnit[uix].Movement);
while Pile.Get(Loc, Time) do
  begin
  // todo: check exit condition, e.g. whether destination reached

  Reached[Loc]:=true;
  V8_to_Loc(Loc, Adjacent);
  for V8:=0 to 7 do
    begin
    NextLoc:=Adjacent[V8];
    if (NextLoc>=0) and not Reached[NextLoc] then
      case CheckStep(MoveStyle, Time, V8 and 1, NextTime, RecoverTurns, Map[Loc], Map[NextLoc]) of
        csOk:
          Pile.Put(NextLoc, NextTime+RecoverTurns*$1000);
        csForbiddenTile:
          Reached[NextLoc]:=true; // don't check moving there again
        csCheckTerritory:
          if RO.Territory[NextLoc]=RO.Territory[Loc] then
            Pile.Put(NextLoc, NextTime+RecoverTurns*$1000);
        end
    end;
  end;
Pile.Free;
end;
*)

function TToolAI.Unit_MoveEx(uix, ToLoc: integer; Options: integer): integer;
type
  tPreLoc = array[0..lxmax * lymax - 1] of integer;

var
  Loc, NextLoc, Temp, FromLoc, EndLoc, Time, V8, MoveResult, RecoverTurns,
  NextTime, MoveStyle: integer;
  Adjacent: TVicinity8Loc;
  PreLoc: tPreLoc;
  Reached: array[0..lxmax * lymax - 1] of boolean;
begin
  Result := eOk;
  FromLoc := MyUnit[uix].Loc;
  if FromLoc = ToLoc then
    exit;

  PreLoc := Default (tPreLoc);

  FillChar(Reached, MapSize, False);
  MoveStyle := GetMyMoveStyle(MyUnit[uix].mix, MyUnit[uix].Health);
  EndLoc := -1;
  Pile.Create(MapSize);
  Pile.Put(FromLoc, $800 - MyUnit[uix].Movement);
  while Pile.Get(Loc, Time) do
  begin
    if (Loc = ToLoc) or (ToLoc = maNextCity) and (Map[Loc] and fCity <> 0) and
      (Map[Loc] and fOwned <> 0) then
    begin
      EndLoc := Loc;
      Break;
    end;
    Reached[Loc] := True;
    V8_to_Loc(Loc, Adjacent);
    for V8 := 0 to 7 do
    begin
      NextLoc := Adjacent[V8];
      if NextLoc >= 0 then
        if (NextLoc = ToLoc) and (Options and mxAdjacent <> 0) then
        begin
          EndLoc := Loc;
          Break;
        end
        else if not Reached[NextLoc] then
        begin
          case CheckStep(MoveStyle, Time, V8 and 1, NextTime, RecoverTurns,
              Map[Loc], Map[NextLoc], NextLoc = ToLoc) of
            csOk:
              if Pile.Put(NextLoc, NextTime + RecoverTurns * $1000) then
                PreLoc[NextLoc] := Loc;
            csForbiddenTile:
              Reached[NextLoc] := True; // don't check moving there again
            csCheckTerritory:
              if RO.Territory[NextLoc] = RO.Territory[Loc] then
                if Pile.Put(NextLoc, NextTime + RecoverTurns * $1000) then
                  PreLoc[NextLoc] := Loc;
          end;
        end;
    end;
    if EndLoc >= 0 then
      Break;
  end;
  Pile.Free;

  if EndLoc >= 0 then
  begin
    Loc := EndLoc;
    NextLoc := PreLoc[Loc];
    while Loc <> FromLoc do
    begin // invert meaning of PreLoc
      Temp := Loc;
      Loc := NextLoc;
      NextLoc := PreLoc[Loc];
      PreLoc[Loc] := Temp;
    end;
    while Loc <> EndLoc do
    begin
      Loc := PreLoc[Loc];
      MoveResult := Unit_Step(uix, Loc);
      if (MoveResult <> eOK) and (MoveResult <> eLoaded) then
      begin
        Result := MoveResult;
        break;
      end;
    end;
  end
  else
    Result := eNoWay;
end;

//------------------------------------------------------------------------------
// Oversea Transport

procedure TToolAI.SeaTransport_BeginInitialize;
begin
  fillchar(TransportAvailable, RO.nUn * sizeof(integer), $FF); // -1
  InitComplete := False;
  HaveDestinations := False;
  nTransportLoad := 0;
  TransportMoveStyle := 0;
  TransportCapacity := $100;
  Pile.Create(MapSize);
end;

procedure TToolAI.SeaTransport_AddLoad(uix: integer);
var
  i: integer;
begin
  assert(not InitComplete); // call order violation!
  if Map[MyUnit[uix].Loc] and fTerrain < fGrass then
    exit;
  for i := 0 to nTransportLoad - 1 do
    if uix = uixTransportLoad[i] then
      exit;
  uixTransportLoad[nTransportLoad] := uix;
  Inc(nTransportLoad);
end;

procedure TToolAI.SeaTransport_AddTransport(uix: integer);
var
  MoveStyle: integer;
begin
  assert(not InitComplete); // call order violation!
  assert(MyModel[MyUnit[uix].mix].Cap[mcSeaTrans] > 0);
  TransportAvailable[uix] := 1;
  with MyModel[MyUnit[uix].mix] do
  begin
    if MTrans * Cap[mcSeaTrans] < TransportCapacity then
      TransportCapacity := MTrans * Cap[mcSeaTrans];
    MoveStyle := GetMyMoveStyle(MyUnit[uix].mix, 100);
    if (TransportMoveStyle = 0) or (MoveStyle < TransportMoveStyle) and
      (MoveStyle and not TransportMoveStyle and 1 = 0) or
      (not MoveStyle and TransportMoveStyle and 1 <> 0) then
      TransportMoveStyle := MoveStyle;
  end;
end;

procedure TToolAI.SeaTransport_AddDestination(Loc: integer);
begin
  assert(not InitComplete); // call order violation!
  Pile.Put(Loc, $800);
  HaveDestinations := True;
end;

procedure TToolAI.SeaTransport_EndInitialize;
var
  Loc0, Time0, V8, Loc1, ArriveTime, RecoverTurns: integer;
  Adjacent: TVicinity8Loc;
begin
  assert(not InitComplete); // call order violation!
  InitComplete := True;
  if HaveDestinations then
  begin // calculate TurnsAfterLoad from destination locs
    fillchar(TurnsAfterLoad, MapSize, $FF); // -1
    while Pile.Get(Loc0, Time0) do
    begin // search backward
      if Time0 = $800 then
        TurnsAfterLoad[Loc0] := 1
      else
        TurnsAfterLoad[Loc0] := Time0 shr 12;
      V8_to_Loc(Loc0, Adjacent);
      for V8 := 0 to 7 do
      begin
        Loc1 := Adjacent[V8];
        if (Loc1 >= 0) and (TurnsAfterLoad[Loc1] = -1) then
        begin
          case CheckStep(TransportMoveStyle, Time0, V8 and 1, ArriveTime,
              RecoverTurns, Map[Loc0], Map[Loc1], False) of
            csOk: Pile.Put(Loc1, ArriveTime);
            csForbiddenStep: TurnsAfterLoad[Loc1] := -2;
          end;
        end;
      end;
    end;
  end;
  Pile.Free;
end;

function TToolAI.SeaTransport_MakeGroupPlan(
  var TransportPlan: TGroupTransportPlan): boolean;
type
  tSelectedLoad = array[0..15] of integer;

var
  V8, i, j, iPicked, uix, Loc0, Time0, Loc1, RecoverTurns, MoveStyle,
  TurnsLoaded, TurnCount, tuix, tuix1, ArriveTime, TotalDelay,
  BestTotalDelay, GroupCount, BestGroupCount, BestLoadLoc, FullMovementLoc,
  nSelectedLoad, f, OriginContinent, a, b: integer;
  CompleteFlag, NotReachedFlag, ContinueUnit: cardinal;
  IsComplete, ok, IsFirstLoc: boolean;
  StartLocPtr, ArrivedEnd: pinteger;
  Adjacent: TVicinity8Loc;
  uixSelectedLoad:  tSelectedLoad;
  tuixSelectedLoad: tSelectedLoad;
  Arrived: array[0..lxmax * lymax] of cardinal;
  ResponsibleTransport: array[0..lxmax * lymax - 1] of integer;
  TurnsBeforeLoad: array[0..lxmax * lymax - 1] of shortint;
  GroupComplete: array[0..lxmax * lymax - 1] of boolean;
begin
  assert(InitComplete); // call order violation!
  bestGroupCount := 0;
  bestTotalDelay := 0;

  uixSelectedLoad	:= Default (tSelectedLoad);
  tuixSelectedLoad	:= Default (tSelectedLoad);

  if HaveDestinations and (nTransportLoad > 0) then
  begin // transport and units already adjacent?
    for uix := 0 to RO.nUn - 1 do
      if (TransportAvailable[uix] > 0) and (Map[MyUnit[uix].Loc] and
        fTerrain = fShore) then
      begin
        GroupCount := 0;
        for tuix := 0 to nTransportLoad - 1 do
        begin
          Loc_to_ab(MyUnit[uix].Loc, MyUnit[uixTransportLoad[tuix]].Loc, a, b);
          if (abs(a) <= 1) and (abs(b) <= 1) then
          begin
            assert((a <> 0) or (b <> 0));
            Inc(GroupCount);
          end;
        end;
        if (GroupCount = nTransportLoad) or (GroupCount >= TransportCapacity) then
        begin
          TransportPlan.LoadLoc := MyUnit[uix].Loc;
          TransportPlan.uixTransport := uix;
          TransportAvailable[uix] := 0;
          TransportPlan.TurnsEmpty := 0;
          TransportPlan.TurnsLoaded := TurnsAfterLoad[TransportPlan.LoadLoc];
          TransportPlan.nLoad := 0;
          for tuix := nTransportLoad - 1 downto 0 do
          begin
            Loc_to_ab(TransportPlan.LoadLoc, MyUnit[uixTransportLoad[tuix]].Loc, a, b);
            if (abs(a) <= 1) and (abs(b) <= 1) then
            begin
              TransportPlan.uixLoad[TransportPlan.nLoad] := uixTransportLoad[tuix];
              uixTransportLoad[tuix] := uixTransportLoad[nTransportLoad - 1];
              Dec(nTransportLoad);
              Inc(TransportPlan.nLoad);
              if TransportPlan.nLoad = TransportCapacity then
                break;
            end;
          end;
          Result := True;
          exit;
        end;
      end;
  end;

  while HaveDestinations and (nTransportLoad > 0) do
  begin
    // select units from same continent
    fillchar(Arrived, 4 * nContinent, 0); // misuse Arrived as counter
    for tuix := 0 to nTransportLoad - 1 do
    begin
      assert(Map[MyUnit[uixTransportLoad[tuix]].Loc] and fTerrain >= fGrass);
      f := Formation[MyUnit[uixTransportLoad[tuix]].Loc];
      if f >= 0 then
        Inc(Arrived[f]);
    end;
    OriginContinent := 0;
    for f := 1 to nContinent - 1 do
      if Arrived[f] > Arrived[OriginContinent] then
        OriginContinent := f;
    nSelectedLoad := 0;
    for tuix := 0 to nTransportLoad - 1 do
      if Formation[MyUnit[uixTransportLoad[tuix]].Loc] = OriginContinent then
      begin
        tuixSelectedLoad[nSelectedLoad] := tuix;
        uixSelectedLoad[nSelectedLoad] := uixTransportLoad[tuix];
        Inc(nSelectedLoad);
        if nSelectedLoad = 16 then
          break;
      end;

    Pile.Create(MapSize);
    fillchar(ResponsibleTransport, MapSize * 2, $FF); // -1
    fillchar(TurnsBeforeLoad, MapSize, $FF); // -1
    ok := False;
    for uix := 0 to RO.nUn - 1 do
      if TransportAvailable[uix] > 0 then
      begin
        ok := True;
        Pile.Put(MyUnit[uix].Loc, ($800 - MyUnit[uix].Movement) shl 12 + uix);
      end;
    if not ok then // no transports
    begin
      TransportPlan.LoadLoc := -1;
      Result := False;
      Pile.Free;
      exit;
    end;
    while Pile.Get(Loc0, Time0) do
    begin
      uix := Time0 and $FFF;
      Time0 := Time0 shr 12;
      ResponsibleTransport[Loc0] := uix;
      TurnsBeforeLoad[Loc0] := Time0 shr 12;
      V8_to_Loc(Loc0, Adjacent);
      for V8 := 0 to 7 do
      begin
        Loc1 := Adjacent[V8];
        if (Loc1 >= 0) and (ResponsibleTransport[Loc1] < 0) then
          case CheckStep(GetMyMoveStyle(MyUnit[uix].mix, MyUnit[uix].Health),
              Time0, V8 and 1, ArriveTime, RecoverTurns, Map[Loc0], Map[Loc1], False) of
            csOk: Pile.Put(Loc1, ArriveTime shl 12 + uix);
            csForbiddenTile: ResponsibleTransport[Loc1] := RO.nUn; // don't check again
          end;
      end;
    end;

    fillchar(Arrived, MapSize * 4, $55); // set NotReachedFlag for all tiles
    fillchar(GroupComplete, MapSize, False);
    BestLoadLoc := -1;

    // check direct loading
    for tuix := 0 to nSelectedLoad - 1 do
    begin
      uix := uixSelectedLoad[tuix];
      if MyUnit[uix].Movement = integer(MyModel[MyUnit[uix].mix].Speed) then
      begin
        NotReachedFlag := 1 shl (2 * tuix);
        CompleteFlag := NotReachedFlag shl 1;
        V8_to_Loc(MyUnit[uix].Loc, Adjacent);
        for V8 := 0 to 7 do
        begin
          Loc1 := Adjacent[V8];
          if (Loc1 >= 0) and (Map[Loc1] and fTerrain < fGrass) and
            not GroupComplete[Loc1] then
          begin // possible transport start location
            Arrived[Loc1] := (Arrived[Loc1] or CompleteFlag) and not NotReachedFlag;
            if (TurnsBeforeLoad[Loc1] >= 0) and (TurnsAfterLoad[Loc1] >= 0) then
            begin
              i := 1;
              GroupCount := 0;
              for tuix1 := 0 to nSelectedLoad - 1 do
              begin
                if Arrived[loc1] and i = 0 then
                  Inc(GroupCount);
                i := i shl 2;
              end;
              assert(GroupCount <= TransportCapacity);
              if (GroupCount = TransportCapacity) or (GroupCount = nSelectedLoad) then
                GroupComplete[loc1] := True;
              TotalDelay := TurnsBeforeLoad[Loc1] + TurnsAfterLoad[Loc1];
              if (BestLoadLoc < 0) or (GroupCount shl 16 -
                TotalDelay > BestGroupCount shl 16 - BestTotalDelay) then
              begin
                BestLoadLoc := Loc1;
                BestGroupCount := GroupCount;
                BestTotalDelay := TotalDelay;
              end;
            end;
          end;
        end;
      end;
    end;

    TurnCount := 0;
    ArrivedEnd := @Arrived[MapSize];

    // check moving+loading
    ContinueUnit := 1 shl nSelectedLoad - 1;
    while (ContinueUnit > 0) and ((BestLoadLoc < 0) or
        (TurnCount < BestTotalDelay - 2)) do
    begin
      for tuix := 0 to nSelectedLoad - 1 do
        if 1 shl tuix and ContinueUnit <> 0 then
        begin
          uix := uixSelectedLoad[tuix];
          MoveStyle := GetMyMoveStyle(MyUnit[uix].mix, MyUnit[uix].Health);
          NotReachedFlag := 1 shl (2 * tuix);
          CompleteFlag := NotReachedFlag shl 1;
          FullMovementLoc := -1;

          Pile.Empty;
          if TurnCount = 0 then
          begin
            Pile.Put(MyUnit[uix].Loc, $1800 - MyUnit[uix].Movement);
            if MyUnit[uix].Movement = integer(MyModel[MyUnit[uix].mix].Speed) then
              FullMovementLoc := MyUnit[uix].Loc;
            // surrounding tiles can be loaded immediately
            StartLocPtr := ArrivedEnd;
          end
          else
            StartLocPtr := @Arrived;
          IsFirstLoc := True;

          repeat
            if StartLocPtr <> ArrivedEnd then
              // search next movement start location for this turn
              StartLocPtr := NextZero(StartLocPtr, ArrivedEnd,
                CompleteFlag or NotReachedFlag);
            if StartLocPtr <> ArrivedEnd then
            begin
              Loc0 := (ptruint(StartLocPtr) - ptruint(@Arrived)) shr 2;
              Inc(StartLocPtr);
              Time0 := $800;
            end
            else if not Pile.Get(Loc0, Time0) then
            begin
              if IsFirstLoc then
                ContinueUnit := ContinueUnit and not (1 shl tuix);
              break;
            end;
            IsFirstLoc := False;

            Arrived[Loc0] := Arrived[Loc0] and not NotReachedFlag;
            if not GroupComplete[Loc0] and (Map[Loc0] and fTerrain <> fMountains) then
            begin // check whether group complete -- no mountains because complete flag might be faked there
              i := 1;
              GroupCount := 0;
              for tuix1 := 0 to nSelectedLoad - 1 do
              begin
                if Arrived[Loc0] and i = 0 then
                  Inc(GroupCount);
                i := i shl 2;
              end;
              assert(GroupCount <= TransportCapacity);
              if (GroupCount = TransportCapacity) or (GroupCount = nSelectedLoad) then
                GroupComplete[Loc0] := True;
            end;

            V8_to_Loc(Loc0, Adjacent);
            IsComplete := True;
            for V8 := 0 to 7 do
            begin
              Loc1 := Adjacent[V8];
              if (Loc1 < G.ly) or (Loc1 >= MapSize - G.ly) then
                Adjacent[V8] := -1 // pole, don't consider moving here
              else if Arrived[Loc1] and NotReachedFlag = 0 then
                Adjacent[V8] := -1 // unit has already arrived this tile
              else if GroupComplete[Loc1] then
                Adjacent[V8] := -1 // already other group complete
              else if Map[Loc1] and fTerrain < fGrass then
              begin // possible transport start location
                Arrived[Loc1] := (Arrived[Loc1] or CompleteFlag) and not NotReachedFlag;
                Adjacent[V8] := -1;
                if (TurnsBeforeLoad[Loc1] >= 0) and (TurnsAfterLoad[Loc1] >= 0) then
                begin
                  i := 1;
                  GroupCount := 0;
                  for tuix1 := 0 to nSelectedLoad - 1 do
                  begin
                    if Arrived[loc1] and i = 0 then
                      Inc(GroupCount);
                    i := i shl 2;
                  end;
                  assert(GroupCount <= TransportCapacity);
                  if (GroupCount = TransportCapacity) or
                    (GroupCount = nSelectedLoad) then
                    GroupComplete[loc1] := True;
                  if TurnsBeforeLoad[Loc1] > TurnCount + 1 then
                    TotalDelay := TurnsBeforeLoad[Loc1] + TurnsAfterLoad[Loc1]
                  else
                    TotalDelay := TurnCount + 1 + TurnsAfterLoad[Loc1];
                  if (BestLoadLoc < 0) or (GroupCount shl
                    16 - TotalDelay > BestGroupCount shl 16 - BestTotalDelay) then
                  begin
                    BestLoadLoc := Loc1;
                    BestGroupCount := GroupCount;
                    BestTotalDelay := TotalDelay;
                  end;
                end;
              end
              else if (Map[Loc1] and fTerrain = fMountains) and
                ((Map[Loc0] and (fRoad or fRR or fCity) = 0) or
                (Map[Loc1] and (fRoad or fRR or fCity) = 0)) and
                (Map[Loc0] and Map[Loc1] and (fRiver or fCanal) = 0) then
              begin // mountain delay too complicated for this algorithm
                Arrived[Loc1] := (Arrived[Loc1] or CompleteFlag) and not NotReachedFlag;
                Adjacent[V8] := -1;
              end
              else
                IsComplete := False;
            end;
            if IsComplete then
            begin
              Arrived[Loc0] := (Arrived[Loc0] or CompleteFlag) and not NotReachedFlag;
              continue;
            end;
            IsComplete := True;
            for V8 := 0 to 7 do
            begin
              Loc1 := Adjacent[V8];
              if Loc1 >= 0 then
              begin
                ok := False;
                case CheckStep(MoveStyle, Time0, V8 and 1, ArriveTime,
                    RecoverTurns, Map[Loc0], Map[Loc1], False) of
                  csOk: ok := True;
                  csForbiddenTile:
                    ;// !!! don't check moving there again
                  csCheckTerritory:
                    ok := RO.Territory[Loc1] = RO.Territory[Loc0];
                end;
                if ok and Pile.TestPut(Loc1, ArriveTime) then
                  if ArriveTime < $2000 then
                    Pile.Put(Loc1, ArriveTime)
                  else
                    IsComplete := False;
              end;
            end;
            if IsComplete then
              Arrived[Loc0] := (Arrived[Loc0] or CompleteFlag) and not NotReachedFlag;
          until False;
        end;

      Inc(TurnCount);
    end;
    Pile.Free;

    if BestLoadLoc >= 0 then
    begin
      TransportPlan.LoadLoc := BestLoadLoc;
      TransportPlan.uixTransport := ResponsibleTransport[BestLoadLoc];
      TransportAvailable[TransportPlan.uixTransport] := 0;
      TransportPlan.TurnsEmpty := BestTotalDelay - TurnsAfterLoad[BestLoadLoc];
      TransportPlan.TurnsLoaded := TurnsAfterLoad[BestLoadLoc];
      TransportPlan.nLoad := 0;
      for tuix := nSelectedLoad - 1 downto 0 do
        if 1 shl (2 * tuix) and Arrived[BestLoadLoc] = 0 then
        begin
          assert(uixTransportLoad[tuixSelectedLoad[tuix]] = uixSelectedLoad[tuix]);
          TransportPlan.uixLoad[TransportPlan.nLoad] := uixSelectedLoad[tuix];
          uixTransportLoad[tuixSelectedLoad[tuix]] :=
            uixTransportLoad[nTransportLoad - 1];
          Dec(nTransportLoad);
          Inc(TransportPlan.nLoad);
        end;
      Result := True;
      exit;
    end;

    // no loading location for a single of these units -- remove all
    // should be pretty rare case
    for tuix := nSelectedLoad - 1 downto 0 do
    begin
      assert(uixTransportLoad[tuixSelectedLoad[tuix]] = uixSelectedLoad[tuix]);
      uixTransportLoad[tuixSelectedLoad[tuix]] :=
        uixTransportLoad[nTransportLoad - 1];
      Dec(nTransportLoad);
    end;
  end;
  TransportPlan.LoadLoc := -1;
  Result := False;
end;

//------------------------------------------------------------------------------
// Misc

function TToolAI.CurrentMStrength(Domain: integer): integer;
var
  i: integer;
begin
  Result := 0;
  for i := 0 to nUpgrade - 1 do
    with upgrade[Domain, i] do
      if (Preq = preNone) or (Preq >= 0) and
        ((RO.Tech[Preq] >= tsApplicable) or (Preq in FutureTech) and
        (RO.Tech[Preq] >= 0)) then
      begin
        if Preq in FutureTech then
          Inc(Result, RO.Tech[Preq] * Strength)
        else
          Inc(Result, Strength);
      end;
end;

//------------------------------------------------------------------------------

procedure SetAdvancedness;
var
  ad, j, Reduction, AgeThreshold: integer;
  known: array[0..nAdv - 1] of integer;

  procedure MarkPreqs(ad: integer);
  var
    i: integer;
  begin
    if known[ad] = 0 then
    begin
      known[ad] := 1;
      for i := 0 to 2 do
        if AdvPreq[ad, i] >= 0 then
          MarkPreqs(AdvPreq[ad, i]);
    end;
  end;

begin
  FillChar(Advancedness, SizeOf(Advancedness), 0);
  for ad := 0 to nAdv - 1 do
  begin
    FillChar(known, SizeOf(known), 0);
    MarkPreqs(ad);
    for j := 0 to nAdv - 1 do
      if known[j] > 0 then
        Inc(Advancedness[ad]);
  end;
  AgeThreshold := Advancedness[adScience];
  Reduction := Advancedness[adScience] div 3;
  for ad := 0 to nAdv - 5 do
    if Advancedness[ad] >= AgeThreshold then
      Dec(Advancedness[ad], Reduction);
  AgeThreshold := Advancedness[adMassProduction];
  Reduction := (Advancedness[adMassProduction] - Advancedness[adScience]) div 3;
  for ad := 0 to nAdv - 5 do
    if Advancedness[ad] >= AgeThreshold then
      Dec(Advancedness[ad], Reduction);
end;


initialization
  SetAdvancedness;

end.