Communeguy

Stationmaster CG current version

Aug 7th, 2015
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.05 KB | None | 0 0
  1. --StationmasterCG by Communeguy
  2. --Purpose-rolled Train-Sorting Application
  3. --Depends on Railcraft for hardware.
  4. --Code documentation in comments.
  5. --Hardware documentation at pastebin: ##Code##
  6. --DevNotes commented at end.
  7.  
  8. --Begin Config Block. These are the only variables the user should manipulate!
  9. local sSide = "left" -- side with the Sensor Line attached.
  10. local rSide = "right" -- side with the Railcraft Line attached.
  11. local mSide = "monitor_0" -- side with the monitor attached, or address of the monitor if networked.
  12. local tSide = "bottom" -- Trigger sensor side
  13. local cSide = "top" -- side with the check receiver.
  14. local train1 = "Kobold Delver" -- name of first train.
  15. local train2 = "Eshu's Bounty"
  16. local train3 = "Hardscrabble Shuttle"
  17. local train4 = "Sidhe Heighliner"
  18.  
  19. --Additional Variable Defaults. Don't touch.
  20. local v = "Alpha 2"
  21. local a = 1
  22. local p1 = "Unknown"
  23. local p2 = "Unknown"
  24. local p3 = "Unknown"
  25. local p4 = "Unknown"
  26. local bypass = false
  27. local incoming = "string"
  28.  
  29. --Functions redefined for brevity and utility.
  30. local function set1() --Set functions trigger the switch tracks they are assigned to.
  31. rs.setBundledOutput(rSide, colors.combine(rs.getBundledOutput(rSide), colors.orange)) end
  32. local function set2()
  33. rs.setBundledOutput(rSide, colors.combine(rs.getBundledOutput(rSide), colors.lightBlue)) end
  34. local function set3()
  35. rs.setBundledOutput(rSide, colors.combine(rs.getBundledOutput(rSide), colors.lime)) end
  36. local function set4()
  37. rs.setBundledOutput(rSide, colors.combine(rs.getBundledOutput(rSide), colors.gray)) end
  38. local function sig1() --Sig functions check to see if there is something on the platform.
  39. return rs.testBundledInput(rSide, colors.white)
  40. end
  41. local function sig2()
  42. return rs.testBundledInput(rSide, colors.magenta)
  43. end
  44. local function sig3()
  45. return rs.testBundledInput(rSide, colors.yellow)
  46. end
  47. local function sig4()
  48. return rs.testBundledInput(rSide, colors.pink)
  49. end
  50. local function det1() -- Det functions are checking to see if any of the Routing Detectors tripped on the train, as part of IDing it.
  51. return rs.testBundledInput(sSide, colors.white)
  52. end
  53. local function det2()
  54. return rs.testBundledInput(sSide, colors.orange)
  55. end
  56. local function det3()
  57. return rs.testBundledInput(sSide, colors.magenta)
  58. end
  59. local function det4()
  60. return rs.testBundledInput(sSide, colors.lightBlue)
  61. end
  62. local function release() --releases the holding track
  63. rs.setBundledOutput(sSide, colors.yellow)
  64. end
  65. local function hold() -- holds the train to the holding track.
  66. rs.setBundledOutput(sSide,0)
  67. end
  68. local function check() -- checks that the signal blocks are correctly assigned.
  69. return rs.getInput(cSide)
  70. end
  71. local function switch() -- Tests the state of the signal blocks and handles their switches accordingly
  72.     rs.setBundledOutput(rSide, colors.black)
  73.     if sig1() == true then
  74.     set1()
  75.     end
  76.     if sig2() == true then
  77.     set2()
  78.     end
  79.     if sig3() == true then
  80.     set3()
  81.     end
  82.     if sig4() == true then
  83.     set4()
  84.     end
  85. end
  86. local function park() -- where's the train going?
  87.     if sig1() == true then
  88.         p1 = incoming
  89.         bypass = false
  90.     elseif sig2() == true then
  91.         p2 = incoming
  92.         bypass = false
  93.     elseif sig3() == true then
  94.         p3 = incoming
  95.         bypass = false
  96.     elseif sig4() == true then
  97.         p4 = incoming
  98.         bypass = false
  99.     else
  100.         bypass = true
  101.     end
  102. end
  103. local function checkDepartures() -- has anyone left?
  104.     if sig1() == true then p1 = "Vacant" end
  105.     if sig2() == true then p2 = "Vacant" end
  106.     if sig3() == true then p3 = "Vacant" end
  107.     if sig4() == true then p4 = "Vacant" end
  108. end
  109.  
  110. --Initialization
  111. local m = peripheral.wrap(mSide) -- wraps the monitor and pushes the outputs to it.
  112. term.redirect(m)
  113. sleep (45) --Allows time for the world to load if that is why the program is loading.
  114.  
  115. --Actual program execution
  116. while true do
  117.     if check() == true then --throws an error if the signal is faulty.
  118.         print("Welcome to Stationmaster CG" .. v)
  119.         print("Signal Assignment Error. Please verify and relaunch")
  120.         sleep(45)
  121.         os.reboot()
  122.     else
  123.         hold()
  124.         switch() -- initialises the state of the platform switches
  125.         sleep(2) -- Prevents those redstone changes from triggering the system
  126.         checkDepartures() -- makes sure trains that have left are noted before printing.
  127.         term.clear()
  128.         term.setCursorPos(1,1)
  129.         print("Welcome to Stationmaster CG " .. v)
  130.         print("Platform #1: " .. p1)
  131.         print("Platform #2: " .. p2)
  132.         print("Platform #3: " .. p3)
  133.         print("Platform #4: " .. p4)
  134.         if bypass == true then
  135.         print(incoming .. "has bypassed the station due to unavailable space. Sorry about that.")
  136.         end
  137.         os.pullEvent("redstone")
  138.             if det1() == true then --IEI determining which train just arrived.
  139.             incoming = train1          
  140.             elseif det2() == true then
  141.             incoming = train2          
  142.             elseif det3() == true then
  143.             incoming = train3          
  144.             elseif det4() == true then
  145.             incoming = train4      
  146.             else
  147.             incoming = "Unknown"
  148.         end
  149.         park() -- where will this train go? Works because it will always go in ascending order of available sets.
  150.         release() -- lets the train go where it needs to.
  151.         sleep(10) -- lets it get there.
  152. end
  153. end
Advertisement
Add Comment
Please, Sign In to add comment