unit g_Land_load;

interface
uses
  g_Data,
  {$INCLUDE uses.inc};

type
  izWayPoint = record
    pos : izPosition_f;
  end;
  zLineWayPoints = ^izLineWayPoints;
  izLineWayPoints = record
    WPoints : array of izWayPoint;
    WPointCount : word;
  end;
  cLineWayPoints = array of zLineWayPoints;

var
 //***Way points**\\
 LineWayPoints : cLineWayPoints;
 LineWPCount : byte = 0;
 ActiveLineWP : byte = 0;
 ActiveWP : byte;

procedure LoadMap(Name : string);
implementation

procedure AddLineWayPoint(var self : cLineWayPoints);
begin
  if LineWPCount < High(byte) then
  begin
    inc(LineWPCount);
    SetLength(self,LineWPCount);
    New(self[LineWPCount-1]);
    self[LineWPCount-1].WPointCount := 0;
    ActiveLineWP := LineWPCount;
  end;
end;

procedure AddWayPoint(var self : cLineWayPoints;x,y : word);
begin
  if self[ActiveLineWP-1].WPointCount < High(word) then
  begin
    inc(self[ActiveLineWP-1].WPointCount);
    SetLength(self[ActiveLineWP-1].WPoints,self[ActiveLineWP-1].WPointCount);
    self[ActiveLineWP-1].WPoints[self[ActiveLineWP-1].WPointCount-1].pos[0] := x;
    self[ActiveLineWP-1].WPoints[self[ActiveLineWP-1].WPointCount-1].pos[1] := y;
  end;
end;

procedure LoadWP(var f : TextFile; self : cLineWayPoints);
var
  i,j : word;
  c : string;
  s,wpc:word;
  pos1,pos2 : word;
begin
  i := 0;
  ReadLn(f,c);
  ReadLn(f,s);
  while i < s do
  begin
    j := 0;
    ReadLn(f,c);
    ReadLn(f,wpc);
    AddLineWayPoint(LineWayPoints);
    while j<wpc do
    begin
      ReadLn(f,pos1);
      ReadLn(f,pos2);
      AddWayPoint(LineWayPoints,pos1,pos2);
      inc(j);
    end;
    inc(i);
  end;
end;

procedure LoadMap(Name : string);
var
  f : TextFile;
  i,j : byte;
  s : string;
begin
  AssignFile(f,Name);
  Reset(f);
  LoadWP(f,LineWayPoints);
  readLn(f,World.MapSize_num[0]);
  readLn(f,World.MapSize_num[1]);
  for i := 0 to World.MapSize_num[0]-1 do begin
    SetLength(Land,i+1);
    for j := 0 to World.MapSize_num[1]-1 do
    begin
      setLength(Land[i],j+1);
      ReadLn(f,Land[i][j].TypeID);
      ReadLn(f,Land[i][j].Block);
    end;
  end;
  j := 0;
  ReadLn(f,ObjCount);
  setLength(Obj,ObjCount);
  While j<ObjCount do
  begin
    ReadLn(f,Obj[j].TypeID);
    ReadLn(f,Obj[j].pos[0]);
    ReadLn(f,Obj[j].pos[1]);
    inc(j);
  end;
  CloseFile(f);
  gfx.MsgOk('Карта ' + Name + ' - загружена');
end;

end.
