unit UNetwork;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ScktComp, ExtCtrls;
                             
type                         
  TForm3 = class(TForm)
    Timer3: TTimer;
    ClientSocket: TClientSocket;
    ServerSocket: TServerSocket;
    procedure ClientSocketConnect(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure ClientSocketDisconnect(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure ClientSocketError(Sender: TObject; Socket: TCustomWinSocket;
      ErrorEvent: TErrorEvent; var ErrorCode: Integer);
    procedure SocketRead(Sender: TObject; Socket: TCustomWinSocket);
    procedure ServerSocketClientConnect(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure Timer3Timer(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure SendMap(Socket: TCustomWinSocket);
    procedure SendNewID(Socket: TCustomWinSocket);
    procedure SendGoalPos(Socket: TCustomWinSocket); 
    procedure RequestNewID(Socket: TCustomWinSocket);
    procedure AddToPlayerArr(ID:Cardinal);
  private
  public
    procedure SendSpeedChange(ID:Cardinal);
    procedure SendRaum(Socket: TCustomWinSocket; RaumPos:TPoint);overload;
    procedure SendRaum(RaumPos:TPoint);overload;
    procedure SendCeilWalk; overload;
    procedure SendCeilWalk(Socket: TCustomWinSocket); overload;
    procedure SendCeilWalk(ID: Cardinal); overload;
    procedure SendCeilWalk(ID: Cardinal; Socket: TCustomWinSocket); overload;
    procedure SendWin(ID: Cardinal); overload;
    procedure SendWin(ID: Cardinal;Socket: TCustomWinSocket); overload;
    procedure SendKill(ID: Cardinal); overload;
    procedure SendKill(ID: Cardinal;Socket: TCustomWinSocket); overload;
    procedure SendJump; overload;
    procedure SendJump(Socket: TCustomWinSocket); overload;
    procedure SendJump(ID: Cardinal); overload;
    procedure SendJump(ID: Cardinal; Socket: TCustomWinSocket); overload;
    procedure SendRaumSorte(Socket: TCustomWinSocket; RaumPos:TPoint); overload;
    procedure SendRaumSorte(RaumPos:TPoint); overload;
    procedure SendRaumSorte(X,Z:Integer); overload;
    procedure SendExplode(X,Z:Integer; Socket: TCustomWinSocket); overload;
    procedure SendExplode(X,Z:Integer); overload;
    procedure SendContaminate(X,Z:Integer);
    procedure SendPosition; overload;
    procedure SendPosition(Socket: TCustomWinSocket); overload;
    procedure SendPosition(ID: Cardinal); overload;
    procedure SendPosition(ID: Cardinal; Socket: TCustomWinSocket); overload;  
    procedure SendInkTrigger(ID: Cardinal); overload;
    procedure SendInkTrigger(ID: Cardinal;Socket: TCustomWinSocket); overload;
  end;

  
var
  Form3:TForm3;
  IDRecieved:Boolean=true;

  Buffer:Ansistring;
  NextObjectToRecieve:Ansistring;
  LastObjectRecieved:Ansistring;
  RoomRecievePos:TPoint;

  MPServerAddress:string='Localhost';
  MPPort:Integer=10808;

implementation
uses Unit1, UMaterials,Math,UCreateRaum;

{$R *.dfm}


procedure TForm3.AddToPlayerArr(ID:Cardinal);
var C3:Integer;
begin
  if ID>=Cardinal(Length(Players)) then setlength(Players,ID+1);
  if ID>=Cardinal(Length(playerarr)) then begin
    Setlength(playerarr,ID+1);
    for C3:=0 to High(playerarr) do
    if not assigned(playerarr[C3])
    then playerarr[C3]:=Form1.NewPlayer(C3);
  end;
  Players[ID].Dead:=false;
end;

procedure TForm3.SocketRead(Sender: TObject;
  Socket: TCustomWinSocket);
var C1,C2,C3,C6,C5:Integer; Data:array of AnsiString;
  P1:PWallset; C4:Byte; S1:AnsiString; B1:Boolean;
begin
  Buffer:=Buffer+Socket.ReceiveText;        
  C2:=0;
  C3:=1;
  for C1:=1 to Length(Buffer) do
  if Buffer[C1]=';' then begin
    Inc(C2);
    Setlength(Data,C2);
    Data[C2-1]:=copy(Buffer,C3,C1-C3);
    C3:=C1+1;
  end;
  S1:=copy(Buffer,C3,Length(Buffer));

  C1:=0;
  try
    while true do begin
      if NextObjectToRecieve='Reload' then begin
        Form1.Reload;
        LastObjectRecieved:=NextObjectToRecieve;
        NextObjectToRecieve:='';
        Continue;
      end;
      
      if NextObjectToRecieve='RequestNewID' then begin
        if ServerSocket.Active then
        SendNewID(Socket);      
        LastObjectRecieved:=NextObjectToRecieve;
        NextObjectToRecieve:='';
        Continue;
      end;

      if C1>High(Data) then Break;      

      if NextObjectToRecieve='Lightning' then begin   
        C2:=strtoint(String(Data[C1]));
        if ServerSocket.Active then SendSpeedChange(C2);
        LastObjectRecieved:=NextObjectToRecieve;
        NextObjectToRecieve:='';
        Continue;
      end;

      if NextObjectToRecieve='PlayerKill' then begin
        C2:=strtoint(String(Data[C1]));
        Players[C2].Dead:=true;


        if ServerSocket.Active then
        SendKill(C2);
        
        LastObjectRecieved:=NextObjectToRecieve;
        NextObjectToRecieve:='';  
        C1:=C1+1;
        Continue;
      end;

      if NextObjectToRecieve='GameWin' then begin
        C2:=strtoint(String(Data[C1]));
        Form1.WinGame(C2);


        if ServerSocket.Active then
        SendWin(C2);
        LastObjectRecieved:=NextObjectToRecieve;
        NextObjectToRecieve:='';  
        C1:=C1+1;
        Continue;
      end;

      if NextObjectToRecieve='InkTrigger' then begin
        C2:=strtoint(String(Data[C1]));
        Form1.Memo1.Visible:=true;


        if ServerSocket.Active then
        SendInkTrigger(C2);
        
        LastObjectRecieved:=NextObjectToRecieve;
        NextObjectToRecieve:='';  
        C1:=C1+1;
        Continue;
      end;

      if NextObjectToRecieve='' then begin
        NextObjectToRecieve:=Data[C1];
        C1:=C1+1;
        Continue;
      end;

      if NextObjectToRecieve='NewID' then begin
        IDRecieved:=true; 
        Players[MyID].Dead:=true;
        SendKill(MyID);
        
        MyID:=strtoint(String(Data[C1]));
        AddToPlayerArr(MyID);
        Form1.ChangeSpeedTo(StartSpeed);
        Form1.GLHUDText1.Text:='Speed: '+Floattostr(Round(100*Players[MyID].Speed)/100);
        Form1.GLCamera1.Position.X:=0;
        Form1.GLCamera1.Position.Z:=0; 
        Form1.GLCamera1.Up.SetVector(0,1,0);
        PosX:=0;
        PosZ:=0;
        Players[MyID].jumping:=false;
        Players[MyID].X:=0;
        Players[MyID].Y:=0;
        Players[MyID].Z:=0;
        Players[MyID].LastX:=0;
        Players[MyID].LastZ:=0;
        Players[MyID].Ceilwalk:=false;      
        Form1.Reload;
        LastObjectRecieved:=NextObjectToRecieve;
        NextObjectToRecieve:='';
        C1:=C1+1;
        Continue;
      end;

      if NextObjectToRecieve='JumpStart' then begin
        C2:=StrToInt(Data[C1]);
        if C2>=Length(Players) then begin 
          LastObjectRecieved:=NextObjectToRecieve;
          NextObjectToRecieve:='';
          C1:=C1+1;
          Continue;
        end;
        Form1.startJump(C2);

        if ServerSocket.Active then
        for C3:=0 to ServerSocket.Socket.ActiveConnections-1 do
        if ServerSocket.Socket.Connections[C3]<>Socket then
        SendJump(C2,ServerSocket.Socket.Connections[C3]);
        
        LastObjectRecieved:=NextObjectToRecieve;
        NextObjectToRecieve:='';

        C1:=C1+1;
        Continue;
      end;


      if C1+1>High(Data) then Break;  

      if NextObjectToRecieve='ChangeSpeed' then begin
        C3:=Strtoint(Data[C1+1]);
        if not ClientSocket.Active then begin
          LastObjectRecieved:=NextObjectToRecieve;
          NextObjectToRecieve:='';
          C1:=C1+2;
          Continue;
        end;

        Form1.MultiplySpeedBy(sqrt(
        sqr(Players[MyID].X-GoalPos.X*20)
        +sqr(Players[MyID].Z-GoalPos.Y*20))
        /StrToFloat(Data[C1]));

        if Cardinal(C3)<>MyID then begin
          Form1.Panel11.Font.Color:=RGB(255,255,0);
          Form1.Panel11.Color:=RGB(0,0,0);
          Form1.Panel11.Visible:=true;
          Form1.Panel11.Caption:='Speed decreased to '
          +Floattostr(Round(100*Players[MyID].Speed)/100);
          Panel11Seconds:=3;
          Form1.GLHUDText1.Text:='Speed: '
          +Floattostr(Round(100*Players[MyID].Speed)/100);
        end;

        LastObjectRecieved:=NextObjectToRecieve;
        NextObjectToRecieve:='';
        C1:=C1+2;
        Continue;
      end;

      if NextObjectToRecieve='CreateRaum' then begin
        RoomRecievePos.X:=Strtoint(Data[C1]);
        RoomRecievePos.Y:=Strtoint(Data[C1+1]);
        if not Map[RoomRecievePos.X,RoomRecievePos.Y].created then
        CreateRaum(RoomRecievePos.X,RoomRecievePos.Y);
        SendRaum(Socket,RoomRecievePos);
        LastObjectRecieved:=NextObjectToRecieve;
        NextObjectToRecieve:='';
        C1:=C1+2;
        Continue;
      end;  

      if NextObjectToRecieve='Explode' then begin
        RoomRecievePos.X:=Strtoint(Data[C1]);
        RoomRecievePos.Y:=Strtoint(Data[C1+1]);
        Form1.Explode(RoomRecievePos.X,RoomRecievePos.Y);
        Form1.Reload;

        if ServerSocket.Active then
        for C3:=0 to ServerSocket.Socket.ActiveConnections-1 do
        if ServerSocket.Socket.Connections[C3]<>Socket then
        SendExplode(RoomRecievePos.X,RoomRecievePos.Y,
        ServerSocket.Socket.Connections[C3]);
        
        LastObjectRecieved:=NextObjectToRecieve;
        NextObjectToRecieve:='';
        C1:=C1+2;
        Continue;
      end; 

      if NextObjectToRecieve='Contaminate' then begin
        RoomRecievePos.X:=Strtoint(Data[C1]);
        RoomRecievePos.Y:=Strtoint(Data[C1+1]);

        Form1.SpreadContamination(RoomRecievePos.X,RoomRecievePos.Y);

        LastObjectRecieved:=NextObjectToRecieve;
        NextObjectToRecieve:='';
        C1:=C1+2;
        Continue;
      end;

      if NextObjectToRecieve='GoalPos' then begin
        if not ServerSocket.Active then begin
          GoalPos.X:=Strtoint(String(Data[C1]));
          GoalPos.Y:=Strtoint(String(Data[C1+1]));
        end;
        LastObjectRecieved:=NextObjectToRecieve;
        NextObjectToRecieve:='';
        C1:=C1+2;
        Continue;
      end;

      if NextObjectToRecieve='CeilWalk' then begin
        C2:=strtoint(Data[C1]);
        
        AddToPlayerArr(C2);

        Players[C2].Ceilwalk:=StrToBool(String(Data[C1+1]));


        if ServerSocket.Active then
        for C3:=0 to ServerSocket.Socket.ActiveConnections-1 do
        if ServerSocket.Socket.Connections[C3]<>Socket then
        SendCeilWalk(C2,ServerSocket.Socket.Connections[C3]);
                                 
        LastObjectRecieved:=NextObjectToRecieve;
        NextObjectToRecieve:='';
        C1:=C1+2;
        Continue;
      end;



      if C1+2>High(Data) then Break;

      if NextObjectToRecieve='RaumSorte' then begin
        RoomRecievePos.X:=Strtoint(String(Data[C1]));
        RoomRecievePos.Y:=Strtoint(String(Data[C1+1]));
        Map[RoomRecievePos.X,RoomRecievePos.Y].Inhalt.Sorte:=
        Strtoint(Data[C1+2]);

        if ServerSocket.Active then
        for C3:=0 to ServerSocket.Socket.ActiveConnections-1 do
        if ServerSocket.Socket.Connections[C3]<>Socket then
        SendRaumSorte(ServerSocket.Socket.Connections[C3],RoomRecievePos);

        C1:=C1+3;
        LastObjectRecieved:=NextObjectToRecieve;
        NextObjectToRecieve:='';  
        Form1.ReloadRoom(RoomRecievePos.X,RoomRecievePos.Y);
        Continue;
      end;

      if NextObjectToRecieve='RaumRichtung' then begin
        RoomRecievePos.X:=Strtoint(Data[C1]);
        RoomRecievePos.Y:=Strtoint(Data[C1+1]);
        Map[RoomRecievePos.X,RoomRecievePos.Y].Inhalt.Daten:=
        @Richtungen[TRichtung(Strtoint(Data[C1+2]))]; 
        Form1.ReloadRoom(RoomRecievePos.X,RoomRecievePos.Y);
        
        C1:=C1+3;
        LastObjectRecieved:=NextObjectToRecieve;
        NextObjectToRecieve:='';
        Continue;
      end;

        
      if C1+3>High(Data) then Break;   

      //Spielerposition empfangen
      if NextObjectToRecieve='Position' then begin
        C2:=strtoint(Data[C1]);
        if Cardinal(C2)=MyID then begin
          C1:=C1+4;
          LastObjectRecieved:=NextObjectToRecieve;
          NextObjectToRecieve:='';
          Continue;
        end;
        AddToPlayerArr(C2);
        Players[C2].LastX:=Players[C2].X;
        Players[C2].LastZ:=Players[C2].Z;
        Players[C2].LastTimeStart:=GetTickCount;
        Players[C2].LastTimeEnd:=GetTickCount+Timer3.Interval;


        Players[C2].X:=Strtofloat(String(Data[C1+1]));
        Players[C2].Z:=Strtofloat(String(Data[C1+2]));
        Players[C2].Speed:=Strtofloat(String(Data[C1+3]));

        C1:=C1+4;
        LastObjectRecieved:=NextObjectToRecieve;
        NextObjectToRecieve:='';
        Continue;
      end;

      if C1+5>High(Data) then Break;

      //Raum empfangen
      if NextObjectToRecieve='Raum' then begin

        B1:=GetRT(PosX,PosZ).Contaminate;


        RoomRecievePos.X:=Strtoint(String(Data[C1]));
        RoomRecievePos.Y:=Strtoint(String(Data[C1+1]));
        C4:=Strtoint(String(Data[C1+2]));
        P1:=@C4;
        Map[RoomRecievePos.X,RoomRecievePos.Y].Walls:=P1^;
        C4:=Strtoint(String(Data[C1+3]));
        P1:=@C4;
        Map[RoomRecievePos.X,RoomRecievePos.Y].UpWalls:=P1^;
        C4:=Strtoint(String(Data[C1+4]));
        P1:=@C4;
        Map[RoomRecievePos.X,RoomRecievePos.Y].DownWalls:=P1^;
        Map[RoomRecievePos.X,RoomRecievePos.Y].Inhalt.Sorte:=
        Strtoint(String(Data[C1+5]));
        Map[RoomRecievePos.X,RoomRecievePos.Y].Inhalt.Daten:=nil;
        Map[RoomRecievePos.X,RoomRecievePos.Y].created:=true;
        LastObjectRecieved:=NextObjectToRecieve;

            


        if GetRT(PosX,PosZ).Contaminate and not B1 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;
        end;

        NextObjectToRecieve:='';
        C1:=C1+6;
        Form1.ReloadRoom(RoomRecievePos.X,RoomRecievePos.Y);
        Continue;
      end;


      LastObjectRecieved:='unknown: '+NextObjectToRecieve;
      NextObjectToRecieve:='';
    end;
  except
    AssignFile(log,'NetworkLog.txt');
    if not FileExists('NetworkLog.txt') then begin
      Rewrite(log);
      Write(log,buffer);
      CloseFile(log);
    end;
  end;

  Buffer:='';
  for C2:=C1 to High(Data) do
  Buffer:=Buffer+Data[C2]+';';
  Buffer:=Buffer+S1;
end;









procedure TForm3.ClientSocketConnect(Sender: TObject;
  Socket: TCustomWinSocket);
var C1,C2:Integer;
begin
  IDRecieved:=false;
  for C1:=-mapsize to mapsize do
  for C2:=-mapsize to mapsize do Map[C1,C2].created:=false;
  ShowMessage('Connected to Server '+MPServerAddress+':'+Inttostr(MPPort));
end;

procedure TForm3.ServerSocketClientConnect(Sender: TObject;
  Socket: TCustomWinSocket);
begin
  SendNewID(Socket);
  SendMap(Socket);
  SendGoalPos(Socket);
end;

procedure TForm3.ClientSocketDisconnect(Sender: TObject;
  Socket: TCustomWinSocket);
begin
  ShowMessage('Disconnected from Server');
end;

procedure TForm3.ClientSocketError(Sender: TObject;
  Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
  var ErrorCode: Integer);
begin
  if ErrorCode=10061 then begin
    ShowMessage('Server not found');
    ErrorCode:=0;
  end;
end;











procedure TForm3.SendExplode(X,Z:Integer; Socket: TCustomWinSocket);
begin
  Socket.SendText(AnsiString(
  'Explode;'
  +IntToStr(X)+';'
  +IntToStr(Z)+';'));
end;

procedure TForm3.SendExplode(X,Z:Integer);
var C2:Integer;
begin
  if ClientSocket.Active then SendExplode(X,Z,ClientSocket.Socket)
  else if ServerSocket.Active then
  for C2:=0 to ServerSocket.Socket.ActiveConnections-1 do begin
    SendExplode(X,Z,ServerSocket.Socket.Connections[C2]);
  end;
end;


procedure TForm3.SendContaminate(X,Z:Integer);
begin
  ClientSocket.Socket.SendText(AnsiString(
  'Contaminate;'
  +IntToStr(X)+';'
  +IntToStr(Z)+';'));
end;

procedure TForm3.SendPosition(Socket: TCustomWinSocket);
begin          
  Socket.SendText(AnsiString(
  'Position;'
  +intToStr(MyID)+';'
  +FloatToStr(Players[MyID].X)+';'
  +FloatToStr(Players[MyID].Z)+';'
  +FloatToStr(Players[MyID].Speed)+';'));
end;

procedure TForm3.SendPosition;
var C2:Integer;
begin
  if ClientSocket.Active then SendPosition(ClientSocket.Socket)
  else if ServerSocket.Active then
  for C2:=0 to ServerSocket.Socket.ActiveConnections-1 do begin
    SendPosition(ServerSocket.Socket.Connections[C2]);
  end;
end;

procedure TForm3.SendPosition(ID:Cardinal);
var C2:Integer;
begin
  if ClientSocket.Active then SendPosition(ID,ClientSocket.Socket)
  else if ServerSocket.Active then
  for C2:=0 to ServerSocket.Socket.ActiveConnections-1 do begin
    SendPosition(ID,ServerSocket.Socket.Connections[C2]);
  end;
end;

procedure TForm3.SendPosition(ID:Cardinal; Socket: TCustomWinSocket);
begin
  Socket.SendText(AnsiString(
  'Position;'
  +intToStr(ID)+';'
  +FloatToStr(Players[ID].X)+';'
  +FloatToStr(Players[ID].Z)+';'
  +FloatToStr(Players[ID].Speed)+';'));
end;




procedure TForm3.Timer3Timer(Sender: TObject);
var C2:Integer;
begin
  if (not IDRecieved) and ClientSocket.Active then Exit;

  if ClientSocket.Active then SendPosition
  else if ServerSocket.Active then begin
    for C2:=0 to High(Players) do
    if not Players[C2].Dead then
    SendPosition(C2);
  end;
end;












procedure TForm3.FormCreate(Sender: TObject);
begin
  ServerSocket.Active:=false;
  ClientSocket.Active:=false;
  ClientSocket.Port:=MPPort;
  ServerSocket.Port:=MPPort;
  ClientSocket.Host:=MPServerAddress;
end;

procedure TForm3.FormDestroy(Sender: TObject);
begin
  Timer3.Enabled:=false;
end;
      

procedure TForm3.SendMap(Socket: TCustomWinSocket);
var C1,C2:Integer;RaumPos:TPoint;
begin
  for C1:=-raumrange to raumrange do
  for C2:=-raumrange to raumrange do
  if Map[C1,C2].created then begin 
    RaumPos.X:=C1;
    RaumPos.Y:=C2;
    SendRaum(Socket,RaumPos);
  end;
end;


procedure TForm3.SendCeilWalk(Socket: TCustomWinSocket);
begin
  Socket.SendText( AnsiString(
  'CeilWalk;'   
  +IntToStr(MyID)+';'
  +BoolToStr(players[MyID].CeilWalk)+';'));
end;

procedure TForm3.SendCeilWalk;
var C2:Integer;
begin
  if ClientSocket.Active then SendCeilWalk(ClientSocket.Socket)
  else if ServerSocket.Active then
  for C2:=0 to ServerSocket.Socket.ActiveConnections-1 do begin
    SendCeilWalk(ServerSocket.Socket.Connections[C2]);
  end;
end;

procedure TForm3.SendCeilWalk(ID:Cardinal);
var C2:Integer;
begin
  if ClientSocket.Active then SendCeilWalk(ID,ClientSocket.Socket)
  else if ServerSocket.Active then
  for C2:=0 to ServerSocket.Socket.ActiveConnections-1 do begin
    SendCeilWalk(ID,ServerSocket.Socket.Connections[C2]);
  end;
end;

procedure TForm3.SendCeilWalk(ID:Cardinal; Socket: TCustomWinSocket);
begin
  Socket.SendText(AnsiString(
  'CeilWalk;'
  +IntToStr(ID)+';'
  +BoolToStr(players[ID].CeilWalk)+';'));
end;


procedure TForm3.SendJump(Socket: TCustomWinSocket);
begin
  Socket.SendText( AnsiString(
  'JumpStart;'   
  +IntToStr(MyID)+';'));
end;

procedure TForm3.SendJump;
var C2:Integer;
begin
  if ClientSocket.Active then SendJump(ClientSocket.Socket)
  else if ServerSocket.Active then
  for C2:=0 to ServerSocket.Socket.ActiveConnections-1 do begin
    SendJump(ServerSocket.Socket.Connections[C2]);
  end;
end;

procedure TForm3.SendJump(ID:Cardinal);
var C2:Integer;
begin
  if ClientSocket.Active then SendJump(ID,ClientSocket.Socket)
  else if ServerSocket.Active then
  for C2:=0 to ServerSocket.Socket.ActiveConnections-1 do begin
    SendJump(ID,ServerSocket.Socket.Connections[C2]);
  end;
end;

procedure TForm3.SendJump(ID:Cardinal; Socket: TCustomWinSocket);
begin
  Socket.SendText(AnsiString(
  'JumpStart;'
  +IntToStr(ID)+';'));
end;





procedure TForm3.SendKill(ID: Cardinal; Socket: TCustomWinSocket);
begin
  Socket.SendText(AnsiString(
  'PlayerKill;'
  +IntToStr(ID)+';'));
end;

procedure TForm3.SendKill(ID: Cardinal);
var C2:Integer;
begin
  if ClientSocket.Active
  then SendKill(ID,ClientSocket.Socket)
  else if ServerSocket.Active
  then for C2:=0 to ServerSocket.Socket.ActiveConnections-1 do
  SendKill(ID,ServerSocket.Socket.Connections[C2]);
end;

procedure TForm3.SendWin(ID: Cardinal; Socket: TCustomWinSocket);
begin
  Socket.SendText(AnsiString(
  'GameWin;'
  +IntToStr(ID)+';'));
end;

procedure TForm3.SendWin(ID: Cardinal);
var C2:Integer;
begin
  if ClientSocket.Active
  then SendWin(ID,ClientSocket.Socket)
  else if ServerSocket.Active
  then for C2:=0 to ServerSocket.Socket.ActiveConnections-1 do
  SendWin(ID,ServerSocket.Socket.Connections[C2]);
end;

procedure TForm3.SendInkTrigger(ID: Cardinal; Socket: TCustomWinSocket);
begin
  Socket.SendText(AnsiString(
  'InkTrigger;'
  +IntToStr(ID)+';'));
end;

procedure TForm3.SendInkTrigger(ID: Cardinal);
var C2:Integer;
begin
  if ClientSocket.Active
  then SendInkTrigger(ID,ClientSocket.Socket)
  else if ServerSocket.Active
  then for C2:=0 to ServerSocket.Socket.ActiveConnections-1 do
  SendInkTrigger(ID,ServerSocket.Socket.Connections[C2]);
end;






procedure TForm3.SendSpeedChange(ID:Cardinal);
var C1,C2:Integer; MaxDist:Real;
begin
  if not ServerSocket.Active then Exit;

  MaxDist:=0;
  for C2:=0 to High(Players) do
  if Cardinal(C2)<>ID then
  if not Players[C2].Dead then
  MaxDist:=Max(MaxDist,
  sqrt(sqr(Players[C2].X-GoalPos.X*20)+sqr(Players[C2].Z-GoalPos.Y*20)));

  MaxDist:=MaxDist*2;


  if ID<>MyID then begin 
    Form1.MultiplySpeedBy(sqrt(
    sqr(Players[MyID].X-GoalPos.X*20)
    +sqr(Players[MyID].Z-GoalPos.Y*20))
    /MaxDist);
    Form1.Panel11.Font.Color:=RGB(255,255,0);
    Form1.Panel11.Color:=RGB(0,0,0);
    Form1.Panel11.Visible:=true;
    Form1.Panel11.Caption:='Speed decreased to '
    +Floattostr(Round(100*Players[MyID].Speed)/100);
    Panel11Seconds:=3;
    Form1.GLHUDText1.Text:='Speed: '
    +Floattostr(Round(100*Players[MyID].Speed)/100);
  end;

  for C1:=0 to ServerSocket.Socket.ActiveConnections-1 do
  ServerSocket.Socket.Connections[C1].SendText(Ansistring(
  'ChangeSpeed;'
  +FloatToStr(MaxDist)+';'
  +FloatToStr(ID)+';'));
end;

procedure TForm3.SendRaum(Socket: TCustomWinSocket; RaumPos:TPoint);
begin
  Socket.SendText(AnsiString(
  'Raum;'
  +IntToStr(RaumPos.X)+';'
  +IntToStr(RaumPos.Y)+';'
  +IntToStr(Byte(Map[RaumPos.X,RaumPos.Y].Walls))+';'
  +IntToStr(Byte(Map[RaumPos.X,RaumPos.Y].UpWalls))+';'
  +IntToStr(Byte(Map[RaumPos.X,RaumPos.Y].DownWalls))+';'
  +IntToStr(Byte(Map[RaumPos.X,RaumPos.Y].Inhalt.Sorte))+';'));
  if GetRT(RaumPos.X,RaumPos.Y).TurnRandom[Up]
  or GetRT(RaumPos.X,RaumPos.Y).TurnRandom[Down] then
  Socket.SendText(AnsiString(
  'RaumRichtung;'
  +IntToStr(RaumPos.X)+';'
  +IntToStr(RaumPos.Y)+';'
  +IntToStr(Byte(Map[RaumPos.X,RaumPos.Y].Inhalt.Daten^))+';'));
end;

procedure TForm3.SendRaum(RaumPos:TPoint);
var C2:Integer;
begin
  if ClientSocket.Active
  then SendRaum(ClientSocket.Socket,RaumPos)
  else if ServerSocket.Active
  then for C2:=0 to ServerSocket.Socket.ActiveConnections-1 do
  SendRaum(ServerSocket.Socket.Connections[C2],RaumPos);
end;

procedure TForm3.SendRaumSorte(Socket: TCustomWinSocket; RaumPos:TPoint);
begin
  Socket.SendText(AnsiString(
  'RaumSorte;'
  +IntToStr(RaumPos.X)+';'
  +IntToStr(RaumPos.Y)+';'
  +IntToStr(Byte(Map[RaumPos.X,RaumPos.Y].Inhalt.Sorte))+';'));
end;

procedure TForm3.SendRaumSorte(RaumPos:TPoint);
var C2:Integer;
begin
  if ClientSocket.Active
  then SendRaumSorte(ClientSocket.Socket,RaumPos)
  else if ServerSocket.Active
  then for C2:=0 to ServerSocket.Socket.ActiveConnections-1 do
  SendRaumSorte(ServerSocket.Socket.Connections[C2],RaumPos);
end;

procedure TForm3.SendRaumSorte(X,Z:Integer);
var RaumPos:TPoint;
begin
  RaumPos.X:=X;
  RaumPos.Y:=Z;
  SendRaumSorte(RaumPos);
end;

procedure TForm3.SendNewID(Socket: TCustomWinSocket);
var C1:Integer;
begin          
  C1:=Length(Players);
  
  AddToPlayerArr(C1);

  Socket.SendText(AnsiString(
  'NewID;'
  +IntToStr(C1)+';'));
end;

procedure TForm3.RequestNewID(Socket: TCustomWinSocket);
begin
  Socket.SendText(
  'RequestNewID;');
end;

procedure TForm3.SendGoalPos(Socket: TCustomWinSocket);
begin
  Socket.SendText(AnsiString(
  'GoalPos;'
  +IntToStr(GoalPos.X)+';'
  +IntToStr(GoalPos.Y)+';'));
end;

end.
