unit _update;

interface

uses _var, _scd, exgine, openGL, math, _load;


procedure update_game;
procedure update_menu;


implementation



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

procedure update_scr (x,y:integer);
var r,mx,my,tx,ty:integer;
begin

tx:=x;
ty:=y;
//mx:=high(level)*64+64-1;
//my:=high(level[0])*64+64-1;
mx:=(LevelW)*64-1;
my:=(LevelH)*64-1;

scr.l:=tx-511; //319;
scr.r:=tx+512; //320;

scr.t:=ty-383;//239;
scr.d:=ty+384;//240;

if tx<511 then //319 then
  begin
    scr.l:=0;
    scr.r:=1023;//639;
  end
  else
  if tx+512>mx then //320
    begin
      scr.r:=mx;
      scr.l:=scr.r-1023;//639;
      end;

if ty<383 then //239
  begin
    scr.t:=0;
    scr.d:=767;//479;
  end
  else
  if ty+384>my then //240
  begin
    scr.d:=my;
    scr.t:=scr.d-767; //479;
  end;

scr.wl:= scr.l div 64;
scr.wt:= scr.t div 64;
scr.wr:= scr.r div 64;
scr.wd:= scr.d div 64;

scr.dx:= scr.l mod 64;
scr.dy:= scr.t mod 64;

end;

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

procedure update_Player;
var a:integer;
begin
if (player.energy<=0) and (player.dead=0) then
  begin
    player.dead:=300;
    snd.Play(sn[s_enemyd],0,0,0,false);
  end;

player.shellangle:=player.shellangle+player.speed;
if player.shellangle>360 then player.shellangle:=player.shellangle-360;
if player.Charged>0 then dec(player.Charged);
if (player.Charged =1*frate) or (player.Charged =2*frate) or (player.Charged =3*frate) then snd.Play(sn[s_bonusend],0,0,0,false);
if player.Charged=1 then snd.Play(sn[s_bonusend1],0,0,0,false);
if player.dead=300 then
  begin
    if abs(player.x-scr.plX)>8 then if player.x>scr.plX then dec(player.x,8) else inc(player.x,8) ;
    if abs(player.y-scr.plY)>8 then if player.Y>scr.plY then dec(player.y,8) else inc(player.y,8) ;
    if (abs(player.x-scr.plX)<=9) and (abs(player.y-scr.plY)<=9) then player.dead:=200;
  end;

if (player.dead>0) and (player.dead<=200) then
  dec(player.dead);
  
if (player.dead=1) then
  begin
    init_player(false);
    player.energy:=200;
    player.dead:=0;
  end;

if player.delay>0 then dec (player.delay);
if player.delay=0 then
  begin
    player.delay:=frate div 20;
    inc (player.frame);
       player.tbn:=random(5);
        player.tba:=random(360);

    if player.frame>=6 then
      begin
        player.frame:=0;
        snd.Play(sn[s_oral],0,0,0,false);
      end;
    player.oral:=oral+player.seq[player.frame];
  end;

if player.dead=0 then
begin
case player.direction of
  8:  begin
  if checklevel(player.x div 64, ((player.y-1) div 64),0,8).pass in [1,8] then
    begin
      player._y:=player._y-player.speed;
    end
    else
    begin
      player.y:=(player.y div 64)*64;
      player._y:=player.y;
    end;
  if control.Right and (checklevel((player.x+64) div 64, (player.y div 64),0,6).pass in [1,6]) and (player.y mod 64<=6) and (player.ready=0) then
    begin
      player.y:=(player.y div 64)*64;
      player._y:=player.y;
      player.direction:=6;
      player.angle:=90;
      player.ready:=8;
    end;
  if control.left and (checklevel((player.x-1) div 64, (player.y div 64),0,4).pass in [1,4]) and (player.y mod 64<=6) and (player.ready=0) then
    begin
      player.y:=(player.y div 64)*64;
      player._y:=player.y;
      player.direction:=4;
      player.angle:=-90;
      player.ready:=8;
    end;
  if control.down and (checklevel((player.x) div 64, ((player.y+64) div 64),0,2).pass in [1,2])  then
    begin
      player._y:=player.y;
      player.direction:=2;
      player.angle:=180;
    end;

  end;

  6: begin
  if checklevel((player.x+64) div 64, (player.y div 64),0,6).pass in [1,6] then
    begin
      player._x:=player._x+player.speed;
    end
    else
    begin
      player.x:=(player.x div 64)*64;
      player._x:=player.x;
    end;
  if control.up and (checklevel((player.x) div 64, ((player.y-1) div 64),0,8).pass in [1,8]) and (player.x mod 64<=6) and (player.ready=0) then
    begin
      player.x:=(player.x div 64)*64;
      player._x:=player.x;
      player.direction:=8;
      player.angle:=0;
      player.ready:=8;
    end;
  if control.left and (checklevel((player.x-1) div 64, (player.y div 64),0,4).pass in [1,4])  then
    begin
      player.direction:=4;
      player.angle:=-90;
    end;
  if control.down and (checklevel((player.x) div 64, ((player.y+64) div 64),0,2).pass in [1,2]) and (player.x mod 64<=6) and (player.ready=0)  then
    begin
      player.x:=(player.x div 64)*64;
      player._x:=player.x;
      player.direction:=2;
      player.angle:=180;
      player.ready:=8;
    end;
  end;


  2: begin
  if checklevel((player.x) div 64, ((player.y+64) div 64),0,2).pass in [1,2] then
    begin
      player._y:=player._y+player.speed;
    end
    else
    begin
      player.y:=(player.y div 64)*64;
      player._y:=player.y;
    end;
  if control.Right and (checklevel((player.x+64) div 64, (player.y div 64),0,6).pass in [1,6]) and (player.y mod 64<=6) and (player.ready=0) then
    begin
      player.y:=(player.y div 64)*64;
      player._y:=player.y;
      player.direction:=6;
      player.angle:=90;
      player.ready:=8;
    end;
  if control.left and (checklevel((player.x-1) div 64, (player.y div 64),0,4).pass in [1,4]) and (player.y mod 64<=6) and (player.ready=0) then
    begin
      player.y:=(player.y div 64)*64;
      player._y:=player.y;
      player.direction:=4;
      player.angle:=-90;
      player.ready:=8;
    end;
  if control.up and (checklevel((player.x) div 64, ((player.y-1) div 64),0,8).pass in [1,8])  then
    begin
      player.direction:=8;
      player.angle:=0;
    end;
  end;

  4: begin
  if checklevel((player.x-1) div 64, ((player.y) div 64),0,4).pass in [1,4] then
    begin
      player._x:=player._x-player.speed;
    end
    else
    begin
      player.x:=(player.x div 64)*64;
      player._x:=player.x;
    end;
  if control.up and (checklevel((player.x) div 64, ((player.y-1) div 64),0,8).pass in [1,8]) and (player.x mod 64<=6) and (player.ready=0) then
    begin
      player.x:=(player.x div 64)*64;
      player._x:=player.x;
      player.direction:=8;
      player.angle:=0;
      player.ready:=8;
    end;
  if control.right and (checklevel((player.x+64) div 64, (player.y div 64),0,6).pass in [1,6])  then
    begin
      player.direction:=6;
      player.angle:=90;
    end;
  if control.down and (checklevel((player.x) div 64, ((player.y+64) div 64),0,2).pass in [1,2]) and (player.x mod 64<=6) and (player.ready=0)  then
    begin
      player.x:=(player.x div 64)*64;
      player._x:=player.x;
      player.direction:=2;
      player.angle:=180;
      player.ready:=8;
    end;
  end;
end;


player.x:=round(player._x);
player.y:=round(player._y);


if (abs(player.x+32-tech_exit.x)<16) and (abs(player.y+32-tech_exit.y)<16) and (gamestate=gsGame) then
  begin
    inc(player.lvl);
    loadlevel(player.lvl);
    init_player(false);
    player.mut:=0;
  end;

end;

if  player.ready>0 then dec (player.ready);



end;

//----------------------------------------------------------------------------------------------------------------------------------------------------
function enemy_program(prog:TEnemyProgram; enemy:integer):string;
var a,b,dir,lx,ly:integer;
    c,dist:extended;
    s:string;
begin
s:='';
case prog of
  epVagabond: begin
                result:=s;
              end;
  epMutter:    begin
                if high(a_mut)>=0 then
                  begin
                    b:=(enemy+1) mod (high(a_mut)+1);
                    a_enemy[enemy].tgt_x:=a_mut[b].x div 64;
                    a_enemy[enemy].tgt_y:=a_mut[b].y div 64;
                  end;
              end;
  epGateKeeper: begin
                    a_enemy[enemy].tgt_x:=tech_exit.x div 64;
                    a_enemy[enemy].tgt_y:=tech_exit.y div 64;
              end;
  epBouncer:    begin
                    a_enemy[enemy].tgt_x:=bonus.x div 64;
                    a_enemy[enemy].tgt_y:=bonus.y div 64;
              end;
  epHound:   begin
                    a_enemy[enemy].tgt_x:=player.x div 64;
                    a_enemy[enemy].tgt_y:=player.y div 64;
              end;
  end;

lx:=a_enemy[enemy].x div 64;
ly:=a_enemy[enemy].y div 64;
if prog<>epVagabond then
  begin
    dist:=1000;
    if checklevel(lx, ly, 1, 8).pass in [1,8] then
      begin
        c:=sqrt( sqr(a_enemy[enemy].tgt_x-lx)+ sqr(a_enemy[enemy].tgt_y-(ly-1)));
        if c<dist then
        begin
          dist:=c; dir :=8;
        end;
      end;

    if checklevel(lx, ly, 1, 6).pass in [1,6] then
      begin
        c:=sqrt( sqr(a_enemy[enemy].tgt_x-(lx+1))+ sqr(a_enemy[enemy].tgt_y-(ly)));
        if c<dist then
        begin
          dist:=c; dir :=6;
        end;
      end;

    if checklevel(lx, ly, 1, 2).pass in [1,2] then
      begin
        c:=sqrt( sqr(a_enemy[enemy].tgt_x-(lx))+ sqr(a_enemy[enemy].tgt_y-(ly+1)));
        if c<dist then
        begin
          dist:=c; dir :=2;
        end;
      end;

    if checklevel(lx, ly, 1, 4).pass in [1,4] then
      begin
        c:=sqrt( sqr(a_enemy[enemy].tgt_x-(lx-1))+ sqr(a_enemy[enemy].tgt_y-(ly)));
        if c<dist then
        begin
          dist:=c; dir :=4;
        end;
      end;
s:=inttostr(dir);
//s:=s+s+s+s;
result:=s;

  end;


end;

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

procedure update_enemy;
var a,b,c:integer;
    s,s1:string;
    ds:set of byte;
begin
 if tech_replica.delay>0 then dec (tech_replica.delay);
  if high(a_enemy)>=0 then
    for a:=0 to high(a_enemy) do
      with a_enemy[a] do
        begin // + all enemy
          if (abs((player.x)-(x))<24) and (abs((player.y)-(y))<24) and (hp>0) and (player.dead=0) then
            if player.Charged>0 then
              begin
                dec(hp,50);
                if hp<=0 then snd.Play(sn[s_enemyd],0,0,0,false)
              end
              else
              begin
                dec(player.energy,50);
                snd.Play(sn[s_hit],0,0,0,false);
                player.hit:=2;
              end;

          if (frz=75) and onscreen(tech_replica.x,tech_replica.y) then snd.Play(sn[s_replicator],0,0,0,false);
          if frz=1 then
            begin
              dec(leveldata.repenemy);
//              tech_replica.e.Delete(0);
            end;
          if frz>0 then dec(frz);
          if turned>0 then dec(turned);
          if delay>0 then dec(delay);
          if delay=0 then
            begin
              delay:=(frate div 25) + random(5);
              inc(frame);
              if frame>7 then frame:=0;
            end;
          if frz=0 then
          begin
          ds:=[1];
          if hp<=0 then ds:=ds+[2,3,4,6,8];
          case direction of
          8:  begin
                if checklevel(x div 64, ((y-1) div 64),0,8).pass in ds+[8] then
                begin
                  _y:=_y-(speed);
                end;
              end;
          6: begin
                if checklevel((x+64) div 64, (y div 64),0,6).pass in ds+[6] then
                begin
                  _x:=_x+(speed);
                end;
              end;
          4: begin
                if checklevel((x-1) div 64, ((y) div 64),0,4).pass in ds+[4] then
                begin
                  _x:=_x-(speed);
                end;
              end;
          2: begin
                if checklevel((x) div 64, ((y+64) div 64),0,2).pass in ds+[2] then
                begin
                  _y:=_y+(speed);
                end;
              end;
          end;

        x:=round(_x);
        y:=round(_y);

            if (abs(x-(tech_replica.x-32))<4) and (abs(y-(tech_replica.y-32+10))<4) and (hp<=0) then
              begin
                x:=tech_replica.x-32;
                y:=tech_replica.y-32-6;
                _x:=x; _y:=y;
                frame:=random(8);
                frz:=replicator_time;
                if tech_replica.delay>0 then frz:=frz+tech_replica.delay;
                inc(leveldata.repenemy);
//                tech_replica.e.Add(inttostr(a));
                tech_replica.delay:=frz;
                direction:=8;
                angle:=0;
                speed:=1.0;
                hit:=0;
                hp:=10;
                if onscreen(tech_replica.x,tech_replica.y) then snd.Play(sn[s_enter_replicator],0,0,100,false);
              end;

        if (x mod 64 <3) and (y mod 64 <3) and (turned=0) then
          begin
            s:='';
            if hp>0 then
            begin
            case direction of
              8: begin
                   if checklevel(x div 64, ((y-1) div 64),0,8).pass in [1,8] then s:=s+'88888888' else s:=s+'2';
                   if checklevel((x+64) div 64, (y div 64),0,6).pass in [1,6] then s:=s+'6666';
                   if checklevel((x-1) div 64, ((y) div 64),0,4).pass in [1,4] then s:=s+'4444';
//                   if checklevel((x) div 64, ((y+64) div 64),0,2).pass in [1,2] then
                   s1:=enemy_program(curr_prog,a);
                   if pos(s1,s)<>0 then s:=s+s1+s1+s1+s1+s1+s1+s1+s1;
                 end;
              6: begin
                   if checklevel(x div 64, ((y-1) div 64),0,8).pass in [1,8] then s:=s+'8888';
                   if checklevel((x+64) div 64, (y div 64),0,6).pass in [1,6] then s:=s+'66666666' else s:=s+'4';
//                   if checklevel((x-1) div 64, ((y) div 64),0,4).pass in [1,4] then
                   if checklevel((x) div 64, ((y+64) div 64),0,2).pass in [1,2] then s:=s+'2222';
                   s1:=enemy_program(curr_prog,a);
                   if pos(s1,s)<>0 then s:=s+s1+s1+s1+s1+s1+s1+s1+s1;
                 end;
              4: begin
                   if checklevel(x div 64, ((y-1) div 64),0,8).pass in [1,8] then s:=s+'8888';
//                   if checklevel((x+64) div 64, (y div 64),0,6).pass in [1,6] then
                   if checklevel((x-1) div 64, ((y) div 64),0,4).pass in [1,4] then s:=s+'44444444' else s:=s+'6';
                   if checklevel((x) div 64, ((y+64) div 64),0,2).pass in [1,2] then s:=s+'2222';
                   s1:=enemy_program(curr_prog,a);
                   if pos(s1,s)<>0 then s:=s+s1+s1+s1+s1+s1+s1+s1+s1;
                 end;
              2: begin
//                   if checklevel(x div 64, ((y-1) div 64),0,8).pass in [1,8] then
                   if checklevel((x+64) div 64, (y div 64),0,6).pass in [1,6] then s:=s+'6666';
                   if checklevel((x-1) div 64, ((y) div 64),0,4).pass in [1,4] then s:=s+'4444';
                   if checklevel((x) div 64, ((y+64) div 64),0,2).pass in [1,2] then s:=s+'22222222'else s:=s+'8';
                   s1:=enemy_program(curr_prog,a);
                   if pos(s1,s)<>0 then s:=s+s1+s1+s1+s1+s1+s1+s1+s1;
                 end;
            end;
            b:=random(length(s))+1;
            c:=strtoint(copy(s,b,1));
            end
            else
            c:=checklevel((x) div 64, ((y) div 64),0,2).ret;
            turned:=8;
            if direction<>c then
              begin
                x:=(x div 64) * 64;
                y:=(y div 64) * 64;
                _x:=x; _y:=y;
                direction:=c;
                angle:=get_angle(direction);
//                dspeed:=((random(5)-3) div 5);
              end;
          end;
          end;
        end;  // - all enemy

end;

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

procedure update_sparker;
var a,b:integer;
begin

  if high(a_sparker)>=0 then
    for a:=0 to high(a_sparker) do
      with a_sparker[a] do
        begin
          if delay>0 then dec(delay);
          if delay=frate div 3 then
          if onscreen(x,y) then
            snd.Play(sn[s_spark],0,0,0,false);
          if delay<frate div 3 then
            begin
              if (abs((player.x+31)-(x))<24) and (abs((player.y+31)-(y))<24) and (player.dead=0) then
                begin
                  dec(player.energy,30);
                  snd.Play(sn[s_hit],0,0,0,false);
                  player.hit:=1;
                end;
              if high(a_enemy)>=0 then
                for b:=0 to high(a_enemy) do
                    if (abs((a_enemy[b].x+31)-(x))<16) and (abs((a_enemy[b].y+31)-(y))<16) and (a_enemy[b].hp>0)  then
                      begin
                        dec(a_enemy[b].hp,5);
                        a_enemy[b].hit:=1;
                        if a_enemy[b].hp<=0 then
                          begin
                            a_enemy[b].speed:=1.5;
                            a_enemy[b].direction:=checklevel((x) div 64, ((y) div 64),0,2).ret;
                            a_enemy[a].angle:=get_angle(a_enemy[b].direction);
                          end;
                      end;
              sparkFr:= random(3);// delay mod 3;
              if random(2)=1 then sparkW:=64 else sparkW:=-64;
              if random(2)=1 then sparkH:=16 else sparkH:=-16;
            end;
            if delay<=0 then delay:=random(frate)+frate;
        end;



end;

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

procedure update_mut;
var a,b:integer;
begin
  b:=0;
  if high(a_mut)>=0 then
    for a:=0 to high(a_mut) do
      with a_mut[a] do
        begin
          angle:=angle+speed;
          if angle>360 then angle:=angle-360;
          if angle<0 then angle:=360+angle;
          if (abs((player.x+31)-(x))<player.consum) and (abs((player.y+31)-(y))<player.consum) and (dy=0) and ((player.dead=0)) then
          begin
            snd.Play(sn[s_mut],0,0,0,false);
            x:=x-scr.l; y:=y-scr.t;
            _x:=x; _y:=y;
            speed:=5;
            a_mut[a].dx:=((hud.dna_x+player.mut*8)-a_mut[a].x)/40;
            a_mut[a].dy:=((hud.y-64+hud.dna_y)-a_mut[a].y)/40;
            player.Charged:=bonustime;
          end;
          if a_mut[a].dy>0 then
            begin
              _x:=_x+dx;
              _y:=_y+dy;
              x:=round(_x);
              y:=round(_y);
              if y>740 then
                begin
                  dy:=-1;
                  inc(player.mut);
                  if player.mut=leveldata.Keys then
                    begin
                      snd.Play(sn[s_exit_open],0,0,0,false);
                      level[tech_exit.x div 64, tech_exit.y div 64].pass:=1;
                    end;
                end;
            end;
         if dy>=0 then
           begin
             a_mut[b]:=a_mut[a];
             inc(b);
          end;
      end;
  setlength(a_mut,b);
end;

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

procedure update_bonus;
var ds:set of byte;
    s:string;
    b,c:integer;
begin
  if bonus.turned>0 then dec(bonus.turned);
  if bonus.TTL>0 then dec(bonus.TTL);
  if (bonus.TTL=1) and (not bonus.active) then
    begin
      bonus.active:=true;
      bonus.TTL:=frate*30;
      bonus.x:=tech_enter.x-32;
      bonus.y:=tech_enter.y-32;
      bonus.tp:=random(2);
      bonus.speed:=1;
      bonus.direction:=tech_enter.direction;
      snd.Play(sn[s_bonus_enter],0,0,0,false);
    end;
  if (bonus.TTL=1) and (bonus.active) then
    begin
      bonus.active:=false;
      bonus.TTL:=20*frate+random(3)*frate;
      snd.Play(sn[s_bonusend1],0,0,0,false);
    end;


  if bonus.active then
  with bonus do
  begin
          ds:=[1];
          case direction of
          8:  begin
                if checklevel(x div 64, ((y-1) div 64),0,8).pass in ds+[8] then
                begin
                  y:=y-(speed);
                end;
              end;
          6: begin
                if checklevel((x+64) div 64, (y div 64),0,6).pass in ds+[6] then
                begin
                  x:=x+(speed);
                end;
              end;
          4: begin
                if checklevel((x-1) div 64, ((y) div 64),0,4).pass in ds+[4] then
                begin
                  x:=x-(speed);
                end;
              end;
          2: begin
                if checklevel((x) div 64, ((y+64) div 64),0,2).pass in ds+[2] then
                begin
                  y:=y+(speed);
                end;
              end;
          end;

        if (x mod 64 <3) and (y mod 64 <3) and (turned=0) then
          begin //+ Turn?
            s:='';
            case direction of
              8: begin
                   if checklevel(x div 64, ((y-1) div 64),0,8).pass in [1,8] then s:=s+'88888888' else s:=s+'2';
                   if checklevel((x+64) div 64, (y div 64),0,6).pass in [1,6] then s:=s+'6666';
                   if checklevel((x-1) div 64, ((y) div 64),0,4).pass in [1,4] then s:=s+'4444';
                 end;
              6: begin
                   if checklevel(x div 64, ((y-1) div 64),0,8).pass in [1,8] then s:=s+'8888';
                   if checklevel((x+64) div 64, (y div 64),0,6).pass in [1,6] then s:=s+'66666666' else s:=s+'4';
                   if checklevel((x) div 64, ((y+64) div 64),0,2).pass in [1,2] then s:=s+'2222';
                 end;
              4: begin
                   if checklevel(x div 64, ((y-1) div 64),0,8).pass in [1,8] then s:=s+'8888';
                   if checklevel((x-1) div 64, ((y) div 64),0,4).pass in [1,4] then s:=s+'44444444' else s:=s+'6';
                   if checklevel((x) div 64, ((y+64) div 64),0,2).pass in [1,2] then s:=s+'2222';
                 end;
              2: begin
                   if checklevel((x+64) div 64, (y div 64),0,6).pass in [1,6] then s:=s+'6666';
                   if checklevel((x-1) div 64, ((y) div 64),0,4).pass in [1,4] then s:=s+'4444';
                   if checklevel((x) div 64, ((y+64) div 64),0,2).pass in [1,2] then s:=s+'22222222'else s:=s+'8';
                 end;
            end;
            b:=random(length(s))+1;
            c:=strtoint(copy(s,b,1));
            turned:=8;
            if direction<>c then
              begin
                x:=(x div 64) * 64;
                y:=(y div 64) * 64;
                direction:=c;
              end;

          end;  //- Turn?
        if (abs((player.x)-(x))<24) and (abs((player.y)-(y))<24) and (player.dead=0) then
          begin
            snd.Play(sn[s_mut],0,0,0,false);
            active:=false;
            TTL:=20*frate+random(3)*frate;
            if tp=0 then player.Charged:=bonustime;
            if tp=1 then player.energy:=player.max_energy;
          end;

  end;



end;

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

procedure update_food;
var a,b,c:integer;
    s:string;
begin
for a:=0 to 16 do
  if a+scr.wl<=LevelW-1 then
    for b:=0 to 12 do
      if b+scr.wt<=LevelH-1 then
      if level[a+scr.wl,b+scr.wt].contain in [1,2] then
        for c:=0 to 29 do
          if level[a+scr.wl,b+scr.wt].food[c].active then
            with level[a+scr.wl,b+scr.wt].food[c] do
              begin
                if (abs((player.x+31)-(x+(a+scr.wl)*64))<player.consum) and (abs((player.y+31)-(y+(b+scr.wt)*64))<player.consum) and (player.energy<player.max_energy) and (player.dead=0) then
                  begin
                    active:=false;
                    inc(player.energy);
                    if player.energy=player.max_energy then snd.Play(sn[s_energy],0,0,0,false);
                  end;
                if delay>0 then dec(delay) else
                begin
                  delay:=random(frate div 4)+frate div 16;
                  s:='';
                  case place of
                    1: s:='1111425';
                    2: s:='2222135';
                    3: s:='3333156';
                    4: s:='4444175';
                    5: s:='555512346789';
                    6: s:='6666395';
                    7: s:='7777485';
                    8: s:='8888759';
                    9: s:='9999856';
                  end;
                  place:=strtoint(copy(s,random(length(s))+1,1));
                    case place of
                    1: begin fx:=x-dfood; fy:=y+dfood; end;
                    2: begin fx:=x; fy:=y+dfood; end;
                    3: begin fx:=x+dfood; fy:=y+dfood; end;
                    4: begin fx:=x-dfood; fy:=y; end;
                    5: begin fx:=x; fy:=y; end;
                    6: begin fx:=x+dfood; fy:=y; end;
                    7: begin fx:=x-dfood; fy:=y-dfood; end;
                    8: begin fx:=x; fy:=y-dfood; end;
                    9: begin fx:=x+dfood; fy:=y-dfood; end;

                  end;
                end;
              end;
end;


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

procedure new_game;
begin
  init_player(true);
  loadlevel(player.lvl);
  gamestate:=gsGame;
end;



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

procedure update_game;

begin

update_food;

update_mut;

update_sparker;

update_enemy;

update_bonus;

control:=input;
update_player;

update_scr(player.x-1,player.y-1);

end;

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

procedure update_menu;

begin

update_food;

update_enemy;

control:=input;
update_player;

if ((player.x div 64)=(1)) and ((player.y div 64)=(11)) then ex.Quit;
if ((player.x div 64)=(7)) and ((player.y div 64)=(11)) then new_game;  


update_scr(player.x-1,player.y-1);

end;
//----------------------------------------------------------------------------------------------------------------------------------------------------


end.



