Advertisement
AquaJD

Dash

Aug 24th, 2017
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.65 KB | None | 0 0
  1. --[[
  2.  
  3.     Dash
  4.     By Dave-ee Jones
  5.  
  6. --]]
  7.  
  8. -- Don't change these (except for TRANSMISSION)
  9. local b_RUNNING = true -- We are running (not the car..)
  10. local b_ACCELERATING = false -- We aren't accelerating
  11. local b_BRAKING = false -- We aren't braking
  12. local b_CLUTCHIN = false -- Clutch isn't in
  13. local b_STALLED = true -- The car is off when the program starts
  14. local b_STARTING = false -- Not currently starting the car..
  15. local b_TRANSMISSION = "manual" -- We're in a manual
  16.  
  17. -- Keys, change to what you want but this makes it feel like pedals
  18. local k_ACCELERATOR = keys.rightAlt -- Press to emulate acceleration
  19. local k_BRAKE = keys.space -- Press to emulate braking
  20. local k_CLUTCH = keys.leftCtrl -- Press to emulate clutch
  21. local k_GEAR_UP = keys.up -- Go up a gear
  22. local k_GEAR_DOWN = keys.down -- Go down a gear
  23. local k_GEAR_N = keys.n -- Neutral gear
  24. local k_GEAR_R = keys.m -- Reverse gear
  25.  
  26. -- You can change these, but don't change the current RPMs
  27. local n_RPM = 0 -- Current RPM
  28. local n_RPM_MAX = 7000 -- Max RPM (way past redline..)
  29. local n_RPM_UPRATE = 100 -- Rate your RPMs rise while accelerating (base)
  30. local n_RPM_DOWNRATE_IDLING = 90 -- Rate your RPMs drop while idle (base)
  31. local n_RPM_DOWNRATE_BRAKING = 110 -- Rate your RPMs drop while braking (base)
  32. local n_GEAR = "n" -- Current gear
  33.  
  34. local n_TIMER_INTERVAL = 0.01 -- Time til next update
  35.  
  36. local t_GRATIOS = {
  37.     3.00, -- 1st gear
  38.     2.10, -- 2nd gear
  39.     1.40, -- 3rd gear
  40.     1.00, -- 4th gear
  41.     0.85, -- 5th gear
  42.     0.55, -- 6th gear
  43.     ["n"] = 1.00, -- Neutral (1.00 so that RPMs aren't affected by it)
  44.     ["r"] = 2.8 -- Reverse
  45. }
  46.  
  47. local w, h = term.getSize()
  48.  
  49. local function printCentered(s)
  50.     local x, y = term.getCursorPos()
  51.     local x = math.max(math.floor((w/2) - (#s / 2)), y)
  52.     term.setCursorPos(x+1,y)
  53.     print(s)
  54. end
  55.  
  56. local function drawGear(s)
  57.     local startX = 37
  58.     local startY = 11
  59.     term.setTextColor(colors.lightGray)
  60.     term.setBackgroundColor(colors.white)
  61.     term.setCursorPos(startX,startY)
  62.     print("R")
  63.     term.setCursorPos(startX,startY+1)
  64.     print("   N   ")
  65.     term.setCursorPos(startX+2,startY)
  66.     print("1")
  67.     term.setCursorPos(startX+2,startY+1)
  68.     print(" ")
  69.     term.setCursorPos(startX+2,startY+2)
  70.     print("2")
  71.     term.setCursorPos(startX+4,startY)
  72.     print("3")
  73.     term.setCursorPos(startX+4,startY+1)
  74.     print(" ")
  75.     term.setCursorPos(startX+4,startY+2)
  76.     print("4")
  77.     term.setCursorPos(startX+6,startY)
  78.     print("5")
  79.     term.setCursorPos(startX+6,startY+1)
  80.     print(" ")
  81.     term.setCursorPos(startX+6,startY+2)
  82.     print("6")
  83.     term.setBackgroundColor(colors.pink)
  84.     term.setTextColor(colors.gray)
  85.     if s == 1 then
  86.         term.setCursorPos(startX+2,startY)
  87.         print("1")
  88.     elseif s == 2 then
  89.         term.setCursorPos(startX+2,startY+2)
  90.         print("2")
  91.     elseif s == 3 then
  92.         term.setCursorPos(startX+4,startY)
  93.         print("3")
  94.     elseif s == 4 then
  95.         term.setCursorPos(startX+4,startY+2)
  96.         print("4")
  97.     elseif s == 5 then
  98.         term.setCursorPos(startX+6,startY)
  99.         print("5")
  100.     elseif s == 6 then
  101.         term.setCursorPos(startX+6,startY+2)
  102.         print("6")
  103.     elseif s == "r" then
  104.         term.setCursorPos(startX,startY)
  105.         print("R")
  106.     elseif s == "n" then
  107.         term.setCursorPos(startX+3,startY+1)
  108.         print("N")
  109.     end
  110.    
  111. end
  112.  
  113. local function drawTachoLine(_bar,_line)
  114.     term.setTextColor(colors.pink)
  115.     term.setCursorPos(4+(_bar*5)+(_line-1),2)
  116.     if _bar == 1 or _bar == 3 or _bar == 5 then
  117.         term.setBackgroundColor(colors.lightGray)
  118.     elseif _bar == 2 or _bar == 4 then
  119.         term.setBackgroundColor(colors.gray)
  120.     elseif _bar == 6 then
  121.         term.setBackgroundColor(colors.orange)
  122.     elseif _bar == 7 then
  123.         term.setBackgroundColor(colors.red)
  124.     end
  125.     for i=1,7 do
  126.         print("|")
  127.         term.setCursorPos(4+(_bar*5)+(_line-1),2+i)
  128.     end
  129. end
  130.  
  131. local function calculateTachoLine()
  132.     if n_RPM >= 0 and n_RPM <= 200 then
  133.         -- First bar, first line
  134.         drawTachoLine(1,1)
  135.     elseif n_RPM >= 201 and n_RPM <= 400 then
  136.         -- First bar, second line
  137.         drawTachoLine(1,2)
  138.     elseif n_RPM >= 401 and n_RPM <= 600 then
  139.         -- First bar, third line
  140.         drawTachoLine(1,3)
  141.     elseif n_RPM >= 601 and n_RPM <= 800 then
  142.         -- First bar, fourth line
  143.         drawTachoLine(1,4)
  144.     elseif n_RPM >= 801 and n_RPM <= 1000 then
  145.         -- First bar, fifth line
  146.         drawTachoLine(1,5)
  147.     elseif n_RPM >= 1001 and n_RPM <= 1200 then
  148.         -- Second bar, first line
  149.         drawTachoLine(2,1)
  150.     elseif n_RPM >= 1201 and n_RPM <= 1400 then
  151.         -- Second bar, second line
  152.         drawTachoLine(2,2)
  153.     elseif n_RPM >= 1401 and n_RPM <= 1600 then
  154.         -- Second bar, third line
  155.         drawTachoLine(2,3)
  156.     elseif n_RPM >= 1601 and n_RPM <= 1800 then
  157.         -- Second bar, fourth line
  158.         drawTachoLine(2,4)
  159.     elseif n_RPM >= 1801 and n_RPM <= 2000 then
  160.         -- Second bar, fifth line
  161.         drawTachoLine(2,5)
  162.     elseif n_RPM >= 2001 and n_RPM <= 2200 then
  163.         -- Third bar, first line
  164.         drawTachoLine(3,1)
  165.     elseif n_RPM >= 2201 and n_RPM <= 2400 then
  166.         -- Third bar, second line
  167.         drawTachoLine(3,2)
  168.     elseif n_RPM >= 2401 and n_RPM <= 2600 then
  169.         -- Third bar, third line
  170.         drawTachoLine(3,3)
  171.     elseif n_RPM >= 2601 and n_RPM <= 2800 then
  172.         -- Third bar, fourth line
  173.         drawTachoLine(3,4)
  174.     elseif n_RPM >= 2801 and n_RPM <= 3000 then
  175.         -- Third bar, fifth line
  176.         drawTachoLine(3,5)
  177.     elseif n_RPM >= 3001 and n_RPM <= 3200 then
  178.         -- Fourth bar, first line
  179.         drawTachoLine(4,1)
  180.     elseif n_RPM >= 3201 and n_RPM <= 3400 then
  181.         -- Fourth bar, second line
  182.         drawTachoLine(4,2)
  183.     elseif n_RPM >= 3401 and n_RPM <= 3600 then
  184.         -- Fourth bar, third line
  185.         drawTachoLine(4,3)
  186.     elseif n_RPM >= 3601 and n_RPM <= 3800 then
  187.         -- Fourth bar, fourth line
  188.         drawTachoLine(4,4)
  189.     elseif n_RPM >= 3801 and n_RPM <= 4000 then
  190.         -- Fourth bar, fifth line
  191.         drawTachoLine(4,5)
  192.     elseif n_RPM >= 4001 and n_RPM <= 4200 then
  193.         -- Fifth bar, first line
  194.         drawTachoLine(5,1)
  195.     elseif n_RPM >= 4201 and n_RPM <= 4400 then
  196.         -- Fifth bar, second line
  197.         drawTachoLine(5,2)
  198.     elseif n_RPM >= 4401 and n_RPM <= 4600 then
  199.         -- Fifth bar, third line
  200.         drawTachoLine(5,3)
  201.     elseif n_RPM >= 4601 and n_RPM <= 4800 then
  202.         -- Fifth bar, fourth line
  203.         drawTachoLine(5,4)
  204.     elseif n_RPM >= 4801 and n_RPM <= 5000 then
  205.         -- Fifth bar, fifth line
  206.         drawTachoLine(5,5)
  207.     elseif n_RPM >= 501 and n_RPM <= 5200 then
  208.         -- Sixth bar, first line
  209.         drawTachoLine(6,1)
  210.     elseif n_RPM >= 5201 and n_RPM <= 5400 then
  211.         -- Sixth bar, second line
  212.         drawTachoLine(6,2)
  213.     elseif n_RPM >= 5401 and n_RPM <= 5600 then
  214.         -- Sixth bar, third line
  215.         drawTachoLine(6,3)
  216.     elseif n_RPM >= 5601 and n_RPM <= 5800 then
  217.         -- Sixth bar, fourth line
  218.         drawTachoLine(6,4)
  219.     elseif n_RPM >= 5801 and n_RPM <= 6000 then
  220.         -- Sixth bar, fifth line
  221.         drawTachoLine(6,5)
  222.     elseif n_RPM >= 6001 and n_RPM <= 6200 then
  223.         -- Seventh bar, first line
  224.         drawTachoLine(7,1)
  225.     elseif n_RPM >= 6201 and n_RPM <= 6400 then
  226.         -- Seventh bar, second line
  227.         drawTachoLine(7,2)
  228.     elseif n_RPM >= 6401 and n_RPM <= 6600 then
  229.         -- Seventh bar, third line
  230.         drawTachoLine(7,3)
  231.     elseif n_RPM >= 6601 and n_RPM <= 6800 then
  232.         -- Seventh bar, fourth line
  233.         drawTachoLine(7,4)
  234.     elseif n_RPM >= 6801 and n_RPM <= 7000 then
  235.         -- Seventh bar, fifth line
  236.         drawTachoLine(7,5)
  237.     end
  238. end
  239.  
  240. local function drawGUI()
  241.     -- Draw RPM
  242.     term.setTextColor(colors.gray)
  243.     term.setBackgroundColor(colors.white)
  244.     term.setCursorPos(9,11)
  245.     for i=1,4 do
  246.         print("      ")
  247.         term.setCursorPos(9,11+i)
  248.     end
  249.     if n_RPM >= 10 and n_RPM <= 99 then
  250.         term.setCursorPos(11,12)
  251.     elseif n_RPM >= 100 then
  252.         term.setCursorPos(10,12)
  253.     end
  254.     if n_RPM > 9 then
  255.         print(tostring(n_RPM))
  256.     end
  257.     term.setTextColor(colors.lightGray)
  258.     term.setCursorPos(10,13)
  259.     print("REVS")
  260.    
  261.     -- Draw ignition
  262.     term.setCursorPos(30,11)
  263.     term.setTextColor(colors.lightGray)
  264.     if not b_STALLED then
  265.         term.setBackgroundColor(colors.green)
  266.         print(" - ")
  267.     elseif b_STALLED and b_STARTING then
  268.         term.setBackgroundColor(colors.yellow)
  269.         print(" / ")
  270.     elseif b_STALLED then
  271.         term.setBackgroundColor(colors.red)
  272.         print(" | ")
  273.     end
  274.    
  275.     -- Draw gear
  276.     drawGear(n_GEAR)
  277.    
  278.     term.setTextColor(colors.white)
  279.     -- Draw tachometer
  280.     for i=1,7 do
  281.         term.setCursorPos(4+(i*5),2)
  282.         if i == 1 or i == 3 or i == 5 then
  283.             term.setBackgroundColor(colors.lightGray)
  284.         elseif i == 2 or i == 4 then
  285.             term.setBackgroundColor(colors.gray)
  286.         elseif i == 6 then
  287.             term.setBackgroundColor(colors.orange)
  288.         elseif i == 7 then
  289.             term.setBackgroundColor(colors.red)
  290.         end
  291.         for k=1,6 do
  292.             print("     ")
  293.             term.setCursorPos(4+(i*5),2+k)
  294.         end
  295.         print((i-1).."    ")
  296.     end
  297.     calculateTachoLine()
  298.     term.setTextColor(colors.white)
  299.    
  300.     -- Draw clutch
  301.     if b_CLUTCHIN then
  302.         term.setBackgroundColor(colors.green)
  303.     else
  304.         term.setBackgroundColor(colors.red)
  305.     end
  306.     term.setCursorPos(7,16)
  307.     print("        ")
  308.     term.setCursorPos(7,17)
  309.     print(" Clutch ")
  310.     term.setCursorPos(7,18)
  311.     print("        ")
  312.    
  313.     -- Draw brake
  314.     if b_BRAKING then
  315.         term.setBackgroundColor(colors.green)
  316.     else
  317.         term.setBackgroundColor(colors.red)
  318.     end
  319.     term.setCursorPos(18,16)
  320.     print("           ")
  321.     term.setCursorPos(18,17)
  322.     print("   Brake   ")
  323.     term.setCursorPos(18,18)
  324.     print("           ")
  325.    
  326.     -- Draw accelerator
  327.     if b_ACCELERATING then
  328.         term.setBackgroundColor(colors.green)
  329.     else
  330.         term.setBackgroundColor(colors.red)
  331.     end
  332.     term.setCursorPos(32,16)
  333.     print("             ")
  334.     term.setCursorPos(32,17)
  335.     print(" Accelerator ")
  336.     term.setCursorPos(32,18)
  337.     print("             ")
  338.    
  339.     term.setBackgroundColor(colors.black)
  340. end
  341.  
  342. term.clear()
  343. term.setBackgroundColor(colors.white)
  344. term.setCursorPos(9,9)
  345. for i=1,35 do
  346.     write(" ")
  347. end
  348. term.setTextColor(colors.lightGray)
  349. term.setCursorPos(9,9)
  350. printCentered("( x1000 RPM )")
  351. local n_TIMER_UPDATE = os.startTimer(n_TIMER_INTERVAL)
  352. local n_TIMER_IGNITION = nil
  353. while b_RUNNING do
  354.     local _event, _id, _x, _y = os.pullEvent()
  355.     local _n_RPM = 0
  356.     if _event == "timer" and _id == n_TIMER_UPDATE then
  357.         -- Accelerating
  358.         if b_ACCELERATING then
  359.             if n_RPM < n_RPM_MAX then
  360.                 -- Is the clutch in? No.
  361.                 if not b_CLUTCHIN then
  362.                     _n_RPM = (n_RPM + (n_RPM_UPRATE * t_GRATIOS[n_GEAR]))
  363.                 -- Is the clutch in? Yes.
  364.                 else
  365.                     _n_RPM = (n_RPM + (n_RPM_UPRATE * t_GRATIOS[1]))
  366.                 end
  367.                 if _n_RPM > n_RPM_MAX then
  368.                     n_RPM = n_RPM_MAX
  369.                 else
  370.                     n_RPM = _n_RPM
  371.                 end
  372.             end
  373.         -- Decelerating (braking/idling)
  374.         else
  375.             if n_RPM > 0 then
  376.                 -- Are you braking? No.
  377.                 if not b_BRAKING then
  378.                     _n_RPM = (n_RPM - n_RPM_DOWNRATE_IDLING)
  379.                     if _n_RPM < 0 then
  380.                         n_RPM = 0
  381.                     else
  382.                         n_RPM = _n_RPM
  383.                     end
  384.                 -- Are you braking? Yes.
  385.                 else
  386.                     _n_RPM = (n_RPM - n_RPM_DOWNRATE_BRAKING)
  387.                     if _n_RPM < 0 then
  388.                         n_RPM = 0
  389.                     else
  390.                         n_RPM = _n_RPM
  391.                     end
  392.                 end
  393.             end
  394.         end
  395.         if n_RPM <= 10 and not b_CLUTCHIN then
  396.             if n_GEAR ~= "n" then
  397.                 b_STALLED = true -- Oh dear, we've stalled
  398.             end
  399.         elseif n_RPM <= 500 then
  400.             if not b_CLUTCHIN then
  401.                 if n_GEAR ~= "n" then
  402.                     if n_GEAR == "r" or n_GEAR >= 3 then
  403.                         b_STALLED = true -- Oh dear, we've stalled
  404.                     end
  405.                 end
  406.             end
  407.         end
  408.         if b_STALLED then
  409.             n_RPM = 0
  410.         end
  411.         n_TIMER_UPDATE = os.startTimer(n_TIMER_INTERVAL)
  412.         drawGUI()
  413.     elseif _event == "timer" and _id == n_TIMER_IGNITION then
  414.         if b_CLUTCHIN and b_STARTING then
  415.             b_STALLED = false
  416.             b_STARTING = false
  417.         end
  418.     elseif _event == "key" then
  419.         if _id == k_ACCELERATOR and not b_STALLED then
  420.             b_ACCELERATING = true -- Started accelerating
  421.         elseif _id == k_BRAKE and not b_STALLED then
  422.             b_BRAKING = true -- Started braking
  423.         elseif _id == k_CLUTCH then
  424.             b_CLUTCHIN = true -- Clutch is in
  425.         end
  426.     elseif _event == "key_up" then
  427.         if _id == k_ACCELERATOR then
  428.             b_ACCELERATING = false -- Stopped accelerating
  429.         elseif _id == k_BRAKE then
  430.             b_BRAKING = false -- Stopped braking
  431.         elseif _id == k_CLUTCH then
  432.             b_CLUTCHIN = false -- Clutch is out
  433.         elseif _id == k_GEAR_UP then
  434.             if b_CLUTCHIN and n_GEAR ~= "n" and n_GEAR ~= "r" and n_GEAR < 6 then
  435.                 n_GEAR = n_GEAR + 1
  436.                 _n_REVS = t_GRATIOS[n_GEAR] * 1000
  437.                 n_REVS = _n_REVS
  438.             elseif b_CLUTCHIN and n_GEAR == "n" or n_GEAR == "r" then
  439.                 n_GEAR = 1 
  440.             end
  441.         elseif _id == k_GEAR_DOWN then
  442.             if b_CLUTCHIN and n_GEAR ~= "n" and n_GEAR ~= "r" and n_GEAR > 1 then
  443.                 n_GEAR = n_GEAR - 1
  444.                 _n_REVS = t_GRATIOS[n_GEAR] * 1000
  445.                 n_REVS = _n_REVS
  446.             elseif b_CLUTCHIN and n_GEAR == "n" or n_GEAR == "r" then
  447.                 n_GEAR = 1
  448.             end
  449.         elseif _id == k_GEAR_N then
  450.             if b_CLUTCHIN then
  451.                 n_GEAR = "n"
  452.             end
  453.         elseif _id == k_GEAR_R then
  454.             if b_CLUTCHIN then
  455.                 n_GEAR = "r"
  456.             end
  457.         end
  458.     elseif _event == "mouse_click" then
  459.         if _id == 1 and _x >= 30 and _x <= 32 and _y == 11 then
  460.             if b_CLUTCHIN then
  461.                 b_STARTING = true
  462.                 n_TIMER_IGNITION = os.startTimer(1)
  463.             end
  464.         end
  465.     elseif _event == "mouse_up" then
  466.         if _id == 1 and _x >= 30 and _x <= 32 and _y == 11 then
  467.             b_STARTING = false
  468.         end
  469.     end
  470. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement