Changeset 223 for trunk/UClientAI.pas


Ignore:
Timestamp:
Jul 14, 2018, 7:21:43 PM (6 years ago)
Author:
chronos
Message:
  • Modified: CellMoves moved from terrain map to player map. Every player should have own moves to avoid affecting moves of different player.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UClientAI.pas

    r222 r223  
    2323    procedure Process;
    2424    procedure FallBack;
    25     function AttackersCount(Cell: TCell): Integer;
     25    function AttackersCount(Cell: TPlayerCell): Integer;
    2626  end;
    2727
     
    3939procedure TComputer.AttackNeutral;
    4040var
    41   AllCells: TCells;
     41  AllCells: TPlayerCells;
    4242  TotalPower: Integer;
    4343  AttackPower: Integer;
    4444  TotalAttackPower: Integer;
    4545  CanAttack: Integer;
    46   TargetCells: TCells;
    47   Cell: TCell;
    48   NeighborCell: TCell;
     46  TargetCells: TPlayerCells;
     47  Cell: TPlayerCell;
     48  NeighborCell: TPlayerCell;
    4949const
    5050  AttackDiff = 1;
    5151begin
    52   AllCells := Game.Map.Cells;
    53   TargetCells := TCells.Create;
     52  AllCells := ControlPlayer.PlayerMap.Cells;
     53  TargetCells := TPlayerCells.Create;
    5454  TargetCells.FreeObjects := False;
    5555
     
    5757  for Cell in AllCells do
    5858  with Cell do begin
    59     if (Terrain <> ttVoid) and (Player = nil) then begin
     59    if (MapCell.Terrain <> ttVoid) and (MapCell.Player = nil) then begin
    6060      CanAttack := 0;
    6161      for NeighborCell in Neighbors do
    62       if NeighborCell.Player = ControlPlayer then begin
     62      if NeighborCell.MapCell.Player = ControlPlayer then begin
    6363        Inc(CanAttack);
    6464      end;
     
    7777      TotalPower := 0;
    7878      for NeighborCell in Neighbors do
    79       if NeighborCell.Player = ControlPlayer then
     79      if NeighborCell.MapCell.Player = ControlPlayer then
    8080        TotalPower := TotalPower + NeighborCell.GetAvialPower;
    8181
    8282      // Attack if target is weaker
    83       if TotalPower >= (Power + AttackDiff) then begin
     83      if TotalPower >= (MapCell.Power + AttackDiff) then begin
    8484        TotalAttackPower := 0;
    8585        for NeighborCell in Neighbors do
    86         if NeighborCell.Player = ControlPlayer then begin
     86        if NeighborCell.MapCell.Player = ControlPlayer then begin
    8787          // Use only necessary power
    88           AttackPower := Power - TotalAttackPower + AttackDiff;
     88          AttackPower := MapCell.Power - TotalAttackPower + AttackDiff;
    8989          if NeighborCell.GetAvialPower < AttackPower then
    9090            AttackPower := NeighborCell.GetAvialPower;
     
    100100procedure TComputer.AttackPlayers;
    101101var
    102   AllCells: TCells;
     102  AllCells: TPlayerCells;
    103103  TotalPower: Integer;
    104104  AttackPower: Integer;
    105105  TotalAttackPower: Integer;
    106106  CanAttack: Integer;
    107   TargetCells: TCells;
    108   TargetCell: TCell;
    109   NeighborCell: TCell;
     107  TargetCells: TPlayerCells;
     108  TargetCell: TPlayerCell;
     109  NeighborCell: TPlayerCell;
    110110begin
    111111if ControlPlayer.Defensive then Exit;
    112112
    113   AllCells := Game.Map.Cells;
    114   TargetCells := TCells.Create;
     113  AllCells := ControlPlayer.PlayerMap.Cells;
     114  TargetCells := TPlayerCells.Create;
    115115  TargetCells.FreeObjects := False;
    116116
    117117  // Get list of all attack target cells
    118118  for TargetCell in AllCells do begin
    119     if (TargetCell.Terrain <> ttVoid) and (TargetCell.Player <> ControlPlayer) and
    120       (TargetCell.Player <> nil) then begin
     119    if (TargetCell.MapCell.Terrain <> ttVoid) and (TargetCell.MapCell.Player <> ControlPlayer) and
     120      (TargetCell.MapCell.Player <> nil) then begin
    121121      CanAttack := 0;
    122122      for NeighborCell in TargetCell.Neighbors do
    123       if NeighborCell.Player = ControlPlayer then begin
     123      if NeighborCell.MapCell.Player = ControlPlayer then begin
    124124        Inc(CanAttack);
    125125      end;
     
    137137      TotalPower := 0;
    138138      for NeighborCell in TargetCell.Neighbors do
    139       if NeighborCell.Player = ControlPlayer then begin
     139      if NeighborCell.MapCell.Player = ControlPlayer then begin
    140140        TotalPower := TotalPower + NeighborCell.GetAvialPower;
    141141      end;
    142142      // Attack if target is weaker
    143       if Game.AttackProbability(TotalPower, TargetCell.Power) >=
     143      if Game.AttackProbability(TotalPower, TargetCell.MapCell.Power) >=
    144144        ComputerAggroProbability[ControlPlayer.Agressivity] then begin
    145145        // Try to limit total attacking power to necessary minimum
    146         while Game.AttackProbability(TotalPower - 1, TargetCell.Power) >=
     146        while Game.AttackProbability(TotalPower - 1, TargetCell.MapCell.Power) >=
    147147          ComputerAggroProbability[ControlPlayer.Agressivity] do
    148148          Dec(TotalPower);
     
    151151        TotalAttackPower := 0;
    152152        for NeighborCell in TargetCell.Neighbors do
    153         if NeighborCell.Player = ControlPlayer then begin
     153        if NeighborCell.MapCell.Player = ControlPlayer then begin
    154154          // Use only necessary power
    155155          AttackPower := TotalPower - TotalAttackPower;
     
    168168procedure TComputer.InnerMoves;
    169169var
    170   AllCells: TCells;
     170  AllCells: TPlayerCells;
    171171  I, J: Integer;
    172172  C: Integer;
    173173  CanAttack: Integer;
    174   TargetCells: TCells;
    175   NewTargetCells: TCells;
    176   Cells2: TCells;
     174  TargetCells: TPlayerCells;
     175  NewTargetCells: TPlayerCells;
     176  Cells2: TPlayerCells;
    177177  MovedPower: Integer;
    178178begin
    179179  // We need to move available power to borders to be available for attacks
    180180  // or defense
    181   AllCells := Game.Map.Cells;
    182   TargetCells := TCells.Create;
     181  AllCells := ControlPlayer.PlayerMap.Cells;
     182  TargetCells := TPlayerCells.Create;
    183183  TargetCells.FreeObjects := False;
    184   NewTargetCells := TCells.Create;
     184  NewTargetCells := TPlayerCells.Create;
    185185  NewTargetCells.FreeObjects := False;
    186186
     
    188188  for C := 0 to AllCells.Count - 1 do
    189189  with AllCells[C] do begin
    190     if (Player <> ControlPlayer) and (Player <> nil) and (Terrain <> ttVoid) then begin
     190    if (MapCell.Player <> ControlPlayer) and (MapCell.Player <> nil) and (MapCell.Terrain <> ttVoid) then begin
    191191      CanAttack := 0;
    192192      for I := 0 to Neighbors.Count - 1 do
    193       if ((Neighbors[I].Player = ControlPlayer) or
    194       (Neighbors[I].Player = nil)) and (Neighbors[I].Terrain <> ttVoid) then begin
     193      if ((Neighbors[I].MapCell.Player = ControlPlayer) or
     194      (Neighbors[I].MapCell.Player = nil)) and (Neighbors[I].MapCell.Terrain <> ttVoid) then begin
    195195        Inc(CanAttack);
    196196      end;
     
    210210    // Set mark for selected border cells
    211211    for C := 0 to TargetCells.Count - 1 do
    212       TargetCells[C].Mark := True;
     212      TargetCells[C].MapCell.Mark := True;
    213213
    214214    // Move all power from unmarked cells and mark them
     
    217217    with TargetCells[C] do begin
    218218      for I := 0 to Neighbors.Count - 1 do begin
    219         if (Neighbors[I].Terrain <> ttVoid) and (not Neighbors[I].Mark) then begin
    220           if (TargetCells[C].Player = ControlPlayer) and
    221           (Neighbors[I].Player = ControlPlayer) then begin
     219        if (Neighbors[I].MapCell.Terrain <> ttVoid) and (not Neighbors[I].MapCell.Mark) then begin
     220          if (TargetCells[C].MapCell.Player = ControlPlayer) and
     221          (Neighbors[I].MapCell.Player = ControlPlayer) then begin
    222222            // Do not take units from front line
    223223            Cells2 := Neighbors[I].Neighbors;
    224224            CanAttack := 0;
    225225            for J := 0 to Cells2.Count - 1 do
    226             if ((Cells2[J].Player <> ControlPlayer) or (Cells2[J].Player = nil))
    227             and (Cells2[J].Terrain <> ttVoid) then begin
     226            if ((Cells2[J].MapCell.Player <> ControlPlayer) or (Cells2[J].MapCell.Player = nil))
     227            and (Cells2[J].MapCell.Terrain <> ttVoid) then begin
    228228              Inc(CanAttack);
    229229            end;
     
    232232              if (TargetCells[C].GetAvialPower + TargetCells[C].GetAttackPower + MovedPower) > Game.Map.MaxPower then
    233233                MovedPower := Game.Map.MaxPower - TargetCells[C].GetAvialPower - TargetCells[C].GetAttackPower;
    234               Player.SetMove(Neighbors[I], TargetCells[C], MovedPower, False);
     234              MapCell.Player.SetMove(Neighbors[I], TargetCells[C], MovedPower, False);
    235235            end;
    236236          end;
    237           Neighbors[I].Mark := True;
     237          Neighbors[I].MapCell.Mark := True;
    238238          NewTargetCells.Add(Neighbors[I]);
    239239        end;
     
    244244    FreeAndNil(TargetCells);
    245245    TargetCells := NewTargetCells;
    246     NewTargetCells := TCells.Create;
     246    NewTargetCells := TPlayerCells.Create;
    247247    NewTargetCells.FreeObjects := False;
    248248  end;
     
    281281  C: Integer;
    282282  I: Integer;
    283   AllCells: TCells;
    284   BorderCells: TCells;
     283  AllCells: TPlayerCells;
     284  BorderCells: TPlayerCells;
    285285  EnemyPower: Integer;
    286286begin
    287   BorderCells := TCells.Create;
     287  BorderCells := TPlayerCells.Create;
    288288  BorderCells.FreeObjects := False;
    289   AllCells := Game.Map.Cells;
     289  AllCells := ControlPlayer.PlayerMap.Cells;
    290290
    291291  // Get list of border cells
    292292  for C := 0 to AllCells.Count - 1 do
    293293  with AllCells[C] do begin
    294     if (Terrain <> ttVoid) and (Player = ControlPlayer)  then begin
     294    if (MapCell.Terrain <> ttVoid) and (MapCell.Player = ControlPlayer)  then begin
    295295      if AttackersCount(AllCells[C]) > 0 then
    296296        BorderCells.Add(AllCells[C]);
     
    305305    EnemyPower := 0;
    306306    for I := 0 to Neighbors.Count - 1 do
    307     if (Neighbors[I].Player <> ControlPlayer) and (Neighbors[I].Player <> nil) then begin
    308       Inc(EnemyPower, Neighbors[I].Power);
     307    if (Neighbors[I].MapCell.Player <> ControlPlayer) and (Neighbors[I].MapCell.Player <> nil) then begin
     308      Inc(EnemyPower, Neighbors[I].MapCell.Power);
    309309    end;
    310310    if EnemyPower > (GetAvialPower + GetAttackPower) then begin
    311311      // Fallback
    312312      for I := MovesTo.Count - 1 downto 0 do
    313         Player.Moves.Remove(MovesTo[I]);
     313        MapCell.Player.Moves.Remove(MovesTo[I]);
    314314      for I := 0 to Neighbors.Count - 1 do
    315       if (Neighbors[I].Player = Player) and (AttackersCount(Neighbors[I]) = 0) then begin
    316         Player.SetMove(BorderCells[C], Neighbors[I], GetAvialPower, False);
     315      if (Neighbors[I].MapCell.Player = MapCell.Player) and (AttackersCount(Neighbors[I]) = 0) then begin
     316        MapCell.Player.SetMove(BorderCells[C], Neighbors[I], GetAvialPower, False);
    317317        Break;
    318318      end;
     
    323323end;
    324324
    325 function TComputer.AttackersCount(Cell: TCell): Integer;
     325function TComputer.AttackersCount(Cell: TPlayerCell): Integer;
    326326var
    327327  I: Integer;
     
    329329  Result := 0;
    330330  for I := 0 to Cell.Neighbors.Count - 1 do
    331   if (Cell.Neighbors[I].Player <> ControlPlayer) and
    332     (Cell.Neighbors[I].Player <> nil) then begin
     331  if (Cell.Neighbors[I].MapCell.Player <> ControlPlayer) and
     332    (Cell.Neighbors[I].MapCell.Player <> nil) then begin
    333333      Inc(Result);
    334334  end;
Note: See TracChangeset for help on using the changeset viewer.