Unit NetUnit;
interface

USES CRT,DOS,IPXSPX,GRAPHICS,GENERAL,Keyb,sound;

Var NetData    :TipoDati;
    MyId       :string[3];
    Ids        :array[1..8] of string[3];
    MySlot     :Byte;
    MaxNetPl   :Byte;
    AllOk,Solo :Boolean;
    AllOkcnt   :integer;
    Ok         :Array[1..8] of boolean;
    OkNet      :boolean;


Function GetVal(Num,Posto:integer):integer;
Function GetId(num:integer):string;
Function RecData:boolean;
Procedure NetLost;
Procedure View;
Procedure InitNet;
Procedure PutVal(Num,Posto,Val:integer);
Procedure PutId(num:integer);
Procedure ClearId(num:integer);
Procedure ClearNet;
Procedure KillNet;
Procedure SenData;
Procedure ResetDataNet;
Procedure Synchro(Val:byte);
Procedure Waiting;

implementation

Var
MioPkt: tipxpacket;

procedure InitNet;
var cnt :byte;
Begin
if IpxInit then Begin
 OkNet:=True;
 Myid:='---';
 for cnt:=1 to 8 do PutId(cnt);
end else OkNet:=False;
end;

Procedure KillNet;
begin
if not OkNet then exit;
IpxDone;
end;

Procedure SenData;
var Data:Pointer;

begin
if OkNet then IpxSendData(NetData);
end;

Function RecData:Boolean;
begin
if OkNet then if IpxCheckData(MioPkt) then Begin Recdata:=True; Move(MioPkt.Buffer,NetData,50); end else Recdata:=False;
end;

Function GetId(num:integer):string;
begin
Getid:=copy(NetData,(Num-1)*5+2,3);
end;

Procedure PutId(num:integer);
var cnt: integer;
begin
For cnt:=1 to 3 do NetData[(Num-1)*5+cnt]:=MyId[cnt];
end;

Procedure PutId2(num:integer; id:string);
var cnt: integer;
begin
For cnt:=1 to 3 do NetData[(Num-1)*5+cnt]:=Id[cnt];
end;

Procedure PutVal(Num,Posto,Val:integer);
begin
NetData[(Num-1)*5+Posto]:=char(Val);
end;

Function GetVal(Num,Posto:integer):integer;
begin
GetVal:=Byte(NetData[(Num-1)*5+Posto]);
end;

Procedure ResetDataNet;
Var Slot,cnt:byte;
begin
For Slot:=1 to 8 do
 For Cnt:=1 to 5 do  PutVal(Slot,cnt,0);
 Putval(9,1,0);
end;

Procedure View;
var cnt:byte;
begin
For cnt:=1 to 45 do Text((10+cnt*6) shl SVGA,10 shl SVGA,Netdata[cnt],true);
end;

Procedure ClearId(num:integer);
var cnt:integer;
begin
For cnt:=1 to 3 do NetData[(Num-1)*5+cnt]:='-';
end;

Procedure Synchro(Val:Byte);
Var cnt,cnt2:integer;
begin
GetFbob(580,0,0,319 shl SVGA,199 shl SVGA);
For cnt:=1 to 8 do Ok[cnt]:=false;
AllOk:=false;
AllOkcnt:=0;
repeat
 Nextsound;
 For cnt2:=1 To Random(5)+5 do
 if Recdata then
  if (GetVal(9,1)<>Myslot) then begin
   For cnt:=1 to MaxNetPl do if (GetVal(cnt,4)=Val) then Ok[cnt]:=True;
  end;

 AllOk:=True;
 For cnt:=1 to MaxNetPl do AllOk:=AllOk and Ok[cnt];

 PutVal(Myslot,4,Val);
 PutVal(9,1,Myslot);
 Sendata;

 if AllOk then inc(AllOkcnt);

 Fbob(580,0,0);

 for cnt:=1 to Maxnetpl do begin
  if Ok[cnt]=false then Text(10,10+cnt*(10 shl SVGA),'Waiting Player '+ToStr(cnt,-15)+' '+ids[cnt],true);
 end;

  scrswap;

until (AllOkcnt>100) or Solo or (Keystate(56) and keystate(16));
ResetTime;
end;

Procedure ClearNet;
Var cnt,cnt2:integer;
begin
GetFbob(580,0,0,319 shl SVGA,199 shl SVGA);
For cnt:=1 to 8 do Ok[cnt]:=false;
AllOk:=false;
AllOkcnt:=0;
repeat
 Nextsound;
 For cnt2:=1 To Random(5)+5 do
 if Recdata then For cnt:=1 to 8 do if GetId(cnt)='---' then Ok[cnt]:=True;

 AllOk:=True;
 For cnt:=1 to 8 do AllOk:=AllOk and Ok[cnt];

 for cnt:=1 to 8 do PutId2(cnt,'---');

 Sendata;

 if AllOk then inc(AllOkcnt);

 Fbob(580,0,0);

 Text(10,20 shl SVGA,'Initializing Net Game...',true);

 scrswap;

until (AllOkcnt>100) or (Keystate(56) and keystate(16));
end;

Procedure NetLost;
begin
 repeat until not keystate(16) and not keystate(56);
 GetFbob(580,0,0,319 shl SVGA,199 shl SVGA);
 fbob(580,0,0);
 text(70 shl SVGA,100 shl svga,'Net Lost....press a key',true);
 scrswap;
 repeat nextsound until keypush>0;
end;

Procedure Waiting;
begin
 GetFbob(580,0,0,319 shl SVGA,199 shl SVGA);
 fbob(580,0,0);
 text(70 shl SVGA,100 shl svga,'Waiting others players...',true);
 scrswap;
end;

begin
end.