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,xb,yb: integer;
Auxsec                              : puntsec;
Chk,Demo                            : boolean;
DemoCnt,DemoFase                    : word;
WinnerName,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;
LoadWinner;

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;

Car[1].chassis:=WinCars[LastWinner];
Car[1].Team:=(LastWinner-1) mod 3;
WinnerName:=Winners[LastWinner];

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

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

if demo then begin
     if Car[1].team=0 then Lbob('Pics\Coppa.pcx' ,'Data\Final.dat',80);
     if Car[1].team=1 then Lbob('Pics\Distin.pcx','Data\Final.dat',80);
     if Car[1].team=2 then Lbob('Pics\Soldi.pcx' ,'Data\Final.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*));

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;

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

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

SoundOn:=False;
(*StartBlaster;*)

scrswap;
 xb:=160 shl svga-gethx(80) div 4+random(Gethx(80) div 2);
 yb:=100 shl svga-gethy(80) div 4+random(Gethy(80) div 2);
ResetTime;
GlobalTime:=300000;
Start:=True;

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

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

(****************************** 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 28 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,1);

 if DemoCnt>3 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);

 if Drawing then begin
  bob(80,160 shl svga-GetHx(80) div 2,100 shl SVGA-gethy(80) div 2);
  if DemoFase>4 then begin
                      DemoFase:=1;
                      xb:=160 shl svga-gethx(80) div 4+random(Gethx(80) div 2);
                      yb:=100 shl svga-gethy(80) div 4+random(Gethy(80) div 2);
                     end;
  if Demofase=1 then bob(81,xb,yb);
  if Demofase=2 then bob(82,xb,yb);
  if DemoFase=3 then Bob(83,xb,yb);
  if DemoFase=4 then Bob(84,xb,yb);
  Text(260 shl SVGa,90 shl SVGA,WinnerName,true);
  Text( 40 shl SVGa,90 shl SVGA,WinnerName,true);
  PutObj(20,0,100,500,Car[1].Gangle,0,0,270 shl SVGA,90 shl SVGA);
  PutObj(20,0,100,500,Car[1].Gangle,0,0, 50 shl SVGA,90 shl SVGA);
  PutObj( 1,0,100,500,Car[1].Gangle,0,0,270 shl SVGA,90 shl SVGA);
  PutObj( 1,0,100,500,Car[1].Gangle,0,0, 50 shl SVGA,90 shl SVGA);
 end;
(*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.