unit uGmAI;

interface

uses
  zglHeader, uGmArena, uGmCreature, uGmPathfind, uGmUtils;

procedure AI_Kumekaet;

implementation

//==============================================================================
procedure AI_Kumekaet;
var
  i, j, nt  : integer;
  tw, twmin : integer;
  HPmin     : integer;
  x2, y2    : integer;
  Enemy     : array of TCreature;
  EnemyCnt  : integer;
  EnemyAt   : TCreature;
begin
  CreateWave( SelCr.TX, SelCr.TY, 100, SelCr.Pat.CanFly );

  EnemyCnt := 0;
  for i := 0 to Arena.CreaturesCnt - 1 do
    if ( Arena.Creatures[ i ].State <> CS_DIE ) and ( Arena.Creatures[ i ].PlayerN <> SelCr.PlayerN ) then
    begin
      INC( EnemyCnt );
      SetLength( Enemy, EnemyCnt );
      Enemy[ EnemyCnt - 1 ] := Arena.Creatures[ i ];
    end;

  if ( SelCr.Pat.CanShoot ) and ( CrIsBlocked( SelCr ) = False ) then
  begin
    SelCr.Shoot := True;
    SelCr.State := CS_ATACK;
    HPmin   := 1000;
    EnemyAt := nil;
    for i := 0 to EnemyCnt - 1 do
      if Enemy[ i ].Health < HPmin then
      begin
        EnemyAt := Enemy[ i ];
        HPmin   := Enemy[ i ].Health;
      end;
    SelCr.AtackCr := EnemyAt;
    Exit;
  end;

  EnemyAt := nil;
  twmin   := 100;
  nt      := 0;

  for i := 0 to EnemyCnt - 1 do
    for j := 0 to 5 do
    begin
      tw := Wave_GetNear( Enemy[ i ].TX, Enemy[ i ].TY, j );
      if ( tw > 0 ) and ( tw < twmin ) then
      begin
        twmin   := tw;
        EnemyAt := Enemy[ i ];
        nt      := j;
      end;
    end;

  if EnemyAt = nil then
  begin
    SelCr.Pohodil := True;
    Exit;
  end;

  if twmin = 1 then
  begin
    SelCr.State   := CS_ATACK;
    SelCr.AtackCr := EnemyAt;
    SelCr.Shoot   := SelCr.Pat.CanShoot;
    SelCr.LookLeft    := ( EnemyAt.X < SelCr.X );
    EnemyAt.LookLeft  := ( EnemyAt.X > SelCr.X );
    Exit;
  end;

  x2 := EnemyAt.TX;
  y2 := EnemyAt.TY;
  if nt = W_R then x2 := x2 + 1;
  if nt = W_L then x2 := x2 - 1;
  if ( nt = W_RD ) or ( nt = W_LD ) then
  begin
    if ( y2 mod 2 = 0 ) and ( nt = W_RD ) then x2 := x2 + 1;
    if ( y2 mod 2 <> 0 ) and ( nt = W_LD ) then x2 := x2 - 1;
    y2 := y2 + 1;
  end;
  if ( nt = W_LU ) or ( nt = W_RU ) then
  begin
    if ( y2 mod 2 = 0 ) and ( nt = W_RU ) then x2 := x2 + 1;
    if ( y2 mod 2 <> 0 ) and ( nt = W_LU ) then x2 := x2 - 1;
    y2 := y2 - 1;
  end;

  CreatePath( SelCr, x2, y2 );
  SelCr.State := CS_WALK;
  if SelCr.PathLen <= SelCr.Pat.Speed then SelCr.AtackCr := EnemyAt;
end;

end.
