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/AI.pas

    r349 r465  
    2323
    2424  nResearchOrder = 46;
    25   ResearchOrder: array[0..1, 0..nResearchOrder - 1] of integer =
     25  ResearchOrder: array[0..1, 0..nResearchOrder - 1] of Integer =
    2626    ((adWheel, adWarriorCode, adHorsebackRiding, adCeremonialBurial, adPolytheism,
    2727    adMonarchy, adMysticism, adPoetry, adAstronomy, adMonotheism,
     
    7676
    7777  // mil research
    78   BetterQuality: array[0..nModelCat - 1] of integer = (50, 50, 80, 80);
     78  BetterQuality: array[0..nModelCat - 1] of Integer = (50, 50, 80, 80);
    7979  MaxBuildWorseThanBestModel = 20;
    8080  MaxExistWorseThanBestModel = 50;
     
    8585  nRequestedTechs = 48;
    8686
    87   PlayerHash: array[0..nPl - 1] of integer =
     87  PlayerHash: array[0..nPl - 1] of Integer =
    8888    (7, 6, 0, 2, 10, 8, 12, 14, 4, 1, 3, 5, 9, 11, 13);
    8989
     
    9292
    9393  TPersistentData = record
    94     LastResearchTech, BehaviorFlags, TheologyPartner: integer;
     94    LastResearchTech, BehaviorFlags, TheologyPartner: Integer;
    9595    RejectTurn: array[Suggestion, 0..15] of smallint;
    96     RequestedTechs: array[0..nRequestedTechs - 1] of integer;
     96    RequestedTechs: array[0..nRequestedTechs - 1] of Integer;
    9797    // ad + p shl 8 + Turn shl 16
    9898  end;
    9999
    100100  TAI = class(TBarbarina)
    101     constructor Create(Nation: integer); override;
     101    constructor Create(Nation: Integer); override;
    102102
    103103    procedure SetDataDefaults; override;
     
    106106    Data: ^TPersistentData;
    107107    WarNations, BombardingNations, mixSettlers, mixCaravan, mixTownGuard,
    108     mixSlaves, mixMilitia, mixCruiser, OceanWithShip: integer;
     108    mixSlaves, mixMilitia, mixCruiser, OceanWithShip: Integer;
    109109    NegoCause: (Routine, CheckBarbarina);
    110     SettlerSurplus: array[0..maxCOD - 1] of integer;
    111     uixPatrol: array[0..maxCOD - 1] of integer;
    112 
    113     ContinentPresence: array[0..maxCOD - 1] of integer;
    114     OceanPresence: array[0..maxCOD - 1] of integer;
    115     UnitLack: array[0..maxCOD - 1, mctGroundDefender..mctGroundAttacker] of integer;
    116 
    117     TotalPopulation: array[0..nPl - 1] of integer;
    118     ContinentPopulation: array[0..nPl - 1, 0..maxCOD - 1] of integer;
     110    SettlerSurplus: array[0..maxCOD - 1] of Integer;
     111    uixPatrol: array[0..maxCOD - 1] of Integer;
     112
     113    ContinentPresence: array[0..maxCOD - 1] of Integer;
     114    OceanPresence: array[0..maxCOD - 1] of Integer;
     115    UnitLack: array[0..maxCOD - 1, mctGroundDefender..mctGroundAttacker] of Integer;
     116
     117    TotalPopulation: array[0..nPl - 1] of Integer;
     118    ContinentPopulation: array[0..nPl - 1, 0..maxCOD - 1] of Integer;
    119119    // 1 means enemy territory spotted but no city
    120     DistrictPopulation: array[0..maxCOD - 1] of integer;
    121 
    122     ModelCat: array[0..nMmax - 1] of integer;
    123     ModelQuality: array[0..nMmax - 1] of integer;
    124     ModelBestQuality: array[0..nModelCat - 1] of integer;
    125 
    126     AdvanceValue: array[0..nAdv - 1] of integer;
    127     AdvanceValuesSet: boolean;
     120    DistrictPopulation: array[0..maxCOD - 1] of Integer;
     121
     122    ModelCat: array[0..nMmax - 1] of Integer;
     123    ModelQuality: array[0..nMmax - 1] of Integer;
     124    ModelBestQuality: array[0..nModelCat - 1] of Integer;
     125
     126    AdvanceValue: array[0..nAdv - 1] of Integer;
     127    AdvanceValuesSet: Boolean;
    128128
    129129    procedure DoTurn; override;
    130130    procedure DoNegotiation; override;
    131     function ChooseResearchAdvance: integer; override;
    132     function ChooseStealAdvance: integer; override;
    133     function ChooseGovernment: integer; override;
    134     function WantNegotiation(Nation: integer; NegoTime: TNegoTime): boolean; override;
    135     function OnNegoRejected_CancelTreaty: boolean; override;
    136 
    137     procedure FindBestTrade(Nation: integer; var adWanted, adGiveAway: integer);
     131    function ChooseResearchAdvance: Integer; override;
     132    function ChooseStealAdvance: Integer; override;
     133    function ChooseGovernment: Integer; override;
     134    function WantNegotiation(Nation: Integer; NegoTime: TNegoTime): Boolean; override;
     135    function OnNegoRejected_CancelTreaty: Boolean; override;
     136
     137    procedure FindBestTrade(Nation: Integer; var adWanted, adGiveAway: Integer);
    138138    procedure CheckGender;
    139139    procedure AnalyzeMap;
     
    141141    procedure AttackAndPatrol;
    142142    procedure MoveUnitsHome;
    143     procedure CheckAttack(uix: integer);
    144     procedure Patrol(uix: integer);
     143    procedure CheckAttack(uix: Integer);
     144    procedure Patrol(uix: Integer);
    145145    procedure SetCityProduction;
    146146    procedure SetAdvanceValues;
    147     function HavePort: boolean;
    148   {$IFDEF DEBUG}procedure TraceAdvanceValues(Nation: integer);{$ENDIF}
     147    function HavePort: Boolean;
     148  {$IFDEF DEBUG}procedure TraceAdvanceValues(Nation: Integer);{$ENDIF}
    149149
    150150    // research
    151     procedure RateModel(const mi: TModelInfo; var Category, Quality: integer);
    152     procedure RateMyModel(mix: integer; var Category, Quality: integer);
    153     function IsBetterModel(const mi: TModelInfo): boolean;
     151    procedure RateModel(const mi: TModelInfo; var Category, Quality: Integer);
     152    procedure RateMyModel(mix: Integer; var Category, Quality: Integer);
     153    function IsBetterModel(const mi: TModelInfo): Boolean;
    154154
    155155    //terraforming
    156     procedure TileWorkPlan(Loc, cix: integer; var Value, NextJob, TotalWork: integer);
     156    procedure TileWorkPlan(Loc, cix: Integer; var Value, NextJob, TotalWork: Integer);
    157157    procedure ProcessSettlers;
    158158
    159159    // diplomacy
    160     function MostWanted(Nation, adGiveAway: integer): integer;
     160    function MostWanted(Nation, adGiveAway: Integer): Integer;
    161161
    162162  end;
     
    174174
    175175var
    176   LeaveOutValue: array[0..nAdv - 1] of integer;
    177 
    178 
    179 constructor TAI.Create(Nation: integer);
     176  LeaveOutValue: array[0..nAdv - 1] of Integer;
     177
     178constructor TAI.Create(Nation: Integer);
    180179begin
    181180  inherited;
    182   Data := pointer(RO.Data);
     181  Data := Pointer(RO.Data);
    183182{$IFDEF DEBUG}
    184183  if Nation = 1 then
     
    193192  begin
    194193    LastResearchTech := -1;
    195     if PlayerHash[me] > 7 then
     194    if PlayerHash[Me] > 7 then
    196195      BehaviorFlags := bFemale
    197196    else
     
    199198    DebugMessage(1, 'Gender:=' + char(48 + BehaviorFlags and bGender));
    200199    TheologyPartner := -1;
    201     fillchar(RejectTurn, sizeof(RejectTurn), $FF);
    202     Fillchar(RequestedTechs, sizeof(RequestedTechs), $FF);
     200    FillChar(RejectTurn, SizeOf(RejectTurn), $FF);
     201    Fillchar(RequestedTechs, SizeOf(RequestedTechs), $FF);
    203202  end;
    204203end;
    205204
    206 function TAI.OnNegoRejected_CancelTreaty: boolean;
     205function TAI.OnNegoRejected_CancelTreaty: Boolean;
    207206begin
    208207  Data.RejectTurn[suContact, Opponent] := RO.Turn;
     
    210209end;
    211210
    212 
    213211//-------------------------------
    214212//            RESEARCH
    215213//-------------------------------
    216214
    217 procedure TAI.RateModel(const mi: TModelInfo; var Category, Quality: integer);
     215procedure TAI.RateModel(const mi: TModelInfo; var Category, Quality: Integer);
    218216var
    219   EffectiveTransport: integer;
     217  EffectiveTransport: Integer;
    220218begin
    221219  if mi.Kind >= mkScout then
    222220  begin
    223221    Category := mctNone;
    224     exit;
     222    Exit;
    225223  end;
    226224  case mi.Domain of
     
    294292end;
    295293
    296 procedure TAI.RateMyModel(mix: integer; var Category, Quality: integer);
     294procedure TAI.RateMyModel(mix: Integer; var Category, Quality: Integer);
    297295var
    298296  mi: TModelInfo;
    299297begin
    300   MakeModelInfo(me, mix, MyModel[mix], mi);
     298  MakeModelInfo(Me, mix, MyModel[mix], mi);
    301299  RateModel(mi, Category, Quality);
    302300end;
    303301
    304 function TAI.IsBetterModel(const mi: TModelInfo): boolean;
     302function TAI.IsBetterModel(const mi: TModelInfo): Boolean;
    305303var
    306   mix, Cat, Quality, Cat1, Quality1: integer;
     304  mix, Cat, Quality, Cat1, Quality1: Integer;
    307305begin
    308306  RateModel(mi, Cat, Quality);
     
    314312      begin
    315313        Result := False;
    316         exit;
     314        Exit;
    317315      end;
    318316    end;
     
    320318end;
    321319
    322 function TAI.ChooseResearchAdvance: integer;
     320function TAI.ChooseResearchAdvance: Integer;
    323321var
    324   adNext, iad, i, ad, Count, EarliestNeeded, EarliestNeeded_NoLeaveOut,
    325   NewResearch, StateOfArt, mix: integer;
     322  adNext, iad, I, ad, Count, EarliestNeeded, EarliestNeeded_NoLeaveOut,
     323  NewResearch, StateOfArt, mix: Integer;
    326324  mi: TModelInfo;
    327   Entry: array[0..nAdv - 1] of boolean;
    328   ok: boolean;
    329 
    330   function MarkEntry(ad: integer): boolean;
     325  Entry: array[0..nAdv - 1] of Boolean;
     326  ok: Boolean;
     327
     328  function MarkEntry(ad: Integer): Boolean;
    331329  begin
    332330    if RO.Tech[ad] >= tsApplicable then
     
    375373  end;
    376374
    377   procedure OptimizeDevModel(OptimizeCaps: integer);
     375  procedure OptimizeDevModel(OptimizeCaps: Integer);
    378376  var
    379     f, Cat, OriginalCat, Quality, BestQuality, Best: integer;
     377    F, Cat, OriginalCat, Quality, BestQuality, Best: Integer;
    380378    mi: TModelInfo;
    381379  begin
    382     MakeModelInfo(me, 0, RO.DevModel, mi);
     380    MakeModelInfo(Me, 0, RO.DevModel, mi);
    383381    RateModel(mi, OriginalCat, BestQuality);
    384382    repeat
    385383      Best := -1;
    386       for f := 0 to nFeature - 1 do
    387         if (1 shl f and OptimizeCaps <> 0) and
    388           ((Feature[f].Preq < 0) or IsResearched(Feature[f].Preq)) // check prerequisite
    389           and (RO.DevModel.Weight + Feature[f].Weight <= RO.DevModel.MaxWeight) and
    390           not ((f >= mcFirstNonCap) and (RO.DevModel.Cap[f] > 0)) then
    391         begin
    392           if SetNewModelFeature(f, RO.DevModel.Cap[f] + 1) >= rExecuted then
     384      for F := 0 to nFeature - 1 do
     385        if (1 shl F and OptimizeCaps <> 0) and
     386          ((Feature[F].Preq < 0) or IsResearched(Feature[F].Preq)) // check prerequisite
     387          and (RO.DevModel.Weight + Feature[F].Weight <= RO.DevModel.MaxWeight) and
     388          not ((F >= mcFirstNonCap) and (RO.DevModel.Cap[F] > 0)) then
     389        begin
     390          if SetNewModelFeature(F, RO.DevModel.Cap[F] + 1) >= rExecuted then
    393391          begin
    394             MakeModelInfo(me, 0, RO.DevModel, mi);
     392            MakeModelInfo(Me, 0, RO.DevModel, mi);
    395393            RateModel(mi, Cat, Quality);
    396             assert(Cat = OriginalCat);
     394            Assert(Cat = OriginalCat);
    397395            if Quality > BestQuality then
    398396            begin
    399               Best := f;
     397              Best := F;
    400398              BestQuality := Quality;
    401399            end;
    402             SetNewModelFeature(f, RO.DevModel.Cap[f] - 1);
     400            SetNewModelFeature(F, RO.DevModel.Cap[F] - 1);
    403401          end;
    404402        end;
     
    408406  end;
    409407
    410   function LeaveOutsMissing(ad: integer): boolean;
     408  function LeaveOutsMissing(ad: Integer): Boolean;
    411409  var
    412     i: integer;
     410    I: Integer;
    413411  begin
    414412    Result := False;
     
    424422        Result := True
    425423      else
    426         for i := 0 to 1 do
    427           if AdvPreq[ad, i] >= 0 then
    428             Result := Result or LeaveOutsMissing(AdvPreq[ad, i]);
     424        for I := 0 to 1 do
     425          if AdvPreq[ad, I] >= 0 then
     426            Result := Result or LeaveOutsMissing(AdvPreq[ad, I]);
    429427  end;
    430428
     
    434432    Result := Barbarina_ChooseResearchAdvance;
    435433    if Result >= 0 then
    436       exit;
     434      Exit;
    437435  end;
    438436
     
    446444      Result := ad;
    447445  if Result >= 0 then
    448     exit;
     446    Exit;
    449447
    450448  if Data.BehaviorFlags and bBarbarina = 0 then
     
    462460      OptimizeDevModel(1 shl mcOffense + 1 shl mcDefense + 1 shl
    463461        mcMob + 1 shl mcLongRange + 1 shl mcFanatic);
    464       MakeModelInfo(me, 0, RO.DevModel, mi);
     462      MakeModelInfo(Me, 0, RO.DevModel, mi);
    465463      if IsBetterModel(mi) then
    466464      begin
    467465        Result := adMilitary;
    468         exit;
     466        Exit;
    469467      end;
    470468
     
    473471      SetNewModelFeature(mcOffense, 1);
    474472      OptimizeDevModel(1 shl mcOffense + 1 shl mcDefense + 1 shl mcFanatic);
    475       MakeModelInfo(me, 0, RO.DevModel, mi);
     473      MakeModelInfo(Me, 0, RO.DevModel, mi);
    476474      if IsBetterModel(mi) then
    477475      begin
    478476        Result := adMilitary;
    479         exit;
     477        Exit;
    480478      end;
    481479    end;
     
    493491        SetNewModelFeature(mcWeapons, 0);
    494492        SetNewModelFeature(mcDefense, 3);
    495         exit;
     493        Exit;
    496494      end;
    497495    end;
     
    508506      OptimizeDevModel(1 shl mcDefense+1 shl mcSeaTrans+1 shl mcTurbines
    509507        +1 shl mcAirDef);
    510       MakeModelInfo(me,0,RO.DevModel,mi);
     508      MakeModelInfo(Me,0,RO.DevModel,mi);
    511509      if IsBetterModel(mi) then
    512         begin result:=adMilitary; exit end;
     510        begin Result:=adMilitary; Exit end;
    513511      end;
    514512
     
    521519      OptimizeDevModel(1 shl mcOffense+1 shl mcDefense
    522520        +1 shl mcLongRange+1 shl mcAirDef+1 shl mcRadar);
    523       MakeModelInfo(me,0,RO.DevModel,mi);
     521      MakeModelInfo(Me,0,RO.DevModel,mi);
    524522      if IsBetterModel(mi) then
    525         begin result:=adMilitary; exit end;
     523        begin Result:=adMilitary; Exit end;
    526524      end
    527525    end;
     
    547545        begin
    548546          ok := True;
    549           break;
     547          Break;
    550548        end;
    551549      if not ok then
     
    590588        begin // 2 of 3 required
    591589          Count := 0;
    592           for i := 0 to 2 do
    593             if RO.Tech[AdvPreq[ad, i]] >= tsApplicable then
     590          for I := 0 to 2 do
     591            if RO.Tech[AdvPreq[ad, I]] >= tsApplicable then
    594592              Inc(Count);
    595593          if Count >= 2 then
    596594          begin
    597595            Result := ad;
    598             exit;
     596            Exit;
    599597          end;
    600598        end
     
    604602        begin
    605603          Result := ad;
    606           exit;
     604          Exit;
    607605        end;
    608606      end;
     
    620618    begin // go for future techs
    621619      Result := -1;
    622       i := 0;
     620      I := 0;
    623621      for ad := nAdv - 4 to nAdv - 1 do
    624622        if (RO.Tech[ad] < MaxFutureTech) and (RO.Tech[AdvPreq[ad, 0]] >=
    625623          tsApplicable) then
    626624        begin
    627           Inc(i);
    628           if random(i) = 0 then
     625          Inc(I);
     626          if random(I) = 0 then
    629627            Result := ad;
    630628        end;
    631       assert((Result < 0) or AdvanceResearchable(Result));
    632       exit;
    633     end;
    634 
    635   assert(NewResearch >= 0);
    636   fillchar(Entry, sizeof(Entry), False);
     629      Assert((Result < 0) or AdvanceResearchable(Result));
     630      Exit;
     631    end;
     632
     633  Assert(NewResearch >= 0);
     634  FillChar(Entry, SizeOf(Entry), False);
    637635  MarkEntry(NewResearch);
    638636  Result := -1;
     
    640638    if Entry[ad] and ((Result < 0) or (Advancedness[ad] > Advancedness[Result])) then
    641639      Result := ad;
    642   assert(Result >= 0);
     640  Assert(Result >= 0);
    643641end;
    644642
    645 function TAI.ChooseStealAdvance: integer;
     643function TAI.ChooseStealAdvance: Integer;
    646644var
    647   ad: integer;
     645  ad: Integer;
    648646begin
    649647  Result := -1;
     
    654652end;
    655653
    656 
    657654//-------------------------------
    658655//         TERRAFORMING
     
    662659  twpAllowFarmland = $0001;
    663660
    664 procedure TAI.TileWorkPlan(Loc, cix: integer; var Value, NextJob, TotalWork: integer);
     661procedure TAI.TileWorkPlan(Loc, cix: Integer; var Value, NextJob, TotalWork: Integer);
    665662var
    666   OldTile, TerrType: cardinal;
     663  OldTile, TerrType: Cardinal;
    667664  TileInfo: TTileInfo;
    668665begin
     
    672669  begin
    673670    Value := 3 * 8 - 1;
    674     exit;
     671    Exit;
    675672  end; // better than any tile with 2 food
    676673
     
    693690      Map[Loc] := Map[Loc] and not fTerrain or fGrass;
    694691      TerrType := fGrass;
    695       Map[Loc] := Map[Loc] or cardinal(SpecialTile(Loc, TerrType, G.lx) shl 5);
     692      Map[Loc] := Map[Loc] or Cardinal(SpecialTile(Loc, TerrType, G.lx) shl 5);
    696693    end
    697694    else if IsResearched(adExplosives) and
     
    704701      Map[Loc] := Map[Loc] and not fTerrain or fGrass;
    705702      TerrType := fGrass;
    706       Map[Loc] := Map[Loc] or cardinal(SpecialTile(Loc, TerrType, G.lx) shl 5);
     703      Map[Loc] := Map[Loc] or Cardinal(SpecialTile(Loc, TerrType, G.lx) shl 5);
    707704    end;
    708705    if (Terrain[TerrType].MineEff > 0) and (RO.Government <> gDespotism) then
     
    767764    end;
    768765  end;
    769   Server(sGetTileInfo, me, Loc, TileInfo);
     766  Server(sGetTileInfo, Me, Loc, TileInfo);
    770767  Value := TileInfo.Food * 8 + TileInfo.Prod * 2 + TileInfo.Trade;
    771768  Map[Loc] := OldTile;
     
    775772procedure TAI.ProcessSettlers;
    776773var
    777   i, uix, cix, ecix, dtr, Loc, RadiusLoc, Special, Food, Prod, Trade,
     774  I, uix, cix, ecix, dtr, Loc, RadiusLoc, Special, Food, Prod, Trade,
    778775  CityFood, Happy, TestScore, BestNearCityScore, BestUnusedValue,
    779   BestUnusedLoc, Value, NextJob, TotalWork, V21, part, Loc1: integer;
    780   Tile: cardinal;
    781   FoodOk, Started: boolean;
     776  BestUnusedLoc, Value, NextJob, TotalWork, V21, part, Loc1: Integer;
     777  Tile: Cardinal;
     778  FoodOk, Started: Boolean;
    782779  Radius: TVicinity21Loc;
    783780  CityAreaInfo: TCityAreaInfo;
    784   TileFood, ResourceScore, CityScore: array[0..lxmax * lymax - 1] of integer;
    785 
    786   procedure AddJob(Loc, Job, Score: integer);
     781  TileFood, ResourceScore, CityScore: array[0..lxmax * lymax - 1] of Integer;
     782
     783  procedure AddJob(Loc, Job, Score: Integer);
    787784  // set Score=1 for low-priority jobs
    788785  begin
     
    792789  end;
    793790
    794   procedure ReserveCityRadius(Loc: integer);
     791  procedure ReserveCityRadius(Loc: Integer);
    795792  var
    796     V21, RadiusLoc: integer;
     793    V21, RadiusLoc: Integer;
    797794    Radius: TVicinity21Loc;
    798795  begin
     
    811808  procedure ScoreRoadConnections;
    812809  var
    813     V8, nFragments, Loc, Loc1, History, RoadScore, a, b, FullyDeveloped,
    814     ConnectMask: integer;
    815     BridgeOk: boolean;
     810    V8, nFragments, Loc, Loc1, History, RoadScore, A, B, FullyDeveloped,
     811    ConnectMask: Integer;
     812    BridgeOk: Boolean;
    816813    Adjacent: TVicinity8Loc;
    817814  begin
     
    824821      if ((1 shl (Map[Loc] and fTerrain)) and (1 shl fOcean or 1 shl
    825822        fShore or 1 shl fDesert or 1 shl fArctic or 1 shl fUNKNOWN) = 0) and
    826         (RO.Territory[Loc] = me) and (Map[Loc] and FullyDeveloped = 0) and
     823        (RO.Territory[Loc] = Me) and (Map[Loc] and FullyDeveloped = 0) and
    827824        (BridgeOk or (Map[Loc] and fRiver = 0)) then
    828825      begin
     
    838835          Loc1 := Adjacent[V8 and 7];
    839836          History := History shl 1;
    840           if (Loc1 >= 0) and (RO.Territory[Loc1] = me) and
     837          if (Loc1 >= 0) and (RO.Territory[Loc1] = Me) and
    841838            (Map[Loc1] and ConnectMask <> 0) then
    842839          begin
     
    854851                  else if History and 4 <> 0 then
    855852                  begin
    856                     V8_to_ab((V8 - 1) and 7, a, b);
    857                     ab_to_Loc(Loc, a shl 1, b shl 1, Loc1);
     853                    V8_to_ab((V8 - 1) and 7, A, B);
     854                    ab_to_Loc(Loc, A shl 1, B shl 1, Loc1);
    858855                    if (Loc1 >= 0) and (Map[Loc1] and ConnectMask <> 0) then
    859856                      Dec(nFragments);
     
    879876
    880877begin
    881   fillchar(SettlerSurplus, sizeof(SettlerSurplus), 0);
     878  FillChar(SettlerSurplus, SizeOf(SettlerSurplus), 0);
    882879  JobAssignment_Initialize;
    883880
    884881  if (Data.BehaviorFlags and bBarbarina = 0) or (RO.nCity < 3) then
    885882  begin
    886     fillchar(TileFood, sizeof(TileFood), 0);
    887     fillchar(ResourceScore, sizeof(ResourceScore), 0);
     883    FillChar(TileFood, SizeOf(TileFood), 0);
     884    FillChar(ResourceScore, SizeOf(ResourceScore), 0);
    888885    for Loc := 0 to MapSize - 1 do
    889886      if Map[Loc] and fTerrain <> fUNKNOWN then
     
    926923
    927924    // rate possible new cities
    928     fillchar(CityScore, MapSize * sizeof(integer), 0);
     925    FillChar(CityScore, MapSize * SizeOf(Integer), 0);
    929926    for Loc := 0 to MapSize - 1 do
    930927    begin
     
    933930        ((RO.Government <> gDespotism) or (Map[Loc] and fSpecial = fSpecial1)) or
    934931        (Map[Loc] and (fTerrain or fSpecial) = fPrairie or fSpecial1));
    935       if FoodOk and ((RO.Territory[Loc] < 0) or (RO.Territory[Loc] = me)) then
     932      if FoodOk and ((RO.Territory[Loc] < 0) or (RO.Territory[Loc] = Me)) then
    936933      begin
    937934        TestScore := 0;
     
    953950        if CityFood >= MinCityFood then // city is worth founding
    954951        begin
    955           TestScore := (72 + 2 * TestScore) shl 8 + ((loc xor me) * 4567) mod 251;
     952          TestScore := (72 + 2 * TestScore) shl 8 + ((loc xor Me) * 4567) mod 251;
    956953          // some unexactness, random but always the same for this tile
    957954          if TestScore > BestNearCityScore then
     
    991988              if not (Map[RadiusLoc] and fTerrain in [fDesert, fArctic]) then
    992989              begin
    993                 assert(RadiusLoc >= 0);
     990                Assert(RadiusLoc >= 0);
    994991                TileWorkPlan(RadiusLoc, cix, Value, NextJob, TotalWork);
    995992                if (NextJob = jRoad) and (Built[imPalace] +
     
    10031000            begin // tile could be exploited
    10041001              RadiusLoc := Radius[V21];
    1005               assert(RadiusLoc >= 0);
     1002              Assert(RadiusLoc >= 0);
    10061003              if not (Map[RadiusLoc] and fTerrain in [fDesert, fArctic]) then
    10071004              begin
     
    10271024  if Data.BehaviorFlags and bBarbarina = 0 then // low priority jobs
    10281025    for Loc := 0 to MapSize - 1 do
    1029       if RO.Territory[Loc] = me then
     1026      if RO.Territory[Loc] = Me then
    10301027      begin
    10311028        Tile := Map[Loc];
     
    10511048  begin
    10521049    for part := 0 to nShipPart - 1 do
    1053       for i := 0 to ColonyShipPlan[part].nLocFoundCity - 1 do
    1054       begin
    1055         Loc := ColonyShipPlan[part].LocFoundCity[i];
     1050      for I := 0 to ColonyShipPlan[part].nLocFoundCity - 1 do
     1051      begin
     1052        Loc := ColonyShipPlan[part].LocFoundCity[I];
    10561053        Started := False;
    10571054        for uix := 0 to RO.nUn - 1 do
     
    10591056          begin
    10601057            Started := True;
    1061             break;
     1058            Break;
    10621059          end;
    10631060        if not Started then
     
    11221119            begin // settlers could be added to this city
    11231120              Happy := BasicHappy;
    1124               for i := 0 to nWonder - 1 do
    1125                 if Built[i] > 0 then
     1121              for I := 0 to nWonder - 1 do
     1122                if Built[I] > 0 then
    11261123                  Inc(Happy);
    11271124              if Built[imTemple] > 0 then
     
    11301127              begin
    11311128                Inc(Happy, 2);
    1132                 if RO.Wonder[woBach].EffectiveOwner = me then
     1129                if RO.Wonder[woBach].EffectiveOwner = Me then
    11331130                  Inc(Happy, 1);
    11341131              end;
     
    11451142        end;
    11461143      end;
    1147 end; // ProcessSettlers
    1148 
     1144end;
    11491145
    11501146//-------------------------------
     
    11541150procedure TAI.DoTurn;
    11551151var
    1156   emix, i, p1, TaxSum, ScienceSum, NewTaxRate: integer;
    1157   AllHateMe: boolean;
     1152  emix, I, p1, TaxSum, ScienceSum, NewTaxRate: Integer;
     1153  AllHateMe: Boolean;
    11581154{$IFDEF PERF}
    11591155  PF, t0, t1, t2, t3, t4, t5, t6, t7, t8, t9: int64;
     
    11611157begin
    11621158{$IFDEF DEBUG}
    1163   fillchar(DebugMap, sizeof(DebugMap), 0);
     1159  FillChar(DebugMap, SizeOf(DebugMap), 0);
    11641160{$ENDIF}
    11651161
     
    11731169  WarNations := PresenceUnknown;
    11741170  for p1 := 0 to nPl - 1 do
    1175     if (p1 <> me) and (1 shl p1 and RO.Alive <> 0) and (RO.Treaty[p1] < trPeace) then
     1171    if (p1 <> Me) and (1 shl p1 and RO.Alive <> 0) and (RO.Treaty[p1] < trPeace) then
    11761172      Inc(WarNations, 1 shl p1);
    11771173  BombardingNations := 0;
     
    11891185    CheckGender;
    11901186
    1191   if G.Difficulty[me] < MaxDiff then // not on beginner level
     1187  if G.Difficulty[Me] < MaxDiff then // not on beginner level
    11921188  begin
    11931189    if (Data.LastResearchTech = adHorsebackRiding) and (RO.ResearchTech < 0) and
    11941190      (random(6) = 0) and (HavePort or (ContinentPresence[0] and not
    1195       (1 shl me or PresenceUnknown) <> 0)) then
     1191      (1 shl Me or PresenceUnknown) <> 0)) then
    11961192    begin
    11971193      Data.BehaviorFlags := Data.BehaviorFlags or bBarbarina_Hide;
     
    12101206          begin
    12111207            AllHateMe := False;
    1212             break;
     1208            Break;
    12131209          end;
    12141210      if AllHateMe then
     
    13061302  else
    13071303  begin
    1308     if (RO.TaxRate = 0) or (RO.Money < (TotalPopulation[me] - 4) * 2) then
     1304    if (RO.TaxRate = 0) or (RO.Money < (TotalPopulation[Me] - 4) * 2) then
    13091305      NewTaxRate := RO.TaxRate // don't check decreasing tax
    13101306    else
     
    13131309    begin
    13141310      SumCities(NewTaxRate, TaxSum, ScienceSum);
    1315       if RO.Money + TaxSum >= (TotalPopulation[me] - 4) then
    1316         break; // enough
     1311      if RO.Money + TaxSum >= (TotalPopulation[Me] - 4) then
     1312        Break; // enough
    13171313      Inc(NewTaxRate, 10);
    13181314    end;
     
    13281324    // research completed
    13291325    for p1 := 0 to nPl - 1 do
    1330       if (p1 <> me) and (1 shl p1 and RO.Alive <> 0) and
     1326      if (p1 <> Me) and (1 shl p1 and RO.Alive <> 0) and
    13311327        (RO.EnemyReport[p1].TurnOfCivilReport + TechReportOutdated > RO.Turn) and
    13321328        (RO.EnemyReport[p1].Tech[Data.LastResearchTech] < tsSeen) then
    13331329      begin // latest researched advance might be of interest to this nation
    1334         for i := 0 to nRequestedTechs - 1 do
    1335           if (Data.RequestedTechs[i] >= 0) and
    1336             (Data.RequestedTechs[i] shr 8 and $F = p1) then
    1337             Data.RequestedTechs[i] := -1;
     1330        for I := 0 to nRequestedTechs - 1 do
     1331          if (Data.RequestedTechs[I] >= 0) and
     1332            (Data.RequestedTechs[I] shr 8 and $F = p1) then
     1333            Data.RequestedTechs[I] := -1;
    13381334      end;
    13391335  if RO.ResearchTech = adMilitary then
     
    13411337  else
    13421338    Data.LastResearchTech := RO.ResearchTech;
    1343   for i := 0 to nRequestedTechs - 1 do
    1344     if (Data.RequestedTechs[i] >= 0) and
    1345       (RO.Tech[Data.RequestedTechs[i] and $FF] >= tsSeen) then
    1346       Data.RequestedTechs[i] := -1;
     1339  for I := 0 to nRequestedTechs - 1 do
     1340    if (Data.RequestedTechs[I] >= 0) and
     1341      (RO.Tech[Data.RequestedTechs[I] and $FF] >= tsSeen) then
     1342      Data.RequestedTechs[I] := -1;
    13471343
    13481344  // prepare negotiation
     
    13501346  SetAdvanceValues;
    13511347
    1352 
    13531348{$IFDEF DEBUG}
    13541349(*for p1:=0 to nPl-1 do
    1355   if (p1<>me) and (1 shl p1 and RO.Alive<>0) and (RO.Treaty[p1]>=trPeace)
     1350  if (p1<>Me) and (1 shl p1 and RO.Alive<>0) and (RO.Treaty[p1]>=trPeace)
    13561351    and (RO.EnemyReport[p1].TurnOfCivilReport>=0) then
    13571352    TraceAdvanceValues(p1);*)
     
    13681363
    13691364{$IFDEF DEBUG}
    1370 procedure TAI.TraceAdvanceValues(Nation: integer);
     1365procedure TAI.TraceAdvanceValues(Nation: Integer);
    13711366var
    1372   ad: integer;
     1367  ad: Integer;
    13731368begin
    13741369  for ad := 0 to nAdv - 1 do
     
    13801375    end;
    13811376end;
    1382 
    13831377{$ENDIF}
    1384 
    13851378
    13861379procedure TAI.CheckGender;
    13871380var
    1388   p1, NewGender: integer;
     1381  p1, NewGender: Integer;
    13891382begin
    13901383  NewGender := -1;
    13911384  for p1 := 0 to nPl - 1 do
    1392     if (p1 <> me) and (1 shl p1 and RO.Alive <> 0) and
     1385    if (p1 <> Me) and (1 shl p1 and RO.Alive <> 0) and
    13931386      (RO.Treaty[p1] >= trFriendlyContact) then
    1394       if PlayerHash[me] > PlayerHash[p1] then
     1387      if PlayerHash[Me] > PlayerHash[p1] then
    13951388      begin
    13961389        if NewGender = bMale then
    13971390        begin
    13981391          NewGender := -2;
    1399           break;
     1392          Break;
    14001393        end; // ambiguous, don't change gender
    14011394        NewGender := bFemale;
     
    14061399        begin
    14071400          NewGender := -2;
    1408           break;
     1401          Break;
    14091402        end; // ambiguous, don't change gender
    14101403        NewGender := bMale;
     
    14171410end;
    14181411
    1419 
    14201412procedure TAI.SetAdvanceValues;
    14211413
    1422   procedure RateResearchAdv(ad, Time: integer);
     1414  procedure RateResearchAdv(ad, Time: Integer);
    14231415  var
    1424     Value: integer;
     1416    Value: Integer;
    14251417  begin
    14261418    if Time = 0 then
     
    14321424  end;
    14331425
    1434   procedure SetPreqValues(ad, Value: integer);
     1426  procedure SetPreqValues(ad, Value: Integer);
    14351427  begin
    14361428    if (RO.Tech[ad] < tsSeen) and (ad <> RO.ResearchTech) then
     
    14551447  end;
    14561448
    1457   procedure RateImpPreq(iix, Value: integer);
     1449  procedure RateImpPreq(iix, Value: Integer);
    14581450  begin
    14591451    if (Value > 0) and (Imp[iix].Preq >= 0) then
     
    14621454
    14631455var
    1464   emix, cix, adMissing, iad, ad, Count, i, Time, d, CurrentCost,
    1465   CurrentStrength, MaxSize, MaxTrade: integer;
    1466   PreView, Emergency, Bombarded: boolean;
     1456  emix, cix, adMissing, iad, ad, Count, I, Time, D, CurrentCost,
     1457  CurrentStrength, MaxSize, MaxTrade: Integer;
     1458  PreView, Emergency, Bombarded: Boolean;
    14671459begin
    14681460  if AdvanceValuesSet then
    1469     exit;
     1461    Exit;
    14701462  AdvanceValuesSet := True;
    14711463
    1472   fillchar(AdvanceValue, sizeof(AdvanceValue), 0);
     1464  FillChar(AdvanceValue, SizeOf(AdvanceValue), 0);
    14731465
    14741466  // rate techs to ensure research progress
     
    14901482      begin // 2 of 3 required
    14911483        Count := 0;
    1492         for i := 0 to 2 do
    1493           if (AdvPreq[ad, i] = RO.ResearchTech) or
    1494             (RO.Tech[AdvPreq[ad, i]] >= tsSeen) then
     1484        for I := 0 to 2 do
     1485          if (AdvPreq[ad, I] = RO.ResearchTech) or
     1486            (RO.Tech[AdvPreq[ad, I]] >= tsSeen) then
    14951487            Inc(Count);
    14961488        if Count >= 2 then
     
    15001492          if ad <> adMassProduction then // don't score third preq for MP
    15011493          begin
    1502             for i := 0 to 2 do
    1503               if (AdvPreq[ad, i] <> RO.ResearchTech) and
    1504                 (RO.Tech[AdvPreq[ad, i]] < tsSeen) then
    1505                 RateResearchAdv(AdvPreq[ad, i], Time);
     1494            for I := 0 to 2 do
     1495              if (AdvPreq[ad, I] <> RO.ResearchTech) and
     1496                (RO.Tech[AdvPreq[ad, I]] < tsSeen) then
     1497                RateResearchAdv(AdvPreq[ad, I], Time);
    15061498          end;
    15071499          Inc(Time, 2 - Count);
     
    15111503      begin
    15121504        Count := 0;
    1513         for i := 0 to 1 do
    1514           if (AdvPreq[ad, i] <> preNone) and (AdvPreq[ad, i] <> RO.ResearchTech) and
    1515             (RO.Tech[AdvPreq[ad, i]] < tsSeen) then
     1505        for I := 0 to 1 do
     1506          if (AdvPreq[ad, I] <> preNone) and (AdvPreq[ad, I] <> RO.ResearchTech) and
     1507            (RO.Tech[AdvPreq[ad, I]] < tsSeen) then
    15161508          begin
    1517             RateResearchAdv(AdvPreq[ad, i], Time);
     1509            RateResearchAdv(AdvPreq[ad, I], Time);
    15181510            Inc(Count);
    15191511          end;
     
    15441536
    15451537  // rate military techs
    1546   for d := 0 to nDomains - 1 do
     1538  for D := 0 to nDomains - 1 do
    15471539  begin
    15481540    CurrentCost := 0;
    15491541    CurrentStrength := 0;
    15501542    for PreView := True downto False do
    1551       for i := 0 to nUpgrade - 1 do
    1552         with Upgrade[d, i] do
     1543      for I := 0 to nUpgrade - 1 do
     1544        with Upgrade[D, I] do
    15531545          if (Preq >= 0) and not (Preq in FutureTech) then
    15541546            if ((Ro.ResearchTech = Preq) or (RO.Tech[Preq] >= tsSeen)) = PreView then
     
    15611553              else
    15621554              begin // rate
    1563                 if (i > 0) and (Trans > 0) then
     1555                if (I > 0) and (Trans > 0) then
    15641556                  Inc(AdvanceValue[Preq], $400);
    15651557                if Cost <= CurrentCost then
    1566                   Inc(AdvanceValue[Preq], (4 - d) * Strength * $400 div
    1567                     (CurrentStrength + Upgrade[d, 0].Strength))
     1558                  Inc(AdvanceValue[Preq], (4 - D) * Strength * $400 div
     1559                    (CurrentStrength + Upgrade[D, 0].Strength))
    15681560                else
    1569                   Inc(AdvanceValue[Preq], (4 - d) * Strength * $200 div
    1570                     (CurrentStrength + Upgrade[d, 0].Strength));
     1561                  Inc(AdvanceValue[Preq], (4 - D) * Strength * $200 div
     1562                    (CurrentStrength + Upgrade[D, 0].Strength));
    15711563              end;
    15721564  end;
     
    16481640procedure TAI.AnalyzeMap;
    16491641var
    1650   cix, Loc, Loc1, V8, f1, p1: integer;
     1642  cix, Loc, Loc1, V8, f1, p1: Integer;
    16511643  Adjacent: TVicinity8Loc;
    16521644begin
     
    16541646
    16551647  // collect nation presence information for continents and oceans
    1656   fillchar(ContinentPresence, sizeof(ContinentPresence), 0);
    1657   fillchar(OceanPresence, sizeof(OceanPresence), 0);
     1648  FillChar(ContinentPresence, SizeOf(ContinentPresence), 0);
     1649  FillChar(OceanPresence, SizeOf(OceanPresence), 0);
    16581650  for Loc := 0 to MapSize - 1 do
    16591651  begin
     
    17071699  end;
    17081700
    1709   fillchar(TotalPopulation, sizeof(TotalPopulation), 0);
    1710   fillchar(ContinentPopulation, sizeof(ContinentPopulation), 0);
    1711   fillchar(DistrictPopulation, 4 * nDistrict, 0);
     1701  FillChar(TotalPopulation, SizeOf(TotalPopulation), 0);
     1702  FillChar(ContinentPopulation, SizeOf(ContinentPopulation), 0);
     1703  FillChar(DistrictPopulation, 4 * nDistrict, 0);
    17121704
    17131705  // count population
     
    17241716      if Loc >= 0 then
    17251717      begin
    1726         Inc(TotalPopulation[me], Size);
    1727         assert(District[Loc] >= 0);
     1718        Inc(TotalPopulation[Me], Size);
     1719        Assert(District[Loc] >= 0);
    17281720        if District[Loc] < maxCOD then
    17291721          Inc(DistrictPopulation[District[Loc]], Size);
     
    17331725procedure TAI.CollectModelCatStat;
    17341726var
    1735   i, uix, Cat, mix, Quality: integer;
     1727  I, uix, Cat, mix, Quality: Integer;
    17361728begin
    17371729  // categorize models
     
    17791771        if (Loc >= 0) and (mix = mixCruiser) and (Map[Loc] and fTerrain < fGrass) then
    17801772        begin
    1781           i := Formation[Loc];
    1782           if (i >= 0) and (i < maxCOD) then
    1783             OceanWithShip := OceanWithShip or (1 shl i);
     1773          I := Formation[Loc];
     1774          if (I >= 0) and (I < maxCOD) then
     1775            OceanWithShip := OceanWithShip or (1 shl I);
    17841776        end;
    17851777end;
    1786 
    17871778
    17881779procedure TAI.MoveUnitsHome;
    17891780const
    17901781  PatrolDestination = lxmax * lymax;
    1791   FirstSurplusLoop: array[mctGroundDefender..mctGroundAttacker] of integer = (2, 1);
     1782  FirstSurplusLoop: array[mctGroundDefender..mctGroundAttacker] of Integer = (2, 1);
    17921783var
    1793   Cat, i, mix, cix, uix, Loop, nModelOrder: integer;
     1784  Cat, I, mix, cix, uix, Loop, nModelOrder: Integer;
    17941785  Adjacent: TVicinity8Loc;
    1795   LocNeed: array[0..lxmax * lymax - 1] of shortint;
    1796   Destination: array[0..nUmax - 1] of integer;
    1797   DistrictNeed, DistrictNeed0: array[0..maxCOD - 1] of integer;
    1798   ModelOrder: array[0..nMmax - 1] of integer;
    1799   complete, Fortified: boolean;
    1800 
    1801   function IsBombarded(cix: integer): boolean;
     1786  LocNeed: array[0..lxmax * lymax - 1] of ShortInt;
     1787  Destination: array[0..nUmax - 1] of Integer;
     1788  DistrictNeed, DistrictNeed0: array[0..maxCOD - 1] of Integer;
     1789  ModelOrder: array[0..nMmax - 1] of Integer;
     1790  complete, Fortified: Boolean;
     1791
     1792  function IsBombarded(cix: Integer): Boolean;
    18021793  var
    1803     Loc1, V8: integer;
     1794    Loc1, V8: Integer;
    18041795    Adjacent: TVicinity8Loc;
    18051796  begin
     
    18181809          begin
    18191810            Result := True;
    1820             exit;
     1811            Exit;
    18211812          end;
    18221813        end;
     
    18241815  end;
    18251816
    1826   procedure TryUtilize(uix: integer);
     1817  procedure TryUtilize(uix: Integer);
    18271818  var
    1828     cix, ProdCost, UtilizeCost: integer;
     1819    cix, ProdCost, UtilizeCost: Integer;
    18291820  begin
    18301821    if (MyUnit[uix].Health = 100) and (Map[MyUnit[uix].Loc] and
     
    18381829          UtilizeCost := MyModel[MyUnit[uix].mix].Cost;
    18391830          if Prod < (ProdCost - UtilizeCost * 2 div 3) *
    1840             BuildCostMod[G.Difficulty[me]] div 12 then
     1831            BuildCostMod[G.Difficulty[Me]] div 12 then
    18411832            Unit_Disband(uix);
    18421833        end;
     
    18441835  end;
    18451836
    1846   procedure FindDestination(uix: integer);
     1837  procedure FindDestination(uix: Integer);
    18471838  var
    1848     MoveStyle, V8, Loc1, Time, NextLoc, NextTime, RecoverTurns: integer;
    1849     Reached: array[0..lxmax * lymax - 1] of boolean;
    1850   begin
    1851     fillchar(Reached, MapSize, False);
     1839    MoveStyle, V8, Loc1, Time, NextLoc, NextTime, RecoverTurns: Integer;
     1840    Reached: array[0..lxmax * lymax - 1] of Boolean;
     1841  begin
     1842    FillChar(Reached, MapSize, False);
    18521843    Pile.Create(MapSize);
    18531844    with MyUnit[uix] do
     
    18631854        if (District[Loc1] >= 0) and (District[Loc1] < maxCOD) then
    18641855        begin
    1865           assert(DistrictNeed[District[Loc1]] > 0);
     1856          Assert(DistrictNeed[District[Loc1]] > 0);
    18661857          Dec(DistrictNeed[District[Loc1]]);
    18671858        end;
    18681859        Destination[uix] := Loc1;
    1869         break;
     1860        Break;
    18701861      end;
    18711862      Reached[Loc1] := True;
     
    18741865      begin
    18751866        NextLoc := Adjacent[V8];
    1876         if (NextLoc >= 0) and not Reached[NextLoc] and (RO.Territory[NextLoc] = me) then
     1867        if (NextLoc >= 0) and not Reached[NextLoc] and (RO.Territory[NextLoc] = Me) then
    18771868          case CheckStep(MoveStyle, Time, V8 and 1, NextTime, RecoverTurns,
    18781869              Map[Loc1], Map[NextLoc], False) of
     
    18821873              Reached[NextLoc] := True; // don't check moving there again
    18831874            csCheckTerritory:
    1884               assert(False);
     1875              Assert(False);
    18851876          end;
    18861877      end;
     
    18961887          Unit_Disband(uix);
    18971888
    1898   fillchar(UnitLack, sizeof(UnitLack), 0);
    1899   fillchar(Destination, 4 * RO.nUn, $FF);
    1900   for i := 0 to maxCOD - 1 do
    1901     if uixPatrol[i] >= 0 then
    1902       Destination[uixPatrol[i]] := PatrolDestination;
     1889  FillChar(UnitLack, SizeOf(UnitLack), 0);
     1890  FillChar(Destination, 4 * RO.nUn, $FF);
     1891  for I := 0 to maxCOD - 1 do
     1892    if uixPatrol[I] >= 0 then
     1893      Destination[uixPatrol[I]] := PatrolDestination;
    19031894  for uix := 0 to RO.nUn - 1 do
    19041895    if (MyUnit[uix].mix = mixMilitia) or (MyUnit[uix].mix = mixCruiser) then
     
    19121903      if ModelCat[mix] = Cat then
    19131904      begin
    1914         i := nModelOrder;
    1915         while (i > 0) and (ModelQuality[mix] < ModelQuality[ModelOrder[i - 1]]) do
    1916         begin
    1917           ModelOrder[i] := ModelOrder[i - 1];
    1918           Dec(i);
    1919         end;
    1920         ModelOrder[i] := mix;
     1905        I := nModelOrder;
     1906        while (I > 0) and (ModelQuality[mix] < ModelQuality[ModelOrder[I - 1]]) do
     1907        begin
     1908          ModelOrder[I] := ModelOrder[I - 1];
     1909          Dec(I);
     1910        end;
     1911        ModelOrder[I] := mix;
    19211912        Inc(nModelOrder);
    19221913      end;
     
    19311922              TryUtilize(uix);
    19321923
    1933       fillchar(LocNeed, MapSize, 0);
    1934       fillchar(DistrictNeed, sizeof(DistrictNeed), 0);
     1924      FillChar(LocNeed, MapSize, 0);
     1925      FillChar(DistrictNeed, SizeOf(DistrictNeed), 0);
    19351926
    19361927      for cix := 0 to RO.nCity - 1 do
     
    19551946        for uix := 0 to RO.nUn - 1 do
    19561947          with MyUnit[uix] do
    1957             if (Loc >= 0) and (Job = jCity) and (RO.Territory[Loc] = me) then
     1948            if (Loc >= 0) and (Job = jCity) and (RO.Territory[Loc] = Me) then
    19581949            begin
    19591950              LocNeed[Loc] := 1;
     
    19631954
    19641955      complete := Loop >= FirstSurplusLoop[Cat];
    1965       for i := nModelOrder - 1 downto 0 do
     1956      for I := nModelOrder - 1 downto 0 do
    19661957      begin
    19671958        for Fortified := True downto False do
    19681959          for uix := 0 to RO.nUn - 1 do
    19691960            with MyUnit[uix] do
    1970               if (mix = ModelOrder[i]) and (Loc >= 0) and
     1961              if (mix = ModelOrder[I]) and (Loc >= 0) and
    19711962                (Destination[uix] < 0) and (Master < 0) and
    19721963                ((Flags and unFortified <> 0) = Fortified) and (LocNeed[Loc] > 0) then
     
    19811972        for uix := 0 to RO.nUn - 1 do
    19821973          with MyUnit[uix] do
    1983             if (mix = ModelOrder[i]) and (Loc >= 0) and (Destination[uix] < 0) and
     1974            if (mix = ModelOrder[I]) and (Loc >= 0) and (Destination[uix] < 0) and
    19841975              (Master < 0) then
    19851976              if (District[Loc] >= 0) and (District[Loc] < maxCOD) and
     
    19981989  // distribute obsolete settlers
    19991990  repeat
    2000     fillchar(LocNeed, MapSize, 0);
    2001     fillchar(DistrictNeed, sizeof(DistrictNeed), 0);
     1991    FillChar(LocNeed, MapSize, 0);
     1992    FillChar(DistrictNeed, SizeOf(DistrictNeed), 0);
    20021993
    20031994    for cix := 0 to RO.nCity - 1 do
     
    20492040  for uix := 0 to RO.nUn - 1 do
    20502041    with MyUnit[uix] do
    2051       if (Loc >= 0) and (RO.Territory[Loc] = me) and (District[Loc] >= 0) and
     2042      if (Loc >= 0) and (RO.Territory[Loc] = Me) and (District[Loc] >= 0) and
    20522043        (District[Loc] < maxCOD) and (ModelQuality[mix] > 0) then
    20532044        case ModelCat[mix] of
     
    20552046            Dec(UnitLack[District[Loc], ModelCat[mix]])
    20562047        end;
    2057 end; // MoveUnitsHome
    2058 
    2059 
    2060 procedure TAI.CheckAttack(uix: integer);
     2048end;
     2049
     2050procedure TAI.CheckAttack(uix: Integer);
    20612051var
    20622052  AttackScore, BestCount, AttackLoc, TestLoc, NextLoc, TestTime, V8,
    20632053  TestScore, euix, MyDamage, EnemyDamage, OldLoc, AttackForecast,
    2064   MoveResult, AttackResult, MoveStyle, NextTime, RecoverTurns: integer;
    2065   Tile: cardinal;
    2066   Exhausted: boolean;
     2054  MoveResult, AttackResult, MoveStyle, NextTime, RecoverTurns: Integer;
     2055  Tile: Cardinal;
     2056  Exhausted: Boolean;
    20672057  Adjacent: TVicinity8Loc;
    2068   Reached: array[0..lxmax * lymax - 1] of boolean;
     2058  Reached: array[0..lxmax * lymax - 1] of Boolean;
    20692059
    20702060begin
     
    20752065      AttackScore := -999999;
    20762066      AttackLoc := -1;
    2077       fillchar(Reached, MapSize, False);
     2067      FillChar(Reached, MapSize, False);
    20782068      Pile.Create(MapSize);
    20792069      Pile.Put(Loc, $800 - Movement);
     
    20872077        if ((Tile and fUnit) <> 0) and ((Tile and fOwned) = 0) then
    20882078        begin // enemy unit
    2089           assert(TestTime < $1000);
     2079          Assert(TestTime < $1000);
    20902080          Unit_FindEnemyDefender(TestLoc, euix);
    20912081          if RO.Treaty[RO.EnemyUn[euix].Owner] < trPeace then
     
    21762166    until Exhausted;
    21772167  end;
    2178 end; // CheckAttack
    2179 
    2180 
    2181 procedure TAI.Patrol(uix: integer);
     2168end;
     2169
     2170procedure TAI.Patrol(uix: Integer);
    21822171const
    21832172  DistanceScore = 4;
    21842173var
    21852174  PatrolScore, BestCount, PatrolLoc, TestLoc, NextLoc, TestTime, V8,
    2186   TestScore, OldLoc, MoveResult, MoveStyle, NextTime, RecoverTurns: integer;
    2187   Tile: cardinal;
    2188   Exhausted, CaptureOnly: boolean;
     2175  TestScore, OldLoc, MoveResult, MoveStyle, NextTime, RecoverTurns: Integer;
     2176  Tile: Cardinal;
     2177  Exhausted, CaptureOnly: Boolean;
    21892178  Adjacent: TVicinity8Loc;
    2190   AdjacentUnknown: array[0..lxmax * lymax - 1] of shortint;
     2179  AdjacentUnknown: array[0..lxmax * lymax - 1] of ShortInt;
    21912180
    21922181begin
     
    22072196          // assume a score of 50 is the best achievable
    22082197          or CaptureOnly and (TestTime >= $1000) then
    2209           break;
     2198          Break;
    22102199
    22112200        TestScore := 0;
     
    22822271    until Exhausted;
    22832272  end;
    2284 end; // Patrol
     2273end;
    22852274
    22862275procedure TAI.AttackAndPatrol;
    22872276const
    22882277  nAttackCatOrder = 3;
    2289   AttackCatOrder: array[0..nAttackCatOrder - 1] of integer =
     2278  AttackCatOrder: array[0..nAttackCatOrder - 1] of Integer =
    22902279    (mctGroundAttacker, mctCruiser, mctGroundDefender);
    22912280var
    2292   iCat, uix, uix1: integer;
    2293   IsPatrolUnit, Fortified: boolean;
     2281  iCat, uix, uix1: Integer;
     2282  IsPatrolUnit, Fortified: Boolean;
    22942283begin
    22952284  for uix := 0 to RO.nUn - 1 do
     
    23102299              CheckAttack(uix);
    23112300
    2312   fillchar(uixPatrol, sizeof(uixPatrol), $FF);
     2301  FillChar(uixPatrol, SizeOf(uixPatrol), $FF);
    23132302  for uix := 0 to RO.nUn - 1 do
    23142303    with MyUnit[uix], MyModel[mix] do
     
    23422331          Patrol(uix);
    23432332      end;
    2344 end; // AttackAndPatrol
    2345 
    2346 
    2347 function TAI.HavePort: boolean;
     2333end;
     2334
     2335function TAI.HavePort: Boolean;
    23482336var
    2349   V8, cix, AdjacentLoc, f: integer;
     2337  V8, cix, AdjacentLoc, F: Integer;
    23502338  Adjacent: TVicinity8Loc;
    23512339begin
     
    23612349          if (AdjacentLoc >= 0) and ((Map[AdjacentLoc] and fTerrain) < fGrass) then
    23622350          begin
    2363             f := Formation[AdjacentLoc];
    2364             if (f >= 0) and (f < maxCOD) and (OceanPresence[f] and
    2365               not (1 shl me) <> 0) then
     2351            F := Formation[AdjacentLoc];
     2352            if (F >= 0) and (F < maxCOD) and (OceanPresence[F] and
     2353              not (1 shl Me) <> 0) then
    23662354              Result := True;
    23672355          end;
     
    23692357      end;
    23702358end;
    2371 
    23722359
    23732360procedure TAI.SetCityProduction;
    23742361var
    23752362  uix, cix, iix, dtr, V8, V21, NewImprovement, AdjacentLoc, MaxSettlers,
    2376   maxcount, cixMilAcademy: integer;
    2377   TerrType: cardinal;
     2363  maxcount, cixMilAcademy: Integer;
     2364  TerrType: Cardinal;
    23782365  IsPort, IsNavalBase, NeedCruiser, CheckProd, Destructed, ProduceSettlers,
    2379   ProduceMil: boolean;
     2366  ProduceMil: Boolean;
    23802367  Adjacent: TVicinity8Loc;
    23812368  Radius: TVicinity21Loc;
    23822369  Report: TCityReport;
    2383   HomeCount, CityProdRep: array[0..nCmax - 1] of integer;
    2384   MilProdCity: array[0..nCmax - 1] of boolean;
    2385 
    2386   procedure TryBuild(Improvement: integer);
     2370  HomeCount, CityProdRep: array[0..nCmax - 1] of Integer;
     2371  MilProdCity: array[0..nCmax - 1] of Boolean;
     2372
     2373  procedure TryBuild(Improvement: Integer);
    23872374  begin
    23882375    if (NewImprovement = imTrGoods) // not already improvement of higher priority found
     
    23942381  end;
    23952382
    2396   procedure TryDestruct(Improvement: integer);
     2383  procedure TryDestruct(Improvement: Integer);
    23972384  begin
    23982385    if Destructed or (MyCity[cix].Built[Improvement] = 0) then
    2399       exit;
     2386      Exit;
    24002387    if City_CurrentImprovementProject(cix) >= 0 then
    24012388      City_RebuildImprovement(cix, Improvement)
     
    24052392      and (Imp[CurrentImprovementProject].Kind in [ikCommon,ikNatGlobal,ikNatLocal])
    24062393      and ((Imp[CurrentImprovementProject].Cost*3-Imp[Improvement].Cost*2)
    2407       *BuildCostMod[G.Difficulty[me]]>MyCity[cix].Prod*(12*3)) then}
     2394      *BuildCostMod[G.Difficulty[Me]]>MyCity[cix].Prod*(12*3)) then}
    24082395    Destructed := True;
    24092396  end;
    24102397
    2411   function ChooseBuildModel(Cat: integer): integer;
     2398  function ChooseBuildModel(Cat: Integer): Integer;
    24122399  var
    2413     Count, mix: integer;
     2400    Count, mix: Integer;
    24142401  begin
    24152402    Count := 0;
     
    24222409          Result := mix;
    24232410      end;
    2424     assert(Count > 0);
     2411    Assert(Count > 0);
    24252412  end;
    24262413
     
    24282415  // find military production cities
    24292416  var
    2430     cix, Total, d, Threshold, NewThreshold, Share, SharePlus, cixWorst: integer;
    2431   begin
    2432     fillchar(MilProdCity, RO.nCity, 0);
     2417    cix, Total, D, Threshold, NewThreshold, Share, SharePlus, cixWorst: Integer;
     2418  begin
     2419    FillChar(MilProdCity, RO.nCity, 0);
    24332420    GetCityProdPotential;
    2434     for d := 0 to maxCOD - 1 do
     2421    for D := 0 to maxCOD - 1 do
    24352422    begin
    24362423      Total := 0;
    24372424      for cix := 0 to RO.nCity - 1 do
    24382425        with MyCity[cix] do
    2439           if (Loc >= 0) and (District[Loc] = d) then
     2426          if (Loc >= 0) and (District[Loc] = D) then
    24402427            Total := Total + CityResult[cix];
    24412428      if Total = 0 then
     
    24462433      for cix := 0 to RO.nCity - 1 do
    24472434        with MyCity[cix] do
    2448           if (Loc >= 0) and (District[Loc] = d) and
     2435          if (Loc >= 0) and (District[Loc] = D) and
    24492436            (Built[imBarracks] + Built[imMilAcademy] > 0) then
    24502437          begin
     
    24612448        for cix := 0 to RO.nCity - 1 do
    24622449          with MyCity[cix] do
    2463             if (Loc >= 0) and (District[Loc] = d) and
     2450            if (Loc >= 0) and (District[Loc] = D) and
    24642451              (Built[imBarracks] + Built[imMilAcademy] = 0) and
    24652452              (Built[imObservatory] = 0) and (CityResult[cix] < Threshold) and
     
    24782465      for cix := 0 to RO.nCity - 1 do
    24792466        with MyCity[cix] do
    2480           if (Loc >= 0) and (District[Loc] = d) and
     2467          if (Loc >= 0) and (District[Loc] = D) and
    24812468            (Built[imBarracks] + Built[imMilAcademy] = 0) and
    24822469            (CityResult[cix] >= Threshold) then
     
    24842471{    if (cixWorst>=0)
    24852472      and (Share-CityResult[cixWorst]*2>=Total*MilProdShare div 100) then
    2486       MilProdCity[cixWorst]:=false;}
     2473      MilProdCity[cixWorst]:=False;}
    24872474    end;
    24882475
     
    24912478    if cixStateImp[imPalace] >= 0 then
    24922479    begin
    2493       d := District[MyCity[cixStateImp[imPalace]].Loc];
    2494       if (d >= 0) and (d < maxCOD) then
     2480      D := District[MyCity[cixStateImp[imPalace]].Loc];
     2481      if (D >= 0) and (D < maxCOD) then
    24952482      begin
    24962483        cixMilAcademy := -1;
    24972484        for cix := 0 to RO.nCity - 1 do
    24982485          with MyCity[cix] do
    2499             if (Loc >= 0) and (District[Loc] = d) and
     2486            if (Loc >= 0) and (District[Loc] = D) and
    25002487              (Built[imObservatory] + Built[imPalace] = 0) and
    25012488              ((cixMilAcademy < 0) or (CityResult[cix] > CityResult[cixMilAcademy])) then
     
    25132500  procedure ChangeHomeCities;
    25142501  var
    2515     uix, NewHome, HomeSupport, NewHomeSupport, SingleSupport: integer;
     2502    uix, NewHome, HomeSupport, NewHomeSupport, SingleSupport: Integer;
    25162503  begin
    25172504    if RO.Government in [gAnarchy, gFundamentalism] then
    2518       exit;
     2505      Exit;
    25192506    for uix := 0 to RO.nUn - 1 do
    25202507      with MyUnit[uix] do
     
    25642551
    25652552begin
    2566   fillchar(HomeCount, 4 * RO.nCity, 0);
     2553  FillChar(HomeCount, 4 * RO.nCity, 0);
    25672554  for uix := 0 to RO.nUn - 1 do
    25682555    with MyUnit[uix] do
     
    27572744                    begin
    27582745                      TryBuild(imHarbor);
    2759                       break;
     2746                      Break;
    27602747                    end;
    27612748                  end;
     
    27752762              TryBuild(imRecycling);
    27762763            if (Report.Trade - Report.Corruption >= 11) and
    2777               (RO.Money < TotalPopulation[me] * 2) then
     2764              (RO.Money < TotalPopulation[Me] * 2) then
    27782765              TryBuild(imBank);
    27792766            if (RO.NatBuilt[imStockEx] = 0) and
     
    28122799
    28132800          // rebuild imps no longer needed
    2814           if (RO.TaxRate = 0) and (RO.Money >= TotalPopulation[me] * 4) then
     2801          if (RO.TaxRate = 0) and (RO.Money >= TotalPopulation[Me] * 4) then
    28152802            TryDestruct(imBank)
    28162803          else if Report.Happy * 2 >= Size + 6 then
     
    28362823
    28372824  ChangeHomeCities;
    2838 end; // SetCityProduction
    2839 
    2840 
    2841 function TAI.ChooseGovernment: integer;
     2825end;
     2826
     2827function TAI.ChooseGovernment: Integer;
    28422828begin
    28432829  if Data.BehaviorFlags and bBarbarina <> 0 then
     
    28562842end;
    28572843
    2858 
    28592844//-------------------------------
    28602845//           DIPLOMACY
    28612846//-------------------------------
    28622847
    2863 function TAI.MostWanted(Nation, adGiveAway: integer): integer;
     2848function TAI.MostWanted(Nation, adGiveAway: Integer): Integer;
    28642849var
    2865   ad: integer;
     2850  ad: Integer;
    28662851begin
    28672852  Result := -1;
     
    28912876end;
    28922877
    2893 procedure TAI.FindBestTrade(Nation: integer; var adWanted, adGiveAway: integer);
     2878procedure TAI.FindBestTrade(Nation: Integer; var adWanted, adGiveAway: Integer);
    28942879var
    2895   i, ad, ead, adTestGiveAway: integer;
     2880  I, ad, ead, adTestGiveAway: Integer;
    28962881begin
    28972882  adWanted := -1;
     
    29032888    begin
    29042889      adTestGiveAway := -1;
    2905       for i := 0 to nRequestedTechs - 1 do
    2906         if (Data.RequestedTechs[i] >= 0) and
    2907           (Data.RequestedTechs[i] and $FFFF = Nation shl 8 + ead) then
     2890      for I := 0 to nRequestedTechs - 1 do
     2891        if (Data.RequestedTechs[I] >= 0) and
     2892          (Data.RequestedTechs[I] and $FFFF = Nation shl 8 + ead) then
    29082893          adTestGiveAway := -2; // already requested before
    29092894      if adTestGiveAway = -1 then
     
    29282913end;
    29292914
    2930 
    2931 function TAI.WantNegotiation(Nation: integer; NegoTime: TNegoTime): boolean;
     2915function TAI.WantNegotiation(Nation: Integer; NegoTime: TNegoTime): Boolean;
    29322916var
    2933   p1, Count, adWanted, adGiveAway: integer;
     2917  p1, Count, adWanted, adGiveAway: Integer;
    29342918begin
    29352919  if Data.BehaviorFlags and bBarbarina = bBarbarina then
    29362920  begin
    29372921    Result := Barbarina_WantNegotiation(Nation, NegoTime);
    2938     exit;
     2922    Exit;
    29392923  end;
    29402924
     
    29442928    begin
    29452929      Result := False;
    2946       exit;
     2930      Exit;
    29472931    end;
    29482932    Count := 0;
    29492933    for p1 := 0 to nPl - 1 do
    2950       if (p1 <> me) and (1 shl p1 and RO.Alive <> 0) and (RO.Treaty[p1] >= trPeace) then
     2934      if (p1 <> Me) and (1 shl p1 and RO.Alive <> 0) and (RO.Treaty[p1] >= trPeace) then
    29512935        Inc(Count);
    29522936    if Count >= 3 then // enough peace made
    29532937    begin
    29542938      Result := False;
    2955       exit;
     2939      Exit;
    29562940    end;
    29572941  end;
     
    29942978procedure TAI.DoNegotiation;
    29952979var
    2996   i, adWanted, adGiveAway, adToGet, Slot: integer;
    2997   BuildFreeOffer: boolean;
     2980  I, adWanted, adGiveAway, adToGet, Slot: Integer;
     2981  BuildFreeOffer: Boolean;
    29982982begin
    29992983  if MyLastAction = scDipOffer then
     
    30263010  begin
    30273011    Barbarina_DoNegotiation;
    3028     exit;
     3012    Exit;
    30293013  end;
    30303014
     
    30323016  begin
    30333017    Barbarina_DoCheckNegotiation;
    3034     exit;
     3018    Exit;
    30353019  end;
    30363020
     
    30473031      (OppoOffer.nDeliver + OppoOffer.nCost = 1) and
    30483032      (OppoOffer.Price[0] and opMask = opTreaty) and
    3049       (integer(OppoOffer.Price[0] - opTreaty) > RO.Treaty[Opponent]) and
     3033      (Integer(OppoOffer.Price[0] - opTreaty) > RO.Treaty[Opponent]) and
    30503034      ((OppoOffer.Price[0] - opTreaty < trAlliance) or
    30513035      (RO.Tech[adScience] >= tsSeen)) then
    30523036      MyAction := scDipAccept // accept all treaties
    30533037    else if (RO.Treaty[Opponent] >= trPeace) and (OppoOffer.nDeliver = 1) and
    3054       (OppoOffer.Price[0] and $FFFF0000 = opCivilReport + cardinal(Opponent) shl 16) and
     3038      (OppoOffer.Price[0] and $FFFF0000 = opCivilReport + Cardinal(Opponent) shl 16) and
    30553039      (OppoOffer.nCost = 1) and (OppoOffer.Price[1] and $FFFF0000 =
    3056       opCivilReport + cardinal(me) shl 16) then
     3040      opCivilReport + Cardinal(Me) shl 16) then
    30573041      MyAction := scDipAccept // accept exchange of civil reports
    30583042    else if (OppoOffer.nDeliver = 1) and (OppoOffer.nCost = 1) and
     
    30843068        adWanted := MostWanted(Opponent, OppoOffer.Price[1] - opTech);
    30853069        if (OppoOffer.Price[0] and opMask = opTech) and
    3086           (cardinal(adWanted) = OppoOffer.Price[0] - opTech) then
     3070          (Cardinal(adWanted) = OppoOffer.Price[0] - opTech) then
    30873071          MyAction := scDipAccept // opponent's offer is already perfect
    30883072        else if adWanted >= 0 then
     
    31403124        MyOffer.Price[1] := opTech + adWanted;
    31413125        MyAction := scDipOffer;
    3142         for i := 0 to nRequestedTechs - 1 do
    3143           if Data.RequestedTechs[i] < 0 then
     3126        for I := 0 to nRequestedTechs - 1 do
     3127          if Data.RequestedTechs[I] < 0 then
    31443128          begin
    3145             Slot := i;
    3146             break;
     3129            Slot := I;
     3130            Break;
    31473131          end
    3148           else if (i = 0) or (Data.RequestedTechs[i] shr 16 <
     3132          else if (I = 0) or (Data.RequestedTechs[I] shr 16 <
    31493133            Data.RequestedTechs[Slot] shr 16) then // find most outdated entry
    3150             Slot := i;
     3134            Slot := I;
    31513135        Data.RequestedTechs[Slot] := RO.Turn shl 16 + Opponent shl 8 + adWanted;
    31523136      end;
    31533137    end;
    31543138  end;
    3155 end; // Negotiation
    3156 
     3139end;
    31573140
    31583141procedure SetLeaveOutValue;
    31593142
    3160   procedure Process(ad: integer);
     3143  procedure Process(ad: Integer);
    31613144  var
    3162     i: integer;
     3145    I: Integer;
    31633146  begin
    31643147    if LeaveOutValue[ad] < 0 then
    31653148    begin
    31663149      LeaveOutValue[ad] := 0;
    3167       for i := 0 to 1 do
    3168         if AdvPreq[ad, i] >= 0 then
    3169         begin
    3170           Process(AdvPreq[ad, i]);
    3171           if AdvPreq[ad, i] in LeaveOutTechs then
    3172             Inc(LeaveOutValue[ad], LeaveOutValue[AdvPreq[ad, i]] + 1);
     3150      for I := 0 to 1 do
     3151        if AdvPreq[ad, I] >= 0 then
     3152        begin
     3153          Process(AdvPreq[ad, I]);
     3154          if AdvPreq[ad, I] in LeaveOutTechs then
     3155            Inc(LeaveOutValue[ad], LeaveOutValue[AdvPreq[ad, I]] + 1);
    31733156        end;
    31743157    end;
     
    31763159
    31773160var
    3178   ad: integer;
     3161  ad: Integer;
    31793162begin
    31803163  FillChar(LeaveOutValue, SizeOf(LeaveOutValue), $FF);
     
    31853168
    31863169initialization
    3187   RWDataSize := sizeof(TPersistentData);
     3170  RWDataSize := SizeOf(TPersistentData);
    31883171  SetLeaveOutValue;
    31893172
Note: See TracChangeset for help on using the changeset viewer.