unit UMoveToRoom;

interface

procedure MoveToRoom(LastX,LastZ:Integer); 
procedure ChangePlayerSpeed(LastX,LastZ:Integer);
procedure WarpWalls(LastX,LastZ:Integer);

implementation

uses Unit1,UNetwork,SysUtils,Types,Windows,Math,UCadencer;


procedure WarpWalls(LastX,LastZ:Integer);
var Pos:TPoint;
begin
  if LastX<>PosX-1 then begin
    if nx in Map[PosX,PosZ].Walls
    then begin
      Map[PosX,PosZ].Walls:=Map[PosX,PosZ].Walls-[nx];
      Map[PosX-1,PosZ].Walls:=Map[PosX-1,PosZ].Walls-[px];
    end else if nx in Map[PosX,PosZ].UpWalls
    then begin
      Map[PosX,PosZ].UpWalls:=Map[PosX,PosZ].UpWalls-[nx];
      Map[PosX-1,PosZ].UpWalls:=Map[PosX-1,PosZ].UpWalls-[px];
      Map[PosX,PosZ].DownWalls:=Map[PosX,PosZ].DownWalls+[nx];
      Map[PosX-1,PosZ].DownWalls:=Map[PosX-1,PosZ].DownWalls+[px];
    end else if nx in Map[PosX,PosZ].DownWalls
    then begin
      Map[PosX,PosZ].DownWalls:=Map[PosX,PosZ].DownWalls-[nx];
      Map[PosX-1,PosZ].DownWalls:=Map[PosX-1,PosZ].DownWalls-[px];
      Map[PosX,PosZ].UpWalls:=Map[PosX,PosZ].UpWalls+[nx];
      Map[PosX-1,PosZ].UpWalls:=Map[PosX-1,PosZ].UpWalls+[px];
    end else begin
      Map[PosX,PosZ].Walls:=Map[PosX,PosZ].Walls+[nx];
      Map[PosX-1,PosZ].Walls:=Map[PosX-1,PosZ].Walls+[px];
    end;
  end;
  if LastX<>PosX+1 then begin
    if px in Map[PosX,PosZ].Walls
    then begin
      Map[PosX,PosZ].Walls:=Map[PosX,PosZ].Walls-[px];
      Map[PosX+1,PosZ].Walls:=Map[PosX+1,PosZ].Walls-[nx];
    end else if px in Map[PosX,PosZ].UpWalls
    then begin
      Map[PosX,PosZ].UpWalls:=Map[PosX,PosZ].UpWalls-[px];
      Map[PosX+1,PosZ].UpWalls:=Map[PosX+1,PosZ].UpWalls-[nx];
      Map[PosX,PosZ].DownWalls:=Map[PosX,PosZ].DownWalls+[px];
      Map[PosX+1,PosZ].DownWalls:=Map[PosX+1,PosZ].DownWalls+[nx];
    end else if px in Map[PosX,PosZ].DownWalls
    then begin
      Map[PosX,PosZ].DownWalls:=Map[PosX,PosZ].DownWalls-[px];
      Map[PosX+1,PosZ].DownWalls:=Map[PosX+1,PosZ].DownWalls-[nx];
      Map[PosX,PosZ].UpWalls:=Map[PosX,PosZ].UpWalls+[px];
      Map[PosX+1,PosZ].UpWalls:=Map[PosX+1,PosZ].UpWalls+[nx];
    end else begin
      Map[PosX,PosZ].Walls:=Map[PosX,PosZ].Walls+[px];
      Map[PosX+1,PosZ].Walls:=Map[PosX+1,PosZ].Walls+[nx];
    end;
  end;
  if LastZ<>PosZ-1 then begin
    if nz in Map[PosX,PosZ].Walls
    then begin
      Map[PosX,PosZ].Walls:=Map[PosX,PosZ].Walls-[nz];
      Map[PosX,PosZ-1].Walls:=Map[PosX,PosZ-1].Walls-[pz];
    end else if nz in Map[PosX,PosZ].UpWalls
    then begin
      Map[PosX,PosZ].UpWalls:=Map[PosX,PosZ].UpWalls-[nz];
      Map[PosX,PosZ-1].UpWalls:=Map[PosX,PosZ-1].UpWalls-[pz];
      Map[PosX,PosZ].DownWalls:=Map[PosX,PosZ].DownWalls+[nz];
      Map[PosX,PosZ-1].DownWalls:=Map[PosX,PosZ-1].DownWalls+[pz];
    end else if nz in Map[PosX,PosZ].DownWalls
    then begin
      Map[PosX,PosZ].DownWalls:=Map[PosX,PosZ].DownWalls-[nz];
      Map[PosX,PosZ-1].DownWalls:=Map[PosX,PosZ-1].DownWalls-[pz];
      Map[PosX,PosZ].UpWalls:=Map[PosX,PosZ].UpWalls+[nz];
      Map[PosX,PosZ-1].UpWalls:=Map[PosX,PosZ-1].UpWalls+[pz];
    end else begin
      Map[PosX,PosZ].Walls:=Map[PosX,PosZ].Walls+[nz];
      Map[PosX,PosZ-1].Walls:=Map[PosX,PosZ-1].Walls+[pz];
    end;
  end;
  if LastZ<>PosZ+1 then begin
    if pz in Map[PosX,PosZ].Walls
    then begin
      Map[PosX,PosZ].Walls:=Map[PosX,PosZ].Walls-[pz];
      Map[PosX,PosZ+1].Walls:=Map[PosX,PosZ+1].Walls-[nz];
    end else if pz in Map[PosX,PosZ].UpWalls
    then begin
      Map[PosX,PosZ].UpWalls:=Map[PosX,PosZ].UpWalls-[pz];
      Map[PosX,PosZ+1].UpWalls:=Map[PosX,PosZ-1].UpWalls-[nz];
      Map[PosX,PosZ].DownWalls:=Map[PosX,PosZ].DownWalls+[pz];
      Map[PosX,PosZ+1].DownWalls:=Map[PosX,PosZ+1].DownWalls+[nz];
    end else if pz in Map[PosX,PosZ].DownWalls
    then begin
      Map[PosX,PosZ].DownWalls:=Map[PosX,PosZ].DownWalls-[pz];
      Map[PosX,PosZ+1].DownWalls:=Map[PosX,PosZ+1].DownWalls-[nz];
      Map[PosX,PosZ].UpWalls:=Map[PosX,PosZ].UpWalls+[pz];
      Map[PosX,PosZ+1].UpWalls:=Map[PosX,PosZ+1].UpWalls+[nz];
    end else begin
      Map[PosX,PosZ].Walls:=Map[PosX,PosZ].Walls+[pz];
      Map[PosX,PosZ+1].Walls:=Map[PosX,PosZ+1].Walls+[nz];
    end;
  end;
  Pos.X:=PosX;
  Pos.Y:=PosZ;
  Form3.SendRaum(Pos);
    
  Pos.X:=PosX+1;
  Form3.SendRaum(Pos);
  Pos.X:=PosX-1;
  Form3.SendRaum(Pos);
              
  Pos.X:=PosX;
  Pos.Y:=PosZ+1;
  Form3.SendRaum(Pos);
  Pos.Y:=PosZ-1;
  Form3.SendRaum(Pos);
