Advertisement
Zantag

trainServer

Dec 2nd, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. os.loadAPI("button")
  2. m = peripheral.wrap("monitor_1")
  3. m.clear()
  4.  
  5. rednet.open("bottom")
  6.  
  7. inputSide = "right"
  8. outputSide = "back"
  9. trainsInYard = 0
  10. stationTime = 0
  11. train = false
  12. stationSelected = false
  13. function checkTrains()
  14. trainsInYard = redstone.getBundledInput(inputSide)
  15. end
  16. function sendTrain()
  17. train = true
  18. if colors.test(trainsInYard, colors.white) then
  19. tbl = {"SEND_TRAIN", colors.white}
  20. print("1st train ordered")
  21. elseif colors.test(trainsInYard, colors.orange) then
  22. tbl = {"SEND_TRAIN", colors.orange}
  23. print("2nd train ordered")
  24. elseif colors.test(trainsInYard, colors.magenta) then
  25. tbl = {"SEND_TRAIN", colors.magenta}
  26. print("3rd train ordered")
  27. elseif colors.test(trainsInYard, colors.lightBlue) then
  28. tbl = {"SEND_TRAIN", colors.lightBlue}
  29. print("4th train ordered")
  30. else
  31. print("No trains available")
  32. train = false
  33. end
  34. if train == true then
  35. rednet.broadcast(textutils.serialize(tbl))
  36. end
  37. end
  38.  
  39. function fillTable()
  40. button.setTable("Spawn", loc1, 10,20,3,5)
  41. button.setTable("AlpacaCO", loc2, 22,32,3,5)
  42. --button.setTable("Test3", loc3, 10,20,8,10)
  43. --button.setTable("Test4", loc4, 22,32,8,10)
  44. button.screen()
  45. end
  46.  
  47. function stationClick()
  48. redstone.setBundledOutput("back", station)
  49. stationTime = os.time()
  50. stationSelected = true
  51. end
  52. function loc1()
  53. button.toggleButton("Spawn")
  54. print("Spawn")
  55. station=colors.white
  56. stationClick()
  57. end
  58. function loc2()
  59. button.toggleButton("AlpacaCO")
  60. print("AlpacaCO")
  61. station = colors.orange
  62. stationClick()
  63. end
  64. function loc3()
  65. button.toggleButton("AlpacaCO")
  66. print("AlpacaCO")
  67. station = colors.magenta
  68. stationClick()
  69. end
  70.  
  71. function clearOutput()
  72. redstone.setBundledOutput(inputSide, 0)
  73. end
  74. function launchTrain()
  75. redstone.setBundledOutput(inputSide, colors.blue)
  76. os.sleep(2)
  77. redstone.setBundledOutput(inputSide, 0)
  78. button.clearButtonToggle()
  79. end
  80. function pickupPassenger()
  81. redstone.setBundledOutput(inputSide, colors.red)
  82. end
  83. function isTrainAtStation()
  84. return colors.test (redstone.getBundledInput(inputSide), colors.black) == true
  85. end
  86. function trainContainPassenger()
  87. return colors.test (redstone.getBundledInput(inputSide), colors.green) == true
  88. end
  89. fillTable()
  90. launchTrain()
  91. sleep(2)
  92. clearOutput()
  93. button.heading("Cake Town Main Station")
  94. currentState = "NO_ACTION"
  95. trainSentTime = os.time() - 2
  96. os.startTimer(2)
  97. while true do
  98. event,side,x,y = os.pullEvent()
  99. if event == "monitor_touch" then
  100. button.checkxy(x,y)
  101. end
  102. if stationSelected == true then
  103. print("Station Selected")
  104. if os.time() - 2.0 > stationTime then
  105. stationSelected = false
  106. if isTrainAtStation() == true then
  107. print("Sending Train Back to Refuel")
  108. launchTrain()
  109. button.clearButtonToggle()
  110. end
  111. elseif isTrainAtStation() == true then
  112. print("Train at Main Station")
  113. pickupPassenger()
  114. if trainContainPassenger() == true then
  115. print("Passenger is in train.")
  116. launchTrain()
  117. print("Train Launched.")
  118. end
  119. end
  120. if isTrainAtStation() == false and os.time() - 2.0 > trainSentTime then
  121. print("No train at Main Station, calling one")
  122. checkTrains()
  123. sendTrain()
  124. trainSentTime = os.time()
  125. end
  126. end
  127. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement