unit g_Land;

interface
uses
  g_Data,g_Land_load,g_Base,
  GPAHeader,
  {$INCLUDE uses.inc};

var
  LandTex,LandSpr,
  ObjTex,ObjSpr : word;

procedure LandInit(MapName : string);
procedure LandDraw(i,j : byte);
procedure ObjDraw(i : word);
procedure WayPointsDraw();
implementation

procedure LandInit(MapName : string);
var
  size : izSize_w;
  i,j : word;
begin
  LoadMap(MapName);
  World.MapSize[0] := World.MapSize_num[0]*64;
  World.MapSize[1] := World.MapSize_num[1]*64;
  Size[0] := 64;
  Size[1] := 64;
  GPAExtractToFileByID(GPAGetIDByName('land.tga',false), 'temp\graphics\land.tga');
  LandTex := Gfx.TexMan.Load(iz_dir_local_temp_gfx + 'land.tga');
  LandSpr := Gfx.SprMan.Add(Gfx.TexMan.GetSize(LandTex),size);
  Size[0] := 64;
  Size[1] := 64;
  GPAExtractToFileByID(GPAGetIDByName('obj.tga',false), 'temp\graphics\obj.tga');
  ObjTex := Gfx.TexMan.Load(iz_dir_local_temp_gfx + 'obj.tga');
  ObjSpr := Gfx.SprMan.Add(Gfx.TexMan.GetSize(ObjTex),size);
  for i := 0 to World.MapSize_num[0]-1 do
  for j := 0 to World.MapSize_num[1]-1 do
  begin
    Land[i,j].pos[0] := i*64+Size[0]/2;
    Land[i,j].pos[1] := j*64+Size[1]/2;
    if Land[i,j].typeID = 1 then Land[i,j].typeID := random(4)+1;
  end;
  for i := 0 to ObjCount - 1 do
  begin
    Obj[i].size[0] := 96;
    Obj[i].size[1] := 96;
    Obj[i].pos[0] := Obj[i].pos[0]+Size[0]/2;
    Obj[i].pos[1] := Obj[i].pos[1]+Size[1]/2;
    Obj[i].Rad := 64;
    Obj[i].angle := random(360);
    if Obj[i].typeID = 0 then Base_Load(@World.Base,Obj[i].pos) else Obj[i].typeID := random(64);
  end;
end;

procedure LandDraw(i,j : byte);
begin
  Gfx.TexMan.Bind(LandTex);
  Gfx.SprMan.Draw_Full(LandSpr,Land[i][j].typeID,Land[i][j].Pos,0);
end;

procedure ObjDraw(i : word);
begin
  Gfx.TexMan.Bind(ObjTex);
  Gfx.SprMan.Draw_Alpha_Scale(ObjSpr,Obj[i].typeID,Obj[i].Pos,Obj[i].size,Obj[i].Angle);
end;

procedure WayPointsDraw();
var i,j : word;
begin
  glPushAttrib(GL_Current_BIT);
  i := 0;
  while i<LineWPCount do
  begin
    j:=0;
    while j<LineWayPoints[i].WPointCount do
    begin
      glColor3f(1,1,1);
      if j <> 0 then begin
      glBegin(GL_LINES);
        glVertex2f(LineWayPoints[i].WPoints[j-1].pos[0],LineWayPoints[i].WPoints[j-1].pos[1]);
        glVertex2f(LineWayPoints[i].WPoints[j].pos[0],LineWayPoints[i].WPoints[j].pos[1]);
      glEnd();
      end;
      glColor3f(0,1,0);
      glBegin(GL_QUADS);
        glVertex2f(LineWayPoints[i].WPoints[j].pos[0]-5,LineWayPoints[i].WPoints[j].pos[1]-5);
        glVertex2f(LineWayPoints[i].WPoints[j].pos[0]-5,LineWayPoints[i].WPoints[j].pos[1]+5);
        glVertex2f(LineWayPoints[i].WPoints[j].pos[0]+5,LineWayPoints[i].WPoints[j].pos[1]+5);
        glVertex2f(LineWayPoints[i].WPoints[j].pos[0]+5,LineWayPoints[i].WPoints[j].pos[1]-5);
      glEnd();
      inc(j);
    end;
    inc(i);
  end;
  glPopAttrib();
end;

end.