end;


procedure NormalSpeedLoss(LastX,LastZ:Integer);
begin
  if not Won then begin
    if (not GetRT(PosX,PosZ).PushPlayer[Up])
    and (not GetRT(PosX,PosZ).PushPlayer[Down])
    and (not GetRT(LastX,LastZ).PushPlayer[Up])
    and (not GetRT(LastX,LastZ).PushPlayer[Down]) then
    Form1.ChangeSpeedBy(-0.08);

    if ((Camera.Position.X<-8.5) and
    (((nx in Map[PosX,PosZ].UpWalls) and players[MyId].CeilWalk)
    or ((nx in Map[PosX,PosZ].DownWalls) and not players[MyId].CeilWalk)))
    then Form1.ChangeSpeedBy(-1);
    if ((Camera.Position.X>8.5) and
    (((px in Map[PosX,PosZ].UpWalls) and players[MyId].CeilWalk)
    or ((px in Map[PosX,PosZ].DownWalls) and not players[MyId].CeilWalk)))
    then Form1.ChangeSpeedBy(-1);
    if ((Camera.Position.Z<-8.5) and
    (((nz in Map[PosX,PosZ].UpWalls) and players[MyId].CeilWalk)
    or ((nz in Map[PosX,PosZ].DownWalls) and not players[MyId].CeilWalk)))
    then Form1.ChangeSpeedBy(-1);
    if ((Camera.Position.Z>8.5) and
    (((pz in Map[PosX,PosZ].UpWalls) and players[MyId].CeilWalk)
    or ((pz in Map[PosX,PosZ].DownWalls) and not players[MyId].CeilWalk)))
    then Form1.ChangeSpeedBy(-1);
  end;
end;


