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

Legend:

Unmodified
Added
Removed
  • branches/highdpi/UnitProcessing.pas

    r303 r465  
    1313  TMoveInfo = record
    1414    MoveType: TMoveType;
    15     Cost, ToMaster, EndHealth, Defender, Dcix, Duix, EndHealthDef: integer;
    16     MountainDelay: boolean;
     15    Cost, ToMaster, EndHealth, Defender, Dcix, Duix, EndHealthDef: Integer;
     16    MountainDelay: Boolean;
    1717  end;
    1818
    1919var
    20   uixSelectedTransport: integer;
    21   Worked: array [0 .. nPl - 1] of integer; { settler work statistics }
     20  uixSelectedTransport: Integer;
     21  Worked: array [0 .. nPl - 1] of Integer; { settler work statistics }
    2222
    2323  // Moving/Combat
    24 function HostileDamage(p, mix, Loc, MP: integer): integer;
    25 function CalculateMove(p, uix, ToLoc, MoveLength: integer; TestOnly: boolean;
    26   var MoveInfo: TMoveInfo): integer;
    27 function GetBattleForecast(Loc: integer; var BattleForecast: TBattleForecast;
    28   var Duix, Dcix, AStr, DStr, ABaseDamage, DBaseDamage: integer): integer;
    29 function LoadUnit(p, uix: integer; TestOnly: boolean): integer;
    30 function UnloadUnit(p, uix: integer; TestOnly: boolean): integer;
    31 procedure Recover(p, uix: integer);
    32 function GetMoveAdvice(p, uix: integer; var a: TMoveAdviceData): integer;
    33 function CanPlaneReturn(p, uix: integer;
    34   PlaneReturnData: TPlaneReturnData): boolean;
     24function HostileDamage(P, mix, Loc, MP: Integer): Integer;
     25function CalculateMove(P, uix, ToLoc, MoveLength: Integer; TestOnly: Boolean;
     26  var MoveInfo: TMoveInfo): Integer;
     27function GetBattleForecast(Loc: Integer; var BattleForecast: TBattleForecast;
     28  var Duix, Dcix, AStr, DStr, ABaseDamage, DBaseDamage: Integer): Integer;
     29function LoadUnit(P, uix: Integer; TestOnly: Boolean): Integer;
     30function UnloadUnit(P, uix: Integer; TestOnly: Boolean): Integer;
     31procedure Recover(P, uix: Integer);
     32function GetMoveAdvice(P, uix: Integer; var A: TMoveAdviceData): Integer;
     33function CanPlaneReturn(P, uix: Integer;
     34  PlaneReturnData: TPlaneReturnData): Boolean;
    3535
    3636// Terrain Improvement
    37 function StartJob(p, uix, NewJob: integer; TestOnly: boolean): integer;
    38 function Work(p, uix: integer): boolean;
    39 function GetJobProgress(p, Loc: integer;
    40   var JobProgressData: TJobProgressData): integer;
     37function StartJob(P, uix, NewJob: Integer; TestOnly: Boolean): Integer;
     38function Work(P, uix: Integer): Boolean;
     39function GetJobProgress(P, Loc: Integer;
     40  var JobProgressData: TJobProgressData): Integer;
    4141
    4242// Start/End Game
    4343procedure InitGame;
    4444procedure ReleaseGame;
     45
    4546
    4647implementation
     
    7374
    7475type
    75   TToWorkList = array [0 .. INFIN, 0 .. nJob - 1] of word;
     76  TToWorkList = array [0 .. INFIN, 0 .. nJob - 1] of Word;
    7677
    7778var
     
    8283    ____________________________________________________________________
    8384  }
    84 function HostileDamage(p, mix, Loc, MP: integer): integer;
    85 var
    86   Tile: integer;
     85function HostileDamage(P, mix, Loc, MP: Integer): Integer;
     86var
     87  Tile: Integer;
    8788begin
    8889  Tile := RealMap[Loc];
    89   if (RW[p].Model[mix].Domain >= dSea) or (RW[p].Model[mix].Kind = mkSettler)
    90     and (RW[p].Model[mix].Speed >= 200) or
     90  if (RW[P].Model[mix].Domain >= dSea) or (RW[P].Model[mix].Kind = mkSettler)
     91    and (RW[P].Model[mix].Speed >= 200) or
    9192    (Tile and (fCity or fRiver or fCanal) <> 0) or (Tile and fTerImp = tiBase)
    92     or (GWonder[woGardens].EffectiveOwner = p) then
    93     result := 0
     93    or (GWonder[woGardens].EffectiveOwner = P) then
     94    Result := 0
    9495  else if (Tile and fTerrain = fDesert) and
    9596    (Tile and fSpecial <> fSpecial1 { Oasis } ) then
    9697  begin
    97     assert((Tile and fTerImp <> tiIrrigation) and (Tile and fTerImp <> tiFarm));
    98     result := (DesertThurst * MP - 1) div RW[p].Model[mix].Speed + 1
     98    Assert((Tile and fTerImp <> tiIrrigation) and (Tile and fTerImp <> tiFarm));
     99    Result := (DesertThurst * MP - 1) div RW[P].Model[mix].Speed + 1
    99100  end
    100101  else if Tile and fTerrain = fArctic then
    101102  begin
    102     assert((Tile and fTerImp <> tiIrrigation) and (Tile and fTerImp <> tiFarm));
    103     result := (ArcticThurst * MP - 1) div RW[p].Model[mix].Speed + 1
     103    Assert((Tile and fTerImp <> tiIrrigation) and (Tile and fTerImp <> tiFarm));
     104    Result := (ArcticThurst * MP - 1) div RW[P].Model[mix].Speed + 1
    104105  end
    105106  else
    106     result := 0
    107 end;
    108 
    109 function Controlled(p, Loc: integer; IsDest: boolean): integer;
     107    Result := 0;
     108end;
     109
     110function Controlled(P, Loc: Integer; IsDest: Boolean): Integer;
    110111{ whether tile at Loc is in control zone of enemy unit
    111112  returns combination of tile control flags }
    112113var
    113   Loc1, V8: integer;
     114  Loc1, V8: Integer;
    114115  Adjacent: TVicinity8Loc;
    115116begin
    116   result := 0;
    117   if IsDest and (Occupant[Loc] = p) and (ZoCMap[Loc] > 0) then
    118     exit;
     117  Result := 0;
     118  if IsDest and (Occupant[Loc] = P) and (ZoCMap[Loc] > 0) then
     119    Exit;
    119120  // destination tile, not controlled if already occupied
    120121
    121   if (RealMap[Loc] and fCity = 0) or (integer(RealMap[Loc] shr 27) <> p) and
    122     (ServerVersion[p] >= $000EF0) then
     122  if (RealMap[Loc] and fCity = 0) or (Integer(RealMap[Loc] shr 27) <> P) and
     123    (ServerVersion[P] >= $000EF0) then
    123124  begin // not own city
    124125    V8_to_Loc(Loc, Adjacent);
     
    127128      Loc1 := Adjacent[V8];
    128129      if (Loc1 >= 0) and (Loc1 < MapSize) and (ZoCMap[Loc1] > 0) and
    129         (Occupant[Loc1] >= 0) and (Occupant[Loc1] <> p) and
    130         (RW[p].Treaty[Occupant[Loc1]] < trAlliance) then
    131         if ObserveLevel[Loc1] and (3 shl (p * 2)) > 0 then
     130        (Occupant[Loc1] >= 0) and (Occupant[Loc1] <> P) and
     131        (RW[P].Treaty[Occupant[Loc1]] < trAlliance) then
     132        if ObserveLevel[Loc1] and (3 shl (P * 2)) > 0 then
    132133        begin // p observes tile
    133           result := coKnown or coTrue;
    134           exit
     134          Result := coKnown or coTrue;
     135          Exit;
    135136        end
    136137        else
    137           result := coTrue; // p does not observe tile
    138     end;
    139   end
    140 end;
    141 
    142 function GetMoveCost(p, mix, FromLoc, ToLoc, MoveLength: integer;
    143   var MoveCost: integer): integer;
     138          Result := coTrue; // p does not observe tile
     139    end;
     140  end;
     141end;
     142
     143function GetMoveCost(P, mix, FromLoc, ToLoc, MoveLength: Integer;
     144  var MoveCost: Integer): Integer;
    144145// MoveLength - 2 for short move, 3 for long move
    145146var
    146   FromTile, ToTile: integer;
    147 begin
    148   result := eOK;
     147  FromTile, ToTile: Integer;
     148begin
     149  Result := eOK;
    149150  FromTile := RealMap[FromLoc];
    150151  ToTile := RealMap[ToLoc];
    151   with RW[p].Model[mix] do
     152  with RW[P].Model[mix] do
    152153  begin
    153154    case Domain of
     
    159160          if (FromTile and (fRR or fCity) <> 0) and
    160161            (ToTile and (fRR or fCity) <> 0) then
    161             if GWonder[woShinkansen].EffectiveOwner = p then
     162            if GWonder[woShinkansen].EffectiveOwner = P then
    162163              MoveCost := 0
    163164            else
     
    173174              MoveCost := 20
    174175          else if Cap[mcOver] > 0 then
    175             result := eNoRoad
     176            Result := eNoRoad
    176177          else
    177178            case Terrain[ToTile and fTerrain].MoveCost of
     
    180181              2:
    181182                begin
    182                   assert(Speed - 150 <= 600);
     183                  Assert(Speed - 150 <= 600);
    183184                  MoveCost := 50 + (Speed - 150) * 13 shr 7; // heavy terrain
    184185                end;
     
    186187                begin
    187188                  MoveCost := Speed;
    188                   result := eMountains;
    189                   exit
     189                  Result := eMountains;
     190                  Exit;
    190191                end;
    191192            end;
     
    193194        end
    194195        else
    195           result := eDomainMismatch;
     196          Result := eDomainMismatch;
    196197
    197198      dSea:
     
    201202            MoveCost := 50 * MoveLength { valid move }
    202203          else
    203             result := eNoNav { navigation required for open sea }
    204         else
    205           result := eDomainMismatch;
     204            Result := eNoNav { navigation required for open sea }
     205        else
     206          Result := eDomainMismatch;
    206207
    207208      dAir:
    208209        MoveCost := 50 * MoveLength; { always valid move }
    209     end
    210   end
    211 end;
    212 
    213 function CalculateMove(p, uix, ToLoc, MoveLength: integer; TestOnly: boolean;
    214   var MoveInfo: TMoveInfo): integer;
     210    end;
     211  end;
     212end;
     213
     214function CalculateMove(P, uix, ToLoc, MoveLength: Integer; TestOnly: Boolean;
     215  var MoveInfo: TMoveInfo): Integer;
    215216var
    216217  uix1, p1, FromLoc, DestControlled, AStr, DStr, ABaseDamage,
    217     DBaseDamage: integer;
     218    DBaseDamage: Integer;
    218219  PModel: ^TModel;
    219220  BattleForecast: TBattleForecast;
    220221begin
    221   with RW[p], Un[uix] do
     222  with RW[P], Un[uix] do
    222223  begin
    223224    PModel := @Model[mix];
    224225    FromLoc := Loc;
    225226
    226     BattleForecast.pAtt := p;
     227    BattleForecast.pAtt := P;
    227228    BattleForecast.mixAtt := mix;
    228229    BattleForecast.HealthAtt := Health;
     
    230231    BattleForecast.FlagsAtt := Flags;
    231232    BattleForecast.Movement := Movement;
    232     result := GetBattleForecast(ToLoc, BattleForecast, MoveInfo.Duix,
     233    Result := GetBattleForecast(ToLoc, BattleForecast, MoveInfo.Duix,
    233234      MoveInfo.Dcix, AStr, DStr, ABaseDamage, DBaseDamage);
    234235
    235     if result = eHiddenUnit then
     236    if Result = eHiddenUnit then
    236237      if TestOnly then
    237         result := eOK // behave just like unit was moving
     238        Result := eOK // behave just like unit was moving
    238239      else if Mode > moLoading_Fast then
    239240        Map[ToLoc] := Map[ToLoc] or fHiddenUnit;
    240     if result = eStealthUnit then
     241    if Result = eStealthUnit then
    241242      if TestOnly then
    242         result := eOK // behave just like unit was moving
     243        Result := eOK // behave just like unit was moving
    243244      else if Mode > moLoading_Fast then
    244245        Map[ToLoc] := Map[ToLoc] or fStealthUnit;
    245     if result < rExecuted then
    246       exit;
    247 
    248     case result of
     246    if Result < rExecuted then
     247      Exit;
     248
     249    case Result of
    249250      eOK:
    250251        MoveInfo.MoveType := mtMove;
     
    260261      begin
    261262        p1 := RealMap[ToLoc] shr 27;
    262         if (p1 < nPl) and (p1 <> p) and
     263        if (p1 < nPl) and (p1 <> P) and
    263264          ((RealMap[Loc] shr 27 <> Cardinal(p1)) and (PModel.Kind <> mkDiplomat)
    264265          and (Treaty[p1] >= trPeace) and (Treaty[p1] < trAlliance) or
    265266          (RealMap[ToLoc] and fCity <> 0) and (Treaty[p1] >= trPeace)) then
    266267        begin
    267           result := eTreaty;
    268           exit
     268          Result := eTreaty;
     269          Exit;
    269270        end; // keep peace treaty!
    270271      end;
    271272      if (RealMap[ToLoc] and fCity <> 0) and
    272         (RealMap[ToLoc] shr 27 <> Cardinal(p)) then // empty enemy city
     273        (RealMap[ToLoc] shr 27 <> Cardinal(P)) then // empty enemy city
    273274        if PModel.Kind = mkDiplomat then
    274275        begin
     
    279280          if PModel.Flags and mdCivil <> 0 then
    280281          begin
    281             result := eNoCapturer;
    282             exit
     282            Result := eNoCapturer;
     283            Exit;
    283284          end;
    284285          MoveInfo.MoveType := mtCapture;
     
    288289          if (PModel.Domain = dSea) and (PModel.Cap[mcArtillery] = 0) then
    289290          begin
    290             result := eDomainMismatch;
    291             exit
     291            Result := eDomainMismatch;
     292            Exit;
    292293          end
    293294          else if (PModel.Attack = 0) and
     
    295296          then
    296297          begin
    297             result := eNoBombarder;
    298             exit
     298            Result := eNoBombarder;
     299            Exit;
    299300          end
    300301          else if Movement < 100 then
    301302          begin
    302             result := eNoTime_Bombard;
    303             exit
     303            Result := eNoTime_Bombard;
     304            Exit;
    304305          end;
    305306          MoveInfo.MoveType := mtBombard;
    306           result := eBombarded;
    307         end
    308     end;
    309 
    310     MoveInfo.MountainDelay := false;
     307          Result := eBombarded;
     308        end;
     309    end;
     310
     311    MoveInfo.MountainDelay := False;
    311312    if MoveInfo.MoveType in [mtAttack, mtBombard, mtExpel] then
    312313    begin
     
    316317      then
    317318      begin
    318         result := eViolation;
    319         exit
     319        Result := eViolation;
     320        Exit;
    320321      end;
    321322      if MoveInfo.MoveType = mtBombard then
     
    328329        MoveInfo.EndHealth := BattleForecast.EndHealthAtt;
    329330        MoveInfo.EndHealthDef := BattleForecast.EndHealthDef;
    330       end
     331      end;
    331332    end
    332333    else // if MoveInfo.MoveType in [mtMove,mtCapture,mtSpyMission] then
     
    336337        MoveInfo.Cost := PModel.Speed;
    337338        if RealMap[ToLoc] and fTerrain < fGrass then
    338           result := eDomainMismatch;
     339          Result := eDomainMismatch;
    339340      end
    340341      else
    341342      begin
    342         result := GetMoveCost(p, mix, FromLoc, ToLoc, MoveLength,
     343        Result := GetMoveCost(P, mix, FromLoc, ToLoc, MoveLength,
    343344          MoveInfo.Cost);
    344         if result = eMountains then
    345         begin
    346           result := eOK;
    347           MoveInfo.MountainDelay := true
     345        if Result = eMountains then
     346        begin
     347          Result := eOK;
     348          MoveInfo.MountainDelay := True
    348349        end;
    349350      end;
    350       if (result >= rExecuted) and (MoveInfo.MoveType = mtSpyMission) then
    351         result := eMissionDone;
     351      if (Result >= rExecuted) and (MoveInfo.MoveType = mtSpyMission) then
     352        Result := eMissionDone;
    352353
    353354      MoveInfo.ToMaster := -1;
    354       if (result = eDomainMismatch) and (PModel.Domain < dSea) and
     355      if (Result = eDomainMismatch) and (PModel.Domain < dSea) and
    355356        (PModel.Cap[mcOver] = 0) then
    356357      begin
     
    360361              (TroopLoad < Model[mix].MTrans * Model[mix].Cap[mcSeaTrans]) then
    361362            begin
    362               result := eLoaded;
     363              Result := eLoaded;
    363364              MoveInfo.Cost := PModel.Speed;
    364365              MoveInfo.ToMaster := uix1;
     
    377378              (AirLoad < Model[mix].MTrans * Model[mix].Cap[mcCarrier]) then
    378379            begin // load plane to ship
    379               result := eLoaded;
     380              Result := eLoaded;
    380381              MoveInfo.ToMaster := uix1;
    381382              if (uixSelectedTransport >= 0) and (uix1 = uixSelectedTransport)
    382383              then
    383384                Break;
    384             end
     385            end;
    385386      end;
    386       if result < rExecuted then
    387         exit;
     387      if Result < rExecuted then
     388        Exit;
    388389
    389390      if (Master < 0) and (MoveInfo.ToMaster < 0) then
    390         MoveInfo.EndHealth := Health - HostileDamage(p, mix, ToLoc,
     391        MoveInfo.EndHealth := Health - HostileDamage(P, mix, ToLoc,
    391392          MoveInfo.Cost)
    392393      else
     
    394395
    395396      if (Mode = moPlaying) and (PModel.Flags and mdZOC <> 0) and (Master < 0)
    396         and (MoveInfo.ToMaster < 0) and (Controlled(p, FromLoc, false) >= coTrue)
     397        and (MoveInfo.ToMaster < 0) and (Controlled(P, FromLoc, False) >= coTrue)
    397398      then
    398399      begin
    399         DestControlled := Controlled(p, ToLoc, true);
     400        DestControlled := Controlled(P, ToLoc, True);
    400401        if DestControlled >= coTrue + coKnown then
    401402        begin
    402           result := eZOC;
    403           exit
     403          Result := eZOC;
     404          Exit;
    404405        end
    405406        else if not TestOnly and (DestControlled >= coTrue) then
    406407        begin
    407           result := eZOC_EnemySpotted;
    408           exit
    409         end
     408          Result := eZOC_EnemySpotted;
     409          Exit;
     410        end;
    410411      end;
    411       if (Movement = 0) and (ServerVersion[p] >= $0100F1) or
     412      if (Movement = 0) and (ServerVersion[P] >= $0100F1) or
    412413        (MoveInfo.Cost > Movement) then
    413414        if (Master >= 0) or (MoveInfo.ToMaster >= 0) then
    414415        begin
    415           result := eNoTime_Load;
    416           exit
     416          Result := eNoTime_Load;
     417          Exit;
    417418        end
    418419        else
    419420        begin
    420           result := eNoTime_Move;
    421           exit
     421          Result := eNoTime_Move;
     422          Exit;
    422423        end;
    423424      if (MoveInfo.EndHealth <= 0) or (MoveInfo.MoveType = mtSpyMission) then
    424         result := result or rUnitRemoved;
     425        Result := Result or rUnitRemoved;
    425426      // spy mission or victim of HostileDamage
    426427
     
    433434      MoveInfo.Defender := RealMap[ToLoc] shr 27;
    434435      SearchCity(ToLoc, MoveInfo.Defender, MoveInfo.Dcix);
    435     end
    436   end
    437 end; // CalculateMove
    438 
    439 function GetBattleForecast(Loc: integer; var BattleForecast: TBattleForecast;
    440   var Duix, Dcix, AStr, DStr, ABaseDamage, DBaseDamage: integer): integer;
    441 var
    442   Time, Defender, ABon, DBon, DCnt, MultiDamage: integer;
     436    end;
     437  end;
     438end;
     439
     440function GetBattleForecast(Loc: Integer; var BattleForecast: TBattleForecast;
     441  var Duix, Dcix, AStr, DStr, ABaseDamage, DBaseDamage: Integer): Integer;
     442var
     443  Time, Defender, ABon, DBon, DCnt, MultiDamage: Integer;
    443444  PModel, DModel: ^TModel;
    444445begin
     
    448449    if (Defender < 0) or (Defender = pAtt) then
    449450    begin
    450       result := eOK;
    451       exit
     451      Result := eOK;
     452      Exit;
    452453    end; // no attack, simple move
    453454
     
    458459      EndHealthAtt := HealthAtt;
    459460      EndHealthDef := RW[Defender].Un[Duix].Health;
    460       result := eOK;
    461       exit
     461      Result := eOK;
     462      Exit;
    462463    end;
    463464
     
    469470        (ObserveLevel[Loc] shr (2 * pAtt) and 3 < lObserveAll) then
    470471      begin
    471         result := eHiddenUnit;
    472         exit;
     472        Result := eHiddenUnit;
     473        Exit;
    473474      end; // attacking submarine not allowed
    474475      if (DModel.Cap[mcStealth] > 0) and
    475476        (ObserveLevel[Loc] shr (2 * pAtt) and 3 <> lObserveSuper) then
    476477      begin
    477         result := eStealthUnit;
    478         exit;
     478        Result := eStealthUnit;
     479        Exit;
    479480      end; // attacking stealth aircraft not allowed
    480481      if (DModel.Domain = dAir) and (DModel.Kind <> mkSpecial_Glider) and
    481482        (PModel.Domain <> dAir) then
    482483      begin
    483         result := eDomainMismatch;
    484         exit
     484        Result := eDomainMismatch;
     485        Exit;
    485486      end; // can't attack plane
    486487    end;
     
    491492      (PModel.Domain = dSea) and (RealMap[Loc] and fTerrain >= fGrass)) then
    492493    begin
    493       result := eDomainMismatch;
    494       exit
     494      Result := eDomainMismatch;
     495      Exit;
    495496    end;
    496497    if (PModel.Attack = 0) and not((PModel.Cap[mcBombs] > 0) and
    497498      (FlagsAtt and unBombsLoaded <> 0) and (DModel.Domain < dAir)) then
    498499    begin
    499       result := eInvalid;
    500       exit
     500      Result := eInvalid;
     501      Exit;
    501502    end;
    502503
    503504    if Movement = 0 then
    504505    begin
    505       result := eNoTime_Attack;
    506       exit
    507     end;
    508 
    509 {$IFOPT O-}assert(InvalidTreatyMap = 0); {$ENDIF}
     506      Result := eNoTime_Attack;
     507      Exit;
     508    end;
     509
     510{$IFOPT O-}Assert(InvalidTreatyMap = 0); {$ENDIF}
    510511    if RW[pAtt].Treaty[Defender] >= trPeace then
    511512    begin
    512513      if (PModel.Domain <> dAir) and (PModel.Attack > 0) and
    513         (integer(RealMap[Loc] shr 27) = pAtt) then
     514        (Integer(RealMap[Loc] shr 27) = pAtt) then
    514515        if Movement >= 100 then
    515516        begin // expel friendly unit
    516517          EndHealthDef := RW[Defender].Un[Duix].Health;
    517518          EndHealthAtt := HealthAtt;
    518           result := eExpelled
     519          Result := eExpelled
    519520        end
    520521        else
    521           result := eNoTime_Expel
     522          Result := eNoTime_Expel
    522523      else
    523         result := eTreaty;
    524       exit;
     524        Result := eTreaty;
     525      Exit;
    525526    end;
    526527
     
    533534        (Continent[RW[Defender].City[Dcix].Loc] = GrWallContinent[Defender]))
    534535      then
    535         inc(DBon, 8)
     536        Inc(DBon, 8)
    536537      else if (PModel.Domain = dSea) and
    537538        (RW[Defender].City[Dcix].Built[imCoastalFort] = 1) then
    538         inc(DBon, 4)
     539        Inc(DBon, 4)
    539540      else if (PModel.Domain = dAir) and
    540541        (RW[Defender].City[Dcix].Built[imMissileBat] = 1) then
    541         inc(DBon, 4);
     542        Inc(DBon, 4);
    542543      if RW[Defender].City[Dcix].Built[imBunker] = 1 then
    543         inc(DBon, 4)
     544        Inc(DBon, 4)
    544545    end;
    545546    if (PModel.Domain = dAir) and (DModel.Cap[mcAirDef] > 0) then
    546       inc(DBon, 4);
     547      Inc(DBon, 4);
    547548    DStr := DModel.Defense * DBon * 100;
    548549    if (DModel.Domain = dAir) and ((RealMap[Loc] and fCity <> 0) or
     
    577578        DBaseDamage := 1;
    578579      if DBaseDamage > RW[Defender].Un[Duix].Health then
    579         DBaseDamage := RW[Defender].Un[Duix].Health
     580        DBaseDamage := RW[Defender].Un[Duix].Health;
    580581    end;
    581582
     
    589590        ABaseDamage := 1;
    590591      if ABaseDamage > HealthAtt then
    591         ABaseDamage := HealthAtt
     592        ABaseDamage := HealthAtt;
    592593    end;
    593594
     
    621622
    622623    if EndHealthDef > 0 then
    623       result := eLost
     624      Result := eLost
    624625    else if EndHealthAtt > 0 then
    625       result := eWon
     626      Result := eWon
    626627    else
    627       result := eBloody
    628   end
    629 end; // GetBattleForecast
    630 
    631 function LoadUnit(p, uix: integer; TestOnly: boolean): integer;
    632 var
    633   uix1, d, Cost, ToMaster: integer;
    634 begin
    635   result := eOK;
    636   with RW[p].Un[uix] do
    637   begin
    638     d := RW[p].Model[mix].Domain;
    639     if (Master >= 0) or (d = dSea) or
    640       (RW[p].Model[mix].Cap[mcAirTrans] + RW[p].Model[mix].Cap[mcOver] > 0) then
    641       result := eViolation
     628      Result := eBloody;
     629  end;
     630end;
     631
     632function LoadUnit(P, uix: Integer; TestOnly: Boolean): Integer;
     633var
     634  uix1, D, Cost, ToMaster: Integer;
     635begin
     636  Result := eOK;
     637  with RW[P].Un[uix] do
     638  begin
     639    D := RW[P].Model[mix].Domain;
     640    if (Master >= 0) or (D = dSea) or
     641      (RW[P].Model[mix].Cap[mcAirTrans] + RW[P].Model[mix].Cap[mcOver] > 0) then
     642      Result := eViolation
    642643    else
    643644    begin
    644645      ToMaster := -1;
    645       for uix1 := 0 to RW[p].nUn - 1 do
    646         if RW[p].Un[uix1].Loc = Loc then
    647           with RW[p].Un[uix1], RW[p].Model[mix] do
    648             if (d < dSea) and
     646      for uix1 := 0 to RW[P].nUn - 1 do
     647        if RW[P].Un[uix1].Loc = Loc then
     648          with RW[P].Un[uix1], RW[P].Model[mix] do
     649            if (D < dSea) and
    649650              (TroopLoad < MTrans * (Cap[mcSeaTrans] + Cap[mcAirTrans])) or
    650               (d = dAir) and (AirLoad < MTrans * Cap[mcCarrier]) then
     651              (D = dAir) and (AirLoad < MTrans * Cap[mcCarrier]) then
    651652            begin { load onto unit uix1 }
    652653              if (uixSelectedTransport < 0) or (uix1 = uixSelectedTransport)
     
    654655              begin
    655656                ToMaster := uix1;
    656                 Break
     657                Break;
    657658              end
    658659              else if ToMaster < 0 then
     
    660661            end;
    661662      if ToMaster < 0 then
    662         result := eNoLoadCapacity
     663        Result := eNoLoadCapacity
    663664      else
    664665      begin
    665         if d = dAir then
     666        if D = dAir then
    666667          Cost := 100
    667668        else
    668           Cost := RW[p].Model[mix].Speed;
     669          Cost := RW[P].Model[mix].Speed;
    669670        if Movement < Cost then
    670           result := eNoTime_Load
     671          Result := eNoTime_Load
    671672        else if not TestOnly then
    672673        begin
    673           FreeUnit(p, uix);
    674           dec(Movement, Cost);
    675           if d = dAir then
    676             inc(RW[p].Un[ToMaster].AirLoad)
     674          FreeUnit(P, uix);
     675          Dec(Movement, Cost);
     676          if D = dAir then
     677            Inc(RW[P].Un[ToMaster].AirLoad)
    677678          else
    678             inc(RW[p].Un[ToMaster].TroopLoad);
     679            Inc(RW[P].Un[ToMaster].TroopLoad);
    679680          Master := ToMaster;
    680681          UpdateUnitMap(Loc);
    681         end
    682       end
    683     end
    684   end
    685 end;
    686 
    687 function UnloadUnit(p, uix: integer; TestOnly: boolean): integer;
    688 var
    689   Cost: integer;
    690 begin
    691   result := eOK;
    692   with RW[p].Un[uix] do
     682        end;
     683      end;
     684    end;
     685  end;
     686end;
     687
     688function UnloadUnit(P, uix: Integer; TestOnly: Boolean): Integer;
     689var
     690  Cost: Integer;
     691begin
     692  Result := eOK;
     693  with RW[P].Un[uix] do
    693694    if Master < 0 then
    694       result := eNotChanged
    695     else if (RW[p].Model[mix].Domain < dSea) and
     695      Result := eNotChanged
     696    else if (RW[P].Model[mix].Domain < dSea) and
    696697      (RealMap[Loc] and fTerrain < fGrass) then
    697       result := eDomainMismatch
     698      Result := eDomainMismatch
    698699      // else if (RW[p].Model[mix].Domain<dSea)
    699700      // and (RW[p].Model[mix].Flags and mdCivil<>0)
     
    701702    else
    702703    begin
    703       if RW[p].Model[mix].Domain = dAir then
     704      if RW[P].Model[mix].Domain = dAir then
    704705        Cost := 100
    705706      else
    706         Cost := RW[p].Model[mix].Speed;
     707        Cost := RW[P].Model[mix].Speed;
    707708      if Movement < Cost then
    708         result := eNoTime_Load
     709        Result := eNoTime_Load
    709710      else if not TestOnly then
    710711      begin
    711         dec(Movement, Cost);
    712         if RW[p].Model[mix].Domain = dAir then
    713           dec(RW[p].Un[Master].AirLoad)
    714         else
    715         begin
    716           dec(RW[p].Un[Master].TroopLoad);
     712        Dec(Movement, Cost);
     713        if RW[P].Model[mix].Domain = dAir then
     714          Dec(RW[P].Un[Master].AirLoad)
     715        else
     716        begin
     717          Dec(RW[P].Un[Master].TroopLoad);
    717718          // Movement:=0 // no more movement after unload
    718719        end;
    719720        Master := -1;
    720         PlaceUnit(p, uix);
     721        PlaceUnit(P, uix);
    721722        UpdateUnitMap(Loc);
    722723      end;
    723     end
    724 end;
    725 
    726 procedure Recover(p, uix: integer);
    727 var
    728   cix, Recovery: integer;
    729 begin
    730   with RW[p], Un[uix] do
     724    end;
     725end;
     726
     727procedure Recover(P, uix: Integer);
     728var
     729  cix, Recovery: Integer;
     730begin
     731  with RW[P], Un[uix] do
    731732  begin
    732733    if (Master >= 0) and (Model[Un[Master].mix].Cap[mcSupplyShip] > 0) then
     
    738739      cix := nCity - 1;
    739740      while (cix >= 0) and (City[cix].Loc <> Loc) do
    740         dec(cix);
     741        Dec(cix);
    741742      if City[cix].Flags and chDisorder <> 0 then
    742743        Recovery := NoCityRecovery
     
    761762    if Recovery > 100 - Health then
    762763      Recovery := 100 - Health;
    763     inc(Health, Recovery);
     764    Inc(Health, Recovery);
    764765  end;
    765766end;
    766767
    767 function GetMoveAdvice(p, uix: integer; var a: TMoveAdviceData): integer;
     768function GetMoveAdvice(P, uix: Integer; var A: TMoveAdviceData): Integer;
    768769const
    769770  // domains
     
    777778  gmaAlpine = 8;
    778779var
    779   i, FromLoc, EndLoc, T, T1, maxmov, initmov, Loc, Loc1, FromTile, ToTile, V8,
     780  I, FromLoc, EndLoc, T, T1, maxmov, initmov, Loc, Loc1, FromTile, ToTile, V8,
    780781    MoveInfo, HeavyCost, RailCost, MoveCost, AddDamage, MaxDamage,
    781     MovementLeft: integer;
     782    MovementLeft: Integer;
    782783  Map: ^TTileList;
    783784  Q: TIPQ;
    784785  Adjacent: TVicinity8Loc;
    785   From: array [0 .. lxmax * lymax - 1] of integer;
    786   Time: array [0 .. lxmax * lymax - 1] of integer;
    787   Damage: array [0 .. lxmax * lymax - 1] of integer;
    788   MountainDelay, Resistant: boolean;
     786  From: array [0 .. lxmax * lymax - 1] of Integer;
     787  Time: array [0 .. lxmax * lymax - 1] of Integer;
     788  Damage: array [0 .. lxmax * lymax - 1] of Integer;
     789  MountainDelay, Resistant: Boolean;
    789790  // tt,tt0: int64;
    790791begin
    791792  // QueryPerformanceCounter(tt0);
    792793
    793   MaxDamage := RW[p].Un[uix].Health - 1;
    794   if MaxDamage > a.MaxHostile_MovementLeft then
    795     if a.MaxHostile_MovementLeft >= 0 then
    796       MaxDamage := a.MaxHostile_MovementLeft
     794  MaxDamage := RW[P].Un[uix].Health - 1;
     795  if MaxDamage > A.MaxHostile_MovementLeft then
     796    if A.MaxHostile_MovementLeft >= 0 then
     797      MaxDamage := A.MaxHostile_MovementLeft
    797798    else
    798799      MaxDamage := 0;
    799800
    800   Map := @(RW[p].Map^);
    801   if (a.ToLoc <> maNextCity) and ((a.ToLoc < 0) or (a.ToLoc >= MapSize)) then
    802   begin
    803     result := eInvalid;
    804     exit
     801  Map := @(RW[P].Map^);
     802  if (A.ToLoc <> maNextCity) and ((A.ToLoc < 0) or (A.ToLoc >= MapSize)) then
     803  begin
     804    Result := eInvalid;
     805    Exit;
    805806  end;
    806   if (a.ToLoc <> maNextCity) and (Map[a.ToLoc] and fTerrain = fUNKNOWN) then
    807   begin
    808     result := eNoWay;
    809     exit
     807  if (A.ToLoc <> maNextCity) and (Map[A.ToLoc] and fTerrain = fUNKNOWN) then
     808  begin
     809    Result := eNoWay;
     810    Exit;
    810811  end;
    811812
    812   with RW[p].Model[RW[p].Un[uix].mix] do
     813  with RW[P].Model[RW[P].Un[uix].mix] do
    813814    case Domain of
    814815      dGround:
    815         if (a.ToLoc <> maNextCity) and (Map[a.ToLoc] and fTerrain = fOcean) then
    816         begin
    817           result := eDomainMismatch;
    818           exit
     816        if (A.ToLoc <> maNextCity) and (Map[A.ToLoc] and fTerrain = fOcean) then
     817        begin
     818          Result := eDomainMismatch;
     819          Exit;
    819820        end
    820821        else
     
    825826            MoveInfo := gmaGround_NoZoC;
    826827          if Cap[mcOver] > 0 then
    827             inc(MoveInfo, gmaOver);
     828            Inc(MoveInfo, gmaOver);
    828829          if Cap[mcAlpine] > 0 then
    829             inc(MoveInfo, gmaAlpine);
     830            Inc(MoveInfo, gmaAlpine);
    830831          HeavyCost := 50 + (Speed - 150) * 13 shr 7;
    831           if GWonder[woShinkansen].EffectiveOwner = p then
     832          if GWonder[woShinkansen].EffectiveOwner = P then
    832833            RailCost := 0
    833834          else
     
    835836          maxmov := Speed;
    836837          initmov := 0;
    837           Resistant := (GWonder[woGardens].EffectiveOwner = p) or
     838          Resistant := (GWonder[woGardens].EffectiveOwner = P) or
    838839            (Kind = mkSettler) and (Speed >= 200);
    839840        end;
    840841      dSea:
    841         if (a.ToLoc <> maNextCity) and (Map[a.ToLoc] and fTerrain >= fGrass) and
    842           (Map[a.ToLoc] and (fCity or fUnit or fCanal) = 0) then
    843         begin
    844           result := eDomainMismatch;
    845           exit
     842        if (A.ToLoc <> maNextCity) and (Map[A.ToLoc] and fTerrain >= fGrass) and
     843          (Map[A.ToLoc] and (fCity or fUnit or fCanal) = 0) then
     844        begin
     845          Result := eDomainMismatch;
     846          Exit;
    846847        end
    847848        else
     
    849850          MoveInfo := gmaSea;
    850851          if Cap[mcNav] > 0 then
    851             inc(MoveInfo, gmaNav);
    852           maxmov := UnitSpeed(p, RW[p].Un[uix].mix, 100);
    853           initmov := maxmov - UnitSpeed(p, RW[p].Un[uix].mix,
    854             RW[p].Un[uix].Health);
     852            Inc(MoveInfo, gmaNav);
     853          maxmov := UnitSpeed(P, RW[P].Un[uix].mix, 100);
     854          initmov := maxmov - UnitSpeed(P, RW[P].Un[uix].mix,
     855            RW[P].Un[uix].Health);
    855856        end;
    856857      dAir:
     
    859860          maxmov := Speed;
    860861          initmov := 0;
    861         end
    862     end;
    863 
    864   FromLoc := RW[p].Un[uix].Loc;
     862        end;
     863    end;
     864
     865  FromLoc := RW[P].Un[uix].Loc;
    865866  FillChar(Time, SizeOf(Time), 255); { -1 }
    866867  Damage[FromLoc] := 0;
    867868  Q := TIPQ.Create(MapSize);
    868   Q.Put(FromLoc, (maxmov - RW[p].Un[uix].Movement) shl 8);
     869  Q.Put(FromLoc, (maxmov - RW[P].Un[uix].Movement) shl 8);
    869870  while Q.Get(Loc, T) do
    870871  begin
    871872    Time[Loc] := T;
    872     if T >= (a.MoreTurns + 1) shl 20 then
     873    if T >= (A.MoreTurns + 1) shl 20 then
    873874    begin
    874875      Loc := -1;
    875       Break
     876      Break;
    876877    end;
    877878    FromTile := Map[Loc];
    878     if (Loc = a.ToLoc) or (a.ToLoc = maNextCity) and (FromTile and fCity <> 0)
     879    if (Loc = A.ToLoc) or (A.ToLoc = maNextCity) and (FromTile and fCity <> 0)
    879880    then
    880881      Break;
    881882    if T and $FFF00 = $FFF00 then
    882       inc(T, $100000); // indicates mountain delay
     883      Inc(T, $100000); // indicates mountain delay
    883884    V8_to_Loc(Loc, Adjacent);
    884885    for V8 := 0 to 7 do
     
    888889      begin
    889890        ToTile := Map[Loc1];
    890         if (Loc1 = a.ToLoc) and (ToTile and (fUnit or fOwned) = fUnit) and
     891        if (Loc1 = A.ToLoc) and (ToTile and (fUnit or fOwned) = fUnit) and
    891892          not((MoveInfo and 3 = gmaSea) and (FromTile and fTerrain >= fGrass))
    892893          and not((MoveInfo and 3 = gmaAir) and ((FromTile and fCity <> 0) or
     
    897898        end
    898899        else if (ToTile and fTerrain <> fUNKNOWN) and
    899           ((Loc1 = a.ToLoc) or (ToTile and (fCity or fOwned) <> fCity))
     900          ((Loc1 = A.ToLoc) or (ToTile and (fCity or fOwned) <> fCity))
    900901        // don't move through enemy cities
    901           and ((Loc1 = a.ToLoc) or (ToTile and (fUnit or fOwned) <> fUnit))
     902          and ((Loc1 = A.ToLoc) or (ToTile and (fUnit or fOwned) <> fUnit))
    902903        // way is blocked
    903904          and (ToTile and not FromTile and fPeace = 0) and
     
    909910          // calculate move cost, must be identic to GetMoveCost function
    910911          AddDamage := 0;
    911           MountainDelay := false;
     912          MountainDelay := False;
    912913          case MoveInfo of
    913914
     
    955956                    begin
    956957                      MoveCost := maxmov;
    957                       MountainDelay := true;
     958                      MountainDelay := True;
    958959                    end;
    959960                end;
     
    989990          if (MoveCost > 0) and not MountainDelay then
    990991            if V8 and 1 <> 0 then
    991               inc(MoveCost, MoveCost * 2)
     992              Inc(MoveCost, MoveCost * 2)
    992993            else
    993               inc(MoveCost, MoveCost);
     994              Inc(MoveCost, MoveCost);
    994995
    995996          if (MoveInfo and 2 <> 0) // ground unit, check transport load/unload
     
    10101011                if FromTile and (fTerrain or fCity or fRiver or fCanal or
    10111012                  fSpecial1 { Oasis } ) = fDesert then
    1012                   inc(AddDamage, (DesertThurst * (maxmov - T shr 8 and $FFF) -
     1013                  Inc(AddDamage, (DesertThurst * (maxmov - T shr 8 and $FFF) -
    10131014                    1) div maxmov + 1)
    10141015                else if FromTile and (fTerrain or fCity or fRiver or fCanal) = fArctic
    10151016                then
    1016                   inc(AddDamage, (ArcticThurst * (maxmov - T shr 8 and $FFF) -
     1017                  Inc(AddDamage, (ArcticThurst * (maxmov - T shr 8 and $FFF) -
    10171018                    1) div maxmov + 1);
    10181019
     
    10291030                From[Loc1] := Loc;
    10301031                Damage[Loc1] := Damage[Loc] + AddDamage;
    1031               end
    1032           end
    1033         end
    1034       end
    1035     end
     1032              end;
     1033          end;
     1034        end;
     1035      end;
     1036    end;
    10361037  end;
    10371038  FreeAndNil(Q);
    1038   if (Loc = a.ToLoc) or (a.ToLoc = maNextCity) and (Loc >= 0) and
     1039  if (Loc = A.ToLoc) or (A.ToLoc = maNextCity) and (Loc >= 0) and
    10391040    (Map[Loc] and fCity <> 0) then
    10401041  begin
    1041     a.MoreTurns := T shr 20;
     1042    A.MoreTurns := T shr 20;
    10421043    EndLoc := Loc;
    1043     a.nStep := 0;
     1044    A.nStep := 0;
    10441045    while Loc <> FromLoc do
    10451046    begin
    10461047      if Time[Loc] < $100000 then
    1047         inc(a.nStep);
     1048        Inc(A.nStep);
    10481049      Loc := From[Loc];
    10491050    end;
    10501051    Loc := EndLoc;
    1051     i := a.nStep;
     1052    I := A.nStep;
    10521053    while Loc <> FromLoc do
    10531054    begin
    10541055      if Time[Loc] < $100000 then
    10551056      begin
    1056         dec(i);
    1057         if i < 25 then
    1058         begin
    1059           a.dx[i] := ((Loc mod lx * 2 + Loc div lx and 1) -
     1057        Dec(I);
     1058        if I < 25 then
     1059        begin
     1060          A.dx[I] := ((Loc mod lx * 2 + Loc div lx and 1) -
    10601061            (From[Loc] mod lx * 2 + From[Loc] div lx and 1) + 3 * lx)
    10611062            mod (2 * lx) - lx;
    1062           a.dy[i] := Loc div lx - From[Loc] div lx;
     1063          A.dy[I] := Loc div lx - From[Loc] div lx;
    10631064        end
    10641065      end;
    10651066      Loc := From[Loc];
    10661067    end;
    1067     a.MaxHostile_MovementLeft := maxmov - Time[EndLoc] shr 8 and $FFF;
    1068     if a.nStep > 25 then
    1069       a.nStep := 25;
    1070     result := eOK
     1068    A.MaxHostile_MovementLeft := maxmov - Time[EndLoc] shr 8 and $FFF;
     1069    if A.nStep > 25 then
     1070      A.nStep := 25;
     1071    Result := eOK
    10711072  end
    10721073  else
    1073     result := eNoWay;
     1074    Result := eNoWay;
    10741075
    10751076  // QueryPerformanceCounter(tt);{time in s is: (tt-tt0)/PerfFreq}
    1076 end; // GetMoveAdvice
    1077 
    1078 function CanPlaneReturn(p, uix: integer;
    1079   PlaneReturnData: TPlaneReturnData): boolean;
     1077end;
     1078
     1079function CanPlaneReturn(P, uix: Integer;
     1080  PlaneReturnData: TPlaneReturnData): Boolean;
    10801081const
    10811082  mfEnd = 1;
    10821083  mfReached = 2;
    10831084var
    1084   uix1, T, T1, Loc, Loc1, FromTile, ToTile, V8, MoveCost, maxmov: integer;
     1085  uix1, T, T1, Loc, Loc1, FromTile, ToTile, V8, MoveCost, maxmov: Integer;
    10851086  Map: ^TTileList;
    10861087  Q: TIPQ;
    10871088  Adjacent: TVicinity8Loc;
    1088   MapFlags: array [0 .. lxmax * lymax - 1] of byte;
    1089 begin
    1090   Map := @(RW[p].Map^);
     1089  MapFlags: array [0 .. lxmax * lymax - 1] of Byte;
     1090begin
     1091  Map := @(RW[P].Map^);
    10911092
    10921093  // calculate possible return points
    10931094  FillChar(MapFlags, SizeOf(MapFlags), 0);
    1094   if RW[p].Model[RW[p].Un[uix].mix].Kind = mkSpecial_Glider then
     1095  if RW[P].Model[RW[P].Un[uix].mix].Kind = mkSpecial_Glider then
    10951096  begin
    10961097    for Loc := 0 to MapSize - 1 do
     
    11051106        (Map[Loc] and (fUnit or fOwned) <> fUnit) then
    11061107        MapFlags[Loc] := MapFlags[Loc] or mfEnd;
    1107     if RW[p].Model[RW[p].Un[uix].mix].Cap[mcAirTrans] = 0 then
     1108    if RW[P].Model[RW[P].Un[uix].mix].Cap[mcAirTrans] = 0 then
    11081109    // plane can land on carriers
    1109       for uix1 := 0 to RW[p].nUn - 1 do
    1110         with RW[p].Un[uix1], RW[p].Model[mix] do
     1110      for uix1 := 0 to RW[P].nUn - 1 do
     1111        with RW[P].Un[uix1], RW[P].Model[mix] do
    11111112          if AirLoad < MTrans * Cap[mcCarrier] then
    11121113            MapFlags[Loc] := MapFlags[Loc] or mfEnd;
    11131114  end;
    11141115
    1115   with RW[p].Un[uix] do
     1116  with RW[P].Un[uix] do
    11161117  begin
    11171118    if Master >= 0 then // can return to same carrier, even if full now
    11181119      MapFlags[Loc] := MapFlags[Loc] or mfEnd;
    1119     maxmov := RW[p].Model[mix].Speed;
     1120    maxmov := RW[P].Model[mix].Speed;
    11201121  end;
    11211122
    1122   result := false;
     1123  Result := False;
    11231124  Q := TIPQ.Create(MapSize);
    11241125  Q.Put(PlaneReturnData.Loc, (maxmov - PlaneReturnData.Movement) shl 8);
     
    11281129    if T >= (PlaneReturnData.Fuel + 1) shl 20 then
    11291130    begin
    1130       result := false;
    1131       Break
     1131      Result := False;
     1132      Break;
    11321133    end;
    11331134    if MapFlags[Loc] and mfEnd <> 0 then
    11341135    begin
    1135       result := true;
    1136       Break
     1136      Result := True;
     1137      Break;
    11371138    end;
    11381139    FromTile := Map[Loc];
     
    11611162            T1 := T + MoveCost shl 8;
    11621163          Q.Put(Loc1, T1);
    1163         end
    1164       end
    1165     end
     1164        end;
     1165      end;
     1166    end;
    11661167  end;
    11671168  FreeAndNil(Q);
    1168 end; // CanPlaneReturn
     1169end;
    11691170
    11701171{
     
    11721173  ____________________________________________________________________
    11731174}
    1174 function CalculateJobWork(p, Loc, Job: integer; var JobWork: integer): integer;
    1175 var
    1176   TerrType: integer;
    1177 begin
    1178   result := eOK;
     1175function CalculateJobWork(P, Loc, Job: Integer; var JobWork: Integer): Integer;
     1176var
     1177  TerrType: Integer;
     1178begin
     1179  Result := eOK;
    11791180  TerrType := RealMap[Loc] and fTerrain;
    11801181  with Terrain[TerrType] do
     
    11821183      jCity:
    11831184        if RealMap[Loc] and fCity <> 0 then
    1184           result := eInvalid
     1185          Result := eInvalid
    11851186        else if IrrEff = 0 then
    1186           result := eNoCityTerrain
     1187          Result := eNoCityTerrain
    11871188        else
    11881189          JobWork := CityWork;
     
    11921193          JobWork := MoveCost * RoadWork;
    11931194          if RealMap[Loc] and fRiver <> 0 then
    1194             if RW[p].Tech[adBridgeBuilding] >= tsApplicable then
    1195               inc(JobWork, RoadBridgeWork) { across river }
     1195            if RW[P].Tech[adBridgeBuilding] >= tsApplicable then
     1196              Inc(JobWork, RoadBridgeWork) { across river }
    11961197            else
    1197               result := eNoBridgeBuilding
     1198              Result := eNoBridgeBuilding;
    11981199        end
    11991200        else
    1200           result := eInvalid;
     1201          Result := eInvalid;
    12011202      jRR:
    12021203        if RealMap[Loc] and fRoad = 0 then
    1203           result := eNoPreq
     1204          Result := eNoPreq
    12041205        else if RealMap[Loc] and fRR <> 0 then
    1205           result := eInvalid
     1206          Result := eInvalid
    12061207        else
    12071208        begin
    12081209          JobWork := MoveCost * RRWork;
    12091210          if RealMap[Loc] and fRiver <> 0 then
    1210             inc(JobWork, RRBridgeWork); { across river }
     1211            Inc(JobWork, RRBridgeWork); { across river }
    12111212        end;
    12121213      jClear:
    1213         if (TerrType = fDesert) and (GWonder[woGardens].EffectiveOwner <> p)
     1214        if (TerrType = fDesert) and (GWonder[woGardens].EffectiveOwner <> P)
    12141215        then
    1215           result := eInvalid
     1216          Result := eInvalid
    12161217        else if ClearTerrain >= 0 then
    12171218          JobWork := IrrClearWork
    12181219        else
    1219           result := eInvalid;
     1220          Result := eInvalid;
    12201221      jIrr:
    12211222        begin
     
    12231224          if (IrrEff = 0) or (RealMap[Loc] and fTerImp = tiIrrigation) or
    12241225            (RealMap[Loc] and fTerImp = tiFarm) then
    1225             result := eInvalid
     1226            Result := eInvalid;
    12261227        end;
    12271228      jFarm:
    12281229        if RealMap[Loc] and fTerImp <> tiIrrigation then
    1229           result := eNoPreq
     1230          Result := eNoPreq
    12301231        else
    12311232        begin
    12321233          JobWork := IrrClearWork * FarmWork;
    12331234          if (JobWork <= 0) or (RealMap[Loc] and fTerImp = tiFarm) then
    1234             result := eInvalid
     1235            Result := eInvalid;
    12351236        end;
    12361237      jAfforest:
     
    12381239          JobWork := MineAfforestWork
    12391240        else
    1240           result := eInvalid;
     1241          Result := eInvalid;
    12411242      jMine:
    12421243        begin
    12431244          JobWork := MineAfforestWork;
    12441245          if (MineEff = 0) or (RealMap[Loc] and fTerImp = tiMine) then
    1245             result := eInvalid
     1246            Result := eInvalid;
    12461247        end;
    12471248      jFort:
     
    12491250          JobWork := MoveCost * FortWork
    12501251        else
    1251           result := eInvalid;
     1252          Result := eInvalid;
    12521253      jCanal:
    12531254        if (RealMap[Loc] and fCanal = 0) and (TerrType in TerrType_Canalable)
     
    12551256          JobWork := CanalWork
    12561257        else
    1257           result := eInvalid;
     1258          Result := eInvalid;
    12581259      jTrans:
    12591260        begin
    12601261          JobWork := TransWork;
    12611262          if JobWork <= 0 then
    1262             result := eInvalid
     1263            Result := eInvalid;
    12631264        end;
    12641265      jPoll:
     
    12661267          JobWork := PollWork
    12671268        else
    1268           result := eInvalid;
     1269          Result := eInvalid;
    12691270      jBase:
    12701271        if RealMap[Loc] and fTerImp <> tiBase then
    12711272          JobWork := MoveCost * BaseWork
    12721273        else
    1273           result := eInvalid;
     1274          Result := eInvalid;
    12741275      jPillage:
    12751276        if RealMap[Loc] and (fRoad or fRR or fCanal or fTerImp) <> 0 then
    12761277          JobWork := PillageWork
    12771278        else
    1278           result := eInvalid;
    1279     end;
    1280 end; // CalculateJobWork
    1281 
    1282 function StartJob(p, uix, NewJob: integer; TestOnly: boolean): integer;
    1283 var
    1284   JobWork, Loc0, p1, uix1, TerrType: integer;
    1285 begin
    1286 {$IFOPT O-}assert(1 shl p and InvalidTreatyMap = 0); {$ENDIF}
    1287   result := eOK;
    1288   with RW[p].Un[uix] do
     1279          Result := eInvalid;
     1280    end;
     1281end;
     1282
     1283function StartJob(P, uix, NewJob: Integer; TestOnly: Boolean): Integer;
     1284var
     1285  JobWork, Loc0, p1, uix1, TerrType: Integer;
     1286begin
     1287{$IFOPT O-}Assert(1 shl P and InvalidTreatyMap = 0); {$ENDIF}
     1288  Result := eOK;
     1289  with RW[P].Un[uix] do
    12891290  begin
    12901291    if NewJob = Job then
    12911292    begin
    1292       result := eNotChanged;
    1293       exit
     1293      Result := eNotChanged;
     1294      Exit;
    12941295    end;
    12951296    if NewJob = jNone then
     
    12971298      if not TestOnly then
    12981299        Job := jNone;
    1299       exit
     1300      Exit;
    13001301    end;
    13011302    Loc0 := Loc;
     
    13031304      (NewJob <> jRR) then
    13041305    begin
    1305       result := eDeadLands;
    1306       exit
     1306      Result := eDeadLands;
     1307      Exit;
    13071308    end;
    13081309    TerrType := RealMap[Loc0] and fTerrain;
    13091310    if (RealMap[Loc0] and fCity <> 0) or (TerrType < fGrass) or (Master >= 0) or
    1310       not((NewJob = jPillage) and (RW[p].Model[mix].Domain = dGround) or
    1311       (RW[p].Model[mix].Kind = mkSettler) or (NewJob <> jCity) and
    1312       (RW[p].Model[mix].Kind = mkSlaves) and (GWonder[woPyramids].EffectiveOwner
     1311      not((NewJob = jPillage) and (RW[P].Model[mix].Domain = dGround) or
     1312      (RW[P].Model[mix].Kind = mkSettler) or (NewJob <> jCity) and
     1313      (RW[P].Model[mix].Kind = mkSlaves) and (GWonder[woPyramids].EffectiveOwner
    13131314      >= 0)) then
    13141315    begin
    1315       result := eInvalid;
    1316       exit
     1316      Result := eInvalid;
     1317      Exit;
    13171318    end;
    13181319    if (JobPreq[NewJob] <> preNone) and
    1319       (RW[p].Tech[JobPreq[NewJob]] < tsApplicable) then
    1320     begin
    1321       result := eNoPreq;
    1322       exit
    1323     end;
    1324 
    1325     result := CalculateJobWork(p, Loc0, NewJob, JobWork);
    1326     if (Mode = moPlaying) and (result = eOK) and (NewJob <> jPoll) then
     1320      (RW[P].Tech[JobPreq[NewJob]] < tsApplicable) then
     1321    begin
     1322      Result := eNoPreq;
     1323      Exit;
     1324    end;
     1325
     1326    Result := CalculateJobWork(P, Loc0, NewJob, JobWork);
     1327    if (Mode = moPlaying) and (Result = eOK) and (NewJob <> jPoll) then
    13271328    begin // not allowed in territory of friendly nation
    13281329      p1 := RealMap[Loc0] shr 27; // owner of territory
    1329       if (p1 < nPl) and (p1 <> p) and (RW[p].Treaty[p1] >= trPeace) then
    1330         result := eTreaty; // keep peace treaty!
    1331     end;
    1332     if TestOnly or (result < rExecuted) then
    1333       exit;
     1330      if (p1 < nPl) and (p1 <> P) and (RW[P].Treaty[p1] >= trPeace) then
     1331        Result := eTreaty; // keep peace treaty!
     1332    end;
     1333    if TestOnly or (Result < rExecuted) then
     1334      Exit;
    13341335
    13351336    if (ToWork[Loc0, NewJob] = 0) or (ToWork[Loc0, NewJob] > JobWork) then
     
    13371338    Job := NewJob;
    13381339    Flags := Flags and not unFortified;
    1339     for uix1 := 0 to RW[p].nUn - 1 do
    1340       if (RW[p].Un[uix1].Loc = Loc) and
    1341         (RW[p].Un[uix1].Job in ContraJobs[NewJob]) then
    1342         RW[p].Un[uix1].Job := jNone; // stop contradictive jobs
    1343     if ServerVersion[p] < $000EF0 then
    1344       if Work(p, uix) then
    1345         result := eJobDone;
    1346     if (NewJob = jCity) and (result = eJobDone) then
    1347     begin
    1348       RemoveUnit_UpdateMap(p, uix);
    1349       result := eCity
     1340    for uix1 := 0 to RW[P].nUn - 1 do
     1341      if (RW[P].Un[uix1].Loc = Loc) and
     1342        (RW[P].Un[uix1].Job in ContraJobs[NewJob]) then
     1343        RW[P].Un[uix1].Job := jNone; // stop contradictive jobs
     1344    if ServerVersion[P] < $000EF0 then
     1345      if Work(P, uix) then
     1346        Result := eJobDone;
     1347    if (NewJob = jCity) and (Result = eJobDone) then
     1348    begin
     1349      RemoveUnit_UpdateMap(P, uix);
     1350      Result := eCity;
    13501351    end
    13511352    else if Health <= 0 then
    13521353    begin // victim of HostileDamage
    1353       RemoveUnit_UpdateMap(p, uix);
    1354       result := result or rUnitRemoved;
     1354      RemoveUnit_UpdateMap(P, uix);
     1355      Result := Result or rUnitRemoved;
    13551356    end;
    13561357    if Mode > moLoading_Fast then
    13571358    begin
    1358       if result = eCity then
    1359       begin
    1360         ObserveLevel[Loc0] := ObserveLevel[Loc0] and not(3 shl (2 * p));
    1361         Discover21(Loc0, p, lObserveUnhidden, true, true);
     1359      if Result = eCity then
     1360      begin
     1361        ObserveLevel[Loc0] := ObserveLevel[Loc0] and not(3 shl (2 * P));
     1362        Discover21(Loc0, P, lObserveUnhidden, True, True);
    13621363        // CheckContact;
    1363       end
    1364     end
    1365   end; // with
    1366 end; // StartJob
    1367 
    1368 function Work(p, uix: integer): boolean;
    1369 var
    1370   uix1, j0: integer;
    1371 begin
    1372   result := false;
    1373   with RW[p].Un[uix] do
     1364      end;
     1365    end;
     1366  end;
     1367end;
     1368
     1369function Work(P, uix: Integer): Boolean;
     1370var
     1371  uix1, j0: Integer;
     1372begin
     1373  Result := False;
     1374  with RW[P].Un[uix] do
    13741375    if Movement >= 100 then
    13751376    begin
    1376       assert(ToWork[Loc, Job] < $FFFF); // should have been set by StartJob
     1377      Assert(ToWork[Loc, Job] < $FFFF); // should have been set by StartJob
    13771378      if Job >= jRoad then
    1378         if integer(Movement) >= integer(ToWork[Loc, Job]) then { work complete }
    1379         begin
    1380           result := true;
     1379        if Integer(Movement) >= Integer(ToWork[Loc, Job]) then { work complete }
     1380        begin
     1381          Result := True;
    13811382          if Job <> jIrr then
    1382             Health := Health - HostileDamage(p, mix, Loc, ToWork[Loc, Job]);
    1383           dec(Movement, ToWork[Loc, Job]);
     1383            Health := Health - HostileDamage(P, mix, Loc, ToWork[Loc, Job]);
     1384          Dec(Movement, ToWork[Loc, Job]);
    13841385          if not(Job in [jCity, jPillage, jPoll]) then
    1385             inc(Worked[p], ToWork[Loc, Job]);
     1386            Inc(Worked[P], ToWork[Loc, Job]);
    13861387          if Job = jCity then
    13871388          begin // found new city
    1388             FoundCity(p, Loc);
    1389             inc(Founded[p]);
    1390             with RW[p].City[RW[p].nCity - 1] do
     1389            FoundCity(P, Loc);
     1390            Inc(Founded[P]);
     1391            with RW[P].City[RW[P].nCity - 1] do
    13911392            begin
    1392               ID := p shl 12 + Founded[p] - 1;
     1393              ID := P shl 12 + Founded[P] - 1;
    13931394              Flags := chFounded;
    13941395            end;
    13951396            if Mode = moPlaying then
    13961397            begin
    1397               LogCheckBorders(p, RW[p].nCity - 1);
    1398               RecalcPeaceMap(p);
     1398              LogCheckBorders(P, RW[P].nCity - 1);
     1399              RecalcPeaceMap(P);
    13991400            end;
    14001401{$IFOPT O-} if Mode < moPlaying then
    1401               InvalidTreatyMap := not(1 shl p); {$ENDIF}
     1402              InvalidTreatyMap := not(1 shl P); {$ENDIF}
    14021403            // territory should not be considered for the rest of the command
    14031404            // execution, because during loading a game it's incorrect before
    14041405            // subsequent sIntExpandTerritory is processed
    1405             RW[p].Un[uix].Health := 0; // causes unit to be removed later
     1406            RW[P].Un[uix].Health := 0; // causes unit to be removed later
    14061407          end
    14071408          else
    1408             CompleteJob(p, Loc, Job);
     1409            CompleteJob(P, Loc, Job);
    14091410          ToWork[Loc, Job] := 0;
    14101411          j0 := Job;
    1411           for uix1 := 0 to RW[p].nUn - 1 do
    1412             if (RW[p].Un[uix1].Loc = Loc) and (RW[p].Un[uix1].Job = j0) then
    1413               RW[p].Un[uix1].Job := jNone
     1412          for uix1 := 0 to RW[P].nUn - 1 do
     1413            if (RW[P].Un[uix1].Loc = Loc) and (RW[P].Un[uix1].Job = j0) then
     1414              RW[P].Un[uix1].Job := jNone
    14141415        end
    14151416        else
    14161417        begin
    1417           dec(ToWork[Loc, Job], Movement);
     1418          Dec(ToWork[Loc, Job], Movement);
    14181419          if not(Job in [jCity, jPillage, jPoll]) then
    1419             inc(Worked[p], Movement);
    1420           Health := Health - HostileDamage(p, mix, Loc, Movement);
     1420            Inc(Worked[P], Movement);
     1421          Health := Health - HostileDamage(P, mix, Loc, Movement);
    14211422          Movement := 0;
    14221423        end
    14231424    end
    1424 end; // work
    1425 
    1426 function GetJobProgress(p, Loc: integer;
    1427   var JobProgressData: TJobProgressData): integer;
    1428 var
    1429   Job, JobResult, uix: integer;
     1425end;
     1426
     1427function GetJobProgress(P, Loc: Integer;
     1428  var JobProgressData: TJobProgressData): Integer;
     1429var
     1430  Job, JobResult, uix: Integer;
    14301431begin
    14311432  for Job := 0 to nJob - 1 do
    14321433  begin
    1433     JobResult := CalculateJobWork(p, Loc, Job, JobProgressData[Job].Required);
     1434    JobResult := CalculateJobWork(P, Loc, Job, JobProgressData[Job].Required);
    14341435    if JobResult = eOK then
    14351436    begin
     
    14381439      else
    14391440        JobProgressData[Job].Done := JobProgressData[Job].Required -
    1440           ToWork[Loc, Job]
     1441          ToWork[Loc, Job];
    14411442    end
    14421443    else
     
    14471448    JobProgressData[Job].NextTurnPlus := 0;
    14481449  end;
    1449   for uix := 0 to RW[p].nUn - 1 do
    1450     if (RW[p].Un[uix].Loc = Loc) and (RW[p].Un[uix].Movement >= 100) then
    1451       inc(JobProgressData[RW[p].Un[uix].Job].NextTurnPlus,
    1452         RW[p].Un[uix].Movement);
    1453   result := eOK;
     1450  for uix := 0 to RW[P].nUn - 1 do
     1451    if (RW[P].Un[uix].Loc = Loc) and (RW[P].Un[uix].Movement >= 100) then
     1452      Inc(JobProgressData[RW[P].Un[uix].Job].NextTurnPlus,
     1453        RW[P].Un[uix].Movement);
     1454  Result := eOK;
    14541455end;
    14551456
Note: See TracChangeset for help on using the changeset viewer.