Changeset 274 for trunk/UPlayer.pas


Ignore:
Timestamp:
Feb 3, 2019, 8:32:20 PM (6 years ago)
Author:
chronos
Message:
  • Added: Keep information about available unit moves during turn.
  • Added: Allow to move units instantly during turn.
  • Added: Game system parameter to play without possibility to merge/split units.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UPlayer.pas

    r273 r274  
    2828    procedure DisconnectFrom(Cell: TPlayerCell);
    2929    function GetAvialPower: Integer;
     30    function GetAvialPowerForMove: Integer;
    3031    function GetAttackPower: Integer;
    3132    procedure LoadFromNode(Node: TDOMNode);
     
    102103    procedure CheckCounterMove(Move: TUnitMove);
    103104    procedure SetMode(AValue: TPlayerMode);
    104     procedure RemoveEmptyUnitMoves;
    105105  public
    106106    Id: Integer;
     
    120120    Moves: TUnitMoves;
    121121    Units: TUnits;
     122    function IsAllowedMoveTarget(CellFrom, CellTo: TPlayerCell): Boolean;
    122123    procedure ReduceMovesPower;
    123124    procedure RemoveInvalidMoves;
    124125    procedure RemoveEmptyUnits;
     126    procedure InitUnitMoves;
    125127    procedure UpdateRepeatMoves;
    126128    procedure UpdateEmptyCellsNeutral;
     
    205207
    206208uses
    207   UGame;
     209  UGame, UGameSystem;
    208210
    209211resourcestring
     
    262264  UnitMove: TUnitMove;
    263265begin
    264   if Assigned(MapCell.OneUnit) then Result := MapCell.OneUnit.Power
    265     else Result := 0;
     266  Result := 0;
     267  if Assigned(MapCell.OneUnit) then
     268    Result := MapCell.OneUnit.Power;
     269  for UnitMove in MovesFrom do
     270    Result := Result - UnitMove.CountOnce;
     271end;
     272
     273function TPlayerCell.GetAvialPowerForMove: Integer;
     274var
     275  UnitMove: TUnitMove;
     276begin
     277  Result := 0;
     278  if Assigned(MapCell.OneUnit) then
     279    if MapCell.OneUnit.Moves > 0 then Result := MapCell.OneUnit.Power;
    266280  for UnitMove in MovesFrom do
    267281    Result := Result - UnitMove.CountOnce;
     
    9991013      // Inner move
    10001014      CellTo.MapCell.OneUnit.Power := CellTo.MapCell.OneUnit.Power + UnitCount;
     1015      Dec(CellTo.MapCell.OneUnit.Moves);
    10011016    end else begin
    10021017      AttackerPower := UnitCount;
     
    10151030        // Defender wins with possible loses
    10161031        CellTo.MapCell.OneUnit.Power := DefenderPower;
     1032        CellTo.MapCell.OneUnit.Moves := CellFrom.MapCell.OneUnit.Moves - 1;
    10171033      end else
    10181034        raise Exception.Create(SUnfinishedBattle);
     
    10201036    CellFrom.MapCell.OneUnit.Power := CellFrom.MapCell.OneUnit.Power - UnitCount;
    10211037  end;
     1038
     1039  // Clean from cell from empty units
     1040  if UnitMove.CellFrom.MapCell.OneUnit.Power = 0 then begin
     1041    Units.Remove(UnitMove.CellFrom.MapCell.OneUnit);
     1042    UnitMove.CellFrom.MapCell.OneUnit := nil;
     1043  end;
     1044
     1045  // Remove empty move
     1046  if (UnitMove.CellFrom.MapCell.Player = Self) and
     1047    (UnitMove.CountOnce = 0) and (UnitMove.CountRepeat = 0) then
     1048    Moves.Remove(UnitMove);
    10221049end;
    10231050
    10241051procedure TPlayer.MoveAll;
    10251052var
    1026   UnitMove: TUnitMove;
    1027 begin
    1028   for UnitMove in Moves do
    1029   with UnitMove do
     1053  I: Integer;
     1054begin
     1055  for I := Moves.Count - 1 downto 0 do
     1056  with Moves[I] do
    10301057  if CountOnce > 0 then begin
    10311058    if CellFrom.MapCell.Player = Self then begin
    1032       MoveUnit(UnitMove);
    1033     end;
    1034   end;
    1035 
    1036   RemoveEmptyUnitMoves;
     1059      MoveUnit(Moves[I]);
     1060    end;
     1061  end;
    10371062end;
    10381063
     
    10771102end;
    10781103
     1104procedure TPlayer.InitUnitMoves;
     1105var
     1106  I: Integer;
     1107begin
     1108  for I := 0 to Units.Count - 1 do
     1109    TUnit(Units[I]).Moves := TUnit(Units[I]).Kind.Moves;
     1110end;
     1111
    10791112procedure TPlayer.ClearMovesFromCell(Cell: TPlayerCell);
    10801113var
     
    11051138
    11061139  if Assigned(Result) then begin
     1140    // Update existing move
    11071141    CountOnce := Result.CountOnce;
    11081142    CountRepeat := Result.CountRepeat;
     
    11111145        FOnMove(CellFrom, CellTo, CountOnce, CountRepeat, True, Confirm);
    11121146  end else begin
     1147    // Create new move
    11131148    CountOnce := Power;
    11141149    CountRepeat := 0;
     
    11391174        Result := NewMove;
    11401175        CheckCounterMove(NewMove);
     1176        if TGame(Game).GameSystem.UnitsMoveImmediately then begin
     1177          MoveUnit(NewMove);
     1178        end;
    11411179      end;
    11421180    end;
     
    11601198    end;
    11611199  end;
    1162   RemoveEmptyUnitMoves;
    11631200end;
    11641201
     
    11731210      MapCell.Player := nil;
    11741211  end;
    1175 end;
    1176 
    1177 procedure TPlayer.RemoveEmptyUnitMoves;
    1178 var
    1179   I: Integer;
    1180 begin
    1181   for I := Moves.Count - 1 downto 0 do
    1182   if (TUnitMove(Moves[I]).CellFrom.MapCell.Player = Self) and
    1183     (TUnitMove(Moves[I]).CountOnce = 0) and (TUnitMove(Moves[I]).CountRepeat = 0) then
    1184     Moves.Delete(I);
    11851212end;
    11861213
     
    12301257end;
    12311258
     1259function TPlayer.IsAllowedMoveTarget(CellFrom, CellTo: TPlayerCell): Boolean;
     1260var
     1261  CellToHasOwnUnit: Boolean;
     1262  UnitHasMoveEnergy: Boolean;
     1263begin
     1264  CellToHasOwnUnit := Assigned(CellTo.MapCell.OneUnit) and
     1265    (CellTo.MapCell.OneUnit.Player = Self) and (CellTo.MapCell.OneUnit.Power > 0);
     1266  UnitHasMoveEnergy := Assigned(CellFrom.MapCell.OneUnit) and
     1267    (CellFrom.MapCell.OneUnit.Moves > 0);
     1268  Result := UnitHasMoveEnergy and
     1269  TGame(Game).Map.IsCellsNeighbor(CellFrom.MapCell, CellTo.MapCell)
     1270    and (TGame(Game).GameSystem.UnitsSplitMerge or
     1271    (not TGame(Game).GameSystem.UnitsSplitMerge and not CellToHasOwnUnit));
     1272end;
     1273
    12321274procedure TPlayer.Grow;
    12331275var
     
    12351277  Addition: Integer;
    12361278  Dies: Integer;
     1279  NewUnit: TUnit;
    12371280begin
    12381281  with TGame(Game).Map do
     
    12411284    if (Player = Self) and ((TGame(Game).GrowCells = gcPlayerAll) or
    12421285    ((TGame(Game).GrowCells = gcPlayerCities) and (Terrain = ttCity))) then begin
     1286      if not Assigned(OneUnit) then begin
     1287        NewUnit := TGame(Game).Units.AddNew(TGame(Game).GameSystem.UnitKinds.First, 0);
     1288        NewUnit.Player := Self;
     1289        NewUnit.MapCell := TCell(Cells[I]);
     1290        Units.Add(NewUnit);
     1291      end;
    12431292      if OneUnit.Power < MaxPower then begin
    12441293        // Increase units count
Note: See TracChangeset for help on using the changeset viewer.