unit g_Monster_Load;

interface
uses
  g_Data,g_Types,g_Character_Load,g_Land_load,g_Utils,
  {$INCLUDE uses.inc};

procedure Monster_Load(self : zMonster);
procedure Monster_Add_Update;
procedure Monster_Add; overload;
procedure Monster_Add(self : zMonster; count : word); overload;
procedure Monster_Add_New(self : zMonster; count : word);
implementation

procedure Monster_Add_Update;
begin
  World.MonsterCreateTTC := GettickCount;
  if World.MonsterCreateTTC-World.MonsterCreateLTC > World.MonsterCreateDelay then
  begin
    if World.EndCharAC < World.CharacterCount then
    if World.EndCharAC <= (World.CharacterCount-World.MaxMonster) then inc(World.EndCharAC,World.MonAddCoef) else World.EndCharAC := World.CharacterCount;
    World.MonsterCreateLTC := World.MonsterCreateTTC;
  end;
end;

procedure Monster_Add;
var i,c,k : word;
begin
  World.EndCharAC := world.MonsterTypeCount;
  c := (World.MaxMonster div 3)*Game.Level;

  if c < (World.MonsterTypeCount)*World.MaxMonster then begin
    gfx.MsgMes(inttostr(World.MonsterTypeCount*World.MaxMonster));
    k := c div World.MaxMonster;

    Monster_Add_New(@World.Monsters[k],World.MonAddCoef);

    World.CharacterActiveCount := World.MonsterTypeCount;
    i := 0;
    while i < k do begin
      Monster_Add(@World.Monsters[i],World.MaxMonster);
      inc(i);
    end;
  end;
end;

procedure Monster_Add(self : zMonster; count : word);
var
  i,k,j : word;
  s: byte;
begin
  k := World.MonsterTypeCount;
  World.CharacterActiveCount := World.CharacterActiveCount + count;
  for i := k to World.CharacterActiveCount-1 do begin
    s := random(LineWPCount);
    for j := 0 to World.MonsterTypeCount-1 do
    begin
    if World.Characters[World.Monsters[i].Character].name = World.Characters[World.Monsters[j].Character].name then
      begin
        World.Characters[World.Monsters[i].Character].Life := World.Characters[World.Monsters[j].Character].Life;
        Break;
      end;
    end;
    World.Characters[World.Monsters[i].Character].State := csMove;
    World.Characters[World.Monsters[i].Character].LineWP := s;
    World.Characters[World.Monsters[i].Character].NextWP := 0;
    World.Characters[World.Monsters[i].Character].Deviation[0] := random(80)-random(80);
    World.Characters[World.Monsters[i].Character].Deviation[1] := random(80)-random(80);
    World.Characters[World.Monsters[i].Character].Pos[0] := LineWayPoints[World.Characters[World.Monsters[i].Character].LineWP].WPoints[World.Characters[World.Monsters[i].Character].NextWP].pos[0]+World.Characters[World.Monsters[i].Character].Deviation[0];
    World.Characters[World.Monsters[i].Character].Pos[1] := LineWayPoints[World.Characters[World.Monsters[i].Character].LineWP].WPoints[World.Characters[World.Monsters[i].Character].NextWP].pos[1]+World.Characters[World.Monsters[i].Character].Deviation[1];
  end;
end;

procedure Monster_Add_New(self : zMonster; count : word);
var
  i,k : word;
  s,j : byte;
