uses Crt,Sound,General,Graphics,Road,PilUnit,Keyb,Perspect,Joys,ConfUnit,CamUnit,CdUnit,CdVars,SelCarun;

const CamDefine = false;

label uscita,uscita2,MAINLOOP,DEMOPOINT;

var
(*
MemFree             : array[1..13000] of byte;*)
F                   : file;

CurrCar                            : byte;
Auxi,Cnt,CutOff,Waiter,xwr,xwd     : integer;
Auxsec                             : puntsec;
Chk,Demo                           : boolean;
DemoCnt,DemoFase                   : word;
CurrDirectory                      : string;
Mytrack                            : word;

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

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

Initgraph(3000);
Initkeyb;
InitMisc;
InitPros;

Demo:=True;
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:

LoadConfig;
GameMode:=0;
Rdifficulty:=Difficulty;
DDifficulty:=Rdifficulty*10;

initsound(1000);

NumPla:=1;
CarPl1:=1;
CarPl2:=2;
Detail:=1;
MaxDistance:=25;
Cam[1].Mode:=4;
CurrCar:=1;
DemoCnt:=5000;
DemoFase:=0;
Difficulty:=2;
SoundOn:=false;

DEMOPOINT:

LoadLogo(Maxbobs-2);
Loadpcx('Pics\Wait.pcx',Pseg,Pofs);
Loadpal('Pics\Wait.pcx', 20,255);
PlaceLogo;
FastEraseAllBob;
scrswap;

Initfont;

Loadfont('Pics\Font.pcx');

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

Loadroad(Groad,'Track'+Tostr(random(3)+1,0));

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

SelectionCar;

 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]);

LoadWir(19,  70,'3Data\Smoke.obj');
LoadWir(20,   1,'3Data\Shadow.obj');

if demo then begin Lbob('Pics\WorScrip.pcx','Data\WorScrip.dat',80);
    end else begin Lbob('Pics\Acces.pcx'   ,'Data\Acces.dat', 80);
                  LFbob('Pics\Acces.pcx'   ,'Data\Over.dat' , 88); end;

Cnt:=GRoad.MaxSec div 8;

AuxSec:=Groad.curs;
for auxi:=0 to 0 do Auxsec:=Auxsec^.last;
PlaceCar(0,1(*N*),Auxsec(*PSec*),-100(*X*),0(*Z*),40(*Y*),0(*A*),0(*S*));
PlaceCar(0,2(*N*),Auxsec(*PSec*), 100(*X*),0(*Z*),40(*Y*),0(*A*),0(*S*));

if AllCars or Demo then begin
AuxSec:=Groad.curs;
for auxi:=0 to 2 do Auxsec:=Auxsec^.next;
PlaceCar(0,3(*N*),Auxsec(*PSec*),  -100(*X*),0(*Z*),40(*Y*),0(*A*),0(*S*));
AuxSec:=Groad.curs;
for auxi:=0 to 6 do Auxsec:=Auxsec^.next;
PlaceCar(0,4(*N*),Auxsec(*PSec*),   100(*X*),0(*Z*),40(*Y*),0(*A*),0(*S*));

AuxSec:=Groad.curs;
for auxi:=0 to 10 do Auxsec:=Auxsec^.next;
PlaceCar(0,5(*N*),Auxsec(*PSec*),  -100(*X*),0(*Z*),40(*Y*),0(*A*),0(*S*));
AuxSec:=Groad.curs;
for auxi:=0 to 14 do Auxsec:=Auxsec^.next;
PlaceCar(0,6(*N*),Auxsec(*PSec*),   100(*X*),0(*Z*),40(*Y*),0(*A*),0(*S*));

AuxSec:=Groad.curs;
for auxi:=0 to 18 do Auxsec:=Auxsec^.next;
PlaceCar(0,7(*N*),Auxsec(*PSec*),  -100(*X*),0(*Z*),40(*Y*),0(*A*),0(*S*));
AuxSec:=Groad.curs;
for auxi:=0 to 22 do Auxsec:=Auxsec^.next;
PlaceCar(0,8(*N*),Auxsec(*PSec*),   100(*X*),0(*Z*),40(*Y*),0(*A*),0(*S*));
for Cnt:=3 to 8 do Lap[Cnt]:=1;
end;

Car[1].Team:=1;
Car[2].Team:=0;
Car[3].Team:=0;
Car[4].Team:=0;
Car[5].Team:=0;
Car[6].Team:=0;
Car[7].Team:=0;
Car[8].Team:=0;

BanCar:=8;
Car[BanCar].Team:=2;

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\Best.Raw' );
loadsample(8 ,'Samples\Final.Raw' );

Secdistance:=140;
NumCamPos:=1;

if SVGA=1 then InitSpdBuf(65);

fade(4); clr(0); scrswap;

Loadpal('Pics\Track2.pcx',20,255);
CreateTable(1,235,235,235);
CreateTable(2,20,20,20);

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

if Not Demo then begin
if (Typecon1=1) and JoyOn1 then begin Typecon1:=3; end else Typecon1:=1;
if (Typecon2=1) and JoyOn1 then begin Typecon2:=3; end else Typecon2:=2;
end else begin Typecon1:=0;Typecon2:=0 end;

if Detail=1 then begin
  MsX1clip[1]:=  0 shl SVGA;  MsY1clip[1]:= 0 shl SVGA;  MsX2clip[1]:=319 shl SVGA;  MsY2clip[1]:=166 shl SVGA;
end else begin
  MsX1clip[1]:= 10 shl SVGA;  MsY1clip[1]:=65 shl SVGA;  MsX2clip[1]:=309 shl SVGA;  MsY2clip[1]:=156 shl SVGA;
end;

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

Clipgraph(MsX1Clip[1],0,MsX2Clip[1],199 shl SVGA);

Loadpcx('Pics\BackGam1.pcx',Pseg,Pofs);
Typecon2:=0;

AuxSec:=Groad.curs;
for auxi:=0 to 5 do Auxsec:=Auxsec^.next;
PlaceCam(1,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;

bob(149,0,MsY2Clip[1]+1);
box(0,0,319 shl SVGA,199 shl SVGA,0);

Mytrack:=5+1;
PlayTrack(Mytrack);Delay(500);

(*StartBlaster;*)

(*if SBOn then Begin PlaySampleHd('Samples\'+'Mus-1.Raw',True); PlayMusic; end;*)
SoundOn:=False;

scrswap;

ResetTime;
GlobalTime:=300000;
Start:=True;

(************************ MAIN ****************************)
repeat
NextSound; (******************** IF SOUND ****************************)

          ControlCar (1,TypeCon1);
PilotCar(1,Xtrottle[1],Ytrottle[1],CGear[1]);

ControlCar (2,TypeCon2);
PilotCar(2,Xtrottle[2],Ytrottle[2],CGear[2]);

if AllCars or Demo then
 for cnt:=3 to 8 do begin
 ControlCar (cnt,0);
 PilotCar(cnt,Xtrottle[cnt],Ytrottle[cnt],CGear[cnt]);
end;

(****************************** 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;
NextSound; (******************** IF SOUND ****************************)

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

for cnt:=1 to 1 do CheckCollision(2,cnt);

if AllCars or demo then begin
 for cnt:=1 to 2 do CheckCollision(3,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);
end;


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

 inc(DemoCnt,10);

 if DemoCnt>2000 then begin
  inc(DemoFase);
  DemoCnt:=0;
  CurrCar:=1;
  inc(Cam[1].mode);
  if Cam[1].Mode>4 then Cam[1].Mode:=1;
  Cam[1].Mode:=4;
  Han[1]:=0;
  Car[CurrCar].Infos:=False;
  Car[CurrCar].Dash :=False;
  MsY2Clip[1]:=AuxY12Clip+32;
 end;

 PilotCam(1);
 ControlCam(1,Cam[1].Mode,CurrCar);

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

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

 if Drawing then TraceRoadCam(1,160,Cam[1].Orizont+MsY2Clip[1]-66 shl SVGA,5);

 inc(xwr,(Xwd-Xwr) div 10);
 if  DemoFase>2              then DemoFase:=1;
 if Demofase=1 then xwd:= 160;
 if Demofase=2 then xwd:=-160;
 if (DemoFase=1) and Drawing then Bob(80,(xwr    ) shl SVGA-GetHx(80) div 2,5 shl SVGA);
 if (DemoFase=2) and Drawing then Bob(81,(xwr+320) shl SVGA-GetHx(81) div 2,5 shl SVGA);
(* if (DemoFase=2) and Drawing then Bob(81,160-GetHx(81) div 2,100-GetHy(81) div 2);(* else
 if (DemoFase=3) and Drawing then Bob(82,160-GetHx(82) div 2,100-GetHy(82) div 2) else
 if (DemoFase=4) and Drawing then Bob(84,160-GetHx(84) div 2,120-GetHy(84) div 2);*)

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

Box(0,0,319 shl SVGA+SVGA,199 shl SVGA+SVGA,0);
if Drawing then scrswap;

auxi:=time;

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

if keypush>0 then empty;

(**************************** CHECK END GAME **********************)

until keypush>0;

SoundOn:=False;
KillMusic;
Fade(4);
FadeAudio(100);
clr(0);
scrswap;

ch[1].st:=0;
ch[2].st:=0;
ch[3].st:=0;
ch[4].st:=0;
ch[1].stAlert:=0;
ch[2].stAlert:=0;
ch[3].stAlert:=0;
ch[4].stAlert:=0;


uscita:

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

(********* EXIT *************)
  assign(f,'password.WWW');
  rewrite(f,1);
  writefile('Menu',8,f);
  close(f);
(****************************)

end.