unit loading;

interface

uses SysUtils, vfm, heights_map, constants, buildings, units,
     variables, path_map, graphics, global, respawns, textures;

procedure LoadLevel(file_name : string);

var level_operators : array[1..max_level_operators] of string =
    ('MAP_SCALE','LANDSCAPE','FOG','MUSIC','RAIN');

implementation

function GetString(n : integer; s : string) : string;
var
i, c : integer;
t : string;
begin
c := 1; s := ' ' + s + ' ';
for i := 0 to length(s) do
  if (copy(s,i,1) <> ' ') then
    t := t + copy(s,i,1)
  else
    if (t <> ' ')and(t <> '') then
      if c <> n then
        begin
          c := c + 1;
          t := '';
        end
      else
        begin
          GetString := t;
          exit;
        end;
end;

procedure LoadPathMap(var pm : RPathMap; file_name : string);
var
b : tbitmap;
i1, i2 : integer;
begin
b := tbitmap.create;
b.LoadFromFile(file_name);
b.width := pm.width;
b.height := pm.height;

for i1 := 1 to pm.width do
  for i2 := 1 to pm.height do
    with pm.map[i1, i2] do
      begin
        walk := true; build := false; child_enemy_id := 0;
        dangerous := 6;
        if ((i1 < 2)or(i1 > pathmap.width-1) or
            (i2 < 2)or(i2 > pathmap.height-1)) then
          begin walk := false; build := false; dangerous := 0; end;
      end;

for i1 := 1 to pm.width do
  for i2 := 1 to pm.height do
    with pm.map[i1, i2] do              
      case b.canvas.pixels[i1 - 1, b.height - i2 + 1] of
        clRed : walk := false;
        clNavy : begin
                   gBase := TBase.Create(i1 + 1, i2 + 1);
                   b.canvas.pixels[i1, b.height - i2 + 1] := clWhite;
                   b.canvas.pixels[i1, b.height - i2] := clWhite;
                   b.canvas.pixels[i1, b.height - i2 - 1] := clWhite;
                   b.canvas.pixels[i1 - 1, b.height - i2] := clWhite;
                   b.canvas.pixels[i1 - 1, b.height - i2 - 1] := clWhite;
                   b.canvas.pixels[i1 + 1, b.height - i2 + 1] := clWhite;
                   b.canvas.pixels[i1 + 1, b.height - i2] := clWhite;
                   b.canvas.pixels[i1 + 1, b.height - i2 - 1] := clWhite;
                 end;
        clGreen : if total_trees < max_trees then
                    begin
                      total_trees := total_trees + 1;
                      gTrees[total_trees] := TTree.Create(i1, i2);
                      gTrees[total_trees].stem.id := id_trees + total_trees;
                      gTrees[total_trees].leaves.id := gTrees[total_trees].stem.id;
                      child_id := total_trees;
                      b.canvas.pixels[i1, b.height - i2 + 1] := clRed;
                      b.canvas.pixels[i1, b.height - i2] := clRed;
                      b.canvas.pixels[i1 - 1, b.height - i2] := clRed;
                    end;
        clYellow : if total_respawns < max_respawns then
                    begin
                      total_respawns := total_respawns + 1;
                      gRespawns[total_respawns] := TRespawn.Create(i1, i2);
                    end;
        clLime : if total_buildplaces < max_buildplaces then
                    begin
                      total_buildplaces := total_buildplaces + 1;
                      gBuildPlaces[total_buildplaces] :=
                        TBuildPlace.Create(i1, i2);
                      gBuildPlaces[total_buildplaces].obj.id :=
                        id_buildplaces + total_buildplaces;
                      child_id := total_buildplaces;
                      end;
      end;

b.free;
end;

procedure GetLevelInfo(i : integer; s : string);
var
i1, i2 : integer;
begin
case i of
1 : begin
      landscape := THeightsMap.Create;
      with landscape do
        begin
          sx := strtofloat(GetString(2, s));
          sy := strtofloat(GetString(3, s));
          sz := strtofloat(GetString(4, s));
        end;
    end;
2 : begin
      landscape.LoadFromImageFile(GetString(2, s), GetString(3, s), GetString(4, s));
      with pathmap do
        begin
          width := landscape.width;
          height := landscape.height;
          LoadPathMap(pathmap, GetString(5, s));
        end;
    end;
3 : begin
      fog_destination_density := strtofloat(GetString(2, s));
      fog_height_density := strtofloat(GetString(3, s));
    end;
4 : music_file_name := GetString(2, s);
5 : if GetString(2, s) = '1' then rain_aviable := true;
end;
end;

procedure LoadLevel(file_name : string);
var
f : textfile;
i1, i2 : integer;
s : string;
begin
if not fileexists(file_name) then exit;
assignfile(f, file_name);
reset(f);
while not eof(f) do
  begin
    readln(f, s);
    for i1 := 1 to max_level_operators do
      if getstring(1,s) = level_operators[i1] then
        GetLevelInfo(i1,s);
  end;
closefile(f);
end;

end.
