unit UCreateRaum;

interface

procedure CreateRaum(x, z: Integer);
                               
var
  WallProb:Real=0.31;
  UPWallProb:Real=0.11;
  DownWallProb:Real=0.11;
  
implementation

uses Unit1,UNetwork,SysUtils;

const emptyMap=false;
     

procedure CreateRaum(x,z:Integer);
var C2,C3:Real; C4:TRichtung; C1,C5:Integer;  found:Boolean;
begin

  repeat
    Map[x,z].Walls:=[];
    Map[x,z].UpWalls:=[];
    Map[x,z].DownWalls:=[];
    for C4:=Low(TRichtung) to High(TRichtung) do begin  
      C2:=Random;
      C3:=0;
      C3:=C3+WallProb; 
      if C2<C3 then begin
        Map[x,z].Walls:=
        Map[x,z].Walls+[C4];
        Continue;
      end;
      C3:=C3+UPWallProb;
      if C2<C3 then begin
        Map[x,z].UpWalls:=
        Map[x,z].UpWalls+[C4];
        Continue;
      end;
      C3:=C3+DownWallProb;
      if C2<C3 then begin
        Map[x,z].DownWalls:=
        Map[x,z].DownWalls+[C4]; 
        Continue;
      end;
    end;



    if Map[x+1,z].created then begin
      if nx in Map[x+1,z].Walls
      then Map[x,z].Walls:=Map[x,z].Walls+[px]
      else Map[x,z].Walls:=Map[x,z].Walls-[px];
      if nx in Map[x+1,z].UpWalls
      then Map[x,z].UpWalls:=Map[x,z].UpWalls+[px]
      else Map[x,z].UpWalls:=Map[x,z].UpWalls-[px];
      if nx in Map[x+1,z].DownWalls
      then Map[x,z].DownWalls:=Map[x,z].DownWalls+[px]
      else Map[x,z].DownWalls:=Map[x,z].DownWalls-[px];
    end;
    if Map[x-1,z].created then begin
      if px in Map[x-1,z].Walls
      then Map[x,z].Walls:=Map[x,z].Walls+[nx]
      else Map[x,z].Walls:=Map[x,z].Walls-[nx];
      if px in Map[x-1,z].UpWalls
      then Map[x,z].UpWalls:=Map[x,z].UpWalls+[nx]
      else Map[x,z].UpWalls:=Map[x,z].UpWalls-[nx];
      if px in Map[x-1,z].DownWalls
      then Map[x,z].DownWalls:=Map[x,z].DownWalls+[nx]
      else Map[x,z].DownWalls:=Map[x,z].DownWalls-[nx];
    end;
    if Map[x,z+1].created then begin
      if nz in Map[x,z+1].Walls
      then Map[x,z].Walls:=Map[x,z].Walls+[pz]
      else Map[x,z].Walls:=Map[x,z].Walls-[pz];
      if nz in Map[x,z+1].UpWalls
      then Map[x,z].UpWalls:=Map[x,z].UpWalls+[pz]
      else Map[x,z].UpWalls:=Map[x,z].UpWalls-[pz];
      if nz in Map[x,z+1].DownWalls
      then Map[x,z].DownWalls:=Map[x,z].DownWalls+[pz]
      else Map[x,z].DownWalls:=Map[x,z].DownWalls-[pz];
    end;
    if Map[x,z-1].created then begin
      if pz in Map[x,z-1].Walls
      then Map[x,z].Walls:=Map[x,z].Walls+[nz]
      else Map[x,z].Walls:=Map[x,z].Walls-[nz];
      if pz in Map[x,z-1].UpWalls
      then Map[x,z].UpWalls:=Map[x,z].UpWalls+[nz]
      else Map[x,z].UpWalls:=Map[x,z].UpWalls-[nz];
      if pz in Map[x,z-1].DownWalls
      then Map[x,z].DownWalls:=Map[x,z].DownWalls+[nz]
      else Map[x,z].DownWalls:=Map[x,z].DownWalls-[nz];
    end;  
    if Map[x,z-1].created
    and Map[x,z+1].created
    and Map[x+1,z].created
    and Map[x-1,z].created then Break;
    if  Map[x,z+1].created
    and Map[x+1,z].created
    and Map[x-1,z].created then Break;
    if Map[x,z-1].created
    and Map[x+1,z].created
    and Map[x-1,z].created then Break;
    if Map[x,z-1].created
    and Map[x,z+1].created
    and Map[x-1,z].created then Break;
    if Map[x,z-1].created
    and Map[x,z+1].created
    and Map[x+1,z].created then Break;
  until ((Map[x,z].Walls<>[]) or (Random>0.75))
  and ((Map[x,z].Walls<>[px,nx,nz,pz]) or (Random>0.9))
  and ((Map[x,z].UpWalls<>[px,nx,nz])
  and (Map[x,z].UpWalls<>[px,nx,pz])
  and (Map[x,z].UpWalls<>[px,nz,pz])
  and (Map[x,z].UpWalls<>[nx,nz,pz])
  or (Random>0.25))
  and ((Map[x,z].DownWalls<>[px,nx,nz])
  and (Map[x,z].DownWalls<>[px,nx,pz])
  and (Map[x,z].DownWalls<>[px,nz,pz])
  and (Map[x,z].DownWalls<>[nx,nz,pz])
  or (Random>0.25))
  and ((Map[x,z].Walls<>[px,nx,nz])
  and (Map[x,z].Walls<>[px,nx,pz])
  and (Map[x,z].Walls<>[px,nz,pz])
  and (Map[x,z].Walls<>[nx,nz,pz])
  or (Random>0.2));




  if not emptyMap then begin
    Map[x,z].Inhalt.Daten:=nil;
    if (x=0) and (z=0) then 
    Map[x,z].Inhalt.Sorte:=1
    else begin

      repeat
        C2:=Random;
        C3:=0;
        Map[x,z].Inhalt.Sorte:=0;
        for C1:=1 to High(Roomtypes) do begin
          C3:=C3+Roomtypes[C1].Roomfrequenzy;
          if C3>C2 then begin
            Map[x,z].Inhalt.Sorte:=C1;
            Break;
          end;
        end;
        if C3<C2 then Map[x,z].Inhalt.Sorte:=0;
        
        found:=false;
        if GetRT(x,z).NotNextToSelf then
        for C1:=X-2 to X+2 do
        for C5:=Z-2 to Z+2 do
        if Map[C1,C5].created then 
        if (Map[x,z].Inhalt.Sorte=Map[C1,C5].Inhalt.Sorte)
        and ((x<>C1) or (z<>C5)) then begin
          found:=true;
          Break;
        end;
      until not found or (Map[x,z].Inhalt.Sorte=0);

      if GetRT(x,z).TurnRandom[Up]
      or GetRT(x,z).TurnRandom[Down] then
      Map[x,z].Inhalt.Daten:=@Richtungen[
      TRichtung(Random(Integer(High(TRichtung))+1))];
    end;
  end;



  if Form3.ClientSocket.Active then begin
    Form3.ClientSocket.Socket.SendText(AnsiString(
    'CreateRaum;'
    +IntToStr(x)+';'
    +IntToStr(z)+';'));
  end else Map[x,z].created:=true;
end;

end.

