
---VARS
x = 25
y = 130
bx = 0
by = 0
bxmax = 0
rpm = 900
rpmmin = 900
rpmmax = 9000
mph = 0
hp = 0
hpmax = 375
gear = 0
gearmin = 0
upCount = 0
downCount = 0
kx = 408
ky = 207
k1x = 398
k1y = 197
k2x = 398
k2y = 217
k3x = 408
k3y = 197
k4x = 408
k4y = 217
k5x = 418
k5y = 197
k6x = 418
k6y = 217
time = 0
max = 0
airpm = 6000
airpmmax = 9000
aigear = 0
aix = 25
aiy = 40
aimph = 0
aitime = 0
aimax = 0
high = 0
musiccount = 0
noslost = 4
nosremain = 0

gradio = Sound.load("sound/gradio.wav", true)

function music()
  musiccount = musiccount+1
end

---CONTROLS
function Pad()
   local pad = Controls.read()
  if pad:square() then
    brake() end
---player shifting
  dx = pad:analogX()
  dy = pad:analogY()  
  if pad:l() then
    hp = 0
    c = 1
  else c = 0 
end    
---player accelling
  
  if pad:r() and gear==0 and style == 1 then
    rpm = rpm+500
  elseif pad:r() and gear==0 and style == 0 then
    rpm = rpm+500 end
  if pad:r() and gear==1 and bx > -5600 and style == 1 then
    rpm = rpm+150 
  elseif pad:r() and gear==1 and bx > -5600 and style == 0 then
    rpm = rpm+140 end
  if pad:r() and gear==2 and bx > -5600 and style == 1 then
    rpm = rpm+120 
  elseif pad:r() and gear==2 and bx > -5600 and style == 0 then
    rpm = rpm+125 end
  if pad:r() and gear==3 and bx > -5600 and style == 1 then
    rpm = rpm+90  
  elseif pad:r() and gear==3 and bx > -5600 and style == 0 then
    rpm = rpm+110 end
  if pad:r() and gear==4 and bx > -5600 and style == 1 then
    rpm = rpm+80 
  elseif pad:r() and gear==4 and bx > -5600 and style == 0 then
    rpm = rpm+90 end
  if pad:r() and gear==5 and bx > -5600 then
    rpm = rpm+80 end
  if pad:r() and gear==6 and bx > -5600 then
    rpm = rpm+80 end
  if pad:triangle() then
    screen:save("save.png") end
  if pad:circle() and nosremain > 0 then
    nx() end
  if pad:select() then 
    gradio = nil
    dofile "menu.lua" 
end
  if pad:start() then 
  dofile "pause.lua" 
end
  if c == 1 and dx < -80 and dy < 20 then  
  gear = 1
  rpm = rpm-1000
elseif c == 1  and dx < -80 and dy > -20 then  
  gear = 2
  rpm = rpm-1000
elseif c == 1 and dx > -90 and dx < 90 and dy < -20 then  
  gear = 3
  rpm = rpm-1000
elseif c == 1 and dx > -90 and dx < 90 and dy > 20 then  
  gear = 4
  rpm = rpm-1000
elseif c == 1 and dx > 80 and dy < -20 and gear < gearmax then  
  gear = 5
  rpm = rpm-1000
elseif c == 1 and dx > 80 and dy > 20 and gear < gearmax then  
  gear = 6
  rpm = rpm-1000
elseif c == 1 and math.abs(dx) < 20 and math.abs(dy) < 20 then  
  gear = 0
end

end

function Pad2()
   local pad = Controls.read()
  if pad:square() then
    brake() end
---player shifting
  if pad:r() and gear < gearmax then
    upCount = upCount+1
  if upCount == 1 and gear < gearmax then
    shiftup() end
   else
  upCount = 0
end
 
  if pad:l() then
    downCount = downCount+1
  if downCount == 1 and gear > 0 then
    shiftdn() end
   else
  downCount = 0 end
---player accelling
  
  if pad:cross() and gear==0 and bx > -5600 then
    rpm = rpm+accel0
end
  if pad:cross() and gear==1 and bx > -5600 then
    rpm = rpm+accel1