begin
  k := World.CharacterCount;
  World.CharacterCount := World.CharacterCount+count;
  SetLength(World.Characters,World.CharacterCount);
  SetLength(World.Monsters,World.CharacterCount);
  i := World.CharacterActiveCount;
  while i < k do begin
    s := random(LineWPCount);
    for j := 0 to World.MonsterTypeCount-1 do
    begin
    if World.Characters[World.Monsters[i].Character].name = World.Characters[World.Monsters[j].Character].name then
      begin
        World.Characters[World.Monsters[i].Character].Life := World.Characters[World.Monsters[j].Character].Life;
        Break;
      end;
    end; 
    World.Characters[World.Monsters[i].Character].Move_end := false;
    World.Characters[World.Monsters[i].Character].State := csMove;
    World.Characters[World.Monsters[i].Character].LineWP := s;
    World.Characters[World.Monsters[i].Character].NextWP := 0;
    World.Characters[World.Monsters[i].Character].Deviation[0] := random(80)-random(80);
    World.Characters[World.Monsters[i].Character].Deviation[1] := random(80)-random(80);
    World.Characters[World.Monsters[i].Character].Pos[0] := LineWayPoints[World.Characters[World.Monsters[i].Character].LineWP].WPoints[World.Characters[World.Monsters[i].Character].NextWP].pos[0]+World.Characters[World.Monsters[i].Character].Deviation[0];
    World.Characters[World.Monsters[i].Character].Pos[1] := LineWayPoints[World.Characters[World.Monsters[i].Character].LineWP].WPoints[World.Characters[World.Monsters[i].Character].NextWP].pos[1]+World.Characters[World.Monsters[i].Character].Deviation[1];
    inc(i);
  end;
  i := k;
  while i < World.CharacterCount do begin
    s := random(LineWPCount);
    World.Monsters[i].Character := i;
    for j := 0 to World.MonsterTypeCount-1 do
    begin
    if World.Characters[self^.Character].name = World.Characters[j].name then
      begin
        World.Characters[World.Monsters[i].Character] := World.Characters[World.Monsters[j].Character];
        Break;
      end;
    end;
    World.Characters[World.Monsters[i].Character].State := csMove;
    World.Characters[World.Monsters[i].Character].LineWP := s;
    World.Characters[World.Monsters[i].Character].NextWP := 0;
    World.Characters[World.Monsters[i].Character].Spr := Gfx.SprMan.Add(Gfx.TexMan.GetSize(World.Characters[World.Monsters[i].Character].Tex),World.Characters[World.Monsters[i].Character].size);
    World.Characters[World.Monsters[i].Character].CHARACTER_ANIM_BODY_MOVE := Gfx.SprMan.Add_Anim(World.Characters[World.Monsters[i].Character].Spr,World.Characters[World.Monsters[i].Character].AniBodyStart,
    World.Characters[World.Monsters[i].Character].AniBodyEnd,random(World.Characters[World.Monsters[i].Character].AniBodyEnd),World.Characters[World.Monsters[i].Character].AniBodySpeed);
    World.Characters[World.Monsters[i].Character].CHARACTER_ANIM_DEAD := Gfx.SprMan.Add_Anim(World.Characters[World.Monsters[i].Character].Spr,World.Characters[World.Monsters[i].Character].AniDeadStart,
    World.Characters[World.Monsters[i].Character].AniDeadEnd,World.Characters[World.Monsters[i].Character].AniDeadStart,World.Characters[World.Monsters[i].Character].AniDeadSpeed);
    World.Characters[World.Monsters[i].Character].Deviation[0] := random(80)-random(80);
    World.Characters[World.Monsters[i].Character].Deviation[1] := random(80)-random(80);
    World.Characters[World.Monsters[i].Character].Pos[0] := LineWayPoints[World.Characters[World.Monsters[i].Character].LineWP].WPoints[World.Characters[World.Monsters[i].Character].NextWP].pos[0]+World.Characters[World.Monsters[i].Character].Deviation[0];
    World.Characters[World.Monsters[i].Character].Pos[1] := LineWayPoints[World.Characters[World.Monsters[i].Character].LineWP].WPoints[World.Characters[World.Monsters[i].Character].NextWP].pos[1]+World.Characters[World.Monsters[i].Character].Deviation[1];
    inc(i);
  end;
end;

procedure Monster_Load(self : zMonster);
begin
  World.Characters[self^.Character].chType:= ctMonster;
  {World.Characters[self^.Character].Col[0] := random;
  World.Characters[self^.Character].Col[1] := random;
  World.Characters[self^.Character].Col[2] := random;}
  Character_Load(@World.Characters[self^.Character]);
end;

end.
