Changeset 447 for trunk/AI


Ignore:
Timestamp:
May 19, 2022, 10:39:34 PM (3 years ago)
Author:
chronos
Message:
  • Modified: Use first capital letter in identifiers.
Location:
trunk/AI/StdAI
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/AI/StdAI/AI.pas

    r442 r447  
    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 constructor TAI.Create(Nation: integer);
     176  LeaveOutValue: array[0..nAdv - 1] of Integer;
     177
     178constructor TAI.Create(Nation: Integer);
    179179begin
    180180  inherited;
    181   Data := pointer(RO.Data);
     181  Data := Pointer(RO.Data);
    182182{$IFDEF DEBUG}
    183183  if Nation = 1 then
     
    192192  begin
    193193    LastResearchTech := -1;
    194     if PlayerHash[me] > 7 then
     194    if PlayerHash[Me] > 7 then
    195195      BehaviorFlags := bFemale
    196196    else
     
    198198    DebugMessage(1, 'Gender:=' + char(48 + BehaviorFlags and bGender));
    199199    TheologyPartner := -1;
    200     fillchar(RejectTurn, sizeof(RejectTurn), $FF);
    201     Fillchar(RequestedTechs, sizeof(RequestedTechs), $FF);
     200    FillChar(RejectTurn, SizeOf(RejectTurn), $FF);
     201    Fillchar(RequestedTechs, SizeOf(RequestedTechs), $FF);
    202202  end;
    203203end;
    204204
    205 function TAI.OnNegoRejected_CancelTreaty: boolean;
     205function TAI.OnNegoRejected_CancelTreaty: Boolean;
    206206begin
    207207  Data.RejectTurn[suContact, Opponent] := RO.Turn;
     
    213213//-------------------------------
    214214
    215 procedure TAI.RateModel(const mi: TModelInfo; var Category, Quality: integer);
     215procedure TAI.RateModel(const mi: TModelInfo; var Category, Quality: Integer);
    216216var
    217   EffectiveTransport: integer;
     217  EffectiveTransport: Integer;
    218218begin
    219219  if mi.Kind >= mkScout then
    220220  begin
    221221    Category := mctNone;
    222     exit;
     222    Exit;
    223223  end;
    224224  case mi.Domain of
     
    292292end;
    293293
    294 procedure TAI.RateMyModel(mix: integer; var Category, Quality: integer);
     294procedure TAI.RateMyModel(mix: Integer; var Category, Quality: Integer);
    295295var
    296296  mi: TModelInfo;
    297297begin
    298   MakeModelInfo(me, mix, MyModel[mix], mi);
     298  MakeModelInfo(Me, mix, MyModel[mix], mi);
    299299  RateModel(mi, Category, Quality);
    300300end;
    301301
    302 function TAI.IsBetterModel(const mi: TModelInfo): boolean;
     302function TAI.IsBetterModel(const mi: TModelInfo): Boolean;
    303303var
    304   mix, Cat, Quality, Cat1, Quality1: integer;
     304  mix, Cat, Quality, Cat1, Quality1: Integer;
    305305begin
    306306  RateModel(mi, Cat, Quality);
     
    312312      begin
    313313        Result := False;
    314         exit;
     314        Exit;
    315315      end;
    316316    end;
     
    318318end;
    319319
    320 function TAI.ChooseResearchAdvance: integer;
     320function TAI.ChooseResearchAdvance: Integer;
    321321var
    322   adNext, iad, i, ad, Count, EarliestNeeded, EarliestNeeded_NoLeaveOut,
    323   NewResearch, StateOfArt, mix: integer;
     322  adNext, iad, I, ad, Count, EarliestNeeded, EarliestNeeded_NoLeaveOut,
     323  NewResearch, StateOfArt, mix: Integer;
    324324  mi: TModelInfo;
    325   Entry: array[0..nAdv - 1] of boolean;
    326   ok: boolean;
    327 
    328   function MarkEntry(ad: integer): boolean;
     325  Entry: array[0..nAdv - 1] of Boolean;
     326  ok: Boolean;
     327
     328  function MarkEntry(ad: Integer): Boolean;
    329329  begin
    330330    if RO.Tech[ad] >= tsApplicable then
     
    373373  end;
    374374
    375   procedure OptimizeDevModel(OptimizeCaps: integer);
     375  procedure OptimizeDevModel(OptimizeCaps: Integer);
    376376  var
    377     f, Cat, OriginalCat, Quality, BestQuality, Best: integer;
     377    F, Cat, OriginalCat, Quality, BestQuality, Best: Integer;
    378378    mi: TModelInfo;
    379379  begin
    380     MakeModelInfo(me, 0, RO.DevModel, mi);
     380    MakeModelInfo(Me, 0, RO.DevModel, mi);
    381381    RateModel(mi, OriginalCat, BestQuality);
    382382    repeat
    383383      Best := -1;
    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
     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
    391391          begin
    392             MakeModelInfo(me, 0, RO.DevModel, mi);
     392            MakeModelInfo(Me, 0, RO.DevModel, mi);
    393393            RateModel(mi, Cat, Quality);
    394             assert(Cat = OriginalCat);
     394            Assert(Cat = OriginalCat);
    395395            if Quality > BestQuality then
    396396            begin
    397               Best := f;
     397              Best := F;
    398398              BestQuality := Quality;
    399399            end;
    400             SetNewModelFeature(f, RO.DevModel.Cap[f] - 1);
     400            SetNewModelFeature(F, RO.DevModel.Cap[F] - 1);
    401401          end;
    402402        end;
     
    406406  end;
    407407
    408   function LeaveOutsMissing(ad: integer): boolean;
     408  function LeaveOutsMissing(ad: Integer): Boolean;
    409409  var
    410     i: integer;
     410    I: Integer;
    411411  begin
    412412    Result := False;
     
    422422        Result := True
    423423      else
    424         for i := 0 to 1 do
    425           if AdvPreq[ad, i] >= 0 then
    426             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]);
    427427  end;
    428428
     
    432432    Result := Barbarina_ChooseResearchAdvance;
    433433    if Result >= 0 then
    434       exit;
     434      Exit;
    435435  end;
    436436
     
    444444      Result := ad;
    445445  if Result >= 0 then
    446     exit;
     446    Exit;
    447447
    448448  if Data.BehaviorFlags and bBarbarina = 0 then
     
    460460      OptimizeDevModel(1 shl mcOffense + 1 shl mcDefense + 1 shl
    461461        mcMob + 1 shl mcLongRange + 1 shl mcFanatic);
    462       MakeModelInfo(me, 0, RO.DevModel, mi);
     462      MakeModelInfo(Me, 0, RO.DevModel, mi);
    463463      if IsBetterModel(mi) then
    464464      begin
    465465        Result := adMilitary;
    466         exit;
     466        Exit;
    467467      end;
    468468
     
    471471      SetNewModelFeature(mcOffense, 1);
    472472      OptimizeDevModel(1 shl mcOffense + 1 shl mcDefense + 1 shl mcFanatic);
    473       MakeModelInfo(me, 0, RO.DevModel, mi);
     473      MakeModelInfo(Me, 0, RO.DevModel, mi);
    474474      if IsBetterModel(mi) then
    475475      begin
    476476        Result := adMilitary;
    477         exit;
     477        Exit;
    478478      end;
    479479    end;
     
    491491        SetNewModelFeature(mcWeapons, 0);
    492492        SetNewModelFeature(mcDefense, 3);
    493         exit;
     493        Exit;
    494494      end;
    495495    end;
     
    506506      OptimizeDevModel(1 shl mcDefense+1 shl mcSeaTrans+1 shl mcTurbines
    507507        +1 shl mcAirDef);
    508       MakeModelInfo(me,0,RO.DevModel,mi);
     508      MakeModelInfo(Me,0,RO.DevModel,mi);
    509509      if IsBetterModel(mi) then
    510         begin result:=adMilitary; exit end;
     510        begin Result:=adMilitary; Exit end;
    511511      end;
    512512
     
    519519      OptimizeDevModel(1 shl mcOffense+1 shl mcDefense
    520520        +1 shl mcLongRange+1 shl mcAirDef+1 shl mcRadar);
    521       MakeModelInfo(me,0,RO.DevModel,mi);
     521      MakeModelInfo(Me,0,RO.DevModel,mi);
    522522      if IsBetterModel(mi) then
    523         begin result:=adMilitary; exit end;
     523        begin Result:=adMilitary; Exit end;
    524524      end
    525525    end;
     
    545545        begin
    546546          ok := True;
    547           break;
     547          Break;
    548548        end;
    549549      if not ok then
     
    588588        begin // 2 of 3 required
    589589          Count := 0;
    590           for i := 0 to 2 do
    591             if RO.Tech[AdvPreq[ad, i]] >= tsApplicable then
     590          for I := 0 to 2 do
     591            if RO.Tech[AdvPreq[ad, I]] >= tsApplicable then
    592592              Inc(Count);
    593593          if Count >= 2 then
    594594          begin
    595595            Result := ad;
    596             exit;
     596            Exit;
    597597          end;
    598598        end
     
    602602        begin
    603603          Result := ad;
    604           exit;
     604          Exit;
    605605        end;
    606606      end;
     
    618618    begin // go for future techs
    619619      Result := -1;
    620       i := 0;
     620      I := 0;
    621621      for ad := nAdv - 4 to nAdv - 1 do
    622622        if (RO.Tech[ad] < MaxFutureTech) and (RO.Tech[AdvPreq[ad, 0]] >=
    623623          tsApplicable) then
    624624        begin
    625           Inc(i);
    626           if random(i) = 0 then
     625          Inc(I);
     626          if random(I) = 0 then
    627627            Result := ad;
    628628        end;
    629       assert((Result < 0) or AdvanceResearchable(Result));
    630       exit;
    631     end;
    632 
    633   assert(NewResearch >= 0);
    634   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);
    635635  MarkEntry(NewResearch);
    636636  Result := -1;
     
    638638    if Entry[ad] and ((Result < 0) or (Advancedness[ad] > Advancedness[Result])) then
    639639      Result := ad;
    640   assert(Result >= 0);
     640  Assert(Result >= 0);
    641641end;
    642642
    643 function TAI.ChooseStealAdvance: integer;
     643function TAI.ChooseStealAdvance: Integer;
    644644var
    645   ad: integer;
     645  ad: Integer;
    646646begin
    647647  Result := -1;
     
    659659  twpAllowFarmland = $0001;
    660660
    661 procedure TAI.TileWorkPlan(Loc, cix: integer; var Value, NextJob, TotalWork: integer);
     661procedure TAI.TileWorkPlan(Loc, cix: Integer; var Value, NextJob, TotalWork: Integer);
    662662var
    663   OldTile, TerrType: cardinal;
     663  OldTile, TerrType: Cardinal;
    664664  TileInfo: TTileInfo;
    665665begin
     
    669669  begin
    670670    Value := 3 * 8 - 1;
    671     exit;
     671    Exit;
    672672  end; // better than any tile with 2 food
    673673
     
    690690      Map[Loc] := Map[Loc] and not fTerrain or fGrass;
    691691      TerrType := fGrass;
    692       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);
    693693    end
    694694    else if IsResearched(adExplosives) and
     
    701701      Map[Loc] := Map[Loc] and not fTerrain or fGrass;
    702702      TerrType := fGrass;
    703       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);
    704704    end;
    705705    if (Terrain[TerrType].MineEff > 0) and (RO.Government <> gDespotism) then
     
    764764    end;
    765765  end;
    766   Server(sGetTileInfo, me, Loc, TileInfo);
     766  Server(sGetTileInfo, Me, Loc, TileInfo);
    767767  Value := TileInfo.Food * 8 + TileInfo.Prod * 2 + TileInfo.Trade;
    768768  Map[Loc] := OldTile;
     
    772772procedure TAI.ProcessSettlers;
    773773var
    774   i, uix, cix, ecix, dtr, Loc, RadiusLoc, Special, Food, Prod, Trade,
     774  I, uix, cix, ecix, dtr, Loc, RadiusLoc, Special, Food, Prod, Trade,
    775775  CityFood, Happy, TestScore, BestNearCityScore, BestUnusedValue,
    776   BestUnusedLoc, Value, NextJob, TotalWork, V21, part, Loc1: integer;
    777   Tile: cardinal;
    778   FoodOk, Started: boolean;
     776  BestUnusedLoc, Value, NextJob, TotalWork, V21, part, Loc1: Integer;
     777  Tile: Cardinal;
     778  FoodOk, Started: Boolean;
    779779  Radius: TVicinity21Loc;
    780780  CityAreaInfo: TCityAreaInfo;
    781   TileFood, ResourceScore, CityScore: array[0..lxmax * lymax - 1] of integer;
    782 
    783   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);
    784784  // set Score=1 for low-priority jobs
    785785  begin
     
    789789  end;
    790790
    791   procedure ReserveCityRadius(Loc: integer);
     791  procedure ReserveCityRadius(Loc: Integer);
    792792  var
    793     V21, RadiusLoc: integer;
     793    V21, RadiusLoc: Integer;
    794794    Radius: TVicinity21Loc;
    795795  begin
     
    808808  procedure ScoreRoadConnections;
    809809  var
    810     V8, nFragments, Loc, Loc1, History, RoadScore, a, b, FullyDeveloped,
    811     ConnectMask: integer;
    812     BridgeOk: boolean;
     810    V8, nFragments, Loc, Loc1, History, RoadScore, A, B, FullyDeveloped,
     811    ConnectMask: Integer;
     812    BridgeOk: Boolean;
    813813    Adjacent: TVicinity8Loc;
    814814  begin
     
    821821      if ((1 shl (Map[Loc] and fTerrain)) and (1 shl fOcean or 1 shl
    822822        fShore or 1 shl fDesert or 1 shl fArctic or 1 shl fUNKNOWN) = 0) and
    823         (RO.Territory[Loc] = me) and (Map[Loc] and FullyDeveloped = 0) and
     823        (RO.Territory[Loc] = Me) and (Map[Loc] and FullyDeveloped = 0) and
    824824        (BridgeOk or (Map[Loc] and fRiver = 0)) then
    825825      begin
     
    835835          Loc1 := Adjacent[V8 and 7];
    836836          History := History shl 1;
    837           if (Loc1 >= 0) and (RO.Territory[Loc1] = me) and
     837          if (Loc1 >= 0) and (RO.Territory[Loc1] = Me) and
    838838            (Map[Loc1] and ConnectMask <> 0) then
    839839          begin
     
    851851                  else if History and 4 <> 0 then
    852852                  begin
    853                     V8_to_ab((V8 - 1) and 7, a, b);
    854                     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);
    855855                    if (Loc1 >= 0) and (Map[Loc1] and ConnectMask <> 0) then
    856856                      Dec(nFragments);
     
    876876
    877877begin
    878   fillchar(SettlerSurplus, sizeof(SettlerSurplus), 0);
     878  FillChar(SettlerSurplus, SizeOf(SettlerSurplus), 0);
    879879  JobAssignment_Initialize;
    880880
    881881  if (Data.BehaviorFlags and bBarbarina = 0) or (RO.nCity < 3) then
    882882  begin
    883     fillchar(TileFood, sizeof(TileFood), 0);
    884     fillchar(ResourceScore, sizeof(ResourceScore), 0);
     883    FillChar(TileFood, SizeOf(TileFood), 0);
     884    FillChar(ResourceScore, SizeOf(ResourceScore), 0);
    885885    for Loc := 0 to MapSize - 1 do
    886886      if Map[Loc] and fTerrain <> fUNKNOWN then
     
    923923
    924924    // rate possible new cities
    925     fillchar(CityScore, MapSize * sizeof(integer), 0);
     925    FillChar(CityScore, MapSize * SizeOf(Integer), 0);
    926926    for Loc := 0 to MapSize - 1 do
    927927    begin
     
    930930        ((RO.Government <> gDespotism) or (Map[Loc] and fSpecial = fSpecial1)) or
    931931        (Map[Loc] and (fTerrain or fSpecial) = fPrairie or fSpecial1));
    932       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
    933933      begin
    934934        TestScore := 0;
     
    950950        if CityFood >= MinCityFood then // city is worth founding
    951951        begin
    952           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;
    953953          // some unexactness, random but always the same for this tile
    954954          if TestScore > BestNearCityScore then
     
    988988              if not (Map[RadiusLoc] and fTerrain in [fDesert, fArctic]) then
    989989              begin
    990                 assert(RadiusLoc >= 0);
     990                Assert(RadiusLoc >= 0);
    991991                TileWorkPlan(RadiusLoc, cix, Value, NextJob, TotalWork);
    992992                if (NextJob = jRoad) and (Built[imPalace] +
     
    10001000            begin // tile could be exploited
    10011001              RadiusLoc := Radius[V21];
    1002               assert(RadiusLoc >= 0);
     1002              Assert(RadiusLoc >= 0);
    10031003              if not (Map[RadiusLoc] and fTerrain in [fDesert, fArctic]) then
    10041004              begin
     
    10241024  if Data.BehaviorFlags and bBarbarina = 0 then // low priority jobs
    10251025    for Loc := 0 to MapSize - 1 do
    1026       if RO.Territory[Loc] = me then
     1026      if RO.Territory[Loc] = Me then
    10271027      begin
    10281028        Tile := Map[Loc];
     
    10481048  begin
    10491049    for part := 0 to nShipPart - 1 do
    1050       for i := 0 to ColonyShipPlan[part].nLocFoundCity - 1 do
    1051       begin
    1052         Loc := ColonyShipPlan[part].LocFoundCity[i];
     1050      for I := 0 to ColonyShipPlan[part].nLocFoundCity - 1 do
     1051      begin
     1052        Loc := ColonyShipPlan[part].LocFoundCity[I];
    10531053        Started := False;
    10541054        for uix := 0 to RO.nUn - 1 do
     
    10561056          begin
    10571057            Started := True;
    1058             break;
     1058            Break;
    10591059          end;
    10601060        if not Started then
     
    11191119            begin // settlers could be added to this city
    11201120              Happy := BasicHappy;
    1121               for i := 0 to nWonder - 1 do
    1122                 if Built[i] > 0 then
     1121              for I := 0 to nWonder - 1 do
     1122                if Built[I] > 0 then
    11231123                  Inc(Happy);
    11241124              if Built[imTemple] > 0 then
     
    11271127              begin
    11281128                Inc(Happy, 2);
    1129                 if RO.Wonder[woBach].EffectiveOwner = me then
     1129                if RO.Wonder[woBach].EffectiveOwner = Me then
    11301130                  Inc(Happy, 1);
    11311131              end;
     
    11501150procedure TAI.DoTurn;
    11511151var
    1152   emix, i, p1, TaxSum, ScienceSum, NewTaxRate: integer;
    1153   AllHateMe: boolean;
     1152  emix, I, p1, TaxSum, ScienceSum, NewTaxRate: Integer;
     1153  AllHateMe: Boolean;
    11541154{$IFDEF PERF}
    11551155  PF, t0, t1, t2, t3, t4, t5, t6, t7, t8, t9: int64;
     
    11571157begin
    11581158{$IFDEF DEBUG}
    1159   fillchar(DebugMap, sizeof(DebugMap), 0);
     1159  FillChar(DebugMap, SizeOf(DebugMap), 0);
    11601160{$ENDIF}
    11611161
     
    11691169  WarNations := PresenceUnknown;
    11701170  for p1 := 0 to nPl - 1 do
    1171     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
    11721172      Inc(WarNations, 1 shl p1);
    11731173  BombardingNations := 0;
     
    11851185    CheckGender;
    11861186
    1187   if G.Difficulty[me] < MaxDiff then // not on beginner level
     1187  if G.Difficulty[Me] < MaxDiff then // not on beginner level
    11881188  begin
    11891189    if (Data.LastResearchTech = adHorsebackRiding) and (RO.ResearchTech < 0) and
    11901190      (random(6) = 0) and (HavePort or (ContinentPresence[0] and not
    1191       (1 shl me or PresenceUnknown) <> 0)) then
     1191      (1 shl Me or PresenceUnknown) <> 0)) then
    11921192    begin
    11931193      Data.BehaviorFlags := Data.BehaviorFlags or bBarbarina_Hide;
     
    12061206          begin
    12071207            AllHateMe := False;
    1208             break;
     1208            Break;
    12091209          end;
    12101210      if AllHateMe then
     
    13021302  else
    13031303  begin
    1304     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
    13051305      NewTaxRate := RO.TaxRate // don't check decreasing tax
    13061306    else
     
    13091309    begin
    13101310      SumCities(NewTaxRate, TaxSum, ScienceSum);
    1311       if RO.Money + TaxSum >= (TotalPopulation[me] - 4) then
    1312         break; // enough
     1311      if RO.Money + TaxSum >= (TotalPopulation[Me] - 4) then
     1312        Break; // enough
    13131313      Inc(NewTaxRate, 10);
    13141314    end;
     
    13241324    // research completed
    13251325    for p1 := 0 to nPl - 1 do
    1326       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
    13271327        (RO.EnemyReport[p1].TurnOfCivilReport + TechReportOutdated > RO.Turn) and
    13281328        (RO.EnemyReport[p1].Tech[Data.LastResearchTech] < tsSeen) then
    13291329      begin // latest researched advance might be of interest to this nation
    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;
     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;
    13341334      end;
    13351335  if RO.ResearchTech = adMilitary then
     
    13371337  else
    13381338    Data.LastResearchTech := RO.ResearchTech;
    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;
     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;
    13431343
    13441344  // prepare negotiation
     
    13481348{$IFDEF DEBUG}
    13491349(*for p1:=0 to nPl-1 do
    1350   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)
    13511351    and (RO.EnemyReport[p1].TurnOfCivilReport>=0) then
    13521352    TraceAdvanceValues(p1);*)
     
    13631363
    13641364{$IFDEF DEBUG}
    1365 procedure TAI.TraceAdvanceValues(Nation: integer);
     1365procedure TAI.TraceAdvanceValues(Nation: Integer);
    13661366var
    1367   ad: integer;
     1367  ad: Integer;
    13681368begin
    13691369  for ad := 0 to nAdv - 1 do
     
    13791379procedure TAI.CheckGender;
    13801380var
    1381   p1, NewGender: integer;
     1381  p1, NewGender: Integer;
    13821382begin
    13831383  NewGender := -1;
    13841384  for p1 := 0 to nPl - 1 do
    1385     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
    13861386      (RO.Treaty[p1] >= trFriendlyContact) then
    1387       if PlayerHash[me] > PlayerHash[p1] then
     1387      if PlayerHash[Me] > PlayerHash[p1] then
    13881388      begin
    13891389        if NewGender = bMale then
    13901390        begin
    13911391          NewGender := -2;
    1392           break;
     1392          Break;
    13931393        end; // ambiguous, don't change gender
    13941394        NewGender := bFemale;
     
    13991399        begin
    14001400          NewGender := -2;
    1401           break;
     1401          Break;
    14021402        end; // ambiguous, don't change gender
    14031403        NewGender := bMale;
     
    14121412procedure TAI.SetAdvanceValues;
    14131413
    1414   procedure RateResearchAdv(ad, Time: integer);
     1414  procedure RateResearchAdv(ad, Time: Integer);
    14151415  var
    1416     Value: integer;
     1416    Value: Integer;
    14171417  begin
    14181418    if Time = 0 then
     
    14241424  end;
    14251425
    1426   procedure SetPreqValues(ad, Value: integer);
     1426  procedure SetPreqValues(ad, Value: Integer);
    14271427  begin
    14281428    if (RO.Tech[ad] < tsSeen) and (ad <> RO.ResearchTech) then
     
    14471447  end;
    14481448
    1449   procedure RateImpPreq(iix, Value: integer);
     1449  procedure RateImpPreq(iix, Value: Integer);
    14501450  begin
    14511451    if (Value > 0) and (Imp[iix].Preq >= 0) then
     
    14541454
    14551455var
    1456   emix, cix, adMissing, iad, ad, Count, i, Time, d, CurrentCost,
    1457   CurrentStrength, MaxSize, MaxTrade: integer;
    1458   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;
    14591459begin
    14601460  if AdvanceValuesSet then
    1461     exit;
     1461    Exit;
    14621462  AdvanceValuesSet := True;
    14631463
    1464   fillchar(AdvanceValue, sizeof(AdvanceValue), 0);
     1464  FillChar(AdvanceValue, SizeOf(AdvanceValue), 0);
    14651465
    14661466  // rate techs to ensure research progress
     
    14821482      begin // 2 of 3 required
    14831483        Count := 0;
    1484         for i := 0 to 2 do
    1485           if (AdvPreq[ad, i] = RO.ResearchTech) or
    1486             (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
    14871487            Inc(Count);
    14881488        if Count >= 2 then
     
    14921492          if ad <> adMassProduction then // don't score third preq for MP
    14931493          begin
    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);
     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);
    14981498          end;
    14991499          Inc(Time, 2 - Count);
     
    15031503      begin
    15041504        Count := 0;
    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
     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
    15081508          begin
    1509             RateResearchAdv(AdvPreq[ad, i], Time);
     1509            RateResearchAdv(AdvPreq[ad, I], Time);
    15101510            Inc(Count);
    15111511          end;
     
    15361536
    15371537  // rate military techs
    1538   for d := 0 to nDomains - 1 do
     1538  for D := 0 to nDomains - 1 do
    15391539  begin
    15401540    CurrentCost := 0;
    15411541    CurrentStrength := 0;
    15421542    for PreView := True downto False do
    1543       for i := 0 to nUpgrade - 1 do
    1544         with Upgrade[d, i] do
     1543      for I := 0 to nUpgrade - 1 do
     1544        with Upgrade[D, I] do
    15451545          if (Preq >= 0) and not (Preq in FutureTech) then
    15461546            if ((Ro.ResearchTech = Preq) or (RO.Tech[Preq] >= tsSeen)) = PreView then
     
    15531553              else
    15541554              begin // rate
    1555                 if (i > 0) and (Trans > 0) then
     1555                if (I > 0) and (Trans > 0) then
    15561556                  Inc(AdvanceValue[Preq], $400);
    15571557                if Cost <= CurrentCost then
    1558                   Inc(AdvanceValue[Preq], (4 - d) * Strength * $400 div
    1559                     (CurrentStrength + Upgrade[d, 0].Strength))
     1558                  Inc(AdvanceValue[Preq], (4 - D) * Strength * $400 div
     1559                    (CurrentStrength + Upgrade[D, 0].Strength))
    15601560                else
    1561                   Inc(AdvanceValue[Preq], (4 - d) * Strength * $200 div
    1562                     (CurrentStrength + Upgrade[d, 0].Strength));
     1561                  Inc(AdvanceValue[Preq], (4 - D) * Strength * $200 div
     1562                    (CurrentStrength + Upgrade[D, 0].Strength));
    15631563              end;
    15641564  end;
     
    16401640procedure TAI.AnalyzeMap;
    16411641var
    1642   cix, Loc, Loc1, V8, f1, p1: integer;
     1642  cix, Loc, Loc1, V8, f1, p1: Integer;
    16431643  Adjacent: TVicinity8Loc;
    16441644begin
     
    16461646
    16471647  // collect nation presence information for continents and oceans
    1648   fillchar(ContinentPresence, sizeof(ContinentPresence), 0);
    1649   fillchar(OceanPresence, sizeof(OceanPresence), 0);
     1648  FillChar(ContinentPresence, SizeOf(ContinentPresence), 0);
     1649  FillChar(OceanPresence, SizeOf(OceanPresence), 0);
    16501650  for Loc := 0 to MapSize - 1 do
    16511651  begin
     
    16991699  end;
    17001700
    1701   fillchar(TotalPopulation, sizeof(TotalPopulation), 0);
    1702   fillchar(ContinentPopulation, sizeof(ContinentPopulation), 0);
    1703   fillchar(DistrictPopulation, 4 * nDistrict, 0);
     1701  FillChar(TotalPopulation, SizeOf(TotalPopulation), 0);
     1702  FillChar(ContinentPopulation, SizeOf(ContinentPopulation), 0);
     1703  FillChar(DistrictPopulation, 4 * nDistrict, 0);
    17041704
    17051705  // count population
     
    17161716      if Loc >= 0 then
    17171717      begin
    1718         Inc(TotalPopulation[me], Size);
    1719         assert(District[Loc] >= 0);
     1718        Inc(TotalPopulation[Me], Size);
     1719        Assert(District[Loc] >= 0);
    17201720        if District[Loc] < maxCOD then
    17211721          Inc(DistrictPopulation[District[Loc]], Size);
     
    17251725procedure TAI.CollectModelCatStat;
    17261726var
    1727   i, uix, Cat, mix, Quality: integer;
     1727  I, uix, Cat, mix, Quality: Integer;
    17281728begin
    17291729  // categorize models
     
    17711771        if (Loc >= 0) and (mix = mixCruiser) and (Map[Loc] and fTerrain < fGrass) then
    17721772        begin
    1773           i := Formation[Loc];
    1774           if (i >= 0) and (i < maxCOD) then
    1775             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);
    17761776        end;
    17771777end;
     
    17801780const
    17811781  PatrolDestination = lxmax * lymax;
    1782   FirstSurplusLoop: array[mctGroundDefender..mctGroundAttacker] of integer = (2, 1);
     1782  FirstSurplusLoop: array[mctGroundDefender..mctGroundAttacker] of Integer = (2, 1);
    17831783var
    1784   Cat, i, mix, cix, uix, Loop, nModelOrder: integer;
     1784  Cat, I, mix, cix, uix, Loop, nModelOrder: Integer;
    17851785  Adjacent: TVicinity8Loc;
    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;
     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;
    17931793  var
    1794     Loc1, V8: integer;
     1794    Loc1, V8: Integer;
    17951795    Adjacent: TVicinity8Loc;
    17961796  begin
     
    18091809          begin
    18101810            Result := True;
    1811             exit;
     1811            Exit;
    18121812          end;
    18131813        end;
     
    18151815  end;
    18161816
    1817   procedure TryUtilize(uix: integer);
     1817  procedure TryUtilize(uix: Integer);
    18181818  var
    1819     cix, ProdCost, UtilizeCost: integer;
     1819    cix, ProdCost, UtilizeCost: Integer;
    18201820  begin
    18211821    if (MyUnit[uix].Health = 100) and (Map[MyUnit[uix].Loc] and
     
    18291829          UtilizeCost := MyModel[MyUnit[uix].mix].Cost;
    18301830          if Prod < (ProdCost - UtilizeCost * 2 div 3) *
    1831             BuildCostMod[G.Difficulty[me]] div 12 then
     1831            BuildCostMod[G.Difficulty[Me]] div 12 then
    18321832            Unit_Disband(uix);
    18331833        end;
     
    18351835  end;
    18361836
    1837   procedure FindDestination(uix: integer);
     1837  procedure FindDestination(uix: Integer);
    18381838  var
    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);
     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);
    18431843    Pile.Create(MapSize);
    18441844    with MyUnit[uix] do
     
    18541854        if (District[Loc1] >= 0) and (District[Loc1] < maxCOD) then
    18551855        begin
    1856           assert(DistrictNeed[District[Loc1]] > 0);
     1856          Assert(DistrictNeed[District[Loc1]] > 0);
    18571857          Dec(DistrictNeed[District[Loc1]]);
    18581858        end;
    18591859        Destination[uix] := Loc1;
    1860         break;
     1860        Break;
    18611861      end;
    18621862      Reached[Loc1] := True;
     
    18651865      begin
    18661866        NextLoc := Adjacent[V8];
    1867         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
    18681868          case CheckStep(MoveStyle, Time, V8 and 1, NextTime, RecoverTurns,
    18691869              Map[Loc1], Map[NextLoc], False) of
     
    18731873              Reached[NextLoc] := True; // don't check moving there again
    18741874            csCheckTerritory:
    1875               assert(False);
     1875              Assert(False);
    18761876          end;
    18771877      end;
     
    18871887          Unit_Disband(uix);
    18881888
    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;
     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;
    18941894  for uix := 0 to RO.nUn - 1 do
    18951895    if (MyUnit[uix].mix = mixMilitia) or (MyUnit[uix].mix = mixCruiser) then
     
    19031903      if ModelCat[mix] = Cat then
    19041904      begin
    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;
     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;
    19121912        Inc(nModelOrder);
    19131913      end;
     
    19221922              TryUtilize(uix);
    19231923
    1924       fillchar(LocNeed, MapSize, 0);
    1925       fillchar(DistrictNeed, sizeof(DistrictNeed), 0);
     1924      FillChar(LocNeed, MapSize, 0);
     1925      FillChar(DistrictNeed, SizeOf(DistrictNeed), 0);
    19261926
    19271927      for cix := 0 to RO.nCity - 1 do
     
    19461946        for uix := 0 to RO.nUn - 1 do
    19471947          with MyUnit[uix] do
    1948             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
    19491949            begin
    19501950              LocNeed[Loc] := 1;
     
    19541954
    19551955      complete := Loop >= FirstSurplusLoop[Cat];
    1956       for i := nModelOrder - 1 downto 0 do
     1956      for I := nModelOrder - 1 downto 0 do
    19571957      begin
    19581958        for Fortified := True downto False do
    19591959          for uix := 0 to RO.nUn - 1 do
    19601960            with MyUnit[uix] do
    1961               if (mix = ModelOrder[i]) and (Loc >= 0) and
     1961              if (mix = ModelOrder[I]) and (Loc >= 0) and
    19621962                (Destination[uix] < 0) and (Master < 0) and
    19631963                ((Flags and unFortified <> 0) = Fortified) and (LocNeed[Loc] > 0) then
     
    19721972        for uix := 0 to RO.nUn - 1 do
    19731973          with MyUnit[uix] do
    1974             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
    19751975              (Master < 0) then
    19761976              if (District[Loc] >= 0) and (District[Loc] < maxCOD) and
     
    19891989  // distribute obsolete settlers
    19901990  repeat
    1991     fillchar(LocNeed, MapSize, 0);
    1992     fillchar(DistrictNeed, sizeof(DistrictNeed), 0);
     1991    FillChar(LocNeed, MapSize, 0);
     1992    FillChar(DistrictNeed, SizeOf(DistrictNeed), 0);
    19931993
    19941994    for cix := 0 to RO.nCity - 1 do
     
    20402040  for uix := 0 to RO.nUn - 1 do
    20412041    with MyUnit[uix] do
    2042       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
    20432043        (District[Loc] < maxCOD) and (ModelQuality[mix] > 0) then
    20442044        case ModelCat[mix] of
     
    20482048end;
    20492049
    2050 procedure TAI.CheckAttack(uix: integer);
     2050procedure TAI.CheckAttack(uix: Integer);
    20512051var
    20522052  AttackScore, BestCount, AttackLoc, TestLoc, NextLoc, TestTime, V8,
    20532053  TestScore, euix, MyDamage, EnemyDamage, OldLoc, AttackForecast,
    2054   MoveResult, AttackResult, MoveStyle, NextTime, RecoverTurns: integer;
    2055   Tile: cardinal;
    2056   Exhausted: boolean;
     2054  MoveResult, AttackResult, MoveStyle, NextTime, RecoverTurns: Integer;
     2055  Tile: Cardinal;
     2056  Exhausted: Boolean;
    20572057  Adjacent: TVicinity8Loc;
    2058   Reached: array[0..lxmax * lymax - 1] of boolean;
     2058  Reached: array[0..lxmax * lymax - 1] of Boolean;
    20592059
    20602060begin
     
    20652065      AttackScore := -999999;
    20662066      AttackLoc := -1;
    2067       fillchar(Reached, MapSize, False);
     2067      FillChar(Reached, MapSize, False);
    20682068      Pile.Create(MapSize);
    20692069      Pile.Put(Loc, $800 - Movement);
     
    20772077        if ((Tile and fUnit) <> 0) and ((Tile and fOwned) = 0) then
    20782078        begin // enemy unit
    2079           assert(TestTime < $1000);
     2079          Assert(TestTime < $1000);
    20802080          Unit_FindEnemyDefender(TestLoc, euix);
    20812081          if RO.Treaty[RO.EnemyUn[euix].Owner] < trPeace then
     
    21682168end;
    21692169
    2170 procedure TAI.Patrol(uix: integer);
     2170procedure TAI.Patrol(uix: Integer);
    21712171const
    21722172  DistanceScore = 4;
    21732173var
    21742174  PatrolScore, BestCount, PatrolLoc, TestLoc, NextLoc, TestTime, V8,
    2175   TestScore, OldLoc, MoveResult, MoveStyle, NextTime, RecoverTurns: integer;
    2176   Tile: cardinal;
    2177   Exhausted, CaptureOnly: boolean;
     2175  TestScore, OldLoc, MoveResult, MoveStyle, NextTime, RecoverTurns: Integer;
     2176  Tile: Cardinal;
     2177  Exhausted, CaptureOnly: Boolean;
    21782178  Adjacent: TVicinity8Loc;
    2179   AdjacentUnknown: array[0..lxmax * lymax - 1] of shortint;
     2179  AdjacentUnknown: array[0..lxmax * lymax - 1] of ShortInt;
    21802180
    21812181begin
     
    21962196          // assume a score of 50 is the best achievable
    21972197          or CaptureOnly and (TestTime >= $1000) then
    2198           break;
     2198          Break;
    21992199
    22002200        TestScore := 0;
     
    22762276const
    22772277  nAttackCatOrder = 3;
    2278   AttackCatOrder: array[0..nAttackCatOrder - 1] of integer =
     2278  AttackCatOrder: array[0..nAttackCatOrder - 1] of Integer =
    22792279    (mctGroundAttacker, mctCruiser, mctGroundDefender);
    22802280var
    2281   iCat, uix, uix1: integer;
    2282   IsPatrolUnit, Fortified: boolean;
     2281  iCat, uix, uix1: Integer;
     2282  IsPatrolUnit, Fortified: Boolean;
    22832283begin
    22842284  for uix := 0 to RO.nUn - 1 do
     
    22992299              CheckAttack(uix);
    23002300
    2301   fillchar(uixPatrol, sizeof(uixPatrol), $FF);
     2301  FillChar(uixPatrol, SizeOf(uixPatrol), $FF);
    23022302  for uix := 0 to RO.nUn - 1 do
    23032303    with MyUnit[uix], MyModel[mix] do
     
    23332333end;
    23342334
    2335 function TAI.HavePort: boolean;
     2335function TAI.HavePort: Boolean;
    23362336var
    2337   V8, cix, AdjacentLoc, f: integer;
     2337  V8, cix, AdjacentLoc, F: Integer;
    23382338  Adjacent: TVicinity8Loc;
    23392339begin
     
    23492349          if (AdjacentLoc >= 0) and ((Map[AdjacentLoc] and fTerrain) < fGrass) then
    23502350          begin
    2351             f := Formation[AdjacentLoc];
    2352             if (f >= 0) and (f < maxCOD) and (OceanPresence[f] and
    2353               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
    23542354              Result := True;
    23552355          end;
     
    23612361var
    23622362  uix, cix, iix, dtr, V8, V21, NewImprovement, AdjacentLoc, MaxSettlers,
    2363   maxcount, cixMilAcademy: integer;
    2364   TerrType: cardinal;
     2363  maxcount, cixMilAcademy: Integer;
     2364  TerrType: Cardinal;
    23652365  IsPort, IsNavalBase, NeedCruiser, CheckProd, Destructed, ProduceSettlers,
    2366   ProduceMil: boolean;
     2366  ProduceMil: Boolean;
    23672367  Adjacent: TVicinity8Loc;
    23682368  Radius: TVicinity21Loc;
    23692369  Report: TCityReport;
    2370   HomeCount, CityProdRep: array[0..nCmax - 1] of integer;
    2371   MilProdCity: array[0..nCmax - 1] of boolean;
    2372 
    2373   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);
    23742374  begin
    23752375    if (NewImprovement = imTrGoods) // not already improvement of higher priority found
     
    23812381  end;
    23822382
    2383   procedure TryDestruct(Improvement: integer);
     2383  procedure TryDestruct(Improvement: Integer);
    23842384  begin
    23852385    if Destructed or (MyCity[cix].Built[Improvement] = 0) then
    2386       exit;
     2386      Exit;
    23872387    if City_CurrentImprovementProject(cix) >= 0 then
    23882388      City_RebuildImprovement(cix, Improvement)
     
    23922392      and (Imp[CurrentImprovementProject].Kind in [ikCommon,ikNatGlobal,ikNatLocal])
    23932393      and ((Imp[CurrentImprovementProject].Cost*3-Imp[Improvement].Cost*2)
    2394       *BuildCostMod[G.Difficulty[me]]>MyCity[cix].Prod*(12*3)) then}
     2394      *BuildCostMod[G.Difficulty[Me]]>MyCity[cix].Prod*(12*3)) then}
    23952395    Destructed := True;
    23962396  end;
    23972397
    2398   function ChooseBuildModel(Cat: integer): integer;
     2398  function ChooseBuildModel(Cat: Integer): Integer;
    23992399  var
    2400     Count, mix: integer;
     2400    Count, mix: Integer;
    24012401  begin
    24022402    Count := 0;
     
    24092409          Result := mix;
    24102410      end;
    2411     assert(Count > 0);
     2411    Assert(Count > 0);
    24122412  end;
    24132413
     
    24152415  // find military production cities
    24162416  var
    2417     cix, Total, d, Threshold, NewThreshold, Share, SharePlus, cixWorst: integer;
    2418   begin
    2419     fillchar(MilProdCity, RO.nCity, 0);
     2417    cix, Total, D, Threshold, NewThreshold, Share, SharePlus, cixWorst: Integer;
     2418  begin
     2419    FillChar(MilProdCity, RO.nCity, 0);
    24202420    GetCityProdPotential;
    2421     for d := 0 to maxCOD - 1 do
     2421    for D := 0 to maxCOD - 1 do
    24222422    begin
    24232423      Total := 0;
    24242424      for cix := 0 to RO.nCity - 1 do
    24252425        with MyCity[cix] do
    2426           if (Loc >= 0) and (District[Loc] = d) then
     2426          if (Loc >= 0) and (District[Loc] = D) then
    24272427            Total := Total + CityResult[cix];
    24282428      if Total = 0 then
     
    24332433      for cix := 0 to RO.nCity - 1 do
    24342434        with MyCity[cix] do
    2435           if (Loc >= 0) and (District[Loc] = d) and
     2435          if (Loc >= 0) and (District[Loc] = D) and
    24362436            (Built[imBarracks] + Built[imMilAcademy] > 0) then
    24372437          begin
     
    24482448        for cix := 0 to RO.nCity - 1 do
    24492449          with MyCity[cix] do
    2450             if (Loc >= 0) and (District[Loc] = d) and
     2450            if (Loc >= 0) and (District[Loc] = D) and
    24512451              (Built[imBarracks] + Built[imMilAcademy] = 0) and
    24522452              (Built[imObservatory] = 0) and (CityResult[cix] < Threshold) and
     
    24652465      for cix := 0 to RO.nCity - 1 do
    24662466        with MyCity[cix] do
    2467           if (Loc >= 0) and (District[Loc] = d) and
     2467          if (Loc >= 0) and (District[Loc] = D) and
    24682468            (Built[imBarracks] + Built[imMilAcademy] = 0) and
    24692469            (CityResult[cix] >= Threshold) then
     
    24712471{    if (cixWorst>=0)
    24722472      and (Share-CityResult[cixWorst]*2>=Total*MilProdShare div 100) then
    2473       MilProdCity[cixWorst]:=false;}
     2473      MilProdCity[cixWorst]:=False;}
    24742474    end;
    24752475
     
    24782478    if cixStateImp[imPalace] >= 0 then
    24792479    begin
    2480       d := District[MyCity[cixStateImp[imPalace]].Loc];
    2481       if (d >= 0) and (d < maxCOD) then
     2480      D := District[MyCity[cixStateImp[imPalace]].Loc];
     2481      if (D >= 0) and (D < maxCOD) then
    24822482      begin
    24832483        cixMilAcademy := -1;
    24842484        for cix := 0 to RO.nCity - 1 do
    24852485          with MyCity[cix] do
    2486             if (Loc >= 0) and (District[Loc] = d) and
     2486            if (Loc >= 0) and (District[Loc] = D) and
    24872487              (Built[imObservatory] + Built[imPalace] = 0) and
    24882488              ((cixMilAcademy < 0) or (CityResult[cix] > CityResult[cixMilAcademy])) then
     
    25002500  procedure ChangeHomeCities;
    25012501  var
    2502     uix, NewHome, HomeSupport, NewHomeSupport, SingleSupport: integer;
     2502    uix, NewHome, HomeSupport, NewHomeSupport, SingleSupport: Integer;
    25032503  begin
    25042504    if RO.Government in [gAnarchy, gFundamentalism] then
    2505       exit;
     2505      Exit;
    25062506    for uix := 0 to RO.nUn - 1 do
    25072507      with MyUnit[uix] do
     
    25512551
    25522552begin
    2553   fillchar(HomeCount, 4 * RO.nCity, 0);
     2553  FillChar(HomeCount, 4 * RO.nCity, 0);
    25542554  for uix := 0 to RO.nUn - 1 do
    25552555    with MyUnit[uix] do
     
    27442744                    begin
    27452745                      TryBuild(imHarbor);
    2746                       break;
     2746                      Break;
    27472747                    end;
    27482748                  end;
     
    27622762              TryBuild(imRecycling);
    27632763            if (Report.Trade - Report.Corruption >= 11) and
    2764               (RO.Money < TotalPopulation[me] * 2) then
     2764              (RO.Money < TotalPopulation[Me] * 2) then
    27652765              TryBuild(imBank);
    27662766            if (RO.NatBuilt[imStockEx] = 0) and
     
    27992799
    28002800          // rebuild imps no longer needed
    2801           if (RO.TaxRate = 0) and (RO.Money >= TotalPopulation[me] * 4) then
     2801          if (RO.TaxRate = 0) and (RO.Money >= TotalPopulation[Me] * 4) then
    28022802            TryDestruct(imBank)
    28032803          else if Report.Happy * 2 >= Size + 6 then
     
    28252825end;
    28262826
    2827 function TAI.ChooseGovernment: integer;
     2827function TAI.ChooseGovernment: Integer;
    28282828begin
    28292829  if Data.BehaviorFlags and bBarbarina <> 0 then
     
    28462846//-------------------------------
    28472847
    2848 function TAI.MostWanted(Nation, adGiveAway: integer): integer;
     2848function TAI.MostWanted(Nation, adGiveAway: Integer): Integer;
    28492849var
    2850   ad: integer;
     2850  ad: Integer;
    28512851begin
    28522852  Result := -1;
     
    28762876end;
    28772877
    2878 procedure TAI.FindBestTrade(Nation: integer; var adWanted, adGiveAway: integer);
     2878procedure TAI.FindBestTrade(Nation: Integer; var adWanted, adGiveAway: Integer);
    28792879var
    2880   i, ad, ead, adTestGiveAway: integer;
     2880  I, ad, ead, adTestGiveAway: Integer;
    28812881begin
    28822882  adWanted := -1;
     
    28882888    begin
    28892889      adTestGiveAway := -1;
    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
     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
    28932893          adTestGiveAway := -2; // already requested before
    28942894      if adTestGiveAway = -1 then
     
    29132913end;
    29142914
    2915 function TAI.WantNegotiation(Nation: integer; NegoTime: TNegoTime): boolean;
     2915function TAI.WantNegotiation(Nation: Integer; NegoTime: TNegoTime): Boolean;
    29162916var
    2917   p1, Count, adWanted, adGiveAway: integer;
     2917  p1, Count, adWanted, adGiveAway: Integer;
    29182918begin
    29192919  if Data.BehaviorFlags and bBarbarina = bBarbarina then
    29202920  begin
    29212921    Result := Barbarina_WantNegotiation(Nation, NegoTime);
    2922     exit;
     2922    Exit;
    29232923  end;
    29242924
     
    29282928    begin
    29292929      Result := False;
    2930       exit;
     2930      Exit;
    29312931    end;
    29322932    Count := 0;
    29332933    for p1 := 0 to nPl - 1 do
    2934       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
    29352935        Inc(Count);
    29362936    if Count >= 3 then // enough peace made
    29372937    begin
    29382938      Result := False;
    2939       exit;
     2939      Exit;
    29402940    end;
    29412941  end;
     
    29782978procedure TAI.DoNegotiation;
    29792979var
    2980   i, adWanted, adGiveAway, adToGet, Slot: integer;
    2981   BuildFreeOffer: boolean;
     2980  I, adWanted, adGiveAway, adToGet, Slot: Integer;
     2981  BuildFreeOffer: Boolean;
    29822982begin
    29832983  if MyLastAction = scDipOffer then
     
    30103010  begin
    30113011    Barbarina_DoNegotiation;
    3012     exit;
     3012    Exit;
    30133013  end;
    30143014
     
    30163016  begin
    30173017    Barbarina_DoCheckNegotiation;
    3018     exit;
     3018    Exit;
    30193019  end;
    30203020
     
    30313031      (OppoOffer.nDeliver + OppoOffer.nCost = 1) and
    30323032      (OppoOffer.Price[0] and opMask = opTreaty) and
    3033       (integer(OppoOffer.Price[0] - opTreaty) > RO.Treaty[Opponent]) and
     3033      (Integer(OppoOffer.Price[0] - opTreaty) > RO.Treaty[Opponent]) and
    30343034      ((OppoOffer.Price[0] - opTreaty < trAlliance) or
    30353035      (RO.Tech[adScience] >= tsSeen)) then
    30363036      MyAction := scDipAccept // accept all treaties
    30373037    else if (RO.Treaty[Opponent] >= trPeace) and (OppoOffer.nDeliver = 1) and
    3038       (OppoOffer.Price[0] and $FFFF0000 = opCivilReport + cardinal(Opponent) shl 16) and
     3038      (OppoOffer.Price[0] and $FFFF0000 = opCivilReport + Cardinal(Opponent) shl 16) and
    30393039      (OppoOffer.nCost = 1) and (OppoOffer.Price[1] and $FFFF0000 =
    3040       opCivilReport + cardinal(me) shl 16) then
     3040      opCivilReport + Cardinal(Me) shl 16) then
    30413041      MyAction := scDipAccept // accept exchange of civil reports
    30423042    else if (OppoOffer.nDeliver = 1) and (OppoOffer.nCost = 1) and
     
    30683068        adWanted := MostWanted(Opponent, OppoOffer.Price[1] - opTech);
    30693069        if (OppoOffer.Price[0] and opMask = opTech) and
    3070           (cardinal(adWanted) = OppoOffer.Price[0] - opTech) then
     3070          (Cardinal(adWanted) = OppoOffer.Price[0] - opTech) then
    30713071          MyAction := scDipAccept // opponent's offer is already perfect
    30723072        else if adWanted >= 0 then
     
    31243124        MyOffer.Price[1] := opTech + adWanted;
    31253125        MyAction := scDipOffer;
    3126         for i := 0 to nRequestedTechs - 1 do
    3127           if Data.RequestedTechs[i] < 0 then
     3126        for I := 0 to nRequestedTechs - 1 do
     3127          if Data.RequestedTechs[I] < 0 then
    31283128          begin
    3129             Slot := i;
    3130             break;
     3129            Slot := I;
     3130            Break;
    31313131          end
    3132           else if (i = 0) or (Data.RequestedTechs[i] shr 16 <
     3132          else if (I = 0) or (Data.RequestedTechs[I] shr 16 <
    31333133            Data.RequestedTechs[Slot] shr 16) then // find most outdated entry
    3134             Slot := i;
     3134            Slot := I;
    31353135        Data.RequestedTechs[Slot] := RO.Turn shl 16 + Opponent shl 8 + adWanted;
    31363136      end;
     
    31413141procedure SetLeaveOutValue;
    31423142
    3143   procedure Process(ad: integer);
     3143  procedure Process(ad: Integer);
    31443144  var
    3145     i: integer;
     3145    I: Integer;
    31463146  begin
    31473147    if LeaveOutValue[ad] < 0 then
    31483148    begin
    31493149      LeaveOutValue[ad] := 0;
    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);
     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);
    31563156        end;
    31573157    end;
     
    31593159
    31603160var
    3161   ad: integer;
     3161  ad: Integer;
    31623162begin
    31633163  FillChar(LeaveOutValue, SizeOf(LeaveOutValue), $FF);
     
    31683168
    31693169initialization
    3170   RWDataSize := sizeof(TPersistentData);
     3170  RWDataSize := SizeOf(TPersistentData);
    31713171  SetLeaveOutValue;
    31723172
  • trunk/AI/StdAI/Barbarina.pas

    r442 r447  
    2525type
    2626  TColonyShipPlan = array[0..nShipPart - 1] of record
    27     cixProducing: integer;
    28     LocResource: array[0..maxModern - 1] of integer;
    29     nLocResource: integer;
    30     LocFoundCity: array[0..maxModern - 1] of integer;
    31     nLocFoundCity: integer;
     27    cixProducing: Integer;
     28    LocResource: array[0..maxModern - 1] of Integer;
     29    nLocResource: Integer;
     30    LocFoundCity: array[0..maxModern - 1] of Integer;
     31    nLocFoundCity: Integer;
    3232  end;
    3333
    3434  TBarbarina = class(TToolAI)
    35     constructor Create(Nation: integer); override;
     35    constructor Create(Nation: Integer); override;
    3636
    3737  protected
    3838    ColonyShipPlan: TColonyShipPlan;
    39     function Barbarina_GoHidden: boolean; // whether we should prepare for barbarina mode
    40     function Barbarina_Go: boolean; // whether we should switch to barbarina mode now
     39    function Barbarina_GoHidden: Boolean; // whether we should prepare for barbarina mode
     40    function Barbarina_Go: Boolean; // whether we should switch to barbarina mode now
    4141    procedure Barbarina_DoTurn;
    4242    procedure Barbarina_SetCityProduction;
    43     function Barbarina_ChooseResearchAdvance: integer;
    44     function Barbarina_WantCheckNegotiation(Nation: integer): boolean;
     43    function Barbarina_ChooseResearchAdvance: Integer;
     44    function Barbarina_WantCheckNegotiation(Nation: Integer): Boolean;
    4545    procedure Barbarina_DoCheckNegotiation;
    46     function Barbarina_WantNegotiation(Nation: integer; NegoTime: TNegoTime): boolean;
     46    function Barbarina_WantNegotiation(Nation: Integer; NegoTime: TNegoTime): Boolean;
    4747    procedure Barbarina_DoNegotiation;
    4848    procedure MakeColonyShipPlan;
    4949
    5050  private
    51     TurnOfMapAnalysis, Neighbours: integer;
    52     ContinentPresence: array[0..maxCOD - 1] of integer;
    53     OceanPresence: array[0..maxCOD - 1] of integer;
    54     ContinentSize: array[0..maxCOD - 1] of integer;
    55     OceanSize: array[0..maxCOD - 1] of integer;
    56     mixBest: array[0..nModelCategory - 1] of integer;
     51    TurnOfMapAnalysis, Neighbours: Integer;
     52    ContinentPresence: array[0..maxCOD - 1] of Integer;
     53    OceanPresence: array[0..maxCOD - 1] of Integer;
     54    ContinentSize: array[0..maxCOD - 1] of Integer;
     55    OceanSize: array[0..maxCOD - 1] of Integer;
     56    mixBest: array[0..nModelCategory - 1] of Integer;
    5757    NegoCause: (CancelTreaty);
    58     function IsModelAvailable(rmix: integer): boolean;
     58    function IsModelAvailable(rmix: Integer): Boolean;
    5959    procedure FindBestModels;
    6060    procedure AnalyzeMap;
    61     procedure RateAttack(uix: integer);
    62     function DoAttack(uix, AttackLoc: integer): boolean;
    63     function ProcessMove(uix: integer): boolean;
     61    procedure RateAttack(uix: Integer);
     62    function DoAttack(uix, AttackLoc: Integer): Boolean;
     63    function ProcessMove(uix: Integer): Boolean;
    6464    procedure AttackAndPatrol;
    6565  end;
     
    7373type
    7474  TResearchModel = record
    75     Category, Domain, Weight, adStop, FutMStrength: integer;
    76     Upgrades: cardinal;
    77     Cap: array [0..nFeature - 1] of integer;
     75    Category, Domain, Weight, adStop, FutMStrength: Integer;
     76    Upgrades: Cardinal;
     77    Cap: array [0..nFeature - 1] of Integer;
    7878  end;
    7979
     
    9393
    9494  nResearchOrder = 40;
    95   ResearchOrder: array[0..nResearchOrder - 1] of integer =
     95  ResearchOrder: array[0..nResearchOrder - 1] of Integer =
    9696    (adBronzeWorking, -adMapMaking, adChivalry, adMonotheism, adIronWorking,
    9797    adGunPowder, adTheology, adConstruction, adCodeOfLaws, -adEngineering,
     
    171171
    172172var
    173   Moved: array[0..numax - 1] of boolean;
    174   UnitPresence: array[0..lxmax * lymax - 1] of byte;
     173  Moved: array[0..numax - 1] of Boolean;
     174  UnitPresence: array[0..lxmax * lymax - 1] of Byte;
    175175  euixMap: array[0..lxmax * lymax - 1] of smallint;
    176176  uixAttack: array[0..neumax - 1] of smallint;
    177   AttackScore: array[0..neumax - 1] of integer;
    178 
    179 constructor TBarbarina.Create(Nation: integer);
     177  AttackScore: array[0..neumax - 1] of Integer;
     178
     179constructor TBarbarina.Create(Nation: Integer);
    180180begin
    181181  inherited;
     
    184184
    185185// whether one of the existing models matches a specific research model
    186 function TBarbarina.IsModelAvailable(rmix: integer): boolean;
     186function TBarbarina.IsModelAvailable(rmix: Integer): Boolean;
    187187var
    188   i, mix, MStrength: integer;
     188  I, mix, MStrength: Integer;
    189189begin
    190190  Result := False;
     
    199199        Result := MStrength < (MyModel[mix].MStrength * 3) div 2;
    200200        // for future techs: don't count model available if 50% stronger possible
    201         for i := 0 to nFeature - 1 do
    202           if MyModel[mix].Cap[i] < Cap[i] then
     201        for I := 0 to nFeature - 1 do
     202          if MyModel[mix].Cap[I] < Cap[I] then
    203203          begin
    204204            Result := False;
    205             break;
     205            Break;
    206206          end;
    207207        if Result then
    208           break;
     208          Break;
    209209      end;
    210210  end;
    211211end;
    212212
    213 function TBarbarina.Barbarina_GoHidden: boolean;
     213function TBarbarina.Barbarina_GoHidden: Boolean;
    214214var
    215   V21, Loc1, cix: integer;
     215  V21, Loc1, cix: Integer;
    216216  Radius: TVicinity21Loc;
    217217begin
     
    238238end;
    239239
    240 function TBarbarina.Barbarina_Go: boolean;
     240function TBarbarina.Barbarina_Go: Boolean;
    241241begin
    242242  if IsResearched(adMassProduction) then
     
    249249    Result := (RO.nCity >= 3) and IsResearched(adMapMaking) and
    250250      IsModelAvailable(EntryModel_Base);
    251     exit;
     251    Exit;
    252252  end;
    253253  Result := Result and ((RO.nUn >= RO.nCity * 3) or
    254     (RO.Wonder[woZeus].EffectiveOwner = me));
     254    (RO.Wonder[woZeus].EffectiveOwner = Me));
    255255end;
    256256
    257257procedure TBarbarina.AnalyzeMap;
    258258var
    259   Loc, Loc1, V8, f1, p1, cix: integer;
     259  Loc, Loc1, V8, f1, p1, cix: Integer;
    260260  Adjacent: TVicinity8Loc;
    261261begin
    262262  if TurnOfMapAnalysis = RO.Turn then
    263     exit;
     263    Exit;
    264264
    265265  // inherited;
    266266
    267267  // collect nation presence information for continents and oceans
    268   fillchar(ContinentPresence, sizeof(ContinentPresence), 0);
    269   fillchar(OceanPresence, sizeof(OceanPresence), 0);
    270   fillchar(ContinentSize, sizeof(ContinentSize), 0);
    271   fillchar(OceanSize, sizeof(OceanSize), 0);
     268  FillChar(ContinentPresence, SizeOf(ContinentPresence), 0);
     269  FillChar(OceanPresence, SizeOf(OceanPresence), 0);
     270  FillChar(ContinentSize, SizeOf(ContinentSize), 0);
     271  FillChar(OceanSize, SizeOf(OceanSize), 0);
    272272  for Loc := 0 to MapSize - 1 do
    273273  begin
     
    339339procedure TBarbarina.FindBestModels;
    340340var
    341   i, mix, rmix, cat: integer;
     341  I, mix, rmix, cat: Integer;
    342342begin
    343   for i := 0 to nModelCategory - 1 do
    344     mixBest[i] := -1;
     343  for I := 0 to nModelCategory - 1 do
     344    mixBest[I] := -1;
    345345  for rmix := nResearchModel - 1 downto 0 do
    346346    with ResearchModel[rmix] do
     
    351351          begin
    352352            mixBest[Category] := mix;
    353             for i := 0 to nFeature - 1 do
    354               if MyModel[mix].Cap[i] < Cap[i] then
     353            for I := 0 to nFeature - 1 do
     354              if MyModel[mix].Cap[I] < Cap[I] then
    355355              begin
    356356                mixBest[Category] := -1;
    357                 break;
     357                Break;
    358358              end;
    359359            if mixBest[Category] >= 0 then
    360               break;
     360              Break;
    361361          end;
    362362  for mix := 3 to RO.nModel - 1 do
     
    387387      begin
    388388        mixBest[ctSeaTrans] := mix;
    389         break;
     389        Break;
    390390      end;
    391391end;
     
    406406
    407407// find one unit to destroy each known enemy unit, result in uixAttack
    408 procedure TBarbarina.RateAttack(uix: integer);
     408procedure TBarbarina.RateAttack(uix: Integer);
    409409var
    410410  MoveStyle, TestLoc, TestTime, NextLoc, NextTime, V8, RemHealth,
    411   RecoverTurns, Score, BestScore, euixBest, uixOld: integer;
    412   NextTile: cardinal;
     411  RecoverTurns, Score, BestScore, euixBest, uixOld: Integer;
     412  NextTile: Cardinal;
    413413  Adjacent: TVicinity8Loc;
    414414  Defense: ^TUnitInfo;
    415   Reached: array[0..lxmax * lymax - 1] of boolean;
     415  Reached: array[0..lxmax * lymax - 1] of Boolean;
    416416begin
    417417  with MyUnit[uix] do
     
    419419    begin
    420420      BestScore := 0;
    421       fillchar(Reached, MapSize, False);
     421      FillChar(Reached, MapSize, False);
    422422      MoveStyle := GetMyMoveStyle(mix, Health);
    423423      Pile.Create(MapSize);
     
    494494end;
    495495
    496 function TBarbarina.DoAttack(uix, AttackLoc: integer): boolean;
     496function TBarbarina.DoAttack(uix, AttackLoc: Integer): Boolean;
    497497  // AttackLoc=maNextCity means bombard only
    498498var
    499499  MoveResult, Kind, Temp, MoveStyle, TestLoc, TestTime, NextLoc,
    500   NextTime, V8, RecoverTurns, ecix: integer;
    501   NextTile: cardinal;
    502   AttackPositionReached, IsBombardment: boolean;
     500  NextTime, V8, RecoverTurns, ecix: Integer;
     501  NextTile: Cardinal;
     502  AttackPositionReached, IsBombardment: Boolean;
    503503  Adjacent: TVicinity8Loc;
    504   PreLoc: array[0..lxmax * lymax - 1] of word;
    505   Reached: array[0..lxmax * lymax - 1] of boolean;
     504  PreLoc: array[0..lxmax * lymax - 1] of Word;
     505  Reached: array[0..lxmax * lymax - 1] of Boolean;
    506506begin
    507507  Result := False;
     
    516516    else
    517517      Kind := 0;
    518     fillchar(Reached, MapSize, False);
     518    FillChar(Reached, MapSize, False);
    519519    AttackPositionReached := False;
    520520    MoveStyle := GetMyMoveStyle(mix, Health);
     
    524524    begin
    525525      if (TestTime >= $800) or (AttackLoc = maNextCity) and (TestTime > $800 - 100) then
    526         break;
     526        Break;
    527527      Reached[TestLoc] := True;
    528528      V8_to_Loc(TestLoc, Adjacent);
     
    537537          begin
    538538            City_FindEnemyCity(NextLoc, ecix);
    539             assert(ecix >= 0);
     539            Assert(ecix >= 0);
    540540            with RO.EnemyCity[ecix] do
    541541              if (Size > 2) and (Flags and ciCoastalFort = 0) then
     
    547547          begin
    548548            AttackPositionReached := True;
    549             break;
     549            Break;
    550550          end
    551551          else if not Reached[NextLoc] then
     
    572572      begin
    573573        PreLoc[NextLoc] := TestLoc;
    574         break;
     574        Break;
    575575      end;
    576576    end;
    577577    Pile.Free;
    578578    if not AttackPositionReached then
    579       exit;
     579      Exit;
    580580
    581581    TestLoc := AttackLoc;
     
    601601    begin
    602602      City_FindEnemyCity(AttackLoc, ecix);
    603       assert(ecix >= 0);
     603      Assert(ecix >= 0);
    604604      while (Movement >= 100) and (RO.EnemyCity[ecix].Size > 2) do
    605605        Unit_Step(uix, AttackLoc);
     
    611611end;
    612612
    613 function TBarbarina.ProcessMove(uix: integer): boolean;
     613function TBarbarina.ProcessMove(uix: Integer): Boolean;
    614614  // return true if no new enemy spotted
    615615const
     
    618618  PatrolScore, BestCount, PatrolLoc, TestLoc, NextLoc, TestTime, V8,
    619619  TestScore, MoveResult, MoveStyle, NextTime, TerrOwner, Kind, Temp,
    620   RecoverTurns, MaxScore: integer;
    621   Tile, NextTile: cardinal;
    622   CaptureOnly, PeaceBorder, done, NextToEnemyCity: boolean;
     620  RecoverTurns, MaxScore: Integer;
     621  Tile, NextTile: Cardinal;
     622  CaptureOnly, PeaceBorder, done, NextToEnemyCity: Boolean;
    623623  Adjacent: TVicinity8Loc;
    624   AdjacentUnknown: array[0..lxmax * lymax - 1] of shortint;
    625   PreLoc: array[0..lxmax * lymax - 1] of word;
    626   MoreTurn: array[0..lxmax * lymax - 1] of byte;
     624  AdjacentUnknown: array[0..lxmax * lymax - 1] of ShortInt;
     625  PreLoc: array[0..lxmax * lymax - 1] of Word;
     626  MoreTurn: array[0..lxmax * lymax - 1] of Byte;
    627627
    628628begin
     
    637637        if Map[Loc] and fCity = 0 then
    638638          Unit_MoveEx(uix, maNextCity);
    639         exit;
     639        Exit;
    640640      end;
    641641
     
    666666          // assume a score of $400 is the best achievable
    667667          or CaptureOnly and (TestTime >= $1000) then
    668           break;
     668          Break;
    669669
    670670        TestScore := 0;
    671671        Tile := Map[TestLoc];
    672         assert(Tile and (fUnit or fOwned) <> fUnit);
     672        Assert(Tile and (fUnit or fOwned) <> fUnit);
    673673        TerrOwner := RO.Territory[TestLoc];
    674674        AdjacentUnknown[TestLoc] := 0;
     
    743743              TestScore := $400 - 14
    744744            else if AdjacentUnknown[TestLoc] > 0 then
    745               if PeaceBorder or (TerrOwner >= 0) and (TerrOwner <> me) and
     745              if PeaceBorder or (TerrOwner >= 0) and (TerrOwner <> Me) and
    746746                (RO.Treaty[TerrOwner] < trPeace) then
    747747                TestScore := $400 - 32 + AdjacentUnknown[TestLoc]
     
    790790        end;
    791791        if PatrolLoc = Loc then
    792           exit;
     792          Exit;
    793793        TestLoc := PatrolLoc;
    794794        NextLoc := PreLoc[TestLoc];
     
    814814            Result := MoveResult and rEnemySpotted = 0;
    815815            done := True;
    816             break;
    817           end;
    818           assert(Loc = NextLoc);
     816            Break;
     817          end;
     818          Assert(Loc = NextLoc);
    819819        end;
    820820        if Loc >= 0 then
     
    839839  procedure SetCityDefenders;
    840840  var
    841     uix, cix, V8, Loc1, Best, uixBest, det: integer;
     841    uix, cix, V8, Loc1, Best, uixBest, det: Integer;
    842842    Adjacent: TVicinity8Loc;
    843     IsPort: boolean;
     843    IsPort: Boolean;
    844844  begin
    845845    for cix := 0 to RO.nCity - 1 do
     
    887887  procedure ProcessSeaTransport;
    888888  var
    889     i, f, uix, Loc1, a, b: integer;
    890     ready, go: boolean;
     889    I, F, uix, Loc1, A, B: Integer;
     890    ready, go: Boolean;
    891891    TransportPlan: TGroupTransportPlan;
    892892  begin
    893893    go := False;
    894     for f := 0 to maxCOD - 1 do
    895       if (f < nContinent) and (ContinentPresence[f] and not
    896         (1 shl me or PresenceUnknown) <> 0) then
     894    for F := 0 to maxCOD - 1 do
     895      if (F < nContinent) and (ContinentPresence[F] and not
     896        (1 shl Me or PresenceUnknown) <> 0) then
    897897        go := True; // any enemy island known?
    898898    if not go then
    899       exit;
     899      Exit;
    900900
    901901    SeaTransport_BeginInitialize;
     
    907907            (MyModel[mix].Attack > 0) and (Map[Loc] and fTerrain >= fGrass) then
    908908          begin
    909             f := Formation[Loc];
    910             if (f >= 0) and (f < maxCOD) and (ContinentPresence[f] and
    911               not (1 shl me) = 0) then
     909            F := Formation[Loc];
     910            if (F >= 0) and (F < maxCOD) and (ContinentPresence[F] and
     911              not (1 shl Me) = 0) then
    912912            begin
    913913              go := True;
     
    932932        if Map[Loc1] and fTerrain >= fGrass then
    933933        begin
    934           f := Formation[Loc1];
    935           if (f >= 0) and (f < maxCOD) and (ContinentPresence[f] and
    936             not (1 shl me or PresenceUnknown) <> 0) then
     934          F := Formation[Loc1];
     935          if (F >= 0) and (F < maxCOD) and (ContinentPresence[F] and
     936            not (1 shl Me or PresenceUnknown) <> 0) then
    937937            SeaTransport_AddDestination(Loc1);
    938938        end;
     
    948948      end;
    949949      if ready then
    950         for i := 0 to TransportPlan.nLoad - 1 do
     950        for I := 0 to TransportPlan.nLoad - 1 do
    951951        begin
    952952          Loc_to_ab(TransportPlan.LoadLoc,
    953             MyUnit[TransportPlan.uixLoad[i]].Loc, a, b);
    954           ready := ready and (abs(a) <= 1) and (abs(b) <= 1);
     953            MyUnit[TransportPlan.uixLoad[I]].Loc, A, B);
     954          ready := ready and (abs(A) <= 1) and (abs(B) <= 1);
    955955        end;
    956956      if ready then
    957957      begin
    958         for i := 0 to TransportPlan.nLoad - 1 do
    959         begin
    960           Unit_Step(TransportPlan.uixLoad[i], TransportPlan.LoadLoc);
    961           Moved[TransportPlan.uixLoad[i]] := True;
     958        for I := 0 to TransportPlan.nLoad - 1 do
     959        begin
     960          Unit_Step(TransportPlan.uixLoad[I], TransportPlan.LoadLoc);
     961          Moved[TransportPlan.uixLoad[I]] := True;
    962962        end;
    963963      end
    964964      else
    965965      begin
    966         for i := 0 to TransportPlan.nLoad - 1 do
    967         begin
    968           Unit_MoveEx(TransportPlan.uixLoad[i], TransportPlan.LoadLoc, mxAdjacent);
    969           Moved[TransportPlan.uixLoad[i]] := True;
     966        for I := 0 to TransportPlan.nLoad - 1 do
     967        begin
     968          Unit_MoveEx(TransportPlan.uixLoad[I], TransportPlan.LoadLoc, mxAdjacent);
     969          Moved[TransportPlan.uixLoad[I]] := True;
    970970        end;
    971971      end;
     
    973973  end;
    974974
    975   procedure ProcessUnload(uix: integer);
    976 
    977     procedure Unload(Kind, ToLoc: integer);
     975  procedure ProcessUnload(uix: Integer);
     976
     977    procedure Unload(Kind, ToLoc: Integer);
    978978    var
    979       uix1: integer;
     979      uix1: Integer;
    980980    begin
    981981      for uix1 := 0 to RO.nUn - 1 do
     
    987987            Unit_Step(uix1, ToLoc);
    988988            UnitPresence[ToLoc] := UnitPresence[ToLoc] or Kind;
    989             break;
     989            Break;
    990990          end;
    991991    end;
     
    993993  var
    994994    uix1, MoveStyle, TestLoc, TestTime, NextLoc, NextTime, V8,
    995     RecoverTurns, nSlow, nFast, SlowUnloadLoc, FastUnloadLoc, EndLoc, f: integer;
    996     NextTile: cardinal;
     995    RecoverTurns, nSlow, nFast, SlowUnloadLoc, FastUnloadLoc, EndLoc, F: Integer;
     996    NextTile: Cardinal;
    997997    Adjacent: TVicinity8Loc;
    998     Reached: array[0..lxmax * lymax - 1] of boolean;
     998    Reached: array[0..lxmax * lymax - 1] of Boolean;
    999999  begin
    10001000    // inventory
     
    10171017        FastUnloadLoc := -1;
    10181018        EndLoc := -1;
    1019         fillchar(Reached, MapSize, False);
     1019        FillChar(Reached, MapSize, False);
    10201020        Pile.Create(MapSize);
    10211021        Pile.Put(Loc, $800 - Movement);
     
    10341034              else if NextTile and fTerrain >= fGrass then
    10351035              begin
    1036                 f := Formation[NextLoc];
    1037                 if (f >= 0) and (f < maxCOD) and
    1038                   (ContinentPresence[f] and not (1 shl me or PresenceUnknown) <> 0) and
     1036                F := Formation[NextLoc];
     1037                if (F >= 0) and (F < maxCOD) and
     1038                  (ContinentPresence[F] and not (1 shl Me or PresenceUnknown) <> 0) and
    10391039                  (NextTile and (fUnit or fOwned) <> fUnit) then
    10401040                begin
     
    10741074
    10751075        if EndLoc < 0 then
    1076           exit;
     1076          Exit;
    10771077        if Loc <> EndLoc then
    10781078          Unit_MoveEx(uix, EndLoc);
    10791079        if Loc <> EndLoc then
    1080           exit;
     1080          Exit;
    10811081        if SlowUnloadLoc >= 0 then
    10821082        begin
     
    10921092        begin
    10931093          Moved[uix] := False;
    1094           exit;
     1094          Exit;
    10951095        end
    10961096      until False;
     
    10991099
    11001100var
    1101   uix, euix, Kind, euixBest, AttackLoc: integer;
    1102   OldTile: cardinal;
    1103   BackToStart, FirstLoop: boolean;
     1101  uix, euix, Kind, euixBest, AttackLoc: Integer;
     1102  OldTile: Cardinal;
     1103  BackToStart, FirstLoop: Boolean;
    11041104begin
    1105   fillchar(UnitPresence, MapSize, 0);
     1105  FillChar(UnitPresence, MapSize, 0);
    11061106  for uix := 0 to RO.nUn - 1 do
    11071107    with MyUnit[uix] do
     
    11161116      end;
    11171117
    1118   fillchar(Moved, RO.nUn, False);
     1118  FillChar(Moved, RO.nUn, False);
    11191119  for uix := 0 to RO.nUn - 1 do
    11201120    if (MyUnit[uix].Master >= 0) or (MyUnit[uix].TroopLoad > 0) then
     
    11281128      if RO.nEnemyUn > 0 then
    11291129      begin
    1130         fillchar(euixMap, MapSize * 2, $FF);
    1131         fillchar(AttackScore, RO.nEnemyUn * 4, 0);
     1130        FillChar(euixMap, MapSize * 2, $FF);
     1131        FillChar(AttackScore, RO.nEnemyUn * 4, 0);
    11321132        for euix := 0 to RO.nEnemyUn - 1 do
    11331133          with RO.EnemyUn[euix] do
     
    11401140      end;
    11411141      if not BackToStart then
    1142         break;
     1142        Break;
    11431143
    11441144      for uix := 0 to RO.nUn - 1 do
     
    11551155            euixBest := euix;
    11561156        if euixBest < 0 then
    1157           break;
     1157          Break;
    11581158        uix := uixAttack[euixBest];
    11591159        AttackLoc := RO.EnemyUn[euixBest].Loc;
     
    12021202            begin
    12031203              BackToStart := True;
    1204               break;
     1204              Break;
    12051205            end
    12061206  until not BackToStart;
     
    12141214    1 shl woMagellan + 1 shl woEiffel + 1 shl woLiberty + 1 shl woShinkansen;
    12151215
    1216   function LowPriority(cix: integer): boolean;
     1216  function LowPriority(cix: Integer): Boolean;
    12171217  var
    1218     part, cixHighPriority, TestDistance: integer;
     1218    part, cixHighPriority, TestDistance: Integer;
    12191219  begin
    12201220    Result := False;
     
    12281228        begin
    12291229          Result := True;
    1230           exit;
     1230          Exit;
    12311231        end;
    12321232      end;
     
    12341234  end;
    12351235
    1236   function ChooseWonderToBuild(WonderAvailable: integer; AllowCoastal: boolean): integer;
     1236  function ChooseWonderToBuild(WonderAvailable: Integer; AllowCoastal: Boolean): Integer;
    12371237  var
    1238     Count, iix: integer;
     1238    Count, iix: Integer;
    12391239  begin
    12401240    if (WonderAvailable and PrimeWonder > 0) and (AllowCoastal or
     
    12671267      begin
    12681268        Result := iix;
    1269         exit;
     1269        Exit;
    12701270      end;
    12711271    end;
     
    12731273
    12741274var
    1275   i, iix, cix, mix, uix, mixProduce, mixShip, V8, V21, Loc1, TotalPop,
    1276   AlonePop, f, f1, nTownGuard, ShipPart, ProduceShipPart, TestDistance,
    1277   part, WonderAvailable, WonderInWork, cixNewCapital, Center, Score, BestScore: integer;
    1278   mixCount: array[0..nmmax - 1] of integer;
     1275  I, iix, cix, mix, uix, mixProduce, mixShip, V8, V21, Loc1, TotalPop,
     1276  AlonePop, F, f1, nTownGuard, ShipPart, ProduceShipPart, TestDistance,
     1277  part, WonderAvailable, WonderInWork, cixNewCapital, Center, Score, BestScore: Integer;
     1278  mixCount: array[0..nmmax - 1] of Integer;
    12791279  //RareLoc: array[0..5] of integer;
    12801280  Adjacent: TVicinity8Loc;
    12811281  IsCoastal, IsPort, IsUnitProjectObsolete, HasSettler, SpezializeShipProduction,
    12821282  AlgaeAvailable, ProjectComplete, DoLowPriority, WillProduceColonyShip,
    1283   ImportantCity: boolean;
     1283  ImportantCity: Boolean;
    12841284  Radius: TVicinity21Loc;
    12851285  Report: TCityReportNew;
     
    12891289  FindBestModels;
    12901290
    1291   fillchar(mixCount, RO.nModel * 4, 0);
     1291  FillChar(mixCount, RO.nModel * 4, 0);
    12921292  for uix := 0 to RO.nUn - 1 do
    12931293    with MyUnit[uix] do
     
    13171317      begin
    13181318        Inc(TotalPop, Size);
    1319         f := Formation[Loc];
    1320         if (f < 0) or (f >= maxCOD) or (ContinentPresence[f] = 1 shl me) then
     1319        F := Formation[Loc];
     1320        if (F < 0) or (F >= maxCOD) or (ContinentPresence[F] = 1 shl Me) then
    13211321          Inc(AlonePop, Size);
    13221322      end;
     
    13581358              if (f1 >= 0) and (f1 < maxCOD) and
    13591359                ((OceanSize[f1] >= 8) or (OceanPresence[f1] and not
    1360                 (1 shl me) <> 0)) then
     1360                (1 shl Me) <> 0)) then
    13611361              begin // prefer non-coastal cities
    13621362                Dec(Score, 18);
    1363                 break;
     1363                Break;
    13641364              end;
    13651365            end;
     
    13901390          (LowPriority(cix) = DoLowPriority) then
    13911391        begin
    1392           f := Formation[Loc];
     1392          F := Formation[Loc];
    13931393          IsCoastal := False;
    13941394          IsPort := False;
     
    14021402              f1 := Formation[Loc1];
    14031403              if (f1 >= 0) and (f1 < maxCOD) and (OceanSize[f1] >= 8) and
    1404                 (OceanPresence[f1] and not (1 shl me) <> 0) then
     1404                (OceanPresence[f1] and not (1 shl Me) <> 0) then
    14051405              begin
    14061406                IsPort := True;
    1407                 break;
     1407                Break;
    14081408              end;
    14091409            end;
     
    14121412            (RO.Model[City_CurrentUnitProject(cix)].Kind <> mkSettler) then
    14131413          begin
    1414             i := nModelCategory - 1;
    1415             while (i >= 0) and (City_CurrentUnitProject(cix) <> mixBest[i]) do
    1416               Dec(i);
    1417             IsUnitProjectObsolete := i < 0;
     1414            I := nModelCategory - 1;
     1415            while (I >= 0) and (City_CurrentUnitProject(cix) <> mixBest[I]) do
     1416              Dec(I);
     1417            IsUnitProjectObsolete := I < 0;
    14181418          end
    14191419          else
     
    15811581              City_StartImprovement(cix,imMissileBat)}
    15821582                else if IsPort and (not SpezializeShipProduction or
    1583                   (f < 0) or (f >= maxCOD) or (ContinentPresence[f] = 1 shl me)) and
     1583                  (F < 0) or (F >= maxCOD) or (ContinentPresence[F] = 1 shl Me)) and
    15841584                  (Built[imDockyard] = 0) and City_Improvable(cix, imDockyard) then
    15851585                  City_StartImprovement(cix, imDockyard)
    15861586                else if IsPort and (mixShip >= 0) and
    1587                   (not SpezializeShipProduction or (f < 0) or
    1588                   (f >= maxCOD) or (ContinentPresence[f] = 1 shl me)) then
     1587                  (not SpezializeShipProduction or (F < 0) or
     1588                  (F >= maxCOD) or (ContinentPresence[F] = 1 shl Me)) then
    15891589                  City_StartUnitProduction(cix, mixShip)
    15901590                else if (Built[imBarracks] + Built[imMilAcademy] = 0) and
     
    16001600          if (City_CurrentImprovementProject(cix) = imCourt) and
    16011601            (Built[imTownHall] > 0) and (prod >= imp[imCourt].cost *
    1602             BuildCostMod[G.Difficulty[me]] div 12 -
    1603             (imp[imTownHall].cost * BuildCostMod[G.Difficulty[me]] div 12) *
     1602            BuildCostMod[G.Difficulty[Me]] div 12 -
     1603            (imp[imTownHall].cost * BuildCostMod[G.Difficulty[Me]] div 12) *
    16041604            2 div 3) then
    16051605            City_RebuildImprovement(cix, imTownHall)
     
    16141614                if City_RebuildImprovement(cix, iix) < rExecuted then
    16151615                  City_SellImprovement(cix, iix);
    1616                 break;
     1616                Break;
    16171617              end;
    16181618        end;
    16191619end;
    16201620
    1621 function TBarbarina.Barbarina_ChooseResearchAdvance: integer;
     1621function TBarbarina.Barbarina_ChooseResearchAdvance: Integer;
    16221622var
    1623   nPreq, rmix, rmixChosen, i, MaxWeight, MaxDefense, ChosenPreq: integer;
    1624   NeedSeaUnits, ready: boolean;
     1623  nPreq, rmix, rmixChosen, I, MaxWeight, MaxDefense, ChosenPreq: Integer;
     1624  NeedSeaUnits, ready: Boolean;
    16251625  ModelExists: set of 0..nModelCategory - 1;
    1626   known: array[0..nAdv - 1] of integer;
    1627 
    1628   procedure ChoosePreq(ad: integer);
     1626  known: array[0..nAdv - 1] of Integer;
     1627
     1628  procedure ChoosePreq(ad: Integer);
    16291629  var
    1630     i: integer;
    1631     PreqOk: boolean;
     1630    I: Integer;
     1631    PreqOk: Boolean;
    16321632  begin
    1633     assert(RO.Tech[ad] < tsApplicable);
     1633    Assert(RO.Tech[ad] < tsApplicable);
    16341634    if known[ad] = 0 then
    16351635    begin
     
    16371637      PreqOk := True;
    16381638      if not (ad in [adScience, adMassProduction]) and (RO.Tech[ad] < tsSeen) then
    1639         for i := 0 to 1 do
    1640           if (AdvPreq[ad, i] >= 0) and (RO.Tech[AdvPreq[ad, i]] < tsApplicable) then
     1639        for I := 0 to 1 do
     1640          if (AdvPreq[ad, I] >= 0) and (RO.Tech[AdvPreq[ad, I]] < tsApplicable) then
    16411641          begin
    16421642            PreqOk := False;
    1643             ChoosePreq(AdvPreq[ad, i]);
     1643            ChoosePreq(AdvPreq[ad, I]);
    16441644          end;
    16451645      if PreqOk then
     
    16971697        ready := (MaxWeight >= Weight) and (MaxDefense >= Cap[mcDefense]);
    16981698        if ready then
    1699           for i := 0 to nFeature - 1 do
    1700             if (Cap[i] > 0) and (Feature[i].Preq <> preNone) and
    1701               ((Feature[i].Preq < 0) or not IsResearched(Feature[i].Preq)) then
     1699          for I := 0 to nFeature - 1 do
     1700            if (Cap[I] > 0) and (Feature[I].Preq <> preNone) and
     1701              ((Feature[I].Preq < 0) or not IsResearched(Feature[I].Preq)) then
    17021702              ready := False;
    17031703        if ready then
    17041704        begin
    1705           for i := 0 to nUpgrade - 1 do
    1706             if (Upgrades and (1 shl i) <> 0) and not
    1707               IsResearched(Upgrade[Domain, i].Preq) then
     1705          for I := 0 to nUpgrade - 1 do
     1706            if (Upgrades and (1 shl I) <> 0) and not
     1707              IsResearched(Upgrade[Domain, I].Preq) then
    17081708              ready := False;
    17091709        end;
    17101710        if ready then
    17111711        begin
    1712           include(ModelExists, Category);
     1712          Include(ModelExists, Category);
    17131713          if not IsModelAvailable(rmix) then
    17141714            rmixChosen := rmix;
     
    17191719    begin
    17201720      PrepareNewModel(Domain);
    1721       for i := 0 to nFeature - 1 do
    1722         if (i < 2) or (Cap[i] > 0) then
    1723           SetNewModelFeature(i, Cap[i]);
    1724       if RO.Wonder[woSun].EffectiveOwner = me then
     1721      for I := 0 to nFeature - 1 do
     1722        if (I < 2) or (Cap[I] > 0) then
     1723          SetNewModelFeature(I, Cap[I]);
     1724      if RO.Wonder[woSun].EffectiveOwner = Me then
    17251725      begin
    17261726        //if Cap[mcWeapons]>=2*Cap[mcArmor] then
     
    17301730      end;
    17311731      Result := adMilitary;
    1732       exit;
     1732      Exit;
    17331733    end;
    17341734
    17351735  NeedSeaUnits := True;
    1736   i := 0;
    1737   while (i < nResearchOrder) and (not NeedSeaUnits and (ResearchOrder[i] < 0) or
    1738       IsResearched(abs(ResearchOrder[i]))) do
    1739     Inc(i);
    1740   if i >= nResearchOrder then // list done, continue with future tech
     1736  I := 0;
     1737  while (I < nResearchOrder) and (not NeedSeaUnits and (ResearchOrder[I] < 0) or
     1738      IsResearched(abs(ResearchOrder[I]))) do
     1739    Inc(I);
     1740  if I >= nResearchOrder then // list done, continue with future tech
    17411741  begin
    17421742    if random(2) = 1 then
     
    17501750    nPreq := 0;
    17511751    ChosenPreq := -1;
    1752     ChoosePreq(abs(ResearchOrder[i]));
    1753     assert(nPreq > 0);
     1752    ChoosePreq(abs(ResearchOrder[I]));
     1753    Assert(nPreq > 0);
    17541754    Result := ChosenPreq;
    17551755  end;
    17561756end;
    17571757
    1758 function TBarbarina.Barbarina_WantCheckNegotiation(Nation: integer): boolean;
     1758function TBarbarina.Barbarina_WantCheckNegotiation(Nation: Integer): Boolean;
    17591759begin
    17601760  if (RO.Tech[adTheRepublic] < tsSeen) and (RO.Tech[adTheology] >= tsApplicable) and
     
    17691769begin
    17701770  if RO.Tech[adTheRepublic] >= tsSeen then
    1771     exit; // default reaction
     1771    Exit; // default reaction
    17721772  if MyLastAction = scContact then
    17731773  begin
     
    17971797end;
    17981798
    1799 function TBarbarina.Barbarina_WantNegotiation(Nation: integer;
    1800   NegoTime: TNegoTime): boolean;
     1799function TBarbarina.Barbarina_WantNegotiation(Nation: Integer;
     1800  NegoTime: TNegoTime): Boolean;
    18011801var
    1802   uix, TestLoc, V8: integer;
     1802  uix, TestLoc, V8: Integer;
    18031803  Adjacent: TVicinity8Loc;
    18041804begin
     
    18121812      if RO.Turn >= RO.LastCancelTreaty[Nation] + CancelTreatyTurns then
    18131813      begin
    1814         if (RO.Turn and 3 = (Nation + $F - me) and 3) and
     1814        if (RO.Turn and 3 = (Nation + $F - Me) and 3) and
    18151815          (RO.Treaty[Nation] > trPeace) then
    18161816        begin
     
    18381838                    NegoCause := CancelTreaty;
    18391839                    Result := True;
    1840                     exit;
     1840                    Exit;
    18411841                  end;
    18421842                end;
     
    18581858procedure TBarbarina.MakeColonyShipPlan;
    18591859var
    1860   i, V21, V21C, CityLoc, Loc1, part, cix, BestValue, TestValue, FoodCount,
    1861   ProdCount, ProdExtra, Score, BestScore: integer;
    1862   Tile: cardinal;
    1863   ok, check: boolean;
     1860  I, V21, V21C, CityLoc, Loc1, part, cix, BestValue, TestValue, FoodCount,
     1861  ProdCount, ProdExtra, Score, BestScore: Integer;
     1862  Tile: Cardinal;
     1863  ok, check: Boolean;
    18641864  Radius, RadiusC: TVicinity21Loc;
    18651865begin
     
    18871887              begin
    18881888                part := (Tile and fModern) shr 25 - 1;
    1889                 if RO.Ship[me].Parts[part] < ShipNeed[part] then
     1889                if RO.Ship[Me].Parts[part] < ShipNeed[part] then
    18901890                  // not enough of this kind already
    18911891                begin
     
    18931893                  if ColonyShipPlan[part].cixProducing >= 0 then
    18941894                  begin // another city is already assigned to this ship part, choose one of the two
    1895                     TestValue := (ID and $FFF) shl 4 + ((ID shr 12) + 15 - me) and $F;
     1895                    TestValue := (ID and $FFF) shl 4 + ((ID shr 12) + 15 - Me) and $F;
    18961896                    BestValue :=
    18971897                      (MyCity[ColonyShipPlan[part].cixProducing].ID and $FFF) shl
    18981898                      4 + ((MyCity[ColonyShipPlan[part].cixProducing].ID shr 12) +
    1899                       15 - me) and $F;
     1899                      15 - Me) and $F;
    19001900                    if TestValue <= BestValue then
    19011901                      ok := False;
     
    19121912    check := False;
    19131913    for part := 0 to nShipPart - 1 do
    1914       if (RO.Ship[me].Parts[part] < ShipNeed[part]) // not enough of this kind already
     1914      if (RO.Ship[Me].Parts[part] < ShipNeed[part]) // not enough of this kind already
    19151915        and (ColonyShipPlan[part].cixProducing < 0) then // no city to produce
    19161916        check := True;
     
    19311931      end;
    19321932      for part := 0 to nShipPart - 1 do
    1933         if (RO.Ship[me].Parts[part] < ShipNeed[part]) // not enough of this kind already
     1933        if (RO.Ship[Me].Parts[part] < ShipNeed[part]) // not enough of this kind already
    19341934          and (ColonyShipPlan[part].cixProducing < 0) // no city to produce
    19351935          and (ColonyShipPlan[part].nLocResource > 0) then // resource is known
    19361936        begin
    1937           for i := 0 to ColonyShipPlan[part].nLocResource - 1 do
     1937          for I := 0 to ColonyShipPlan[part].nLocResource - 1 do
    19381938          begin
    19391939            BestScore := 0;
    1940             V21_to_Loc(ColonyShipPlan[part].LocResource[i], Radius);
     1940            V21_to_Loc(ColonyShipPlan[part].LocResource[I], Radius);
    19411941            for V21 := 1 to 26 do
    19421942            begin // check all potential cities in range
     
    19841984                      Dec(ProdCount, 5 - FoodCount);
    19851985                    Score := ProdCount * 4 + ProdExtra * 8 + FoodCount;
    1986                     Score := Score shl 8 + ((CityLoc xor me) * 4567) mod 251;
     1986                    Score := Score shl 8 + ((CityLoc xor Me) * 4567) mod 251;
    19871987                    // some unexactness, random but always the same for this tile
    19881988                  end;
  • trunk/AI/StdAI/CustomAI.pas

    r289 r447  
    1313  TCustomAI = class
    1414  public
    15     procedure Process(Command: integer; var Data);
     15    procedure Process(Command: Integer; var Data);
    1616
    1717    // overridables
    18     constructor Create(Nation: integer); virtual;
     18    constructor Create(Nation: Integer); virtual;
    1919    destructor Destroy; override;
    2020    procedure SetDataDefaults; virtual;
    2121    procedure SetDataRandom; virtual;
    2222    procedure OnBeforeEnemyAttack(UnitInfo: TUnitInfo;
    23       ToLoc, EndHealth, EndHealthDef: integer); virtual;
    24     procedure OnBeforeEnemyCapture(UnitInfo: TUnitInfo; ToLoc: integer); virtual;
     23      ToLoc, EndHealth, EndHealthDef: Integer); virtual;
     24    procedure OnBeforeEnemyCapture(UnitInfo: TUnitInfo; ToLoc: Integer); virtual;
    2525    procedure OnAfterEnemyAttack; virtual;
    2626    procedure OnAfterEnemyCapture; virtual;
    2727
    2828  protected
    29     me: integer; // index of the controlled nation
     29    Me: Integer; // index of the controlled nation
    3030    RO: ^TPlayerContext;
    3131    Map: ^TTileList;
     
    3434    MyModel: ^TModelList;
    3535
    36     cixStateImp: array[imPalace..imSpacePort] of integer;
     36    cixStateImp: array[imPalace..imSpacePort] of Integer;
    3737
    3838    // negotiation
    39     Opponent: integer; // nation i'm in negotiation with, -1 indicates no-negotiation mode
    40     MyAction, MyLastAction, OppoAction: integer;
     39    Opponent: Integer; // nation i'm in negotiation with, -1 indicates no-negotiation mode
     40    MyAction, MyLastAction, OppoAction: Integer;
    4141    MyOffer, MyLastOffer, OppoOffer: TOffer;
    4242
     
    4444    procedure DoTurn; virtual;
    4545    procedure DoNegotiation; virtual;
    46     function ChooseResearchAdvance: integer; virtual;
    47     function ChooseStealAdvance: integer; virtual;
    48     function ChooseGovernment: integer; virtual;
    49     function WantNegotiation(Nation: integer; NegoTime: TNegoTime): boolean; virtual;
    50     function OnNegoRejected_CancelTreaty: boolean; virtual;
     46    function ChooseResearchAdvance: Integer; virtual;
     47    function ChooseStealAdvance: Integer; virtual;
     48    function ChooseGovernment: Integer; virtual;
     49    function WantNegotiation(Nation: Integer; NegoTime: TNegoTime): Boolean; virtual;
     50    function OnNegoRejected_CancelTreaty: Boolean; virtual;
    5151
    5252    // general functions
    53     function IsResearched(Advance: integer): boolean;
    54     function ResearchCost: integer;
    55     function ChangeAttitude(Nation, Attitude: integer): integer;
    56     function Revolution: integer;
    57     function ChangeRates(Tax, Lux: integer): integer;
    58     function PrepareNewModel(Domain: integer): integer;
    59     function SetNewModelFeature(F, Count: integer): integer;
    60     function AdvanceResearchable(Advance: integer): boolean;
    61     function AdvanceStealable(Advance: integer): boolean;
    62     function GetJobProgress(Loc: integer; var JobProgress: TJobProgressData): boolean;
    63     function DebugMessage(Level: integer; Text: string): boolean;
    64     function SetDebugMap(var DebugMap): boolean;
     53    function IsResearched(Advance: Integer): Boolean;
     54    function ResearchCost: Integer;
     55    function ChangeAttitude(Nation, Attitude: Integer): Integer;
     56    function Revolution: Integer;
     57    function ChangeRates(Tax, Lux: Integer): Integer;
     58    function PrepareNewModel(Domain: Integer): Integer;
     59    function SetNewModelFeature(F, Count: Integer): Integer;
     60    function AdvanceResearchable(Advance: Integer): Boolean;
     61    function AdvanceStealable(Advance: Integer): Boolean;
     62    function GetJobProgress(Loc: Integer; var JobProgress: TJobProgressData): Boolean;
     63    function DebugMessage(Level: Integer; Text: string): Boolean;
     64    function SetDebugMap(var DebugMap): Boolean;
    6565
    6666    // unit functions
    67     procedure Unit_FindMyDefender(Loc: integer; var uix: integer);
    68     procedure Unit_FindEnemyDefender(Loc: integer; var euix: integer);
    69     function Unit_Move(uix, ToLoc: integer): integer;
    70     function Unit_Step(uix, ToLoc: integer): integer;
    71     function Unit_Attack(uix, ToLoc: integer): integer;
    72     function Unit_DoMission(uix, MissionType, ToLoc: integer): integer;
    73     function Unit_MoveForecast(uix, ToLoc: integer;
    74       var RemainingMovement: integer): boolean;
    75     function Unit_AttackForecast(uix, ToLoc, AttackMovement: integer;
    76       var RemainingHealth: integer): boolean;
    77     function Unit_DefenseForecast(euix, ToLoc: integer;
    78       var RemainingHealth: integer): boolean;
    79     function Unit_Disband(uix: integer): integer;
    80     function Unit_StartJob(uix, NewJob: integer): integer;
    81     function Unit_SetHomeHere(uix: integer): integer;
    82     function Unit_Load(uix: integer): integer;
    83     function Unit_Unload(uix: integer): integer;
    84     function Unit_SelectTransport(uix: integer): integer;
    85     function Unit_AddToCity(uix: integer): integer;
     67    procedure Unit_FindMyDefender(Loc: Integer; var uix: Integer);
     68    procedure Unit_FindEnemyDefender(Loc: Integer; var euix: Integer);
     69    function Unit_Move(uix, ToLoc: Integer): Integer;
     70    function Unit_Step(uix, ToLoc: Integer): Integer;
     71    function Unit_Attack(uix, ToLoc: Integer): Integer;
     72    function Unit_DoMission(uix, MissionType, ToLoc: Integer): Integer;
     73    function Unit_MoveForecast(uix, ToLoc: Integer;
     74      var RemainingMovement: Integer): Boolean;
     75    function Unit_AttackForecast(uix, ToLoc, AttackMovement: Integer;
     76      var RemainingHealth: Integer): Boolean;
     77    function Unit_DefenseForecast(euix, ToLoc: Integer;
     78      var RemainingHealth: Integer): Boolean;
     79    function Unit_Disband(uix: Integer): Integer;
     80    function Unit_StartJob(uix, NewJob: Integer): Integer;
     81    function Unit_SetHomeHere(uix: Integer): Integer;
     82    function Unit_Load(uix: Integer): Integer;
     83    function Unit_Unload(uix: Integer): Integer;
     84    function Unit_SelectTransport(uix: Integer): Integer;
     85    function Unit_AddToCity(uix: Integer): Integer;
    8686
    8787    // city functions
    88     procedure City_FindMyCity(Loc: integer; var cix: integer);
    89     procedure City_FindEnemyCity(Loc: integer; var ecix: integer);
    90     function City_HasProject(cix: integer): boolean;
    91     function City_CurrentImprovementProject(cix: integer): integer;
    92     function City_CurrentUnitProject(cix: integer): integer;
    93     function City_GetTileInfo(cix, TileLoc: integer; var TileInfo: TTileInfo): integer;
    94     function City_GetReport(cix: integer; var Report: TCityReport): integer;
    95     function City_GetHypoReport(cix, HypoTiles, HypoTax, HypoLux: integer;
    96       var Report: TCityReport): integer;
    97     function City_GetReportNew(cix: integer; var Report: TCityReportNew): integer;
    98     function City_GetHypoReportNew(cix, HypoTiles, HypoTaxRate, HypoLuxuryRate: integer;
    99       var Report: TCityReportNew): integer;
    100     function City_GetAreaInfo(cix: integer; var AreaInfo: TCityAreaInfo): integer;
    101     function City_StartUnitProduction(cix, mix: integer): integer;
    102     function City_StartEmigration(cix, mix: integer;
    103       AllowDisbandCity, AsConscripts: boolean): integer;
    104     function City_StartImprovement(cix, iix: integer): integer;
    105     function City_Improvable(cix, iix: integer): boolean;
    106     function City_StopProduction(cix: integer): integer;
    107     function City_BuyProject(cix: integer): integer;
    108     function City_SellImprovement(cix, iix: integer): integer;
    109     function City_RebuildImprovement(cix, iix: integer): integer;
    110     function City_SetTiles(cix, NewTiles: integer): integer;
    111     procedure City_OptimizeTiles(cix: integer; ResourceWeights: cardinal = rwMaxGrowth);
     88    procedure City_FindMyCity(Loc: Integer; var cix: Integer);
     89    procedure City_FindEnemyCity(Loc: Integer; var ecix: Integer);
     90    function City_HasProject(cix: Integer): Boolean;
     91    function City_CurrentImprovementProject(cix: Integer): Integer;
     92    function City_CurrentUnitProject(cix: Integer): Integer;
     93    function City_GetTileInfo(cix, TileLoc: Integer; var TileInfo: TTileInfo): Integer;
     94    function City_GetReport(cix: Integer; var Report: TCityReport): Integer;
     95    function City_GetHypoReport(cix, HypoTiles, HypoTax, HypoLux: Integer;
     96      var Report: TCityReport): Integer;
     97    function City_GetReportNew(cix: Integer; var Report: TCityReportNew): Integer;
     98    function City_GetHypoReportNew(cix, HypoTiles, HypoTaxRate, HypoLuxuryRate: Integer;
     99      var Report: TCityReportNew): Integer;
     100    function City_GetAreaInfo(cix: Integer; var AreaInfo: TCityAreaInfo): Integer;
     101    function City_StartUnitProduction(cix, mix: Integer): Integer;
     102    function City_StartEmigration(cix, mix: Integer;
     103      AllowDisbandCity, AsConscripts: Boolean): Integer;
     104    function City_StartImprovement(cix, iix: Integer): Integer;
     105    function City_Improvable(cix, iix: Integer): Boolean;
     106    function City_StopProduction(cix: Integer): Integer;
     107    function City_BuyProject(cix: Integer): Integer;
     108    function City_SellImprovement(cix, iix: Integer): Integer;
     109    function City_RebuildImprovement(cix, iix: Integer): Integer;
     110    function City_SetTiles(cix, NewTiles: Integer): Integer;
     111    procedure City_OptimizeTiles(cix: Integer; ResourceWeights: Cardinal = rwMaxGrowth);
    112112
    113113    // negotiation
    114     function Nego_CheckMyAction: integer;
     114    function Nego_CheckMyAction: Integer;
    115115
    116116  private
    117     HaveTurned: boolean;
     117    HaveTurned: Boolean;
    118118    UnwantedNego: set of 0..nPl - 1;
    119119    Contacted: set of 0..nPl - 1;
     
    125125  Server: TServerCall;
    126126  G: TNewGameData;
    127   RWDataSize, MapSize: integer;
    128   decompose24: cardinal;
    129   nodata: pointer;
     127  RWDataSize, MapSize: Integer;
     128  decompose24: Cardinal;
     129  nodata: Pointer;
    130130
    131131const
     
    139139
    140140type
    141   TVicinity8Loc = array[0..7] of integer;
    142   TVicinity21Loc = array[0..27] of integer;
     141  TVicinity8Loc = array[0..7] of Integer;
     142  TVicinity21Loc = array[0..27] of Integer;
    143143
    144144
    145145procedure Init(NewGameData: TNewGameData);
    146146
    147 procedure ab_to_Loc(Loc0, a, b: integer; var Loc: integer);
    148 procedure Loc_to_ab(Loc0, Loc: integer; var a, b: integer);
    149 procedure ab_to_V8(a, b: integer; var V8: integer);
    150 procedure V8_to_ab(V8: integer; var a, b: integer);
    151 procedure ab_to_V21(a, b: integer; var V21: integer);
    152 procedure V21_to_ab(V21: integer; var a, b: integer);
    153 procedure V8_to_Loc(Loc0: integer; var VicinityLoc: TVicinity8Loc);
    154 procedure V21_to_Loc(Loc0: integer; var VicinityLoc: TVicinity21Loc);
    155 function Distance(Loc0, Loc1: integer): integer;
     147procedure ab_to_Loc(Loc0, A, B: Integer; var Loc: Integer);
     148procedure Loc_to_ab(Loc0, Loc: Integer; var A, B: Integer);
     149procedure ab_to_V8(A, B: Integer; var V8: Integer);
     150procedure V8_to_ab(V8: Integer; var A, B: Integer);
     151procedure ab_to_V21(A, B: Integer; var V21: Integer);
     152procedure V21_to_ab(V21: Integer; var A, B: Integer);
     153procedure V8_to_Loc(Loc0: Integer; var VicinityLoc: TVicinity8Loc);
     154procedure V21_to_Loc(Loc0: Integer; var VicinityLoc: TVicinity21Loc);
     155function Distance(Loc0, Loc1: Integer): Integer;
    156156
    157157
     
    159159
    160160const
    161   ab_v8: array[-4..4] of integer = (5, 6, 7, 4, -1, 0, 3, 2, 1);
    162   v8_a: array[0..7] of integer = (1, 1, 0, -1, -1, -1, 0, 1);
    163   v8_b: array[0..7] of integer = (0, 1, 1, 1, 0, -1, -1, -1);
    164 
    165 
    166 procedure ab_to_Loc(Loc0, a, b: integer; var Loc: integer);
     161  ab_v8: array[-4..4] of Integer = (5, 6, 7, 4, -1, 0, 3, 2, 1);
     162  v8_a: array[0..7] of Integer = (1, 1, 0, -1, -1, -1, 0, 1);
     163  v8_b: array[0..7] of Integer = (0, 1, 1, 1, 0, -1, -1, -1);
     164
     165
     166procedure ab_to_Loc(Loc0, A, B: Integer; var Loc: Integer);
    167167{relative location from Loc0}
    168168var
    169   y0: integer;
    170 begin
    171   assert((Loc0 >= 0) and (Loc0 < MapSize) and (a - b + G.lx >= 0));
    172   y0 := cardinal(Loc0) * decompose24 shr 24;
    173   Loc := (Loc0 + (a - b + y0 and 1 + G.lx + G.lx) shr 1) mod G.lx + G.lx * (y0 + a + b);
     169  y0: Integer;
     170begin
     171  Assert((Loc0 >= 0) and (Loc0 < MapSize) and (A - B + G.lx >= 0));
     172  y0 := Cardinal(Loc0) * decompose24 shr 24;
     173  Loc := (Loc0 + (A - B + y0 and 1 + G.lx + G.lx) shr 1) mod G.lx + G.lx * (y0 + A + B);
    174174  if Loc >= MapSize then
    175175    Loc := -$1000;
    176176end;
    177177
    178 procedure Loc_to_ab(Loc0, Loc: integer; var a, b: integer);
     178procedure Loc_to_ab(Loc0, Loc: Integer; var A, B: Integer);
    179179{$IFDEF FPC}// freepascal
    180180var
    181   dx, dy: integer;
     181  dx, dy: Integer;
    182182begin
    183183  dx := ((Loc mod G.lx * 2 + Loc div G.lx and 1) - (Loc0 mod G.lx * 2 + Loc0 div
    184184    G.lx and 1) + 3 * G.lx) mod (2 * G.lx) - G.lx;
    185185  dy := Loc div G.lx - Loc0 div G.lx;
    186   a := (dx + dy) div 2;
    187   b := (dy - dx) div 2;
     186  A := (dx + dy) div 2;
     187  B := (dy - dx) div 2;
    188188end;
    189189
     
    195195// calculate
    196196push ecx
    197 div byte ptr [G]
     197div Byte ptr [G]
    198198xor ebx,ebx
    199199mov bl,ah  // ebx:=Loc0 mod G.lx
     
    201201and ecx,$000000FF // ecx:=Loc0 div G.lx
    202202mov eax,edx
    203 div byte ptr [G]
     203div Byte ptr [G]
    204204xor edx,edx
    205205mov dl,ah // edx:=Loc mod G.lx
     
    218218mov edx,dword ptr [G]
    219219cmp eax,edx
    220 jl @a
     220jl @A
    221221  sub eax,edx
    222222  sub eax,edx
    223223  jmp @ok
    224 @a:
     224@A:
    225225neg edx
    226226cmp eax,edx
     
    235235add eax,ebx
    236236sar edx,1 // edx:=b
    237 mov ebx,[b]
     237mov ebx,[B]
    238238mov [ebx],edx
    239239sar eax,1 // eax:=a
    240 mov [a],eax
     240mov [A],eax
    241241
    242242pop ebx
     
    244244{$ENDIF}
    245245
    246 procedure ab_to_V8(a, b: integer; var V8: integer);
    247 begin
    248   assert((abs(a) <= 1) and (abs(b) <= 1) and ((a <> 0) or (b <> 0)));
    249   V8 := ab_v8[2 * b + b + a];
    250 end;
    251 
    252 procedure V8_to_ab(V8: integer; var a, b: integer);
    253 begin
    254   a := v8_a[V8];
    255   b := V8_b[V8];
    256 end;
    257 
    258 procedure ab_to_V21(a, b: integer; var V21: integer);
    259 begin
    260   V21 := (a + b + 3) shl 2 + (a - b + 3) shr 1;
    261 end;
    262 
    263 procedure V21_to_ab(V21: integer; var a, b: integer);
    264 var
    265   dx, dy: integer;
     246procedure ab_to_V8(A, B: Integer; var V8: Integer);
     247begin
     248  Assert((abs(A) <= 1) and (abs(B) <= 1) and ((A <> 0) or (B <> 0)));
     249  V8 := ab_v8[2 * B + B + A];
     250end;
     251
     252procedure V8_to_ab(V8: Integer; var A, B: Integer);
     253begin
     254  A := v8_a[V8];
     255  B := V8_b[V8];
     256end;
     257
     258procedure ab_to_V21(A, B: Integer; var V21: Integer);
     259begin
     260  V21 := (A + B + 3) shl 2 + (A - B + 3) shr 1;
     261end;
     262
     263procedure V21_to_ab(V21: Integer; var A, B: Integer);
     264var
     265  dx, dy: Integer;
    266266begin
    267267  dy := V21 shr 2 - 3;
    268268  dx := V21 and 3 shl 1 - 3 + (dy + 3) and 1;
    269   a := (dx + dy) div 2;
    270   b := (dy - dx) div 2;
    271 end;
    272 
    273 procedure V8_to_Loc(Loc0: integer; var VicinityLoc: TVicinity8Loc);
    274 var
    275   x0, y0, lx: integer;
     269  A := (dx + dy) div 2;
     270  B := (dy - dx) div 2;
     271end;
     272
     273procedure V8_to_Loc(Loc0: Integer; var VicinityLoc: TVicinity8Loc);
     274var
     275  x0, y0, lx: Integer;
    276276begin
    277277  lx := G.lx;
    278   y0 := cardinal(Loc0) * decompose24 shr 24;
     278  y0 := Cardinal(Loc0) * decompose24 shr 24;
    279279  x0 := Loc0 - y0 * lx; // Loc0 mod lx;
    280280  VicinityLoc[1] := Loc0 + lx * 2;
     
    323323end;
    324324
    325 procedure V21_to_Loc(Loc0: integer; var VicinityLoc: TVicinity21Loc);
    326 var
    327   dx, dy, bit, y0, xComp, yComp, xComp0, xCompSwitch: integer;
    328   dst: ^integer;
    329 begin
    330   y0 := cardinal(Loc0) * decompose24 shr 24;
     325procedure V21_to_Loc(Loc0: Integer; var VicinityLoc: TVicinity21Loc);
     326var
     327  dx, dy, bit, y0, xComp, yComp, xComp0, xCompSwitch: Integer;
     328  dst: ^Integer;
     329begin
     330  y0 := Cardinal(Loc0) * decompose24 shr 24;
    331331  xComp0 := Loc0 - y0 * G.lx - 1; // Loc0 mod G.lx -1
    332332  xCompSwitch := xComp0 - 1 + y0 and 1;
     
    368368end;
    369369
    370 function Distance(Loc0, Loc1: integer): integer;
    371 var
    372   a, b, dx, dy: integer;
    373 begin
    374   Loc_to_ab(Loc0, Loc1, a, b);
    375   dx := abs(a - b);
    376   dy := abs(a + b);
     370function Distance(Loc0, Loc1: Integer): Integer;
     371var
     372  A, B, dx, dy: Integer;
     373begin
     374  Loc_to_ab(Loc0, Loc1, A, B);
     375  dx := abs(A - B);
     376  dy := abs(A + B);
    377377  Result := dx + dy + abs(dx - dy) shr 1;
    378378end;
     
    381381procedure Init(NewGameData: TNewGameData);
    382382{$IFDEF DEBUG}var
    383   Loc: integer;
     383  Loc: Integer;
    384384{$ENDIF}
    385385begin
     
    389389{$IFDEF DEBUG}
    390390  for Loc := 0 to MapSize - 1 do
    391     assert(cardinal(Loc) * decompose24 shr 24 = cardinal(Loc div G.lx));
     391    Assert(Cardinal(Loc) * decompose24 shr 24 = Cardinal(Loc div G.lx));
    392392{$ENDIF}
    393393end;
    394394
    395395
    396 constructor TCustomAI.Create(Nation: integer);
     396constructor TCustomAI.Create(Nation: Integer);
    397397begin
    398398  inherited Create;
    399   me := Nation;
    400   RO := pointer(G.RO[Nation]);
    401   Map := pointer(RO.Map);
    402   MyUnit := pointer(RO.Un);
    403   MyCity := pointer(RO.City);
    404   MyModel := pointer(RO.Model);
     399  Me := Nation;
     400  RO := Pointer(G.RO[Nation]);
     401  Map := Pointer(RO.Map);
     402  MyUnit := Pointer(RO.Un);
     403  MyCity := Pointer(RO.City);
     404  MyModel := Pointer(RO.Model);
    405405  Opponent := -1;
    406406end;
     
    408408destructor TCustomAI.Destroy;
    409409begin
    410   Server(sSetDebugMap, me, 0, nodata^);
    411 end;
    412 
    413 
    414 procedure TCustomAI.Process(Command: integer; var Data);
    415 var
    416   Nation, NewResearch, NewGov, Count, ad, cix, iix: integer;
     410  Server(sSetDebugMap, Me, 0, nodata^);
     411end;
     412
     413
     414procedure TCustomAI.Process(Command: Integer; var Data);
     415var
     416  Nation, NewResearch, NewGov, Count, ad, cix, iix: Integer;
    417417  NegoTime: TNegoTime;
    418418begin
     
    420420    cTurn, cContinue:
    421421    begin
    422       if RO.Alive and (1 shl me) = 0 then
     422      if RO.Alive and (1 shl Me) = 0 then
    423423      begin // I'm dead, huhu
    424         Server(sTurn, me, 0, nodata^);
    425         exit;
     424        Server(sTurn, Me, 0, nodata^);
     425        Exit;
    426426      end;
    427427      if Command = cTurn then
    428428      begin
    429         fillchar(cixStateImp, sizeof(cixStateImp), $FF);
     429        FillChar(cixStateImp, SizeOf(cixStateImp), $FF);
    430430        for cix := 0 to RO.nCity - 1 do
    431431          if MyCity[cix].Loc >= 0 then
     
    437437          NewGov := ChooseGovernment;
    438438          if NewGov > gAnarchy then
    439             Server(sSetGovernment, me, NewGov, nodata^);
     439            Server(sSetGovernment, Me, NewGov, nodata^);
    440440        end;
    441441        HaveTurned := False;
     
    446446        if OnNegoRejected_CancelTreaty then
    447447          if RO.Treaty[Opponent] >= trPeace then
    448             if Server(sCancelTreaty, me, 0, nodata^) < rExecuted then
    449               assert(False);
     448            if Server(sCancelTreaty, Me, 0, nodata^) < rExecuted then
     449              Assert(False);
    450450      end
    451451      else
     
    459459        if RO.Government <> gAnarchy then
    460460          for Nation := 0 to nPl - 1 do
    461             if (Nation <> me) and (1 shl Nation and RO.Alive <> 0) and
     461            if (Nation <> Me) and (1 shl Nation and RO.Alive <> 0) and
    462462              (RO.Treaty[Nation] >= trNone) and not (Nation in Contacted) and not
    463463              (Nation in UnwantedNego) and
    464               (Server(scContact - sExecute + Nation shl 4, me, 0, nodata^) >= rExecuted) then
     464              (Server(scContact - sExecute + Nation shl 4, Me, 0, nodata^) >= rExecuted) then
    465465              if WantNegotiation(Nation, NegoTime) then
    466466              begin
    467                 if Server(scContact + Nation shl 4, me, 0, nodata^) >= rExecuted then
     467                if Server(scContact + Nation shl 4, Me, 0, nodata^) >= rExecuted then
    468468                begin
    469                   include(Contacted, Nation);
     469                  Include(Contacted, Nation);
    470470                  Opponent := Nation;
    471471                  MyAction := scContact;
    472                   exit;
     472                  Exit;
    473473                end;
    474474              end
    475475              else
    476                 include(UnwantedNego, Nation);
     476                Include(UnwantedNego, Nation);
    477477        if NegoTime = BeginOfTurn then
    478478        begin
     
    483483        end
    484484        else
    485           break;
     485          Break;
    486486      until False;
    487487      if RO.Happened and phTech <> 0 then
     
    499499            end;
    500500        end;
    501         Server(sSetResearch, me, NewResearch, nodata^);
     501        Server(sSetResearch, Me, NewResearch, nodata^);
    502502      end;
    503       if Server(sTurn, me, 0, nodata^) < rExecuted then
    504         assert(False);
     503      if Server(sTurn, Me, 0, nodata^) < rExecuted then
     504        Assert(False);
    505505    end;
    506506    scContact:
    507       if WantNegotiation(integer(Data), EnemyCalled) then
     507      if WantNegotiation(Integer(Data), EnemyCalled) then
    508508      begin
    509         if Server(scDipStart, me, 0, nodata^) < rExecuted then
    510           assert(False);
    511         Opponent := integer(Data);
     509        if Server(scDipStart, Me, 0, nodata^) < rExecuted then
     510          Assert(False);
     511        Opponent := Integer(Data);
    512512        MyAction := scDipStart;
    513513      end
    514514      else
    515515      begin
    516         if Server(scReject, me, 0, nodata^) < rExecuted then
    517           assert(False);
     516        if Server(scReject, Me, 0, nodata^) < rExecuted then
     517          Assert(False);
    518518      end;
    519519    scDipStart, scDipNotice, scDipAccept, scDipCancelTreaty, scDipOffer, scDipBreak:
     
    538538      end;
    539539      DoNegotiation;
    540       assert((MyAction = scDipNotice) or (MyAction = scDipAccept) or
     540      Assert((MyAction = scDipNotice) or (MyAction = scDipAccept) or
    541541        (MyAction = scDipCancelTreaty) or (MyAction = scDipOffer) or (MyAction = scDipBreak));
    542542      if MyAction = scDipOffer then
    543         Server(MyAction, me, 0, MyOffer)
     543        Server(MyAction, Me, 0, MyOffer)
    544544      else
    545         Server(MyAction, me, 0, nodata^);
     545        Server(MyAction, Me, 0, nodata^);
    546546    end;
    547547    cShowEndContact:
     
    568568
    569569procedure TCustomAI.OnBeforeEnemyAttack(UnitInfo: TUnitInfo;
    570   ToLoc, EndHealth, EndHealthDef: integer);
    571 begin
    572 end;
    573 
    574 procedure TCustomAI.OnBeforeEnemyCapture(UnitInfo: TUnitInfo; ToLoc: integer);
     570  ToLoc, EndHealth, EndHealthDef: Integer);
     571begin
     572end;
     573
     574procedure TCustomAI.OnBeforeEnemyCapture(UnitInfo: TUnitInfo; ToLoc: Integer);
    575575begin
    576576end;
     
    584584end;
    585585
    586 function TCustomAI.ChooseResearchAdvance: integer;
     586function TCustomAI.ChooseResearchAdvance: Integer;
    587587begin
    588588  Result := -1;
    589589end;
    590590
    591 function TCustomAI.ChooseStealAdvance: integer;
     591function TCustomAI.ChooseStealAdvance: Integer;
    592592begin
    593593  Result := -1;
    594594end;
    595595
    596 function TCustomAI.ChooseGovernment: integer;
     596function TCustomAI.ChooseGovernment: Integer;
    597597begin
    598598  Result := gDespotism;
    599599end;
    600600
    601 function TCustomAI.WantNegotiation(Nation: integer; NegoTime: TNegoTime): boolean;
     601function TCustomAI.WantNegotiation(Nation: Integer; NegoTime: TNegoTime): Boolean;
    602602begin
    603603  Result := False;
    604604end;
    605605
    606 function TCustomAI.OnNegoRejected_CancelTreaty: boolean;
     606function TCustomAI.OnNegoRejected_CancelTreaty: Boolean;
    607607begin
    608608  Result := False;
     
    613613procedure TCustomAI.StealAdvance;
    614614var
    615   Steal, ad, Count: integer;
     615  Steal, ad, Count: Integer;
    616616begin
    617617  Steal := ChooseStealAdvance;
     
    628628  end;
    629629  if Steal >= 0 then
    630     Server(sStealTech, me, Steal, nodata^);
     630    Server(sStealTech, Me, Steal, nodata^);
    631631  RO.Happened := RO.Happened and not phStealTech;
    632632end;
    633633
    634 function TCustomAI.IsResearched(Advance: integer): boolean;
     634function TCustomAI.IsResearched(Advance: Integer): Boolean;
    635635begin
    636636  Result := (Advance = preNone) or (Advance <> preNA) and (RO.Tech[Advance] >= tsApplicable);
    637637end;
    638638
    639 function TCustomAI.ResearchCost: integer;
    640 begin
    641   Server(sGetTechCost, me, 0, Result);
    642 end;
    643 
    644 function TCustomAI.ChangeAttitude(Nation, Attitude: integer): integer;
    645 begin
    646   Result := Server(sSetAttitude + Nation shl 4, me, Attitude, nodata^);
    647 end;
    648 
    649 function TCustomAI.Revolution: integer;
    650 begin
    651   Result := Server(sRevolution, me, 0, nodata^);
    652 end;
    653 
    654 function TCustomAI.ChangeRates(Tax, Lux: integer): integer;
    655 begin
    656   Result := Server(sSetRates, me, Tax div 10 and $f + Lux div 10 and $f shl 4, nodata^);
    657 end;
    658 
    659 function TCustomAI.PrepareNewModel(Domain: integer): integer;
    660 begin
    661   Result := Server(sCreateDevModel, me, Domain, nodata^);
    662 end;
    663 
    664 function TCustomAI.SetNewModelFeature(F, Count: integer): integer;
    665 begin
    666   Result := Server(sSetDevModelCap + Count shl 4, me, F, nodata^);
    667 end;
    668 
    669 function TCustomAI.AdvanceResearchable(Advance: integer): boolean;
    670 begin
    671   Result := Server(sSetResearch - sExecute, me, Advance, nodata^) >= rExecuted;
    672 end;
    673 
    674 function TCustomAI.AdvanceStealable(Advance: integer): boolean;
    675 begin
    676   Result := Server(sStealTech - sExecute, me, Advance, nodata^) >= rExecuted;
    677 end;
    678 
    679 function TCustomAI.GetJobProgress(Loc: integer;
    680   var JobProgress: TJobProgressData): boolean;
    681 begin
    682   Result := Server(sGetJobProgress, me, Loc, JobProgress) >= rExecuted;
    683 end;
    684 
    685 function TCustomAI.DebugMessage(Level: integer; Text: string): boolean;
    686 begin
    687   Text := copy('P' + char(48 + me) + ' ' + Text, 1, 254);
    688   Server(sMessage, me, Level, PChar(Text)^);
     639function TCustomAI.ResearchCost: Integer;
     640begin
     641  Server(sGetTechCost, Me, 0, Result);
     642end;
     643
     644function TCustomAI.ChangeAttitude(Nation, Attitude: Integer): Integer;
     645begin
     646  Result := Server(sSetAttitude + Nation shl 4, Me, Attitude, nodata^);
     647end;
     648
     649function TCustomAI.Revolution: Integer;
     650begin
     651  Result := Server(sRevolution, Me, 0, nodata^);
     652end;
     653
     654function TCustomAI.ChangeRates(Tax, Lux: Integer): Integer;
     655begin
     656  Result := Server(sSetRates, Me, Tax div 10 and $F + Lux div 10 and $F shl 4, nodata^);
     657end;
     658
     659function TCustomAI.PrepareNewModel(Domain: Integer): Integer;
     660begin
     661  Result := Server(sCreateDevModel, Me, Domain, nodata^);
     662end;
     663
     664function TCustomAI.SetNewModelFeature(F, Count: Integer): Integer;
     665begin
     666  Result := Server(sSetDevModelCap + Count shl 4, Me, F, nodata^);
     667end;
     668
     669function TCustomAI.AdvanceResearchable(Advance: Integer): Boolean;
     670begin
     671  Result := Server(sSetResearch - sExecute, Me, Advance, nodata^) >= rExecuted;
     672end;
     673
     674function TCustomAI.AdvanceStealable(Advance: Integer): Boolean;
     675begin
     676  Result := Server(sStealTech - sExecute, Me, Advance, nodata^) >= rExecuted;
     677end;
     678
     679function TCustomAI.GetJobProgress(Loc: Integer;
     680  var JobProgress: TJobProgressData): Boolean;
     681begin
     682  Result := Server(sGetJobProgress, Me, Loc, JobProgress) >= rExecuted;
     683end;
     684
     685function TCustomAI.DebugMessage(Level: Integer; Text: string): Boolean;
     686begin
     687  Text := Copy('P' + char(48 + Me) + ' ' + Text, 1, 254);
     688  Server(sMessage, Me, Level, PChar(Text)^);
    689689
    690690  Result := True;
     
    693693end;
    694694
    695 function TCustomAI.SetDebugMap(var DebugMap): boolean;
    696 begin
    697   Server(sSetDebugMap, me, 0, DebugMap);
     695function TCustomAI.SetDebugMap(var DebugMap): Boolean;
     696begin
     697  Server(sSetDebugMap, Me, 0, DebugMap);
    698698
    699699  Result := True;
     
    702702end;
    703703
    704 procedure TCustomAI.Unit_FindMyDefender(Loc: integer; var uix: integer);
    705 begin
    706   if Server(sGetDefender, me, Loc, uix) < rExecuted then
     704procedure TCustomAI.Unit_FindMyDefender(Loc: Integer; var uix: Integer);
     705begin
     706  if Server(sGetDefender, Me, Loc, uix) < rExecuted then
    707707    uix := -1;
    708708end;
    709709
    710 procedure TCustomAI.Unit_FindEnemyDefender(Loc: integer; var euix: integer);
     710procedure TCustomAI.Unit_FindEnemyDefender(Loc: Integer; var euix: Integer);
    711711begin
    712712  euix := RO.nEnemyUn - 1;
     
    715715end;
    716716
    717 function TCustomAI.Unit_Move(uix, ToLoc: integer): integer;
    718 var
    719   Step: integer;
    720   DestinationReached: boolean;
     717function TCustomAI.Unit_Move(uix, ToLoc: Integer): Integer;
     718var
     719  Step: Integer;
     720  DestinationReached: Boolean;
    721721  Advice: TMoveAdviceData;
    722722begin
    723   assert((uix >= 0) and (uix < RO.nUn) and (MyUnit[uix].Loc >= 0)); // is a unit
     723  Assert((uix >= 0) and (uix < RO.nUn) and (MyUnit[uix].Loc >= 0)); // is a unit
    724724{Loc_to_ab(MyUnit[uix].Loc,ToLoc,a,b);
    725 assert((a<>0) or (b<>0));
    726 if (a>=-1) and (a<=1) and (b>=-1) and (b<=1) then
     725Assert((A<>0) or (B<>0));
     726if (A>=-1) and (A<=1) and (B>=-1) and (B<=1) then
    727727  begin // move to adjacent tile
    728   !!!problem: if move is invalid, return codes are not consistent with other branch (eNoWay)
     728  !!!problem: if Move is invalid, return codes are not consistent with other branch (eNoWay)
    729729  Advice.nStep:=1;
    730   Advice.dx[0]:=a-b;
    731   Advice.dy[0]:=a+b;
     730  Advice.dx[0]:=A-B;
     731  Advice.dy[0]:=A+B;
    732732  Advice.MoreTurns:=0;
    733733  Advice.MaxHostile_MovementLeft:=MyUnit[uix].Movement;
    734   result:=eOK;
     734  Result:=eOK;
    735735  end
    736736else}
     
    739739    Advice.MoreTurns := 9999;
    740740    Advice.MaxHostile_MovementLeft := 100;
    741     Result := Server(sGetMoveAdvice, me, uix, Advice);
     741    Result := Server(sGetMoveAdvice, Me, uix, Advice);
    742742  end;
    743743  if Result = eOk then
     
    755755        begin
    756756          DestinationReached := True;
    757           break;
     757          Break;
    758758        end // stop next to destination
    759759        else if Step = Advice.nStep then
     
    761761
    762762      if (Step = Advice.nStep) or (Result <> eOK) and (Result <> eLoaded) then
    763         break;
     763        Break;
    764764
    765765      Result := Server(sMoveUnit + (Advice.dx[Step] and 7) shl 4 +
    766         (Advice.dy[Step] and 7) shl 7, me, uix, nodata^);
     766        (Advice.dy[Step] and 7) shl 7, Me, uix, nodata^);
    767767      Inc(Step);
    768768      if RO.Happened and phStealTech <> 0 then
     
    779779end;
    780780
    781 function TCustomAI.Unit_Step(uix, ToLoc: integer): integer;
    782 var
    783   a, b: integer;
    784 begin
    785   Loc_to_ab(MyUnit[uix].Loc, ToLoc, a, b);
    786   assert(((a <> 0) or (b <> 0)) and (a >= -1) and (a <= 1) and (b >= -1) and (b <= 1));
    787   Result := Server(sMoveUnit + ((a - b) and 7) shl 4 + ((a + b) and 7) shl 7, me, uix, nodata^);
     781function TCustomAI.Unit_Step(uix, ToLoc: Integer): Integer;
     782var
     783  A, B: Integer;
     784begin
     785  Loc_to_ab(MyUnit[uix].Loc, ToLoc, A, B);
     786  Assert(((A <> 0) or (B <> 0)) and (A >= -1) and (A <= 1) and (B >= -1) and (B <= 1));
     787  Result := Server(sMoveUnit + ((A - B) and 7) shl 4 + ((A + B) and 7) shl 7, Me, uix, nodata^);
    788788  if RO.Happened and phStealTech <> 0 then
    789789    StealAdvance;
    790790end;
    791791
    792 function TCustomAI.Unit_Attack(uix, ToLoc: integer): integer;
    793 var
    794   a, b: integer;
    795 begin
    796   assert((uix >= 0) and (uix < RO.nUn) and (MyUnit[uix].Loc >= 0) // is a unit
     792function TCustomAI.Unit_Attack(uix, ToLoc: Integer): Integer;
     793var
     794  A, B: Integer;
     795begin
     796  Assert((uix >= 0) and (uix < RO.nUn) and (MyUnit[uix].Loc >= 0) // is a unit
    797797    and ((Map[ToLoc] and (fUnit or fOwned) = fUnit) // is an attack
    798798    or (Map[ToLoc] and (fCity or fOwned) = fCity) and
    799799    (MyModel[MyUnit[uix].mix].Domain <> dGround))); // is a bombardment
    800   Loc_to_ab(MyUnit[uix].Loc, ToLoc, a, b);
    801   assert(((a <> 0) or (b <> 0)) and (a >= -1) and (a <= 1) and (b >= -1) and (b <= 1));
     800  Loc_to_ab(MyUnit[uix].Loc, ToLoc, A, B);
     801  Assert(((A <> 0) or (B <> 0)) and (A >= -1) and (A <= 1) and (B >= -1) and (B <= 1));
    802802  // attack to adjacent tile
    803   Result := Server(sMoveUnit + (a - b) and 7 shl 4 + (a + b) and 7 shl 7, me, uix, nodata^);
    804 end;
    805 
    806 function TCustomAI.Unit_DoMission(uix, MissionType, ToLoc: integer): integer;
    807 var
    808   a, b: integer;
    809 begin
    810   Result := Server(sSetSpyMission + MissionType shl 4, me, 0, nodata^);
     803  Result := Server(sMoveUnit + (A - B) and 7 shl 4 + (A + B) and 7 shl 7, Me, uix, nodata^);
     804end;
     805
     806function TCustomAI.Unit_DoMission(uix, MissionType, ToLoc: Integer): Integer;
     807var
     808  A, B: Integer;
     809begin
     810  Result := Server(sSetSpyMission + MissionType shl 4, Me, 0, nodata^);
    811811  if Result >= rExecuted then
    812812  begin
    813     assert((uix >= 0) and (uix < RO.nUn) and (MyUnit[uix].Loc >= 0) // is a unit
     813    Assert((uix >= 0) and (uix < RO.nUn) and (MyUnit[uix].Loc >= 0) // is a unit
    814814      and (MyModel[MyUnit[uix].mix].Kind = mkDiplomat)); // is a commando
    815     Loc_to_ab(MyUnit[uix].Loc, ToLoc, a, b);
    816     assert(((a <> 0) or (b <> 0)) and (a >= -1) and (a <= 1) and (b >= -1) and (b <= 1));
     815    Loc_to_ab(MyUnit[uix].Loc, ToLoc, A, B);
     816    Assert(((A <> 0) or (B <> 0)) and (A >= -1) and (A <= 1) and (B >= -1) and (B <= 1));
    817817    // city must be adjacent
    818     Result := Server(sMoveUnit - sExecute + (a - b) and 7 shl 4 + (a + b) and 7 shl 7, me, uix, nodata^);
     818    Result := Server(sMoveUnit - sExecute + (A - B) and 7 shl 4 + (A + B) and 7 shl 7, Me, uix, nodata^);
    819819    if Result = eMissionDone then
    820       Result := Server(sMoveUnit + (a - b) and 7 shl 4 + (a + b) and 7 shl 7, me, uix, nodata^)
     820      Result := Server(sMoveUnit + (A - B) and 7 shl 4 + (A + B) and 7 shl 7, Me, uix, nodata^)
    821821    else if (Result <> eNoTime_Move) and (Result <> eTreaty) and (Result <> eNoTurn) then
    822822      Result := eInvalid; // not a special commando mission!
     
    824824end;
    825825
    826 function TCustomAI.Unit_MoveForecast(uix, ToLoc: integer;
    827   var RemainingMovement: integer): boolean;
     826function TCustomAI.Unit_MoveForecast(uix, ToLoc: Integer;
     827  var RemainingMovement: Integer): Boolean;
    828828var
    829829  Advice: TMoveAdviceData;
    830830begin
    831   assert((uix >= 0) and (uix < RO.nUn) and (MyUnit[uix].Loc >= 0)); // is a unit
     831  Assert((uix >= 0) and (uix < RO.nUn) and (MyUnit[uix].Loc >= 0)); // is a unit
    832832  Advice.ToLoc := ToLoc;
    833833  Advice.MoreTurns := 0;
    834834  Advice.MaxHostile_MovementLeft := 100;
    835   if Server(sGetMoveAdvice, me, uix, Advice) = eOk then
     835  if Server(sGetMoveAdvice, Me, uix, Advice) = eOk then
    836836  begin
    837837    RemainingMovement := Advice.MaxHostile_MovementLeft;
     
    846846
    847847// negative RemainingHealth is remaining helth of defender if lost
    848 function TCustomAI.Unit_AttackForecast(uix, ToLoc, AttackMovement: integer;
    849   var RemainingHealth: integer): boolean;
     848function TCustomAI.Unit_AttackForecast(uix, ToLoc, AttackMovement: Integer;
     849  var RemainingHealth: Integer): Boolean;
    850850var
    851851  BattleForecast: TBattleForecast;
    852852begin
    853   assert((uix >= 0) and (uix < RO.nUn) and (MyUnit[uix].Loc >= 0) // is a unit
     853  Assert((uix >= 0) and (uix < RO.nUn) and (MyUnit[uix].Loc >= 0) // is a unit
    854854    and (Map[ToLoc] and (fUnit or fOwned) = fUnit)); // is an attack
    855855  RemainingHealth := -$100;
     
    858858    with MyUnit[uix] do
    859859    begin
    860       BattleForecast.pAtt := me;
     860      BattleForecast.pAtt := Me;
    861861      BattleForecast.mixAtt := mix;
    862862      BattleForecast.HealthAtt := Health;
     
    864864      BattleForecast.FlagsAtt := Flags;
    865865      BattleForecast.Movement := AttackMovement;
    866       if Server(sGetBattleForecast, me, ToLoc, BattleForecast) >= rExecuted then
     866      if Server(sGetBattleForecast, Me, ToLoc, BattleForecast) >= rExecuted then
    867867      begin
    868868        if BattleForecast.EndHealthAtt > 0 then
     
    875875end;
    876876
    877 function TCustomAI.Unit_DefenseForecast(euix, ToLoc: integer;
    878   var RemainingHealth: integer): boolean;
     877function TCustomAI.Unit_DefenseForecast(euix, ToLoc: Integer;
     878  var RemainingHealth: Integer): Boolean;
    879879var
    880880  BattleForecast: TBattleForecast;
    881881begin
    882   assert((euix >= 0) and (euix < RO.nEnemyUn) and (RO.EnemyUn[euix].Loc >= 0) // is an enemy unit
     882  Assert((euix >= 0) and (euix < RO.nEnemyUn) and (RO.EnemyUn[euix].Loc >= 0) // is an enemy unit
    883883    and (Map[ToLoc] and (fUnit or fOwned) = (fUnit or fOwned))); // is an attack
    884884  RemainingHealth := $100;
     
    892892    BattleForecast.FlagsAtt := Flags;
    893893    BattleForecast.Movement := 100;
    894     if Server(sGetBattleForecast, me, ToLoc, BattleForecast) >= rExecuted then
     894    if Server(sGetBattleForecast, Me, ToLoc, BattleForecast) >= rExecuted then
    895895    begin
    896896      if BattleForecast.EndHealthDef > 0 then
     
    903903end;
    904904
    905 function TCustomAI.Unit_Disband(uix: integer): integer;
    906 begin
    907   Result := Server(sRemoveUnit, me, uix, nodata^);
    908 end;
    909 
    910 function TCustomAI.Unit_StartJob(uix, NewJob: integer): integer;
    911 begin
    912   Result := Server(sStartJob + NewJob shl 4, me, uix, nodata^);
    913 end;
    914 
    915 function TCustomAI.Unit_SetHomeHere(uix: integer): integer;
    916 begin
    917   Result := Server(sSetUnitHome, me, uix, nodata^);
    918 end;
    919 
    920 function TCustomAI.Unit_Load(uix: integer): integer;
    921 begin
    922   Result := Server(sLoadUnit, me, uix, nodata^);
    923 end;
    924 
    925 function TCustomAI.Unit_Unload(uix: integer): integer;
    926 begin
    927   Result := Server(sUnloadUnit, me, uix, nodata^);
    928 end;
    929 
    930 function TCustomAI.Unit_AddToCity(uix: integer): integer;
    931 begin
    932   Result := Server(sAddToCity, me, uix, nodata^);
    933 end;
    934 
    935 function TCustomAI.Unit_SelectTransport(uix: integer): integer;
    936 begin
    937   Result := Server(sSelectTransport, me, uix, nodata^);
    938 end;
    939 
    940 
    941 procedure TCustomAI.City_FindMyCity(Loc: integer; var cix: integer);
     905function TCustomAI.Unit_Disband(uix: Integer): Integer;
     906begin
     907  Result := Server(sRemoveUnit, Me, uix, nodata^);
     908end;
     909
     910function TCustomAI.Unit_StartJob(uix, NewJob: Integer): Integer;
     911begin
     912  Result := Server(sStartJob + NewJob shl 4, Me, uix, nodata^);
     913end;
     914
     915function TCustomAI.Unit_SetHomeHere(uix: Integer): Integer;
     916begin
     917  Result := Server(sSetUnitHome, Me, uix, nodata^);
     918end;
     919
     920function TCustomAI.Unit_Load(uix: Integer): Integer;
     921begin
     922  Result := Server(sLoadUnit, Me, uix, nodata^);
     923end;
     924
     925function TCustomAI.Unit_Unload(uix: Integer): Integer;
     926begin
     927  Result := Server(sUnloadUnit, Me, uix, nodata^);
     928end;
     929
     930function TCustomAI.Unit_AddToCity(uix: Integer): Integer;
     931begin
     932  Result := Server(sAddToCity, Me, uix, nodata^);
     933end;
     934
     935function TCustomAI.Unit_SelectTransport(uix: Integer): Integer;
     936begin
     937  Result := Server(sSelectTransport, Me, uix, nodata^);
     938end;
     939
     940
     941procedure TCustomAI.City_FindMyCity(Loc: Integer; var cix: Integer);
    942942begin
    943943  if Map[Loc] and (fCity or fOwned) <> fCity or fOwned then
     
    951951end;
    952952
    953 procedure TCustomAI.City_FindEnemyCity(Loc: integer; var ecix: integer);
     953procedure TCustomAI.City_FindEnemyCity(Loc: Integer; var ecix: Integer);
    954954begin
    955955  if Map[Loc] and (fCity or fOwned) <> fCity then
     
    963963end;
    964964
    965 function TCustomAI.City_HasProject(cix: integer): boolean;
     965function TCustomAI.City_HasProject(cix: Integer): Boolean;
    966966begin
    967967  Result := MyCity[cix].Project and (cpImp + cpIndex) <> cpImp + imTrGoods;
    968968end;
    969969
    970 function TCustomAI.City_CurrentImprovementProject(cix: integer): integer;
     970function TCustomAI.City_CurrentImprovementProject(cix: Integer): Integer;
    971971begin
    972972  if MyCity[cix].Project and cpImp = 0 then
     
    980980end;
    981981
    982 function TCustomAI.City_CurrentUnitProject(cix: integer): integer;
     982function TCustomAI.City_CurrentUnitProject(cix: Integer): Integer;
    983983begin
    984984  if MyCity[cix].Project and cpImp <> 0 then
     
    988988end;
    989989
    990 function TCustomAI.City_GetTileInfo(cix, TileLoc: integer;
    991   var TileInfo: TTileInfo): integer;
     990function TCustomAI.City_GetTileInfo(cix, TileLoc: Integer;
     991  var TileInfo: TTileInfo): Integer;
    992992begin
    993993  TileInfo.ExplCity := cix;
    994   Result := Server(sGetHypoCityTileInfo, me, TileLoc, TileInfo);
    995 end;
    996 
    997 function TCustomAI.City_GetReport(cix: integer; var Report: TCityReport): integer;
     994  Result := Server(sGetHypoCityTileInfo, Me, TileLoc, TileInfo);
     995end;
     996
     997function TCustomAI.City_GetReport(cix: Integer; var Report: TCityReport): Integer;
    998998begin
    999999  Report.HypoTiles := -1;
    10001000  Report.HypoTax := -1;
    10011001  Report.HypoLux := -1;
    1002   Result := Server(sGetCityReport, me, cix, Report);
    1003 end;
    1004 
    1005 function TCustomAI.City_GetHypoReport(cix, HypoTiles, HypoTax, HypoLux: integer;
    1006   var Report: TCityReport): integer;
     1002  Result := Server(sGetCityReport, Me, cix, Report);
     1003end;
     1004
     1005function TCustomAI.City_GetHypoReport(cix, HypoTiles, HypoTax, HypoLux: Integer;
     1006  var Report: TCityReport): Integer;
    10071007begin
    10081008  Report.HypoTiles := HypoTiles;
    10091009  Report.HypoTax := HypoTax;
    10101010  Report.HypoLux := HypoLux;
    1011   Result := Server(sGetCityReport, me, cix, Report);
    1012 end;
    1013 
    1014 function TCustomAI.City_GetReportNew(cix: integer; var Report: TCityReportNew): integer;
     1011  Result := Server(sGetCityReport, Me, cix, Report);
     1012end;
     1013
     1014function TCustomAI.City_GetReportNew(cix: Integer; var Report: TCityReportNew): Integer;
    10151015begin
    10161016  Report.HypoTiles := -1;
    10171017  Report.HypoTaxRate := -1;
    10181018  Report.HypoLuxuryRate := -1;
    1019   Result := Server(sGetCityReportNew, me, cix, Report);
     1019  Result := Server(sGetCityReportNew, Me, cix, Report);
    10201020end;
    10211021
    10221022function TCustomAI.City_GetHypoReportNew(cix, HypoTiles, HypoTaxRate,
    1023   HypoLuxuryRate: integer; var Report: TCityReportNew): integer;
     1023  HypoLuxuryRate: Integer; var Report: TCityReportNew): Integer;
    10241024begin
    10251025  Report.HypoTiles := HypoTiles;
    10261026  Report.HypoTaxRate := HypoTaxRate;
    10271027  Report.HypoLuxuryRate := HypoLuxuryRate;
    1028   Result := Server(sGetCityReportNew, me, cix, Report);
    1029 end;
    1030 
    1031 function TCustomAI.City_GetAreaInfo(cix: integer; var AreaInfo: TCityAreaInfo): integer;
    1032 begin
    1033   Result := Server(sGetCityAreaInfo, me, cix, AreaInfo);
    1034 end;
    1035 
    1036 function TCustomAI.City_StartUnitProduction(cix, mix: integer): integer;
     1028  Result := Server(sGetCityReportNew, Me, cix, Report);
     1029end;
     1030
     1031function TCustomAI.City_GetAreaInfo(cix: Integer; var AreaInfo: TCityAreaInfo): Integer;
     1032begin
     1033  Result := Server(sGetCityAreaInfo, Me, cix, AreaInfo);
     1034end;
     1035
     1036function TCustomAI.City_StartUnitProduction(cix, mix: Integer): Integer;
    10371037begin
    10381038  if (MyCity[cix].Project and (cpImp + cpIndex) <> mix) then
    10391039    // not already producing that
    1040     Result := Server(sSetCityProject, me, cix, mix);
    1041 end;
    1042 
    1043 function TCustomAI.City_StartEmigration(cix, mix: integer;
    1044   AllowDisbandCity, AsConscripts: boolean): integer;
    1045 var
    1046   NewProject: integer;
     1040    Result := Server(sSetCityProject, Me, cix, mix);
     1041end;
     1042
     1043function TCustomAI.City_StartEmigration(cix, mix: Integer;
     1044  AllowDisbandCity, AsConscripts: Boolean): Integer;
     1045var
     1046  NewProject: Integer;
    10471047begin
    10481048  NewProject := mix;
     
    10511051  if AsConscripts then
    10521052    NewProject := NewProject or cpConscripts;
    1053   Result := Server(sSetCityProject, me, cix, NewProject);
    1054 end;
    1055 
    1056 function TCustomAI.City_StartImprovement(cix, iix: integer): integer;
    1057 var
    1058   NewProject: integer;
     1053  Result := Server(sSetCityProject, Me, cix, NewProject);
     1054end;
     1055
     1056function TCustomAI.City_StartImprovement(cix, iix: Integer): Integer;
     1057var
     1058  NewProject: Integer;
    10591059begin
    10601060  NewProject := iix + cpImp;
    10611061  if (MyCity[cix].Project and (cpImp + cpIndex) <> NewProject) then
    10621062    // not already producing that
    1063     Result := Server(sSetCityProject, me, cix, NewProject);
    1064 end;
    1065 
    1066 function TCustomAI.City_Improvable(cix, iix: integer): boolean;
    1067 var
    1068   NewProject: integer;
     1063    Result := Server(sSetCityProject, Me, cix, NewProject);
     1064end;
     1065
     1066function TCustomAI.City_Improvable(cix, iix: Integer): Boolean;
     1067var
     1068  NewProject: Integer;
    10691069begin
    10701070  NewProject := iix + cpImp;
    1071   Result := Server(sSetCityProject - sExecute, me, cix, NewProject) >= rExecuted;
    1072 end;
    1073 
    1074 function TCustomAI.City_StopProduction(cix: integer): integer;
    1075 var
    1076   NewProject: integer;
     1071  Result := Server(sSetCityProject - sExecute, Me, cix, NewProject) >= rExecuted;
     1072end;
     1073
     1074function TCustomAI.City_StopProduction(cix: Integer): Integer;
     1075var
     1076  NewProject: Integer;
    10771077begin
    10781078  NewProject := imTrGoods + cpImp;
    1079   Result := Server(sSetCityProject, me, cix, NewProject);
    1080 end;
    1081 
    1082 function TCustomAI.City_BuyProject(cix: integer): integer;
    1083 begin
    1084   Result := Server(sBuyCityProject, me, cix, nodata^);
    1085 end;
    1086 
    1087 function TCustomAI.City_SellImprovement(cix, iix: integer): integer;
    1088 begin
    1089   Result := Server(sSellCityImprovement, me, cix, iix);
    1090 end;
    1091 
    1092 function TCustomAI.City_RebuildImprovement(cix, iix: integer): integer;
    1093 begin
    1094   Result := Server(sRebuildCityImprovement, me, cix, iix);
    1095 end;
    1096 
    1097 function TCustomAI.City_SetTiles(cix, NewTiles: integer): integer;
    1098 begin
    1099   Result := Server(sSetCityTiles, me, cix, NewTiles);
    1100 end;
    1101 
    1102 procedure TCustomAI.City_OptimizeTiles(cix: integer; ResourceWeights: cardinal);
     1079  Result := Server(sSetCityProject, Me, cix, NewProject);
     1080end;
     1081
     1082function TCustomAI.City_BuyProject(cix: Integer): Integer;
     1083begin
     1084  Result := Server(sBuyCityProject, Me, cix, nodata^);
     1085end;
     1086
     1087function TCustomAI.City_SellImprovement(cix, iix: Integer): Integer;
     1088begin
     1089  Result := Server(sSellCityImprovement, Me, cix, iix);
     1090end;
     1091
     1092function TCustomAI.City_RebuildImprovement(cix, iix: Integer): Integer;
     1093begin
     1094  Result := Server(sRebuildCityImprovement, Me, cix, iix);
     1095end;
     1096
     1097function TCustomAI.City_SetTiles(cix, NewTiles: Integer): Integer;
     1098begin
     1099  Result := Server(sSetCityTiles, Me, cix, NewTiles);
     1100end;
     1101
     1102procedure TCustomAI.City_OptimizeTiles(cix: Integer; ResourceWeights: Cardinal);
    11031103var
    11041104  Advice: TCityTileAdviceData;
    11051105begin
    11061106  Advice.ResourceWeights := ResourceWeights;
    1107   Server(sGetCityTileAdvice, me, cix, Advice);
     1107  Server(sGetCityTileAdvice, Me, cix, Advice);
    11081108  City_SetTiles(cix, Advice.Tiles);
    11091109end;
     
    11111111
    11121112// negotiation
    1113 function TCustomAI.Nego_CheckMyAction: integer;
    1114 begin
    1115   assert(Opponent >= 0); // only allowed in negotiation mode
    1116   assert((MyAction = scDipNotice) or (MyAction = scDipAccept) or
     1113function TCustomAI.Nego_CheckMyAction: Integer;
     1114begin
     1115  Assert(Opponent >= 0); // only allowed in negotiation mode
     1116  Assert((MyAction = scDipNotice) or (MyAction = scDipAccept) or
    11171117    (MyAction = scDipCancelTreaty) or (MyAction = scDipOffer) or (MyAction = scDipBreak));
    11181118  if MyAction = scDipOffer then
    1119     Result := Server(MyAction - sExecute, me, 0, MyOffer)
     1119    Result := Server(MyAction - sExecute, Me, 0, MyOffer)
    11201120  else
    1121     Result := Server(MyAction - sExecute, me, 0, nodata^);
     1121    Result := Server(MyAction - sExecute, Me, 0, nodata^);
    11221122end;
    11231123
    11241124
    11251125initialization
    1126   nodata := pointer(0);
     1126  nodata := Pointer(0);
    11271127  RWDataSize := 0;
    11281128
  • trunk/AI/StdAI/CustomAI_Reload.pas

    r160 r447  
    1313TCustomAI=class
    1414public
    15   procedure Process(Command: integer; var Data);
     15  procedure Process(Command: Integer; var Data);
    1616
    1717  // overridables
    18   constructor Create(Nation: integer); virtual;
     18  constructor Create(Nation: Integer); virtual;
    1919  destructor Destroy; override;
    2020  procedure SetDataDefaults; virtual;
    2121  procedure SetDataRandom; virtual;
    2222  procedure OnBeforeEnemyAttack(UnitInfo: TUnitInfo;
    23     ToLoc, EndHealth, EndHealthDef: integer); virtual;
    24   procedure OnBeforeEnemyCapture(UnitInfo: TUnitInfo; ToLoc: integer); virtual;
     23    ToLoc, EndHealth, EndHealthDef: Integer); virtual;
     24  procedure OnBeforeEnemyCapture(UnitInfo: TUnitInfo; ToLoc: Integer); virtual;
    2525  procedure OnAfterEnemyAttack; virtual;
    2626  procedure OnAfterEnemyCapture; virtual;
    2727
    2828protected
    29   me: integer; // index of the controlled nation
     29  Me: Integer; // index of the controlled nation
    3030  RO: ^TPlayerContext;
    3131  Map: ^TTileList;
     
    3434  MyModel: ^TModelList;
    3535
    36   cixStateImp: array[imPalace..imSpacePort] of integer;
     36  cixStateImp: array[imPalace..imSpacePort] of Integer;
    3737
    3838  // negotiation
    39   Opponent: integer; // nation i'm in negotiation with, -1 indicates no-negotiation mode
    40   MyAction, MyLastAction, OppoAction: integer;
     39  Opponent: Integer; // nation i'm in negotiation with, -1 indicates no-negotiation mode
     40  MyAction, MyLastAction, OppoAction: Integer;
    4141  MyOffer, MyLastOffer, OppoOffer: TOffer;
    4242
     
    4444  procedure DoTurn; virtual;
    4545  procedure DoNegotiation; virtual;
    46   function ChooseResearchAdvance: integer; virtual;
    47   function ChooseStealAdvance: integer; virtual;
    48   function ChooseGovernment: integer; virtual;
    49   function WantNegotiation(Nation: integer; NegoTime: TNegoTime): boolean; virtual;
    50   function OnNegoRejected_CancelTreaty: boolean; virtual;
     46  function ChooseResearchAdvance: Integer; virtual;
     47  function ChooseStealAdvance: Integer; virtual;
     48  function ChooseGovernment: Integer; virtual;
     49  function WantNegotiation(Nation: Integer; NegoTime: TNegoTime): Boolean; virtual;
     50  function OnNegoRejected_CancelTreaty: Boolean; virtual;
    5151
    5252  // general functions
    53   function IsResearched(Advance: integer): boolean;
    54   function ResearchCost: integer;
    55   function ChangeAttitude(Nation, Attitude: integer): integer;
    56   function Revolution: integer;
    57   function ChangeRates(Tax,Lux: integer): integer;
    58   function PrepareNewModel(Domain: integer): integer;
    59   function SetNewModelFeature(F, Count: integer): integer;
    60   function AdvanceResearchable(Advance: integer): boolean;
    61   function AdvanceStealable(Advance: integer): boolean;
    62   function DebugMessage(Level: integer; Text: string): boolean;
    63   function SetDebugMap(var DebugMap): boolean;
     53  function IsResearched(Advance: Integer): Boolean;
     54  function ResearchCost: Integer;
     55  function ChangeAttitude(Nation, Attitude: Integer): Integer;
     56  function Revolution: Integer;
     57  function ChangeRates(Tax,Lux: Integer): Integer;
     58  function PrepareNewModel(Domain: Integer): Integer;
     59  function SetNewModelFeature(F, Count: Integer): Integer;
     60  function AdvanceResearchable(Advance: Integer): Boolean;
     61  function AdvanceStealable(Advance: Integer): Boolean;
     62  function DebugMessage(Level: Integer; Text: string): Boolean;
     63  function SetDebugMap(var DebugMap): Boolean;
    6464
    6565  // unit functions
    66   procedure Unit_FindMyDefender(Loc: integer; var uix: integer);
    67   procedure Unit_FindEnemyDefender(Loc: integer; var euix: integer);
    68   function Unit_Move(uix,ToLoc: integer): integer;
    69   function Unit_Step(uix,ToLoc: integer): integer;
    70   function Unit_Attack(uix,ToLoc: integer): integer;
    71   function Unit_DoMission(uix,MissionType,ToLoc: integer): integer;
    72   function Unit_MoveForecast(uix,ToLoc: integer; var RemainingMovement: integer): boolean;
    73   function Unit_AttackForecast(uix,ToLoc,AttackMovement: integer; var RemainingHealth: integer): boolean;
    74   function Unit_DefenseForecast(euix,ToLoc: integer; var RemainingHealth: integer): boolean;
    75   function Unit_Disband(uix: integer): integer;
    76   function Unit_StartJob(uix,NewJob: integer): integer;
    77   function Unit_SetHomeHere(uix: integer): integer;
    78   function Unit_Load(uix: integer): integer;
    79   function Unit_Unload(uix: integer): integer;
    80   function Unit_AddToCity(uix: integer): integer;
     66  procedure Unit_FindMyDefender(Loc: Integer; var uix: Integer);
     67  procedure Unit_FindEnemyDefender(Loc: Integer; var euix: Integer);
     68  function Unit_Move(uix,ToLoc: Integer): Integer;
     69  function Unit_Step(uix,ToLoc: Integer): Integer;
     70  function Unit_Attack(uix,ToLoc: Integer): Integer;
     71  function Unit_DoMission(uix,MissionType,ToLoc: Integer): Integer;
     72  function Unit_MoveForecast(uix,ToLoc: Integer; var RemainingMovement: Integer): Boolean;
     73  function Unit_AttackForecast(uix,ToLoc,AttackMovement: Integer; var RemainingHealth: Integer): Boolean;
     74  function Unit_DefenseForecast(euix,ToLoc: Integer; var RemainingHealth: Integer): Boolean;
     75  function Unit_Disband(uix: Integer): Integer;
     76  function Unit_StartJob(uix,NewJob: Integer): Integer;
     77  function Unit_SetHomeHere(uix: Integer): Integer;
     78  function Unit_Load(uix: Integer): Integer;
     79  function Unit_Unload(uix: Integer): Integer;
     80  function Unit_AddToCity(uix: Integer): Integer;
    8181
    8282  // city functions
    83   procedure City_FindMyCity(Loc: integer; var cix: integer);
    84   procedure City_FindEnemyCity(Loc: integer; var ecix: integer);
    85   function City_HasProject(cix: integer): boolean;
    86   function City_CurrentImprovementProject(cix: integer): integer;
    87   function City_CurrentUnitProject(cix: integer): integer;
    88   function City_GetTileInfo(cix,TileLoc: integer; var TileInfo: TTileInfo): integer;
    89   function City_GetReport(cix: integer; var Report: TCityReport): integer;
    90   function City_GetHypoReport(cix, HypoTiles, HypoTax, HypoLux: integer; var Report: TCityReport): integer;
    91   function City_GetAreaInfo(cix: integer; var AreaInfo: TCityAreaInfo): integer;
    92   function City_StartUnitProduction(cix,mix: integer): integer;
    93   function City_StartEmigration(cix,mix: integer; AllowDisbandCity, AsConscripts: boolean): integer;
    94   function City_StartImprovement(cix,iix: integer): integer;
    95   function City_Improvable(cix,iix: integer): boolean;
    96   function City_StopProduction(cix: integer): integer;
    97   function City_BuyProject(cix: integer): integer;
    98   function City_SellImprovement(cix,iix: integer): integer;
    99   function City_RebuildImprovement(cix,iix: integer): integer;
    100   function City_SetTiles(cix,NewTiles: integer): integer;
     83  procedure City_FindMyCity(Loc: Integer; var cix: Integer);
     84  procedure City_FindEnemyCity(Loc: Integer; var ecix: Integer);
     85  function City_HasProject(cix: Integer): Boolean;
     86  function City_CurrentImprovementProject(cix: Integer): Integer;
     87  function City_CurrentUnitProject(cix: Integer): Integer;
     88  function City_GetTileInfo(cix,TileLoc: Integer; var TileInfo: TTileInfo): Integer;
     89  function City_GetReport(cix: Integer; var Report: TCityReport): Integer;
     90  function City_GetHypoReport(cix, HypoTiles, HypoTax, HypoLux: Integer; var Report: TCityReport): Integer;
     91  function City_GetAreaInfo(cix: Integer; var AreaInfo: TCityAreaInfo): Integer;
     92  function City_StartUnitProduction(cix,mix: Integer): Integer;
     93  function City_StartEmigration(cix,mix: Integer; AllowDisbandCity, AsConscripts: Boolean): Integer;
     94  function City_StartImprovement(cix,iix: Integer): Integer;
     95  function City_Improvable(cix,iix: Integer): Boolean;
     96  function City_StopProduction(cix: Integer): Integer;
     97  function City_BuyProject(cix: Integer): Integer;
     98  function City_SellImprovement(cix,iix: Integer): Integer;
     99  function City_RebuildImprovement(cix,iix: Integer): Integer;
     100  function City_SetTiles(cix,NewTiles: Integer): Integer;
    101101
    102102  // negotiation
    103   function Nego_CheckMyAction: integer;
     103  function Nego_CheckMyAction: Integer;
    104104
    105105private
    106   HaveTurned: boolean;
     106  HaveTurned: Boolean;
    107107  UnwantedNego: set of 0..nPl-1;
    108108  Contacted: set of 0..nPl-1;
     
    114114Server: TServerCall;
    115115G: TNewGameData;
    116 RWDataSize, MapSize: integer;
    117 decompose24: cardinal;
    118 nodata: pointer;
     116RWDataSize, MapSize: Integer;
     117decompose24: Cardinal;
     118nodata: Pointer;
    119119
    120120const
     
    126126
    127127type
    128 TVicinity8Loc=array[0..7] of integer;
    129 TVicinity21Loc=array[0..27] of integer;
     128TVicinity8Loc=array[0..7] of Integer;
     129TVicinity21Loc=array[0..27] of Integer;
    130130
    131131
    132132procedure Init(NewGameData: TNewGameData);
    133133
    134 procedure ab_to_Loc(Loc0,a,b: integer; var Loc: integer);
    135 procedure Loc_to_ab(Loc0,Loc: integer; var a,b: integer);
    136 procedure ab_to_V8(a,b: integer; var V8: integer);
    137 procedure V8_to_ab(V8: integer; var a,b: integer);
    138 procedure ab_to_V21(a,b: integer; var V21: integer);
    139 procedure V21_to_ab(V21: integer; var a,b: integer);
    140 procedure V8_to_Loc(Loc0: integer; var VicinityLoc: TVicinity8Loc);
    141 procedure V21_to_Loc(Loc0: integer; var VicinityLoc: TVicinity21Loc);
     134procedure ab_to_Loc(Loc0,A,B: Integer; var Loc: Integer);
     135procedure Loc_to_ab(Loc0,Loc: Integer; var A,B: Integer);
     136procedure ab_to_V8(A,B: Integer; var V8: Integer);
     137procedure V8_to_ab(V8: Integer; var A,B: Integer);
     138procedure ab_to_V21(A,B: Integer; var V21: Integer);
     139procedure V21_to_ab(V21: Integer; var A,B: Integer);
     140procedure V8_to_Loc(Loc0: Integer; var VicinityLoc: TVicinity8Loc);
     141procedure V21_to_Loc(Loc0: Integer; var VicinityLoc: TVicinity21Loc);
    142142
    143143
     
    145145
    146146const
    147 ab_v8: array[-4..4] of integer = (5,6,7,4,-1,0,3,2,1);
    148 v8_a: array[0..7] of integer = (1,1,0,-1,-1,-1,0,1);
    149 v8_b: array[0..7] of integer = (0,1,1,1,0,-1,-1,-1);
    150 
    151 
    152 procedure ab_to_Loc(Loc0,a,b: integer; var Loc: integer);
     147ab_v8: array[-4..4] of Integer = (5,6,7,4,-1,0,3,2,1);
     148v8_a: array[0..7] of Integer = (1,1,0,-1,-1,-1,0,1);
     149v8_b: array[0..7] of Integer = (0,1,1,1,0,-1,-1,-1);
     150
     151
     152procedure ab_to_Loc(Loc0,A,B: Integer; var Loc: Integer);
    153153{relative location from Loc0}
    154154var
    155 y0: integer;
    156 begin
    157 assert((Loc0>=0) and (Loc0<MapSize) and (a-b+G.lx>=0));
    158 y0:=cardinal(Loc0)*decompose24 shr 24;
    159 Loc:=(Loc0+(a-b+y0 and 1+G.lx+G.lx) shr 1) mod G.lx +G.lx*(y0+a+b);
     155y0: Integer;
     156begin
     157Assert((Loc0>=0) and (Loc0<MapSize) and (A-B+G.lx>=0));
     158y0:=Cardinal(Loc0)*decompose24 shr 24;
     159Loc:=(Loc0+(A-B+y0 and 1+G.lx+G.lx) shr 1) mod G.lx +G.lx*(y0+A+B);
    160160if Loc>=MapSize then Loc:=-$1000
    161161end;
    162162
    163 procedure Loc_to_ab(Loc0,Loc: integer; var a,b: integer);
     163procedure Loc_to_ab(Loc0,Loc: Integer; var A,B: Integer);
    164164{$IFDEF FPC} // freepascal
    165165var
    166 dx,dy: integer;
     166dx,dy: Integer;
    167167begin
    168168dx:=((Loc mod G.lx *2 +Loc div G.lx and 1)
    169169  -(Loc0 mod G.lx *2 +Loc0 div G.lx and 1)+3*G.lx) mod (2*G.lx) -G.lx;
    170170dy:=Loc div G.lx-Loc0 div G.lx;
    171 a:=(dx+dy) div 2;
    172 b:=(dy-dx) div 2;
     171A:=(dx+dy) div 2;
     172B:=(dy-dx) div 2;
    173173end;
    174174{$ELSE} // delphi
     
    179179// calculate
    180180push ecx
    181 div byte ptr [G]
     181div Byte ptr [G]
    182182xor ebx,ebx
    183183mov bl,ah  // ebx:=Loc0 mod G.lx
     
    185185and ecx,$000000FF // ecx:=Loc0 div G.lx
    186186mov eax,edx
    187 div byte ptr [G]
     187div Byte ptr [G]
    188188xor edx,edx
    189189mov dl,ah // edx:=Loc mod G.lx
     
    202202mov edx,dword ptr [G]
    203203cmp eax,edx
    204 jl @a
     204jl @A
    205205  sub eax,edx
    206206  sub eax,edx
    207207  jmp @ok
    208 @a:
     208@A:
    209209neg edx
    210210cmp eax,edx
     
    219219add eax,ebx
    220220sar edx,1 // edx:=b
    221 mov ebx,[b]
     221mov ebx,[B]
    222222mov [ebx],edx
    223223sar eax,1 // eax:=a
    224 mov [a],eax
     224mov [A],eax
    225225
    226226pop ebx
     
    228228{$ENDIF}
    229229
    230 procedure ab_to_V8(a,b: integer; var V8: integer);
    231 begin
    232 assert((abs(a)<=1) and (abs(b)<=1) and ((a<>0) or (b<>0)));
    233 V8:=ab_v8[2*b+b+a];
    234 end;
    235 
    236 procedure V8_to_ab(V8: integer; var a,b: integer);
    237 begin
    238 a:=v8_a[V8]; b:=V8_b[V8];
    239 end;
    240 
    241 procedure ab_to_V21(a,b: integer; var V21: integer);
    242 begin
    243 V21:=(a+b+3) shl 2+(a-b+3) shr 1;
    244 end;
    245 
    246 procedure V21_to_ab(V21: integer; var a,b: integer);
    247 var
    248 dx,dy: integer;
     230procedure ab_to_V8(A,B: Integer; var V8: Integer);
     231begin
     232Assert((abs(A)<=1) and (abs(B)<=1) and ((A<>0) or (B<>0)));
     233V8:=ab_v8[2*B+B+A];
     234end;
     235
     236procedure V8_to_ab(V8: Integer; var A,B: Integer);
     237begin
     238A:=v8_a[V8]; B:=V8_b[V8];
     239end;
     240
     241procedure ab_to_V21(A,B: Integer; var V21: Integer);
     242begin
     243V21:=(A+B+3) shl 2+(A-B+3) shr 1;
     244end;
     245
     246procedure V21_to_ab(V21: Integer; var A,B: Integer);
     247var
     248dx,dy: Integer;
    249249begin
    250250dy:=V21 shr 2-3;
    251251dx:=V21 and 3 shl 1 -3 + (dy+3) and 1;
    252 a:=(dx+dy) div 2;
    253 b:=(dy-dx) div 2;
    254 end;
    255 
    256 procedure V8_to_Loc(Loc0: integer; var VicinityLoc: TVicinity8Loc);
    257 var
    258 x0,y0,lx: integer;
     252A:=(dx+dy) div 2;
     253B:=(dy-dx) div 2;
     254end;
     255
     256procedure V8_to_Loc(Loc0: Integer; var VicinityLoc: TVicinity8Loc);
     257var
     258x0,y0,lx: Integer;
    259259begin
    260260lx:=G.lx;
    261 y0:=cardinal(Loc0)*decompose24 shr 24;
     261y0:=Cardinal(Loc0)*decompose24 shr 24;
    262262x0:=Loc0-y0*lx; // Loc0 mod lx;
    263263VicinityLoc[1]:=Loc0+lx*2;
     
    265265VicinityLoc[5]:=Loc0-lx*2;
    266266VicinityLoc[7]:=Loc0+1;
    267 inc(Loc0,y0 and 1);
     267Inc(Loc0,y0 and 1);
    268268VicinityLoc[0]:=Loc0+lx;
    269269VicinityLoc[2]:=Loc0+lx-1;
     
    276276  if x0=0 then
    277277    begin
    278     inc(VicinityLoc[3],lx);
     278    Inc(VicinityLoc[3],lx);
    279279    if y0 and 1=0 then
    280280      begin
    281       inc(VicinityLoc[2],lx);
    282       inc(VicinityLoc[4],lx);
     281      Inc(VicinityLoc[2],lx);
     282      Inc(VicinityLoc[4],lx);
    283283      end
    284284    end
     
    286286else
    287287  begin
    288   dec(VicinityLoc[7],lx);
     288  Dec(VicinityLoc[7],lx);
    289289  if y0 and 1=1 then
    290290    begin
    291     dec(VicinityLoc[0],lx);
    292     dec(VicinityLoc[6],lx);
     291    Dec(VicinityLoc[0],lx);
     292    Dec(VicinityLoc[6],lx);
    293293    end
    294294  end;
     
    306306end;
    307307
    308 procedure V21_to_Loc(Loc0: integer; var VicinityLoc: TVicinity21Loc);
    309 var
    310 dx,dy,bit,y0,xComp,yComp,xComp0,xCompSwitch: integer;
    311 dst: ^integer;
    312 begin
    313 y0:=cardinal(Loc0)*decompose24 shr 24;
     308procedure V21_to_Loc(Loc0: Integer; var VicinityLoc: TVicinity21Loc);
     309var
     310dx,dy,bit,y0,xComp,yComp,xComp0,xCompSwitch: Integer;
     311dst: ^Integer;
     312begin
     313y0:=Cardinal(Loc0)*decompose24 shr 24;
    314314xComp0:=Loc0-y0*G.lx-1; // Loc0 mod G.lx -1
    315315xCompSwitch:=xComp0-1+y0 and 1;
    316 if xComp0<0 then inc(xComp0,G.lx);
    317 if xCompSwitch<0 then inc(xCompSwitch,G.lx);
     316if xComp0<0 then Inc(xComp0,G.lx);
     317if xCompSwitch<0 then Inc(xCompSwitch,G.lx);
    318318xCompSwitch:=xCompSwitch xor xComp0;
    319319yComp:=G.lx*(y0-3);
     
    329329      if bit and $67F7F76<>0 then dst^:=xComp+yComp
    330330      else dst^:=-1;
    331       inc(xComp);
    332       if xComp>=G.lx then dec(xComp, G.lx);
    333       inc(dst);
     331      Inc(xComp);
     332      if xComp>=G.lx then Dec(xComp, G.lx);
     333      Inc(dst);
    334334      bit:=bit shl 1;
    335335      end;
    336     inc(yComp,G.lx);
     336    Inc(yComp,G.lx);
    337337    end
    338338  else
    339339    begin
    340340    for dx:=0 to 3 do
    341       begin dst^:=-$1000; inc(dst); end;
     341      begin dst^:=-$1000; Inc(dst); end;
    342342    end
    343343end;
     
    345345
    346346procedure Init(NewGameData: TNewGameData);
    347 {$IFDEF DEBUG}var Loc: integer;{$ENDIF}
     347{$IFDEF DEBUG}var Loc: Integer;{$ENDIF}
    348348begin
    349349G:=NewGameData;
    350350MapSize:=G.lx*G.ly;
    351351decompose24:=(1 shl 24-1) div G.lx +1;
    352 {$IFDEF DEBUG}for Loc:=0 to MapSize-1 do assert(cardinal(Loc)*decompose24 shr 24=cardinal(Loc div G.lx));{$ENDIF}
    353 end;
    354 
    355 
    356 constructor TCustomAI.Create(Nation: integer);
     352{$IFDEF DEBUG}for Loc:=0 to MapSize-1 do Assert(Cardinal(Loc)*decompose24 shr 24=Cardinal(Loc div G.lx));{$ENDIF}
     353end;
     354
     355
     356constructor TCustomAI.Create(Nation: Integer);
    357357begin
    358358inherited Create;
    359 me:=Nation;
    360 RO:=pointer(G.RO[Nation]);
    361 Map:=pointer(RO.Map);
    362 MyUnit:=pointer(RO.Un);
    363 MyCity:=pointer(RO.City);
    364 MyModel:=pointer(RO.Model);
     359Me:=Nation;
     360RO:=Pointer(G.RO[Nation]);
     361Map:=Pointer(RO.Map);
     362MyUnit:=Pointer(RO.Un);
     363MyCity:=Pointer(RO.City);
     364MyModel:=Pointer(RO.Model);
    365365Opponent:=-1;
    366366end;
     
    368368destructor TCustomAI.Destroy;
    369369begin
    370 Server(sSetDebugMap,me,0,nodata^);
    371 end;
    372 
    373 
    374 procedure TCustomAI.Process(Command: integer; var Data);
    375 var
    376 Nation,NewResearch,NewGov,count,ad,cix,iix: integer;
     370Server(sSetDebugMap,Me,0,nodata^);
     371end;
     372
     373
     374procedure TCustomAI.Process(Command: Integer; var Data);
     375var
     376Nation,NewResearch,NewGov,count,ad,cix,iix: Integer;
    377377NegoTime: TNegoTime;
    378378begin
     
    380380  cTurn, cContinue:
    381381    begin
    382     if RO.Alive and (1 shl me)=0 then
     382    if RO.Alive and (1 shl Me)=0 then
    383383      begin // I'm dead, huhu
    384       Server(sTurn,me,0,nodata^);
    385       exit
     384      Server(sTurn,Me,0,nodata^);
     385      Exit
    386386      end;
    387387    if Command=cTurn then
    388388      begin
    389       fillchar(cixStateImp, sizeof(cixStateImp), $FF);
     389      FillChar(cixStateImp, SizeOf(cixStateImp), $FF);
    390390      for cix:=0 to RO.nCity-1 do if MyCity[cix].Loc>=0 then
    391391        for iix:=imPalace to imSpacePort do
     
    396396        NewGov:=ChooseGovernment;
    397397        if NewGov>gAnarchy then
    398           Server(sSetGovernment,me,NewGov,nodata^);
     398          Server(sSetGovernment,Me,NewGov,nodata^);
    399399        end;
    400       HaveTurned:=false;
     400      HaveTurned:=False;
    401401      Contacted:=[];
    402402      end;
     
    405405      if OnNegoRejected_CancelTreaty then
    406406        if RO.Treaty[Opponent]>=trPeace then
    407           if Server(sCancelTreaty,me,0,nodata^)<rExecuted then
    408             assert(false)
     407          if Server(sCancelTreaty,Me,0,nodata^)<rExecuted then
     408            Assert(False)
    409409      end
    410410    else UnwantedNego:=[];
     
    415415      if RO.Government<>gAnarchy then
    416416        for Nation:=0 to nPl-1 do
    417            if (Nation<>me) and (1 shl Nation and RO.Alive<>0)
     417           if (Nation<>Me) and (1 shl Nation and RO.Alive<>0)
    418418             and (RO.Treaty[Nation]>=trNone)
    419419             and not (Nation in Contacted) and not (Nation in UnwantedNego)
    420              and (Server(scContact-sExecute + Nation shl 4, me, 0, nodata^)>=rExecuted) then
     420             and (Server(scContact-sExecute + Nation shl 4, Me, 0, nodata^)>=rExecuted) then
    421421             if WantNegotiation(Nation, NegoTime) then
    422422               begin
    423                if Server(scContact + Nation shl 4, me, 0, nodata^)>=rExecuted then
     423               if Server(scContact + Nation shl 4, Me, 0, nodata^)>=rExecuted then
    424424                 begin
    425                  include(Contacted, Nation);
     425                 Include(Contacted, Nation);
    426426                 Opponent:=Nation;
    427427                 MyAction:=scContact;
    428                  exit;
     428                 Exit;
    429429                 end;
    430430               end
    431              else include(UnwantedNego,Nation);
     431             else Include(UnwantedNego,Nation);
    432432      if NegoTime=BeginOfTurn then
    433433        begin
    434434        DoTurn;
    435         HaveTurned:=true;
     435        HaveTurned:=True;
    436436        Contacted:=[];
    437437        UnwantedNego:=[];
    438438        end
    439       else break;
    440     until false;
     439      else Break;
     440    until False;
    441441    if RO.Happened and phTech<>0 then
    442442      begin
     
    446446        count:=0;
    447447        for ad:=0 to nAdv-1 do if AdvanceResearchable(ad) then
    448           begin inc(count); if random(count)=0 then NewResearch:=ad end
     448          begin Inc(count); if random(count)=0 then NewResearch:=ad end
    449449        end;
    450       Server(sSetResearch,me,NewResearch,nodata^)
     450      Server(sSetResearch,Me,NewResearch,nodata^)
    451451      end;
    452     if (me=1) and (RO.Turn=800) then
     452    if (Me=1) and (RO.Turn=800) then
    453453      begin
    454454      count:=0;
    455       Server(sReload,me,0,count)
     455      Server(sReload,Me,0,count)
    456456      end
    457457    else if (RO.Turn>10) and (random(1000)=0) then
    458458      begin
    459459      count:=RO.Turn-10;
    460       Server(sReload,me,0,count)
     460      Server(sReload,Me,0,count)
    461461      end
    462     else if Server(sTurn,me,0,nodata^)<rExecuted then
    463       assert(false);
     462    else if Server(sTurn,Me,0,nodata^)<rExecuted then
     463      Assert(False);
    464464    end;
    465465  scContact:
    466     if WantNegotiation(integer(Data), EnemyCalled) then
     466    if WantNegotiation(Integer(Data), EnemyCalled) then
    467467      begin
    468       if Server(scDipStart, me, 0, nodata^)<rExecuted then
    469         assert(false);
    470       Opponent:=integer(Data);
     468      if Server(scDipStart, Me, 0, nodata^)<rExecuted then
     469        Assert(False);
     470      Opponent:=Integer(Data);
    471471      MyAction:=scDipStart;
    472472      end
    473473    else
    474474      begin
    475       if Server(scReject, me, 0, nodata^)<rExecuted then
    476         assert(false);
     475      if Server(scReject, Me, 0, nodata^)<rExecuted then
     476        Assert(False);
    477477      end;
    478478  scDipStart, scDipNotice, scDipAccept, scDipCancelTreaty, scDipOffer, scDipBreak:
     
    486486    else begin MyAction:=scDipOffer; MyOffer.nDeliver:=0; MyOffer.nCost:=0; end;
    487487    DoNegotiation;
    488     assert((MyAction=scDipNotice) or (MyAction=scDipAccept)
     488    Assert((MyAction=scDipNotice) or (MyAction=scDipAccept)
    489489      or (MyAction=scDipCancelTreaty) or (MyAction=scDipOffer)
    490490      or (MyAction=scDipBreak));
    491     if MyAction=scDipOffer then Server(MyAction, me, 0, MyOffer)
    492     else Server(MyAction, me, 0, nodata^);
     491    if MyAction=scDipOffer then Server(MyAction, Me, 0, MyOffer)
     492    else Server(MyAction, Me, 0, nodata^);
    493493    end;
    494494  cShowEndContact:
     
    515515
    516516procedure TCustomAI.OnBeforeEnemyAttack(UnitInfo: TUnitInfo; ToLoc, EndHealth,
    517   EndHealthDef: integer);
    518 begin
    519 end;
    520 
    521 procedure TCustomAI.OnBeforeEnemyCapture(UnitInfo: TUnitInfo; ToLoc: integer);
     517  EndHealthDef: Integer);
     518begin
     519end;
     520
     521procedure TCustomAI.OnBeforeEnemyCapture(UnitInfo: TUnitInfo; ToLoc: Integer);
    522522begin
    523523end;
     
    531531end;
    532532
    533 function TCustomAI.ChooseResearchAdvance: integer;
    534 begin
    535 result:=-1
    536 end;
    537 
    538 function TCustomAI.ChooseStealAdvance: integer;
    539 begin
    540 result:=-1
    541 end;
    542 
    543 function TCustomAI.ChooseGovernment: integer;
    544 begin
    545 result:=gDespotism
    546 end;
    547 
    548 function TCustomAI.WantNegotiation(Nation: integer; NegoTime: TNegoTime): boolean;
    549 begin
    550 result:=false;
    551 end;
    552 
    553 function TCustomAI.OnNegoRejected_CancelTreaty: boolean;
    554 begin
    555 result:=false;
     533function TCustomAI.ChooseResearchAdvance: Integer;
     534begin
     535Result:=-1
     536end;
     537
     538function TCustomAI.ChooseStealAdvance: Integer;
     539begin
     540Result:=-1
     541end;
     542
     543function TCustomAI.ChooseGovernment: Integer;
     544begin
     545Result:=gDespotism
     546end;
     547
     548function TCustomAI.WantNegotiation(Nation: Integer; NegoTime: TNegoTime): Boolean;
     549begin
     550Result:=False;
     551end;
     552
     553function TCustomAI.OnNegoRejected_CancelTreaty: Boolean;
     554begin
     555Result:=False;
    556556end;
    557557{$HINTS ON}
     
    559559procedure TCustomAI.StealAdvance;
    560560var
    561 Steal, ad, count: integer;
     561Steal, ad, count: Integer;
    562562begin
    563563Steal:=ChooseStealAdvance;
     
    566566  count:=0;
    567567  for ad:=0 to nAdv-1 do if AdvanceStealable(ad) then
    568     begin inc(count); if random(count)=0 then Steal:=ad end
     568    begin Inc(count); if random(count)=0 then Steal:=ad end
    569569  end;
    570 if Steal>=0 then Server(sStealTech,me,Steal,nodata^);
     570if Steal>=0 then Server(sStealTech,Me,Steal,nodata^);
    571571RO.Happened:=RO.Happened and not phStealTech
    572572end;
    573573
    574 function TCustomAI.IsResearched(Advance: integer): boolean;
    575 begin
    576 result:= RO.Tech[Advance]>=tsApplicable
    577 end;
    578 
    579 function TCustomAI.ResearchCost: integer;
    580 begin
    581 Server(sGetTechCost,me,0,result)
    582 end;
    583 
    584 function TCustomAI.ChangeAttitude(Nation, Attitude: integer): integer;
    585 begin
    586 result:=Server(sSetAttitude+Nation shl 4,me,Attitude,nodata^)
    587 end;
    588 
    589 function TCustomAI.Revolution: integer;
    590 begin
    591 result:=Server(sRevolution,me,0,nodata^);
    592 end;
    593 
    594 function TCustomAI.ChangeRates(Tax,Lux: integer): integer;
    595 begin
    596 result:=Server(sSetRates,me,Tax div 10 and $f+Lux div 10 and $f shl 4,nodata^)
    597 end;
    598 
    599 function TCustomAI.PrepareNewModel(Domain: integer): integer;
    600 begin
    601 result:=Server(sCreateDevModel,me,Domain,nodata^);
    602 end;
    603 
    604 function TCustomAI.SetNewModelFeature(F, Count: integer): integer;
    605 begin
    606 result:=Server(sSetDevModelCap+Count shl 4,me,F,nodata^)
    607 end;
    608 
    609 function TCustomAI.AdvanceResearchable(Advance: integer): boolean;
    610 begin
    611 result:= Server(sSetResearch-sExecute,me,Advance,nodata^)>=rExecuted;
    612 end;
    613 
    614 function TCustomAI.AdvanceStealable(Advance: integer): boolean;
    615 begin
    616 result:= Server(sStealTech-sExecute,me,Advance,nodata^)>=rExecuted;
    617 end;
    618 
    619 function TCustomAI.DebugMessage(Level: integer; Text: string): boolean;
    620 begin
    621 Text:=copy('P'+char(48+me)+' '+Text,1,254);
    622 Server(sMessage,me,Level,pchar(Text)^);
    623 
    624 result:=true;
     574function TCustomAI.IsResearched(Advance: Integer): Boolean;
     575begin
     576Result:= RO.Tech[Advance]>=tsApplicable
     577end;
     578
     579function TCustomAI.ResearchCost: Integer;
     580begin
     581Server(sGetTechCost,Me,0,Result)
     582end;
     583
     584function TCustomAI.ChangeAttitude(Nation, Attitude: Integer): Integer;
     585begin
     586Result:=Server(sSetAttitude+Nation shl 4,Me,Attitude,nodata^)
     587end;
     588
     589function TCustomAI.Revolution: Integer;
     590begin
     591Result:=Server(sRevolution,Me,0,nodata^);
     592end;
     593
     594function TCustomAI.ChangeRates(Tax,Lux: Integer): Integer;
     595begin
     596Result:=Server(sSetRates,Me,Tax div 10 and $F+Lux div 10 and $F shl 4,nodata^)
     597end;
     598
     599function TCustomAI.PrepareNewModel(Domain: Integer): Integer;
     600begin
     601Result:=Server(sCreateDevModel,Me,Domain,nodata^);
     602end;
     603
     604function TCustomAI.SetNewModelFeature(F, Count: Integer): Integer;
     605begin
     606Result:=Server(sSetDevModelCap+Count shl 4,Me,F,nodata^)
     607end;
     608
     609function TCustomAI.AdvanceResearchable(Advance: Integer): Boolean;
     610begin
     611Result:= Server(sSetResearch-sExecute,Me,Advance,nodata^)>=rExecuted;
     612end;
     613
     614function TCustomAI.AdvanceStealable(Advance: Integer): Boolean;
     615begin
     616Result:= Server(sStealTech-sExecute,Me,Advance,nodata^)>=rExecuted;
     617end;
     618
     619function TCustomAI.DebugMessage(Level: Integer; Text: string): Boolean;
     620begin
     621Text:=Copy('P'+char(48+Me)+' '+Text,1,254);
     622Server(sMessage,Me,Level,PChar(Text)^);
     623
     624Result:=True;
    625625  // always returns true so that it can be used like
    626626  // "assert(DebugMessage(...));" -> not compiled in release build
    627627end;
    628628
    629 function TCustomAI.SetDebugMap(var DebugMap): boolean;
    630 begin
    631 Server(sSetDebugMap, me, 0, DebugMap);
    632 
    633 result:=true;
     629function TCustomAI.SetDebugMap(var DebugMap): Boolean;
     630begin
     631Server(sSetDebugMap, Me, 0, DebugMap);
     632
     633Result:=True;
    634634  // always returns true so that it can be used like
    635635  // "assert(SetDebugMap(...));" -> not compiled in release build
    636636end;
    637637
    638 procedure TCustomAI.Unit_FindMyDefender(Loc: integer; var uix: integer);
    639 begin
    640 if Server(sGetDefender,me,Loc,uix)<rExecuted then uix:=-1
    641 end;
    642 
    643 procedure TCustomAI.Unit_FindEnemyDefender(Loc: integer; var euix: integer);
     638procedure TCustomAI.Unit_FindMyDefender(Loc: Integer; var uix: Integer);
     639begin
     640if Server(sGetDefender,Me,Loc,uix)<rExecuted then uix:=-1
     641end;
     642
     643procedure TCustomAI.Unit_FindEnemyDefender(Loc: Integer; var euix: Integer);
    644644begin
    645645euix:=RO.nEnemyUn-1;
    646646while (euix>=0) and (RO.EnemyUn[euix].Loc<>Loc) do
    647   dec(euix);
    648 end;
    649 
    650 function TCustomAI.Unit_Move(uix,ToLoc: integer): integer;
    651 var
    652 Step: integer;
    653 DestinationReached: boolean;
     647  Dec(euix);
     648end;
     649
     650function TCustomAI.Unit_Move(uix,ToLoc: Integer): Integer;
     651var
     652Step: Integer;
     653DestinationReached: Boolean;
    654654Advice: TMoveAdviceData;
    655655begin
    656 assert((uix>=0) and (uix<RO.nUn) and (MyUnit[uix].Loc>=0)); // is a unit
     656Assert((uix>=0) and (uix<RO.nUn) and (MyUnit[uix].Loc>=0)); // is a unit
    657657{Loc_to_ab(MyUnit[uix].Loc,ToLoc,a,b);
    658 assert((a<>0) or (b<>0));
    659 if (a>=-1) and (a<=1) and (b>=-1) and (b<=1) then
     658Assert((A<>0) or (B<>0));
     659if (A>=-1) and (A<=1) and (B>=-1) and (B<=1) then
    660660  begin // move to adjacent tile
    661   !!!problem: if move is invalid, return codes are not consistent with other branch (eNoWay)
     661  !!!problem: if Move is invalid, return codes are not consistent with other branch (eNoWay)
    662662  Advice.nStep:=1;
    663   Advice.dx[0]:=a-b;
    664   Advice.dy[0]:=a+b;
     663  Advice.dx[0]:=A-B;
     664  Advice.dy[0]:=A+B;
    665665  Advice.MoreTurns:=0;
    666666  Advice.MaxHostile_MovementLeft:=MyUnit[uix].Movement;
    667   result:=eOK;
     667  Result:=eOK;
    668668  end
    669669else}
     
    672672  Advice.MoreTurns:=9999;
    673673  Advice.MaxHostile_MovementLeft:=100;
    674   result:=Server(sGetMoveAdvice,me,uix,Advice);
     674  Result:=Server(sGetMoveAdvice,Me,uix,Advice);
    675675  end;
    676 if result=eOk then
     676if Result=eOk then
    677677  begin
    678   DestinationReached:=false;
     678  DestinationReached:=False;
    679679  Step:=0;
    680680  repeat
    681     if result and (rExecuted or rUnitRemoved)=rExecuted then // check if destination reached
     681    if Result and (rExecuted or rUnitRemoved)=rExecuted then // check if destination reached
    682682      if (ToLoc>=0) and (Advice.MoreTurns=0) and (Step=Advice.nStep-1)
    683683        and ((Map[ToLoc] and (fUnit or fOwned)=fUnit) // attack
     
    685685          and ((MyModel[MyUnit[uix].mix].Domain<>dGround) // bombardment
    686686            or (MyModel[MyUnit[uix].mix].Flags and mdCivil<>0))) then // can't capture
    687         begin DestinationReached:=true; break end // stop next to destination
     687        begin DestinationReached:=True; Break end // stop next to destination
    688688      else if Step=Advice.nStep then
    689         DestinationReached:=true; // normal move -- stop at destination
    690 
    691     if (Step=Advice.nStep) or (result<>eOK) and (result<>eLoaded) then
    692       break;
    693 
    694     result:=Server(sMoveUnit+(Advice.dx[Step] and 7) shl 4 +(Advice.dy[Step] and 7) shl 7,
    695       me,uix,nodata^);
    696     inc(Step);
     689        DestinationReached:=True; // normal move -- stop at destination
     690
     691    if (Step=Advice.nStep) or (Result<>eOK) and (Result<>eLoaded) then
     692      Break;
     693
     694    Result:=Server(sMoveUnit+(Advice.dx[Step] and 7) shl 4 +(Advice.dy[Step] and 7) shl 7,
     695      Me,uix,nodata^);
     696    Inc(Step);
    697697    if RO.Happened and phStealTech<>0 then StealAdvance;
    698   until false;
     698  until False;
    699699  if DestinationReached then
    700700    if Advice.nStep=25 then
    701       result:=Unit_Move(uix,ToLoc) // Shinkansen
     701      Result:=Unit_Move(uix,ToLoc) // Shinkansen
    702702    else if Advice.MoreTurns=0 then
    703       result:=result or rLocationReached
    704     else result:=result or rMoreTurns;
    705   end
    706 end;
    707 
    708 function TCustomAI.Unit_Step(uix,ToLoc: integer): integer;
    709 var
    710 a,b: integer;
    711 begin
    712 Loc_to_ab(MyUnit[uix].Loc, ToLoc, a, b);
    713 assert(((a<>0) or (b<>0)) and (a>=-1) and (a<=1) and (b>=-1) and (b<=1));
    714 result:=Server(sMoveUnit+((a-b) and 7) shl 4 +((a+b) and 7) shl 7, me, uix, nodata^);
     703      Result:=Result or rLocationReached
     704    else Result:=Result or rMoreTurns;
     705  end
     706end;
     707
     708function TCustomAI.Unit_Step(uix,ToLoc: Integer): Integer;
     709var
     710A,B: Integer;
     711begin
     712Loc_to_ab(MyUnit[uix].Loc, ToLoc, A, B);
     713Assert(((A<>0) or (B<>0)) and (A>=-1) and (A<=1) and (B>=-1) and (B<=1));
     714Result:=Server(sMoveUnit+((A-B) and 7) shl 4 +((A+B) and 7) shl 7, Me, uix, nodata^);
    715715if RO.Happened and phStealTech<>0 then StealAdvance;
    716716end;
    717717
    718 function TCustomAI.Unit_Attack(uix,ToLoc: integer): integer;
    719 var
    720 a,b: integer;
    721 begin
    722 assert((uix>=0) and (uix<RO.nUn) and (MyUnit[uix].Loc>=0) // is a unit
     718function TCustomAI.Unit_Attack(uix,ToLoc: Integer): Integer;
     719var
     720A,B: Integer;
     721begin
     722Assert((uix>=0) and (uix<RO.nUn) and (MyUnit[uix].Loc>=0) // is a unit
    723723  and ((Map[ToLoc] and (fUnit or fOwned)=fUnit) // is an attack
    724724  or (Map[ToLoc] and (fCity or fOwned)=fCity)
    725725  and (MyModel[MyUnit[uix].mix].Domain<>dGround))); // is a bombardment
    726 Loc_to_ab(MyUnit[uix].Loc,ToLoc,a,b);
    727 assert(((a<>0) or (b<>0)) and (a>=-1) and (a<=1) and (b>=-1) and (b<=1)); // attack to adjacent tile
    728 result:=Server(sMoveUnit+(a-b) and 7 shl 4 +(a+b) and 7 shl 7,me,uix,nodata^);
    729 end;
    730 
    731 function TCustomAI.Unit_DoMission(uix,MissionType,ToLoc: integer): integer;
    732 var
    733 a,b: integer;
    734 begin
    735 result:=Server(sSetSpyMission + MissionType shl 4,me,0,nodata^);
    736 if result>=rExecuted then
     726Loc_to_ab(MyUnit[uix].Loc,ToLoc,A,B);
     727Assert(((A<>0) or (B<>0)) and (A>=-1) and (A<=1) and (B>=-1) and (B<=1)); // attack to adjacent tile
     728Result:=Server(sMoveUnit+(A-B) and 7 shl 4 +(A+B) and 7 shl 7,Me,uix,nodata^);
     729end;
     730
     731function TCustomAI.Unit_DoMission(uix,MissionType,ToLoc: Integer): Integer;
     732var
     733A,B: Integer;
     734begin
     735Result:=Server(sSetSpyMission + MissionType shl 4,Me,0,nodata^);
     736if Result>=rExecuted then
    737737  begin
    738   assert((uix>=0) and (uix<RO.nUn) and (MyUnit[uix].Loc>=0) // is a unit
     738  Assert((uix>=0) and (uix<RO.nUn) and (MyUnit[uix].Loc>=0) // is a unit
    739739    and (MyModel[MyUnit[uix].mix].Kind=mkDiplomat)); // is a commando
    740   Loc_to_ab(MyUnit[uix].Loc,ToLoc,a,b);
    741   assert(((a<>0) or (b<>0)) and (a>=-1) and (a<=1) and (b>=-1) and (b<=1)); // city must be adjacent
    742   result:=Server(sMoveUnit-sExecute+(a-b) and 7 shl 4 +(a+b) and 7 shl 7,me,uix,nodata^);
    743   if result=eMissionDone then
    744     result:=Server(sMoveUnit+(a-b) and 7 shl 4 +(a+b) and 7 shl 7,me,uix,nodata^)
    745   else if (result<>eNoTime_Move) and (result<>eTreaty) and (result<>eNoTurn) then
    746     result:=eInvalid // not a special commando mission!
    747   end
    748 end;
    749 
    750 function TCustomAI.Unit_MoveForecast(uix,ToLoc: integer;
    751   var RemainingMovement: integer): boolean;
     740  Loc_to_ab(MyUnit[uix].Loc,ToLoc,A,B);
     741  Assert(((A<>0) or (B<>0)) and (A>=-1) and (A<=1) and (B>=-1) and (B<=1)); // city must be adjacent
     742  Result:=Server(sMoveUnit-sExecute+(A-B) and 7 shl 4 +(A+B) and 7 shl 7,Me,uix,nodata^);
     743  if Result=eMissionDone then
     744    Result:=Server(sMoveUnit+(A-B) and 7 shl 4 +(A+B) and 7 shl 7,Me,uix,nodata^)
     745  else if (Result<>eNoTime_Move) and (Result<>eTreaty) and (Result<>eNoTurn) then
     746    Result:=eInvalid // not a special commando mission!
     747  end
     748end;
     749
     750function TCustomAI.Unit_MoveForecast(uix,ToLoc: Integer;
     751  var RemainingMovement: Integer): Boolean;
    752752var
    753753Advice: TMoveAdviceData;
    754754begin
    755 assert((uix>=0) and (uix<RO.nUn) and (MyUnit[uix].Loc>=0)); // is a unit
     755Assert((uix>=0) and (uix<RO.nUn) and (MyUnit[uix].Loc>=0)); // is a unit
    756756Advice.ToLoc:=ToLoc;
    757757Advice.MoreTurns:=0;
    758758Advice.MaxHostile_MovementLeft:=100;
    759 if Server(sGetMoveAdvice,me,uix,Advice)=eOk then
     759if Server(sGetMoveAdvice,Me,uix,Advice)=eOk then
    760760  begin
    761761  RemainingMovement:=Advice.MaxHostile_MovementLeft;
    762   result:=true
     762  Result:=True
    763763  end
    764764else
    765765  begin
    766766  RemainingMovement:=-1;
    767   result:=false
    768   end
    769 end;
    770 
    771 function TCustomAI.Unit_AttackForecast(uix,ToLoc,AttackMovement: integer;
    772   var RemainingHealth: integer): boolean;
     767  Result:=False
     768  end
     769end;
     770
     771function TCustomAI.Unit_AttackForecast(uix,ToLoc,AttackMovement: Integer;
     772  var RemainingHealth: Integer): Boolean;
    773773var
    774774BattleForecast: TBattleForecast;
    775775begin
    776 assert((uix>=0) and (uix<RO.nUn) and (MyUnit[uix].Loc>=0) // is a unit
     776Assert((uix>=0) and (uix<RO.nUn) and (MyUnit[uix].Loc>=0) // is a unit
    777777  and (Map[ToLoc] and (fUnit or fOwned)=fUnit)); // is an attack
    778778RemainingHealth:=-$100;
    779 result:=false;
     779Result:=False;
    780780if AttackMovement>=0 then with MyUnit[uix] do
    781781  begin
    782   BattleForecast.pAtt:=me;
     782  BattleForecast.pAtt:=Me;
    783783  BattleForecast.mixAtt:=mix;
    784784  BattleForecast.HealthAtt:=Health;
     
    786786  BattleForecast.FlagsAtt:=Flags;
    787787  BattleForecast.Movement:=AttackMovement;
    788   if Server(sGetBattleForecast,me,ToLoc,BattleForecast)>=rExecuted then
     788  if Server(sGetBattleForecast,Me,ToLoc,BattleForecast)>=rExecuted then
    789789    begin
    790790    if BattleForecast.EndHealthAtt>0 then
    791791      RemainingHealth:=BattleForecast.EndHealthAtt
    792792    else RemainingHealth:=-BattleForecast.EndHealthDef;
    793     result:=true
     793    Result:=True
    794794    end
    795795  end
    796796end;
    797797
    798 function TCustomAI.Unit_DefenseForecast(euix,ToLoc: integer;
    799   var RemainingHealth: integer): boolean;
     798function TCustomAI.Unit_DefenseForecast(euix,ToLoc: Integer;
     799  var RemainingHealth: Integer): Boolean;
    800800var
    801801BattleForecast: TBattleForecast;
    802802begin
    803 assert((euix>=0) and (euix<RO.nEnemyUn) and (RO.EnemyUn[euix].Loc>=0) // is an enemy unit
     803Assert((euix>=0) and (euix<RO.nEnemyUn) and (RO.EnemyUn[euix].Loc>=0) // is an enemy unit
    804804  and (Map[ToLoc] and (fUnit or fOwned)=(fUnit or fOwned))); // is an attack
    805805RemainingHealth:=$100;
    806 result:=false;
     806Result:=False;
    807807with RO.EnemyUn[euix] do
    808808  begin
     
    813813  BattleForecast.FlagsAtt:=Flags;
    814814  BattleForecast.Movement:=100;
    815   if Server(sGetBattleForecast,me,ToLoc,BattleForecast)>=rExecuted then
     815  if Server(sGetBattleForecast,Me,ToLoc,BattleForecast)>=rExecuted then
    816816    begin
    817817    if BattleForecast.EndHealthDef>0 then
    818818      RemainingHealth:=BattleForecast.EndHealthDef
    819819    else RemainingHealth:=-BattleForecast.EndHealthAtt;
    820     result:=true
     820    Result:=True
    821821    end
    822822  end
    823823end;
    824824
    825 function TCustomAI.Unit_Disband(uix: integer): integer;
    826 begin
    827 result:=Server(sRemoveUnit,me,uix,nodata^)
    828 end;
    829 
    830 function TCustomAI.Unit_StartJob(uix,NewJob: integer): integer;
    831 begin
    832 result:=Server(sStartJob+NewJob shl 4,me,uix,nodata^)
    833 end;
    834 
    835 function TCustomAI.Unit_SetHomeHere(uix: integer): integer;
    836 begin
    837 result:=Server(sSetUnitHome,me,uix,nodata^)
    838 end;
    839 
    840 function TCustomAI.Unit_Load(uix: integer): integer;
    841 begin
    842 result:=Server(sLoadUnit,me,uix,nodata^)
    843 end;
    844 
    845 function TCustomAI.Unit_Unload(uix: integer): integer;
    846 begin
    847 result:=Server(sUnloadUnit,me,uix,nodata^)
    848 end;
    849 
    850 function TCustomAI.Unit_AddToCity(uix: integer): integer;
    851 begin
    852 result:=Server(sAddToCity,me,uix,nodata^)
    853 end;
    854 
    855 
    856 procedure TCustomAI.City_FindMyCity(Loc: integer; var cix: integer);
     825function TCustomAI.Unit_Disband(uix: Integer): Integer;
     826begin
     827Result:=Server(sRemoveUnit,Me,uix,nodata^)
     828end;
     829
     830function TCustomAI.Unit_StartJob(uix,NewJob: Integer): Integer;
     831begin
     832Result:=Server(sStartJob+NewJob shl 4,Me,uix,nodata^)
     833end;
     834
     835function TCustomAI.Unit_SetHomeHere(uix: Integer): Integer;
     836begin
     837Result:=Server(sSetUnitHome,Me,uix,nodata^)
     838end;
     839
     840function TCustomAI.Unit_Load(uix: Integer): Integer;
     841begin
     842Result:=Server(sLoadUnit,Me,uix,nodata^)
     843end;
     844
     845function TCustomAI.Unit_Unload(uix: Integer): Integer;
     846begin
     847Result:=Server(sUnloadUnit,Me,uix,nodata^)
     848end;
     849
     850function TCustomAI.Unit_AddToCity(uix: Integer): Integer;
     851begin
     852Result:=Server(sAddToCity,Me,uix,nodata^)
     853end;
     854
     855
     856procedure TCustomAI.City_FindMyCity(Loc: Integer; var cix: Integer);
    857857begin
    858858if Map[Loc] and (fCity or fOwned)<>fCity or fOwned then
     
    862862  cix:=RO.nCity-1;
    863863  while (cix>=0) and (MyCity[cix].Loc<>Loc) do
    864     dec(cix);
    865   end
    866 end;
    867 
    868 procedure TCustomAI.City_FindEnemyCity(Loc: integer; var ecix: integer);
     864    Dec(cix);
     865  end
     866end;
     867
     868procedure TCustomAI.City_FindEnemyCity(Loc: Integer; var ecix: Integer);
    869869begin
    870870if Map[Loc] and (fCity or fOwned)<>fCity then
     
    874874  ecix:=RO.nEnemyCity-1;
    875875  while (ecix>=0) and (RO.EnemyCity[ecix].Loc<>Loc) do
    876     dec(ecix);
    877   end
    878 end;
    879 
    880 function TCustomAI.City_HasProject(cix: integer): boolean;
    881 begin
    882 result:= MyCity[cix].Project and (cpImp+cpIndex)<>cpImp+imTrGoods
    883 end;
    884 
    885 function TCustomAI.City_CurrentImprovementProject(cix: integer): integer;
    886 begin
    887 if MyCity[cix].Project and cpImp=0 then result:=-1
     876    Dec(ecix);
     877  end
     878end;
     879
     880function TCustomAI.City_HasProject(cix: Integer): Boolean;
     881begin
     882Result:= MyCity[cix].Project and (cpImp+cpIndex)<>cpImp+imTrGoods
     883end;
     884
     885function TCustomAI.City_CurrentImprovementProject(cix: Integer): Integer;
     886begin
     887if MyCity[cix].Project and cpImp=0 then Result:=-1
    888888else
    889889  begin
    890   result:=MyCity[cix].Project and cpIndex;
    891   if result=imTrGoods then result:=-1
    892   end
    893 end;
    894 
    895 function TCustomAI.City_CurrentUnitProject(cix: integer): integer;
    896 begin
    897 if MyCity[cix].Project and cpImp<>0 then result:=-1
    898 else result:=MyCity[cix].Project and cpIndex;
    899 end;
    900 
    901 function TCustomAI.City_GetTileInfo(cix,TileLoc: integer; var TileInfo: TTileInfo): integer;
     890  Result:=MyCity[cix].Project and cpIndex;
     891  if Result=imTrGoods then Result:=-1
     892  end
     893end;
     894
     895function TCustomAI.City_CurrentUnitProject(cix: Integer): Integer;
     896begin
     897if MyCity[cix].Project and cpImp<>0 then Result:=-1
     898else Result:=MyCity[cix].Project and cpIndex;
     899end;
     900
     901function TCustomAI.City_GetTileInfo(cix,TileLoc: Integer; var TileInfo: TTileInfo): Integer;
    902902begin
    903903TileInfo.ExplCity:=cix;
    904 result:=Server(sGetHypoCityTileInfo,me,TileLoc,TileInfo)
    905 end;
    906 
    907 function TCustomAI.City_GetReport(cix: integer; var Report: TCityReport): integer;
     904Result:=Server(sGetHypoCityTileInfo,Me,TileLoc,TileInfo)
     905end;
     906
     907function TCustomAI.City_GetReport(cix: Integer; var Report: TCityReport): Integer;
    908908begin
    909909Report.HypoTiles:=-1;
    910910Report.HypoTax:=-1;
    911911Report.HypoLux:=-1;
    912 result:=Server(sGetCityReport,me,cix,Report)
    913 end;
    914 
    915 function TCustomAI.City_GetHypoReport(cix, HypoTiles, HypoTax, HypoLux: integer;
    916   var Report: TCityReport): integer;
     912Result:=Server(sGetCityReport,Me,cix,Report)
     913end;
     914
     915function TCustomAI.City_GetHypoReport(cix, HypoTiles, HypoTax, HypoLux: Integer;
     916  var Report: TCityReport): Integer;
    917917begin
    918918Report.HypoTiles:=HypoTiles;
    919919Report.HypoTax:=HypoTax;
    920920Report.HypoLux:=HypoLux;
    921 result:=Server(sGetCityReport,me,cix,Report)
    922 end;
    923 
    924 function TCustomAI.City_GetAreaInfo(cix: integer; var AreaInfo: TCityAreaInfo): integer;
    925 begin
    926 result:=Server(sGetCityAreaInfo,me,cix,AreaInfo)
    927 end;
    928 
    929 function TCustomAI.City_StartUnitProduction(cix,mix: integer): integer;
    930 begin
    931 result:=Server(sSetCityProject,me,cix,mix)
    932 end;
    933 
    934 function TCustomAI.City_StartEmigration(cix,mix: integer;
    935   AllowDisbandCity, AsConscripts: boolean): integer;
    936 var
    937 NewProject: integer;
     921Result:=Server(sGetCityReport,Me,cix,Report)
     922end;
     923
     924function TCustomAI.City_GetAreaInfo(cix: Integer; var AreaInfo: TCityAreaInfo): Integer;
     925begin
     926Result:=Server(sGetCityAreaInfo,Me,cix,AreaInfo)
     927end;
     928
     929function TCustomAI.City_StartUnitProduction(cix,mix: Integer): Integer;
     930begin
     931Result:=Server(sSetCityProject,Me,cix,mix)
     932end;
     933
     934function TCustomAI.City_StartEmigration(cix,mix: Integer;
     935  AllowDisbandCity, AsConscripts: Boolean): Integer;
     936var
     937NewProject: Integer;
    938938begin
    939939NewProject:=mix;
    940940if AllowDisbandCity then NewProject:=NewProject or cpDisbandCity;
    941941if AsConscripts then NewProject:=NewProject or cpConscripts;
    942 result:=Server(sSetCityProject,me,cix,NewProject)
    943 end;
    944 
    945 function TCustomAI.City_StartImprovement(cix,iix: integer): integer;
    946 var
    947 NewProject: integer;
     942Result:=Server(sSetCityProject,Me,cix,NewProject)
     943end;
     944
     945function TCustomAI.City_StartImprovement(cix,iix: Integer): Integer;
     946var
     947NewProject: Integer;
    948948begin
    949949NewProject:=iix+cpImp;
    950 result:=Server(sSetCityProject,me,cix,NewProject)
    951 end;
    952 
    953 function TCustomAI.City_Improvable(cix,iix: integer): boolean;
    954 var
    955 NewProject: integer;
     950Result:=Server(sSetCityProject,Me,cix,NewProject)
     951end;
     952
     953function TCustomAI.City_Improvable(cix,iix: Integer): Boolean;
     954var
     955NewProject: Integer;
    956956begin
    957957NewProject:=iix+cpImp;
    958 result:= Server(sSetCityProject-sExecute,me,cix,NewProject)>=rExecuted;
    959 end;
    960 
    961 function TCustomAI.City_StopProduction(cix: integer): integer;
    962 var
    963 NewProject: integer;
     958Result:= Server(sSetCityProject-sExecute,Me,cix,NewProject)>=rExecuted;
     959end;
     960
     961function TCustomAI.City_StopProduction(cix: Integer): Integer;
     962var
     963NewProject: Integer;
    964964begin
    965965NewProject:=imTrGoods+cpImp;
    966 result:=Server(sSetCityProject,me,cix,NewProject)
    967 end;
    968 
    969 function TCustomAI.City_BuyProject(cix: integer): integer;
    970 begin
    971 result:=Server(sBuyCityProject,me,cix,nodata^)
    972 end;
    973 
    974 function TCustomAI.City_SellImprovement(cix,iix: integer): integer;
    975 begin
    976 result:=Server(sSellCityImprovement,me,cix,iix)
    977 end;
    978 
    979 function TCustomAI.City_RebuildImprovement(cix,iix: integer): integer;
    980 begin
    981 result:=Server(sRebuildCityImprovement,me,cix,iix)
    982 end;
    983 
    984 function TCustomAI.City_SetTiles(cix,NewTiles: integer): integer;
    985 begin
    986 result:=Server(sSetCityTiles,me,cix,NewTiles)
     966Result:=Server(sSetCityProject,Me,cix,NewProject)
     967end;
     968
     969function TCustomAI.City_BuyProject(cix: Integer): Integer;
     970begin
     971Result:=Server(sBuyCityProject,Me,cix,nodata^)
     972end;
     973
     974function TCustomAI.City_SellImprovement(cix,iix: Integer): Integer;
     975begin
     976Result:=Server(sSellCityImprovement,Me,cix,iix)
     977end;
     978
     979function TCustomAI.City_RebuildImprovement(cix,iix: Integer): Integer;
     980begin
     981Result:=Server(sRebuildCityImprovement,Me,cix,iix)
     982end;
     983
     984function TCustomAI.City_SetTiles(cix,NewTiles: Integer): Integer;
     985begin
     986Result:=Server(sSetCityTiles,Me,cix,NewTiles)
    987987end;
    988988
    989989
    990990// negotiation
    991 function TCustomAI.Nego_CheckMyAction: integer;
    992 begin
    993 assert(Opponent>=0); // only allowed in negotiation mode
    994 assert((MyAction=scDipNotice) or (MyAction=scDipAccept)
     991function TCustomAI.Nego_CheckMyAction: Integer;
     992begin
     993Assert(Opponent>=0); // only allowed in negotiation mode
     994Assert((MyAction=scDipNotice) or (MyAction=scDipAccept)
    995995  or (MyAction=scDipCancelTreaty) or (MyAction=scDipOffer)
    996996  or (MyAction=scDipBreak));
    997 if MyAction=scDipOffer then result:=Server(MyAction-sExecute, me, 0, MyOffer)
    998 else result:=Server(MyAction-sExecute, me, 0, nodata^);
     997if MyAction=scDipOffer then Result:=Server(MyAction-sExecute, Me, 0, MyOffer)
     998else Result:=Server(MyAction-sExecute, Me, 0, nodata^);
    999999end;
    10001000
    10011001
    10021002initialization
    1003 nodata:=pointer(0);
     1003nodata:=Pointer(0);
    10041004RWDataSize:=0;
    10051005
  • trunk/AI/StdAI/Pile.pas

    r289 r447  
    88interface
    99
    10 procedure Create(Size: integer);
     10procedure Create(Size: Integer);
    1111procedure Free;
    1212procedure Empty;
    13 function Put(Item, Value: integer): boolean;
    14 function TestPut(Item, Value: integer): boolean;
    15 function Get(var Item, Value: integer): boolean;
     13function Put(Item, Value: Integer): Boolean;
     14function TestPut(Item, Value: Integer): Boolean;
     15function Get(var Item, Value: Integer): Boolean;
    1616
    1717
     
    2323type
    2424  TheapItem = record
    25     Item: integer;
    26     Value: integer;
     25    Item: Integer;
     26    Value: Integer;
    2727  end;
    2828
    2929var
    3030  bh: array[0..MaxSize - 1] of TheapItem;
    31   Ix: array[0..MaxSize - 1] of integer;
    32   n, CurrentSize: integer;
    33 {$IFDEF DEBUG}InUse: boolean;{$ENDIF}
     31  Ix: array[0..MaxSize - 1] of Integer;
     32  N, CurrentSize: Integer;
     33{$IFDEF DEBUG}InUse: Boolean;{$ENDIF}
    3434
    3535
    36 procedure Create(Size: integer);
     36procedure Create(Size: Integer);
    3737begin
    3838  {$IFDEF DEBUG}
    39   assert(not InUse, 'Pile is a single instance class, ' +
     39  Assert(not InUse, 'Pile is a single instance class, ' +
    4040    'no multiple usage possible. Always call Pile.Free after use.');
    4141{$ENDIF}
    42   assert(Size <= MaxSize);
    43   if (n <> 0) or (Size > CurrentSize) then
     42  Assert(Size <= MaxSize);
     43  if (N <> 0) or (Size > CurrentSize) then
    4444  begin
    45     FillChar(Ix, Size * sizeOf(integer), 255);
    46     n := 0;
     45    FillChar(Ix, Size * sizeOf(Integer), 255);
     46    N := 0;
    4747  end;
    4848  CurrentSize := Size;
     
    5555begin
    5656        {$IFDEF DEBUG}
    57   assert(InUse);
     57  Assert(InUse);
    5858  InUse := False;
    5959{$ENDIF}
     
    6262procedure Empty;
    6363begin
    64   if n <> 0 then
     64  if N <> 0 then
    6565  begin
    66     FillChar(Ix, CurrentSize * sizeOf(integer), 255);
    67     n := 0;
     66    FillChar(Ix, CurrentSize * sizeOf(Integer), 255);
     67    N := 0;
    6868  end;
    6969end;
    7070
    7171//Parent(i) = (i-1)/2.
    72 function Put(Item, Value: integer): boolean; //O(lg(n))
     72function Put(Item, Value: Integer): Boolean; //O(lg(n))
    7373var
    74   i, j: integer;
     74  I, J: Integer;
    7575begin
    76   assert(Item < CurrentSize);
    77   i := Ix[Item];
    78   if i >= 0 then
     76  Assert(Item < CurrentSize);
     77  I := Ix[Item];
     78  if I >= 0 then
    7979  begin
    80     if bh[i].Value <= Value then
     80    if bh[I].Value <= Value then
    8181    begin
    8282      Result := False;
    83       exit;
     83      Exit;
    8484    end;
    8585  end
    8686  else
    8787  begin
    88     i := n;
    89     Inc(n);
     88    I := N;
     89    Inc(N);
    9090  end;
    9191
    92   while i > 0 do
     92  while I > 0 do
    9393  begin
    94     j := (i - 1) shr 1;  //Parent(i) = (i-1)/2
    95     if Value >= bh[j].Value then
    96       break;
    97     bh[i] := bh[j];
    98     Ix[bh[i].Item] := i;
    99     i := j;
     94    J := (I - 1) shr 1;  //Parent(i) = (i-1)/2
     95    if Value >= bh[J].Value then
     96      Break;
     97    bh[I] := bh[J];
     98    Ix[bh[I].Item] := I;
     99    I := J;
    100100  end;
    101101  //  Insert the new Item at the insertion point found.
    102   bh[i].Value := Value;
    103   bh[i].Item := Item;
    104   Ix[bh[i].Item] := i;
     102  bh[I].Value := Value;
     103  bh[I].Item := Item;
     104  Ix[bh[I].Item] := I;
    105105  Result := True;
    106106end;
    107107
    108 function TestPut(Item, Value: integer): boolean;
     108function TestPut(Item, Value: Integer): Boolean;
    109109var
    110   i: integer;
     110  I: Integer;
    111111begin
    112   assert(Item < CurrentSize);
    113   i := Ix[Item];
    114   Result := (i < 0) or (bh[i].Value > Value);
     112  Assert(Item < CurrentSize);
     113  I := Ix[Item];
     114  Result := (I < 0) or (bh[I].Value > Value);
    115115end;
    116116
    117117//Left(i) = 2*i+1.
    118118//Right(i) = 2*i+2 => Left(i)+1
    119 function Get(var Item, Value: integer): boolean; //O(lg(n))
     119function Get(var Item, Value: Integer): Boolean; //O(lg(n))
    120120var
    121   i, j: integer;
    122   last: TheapItem;
     121  I, J: Integer;
     122  Last: TheapItem;
    123123begin
    124   if n = 0 then
     124  if N = 0 then
    125125  begin
    126126    Result := False;
    127     exit;
     127    Exit;
    128128  end;
    129129
     
    133133  Ix[Item] := -1;
    134134
    135   Dec(n);
    136   if n > 0 then
     135  Dec(N);
     136  if N > 0 then
    137137  begin
    138     last := bh[n];
    139     i := 0;
    140     j := 1;
    141     while j < n do
     138    Last := bh[N];
     139    I := 0;
     140    J := 1;
     141    while J < N do
    142142    begin
    143143      //  Right(i) = Left(i)+1
    144       if (j < n - 1) and (bh[j].Value > bh[j + 1].Value) then
    145         Inc(j);
    146       if last.Value <= bh[j].Value then
    147         break;
     144      if (J < N - 1) and (bh[J].Value > bh[J + 1].Value) then
     145        Inc(J);
     146      if Last.Value <= bh[J].Value then
     147        Break;
    148148
    149       bh[i] := bh[j];
    150       Ix[bh[i].Item] := i;
    151       i := j;
    152       j := j shl 1 + 1;  //Left(j) = 2*j+1
     149      bh[I] := bh[J];
     150      Ix[bh[I].Item] := I;
     151      I := J;
     152      J := J shl 1 + 1;  //Left(j) = 2*j+1
    153153    end;
    154154
    155155    // Insert the root in the correct place in the heap.
    156     bh[i] := last;
    157     Ix[last.Item] := i;
     156    bh[I] := Last;
     157    Ix[Last.Item] := I;
    158158  end;
    159159  Result := True;
     
    161161
    162162initialization
    163   n := 0;
     163  N := 0;
    164164  CurrentSize := 0;
    165165        {$IFDEF DEBUG}
  • trunk/AI/StdAI/Protocol.pas

    r328 r447  
    916916  Cost: Integer;
    917917  Maint: Integer;
    918   Expiration: integer;
     918  Expiration: Integer;
    919919end
    920920= ((Kind: ikWonder; Preq: adMathematics; Cost: 400; Maint: 0;
     
    11201120  Strength: Integer;
    11211121  Trans: Integer;
    1122   Cost: integer;
     1122  Cost: Integer;
    11231123end
    11241124= (((Preq: adWarriorCode; Strength: 4; Trans: 0; Cost: 3),
     
    15101510  TGetCityData = record
    15111511    Owner: Integer;
    1512     c: TCity;
     1512    C: TCity;
    15131513  end;
    15141514
     
    15911591  TCreateUnitData = record
    15921592    Loc: Integer;
    1593     p: Integer;
     1593    P: Integer;
    15941594    mix: Integer;
    15951595  end;
     
    16001600  TOwnerList = array [0 .. INFIN] of ShortInt;
    16011601  TByteList = array [0 .. INFIN] of Byte;
    1602   TIntList = array [0 .. INFIN] of integer;
     1602  TIntList = array [0 .. INFIN] of Integer;
    16031603  TCityList = array [0 .. INFIN] of TCity;
    16041604  TUnList = array [0 .. INFIN] of TUn;
     
    17611761    0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)));
    17621762
    1763   SpecialModelPreq: array [0 .. nSpecialModel - 1] of integer = (preNone,
     1763  SpecialModelPreq: array [0 .. nSpecialModel - 1] of Integer = (preNone,
    17641764    adExplosives, preNone, preNone, (* adWri, *) adIntelligence, adTrade,
    17651765    (* adTheCorporation,adHorsebackRiding,adAutomobile,adNavigation,
     
    17701770  DelphiRandSeed: Integer;
    17711771
    1772 procedure MakeUnitInfo(p: Integer; const u: TUn; var ui: TUnitInfo);
    1773 procedure MakeModelInfo(p, mix: Integer; const m: TModel; var mi: TModelInfo);
     1772procedure MakeUnitInfo(P: Integer; const U: TUn; var ui: TUnitInfo);
     1773procedure MakeModelInfo(P, mix: Integer; const M: TModel; var mi: TModelInfo);
    17741774function IsSameModel(const mi1, mi2: TModelInfo): Boolean;
    17751775function SpecialTile(Loc, TerrType, lx: Integer): Integer;
     
    17811781implementation
    17821782
    1783 procedure MakeUnitInfo(p: Integer; const u: TUn; var ui: TUnitInfo);
     1783procedure MakeUnitInfo(P: Integer; const U: TUn; var ui: TUnitInfo);
    17841784begin
    1785   ui.Owner := p;
    1786   ui.Loc := u.Loc;
    1787   ui.Health := u.Health;
    1788   ui.Fuel := u.Fuel;
    1789   ui.Job := u.Job;
    1790   ui.Exp := u.Exp;
    1791   ui.Load := u.TroopLoad + u.AirLoad;
    1792   ui.mix := u.mix;
    1793   ui.Flags := u.Flags;
     1785  ui.Owner := P;
     1786  ui.Loc := U.Loc;
     1787  ui.Health := U.Health;
     1788  ui.Fuel := U.Fuel;
     1789  ui.Job := U.Job;
     1790  ui.Exp := U.Exp;
     1791  ui.Load := U.TroopLoad + U.AirLoad;
     1792  ui.mix := U.mix;
     1793  ui.Flags := U.Flags;
    17941794end;
    17951795
    1796 procedure MakeModelInfo(p, mix: Integer; const m: TModel; var mi: TModelInfo);
     1796procedure MakeModelInfo(P, mix: Integer; const M: TModel; var mi: TModelInfo);
    17971797var
    1798   i: Integer;
     1798  I: Integer;
    17991799begin
    1800   mi.Owner := p;
     1800  mi.Owner := P;
    18011801  mi.mix := mix;
    1802   mi.ID := m.ID;
    1803   mi.Domain := m.Domain;
    1804   if m.Kind = mkEnemyDeveloped then
     1802  mi.ID := M.ID;
     1803  mi.Domain := M.Domain;
     1804  if M.Kind = mkEnemyDeveloped then
    18051805    mi.Kind := mkSelfDeveloped // important for IsSameModel()
    18061806  else
    1807     mi.Kind := m.Kind;
    1808   mi.Attack := m.Attack;
    1809   mi.Defense := m.Defense;
    1810   mi.Speed := m.Speed;
    1811   mi.Cost := m.Cost;
     1807    mi.Kind := M.Kind;
     1808  mi.Attack := M.Attack;
     1809  mi.Defense := M.Defense;
     1810  mi.Speed := M.Speed;
     1811  mi.Cost := M.Cost;
    18121812  if mi.Domain = dAir then
    18131813  begin
    1814     mi.TTrans := m.Cap[mcAirTrans] * m.MTrans;
    1815     mi.ATrans_Fuel := m.Cap[mcFuel];
     1814    mi.TTrans := M.Cap[mcAirTrans] * M.MTrans;
     1815    mi.ATrans_Fuel := M.Cap[mcFuel];
    18161816  end
    18171817  else
    18181818  begin
    1819     mi.TTrans := m.Cap[mcSeaTrans] * m.MTrans;
    1820     mi.ATrans_Fuel := m.Cap[mcCarrier] * m.MTrans;
    1821   end;
    1822   mi.Bombs := m.Cap[mcBombs] * m.MStrength * 2;
     1819    mi.TTrans := M.Cap[mcSeaTrans] * M.MTrans;
     1820    mi.ATrans_Fuel := M.Cap[mcCarrier] * M.MTrans;
     1821  end;
     1822  mi.Bombs := M.Cap[mcBombs] * M.MStrength * 2;
    18231823  mi.Cap := 0;
    1824   for i := mcFirstNonCap to nFeature - 1 do
    1825     if m.Cap[i] > 0 then
    1826       mi.Cap := mi.Cap or (1 shl (i - mcFirstNonCap));
     1824  for I := mcFirstNonCap to nFeature - 1 do
     1825    if M.Cap[I] > 0 then
     1826      mi.Cap := mi.Cap or (1 shl (I - mcFirstNonCap));
    18271827  mi.MaxUpgrade := 0;
    1828   for i := 1 to nUpgrade - 1 do
    1829     if m.Upgrades and (1 shl i) <> 0 then
    1830       mi.MaxUpgrade := i;
    1831   mi.Weight := m.Weight;
     1828  for I := 1 to nUpgrade - 1 do
     1829    if M.Upgrades and (1 shl I) <> 0 then
     1830      mi.MaxUpgrade := I;
     1831  mi.Weight := M.Weight;
    18321832  mi.Lost := 0;
    18331833end;
     
    18411841  Compare1 := @mi1;
    18421842  Compare2 := @mi2;
    1843   result := (Compare1[1] and $FFFF0000 = Compare2[1] and $FFFF0000) and
     1843  Result := (Compare1[1] and $FFFF0000 = Compare2[1] and $FFFF0000) and
    18441844    (Compare1[2] = Compare2[2]) and (Compare1[3] = Compare2[3]) and
    18451845    (Compare1[4] = Compare2[4]) and (Compare1[5] = Compare2[5])
     
    18481848function SpecialTile(Loc, TerrType, lx: Integer): Integer;
    18491849var
    1850   x, y, qx, qy, a: Integer;
     1850  X, Y, qx, qy, A: Integer;
    18511851begin
    18521852  if TerrType = fOcean then
    1853     result := 0
     1853    Result := 0
    18541854  else
    18551855  begin
    1856     y := Loc div lx;
    1857     x := Loc - y * lx;
     1856    Y := Loc div lx;
     1857    X := Loc - Y * lx;
    18581858    if TerrType = fGrass then { formula for productive grassland }
    1859       if Odd((lymax + x - y shr 1) shr 1 + x + (y + 1) shr 1) then
    1860         result := 1
     1859      if Odd((lymax + X - Y shr 1) shr 1 + X + (Y + 1) shr 1) then
     1860        Result := 1
    18611861      else
    1862         result := 0
     1862        Result := 0
    18631863    else { formula for special resources }
    18641864    begin
    1865       a := 4 * x - y + 9980;
    1866       qx := a div 10;
    1867       if (qx * 10 = a) and (qx and 3 <> 0) then
     1865      A := 4 * X - Y + 9980;
     1866      qx := A div 10;
     1867      if (qx * 10 = A) and (qx and 3 <> 0) then
    18681868      begin
    1869         qy := (y + x) div 5;
     1869        qy := (Y + X) div 5;
    18701870        if qy and 3 <> qx shr 2 and 1 * 2 then
    18711871          if (TerrType = fArctic) or (TerrType = fSwamp) then
    1872             result := 1
     1872            Result := 1
    18731873          else if TerrType = fShore then
    18741874          begin
    18751875            if (qx + qy) and 1 = 0 then
    18761876              if qx and 3 = 2 then
    1877                 result := 2
     1877                Result := 2
    18781878              else
    1879                 result := 1
     1879                Result := 1
    18801880            else
    1881               result := 0
     1881              Result := 0
    18821882          end
    18831883          else
    1884             result := (qx + qy) and 1 + 1
     1884            Result := (qx + qy) and 1 + 1
    18851885        else
    1886           result := 0;
     1886          Result := 0;
    18871887      end
    18881888      else
    1889         result := 0;
     1889        Result := 0;
    18901890    end
    18911891  end;
  • trunk/AI/StdAI/StdAI.lpr

    r289 r447  
    1212var
    1313  AIList: array[0..nPl - 1] of TCustomAI;
    14   Defender: integer;
     14  Defender: Integer;
    1515
    1616
    17   procedure Client(Command, Player: integer; var Data); stdcall;
     17  procedure Client(Command, Player: Integer; var Data); stdcall;
    1818  var
    19     p, y0, ToLoc: integer;
     19    P, y0, ToLoc: Integer;
    2020    UnitInfo: TUnitInfo;
    2121  begin
     
    3232{$ENDIF}
    3333        CustomAI.Init(TNewGameData(Data));
    34         for p := nPl - 1 downto 0 do
    35           if G.RO[p] <> nil then
     34        for P := nPl - 1 downto 0 do
     35          if G.RO[P] <> nil then
    3636          begin
    37             AIList[p] := TAI.Create(p);
    38             AIList[p].SetDataDefaults;
     37            AIList[P] := TAI.Create(P);
     38            AIList[P].SetDataDefaults;
    3939          end
    4040          else
    41             AIList[p] := nil;
     41            AIList[P] := nil;
    4242        Defender := -1;
    4343      end;
    4444      cGetReady:
    45         for p := nPl - 1 downto 0 do
    46           if AIList[p] <> nil then
    47             AIList[p].SetDataRandom;
     45        for P := nPl - 1 downto 0 do
     46          if AIList[P] <> nil then
     47            AIList[P].SetDataRandom;
    4848      cBreakGame:
    49         for p := 0 to nPl - 1 do
    50           if AIList[p] <> nil then
    51             AIList[p].Free;
     49        for P := 0 to nPl - 1 do
     50          if AIList[P] <> nil then
     51            AIList[P].Free;
    5252
    5353      cTurn, cContinue, scContact..scDipBreak, cShowEndContact:
  • trunk/AI/StdAI/ToolAI.pas

    r442 r447  
    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;
     105    function CurrentMStrength(Domain: Integer): Integer;
    106106  end;
    107107
     
    132132
    133133var
    134   nContinent, nOcean, nDistrict: integer;
    135   Formation: array[0..lxmax * lymax - 1] of integer;
     134  nContinent, nOcean, nDistrict: Integer;
     135  Formation: array[0..lxmax * lymax - 1] of Integer;
    136136  // water: ocean index, land: continent index, sorted by size
    137137  // territory unpassable due to peace treaty divides a continent
    138   District: array[0..lxmax * lymax - 1] of integer;
     138  District: array[0..lxmax * lymax - 1] of Integer;
    139139  // index of coherent own territory, sorted by size
    140   CityResult: array[0..nCmax - 1] of integer;
    141 
    142   Advancedness: array[0..nAdv - 1] of integer;
     140  CityResult: array[0..nCmax - 1] of Integer;
     141
     142  Advancedness: array[0..nAdv - 1] of Integer;
    143143// total number of prerequisites for each advance
    144144
     
    149149
    150150type
    151   pinteger = ^integer;
     151  pinteger = ^Integer;
    152152
    153153var
    154154  // for JobAssignment
    155   MaxScore: integer;
    156   TileJob, TileJobScore: array[0..lxmax * lymax - 1] of byte;
    157   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
    158158
    159159  // for Transport
    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 
    165 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);
    166166begin
    167167  while Start <> Stop do
     
    173173end;
    174174
    175 function NextZero(Start, Stop: pinteger; Mask: cardinal): pinteger;
     175function NextZero(Start, Stop: pinteger; Mask: Cardinal): pinteger;
    176176begin
    177177  while (Start <> Stop) and (Start^ and Mask <> 0) do
     
    180180end;
    181181
    182 function TToolAI.CenterOfEmpire: integer;
    183 var
    184   cix, Loc, x, y, sy, n: integer;
    185   a, su, sv: double;
    186 begin
    187   n := 0;
     182function TToolAI.CenterOfEmpire: Integer;
     183var
     184  cix, Loc, X, Y, sy, N: Integer;
     185  A, su, sv: Double;
     186begin
     187  N := 0;
    188188  sy := 0;
    189189  su := 0;
     
    194194    if Loc >= 0 then
    195195    begin
    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;
    212 end;
    213 
    214 function TToolAI.CityTaxBalance(cix: integer; const CityReport: TCityReport): integer;
    215 var
    216   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;
    217217begin
    218218  Result := 0;
     
    229229      Inc(Result, CityReport.FoodRep - CityReport.Eaten);
    230230  end;
    231   for i := nWonder to nImp - 1 do
    232     if MyCity[cix].Built[i] > 0 then
    233       Dec(Result, Imp[i].Maint);
    234 end;
    235 
    236 procedure TToolAI.SumCities(TaxRate: integer; var TaxSum, ScienceSum: integer);
    237 var
    238   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;
    239239  CityReport: TCityReport;
    240240begin
     
    242242  ScienceSum := 0;
    243243  if RO.Government = gAnarchy then
    244     exit;
     244    Exit;
    245245  for p1 := 0 to nPl - 1 do
    246246    if RO.Tribute[p1] <= RO.TributePaid[p1] then
     
    268268procedure TToolAI.OptimizeCityTiles;
    269269var
    270   cix: integer;
     270  cix: Integer;
    271271begin
    272272  for cix := 0 to RO.nCity - 1 do
     
    278278procedure TToolAI.GetCityProdPotential;
    279279var
    280   cix: integer;
     280  cix: Integer;
    281281  Advice: TCityTileAdviceData;
    282282begin
     
    286286      begin
    287287        Advice.ResourceWeights := rwMaxProd;
    288         Server(sGetCityTileAdvice, me, cix, Advice);
     288        Server(sGetCityTileAdvice, Me, cix, Advice);
    289289        CityResult[cix] := Advice.CityReport.ProdRep; // considers factory, but shouldn't
    290290      end;
     
    293293procedure TToolAI.GetCityTradePotential;
    294294var
    295   cix: integer;
     295  cix: Integer;
    296296  Advice: TCityTileAdviceData;
    297297begin
     
    301301      begin
    302302        Advice.ResourceWeights := rwMaxScience;
    303         Server(sGetCityTileAdvice, me, cix, Advice);
     303        Server(sGetCityTileAdvice, Me, cix, Advice);
    304304        CityResult[cix] := Advice.CityReport.Trade;
    305305      end;
     
    314314procedure TToolAI.JobAssignment_Initialize;
    315315begin
    316   fillchar(JobLocOfSettler, RO.nUn * sizeof(integer), $FF); // -1
    317   fillchar(TileJob, MapSize, jNone);
    318   fillchar(TileJobScore, MapSize, 0);
     316  FillChar(JobLocOfSettler, RO.nUn * SizeOf(Integer), $FF); // -1
     317  FillChar(TileJob, MapSize, jNone);
     318  FillChar(TileJobScore, MapSize, 0);
    319319  MaxScore := 0;
    320320end;
    321321
    322 procedure TToolAI.JobAssignment_AddJob(Loc, Job, Score: integer);
     322procedure TToolAI.JobAssignment_AddJob(Loc, Job, Score: Integer);
    323323begin
    324324  if Score > 255 then
     
    333333end;
    334334
    335 procedure TToolAI.JobAssignment_AddUnit(uix: integer);
    336 begin
    337   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]);
    338338  JobLocOfSettler[uix] := ToAssign;
    339339end;
    340340
    341 function TToolAI.JobAssignment_GotJob(uix: integer): boolean;
     341function TToolAI.JobAssignment_GotJob(uix: Integer): Boolean;
    342342begin
    343343  Result := JobLocOfSettler[uix] >= 0;
     
    347347const
    348348  DistanceScore = 4;
    349   StepSizeByTerrain: array[0..11] of integer =
     349  StepSizeByTerrain: array[0..11] of Integer =
    350350    (0, 0, 1, 2, 1, 1, 0, 1, 0, 1, 1, 2);
    351351  //Oc-Sh-Gr-De-Pr-Tu-Ar-Sw-XX-Fo-Hi-Mo
    352352var
    353353  uix, BestScore, BestCount, BestLoc, BestJob, BestDistance, TestLoc,
    354   NextLoc, TestDistance, V8, TestScore, StepSize, MoveResult: integer;
    355   UnitsToAssign: boolean;
     354  NextLoc, TestDistance, V8, TestScore, StepSize, MoveResult: Integer;
     355  UnitsToAssign: Boolean;
    356356  Adjacent: TVicinity8Loc;
    357357  SettlerOfJobLoc, DistToLoc: array[0..lxmax * lymax - 1] of smallint;
    358358  // DistToLoc is only defined where SettlerOfJobLoc>=0
    359   TileChecked: array[0..lxmax * lymax - 1] of boolean;
    360 begin
    361   fillchar(SettlerOfJobLoc, MapSize * 2, $FF); // -1
     359  TileChecked: array[0..lxmax * lymax - 1] of Boolean;
     360begin
     361  FillChar(SettlerOfJobLoc, MapSize * 2, $FF); // -1
    362362
    363363  // keep up jobs that are already started
     
    380380        BestJob := jNone;
    381381        BestScore := -999999;
    382         FillChar(TileChecked, MapSize * sizeof(boolean), False);
     382        FillChar(TileChecked, MapSize * SizeOf(Boolean), False);
    383383        Pile.Create(MapSize);
    384384        Pile.Put(MyUnit[uix].Loc, 0); // start search for new job at current location
     
    399399                  and (Map[NextLoc] and (fUnit or fOwned) <> fUnit) // no foreign unit
    400400                  and ((RO.Territory[NextLoc] < 0) or
    401                   (RO.Territory[NextLoc] = me)) // no foreign territory
     401                  (RO.Territory[NextLoc] = Me)) // no foreign territory
    402402                  and (Map[TestLoc] and Map[NextLoc] and fInEnemyZoC = 0) then
    403403                  // move not prevented by ZoC
     
    414414            ((SettlerOfJobLoc[TestLoc] < 0) or (DistToLoc[TestLoc] > TestDistance)) then
    415415          begin
    416             TestScore := integer(TileJobScore[TestLoc]) - DistanceScore * TestDistance;
     416            TestScore := Integer(TileJobScore[TestLoc]) - DistanceScore * TestDistance;
    417417            if TestScore > BestScore then
    418418              BestCount := 0;
     
    469469procedure TToolAI.AnalyzeMap;
    470470var
    471   i, j, Loc, Loc1, V8, Count, Kind, MostIndex: integer;
     471  I, J, Loc, Loc1, V8, Count, Kind, MostIndex: Integer;
    472472  Adjacent: TVicinity8Loc;
    473473  IndexOfID: array[0..lxmax * lymax - 1] of smallint;
    474474  IDOfIndex: array[0..lxmax * lymax div 2 - 1] of smallint;
    475475begin
    476   fillchar(District, MapSize * 4, $FF);
     476  FillChar(District, MapSize * 4, $FF);
    477477  for Loc := 0 to MapSize - 1 do
    478478    if Map[Loc] and fTerrain = fUNKNOWN then
     
    500500              Formation[Loc], Formation[Loc1]);
    501501      end;
    502       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
    503503      begin
    504504        District[Loc] := Loc;
     
    545545        Inc(Count);
    546546      end;
    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;
     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;
    562562
    563563    case Kind of
     
    605605// other:  |   Basic   | 0| Speed  |              X X X             | MaxTerrType  |
    606606
    607 function TToolAI.GetMyMoveStyle(mix, Health: integer): integer;
     607function TToolAI.GetMyMoveStyle(mix, Health: Integer): Integer;
    608608begin
    609609  with MyModel[mix] do
     
    614614      begin
    615615        Inc(Result, (50 + (Speed - 150) * 13 shr 7) shl 8); //HeavyCost
    616         if RO.Wonder[woShinkansen].EffectiveOwner <> me then
     616        if RO.Wonder[woShinkansen].EffectiveOwner <> Me then
    617617          Inc(Result, Speed * (4 * 1311) shr 17); // RailCost
    618         if (RO.Wonder[woGardens].EffectiveOwner <> me) or
     618        if (RO.Wonder[woGardens].EffectiveOwner <> Me) or
    619619          (Kind = mkSettler) and (Speed >= 200) then
    620620          Inc(Result, msHostile);
     
    631631      begin
    632632        Result := Speed;
    633         if RO.Wonder[woMagellan].EffectiveOwner = me then
     633        if RO.Wonder[woMagellan].EffectiveOwner = Me then
    634634          Inc(Result, 200);
    635635        if Health < 100 then
     
    646646end;
    647647
    648 function TToolAI.CheckStep(MoveStyle, TimeBeforeStep, CrossCorner: integer;
    649   var TimeAfterStep, RecoverTurns: integer; FromTile, ToTile: integer;
    650   IsCapture: boolean): integer;
    651 var
    652   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;
    653653begin
    654654  //IsCapture:=true;
    655   assert(((FromTile and fTerrain <= fMountains) or (FromTile and
     655  Assert(((FromTile and fTerrain <= fMountains) or (FromTile and
    656656    fTerrain = fUNKNOWN)) and ((ToTile and fTerrain <= fMountains) or
    657657    (ToTile and fTerrain = fUNKNOWN)));
     
    701701                if ToTile and fPeace <> 0 then
    702702                  Result := csCheckTerritory;
    703                 exit;
     703                Exit;
    704704              end;
    705705            end;
     
    820820          begin
    821821            Result := csForbiddenTile;
    822             exit;
     822            Exit;
    823823          end;
    824824        end
     
    869869              // must wait for next turn
    870870              Result := csOk;
    871               exit;
     871              Exit;
    872872            end;
    873873          end;
     
    894894-------- Pathfinding Reference Implementation --------
    895895var
    896 MoveStyle,V8,Loc,Time,NextLoc,NextTime,RecoverTurns: integer;
     896MoveStyle,V8,Loc,Time,NextLoc,NextTime,RecoverTurns: Integer;
    897897Adjacent: TVicinity8Loc;
    898 Reached: array[0..lxmax*lymax-1] of boolean;
    899 begin
    900 fillchar(Reached, MapSize, false);
     898Reached: array[0..lxmax*lymax-1] of Boolean;
     899begin
     900FillChar(Reached, MapSize, False);
    901901MoveStyle:=GetMyMoveStyle(MyUnit[uix].mix, MyUnit[uix].Health);
    902902Pile.Create(MapSize);
     
    906906  // todo: check exit condition, e.g. whether destination reached
    907907
    908   Reached[Loc]:=true;
     908  Reached[Loc]:=True;
    909909  V8_to_Loc(Loc, Adjacent);
    910910  for V8:=0 to 7 do
     
    916916          Pile.Put(NextLoc, NextTime+RecoverTurns*$1000);
    917917        csForbiddenTile:
    918           Reached[NextLoc]:=true; // don't check moving there again
     918          Reached[NextLoc]:=True; // don't check moving there again
    919919        csCheckTerritory:
    920920          if RO.Territory[NextLoc]=RO.Territory[Loc] then
     
    927927*)
    928928
    929 function TToolAI.Unit_MoveEx(uix, ToLoc: integer; Options: integer): integer;
     929function TToolAI.Unit_MoveEx(uix, ToLoc: Integer; Options: Integer): Integer;
    930930var
    931931  Loc, NextLoc, Temp, FromLoc, EndLoc, Time, V8, MoveResult, RecoverTurns,
    932   NextTime, MoveStyle: integer;
     932  NextTime, MoveStyle: Integer;
    933933  Adjacent: TVicinity8Loc;
    934   PreLoc: array[0..lxmax * lymax - 1] of integer;
    935   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;
    936936begin
    937937  Result := eOk;
    938938  FromLoc := MyUnit[uix].Loc;
    939939  if FromLoc = ToLoc then
    940     exit;
     940    Exit;
    941941
    942942  FillChar(Reached, MapSize, False);
     
    10031003      begin
    10041004        Result := MoveResult;
    1005         break;
     1005        Break;
    10061006      end;
    10071007    end;
     
    10161016procedure TToolAI.SeaTransport_BeginInitialize;
    10171017begin
    1018   fillchar(TransportAvailable, RO.nUn * sizeof(integer), $FF); // -1
     1018  FillChar(TransportAvailable, RO.nUn * SizeOf(Integer), $FF); // -1
    10191019  InitComplete := False;
    10201020  HaveDestinations := False;
     
    10251025end;
    10261026
    1027 procedure TToolAI.SeaTransport_AddLoad(uix: integer);
    1028 var
    1029   i: integer;
    1030 begin
    1031   assert(not InitComplete); // call order violation!
     1027procedure TToolAI.SeaTransport_AddLoad(uix: Integer);
     1028var
     1029  I: Integer;
     1030begin
     1031  Assert(not InitComplete); // call order violation!
    10321032  if Map[MyUnit[uix].Loc] and fTerrain < fGrass then
    1033     exit;
    1034   for i := 0 to nTransportLoad - 1 do
    1035     if uix = uixTransportLoad[i] then
    1036       exit;
     1033    Exit;
     1034  for I := 0 to nTransportLoad - 1 do
     1035    if uix = uixTransportLoad[I] then
     1036      Exit;
    10371037  uixTransportLoad[nTransportLoad] := uix;
    10381038  Inc(nTransportLoad);
    10391039end;
    10401040
    1041 procedure TToolAI.SeaTransport_AddTransport(uix: integer);
    1042 var
    1043   MoveStyle: integer;
    1044 begin
    1045   assert(not InitComplete); // call order violation!
    1046   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);
    10471047  TransportAvailable[uix] := 1;
    10481048  with MyModel[MyUnit[uix].mix] do
     
    10581058end;
    10591059
    1060 procedure TToolAI.SeaTransport_AddDestination(Loc: integer);
    1061 begin
    1062   assert(not InitComplete); // call order violation!
     1060procedure TToolAI.SeaTransport_AddDestination(Loc: Integer);
     1061begin
     1062  Assert(not InitComplete); // call order violation!
    10631063  Pile.Put(Loc, $800);
    10641064  HaveDestinations := True;
     
    10671067procedure TToolAI.SeaTransport_EndInitialize;
    10681068var
    1069   Loc0, Time0, V8, Loc1, ArriveTime, RecoverTurns: integer;
     1069  Loc0, Time0, V8, Loc1, ArriveTime, RecoverTurns: Integer;
    10701070  Adjacent: TVicinity8Loc;
    10711071begin
    1072   assert(not InitComplete); // call order violation!
     1072  Assert(not InitComplete); // call order violation!
    10731073  InitComplete := True;
    10741074  if HaveDestinations then
    10751075  begin // calculate TurnsAfterLoad from destination locs
    1076     fillchar(TurnsAfterLoad, MapSize, $FF); // -1
     1076    FillChar(TurnsAfterLoad, MapSize, $FF); // -1
    10771077    while Pile.Get(Loc0, Time0) do
    10781078    begin // search backward
     
    11001100
    11011101function TToolAI.SeaTransport_MakeGroupPlan(
    1102   var TransportPlan: TGroupTransportPlan): boolean;
    1103 var
    1104   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,
    11051105  TurnsLoaded, TurnCount, tuix, tuix1, ArriveTime, TotalDelay,
    11061106  BestTotalDelay, GroupCount, BestGroupCount, BestLoadLoc, FullMovementLoc,
    1107   nSelectedLoad, f, OriginContinent, a, b: integer;
    1108   CompleteFlag, NotReachedFlag, ContinueUnit: cardinal;
    1109   IsComplete, ok, IsFirstLoc: boolean;
     1107  nSelectedLoad, F, OriginContinent, A, B: Integer;
     1108  CompleteFlag, NotReachedFlag, ContinueUnit: Cardinal;
     1109  IsComplete, ok, IsFirstLoc: Boolean;
    11101110  StartLocPtr, ArrivedEnd: pinteger;
    11111111  Adjacent: TVicinity8Loc;
    1112   uixSelectedLoad: array[0..15] of integer;
    1113   tuixSelectedLoad: array[0..15] of integer;
    1114   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;
    11151115  ResponsibleTransport: array[0..lxmax * lymax - 1] of smallint;
    1116   TurnsBeforeLoad: array[0..lxmax * lymax - 1] of shortint;
    1117   GroupComplete: array[0..lxmax * lymax - 1] of boolean;
    1118 begin
    1119   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!
    11201120
    11211121  if HaveDestinations and (nTransportLoad > 0) then
     
    11281128        for tuix := 0 to nTransportLoad - 1 do
    11291129        begin
    1130           Loc_to_ab(MyUnit[uix].Loc, MyUnit[uixTransportLoad[tuix]].Loc, a, b);
    1131           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
    11321132          begin
    1133             assert((a <> 0) or (b <> 0));
     1133            Assert((A <> 0) or (B <> 0));
    11341134            Inc(GroupCount);
    11351135          end;
     
    11451145          for tuix := nTransportLoad - 1 downto 0 do
    11461146          begin
    1147             Loc_to_ab(TransportPlan.LoadLoc, MyUnit[uixTransportLoad[tuix]].Loc, a, b);
    1148             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
    11491149            begin
    11501150              TransportPlan.uixLoad[TransportPlan.nLoad] := uixTransportLoad[tuix];
     
    11531153              Inc(TransportPlan.nLoad);
    11541154              if TransportPlan.nLoad = TransportCapacity then
    1155                 break;
     1155                Break;
    11561156            end;
    11571157          end;
    11581158          Result := True;
    1159           exit;
     1159          Exit;
    11601160        end;
    11611161      end;
     
    11651165  begin
    11661166    // select units from same continent
    1167     fillchar(Arrived, 4 * nContinent, 0); // misuse Arrived as counter
     1167    FillChar(Arrived, 4 * nContinent, 0); // misuse Arrived as counter
    11681168    for tuix := 0 to nTransportLoad - 1 do
    11691169    begin
    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]);
     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]);
    11741174    end;
    11751175    OriginContinent := 0;
    1176     for f := 1 to nContinent - 1 do
    1177       if Arrived[f] > Arrived[OriginContinent] then
    1178         OriginContinent := f;
     1176    for F := 1 to nContinent - 1 do
     1177      if Arrived[F] > Arrived[OriginContinent] then
     1178        OriginContinent := F;
    11791179    nSelectedLoad := 0;
    11801180    for tuix := 0 to nTransportLoad - 1 do
     
    11851185        Inc(nSelectedLoad);
    11861186        if nSelectedLoad = 16 then
    1187           break;
     1187          Break;
    11881188      end;
    11891189
    11901190    Pile.Create(MapSize);
    1191     fillchar(ResponsibleTransport, MapSize * 2, $FF); // -1
    1192     fillchar(TurnsBeforeLoad, MapSize, $FF); // -1
     1191    FillChar(ResponsibleTransport, MapSize * 2, $FF); // -1
     1192    FillChar(TurnsBeforeLoad, MapSize, $FF); // -1
    11931193    ok := False;
    11941194    for uix := 0 to RO.nUn - 1 do
     
    12031203      Result := False;
    12041204      Pile.Free;
    1205       exit;
     1205      Exit;
    12061206    end;
    12071207    while Pile.Get(Loc0, Time0) do
     
    12241224    end;
    12251225
    1226     fillchar(Arrived, MapSize * 4, $55); // set NotReachedFlag for all tiles
    1227     fillchar(GroupComplete, MapSize, False);
     1226    FillChar(Arrived, MapSize * 4, $55); // set NotReachedFlag for all tiles
     1227    FillChar(GroupComplete, MapSize, False);
    12281228    BestLoadLoc := -1;
    12291229
     
    12321232    begin
    12331233      uix := uixSelectedLoad[tuix];
    1234       if MyUnit[uix].Movement = integer(MyModel[MyUnit[uix].mix].Speed) then
     1234      if MyUnit[uix].Movement = Integer(MyModel[MyUnit[uix].mix].Speed) then
    12351235      begin
    12361236        NotReachedFlag := 1 shl (2 * tuix);
     
    12461246            if (TurnsBeforeLoad[Loc1] >= 0) and (TurnsAfterLoad[Loc1] >= 0) then
    12471247            begin
    1248               i := 1;
     1248              I := 1;
    12491249              GroupCount := 0;
    12501250              for tuix1 := 0 to nSelectedLoad - 1 do
    12511251              begin
    1252                 if Arrived[loc1] and i = 0 then
     1252                if Arrived[loc1] and I = 0 then
    12531253                  Inc(GroupCount);
    1254                 i := i shl 2;
     1254                I := I shl 2;
    12551255              end;
    1256               assert(GroupCount <= TransportCapacity);
     1256              Assert(GroupCount <= TransportCapacity);
    12571257              if (GroupCount = TransportCapacity) or (GroupCount = nSelectedLoad) then
    12581258                GroupComplete[loc1] := True;
     
    12921292          begin
    12931293            Pile.Put(MyUnit[uix].Loc, $1800 - MyUnit[uix].Movement);
    1294             if MyUnit[uix].Movement = integer(MyModel[MyUnit[uix].mix].Speed) then
     1294            if MyUnit[uix].Movement = Integer(MyModel[MyUnit[uix].mix].Speed) then
    12951295              FullMovementLoc := MyUnit[uix].Loc;
    12961296            // surrounding tiles can be loaded immediately
     
    13081308            if StartLocPtr <> ArrivedEnd then
    13091309            begin
    1310               Loc0 := (integer(StartLocPtr) - integer(@Arrived)) shr 2;
     1310              Loc0 := (Integer(StartLocPtr) - Integer(@Arrived)) shr 2;
    13111311              Inc(StartLocPtr);
    13121312              Time0 := $800;
     
    13161316              if IsFirstLoc then
    13171317                ContinueUnit := ContinueUnit and not (1 shl tuix);
    1318               break;
     1318              Break;
    13191319            end;
    13201320            IsFirstLoc := False;
     
    13231323            if not GroupComplete[Loc0] and (Map[Loc0] and fTerrain <> fMountains) then
    13241324            begin // check whether group complete -- no mountains because complete flag might be faked there
    1325               i := 1;
     1325              I := 1;
    13261326              GroupCount := 0;
    13271327              for tuix1 := 0 to nSelectedLoad - 1 do
    13281328              begin
    1329                 if Arrived[Loc0] and i = 0 then
     1329                if Arrived[Loc0] and I = 0 then
    13301330                  Inc(GroupCount);
    1331                 i := i shl 2;
     1331                I := I shl 2;
    13321332              end;
    1333               assert(GroupCount <= TransportCapacity);
     1333              Assert(GroupCount <= TransportCapacity);
    13341334              if (GroupCount = TransportCapacity) or (GroupCount = nSelectedLoad) then
    13351335                GroupComplete[Loc0] := True;
     
    13531353                if (TurnsBeforeLoad[Loc1] >= 0) and (TurnsAfterLoad[Loc1] >= 0) then
    13541354                begin
    1355                   i := 1;
     1355                  I := 1;
    13561356                  GroupCount := 0;
    13571357                  for tuix1 := 0 to nSelectedLoad - 1 do
    13581358                  begin
    1359                     if Arrived[loc1] and i = 0 then
     1359                    if Arrived[loc1] and I = 0 then
    13601360                      Inc(GroupCount);
    1361                     i := i shl 2;
     1361                    I := I shl 2;
    13621362                  end;
    1363                   assert(GroupCount <= TransportCapacity);
     1363                  Assert(GroupCount <= TransportCapacity);
    13641364                  if (GroupCount = TransportCapacity) or
    13651365                    (GroupCount = nSelectedLoad) then
     
    14361436        if 1 shl (2 * tuix) and Arrived[BestLoadLoc] = 0 then
    14371437        begin
    1438           assert(uixTransportLoad[tuixSelectedLoad[tuix]] = uixSelectedLoad[tuix]);
     1438          Assert(uixTransportLoad[tuixSelectedLoad[tuix]] = uixSelectedLoad[tuix]);
    14391439          TransportPlan.uixLoad[TransportPlan.nLoad] := uixSelectedLoad[tuix];
    14401440          uixTransportLoad[tuixSelectedLoad[tuix]] :=
     
    14441444        end;
    14451445      Result := True;
    1446       exit;
     1446      Exit;
    14471447    end;
    14481448
     
    14511451    for tuix := nSelectedLoad - 1 downto 0 do
    14521452    begin
    1453       assert(uixTransportLoad[tuixSelectedLoad[tuix]] = uixSelectedLoad[tuix]);
     1453      Assert(uixTransportLoad[tuixSelectedLoad[tuix]] = uixSelectedLoad[tuix]);
    14541454      uixTransportLoad[tuixSelectedLoad[tuix]] :=
    14551455        uixTransportLoad[nTransportLoad - 1];
     
    14641464// Misc
    14651465
    1466 function TToolAI.CurrentMStrength(Domain: integer): integer;
    1467 var
    1468   i: integer;
     1466function TToolAI.CurrentMStrength(Domain: Integer): Integer;
     1467var
     1468  I: Integer;
    14691469begin
    14701470  Result := 0;
    1471   for i := 0 to nUpgrade - 1 do
    1472     with upgrade[Domain, i] do
     1471  for I := 0 to nUpgrade - 1 do
     1472    with upgrade[Domain, I] do
    14731473      if (Preq = preNone) or (Preq >= 0) and
    14741474        ((RO.Tech[Preq] >= tsApplicable) or (Preq in FutureTech) and
     
    14861486procedure SetAdvancedness;
    14871487var
    1488   ad, j, Reduction, AgeThreshold: integer;
    1489   known: array[0..nAdv - 1] of integer;
    1490 
    1491   procedure MarkPreqs(ad: integer);
     1488  ad, J, Reduction, AgeThreshold: Integer;
     1489  known: array[0..nAdv - 1] of Integer;
     1490
     1491  procedure MarkPreqs(ad: Integer);
    14921492  var
    1493     i: integer;
     1493    I: Integer;
    14941494  begin
    14951495    if known[ad] = 0 then
    14961496    begin
    14971497      known[ad] := 1;
    1498       for i := 0 to 2 do
    1499         if AdvPreq[ad, i] >= 0 then
    1500           MarkPreqs(AdvPreq[ad, i]);
     1498      for I := 0 to 2 do
     1499        if AdvPreq[ad, I] >= 0 then
     1500          MarkPreqs(AdvPreq[ad, I]);
    15011501    end;
    15021502  end;
     
    15081508    FillChar(known, SizeOf(known), 0);
    15091509    MarkPreqs(ad);
    1510     for j := 0 to nAdv - 1 do
    1511       if known[j] > 0 then
     1510    for J := 0 to nAdv - 1 do
     1511      if known[J] > 0 then
    15121512        Inc(Advancedness[ad]);
    15131513  end;
Note: See TracChangeset for help on using the changeset viewer.