Uses Dos,Crt,Sound,General,Graphics,Road,PilUnit,Keyb,Perspect,Joys,ConfUnit,CamUnit,CdUnit,CdVars,NetUnit,SelCarUn;

label USCITA,MAINLOOP,STARTPOINT,Reinit;

var
SpecialCam: Byte;
Malus:integer;
AUXX,AUXY,AUXC:integer;
AUXX2,AUXY2,AUXC2:integer;
AngleCur                                       : Integer;
OneFrame,RicReplay,RicPausa,NoPause                     : Boolean;
F                                              : File;
SafeCd,SafeTC1,SafeTC2,ImgY,Imgy1,Ntrack,Tas   : byte;
Auxi,Cnt,Cnt2,CutOff,Ycd,EndCount              : integer;
Auxsec                                    : puntsec;
Esc,EndGame,PreEnd,GradCD                 : boolean;
CurrDirectory                             : string;
Waiter                                    : word;
XCdTas,YCdTas                             : Array[0..10] of Integer;
Mode                                      : byte;
MyTrack                                   : word;
Frame                                     : Longint;

Procedure Recd;
Begin
inc(Waiter);
if (Waiter>50) then Begin
  Waiter:=0;
  DeviceStatus;
  if GradCD and Audio and DiscinDrive then begin
   Audio_Disk_info;
   if not Busy then begin InitCD; PlayTrack(2+Mytrack); end;
   ResetTime;
  end
 end
end;


Procedure PausaCD;
Begin
repeat
 clipgraph(0,0,319 shl SVGA,199 shl SVGA);
 Fbob(580,0,0);
 inc(ycd,10-ycd div 8);
 Bar(60 shl SVGA,(ycd+10) shl SVGA,
    280 shl SVGA,(ycd+55) shl SVGA,0);
 Fbob(581,50 shl SVGA,ycd shl SVGa);
 Fbob(582+Mode,120 shl SVGA,(28+ycd) shl SVGA);
 Fbob(583+Tas,(XCdTas[Tas]+50) shl SVGA,(YCdTas[Tas]+ycd) shl SVGA);
 Audio_Disk_Info;
 DeviceStatus;
 if CurrentTrack<10 then Text(110 shl SVGA,(28+ycd) shl SVGA,Tostr(CurrentTrack,0),True)
                    else Text(103 shl SVGA,(28+ycd) shl SVGA,Tostr(CurrentTrack,0),True);

 if Mode=0 then bar(90 shl SVGA,(39+ycd) shl SVGA,(90+CdVol*2) shl SVGA,(40+ycd) shl SVGA,80) else
 if Mode=1 then bar(90 shl SVGA,(39+ycd) shl SVGA,(90+DgVol*2) shl SVGA,(40+ycd) shl SVGA,80);

 if keystate(75) then dec(Tas);
 if keystate(77) then inc(Tas);
 if keystate(80) then begin
                      if Tas=1 then Tas:=1  else
                      if Tas=2 then Tas:=6  else
                      if Tas=3 then Tas:=7  else
                      if Tas=4 then Tas:=8  else
                      if Tas=5 then Tas:=10 ;
                      end;
 if keystate(72) then begin
                      if Tas=1 then Tas:=1  else
                      if Tas=6 then Tas:=2  else
                      if Tas=7 then Tas:=3  else
                      if Tas=8 then Tas:=4  else
                      if Tas=10then Tas:=5  ;
                      end;
 if Tas>10 then Tas:=1 else if Tas<1 then Tas:=10;
 if keystate(57) or keystate(28) then begin
     PlaySample(1,7,64,100,100);
     if Tas=1  then Mode:=1-Mode else
     if Tas=2  then StopAudio else
     if Tas=3  then if DoorOpen then begin CloseBay;
                                     Repeat DeviceStatus Until Not DoorOpen;
                                     InitCD; CurrentTrack:=StartTrack end
                                else begin StopAudio; Eject end else

     if (Tas=4) then begin KeyClear;
                     if (Mode=0) And (CdVol<12) then inc(CdVol); CDRomVol(CdVol);
                     if (Mode=1) And (DgVol<12) then inc(DgVol); DigiVol (DgVol);
                     end else
     if (Tas=5) then begin KeyClear;
                     if (Mode=0) And (CdVol>0) then dec(CdVol); CDRomVol(CdVol);
                     if (Mode=1) And (DgVol>0) then dec(DgVol); DigiVol (DgVol);
                     end else
     if Tas=6  then Begin CurrentTrack:=StartTrack; StopAudio; PlayTrack(CurrentTrack);End;
     if Tas=7  then Begin dec(CurrentTrack); StopAudio; PlayTrack(CurrentTrack);End;
     if Tas=8  then PlayTrack(CurrentTrack);
     if Tas=9  then Begin inc(CurrentTrack); StopAudio; PlayTrack(CurrentTrack);End;
     if Tas=10 then Begin CurrentTrack:=EndTrack  ; StopAudio; PlayTrack(CurrentTrack);End;
 end;

 repeat Nextsound until not (keystate(28) or keystate(57) or keystate(72) or keystate(75) or keystate(80) or keystate(77));
 scrswap;
until keystate(1);
  repeat Nextsound until not keystate(1);
  Ch[1].StAlert:=0;Ch[1].St:=0;
  Fbob(580,0,0);
  Mytrack:=CurrentTrack;
  GradCd:=Busy;
end;

Procedure Menu;

begin
  GetFbob(580,0,0,319 shl SVGA,199 shl SVGA+SVGA shl 1);
 if ReplayStatus=1 then SaveFFWDTimes;
 Auxi:=2;
 StopMusic;
 if Not FFWD then begin
  repeat
  ClipGraph(0,0,319 shl SVGA,199 shl SVGA+SVGA shl 1);
  Fbob(580,0,0);
  inc(AngleCur,5); if AngleCur>350 then AngleCur:=0;
  If keystate(Up1) or keystate(Up2) or Keystate(72) then dec(Auxi);
  If keystate(Dw1) or keystate(Dw2) or Keystate(80) then inc(Auxi);
  repeat NextSound until not (keystate(Up1) or keystate(Up2) or Keystate(72) and
                              keystate(Dw1) or keystate(Dw2) or Keystate(80));

  if Auxi>4 then Auxi:=1 else if Auxi<1 then Auxi:=4;
  if ReplayAval then
  Text(110 shl SVGA, 55 shl SVGA,'Replay Action',true)
  else
  Text(110 shl SVGA, 55 shl SVGA,'Rep.Not Aval.',true);
  Text(110 shl SVGA, 75 shl SVGA,'Continue Game',true);
  Text(110 shl SVGA, 95 shl SVGA,'CD Rom Player',true);
  Text(110 shl SVGA,115 shl SVGA,'  Exit Game'  ,true);
  PutObj(Myslot,0,0,800,AngleCur,0,0,80 shl SVGA, (45+Auxi*20) shl SVGA);
  if NumPla=2 then PutObj(2,0,0,800,AngleCur,0,0,240 shl SVGA, (45+Auxi*20) shl SVGA)
              else PutObj(Myslot,0,0,800,AngleCur,0,0,240 shl SVGA, (45+Auxi*20) shl SVGA);
  repeat NextSound until Time>50;
  ResetTime;
  scrswap;
  RicReplay:=Auxi=1;
  Esc      :=Auxi=4;
  RicPausa :=Auxi=3;

  if RicPausa and (Keystate(Ba1) or Keystate(Ba2) or keystate(28) or keystate(57))
   then begin
   repeat Nextsound until not (Keystate(Ba1) or Keystate(Ba2) or keystate(28) or keystate(57));
   Ycd:=-50;Tas:=1;
   PausaCd;
  end;
  until (Keystate(Ba1) or Keystate(Ba2) or keystate(28) or keystate(57)) and not (RicReplay and not ReplayAval);

  FFWD:=(Auxi=2) and (ReplayStatus=0);

  if FFWD then begin
   Fbob(580,0,0);
   Text(105 shl SVGA, MsY2Clip[Myslot]-50 shl SVGA,'Please Wait...',true);
   if Numpla=2 then Text(105 shl SVGA, MsY2Clip[2]-50 shl SVGA,'Please Wait...',true);
   scrswap;
  end;

  Fbob(580,0,0);
 end;

  if FFWD and EndReplay then begin
  Fbob(580,0,0);
  LoadFFWDTimes;
  Text(105 shl SVGA, MsY2Clip[Myslot]-50 shl SVGA,'Press Pedal...',true);
  if Numpla=2 then Text(105 shl SVGA, MsY2Clip[2]-50 shl SVGA,'Press Pedal...',true);
  scrswap;
  repeat
    Nextsound;
                    ControlCar(1,Car[1].PilotType);
   if Numpla=2 then ControlCar(2,Car[2].PilotType);
  until (((Car[1].pilottype=4) or (Ytrottle[1]<-10)) and ((Car[2].pilottype=4) or (Ytrottle[2]<-10) or (NumPla=1)));
  CutOff:=0;
 end;

 if EndReplay then begin
  FFWD:=False;
  ReplayStatus:=1;
  CloseReplay;
  SaveInitReplay(1);
 end;

 StartMusic;
 ResetTime;
end;

(********************************* INIZIO MAIN ***************************)
begin
Randomize;
ReplayStatus:=1;
RicReplay:=false;

REINIT:

initCD;

XcdTas[ 1]:= 89;YcdTas[ 1]:=23;
XcdTas[ 2]:=110;YcdTas[ 2]:=23;
XcdTas[ 3]:=131;YcdTas[ 3]:=23;
XcdTas[ 4]:=153;YcdTas[ 4]:=23;
XcdTas[ 5]:=202;YcdTas[ 5]:=23;
XcdTas[ 6]:=110;YcdTas[ 6]:=35;
XcdTas[ 7]:=131;YcdTas[ 7]:=35;
XcdTas[ 8]:=153;YcdTas[ 8]:=35;
XcdTas[ 9]:=175;YcdTas[ 9]:=35;
XcdTas[10]:=197;YcdTas[10]:=35;
Tas:=1;

(*Randomize;*)
(******************* INIT GRAPH **************************)
GetDir(0,CurrDirectory);
chdir(CurrDirectory);

Initkeyb;
Initgraph(3000);

InitMisc;
InitPros;

MinX1:=0;
MaxX1:=0;
MinY1:=0;
MaxY1:=0;
CarPl1:=1;
CarPl2:=2;
Difficulty:=1;
Detail:=1;
TypeCon1:=1;
TypeCon2:=0;
NumPla:=1;
Baseport:=554;
Irq:=5;
dma:=1;
Up1:=30;
Dw1:=44;
Lf1:=51;
Rg1:=52;
Ba1:=57;
Bb1:=56;
Up2:=76;
Dw2:=80;
Lf2:=79;
Rg2:=81;
Ba2:=54;
Bb2:=29;

XPosTol1:=MaxX1 shl 1;
XNegTol1:=MinX1 shl 1;
YPosTol1:=MaxY1 shl 1;
YNegTol1:=MinY1 shl 1;


MAINLOOP:

LoadSlot;
LoadConfig;
LoadStatus;

if MaxnetPl<1 then maxnetPl:=1;
Solo:=MaxNetPl=1;
if solo then Myslot:=1;
MaxCars:=MaxNetPl;


if Gamemode=3 then begin
MaxCars:=MaxNetPl;
For cnt:=2 to 8 do Car[cnt].Automatic:=False;
Car[Myslot].Automatic:=Car[1].Automatic; InitNet;
end;

RDifficulty:=Difficulty*5+Cround+1;

if Gamemode=1 then Rdifficulty:=Difficulty*5+4;

if (Cround=5) then Malus:=4;
if (Cround=3) then Malus:=3;

Rdifficulty:=Rdifficulty-Malus;

DDifficulty:=(RDifficulty)*10;

SafeTC1:=TypeCon1;
SafeTC2:=TypeCon2;

STARTPOINT:

Initfont;
Loadfont('Pics\Font.pcx');

LoadLogo(MaxBobs-2);
Loadpic('Pics\Wait.pcx',Pseg,Pofs);
Loadpal('Pics\Wait.pcx', 20,255);
PlaceLogo;

If ReplayStatus=0 then Text(65 shl SVGA,90 shl SVGA,'Loading Replay Action...',True);

scrswap;

initsound(1024);
SoundOn:=False;

Groad.curs:=nil;
Groad.primo:=nil;
Groad.maxsec:=0;

if Cround=0 then Cround:=1;
Ntrack:=CRound;
Loadroad(Groad,'Track'+tostr(Ntrack,0));

LoadRec(NTrack);
RecTimeS:=TimeToStr(RecTime);

Groad.primo^.last:=Groad.ultimo;
Groad.ultimo^.next:=Groad.primo;
Groad.curs:=Groad.primo;

CarBandit:=Cround+3; BanCar:=2;

if Allcars then MaxCars:=8 else MaxCars:=2;

If GameMode=0 Then PlaceCarsRace  else
If GameMode=1 Then PlaceCarsRace  else
If GameMode=2 Then PlaceCarsChase else
If GameMode=3 Then begin MaxCars:=MaxNetPl;PlaceCarsNet;end;

LoadRace;

(*For cnt:=1 to 8 do car[cnt].chassis:=cnt;
For cnt:=1 to 8 do writeln(Car[cnt].chassis);
setrgb(0,255,255,255);
delay(1000); (** WORKS***)

 LoadCar(1,100,Carname[Car[1].Chassis]);
 LoadCar(2,150,Carname[Car[2].Chassis]);
 LoadCar(3,200,Carname[Car[3].Chassis]);
 LoadCar(4,250,Carname[Car[4].Chassis]);
 LoadCar(5,300,Carname[Car[5].Chassis]);
 LoadCar(6,350,Carname[Car[6].Chassis]);
 LoadCar(7,400,Carname[Car[7].Chassis]);
 LoadCar(8,450,Carname[Car[8].Chassis]);

 LoadObj(30,575,'3Data\Signal.obj');
 LoadWir(19, 70,'3Data\Smoke.obj' );
 LoadWir(20,  1,'3Data\Shadow.obj');
 LoadWir(29,571,'3Data\Smoke.obj' );

  Lbob('Pics\Acces.pcx'   ,'Data\Acces.dat'    , 80);
  Lbob('Pics\Acces.pcx'   ,'Data\Position.dat' ,550);
  GetfBob(570,0,0,256 shl SVGA,5 shl SVGA+SVGA);
 LFbob('Pics\Acces2.pcx'  ,'Data\Smoke2.dat'   ,571);
 LFbob('Pics\Acces.pcx'   ,'Data\Over.dat'     , 88);
 LFbob('Pics\CdPlayer.pcx','Data\Cdplayer.dat' ,581);
 if SVGA=1 then InitSpdBuf(65);

Cnt:=GRoad.MaxSec div 8;
AuxSec:=Groad.curs;
clr(0);

 loadsample(2 ,'Samples\EngineT.Raw');
 loadsample(3 ,'Samples\Engine.Raw' );
 loadsample(4 ,'Samples\Sgom.Raw'   );
 loadsample(5 ,'Samples\Crash3.Raw' );
 loadsample(6 ,'Samples\Crash4.Raw' );
 loadsample(7 ,'Samples\CDClick.Raw');
 loadsample(8 ,'Samples\Sirena.Raw');

 Secdistance:=150;
 NumCamPos:=1;

fade(4); clr(0); ScrSwap;

Loadpal('Pics\Track2.pcx',20,255);

 CreateTable(1,235,235,235);
 CreateTable(2,5,5,5);

 JoyOn1:=CheckJoy1;
 if (MinX1=MaxX1) or (MinY1=MaxY1) then JoyOn1:=false;

 if (Typecon1=1) and JoyOn1 then Typecon1:=3 else Typecon1:=1;
 if (Typecon2=1) and JoyOn1 then Typecon2:=3 else Typecon2:=2;

if Detail=1 then begin
 if NumPla=1 then begin
  MsX1clip[MySlot]:=0 Shl SVGA; MsY1clip[MySlot]:=0 Shl SVGA; MsX2clip[MySlot]:=319 Shl SVGA; MsY2clip[MySlot]:=166 Shl SVGA;
 end else
 if NumPla=2 then begin
  MsX1clip[MySlot]:=0 Shl SVGA; MsY1clip[MySlot]:=105 Shl SVGA; MsX2clip[MySlot]:=277 Shl SVGA; MsY2clip[MySlot]:=166 Shl SVGA;
  MsX1clip[2]:=  0 Shl SVGA;  MsY1clip[2]:=  5 Shl SVGA;  MsX2clip[2]:=277 Shl SVGA;  MsY2clip[2]:= 66 Shl SVGA;
 end
end else begin
 if NumPla=1 then begin
  MsX1clip[MySlot]:= 10 Shl SVGA; MsY1clip[MySlot]:= 65 Shl SVGA;
  MsX2clip[MySlot]:=309 Shl SVGA; MsY2clip[MySlot]:=156 Shl SVGA;
 end else
 if NumPla=2 then begin
  MsX1clip[MySlot]:= 20 Shl SVGA; MsY1clip[MySlot]:=105 Shl SVGA;
  MsX2clip[MySlot]:=277 Shl SVGA; MsY2clip[MySlot]:=166 Shl SVGA;
  MsX1clip[2]:= 20 Shl SVGA;  MsY1clip[2]:=  5 Shl SVGA;  MsX2clip[2]:=277 Shl SVGA;  MsY2clip[2]:= 66 Shl SVGA;
 end
end;

AuxY12Clip:=MsY2Clip[MySlot];
AuxY22Clip:=MsY2Clip[2];

Clipgraph(MsX1Clip[MySlot],0,MsX2Clip[MySlot],199 Shl SVGA);

AuxSec:=Groad.curs;
for auxi:=0 to 5 do Auxsec:=Auxsec^.next;
PlaceCam(Myslot,Auxsec,0,0,40,0);
PlaceCam(2,Auxsec,0,0,40,0);

for cnt:=1 to 4 do begin Ch[cnt].StAlert:=0; Ch[cnt].st:=0; end;
for cnt:=1 to 8 do Smoke[cnt].img:=0;

MyTrack:=(NTrack mod 4)+2+1;
PlayTrack(MyTrack);
CdRomVol(CDvol);

Car[MySlot].PilotType:=TypeCon1;

if NumPla=2 then begin
 Loadpic('Pics\BackGam2.pcx',Pseg,Pofs);
 bob(149,0,MsY2Clip[MySlot]+1);
 bob(199,0,MsY2Clip[2]+1);
 box(0,0,319 shl SVGA,199 shl SVGA,0);
 Scrswap;
 Loadpic('Pics\BackGam2.pcx',Pseg,Pofs);
 bob(149,0,MsY2Clip[MySlot]+1);
 bob(199,0,MsY2Clip[2]+1);
 box(0,0,319 shl SVGA,199 shl SVGA,0);
 Car[     2].PilotType:=TypeCon2;
end else begin
 Loadpic('Pics\BackGam1.pcx',Pseg,Pofs);
 bob(149+(Myslot-1)*50,0,MsY2Clip[MySlot]+1);
 box(0,0,319 shl SVGA,199 shl SVGA,0);
 ScrSwap;
 Loadpic('Pics\BackGam1.pcx',Pseg,Pofs);
 bob(149+(Myslot-1)*50,0,MsY2Clip[MySlot]+1);
 box(0,0,319 shl SVGA,199 shl SVGA,0);
end;


MasterVol(14);

AngleCur:=0;
SoundOn:=SBOn;
Mode:=0;
Start:=False;
StStart:=0;
resettime;
GlobalTime:=300000;
EndGame:=False;
PreEnd:=False;
FFWD:=False;
CutOff:=0;
Waiter:=0;
Drawing:=True;
Oscil:=True;
GradCd:=True;
Frame:=0;SpecialCam:=0;
Timurto:=0;
Safe:=0;
Safe2:=0;
Solo:=MaxNetPl=1;
OneFrame:=False;
(******* WORKS **************)

if gamemode=3 then synchro(150);
scrswap;

if ReplayStatus=0 then LoadInitReplay;
if ReplayStatus=1 then SaveInitReplay(0);

(************************ MAIN ****************************)
repeat
if Timurto<22 then inc(Timurto);

NextSound; (******************** IF SOUND ****************************)

 if gamemode<>3 then begin
 if NumPla=2 then TAddHan:=Min(AddHan[MySlot],AddHan[2])
             else TAddHan:=AddHan[MySlot];
 end else Begin
 Taddhan:=8;
 for cnt:=1 to MaxNetPl do TaddHan:=Min(Taddhan,position(cnt));
 end;

 if keystate(50) then begin inc(Mirror);TimeMirr:=0;if Mirror>5 then Mirror:=0;
                  repeat until not keystate(50);
                 end;

if GameMode=3 then begin
 NoPause:=False;
 inc(Frame); if frame>200 then frame:=0;

 ControlCar(MySlot,Car[MySlot].PilotType);
(* For cnt:=1+MaxNetPl to 8 do ControlCar(Cnt,Car[cnt].PilotType);*)

 AllOkcnt:=0;

 For cnt:=1 to 8 do Ok[cnt]:=false;
 AllOk:=false;
 repeat
  Nextsound;
  For cnt2:=1 to 10*MaxNetPl do
  if Recdata then
   if (GetVal(9,1)<>Myslot) then begin
    For cnt:=1 to Maxnetpl do if (GetVal(cnt,4)=frame) and not Ok[cnt] then begin
     Ok[cnt]:=True;
     Xtrottle[cnt]:=Integer (GetVal(cnt,1))-100;
     Ytrottle[cnt]:=Integer (GetVal(cnt,2))-100;
     Cgear   [cnt]:=Shortint(GetVal(cnt,3))-100;
    end;
   end;

   AllOk:=True;
   for cnt:=1 to Maxnetpl do AllOk:=AllOk and Ok[cnt];
   AllOk:=AllOk or Solo;

    PutVal(Myslot,1,Byte(Xtrottle[Myslot]+100));
    PutVal(Myslot,2,Byte(YTrottle[Myslot]+100));
    PutVal(Myslot,3,Byte(Cgear   [Myslot]+100));
    PutVal(Myslot,4,frame);
    PutVal(9,1,Myslot);

   Sendata;

   if AllOk then inc(AllOkcnt) else begin
    if (time>500) and OneFrame then begin
     if Not Nopause then begin Nopause:=True; menu; end else waiting;
    end;
   end;

   Esc:=(Keystate(56) and keystate(16)) or Esc;

  until (AllOkcnt>20) or Esc;
  repeat Nextsound until time>20;
             (*** ELSE IF NOT NET ***********)
 end else begin
  for cnt:=1 to 8 do ControlCar (cnt,Car[Cnt].PilotType);
  if ReplayStatus=0 then LoadRepFrame;
  if ReplayStatus=1 then SaveRepFrame;
 end;

 if Start then begin
     For cnt:=1 to MaxCars do begin
      PilotCar(cnt,Xtrottle[cnt],Ytrottle[cnt],Cgear[cnt]);
     end;
  end;

 NextSound; (******************** IF SOUND ****************************)

 (****************************** SMOKE HANDLING *************************)
 For auxi:=1 to 8 do
 if Smoke[auxi].img>0 then  begin
  inc(Smoke[auxi].img);
  if Smoke[auxi].img>15 then begin
   Smoke[auxi].img:=0;
   dec(Smoke[auxi].Sec^.NumSmk,Smoke[auxi].id);
  end
 end;

(****************************** SPECIAL HANDLING ***********************)
 for cnt:=21 to 28 do if SpecialObj[cnt].id>0 then begin
  ControlSpc(cnt);
  PilotSpc(cnt);
 end;
 (***********************************************************************)

CheckCollision(2,1);

 if AllCars then
  for cnt2:=2 to MaxCars-1 do
  for cnt:=1 to cnt2 do CheckCollision(cnt2+1,cnt);
(*
  for cnt:=1 to 3 do CheckCollision(4,cnt);
  for cnt:=1 to 4 do CheckCollision(5,cnt);
  for cnt:=1 to 5 do CheckCollision(6,cnt);
  for cnt:=1 to 6 do CheckCollision(7,cnt);
  for cnt:=1 to 7 do CheckCollision(8,cnt);*)

 NextSound; (******************** IF SOUND ****************************)

 if (Keystate(1) or EndReplay) or (FFWD and EndReplay) then menu;


if FFWD then Begin
  drawing:=ReplayIndex>ReplaySize-800;
  SoundOn:=Drawing;
 end else SoundOn:=true;

if NumPla=2 then begin

if Drawing then begin
 if keystate(63) and (Cam[2].Mode<>0) then begin
  ClipGraph(MsX1clip[2],MsY1clip[2],MsX2Clip[2],199 shl SVGA+SVGA*3);
  if Svga=1 then line(MsX1Clip[2],MsY2clip[2]-1,MsX2clip[2],MsY2clip[2]-1,0);
  bob(199,0,AuxY22Clip+1); Scrswap;
  if Svga=1 then line(MsX1Clip[2],MsY2Clip[2]-1,MsX2clip[2],MsY2clip[2]-1,0);
  bob(199,0,AuxY22Clip+1);
        Car[2].Infos:=True ;Car[2].Dash:=True ; Cam[2].Mode:=0; MsY2Clip[2]:=AuxY22Clip+SVGA end
  else if keystate(64) and (Cam[2].Mode<>1) then
  begin Car[2].Infos:=True ;Car[2].Dash:=False; Cam[2].Mode:=1; MsY2Clip[2]:=AuxY22Clip+32 shl SVGA+SVGA end
  else if keystate(65) and (Cam[2].Mode<>2) then
  begin Car[2].Infos:=True ;Car[2].Dash:=False; Cam[2].Mode:=2; MsY2Clip[2]:=AuxY22Clip+32 shl SVGA+SVGA end
  else if keystate(66) and (Cam[2].Mode<>3) then
  begin Car[2].Infos:=True ;Car[2].Dash:=False; Cam[2].Mode:=3; MsY2Clip[2]:=AuxY22Clip+32 shl SVGA+SVGA end
  else if keystate(87) and (Cam[2].Mode<>4) then
  begin Car[2].Infos:=True ;Car[2].Dash:=False; Cam[2].Mode:=4; MsY2Clip[2]:=AuxY22Clip+32 shl SVGA+SVGA end
  else if keystate(88) and (Cam[2].Mode<>6) then
  begin Car[2].Infos:=True ;Car[2].Dash:=False; Cam[2].Mode:=6; MsY2Clip[2]:=AuxY22Clip+32 shl SVGA+SVGA end
  else if ((SpecialCam=1)or((gamemode<>2) and Car[2].fin)) and (Cam[2].Mode<>6) then
  Begin Car[2].Infos:=True ;Car[2].Dash:=False; Cam[2].Mode:=6; MsY2Clip[2]:=AuxY22Clip+32 shl SVGA+SVGA end
  else if (SpecialCam=2) and (Cam[2].Mode<>4) then
  Begin Car[2].Infos:=True;Car[2].Dash:=False; Cam[2].Mode:=4; MsY2Clip[2]:=AuxY22Clip+32 shl SVGA+SVGA end;
   if Cam[2].Mode>0 then begin
   Clipgraph(MsX1Clip[2],MsY1Clip[2],MsX2Clip[2],MsY2Clip[2]+SVGA*3);
   if (SpecialCam=2) then begin
    ControlCam(2,Cam[2].Mode,BanCar);
    PilotCam(2);
   end else begin
    PilotCam(2);
    ControlCam(2,Cam[2].Mode,2);
   end;
   NextSound; (******************** IF SOUND ****************************)
   TraceRoadCam(2,160,Cam[2].Orizont+MsY2Clip[2]-66 shl SVGA,5)
  end else begin
   Clipgraph(MsX1Clip[2],MsY1Clip[2],MsX2Clip[2],MsY2Clip[2]);
   NextSound; (******************** IF SOUND ****************************)
   TraceRoadCar(2,MsY2Clip[2]-66 shl SVGA+SVGA*20);
   if (Mirror>0) and Start then begin
    if (Detail=0) or (Numpla=2) then begin
     ClipGraph(190 shl SVGA,MsY2clip[2]-30 shl SVGA,
               270 shl SVGA,MsY2clip[2]-10 shl SVGA);
     PilotCam(2);
     ControlCam(2,Mirror,2);
     If Mirror=5 then
     TraceRoadMir(2,230,MsY2Clip[2]-40 shl SVGA+20*SVGA,3)
         else
     TraceRoadCam(2,230,MsY2Clip[2]-46 shl SVGA+20*SVGA,3);
     Box(190 shl SVGA,MsY2clip[2]-30 shl SVGA,
         270 shl SVGA,MsY2clip[2]-10 shl SVGA,0);
     if TimeMirr<20 then begin
      inc(TimeMirr);
      Text(191 shl SVGA,Msy2Clip[2]-31 shl SVGA,MirrMess[Mirror],True);
     end
    end
   end
  end
 end; (*END DRAWING *)
 TraceDash (2,MsY2Clip[2]-61 shl SVGA);
end;

if Drawing then begin

 if keystate(59) and (Cam[MySlot].Mode<>0) then Begin
  ClipGraph(MsX1clip[MySlot],MsY1clip[MySlot],MsX2Clip[MySlot],199 shl SVGA+SVGA*3);
  if Svga=1 then line(MsX1Clip[MySlot],MsY1clip[MySlot]-1,MsX2clip[MySlot],MsY1clip[MySlot]-1,0);
  bob(149+50*(Myslot-1),0,AuxY12Clip+1); Scrswap;
  if Svga=1 then line(MsX1Clip[MySlot],MsY1clip[MySlot]-1,MsX2clip[MySlot],MsY1clip[MySlot]-1,0);
  bob(149+50*(Myslot-1),0,AuxY12Clip+1);
        Car[MySlot].Infos:=True ;Car[MySlot].Dash:=True ;Cam[MySlot].Mode:=0;MsY2Clip[MySlot]:=AuxY12Clip+SVGA end
 else if keystate(60) and (Cam[MySlot].Mode<>1) then
 Begin Car[MySlot].Infos:=True ;Car[MySlot].Dash:=False;Cam[MySlot].Mode:=1;MsY2Clip[MySlot]:=AuxY12Clip+32 shl SVGA+SVGA end
 else if keystate(61) and (Cam[MySlot].Mode<>2) then
 Begin Car[MySlot].Infos:=True ;Car[MySlot].Dash:=False;Cam[MySlot].Mode:=2;MsY2Clip[MySlot]:=AuxY12Clip+32 shl SVGA+SVGA end
 else if keystate(62) and (Cam[MySlot].Mode<>3) then
 Begin Car[MySlot].Infos:=True ;Car[MySlot].Dash:=False;Cam[MySlot].Mode:=3;MsY2Clip[MySlot]:=AuxY12Clip+32 shl SVGA+SVGA end
 else if keystate(67) and (Cam[MySlot].Mode<>4) then
 Begin Car[MySlot].Infos:=True ;Car[MySlot].Dash:=False;Cam[MySlot].Mode:=4;MsY2Clip[MySlot]:=AuxY12Clip+32 shl SVGA+SVGA end
 else if keystate(68) and (Cam[MySlot].Mode<>6) then
 Begin Car[MySlot].Infos:=True ;Car[MySlot].Dash:=False;Cam[MySlot].Mode:=6;MsY2Clip[MySlot]:=AuxY12Clip+32 shl SVGA+SVGA end
 else if ((SpecialCam=1) or ((gamemode<>2) and Car[Myslot].fin)) and (Cam[MySlot].Mode<>6) then
 Begin Car[MySlot].Infos:=True ;Car[MySlot].Dash:=False;Cam[MySlot].Mode:=6;MsY2Clip[MySlot]:=AuxY12Clip+32 shl SVGA+SVGA end
 else if (SpecialCam=2) and (Cam[2].Mode<>4) then
 Begin Car[MySlot].Infos:=True ;Car[MySlot].Dash:=False;Cam[Myslot].Mode:=4;MsY2Clip[MySlot]:=AuxY12Clip+32 shl SVGA+SVGA end;

 if Cam[MySlot].Mode>0 then begin
  Clipgraph(MsX1Clip[MySlot],MsY1Clip[MySlot],MsX2Clip[MySlot],MsY2Clip[MySlot]+SVGA*3);
  if SpecialCam=2 then begin
   ControlCam(Myslot,Cam[Myslot].Mode,Bancar);
   PilotCam(Myslot);
  end else begin
   PilotCam(Myslot);
   ControlCam(Myslot,Cam[Myslot].Mode,Myslot);
  end;
  NextSound; (******************** IF SOUND ****************************)
  TraceRoadCam(MySlot,160,Cam[MySlot].Orizont+MsY2Clip[MySlot]-66 shl SVGA,5)
 end else begin
  Clipgraph(MsX1Clip[MySlot],MsY1Clip[MySlot],MsX2Clip[MySlot],MsY2Clip[MySlot]);
  NextSound; (******************** IF SOUND ****************************)
  TraceRoadCar(MySlot,MsY2Clip[MySlot]-66 shl SVGA+SVGA*20);
  if (Mirror>0) and Start then begin
   if (Detail=0) or (Numpla=2) then begin
    ClipGraph(190 shl SVGA,MsY2clip[MySlot]-30 shl SVGA,
              270 shl SVGA,MsY2clip[MySlot]-10 shl SVGA);
    PilotCam(MySlot);
    ControlCam(MySlot,Mirror,MySlot);
    If Mirror=5 then
    TraceRoadMir(MySlot,230,MsY2Clip[MySlot]-40 shl SVGA+20*SVGA,3)
                else
    TraceRoadCam(MySlot,230,MsY2Clip[MySlot]-46 shl SVGA+20*SVGA,3);
    Box(190 shl SVGA,MsY2clip[MySlot]-30 shl SVGA,
        270 shl SVGA,MsY2clip[MySlot]-10 shl SVGA,0);
    if TimeMirr<20 then begin
     inc(TimeMirr);
     Text(191 shl SVGA,Msy2Clip[MySlot]-31 shl SVGA,MirrMess[Mirror],True);
    end
   end else begin
    ClipGraph(100 shl SVGA,MsY1clip[MySlot]+(30+20) shl SVGA,
              220 shl SVGA,MsY1clip[MySlot]+(30+50) shl SVGA);
              PilotCam(MySlot);
    ControlCam(MySlot,Mirror,MySlot);
    if Mirror=5 then
    TraceRoadMir(MySlot,160,MsY1Clip[MySlot]+45 shl SVGA,4)
                else
    TraceRoadCam(MySlot,160,MsY1Clip[MySlot]+30 shl SVGA+30*SVGA,4);
    Box(100 shl SVGA,MsY1clip[MySlot]+(30+20) shl SVGA,
        220 shl SVGA,MsY1clip[MySlot]+(30+50) shl SVGA,0);
    if TimeMirr<20 then begin
     inc(TimeMirr);
     Text(101 shl SVGA,Msy1Clip[MySlot]+(30+19) shl SVGA,MirrMess[Mirror],True);
    end
   end
  end
 end
end;(*END DRAWING *)

TraceDash (MySlot,MsY2Clip[MySlot]-61 shl SVGA);

 if Not Start then  begin
  if not(GameMode=2) then begin
   if StStart=0 then begin
    inc(StStart);
    if SbOn then PlaySampleHd('Samples\Starter.Raw',False);
   end;

   NextSound; (******************** IF SOUND ****************************)

   if ImgY<85 then Imgy1:=Imgy else imgy1:=85;

   if Drawing then begin
    Clipgraph(MsX1Clip[Myslot],0,MsX2Clip[Myslot],199 shl SVGA);
    if NumPla=1 then PutOBj(30,0,20,300-ImgY1,190-imgy1,0,0,160 shl SVGA, MsY2Clip[Myslot]-65 shl SVGA)
                else begin
     PutOBj(30,0,20,300-imgY1,190-Imgy1,0,0,160 shl SVGA, MsY2Clip[Myslot]-65 shl SVGA);
     PutOBj(30,0,20,300-imgY1,190-Imgy1,0,0,160 shl SVGA, MsY2Clip[2     ]-65 shl SVGA);
    end;
   end;

   inc(ImgY,10);
   if ImgY>185  then Begin ImgY:=0;  inc(StStart);
                        if StStart=2 then chgface(30,3,2);
                        if StStart=3 then chgface(30,4,2);
                        if StStart=4 then chgface(30,5,2);
                     end;

   if StStart>4 then Begin KillMusic; Start:=True; end;
  end else begin
   if Drawing then begin
    Clipgraph(MsX1Clip[1],0,MsX2Clip[1],199 shl SVGA);
                     Text(85 shl SVGA,129 shl SVGA,'Press Pedal when Ready',True);
    if NumPla=2 then Text(85 shl SVGA, 29 shl SVGA,'Press Pedal when Ready',False);
   end;
   Start:=(Ytrottle[MySlot]<-20) and ((Ytrottle[2]<-20) or (NumPla=1));
  end
 end;

 Clipgraph(0,0,319 shl SVGA,199 shl SVGA);

 if numpla=2 then  Line(MsX1Clip[2],MsY1Clip[2]-1,MsX2Clip[2],MsY1Clip[2]-1,0);
                   Line(MsX1Clip[MySlot],MsY1Clip[MySlot]-1,MsX2Clip[MySlot],MsY1Clip[MySlot]-1,0);

 NextSound; (******************** IF SOUND ****************************)

(**************************** CHECK END GAME **********************)
if GameMode<2 then begin
                   if (NumPla=1) and Car[MySlot].Fin and (Car[MySlot].Speed<5) then EndGame:=True else
                   if (NumPla=2) and Car[MySlot].Fin and (Car[MySlot].Speed<5)
                                 and Car[2].Fin and (Car[2].Speed<5) then EndGame:=True
                   end else
if GameMode=3 then begin
                   EndGame:=True;
                   For cnt:=1 to MaxNetPl do EndGame:=EndGame and (Car[cnt].fin) and (Car[cnt].Speed<5)
                   end else

if GameMode=2 then begin
                    if (Car[BanCar].Energy=0) then begin
                    Safe:=1;SpecialCam:=1;
                    For cnt:=1 to Numpla do Car[cnt].PilotType:=4;
                    TimeMess:=0; ExtipoMess[1]:=7;Extipomess[2]:=7;
                    Car[BanCar].Fin:=True;
                    if (Position2(CopCar) or (Position2(2) and (NumPla=2))) then PreEnd:=True;
                    end;
                    if (BanDis[1]>60) and (BanDis[2]>60) then begin
                    SpecialCam:=2;
                    TimeMess:=0; ExtipoMess[1]:=5;ExtipoMess[2]:=5;
                    PreEnd:=True;
                    Safe:=0;
                    end;
                    if Lap[BanCar]>5 then begin
                    SpecialCam:=2;
                    TimeMess:=0; ExtipoMess[1]:=6;ExtipoMess[2]:=6;
                    PreEnd:=True;
                    Safe:=0;
                    end;
                    if PreEnd then begin
                        Car[1].Fin:=True;
                        Car[2].Fin:=True;
                        if Car[BanCar].Fin and (Car[BanCar].Speed<5) then begin
                         TimeMess:=0;
                         if Car[BanCar].Energy=0 then begin
                          ExtipoMess[1]:=8;
                          ExtipoMess[2]:=8;
                         end;
                        end;
                        if (NumPla=1) and (Car[1].Speed<5) then EndGame:=True else
                        if (NumPla=2) and (Car[1].Speed<5)
                                      and (Car[2].Speed<5) then EndGame:=True
                    end
                   end;
(**************************** CHECK END GAME **********************)
 if Drawing then begin
  if GameMode=3 then Text(10 shl SVGA,30 shl SVGA,'Player '+Tostr(MySlot,-15),True);
  if ReplayStatus=0 then begin
   if NumPla=1 then begin
    Text(270 shl SVGA, MsY2Clip[Myslot]-18 shl SVGA,'Replay',true);
   end else begin
    Text(229 shl SVGA, MsY1Clip[Myslot],'Replay',true);
    Text(229 shl SVGA, MsY1Clip[2],'Replay',true);
   end;
  end;

(*  For cnt:=1 to 8 do Text(10,50+cnt*10,Tostr(210-Han[cnt]-TAddHan shl 2,0),true);
    For cnt:=1 to 8 do Text(40,50+cnt*10,Tostr(Car[cnt].speed,0),true);

(*  For cnt:=1 to 8 do Text(50,50+cnt*10,Tostr(Ytrottle[cnt],0),true);*)
(*  For cnt:=1 to 8 do Text(80,50+cnt*10,Tostr(CGear   [cnt],0),true);*)
(*  For cnt:=1 to 8 do Text(120,50+cnt*10,Tostr(Car[cnt].Gear,0),true);*)
    (*
  text(190,80, tostr(han[3],0),true);
  text(190,100,Tostr(xtrottle[3],0),true);
  text(190,120,Tostr(ytrottle[3],0),true);

(*  text(130,80, tostr(car[3].sec^.numsec,0),true);
(*text(130,100,Tostr(car[3].intZ,0),true);
  text(130,120,Tostr(Cgear [3],0),true);*)


(*  text(160,110,Tostr(Car[1].team,0),true);
(*  text(160,120,Tostr(Mon2,0),true);
  text(160,130,Tostr(Mon3,0),true);
  text(160,140,Tostr(Mon4,0),true);*)

  if SVGA=1 then line(0,400,319 shl SVGA,400,0);
  ScrSwap; (*********** GLOBAL DISPLAY *************)
  OneFrame:=True;
 end else
  if FFWD and (ReplayIndex<ReplaySize-800) then
   if ReplayIndex div 1000*1000=ReplayIndex then begin
    Fbob(580,0,0);
    Text(120 shl SVGA, MsY2Clip[Myslot]-40 shl SVGA,
     Tostr(Replayindex div 100,-15)+'/'+Tostr(FileSize(Replay) div 100,-15),true);
    Text(105 shl SVGA, MsY2Clip[Myslot]-50 shl SVGA,'Please Wait...',true);
    ScrSwap;
   end;

 NextSound; (******************** IF SOUND ************************)

 auxi:=time;
 if Drawing then begin
  if auxi<50 then CutOff:=0;
  repeat Nextsound; until time>50;
  Flash:=Not Flash;
 end;
 inc(GlobalTime,time);
 resettime;

 inc(CutOff,Auxi-50);
 Drawing:=CutOff<0;

if keypush>0 then empty;

ReCd;

if EndGame then inc(EndCount);
until RicReplay or Esc or (EndCount>100);

if Esc and (gamemode=3) then NetLost;

KillMusic;
for cnt:=1 to 1000 do begin delay(1); nextsound; end;

fade(4);
SafeCD:=CdVol;
FadeAudio(100);
clr(0);
scrswap;

uscita:

if GameMode<>2 then begin
 if AllCars then
 for Cnt:=1 to 8 do begin
  if Not Car[Cnt].Fin then Car[cnt].FinPos:=Position(cnt);
  if Car[Cnt].FinPos=1 then inc(Car[cnt].Points,10);
  if Car[Cnt].FinPos=2 then inc(Car[cnt].Points, 8);
  if Car[Cnt].FinPos=3 then inc(Car[cnt].Points, 6);
  if Car[Cnt].FinPos=4 then inc(Car[cnt].Points, 4);
  if Car[Cnt].FinPos=5 then inc(Car[cnt].Points, 2);
  if Car[Cnt].FinPos=6 then inc(Car[cnt].Points, 1);
 end else
 for Cnt:=1 to 2 do begin
  if Not Car[Cnt].Fin then Car[cnt].FinPos:=Position(cnt);
  if Car[Cnt].FinPos=1 then inc(Car[cnt].Points,10);
  if Car[Cnt].FinPos=2 then inc(Car[cnt].Points, 4);
 end
end else begin
 if Safe=0 then inc(Car[BanCar].Points) else inc(Car[Safe2].Points);
end;

TypeCon1:=SafeTC1;
TypeCon2:=SafeTC2;
CdVol:=SafeCD;

EraseAllObjs;
KillFont;
KillSound;
StopAudio;
if SVGA=1 then KillSpdBuf;
KillTable(1);KillTable(2);
KillGraph;
KillKeyb;
KillMisc;
KillRoad(GRoad);

SaveConfig;

CloseReplay;
ReplayStatus:=0;
if RicReplay then begin Ricreplay:=False; goto Reinit end;

 SaveRace;
 SaveRec(Ntrack);

KillReplay;

if GameMode=3 then KillNet;

if Esc then begin
(********* EXIT *************)
  assign(F,'password.WWW');
  rewrite(f,1);
  writefile('Menu',8,f);
  close(f);
(****************************)
end else begin
(********* EXIT *************)
  assign(F,'password.WWW');
  rewrite(f,1);
  writefile('Round',8,f);
  close(f);
(****************************)
end;

end.