procedure ChangePlayerSpeed(LastX,LastZ:Integer);
label LNormalLoss;
begin
  if sqrt(sqr(PosX)+sqr(PosZ))>Spawnradius then begin
  
    if GetRT(PosX,PosZ).SPInvisible
    and not Form3.ClientSocket.Active
    and not Form3.ServerSocket.Active then begin
      NormalSpeedLoss(LastX,LastZ);
      Exit;
    end;

    if GetRT(PosX,PosZ).ChangeSpeedRandom[THeight(Players[MyID].Ceilwalk)]
    then begin
      if not Won then begin
        Form1.ChangeSpeedBy(
        GetRT(PosX,PosZ).ChangeSpeedAtArrival*(Random-0.4)/0.6);
      end else begin
        Form1.ChangeSpeedBy(GetRT(PosX,PosZ).ChangeSpeedAtArrival*Random);
      end;   
      Form1.Panel11.Visible:=true;
      Form1.Panel11.Caption:='Speed changed randomly to '
      +Floattostr(Round(100*Players[MyID].Speed)/100);
      Panel11Seconds:=3;
    end else if (GetRT(PosX,PosZ).ChangeSpeedAtArrival<0.0001)
    and (GetRT(PosX,PosZ).ChangeSpeedAtArrival>-0.0001) then begin
      NormalSpeedLoss(LastX,LastZ)
    end else if (GetRT(PosX,PosZ).ChangeSpeedAtArrival<0.0001)
    and not Won then begin
      Form1.ChangeSpeedBy(GetRT(PosX,PosZ).ChangeSpeedAtArrival);
    end else if (GetRT(PosX,PosZ).ChangeSpeedAtArrival>0.0001) then begin
      Form1.ChangeSpeedBy(GetRT(PosX,PosZ).ChangeSpeedAtArrival);
    end;
  end;
end;