end
  if pad:cross() and gear==2 and bx > -5600 then
    rpm = rpm+accel2 
end
  if pad:cross() and gear==3 and bx > -5600 then
    rpm = rpm+accel3  
end
  if pad:cross() and gear==4 and bx > -5600 then
    rpm = rpm+accel4 
end
  if pad:cross() and gear==5 and bx > -5600 then
    rpm = rpm+accel5 end
  if pad:cross() and gear==6 and bx > -5600 then
    rpm = rpm+accel6 end
  if pad:triangle() then
    screen:save("save.png") end
  if pad:circle() and nosremain > 0 then
    nx() end
  if pad:select() then 
    gradio = nil
    dofile "menu.lua" 
end
  if pad:start() then 
  dofile "pause.lua" 
end
end
--player shifting

function shiftup()
  if rpm > 6000 and gear == 0 then
  burnout:play() 
  gear = gear + 1
  rpm = rpm - 2000
elseif gear < gearmax then
  gear = gear+1
  rpm = rpm-2000 end
  
end 

function shiftdn()
  gear = gear-1
end



--player gears
function neutral()
  hp = 0
  kx = 408
  ky = 207
end
function firstgear()
  mph = hp*first
  kx = k1x
  ky = k1y
end
function secondgear()
  mph = hp*second
  kx = k2x
  ky = k2y
end
function thirdgear()
  mph = hp*third
  kx = k3x
  ky = k3y
  
end
function fourthgear()
  mph = hp*fourth
  kx = k4x
  ky = k4y
  
end
function fifthgear()
  mph = hp*fifth  
  kx = k5x
  ky = k5y
  
end
function sixthgear()
  mph = hp*sixth
  kx = k6x
  ky = k6y
  
end

---AI
---aigears
function aineutral()
  aihp = 0
  
end
function aifirstgear()
  aimph = aihp*aifirst
  
end
function aisecondgear()
  aimph = aihp*aisecond
  
end
function aithirdgear()
  aimph = aihp*aithird
  
  
end
function aifourthgear()
  aimph = aihp*aifourth
  
  
end
function aififthgear()
  aimph = aihp*aififth  
  
  
end
function aisixthgear()
  aimph = aihp*aisixth
  
  
end


---ai acceling
function easyai()
  aiaccel = math.random(1,3)
if aigear == 0 then 
  aigear = 1
end
if aiaccel == 1 and aigear==1 and aix+75 < bx+5600 then
    airpm = airpm+175 
elseif aiaccel == 2 and aigear==1 and aix+75 < bx+5600 then
    airpm = airpm+165
elseif aiaccel == 3 and aigear==1 and aix+75 < bx+5600 then
    airpm = airpm+155
end
if aiaccel == 1 and aigear==2 and aix+75 < bx+5600 then
    airpm = airpm+175
elseif aiaccel == 2 and aigear==2 and aix+75 < bx+5600 then
    airpm = airpm+165
elseif aiaccel == 3 and aigear==2 and aix+75 < bx+5600 then
    airpm = airpm+155
end
if aiaccel == 1 and aigear==3 and aix+75 < bx+5600 then
    airpm = airpm+135
elseif aiaccel == 2 and aigear==3 and aix+75 < bx+5600 then
    airpm = airpm+125
elseif aiaccel == 3 and aigear==3 and aix+75 < bx+5600 then
    airpm = airpm+115
end
if aiaccel == 1 and aigear==4 and aix+75 < bx+5600 then
    airpm = airpm+120
elseif aiaccel == 2 and aigear==4 and aix+75 < bx+5600 then
    airpm = airpm+110
elseif aiaccel == 3 and aigear==4 and aix+75 < bx+5600 then
    airpm = airpm+100
end
if aiaccel == 1 and aigear==5 and aix+75 < bx+5600 then
    airpm = airpm+105 
elseif aiaccel == 2 and aigear==5 and aix+75 < bx+5600 then
    airpm = airpm+95
elseif aiaccel == 3 and aigear==5 and aix+75 < bx+5600 then
    airpm = airpm+85
