unit _load;

interface

uses _var, _scd, exgine, Graphics, Classes;

procedure LoadData; // Loading game resources
procedure Loadlevel(lvl:integer);
function checklevel (x,y,delta,dir:integer):Tlevel;

implementation

//----------------------------------------------------------------------------------------------------------------------------------------------------

function loadTex(Name:string; mipmap:boolean=true):TTexture;
var sz:integer;
    pn:pointer;
begin
  sz:=getdata(name,pn);
//  Load(Name: PChar; Mem: Pointer; Size: Integer; var W, H, BPP: Integer; var Data: Pointer): Boolean
  result:=tex.Load(pchar(name),pn,sz,true,true);

end;

//----------------------------------------------------------------------------------------------------------------------------------------------------

function loadSnd(Name:string):TSound;
var sz:integer;
    pn:pointer;
begin
  sz:=getdata(name,pn);
  result:=snd.Load(pchar(name),pn,sz,0);
end;

//----------------------------------------------------------------------------------------------------------------------------------------------------

function checklevel (x,y,delta,dir:integer):Tlevel;
begin
//result:=level[x,y];
result.pass:=0;
if dir=8 then
  if y-delta>=0 then result:=level[x,y-delta];
if dir=6 then
  if x+delta<=levelW-1 then result:=level[x+delta,y];
if dir=2 then
  if y+delta<=levelH-1 then result:=level[x,y+delta];
if dir=4 then
  if x-delta>=0 then result:=level[x-delta,y];
end;

//----------------------------------------------------------------------------------------------------------------------------------------------------

function zeroone(x:integer):integer;
begin
if x>0 then result:=1 else result:=0;
end;

//----------------------------------------------------------------------------------------------------------------------------------------------------


//----------------------------------------------------------------------------------------------------------------------------------------------------

procedure Loadlevel(lvl:integer);
var a,b,c,d:integer;
    s:string;
  procedure colorize(var x:tfood);
  var a:integer;
  begin
    x.colR:=0; x.colG:=0; x.colB:=0;
    a:=random(4);
    case a of
      0: x.colR:=255;
      1: x.colG:=255;
      2: begin x.colB:=255; x.colG:=178; end;
      3: begin x.colR:=255; x.colG:=255; end;
    end;
  end;

begin
getdata('level'+inttostr((lvl) mod 4),ms);
tsl.Clear;
tsl.LoadFromStream(ms);
setlength(a_mut,0);
setlength(a_ret,0);
setlength(a_sparker,0);
setlength(a_enemy,0);
//tech_replica.e.Clear;
leveldata.repenemy:=0;
levelData.colR:=strtoint(copy(tsl[0],1,3)); levelData.colG:=strtoint(copy(tsl[0],4,3)); levelData.colB:=strtoint(copy(tsl[0],7,3));
levelData.precolR:=levelData.colR; levelData.precolG:=levelData.colG; levelData.precolG:=levelData.colG;
tsl.Delete(0);
levelData.enemyseq:=tsl[0];
tsl.Delete(0);
levelW:=length(tsl[0]);
levelH:=tsl.Count;
setlength(level, levelW);
LevelData.Keys:=0;
//ex.log.Print(pchar('levelW' + inttostr(levelW)+' levelH' + inttostr(levelH)));
for a:=0 to levelW-1 do
  begin //+ LevelW
    setlength(level[a], levelH);
    for b:=0 to levelH-1 do
      begin //+ LevelH
        Level[a,b].pass:=1; level[a,b].tube:=0; level[a,b].ret:=0; level[a,b].wall:=0; level[a,b].contain:=0;
        for c:=0 to 29 do
          Level[a,b].food[c].active:=false;
          
        if tsl[b][a+1]='x' then
          begin
            Level[a,b].pass:=0;
            Level[a,b].contain:=255;
            level[a,b].wall:=random(4);
          end;

        if tsl[b][a+1]='.' then
          begin
            Level[a,b].pass:=1;
            Level[a,b].contain:=1;
          end;

        if tsl[b][a+1]='o' then
          begin
            Level[a,b].pass:=1;
            Level[a,b].contain:=2;
          end;

        if tsl[b][a+1]='i' then
          begin
            scr.plX:=a*64;
            scr.plY:=b*64;
            tech_enter.x:=a*64+32;
            tech_enter.y:=b*64+32;
            Level[a,b].pass:=3;
            Level[a,b].contain:=4;
            init_player(false);
          end;

        if tsl[b][a+1]='e' then
          begin
            tech_exit.x:=a*64+32;
            tech_exit.y:=b*64+32;
            Level[a,b].pass:=3;
            Level[a,b].contain:=5;
          end;

        if tsl[b][a+1]='p' then
          begin
            tech_replica.x:=a*64+32;
            tech_replica.y:=b*64+32;
            Level[a,b].pass:=3;
            Level[a,b].contain:=3;
          end;

        if tsl[b][a+1]='b' then
          begin
            tech_bonuser.x:=a*64+32;
            tech_bonuser.y:=b*64+32;
            Level[a,b].pass:=3;
            Level[a,b].contain:=7;
          end;


        if tsl[b][a+1] in ['2','4','6','8'] then
          begin
            Level[a,b].pass:=strtoint(tsl[b][a+1]);
//            Level[a,b].contain:=2;
          end;

        if tsl[b][a+1]='s' then
          begin
            Level[a,b].contain:=6;
          end;
       end;
      end;
for a:=0 to levelW-1 do
  for b:=0 to levelH-1 do
    begin // + main cycle
      if level[a,b].contain<>255 then
        begin
          c:=0;
          c:=c+zeroone(checklevel(a,b,1,8).pass)*1;
          c:=c+zeroone(checklevel(a,b,1,6).pass)*2;
          c:=c+zeroone(checklevel(a,b,1,2).pass)*4;
          c:=c+zeroone(checklevel(a,b,1,4).pass)*8;
          level[a,b].tube:=c;
        end;

      if level[a,b].contain in [1,2] then
        with level[a,b] do
          begin //+ food
            for c:=0 to 5 do
            begin
              food[c].active:=true;
              food[c].x:=random(8)*2+24;
              food[c].y:=random(8)*2+24;
              food[c].fx:=food[c].x;
              food[c].fy:=food[c].y;
              food[c].place:=5;
              colorize(food[c]);
            end;

            if checklevel(a,b,1,8).contain in [1,2] then
              for c:=6 to 11 do
                begin
                  food[c].active:=true;
                  food[c].x:=random(8)*2+24;
                  food[c].y:=random(12)*2;
                  food[c].fx:=food[c].x;
                  food[c].fy:=food[c].y;
                  food[c].place:=5;
                  colorize(food[c]);
                end;

            if checklevel(a,b,1,6).contain in [1,2] then
              for c:=12 to 17 do
                begin
                  food[c].active:=true;
                  food[c].x:=random(12)*2+40;
                  food[c].y:=random(8)*2+24;
                  food[c].fx:=food[c].x;
                  food[c].fy:=food[c].y;
                  food[c].place:=5;
                  colorize(food[c]);
                end;

            if checklevel(a,b,1,2).contain in [1,2] then
              for c:=18 to 23 do
                begin
                  food[c].active:=true;
                  food[c].x:=random(8)*2+24;
                  food[c].y:=random(12)*2+40;
                  food[c].fx:=food[c].x;
                  food[c].fy:=food[c].y;
                  food[c].place:=5;
                  colorize(food[c]);
                end;

            if checklevel(a,b,1,4).contain in [1,2] then
              for c:=24 to 29 do
                begin
                  food[c].active:=true;
                  food[c].x:=random(12)*2;
                  food[c].y:=random(8)*2+24;
                  food[c].fx:=food[c].x;
                  food[c].fy:=food[c].y;
                  food[c].place:=5;
                  colorize(food[c]);
                end;

            if level[a,b].contain=2 then
              begin
                d:=high(a_mut)+1;
                setlength(a_mut,d+1);
                a_mut[d].x:=a*64+32-1;
                a_mut[d].y:=b*64+32-1;
                a_mut[d].angle:=random(360);
                a_mut[d].speed:=(random(10)+1)/5;
                a_mut[d].dy:=0;
                if random(2)=1 then a_mut[d].speed:=a_mut[d].speed*-1;
                inc(LevelData.Keys);
              end;

        end;  //- food

        if level[a,b].contain in [4,5] then
          begin // + Exit+Enter
            if checklevel(a,b,1,8).pass = 1 then
              if level[a,b].contain=4 then
              begin
                player.direction:=8;
                player.angle:=0;
                tech_enter.angle:=0;
                tech_enter.direction:=8;
              end
              else
                tech_exit.angle:=0;

            if checklevel(a,b,1,6).pass = 1  then
              if level[a,b].contain=4 then
              begin
                player.direction:=6;
                player.angle:=90;
                tech_enter.angle:=90;
                tech_enter.direction:=6;
              end
              else
                tech_exit.angle:=90;

            if checklevel(a,b,1,2).pass = 1 then
              if level[a,b].contain=4 then
              begin
                player.direction:=2;
                player.angle:=180;
                tech_enter.angle:=180;
                tech_enter.direction:=2;
              end
              else
                tech_exit.angle:=180;

            if checklevel(a,b,1,4).pass = 1  then
              if level[a,b].contain=4 then
              begin
                player.direction:=4;
                player.angle:=-90;
                tech_enter.angle:=-90;
                tech_enter.direction:=4;
              end
              else
                tech_exit.angle:=-90;
          end;  // - Exit+Enter

          if level[a,b].contain=3 then
          begin
            d:=high(a_ret)+1;
            setlength(a_ret,d+1);
            a_ret[d].x:=a;
            a_ret[d].y:=b+1;
            level[a,b+1].ret:=8;
          end;

          if level[a,b].contain=6 then
          begin
            d:=high(a_sparker)+1;
            setlength(a_sparker,d+1);
            a_sparker[d].x:=a*64+32;
            a_sparker[d].y:=b*64+32;
            a_sparker[d].delay:=frate;
            if checklevel(a,b,1,8).contain=255 then a_sparker[d].tp:=1 else a_sparker[d].tp:=0;
          end;

          if level[a,b].contain=7 then
          begin
            tech_bonuser.x:=a*64+32;
            tech_bonuser.y:=b*64+32;
            tech_bonuser.delay:=frate*20;
            tech_bonuser.frame:=0;
            if checklevel(a,b,1,8).pass>0 then c:=8;
            if checklevel(a,b,1,6).pass>0 then c:=6;
            if checklevel(a,b,1,2).pass>0 then c:=2;
            if checklevel(a,b,1,4).pass>0 then c:=4;
            tech_bonuser.angle:=get_angle(c);
            tech_bonuser.direction:=c;

          end;
     {
     s:='a:'+inttostr(a)+', b:'+inttostr(b)+', C:'+inttostr(level[a,b].contain)+', w:'+inttostr(level[a,b].wall)+' f:' ; //+debug
     for c:=0 to 29 do
      if level[a,b].food[c].active then s:=s+'A' else s:=s+'.';
     tsl.Add(s);
       //-debug

}
    end; // - main cycle