procedure MoveToRoom(LastX,LastZ:Integer);
var Pos:TPoint; C1:Integer; OrigHeight:THeight;
label LSPInvisible,LOutOfMaze;
begin
  OrigHeight:=THeight(Players[MyID].Ceilwalk);
  
  if PosX<-mapsize+RaumRange+1 then
  PosX:=mapsize-RaumRange-1;
  if PosX>mapsize-RaumRange-1 then
  PosX:=-mapsize+RaumRange+1;
  if PosZ<-mapsize+RaumRange+1 then
  PosZ:=mapsize-RaumRange-1;
  if PosZ>mapsize-RaumRange-1 then
  PosZ:=-mapsize+RaumRange+1;

  if Players[MyID].OutOfMaze then
  goto LOutOfMaze;

  ChangePlayerSpeed(LastX,LastZ);

  if GetRT(PosX,PosZ).SPInvisible
  and not Form3.ClientSocket.Active
  and not Form3.ServerSocket.Active then
  goto LSPInvisible;

  if GetRT(PosX,PosZ).PortPlayerToCenter and not Won then begin
    Camera.Up.X:=0;
    Camera.Up.Z:=0;
    Camera.Position.X:=0;
    Camera.Position.Z:=0;
  end;

  
  if GetRT(PosX,PosZ).TurnPlayer and not Won then begin
    Camera.Direction.X:=0;
    Camera.Direction.Y:=0;
    Camera.Direction.Z:=0;
    if not players[MyId].CeilWalk
    then Camera.Up.Y:=1
    else Camera.Up.Y:=-1;
    Camera.Turn(Random(360));
  end;
        
  if GetRT(PosX,PosZ).WinAtArrival then
  if not Won then begin
    Form1.WinGame(MyID);  
    Form3.SendWin(MyID);
  end;

  if not Players[MyID].jumping then begin
    Players[MyID].jumptime:=
    (100*sqrt(sqr(Form4.JumpStrength(Players[MyID].Speed))-
    (981*(-2+PlayerDepth[false]))/50)+100
    *Form4.JumpStrength(Players[MyID].Speed))/981
  end;

  if GetRT(PosX,PosZ).YSNP[THeight(Players[MyID].Ceilwalk)] and (not Won)
  then begin
    Form1.Panel8.Font.Color:=RGB(150,25,25);
    Form1.Panel8.Color:=RGB(38,38,38);
    Form1.Panel8.Visible:=true;
    Form1.Panel8.Caption:='You Shall not Pass!';
    Form1.LoseGame;
  end;

  if GetRT(PosX,PosZ).OPTanzGegner and (not Won)
  then begin
    Form1.Panel8.Font.Color:=RGB(202,0,215);
    Form1.Panel8.Color:=RGB(127,127,127);
    Form1.Panel8.Visible:=true;
    Form1.Panel8.Caption:='Overpowered: Muh!';
    Form1.LoseGame;
  end;
  
  if (Map[PosX,PosZ].Inhalt.Sorte=Integer(RainbowTrap)) and (not Won)
  then begin   //todo 2:ini
    Form1.RichEdit1.Color:=RGB(38,38,38);
    Form1.RichEdit1.Visible:=true;
    Form1.RichEdit1.Lines.Clear;
    Form1.RichEdit1.Lines.Add('');
    Form1.RichEdit1.Lines.Add('A Hidden Rainbow!');

    Form1.RichEdit1.SelStart:=2;
    Form1.RichEdit1.SelLength:=1;
    Form1.RichEdit1.SelAttributes.Color := RGB(122,0,255);

    Form1.RichEdit1.SelStart:=4;
    Form1.RichEdit1.SelLength:=2;
    Form1.RichEdit1.SelAttributes.Color := RGB(0,0,255);

    Form1.RichEdit1.SelStart:=6;
    Form1.RichEdit1.SelLength:=2;
    Form1.RichEdit1.SelAttributes.Color := RGB(0,184,0);

    Form1.RichEdit1.SelStart:=8;
    Form1.RichEdit1.SelLength:=2;
    Form1.RichEdit1.SelAttributes.Color := RGB(244,244,0);

    Form1.RichEdit1.SelStart:=11;
    Form1.RichEdit1.SelLength:=1;
    Form1.RichEdit1.SelAttributes.Color := RGB(244,177,0);

    Form1.RichEdit1.SelStart:=12;
    Form1.RichEdit1.SelLength:=2;
    Form1.RichEdit1.SelAttributes.Color := RGB(246,74,0);

    Form1.RichEdit1.SelStart:=14;
    Form1.RichEdit1.SelLength:=1;
    Form1.RichEdit1.SelAttributes.Color := RGB(255,0,0);

    Form1.RichEdit1.SelStart:=16;
    Form1.RichEdit1.SelLength:=2;
    Form1.RichEdit1.SelAttributes.Color := RGB(135,1,0);

    Form1.RichEdit1.SelStart:=18;
    Form1.RichEdit1.SelLength:=1;
    Form1.RichEdit1.SelAttributes.Color := RGB(0,0,0);

    
    RE1Seconds:=3;
  end;

  if GetRT(PosX,PosZ).ChangeWallsAtArrival and (not Won) then
  WarpWalls(LastX,LastZ);

  if GetRT(PosX,PosZ).WallCageTrap and (not Won) then begin
    Map[PosX,PosZ].Walls:=[pz,px,nx,nz];
    Map[PosX,PosZ].UpWalls:=[];
    Map[PosX,PosZ].DownWalls:=[];
    Map[PosX+1,PosZ].Walls:=Map[PosX+1,PosZ].Walls+[nx];
    Map[PosX-1,PosZ].Walls:=Map[PosX+1,PosZ].Walls+[px];
    Map[PosX,PosZ+1].Walls:=Map[PosX+1,PosZ].Walls+[nz];
    Map[PosX,PosZ-1].Walls:=Map[PosX+1,PosZ].Walls+[pz];
    Map[PosX+1,PosZ].UpWalls:=Map[PosX+1,PosZ].UpWalls-[nx];
    Map[PosX-1,PosZ].UpWalls:=Map[PosX+1,PosZ].UpWalls-[px];
    Map[PosX,PosZ+1].UpWalls:=Map[PosX+1,PosZ].UpWalls-[nz];
    Map[PosX,PosZ-1].UpWalls:=Map[PosX+1,PosZ].UpWalls-[pz];
    Map[PosX+1,PosZ].DownWalls:=Map[PosX+1,PosZ].DownWalls-[nx];
    Map[PosX-1,PosZ].DownWalls:=Map[PosX+1,PosZ].DownWalls-[px];
    Map[PosX,PosZ+1].DownWalls:=Map[PosX+1,PosZ].DownWalls-[nz];
    Map[PosX,PosZ-1].DownWalls:=Map[PosX+1,PosZ].DownWalls-[pz];
    Pos.X:=PosX;
    Pos.Y:=PosZ;
    Form3.SendRaum(Pos);
    Pos.X:=PosX+1;
    Form3.SendRaum(Pos); 
    Pos.X:=PosX-1;
    Form3.SendRaum(Pos); 
    Pos.X:=PosX;
    Pos.Y:=PosZ+1;
    Form3.SendRaum(Pos);
    Pos.Y:=PosZ-1;
    Form3.SendRaum(Pos);
  end;   
  if GetRT(PosX,PosZ).Contaminate and (not Won)  
  then begin
    Form1.ChangeSpeedBy(-RandomRange(-50,292)/100);
    Form1.Panel11.Font.Color:=RGB(0,0,0);
    Form1.Panel11.Color:=RGB(249,245,0);
    Form1.Panel11.Visible:=true;
    Form1.Panel11.Caption:='Contamination! Speed decreased to '
    +Floattostr(Round(100*Players[MyID].Speed)/100);
    Panel11Seconds:=3;
    if Form3.ClientSocket.Active then  
    Form3.SendContaminate(PosX,PosZ) else
    Form1.SpreadContamination(PosX,PosZ);
  end;
  
  if GetRT(PosX,PosZ).InkOthers[THeight(Players[MyID].Ceilwalk)]
  then Form3.SendInkTrigger(MyID);

  if GetRT(PosX,PosZ).PortTo[Up] and (not Won)
  and not players[MyId].CeilWalk
  then begin
    players[MyId].CeilWalk:=true;
    Form3.SendCeilWalk;
    Camera.Up.SetVector(0,-1,0);
  end else
  if GetRT(PosX,PosZ).PortTo[Down] and (not Won)
  and players[MyId].CeilWalk
  then begin
    players[MyId].CeilWalk:=false;
    Form3.SendCeilWalk;
    Camera.Up.SetVector(0,1,0);
  end;
  if GetRT(PosX,PosZ).Explode[THeight(Players[MyID].Ceilwalk)]
  then begin              
    Form3.SendExplode(PosX,PosZ);
    Form1.Explode(PosX,PosZ);
  end else if GetRT(PosX,PosZ).ChangeOtherPlayerSpeed then begin
    if Form3.ClientSocket.Active then begin
      Form3.ClientSocket.Socket.SendText(
      'Lightning;'
      +IntToStr(MyID)+';')
    end else if Form3.ServerSocket.Active then begin
      Form3.SendSpeedChange(MyID);
    end;
  end;
  if GetRT(PosX,PosZ).ExplodeOtherPlayer[THeight(Players[MyID].Ceilwalk)]
  then begin
    for C1:=0 to High(Players) do
    if (Cardinal(C1)<>MyID)
    and not Players[C1].Dead then begin   
      Form3.SendExplode(Round(Players[C1].X/20),Round(Players[C1].Z/20));
      Form1.Explode(Round(Players[C1].X/20),Round(Players[C1].Z/20));
    end;
    if not Won then begin
      Form1.Panel13.Font.Color:=RGB(255,255,255);
      Form1.Panel13.Color:=RGB(0,0,0);
      Form1.Panel13.Visible:=true;
      Form1.Panel13.Caption:='BOOM';
      Panel13Seconds:=2;
    end;
  end;

  LSPInvisible:


  Form1.GLHUDText1.Text:='Speed: '
  +Floattostr(Round(100*Players[MyID].Speed)/100)+' m/s';
  if Players[MyID].Speed<0.0001 then
  Form1.GLHUDText1.Text:='Speed: 0 m/s'; 

  if GetPC(PosX,PosZ).Text<>'' then
  Form1.ShowPanel(Map[PosX,PosZ].Inhalt.Sorte);

  if GetRT(PosX,PosZ).ChangeAtArrivalTo[OrigHeight]>=0 then begin
    Map[PosX,PosZ].Inhalt.Sorte:=GetRT(PosX,PosZ).ChangeAtArrivalTo[OrigHeight];

    if GetRT(PosX,PosZ).TurnRandom[Up]
    or GetRT(PosX,PosZ).TurnRandom[Down] then begin
      if Assigned(Map[PosX,PosZ].Inhalt.Daten) then
      Map[PosX,PosZ].Inhalt.Daten:=@Richtungen[
      TRichtung(Random(Integer(High(TRichtung))+1))];
      Pos.X:=PosX;
      Pos.Y:=PosZ;
      Form3.SendRaum(Pos);
    end else 
    Form3.SendRaumSorte(PosX,PosZ);
  end;

  LOutOfMaze:

  if Players[MyID].Ceilwalk then
  Form1.GLNavigator1.VirtualUp.SetPoint(0,-1,0)
  else
  Form1.GLNavigator1.VirtualUp.SetPoint(0,1,0);

  if Random<0.2 then
  Form1.Memo1.Visible:=false;

  Form1.Reload;
end;


end.
