NoxiaZ

Train Client

Sep 6th, 2013
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.58 KB | None | 0 0
  1. print("=================================================")
  2. print("======            Train Client             ======")
  3. print("======         Created by NoxiaZ           ======")
  4. print("======                                     ======")
  5. print("======      Inspired by OminousPenguin     ======")
  6. print("======           Version 0.1A              ======")
  7. print("=================================================")
  8. computerID = os.getComputerID()
  9. print("ComputerID: ",computerID)
  10.  
  11. function preparetable(t)
  12.     setmetatable(t,{__newindex=function(self,k,v) rawset(self,v,true) end})
  13. end
  14.  
  15. function compare(a,b)
  16.   return a[1] < b[1]
  17. end
  18.  
  19. function explode(inputstr, sep)
  20.     if sep == nil then
  21.                 sep = "%s"
  22.         end
  23.         t={} ; i=1
  24.         for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  25.                 t[i] = str
  26.                 i = i + 1
  27.         end
  28.         return t
  29. end
  30.  
  31. function insert(gpsY,id,name)
  32.     local value = id .. explodeChar .. name
  33.     gpsY = tonumber(gpsY)
  34.     if find(gpsY) then
  35.    
  36.     else
  37.         table.insert(stationList,gpsY,value)
  38.     end
  39. end
  40.  
  41. function find(key)
  42.     if (stationList[key] == nil) then
  43.         return false
  44.     else
  45.         return true
  46.     end
  47. end
  48.  
  49. function getColorNumber(color)
  50.     for key,value in pairs(colors) do
  51.         if key == color then
  52.             return value
  53.         end
  54.     end
  55.     return 0
  56. end
  57.  
  58. function removeBundleColor(color)
  59.     activeBundles = activeBundles - getColorNumber(color)
  60.     setBundle()
  61. end
  62.  
  63. function addBundleColor(color)
  64.     activeBundles = activeBundles + getColorNumber(color)
  65.     setBundle()
  66. end
  67.  
  68. function trackOn()
  69.     if (isTrackOn == false) then
  70.         isTrackOn = true
  71.         print("Turn on elevatortrack")
  72.         addBundleColor(elevatorTrack)  
  73.     end
  74. end
  75.  
  76. function trackOff()
  77.     if (isTrackOn == true) then
  78.         isTrackOn = false
  79.         print("Turn off elevatortrack")
  80.         removeBundleColor(elevatorTrack)
  81.     end
  82. end
  83.  
  84. function releaseCart()
  85.     addBundleColor(boardingTrack)
  86.     sleep(0.5)
  87.     removeBundleColor(boardingTrack)
  88. end
  89.  
  90. function setBundle()
  91.     redstone.setBundledOutput(bundle,activeBundles)
  92. end
  93.  
  94. function clearMonitor()
  95.     monitor.clear()
  96.     monitor.setCursorPos(1,1)
  97. end
  98.  
  99. function writeMonitor()
  100.     clearMonitor()
  101.     monitor.write("Pick your place:")
  102.     table.sort(stationList, compare)
  103.     for i, v in pairs(stationList) do
  104.         local cX,cY = monitor.getCursorPos()
  105.         monitor.setCursorPos(1,cY+1)
  106.        
  107.         local data = explode(v,explodeChar)
  108.         monitor.write(data[2])
  109.     end
  110. end
  111.  
  112. isTrackOn = false
  113. serverID = -1
  114. bundle = "bottom"
  115. elevatorTrack = "orange"
  116. boardingTrack = "white"
  117. modem = "back"
  118. monitorPlace="top"
  119. explodeChar="\31"
  120. activeBundles = 0
  121. stationList={}
  122. pageIndex = 0
  123. pageCount = 5
  124.  
  125. preparetable(stationList)
  126. monitor = peripheral.wrap(monitorPlace)
  127. monitor.clear()
  128. rednet.open(modem)
  129. setBundle()
  130. clearMonitor()
  131. local senderId, message, distance
  132. local keepRunning = true;
  133. while (keepRunning) do
  134.     rednet.broadcast("who")
  135.     senderId, message, distance = rednet.receive(5)
  136.     if (message == "server") then
  137.         keepRunning = false
  138.     end
  139. end
  140.  
  141. serverID = tonumber(senderId)
  142. print("Found server ID: ",senderId)
  143. write("Enter your Y GPS coordinate: ")
  144. local y = read()
  145. write("Enter name of floor: ")
  146. local n = read()
  147. rednet.send(senderId,"add " .. explodeChar .. " " .. y .. explodeChar .. computerID .. explodeChar .. n)
  148.  
  149. local run = true
  150. while run do
  151.     local event = {os.pullEvent()}
  152.     if (event[1] == "rednet_message") then
  153.         local senderID = event[2]
  154.         local message = event[3]
  155.         print("Message: ", message)
  156.         local array = explode(message, " ")
  157.         local command = array[1]
  158.         if (command == "true") then
  159.             trackOn()
  160.         elseif (command == "false") then
  161.             trackOff()
  162.         elseif (command =="release") then
  163.             releaseCart()
  164.         elseif (command == "update") then
  165.             shell.run("delete","startup")  
  166.             shell.run("pastebin","get","GRxRhE4X","startup")
  167.             shell.run("reboot")
  168.         elseif (command == "station") then
  169.             local data = explode(array[3],array[2])
  170.             data = explode(array[3],array[2])
  171.             local gpsY = data[1]
  172.             local id = data[2]
  173.             local name = data[3]
  174.             insert(gpsY,id,name)
  175.             writeMonitor()         
  176.         end    
  177.     elseif (event[1] == "redstone") then
  178.        
  179.     elseif (event[2] == "q") then
  180.         run = false
  181.     elseif (event[1] == "monitor_touch") then
  182.         local x = tonumber(event[3])
  183.         local y = tonumber(event[4])
  184.         local nY = y-1
  185.         write(" X: ")
  186.         write(x)
  187.         write(" Y: ")
  188.         write(y)
  189.         write(" nY: ")
  190.         print(nY)
  191.         i = 1
  192.         for k, v in pairs(stationList) do
  193.             if (i == nY) then
  194.                 local data = explode(v,explodeChar)
  195.                 print("Going to: ", data[2])
  196.                 rednet.send(serverID,"travel " .. data[1])             
  197.                 break
  198.             end
  199.             i = i +1
  200.         end
  201.     end
  202. end
Advertisement
Add Comment
Please, Sign In to add comment