Changeset 447 for trunk/Database.pas


Ignore:
Timestamp:
May 19, 2022, 10:39:34 PM (2 years ago)
Author:
chronos
Message:
  • Modified: Use first capital letter in identifiers.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Database.pas

    r442 r447  
    2626
    2727  nStartUn = 1;
    28   StartUn: array [0 .. nStartUn - 1] of integer = (0); // mix of start units
     28  StartUn: array [0 .. nStartUn - 1] of Integer = (0); // mix of start units
    2929
    3030  CityOwnTile = 13;
     
    5050  Mode: TGameMode;
    5151  GWonder: array [0 .. nWonder - 1] of TWonderInfo;
    52   ServerVersion: array [0 .. nPl - 1] of integer;
    53   ProcessClientData: array [0 .. nPl - 1] of boolean;
     52  ServerVersion: array [0 .. nPl - 1] of Integer;
     53  ProcessClientData: array [0 .. nPl - 1] of Boolean;
    5454  CL: TCmdList;
    5555{$IFDEF TEXTLOG}CmdInfo: string;
     
    5858  // map data
    5959  RealMap: array [0 .. lxmax * lymax - 1] of Cardinal;
    60   Continent: array [0 .. lxmax * lymax - 1] of integer;
     60  Continent: array [0 .. lxmax * lymax - 1] of Integer;
    6161  { continent id for each tile }
    6262  Occupant: array [0 .. lxmax * lymax - 1] of ShortInt;
     
    6565  ObserveLevel: array [0 .. lxmax * lymax - 1] of Cardinal;
    6666  { Observe Level of player p in bits 2*p and 2*p+1 }
    67   UsedByCity: array [0 .. lxmax * lymax - 1] of integer;
     67  UsedByCity: array [0 .. lxmax * lymax - 1] of Integer;
    6868  { location of exploiting city for
    6969    each tile, =-1 if not exploited }
     
    7171  // player data
    7272  RW: array [0 .. nPl - 1] of TPlayerContext; { player data }
    73   Difficulty: array [0 .. nPl - 1] of integer;
     73  Difficulty: array [0 .. nPl - 1] of Integer;
    7474  GShip: array [0 .. nPl - 1] of TShipInfo;
    7575  ResourceMask: array [0 .. nPl - 1] of Cardinal;
    76   Founded: array [0 .. nPl - 1] of integer; { number of cities founded }
    77   TerritoryCount: array [0 .. nPl] of integer;
     76  Founded: array [0 .. nPl - 1] of Integer; { number of cities founded }
     77  TerritoryCount: array [0 .. nPl] of Integer;
    7878  LastValidStat, Researched, Discovered, // number of tiles discovered
    79   GrWallContinent: array [0 .. nPl - 1] of integer;
     79  GrWallContinent: array [0 .. nPl - 1] of Integer;
    8080  RWemix: array [0 .. nPl - 1, 0 .. nPl - 1, 0 .. nmmax - 1] of SmallInt;
    8181  // [p1,p2,mix] -> index of p2's model mix in p1's enemy model list
    8282  Destroyed: array [0 .. nPl - 1, 0 .. nPl - 1, 0 .. nmmax - 1] of SmallInt;
    8383  // [p1,p2,mix] -> number of p2's units with model mix that p1 has destroyed
    84   nTech: array [0 .. nPl - 1] of integer; { number of known techs }
     84  nTech: array [0 .. nPl - 1] of Integer; { number of known techs }
    8585  // NewContact: array[0..nPl-1,0..nPl-1] of boolean;
    8686
    8787type
    88   TVicinity8Loc = array [0 .. 7] of integer;
    89   TVicinity21Loc = array [0 .. 27] of integer;
    90 
    91 procedure MaskD(var x: array of Cardinal; Count, Mask: Cardinal);
    92 procedure IntServer(Command, Player, Subject: integer; var Data);
    93 procedure CompactLists(p: integer);
    94 procedure ClearTestFlags(ClearFlags: integer);
    95 procedure SetTestFlags(p, SetFlags: integer);
     88  TVicinity8Loc = array [0 .. 7] of Integer;
     89  TVicinity21Loc = array [0 .. 27] of Integer;
     90
     91procedure MaskD(var X: array of Cardinal; Count, Mask: Cardinal);
     92procedure IntServer(Command, Player, Subject: Integer; var Data);
     93procedure CompactLists(P: Integer);
     94procedure ClearTestFlags(ClearFlags: Integer);
     95procedure SetTestFlags(P, SetFlags: Integer);
    9696
    9797// Tech Related Functions
    98 function TechBaseCost(nTech, diff: integer): integer;
    99 function TechCost(p: integer): integer;
    100 procedure CalculateModel(var m: TModel);
    101 procedure CheckSpecialModels(p, pre: integer);
    102 procedure EnableDevModel(p: integer);
    103 procedure SeeTech(p, ad: integer);
    104 procedure DiscoverTech(p, ad: integer);
    105 procedure CheckExpiration(Wonder: integer);
     98function TechBaseCost(nTech, diff: Integer): Integer;
     99function TechCost(P: Integer): Integer;
     100procedure CalculateModel(var M: TModel);
     101procedure CheckSpecialModels(P, pre: Integer);
     102procedure EnableDevModel(P: Integer);
     103procedure SeeTech(P, ad: Integer);
     104procedure DiscoverTech(P, ad: Integer);
     105procedure CheckExpiration(Wonder: Integer);
    106106
    107107// Location Navigation
    108 function dLoc(Loc, dx, dy: integer): integer;
    109 procedure dxdy(Loc0, Loc1: integer; var dx, dy: integer);
    110 function Distance(Loc0, Loc1: integer): integer;
    111 procedure V8_to_Loc(Loc0: integer; var VicinityLoc: TVicinity8Loc);
    112 procedure V21_to_Loc(Loc0: integer; var VicinityLoc: TVicinity21Loc);
     108function dLoc(Loc, dx, dy: Integer): Integer;
     109procedure dxdy(Loc0, Loc1: Integer; var dx, dy: Integer);
     110function Distance(Loc0, Loc1: Integer): Integer;
     111procedure V8_to_Loc(Loc0: Integer; var VicinityLoc: TVicinity8Loc);
     112procedure V21_to_Loc(Loc0: Integer; var VicinityLoc: TVicinity21Loc);
    113113
    114114// Game Initialization
    115115procedure InitRandomGame;
    116 procedure InitMapGame(Human: integer);
     116procedure InitMapGame(Human: Integer);
    117117procedure ReleaseGame;
    118118
    119119// Map Editor
    120 function MapGeneratorAvailable: boolean;
     120function MapGeneratorAvailable: Boolean;
    121121procedure CreateElevation;
    122 procedure CreateMap(preview: boolean);
     122procedure CreateMap(preview: Boolean);
    123123procedure InitMapEditor;
    124124procedure ReleaseMapEditor;
    125 procedure EditTile(Loc, NewTile: integer);
     125procedure EditTile(Loc, NewTile: Integer);
    126126
    127127// Map Revealing
    128 function GetTileInfo(p, cix, Loc: integer; var Info: TTileInfo): integer;
    129 procedure Strongest(Loc: integer; var uix, Strength, Bonus, Cnt: integer);
    130 function UnitSpeed(p, mix, Health: integer): integer;
    131 procedure GetUnitReport(p, uix: integer; var UnitReport: TUnitReport);
    132 procedure SearchCity(Loc: integer; var p, cix: integer);
    133 procedure TellAboutModel(p, taOwner, tamix: integer);
    134 function emixSafe(p, taOwner, tamix: integer): integer;
    135 function Discover9(Loc, p, Level: integer;
    136   TellAllied, EnableContact: boolean): boolean;
    137 function Discover21(Loc, p, AdjacentLevel: integer;
    138   TellAllied, EnableContact: boolean): boolean;
    139 procedure DiscoverAll(p, Level: integer);
    140 procedure DiscoverViewAreas(p: integer);
    141 function GetUnitStack(p, Loc: integer): integer;
    142 procedure UpdateUnitMap(Loc: integer; CityChange: boolean = false);
    143 procedure RecalcV8ZoC(p, Loc: integer);
    144 procedure RecalcMapZoC(p: integer);
    145 procedure RecalcPeaceMap(p: integer);
     128function GetTileInfo(P, cix, Loc: Integer; var Info: TTileInfo): Integer;
     129procedure Strongest(Loc: Integer; var uix, Strength, Bonus, Cnt: Integer);
     130function UnitSpeed(P, mix, Health: Integer): Integer;
     131procedure GetUnitReport(P, uix: Integer; var UnitReport: TUnitReport);
     132procedure SearchCity(Loc: Integer; var P, cix: Integer);
     133procedure TellAboutModel(P, taOwner, tamix: Integer);
     134function emixSafe(P, taOwner, tamix: Integer): Integer;
     135function Discover9(Loc, P, Level: Integer;
     136  TellAllied, EnableContact: Boolean): Boolean;
     137function Discover21(Loc, P, AdjacentLevel: Integer;
     138  TellAllied, EnableContact: Boolean): Boolean;
     139procedure DiscoverAll(P, Level: Integer);
     140procedure DiscoverViewAreas(P: Integer);
     141function GetUnitStack(P, Loc: Integer): Integer;
     142procedure UpdateUnitMap(Loc: Integer; CityChange: Boolean = False);
     143procedure RecalcV8ZoC(P, Loc: Integer);
     144procedure RecalcMapZoC(P: Integer);
     145procedure RecalcPeaceMap(P: Integer);
    146146
    147147// Territory Calculation
    148 procedure CheckBorders(OriginLoc: integer; PlayerLosingCity: integer = -1);
    149 procedure LogCheckBorders(p, cix: integer; PlayerLosingCity: integer = -1);
     148procedure CheckBorders(OriginLoc: Integer; PlayerLosingCity: Integer = -1);
     149procedure LogCheckBorders(P, cix: Integer; PlayerLosingCity: Integer = -1);
    150150
    151151// Map Processing
    152 procedure CreateUnit(p, mix: integer);
    153 procedure FreeUnit(p, uix: integer);
    154 procedure PlaceUnit(p, uix: integer);
    155 procedure RemoveUnit(p, uix: integer; Enemy: integer = -1);
    156 procedure RemoveUnit_UpdateMap(p, uix: integer);
    157 procedure RemoveAllUnits(p, Loc: integer; Enemy: integer = -1);
    158 procedure RemoveDomainUnits(d, p, Loc: integer);
    159 procedure FoundCity(p, FoundLoc: integer);
    160 procedure DestroyCity(p, cix: integer; SaveUnits: boolean);
    161 procedure ChangeCityOwner(pOld, cixOld, pNew: integer);
    162 procedure CompleteJob(p, Loc, Job: integer);
     152procedure CreateUnit(P, mix: Integer);
     153procedure FreeUnit(P, uix: Integer);
     154procedure PlaceUnit(P, uix: Integer);
     155procedure RemoveUnit(P, uix: Integer; Enemy: Integer = -1);
     156procedure RemoveUnit_UpdateMap(P, uix: Integer);
     157procedure RemoveAllUnits(P, Loc: Integer; Enemy: Integer = -1);
     158procedure RemoveDomainUnits(D, P, Loc: Integer);
     159procedure FoundCity(P, FoundLoc: Integer);
     160procedure DestroyCity(P, cix: Integer; SaveUnits: Boolean);
     161procedure ChangeCityOwner(pOld, cixOld, pNew: Integer);
     162procedure CompleteJob(P, Loc, Job: Integer);
    163163
    164164// Diplomacy
    165 procedure IntroduceEnemy(p1, p2: integer);
    166 procedure GiveCivilReport(p, pAbout: integer);
    167 procedure GiveMilReport(p, pAbout: integer);
    168 procedure ShowPrice(pSender, pTarget, Price: integer);
    169 function PayPrice(pSender, pTarget, Price: integer; execute: boolean): boolean;
    170 procedure CancelTreaty(p, pWith: integer; DecreaseCredibility: boolean = true);
    171 function DoSpyMission(p, pCity, cix, Mission: integer): Cardinal;
     165procedure IntroduceEnemy(p1, p2: Integer);
     166procedure GiveCivilReport(P, pAbout: Integer);
     167procedure GiveMilReport(P, pAbout: Integer);
     168procedure ShowPrice(pSender, pTarget, Price: Integer);
     169function PayPrice(pSender, pTarget, Price: Integer; execute: Boolean): Boolean;
     170procedure CancelTreaty(P, pWith: Integer; DecreaseCredibility: Boolean = True);
     171function DoSpyMission(P, pCity, cix, Mission: Integer): Cardinal;
    172172
    173173
     
    180180
    181181var
    182   UnBuilt: array [0 .. nPl - 1] of integer; { number of units built }
    183 
    184 procedure MaskD(var x: array of Cardinal; Count, Mask: Cardinal);
     182  UnBuilt: array [0 .. nPl - 1] of Integer; { number of units built }
     183
     184procedure MaskD(var X: array of Cardinal; Count, Mask: Cardinal);
    185185var
    186186  I: Integer;
    187187begin
    188188  for I := 0 to Count - 1 do
    189     x[I] := x[I] and Mask;
    190 end;
    191 
    192 procedure CompactLists(p: integer);
    193 var
    194   uix, uix1, cix: integer;
    195 {$IFOPT O-}V21: integer;
     189    X[I] := X[I] and Mask;
     190end;
     191
     192procedure CompactLists(P: Integer);
     193var
     194  uix, uix1, cix: Integer;
     195{$IFOPT O-}V21: Integer;
    196196  Radius: TVicinity21Loc; {$ENDIF}
    197197begin
    198   with RW[p] do
     198  with RW[P] do
    199199  begin
    200200    // compact unit list
     
    203203      if Un[uix].Loc < 0 then
    204204      begin
    205         dec(nUn);
     205        Dec(nUn);
    206206        Un[uix] := Un[nUn]; { replace removed unit by last }
    207207        if (Un[uix].TroopLoad > 0) or (Un[uix].AirLoad > 0) then
     
    212212      end
    213213      else
    214         inc(uix);
     214        Inc(uix);
    215215
    216216    // compact city list
     
    219219      if City[cix].Loc < 0 then
    220220      begin
    221         dec(nCity);
     221        Dec(nCity);
    222222        City[cix] := City[nCity]; { replace city by last }
    223223        for uix1 := 0 to nUn - 1 do
     
    227227      end
    228228      else
    229         inc(cix);
     229        Inc(cix);
    230230
    231231    // compact enemy city list
     
    234234      if EnemyCity[cix].Loc < 0 then
    235235      begin
    236         dec(nEnemyCity);
     236        Dec(nEnemyCity);
    237237        EnemyCity[cix] := EnemyCity[nEnemyCity]; { replace city by last }
    238238      end
    239239      else
    240         inc(cix);
     240        Inc(cix);
    241241
    242242{$IFOPT O-}
     
    247247        for V21 := 1 to 26 do
    248248          if Tiles and (1 shl V21) <> 0 then
    249             assert(UsedByCity[Radius[V21]] = Loc);
     249            Assert(UsedByCity[Radius[V21]] = Loc);
    250250      end;
    251251{$ENDIF}
     
    257257  ____________________________________________________________________
    258258}
    259 function TechBaseCost(nTech, diff: integer): integer;
    260 var
    261   c0: single;
     259function TechBaseCost(nTech, diff: Integer): Integer;
     260var
     261  c0: Single;
    262262begin
    263263  c0 := TechFormula_M[diff] * (nTech + 4) *
    264264    exp((nTech + 4) / TechFormula_D[diff]);
    265265  if c0 >= $10000000 then
    266     result := $10000000
     266    Result := $10000000
    267267  else
    268     result := trunc(c0);
    269 end;
    270 
    271 function TechCost(p: integer): integer;
    272 begin
    273   with RW[p] do
    274   begin
    275     result := TechBaseCost(nTech[p], Difficulty[p]);
     268    Result := trunc(c0);
     269end;
     270
     271function TechCost(P: Integer): Integer;
     272begin
     273  with RW[P] do
     274  begin
     275    Result := TechBaseCost(nTech[P], Difficulty[P]);
    276276    if ResearchTech >= 0 then
    277277      if (ResearchTech = adMilitary) or (Tech[ResearchTech] = tsSeen) then
    278         result := result shr 1
     278        Result := Result shr 1
    279279      else if ResearchTech in FutureTech then
    280280        if Government = gFuture then
    281           result := result * 2
     281          Result := Result * 2
    282282        else
    283           result := result * 4;
    284   end;
    285 end;
    286 
    287 procedure SetModelFlags(var m: TModel);
    288 begin
    289   m.Flags := 0;
    290   if (m.Domain = dGround) and (m.Kind <> mkDiplomat) then
    291     m.Flags := m.Flags or mdZOC;
    292   if (m.Kind = mkDiplomat) or (m.Attack + m.Cap[mcBombs] = 0) then
    293     m.Flags := m.Flags or mdCivil;
    294   if (m.Cap[mcOver] > 0) or (m.Domain = dSea) and (m.Weight >= 6) then
    295     m.Flags := m.Flags or mdDoubleSupport;
    296 end;
    297 
    298 procedure CalculateModel(var m: TModel);
     283          Result := Result * 4;
     284  end;
     285end;
     286
     287procedure SetModelFlags(var M: TModel);
     288begin
     289  M.Flags := 0;
     290  if (M.Domain = dGround) and (M.Kind <> mkDiplomat) then
     291    M.Flags := M.Flags or mdZOC;
     292  if (M.Kind = mkDiplomat) or (M.Attack + M.Cap[mcBombs] = 0) then
     293    M.Flags := M.Flags or mdCivil;
     294  if (M.Cap[mcOver] > 0) or (M.Domain = dSea) and (M.Weight >= 6) then
     295    M.Flags := M.Flags or mdDoubleSupport;
     296end;
     297
     298procedure CalculateModel(var M: TModel);
    299299{ calculate attack, defense, cost... of a model by features }
    300300var
    301   i: integer;
    302 begin
    303   with m do
     301  I: Integer;
     302begin
     303  with M do
    304304  begin
    305305    Attack := (Cap[mcOffense] + Cap[mcOver]) * MStrength;
     
    312312          Speed := 350 + 200 * Cap[mcNP] + 200 * Cap[mcTurbines];
    313313          if Cap[mcNP] = 0 then
    314             inc(Speed, 100 * Cap[mcSE]);
     314            Inc(Speed, 100 * Cap[mcSE]);
    315315        end;
    316316      dAir:
     
    318318    end;
    319319    Cost := 0;
    320     for i := 0 to nFeature - 1 do
    321       if 1 shl Domain and Feature[i].Domains <> 0 then
    322         inc(Cost, Cap[i] * Feature[i].Cost);
     320    for I := 0 to nFeature - 1 do
     321      if 1 shl Domain and Feature[I].Domains <> 0 then
     322        Inc(Cost, Cap[I] * Feature[I].Cost);
    323323    Cost := Cost * MCost;
    324324    Weight := 0;
    325     for i := 0 to nFeature - 1 do
    326       if 1 shl Domain and Feature[i].Domains <> 0 then
    327         if (Domain = dGround) and (i = mcDefense) then
    328           inc(Weight, Cap[i] * 2)
     325    for I := 0 to nFeature - 1 do
     326      if 1 shl Domain and Feature[I].Domains <> 0 then
     327        if (Domain = dGround) and (I = mcDefense) then
     328          Inc(Weight, Cap[I] * 2)
    329329        else
    330           inc(Weight, Cap[i] * Feature[i].Weight);
    331   end;
    332   SetModelFlags(m);
    333 end;
    334 
    335 procedure CheckSpecialModels(p, pre: integer);
    336 var
    337   i, mix1: integer;
    338   HasAlready: boolean;
    339 begin
    340   for i := 0 to nSpecialModel -
     330          Inc(Weight, Cap[I] * Feature[I].Weight);
     331  end;
     332  SetModelFlags(M);
     333end;
     334
     335procedure CheckSpecialModels(P, pre: Integer);
     336var
     337  I, mix1: Integer;
     338  HasAlready: Boolean;
     339begin
     340  for I := 0 to nSpecialModel -
    341341    1 do { check whether new special model available }
    342     if (SpecialModelPreq[i] = pre) and (RW[p].nModel < nmmax) then
    343     begin
    344       HasAlready := false;
    345       for mix1 := 0 to RW[p].nModel - 1 do
    346         if (RW[p].Model[mix1].Kind = SpecialModel[i].Kind) and
    347           (RW[p].Model[mix1].Attack = SpecialModel[i].Attack) and
    348           (RW[p].Model[mix1].Speed = SpecialModel[i].Speed) then
    349           HasAlready := true;
     342    if (SpecialModelPreq[I] = pre) and (RW[P].nModel < nmmax) then
     343    begin
     344      HasAlready := False;
     345      for mix1 := 0 to RW[P].nModel - 1 do
     346        if (RW[P].Model[mix1].Kind = SpecialModel[I].Kind) and
     347          (RW[P].Model[mix1].Attack = SpecialModel[I].Attack) and
     348          (RW[P].Model[mix1].Speed = SpecialModel[I].Speed) then
     349          HasAlready := True;
    350350      if not HasAlready then
    351351      begin
    352         RW[p].Model[RW[p].nModel] := SpecialModel[i];
    353         SetModelFlags(RW[p].Model[RW[p].nModel]);
    354         with RW[p].Model[RW[p].nModel] do
     352        RW[P].Model[RW[P].nModel] := SpecialModel[I];
     353        SetModelFlags(RW[P].Model[RW[P].nModel]);
     354        with RW[P].Model[RW[P].nModel] do
    355355        begin
    356356          Status := 0;
     
    359359          Built := 0;
    360360          Lost := 0;
    361           ID := p shl 12 + RW[p].nModel;
    362           if (Kind = mkSpecial_Boat) and (ServerVersion[p] < $000EF0) then
     361          ID := P shl 12 + RW[P].nModel;
     362          if (Kind = mkSpecial_Boat) and (ServerVersion[P] < $000EF0) then
    363363            Speed := 350; // old longboat
    364364        end;
    365         inc(RW[p].nModel);
    366       end;
    367     end;
    368 end;
    369 
    370 procedure EnableDevModel(p: integer);
    371 begin
    372   with RW[p] do
     365        Inc(RW[P].nModel);
     366      end;
     367    end;
     368end;
     369
     370procedure EnableDevModel(P: Integer);
     371begin
     372  with RW[P] do
    373373    if nModel < nmmax then
    374374    begin
     
    381381        Built := 0;
    382382        Lost := 0;
    383         ID := p shl 12 + nModel;
    384       end;
    385       inc(nModel);
    386       inc(Researched[p]);
    387     end;
    388 end;
    389 
    390 procedure SeeTech(p, ad: integer);
    391 begin
    392 {$IFDEF TEXTLOG}CmdInfo := CmdInfo + Format(' P%d:A%d', [p, ad]); {$ENDIF}
    393   RW[p].Tech[ad] := tsSeen;
     383        ID := P shl 12 + nModel;
     384      end;
     385      Inc(nModel);
     386      Inc(Researched[P]);
     387    end;
     388end;
     389
     390procedure SeeTech(P, ad: Integer);
     391begin
     392{$IFDEF TEXTLOG}CmdInfo := CmdInfo + Format(' P%d:A%d', [P, ad]); {$ENDIF}
     393  RW[P].Tech[ad] := tsSeen;
    394394  // inc(nTech[p]);
    395   inc(Researched[p]);
     395  Inc(Researched[P]);
    396396end;
    397397
    398398procedure FreeSlaves;
    399399var
    400   p1, uix: integer;
     400  p1, uix: Integer;
    401401begin
    402402  for p1 := 0 to nPl - 1 do
     
    407407end;
    408408
    409 procedure DiscoverTech(p, ad: integer);
    410 
    411   procedure TellAboutKeyTech(p, Source: integer);
     409procedure DiscoverTech(P, ad: Integer);
     410
     411  procedure TellAboutKeyTech(P, Source: Integer);
    412412  var
    413     i, p1: integer;
    414   begin
    415     for i := 1 to 3 do
    416       if ad = AgePreq[i] then
     413    I, p1: Integer;
     414  begin
     415    for I := 1 to 3 do
     416      if ad = AgePreq[I] then
    417417        for p1 := 0 to nPl - 1 do
    418           if (p1 <> p) and ((GAlive or GWatching) and (1 shl p1) <> 0) then
    419             RW[p1].EnemyReport[p].Tech[ad] := Source;
    420   end;
    421 
    422 var
    423   i: integer;
     418          if (p1 <> P) and ((GAlive or GWatching) and (1 shl p1) <> 0) then
     419            RW[p1].EnemyReport[P].Tech[ad] := Source;
     420  end;
     421
     422var
     423  I: Integer;
    424424begin
    425425  if ad in FutureTech then
    426426  begin
    427     if RW[p].Tech[ad] < tsApplicable then
    428       RW[p].Tech[ad] := 1
     427    if RW[P].Tech[ad] < tsApplicable then
     428      RW[P].Tech[ad] := 1
    429429    else
    430       inc(RW[p].Tech[ad]);
     430      Inc(RW[P].Tech[ad]);
    431431    if ad <> futResearchTechnology then
    432       inc(nTech[p], 2);
    433     inc(Researched[p], 8);
    434     exit;
    435   end;
    436 
    437   if RW[p].Tech[ad] = tsSeen then
    438   begin
    439     inc(nTech[p]);
    440     inc(Researched[p]);
     432      Inc(nTech[P], 2);
     433    Inc(Researched[P], 8);
     434    Exit;
     435  end;
     436
     437  if RW[P].Tech[ad] = tsSeen then
     438  begin
     439    Inc(nTech[P]);
     440    Inc(Researched[P]);
    441441  end
    442442  else
    443443  begin
    444     inc(nTech[p], 2);
    445     inc(Researched[p], 2);
    446   end;
    447   RW[p].Tech[ad] := tsResearched;
    448   TellAboutKeyTech(p, tsResearched);
    449   CheckSpecialModels(p, ad);
     444    Inc(nTech[P], 2);
     445    Inc(Researched[P], 2);
     446  end;
     447  RW[P].Tech[ad] := tsResearched;
     448  TellAboutKeyTech(P, tsResearched);
     449  CheckSpecialModels(P, ad);
    450450  if ad = adScience then
    451     ResourceMask[p] := ResourceMask[p] or fSpecial2;
     451    ResourceMask[P] := ResourceMask[P] or fSpecial2;
    452452  if ad = adMassProduction then
    453     ResourceMask[p] := ResourceMask[p] or fModern;
    454 
    455   for i := 0 to nWonder - 1 do { check whether wonders expired }
    456     if (GWonder[i].EffectiveOwner <> GWonder[woEiffel].EffectiveOwner) and
    457       (Imp[i].Expiration = ad) then
    458     begin
    459       GWonder[i].EffectiveOwner := -1;
    460       if i = woPyramids then
     453    ResourceMask[P] := ResourceMask[P] or fModern;
     454
     455  for I := 0 to nWonder - 1 do { check whether wonders expired }
     456    if (GWonder[I].EffectiveOwner <> GWonder[woEiffel].EffectiveOwner) and
     457      (Imp[I].Expiration = ad) then
     458    begin
     459      GWonder[I].EffectiveOwner := -1;
     460      if I = woPyramids then
    461461        FreeSlaves;
    462462    end;
    463463end;
    464464
    465 procedure CheckExpiration(Wonder: integer);
     465procedure CheckExpiration(Wonder: Integer);
    466466// GWonder[Wonder].EffectiveOwner must be set before!
    467467var
    468   p: integer;
     468  P: Integer;
    469469begin
    470470  if (Imp[Wonder].Expiration >= 0) and
    471471    (GWonder[woEiffel].EffectiveOwner <> GWonder[Wonder].EffectiveOwner) then
    472     for p := 0 to nPl - 1 do // check if already expired
    473       if (1 shl p and GAlive <> 0) and
    474         (RW[p].Tech[Imp[Wonder].Expiration] >= tsApplicable) then
     472    for P := 0 to nPl - 1 do // check if already expired
     473      if (1 shl P and GAlive <> 0) and
     474        (RW[P].Tech[Imp[Wonder].Expiration] >= tsApplicable) then
    475475      begin
    476476        GWonder[Wonder].EffectiveOwner := -1;
     
    484484  ____________________________________________________________________
    485485}
    486 function dLoc(Loc, dx, dy: integer): integer;
     486function dLoc(Loc, dx, dy: Integer): Integer;
    487487{ relative location, dx in hor and dy in ver direction from Loc }
    488488var
    489   y0: integer;
     489  y0: Integer;
    490490begin
    491491  if not (Loc >= 0) and (Loc < MapSize) and (dx + lx >= 0) then
    492492    raise Exception.Create('Relative location error');
    493   assert((Loc >= 0) and (Loc < MapSize) and (dx + lx >= 0));
     493  Assert((Loc >= 0) and (Loc < MapSize) and (dx + lx >= 0));
    494494  y0 := Loc div lx;
    495   result := (Loc + (dx + y0 and 1 + lx + lx) shr 1) mod lx + lx * (y0 + dy);
    496   if (result < 0) or (result >= MapSize) then
    497     result := -1;
    498 end;
    499 
    500 procedure dxdy(Loc0, Loc1: integer; var dx, dy: integer);
     495  Result := (Loc + (dx + y0 and 1 + lx + lx) shr 1) mod lx + lx * (y0 + dy);
     496  if (Result < 0) or (Result >= MapSize) then
     497    Result := -1;
     498end;
     499
     500procedure dxdy(Loc0, Loc1: Integer; var dx, dy: Integer);
    501501begin
    502502  dx := ((Loc1 mod lx * 2 + Loc1 div lx and 1) -
     
    505505end;
    506506
    507 function Distance(Loc0, Loc1: integer): integer;
    508 var
    509   dx, dy: integer;
     507function Distance(Loc0, Loc1: Integer): Integer;
     508var
     509  dx, dy: Integer;
    510510begin
    511511  dxdy(Loc0, Loc1, dx, dy);
    512512  dx := abs(dx);
    513513  dy := abs(dy);
    514   result := dx + dy + abs(dx - dy) shr 1;
    515 end;
    516 
    517 procedure V8_to_Loc(Loc0: integer; var VicinityLoc: TVicinity8Loc);
    518 var
    519   x0, y0, lx0: integer;
     514  Result := dx + dy + abs(dx - dy) shr 1;
     515end;
     516
     517procedure V8_to_Loc(Loc0: Integer; var VicinityLoc: TVicinity8Loc);
     518var
     519  x0, y0, lx0: Integer;
    520520begin
    521521  lx0 := lx; // put in register!
     
    527527  VicinityLoc[5] := Loc0 - lx0 * 2;
    528528  VicinityLoc[7] := Loc0 + 1;
    529   inc(Loc0, y0);
     529  Inc(Loc0, y0);
    530530  VicinityLoc[0] := Loc0 + lx0;
    531531  VicinityLoc[2] := Loc0 + lx0 - 1;
     
    538538    if x0 = 0 then
    539539    begin
    540       inc(VicinityLoc[3], lx0);
     540      Inc(VicinityLoc[3], lx0);
    541541      if y0 = 0 then
    542542      begin
    543         inc(VicinityLoc[2], lx0);
    544         inc(VicinityLoc[4], lx0);
     543        Inc(VicinityLoc[2], lx0);
     544        Inc(VicinityLoc[4], lx0);
    545545      end;
    546546    end;
     
    548548  else
    549549  begin
    550     dec(VicinityLoc[7], lx0);
     550    Dec(VicinityLoc[7], lx0);
    551551    if y0 = 1 then
    552552    begin
    553       dec(VicinityLoc[0], lx0);
    554       dec(VicinityLoc[6], lx0);
    555     end;
    556   end;
    557 end;
    558 
    559 procedure V21_to_Loc(Loc0: integer; var VicinityLoc: TVicinity21Loc);
    560 var
    561   dx, dy, bit, y0, xComp, yComp, xComp0, xCompSwitch: integer;
    562   dst: ^integer;
     553      Dec(VicinityLoc[0], lx0);
     554      Dec(VicinityLoc[6], lx0);
     555    end;
     556  end;
     557end;
     558
     559procedure V21_to_Loc(Loc0: Integer; var VicinityLoc: TVicinity21Loc);
     560var
     561  dx, dy, bit, y0, xComp, yComp, xComp0, xCompSwitch: Integer;
     562  dst: ^Integer;
    563563begin
    564564  y0 := Loc0 div lx;
     
    566566  xCompSwitch := xComp0 - 1 + y0 and 1;
    567567  if xComp0 < 0 then
    568     inc(xComp0, lx);
     568    Inc(xComp0, lx);
    569569  if xCompSwitch < 0 then
    570     inc(xCompSwitch, lx);
     570    Inc(xCompSwitch, lx);
    571571  xCompSwitch := xCompSwitch xor xComp0;
    572572  yComp := lx * (y0 - 3);
     
    583583      else
    584584        dst^ := -1;
    585       inc(xComp);
     585      Inc(xComp);
    586586      if xComp >= lx then
    587         dec(xComp, lx);
    588       inc(dst);
     587        Dec(xComp, lx);
     588      Inc(dst);
    589589      bit := bit shl 1;
    590590    end;
    591     inc(yComp, lx);
     591    Inc(yComp, lx);
    592592  end;
    593593end;
     
    598598}
    599599var
    600   primitive: integer;
    601   StartLoc, StartLoc2: array [0 .. nPl - 1] of integer; { starting coordinates }
     600  primitive: Integer;
     601  StartLoc, StartLoc2: array [0 .. nPl - 1] of Integer; { starting coordinates }
    602602  Elevation: array [0 .. lxmax * lymax - 1] of Byte; { map elevation }
    603   ElCount: array [Byte] of integer; { count of elevation occurance }
     603  ElCount: array [Byte] of Integer; { count of elevation occurance }
    604604
    605605procedure CalculatePrimitive;
    606606var
    607   i, j: integer;
     607  I, J: Integer;
    608608begin
    609609  primitive := 1;
    610   i := 2;
    611   while i * i <= MapSize + 1 do // test whether prime
    612   begin
    613     if (MapSize + 1) mod i = 0 then
     610  I := 2;
     611  while I * I <= MapSize + 1 do // test whether prime
     612  begin
     613    if (MapSize + 1) mod I = 0 then
    614614      primitive := 0;
    615     inc(i);
     615    Inc(I);
    616616  end;
    617617
    618618  if primitive > 0 then
    619619    repeat
    620       inc(primitive);
    621       i := 1;
    622       j := 0;
     620      Inc(primitive);
     621      I := 1;
     622      J := 0;
    623623      repeat
    624         inc(j);
    625         i := i * primitive mod (MapSize + 1);
    626       until (i = 1) or (j = MapSize + 1);
    627     until j = MapSize;
    628 end;
    629 
    630 function MapGeneratorAvailable: boolean;
    631 begin
    632   result := (primitive > 0) and (lx >= 20) and (ly >= 40);
     624        Inc(J);
     625        I := I * primitive mod (MapSize + 1);
     626      until (I = 1) or (J = MapSize + 1);
     627    until J = MapSize;
     628end;
     629
     630function MapGeneratorAvailable: Boolean;
     631begin
     632  Result := (primitive > 0) and (lx >= 20) and (ly >= 40);
    633633end;
    634634
    635635procedure CreateElevation;
    636636const
    637   d = 64;
     637  D = 64;
    638638  Smooth = 0.049; { causes low amplitude of short waves }
    639639  Detail = 0.095; { causes short period of short waves }
     
    642642
    643643var
    644   sa, ca, f1, f2: array [1 .. d] of single;
    645   imerge, x, y: integer;
    646   v, maxv: single;
    647 
    648   function Value(x, y: integer): single; { elevation formula }
     644  sa, ca, f1, f2: array [1 .. D] of Single;
     645  imerge, X, Y: Integer;
     646  V, maxv: Single;
     647
     648  function Value(X, Y: Integer): Single; { elevation formula }
    649649  var
    650     i: integer;
    651   begin
    652     result := 0;
    653     for i := 1 to d do
    654       result := result + sin(f1[i] * ((x * 2 + y and 1) * sa[i] + y * 1.5 *
    655         ca[i])) * f2[i];
     650    I: Integer;
     651  begin
     652    Result := 0;
     653    for I := 1 to D do
     654      Result := Result + sin(f1[I] * ((X * 2 + Y and 1) * sa[I] + Y * 1.5 *
     655        ca[I])) * f2[I];
    656656    { x values effectively multiplied with 2 to get 2 horizantal periods
    657657      of the prime waves }
     
    659659
    660660begin
    661   for x := 1 to d do { prepare formula parameters }
    662   begin
    663 {$IFNDEF SCR} if x = 1 then
    664       v := pi / 2 { first wave goes horizontal }
    665     else {$ENDIF} v := DelphiRandom * 2 * pi;
    666     sa[x] := sin(v) / lx;
    667     ca[x] := cos(v) / ly;
    668     f1[x] := 2 * pi * exp(Detail * (x - 1));
    669     f2[x] := exp(-x * Smooth);
     661  for X := 1 to D do { prepare formula parameters }
     662  begin
     663{$IFNDEF SCR} if X = 1 then
     664      V := pi / 2 { first wave goes horizontal }
     665    else {$ENDIF} V := DelphiRandom * 2 * pi;
     666    sa[X] := sin(V) / lx;
     667    ca[X] := cos(V) / ly;
     668    f1[X] := 2 * pi * exp(Detail * (X - 1));
     669    f2[X] := exp(-X * Smooth);
    670670  end;
    671671
     
    673673  FillChar(ElCount, SizeOf(ElCount), 0);
    674674  maxv := 0;
    675   for x := 0 to lx - 1 do
    676     for y := 0 to ly - 1 do
    677     begin
    678       v := Value(x, y);
    679       if x * 2 < imerge then
    680         v := (x * 2 * v + (imerge - x * 2) * Value(x + lx, y)) / imerge;
    681       v := v - sqr(sqr(2 * y / ly - 1)); { soft cut at poles }
    682       if v > maxv then
    683         maxv := v;
    684 
    685       if v < -4 then
    686         Elevation[x + lx * y] := 0
    687       else if v > 8.75 then
    688         Elevation[x + lx * y] := 255
     675  for X := 0 to lx - 1 do
     676    for Y := 0 to ly - 1 do
     677    begin
     678      V := Value(X, Y);
     679      if X * 2 < imerge then
     680        V := (X * 2 * V + (imerge - X * 2) * Value(X + lx, Y)) / imerge;
     681      V := V - sqr(sqr(2 * Y / ly - 1)); { soft cut at poles }
     682      if V > maxv then
     683        maxv := V;
     684
     685      if V < -4 then
     686        Elevation[X + lx * Y] := 0
     687      else if V > 8.75 then
     688        Elevation[X + lx * Y] := 255
    689689      else
    690         Elevation[x + lx * y] := Round((v + 4) * 20);
    691       inc(ElCount[Elevation[x + lx * y]]);
     690        Elevation[X + lx * Y] := Round((V + 4) * 20);
     691      Inc(ElCount[Elevation[X + lx * Y]]);
    692692    end;
    693693end;
     
    695695procedure FindContinents;
    696696
    697   procedure ReplaceCont(a, b, Stop: integer);
     697  procedure ReplaceCont(A, B, Stop: Integer);
    698698  { replace continent name a by b }
    699699  // make sure always continent[loc]<=loc
    700700  var
    701     i: integer;
    702   begin
    703     if a < b then
    704     begin
    705       i := a;
    706       a := b;
    707       b := i
    708     end;
    709     if a > b then
    710       for i := a to Stop do
    711         if Continent[i] = a then
    712           Continent[i] := b;
    713   end;
    714 
    715 var
    716   x, y, Loc, Wrong: integer;
    717 begin
    718   for y := 1 to ly - 2 do
    719     for x := 0 to lx - 1 do
    720     begin
    721       Loc := x + lx * y;
     701    I: Integer;
     702  begin
     703    if A < B then
     704    begin
     705      I := A;
     706      A := B;
     707      B := I
     708    end;
     709    if A > B then
     710      for I := A to Stop do
     711        if Continent[I] = A then
     712          Continent[I] := B;
     713  end;
     714
     715var
     716  X, Y, Loc, Wrong: Integer;
     717begin
     718  for Y := 1 to ly - 2 do
     719    for X := 0 to lx - 1 do
     720    begin
     721      Loc := X + lx * Y;
    722722      Continent[Loc] := -1;
    723723      if RealMap[Loc] and fTerrain >= fGrass then
    724724      begin
    725         if (y - 2 >= 1) and (RealMap[Loc - 2 * lx] and fTerrain >= fGrass) then
     725        if (Y - 2 >= 1) and (RealMap[Loc - 2 * lx] and fTerrain >= fGrass) then
    726726          Continent[Loc] := Continent[Loc - 2 * lx];
    727         if (x - 1 + y and 1 >= 0) and (y - 1 >= 1) and
    728           (RealMap[Loc - 1 + y and 1 - lx] and fTerrain >= fGrass) then
    729           Continent[Loc] := Continent[Loc - 1 + y and 1 - lx];
    730         if (x + y and 1 < lx) and (y - 1 >= 1) and
    731           (RealMap[Loc + y and 1 - lx] and fTerrain >= fGrass) then
    732           Continent[Loc] := Continent[Loc + y and 1 - lx];
    733         if (x - 1 >= 0) and (RealMap[Loc - 1] and fTerrain >= fGrass) then
     727        if (X - 1 + Y and 1 >= 0) and (Y - 1 >= 1) and
     728          (RealMap[Loc - 1 + Y and 1 - lx] and fTerrain >= fGrass) then
     729          Continent[Loc] := Continent[Loc - 1 + Y and 1 - lx];
     730        if (X + Y and 1 < lx) and (Y - 1 >= 1) and
     731          (RealMap[Loc + Y and 1 - lx] and fTerrain >= fGrass) then
     732          Continent[Loc] := Continent[Loc + Y and 1 - lx];
     733        if (X - 1 >= 0) and (RealMap[Loc - 1] and fTerrain >= fGrass) then
    734734          if Continent[Loc] = -1 then
    735735            Continent[Loc] := Continent[Loc - 1]
     
    742742
    743743  { connect continents due to round earth }
    744   for y := 1 to ly - 2 do
    745     if RealMap[lx * y] and fTerrain >= fGrass then
     744  for Y := 1 to ly - 2 do
     745    if RealMap[lx * Y] and fTerrain >= fGrass then
    746746    begin
    747747      Wrong := -1;
    748       if RealMap[lx - 1 + lx * y] and fTerrain >= fGrass then
    749         Wrong := Continent[lx - 1 + lx * y];
    750       if (y and 1 = 0) and (y - 1 >= 1) and
    751         (RealMap[lx - 1 + lx * (y - 1)] and fTerrain >= fGrass) then
    752         Wrong := Continent[lx - 1 + lx * (y - 1)];
    753       if (y and 1 = 0) and (y + 1 < ly - 1) and
    754         (RealMap[lx - 1 + lx * (y + 1)] and fTerrain >= fGrass) then
    755         Wrong := Continent[lx - 1 + lx * (y + 1)];
     748      if RealMap[lx - 1 + lx * Y] and fTerrain >= fGrass then
     749        Wrong := Continent[lx - 1 + lx * Y];
     750      if (Y and 1 = 0) and (Y - 1 >= 1) and
     751        (RealMap[lx - 1 + lx * (Y - 1)] and fTerrain >= fGrass) then
     752        Wrong := Continent[lx - 1 + lx * (Y - 1)];
     753      if (Y and 1 = 0) and (Y + 1 < ly - 1) and
     754        (RealMap[lx - 1 + lx * (Y + 1)] and fTerrain >= fGrass) then
     755        Wrong := Continent[lx - 1 + lx * (Y + 1)];
    756756      if Wrong >= 0 then
    757         ReplaceCont(Wrong, Continent[lx * y], MapSize - 1);
     757        ReplaceCont(Wrong, Continent[lx * Y], MapSize - 1);
    758758    end;
    759759end;
     
    763763// must be done after FindContinents
    764764var
    765   i, j, Cnt, x, y, dx, dy, Loc0, Loc1, xworst, yworst, totalrare, RareMaxWater,
    766     RareType, iBest, jbest, MinDist, xBlock, yBlock, V8: integer;
    767   AreaCount, RareByArea, RareAdjacent: array [0 .. 7, 0 .. 4] of integer;
    768   RareLoc: array [0 .. 11] of integer;
    769   Dist: array [0 .. 11, 0 .. 11] of integer;
     765  I, J, Cnt, X, Y, dx, dy, Loc0, Loc1, xworst, yworst, totalrare, RareMaxWater,
     766    RareType, iBest, jbest, MinDist, xBlock, yBlock, V8: Integer;
     767  AreaCount, RareByArea, RareAdjacent: array [0 .. 7, 0 .. 4] of Integer;
     768  RareLoc: array [0 .. 11] of Integer;
     769  Dist: array [0 .. 11, 0 .. 11] of Integer;
    770770  Adjacent: TVicinity8Loc;
    771771begin
     
    773773  repeat
    774774    FillChar(AreaCount, SizeOf(AreaCount), 0);
    775     for y := 1 to ly - 2 do
    776     begin
    777       yBlock := y * 5 div ly;
    778       if yBlock = (y + 1) * 5 div ly then
    779         for x := 0 to lx - 1 do
    780         begin
    781           xBlock := x * 8 div lx;
    782           if xBlock = (x + 1) * 8 div lx then
     775    for Y := 1 to ly - 2 do
     776    begin
     777      yBlock := Y * 5 div ly;
     778      if yBlock = (Y + 1) * 5 div ly then
     779        for X := 0 to lx - 1 do
     780        begin
     781          xBlock := X * 8 div lx;
     782          if xBlock = (X + 1) * 8 div lx then
    783783          begin
    784             Loc0 := x + lx * y;
     784            Loc0 := X + lx * Y;
    785785            if RealMap[Loc0] and fTerrain >= fGrass then
    786786            begin
     
    792792                if (Loc1 >= 0) and (Loc1 < MapSize) and
    793793                  (RealMap[Loc1] and fTerrain < fGrass) then
    794                   inc(Cnt); // count adjacent water
     794                  Inc(Cnt); // count adjacent water
    795795              end;
    796796              if Cnt <= RareMaxWater then // inner land
    797797              begin
    798                 inc(AreaCount[xBlock, yBlock]);
     798                Inc(AreaCount[xBlock, yBlock]);
    799799                if DelphiRandom(AreaCount[xBlock, yBlock]) = 0 then
    800800                  RareByArea[xBlock, yBlock] := Loc0;
     
    805805    end;
    806806    totalrare := 0;
    807     for x := 0 to 7 do
    808       for y := 0 to 4 do
    809         if AreaCount[x, y] > 0 then
    810           inc(totalrare);
    811     inc(RareMaxWater);
     807    for X := 0 to 7 do
     808      for Y := 0 to 4 do
     809        if AreaCount[X, Y] > 0 then
     810          Inc(totalrare);
     811    Inc(RareMaxWater);
    812812  until totalrare >= 12;
    813813
     
    815815  begin // remove rarebyarea resources too close to each other
    816816    FillChar(RareAdjacent, SizeOf(RareAdjacent), 0);
    817     for x := 0 to 7 do
    818       for y := 0 to 4 do
    819         if AreaCount[x, y] > 0 then
    820         begin
    821           if (AreaCount[(x + 1) mod 8, y] > 0) and
    822             (Continent[RareByArea[x, y]] = Continent
    823             [RareByArea[(x + 1) mod 8, y]]) then
     817    for X := 0 to 7 do
     818      for Y := 0 to 4 do
     819        if AreaCount[X, Y] > 0 then
     820        begin
     821          if (AreaCount[(X + 1) mod 8, Y] > 0) and
     822            (Continent[RareByArea[X, Y]] = Continent
     823            [RareByArea[(X + 1) mod 8, Y]]) then
    824824          begin
    825             inc(RareAdjacent[x, y]);
    826             inc(RareAdjacent[(x + 1) mod 8, y]);
     825            Inc(RareAdjacent[X, Y]);
     826            Inc(RareAdjacent[(X + 1) mod 8, Y]);
    827827          end;
    828           if y < 4 then
     828          if Y < 4 then
    829829          begin
    830             if (AreaCount[x, y + 1] > 0) and
    831               (Continent[RareByArea[x, y]] = Continent[RareByArea[x, y + 1]])
     830            if (AreaCount[X, Y + 1] > 0) and
     831              (Continent[RareByArea[X, Y]] = Continent[RareByArea[X, Y + 1]])
    832832            then
    833833            begin
    834               inc(RareAdjacent[x, y]);
    835               inc(RareAdjacent[x, y + 1]);
     834              Inc(RareAdjacent[X, Y]);
     835              Inc(RareAdjacent[X, Y + 1]);
    836836            end;
    837             if (AreaCount[(x + 1) mod 8, y + 1] > 0) and
    838               (Continent[RareByArea[x, y]] = Continent[RareByArea[(x + 1) mod 8,
    839               y + 1]]) then
     837            if (AreaCount[(X + 1) mod 8, Y + 1] > 0) and
     838              (Continent[RareByArea[X, Y]] = Continent[RareByArea[(X + 1) mod 8,
     839              Y + 1]]) then
    840840            begin
    841               inc(RareAdjacent[x, y]);
    842               inc(RareAdjacent[(x + 1) mod 8, y + 1]);
     841              Inc(RareAdjacent[X, Y]);
     842              Inc(RareAdjacent[(X + 1) mod 8, Y + 1]);
    843843            end;
    844             if (AreaCount[(x + 7) mod 8, y + 1] > 0) and
    845               (Continent[RareByArea[x, y]] = Continent[RareByArea[(x + 7) mod 8,
    846               y + 1]]) then
     844            if (AreaCount[(X + 7) mod 8, Y + 1] > 0) and
     845              (Continent[RareByArea[X, Y]] = Continent[RareByArea[(X + 7) mod 8,
     846              Y + 1]]) then
    847847            begin
    848               inc(RareAdjacent[x, y]);
    849               inc(RareAdjacent[(x + 7) mod 8, y + 1]);
     848              Inc(RareAdjacent[X, Y]);
     849              Inc(RareAdjacent[(X + 7) mod 8, Y + 1]);
    850850            end;
    851851          end;
     
    854854    yworst := 0;
    855855    Cnt := 0;
    856     for x := 0 to 7 do
    857       for y := 0 to 4 do
    858         if AreaCount[x, y] > 0 then
    859         begin
    860           if (Cnt = 0) or (RareAdjacent[x, y] > RareAdjacent[xworst, yworst])
     856    for X := 0 to 7 do
     857      for Y := 0 to 4 do
     858        if AreaCount[X, Y] > 0 then
     859        begin
     860          if (Cnt = 0) or (RareAdjacent[X, Y] > RareAdjacent[xworst, yworst])
    861861          then
    862862          begin
    863             xworst := x;
    864             yworst := y;
     863            xworst := X;
     864            yworst := Y;
    865865            Cnt := 1;
    866866          end
    867           else if (RareAdjacent[x, y] = RareAdjacent[xworst, yworst]) then
     867          else if (RareAdjacent[X, Y] = RareAdjacent[xworst, yworst]) then
    868868          begin
    869             inc(Cnt);
     869            Inc(Cnt);
    870870            if DelphiRandom(Cnt) = 0 then
    871871            begin
    872               xworst := x;
    873               yworst := y;
     872              xworst := X;
     873              yworst := Y;
    874874            end;
    875875          end;
    876876        end;
    877877    AreaCount[xworst, yworst] := 0;
    878     dec(totalrare);
     878    Dec(totalrare);
    879879  end;
    880880
    881881  Cnt := 0;
    882   for x := 0 to 7 do
    883     for y := 0 to 4 do
    884       if AreaCount[x, y] > 0 then
    885       begin
    886         RareLoc[Cnt] := RareByArea[x, y];
    887         inc(Cnt);
    888       end;
    889   for i := 0 to 11 do
    890   begin
    891     RealMap[RareLoc[i]] := RealMap[RareLoc[i]] and not(fTerrain or fSpecial) or
     882  for X := 0 to 7 do
     883    for Y := 0 to 4 do
     884      if AreaCount[X, Y] > 0 then
     885      begin
     886        RareLoc[Cnt] := RareByArea[X, Y];
     887        Inc(Cnt);
     888      end;
     889  for I := 0 to 11 do
     890  begin
     891    RealMap[RareLoc[I]] := RealMap[RareLoc[I]] and not(fTerrain or fSpecial) or
    892892      (fDesert or fDeadLands);
    893893    for dy := -1 to 1 do
     
    895895        if (dx + dy) and 1 = 0 then
    896896        begin
    897           Loc1 := dLoc(RareLoc[i], dx, dy);
     897          Loc1 := dLoc(RareLoc[I], dx, dy);
    898898          if (Loc1 >= 0) and (RealMap[Loc1] and fTerrain = fMountains) then
    899899            RealMap[Loc1] := RealMap[Loc1] and not fTerrain or fHills;
    900900        end;
    901901  end;
    902   for i := 0 to 11 do
    903     for j := 0 to 11 do
    904       Dist[i, j] := Distance(RareLoc[i], RareLoc[j]);
     902  for I := 0 to 11 do
     903    for J := 0 to 11 do
     904      Dist[I, J] := Distance(RareLoc[I], RareLoc[J]);
    905905
    906906  ibest := 0;
     
    910910  begin
    911911    Cnt := 0;
    912     for i := 0 to 11 do
    913       if RareLoc[i] >= 0 then
    914         for j := 0 to 11 do
    915           if RareLoc[j] >= 0 then
     912    for I := 0 to 11 do
     913      if RareLoc[I] >= 0 then
     914        for J := 0 to 11 do
     915          if RareLoc[J] >= 0 then
    916916            if (Cnt > 0) and (Dist[iBest, jbest] >= MinDist) then
    917917            begin
    918               if Dist[i, j] >= MinDist then
     918              if Dist[I, J] >= MinDist then
    919919              begin
    920                 inc(Cnt);
     920                Inc(Cnt);
    921921                if DelphiRandom(Cnt) = 0 then
    922922                begin
    923                   iBest := i;
    924                   jbest := j;
     923                  iBest := I;
     924                  jbest := J;
    925925                end;
    926926              end;
    927927            end
    928             else if (Cnt = 0) or (Dist[i, j] > Dist[iBest, jbest]) then
     928            else if (Cnt = 0) or (Dist[I, J] > Dist[iBest, jbest]) then
    929929            begin
    930               iBest := i;
    931               jbest := j;
     930              iBest := I;
     931              jbest := J;
    932932              Cnt := 1;
    933933            end;
     
    941941end;
    942942
    943 function CheckShore(Loc: integer): boolean;
    944 var
    945   Loc1, OldTile, V21: integer;
     943function CheckShore(Loc: Integer): Boolean;
     944var
     945  Loc1, OldTile, V21: Integer;
    946946  Radius: TVicinity21Loc;
    947947begin
    948   result := false;
     948  Result := False;
    949949  OldTile := RealMap[Loc];
    950950  if OldTile and fTerrain < fGrass then
     
    961961    end;
    962962    if (RealMap[Loc] xor Cardinal(OldTile)) and fTerrain <> 0 then
    963       result := true;
    964   end;
    965 end;
    966 
    967 function ActualSpecialTile(Loc: integer): Cardinal;
    968 begin
    969   result := SpecialTile(Loc, RealMap[Loc] and fTerrain, lx);
    970 end;
    971 
    972 procedure CreateMap(preview: boolean);
     963      Result := True;
     964  end;
     965end;
     966
     967function ActualSpecialTile(Loc: Integer): Cardinal;
     968begin
     969  Result := SpecialTile(Loc, RealMap[Loc] and fTerrain, lx);
     970end;
     971
     972procedure CreateMap(preview: Boolean);
    973973const
    974974  ShHiHills = 6; { of land }
     
    981981  hotunification = 50; // min. 25
    982982
    983   Zone: array [0 .. 3, 2 .. 9] of single = { terrain distribution }
     983  Zone: array [0 .. 3, 2 .. 9] of Single = { terrain distribution }
    984984    ((0.25, 0, 0, 0.4, 0, 0, 0, 0.35), (0.55, 0, 0.1, 0, 0, 0, 0, 0.35),
    985985    (0.4, 0, 0.35, 0, 0, 0, 0, 0.25), (0, 0.7, 0, 0, 0, 0, 0, 0.3));
    986986  { Grs  Dst  Pra  Tun  - - - For }
    987987
    988   function RndLow(y: integer): Cardinal;
     988  function RndLow(Y: Integer): Cardinal;
    989989  { random lowland appropriate to climate }
    990990  var
    991     z0, i: integer;
    992     p, p0, ZPlus: single;
    993   begin
    994     if ly - 1 - y > y then
    995     begin
    996       z0 := 6 * y div ly;
    997       ZPlus := 6 * y / ly - z0;
     991    z0, I: Integer;
     992    P, p0, ZPlus: Single;
     993  begin
     994    if ly - 1 - Y > Y then
     995    begin
     996      z0 := 6 * Y div ly;
     997      ZPlus := 6 * Y / ly - z0;
    998998    end
    999999    else
    10001000    begin
    1001       z0 := 6 * (ly - 1 - y) div ly;
    1002       ZPlus := 6 * (ly - 1 - y) / ly - z0;
     1001      z0 := 6 * (ly - 1 - Y) div ly;
     1002      ZPlus := 6 * (ly - 1 - Y) / ly - z0;
    10031003    end;
    10041004    p0 := 1;
    1005     for i := 2 to 9 do
    1006     begin
    1007       p := Zone[z0, i] * (1 - ZPlus) + Zone[z0 + 1, i] * ZPlus;
     1005    for I := 2 to 9 do
     1006    begin
     1007      P := Zone[z0, I] * (1 - ZPlus) + Zone[z0 + 1, I] * ZPlus;
    10081008      { weight between zones z0 and z0+1 }
    1009       if DelphiRandom * p0 < p then
    1010       begin
    1011         RndLow := i;
     1009      if DelphiRandom * p0 < P then
     1010      begin
     1011        RndLow := I;
    10121012        Break;
    10131013      end;
    1014       p0 := p0 - p;
    1015     end;
    1016   end;
    1017 
    1018   function RunRiver(Loc0: integer): integer;
     1014      p0 := p0 - P;
     1015    end;
     1016  end;
     1017
     1018  function RunRiver(Loc0: Integer): Integer;
    10191019  { runs river from start point Loc0; return value: length }
    10201020  var
    1021     Dir, T, Loc, Loc1, Cost: integer;
     1021    Dir, T, Loc, Loc1, Cost: Integer;
    10221022    Q: TIPQ;
    1023     From: array [0 .. lxmax * lymax - 1] of integer;
    1024     Time: array [0 .. lxmax * lymax - 1] of integer;
    1025     OneTileLake: boolean;
     1023    From: array [0 .. lxmax * lymax - 1] of Integer;
     1024    Time: array [0 .. lxmax * lymax - 1] of Integer;
     1025    OneTileLake: Boolean;
    10261026  begin
    10271027    FillChar(Time, SizeOf(Time), 255); { -1 }
     
    10321032      if (RealMap[Loc] and fTerrain < fGrass) then
    10331033      begin
    1034         OneTileLake := true;
     1034        OneTileLake := True;
    10351035        for Dir := 0 to 3 do
    10361036        begin
    10371037          Loc1 := dLoc(Loc, Dir and 1 * 2 - 1, Dir shr 1 * 2 - 1);
    10381038          if (Loc1 >= 0) and (RealMap[Loc1] and fTerrain < fGrass) then
    1039             OneTileLake := false;
     1039            OneTileLake := False;
    10401040        end;
    10411041        if not OneTileLake then
     
    10621062    end;
    10631063    Loc1 := Loc;
    1064     result := 0;
     1064    Result := 0;
    10651065    while Loc <> Loc0 do
    10661066    begin
    10671067      Loc := From[Loc];
    1068       inc(result);
    1069     end;
    1070     if (result > 1) and ((result >= MinRivLen) or
     1068      Inc(Result);
     1069    end;
     1070    if (Result > 1) and ((Result >= MinRivLen) or
    10711071      (RealMap[Loc1] and fTerrain >= fGrass)) then
    10721072    begin
     
    10821082    end
    10831083    else
    1084       result := 0;
     1084      Result := 0;
    10851085    FreeAndNil(Q);
    10861086  end;
    10871087
    10881088var
    1089   x, y, n, Dir, plus, Count, Loc0, Loc1, bLand, bHills, bMountains, V8: integer;
    1090   CopyFrom: array [0 .. lxmax * lymax - 1] of integer;
     1089  X, Y, N, Dir, plus, Count, Loc0, Loc1, bLand, bHills, bMountains, V8: Integer;
     1090  CopyFrom: array [0 .. lxmax * lymax - 1] of Integer;
    10911091  Adjacent: TVicinity8Loc;
    10921092
     
    10971097  while plus < MapSize * LandMass * ShMountains div 10000 do
    10981098  begin
    1099     dec(bMountains);
    1100     inc(plus, ElCount[bMountains]);
     1099    Dec(bMountains);
     1100    Inc(plus, ElCount[bMountains]);
    11011101  end;
    11021102  Count := plus;
     
    11051105  while plus < MapSize * LandMass * ShHiHills div 10000 do
    11061106  begin
    1107     dec(bHills);
    1108     inc(plus, ElCount[bHills]);
    1109   end;
    1110   inc(Count, plus);
     1107    Dec(bHills);
     1108    Inc(plus, ElCount[bHills]);
     1109  end;
     1110  Inc(Count, plus);
    11111111  bLand := bHills;
    11121112  while Count < MapSize * LandMass div 100 do
    11131113  begin
    1114     dec(bLand);
    1115     inc(Count, ElCount[bLand]);
     1114    Dec(bLand);
     1115    Inc(Count, ElCount[bLand]);
    11161116  end;
    11171117
     
    11361136          (RealMap[Loc1] and fTerrain < fGrass) or
    11371137          (RealMap[Loc1] and fTerrain = fArctic) then
    1138           inc(Count); // count adjacent water
     1138          Inc(Count); // count adjacent water
    11391139      end;
    11401140      if Count = 8 then
     
    11481148      plus := MapSize;
    11491149    Loc0 := DelphiRandom(MapSize);
    1150     for n := 0 to plus - 1 do
     1150    for N := 0 to plus - 1 do
    11511151    begin
    11521152      if (RealMap[Loc0] and fTerrain >= fGrass) and (Loc0 >= lx) and
     
    11651165    CopyFrom[Loc0] := Loc0;
    11661166
    1167   for n := 0 to unification * MapSize div 100 do
    1168   begin
    1169     y := DelphiRandom(ly);
    1170     if abs(y - (ly shr 1)) > ly div 4 + DelphiRandom(ly * hotunification div 100) then
    1171       if y < ly shr 1 then
    1172         y := ly shr 1 - y
     1167  for N := 0 to unification * MapSize div 100 do
     1168  begin
     1169    Y := DelphiRandom(ly);
     1170    if abs(Y - (ly shr 1)) > ly div 4 + DelphiRandom(ly * hotunification div 100) then
     1171      if Y < ly shr 1 then
     1172        Y := ly shr 1 - Y
    11731173      else
    1174         y := 3 * ly shr 1 - y;
    1175     Loc0 := lx * y + DelphiRandom(lx);
     1174        Y := 3 * ly shr 1 - Y;
     1175    Loc0 := lx * Y + DelphiRandom(lx);
    11761176    if RealMap[Loc0] and fTerrain = fGrass then
    11771177    begin
     
    12261226          if Loc1 >= 0 then
    12271227            if RealMap[Loc1] and fTerrain < fGrass then
    1228               inc(Count, 2);
     1228              Inc(Count, 2);
    12291229        end;
    12301230      end;
     
    12421242        if Loc1 >= 0 then
    12431243          if RealMap[Loc1] and fTerrain <> fDesert then
    1244             inc(Count);
     1244            Inc(Count);
    12451245      end;
    12461246      if Count >= 4 then
     
    12501250  for Loc0 := 0 to MapSize - 1 do
    12511251    CheckShore(Loc0); // change ocean to shore
    1252   for x := 0 to lx - 1 do
    1253   begin
    1254     RealMap[x + lx * 0] := fArctic;
    1255     if RealMap[x + lx * 1] >= fGrass then
    1256       RealMap[x + lx * 1] := RealMap[x + lx * 1] and not fTerrain or fTundra;
    1257     if RealMap[x + lx * (ly - 2)] >= fGrass then
    1258       RealMap[x + lx * (ly - 2)] := RealMap[x + lx * (ly - 2)] and
     1252  for X := 0 to lx - 1 do
     1253  begin
     1254    RealMap[X + lx * 0] := fArctic;
     1255    if RealMap[X + lx * 1] >= fGrass then
     1256      RealMap[X + lx * 1] := RealMap[X + lx * 1] and not fTerrain or fTundra;
     1257    if RealMap[X + lx * (ly - 2)] >= fGrass then
     1258      RealMap[X + lx * (ly - 2)] := RealMap[X + lx * (ly - 2)] and
    12591259        not fTerrain or fTundra;
    1260     RealMap[x + lx * (ly - 1)] := fArctic;
     1260    RealMap[X + lx * (ly - 1)] := fArctic;
    12611261  end;
    12621262
     
    12791279  CountGood: (cgBest, cgFlat, cgLand);
    12801280
    1281   function IsGoodTile(Loc: integer): boolean;
     1281  function IsGoodTile(Loc: Integer): Boolean;
    12821282  var
    1283     xLoc, yLoc: integer;
     1283    xLoc, yLoc: Integer;
    12841284  begin
    12851285    xLoc := Loc mod lx;
    12861286    yLoc := Loc div lx;
    12871287    if RealMap[Loc] and fDeadLands <> 0 then
    1288       result := false
     1288      Result := False
    12891289    else
    12901290      case CountGood of
    12911291        cgBest:
    1292           result := (RealMap[Loc] and fTerrain in [fGrass, fPrairie, fTundra,
     1292          Result := (RealMap[Loc] and fTerrain in [fGrass, fPrairie, fTundra,
    12931293            fSwamp, fForest]) and Odd((lymax + xLoc - yLoc shr 1) shr 1 + xLoc +
    12941294            (yLoc + 1) shr 1);
    12951295        cgFlat:
    1296           result := (RealMap[Loc] and fTerrain in [fGrass, fPrairie, fTundra,
     1296          Result := (RealMap[Loc] and fTerrain in [fGrass, fPrairie, fTundra,
    12971297            fSwamp, fForest]);
    12981298        cgLand:
    1299           result := RealMap[Loc] and fTerrain >= fGrass;
     1299          Result := RealMap[Loc] and fTerrain >= fGrass;
    13001300      end;
    13011301  end;
     
    13051305
    13061306var
    1307   p1, p2, nAlive, c, Loc, Loc1, CntGood, CntGoodGrass, MinDist, i, j, n,
     1307  p1, p2, nAlive, C, Loc, Loc1, CntGood, CntGoodGrass, MinDist, I, J, N,
    13081308    nsc, V21, V8, BestDist, TestDist, MinGood, nIrrLoc,
    1309     FineDistSQR, nRest: integer;
    1310   ccount: array [0 .. lxmax * lymax - 1] of word;
    1311   sc, StartLoc0, sccount: array [1 .. nPl] of integer;
    1312   TestStartLoc: array [0 .. nPl - 1] of integer;
    1313   CityLoc: array [1 .. nPl, 0 .. MaxCityLoc - 1] of integer;
    1314   nCityLoc: array [1 .. nPl] of integer;
    1315   RestLoc: array [0 .. MaxCityLoc - 1] of integer;
    1316   IrrLoc: array [0 .. 20] of integer;
     1309    FineDistSQR, nRest: Integer;
     1310  ccount: array [0 .. lxmax * lymax - 1] of Word;
     1311  sc, StartLoc0, sccount: array [1 .. nPl] of Integer;
     1312  TestStartLoc: array [0 .. nPl - 1] of Integer;
     1313  CityLoc: array [1 .. nPl, 0 .. MaxCityLoc - 1] of Integer;
     1314  nCityLoc: array [1 .. nPl] of Integer;
     1315  RestLoc: array [0 .. MaxCityLoc - 1] of Integer;
     1316  IrrLoc: array [0 .. 20] of Integer;
    13171317  Radius: TVicinity21Loc;
    13181318  Adjacent: TVicinity8Loc;
    1319   ok: boolean;
     1319  ok: Boolean;
    13201320
    13211321begin
     
    13231323  for p1 := 0 to nPl - 1 do
    13241324    if 1 shl p1 and GAlive <> 0 then
    1325       inc(nAlive);
     1325      Inc(nAlive);
    13261326  if nAlive = 0 then
    1327     exit;
     1327    Exit;
    13281328
    13291329  { count good tiles }
     
    13321332    if RealMap[Loc] and fTerrain = fGrass then
    13331333      if ActualSpecialTile(Loc) = 1 then
    1334         inc(ccount[Continent[Loc]], 3)
     1334        Inc(ccount[Continent[Loc]], 3)
    13351335      else
    1336         inc(ccount[Continent[Loc]], 2)
     1336        Inc(ccount[Continent[Loc]], 2)
    13371337    else if RealMap[Loc] and fTerrain in [fPrairie, fSwamp, fForest, fHills]
    13381338    then
    1339       inc(ccount[Continent[Loc]]);
     1339      Inc(ccount[Continent[Loc]]);
    13401340
    13411341  Loc := 0;
    13421342  while ccount[Loc] > 0 do
    1343     inc(Loc);
    1344   for i := 1 to nAlive do
    1345   begin
    1346     sc[i] := Loc;
    1347     sccount[i] := 1
     1343    Inc(Loc);
     1344  for I := 1 to nAlive do
     1345  begin
     1346    sc[I] := Loc;
     1347    sccount[I] := 1
    13481348  end;
    13491349  { init with zero size start continents, then search bigger ones }
     
    13561356        if p1 < nAlive + 1 then
    13571357          sc[p1] := sc[p1 - 1];
    1358         dec(p1);
     1358        Dec(p1);
    13591359      end;
    13601360      if p1 < nAlive + 1 then
     
    13631363  nsc := nAlive;
    13641364  repeat
    1365     c := 1; // search least crowded continent after smallest
    1366     for i := 2 to nsc - 1 do
    1367       if ccount[sc[i]] * (2 * sccount[c] + 1) > ccount[sc[c]] *
    1368         (2 * sccount[i] + 1) then
    1369         c := i;
    1370     if ccount[sc[nsc]] * (2 * sccount[c] + 1) > ccount[sc[c]] then
     1365    C := 1; // search least crowded continent after smallest
     1366    for I := 2 to nsc - 1 do
     1367      if ccount[sc[I]] * (2 * sccount[C] + 1) > ccount[sc[C]] *
     1368        (2 * sccount[I] + 1) then
     1369        C := I;
     1370    if ccount[sc[nsc]] * (2 * sccount[C] + 1) > ccount[sc[C]] then
    13711371      Break; // even least crowded continent is more crowded than smallest
    1372     inc(sccount[c]);
    1373     dec(nsc);
     1372    Inc(sccount[C]);
     1373    Dec(nsc);
    13741374  until sccount[nsc] > 1;
    13751375
     
    13771377  CountGood := cgBest;
    13781378  repeat
    1379     dec(MinGood);
     1379    Dec(MinGood);
    13801380    if (MinGood = 3) and (CountGood < cgLand) then // too demanding!
    13811381    begin
    1382       inc(CountGood);
     1382      Inc(CountGood);
    13831383      MinGood := 6;
    13841384    end;
    13851385    FillChar(nCityLoc, SizeOf(nCityLoc), 0);
    13861386    Loc := DelphiRandom(MapSize);
    1387     for i := 0 to MapSize - 1 do
     1387    for I := 0 to MapSize - 1 do
    13881388    begin
    13891389      if ((Loc >= 4 * lx) and (Loc < MapSize - 4 * lx) or (CountGood >= cgLand))
    13901390        and IsGoodTile(Loc) then
    13911391      begin
    1392         c := nsc;
    1393         while (c > 0) and (Continent[Loc] <> sc[c]) do
    1394           dec(c);
    1395         if (c > 0) and (nCityLoc[c] < MaxCityLoc) then
     1392        C := nsc;
     1393        while (C > 0) and (Continent[Loc] <> sc[C]) do
     1394          Dec(C);
     1395        if (C > 0) and (nCityLoc[C] < MaxCityLoc) then
    13961396        begin
    13971397          CntGood := 1;
     
    14021402              Loc1 := Radius[V21];
    14031403              if (Loc1 >= 0) and (Loc1 < MapSize) and IsGoodTile(Loc1) then
    1404                 inc(CntGood);
     1404                Inc(CntGood);
    14051405            end;
    14061406          if CntGood >= MinGood then
    14071407          begin
    1408             CityLoc[c, nCityLoc[c]] := Loc;
    1409             inc(nCityLoc[c]);
     1408            CityLoc[C, nCityLoc[C]] := Loc;
     1409            Inc(nCityLoc[C]);
    14101410          end;
    14111411        end;
     
    14141414    end;
    14151415
    1416     ok := true;
    1417     for c := 1 to nsc do
    1418       if nCityLoc[c] < sccount[c] * (8 - MinGood) div (7 - MinGood) then
    1419         ok := false;
     1416    ok := True;
     1417    for C := 1 to nsc do
     1418      if nCityLoc[C] < sccount[C] * (8 - MinGood) div (7 - MinGood) then
     1419        ok := False;
    14201420  until ok;
    14211421
    14221422  FineDistSQR := MapSize * LandMass * 9 div (nAlive * 100);
    14231423  p1 := 1;
    1424   for c := 1 to nsc do
     1424  for C := 1 to nsc do
    14251425  begin // for all start continents
    1426     if sccount[c] = 1 then
    1427       StartLoc0[p1] := CityLoc[c, DelphiRandom(nCityLoc[c])]
     1426    if sccount[C] = 1 then
     1427      StartLoc0[p1] := CityLoc[C, DelphiRandom(nCityLoc[C])]
    14281428    else
    14291429    begin
    14301430      BestDist := 0;
    1431       n := 1 shl sccount[c] * 32; // number of tries to find good distribution
    1432       if n > 1 shl 12 then
    1433         n := 1 shl 12;
    1434       while (n > 0) and (BestDist * BestDist < FineDistSQR) do
     1431      N := 1 shl sccount[C] * 32; // number of tries to find good distribution
     1432      if N > 1 shl 12 then
     1433        N := 1 shl 12;
     1434      while (N > 0) and (BestDist * BestDist < FineDistSQR) do
    14351435      begin
    14361436        MinDist := MaxInt;
    1437         nRest := nCityLoc[c];
    1438         for i := 0 to nRest - 1 do
    1439           RestLoc[i] := CityLoc[c, i];
    1440         for i := 0 to sccount[c] - 1 do
     1437        nRest := nCityLoc[C];
     1438        for I := 0 to nRest - 1 do
     1439          RestLoc[I] := CityLoc[C, I];
     1440        for I := 0 to sccount[C] - 1 do
    14411441        begin
    14421442          if nRest = 0 then
    14431443            Break;
    1444           j := DelphiRandom(nRest);
    1445           TestStartLoc[i] := RestLoc[j];
    1446           RestLoc[j] := RestLoc[nRest - 1];
    1447           dec(nRest);
    1448           for j := 0 to i - 1 do
     1444          J := DelphiRandom(nRest);
     1445          TestStartLoc[I] := RestLoc[J];
     1446          RestLoc[J] := RestLoc[nRest - 1];
     1447          Dec(nRest);
     1448          for J := 0 to I - 1 do
    14491449          begin
    1450             TestDist := Distance(TestStartLoc[i], TestStartLoc[j]);
     1450            TestDist := Distance(TestStartLoc[I], TestStartLoc[J]);
    14511451            if TestDist < MinDist then
    14521452              MinDist := TestDist;
    14531453          end;
    1454           if i = sccount[c] - 1 then
     1454          if I = sccount[C] - 1 then
    14551455          begin
    1456             assert(MinDist > BestDist);
     1456            Assert(MinDist > BestDist);
    14571457            BestDist := MinDist;
    1458             for j := 0 to sccount[c] - 1 do
    1459               StartLoc0[p1 + j] := TestStartLoc[j];
     1458            for J := 0 to sccount[C] - 1 do
     1459              StartLoc0[p1 + J] := TestStartLoc[J];
    14601460          end
    14611461          else if BestDist > 0 then
    14621462          begin
    1463             j := 0;
    1464             while j < nRest do
     1463            J := 0;
     1464            while J < nRest do
    14651465            begin // remove all locs from rest which have too little distance to this one
    1466               TestDist := Distance(TestStartLoc[i], RestLoc[j]);
     1466              TestDist := Distance(TestStartLoc[I], RestLoc[J]);
    14671467              if TestDist <= BestDist then
    14681468              begin
    1469                 RestLoc[j] := RestLoc[nRest - 1];
    1470                 dec(nRest);
     1469                RestLoc[J] := RestLoc[nRest - 1];
     1470                Dec(nRest);
    14711471              end
    14721472              else
    1473                 inc(j);
     1473                Inc(J);
    14741474            end;
    14751475          end;
    14761476        end;
    1477         dec(n)
    1478       end;
    1479     end;
    1480     p1 := p1 + sccount[c]
     1477        Dec(N)
     1478      end;
     1479    end;
     1480    p1 := p1 + sccount[C]
    14811481  end;
    14821482
     
    14951495        if (Loc1 >= 0) and (Loc1 < MapSize) and IsGoodTile(Loc1) then
    14961496          if RealMap[Loc1] and fTerrain = fGrass then
    1497             inc(CntGoodGrass)
     1497            Inc(CntGoodGrass)
    14981498          else
    1499             inc(CntGood);
     1499            Inc(CntGood);
    15001500      end;
    15011501    for V21 := 1 to 26 do
     
    15291529        begin
    15301530          IrrLoc[nIrrLoc] := Loc1;
    1531           inc(nIrrLoc);
    1532         end;
    1533       end;
    1534     i := 2;
    1535     if i > nIrrLoc then
    1536       i := nIrrLoc;
    1537     while i > 0 do
    1538     begin
    1539       j := DelphiRandom(nIrrLoc);
    1540       RealMap[IrrLoc[j]] := RealMap[IrrLoc[j]] or tiIrrigation;
    1541       IrrLoc[j] := IrrLoc[nIrrLoc - 1];
    1542       dec(nIrrLoc);
    1543       dec(i);
     1531          Inc(nIrrLoc);
     1532        end;
     1533      end;
     1534    I := 2;
     1535    if I > nIrrLoc then
     1536      I := nIrrLoc;
     1537    while I > 0 do
     1538    begin
     1539      J := DelphiRandom(nIrrLoc);
     1540      RealMap[IrrLoc[J]] := RealMap[IrrLoc[J]] or tiIrrigation;
     1541      IrrLoc[J] := IrrLoc[nIrrLoc - 1];
     1542      Dec(nIrrLoc);
     1543      Dec(I);
    15441544    end;
    15451545  end;
     
    15501550    begin
    15511551      repeat
    1552         i := DelphiRandom(nAlive) + 1
    1553       until StartLoc0[i] >= 0;
    1554       StartLoc[p1] := StartLoc0[i];
    1555       StartLoc0[i] := -1
     1552        I := DelphiRandom(nAlive) + 1
     1553      until StartLoc0[I] >= 0;
     1554      StartLoc[p1] := StartLoc0[I];
     1555      StartLoc0[I] := -1
    15561556    end;
    15571557  SaveMapCenterLoc := StartLoc[0];
     
    15861586          StartLoc2[p1] := Loc1;
    15871587          BestDist := TestDist;
    1588           n := 1;
     1588          N := 1;
    15891589        end
    15901590        else if TestDist = BestDist then
    15911591        begin
    1592           inc(n);
    1593           if DelphiRandom(n) = 0 then
     1592          Inc(N);
     1593          if DelphiRandom(N) = 0 then
    15941594            StartLoc2[p1] := Loc1;
    15951595        end;
     
    15981598end;
    15991599
    1600 procedure PredefinedStartPositions(Human: integer);
     1600procedure PredefinedStartPositions(Human: Integer);
    16011601// use predefined nation start positions
    16021602var
    1603   i, p1, Loc1, nAlive, nStartLoc0, nPrefStartLoc0, imax: integer;
    1604   StartLoc0: array [0 .. lxmax * lymax - 1] of integer;
    1605   ishuman: boolean;
     1603  I, p1, Loc1, nAlive, nStartLoc0, nPrefStartLoc0, imax: Integer;
     1604  StartLoc0: array [0 .. lxmax * lymax - 1] of Integer;
     1605  ishuman: Boolean;
    16061606begin
    16071607  nAlive := 0;
    16081608  for p1 := 0 to nPl - 1 do
    16091609    if 1 shl p1 and GAlive <> 0 then
    1610       inc(nAlive);
     1610      Inc(nAlive);
    16111611  if nAlive = 0 then
    1612     exit;
     1612    Exit;
    16131613
    16141614  for I := 0 to Length(StartLoc0) - 1 do
     
    16231623      StartLoc0[nStartLoc0] := StartLoc0[nPrefStartLoc0];
    16241624      StartLoc0[nPrefStartLoc0] := Loc1;
    1625       inc(nPrefStartLoc0);
    1626       inc(nStartLoc0);
     1625      Inc(nPrefStartLoc0);
     1626      Inc(nStartLoc0);
    16271627      RealMap[Loc1] := RealMap[Loc1] and not fPrefStartPos;
    16281628    end
     
    16301630    begin
    16311631      StartLoc0[nStartLoc0] := Loc1;
    1632       inc(nStartLoc0);
     1632      Inc(nStartLoc0);
    16331633      RealMap[Loc1] := RealMap[Loc1] and not fStartPos;
    16341634    end;
    1635   assert(nStartLoc0 >= nAlive);
     1635  Assert(nStartLoc0 >= nAlive);
    16361636
    16371637  StartLoc[0] := 0;
    1638   for ishuman := true downto false do
     1638  for ishuman := True downto False do
    16391639    for p1 := 0 to nPl - 1 do
    16401640      if (1 shl p1 and GAlive <> 0) and ((1 shl p1 and Human <> 0) = ishuman)
    16411641      then
    16421642      begin
    1643         dec(nStartLoc0);
     1643        Dec(nStartLoc0);
    16441644        imax := nStartLoc0;
    16451645        if nPrefStartLoc0 > 0 then
    16461646        begin
    1647           dec(nPrefStartLoc0);
     1647          Dec(nPrefStartLoc0);
    16481648          imax := nPrefStartLoc0;
    16491649        end;
    1650         i := DelphiRandom(imax + 1);
    1651         StartLoc[p1] := StartLoc0[i];
    1652         StartLoc2[p1] := StartLoc0[i];
    1653         StartLoc0[i] := StartLoc0[imax];
     1650        I := DelphiRandom(imax + 1);
     1651        StartLoc[p1] := StartLoc0[I];
     1652        StartLoc2[p1] := StartLoc0[I];
     1653        StartLoc0[I] := StartLoc0[imax];
    16541654        StartLoc0[imax] := StartLoc0[nStartLoc0];
    16551655      end;
     
    16591659procedure InitGame;
    16601660var
    1661   i, p, p1, uix, Loc1: integer;
     1661  I, P, p1, uix, Loc1: Integer;
    16621662begin
    16631663  {$IFDEF FastContact}
     
    16831683  GTestFlags := 0;
    16841684  GInitialized := GAlive or GWatching;
    1685   for p := 0 to nPl - 1 do
    1686     if 1 shl p and GInitialized <> 0 then
    1687       with RW[p] do
    1688       begin
    1689         Researched[p] := 0;
    1690         Discovered[p] := 0;
    1691         TerritoryCount[p] := 0;
    1692         nTech[p] := 0;
    1693         if Difficulty[p] = 0 then
    1694           ResourceMask[p] := $FFFFFFFF
     1685  for P := 0 to nPl - 1 do
     1686    if 1 shl P and GInitialized <> 0 then
     1687      with RW[P] do
     1688      begin
     1689        Researched[P] := 0;
     1690        Discovered[P] := 0;
     1691        TerritoryCount[P] := 0;
     1692        nTech[P] := 0;
     1693        if Difficulty[P] = 0 then
     1694          ResourceMask[P] := $FFFFFFFF
    16951695        else
    1696           ResourceMask[p] := $FFFFFFFF and not(fSpecial2 or fModern);
    1697         GrWallContinent[p] := -1;
     1696          ResourceMask[P] := $FFFFFFFF and not(fSpecial2 or fModern);
     1697        GrWallContinent[P] := -1;
    16981698
    16991699        GetMem(Map, 4 * MapSize);
     
    17131713          if 1 shl p1 and GInitialized <> 0 then
    17141714          begin
    1715             FillChar(RWemix[p, p1], SizeOf(RWemix[p, p1]), 255); { -1 }
    1716             FillChar(Destroyed[p, p1], SizeOf(Destroyed[p, p1]), 0);
     1715            FillChar(RWemix[P, p1], SizeOf(RWemix[P, p1]), 255); { -1 }
     1716            FillChar(Destroyed[P, p1], SizeOf(Destroyed[P, p1]), 0);
    17171717          end;
    17181718          Attitude[p1] := atNeutral;
     
    17221722          Tribute[p1] := 0;
    17231723          TributePaid[p1] := 0;
    1724           if (p1 <> p) and (1 shl p1 and GAlive <> 0) then
     1724          if (p1 <> P) and (1 shl p1 and GAlive <> 0) then
    17251725          begin // initialize enemy report
    17261726            GetMem(EnemyReport[p1], SizeOf(TEnemyReport) - 2 *
     
    17321732            EnemyReport[p1].Attitude := atNeutral;
    17331733            EnemyReport[p1].Government := gDespotism;
    1734             if 1 shl p and GAlive = 0 then
     1734            if 1 shl P and GAlive = 0 then
    17351735              Treaty[p1] := trNone // supervisor
    17361736          end
     
    17541754
    17551755  // create initial models and units
    1756   for p := 0 to nPl - 1 do
    1757     if (1 shl p and GAlive <> 0) then
    1758       with RW[p] do
     1756  for P := 0 to nPl - 1 do
     1757    if (1 shl P and GAlive <> 0) then
     1758      with RW[P] do
    17591759      begin
    17601760        nModel := 0;
    1761         for i := 0 to nSpecialModel - 1 do
    1762           if SpecialModelPreq[i] = preNone then
     1761        for I := 0 to nSpecialModel - 1 do
     1762          if SpecialModelPreq[I] = preNone then
    17631763          begin
    1764             Model[nModel] := SpecialModel[i];
     1764            Model[nModel] := SpecialModel[I];
    17651765            Model[nModel].Status := 0;
    17661766            Model[nModel].IntroTurn := 0;
    17671767            Model[nModel].Built := 0;
    17681768            Model[nModel].Lost := 0;
    1769             Model[nModel].ID := p shl 12 + nModel;
     1769            Model[nModel].ID := P shl 12 + nModel;
    17701770            SetModelFlags(Model[nModel]);
    1771             inc(nModel);
     1771            Inc(nModel);
    17721772          end;
    17731773        nUn := 0;
    1774         UnBuilt[p] := 0;
     1774        UnBuilt[P] := 0;
    17751775        for uix := 0 to nStartUn - 1 do
    17761776        begin
    1777           CreateUnit(p, StartUn[uix]);
    1778           dec(Model[StartUn[uix]].Built);
    1779           Un[uix].Loc := StartLoc2[p];
    1780           PlaceUnit(p, uix);
    1781         end;
    1782         FoundCity(p, StartLoc[p]); // capital
    1783         Founded[p] := 1;
     1777          CreateUnit(P, StartUn[uix]);
     1778          Dec(Model[StartUn[uix]].Built);
     1779          Un[uix].Loc := StartLoc2[P];
     1780          PlaceUnit(P, uix);
     1781        end;
     1782        FoundCity(P, StartLoc[P]); // capital
     1783        Founded[P] := 1;
    17841784        with City[0] do
    17851785        begin
    1786           ID := p shl 12;
     1786          ID := P shl 12;
    17871787          Flags := chFounded;
    17881788        end;
     
    17981798  CalculatePrimitive;
    17991799  CreateElevation;
    1800   CreateMap(false);
     1800  CreateMap(False);
    18011801  StartPositions;
    18021802  InitGame;
    18031803end;
    18041804
    1805 procedure InitMapGame(Human: integer);
     1805procedure InitMapGame(Human: Integer);
    18061806begin
    18071807  DelphiRandSeed := RND;
     
    18131813procedure ReleaseGame;
    18141814var
    1815   p1, p2: integer;
     1815  p1, p2: Integer;
    18161816begin
    18171817  for p1 := 0 to nPl - 1 do
     
    18351835procedure InitMapEditor;
    18361836var
    1837   p1: integer;
     1837  p1: Integer;
    18381838begin
    18391839  CalculatePrimitive;
     
    18721872end;
    18731873
    1874 procedure EditTile(Loc, NewTile: integer);
    1875 var
    1876   Loc1, V21: integer;
     1874procedure EditTile(Loc, NewTile: Integer);
     1875var
     1876  Loc1, V21: Integer;
    18771877  Radius: TVicinity21Loc;
    18781878begin
     
    19321932  ____________________________________________________________________
    19331933}
    1934 function GetTileInfo(p, cix, Loc: integer; var Info: TTileInfo): integer;
     1934function GetTileInfo(P, cix, Loc: Integer; var Info: TTileInfo): Integer;
    19351935// cix>=0 - known city index of player p -- only core internal!
    19361936// cix=-1 - search city, player unknown, only if permission for p
    19371937// cix=-2 - don't search city, don't calculate city benefits, just government of player p
    19381938var
    1939   p0, Tile, special: integer;
     1939  p0, Tile, special: Integer;
    19401940begin
    19411941  with Info do
    19421942  begin
    1943     p0 := p;
     1943    p0 := P;
    19441944    if cix >= 0 then
    19451945      Tile := RealMap[Loc]
    19461946    else
    19471947    begin
    1948       Tile := RW[p].Map[Loc];
     1948      Tile := RW[P].Map[Loc];
    19491949      if Tile and fTerrain = fUNKNOWN then
    19501950      begin
    1951         result := eNoPreq;
    1952         exit;
     1951        Result := eNoPreq;
     1952        Exit;
    19531953      end;
    19541954    end;
     
    19561956    if (cix = -1) and (UsedByCity[Loc] >= 0) then
    19571957    begin // search exploiting player and city
    1958       SearchCity(UsedByCity[Loc], p, cix);
    1959       if not((p = p0) or (ObserveLevel[UsedByCity[Loc]] shr (2 * p0) and
     1958      SearchCity(UsedByCity[Loc], P, cix);
     1959      if not((P = p0) or (ObserveLevel[UsedByCity[Loc]] shr (2 * p0) and
    19601960        3 = lObserveSuper)) then
    19611961        cix := -1
     
    19631963    if cix = -1 then
    19641964    begin
    1965       result := eInvalid;
    1966       exit;
     1965      Result := eInvalid;
     1966      Exit;
    19671967    end; // no city found here
    19681968
    1969     special := Tile and fSpecial and ResourceMask[p] shr 5;
     1969    special := Tile and fSpecial and ResourceMask[P] shr 5;
    19701970    with Terrain[Tile and fTerrain] do
    19711971    begin
     
    19741974      Trade := TradeRes[special];
    19751975      if (special > 0) and (Tile and fTerrain <> fGrass) and
    1976         (RW[p].NatBuilt[imSpacePort] > 0) then
     1976        (RW[P].NatBuilt[imSpacePort] > 0) then
    19771977      begin // GeoSat effect
    19781978        Food := 2 * Food - FoodRes[0];
     
    19831983      if (Tile and fTerImp = tiIrrigation) or (Tile and fTerImp = tiFarm) or
    19841984        (Tile and fCity <> 0) then
    1985         inc(Food, IrrEff); { irrigation effect }
     1985        Inc(Food, IrrEff); { irrigation effect }
    19861986      if Tile and fTerImp = tiMine then
    1987         inc(Prod, MineEff); { mining effect }
    1988       if (Tile and fRiver <> 0) and (RW[p].Tech[adMapMaking] >= tsApplicable)
     1987        Inc(Prod, MineEff); { mining effect }
     1988      if (Tile and fRiver <> 0) and (RW[P].Tech[adMapMaking] >= tsApplicable)
    19891989      then
    1990         inc(Trade); { river effect }
     1990        Inc(Trade); { river effect }
    19911991      if (Tile and (fRoad or fRR) <> 0) and (MoveCost = 1) and
    1992         (RW[p].Tech[adWheel] >= tsApplicable) then
    1993         inc(Trade); { road effect }
     1992        (RW[P].Tech[adWheel] >= tsApplicable) then
     1993        Inc(Trade); { road effect }
    19941994      if (Tile and (fRR or fCity) <> 0) and
    1995         (RW[p].Tech[adRailroad] >= tsApplicable) then
    1996         inc(Prod, Prod shr 1); { railroad effect }
     1995        (RW[P].Tech[adRailroad] >= tsApplicable) then
     1996        Inc(Prod, Prod shr 1); { railroad effect }
    19971997
    19981998      ExplCity := -1;
    1999       if (cix >= 0) and (p = p0) then
     1999      if (cix >= 0) and (P = p0) then
    20002000        ExplCity := cix;
    20012001      if cix >= 0 then
     
    20032003        begin
    20042004          if ((Tile and fTerImp = tiFarm) or (Tile and fCity <> 0)) and
    2005             (RW[p].City[cix].Built[imSupermarket] > 0) then
    2006             inc(Food, Food shr 1); { farmland effect }
     2005            (RW[P].City[cix].Built[imSupermarket] > 0) then
     2006            Inc(Food, Food shr 1); { farmland effect }
    20072007          if (Tile and (fRoad or fRR) <> 0) and (MoveCost = 1) and
    2008             (RW[p].City[cix].Built[imHighways] > 0) then
    2009             inc(Trade, 1); { superhighway effect }
     2008            (RW[P].City[cix].Built[imHighways] > 0) then
     2009            Inc(Trade, 1); { superhighway effect }
    20102010        end
    20112011        else
    20122012        begin
    2013           if RW[p].City[cix].Built[imHarbor] > 0 then
    2014             inc(Food); { harbour effect }
    2015           if RW[p].City[cix].Built[imPlatform] > 0 then
    2016             inc(Prod); { oil platform effect }
    2017           if GWonder[woLighthouse].EffectiveOwner = p then
    2018             inc(Prod);
     2013          if RW[P].City[cix].Built[imHarbor] > 0 then
     2014            Inc(Food); { harbour effect }
     2015          if RW[P].City[cix].Built[imPlatform] > 0 then
     2016            Inc(Prod); { oil platform effect }
     2017          if GWonder[woLighthouse].EffectiveOwner = P then
     2018            Inc(Prod);
    20192019        end;
    20202020    end;
    20212021
    20222022    { good government influence }
    2023     if (RW[p].Government in [gRepublic, gDemocracy, gFuture]) and (Trade > 0)
     2023    if (RW[P].Government in [gRepublic, gDemocracy, gFuture]) and (Trade > 0)
    20242024    then
    2025       inc(Trade);
    2026     if (RW[p].Government = gCommunism) and (Prod > 1) then
    2027       inc(Prod);
    2028 
    2029     if RW[p].Government in [gAnarchy, gDespotism] then
     2025      Inc(Trade);
     2026    if (RW[P].Government = gCommunism) and (Prod > 1) then
     2027      Inc(Prod);
     2028
     2029    if RW[P].Government in [gAnarchy, gDespotism] then
    20302030    begin { bad government influence }
    20312031      if Food > 3 then
     
    20392039    if Tile and (fTerrain or fPoll) > fPoll then
    20402040    begin { pollution - decrease ressources }
    2041       dec(Food, Food shr 1);
    2042       dec(Prod, Prod shr 1);
    2043       dec(Trade, Trade shr 1);
     2041      Dec(Food, Food shr 1);
     2042      Dec(Prod, Prod shr 1);
     2043      Dec(Trade, Trade shr 1);
    20442044    end;
    20452045
    20462046    if Tile and fCity <> 0 then
    20472047      Trade := 0
    2048     else if (cix >= 0) and (RW[p].City[cix].Built[imCourt] + RW[p].City[cix]
     2048    else if (cix >= 0) and (RW[P].City[cix].Built[imCourt] + RW[P].City[cix]
    20492049      .Built[imPalace] = 0) then
    2050       if RW[p].City[cix].Built[imTownHall] = 0 then
     2050      if RW[P].City[cix].Built[imTownHall] = 0 then
    20512051        Trade := 0
    20522052      else if Trade > 3 then
    20532053        Trade := 3;
    20542054  end;
    2055   result := eOK;
    2056 end;
    2057 
    2058 procedure Strongest(Loc: integer; var uix, Strength, Bonus, Cnt: integer);
     2055  Result := eOK;
     2056end;
     2057
     2058procedure Strongest(Loc: Integer; var uix, Strength, Bonus, Cnt: Integer);
    20592059{ find strongest defender at Loc }
    20602060var
    20612061  Defender, uix1, Det, Cost, TestStrength, TestBonus, TestDet, TestCost,
    2062     Domain: integer;
     2062    Domain: Integer;
    20632063  PUn: ^TUn;
    20642064  PModel: ^TModel;
     
    20782078    if PUn.Loc = Loc then
    20792079    begin
    2080       inc(Cnt);
     2080      Inc(Cnt);
    20812081      if PUn.Master < 0 then
    20822082      begin
     
    20852085          TestBonus := Terrain[RealMap[Loc] and fTerrain].Defense;
    20862086          if RealMap[Loc] and fTerImp = tiFort then
    2087             inc(TestBonus, 4);
     2087            Inc(TestBonus, 4);
    20882088          if PUn.Flags and unFortified <> 0 then
    2089             inc(TestBonus, 2);
     2089            Inc(TestBonus, 2);
    20902090          if (PModel.Kind = mkSpecial_TownGuard) and
    20912091            (RealMap[Loc] and fCity <> 0) then
    2092             inc(TestBonus, 4);
     2092            Inc(TestBonus, 4);
    20932093        end
    20942094        else
    20952095          TestBonus := 4;
    2096         inc(TestBonus, PUn.exp div ExpCost);
     2096        Inc(TestBonus, PUn.exp div ExpCost);
    20972097        TestStrength := PModel.Defense * TestBonus * PUn.Health;
    20982098        if (Domain = dAir) and ((RealMap[Loc] and fCity <> 0) or
     
    21042104        if PModel.Cap[mcStealth] > 0 then
    21052105        else if PModel.Cap[mcSub] > 0 then
    2106           inc(TestDet, 1 shl 28)
     2106          Inc(TestDet, 1 shl 28)
    21072107        else if (Domain = dGround) and (PModel.Cap[mcFanatic] > 0) and
    21082108          not(RW[Defender].Government in [gRepublic, gDemocracy, gFuture]) then
    2109           inc(TestDet, 4 shl 28) // fanatic ground units always defend
     2109          Inc(TestDet, 4 shl 28) // fanatic ground units always defend
    21102110        else if PModel.Flags and mdZOC <> 0 then
    2111           inc(TestDet, 3 shl 28)
     2111          Inc(TestDet, 3 shl 28)
    21122112        else
    2113           inc(TestDet, 2 shl 28);
     2113          Inc(TestDet, 2 shl 28);
    21142114        TestCost := RW[Defender].Model[PUn.mix].Cost;
    21152115        if (TestDet > Det) or (TestDet = Det) and (TestCost < Cost) then
     
    21262126end;
    21272127
    2128 function UnitSpeed(p, mix, Health: integer): integer;
    2129 begin
    2130   with RW[p].Model[mix] do
    2131   begin
    2132     result := Speed;
     2128function UnitSpeed(P, mix, Health: Integer): Integer;
     2129begin
     2130  with RW[P].Model[mix] do
     2131  begin
     2132    Result := Speed;
    21332133    if Domain = dSea then
    21342134    begin
    2135       if GWonder[woMagellan].EffectiveOwner = p then
    2136         inc(result, 200);
     2135      if GWonder[woMagellan].EffectiveOwner = P then
     2136        Inc(Result, 200);
    21372137      if Health < 100 then
    2138         result := ((result - 250) * Health div 5000) * 50 + 250;
    2139     end;
    2140   end;
    2141 end;
    2142 
    2143 procedure GetUnitReport(p, uix: integer; var UnitReport: TUnitReport);
    2144 var
    2145   TerrOwner: integer;
     2138        Result := ((Result - 250) * Health div 5000) * 50 + 250;
     2139    end;
     2140  end;
     2141end;
     2142
     2143procedure GetUnitReport(P, uix: Integer; var UnitReport: TUnitReport);
     2144var
     2145  TerrOwner: Integer;
    21462146  PModel: ^TModel;
    21472147begin
     
    21492149  UnitReport.ProdSupport := 0;
    21502150  UnitReport.ReportFlags := 0;
    2151   if RW[p].Government <> gAnarchy then
    2152     with RW[p].Un[uix] do
    2153     begin
    2154       PModel := @RW[p].Model[mix];
     2151  if RW[P].Government <> gAnarchy then
     2152    with RW[P].Un[uix] do
     2153    begin
     2154      PModel := @RW[P].Model[mix];
    21552155      if (PModel.Kind = mkSettler)
    21562156      { and (GWonder[woFreeSettlers].EffectiveOwner<>p) } then
    2157         UnitReport.FoodSupport := SettlerFood[RW[p].Government]
     2157        UnitReport.FoodSupport := SettlerFood[RW[P].Government]
    21582158      else if Flags and unConscripts <> 0 then
    21592159        UnitReport.FoodSupport := 1;
    21602160
    2161       if RW[p].Government <> gFundamentalism then
     2161      if RW[P].Government <> gFundamentalism then
    21622162      begin
    21632163        if GTestFlags and tfImmImprove = 0 then
     
    21742174        begin
    21752175          TerrOwner := RealMap[Loc] shr 27;
    2176           case RW[p].Government of
     2176          case RW[P].Government of
    21772177            gRepublic, gFuture:
    2178               if (TerrOwner <> p) and (TerrOwner < nPl) and
    2179                 (RW[p].Treaty[TerrOwner] < trAlliance) then
     2178              if (TerrOwner <> P) and (TerrOwner < nPl) and
     2179                (RW[P].Treaty[TerrOwner] < trAlliance) then
    21802180                UnitReport.ReportFlags := UnitReport.ReportFlags or urfDeployed;
    21812181            gDemocracy:
    2182               if (TerrOwner >= nPl) or (TerrOwner <> p) and
    2183                 (RW[p].Treaty[TerrOwner] < trAlliance) then
     2182              if (TerrOwner >= nPl) or (TerrOwner <> P) and
     2183                (RW[P].Treaty[TerrOwner] < trAlliance) then
    21842184                UnitReport.ReportFlags := UnitReport.ReportFlags or urfDeployed;
    21852185          end;
     
    21892189end;
    21902190
    2191 procedure SearchCity(Loc: integer; var p, cix: integer);
     2191procedure SearchCity(Loc: Integer; var P, cix: Integer);
    21922192// set p to supposed owner before call
    21932193var
    2194   i: integer;
     2194  I: Integer;
    21952195begin
    21962196  if RealMap[Loc] < nPl shl 27 then
    2197     p := RealMap[Loc] shr 27;
    2198   for i := 0 to nPl - 1 do
    2199   begin
    2200     if 1 shl p and GAlive <> 0 then
    2201       with RW[p] do
     2197    P := RealMap[Loc] shr 27;
     2198  for I := 0 to nPl - 1 do
     2199  begin
     2200    if 1 shl P and GAlive <> 0 then
     2201      with RW[P] do
    22022202      begin
    22032203        cix := nCity - 1;
    22042204        while (cix >= 0) and (City[cix].Loc <> Loc) do
    2205           dec(cix);
     2205          Dec(cix);
    22062206        if cix >= 0 then
    2207           exit;
    2208       end;
    2209     assert(i < nPl - 1);
    2210     p := (p + 1) mod nPl;
    2211   end;
    2212 end;
    2213 
    2214 procedure MakeCityInfo(p, cix: integer; var ci: TCityInfo);
    2215 begin
    2216   assert((p >= 0) and (p < nPl));
    2217   assert((cix >= 0) and (cix < RW[p].nCity));
    2218   with RW[p].City[cix] do
     2207          Exit;
     2208      end;
     2209    Assert(I < nPl - 1);
     2210    P := (P + 1) mod nPl;
     2211  end;
     2212end;
     2213
     2214procedure MakeCityInfo(P, cix: Integer; var ci: TCityInfo);
     2215begin
     2216  Assert((P >= 0) and (P < nPl));
     2217  Assert((cix >= 0) and (cix < RW[P].nCity));
     2218  with RW[P].City[cix] do
    22192219  begin
    22202220    ci.Loc := Loc;
    22212221    ci.ID := ID;
    2222     ci.Owner := p;
     2222    ci.Owner := P;
    22232223    ci.Size := Size;
    22242224    ci.Flags := 0;
    22252225    if Built[imPalace] > 0 then
    2226       inc(ci.Flags, ciCapital);
    2227     if (Built[imWalls] > 0) or (Continent[Loc] = GrWallContinent[p]) then
    2228       inc(ci.Flags, ciWalled);
     2226      Inc(ci.Flags, ciCapital);
     2227    if (Built[imWalls] > 0) or (Continent[Loc] = GrWallContinent[P]) then
     2228      Inc(ci.Flags, ciWalled);
    22292229    if Built[imCoastalFort] > 0 then
    2230       inc(ci.Flags, ciCoastalFort);
     2230      Inc(ci.Flags, ciCoastalFort);
    22312231    if Built[imMissileBat] > 0 then
    2232       inc(ci.Flags, ciMissileBat);
     2232      Inc(ci.Flags, ciMissileBat);
    22332233    if Built[imBunker] > 0 then
    2234       inc(ci.Flags, ciBunker);
     2234      Inc(ci.Flags, ciBunker);
    22352235    if Built[imSpacePort] > 0 then
    2236       inc(ci.Flags, ciSpacePort);
    2237   end;
    2238 end;
    2239 
    2240 procedure TellAboutModel(p, taOwner, tamix: integer);
    2241 var
    2242   i: integer;
    2243 begin
    2244   if (p = taOwner) or (Mode < moPlaying) then
    2245     exit;
    2246   i := 0;
    2247   while (i < RW[p].nEnemyModel) and ((RW[p].EnemyModel[i].Owner <> taOwner) or
    2248     (RW[p].EnemyModel[i].mix <> tamix)) do
    2249     inc(i);
    2250   if i = RW[p].nEnemyModel then
    2251     IntServer(sIntTellAboutModel + p shl 4, taOwner, tamix, nil^);
    2252 end;
    2253 
    2254 function emixSafe(p, taOwner, tamix: integer): integer;
    2255 begin
    2256   result := RWemix[p, taOwner, tamix];
    2257   if result < 0 then
     2236      Inc(ci.Flags, ciSpacePort);
     2237  end;
     2238end;
     2239
     2240procedure TellAboutModel(P, taOwner, tamix: Integer);
     2241var
     2242  I: Integer;
     2243begin
     2244  if (P = taOwner) or (Mode < moPlaying) then
     2245    Exit;
     2246  I := 0;
     2247  while (I < RW[P].nEnemyModel) and ((RW[P].EnemyModel[I].Owner <> taOwner) or
     2248    (RW[P].EnemyModel[I].mix <> tamix)) do
     2249    Inc(I);
     2250  if I = RW[P].nEnemyModel then
     2251    IntServer(sIntTellAboutModel + P shl 4, taOwner, tamix, nil^);
     2252end;
     2253
     2254function emixSafe(P, taOwner, tamix: Integer): Integer;
     2255begin
     2256  Result := RWemix[P, taOwner, tamix];
     2257  if Result < 0 then
    22582258  begin // sIntTellAboutModel comes too late
    2259     assert(Mode = moMovie);
    2260     result := $FFFF;
    2261   end;
    2262 end;
    2263 
    2264 procedure IntroduceEnemy(p1, p2: integer);
     2259    Assert(Mode = moMovie);
     2260    Result := $FFFF;
     2261  end;
     2262end;
     2263
     2264procedure IntroduceEnemy(p1, p2: Integer);
    22652265begin
    22662266  RW[p1].Treaty[p2] := trNone;
     
    22682268end;
    22692269
    2270 function DiscoverTile(Loc, p, pTell, Level: integer; EnableContact: boolean;
    2271   euix: integer = -2): boolean;
     2270function DiscoverTile(Loc, P, pTell, Level: Integer; EnableContact: Boolean;
     2271  euix: Integer = -2): Boolean;
    22722272// euix = -2: full discover
    22732273// euix = -1: unit and city only, append units in EnemyUn
    22742274// euix >= 0: unit and city only, replace EnemyUn[euix]
    22752275
    2276   procedure SetContact(p1, p2: integer);
     2276  procedure SetContact(p1, p2: Integer);
    22772277  begin
    22782278    if (Mode < moPlaying) or (p1 = p2) or (RW[p1].Treaty[p2] > trNoContact) then
    2279       exit;
     2279      Exit;
    22802280    IntServer(sIntTellAboutNation, p1, p2, nil^);
    22812281    // NewContact[p1,p2]:=true
     
    22832283
    22842284var
    2285   i, uix, cix, TerrOwner, TerrOwnerTreaty, Strength, Bonus, Cnt, pFoundCity,
    2286     cixFoundCity, MinLevel, Loc1, V8: integer;
     2285  I, uix, cix, TerrOwner, TerrOwnerTreaty, Strength, Bonus, Cnt, pFoundCity,
     2286    cixFoundCity, MinLevel, Loc1, V8: Integer;
    22872287  Tile, AddFlags: Cardinal;
    22882288  Adjacent: TVicinity8Loc;
     
    22902290  mox: ^TModel;
    22912291begin
    2292   result := false;
     2292  Result := False;
    22932293  with RW[pTell] do
    22942294  begin
     
    23062306        AddFlags := AddFlags or fGrWall;
    23072307      if (Mode = moPlaying) and ((Tile and (nPl shl 27) <> nPl shl 27) and
    2308         (pTell = p)) then
     2308        (pTell = P)) then
    23092309      begin // set fPeace flag?
    23102310        TerrOwner := Tile shr 27;
     
    23332333          unx := @RW[Occupant[Loc]].Un[uix];
    23342334          mox := @RW[Occupant[Loc]].Model[unx.mix];
    2335           assert((ZoCMap[Loc] <> 0) = (mox.Flags and mdZOC <> 0));
     2335          Assert((ZoCMap[Loc] <> 0) = (mox.Flags and mdZOC <> 0));
    23362336          if (mox.Cap[mcStealth] > 0) and (Tile and fCity = 0) and
    23372337            (Tile and fTerImp <> tiBase) then
     
    23492349            begin
    23502350              uix := nEnemyUn;
    2351               inc(nEnemyUn);
    2352               assert(nEnemyUn < neumax);
     2351              Inc(nEnemyUn);
     2352              Assert(nEnemyUn < neumax);
    23532353            end;
    23542354            MakeUnitInfo(Occupant[Loc], unx^, EnemyUn[uix]);
    23552355            if Cnt > 1 then
    23562356              EnemyUn[uix].Flags := EnemyUn[uix].Flags or unMulti;
    2357             if (mox.Flags and mdZOC <> 0) and (pTell = p) and
     2357            if (mox.Flags and mdZOC <> 0) and (pTell = P) and
    23582358              (Treaty[Occupant[Loc]] < trAlliance) then
    23592359            begin // set fInEnemyZoC flags of surrounding tiles
     
    23742374            end;
    23752375            // Level:=lObserveSuper; // don't discover unit twice
    2376             if (pTell = p) and
     2376            if (pTell = P) and
    23772377              ((Tile and fCity = 0) or (1 shl pTell and GAI <> 0)) then
    2378               result := true;
     2378              Result := True;
    23792379          end
    23802380          else
     
    23982398          while (cixFoundCity >= 0) and
    23992399            (RW[pFoundCity].City[cixFoundCity].Loc <> Loc) do
    2400             dec(cixFoundCity);
    2401           assert(cixFoundCity >= 0);
    2402           i := 0;
    2403           while (i < nEnemyCity) and (EnemyCity[i].Loc <> Loc) do
    2404             inc(i);
    2405           if i = nEnemyCity then
     2400            Dec(cixFoundCity);
     2401          Assert(cixFoundCity >= 0);
     2402          I := 0;
     2403          while (I < nEnemyCity) and (EnemyCity[I].Loc <> Loc) do
     2404            Inc(I);
     2405          if I = nEnemyCity then
    24062406          begin
    2407             inc(nEnemyCity);
    2408             assert(nEnemyCity < necmax);
    2409             EnemyCity[i].Status := 0;
    2410             EnemyCity[i].SavedStatus := 0;
    2411             if pTell = p then
    2412               result := true;
     2407            Inc(nEnemyCity);
     2408            Assert(nEnemyCity < necmax);
     2409            EnemyCity[I].Status := 0;
     2410            EnemyCity[I].SavedStatus := 0;
     2411            if pTell = P then
     2412              Result := True;
    24132413          end;
    2414           MakeCityInfo(pFoundCity, cixFoundCity, EnemyCity[i]);
     2414          MakeCityInfo(pFoundCity, cixFoundCity, EnemyCity[I]);
    24152415        end;
    24162416      end
     
    24212421
    24222422    if Map[Loc] and fTerrain = fUNKNOWN then
    2423       inc(Discovered[pTell]);
     2423      Inc(Discovered[pTell]);
    24242424    if euix >= -1 then
    24252425      Map[Loc] := Map[Loc] and not(fUnit or fCity or fOwned or fOwnZoCUnit) or
     
    24402440end;
    24412441
    2442 function Discover9(Loc, p, Level: integer;
    2443   TellAllied, EnableContact: boolean): boolean;
    2444 var
    2445   V9, Loc1, pTell, OldLevel: integer;
     2442function Discover9(Loc, P, Level: Integer;
     2443  TellAllied, EnableContact: Boolean): Boolean;
     2444var
     2445  V9, Loc1, pTell, OldLevel: Integer;
    24462446  Radius: TVicinity8Loc;
    24472447begin
    2448   assert((Mode > moLoading_Fast) or (RW[p].nEnemyUn = 0));
    2449   result := false;
     2448  Assert((Mode > moLoading_Fast) or (RW[P].nEnemyUn = 0));
     2449  Result := False;
    24502450  V8_to_Loc(Loc, Radius);
    24512451  for V9 := 0 to 8 do
     
    24592459      begin
    24602460        for pTell := 0 to nPl - 1 do
    2461           if (pTell = p) or (1 shl pTell and GAlive <> 0) and
    2462             (RW[p].Treaty[pTell] = trAlliance) then
     2461          if (pTell = P) or (1 shl pTell and GAlive <> 0) and
     2462            (RW[P].Treaty[pTell] = trAlliance) then
    24632463          begin
    24642464            OldLevel := ObserveLevel[Loc1] shr (2 * pTell) and 3;
    24652465            if Level > OldLevel then
    2466               result := DiscoverTile(Loc1, p, pTell, Level, EnableContact)
    2467                 or result;
     2466              Result := DiscoverTile(Loc1, P, pTell, Level, EnableContact)
     2467                or Result;
    24682468          end;
    24692469      end
    24702470      else
    24712471      begin
    2472         OldLevel := ObserveLevel[Loc1] shr (2 * p) and 3;
     2472        OldLevel := ObserveLevel[Loc1] shr (2 * P) and 3;
    24732473        if Level > OldLevel then
    2474           result := DiscoverTile(Loc1, p, p, Level, EnableContact) or result;
    2475       end;
    2476   end;
    2477 end;
    2478 
    2479 function Discover21(Loc, p, AdjacentLevel: integer;
    2480   TellAllied, EnableContact: boolean): boolean;
    2481 var
    2482   V21, Loc1, pTell, Level, OldLevel, AdjacentFlags: integer;
     2474          Result := DiscoverTile(Loc1, P, P, Level, EnableContact) or Result;
     2475      end;
     2476  end;
     2477end;
     2478
     2479function Discover21(Loc, P, AdjacentLevel: Integer;
     2480  TellAllied, EnableContact: Boolean): Boolean;
     2481var
     2482  V21, Loc1, pTell, Level, OldLevel, AdjacentFlags: Integer;
    24832483  Radius: TVicinity21Loc;
    24842484begin
    2485   assert((Mode > moLoading_Fast) or (RW[p].nEnemyUn = 0));
    2486   result := false;
     2485  Assert((Mode > moLoading_Fast) or (RW[P].nEnemyUn = 0));
     2486  Result := False;
    24872487  AdjacentFlags := $00267620 shr 1;
    24882488  V21_to_Loc(Loc, Radius);
     
    24992499      begin
    25002500        for pTell := 0 to nPl - 1 do
    2501           if (pTell = p) or (1 shl pTell and GAlive <> 0) and
    2502             (RW[p].Treaty[pTell] = trAlliance) then
     2501          if (pTell = P) or (1 shl pTell and GAlive <> 0) and
     2502            (RW[P].Treaty[pTell] = trAlliance) then
    25032503          begin
    25042504            OldLevel := ObserveLevel[Loc1] shr (2 * pTell) and 3;
    25052505            if Level > OldLevel then
    2506               result := DiscoverTile(Loc1, p, pTell, Level, EnableContact)
    2507                 or result;
     2506              Result := DiscoverTile(Loc1, P, pTell, Level, EnableContact)
     2507                or Result;
    25082508          end;
    25092509      end
    25102510      else
    25112511      begin
    2512         OldLevel := ObserveLevel[Loc1] shr (2 * p) and 3;
     2512        OldLevel := ObserveLevel[Loc1] shr (2 * P) and 3;
    25132513        if Level > OldLevel then
    2514           result := DiscoverTile(Loc1, p, p, Level, EnableContact) or result;
     2514          Result := DiscoverTile(Loc1, P, P, Level, EnableContact) or Result;
    25152515      end;
    25162516    end;
     
    25192519end;
    25202520
    2521 procedure DiscoverAll(p, Level: integer);
     2521procedure DiscoverAll(P, Level: Integer);
    25222522{ player p discovers complete playground (for supervisor) }
    25232523var
    2524   Loc, OldLevel: integer;
    2525 begin
    2526   assert((Mode > moLoading_Fast) or (RW[p].nEnemyUn = 0));
     2524  Loc, OldLevel: Integer;
     2525begin
     2526  Assert((Mode > moLoading_Fast) or (RW[P].nEnemyUn = 0));
    25272527  for Loc := 0 to MapSize - 1 do
    25282528  begin
    2529     OldLevel := ObserveLevel[Loc] shr (2 * p) and 3;
     2529    OldLevel := ObserveLevel[Loc] shr (2 * P) and 3;
    25302530    if Level > OldLevel then
    2531       DiscoverTile(Loc, p, p, Level, false);
    2532   end;
    2533 end;
    2534 
    2535 procedure DiscoverViewAreas(p: integer);
    2536 var
    2537   pTell, uix, cix, ecix, Loc, RealOwner: integer;
     2531      DiscoverTile(Loc, P, P, Level, False);
     2532  end;
     2533end;
     2534
     2535procedure DiscoverViewAreas(P: Integer);
     2536var
     2537  pTell, uix, cix, ecix, Loc, RealOwner: Integer;
    25382538  PModel: ^TModel;
    25392539begin // discover unit and city view areas
    25402540  for pTell := 0 to nPl - 1 do
    2541     if (pTell = p) or (RW[p].Treaty[pTell] = trAlliance) then
     2541    if (pTell = P) or (RW[P].Treaty[pTell] = trAlliance) then
    25422542    begin
    25432543      for uix := 0 to RW[pTell].nUn - 1 do
     
    25472547            PModel := @RW[pTell].Model[mix];
    25482548            if (PModel.Kind = mkDiplomat) or (PModel.Cap[mcSpy] > 0) then
    2549               Discover21(Loc, p, lObserveSuper, false, true)
     2549              Discover21(Loc, P, lObserveSuper, False, True)
    25502550            else if (PModel.Cap[mcRadar] + PModel.Cap[mcCarrier] > 0) or
    25512551              (PModel.Domain = dAir) then
    2552               Discover21(Loc, p, lObserveAll, false, false)
     2552              Discover21(Loc, P, lObserveAll, False, False)
    25532553            else if (RealMap[Loc] and fTerrain = fMountains) or
    25542554              (RealMap[Loc] and fTerImp = tiFort) or
    25552555              (RealMap[Loc] and fTerImp = tiBase) or (PModel.Cap[mcAcademy] > 0)
    25562556            then
    2557               Discover21(Loc, p, lObserveUnhidden, false,
     2557              Discover21(Loc, P, lObserveUnhidden, False,
    25582558                PModel.Domain = dGround)
    25592559            else
    2560               Discover9(Loc, p, lObserveUnhidden, false,
     2560              Discover9(Loc, P, lObserveUnhidden, False,
    25612561                PModel.Domain = dGround);
    25622562          end;
    25632563      for cix := 0 to RW[pTell].nCity - 1 do
    25642564        if RW[pTell].City[cix].Loc >= 0 then
    2565           Discover21(RW[pTell].City[cix].Loc, p, lObserveUnhidden, false, true);
     2565          Discover21(RW[pTell].City[cix].Loc, P, lObserveUnhidden, False, True);
    25662566      for ecix := 0 to RW[pTell].nEnemyCity - 1 do
    25672567      begin // players know territory, so no use in hiding city owner
     
    25822582end;
    25832583
    2584 function GetUnitStack(p, Loc: integer): integer;
    2585 var
    2586   uix: integer;
     2584function GetUnitStack(P, Loc: Integer): Integer;
     2585var
     2586  uix: Integer;
    25872587  unx: ^TUn;
    25882588begin
    2589   result := 0;
     2589  Result := 0;
    25902590  if Occupant[Loc] < 0 then
    2591     exit;
     2591    Exit;
    25922592  for uix := 0 to RW[Occupant[Loc]].nUn - 1 do
    25932593  begin
     
    25952595    if unx.Loc = Loc then
    25962596    begin
    2597       MakeUnitInfo(Occupant[Loc], unx^, RW[p].EnemyUn[RW[p].nEnemyUn + result]);
    2598       TellAboutModel(p, Occupant[Loc], unx.mix);
    2599       RW[p].EnemyUn[RW[p].nEnemyUn + result].emix :=
    2600         RWemix[p, Occupant[Loc], unx.mix];
    2601       inc(result);
    2602     end;
    2603   end;
    2604 end;
    2605 
    2606 procedure UpdateUnitMap(Loc: integer; CityChange: boolean = false);
     2597      MakeUnitInfo(Occupant[Loc], unx^, RW[P].EnemyUn[RW[P].nEnemyUn + Result]);
     2598      TellAboutModel(P, Occupant[Loc], unx.mix);
     2599      RW[P].EnemyUn[RW[P].nEnemyUn + Result].emix :=
     2600        RWemix[P, Occupant[Loc], unx.mix];
     2601      Inc(Result);
     2602    end;
     2603  end;
     2604end;
     2605
     2606procedure UpdateUnitMap(Loc: Integer; CityChange: Boolean = False);
    26072607// update maps and enemy units of all players after unit change
    26082608var
    2609   p, euix, OldLevel: integer;
     2609  P, euix, OldLevel: Integer;
    26102610  AddFlags, ClearFlags: Cardinal;
    26112611begin
    26122612  if (Mode = moLoading_Fast) and not CityChange then
    2613     exit;
    2614   for p := 0 to nPl - 1 do
    2615     if 1 shl p and (GAlive or GWatching) <> 0 then
    2616     begin
    2617       OldLevel := ObserveLevel[Loc] shr (2 * p) and 3;
     2613    Exit;
     2614  for P := 0 to nPl - 1 do
     2615    if 1 shl P and (GAlive or GWatching) <> 0 then
     2616    begin
     2617      OldLevel := ObserveLevel[Loc] shr (2 * P) and 3;
    26182618      if OldLevel > lNoObserve then
    26192619      begin
    2620         if RW[p].Map[Loc] and (fUnit or fOwned) = fUnit then
     2620        if RW[P].Map[Loc] and (fUnit or fOwned) = fUnit then
    26212621        begin
    26222622          // replace unit located here in EnemyUn
    26232623          // do not just set loc:=-1 because total number would be unlimited
    2624           euix := RW[p].nEnemyUn - 1;
     2624          euix := RW[P].nEnemyUn - 1;
    26252625          while euix >= 0 do
    26262626          begin
    2627             if RW[p].EnemyUn[euix].Loc = Loc then
     2627            if RW[P].EnemyUn[euix].Loc = Loc then
    26282628            begin
    2629               RW[p].EnemyUn[euix].Loc := -1;
     2629              RW[P].EnemyUn[euix].Loc := -1;
    26302630              Break;
    26312631            end;
    2632             dec(euix);
     2632            Dec(euix);
    26332633          end;
    2634           RW[p].Map[Loc] := RW[p].Map[Loc] and not fUnit
     2634          RW[P].Map[Loc] := RW[P].Map[Loc] and not fUnit
    26352635        end
    26362636        else
    26372637        begin // look for empty slot in EnemyUn
    2638           euix := RW[p].nEnemyUn - 1;
    2639           while (euix >= 0) and (RW[p].EnemyUn[euix].Loc >= 0) do
    2640             dec(euix);
     2638          euix := RW[P].nEnemyUn - 1;
     2639          while (euix >= 0) and (RW[P].EnemyUn[euix].Loc >= 0) do
     2640            Dec(euix);
    26412641        end;
    26422642        if (Occupant[Loc] < 0) and not CityChange then
     
    26452645          if RealMap[Loc] and fCity = 0 then
    26462646            ClearFlags := ClearFlags or fOwned;
    2647           RW[p].Map[Loc] := RW[p].Map[Loc] and not ClearFlags;
     2647          RW[P].Map[Loc] := RW[P].Map[Loc] and not ClearFlags;
    26482648        end
    2649         else if (Occupant[Loc] <> p) or CityChange then
     2649        else if (Occupant[Loc] <> P) or CityChange then
    26502650        begin // city or enemy unit update necessary, call DiscoverTile
    2651           ObserveLevel[Loc] := ObserveLevel[Loc] and not(3 shl (2 * p));
    2652           DiscoverTile(Loc, p, p, OldLevel, false, euix);
     2651          ObserveLevel[Loc] := ObserveLevel[Loc] and not(3 shl (2 * P));
     2652          DiscoverTile(Loc, P, P, OldLevel, False, euix);
    26532653        end
    26542654        else { if (Occupant[Loc]=p) and not CityChange then }
     
    26602660          else
    26612661            ClearFlags := ClearFlags or fOwnZoCUnit;
    2662           RW[p].Map[Loc] := RW[p].Map[Loc] and not ClearFlags or AddFlags;
    2663         end;
    2664       end;
    2665     end;
    2666 end;
    2667 
    2668 procedure RecalcV8ZoC(p, Loc: integer);
     2662          RW[P].Map[Loc] := RW[P].Map[Loc] and not ClearFlags or AddFlags;
     2663        end;
     2664      end;
     2665    end;
     2666end;
     2667
     2668procedure RecalcV8ZoC(P, Loc: Integer);
    26692669// recalculate fInEnemyZoC flags around single tile
    26702670var
    2671   V8, V8V8, Loc1, Loc2, p1, ObserveMask: integer;
     2671  V8, V8V8, Loc1, Loc2, p1, ObserveMask: Integer;
    26722672  Tile1: ^Cardinal;
    26732673  Adjacent, AdjacentAdjacent: TVicinity8Loc;
    26742674begin
    26752675  if Mode = moLoading_Fast then
    2676     exit;
    2677   ObserveMask := 3 shl (2 * p);
     2676    Exit;
     2677  ObserveMask := 3 shl (2 * P);
    26782678  V8_to_Loc(Loc, Adjacent);
    26792679  for V8 := 0 to 7 do
     
    26822682    if (Loc1 >= 0) and (Loc1 < MapSize) then
    26832683    begin
    2684       Tile1 := @RW[p].Map[Loc1];
     2684      Tile1 := @RW[P].Map[Loc1];
    26852685      Tile1^ := Tile1^ and not fInEnemyZoC;
    26862686      V8_to_Loc(Loc1, AdjacentAdjacent);
     
    26922692        begin
    26932693          p1 := Occupant[Loc2];
    2694           assert(p1 <> nPl);
    2695           if (p1 <> p) and (RW[p].Treaty[p1] < trAlliance) then
     2694          Assert(p1 <> nPl);
     2695          if (p1 <> P) and (RW[P].Treaty[p1] < trAlliance) then
    26962696          begin
    26972697            Tile1^ := Tile1^ or fInEnemyZoC;
     
    27042704end;
    27052705
    2706 procedure RecalcMapZoC(p: integer);
     2706procedure RecalcMapZoC(P: Integer);
    27072707// recalculate fInEnemyZoC flags for the whole map
    27082708var
    2709   Loc, Loc1, V8, p1, ObserveMask: integer;
     2709  Loc, Loc1, V8, p1, ObserveMask: Integer;
    27102710  Adjacent: TVicinity8Loc;
    27112711begin
    27122712  if Mode = moLoading_Fast then
    2713     exit;
    2714   MaskD(RW[p].Map^, MapSize, Cardinal(not Cardinal(fInEnemyZoC)));
    2715   ObserveMask := 3 shl (2 * p);
     2713    Exit;
     2714  MaskD(RW[P].Map^, MapSize, Cardinal(not Cardinal(fInEnemyZoC)));
     2715  ObserveMask := 3 shl (2 * P);
    27162716  for Loc := 0 to MapSize - 1 do
    27172717    if (ZoCMap[Loc] > 0) and (ObserveLevel[Loc] and ObserveMask <> 0) then
    27182718    begin
    27192719      p1 := Occupant[Loc];
    2720       assert(p1 <> nPl);
    2721       if (p1 <> p) and (RW[p].Treaty[p1] < trAlliance) then
     2720      Assert(p1 <> nPl);
     2721      if (p1 <> P) and (RW[P].Treaty[p1] < trAlliance) then
    27222722      begin // this non-allied enemy ZoC unit is known to this player -- set flags!
    27232723        V8_to_Loc(Loc, Adjacent);
     
    27262726          Loc1 := Adjacent[V8];
    27272727          if (Loc1 >= 0) and (Loc1 < MapSize) then
    2728             RW[p].Map[Loc1] := RW[p].Map[Loc1] or fInEnemyZoC;
    2729         end;
    2730       end;
    2731     end;
    2732 end;
    2733 
    2734 procedure RecalcPeaceMap(p: integer);
     2728            RW[P].Map[Loc1] := RW[P].Map[Loc1] or fInEnemyZoC;
     2729        end;
     2730      end;
     2731    end;
     2732end;
     2733
     2734procedure RecalcPeaceMap(P: Integer);
    27352735// recalculate fPeace flags for the whole map
    27362736var
    2737   Loc, p1: integer;
    2738   PeacePlayer: array [-1 .. nPl - 1] of boolean;
     2737  Loc, p1: Integer;
     2738  PeacePlayer: array [-1 .. nPl - 1] of Boolean;
    27392739begin
    27402740  if Mode <> moPlaying then
    2741     exit;
    2742   MaskD(RW[p].Map^, MapSize, Cardinal(not Cardinal(fPeace)));
     2741    Exit;
     2742  MaskD(RW[P].Map^, MapSize, Cardinal(not Cardinal(fPeace)));
    27432743  for p1 := -1 to nPl - 1 do
    2744     PeacePlayer[p1] := (p1 >= 0) and (p1 <> p) and (1 shl p1 and GAlive <> 0)
    2745       and (RW[p].Treaty[p1] in [trPeace, TrFriendlyContact]);
     2744    PeacePlayer[p1] := (p1 >= 0) and (p1 <> P) and (1 shl p1 and GAlive <> 0)
     2745      and (RW[P].Treaty[p1] in [trPeace, TrFriendlyContact]);
    27462746  for Loc := 0 to MapSize - 1 do
    2747     if PeacePlayer[RW[p].Territory[Loc]] then
    2748       RW[p].Map[Loc] := RW[p].Map[Loc] or fPeace;
     2747    if PeacePlayer[RW[P].Territory[Loc]] then
     2748      RW[P].Map[Loc] := RW[P].Map[Loc] or fPeace;
    27492749end;
    27502750
     
    27562756  BorderChanges: array [0 .. sIntExpandTerritory and $F - 1] of Cardinal;
    27572757
    2758 procedure ChangeTerritory(Loc, p: integer);
    2759 var
    2760   p1: integer;
    2761 begin
    2762   Assert(p >= 0); // no player's territory indicated by p=nPl
     2758procedure ChangeTerritory(Loc, P: Integer);
     2759var
     2760  p1: Integer;
     2761begin
     2762  Assert(P >= 0); // no player's territory indicated by p=nPl
    27632763  Dec(TerritoryCount[RealMap[Loc] shr 27]);
    2764   Inc(TerritoryCount[p]);
    2765   RealMap[Loc] := RealMap[Loc] and not($F shl 27) or Cardinal(p) shl 27;
    2766   if p = $F then
    2767     p := -1;
     2764  Inc(TerritoryCount[P]);
     2765  RealMap[Loc] := RealMap[Loc] and not($F shl 27) or Cardinal(P) shl 27;
     2766  if P = $F then
     2767    P := -1;
    27682768  for p1 := 0 to nPl - 1 do
    27692769    if 1 shl p1 and (GAlive or GWatching) <> 0 then
    27702770      if RW[p1].Map[Loc] and fTerrain <> fUNKNOWN then
    27712771      begin
    2772         RW[p1].Territory[Loc] := p;
    2773         if (p < nPl) and (p <> p1) and (1 shl p and GAlive <> 0) and
    2774           (RW[p1].Treaty[p] in [trPeace, TrFriendlyContact]) then
     2772        RW[p1].Territory[Loc] := P;
     2773        if (P < nPl) and (P <> p1) and (1 shl P and GAlive <> 0) and
     2774          (RW[p1].Treaty[P] in [trPeace, TrFriendlyContact]) then
    27752775          RW[p1].Map[Loc] := RW[p1].Map[Loc] or fPeace
    27762776        else
     
    27792779end;
    27802780
    2781 procedure ExpandTerritory(OriginLoc: integer);
    2782 var
    2783   i, dx, dy, dxMax, dyMax, Loc, NewOwner: integer;
     2781procedure ExpandTerritory(OriginLoc: Integer);
     2782var
     2783  I, dx, dy, dxMax, dyMax, Loc, NewOwner: Integer;
    27842784begin
    27852785  if OriginLoc = -1 then
    27862786    raise Exception.Create('Location error');
    2787   i := 0;
     2787  I := 0;
    27882788  dyMax := 0;
    27892789  while (dyMax + 1) + (dyMax + 1) shr 1 <= CountryRadius do
    2790     inc(dyMax);
     2790    Inc(dyMax);
    27912791  for dy := -dyMax to dyMax do
    27922792  begin
     
    27942794    while abs(dy) + (dxMax + 2) + abs(abs(dy) - (dxMax + 2)) shr 1 <=
    27952795      CountryRadius do
    2796       inc(dxMax, 2);
     2796      Inc(dxMax, 2);
    27972797    for dx := -dxMax to dxMax do
    27982798      if (dy + dx) and 1 = 0 then
    27992799      begin
    2800         NewOwner := BorderChanges[i div 8] shr (i mod 8 * 4) and $F;
     2800        NewOwner := BorderChanges[I div 8] shr (I mod 8 * 4) and $F;
    28012801        Loc := dLoc(OriginLoc, dx, dy);
    28022802        if (Loc >= 0) and (Cardinal(NewOwner) <> RealMap[Loc] shr 27) then
    28032803          ChangeTerritory(Loc, NewOwner);
    2804         inc(i);
    2805       end;
    2806   end;
    2807 end;
    2808 
    2809 procedure CheckBorders(OriginLoc, PlayerLosingCity: integer);
     2804        Inc(I);
     2805      end;
     2806  end;
     2807end;
     2808
     2809procedure CheckBorders(OriginLoc, PlayerLosingCity: Integer);
    28102810// OriginLoc: only changes in CountryRadius around this location possible,
    28112811// -1 for complete map, -2 for double-check (no more changes allowed)
     
    28132813// player's territory, -1 for full border recalculation
    28142814var
    2815   i, r, Loc, Loc1, dx, dy, p1, p2, cix, NewDist, dxMax, dyMax, OldOwner, V8: Integer;
     2815  I, R, Loc, Loc1, dx, dy, p1, p2, cix, NewDist, dxMax, dyMax, OldOwner, V8: Integer;
    28162816  NewOwner: Cardinal;
    28172817  Adjacent: TVicinity8Loc;
    2818   AtPeace: array [0 .. nPl, 0 .. nPl] of boolean;
     2818  AtPeace: array [0 .. nPl, 0 .. nPl] of Boolean;
    28192819  Country, FormerCountry, { to who's country a tile belongs }
    28202820  Dist, FormerDist, StolenDist: array [0 .. lxmax * lymax - 1] of ShortInt;
     
    28282828        StolenDist[RW[PlayerLosingCity].City[cix].Loc] := 0;
    28292829
    2830     for r := 1 to CountryRadius shr 1 do
    2831     begin
    2832       move(StolenDist, FormerDist, MapSize);
     2830    for R := 1 to CountryRadius shr 1 do
     2831    begin
     2832      Move(StolenDist, FormerDist, MapSize);
    28332833      for Loc := 0 to MapSize - 1 do
    28342834        if (FormerDist[Loc] <= CountryRadius - 2)
     
    28622862        end;
    28632863
    2864   for r := 1 to CountryRadius shr 1 do
    2865   begin
    2866     move(Country, FormerCountry, MapSize);
    2867     move(Dist, FormerDist, MapSize);
     2864  for R := 1 to CountryRadius shr 1 do
     2865  begin
     2866    Move(Country, FormerCountry, MapSize);
     2867    Move(Dist, FormerDist, MapSize);
    28682868    for Loc := 0 to MapSize - 1 do
    28692869      if (FormerDist[Loc] <= CountryRadius - 2) // use same conditions as above!
     
    28712871        (1 shl fShore + 1 shl fMountains + 1 shl fArctic) = 0) then
    28722872      begin
    2873         assert(FormerCountry[Loc] >= 0);
     2873        Assert(FormerCountry[Loc] >= 0);
    28742874        V8_to_Loc(Loc, Adjacent);
    28752875        for V8 := 0 to 7 do
     
    28862886  end;
    28872887
    2888   FillChar(AtPeace, SizeOf(AtPeace), false);
     2888  FillChar(AtPeace, SizeOf(AtPeace), False);
    28892889  for p1 := 0 to nPl - 1 do
    28902890    if 1 shl p1 and GAlive <> 0 then
     
    28922892        if (p2 <> p1) and (1 shl p2 and GAlive <> 0) and
    28932893          (RW[p1].Treaty[p2] >= trPeace) then
    2894           AtPeace[p1, p2] := true;
     2894          AtPeace[p1, p2] := True;
    28952895
    28962896  if OriginLoc >= 0 then
    28972897  begin // update area only
    2898     i := 0;
     2898    I := 0;
    28992899    FillChar(BorderChanges, SizeOf(BorderChanges), 0);
    29002900    dyMax := 0;
    29012901    while (dyMax + 1) + (dyMax + 1) shr 1 <= CountryRadius do
    2902       inc(dyMax);
     2902      Inc(dyMax);
    29032903    for dy := -dyMax to dyMax do
    29042904    begin
     
    29062906      while abs(dy) + (dxMax + 2) + abs(abs(dy) - (dxMax + 2)) shr 1 <=
    29072907        CountryRadius do
    2908         inc(dxMax, 2);
     2908        Inc(dxMax, 2);
    29092909      for dx := -dxMax to dxMax do
    29102910        if (dy + dx) and 1 = 0 then
     
    29222922              else
    29232923                ChangeTerritory(Loc, NewOwner);
    2924             BorderChanges[i shr 3] := BorderChanges[i shr 3] or
    2925               ((NewOwner shl ((i and 7) * 4)) and $ffffffff);
     2924            BorderChanges[I shr 3] := BorderChanges[I shr 3] or
     2925              ((NewOwner shl ((I and 7) * 4)) and $ffffffff);
    29262926          end;
    2927           inc(i);
     2927          Inc(I);
    29282928        end;
    29292929    end;
     
    29382938      then
    29392939      begin
    2940         assert(OriginLoc <> -2); // test if border saving works
     2940        Assert(OriginLoc <> -2); // test if border saving works
    29412941        ChangeTerritory(Loc, NewOwner);
    29422942      end;
     
    29472947end;
    29482948
    2949 procedure LogCheckBorders(p, cix, PlayerLosingCity: integer);
    2950 begin
    2951   CheckBorders(RW[p].City[cix].Loc, PlayerLosingCity);
    2952   IntServer(sIntExpandTerritory, p, cix, BorderChanges);
     2949procedure LogCheckBorders(P, cix, PlayerLosingCity: Integer);
     2950begin
     2951  CheckBorders(RW[P].City[cix].Loc, PlayerLosingCity);
     2952  IntServer(sIntExpandTerritory, P, cix, BorderChanges);
    29532953end;
    29542954
     
    29582958}
    29592959
    2960 procedure CreateUnit(p, mix: integer);
    2961 begin
    2962   with RW[p] do
     2960procedure CreateUnit(P, mix: Integer);
     2961begin
     2962  with RW[P] do
    29632963  begin
    29642964    Un[nUn].mix := mix;
    29652965    with Un[nUn] do
    29662966    begin
    2967       ID := UnBuilt[p];
    2968       inc(UnBuilt[p]);
     2967      ID := UnBuilt[P];
     2968      Inc(UnBuilt[P]);
    29692969      Status := 0;
    29702970      SavedStatus := 0;
    2971       inc(Model[mix].Built);
     2971      Inc(Model[mix].Built);
    29722972      Home := -1;
    29732973      Health := 100;
     
    29852985      Master := -1;
    29862986    end;
    2987     inc(nUn);
     2987    Inc(nUn);
    29882988  end
    29892989end;
    29902990
    2991 procedure FreeUnit(p, uix: integer);
     2991procedure FreeUnit(P, uix: Integer);
    29922992// loc or master should be set after call
    29932993// implementation is critical for loading performance, change carefully
    29942994var
    2995   Loc0, uix1: integer;
    2996   Occ, ZoC: boolean;
    2997 begin
    2998   with RW[p].Un[uix] do
     2995  Loc0, uix1: Integer;
     2996  Occ, ZoC: Boolean;
     2997begin
     2998  with RW[P].Un[uix] do
    29992999  begin
    30003000    Job := jNone;
     
    30043004  if Occupant[Loc0] >= 0 then
    30053005  begin
    3006     assert(Occupant[Loc0] = p);
    3007     Occ := false;
    3008     ZoC := false;
    3009     for uix1 := 0 to RW[p].nUn - 1 do
    3010       with RW[p].Un[uix1] do
     3006    Assert(Occupant[Loc0] = P);
     3007    Occ := False;
     3008    ZoC := False;
     3009    for uix1 := 0 to RW[P].nUn - 1 do
     3010      with RW[P].Un[uix1] do
    30113011        if (Loc = Loc0) and (Master < 0) and (uix1 <> uix) then
    30123012        begin
    3013           Occ := true;
    3014           if RW[p].Model[mix].Flags and mdZOC <> 0 then
     3013          Occ := True;
     3014          if RW[P].Model[mix].Flags and mdZOC <> 0 then
    30153015          begin
    3016             ZoC := true;
     3016            ZoC := True;
    30173017            Break;
    30183018          end;
     
    30253025end;
    30263026
    3027 procedure PlaceUnit(p, uix: integer);
    3028 begin
    3029   with RW[p].Un[uix] do
    3030   begin
    3031     Occupant[Loc] := p;
    3032     if RW[p].Model[mix].Flags and mdZOC <> 0 then
     3027procedure PlaceUnit(P, uix: Integer);
     3028begin
     3029  with RW[P].Un[uix] do
     3030  begin
     3031    Occupant[Loc] := P;
     3032    if RW[P].Model[mix].Flags and mdZOC <> 0 then
    30333033      ZoCMap[Loc] := 1;
    30343034  end;
    30353035end;
    30363036
    3037 procedure CountLost(p, mix, Enemy: integer);
    3038 begin
    3039   Inc(RW[p].Model[mix].Lost);
    3040   TellAboutModel(Enemy, p, mix);
    3041   Inc(Destroyed[Enemy, p, mix]);
    3042 end;
    3043 
    3044 procedure RemoveUnit(p, uix: integer; Enemy: integer = -1);
     3037procedure CountLost(P, mix, Enemy: Integer);
     3038begin
     3039  Inc(RW[P].Model[mix].Lost);
     3040  TellAboutModel(Enemy, P, mix);
     3041  Inc(Destroyed[Enemy, P, mix]);
     3042end;
     3043
     3044procedure RemoveUnit(P, uix: Integer; Enemy: Integer = -1);
    30453045// use enemy only from inside sMoveUnit if attack
    30463046var
    3047   uix1: integer;
    3048 begin
    3049   with RW[p].Un[uix] do
    3050   begin
    3051     assert((Loc >= 0) or (RW[p].Model[mix].Kind = mkDiplomat));
     3047  uix1: Integer;
     3048begin
     3049  with RW[P].Un[uix] do
     3050  begin
     3051    Assert((Loc >= 0) or (RW[P].Model[mix].Kind = mkDiplomat));
    30523052    // already freed when spy mission
    30533053    if Loc >= 0 then
    3054       FreeUnit(p, uix);
     3054      FreeUnit(P, uix);
    30553055    if Master >= 0 then
    3056       if RW[p].Model[mix].Domain = dAir then
    3057         dec(RW[p].Un[Master].AirLoad)
     3056      if RW[P].Model[mix].Domain = dAir then
     3057        Dec(RW[P].Un[Master].AirLoad)
    30583058      else
    3059         dec(RW[p].Un[Master].TroopLoad);
     3059        Dec(RW[P].Un[Master].TroopLoad);
    30603060    if (TroopLoad > 0) or (AirLoad > 0) then
    3061       for uix1 := 0 to RW[p].nUn - 1 do
    3062         if (RW[p].Un[uix1].Loc >= 0) and (RW[p].Un[uix1].Master = uix) then
     3061      for uix1 := 0 to RW[P].nUn - 1 do
     3062        if (RW[P].Un[uix1].Loc >= 0) and (RW[P].Un[uix1].Master = uix) then
    30633063        { unit mastered by removed unit -- remove too }
    30643064        begin
    3065           RW[p].Un[uix1].Loc := -1;
     3065          RW[P].Un[uix1].Loc := -1;
    30663066          if Enemy >= 0 then
    3067             CountLost(p, RW[p].Un[uix1].mix, Enemy);
     3067            CountLost(P, RW[P].Un[uix1].mix, Enemy);
    30683068        end;
    30693069    Loc := -1;
    30703070    if Enemy >= 0 then
    3071       CountLost(p, mix, Enemy);
    3072   end;
    3073 end;
    3074 
    3075 procedure RemoveUnit_UpdateMap(p, uix: integer);
     3071      CountLost(P, mix, Enemy);
     3072  end;
     3073end;
     3074
     3075procedure RemoveUnit_UpdateMap(P, uix: Integer);
    30763076var
    30773077  Loc0: Integer;
    30783078begin
    3079   Loc0 := RW[p].Un[uix].Loc;
    3080   RemoveUnit(p, uix);
     3079  Loc0 := RW[P].Un[uix].Loc;
     3080  RemoveUnit(P, uix);
    30813081  if Mode > moLoading_Fast then
    30823082    UpdateUnitMap(Loc0);
    30833083end;
    30843084
    3085 procedure RemoveAllUnits(p, Loc: integer; Enemy: integer = -1);
    3086 var
    3087   uix: integer;
    3088 begin
    3089   for uix := 0 to RW[p].nUn - 1 do
    3090     if RW[p].Un[uix].Loc = Loc then
     3085procedure RemoveAllUnits(P, Loc: Integer; Enemy: Integer = -1);
     3086var
     3087  uix: Integer;
     3088begin
     3089  for uix := 0 to RW[P].nUn - 1 do
     3090    if RW[P].Un[uix].Loc = Loc then
    30913091    begin
    30923092      if Enemy >= 0 then
    3093         CountLost(p, RW[p].Un[uix].mix, Enemy);
    3094       RW[p].Un[uix].Loc := -1;
     3093        CountLost(P, RW[P].Un[uix].mix, Enemy);
     3094      RW[P].Un[uix].Loc := -1;
    30953095    end;
    30963096  Occupant[Loc] := -1;
     
    30983098end;
    30993099
    3100 procedure RemoveDomainUnits(d, p, Loc: integer);
    3101 var
    3102   uix: integer;
    3103 begin
    3104   for uix := 0 to RW[p].nUn - 1 do
    3105     if (RW[p].Model[RW[p].Un[uix].mix].Domain = d) and (RW[p].Un[uix].Loc = Loc)
     3100procedure RemoveDomainUnits(D, P, Loc: Integer);
     3101var
     3102  uix: Integer;
     3103begin
     3104  for uix := 0 to RW[P].nUn - 1 do
     3105    if (RW[P].Model[RW[P].Un[uix].mix].Domain = D) and (RW[P].Un[uix].Loc = Loc)
    31063106    then
    3107       RemoveUnit(p, uix);
    3108 end;
    3109 
    3110 procedure FoundCity(p, FoundLoc: integer);
    3111 var
    3112   p1, cix1, V21, dx, dy: integer;
    3113 begin
    3114   if RW[p].nCity = ncmax then
    3115     exit;
    3116   inc(RW[p].nCity);
    3117   with RW[p].City[RW[p].nCity - 1] do
     3107      RemoveUnit(P, uix);
     3108end;
     3109
     3110procedure FoundCity(P, FoundLoc: Integer);
     3111var
     3112  p1, cix1, V21, dx, dy: Integer;
     3113begin
     3114  if RW[P].nCity = ncmax then
     3115    Exit;
     3116  Inc(RW[P].nCity);
     3117  with RW[P].City[RW[P].nCity - 1] do
    31183118  begin
    31193119    Size := 2;
     
    31313131    if UsedByCity[FoundLoc] >= 0 then
    31323132    begin { central tile is exploited - toggle in exploiting city }
    3133       p1 := p;
     3133      p1 := P;
    31343134      SearchCity(UsedByCity[FoundLoc], p1, cix1);
    31353135      dxdy(UsedByCity[FoundLoc], FoundLoc, dx, dy);
     
    31423142      (fTerrain or fSpecial or fRiver or nPl shl 27) or fCity;
    31433143
    3144     ChangeTerritory(Loc, p);
    3145   end;
    3146 end;
    3147 
    3148 procedure StealCity(p, cix: integer; SaveUnits: boolean);
    3149 var
    3150   i, j, uix1, cix1, nearest: integer;
    3151 begin
    3152   for i := 0 to nWonder - 1 do
    3153     if RW[p].City[cix].Built[i] = 1 then
    3154     begin
    3155       GWonder[i].EffectiveOwner := -1;
    3156       if i = woPyramids then
     3144    ChangeTerritory(Loc, P);
     3145  end;
     3146end;
     3147
     3148procedure StealCity(P, cix: Integer; SaveUnits: Boolean);
     3149var
     3150  I, J, uix1, cix1, nearest: Integer;
     3151begin
     3152  for I := 0 to nWonder - 1 do
     3153    if RW[P].City[cix].Built[I] = 1 then
     3154    begin
     3155      GWonder[I].EffectiveOwner := -1;
     3156      if I = woPyramids then
    31573157        FreeSlaves;
    3158       if i = woEiffel then // deactivate expired wonders
    3159         for j := 0 to nWonder - 1 do
    3160           if GWonder[j].EffectiveOwner = p then
    3161             CheckExpiration(j);
    3162     end;
    3163   for i := nWonder to nImp - 1 do
    3164     if (Imp[i].Kind <> ikCommon) and (RW[p].City[cix].Built[i] > 0) then
     3158      if I = woEiffel then // deactivate expired wonders
     3159        for J := 0 to nWonder - 1 do
     3160          if GWonder[J].EffectiveOwner = P then
     3161            CheckExpiration(J);
     3162    end;
     3163  for I := nWonder to nImp - 1 do
     3164    if (Imp[I].Kind <> ikCommon) and (RW[P].City[cix].Built[I] > 0) then
    31653165    begin { destroy national projects }
    3166       RW[p].NatBuilt[i] := 0;
    3167       if i = imGrWall then
    3168         GrWallContinent[p] := -1;
    3169     end;
    3170 
    3171   for uix1 := 0 to RW[p].nUn - 1 do
    3172     with RW[p].Un[uix1] do
     3166      RW[P].NatBuilt[I] := 0;
     3167      if I = imGrWall then
     3168        GrWallContinent[P] := -1;
     3169    end;
     3170
     3171  for uix1 := 0 to RW[P].nUn - 1 do
     3172    with RW[P].Un[uix1] do
    31733173      if (Loc >= 0) and (Home = cix) then
    31743174        if SaveUnits then
    31753175        begin // support units by nearest other city
    31763176          nearest := -1;
    3177           for cix1 := 0 to RW[p].nCity - 1 do
    3178             if (cix1 <> cix) and (RW[p].City[cix1].Loc >= 0) and
    3179               ((nearest < 0) or (Distance(RW[p].City[cix1].Loc, Loc) <
    3180               Distance(RW[p].City[nearest].Loc, Loc))) then
     3177          for cix1 := 0 to RW[P].nCity - 1 do
     3178            if (cix1 <> cix) and (RW[P].City[cix1].Loc >= 0) and
     3179              ((nearest < 0) or (Distance(RW[P].City[cix1].Loc, Loc) <
     3180              Distance(RW[P].City[nearest].Loc, Loc))) then
    31813181              nearest := cix1;
    31823182          Home := nearest;
    31833183        end
    31843184        else
    3185           RemoveUnit(p, uix1); // destroy supported units
    3186 end;
    3187 
    3188 procedure DestroyCity(p, cix: integer; SaveUnits: boolean);
    3189 var
    3190   i, V21: integer;
     3185          RemoveUnit(P, uix1); // destroy supported units
     3186end;
     3187
     3188procedure DestroyCity(P, cix: Integer; SaveUnits: Boolean);
     3189var
     3190  I, V21: Integer;
    31913191  Radius: TVicinity21Loc;
    31923192begin
    3193   StealCity(p, cix, SaveUnits);
    3194   with RW[p].City[cix] do begin
    3195     for i := 0 to nWonder - 1 do
    3196       if Built[i] > 0 then
    3197         GWonder[i].CityID := WonderDestroyed;
     3193  StealCity(P, cix, SaveUnits);
     3194  with RW[P].City[cix] do begin
     3195    for I := 0 to nWonder - 1 do
     3196      if Built[I] > 0 then
     3197        GWonder[I].CityID := WonderDestroyed;
    31983198    V21_to_Loc(Loc, Radius);
    31993199    for V21 := 1 to 26 do
     
    32013201        UsedByCity[Radius[V21]] := -1;
    32023202    RealMap[Loc] := RealMap[Loc] and not fCity;
    3203     Loc := -1
    3204   end;
    3205 end;
    3206 
    3207 procedure ChangeCityOwner(pOld, cixOld, pNew: integer);
    3208 var
    3209   i, j, cix1, Loc1, V21: integer;
     3203    Loc := -1;
     3204  end;
     3205end;
     3206
     3207procedure ChangeCityOwner(pOld, cixOld, pNew: Integer);
     3208var
     3209  I, J, cix1, Loc1, V21: Integer;
    32103210  Radius: TVicinity21Loc;
    32113211begin
    3212   inc(RW[pNew].nCity);
     3212  Inc(RW[pNew].nCity);
    32133213  RW[pNew].City[RW[pNew].nCity - 1] := RW[pOld].City[cixOld];
    3214   StealCity(pOld, cixOld, false);
     3214  StealCity(pOld, cixOld, False);
    32153215  RW[pOld].City[cixOld].Loc := -1;
    32163216  with RW[pNew].City[(RW[pNew].nCity - 1)] do
     
    32313231      begin
    32323232        Loc1 := Radius[V21];
    3233         assert((Loc1 >= 0) and (Loc1 < MapSize) and (UsedByCity[Loc1] = Loc));
     3233        Assert((Loc1 >= 0) and (Loc1 < MapSize) and (UsedByCity[Loc1] = Loc));
    32343234        if (ZoCMap[Loc1] > 0) and (Occupant[Loc1] <> pNew) and
    32353235          (RW[pNew].Treaty[Occupant[Loc1]] < trAlliance) then
     
    32433243    Built[imTownHall] := 0;
    32443244    Built[imCourt] := 0;
    3245     for i := nWonder to nImp - 1 do
    3246       if Imp[i].Kind <> ikCommon then
    3247         Built[i] := 0; { destroy national projects }
    3248     for i := 0 to nWonder - 1 do
    3249       if Built[i] = 1 then
     3245    for I := nWonder to nImp - 1 do
     3246      if Imp[I].Kind <> ikCommon then
     3247        Built[I] := 0; { destroy national projects }
     3248    for I := 0 to nWonder - 1 do
     3249      if Built[I] = 1 then
    32503250      begin // new wonder owner!
    3251         GWonder[i].EffectiveOwner := pNew;
    3252         if i = woEiffel then // reactivate expired wonders
    3253         begin
    3254           for j := 0 to nWonder - 1 do
    3255             if Imp[j].Expiration >= 0 then
     3251        GWonder[I].EffectiveOwner := pNew;
     3252        if I = woEiffel then // reactivate expired wonders
     3253        begin
     3254          for J := 0 to nWonder - 1 do
     3255            if Imp[J].Expiration >= 0 then
    32563256              for cix1 := 0 to (RW[pNew].nCity - 1) do
    3257                 if RW[pNew].City[cix1].Built[j] = 1 then
    3258                   GWonder[j].EffectiveOwner := pNew;
     3257                if RW[pNew].City[cix1].Built[J] = 1 then
     3258                  GWonder[J].EffectiveOwner := pNew;
    32593259        end
    32603260        else
    3261           CheckExpiration(i);
    3262         case i of
     3261          CheckExpiration(I);
     3262        case I of
    32633263          woLighthouse:
    32643264            CheckSpecialModels(pNew, preLighthouse);
     
    32743274    cix1 := RW[pNew].nEnemyCity - 1;
    32753275    while (cix1 >= 0) and (RW[pNew].EnemyCity[cix1].Loc <> Loc) do
    3276       dec(cix1);
    3277     assert(cix1 >= 0);
     3276      Dec(cix1);
     3277    Assert(cix1 >= 0);
    32783278    RW[pNew].EnemyCity[cix1].Loc := -1;
    32793279
     
    32823282end;
    32833283
    3284 procedure CompleteJob(p, Loc, Job: integer);
    3285 var
    3286   ChangedTerrain, p1: integer;
    3287 begin
    3288   assert(Job <> jCity);
     3284procedure CompleteJob(P, Loc, Job: Integer);
     3285var
     3286  ChangedTerrain, p1: Integer;
     3287begin
     3288  Assert(Job <> jCity);
    32893289  ChangedTerrain := -1;
    32903290  case Job of
     
    33283328        if not(RealMap[Loc] and fTerrain in TerrType_Canalable) then
    33293329        begin
    3330           RemoveDomainUnits(dSea, p, Loc);
     3330          RemoveDomainUnits(dSea, P, Loc);
    33313331          RealMap[Loc] := RealMap[Loc] and not fCanal;
    33323332        end;
     
    33403340      begin
    33413341        if RealMap[Loc] and fTerImp = tiBase then
    3342           RemoveDomainUnits(dAir, p, Loc);
     3342          RemoveDomainUnits(dAir, P, Loc);
    33433343        RealMap[Loc] := RealMap[Loc] and not fTerImp
    33443344      end
    33453345      else if RealMap[Loc] and fCanal <> 0 then
    33463346      begin
    3347         RemoveDomainUnits(dSea, p, Loc);
     3347        RemoveDomainUnits(dSea, P, Loc);
    33483348        RealMap[Loc] := RealMap[Loc] and not fCanal
    33493349      end
     
    33813381  ____________________________________________________________________
    33823382}
    3383 procedure GiveCivilReport(p, pAbout: integer);
    3384 begin
    3385   with RW[p].EnemyReport[pAbout]^ do
     3383procedure GiveCivilReport(P, pAbout: Integer);
     3384begin
     3385  with RW[P].EnemyReport[pAbout]^ do
    33863386  begin
    33873387    // general info
    33883388    TurnOfCivilReport := LastValidStat[pAbout];
    3389     move(RW[pAbout].Treaty, Treaty, SizeOf(Treaty));
     3389    Move(RW[pAbout].Treaty, Treaty, SizeOf(Treaty));
    33903390    Government := RW[pAbout].Government;
    33913391    Money := RW[pAbout].Money;
     
    33963396    if ResearchDone > 100 then
    33973397      ResearchDone := 100;
    3398     move(RW[pAbout].Tech, Tech, nAdv);
    3399   end;
    3400 end;
    3401 
    3402 procedure GiveMilReport(p, pAbout: integer);
    3403 var
    3404   uix, mix: integer;
    3405 begin
    3406   with RW[p].EnemyReport[pAbout]^ do
     3398    Move(RW[pAbout].Tech, Tech, nAdv);
     3399  end;
     3400end;
     3401
     3402procedure GiveMilReport(P, pAbout: Integer);
     3403var
     3404  uix, mix: Integer;
     3405begin
     3406  with RW[P].EnemyReport[pAbout]^ do
    34073407  begin
    34083408    TurnOfMilReport := LastValidStat[pAbout];
     
    34103410    for mix := 0 to RW[pAbout].nModel - 1 do
    34113411    begin
    3412       TellAboutModel(p, pAbout, mix);
     3412      TellAboutModel(P, pAbout, mix);
    34133413      UnCount[mix] := 0
    34143414    end;
    34153415    for uix := 0 to RW[pAbout].nUn - 1 do
    34163416      if RW[pAbout].Un[uix].Loc >= 0 then
    3417         inc(UnCount[RW[pAbout].Un[uix].mix]);
    3418   end;
    3419 end;
    3420 
    3421 procedure ShowPrice(pSender, pTarget, Price: integer);
     3417        Inc(UnCount[RW[pAbout].Un[uix].mix]);
     3418  end;
     3419end;
     3420
     3421procedure ShowPrice(pSender, pTarget, Price: Integer);
    34223422begin
    34233423  case Price and opMask of
     
    34343434end;
    34353435
    3436 function CopyCivilReport(pSender, pTarget, pAbout: integer): boolean;
    3437 var
    3438   i: integer;
     3436function CopyCivilReport(pSender, pTarget, pAbout: Integer): Boolean;
     3437var
     3438  I: Integer;
    34393439  rSender, rTarget: ^TEnemyReport;
    34403440begin // copy third nation civil report
    3441   result := false;
     3441  Result := False;
    34423442  if RW[pTarget].Treaty[pAbout] = trNoContact then
    34433443    IntroduceEnemy(pTarget, pAbout);
    3444   rSender := pointer(RW[pSender].EnemyReport[pAbout]);
    3445   rTarget := pointer(RW[pTarget].EnemyReport[pAbout]);
     3444  rSender := Pointer(RW[pSender].EnemyReport[pAbout]);
     3445  rTarget := Pointer(RW[pTarget].EnemyReport[pAbout]);
    34463446  if rSender.TurnOfCivilReport > rTarget.TurnOfCivilReport then
    34473447  begin // only if newer than current information
     
    34523452    rTarget.ResearchTech := rSender.ResearchTech;
    34533453    rTarget.ResearchDone := rSender.ResearchDone;
    3454     result := true;
    3455   end;
    3456   for i := 0 to nAdv - 1 do
    3457     if rTarget.Tech[i] < rSender.Tech[i] then
    3458     begin
    3459       rTarget.Tech[i] := rSender.Tech[i];
    3460       result := true;
    3461     end;
    3462 end;
    3463 
    3464 function CopyMilReport(pSender, pTarget, pAbout: integer): boolean;
    3465 var
    3466   mix: integer;
     3454    Result := True;
     3455  end;
     3456  for I := 0 to nAdv - 1 do
     3457    if rTarget.Tech[I] < rSender.Tech[I] then
     3458    begin
     3459      rTarget.Tech[I] := rSender.Tech[I];
     3460      Result := True;
     3461    end;
     3462end;
     3463
     3464function CopyMilReport(pSender, pTarget, pAbout: Integer): Boolean;
     3465var
     3466  mix: Integer;
    34673467  rSender, rTarget: ^TEnemyReport;
    34683468begin // copy third nation military report
    3469   result := false;
     3469  Result := False;
    34703470  if RW[pTarget].Treaty[pAbout] = trNoContact then
    34713471    IntroduceEnemy(pTarget, pAbout);
    3472   rSender := pointer(RW[pSender].EnemyReport[pAbout]);
    3473   rTarget := pointer(RW[pTarget].EnemyReport[pAbout]);
     3472  rSender := Pointer(RW[pSender].EnemyReport[pAbout]);
     3473  rTarget := Pointer(RW[pTarget].EnemyReport[pAbout]);
    34743474  if rSender.TurnOfMilReport > rTarget.TurnOfMilReport then
    34753475  begin // only if newer than current information
    34763476    rTarget.TurnOfMilReport := rSender.TurnOfMilReport;
    34773477    rTarget.nModelCounted := rSender.nModelCounted;
    3478     move(rSender.UnCount, rTarget.UnCount, 2 * rSender.nModelCounted);
     3478    Move(rSender.UnCount, rTarget.UnCount, 2 * rSender.nModelCounted);
    34793479    for mix := 0 to rTarget.nModelCounted - 1 do
    34803480      TellAboutModel(pTarget, pAbout, mix);
    3481     result := true;
    3482   end;
    3483 end;
    3484 
    3485 procedure CopyModel(pSender, pTarget, mix: integer);
    3486 var
    3487   i: integer;
     3481    Result := True;
     3482  end;
     3483end;
     3484
     3485procedure CopyModel(pSender, pTarget, mix: Integer);
     3486var
     3487  I: Integer;
    34883488  miSender, miTarget: TModelInfo;
    3489   ok: boolean;
     3489  ok: Boolean;
    34903490begin
    34913491  // only if target doesn't already have a model like this
    34923492  ok := RW[pTarget].nModel < nmmax;
    34933493  MakeModelInfo(pSender, mix, RW[pSender].Model[mix], miSender);
    3494   for i := 0 to RW[pTarget].nModel - 1 do
    3495   begin
    3496     MakeModelInfo(pTarget, i, RW[pTarget].Model[i], miTarget);
     3494  for I := 0 to RW[pTarget].nModel - 1 do
     3495  begin
     3496    MakeModelInfo(pTarget, I, RW[pTarget].Model[I], miTarget);
    34973497    if IsSameModel(miSender, miTarget) then
    3498       ok := false;
     3498      ok := False;
    34993499  end;
    35003500  if ok then
     
    35113511      Lost := 0;
    35123512    end;
    3513     inc(RW[pTarget].nModel);
    3514     inc(Researched[pTarget]);
     3513    Inc(RW[pTarget].nModel);
     3514    Inc(Researched[pTarget]);
    35153515    TellAboutModel(pSender, pTarget, RW[pTarget].nModel - 1);
    35163516  end;
    35173517end;
    35183518
    3519 procedure CopyMap(pSender, pTarget: integer);
    3520 var
    3521   Loc, i, cix: integer;
     3519procedure CopyMap(pSender, pTarget: Integer);
     3520var
     3521  Loc, I, cix: Integer;
    35223522  Tile: Cardinal;
    35233523begin
     
    35293529      if Tile and fCity <> 0 then
    35303530      begin
    3531         i := 0;
    3532         while (i < RW[pTarget].nEnemyCity) and
    3533           (RW[pTarget].EnemyCity[i].Loc <> Loc) do
    3534           inc(i);
    3535         if i = RW[pTarget].nEnemyCity then
    3536         begin
    3537           inc(RW[pTarget].nEnemyCity);
    3538           assert(RW[pTarget].nEnemyCity < necmax);
    3539           RW[pTarget].EnemyCity[i].Status := 0;
    3540           RW[pTarget].EnemyCity[i].SavedStatus := 0;
     3531        I := 0;
     3532        while (I < RW[pTarget].nEnemyCity) and
     3533          (RW[pTarget].EnemyCity[I].Loc <> Loc) do
     3534          Inc(I);
     3535        if I = RW[pTarget].nEnemyCity then
     3536        begin
     3537          Inc(RW[pTarget].nEnemyCity);
     3538          Assert(RW[pTarget].nEnemyCity < necmax);
     3539          RW[pTarget].EnemyCity[I].Status := 0;
     3540          RW[pTarget].EnemyCity[I].SavedStatus := 0;
    35413541        end;
    35423542        if Tile and fOwned <> 0 then
     
    35443544          cix := RW[pSender].nCity - 1;
    35453545          while (cix >= 0) and (RW[pSender].City[cix].Loc <> Loc) do
    3546             dec(cix);
    3547           MakeCityInfo(pSender, cix, RW[pTarget].EnemyCity[i]);
     3546            Dec(cix);
     3547          MakeCityInfo(pSender, cix, RW[pTarget].EnemyCity[I]);
    35483548        end
    35493549        else // city not owned by sender -- copy old info
     
    35513551          cix := RW[pSender].nEnemyCity - 1;
    35523552          while (cix >= 0) and (RW[pSender].EnemyCity[cix].Loc <> Loc) do
    3553             dec(cix);
    3554           RW[pTarget].EnemyCity[i] := RW[pSender].EnemyCity[cix];
     3553            Dec(cix);
     3554          RW[pTarget].EnemyCity[I] := RW[pSender].EnemyCity[cix];
    35553555        end;
    35563556      end
     
    35663566
    35673567      if RW[pTarget].Map[Loc] and fTerrain = fUNKNOWN then
    3568         inc(Discovered[pTarget]);
     3568        Inc(Discovered[pTarget]);
    35693569      RW[pTarget].Map[Loc] := RW[pTarget].Map[Loc] and fInEnemyZoC
    35703570      // always preserve this flag!
     
    35823582end;
    35833583
    3584 function PayPrice(pSender, pTarget, Price: integer; execute: boolean): boolean;
    3585 var
    3586   pSubject, i, n, NewTreaty: integer;
    3587 begin
    3588   result := true;
     3584function PayPrice(pSender, pTarget, Price: Integer; execute: Boolean): Boolean;
     3585var
     3586  pSubject, I, N, NewTreaty: Integer;
     3587begin
     3588  Result := True;
    35893589  case Price and opMask of
    35903590    opCivilReport: // + turn + concerned player shl 16
     
    35923592        pSubject := Price shr 16 and $F;
    35933593        if pTarget = pSubject then
    3594           result := false
     3594          Result := False
    35953595        else if pSender = pSubject then
    35963596        begin
     
    35993599        end
    36003600        else if RW[pSender].EnemyReport[pSubject].TurnOfCivilReport < 0 then
    3601           result := false
     3601          Result := False
    36023602        else if execute then
    36033603          CopyCivilReport(pSender, pTarget, pSubject);
     
    36073607        pSubject := Price shr 16 and $F;
    36083608        if pTarget = pSubject then
    3609           result := false
     3609          Result := False
    36103610        else if pSender = pSubject then
    36113611        begin
     
    36143614        end
    36153615        else if RW[pSender].EnemyReport[pSubject].TurnOfMilReport < 0 then
    3616           result := false
     3616          Result := False
    36173617        else if execute then
    36183618          CopyMilReport(pSender, pTarget, pSubject);
     
    36293629        begin // agreed treaty end
    36303630          if execute then
    3631             CancelTreaty(pSender, pTarget, false);
     3631            CancelTreaty(pSender, pTarget, False);
    36323632        end
    36333633        else
     
    36403640            NewTreaty := trPeace;
    36413641          if NewTreaty < 0 then
    3642             result := false
     3642            Result := False
    36433643          else if execute then
    36443644          begin
    3645             assert(NewTreaty > RW[pSender].Treaty[pTarget]);
     3645            Assert(NewTreaty > RW[pSender].Treaty[pTarget]);
    36463646            RW[pSender].Treaty[pTarget] := NewTreaty;
    36473647            RW[pTarget].Treaty[pSender] := NewTreaty;
     
    36723672    opShipParts: // + number + part type shl 16
    36733673      begin
    3674         n := Price and $FFFF; // number
    3675         i := Price shr 16 and $F; // type
    3676         if (i < nShipPart) and (GShip[pSender].Parts[i] >= n) then
     3674        N := Price and $FFFF; // number
     3675        I := Price shr 16 and $F; // type
     3676        if (I < nShipPart) and (GShip[pSender].Parts[I] >= N) then
    36773677        begin
    36783678          if execute then
    36793679          begin
    3680             dec(GShip[pSender].Parts[i], n);
    3681             RW[pSender].Ship[pSender].Parts[i] := GShip[pSender].Parts[i];
    3682             RW[pTarget].Ship[pSender].Parts[i] := GShip[pSender].Parts[i];
     3680            Dec(GShip[pSender].Parts[I], N);
     3681            RW[pSender].Ship[pSender].Parts[I] := GShip[pSender].Parts[I];
     3682            RW[pTarget].Ship[pSender].Parts[I] := GShip[pSender].Parts[I];
    36833683            if RW[pTarget].NatBuilt[imSpacePort] > 0 then
    36843684            begin // space ship control requires space port
    3685               inc(GShip[pTarget].Parts[i], n);
    3686               RW[pSender].Ship[pTarget].Parts[i] := GShip[pTarget].Parts[i];
    3687               RW[pTarget].Ship[pTarget].Parts[i] := GShip[pTarget].Parts[i];
     3685              Inc(GShip[pTarget].Parts[I], N);
     3686              RW[pSender].Ship[pTarget].Parts[I] := GShip[pTarget].Parts[I];
     3687              RW[pTarget].Ship[pTarget].Parts[I] := GShip[pTarget].Parts[I];
    36883688            end;
    36893689          end;
    36903690        end
    36913691        else
    3692           result := false;
     3692          Result := False;
    36933693      end;
    36943694    opMoney: // + value
     
    36983698        if execute then
    36993699        begin
    3700           dec(RW[pSender].Money, Price - opMoney);
    3701           inc(RW[pTarget].Money, Price - opMoney);
     3700          Dec(RW[pSender].Money, Price - opMoney);
     3701          Inc(RW[pTarget].Money, Price - opMoney);
    37023702        end;
    37033703      end
    37043704      else
    3705         result := false;
     3705        Result := False;
    37063706    opTribute: // + value
    37073707      if execute then
     
    37183718      end
    37193719      else
    3720         result := false;
     3720        Result := False;
    37213721    opAllTech:
    37223722      if execute then
    3723         for i := 0 to nAdv - 1 do
    3724           if (RW[pSender].Tech[i] >= tsApplicable) and
    3725             (RW[pTarget].Tech[i] = tsNA) then
     3723        for I := 0 to nAdv - 1 do
     3724          if (RW[pSender].Tech[I] >= tsApplicable) and
     3725            (RW[pTarget].Tech[I] = tsNA) then
    37263726          begin
    3727             SeeTech(pTarget, i);
    3728             RW[pSender].EnemyReport[pTarget].Tech[i] := tsSeen;
    3729             RW[pTarget].EnemyReport[pSender].Tech[i] := tsApplicable;
     3727            SeeTech(pTarget, I);
     3728            RW[pSender].EnemyReport[pTarget].Tech[I] := tsSeen;
     3729            RW[pTarget].EnemyReport[pSender].Tech[I] := tsApplicable;
    37303730          end;
    37313731    opModel: // + model index
     
    37363736      end
    37373737      else
    3738         result := false;
     3738        Result := False;
    37393739    opAllModel:
    37403740      if execute then
    3741         for i := 0 to RW[pSender].nModel - 1 do
    3742         begin
    3743           TellAboutModel(pTarget, pSender, i);
    3744           CopyModel(pSender, pTarget, i);
     3741        for I := 0 to RW[pSender].nModel - 1 do
     3742        begin
     3743          TellAboutModel(pTarget, pSender, I);
     3744          CopyModel(pSender, pTarget, I);
    37453745        end;
    37463746    { opCity: // + city ID
    37473747      begin
    3748       result:=false
     3748      Result:=False
    37493749      end; }
    37503750  end;
    37513751end;
    37523752
    3753 procedure CancelTreaty(p, pWith: integer; DecreaseCredibility: boolean);
     3753procedure CancelTreaty(P, pWith: Integer; DecreaseCredibility: Boolean);
    37543754// side effect: PeaceEnded := bitarray of players with which peace treaty was canceled
    37553755var
    3756   p1, OldTreaty: integer;
    3757 begin
    3758   OldTreaty := RW[p].Treaty[pWith];
     3756  p1, OldTreaty: Integer;
     3757begin
     3758  OldTreaty := RW[P].Treaty[pWith];
    37593759  PeaceEnded := 0;
    37603760  if OldTreaty >= trPeace then
    3761     RW[p].LastCancelTreaty[pWith] := GTurn;
     3761    RW[P].LastCancelTreaty[pWith] := GTurn;
    37623762  if DecreaseCredibility then
    37633763  begin
     
    37653765      trPeace:
    37663766        begin
    3767           RW[p].Credibility := RW[p].Credibility shr 1;
    3768           if RW[p].MaxCredibility > 0 then
    3769             dec(RW[p].MaxCredibility, 10);
    3770           if RW[p].Credibility > RW[p].MaxCredibility then
    3771             RW[p].Credibility := RW[p].MaxCredibility;
     3767          RW[P].Credibility := RW[P].Credibility shr 1;
     3768          if RW[P].MaxCredibility > 0 then
     3769            Dec(RW[P].MaxCredibility, 10);
     3770          if RW[P].Credibility > RW[P].MaxCredibility then
     3771            RW[P].Credibility := RW[P].MaxCredibility;
    37723772        end;
    37733773      trAlliance:
    3774         RW[p].Credibility := RW[p].Credibility * 3 div 4;
    3775     end;
    3776     RW[pWith].EnemyReport[p].Credibility := RW[p].Credibility;
     3774        RW[P].Credibility := RW[P].Credibility * 3 div 4;
     3775    end;
     3776    RW[pWith].EnemyReport[P].Credibility := RW[P].Credibility;
    37773777  end;
    37783778
     
    37803780  begin
    37813781    for p1 := 0 to nPl - 1 do
    3782       if (p1 = pWith) or DecreaseCredibility and (p1 <> p) and
    3783         (RW[pWith].Treaty[p1] = trAlliance) and (RW[p].Treaty[p1] >= trPeace)
     3782      if (p1 = pWith) or DecreaseCredibility and (p1 <> P) and
     3783        (RW[pWith].Treaty[p1] = trAlliance) and (RW[P].Treaty[p1] >= trPeace)
    37843784      then
    37853785      begin
    3786         RW[p].Treaty[p1] := trNone;
    3787         RW[p1].Treaty[p] := trNone;
    3788         RW[p].EvaStart[p1] := -PeaceEvaTurns - 1;
    3789         RW[p1].EvaStart[p] := -PeaceEvaTurns - 1;
    3790         inc(PeaceEnded, 1 shl p1);
     3786        RW[P].Treaty[p1] := trNone;
     3787        RW[p1].Treaty[P] := trNone;
     3788        RW[P].EvaStart[p1] := -PeaceEvaTurns - 1;
     3789        RW[p1].EvaStart[P] := -PeaceEvaTurns - 1;
     3790        Inc(PeaceEnded, 1 shl p1);
    37913791      end;
    37923792    CheckBorders(-1);
    37933793    if (Mode > moLoading_Fast) and (PeaceEnded > 0) then
    3794       RecalcMapZoC(p);
     3794      RecalcMapZoC(P);
    37953795  end
    37963796  else
    37973797  begin
    3798     RW[p].Treaty[pWith] := OldTreaty - 1;
    3799     RW[pWith].Treaty[p] := OldTreaty - 1;
     3798    RW[P].Treaty[pWith] := OldTreaty - 1;
     3799    RW[pWith].Treaty[P] := OldTreaty - 1;
    38003800    if OldTreaty = TrFriendlyContact then
    38013801    begin // necessary for loading
    3802       GiveCivilReport(p, pWith);
    3803       GiveCivilReport(pWith, p);
     3802      GiveCivilReport(P, pWith);
     3803      GiveCivilReport(pWith, P);
    38043804    end
    38053805    else if OldTreaty = trAlliance then
    38063806    begin // necessary for loading
    3807       GiveMilReport(p, pWith);
    3808       GiveMilReport(pWith, p);
     3807      GiveMilReport(P, pWith);
     3808      GiveMilReport(pWith, P);
    38093809    end;
    38103810    if (Mode > moLoading_Fast) and (OldTreaty = trAlliance) then
    38113811    begin
    3812       RecalcMapZoC(p);
     3812      RecalcMapZoC(P);
    38133813      RecalcMapZoC(pWith);
    38143814    end;
     
    38163816  if OldTreaty in [trPeace, trAlliance] then
    38173817  begin
    3818     RecalcPeaceMap(p);
     3818    RecalcPeaceMap(P);
    38193819    RecalcPeaceMap(pWith);
    38203820  end;
    38213821end;
    38223822
    3823 function DoSpyMission(p, pCity, cix, Mission: integer): Cardinal;
    3824 var
    3825   p1: integer;
    3826 begin
    3827   result := 0;
     3823function DoSpyMission(P, pCity, cix, Mission: Integer): Cardinal;
     3824var
     3825  p1: Integer;
     3826begin
     3827  Result := 0;
    38283828  case Mission of
    38293829    smSabotageProd:
     
    38323832    smStealMap:
    38333833      begin
    3834         CopyMap(pCity, p);
    3835         RecalcPeaceMap(p);
     3834        CopyMap(pCity, P);
     3835        RecalcPeaceMap(P);
    38363836      end;
    38373837    smStealCivilReport:
    38383838      begin
    3839         if RW[p].Treaty[pCity] = trNoContact then
    3840           IntroduceEnemy(p, pCity);
    3841         GiveCivilReport(p, pCity);
     3839        if RW[P].Treaty[pCity] = trNoContact then
     3840          IntroduceEnemy(P, pCity);
     3841        GiveCivilReport(P, pCity);
    38423842      end;
    38433843    smStealMilReport:
    38443844      begin
    3845         if RW[p].Treaty[pCity] = trNoContact then
    3846           IntroduceEnemy(p, pCity);
    3847         GiveMilReport(p, pCity);
     3845        if RW[P].Treaty[pCity] = trNoContact then
     3846          IntroduceEnemy(P, pCity);
     3847        GiveMilReport(P, pCity);
    38483848      end;
    38493849    smStealForeignReports:
    38503850      begin
    38513851        for p1 := 0 to nPl - 1 do
    3852           if (p1 <> p) and (p1 <> pCity) and (RW[pCity].EnemyReport[p1] <> nil)
     3852          if (p1 <> P) and (p1 <> pCity) and (RW[pCity].EnemyReport[p1] <> nil)
    38533853          then
    38543854          begin
    38553855            if RW[pCity].EnemyReport[p1].TurnOfCivilReport >= 0 then
    3856               if CopyCivilReport(pCity, p, p1) then
    3857                 result := result or (1 shl (2 * p1));
     3856              if CopyCivilReport(pCity, P, p1) then
     3857                Result := Result or (1 shl (2 * p1));
    38583858            if RW[pCity].EnemyReport[p1].TurnOfMilReport >= 0 then
    3859               if CopyMilReport(pCity, p, p1) then
    3860                 result := result or (2 shl (2 * p1));
     3859              if CopyMilReport(pCity, P, p1) then
     3860                Result := Result or (2 shl (2 * p1));
    38613861          end;
    38623862      end;
     
    38683868  ____________________________________________________________________
    38693869}
    3870 procedure ClearTestFlags(ClearFlags: integer);
    3871 var
    3872   p1: integer;
     3870procedure ClearTestFlags(ClearFlags: Integer);
     3871var
     3872  p1: Integer;
    38733873begin
    38743874  GTestFlags := GTestFlags and (not ClearFlags or tfTested or tfAllTechs or
     
    38793879end;
    38803880
    3881 procedure SetTestFlags(p, SetFlags: integer);
    3882 var
    3883   i, p1, p2, MoreFlags: integer;
     3881procedure SetTestFlags(P, SetFlags: Integer);
     3882var
     3883  I, p1, p2, MoreFlags: Integer;
    38843884begin
    38853885  MoreFlags := SetFlags and not GTestFlags;
     
    39053905      if 1 shl p1 and GAlive <> 0 then
    39063906      begin
    3907         for i := 0 to nAdv - 1 do // give all techs to player p1
    3908           if not(i in FutureTech) and (RW[p1].Tech[i] < tsApplicable) then
     3907        for I := 0 to nAdv - 1 do // give all techs to player p1
     3908          if not(I in FutureTech) and (RW[p1].Tech[I] < tsApplicable) then
    39093909          begin
    3910             RW[p1].Tech[i] := tsCheat;
    3911             CheckSpecialModels(p1, i);
     3910            RW[p1].Tech[I] := tsCheat;
     3911            CheckSpecialModels(p1, I);
    39123912          end;
    39133913        for p2 := 0 to nPl - 1 do
    39143914          if (p2 <> p1) and (1 shl p2 and (GAlive or GWatching) <> 0) then
    3915             for i := 1 to 3 do
    3916               if RW[p2].EnemyReport[p1].Tech[AgePreq[i]] < tsApplicable then
    3917                 RW[p2].EnemyReport[p1].Tech[AgePreq[i]] := tsCheat;
     3915            for I := 1 to 3 do
     3916              if RW[p2].EnemyReport[p1].Tech[AgePreq[I]] < tsApplicable then
     3917                RW[p2].EnemyReport[p1].Tech[AgePreq[I]] := tsCheat;
    39183918      end;
    39193919    end;
     
    39213921  if MoreFlags and tfUncover <> 0 then
    39223922  begin
    3923     DiscoverAll(p, lObserveSuper);
     3923    DiscoverAll(P, lObserveSuper);
    39243924    for p1 := 0 to nPl - 1 do
    39253925      if 1 shl p1 and GAlive <> 0 then
    39263926      begin
    39273927        ResourceMask[p1] := $FFFFFFFF;
    3928         if p1 <> p then
    3929         begin
    3930           GiveCivilReport(p, p1);
    3931           GiveMilReport(p, p1);
     3928        if p1 <> P then
     3929        begin
     3930          GiveCivilReport(P, p1);
     3931          GiveMilReport(P, p1);
    39323932        end;
    39333933      end;
     
    39393939  ____________________________________________________________________
    39403940}
    3941 procedure IntServer(Command, Player, Subject: integer; var Data);
    3942 var
    3943   i, p1: integer;
     3941procedure IntServer(Command, Player, Subject: Integer; var Data);
     3942var
     3943  I, p1: Integer;
    39443944begin
    39453945  if Mode = moPlaying then
     
    39513951      begin
    39523952{$IFDEF TEXTLOG}CmdInfo := Format('IntTellAboutNation P%d+P%d', [Player, Subject]); {$ENDIF}
    3953         assert((Player >= 0) and (Player < nPl) and (Subject >= 0) and
     3953        Assert((Player >= 0) and (Player < nPl) and (Subject >= 0) and
    39543954          (Subject < nPl));
    39553955        IntroduceEnemy(Player, Subject);
     
    39593959      begin
    39603960{$IFDEF TEXTLOG}CmdInfo := Format('IntHaveContact P%d+P%d', [Player, Subject]); {$ENDIF}
    3961         assert(RW[Player].Treaty[Subject] > trNoContact);
     3961        Assert(RW[Player].Treaty[Subject] > trNoContact);
    39623962        RW[Player].EnemyReport[Subject].TurnOfContact := GTurn;
    39633963        RW[Subject].EnemyReport[Player].TurnOfContact := GTurn;
     
    39813981        p1 := (Command - sIntTellAboutModel) shr 4; // told player
    39823982{$IFDEF TEXTLOG}CmdInfo := Format('IntTellAboutModel P%d about P%d Mod%d', [p1, Player, Subject]); {$ENDIF}
    3983         assert((Player >= 0) and (Player < nPl));
    3984         assert((Subject >= 0) and (Subject < RW[Player].nModel));
     3983        Assert((Player >= 0) and (Player < nPl));
     3984        Assert((Subject >= 0) and (Subject < RW[Player].nModel));
    39853985        MakeModelInfo(Player, Subject, RW[Player].Model[Subject],
    39863986          RW[p1].EnemyModel[RW[p1].nEnemyModel]);
    39873987        RWemix[p1, Player, Subject] := RW[p1].nEnemyModel;
    3988         inc(RW[p1].nEnemyModel);
    3989         assert(RW[p1].nEnemyModel < nemmax);
     3988        Inc(RW[p1].nEnemyModel);
     3989        Assert(RW[p1].nEnemyModel < nemmax);
    39903990      end;
    39913991
    39923992    sIntDiscoverZOC:
    39933993      begin
    3994 {$IFDEF TEXTLOG}CmdInfo := Format('IntDiscoverZOC P%d Loc%d', [Player, integer(Data)]); {$ENDIF}
    3995         Discover9(integer(Data), Player, lObserveUnhidden, true, false);
     3994{$IFDEF TEXTLOG}CmdInfo := Format('IntDiscoverZOC P%d Loc%d', [Player, Integer(Data)]); {$ENDIF}
     3995        Discover9(Integer(Data), Player, lObserveUnhidden, True, False);
    39963996      end;
    39973997
     
    40004000      begin
    40014001{$IFDEF TEXTLOG}CmdInfo := Format('IntExpandTerritory P%d Loc%d', [Player, RW[Player].City[Subject].Loc]); {$ENDIF}
    4002         move(Data, BorderChanges, SizeOf(BorderChanges));
     4002        Move(Data, BorderChanges, SizeOf(BorderChanges));
    40034003        ExpandTerritory(RW[Player].City[Subject].Loc);
    40044004      end;
     
    40074007      with RW[Player].City[Subject] do
    40084008      begin
    4009 {$IFDEF TEXTLOG}CmdInfo := Format('IntBuyMaterial P%d Loc%d Cost%d', [Player, Loc, integer(Data)]); {$ENDIF}
    4010         dec(RW[Player].Money, integer(Data));
     4009{$IFDEF TEXTLOG}CmdInfo := Format('IntBuyMaterial P%d Loc%d Cost%d', [Player, Loc, Integer(Data)]); {$ENDIF}
     4010        Dec(RW[Player].Money, Integer(Data));
    40114011        if (GWonder[woMich].EffectiveOwner = Player) and (Project and cpImp <> 0)
    40124012        then
    4013           inc(Prod, integer(Data) div 2)
     4013          Inc(Prod, Integer(Data) div 2)
    40144014        else
    4015           inc(Prod, integer(Data) div 4);
     4015          Inc(Prod, Integer(Data) div 4);
    40164016        if Project0 and not cpAuto <> Project and not cpAuto then
    40174017          Project0 := Project;
     
    40224022      begin
    40234023{$IFDEF TEXTLOG}CmdInfo := Format('IntPayPrices P%d+P%d', [Player, Subject]); {$ENDIF}
    4024         for i := 0 to TOffer(Data).nDeliver - 1 do
    4025           PayPrice(Player, Subject, TOffer(Data).Price[i], true);
    4026         for i := 0 to TOffer(Data).nCost - 1 do
     4024        for I := 0 to TOffer(Data).nDeliver - 1 do
     4025          PayPrice(Player, Subject, TOffer(Data).Price[I], True);
     4026        for I := 0 to TOffer(Data).nCost - 1 do
    40274027          PayPrice(Subject, Player, TOffer(Data).Price[TOffer(Data).nDeliver
    4028             + i], true);
    4029         for i := 0 to TOffer(Data).nDeliver + TOffer(Data).nCost - 1 do
    4030           if TOffer(Data).Price[i] = opTreaty + trAlliance then
     4028            + I], True);
     4029        for I := 0 to TOffer(Data).nDeliver + TOffer(Data).nCost - 1 do
     4030          if TOffer(Data).Price[I] = opTreaty + trAlliance then
    40314031          begin // add view area of allied player
    40324032            DiscoverViewAreas(Player);
     
    40454045{$IFDEF TEXTLOG}CmdInfo := Format('IntSetModelStatus P%d', [Player]);
    40464046        {$ENDIF}
    4047         RW[Player].Model[Subject].Status := integer(Data);
     4047        RW[Player].Model[Subject].Status := Integer(Data);
    40484048      end;
    40494049
     
    40534053{$IFDEF TEXTLOG}CmdInfo := Format('IntSetUnitStatus P%d', [Player]);
    40544054        {$ENDIF}
    4055         RW[Player].Un[Subject].Status := integer(Data);
     4055        RW[Player].Un[Subject].Status := Integer(Data);
    40564056      end;
    40574057
     
    40614061{$IFDEF TEXTLOG}CmdInfo := Format('IntSetCityStatus P%d', [Player]);
    40624062        {$ENDIF}
    4063         RW[Player].City[Subject].Status := integer(Data);
     4063        RW[Player].City[Subject].Status := Integer(Data);
    40644064      end;
    40654065
     
    40694069{$IFDEF TEXTLOG}CmdInfo := Format('IntSetECityStatus P%d', [Player]);
    40704070        {$ENDIF}
    4071         RW[Player].EnemyCity[Subject].Status := integer(Data);
     4071        RW[Player].EnemyCity[Subject].Status := Integer(Data);
    40724072      end;
    40734073  end;
Note: See TracChangeset for help on using the changeset viewer.