Ignore:
Timestamp:
Nov 30, 2023, 10:16:14 PM (5 months ago)
Author:
chronos
Message:
  • Modified: Updated high dpi branch from trunk.
  • Modified: Use generics.collections instead of fgl.
  • Modified: Compile with Delphi syntax.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/highdpi/AI/StdAI/ToolAI.pas

    r349 r465  
    1111type
    1212  TGroupTransportPlan = record
    13     LoadLoc, uixTransport, nLoad, TurnsEmpty, TurnsLoaded: integer;
    14     uixLoad: array[0..15] of integer;
     13    LoadLoc, uixTransport, nLoad, TurnsEmpty, TurnsLoaded: Integer;
     14    uixLoad: array[0..15] of Integer;
    1515  end;
    1616
     
    1818  TToolAI = class(TCustomAI)
    1919  protected
    20   {$IFDEF DEBUG}DebugMap: array[0..lxmax * lymax - 1] of integer;{$ENDIF}
    21 
    22     function CenterOfEmpire: integer;
     20  {$IFDEF DEBUG}DebugMap: array[0..lxmax * lymax - 1] of Integer;{$ENDIF}
     21
     22    function CenterOfEmpire: Integer;
    2323    // tile that is in the middle of all own cities
    2424
    25     function CityTaxBalance(cix: integer; const CityReport: TCityReport): integer;
     25    function CityTaxBalance(cix: Integer; const CityReport: TCityReport): Integer;
    2626    // calculates exact difference of income and maintenance cost for a single city
    2727    // positive result = income higher than maintenance
     
    2929    // respects production and food converted to gold
    3030    // CityReport must have been prepared before
    31     procedure SumCities(TaxRate: integer; var TaxSum, ScienceSum: integer);
     31    procedure SumCities(TaxRate: Integer; var TaxSum, ScienceSum: Integer);
    3232    // calculates exact total tax and science income
    3333    // tax is reduced by maintenance (so might be negative)
     
    4646    procedure JobAssignment_Initialize;
    4747    // initialization, must be called first of the JobAssignment functions
    48     procedure JobAssignment_AddJob(Loc, Job, Score: integer);
     48    procedure JobAssignment_AddJob(Loc, Job, Score: Integer);
    4949    // add job for settlers with certain score
    5050    // jobs include founding cities!
    51     procedure JobAssignment_AddUnit(uix: integer);
     51    procedure JobAssignment_AddUnit(uix: Integer);
    5252    // add a settler unit to do jobs
    5353    procedure JobAssignment_Go;
     
    5757    // starting a job one turn earlier counts the same as 4 points of score
    5858    // function does not cancel jobs that are already started
    59     function JobAssignment_GotJob(uix: integer): boolean;
     59    function JobAssignment_GotJob(uix: Integer): Boolean;
    6060    // can be called after JobAssignment_Go to find out whether
    6161    // a certain settler has been assigned a job to
     
    6464    // calculates formations and districts
    6565
    66     function CheckStep(MoveStyle, TimeBeforeStep, CrossCorner: integer;
    67       var TimeAfterStep, RecoverTurns: integer; FromTile, ToTile: integer;
    68       IsCapture: boolean): integer;
     66    function CheckStep(MoveStyle, TimeBeforeStep, CrossCorner: Integer;
     67      var TimeAfterStep, RecoverTurns: Integer; FromTile, ToTile: Integer;
     68      IsCapture: Boolean): Integer;
    6969    // forecast single unit move between adjacent tiles
    7070    // format of TimeBeforeStep and TimeAfterStep: $1000*number of turns + $800-MP left
     
    7474    // CrossCorner=1 for long moves that cross the tile corner, =0 for short ones that don't
    7575
    76     function GetMyMoveStyle(mix, Health: integer): integer;
    77 
    78     function Unit_MoveEx(uix, ToLoc: integer; Options: integer = 0): integer;
     76    function GetMyMoveStyle(mix, Health: Integer): Integer;
     77
     78    function Unit_MoveEx(uix, ToLoc: Integer; Options: Integer = 0): Integer;
    7979
    8080    procedure SeaTransport_BeginInitialize;
     
    9090    // - all transports have same capacity
    9191    // - no transport is damaged
    92     procedure SeaTransport_AddLoad(uix: integer);
    93     procedure SeaTransport_AddTransport(uix: integer);
    94     procedure SeaTransport_AddDestination(Loc: integer);
    95     function SeaTransport_MakeGroupPlan(var TransportPlan: TGroupTransportPlan): boolean;
     92    procedure SeaTransport_AddLoad(uix: Integer);
     93    procedure SeaTransport_AddTransport(uix: Integer);
     94    procedure SeaTransport_AddDestination(Loc: Integer);
     95    function SeaTransport_MakeGroupPlan(var TransportPlan: TGroupTransportPlan): Boolean;
    9696    // make plan for group of units to transport from a single loading location by a single transport
    9797    // the plan optimizes:
     
    103103    // function returns false if no more transports are possible
    104104
    105     function CurrentMStrength(Domain: integer): integer;
    106   end;
    107 
     105    function CurrentMStrength(Domain: Integer): Integer;
     106  end;
    108107
    109108const
     
    132131  mxAdjacent = $00000001;
    133132
    134 
    135 var
    136   nContinent, nOcean, nDistrict: integer;
    137   Formation: array[0..lxmax * lymax - 1] of integer;
     133var
     134  nContinent, nOcean, nDistrict: Integer;
     135  Formation: array[0..lxmax * lymax - 1] of Integer;
    138136  // water: ocean index, land: continent index, sorted by size
    139137  // territory unpassable due to peace treaty divides a continent
    140   District: array[0..lxmax * lymax - 1] of integer;
     138  District: array[0..lxmax * lymax - 1] of Integer;
    141139  // index of coherent own territory, sorted by size
    142   CityResult: array[0..nCmax - 1] of integer;
    143 
    144   Advancedness: array[0..nAdv - 1] of integer;
     140  CityResult: array[0..nCmax - 1] of Integer;
     141
     142  Advancedness: array[0..nAdv - 1] of Integer;
    145143// total number of prerequisites for each advance
    146 
    147144
    148145implementation
     
    152149
    153150type
    154   pinteger = ^integer;
     151  pinteger = ^Integer;
    155152
    156153var
    157154  // for JobAssignment
    158   MaxScore: integer;
    159   TileJob, TileJobScore: array[0..lxmax * lymax - 1] of byte;
    160   JobLocOfSettler: array[0..nUmax - 1] of integer; // ToAssign = find job
     155  MaxScore: Integer;
     156  TileJob, TileJobScore: array[0..lxmax * lymax - 1] of Byte;
     157  JobLocOfSettler: array[0..nUmax - 1] of Integer; // ToAssign = find job
    161158
    162159  // for Transport
    163   TransportMoveStyle, TransportCapacity, nTransportLoad: integer;
    164   InitComplete, HaveDestinations: boolean;
    165   uixTransportLoad, TransportAvailable: array[0..nUmax - 1] of integer;
    166   TurnsAfterLoad: array[0..lxmax * lymax - 1] of shortint;
    167 
    168 
    169 procedure ReplaceD(Start, Stop: pinteger; Raider, Twix: integer);
     160  TransportMoveStyle, TransportCapacity, nTransportLoad: Integer;
     161  InitComplete, HaveDestinations: Boolean;
     162  uixTransportLoad, TransportAvailable: array[0..nUmax - 1] of Integer;
     163  TurnsAfterLoad: array[0..lxmax * lymax - 1] of ShortInt;
     164
     165procedure ReplaceD(Start, Stop: pinteger; Raider, Twix: Integer);
    170166begin
    171167  while Start <> Stop do
     
    177173end;
    178174
    179 function NextZero(Start, Stop: pinteger; Mask: cardinal): pinteger;
     175function NextZero(Start, Stop: pinteger; Mask: Cardinal): pinteger;
    180176begin
    181177  while (Start <> Stop) and (Start^ and Mask <> 0) do
     
    184180end;
    185181
    186 
    187 function TToolAI.CenterOfEmpire: integer;
    188 var
    189   cix, Loc, x, y, sy, n: integer;
    190   a, su, sv: double;
    191 begin
    192   n := 0;
     182function TToolAI.CenterOfEmpire: Integer;
     183var
     184  cix, Loc, X, Y, sy, N: Integer;
     185  A, su, sv: Double;
     186begin
     187  N := 0;
    193188  sy := 0;
    194189  su := 0;
     
    199194    if Loc >= 0 then
    200195    begin
    201       y := Loc div G.lx;
    202       x := Loc - y * G.lx;
    203       Inc(sy, y);
    204       a := 2 * pi * x / G.lx;
    205       su := su + cos(a);
    206       sv := sv + sin(a);
    207       Inc(n);
    208     end;
    209   end;
    210   a := arctan2(sv, su);
    211   x := round(G.lx * a / (2 * pi));
    212   while x >= G.lx do
    213     Dec(x, G.lx);
    214   while x < 0 do
    215     Inc(x, G.lx);
    216   Result := ((2 * sy + n) div (2 * n)) * G.lx + x;
    217 end;
    218 
    219 function TToolAI.CityTaxBalance(cix: integer; const CityReport: TCityReport): integer;
    220 var
    221   i: integer;
     196      Y := Loc div G.lx;
     197      X := Loc - Y * G.lx;
     198      Inc(sy, Y);
     199      A := 2 * pi * X / G.lx;
     200      su := su + cos(A);
     201      sv := sv + sin(A);
     202      Inc(N);
     203    end;
     204  end;
     205  A := arctan2(sv, su);
     206  X := round(G.lx * A / (2 * pi));
     207  while X >= G.lx do
     208    Dec(X, G.lx);
     209  while X < 0 do
     210    Inc(X, G.lx);
     211  Result := ((2 * sy + N) div (2 * N)) * G.lx + X;
     212end;
     213
     214function TToolAI.CityTaxBalance(cix: Integer; const CityReport: TCityReport): Integer;
     215var
     216  I: Integer;
    222217begin
    223218  Result := 0;
     
    234229      Inc(Result, CityReport.FoodRep - CityReport.Eaten);
    235230  end;
    236   for i := nWonder to nImp - 1 do
    237     if MyCity[cix].Built[i] > 0 then
    238       Dec(Result, Imp[i].Maint);
    239 end;
    240 
    241 procedure TToolAI.SumCities(TaxRate: integer; var TaxSum, ScienceSum: integer);
    242 var
    243   cix, p1: integer;
     231  for I := nWonder to nImp - 1 do
     232    if MyCity[cix].Built[I] > 0 then
     233      Dec(Result, Imp[I].Maint);
     234end;
     235
     236procedure TToolAI.SumCities(TaxRate: Integer; var TaxSum, ScienceSum: Integer);
     237var
     238  cix, p1: Integer;
    244239  CityReport: TCityReport;
    245240begin
     
    247242  ScienceSum := 0;
    248243  if RO.Government = gAnarchy then
    249     exit;
     244    Exit;
    250245  for p1 := 0 to nPl - 1 do
    251246    if RO.Tribute[p1] <= RO.TributePaid[p1] then
     
    263258end;
    264259
    265 
    266260//------------------------------------------------------------------------------
    267261// City Tiles Processing
     
    274268procedure TToolAI.OptimizeCityTiles;
    275269var
    276   cix: integer;
     270  cix: Integer;
    277271begin
    278272  for cix := 0 to RO.nCity - 1 do
     
    284278procedure TToolAI.GetCityProdPotential;
    285279var
    286   cix: integer;
     280  cix: Integer;
    287281  Advice: TCityTileAdviceData;
    288282begin
     
    292286      begin
    293287        Advice.ResourceWeights := rwMaxProd;
    294         Server(sGetCityTileAdvice, me, cix, Advice);
     288        Server(sGetCityTileAdvice, Me, cix, Advice);
    295289        CityResult[cix] := Advice.CityReport.ProdRep; // considers factory, but shouldn't
    296290      end;
     
    299293procedure TToolAI.GetCityTradePotential;
    300294var
    301   cix: integer;
     295  cix: Integer;
    302296  Advice: TCityTileAdviceData;
    303297begin
     
    307301      begin
    308302        Advice.ResourceWeights := rwMaxScience;
    309         Server(sGetCityTileAdvice, me, cix, Advice);
     303        Server(sGetCityTileAdvice, Me, cix, Advice);
    310304        CityResult[cix] := Advice.CityReport.Trade;
    311305      end;
    312306end;
    313 
    314307
    315308//------------------------------------------------------------------------------
     
    321314procedure TToolAI.JobAssignment_Initialize;
    322315begin
    323   fillchar(JobLocOfSettler, RO.nUn * sizeof(integer), $FF); // -1
    324   fillchar(TileJob, MapSize, jNone);
    325   fillchar(TileJobScore, MapSize, 0);
     316  FillChar(JobLocOfSettler, RO.nUn * SizeOf(Integer), $FF); // -1
     317  FillChar(TileJob, MapSize, jNone);
     318  FillChar(TileJobScore, MapSize, 0);
    326319  MaxScore := 0;
    327320end;
    328321
    329 procedure TToolAI.JobAssignment_AddJob(Loc, Job, Score: integer);
     322procedure TToolAI.JobAssignment_AddJob(Loc, Job, Score: Integer);
    330323begin
    331324  if Score > 255 then
     
    340333end;
    341334
    342 procedure TToolAI.JobAssignment_AddUnit(uix: integer);
    343 begin
    344   assert(MyModel[MyUnit[uix].mix].Kind in [mkSettler, mkSlaves]);
     335procedure TToolAI.JobAssignment_AddUnit(uix: Integer);
     336begin
     337  Assert(MyModel[MyUnit[uix].mix].Kind in [mkSettler, mkSlaves]);
    345338  JobLocOfSettler[uix] := ToAssign;
    346339end;
    347340
    348 function TToolAI.JobAssignment_GotJob(uix: integer): boolean;
     341function TToolAI.JobAssignment_GotJob(uix: Integer): Boolean;
    349342begin
    350343  Result := JobLocOfSettler[uix] >= 0;
     
    354347const
    355348  DistanceScore = 4;
    356   StepSizeByTerrain: array[0..11] of integer =
     349  StepSizeByTerrain: array[0..11] of Integer =
    357350    (0, 0, 1, 2, 1, 1, 0, 1, 0, 1, 1, 2);
    358351  //Oc-Sh-Gr-De-Pr-Tu-Ar-Sw-XX-Fo-Hi-Mo
    359352var
    360353  uix, BestScore, BestCount, BestLoc, BestJob, BestDistance, TestLoc,
    361   NextLoc, TestDistance, V8, TestScore, StepSize, MoveResult: integer;
    362   UnitsToAssign: boolean;
     354  NextLoc, TestDistance, V8, TestScore, StepSize, MoveResult: Integer;
     355  UnitsToAssign: Boolean;
    363356  Adjacent: TVicinity8Loc;
    364357  SettlerOfJobLoc, DistToLoc: array[0..lxmax * lymax - 1] of smallint;
    365358  // DistToLoc is only defined where SettlerOfJobLoc>=0
    366   TileChecked: array[0..lxmax * lymax - 1] of boolean;
    367 begin
    368   fillchar(SettlerOfJobLoc, MapSize * 2, $FF); // -1
     359  TileChecked: array[0..lxmax * lymax - 1] of Boolean;
     360begin
     361  FillChar(SettlerOfJobLoc, MapSize * 2, $FF); // -1
    369362
    370363  // keep up jobs that are already started
     
    387380        BestJob := jNone;
    388381        BestScore := -999999;
    389         FillChar(TileChecked, MapSize * sizeof(boolean), False);
     382        FillChar(TileChecked, MapSize * SizeOf(Boolean), False);
    390383        Pile.Create(MapSize);
    391384        Pile.Put(MyUnit[uix].Loc, 0); // start search for new job at current location
     
    406399                  and (Map[NextLoc] and (fUnit or fOwned) <> fUnit) // no foreign unit
    407400                  and ((RO.Territory[NextLoc] < 0) or
    408                   (RO.Territory[NextLoc] = me)) // no foreign territory
     401                  (RO.Territory[NextLoc] = Me)) // no foreign territory
    409402                  and (Map[TestLoc] and Map[NextLoc] and fInEnemyZoC = 0) then
    410403                  // move not prevented by ZoC
     
    421414            ((SettlerOfJobLoc[TestLoc] < 0) or (DistToLoc[TestLoc] > TestDistance)) then
    422415          begin
    423             TestScore := integer(TileJobScore[TestLoc]) - DistanceScore * TestDistance;
     416            TestScore := Integer(TileJobScore[TestLoc]) - DistanceScore * TestDistance;
    424417            if TestScore > BestScore then
    425418              BestCount := 0;
     
    469462          Unit_StartJob(uix, TileJob[JobLocOfSettler[uix]]);
    470463      end;
    471 end; // JobAssignment_Go
    472 
     464end;
    473465
    474466//------------------------------------------------------------------------------
     
    477469procedure TToolAI.AnalyzeMap;
    478470var
    479   i, j, Loc, Loc1, V8, Count, Kind, MostIndex: integer;
     471  I, J, Loc, Loc1, V8, Count, Kind, MostIndex: Integer;
    480472  Adjacent: TVicinity8Loc;
    481473  IndexOfID: array[0..lxmax * lymax - 1] of smallint;
    482474  IDOfIndex: array[0..lxmax * lymax div 2 - 1] of smallint;
    483475begin
    484   fillchar(District, MapSize * 4, $FF);
     476  FillChar(District, MapSize * 4, $FF);
    485477  for Loc := 0 to MapSize - 1 do
    486478    if Map[Loc] and fTerrain = fUNKNOWN then
     
    508500              Formation[Loc], Formation[Loc1]);
    509501      end;
    510       if (RO.Territory[Loc] = me) and (Map[Loc] and fTerrain >= fGrass) then
     502      if (RO.Territory[Loc] = Me) and (Map[Loc] and fTerrain >= fGrass) then
    511503      begin
    512504        District[Loc] := Loc;
     
    553545        Inc(Count);
    554546      end;
    555     for i := 0 to Count - 2 do
    556     begin
    557       MostIndex := i;
    558       for j := i + 1 to Count - 1 do
    559         if IndexOfID[IDOfIndex[j]] > IndexOfID[IDOfIndex[MostIndex]] then
    560           MostIndex := j;
    561       if MostIndex <> i then
    562       begin
    563         j := IDOfIndex[i];
    564         IDOfIndex[i] := IDOfIndex[MostIndex];
    565         IDOfIndex[MostIndex] := j;
    566       end;
    567     end;
    568     for i := 0 to Count - 1 do
    569       IndexOfID[IDOfIndex[i]] := i;
     547    for I := 0 to Count - 2 do
     548    begin
     549      MostIndex := I;
     550      for J := I + 1 to Count - 1 do
     551        if IndexOfID[IDOfIndex[J]] > IndexOfID[IDOfIndex[MostIndex]] then
     552          MostIndex := J;
     553      if MostIndex <> I then
     554      begin
     555        J := IDOfIndex[I];
     556        IDOfIndex[I] := IDOfIndex[MostIndex];
     557        IDOfIndex[MostIndex] := J;
     558      end;
     559    end;
     560    for I := 0 to Count - 1 do
     561      IndexOfID[IDOfIndex[I]] := I;
    570562
    571563    case Kind of
     
    594586  end;
    595587end;
    596 
    597588
    598589//------------------------------------------------------------------------------
     
    614605// other:  |   Basic   | 0| Speed  |              X X X             | MaxTerrType  |
    615606
    616 function TToolAI.GetMyMoveStyle(mix, Health: integer): integer;
     607function TToolAI.GetMyMoveStyle(mix, Health: Integer): Integer;
    617608begin
    618609  with MyModel[mix] do
     
    623614      begin
    624615        Inc(Result, (50 + (Speed - 150) * 13 shr 7) shl 8); //HeavyCost
    625         if RO.Wonder[woShinkansen].EffectiveOwner <> me then
     616        if RO.Wonder[woShinkansen].EffectiveOwner <> Me then
    626617          Inc(Result, Speed * (4 * 1311) shr 17); // RailCost
    627         if (RO.Wonder[woGardens].EffectiveOwner <> me) or
     618        if (RO.Wonder[woGardens].EffectiveOwner <> Me) or
    628619          (Kind = mkSettler) and (Speed >= 200) then
    629620          Inc(Result, msHostile);
     
    640631      begin
    641632        Result := Speed;
    642         if RO.Wonder[woMagellan].EffectiveOwner = me then
     633        if RO.Wonder[woMagellan].EffectiveOwner = Me then
    643634          Inc(Result, 200);
    644635        if Health < 100 then
     
    655646end;
    656647
    657 function TToolAI.CheckStep(MoveStyle, TimeBeforeStep, CrossCorner: integer;
    658   var TimeAfterStep, RecoverTurns: integer; FromTile, ToTile: integer;
    659   IsCapture: boolean): integer;
    660 var
    661   MoveCost, RecoverCost: integer;
     648function TToolAI.CheckStep(MoveStyle, TimeBeforeStep, CrossCorner: Integer;
     649  var TimeAfterStep, RecoverTurns: Integer; FromTile, ToTile: Integer;
     650  IsCapture: Boolean): Integer;
     651var
     652  MoveCost, RecoverCost: Integer;
    662653begin
    663654  //IsCapture:=true;
    664   assert(((FromTile and fTerrain <= fMountains) or (FromTile and
     655  Assert(((FromTile and fTerrain <= fMountains) or (FromTile and
    665656    fTerrain = fUNKNOWN)) and ((ToTile and fTerrain <= fMountains) or
    666657    (ToTile and fTerrain = fUNKNOWN)));
     
    710701                if ToTile and fPeace <> 0 then
    711702                  Result := csCheckTerritory;
    712                 exit;
     703                Exit;
    713704              end;
    714705            end;
     
    829820          begin
    830821            Result := csForbiddenTile;
    831             exit;
     822            Exit;
    832823          end;
    833824        end
     
    878869              // must wait for next turn
    879870              Result := csOk;
    880               exit;
     871              Exit;
    881872            end;
    882873          end;
     
    898889      Result := csForbiddenTile;
    899890  end;
    900 end; // CheckStep
     891end;
    901892
    902893(*
    903894-------- Pathfinding Reference Implementation --------
    904895var
    905 MoveStyle,V8,Loc,Time,NextLoc,NextTime,RecoverTurns: integer;
     896MoveStyle,V8,Loc,Time,NextLoc,NextTime,RecoverTurns: Integer;
    906897Adjacent: TVicinity8Loc;
    907 Reached: array[0..lxmax*lymax-1] of boolean;
    908 begin
    909 fillchar(Reached, MapSize, false);
     898Reached: array[0..lxmax*lymax-1] of Boolean;
     899begin
     900FillChar(Reached, MapSize, False);
    910901MoveStyle:=GetMyMoveStyle(MyUnit[uix].mix, MyUnit[uix].Health);
    911902Pile.Create(MapSize);
     
    915906  // todo: check exit condition, e.g. whether destination reached
    916907
    917   Reached[Loc]:=true;
     908  Reached[Loc]:=True;
    918909  V8_to_Loc(Loc, Adjacent);
    919910  for V8:=0 to 7 do
     
    925916          Pile.Put(NextLoc, NextTime+RecoverTurns*$1000);
    926917        csForbiddenTile:
    927           Reached[NextLoc]:=true; // don't check moving there again
     918          Reached[NextLoc]:=True; // don't check moving there again
    928919        csCheckTerritory:
    929920          if RO.Territory[NextLoc]=RO.Territory[Loc] then
     
    936927*)
    937928
    938 function TToolAI.Unit_MoveEx(uix, ToLoc: integer; Options: integer): integer;
     929function TToolAI.Unit_MoveEx(uix, ToLoc: Integer; Options: Integer): Integer;
    939930var
    940931  Loc, NextLoc, Temp, FromLoc, EndLoc, Time, V8, MoveResult, RecoverTurns,
    941   NextTime, MoveStyle: integer;
     932  NextTime, MoveStyle: Integer;
    942933  Adjacent: TVicinity8Loc;
    943   PreLoc: array[0..lxmax * lymax - 1] of integer;
    944   Reached: array[0..lxmax * lymax - 1] of boolean;
     934  PreLoc: array[0..lxmax * lymax - 1] of Integer;
     935  Reached: array[0..lxmax * lymax - 1] of Boolean;
    945936begin
    946937  Result := eOk;
    947938  FromLoc := MyUnit[uix].Loc;
    948939  if FromLoc = ToLoc then
    949     exit;
     940    Exit;
    950941
    951942  FillChar(Reached, MapSize, False);
     
    10121003      begin
    10131004        Result := MoveResult;
    1014         break;
     1005        Break;
    10151006      end;
    10161007    end;
     
    10201011end;
    10211012
    1022 
    10231013//------------------------------------------------------------------------------
    10241014// Oversea Transport
     
    10261016procedure TToolAI.SeaTransport_BeginInitialize;
    10271017begin
    1028   fillchar(TransportAvailable, RO.nUn * sizeof(integer), $FF); // -1
     1018  FillChar(TransportAvailable, RO.nUn * SizeOf(Integer), $FF); // -1
    10291019  InitComplete := False;
    10301020  HaveDestinations := False;
     
    10351025end;
    10361026
    1037 procedure TToolAI.SeaTransport_AddLoad(uix: integer);
    1038 var
    1039   i: integer;
    1040 begin
    1041   assert(not InitComplete); // call order violation!
     1027procedure TToolAI.SeaTransport_AddLoad(uix: Integer);
     1028var
     1029  I: Integer;
     1030begin
     1031  Assert(not InitComplete); // call order violation!
    10421032  if Map[MyUnit[uix].Loc] and fTerrain < fGrass then
    1043     exit;
    1044   for i := 0 to nTransportLoad - 1 do
    1045     if uix = uixTransportLoad[i] then
    1046       exit;
     1033    Exit;
     1034  for I := 0 to nTransportLoad - 1 do
     1035    if uix = uixTransportLoad[I] then
     1036      Exit;
    10471037  uixTransportLoad[nTransportLoad] := uix;
    10481038  Inc(nTransportLoad);
    10491039end;
    10501040
    1051 procedure TToolAI.SeaTransport_AddTransport(uix: integer);
    1052 var
    1053   MoveStyle: integer;
    1054 begin
    1055   assert(not InitComplete); // call order violation!
    1056   assert(MyModel[MyUnit[uix].mix].Cap[mcSeaTrans] > 0);
     1041procedure TToolAI.SeaTransport_AddTransport(uix: Integer);
     1042var
     1043  MoveStyle: Integer;
     1044begin
     1045  Assert(not InitComplete); // call order violation!
     1046  Assert(MyModel[MyUnit[uix].mix].Cap[mcSeaTrans] > 0);
    10571047  TransportAvailable[uix] := 1;
    10581048  with MyModel[MyUnit[uix].mix] do
     
    10681058end;
    10691059
    1070 procedure TToolAI.SeaTransport_AddDestination(Loc: integer);
    1071 begin
    1072   assert(not InitComplete); // call order violation!
     1060procedure TToolAI.SeaTransport_AddDestination(Loc: Integer);
     1061begin
     1062  Assert(not InitComplete); // call order violation!
    10731063  Pile.Put(Loc, $800);
    10741064  HaveDestinations := True;
     
    10771067procedure TToolAI.SeaTransport_EndInitialize;
    10781068var
    1079   Loc0, Time0, V8, Loc1, ArriveTime, RecoverTurns: integer;
     1069  Loc0, Time0, V8, Loc1, ArriveTime, RecoverTurns: Integer;
    10801070  Adjacent: TVicinity8Loc;
    10811071begin
    1082   assert(not InitComplete); // call order violation!
     1072  Assert(not InitComplete); // call order violation!
    10831073  InitComplete := True;
    10841074  if HaveDestinations then
    10851075  begin // calculate TurnsAfterLoad from destination locs
    1086     fillchar(TurnsAfterLoad, MapSize, $FF); // -1
     1076    FillChar(TurnsAfterLoad, MapSize, $FF); // -1
    10871077    while Pile.Get(Loc0, Time0) do
    10881078    begin // search backward
     
    11091099end;
    11101100
    1111 
    11121101function TToolAI.SeaTransport_MakeGroupPlan(
    1113   var TransportPlan: TGroupTransportPlan): boolean;
    1114 var
    1115   V8, i, j, iPicked, uix, Loc0, Time0, Loc1, RecoverTurns, MoveStyle,
     1102  var TransportPlan: TGroupTransportPlan): Boolean;
     1103var
     1104  V8, I, J, iPicked, uix, Loc0, Time0, Loc1, RecoverTurns, MoveStyle,
    11161105  TurnsLoaded, TurnCount, tuix, tuix1, ArriveTime, TotalDelay,
    11171106  BestTotalDelay, GroupCount, BestGroupCount, BestLoadLoc, FullMovementLoc,
    1118   nSelectedLoad, f, OriginContinent, a, b: integer;
    1119   CompleteFlag, NotReachedFlag, ContinueUnit: cardinal;
    1120   IsComplete, ok, IsFirstLoc: boolean;
     1107  nSelectedLoad, F, OriginContinent, A, B: Integer;
     1108  CompleteFlag, NotReachedFlag, ContinueUnit: Cardinal;
     1109  IsComplete, ok, IsFirstLoc: Boolean;
    11211110  StartLocPtr, ArrivedEnd: pinteger;
    11221111  Adjacent: TVicinity8Loc;
    1123   uixSelectedLoad: array[0..15] of integer;
    1124   tuixSelectedLoad: array[0..15] of integer;
    1125   Arrived: array[0..lxmax * lymax] of cardinal;
     1112  uixSelectedLoad: array[0..15] of Integer;
     1113  tuixSelectedLoad: array[0..15] of Integer;
     1114  Arrived: array[0..lxmax * lymax] of Cardinal;
    11261115  ResponsibleTransport: array[0..lxmax * lymax - 1] of smallint;
    1127   TurnsBeforeLoad: array[0..lxmax * lymax - 1] of shortint;
    1128   GroupComplete: array[0..lxmax * lymax - 1] of boolean;
    1129 begin
    1130   assert(InitComplete); // call order violation!
     1116  TurnsBeforeLoad: array[0..lxmax * lymax - 1] of ShortInt;
     1117  GroupComplete: array[0..lxmax * lymax - 1] of Boolean;
     1118begin
     1119  Assert(InitComplete); // call order violation!
    11311120
    11321121  if HaveDestinations and (nTransportLoad > 0) then
     
    11391128        for tuix := 0 to nTransportLoad - 1 do
    11401129        begin
    1141           Loc_to_ab(MyUnit[uix].Loc, MyUnit[uixTransportLoad[tuix]].Loc, a, b);
    1142           if (abs(a) <= 1) and (abs(b) <= 1) then
     1130          Loc_to_ab(MyUnit[uix].Loc, MyUnit[uixTransportLoad[tuix]].Loc, A, B);
     1131          if (abs(A) <= 1) and (abs(B) <= 1) then
    11431132          begin
    1144             assert((a <> 0) or (b <> 0));
     1133            Assert((A <> 0) or (B <> 0));
    11451134            Inc(GroupCount);
    11461135          end;
     
    11561145          for tuix := nTransportLoad - 1 downto 0 do
    11571146          begin
    1158             Loc_to_ab(TransportPlan.LoadLoc, MyUnit[uixTransportLoad[tuix]].Loc, a, b);
    1159             if (abs(a) <= 1) and (abs(b) <= 1) then
     1147            Loc_to_ab(TransportPlan.LoadLoc, MyUnit[uixTransportLoad[tuix]].Loc, A, B);
     1148            if (abs(A) <= 1) and (abs(B) <= 1) then
    11601149            begin
    11611150              TransportPlan.uixLoad[TransportPlan.nLoad] := uixTransportLoad[tuix];
     
    11641153              Inc(TransportPlan.nLoad);
    11651154              if TransportPlan.nLoad = TransportCapacity then
    1166                 break;
     1155                Break;
    11671156            end;
    11681157          end;
    11691158          Result := True;
    1170           exit;
     1159          Exit;
    11711160        end;
    11721161      end;
     
    11761165  begin
    11771166    // select units from same continent
    1178     fillchar(Arrived, 4 * nContinent, 0); // misuse Arrived as counter
     1167    FillChar(Arrived, 4 * nContinent, 0); // misuse Arrived as counter
    11791168    for tuix := 0 to nTransportLoad - 1 do
    11801169    begin
    1181       assert(Map[MyUnit[uixTransportLoad[tuix]].Loc] and fTerrain >= fGrass);
    1182       f := Formation[MyUnit[uixTransportLoad[tuix]].Loc];
    1183       if f >= 0 then
    1184         Inc(Arrived[f]);
     1170      Assert(Map[MyUnit[uixTransportLoad[tuix]].Loc] and fTerrain >= fGrass);
     1171      F := Formation[MyUnit[uixTransportLoad[tuix]].Loc];
     1172      if F >= 0 then
     1173        Inc(Arrived[F]);
    11851174    end;
    11861175    OriginContinent := 0;
    1187     for f := 1 to nContinent - 1 do
    1188       if Arrived[f] > Arrived[OriginContinent] then
    1189         OriginContinent := f;
     1176    for F := 1 to nContinent - 1 do
     1177      if Arrived[F] > Arrived[OriginContinent] then
     1178        OriginContinent := F;
    11901179    nSelectedLoad := 0;
    11911180    for tuix := 0 to nTransportLoad - 1 do
     
    11961185        Inc(nSelectedLoad);
    11971186        if nSelectedLoad = 16 then
    1198           break;
     1187          Break;
    11991188      end;
    12001189
    12011190    Pile.Create(MapSize);
    1202     fillchar(ResponsibleTransport, MapSize * 2, $FF); // -1
    1203     fillchar(TurnsBeforeLoad, MapSize, $FF); // -1
     1191    FillChar(ResponsibleTransport, MapSize * 2, $FF); // -1
     1192    FillChar(TurnsBeforeLoad, MapSize, $FF); // -1
    12041193    ok := False;
    12051194    for uix := 0 to RO.nUn - 1 do
     
    12141203      Result := False;
    12151204      Pile.Free;
    1216       exit;
     1205      Exit;
    12171206    end;
    12181207    while Pile.Get(Loc0, Time0) do
     
    12351224    end;
    12361225
    1237     fillchar(Arrived, MapSize * 4, $55); // set NotReachedFlag for all tiles
    1238     fillchar(GroupComplete, MapSize, False);
     1226    FillChar(Arrived, MapSize * 4, $55); // set NotReachedFlag for all tiles
     1227    FillChar(GroupComplete, MapSize, False);
    12391228    BestLoadLoc := -1;
    12401229
     
    12431232    begin
    12441233      uix := uixSelectedLoad[tuix];
    1245       if MyUnit[uix].Movement = integer(MyModel[MyUnit[uix].mix].Speed) then
     1234      if MyUnit[uix].Movement = Integer(MyModel[MyUnit[uix].mix].Speed) then
    12461235      begin
    12471236        NotReachedFlag := 1 shl (2 * tuix);
     
    12571246            if (TurnsBeforeLoad[Loc1] >= 0) and (TurnsAfterLoad[Loc1] >= 0) then
    12581247            begin
    1259               i := 1;
     1248              I := 1;
    12601249              GroupCount := 0;
    12611250              for tuix1 := 0 to nSelectedLoad - 1 do
    12621251              begin
    1263                 if Arrived[loc1] and i = 0 then
     1252                if Arrived[loc1] and I = 0 then
    12641253                  Inc(GroupCount);
    1265                 i := i shl 2;
     1254                I := I shl 2;
    12661255              end;
    1267               assert(GroupCount <= TransportCapacity);
     1256              Assert(GroupCount <= TransportCapacity);
    12681257              if (GroupCount = TransportCapacity) or (GroupCount = nSelectedLoad) then
    12691258                GroupComplete[loc1] := True;
     
    13031292          begin
    13041293            Pile.Put(MyUnit[uix].Loc, $1800 - MyUnit[uix].Movement);
    1305             if MyUnit[uix].Movement = integer(MyModel[MyUnit[uix].mix].Speed) then
     1294            if MyUnit[uix].Movement = Integer(MyModel[MyUnit[uix].mix].Speed) then
    13061295              FullMovementLoc := MyUnit[uix].Loc;
    13071296            // surrounding tiles can be loaded immediately
     
    13191308            if StartLocPtr <> ArrivedEnd then
    13201309            begin
    1321               Loc0 := (integer(StartLocPtr) - integer(@Arrived)) shr 2;
     1310              Loc0 := (Integer(StartLocPtr) - Integer(@Arrived)) shr 2;
    13221311              Inc(StartLocPtr);
    13231312              Time0 := $800;
     
    13271316              if IsFirstLoc then
    13281317                ContinueUnit := ContinueUnit and not (1 shl tuix);
    1329               break;
     1318              Break;
    13301319            end;
    13311320            IsFirstLoc := False;
     
    13341323            if not GroupComplete[Loc0] and (Map[Loc0] and fTerrain <> fMountains) then
    13351324            begin // check whether group complete -- no mountains because complete flag might be faked there
    1336               i := 1;
     1325              I := 1;
    13371326              GroupCount := 0;
    13381327              for tuix1 := 0 to nSelectedLoad - 1 do
    13391328              begin
    1340                 if Arrived[Loc0] and i = 0 then
     1329                if Arrived[Loc0] and I = 0 then
    13411330                  Inc(GroupCount);
    1342                 i := i shl 2;
     1331                I := I shl 2;
    13431332              end;
    1344               assert(GroupCount <= TransportCapacity);
     1333              Assert(GroupCount <= TransportCapacity);
    13451334              if (GroupCount = TransportCapacity) or (GroupCount = nSelectedLoad) then
    13461335                GroupComplete[Loc0] := True;
     
    13641353                if (TurnsBeforeLoad[Loc1] >= 0) and (TurnsAfterLoad[Loc1] >= 0) then
    13651354                begin
    1366                   i := 1;
     1355                  I := 1;
    13671356                  GroupCount := 0;
    13681357                  for tuix1 := 0 to nSelectedLoad - 1 do
    13691358                  begin
    1370                     if Arrived[loc1] and i = 0 then
     1359                    if Arrived[loc1] and I = 0 then
    13711360                      Inc(GroupCount);
    1372                     i := i shl 2;
     1361                    I := I shl 2;
    13731362                  end;
    1374                   assert(GroupCount <= TransportCapacity);
     1363                  Assert(GroupCount <= TransportCapacity);
    13751364                  if (GroupCount = TransportCapacity) or
    13761365                    (GroupCount = nSelectedLoad) then
     
    14471436        if 1 shl (2 * tuix) and Arrived[BestLoadLoc] = 0 then
    14481437        begin
    1449           assert(uixTransportLoad[tuixSelectedLoad[tuix]] = uixSelectedLoad[tuix]);
     1438          Assert(uixTransportLoad[tuixSelectedLoad[tuix]] = uixSelectedLoad[tuix]);
    14501439          TransportPlan.uixLoad[TransportPlan.nLoad] := uixSelectedLoad[tuix];
    14511440          uixTransportLoad[tuixSelectedLoad[tuix]] :=
     
    14551444        end;
    14561445      Result := True;
    1457       exit;
     1446      Exit;
    14581447    end;
    14591448
     
    14621451    for tuix := nSelectedLoad - 1 downto 0 do
    14631452    begin
    1464       assert(uixTransportLoad[tuixSelectedLoad[tuix]] = uixSelectedLoad[tuix]);
     1453      Assert(uixTransportLoad[tuixSelectedLoad[tuix]] = uixSelectedLoad[tuix]);
    14651454      uixTransportLoad[tuixSelectedLoad[tuix]] :=
    14661455        uixTransportLoad[nTransportLoad - 1];
     
    14721461end;
    14731462
    1474 
    14751463//------------------------------------------------------------------------------
    14761464// Misc
    14771465
    1478 function TToolAI.CurrentMStrength(Domain: integer): integer;
    1479 var
    1480   i: integer;
     1466function TToolAI.CurrentMStrength(Domain: Integer): Integer;
     1467var
     1468  I: Integer;
    14811469begin
    14821470  Result := 0;
    1483   for i := 0 to nUpgrade - 1 do
    1484     with upgrade[Domain, i] do
     1471  for I := 0 to nUpgrade - 1 do
     1472    with upgrade[Domain, I] do
    14851473      if (Preq = preNone) or (Preq >= 0) and
    14861474        ((RO.Tech[Preq] >= tsApplicable) or (Preq in FutureTech) and
     
    14941482end;
    14951483
    1496 
    14971484//------------------------------------------------------------------------------
    14981485
    14991486procedure SetAdvancedness;
    15001487var
    1501   ad, j, Reduction, AgeThreshold: integer;
    1502   known: array[0..nAdv - 1] of integer;
    1503 
    1504   procedure MarkPreqs(ad: integer);
     1488  ad, J, Reduction, AgeThreshold: Integer;
     1489  known: array[0..nAdv - 1] of Integer;
     1490
     1491  procedure MarkPreqs(ad: Integer);
    15051492  var
    1506     i: integer;
     1493    I: Integer;
    15071494  begin
    15081495    if known[ad] = 0 then
    15091496    begin
    15101497      known[ad] := 1;
    1511       for i := 0 to 2 do
    1512         if AdvPreq[ad, i] >= 0 then
    1513           MarkPreqs(AdvPreq[ad, i]);
     1498      for I := 0 to 2 do
     1499        if AdvPreq[ad, I] >= 0 then
     1500          MarkPreqs(AdvPreq[ad, I]);
    15141501    end;
    15151502  end;
     
    15211508    FillChar(known, SizeOf(known), 0);
    15221509    MarkPreqs(ad);
    1523     for j := 0 to nAdv - 1 do
    1524       if known[j] > 0 then
     1510    for J := 0 to nAdv - 1 do
     1511      if known[J] > 0 then
    15251512        Inc(Advancedness[ad]);
    15261513  end;
Note: See TracChangeset for help on using the changeset viewer.