Talonsfold

trainyard v1.6

Jul 23rd, 2025
868
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.86 KB | Gaming | 0 0
  1. --*******************************************************************
  2. --*Notes:
  3. --*
  4. --*
  5. --*
  6. --*
  7. --*
  8. --*
  9. --*
  10. --*
  11.  
  12. --****************
  13. --*Initializing  *
  14. --****************
  15.  
  16.  
  17. --Variables Setup
  18. cvar = 0 -- used for counting
  19. numlanes = 7 -- used as a sanity check when rolling though the lanes
  20. lanecolors = {} -- stores lane color info
  21. lanestat = {} -- stores lane status
  22. lanedock = {} -- stores if the train in that lane is in the unload area or ready to release
  23. lanelocked = {} -- stores if a lane is manually shutdown
  24. lanescreen = {} -- stores the monitor list for the lane displays
  25. yardscram = 0 -- stores state for emergency shutdown process
  26. exitlane = 0 -- stores which lane is good to be released.
  27. traincount = 395 -- train count since last computer reset
  28. id=0 -- not implemented yet
  29. countmessage=0 -- not implemented yet
  30. tempmonitor=""
  31.  
  32. --  values assigned to each color transferred to a linked variable for easy access
  33. -- entrylanecolors = colors.black
  34. lanecolors[1] = colors.white
  35. lanecolors[2] = colors.orange
  36. lanecolors[3] = colors.magenta
  37. lanecolors[4] = colors.lightBlue
  38. lanecolors[5] = colors.yellow
  39. lanecolors[6] = colors.lime
  40. lanecolors[7] = colors.pink
  41. exitlanecolor = colors.black
  42. traincountercolor = colors.red
  43. laneclearcolor = colors.green
  44. laneunloadingcolor = colors.orange
  45. lanewaitingcolor = colors.yellow
  46. lanelockedcolor = colors.red
  47. lockedlanesx1 = {2,24,44,64,84,104,124} --workaround for floating point glitch on locked lane monitors
  48. lockedlanesx2 = {22,42,62,82,102,122,142} --workaround for floating point glitch on locked lane monitors
  49.  
  50.  
  51. --Monitor setup
  52. local original_terminal = term.current()
  53.  
  54. --main display
  55. local monitor = peripheral.wrap("monitor_11")
  56. term.redirect(monitor)
  57. monitor.clear()
  58. monitor.setTextScale(1)
  59.  
  60. --Locked lane display
  61. local monitor2 = peripheral.wrap("monitor_10")
  62. monitor2.clear()
  63. monitor2.setTextScale(.5)
  64.  
  65. --lane displays
  66. lanescreen[1] = peripheral.wrap("monitor_3")
  67. lanescreen[2] = peripheral.wrap("monitor_4")
  68. lanescreen[3] = peripheral.wrap("monitor_5")
  69. lanescreen[4] = peripheral.wrap("monitor_6")
  70. lanescreen[5] = peripheral.wrap("monitor_7")
  71. lanescreen[6] = peripheral.wrap("monitor_2")
  72. lanescreen[7] = peripheral.wrap("monitor_8")
  73.  
  74.  
  75.  
  76. m1x, m1y = monitor.getSize() --screen size grab for automated sizing
  77. lsx, lsy = lanescreen[1].getSize() --screen size grab for automated sizing
  78.  
  79. --m2x, m2y = monitor2.getSize() --disabled/not used due to floating point bug
  80.  
  81.  
  82.  
  83.  
  84. --******************************************************************************************
  85. --*Functions - are defined in the beginning and can be called from within the main loop    *
  86. --******************************************************************************************
  87.  
  88. function scanlanes() --process lanes and assign status to variables.
  89.     for cvar = 1,numlanes,1 do -- uses a counter to roll through each lane and check for the status. The status is then assigned to a variable for storage
  90.         if rs.testBundledInput("left", lanecolors[cvar]) == true then lanestat[cvar] = false else lanestat[cvar] = true end  -- check for occupied lanes
  91.         if rs.testBundledInput("bottom", lanecolors[cvar]) == true then lanedock[cvar] = false else lanedock[cvar] = true end -- see if occupied train is in unloading or exit sections
  92.         if rs.testBundledInput("back", lanecolors[cvar]) == true then lanelocked[cvar] = true else lanelocked[cvar] = false end -- check to see if any lanes have been manually locked out
  93.     end
  94. end
  95.  
  96. function release(releaselane) --process the release on the current selected exit lane. (also updates status displays
  97.     term.setTextColor(colors.white)
  98.     monitordraw(monitor, 2,6+((releaselane-1)*3),70,6+((releaselane-1)*3)+1,colors.blue, 5, 7+((releaselane-1)*3), "Lane: " .. releaselane .. " Released")
  99.     rs.setBundledOutput("right", lanecolors[releaselane]) --sets the output to release a lane
  100.     sleep(3) --delay to allow time for train to move into exit lane
  101.     rs.setBundledOutput("right", 0) -- reset release
  102. end
  103.  
  104. function monitordraw (display, x1, y1, x2, y2, lanecolor, textx, texty, lanetext)
  105.     term.redirect(display)
  106.     paintutils.drawFilledBox(x1,y1,x2,y2,lanecolor)
  107.     monitor.setTextColor(colors.black)
  108.     monitor.setCursorPos(textx, texty)
  109.     print(lanetext)            
  110.     monitor.setBackgroundColor(colors.black)
  111.     term.redirect(monitor)
  112. end
  113.  
  114.  
  115. function drawupdate()
  116.     monitor.clear()
  117.     paintutils.drawFilledBox(1,1,110,5,colors.blue)
  118.     paintutils.drawFilledBox(3,3,27,4,colors.black)
  119.     paintutils.drawFilledBox(33,3,55,4,colors.black)
  120.     term.setCursorPos(3,3)
  121.     monitor.setTextColor(colors.white)
  122.     print("Train Station Monitoring")
  123.     monitor.setTextColor(colors.white)
  124.     monitor.setCursorPos(33,3)
  125.     print("Trains Since Reset: " .. traincount)
  126.  
  127.     --Draw Main Monitor lanes
  128.     for j=1,numlanes,1 do
  129.         if lanelocked[j] == true then
  130.             monitordraw(monitor, 2,6+((j-1)*3),70,6+((j-1)*3)+1,lanelockedcolor, 5, 7+((j-1)*3), "Lane: " .. j .. " Locked down")
  131.         else
  132.             if lanestat[j] == true then
  133.                 if lanedock[j] == true then
  134.                     monitordraw(monitor, 2,6+((j-1)*3),70,6+((j-1)*3)+1,laneunloadingcolor, 5, 7+((j-1)*3), "Lane: " .. j .. " Unloading")
  135.                 else
  136.                     monitordraw(monitor, 2,6+((j-1)*3),70,6+((j-1)*3)+1,lanewaitingcolor, 5, 7+((j-1)*3), "Lane: " .. j .. " Waiting for Release")              end
  137.             else
  138.                     monitordraw(monitor, 2,6+((j-1)*3),70,6+((j-1)*3)+1,laneclearcolor, 5, 7+((j-1)*3), "Lane: " .. j .. " Vacant")             end
  139.             end
  140.         end
  141.     -- Draw Lane Lock displays
  142.     term.redirect(monitor2)
  143.     term.setBackgroundColor(colors.black)
  144.     term.clear()
  145.     for j = 1,numlanes,1 do
  146.         if lanelocked[j] == true then
  147.             --monitordraw(monitor2,lockedlanesx1[j],1,lockedlanesx2[j],10,colors.red, lockedlanesx1[j]+2,5, "Lane: ".. j.. " Locked")
  148.             paintutils.drawFilledBox(lockedlanesx1[j],1,lockedlanesx2[j],10,colors.red)
  149.             monitor2.setTextColor(colors.white)
  150.             monitor2.setCursorPos(lockedlanesx1[j]+2,5)
  151.             print("Lane".. j.. ": Locked")
  152.         else
  153.             --monitordraw(monitor2,lockedlanesx1[j],1,lockedlanesx2[j],9,colors.greeb, lockedlanesx1[j]+2,5, "Lane: ".. j.. " Open")
  154.             paintutils.drawFilledBox(lockedlanesx1[j],1,lockedlanesx2[j],10,colors.green)
  155.             monitor2.setTextColor(colors.white)
  156.             monitor2.setCursorPos(lockedlanesx1[j]+2,5)
  157.             print("Lane".. j.. ": Open")
  158.         end
  159.     end
  160.  
  161.  
  162.     -- draw lanesigns
  163.    
  164.     for j = 1, numlanes, 1 do
  165.         term.redirect(lanescreen[j])
  166.  
  167.         if lanelocked[j] == true then
  168.             term.setBackgroundColor(lanelockedcolor)
  169.             term.clear()
  170.             term.setCursorPos(2,5)
  171.             print("Lane ".. j)
  172.         else
  173.             if lanestat[j] == true then
  174.                 if lanedock[j] == true then
  175.                     term.setBackgroundColor(laneunloadingcolor)
  176.                     term.clear()
  177.                     term.setCursorPos(2,5)
  178.                     print("Lane ".. j)
  179.                 else
  180.                     term.setBackgroundColor(lanewaitingcolor)
  181.                     term.clear()
  182.                     term.setCursorPos(2,5)
  183.                     print("Lane ".. j)
  184.                 end
  185.             else
  186.                     term.setBackgroundColor(laneclearcolor)
  187.                     term.clear()
  188.                     term.setCursorPos(2,5)
  189.                     print("Lane ".. j)
  190.             end
  191.             end
  192.         end
  193.  
  194.    
  195.     term.redirect(monitor)
  196. end
  197.  
  198. function counterincrement()
  199.     traincount=traincount+1
  200.     while (rs.testBundledInput("bottom",traincountercolor)) do
  201.     sleep(.1)
  202.     end
  203. end
  204.  
  205. --****************
  206. --*Main Loop     *
  207. --****************
  208.  
  209. while(true) do
  210.     scanlanes() -- check to see what lanes are full
  211.     exitlane=exitlane+1
  212.     if rs.testBundledInput("bottom",traincountercolor) == true then counterincrement() end
  213.     if exitlane>numlanes then exitlane=1 end
  214.         if rs.testBundledInput("left", exitlanecolor) == false then  -- make sure the exit lane is clear.
  215.             if lanestat[exitlane] == true then -- check if there is a train in the current tested lane.
  216.                 if lanedock[exitlane] == false then --if there is a lane but it is in the unloading area then skip to the next one
  217.                     if lanelocked[exitlane] == false then
  218.                         release(exitlane) -- if it meets all conditions then release
  219.                     end
  220.             end
  221.         end
  222.     end
  223.     drawupdate() -- update display
  224.     sleep(.3)
  225. end
  226.  
  227.  
  228.  
Advertisement
Add Comment
Please, Sign In to add comment