//    tsl.SaveToFile('food');


while high(a_ret)>=0 do
  begin
    c:=high(a_ret);
    for a:=0 to c do
    begin
      a_ret[a].angle:=-1;
      if  checklevel(a_ret[a].x,a_ret[a].y,1,8).ret=0 then
        if (checklevel(a_ret[a].x,a_ret[a].y,1,8).pass in [1,2,4,6,8]) then
          begin
            level[a_ret[a].x,a_ret[a].y-1].ret:=2;
            d:=high(a_ret)+1;
            setlength(a_ret,d+1);
            a_ret[d].x:=a_ret[a].x;
            a_ret[d].y:=a_ret[a].y-1;
            a_ret[d].angle:=1;
          end;
      if  checklevel(a_ret[a].x,a_ret[a].y,1,2).ret=0 then
        if (checklevel(a_ret[a].x,a_ret[a].y,1,2).pass in [1,2,4,6,8]) then
          begin
            level[a_ret[a].x,a_ret[a].y+1].ret:=8;
            d:=high(a_ret)+1;
            setlength(a_ret,d+1);
            a_ret[d].x:=a_ret[a].x;
            a_ret[d].y:=a_ret[a].y+1;
            a_ret[d].angle:=1;
          end;
      if  checklevel(a_ret[a].x,a_ret[a].y,1,6).ret=0 then
        if (checklevel(a_ret[a].x,a_ret[a].y,1,6).pass in [1,2,4,6,8]) then
          begin
            level[a_ret[a].x+1,a_ret[a].y].ret:=4;
            d:=high(a_ret)+1;
            setlength(a_ret,d+1);
            a_ret[d].x:=a_ret[a].x+1;
            a_ret[d].y:=a_ret[a].y;
            a_ret[d].angle:=1;
          end;
      if  checklevel(a_ret[a].x,a_ret[a].y,1,4).ret=0 then
        if (checklevel(a_ret[a].x,a_ret[a].y,1,4).pass in [1,2,4,6,8]) then
          begin
            level[a_ret[a].x-1,a_ret[a].y].ret:=6;
            d:=high(a_ret)+1;
            setlength(a_ret,d+1);
            a_ret[d].x:=a_ret[a].x-1;
            a_ret[d].y:=a_ret[a].y;
            a_ret[d].angle:=1;
          end;
      end;
      b:=0;
      for a:=0 to high(a_ret) do
        if a_ret[a].angle=1 then
          begin
            a_ret[b]:=a_ret[a];
            inc(b);
          end;

    setlength(a_ret,b);
  end;