end
if aiaccel == 1 and aigear==6 and aix+75 < bx+5600 then
    airpm = airpm+100
elseif aiaccel == 2 and aigear==6 and aix+75 < bx+5600 then
    airpm = airpm+90
elseif aiaccel == 3 and aigear==6 and aix+75 < bx+5600 then
    airpm = airpm+80
end
end

function hardai()
  aiaccel = math.random(1,3)
if aigear == 0 then 
  aigear = 1
end
if aiaccel == 1 and aigear==1 and aix+75 < bx+5600 then
    airpm = airpm+185 
elseif aiaccel == 2 and aigear==1 and aix+75 < bx+5600 then
    airpm = airpm+175
elseif aiaccel == 3 and aigear==1 and aix+75 < bx+5600 then
    airpm = airpm+160
end
if aiaccel == 1 and aigear==2 and aix+75 < bx+5600 then
    airpm = airpm+175
elseif aiaccel == 2 and aigear==2 and aix+75 < bx+5600 then
    airpm = airpm+165
elseif aiaccel == 3 and aigear==2 and aix+75 < bx+5600 then
    airpm = airpm+155
end
if aiaccel == 1 and aigear==3 and aix+75 < bx+5600 then
    airpm = airpm+155
elseif aiaccel == 2 and aigear==3 and aix+75 < bx+5600 then
    airpm = airpm+145
elseif aiaccel == 3 and aigear==3 and aix+75 < bx+5600 then
    airpm = airpm+135
end
if aiaccel == 1 and aigear==4 and aix+75 < bx+5600 then
    airpm = airpm+140
elseif aiaccel == 2 and aigear==4 and aix+75 < bx+5600 then
    airpm = airpm+135
elseif aiaccel == 3 and aigear==4 and aix+75 < bx+5600 then
    airpm = airpm+125
end
if aiaccel == 1 and aigear==5 and aix+75 < bx+5600 then
    airpm = airpm+115 
elseif aiaccel == 2 and aigear==5 and aix+75 < bx+5600 then
    airpm = airpm+105
elseif aiaccel == 3 and aigear==5 and aix+75 < bx+5600 then
    airpm = airpm+95
end
if aiaccel == 1 and aigear==6 and aix+75 < bx+5600 then
    airpm = airpm+110
elseif aiaccel == 2 and aigear==6 and aix+75 < bx+5600 then
    airpm = airpm+105
elseif aiaccel == 3 and aigear==6 and aix+75 < bx+5600 then
    airpm = airpm+100
end
end


---ai shifting
function aishift()
  aishifting = math.random(1,2)
  if aishifting == 1 and aigear == 0 and airpm > 8500 and bx < 0 then
    aigear = 1
    airpm = 7000
elseif aigear == 0 and airpm > 7500 then
    aigear = 1
    airpm = 5000
end
  if aishifting == 1 and aigear == 1 and airpm > 8500 and bx < 0 then
    aigear = 2
    airpm = 7000
elseif aigear == 1 and airpm > 7500 then
    aigear = 2
    airpm = 5000
end
  if aishifting == 1 and aigear == 2 and airpm > 8500 then
    aigear = 3
    airpm = 7000
elseif aigear == 2 and airpm > 7500 then
    aigear = 3
    airpm = 5000
end
  if aishifting == 1 and aigear == 3 and airpm > 8500 then
    aigear = 4
    airpm = 7000
elseif aigear == 3 and airpm > 7500 then
    aigear = 4
    airpm = 5000
end
  if aishifting == 1 and aigear == 4 and airpm > 8500 and aigear < gearmax then
    aigear = 5
    airpm = 7000
elseif aigear == 4 and airpm > 7500 then
    aigear = 5
    airpm = 5000
end
  if aishifting == 1 and aigear == 5 and airpm > 8500 and aigear < gearmax then
    aigear = 6
    airpm = 7000
elseif aigear == 5 and airpm > 7500 then
    aigear = 6
    airpm = 5000
end
end


---ai brake
function aibrake()
  aihp = 0
  aimph = 0
 end

---player brake
function brake()
  if rpm > 900 then
  rpm = rpm-100 end
  if mph > 10 then
  tiresound:play() 
end
end

--slow while not accelling
function slow()
  if gear == 0 and rpm > 900 then
  rpm = rpm-350
  elseif gear > 0 and rpm > 900 then
  rpm = rpm-75
  airpm = airpm-75
end
end

---player time
function timer()
  if (bx < 0) and (x+75 < bx+5600) then
    time = time+1
end
end

---ai time
function aitimer()
  if (bx < 0) and (aix+75 < bx+5600) then
    aitime = aitime+1
end
end

---nos
function nx()
  if nosremain > 0 then
  rpm = rpm+100
  nosremain = nosremain-noslost
end
end


function aimove()
if aix < bx+5650 then
 aix = aix+aimph/4
end
end

-----
function saveHighscore()
  file = io.open(highscoreFile, "w")
  if file then
    file:write(time/25)
    file:close()
  end
end
 
function loadHighscore()
  file = io.open(highscoreFile, "r")
  if file then
    high = file:read("*n")
    file:close()
  end
end
------
function savewins()
  winfile = io.open(winsFile, "w")
  if winfile then
    winfile:write(wins)
    winfile:close()
  end
end
-----
function savemoney()
  moneyfile = io.open(moneyFile, "w")
  if moneyfile then
    moneyfile:write(money)
    moneyfile:close()
  end
end



while true do

---EVERYTHING

--rev limiter
if rpm > rpmmax then
  rpm = 8800
end
if rpm < 900 then
  rpm = 900
end

--hp limiter
if hp > hpmax then
  hp = hp-1
end
--gears
if car == iroc or car == prelude or car == supra2 then
first = .0982 
second = .1090
third = .1548
fourth = .2597
fifth = .3279
sixth = 0
end

if car == s13 or car == crx then
first = .0906
second = .1098
third = .1374
fourth = .2397
fifth = .3094
sixth = 0
end

if car == rx7 then
first = .0966
second = .1106
third = .1448
fourth = .2125
fifth = .2904
sixth = .3485
end

if car == vette or car == stang or car == gtr or car == supra or car == gto then
first = .0942
second = .1156
third = .1908
fourth = .2387
fifth = .3148
sixth = .4101
end

if car == charger then
first = .0964
second = .1604
third = .3148
fourth = .3289
fifth = 0
sixth = 0
end

if car == sting then
first = .0984
second = .1504
third = .2848
fourth = .3398
fifth = 0
sixth = 0
end

if car == s2000 then
first = .0966
second = .1106
third = .1848
fourth = .2125
fifth = .2904
sixth = .3485
end

if car == gt350 then
first = .0994
second = .1504
third = .2548
fourth = .3298
fifth = 0
sixth = 0
end


if car == integra then
first = .0936
second = .1056
third = .1648
fouth = .2125
fifth = .3064
sixth = 0
end

if style == 0 then
accel0 = 500
accel1 = 160
accel2 = 145
accel3 = 130
accel4 = 120
accel5 = 110
accel6 = 110
end

if style == 1 then
accel0 = 500
accel1 = 160
accel2 = 130
accel3 = 100
accel4 = 80
accel5 = 0
accel6 = 0
end

----
if intakeopt == 1 then
first=first+.005
second=second+.005
third=third+.005
fourth=fourth+.005
fifth=fifth+.005
sixth=sixth+.005
end
if intakeopt == 2 then
first=first+.009
second=second+.009
third=third+.009
fourth=fourth+.009
fifth=fifth+.009
sixth=sixth+.009
end
if intakeopt == 3 then
first=first+.016
second=second+.016
third=third+.016
fourth=fourth+.016
fifth=fifth+.016
sixth=sixth+.016
end
----
if exhaustopt == 1 then
first=first+.005
second=second+.005
third=third+.005
fourth=fourth+.005
fifth=fifth+.005
sixth=sixth+.005
end
if exhaustopt == 2 then
first=first+.009
second=second+.009
third=third+.009
fourth=fourth+.009
fifth=fifth+.009
sixth=sixth+.009
end
if exhaustopt == 3 then
first=first+.016
second=second+.016
third=third+.016
fourth=fourth+.016
fifth=fifth+.016
sixth=sixth+.016
end
----
if cpuopt == 1 then
first=first+.007
second=second+.007
third=third+.007
fourth=fourth+.007
fifth=fifth+.007
sixth=sixth+.007
end
if cpuopt == 2 then
first=first+.01
second=second+.01
third=third+.01
fourth=fourth+.01
fifth=fifth+.01
sixth=sixth+.01
end
if cpuopt == 3 then
first=first+.019
second=second+.019
third=third+.019
fourth=fourth+.019
fifth=fifth+.019
sixth=sixth+.019
end
----
if turboopt == 1 then
first=first+.009
second=second+.009
third=third+.009
fourth=fourth+.009
fifth=fifth+.009
sixth=sixth+.009
end
if turboopt == 2 then
first=first+.014
second=second+.014
third=third+.014
fourth=fourth+.014
fifth=fifth+.014
sixth=sixth+.014
end
if turboopt == 3 then
first=first+.024
second=second+.024
third=third+.024
fourth=fourth+.024
fifth=fifth+.024
sixth=sixth+.024
end
----
if tiresopt == 1 then
first=first+.003
second=second+.003
end
if tiresopt == 2 then
first=first+.007
second=second+.007
end
if tiresopt == 3 then
first=first+.015
second=second+.015
end
----
if tranopt == 1 then
accel1 = accel1 + 5
accel2 = accel2 + 5
accel3 = accel3 + 5
accel4 = accel4 + 5
accel5 = accel5 + 5
accel6 = accel6 + 5
end
if tranopt == 2 then
accel1 = accel1 + 10
accel2 = accel2 + 10
accel3 = accel3 + 10
accel4 = accel4 + 10
accel5 = accel5 + 10
accel6 = accel6 + 10
end
if tranopt == 3 then
accel1 = accel1 + 15
accel2 = accel2 + 15
accel3 = accel3 + 15
accel4 = accel4 + 15
accel5 = accel5 + 15
accel6 = accel6 + 15
end

if gear == 0 then
  neutral() end
if gear == 1 then
  firstgear() end
if gear == 2 then
  secondgear() end
if gear == 3 then
  thirdgear() end
if gear == 4 then
  fourthgear() end
if gear == 5 and gear <= gearmax then
  fifthgear() end
if gear == 6 and gear <= gearmax then
  sixthgear() end
  
if gear >= gearmax then
  gear = gearmax end
if gear <= gearmin then
  gear = gearmin end

if car == supra then maxhp = 450 end
if car == gtr then maxhp = 450 end
if car == stang then maxhp = 500 end
if car == rx7 then maxhp = 350 end
if car == s13 then maxhp = 250 end
if car == gto then maxhp = 400 end
if car == vette then maxhp = 500 end
if car == charger then maxhp = 450 gearmax = 4 end
if car == sting then maxhp = 400 gearmax = 4 end
if car == s2000 then maxhp = 275 end
if car == gt350 then maxhp = 500 end
if car == crx then maxhp = 220 end
if car == iroc then maxhp = 300 end
if car == integra then maxhp = 250 end
if car == prelude then maxhp = 250 end
if car == supra2 then maxhp = 300 end


--sounds

---finshed?
if x+75 > bx+5600 then
  brake()
end
if aix+75 > bx+5700 then
  aibrake()
end
if x+75 > bx+5600 and aix+75 > bx+5600 then
  savenos()
  gradio = nil
  dofile "end.lua"
end


---movebg
bx = bx-mph/5
aix = aix-mph/5

---get hp
hp = rpm*.0425
aihp = airpm*.0475

if rpm <= 900 then
  mph = 0
  hp = 0
end

if airpm > 9000 then
  airpm = 7500
end

---nos empty
if nosremain < 1 then 
nosremain = 0 end


---max speed
if mph > max then
  max = mph
end
if aimph > aimax then
  aimax = aimph
end

---ai start
if bx<0 and aix+75 < bx+5600 and ai == s13 then
  easyai()
  
end
if bx<0 and aix+75 < bx+5600 and ai == supra then
  hardai()
  
end
if bx<0 and aix+75 < bx+5600 and ai == skyline then
  hardai()
  
end
if bx<0 and aix+75 < bx+5600 and ai == rx7 then
  hardai()
  
end
if bx<0 and aix+75 < bx+5600 and ai == vette then
  hardai()
  
end
if bx<0 and aix+75 < bx+5600 and ai == stang then
  hardai()
  
end
if bx<0 and aix+75 < bx+5600 and ai == gto then
  hardai()
  
end
if bx<0 and aix+75 < bx+5600 and ai == charger then
  hardai()
  
end
if bx<0 and aix+75 < bx+5600 and ai == s13 then
  easyai()
  
end
if bx<0 and aix+75 < bx+5600 and ai == sting then
  hardai()
  
end
if bx<0 and aix+75 < bx+5600 and ai == s2000 then
  easyai()
  
end

----ai gears

if ai == iroc or ai == prelude or ai == supra2 then
aifirst = .0922 
aisecond = .0990
aithird = .1548
aifourth = .2197
aififth = .2979
aisixth = .2979
end

if ai == s13 or ai == crx then
aifirst = .0906
aisecond = .0966
aithird = .1348
aifourth = .2097
aififth = .2904
end

if ai == rx7 then
aifirst = .0976
aisecond = .1006
aithird = .1425
aifourth = .2105
aififth = .2604
aisixth = .3195
end

if ai == vette or ai == stang or ai == skyline or ai == supra or ai == gto then
aifirst = .1006
aisecond = .1256
aithird = .1708
aifourth = .2587
aififth = .3348
aisixth = .4101
end

if ai == charger then
aifirst = .0964
aisecond = .1204
aithird = .2248
aifourth = .3189
end

if ai == sting then
aifirst = .0984
aisecond = .1504
aithird = .2848
aifourth = .3398
end

if ai == s2000 then
aifirst = .0966
aisecond = .1106
aithird = .1848
aifourth = .2125
aififth = .2904
aisixth = .3485
end

if ai == gt350 then
aifirst = .0994
aisecond = .1504
aithird = .2548
aifourth = .3298
aififth = .3298
end


if ai == integra then
aifirsta = .0936
aisecond = .1056
aithird = .1648
aifouth = .2125
aififth = .3064

end


if aigear == 1 then
    aifirstgear() end
if aigear == 2 then
    aisecondgear() end
if aigear == 3 then
    aithirdgear() end
if aigear == 4 then
    aifourthgear() end
if aigear == 5 and aigear < aigearmax then
    aififthgear() end
if aigear == 6 and aigear < aigearmax then
    aisixthgear() end

if aigear >= aigearmax then
    aigear = aigearmax end



if cnttype == 2 then
  Pad()
else Pad2()
end

if musiccount == 12 then
gradio:play()
end

if musiccount == 1 then
loadparts()
end

music()
aimove()
slow()
timer()
aitimer()
aishift()
screen:clear()
screen:blit(bx, by, bg)
screen:blit(bx+480, by, bg)
screen:blit(bx+960, by, bg)
screen:blit(bx+1440, by, bg)
screen:blit(bx+1920, by, bg)
screen:blit(bx+2400, by, bg)
screen:blit(bx+2880, by, bg)
screen:blit(bx+3360, by, bg)
screen:blit(bx+3840, by, bg)
screen:blit(bx+4320, by, bg)
screen:blit(bx+4800, by, bg)
screen:blit(bx+5280, by, bg)
screen:blit(bx+5600, by, finish)
screen:blit(bx+5760, by, bg)
screen:blit(bx+6240, by, bg)
screen:blit(bx+6720, by, bg)
screen:blit(x, y, car)
screen:blit(aix, aiy, ai)
screen:blit(0, 0, dash)
screen:blit(395, 203, boot)
screen:blit(kx, ky, knob)
screen:print(155, 245, rpm, red)
screen:print(315, 245, mph, red)
screen:print(55, 213, time/25, red)
screen:print(20, 213, "TIME", red)
screen:print(55, 235, nosremain, red)
screen:print(20, 235, "NOS", red)
screen:flip()
screen.waitVblankStart()
end
