bgMusic = nil
shopmusic = Sound.load("sound/music1.wav", true)
shopmusic:play()

incolor = yellow
excolor = yellow
cpucolor = yellow
turbocolor = yellow
noscolor = yellow
tirecolor = yellow
trancolor = yellow

intake = 0
exhaust = 0
cpu = 0
turbo = 0
nos = 0
tires = 0
tran = 0
optdown = 0
optup = 0
optleft = 0
optright = 0
intakeup = 0
exhaustup = 0
cpuup = 0
turboup = 0
nosup = 0 
tiresup = 0
tranup = 0
intakedown = 0
exhaustdown = 0
cpudown = 0
turbodown = 0
nosdown = 0 
tiresdown = 0
trandown = 0
opt = 0
intakeprice = 0
exhaustprice = 0
cpuprice = 0
turboprice = 0
nosprice = 0
tiresprice = 0
tranprice = 0
buycount = 0
line1 = " "
line2 = " "
line3 = " "
line4 = " "
line5 = " "
nocount = 0
load = 0
loadopt = 0
intakeopt = 0
exhaustopt = 0
cpuopt = 0
turboopt = 0
tiresopt = 0
tranopt = 0

function loadparts()
  loadintake()
  loadexhaust()
  loadcpu()
  loadturbo()
  loadnos()
  loadtires()
  loadtran()
end

function intakeright()
  intake = intake +1
  if intake > 3 then intake = 0 end
  if intake < 0 then intake = 3 end
end

function exhaustright()
  exhaust = exhaust +1
  if exhaust > 3 then exhaust = 0 end
  if exhaust < 0 then exhaust = 3 end
end

function cpuright()
  cpu = cpu +1
  if cpu > 3 then cpu = 0 end
  if cpu < 0 then cpu = 3 end
end

function turboright()
  turbo = turbo +1
  if turbo > 3 then turbo = 0 end
  if turbo < 0 then turbo = 3 end
end

function nosright()
  nos = nos +1
  if nos > 1 then nos = 0 end
  if nos < 0 then nos = 1 end
end

function tiresright()
  tires = tires +1
  if tires > 3 then tires = 0 end
  if tires < 0 then tires = 3 end
end


function tranright()
  tran = tran +1
  if tran > 3 then tran = 0 end
  if tran < 0 then tran = 3 end
end
-----
function intakeleft()
  intake = intake -1
  if intake > 3 then intake = 0 end
  if intake < 0 then intake = 3 end
end

function exhaustleft()
  exhaust = exhaust -1
  if exhaust > 3 then exhaust = 0 end
  if exhaust < 0 then exhaust = 3 end
end

function cpuleft()
  cpu = cpu -1
  if cpu > 3 then cpu = 0 end
  if cpu < 0 then cpu = 3 end
end

function turboleft()
  turbo = turbo -1
  if turbo > 3 then turbo = 0 end
  if turbo < 0 then turbo = 3 end
end

function nosleft()
  nos = nos -1
  if nos > 3 then nos = 0 end
  if nos < 0 then nos = 3 end
end

function tiresleft()
  tires = tires -1
  if tires > 3 then tires = 0 end
  if tires < 0 then tires = 3 end
end

function tranleft()
  tran = tran -1
  if tran > 3 then tran = 0 end
  if tran < 0 then tran = 3 end
end

function shopright() 
if opt == 0 then intakeright() end
if opt == 1 then exhaustright() end 
if opt == 2 then cpuright() end
if opt == 3 then turboright() end
if opt == 4 then nosright() end
if opt == 5 then tiresright() end
if opt == 6 then tranright() end
end

function shopleft() 
if opt == 0 then intakeleft() end
if opt == 1 then exhaustleft() end
if opt == 2 then cpuleft() end
if opt == 3 then turboleft() end
if opt == 4 then nosleft() end
if opt == 5 then tiresleft() end
if opt == 6 then tranleft() end
end


function shopcontrols()
shoppad = Controls.read()
  if shoppad:up() then
    optup = optup+1
  else optup = 0
end
  if shoppad:down() then
    optdown = optdown+1
  else optdown = 0
end
  if shoppad:right() then
    optright = optright+1
  if optright == 1 then
    shopright() end
   else
  optright = 0
end
  if shoppad:left() then 
    optleft = optleft+1
  if optleft == 1 then
    shopleft() end
   else
  optleft = 0 
end
  if shoppad:cross() and opt == 7 and total <= money and total > 0 then 
    buycount = buycount+1
  else buycount = 0
end
  if shoppad:cross() and opt == 7 and total > money then
    nocount = nocount+1
  else nocount = 0
end
  if shoppad:cross() and opt == 8 then
    shopmusic = nil
    dofile "menu.lua"
end
end

while true do

if optup == 1 then opt = opt - 1 end
if optdown == 1 then opt = opt + 1 end

if opt < 0 then opt = 8 end
if opt > 8 then opt = 0 end

if opt == 0 then incolor = orange else incolor = yellow end
if opt == 1 then excolor = orange else excolor = yellow end
if opt == 2 then cpucolor = orange else cpucolor = yellow end
if opt == 3 then turbocolor = orange else turbocolor = yellow end
if opt == 4 then noscolor = orange else noscolor = yellow end
if opt == 5 then tirecolor = orange else tirecolor = yellow end
if opt == 6 then trancolor = orange else trancolor = yellow end
if opt == 7 then buycolor = orange else buycolor = yellow end
if opt == 8 then backcolor = orange else backcolor = yellow end

if opt == 0 then
  line1 = "One of the cheapest and most effective"
  line2 = "ways to increase horsepower is by"
  line3 = "changing the intake.  More and cleaner"
  line4 = "air equals more horsepower."
  line5 = " "
end
if opt == 1 then
  line1 = "A hot engine means wasted energy."
  line2 = "You can increase efficiency and "
  line3 = "horsepower by changing the exhaust "
  line4 = "system."
  line5 = " "
end
if opt == 2 then
  line1 = "The car's onboard computer controls your"
  line2 = "cars performance. Adding a new CPU alters"
  line3 = "your air/fuel ratio, ignition timing, "
  line4 = "and other settings to unlock the "
  line5 = "engine's full power. "
end
if opt == 3 then
  line1 = "Adding a turbo boost to your engine will"
  line2 = "make a dramatic difference in power. A"
  line3 = "turbo uses the engines exhaust to spin a"
  line4 = "turbine that forces more air and fuel into"
  line5 = "the engine."
end
if opt == 4 then
  line1 = "Nitrous Oxide is an oxygen-rich and"
  line2 = "thus highly combustable gas. The nitrous"
  line3 = "is injected through the intake into the"
  line4 = "engine adding a huge amount of horsepower."
  line5 = " "
end
if opt == 5 then
  line1 = "It doesn't matter how much power you"
  line2 = "have if none of it can reach the road."
  line3 = "Upgrading your tires can give better "
  line4 = "grip. Better grip means faster "
  line5 = "acceleration."
end
if opt == 6 then
  line1 = "That horsepower has to get to the"
  line2 = "wheels somehow.  The transmission is"
  line3 = "the way to do it.  Easy and fast"
  line4 = "shifting can get you to your top speed"
  line5 = "faster."
end
if opt == 7 then
  line1 = " "
  line2 = " "
  line3 = " "
  line4 = " "
  line5 = " "
end
if opt == 8 then
  line1 = " "
  line2 = " "
  line3 = " "
  line4 = " "
  line5 = " "
end

if nos == 0 then shopnos = "none" end
if nos == 1 then shopnos = "Refill" end


if intake == 1 then intakeprice = 200 
elseif intake == 2 then intakeprice = 800
elseif intake == 3 then intakeprice = 1200
else intakeprice = 0
end
if exhaust == 1 then exhaustprice = 200 
elseif exhaust == 2 then exhaustprice = 800
elseif exhaust == 3 then exhaustprice = 1200
else exhaustprice = 0
end
if cpu == 1 then cpuprice = 500 
elseif cpu == 2 then cpuprice = 800
elseif cpu == 3 then cpuprice = 1000
else cpuprice = 0
end
if turbo == 1 then turboprice = 500 
elseif turbo == 2 then turboprice = 1000
elseif turbo == 3 then turboprice = 2000
else turboprice = 0
end
if nos == 1 then nosprice = 100 
else nosprice = 0
end
if tires == 1 then tiresprice = 200 
elseif tires == 2 then tiresprice = 800
elseif tires == 3 then tiresprice = 1200
else tiresprice = 0
end
if tran == 1 then tranprice = 200 
elseif tran == 2 then tranprice = 800
elseif tran == 3 then tranprice = 1200
else tranprice = 0
end




if buycount == 1 and intake > intakeopt then 
money = money - intakeprice
saveintake()
end
if buycount == 1 and exhaust > exhaustopt then 
money = money - exhaustprice
saveexhaust()
end
if buycount == 1 and cpu > cpuopt then 
money = money - cpuprice
savecpu()
end
if buycount == 1 and turbo > turboopt then 
money = money - turboprice
saveturbo()
end
if buycount == 1 and nos == 1 then 
money = money - nosprice
nosremain = 100
savenos()
end
if buycount == 1 and tires > tiresopt then 
money = money - tiresprice
savetires()
end
if buycount == 1 and tran > tranopt then 
money = money - tranprice
savetran()
end

total = intakeprice + exhaustprice + cpuprice + turboprice + nosprice + tiresprice +tranprice

if buycount == 1 and nosremain < 100 then   
  intake = 0
  exhaust = 0
  cpu = 0
  turbo = 0
  nos = 0
  tires = 0
  tran = 0
  savemoney()
  loadparts()
end
if buycount == 1 and nosremain == 100 then 
  intake = 0
  exhaust = 0
  cpu = 0
  turbo = 0
  nos = 0
  tires = 0
  tran = 0
  savemoney()
  loadparts()
end

if nocount == 1 then
  no:play()
end


if loadopt == 1 then 
loadparts()
end

loading()
shopcontrols()
screen:clear()
screen:blit(0, 0, engine)
----
screen:print(10, 20, "Intake", incolor)
screen:print(10, 30, intake, red)
screen:print(90, 30, "$", red)
screen:print(100, 30, intakeprice, red)
---
screen:print(10, 50, "Exhaust", excolor)
screen:print(10, 60, exhaust, red)
screen:print(90, 60, "$", red)
screen:print(100, 60, exhaustprice, red)
----
screen:print(10, 80, "CPU", cpucolor)
screen:print(10, 90, cpu, red)
screen:print(90, 90, "$", red)
screen:print(100, 90, cpuprice, red)
----
screen:print(10, 110, "Turbo", turbocolor)
screen:print(10, 120, turbo, red)
screen:print(90, 120, "$", red)
screen:print(100, 120, turboprice, red)
----
screen:print(10, 140, "Nos", noscolor)
screen:print(10, 150, shopnos, red)
screen:print(90, 150, "$", red)
screen:print(100, 150, nosprice, red)
----
screen:print(10, 170, "Tires", tirecolor)
screen:print(10, 180, tires, red)
screen:print(90, 180, "$", red)
screen:print(100, 180, tiresprice, red)
----
screen:print(10, 200, "Transmission", trancolor)
screen:print(10, 210, tran, red)
screen:print(90, 210, "$", red)
screen:print(100, 210, tranprice, red)
----
screen:print(10, 240, "Total", red)
screen:print(90, 240, "$", red)
screen:print(100, 240, total, red)
----
screen:print(200, 240, "Purchase", buycolor)
----
screen:print(203, 260, "Go Back", backcolor)
----
screen:print(385, 20, "Money", red)
screen:print(382, 30, "$", red)
screen:print(390, 30, money, red)
----
screen:print(140, 100, line1, yellow)
screen:print(140, 110, line2, yellow)
screen:print(140, 120, line3, yellow)
screen:print(140, 130, line4, yellow)
screen:print(140, 140, line5, yellow)
----
screen:print(140, 170, "You Have Installed:", yellow)
screen:print(140, 180, "Intake", yellow)
screen:print(140, 190, intakeopt, yellow)
screen:print(195, 180, "Exhaust", yellow)
screen:print(195, 190, exhaustopt, yellow)
screen:print(255, 180, "CPU", yellow)
screen:print(255, 190, cpuopt, yellow)
screen:print(285, 180, "Turbo", yellow)
screen:print(285, 190, turboopt, yellow)
screen:print(330, 180, "NOS", yellow)
screen:print(330, 190, nosremain, yellow)
screen:print(360, 180, "Tires", yellow)
screen:print(360, 190, tiresopt, yellow)
screen:print(410, 180, "Tranny", yellow)
screen:print(410, 190, tranopt, yellow)
----
screen:flip()
screen.waitVblankStart()
end