for a:=0 to length(leveldata.enemyseq)-1 do
  begin
    d:=high(a_enemy)+1;
    setlength(a_enemy,d+1);
    with a_enemy[d] do
      begin
        case leveldata.enemyseq[a+1] of
          'V': begin // Vagabond
                colR:=248; colG:=176; colB:=16;
                curr_prog:= epVagabond;
                base_prog:= epVagabond;
                spr:=enemy0;
               end;
          'M': begin // Mutter
                colR:=144; colG:=240; colB:=64;
                curr_prog:= epMutter;
                base_prog:= epMutter;
                spr:=enemy1;
               end;
          'G': begin // Gate keeper
                colR:=88; colG:=176; colB:=184;
                curr_prog:= epGateKeeper;
                base_prog:= epGateKeeper;
                spr:=enemy2;
               end;
          'B': begin // Bouncer
                colR:=120; colG:=128; colB:=248;
                curr_prog:= epBouncer;
                base_prog:= epBouncer;
                spr:=enemy3;
               end;
          'H': begin // Hound
                colR:=248; colG:=0; colB:=16;
                curr_prog:= epHound;
                base_prog:= epHound;
                spr:=enemy4;
               end;
        end;
        x:=tech_replica.x-32;
        y:=tech_replica.y-32-6;
        _x:=x; _y:=y;
        frame:=random(8);
        frz:=frate*a+frate;
        inc(leveldata.repenemy);
        direction:=8;
        angle:=0;
        speed:=1.0;
        hit:=0;
        hp:=10;
//        dspeed:=((random(5)-3) div 10);
//        tech_replica.e.Add(inttostr(d));
      end;
  end;

bonus.active:=false;
bonus.TTL:=20*frate+random(3)*frate;

if lvl=-1 then
  begin
    level[1,11].pass:=1;
    level[7,11].pass:=1;
  end;

//snd.Play(sn[s_amb],0,0,0,true);
end;

//----------------------------------------------------------------------------------------------------------------------------------------------------

procedure LoadData;
var a:integer;
    bm:TBitmap;
begin
GetSCD('cr.scd');
player.seq[0]:=0;
player.seq[1]:=1;
player.seq[2]:=2;
player.seq[3]:=3;
player.seq[4]:=2;
player.seq[5]:=1;
player.frame:=0;
player.delay:=frate div 10;
player.oral:=oral+player.seq[player.frame];
HUD.x:=0;
HUD.y:=768;
HUD.energy_x:=10;
HUD.energy_y:=29;
HUD.DNA_x:=10;
HUD.DNA_y:=43;




//t[pg]:=loadTex('pg.bmp');
t[wall]:=loadTex('wall.bmp');
t[mut]:=loadTex('mut.tga');
t[enter]:=loadTex('enter.tga');
t[exit]:=loadTex('exit.tga');
t[exit+1]:=loadTex('exit1.tga');
t[replicator]:=loadTex('replicator.tga');
t[faketube]:=loadTex('faketube.bmp');
t[pass]:=loadTex('pass.tga');
t[core]:=loadTex('core.bmp');
t[pg]:=loadTex('pg.bmp');
t[t_hud]:=loadTex('hud.tga');
t[t_energy]:=loadTex('energy.bmp');
t[t_DNA0]:=loadTex('dna_hud0.bmp');
t[t_DNA1]:=loadTex('dna_hud1.bmp');
t[bbox]:=loadTex('bbox.bmp');
t[repliflash]:=loadTex('repliflash.bmp');
t[replilight]:=loadTex('replilight.bmp');
t[coreinv]:=loadTex('coreinv.bmp');
t[t_bonus]:=loadTex('bonus.bmp');
//t[bonuser]:=loadTex('bonuser.tga');
t[t_logo]:=loadTex('logo.bmp');
t[t_copyr]:=loadTex('copyr.bmp');

for a:=0 to 15 do
  t[tube+a]:=loadTex('tube'+inttostr(a)+'.bmp');

for a:=0 to 3 do
  t[oral+a]:=loadTex('oral'+inttostr(a)+'.tga');

for a:=0 to 2 do
  t[spark+a]:=loadTex('spark'+inttostr(a)+'.bmp');

for a:=0 to 1 do
  t[sparker+a]:=loadTex('sparker'+inttostr(a)+'.tga');

for a:=0 to 7 do
  t[enemy0+a]:=loadTex('enemy0'+inttostr(a)+'.tga');

for a:=0 to 7 do
  t[enemy1+a]:=loadTex('enemy1'+inttostr(a)+'.tga');

for a:=0 to 7 do
  t[enemy2+a]:=loadTex('enemy2'+inttostr(a)+'.tga');

for a:=0 to 7 do
  t[enemy3+a]:=loadTex('enemy3'+inttostr(a)+'.tga');

for a:=0 to 7 do
  t[enemy4+a]:=loadTex('enemy4'+inttostr(a)+'.tga');
    
for a:=0 to 4 do
  t[thunder+a]:=loadTex('thunder'+inttostr(a)+'.bmp');
 

sn[s_oral]:=loadsnd('oral.wav');
sn[s_mut]:=loadsnd('mut.wav');
sn[s_spark]:=loadsnd('spark.wav');
sn[s_amb]:=loadsnd('amb0.wav');
sn[s_exit_open]:=loadsnd('exit_open.wav');
sn[s_hit]:=loadsnd('hit.wav');
sn[s_replicator]:=loadsnd('replicator.wav');
sn[s_energy]:=loadsnd('energy.wav');
sn[s_enter_replicator]:=loadsnd('enter_replicator.wav');
sn[s_bonusend]:=loadsnd('bonusend.wav');
sn[s_bonusend1]:=loadsnd('bonusend1.wav');
sn[s_enemyd]:=loadsnd('enemyd.wav');
sn[s_bonus_enter]:=loadsnd('bonus_enter.wav');

end;


//----------------------------------------------------------------------------------------------------------------------------------------------------

end.




