unit uGmUtils;

interface

uses
  zglHeader, uGmCreature, uGmPathfind, uGmArena;

procedure GetPickedHex( X, Y : integer; var tx, ty : integer );
function GetCursorAtackAngle( tx, ty, CurX, CurY : integer ): integer;
function CrIsBlocked( cr : TCreature ): boolean;
function GetFreeTileY : integer;

implementation

//==============================================================================
procedure GetPickedHex( X, Y : integer; var tx, ty : integer );
var
  s, h, r, sectx, secty, sectPxlx, sectPxly : integer;
  m : single;
begin
  s := 32;
  h := 14;
  r := 27;
  X := X - 84 + 22;
  Y := Y - 100;

  m         := h / r;
  sectx     := X div ( 2 * r );
  secty     := Y div ( h + s );
  sectPxlx  := X mod ( 2 * r );
  sectPxly  := Y mod ( h + s );

  if secty mod 2 = 0 then
  begin
    if sectPxly < ( h - sectPxlx * m ) then
    begin
      secty := secty - 1;
      sectx := sectx - 1;
    end;

    if sectPxly < ( -h + sectPxlx * m ) then
      secty := secty - 1;
  end
  else
  begin
    if sectPxlx >= r then
      if sectPxly < ( 2 * h - sectPxlx * m ) then
        secty := secty - 1;

    if sectPxlx < r then
      if sectPxly < ( sectPxlx * m ) then
        secty := secty - 1
      else
        sectx := sectx - 1;
  end;

  tx := sectx;
  ty := secty;
  if secty mod 2 = 0 then tx := sectx - 1;
end;

//==============================================================================
function GetCursorAtackAngle( tx, ty, CurX, CurY : integer ): integer;
var
  cx, cy, ang2, n, n1, n2 : integer;
  ang : single;
begin
  cx    := tx * 54 + 84 + 32;
  cy    := ty * 46 + 100 + 32;
  if ty mod 2 = 0 then cx := cx + 27;
  ang   := m_Angle( cx, cy, CurX, CurY );
  ang2  := -1;

  if ( ang <= 30 ) or ( ang >= 330 ) then ang2 := 0;
  if ( ang > 30 ) and ( ang <= 90 ) then ang2 := 45;
  if ( ang > 90 ) and ( ang < 150 ) then ang2 := 135;
  if ( ang >= 150 ) and ( ang <= 210 ) then ang2 := 180;
  if ( ang > 210 ) and ( ang <= 270 ) then ang2 := 225;
  if ( ang > 270 ) and ( ang < 330 ) then ang2 := 315;

  Result := ang2;

  n := 0;
  if ang2 = 0   then n := W_L;
  if ang2 = 45  then n := W_LU;
  if ang2 = 135 then n := W_RU;
  if ang2 = 180 then n := W_R;
  if ang2 = 225 then n := W_RD;
  if ang2 = 315 then n := W_LD;

  if Wave_GetNear( tx, ty, n ) > 0 then Exit;

  n1 := n;
  n2 := n;
  n  := -1;
  repeat
    n1 := n1 - 1;
    n2 := n2 + 1;
    if n1 < 0 then n1 := 5;
    if n2 > 5 then n2 := 0;
    if Wave_GetNear( tx, ty, n2 ) > 0 then n := n2;    
    if Wave_GetNear( tx, ty, n1 ) > 0 then n := n1;
    if n <> -1 then
    begin
      if n = W_L  then ang2 := 0;
      if n = W_LU then ang2 := 45;
      if n = W_RU then ang2 := 135;
      if n = W_R  then ang2 := 180;
      if n = W_RD then ang2 := 225;
      if n = W_LD then ang2 := 315;
      Result := ang2;
      Exit;
    end;
  until n1 = n2;

  Result := -1;
end;

//==============================================================================
function CrIsBlocked( cr : TCreature ): boolean;
var
  i, wn : integer;
begin
  Result := False;
  for i := 0 to 5 do
  begin
    wn := Wave_GetNear( cr.TX, cr.TY, i );
    if ( ( cr.PlayerN = 1 ) and ( wn = -2 ) ) or ( ( cr.PlayerN = 2 ) and ( wn = -1 ) ) then
    begin
      Result := True;
      Exit;
    end;
  end;
end;

//==============================================================================
function GetFreeTileY : integer;
var
  i, j, k : integer;
  bool    : boolean;
begin
  Result := -1;
  for j := 0 to 8 do
  begin
    bool := True;
    k := j * 2 + 1;
    if j >= 4 then k := ( j - 4 ) * 2;

    for i := 0 to Arena.CreaturesCnt - 1 do
      if ( Arena.Creatures[ i ].TX = 0 ) and ( Arena.Creatures[ i ].TY = k ) then
      begin
        bool := False;
        Break;
      end;
    if bool = True then
    begin
      Result := k;
      Exit;
    end;
  end;
end;

end.
