rhn

Ticket Machine Default

rhn
Jan 29th, 2017
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --Destination selector for Railcraft and OpenPeripheral Ticket Machine.
  2. --Simple extract the ticket from Ticket Machine and insert into Locomotive with (Adv.) Item Loader.
  3. --Redstone pulse will be emitted through the defined side(below) of Computer upon selecting destination. Can for example be used to dispense train, emit sound(Note Block) etc.
  4. --List of names of stations and correlating Routing dest's are loaded from separate script. Each station are then chosen among the list upon starting this script the first time. Each station updates automatically to new added stations upon reload of script.
  5. --Attach Advanced Monitors and Ticket Machine by either wired modems or placed directly against any side of the Computer. Width/height of monitor depends on number of stations and length of names, though a 2x3 is a good estimate.
  6. --For custom stations make a copy of http://pastebin.com/0FTF2FU3 and change url below.
  7.  
  8. --Url of list of stations.
  9. local stationURL = "tFC4W3H4"
  10.  
  11. --Side of redstone output
  12. local redstoneside = "bottom"
  13.  
  14.  
  15. --Device side detection function
  16. function detectDevice(DeviceName)
  17. DeviceSide="none"
  18. for k,v in pairs(redstone.getSides()) do
  19.   if peripheral.getType(v) and string.find(peripheral.getType(v), DeviceName) then
  20.       DeviceSide = v
  21.       break
  22.    --end
  23.   end
  24. end
  25.   return(DeviceSide)
  26. end
  27.  
  28. --Get list of devices
  29. monitor = "none"
  30. ticket = "none"
  31. local peripheralList = peripheral.getNames()
  32.  
  33. --Detect monitor
  34. MonitorSide=detectDevice("monitor")
  35.  
  36. if MonitorSide~="none" then
  37.       monitor=peripheral.wrap(MonitorSide)
  38.    print ("Monitor on the " .. MonitorSide .. " connected.")
  39.    else
  40.     for Index = 1, #peripheralList do
  41.         if string.find(peripheralList[Index], "monitor") then
  42.             monitor=peripheral.wrap(peripheralList[Index])
  43.             print ("Monitor on wired modem: "..peripheralList[Index].." connected.")
  44.         end
  45.     end --for
  46.     if monitor == "none" then
  47.         print ("Warning - No Monitor attached, halting script!")
  48.         return
  49.     end
  50. end
  51.  
  52. w,h=monitor.getSize()
  53.  
  54. print("Monitor dimensions: w: "..w..", h: "..h)
  55.  
  56.  
  57.  
  58. --Detect ticketmachine
  59. TicketSide=detectDevice("ticketmachine")
  60.  
  61. if TicketSide~="none" then
  62.       ticket=peripheral.wrap(TicketSide)
  63.    print ("Ticket Machine on the " .. TicketSide .. " connected.")
  64.    else
  65.     for Index = 1, #peripheralList do
  66.         if string.find(peripheralList[Index], "ticketmachine") then
  67.             ticket=peripheral.wrap(peripheralList[Index])
  68.             print ("Ticket Machine on wired modem: "..peripheralList[Index].." connected.")
  69.         end
  70.     end --for
  71.     if ticket == "none" then
  72.         print ("Warning - No Ticket machine attached, halting script!")
  73.         return
  74.     end
  75. end
  76.  
  77.  
  78.  
  79. --Download station file
  80. if (fs.exists("stationFile") == true) then
  81.     shell.run("delete stationFile")
  82. end
  83. shell.run("pastebin", "get", stationURL, "stationFile")
  84.  
  85. --Load station file and save to variable
  86. os.loadAPI("stationFile")
  87. local stationList = stationFile.stationList
  88. local stationDest = stationFile.stationDest
  89.  
  90. --first time run, pick station from station list. Saving to file to make script reboot friendly
  91. if (fs.exists("thisstation") == false) then
  92.     while true do
  93.         for i=1, #stationList do
  94.             print(i..". "..stationList[i])
  95.         end
  96.         print("Which station is this?")
  97.         local answer = read()
  98.         if tonumber(answer)==nill then
  99.             print("Wrong answer, input not a number")
  100.         else
  101.             local answerv = tonumber(string.format("%." .. 0 .. "f", answer))
  102.             if (0 < answerv and answerv <= #stationList) then
  103.                 local file = fs.open("thisstation","w")
  104.                 file.writeLine(stationList[answerv])
  105.                 file.close()
  106.                 break
  107.             else
  108.                 print("Wrong answer, input does not match number of stations")
  109.             end
  110.         end
  111.     end
  112. end
  113.  
  114. --load station name from file
  115. local file = fs.open("thisstation","r")
  116. local thisstation = file.readLine()
  117. file.close()
  118. print("Setting station as: "..thisstation)
  119.  
  120. --This stations number on the list
  121. for i=1, #stationList do
  122.     if string.find(stationList[i], thisstation) then
  123.         thisstationnum = i
  124.         print("Number "..thisstationnum.." on list of stations")
  125.     end
  126. end
  127.  
  128.  
  129.  
  130. --Write buttons on monitor
  131.     monitor.clear()
  132.     monitor.setBackgroundColour((colours.blue))
  133.     monitor.setCursorPos(1,1)
  134.     monitor.write(" Destination:                                       ")
  135.  
  136.     for i=1, thisstationnum-1 do
  137.         monitor.setCursorPos(1,i*2+1)
  138.         monitor.setBackgroundColour((colours.green))
  139.         monitor.write(" "..stationList[i].."                                    ")
  140.     end
  141.     for i=thisstationnum+1, #stationList do
  142.         monitor.setCursorPos(1,i*2-1)
  143.         monitor.setBackgroundColour((colours.green))
  144.         monitor.write(" "..stationList[i].."                                    ")
  145.     end
  146.  
  147.     monitor.setBackgroundColour((colours.black))
  148.  
  149.  
  150.  
  151. --Click checking function, printing tickets
  152. function checkClickPosition()
  153.     for i=1, thisstationnum-1 do
  154.         if mouseHeight == i*2+1 then
  155.             ticket.createTicket(stationDest[i], 1)
  156.             sleep(0.5)
  157.             redstone.setOutput(redstoneside, true)
  158.             sleep(0.5)
  159.             redstone.setOutput(redstoneside, false)
  160.             sleep(0.5)
  161.         end
  162.     end
  163.     for i=thisstationnum+1, #stationList do
  164.         if mouseHeight == i*2-1 then
  165.             ticket.createTicket(stationDest[i], 1)
  166.             sleep(0.5)
  167.             redstone.setOutput(redstoneside, true)
  168.             sleep(0.5)
  169.             redstone.setOutput(redstoneside, false)
  170.             sleep(0.5)
  171.         end
  172.     end
  173. end
  174.  
  175.  
  176. --Check for clicks
  177. repeat
  178.         event,p1,p2,p3 = os.pullEvent()
  179.                 if event=="monitor_touch" then
  180.                         mouseWidth = p2 -- sets mouseWidth
  181.                         mouseHeight = p3 -- and mouseHeight
  182.                         checkClickPosition() -- this runs our function
  183.                 end
  184. until event=="char" and p1==("x")
Add Comment
Please, Sign In to add comment