Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Dash
- By Dave-ee Jones
- --]]
- -- Don't change these (except for TRANSMISSION)
- local b_RUNNING = true -- We are running (not the car..)
- local b_ACCELERATING = false -- We aren't accelerating
- local b_BRAKING = false -- We aren't braking
- local b_CLUTCHIN = false -- Clutch isn't in
- local b_STALLED = true -- The car is off when the program starts
- local b_STARTING = false -- Not currently starting the car..
- local b_TRANSMISSION = "manual" -- We're in a manual
- -- Keys, change to what you want but this makes it feel like pedals
- local k_ACCELERATOR = keys.rightAlt -- Press to emulate acceleration
- local k_BRAKE = keys.space -- Press to emulate braking
- local k_CLUTCH = keys.leftCtrl -- Press to emulate clutch
- local k_GEAR_UP = keys.up -- Go up a gear
- local k_GEAR_DOWN = keys.down -- Go down a gear
- local k_GEAR_N = keys.n -- Neutral gear
- local k_GEAR_R = keys.m -- Reverse gear
- -- You can change these, but don't change the current RPMs
- local n_RPM = 0 -- Current RPM
- local n_RPM_MAX = 7000 -- Max RPM (way past redline..)
- local n_RPM_UPRATE = 100 -- Rate your RPMs rise while accelerating (base)
- local n_RPM_DOWNRATE_IDLING = 90 -- Rate your RPMs drop while idle (base)
- local n_RPM_DOWNRATE_BRAKING = 110 -- Rate your RPMs drop while braking (base)
- local n_GEAR = "n" -- Current gear
- local n_TIMER_INTERVAL = 0.01 -- Time til next update
- local t_GRATIOS = {
- 3.00, -- 1st gear
- 2.10, -- 2nd gear
- 1.40, -- 3rd gear
- 1.00, -- 4th gear
- 0.85, -- 5th gear
- 0.55, -- 6th gear
- ["n"] = 1.00, -- Neutral (1.00 so that RPMs aren't affected by it)
- ["r"] = 2.8 -- Reverse
- }
- local w, h = term.getSize()
- local function printCentered(s)
- local x, y = term.getCursorPos()
- local x = math.max(math.floor((w/2) - (#s / 2)), y)
- term.setCursorPos(x+1,y)
- print(s)
- end
- local function drawGear(s)
- local startX = 37
- local startY = 11
- term.setTextColor(colors.lightGray)
- term.setBackgroundColor(colors.white)
- term.setCursorPos(startX,startY)
- print("R")
- term.setCursorPos(startX,startY+1)
- print(" N ")
- term.setCursorPos(startX+2,startY)
- print("1")
- term.setCursorPos(startX+2,startY+1)
- print(" ")
- term.setCursorPos(startX+2,startY+2)
- print("2")
- term.setCursorPos(startX+4,startY)
- print("3")
- term.setCursorPos(startX+4,startY+1)
- print(" ")
- term.setCursorPos(startX+4,startY+2)
- print("4")
- term.setCursorPos(startX+6,startY)
- print("5")
- term.setCursorPos(startX+6,startY+1)
- print(" ")
- term.setCursorPos(startX+6,startY+2)
- print("6")
- term.setBackgroundColor(colors.pink)
- term.setTextColor(colors.gray)
- if s == 1 then
- term.setCursorPos(startX+2,startY)
- print("1")
- elseif s == 2 then
- term.setCursorPos(startX+2,startY+2)
- print("2")
- elseif s == 3 then
- term.setCursorPos(startX+4,startY)
- print("3")
- elseif s == 4 then
- term.setCursorPos(startX+4,startY+2)
- print("4")
- elseif s == 5 then
- term.setCursorPos(startX+6,startY)
- print("5")
- elseif s == 6 then
- term.setCursorPos(startX+6,startY+2)
- print("6")
- elseif s == "r" then
- term.setCursorPos(startX,startY)
- print("R")
- elseif s == "n" then
- term.setCursorPos(startX+3,startY+1)
- print("N")
- end
- end
- local function drawTachoLine(_bar,_line)
- term.setTextColor(colors.pink)
- term.setCursorPos(4+(_bar*5)+(_line-1),2)
- if _bar == 1 or _bar == 3 or _bar == 5 then
- term.setBackgroundColor(colors.lightGray)
- elseif _bar == 2 or _bar == 4 then
- term.setBackgroundColor(colors.gray)
- elseif _bar == 6 then
- term.setBackgroundColor(colors.orange)
- elseif _bar == 7 then
- term.setBackgroundColor(colors.red)
- end
- for i=1,7 do
- print("|")
- term.setCursorPos(4+(_bar*5)+(_line-1),2+i)
- end
- end
- local function calculateTachoLine()
- if n_RPM >= 0 and n_RPM <= 200 then
- -- First bar, first line
- drawTachoLine(1,1)
- elseif n_RPM >= 201 and n_RPM <= 400 then
- -- First bar, second line
- drawTachoLine(1,2)
- elseif n_RPM >= 401 and n_RPM <= 600 then
- -- First bar, third line
- drawTachoLine(1,3)
- elseif n_RPM >= 601 and n_RPM <= 800 then
- -- First bar, fourth line
- drawTachoLine(1,4)
- elseif n_RPM >= 801 and n_RPM <= 1000 then
- -- First bar, fifth line
- drawTachoLine(1,5)
- elseif n_RPM >= 1001 and n_RPM <= 1200 then
- -- Second bar, first line
- drawTachoLine(2,1)
- elseif n_RPM >= 1201 and n_RPM <= 1400 then
- -- Second bar, second line
- drawTachoLine(2,2)
- elseif n_RPM >= 1401 and n_RPM <= 1600 then
- -- Second bar, third line
- drawTachoLine(2,3)
- elseif n_RPM >= 1601 and n_RPM <= 1800 then
- -- Second bar, fourth line
- drawTachoLine(2,4)
- elseif n_RPM >= 1801 and n_RPM <= 2000 then
- -- Second bar, fifth line
- drawTachoLine(2,5)
- elseif n_RPM >= 2001 and n_RPM <= 2200 then
- -- Third bar, first line
- drawTachoLine(3,1)
- elseif n_RPM >= 2201 and n_RPM <= 2400 then
- -- Third bar, second line
- drawTachoLine(3,2)
- elseif n_RPM >= 2401 and n_RPM <= 2600 then
- -- Third bar, third line
- drawTachoLine(3,3)
- elseif n_RPM >= 2601 and n_RPM <= 2800 then
- -- Third bar, fourth line
- drawTachoLine(3,4)
- elseif n_RPM >= 2801 and n_RPM <= 3000 then
- -- Third bar, fifth line
- drawTachoLine(3,5)
- elseif n_RPM >= 3001 and n_RPM <= 3200 then
- -- Fourth bar, first line
- drawTachoLine(4,1)
- elseif n_RPM >= 3201 and n_RPM <= 3400 then
- -- Fourth bar, second line
- drawTachoLine(4,2)
- elseif n_RPM >= 3401 and n_RPM <= 3600 then
- -- Fourth bar, third line
- drawTachoLine(4,3)
- elseif n_RPM >= 3601 and n_RPM <= 3800 then
- -- Fourth bar, fourth line
- drawTachoLine(4,4)
- elseif n_RPM >= 3801 and n_RPM <= 4000 then
- -- Fourth bar, fifth line
- drawTachoLine(4,5)
- elseif n_RPM >= 4001 and n_RPM <= 4200 then
- -- Fifth bar, first line
- drawTachoLine(5,1)
- elseif n_RPM >= 4201 and n_RPM <= 4400 then
- -- Fifth bar, second line
- drawTachoLine(5,2)
- elseif n_RPM >= 4401 and n_RPM <= 4600 then
- -- Fifth bar, third line
- drawTachoLine(5,3)
- elseif n_RPM >= 4601 and n_RPM <= 4800 then
- -- Fifth bar, fourth line
- drawTachoLine(5,4)
- elseif n_RPM >= 4801 and n_RPM <= 5000 then
- -- Fifth bar, fifth line
- drawTachoLine(5,5)
- elseif n_RPM >= 501 and n_RPM <= 5200 then
- -- Sixth bar, first line
- drawTachoLine(6,1)
- elseif n_RPM >= 5201 and n_RPM <= 5400 then
- -- Sixth bar, second line
- drawTachoLine(6,2)
- elseif n_RPM >= 5401 and n_RPM <= 5600 then
- -- Sixth bar, third line
- drawTachoLine(6,3)
- elseif n_RPM >= 5601 and n_RPM <= 5800 then
- -- Sixth bar, fourth line
- drawTachoLine(6,4)
- elseif n_RPM >= 5801 and n_RPM <= 6000 then
- -- Sixth bar, fifth line
- drawTachoLine(6,5)
- elseif n_RPM >= 6001 and n_RPM <= 6200 then
- -- Seventh bar, first line
- drawTachoLine(7,1)
- elseif n_RPM >= 6201 and n_RPM <= 6400 then
- -- Seventh bar, second line
- drawTachoLine(7,2)
- elseif n_RPM >= 6401 and n_RPM <= 6600 then
- -- Seventh bar, third line
- drawTachoLine(7,3)
- elseif n_RPM >= 6601 and n_RPM <= 6800 then
- -- Seventh bar, fourth line
- drawTachoLine(7,4)
- elseif n_RPM >= 6801 and n_RPM <= 7000 then
- -- Seventh bar, fifth line
- drawTachoLine(7,5)
- end
- end
- local function drawGUI()
- -- Draw RPM
- term.setTextColor(colors.gray)
- term.setBackgroundColor(colors.white)
- term.setCursorPos(9,11)
- for i=1,4 do
- print(" ")
- term.setCursorPos(9,11+i)
- end
- if n_RPM >= 10 and n_RPM <= 99 then
- term.setCursorPos(11,12)
- elseif n_RPM >= 100 then
- term.setCursorPos(10,12)
- end
- if n_RPM > 9 then
- print(tostring(n_RPM))
- end
- term.setTextColor(colors.lightGray)
- term.setCursorPos(10,13)
- print("REVS")
- -- Draw ignition
- term.setCursorPos(30,11)
- term.setTextColor(colors.lightGray)
- if not b_STALLED then
- term.setBackgroundColor(colors.green)
- print(" - ")
- elseif b_STALLED and b_STARTING then
- term.setBackgroundColor(colors.yellow)
- print(" / ")
- elseif b_STALLED then
- term.setBackgroundColor(colors.red)
- print(" | ")
- end
- -- Draw gear
- drawGear(n_GEAR)
- term.setTextColor(colors.white)
- -- Draw tachometer
- for i=1,7 do
- term.setCursorPos(4+(i*5),2)
- if i == 1 or i == 3 or i == 5 then
- term.setBackgroundColor(colors.lightGray)
- elseif i == 2 or i == 4 then
- term.setBackgroundColor(colors.gray)
- elseif i == 6 then
- term.setBackgroundColor(colors.orange)
- elseif i == 7 then
- term.setBackgroundColor(colors.red)
- end
- for k=1,6 do
- print(" ")
- term.setCursorPos(4+(i*5),2+k)
- end
- print((i-1).." ")
- end
- calculateTachoLine()
- term.setTextColor(colors.white)
- -- Draw clutch
- if b_CLUTCHIN then
- term.setBackgroundColor(colors.green)
- else
- term.setBackgroundColor(colors.red)
- end
- term.setCursorPos(7,16)
- print(" ")
- term.setCursorPos(7,17)
- print(" Clutch ")
- term.setCursorPos(7,18)
- print(" ")
- -- Draw brake
- if b_BRAKING then
- term.setBackgroundColor(colors.green)
- else
- term.setBackgroundColor(colors.red)
- end
- term.setCursorPos(18,16)
- print(" ")
- term.setCursorPos(18,17)
- print(" Brake ")
- term.setCursorPos(18,18)
- print(" ")
- -- Draw accelerator
- if b_ACCELERATING then
- term.setBackgroundColor(colors.green)
- else
- term.setBackgroundColor(colors.red)
- end
- term.setCursorPos(32,16)
- print(" ")
- term.setCursorPos(32,17)
- print(" Accelerator ")
- term.setCursorPos(32,18)
- print(" ")
- term.setBackgroundColor(colors.black)
- end
- term.clear()
- term.setBackgroundColor(colors.white)
- term.setCursorPos(9,9)
- for i=1,35 do
- write(" ")
- end
- term.setTextColor(colors.lightGray)
- term.setCursorPos(9,9)
- printCentered("( x1000 RPM )")
- local n_TIMER_UPDATE = os.startTimer(n_TIMER_INTERVAL)
- local n_TIMER_IGNITION = nil
- while b_RUNNING do
- local _event, _id, _x, _y = os.pullEvent()
- local _n_RPM = 0
- if _event == "timer" and _id == n_TIMER_UPDATE then
- -- Accelerating
- if b_ACCELERATING then
- if n_RPM < n_RPM_MAX then
- -- Is the clutch in? No.
- if not b_CLUTCHIN then
- _n_RPM = (n_RPM + (n_RPM_UPRATE * t_GRATIOS[n_GEAR]))
- -- Is the clutch in? Yes.
- else
- _n_RPM = (n_RPM + (n_RPM_UPRATE * t_GRATIOS[1]))
- end
- if _n_RPM > n_RPM_MAX then
- n_RPM = n_RPM_MAX
- else
- n_RPM = _n_RPM
- end
- end
- -- Decelerating (braking/idling)
- else
- if n_RPM > 0 then
- -- Are you braking? No.
- if not b_BRAKING then
- _n_RPM = (n_RPM - n_RPM_DOWNRATE_IDLING)
- if _n_RPM < 0 then
- n_RPM = 0
- else
- n_RPM = _n_RPM
- end
- -- Are you braking? Yes.
- else
- _n_RPM = (n_RPM - n_RPM_DOWNRATE_BRAKING)
- if _n_RPM < 0 then
- n_RPM = 0
- else
- n_RPM = _n_RPM
- end
- end
- end
- end
- if n_RPM <= 10 and not b_CLUTCHIN then
- if n_GEAR ~= "n" then
- b_STALLED = true -- Oh dear, we've stalled
- end
- elseif n_RPM <= 500 then
- if not b_CLUTCHIN then
- if n_GEAR ~= "n" then
- if n_GEAR == "r" or n_GEAR >= 3 then
- b_STALLED = true -- Oh dear, we've stalled
- end
- end
- end
- end
- if b_STALLED then
- n_RPM = 0
- end
- n_TIMER_UPDATE = os.startTimer(n_TIMER_INTERVAL)
- drawGUI()
- elseif _event == "timer" and _id == n_TIMER_IGNITION then
- if b_CLUTCHIN and b_STARTING then
- b_STALLED = false
- b_STARTING = false
- end
- elseif _event == "key" then
- if _id == k_ACCELERATOR and not b_STALLED then
- b_ACCELERATING = true -- Started accelerating
- elseif _id == k_BRAKE and not b_STALLED then
- b_BRAKING = true -- Started braking
- elseif _id == k_CLUTCH then
- b_CLUTCHIN = true -- Clutch is in
- end
- elseif _event == "key_up" then
- if _id == k_ACCELERATOR then
- b_ACCELERATING = false -- Stopped accelerating
- elseif _id == k_BRAKE then
- b_BRAKING = false -- Stopped braking
- elseif _id == k_CLUTCH then
- b_CLUTCHIN = false -- Clutch is out
- elseif _id == k_GEAR_UP then
- if b_CLUTCHIN and n_GEAR ~= "n" and n_GEAR ~= "r" and n_GEAR < 6 then
- n_GEAR = n_GEAR + 1
- _n_REVS = t_GRATIOS[n_GEAR] * 1000
- n_REVS = _n_REVS
- elseif b_CLUTCHIN and n_GEAR == "n" or n_GEAR == "r" then
- n_GEAR = 1
- end
- elseif _id == k_GEAR_DOWN then
- if b_CLUTCHIN and n_GEAR ~= "n" and n_GEAR ~= "r" and n_GEAR > 1 then
- n_GEAR = n_GEAR - 1
- _n_REVS = t_GRATIOS[n_GEAR] * 1000
- n_REVS = _n_REVS
- elseif b_CLUTCHIN and n_GEAR == "n" or n_GEAR == "r" then
- n_GEAR = 1
- end
- elseif _id == k_GEAR_N then
- if b_CLUTCHIN then
- n_GEAR = "n"
- end
- elseif _id == k_GEAR_R then
- if b_CLUTCHIN then
- n_GEAR = "r"
- end
- end
- elseif _event == "mouse_click" then
- if _id == 1 and _x >= 30 and _x <= 32 and _y == 11 then
- if b_CLUTCHIN then
- b_STARTING = true
- n_TIMER_IGNITION = os.startTimer(1)
- end
- end
- elseif _event == "mouse_up" then
- if _id == 1 and _x >= 30 and _x <= 32 and _y == 11 then
- b_STARTING = false
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement