rules54

retreiver

Jul 17th, 2013
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.34 KB | None | 0 0
  1. -- Determine functions. variables, tables, etc.
  2. local modem = nil
  3. local channels = {}
  4. local sides = {"front", "back", "left", "right", "top", "bottom"}
  5. local default = 1
  6. local id = tonumber(os.getComputerID())
  7. local label = os.getComputerLabel()
  8. local drive = false
  9. local link = false
  10. local spawn = false
  11. local existance = false
  12. local attempt = 1
  13. local x, y
  14. local function receive(timeout)
  15.   local e, s, sC, rC, m, sD = nil
  16.   local function rnEvent()
  17.     e, s, sC, rC, m, sD = os.pullEvent("modem_message")
  18.     if e ~= nil then returnValue = true end
  19.   end
  20.   parallel.waitForAny(function() sleep(timeout) end, rnEvent)
  21.   if returnValue then return e, s, sC, rC, m, sd end
  22. end
  23.  
  24. --Determine which side the modem/drive is located
  25. for i=1, #sides do
  26.   if peripheral.getType(sides[i]) == "modem" then
  27.     modemSide = sides[i]
  28.     modem = peripheral.wrap(sides[i])
  29.   elseif peripheral.getType(sides[i]) == "drive" then
  30.     drive = true
  31.   end
  32. end
  33.  
  34. --prevent script from continuing if errors
  35. assert(label ~= nil, "Please label this computer as the spawner it will control. Ex: Creepers")
  36.  
  37. --if a drive is present
  38. --copy the main channel
  39. if drive then
  40.   local f = io.open("disk/mobSpawnerChannels", "r")
  41.   if fs.exists("disk/mobSpawnerChannels") then
  42.     channels = textutils.unserialize(f:read("*a"))
  43.   else
  44.     channels = {}
  45.   end
  46.  
  47.   if channels.main ~= nil then
  48.     default = tonumber(channels.main)
  49.   end
  50.  
  51.   for k,v in pairs(channels) do
  52.     if tonumber(v) == tonumber(id) then
  53.       existance = true
  54.     end
  55.   end
  56.  
  57.   if not existance then
  58.     table.insert(channels, id)
  59.   end
  60.  
  61.   local f = io.open("disk/mobSpawnerChannels", "w")
  62.   f:write(textutils.serialize(channels))
  63.   f:close()
  64.   local f = io.open("mobSpawnerChannels", "w")
  65.   f:write(channels.main)
  66.   f:close()
  67.  
  68. else
  69.   local f = io.open("mobSpawnerChannels")
  70.   default = tonumber(f:read("*a"))
  71.   f:close()
  72. end
  73.  
  74. assert(modem ~= nil, "Please attach a wireless modem to any side.")
  75.  
  76. --Print some debug
  77.   term.clear() term.setCursorPos(1,1)
  78.   print("Spawner type: ".. label)
  79.   print("Modem located on: ".. modemSide)
  80.   print("Link: ".. tostring(link))
  81.  
  82. --Open channels to receive messages
  83.   print("Opening channel: ".. id)
  84.   modem.open(id)
  85.  
  86. --Begin loop
  87. while true do
  88.  
  89.   --Look at spawn values and turn spawner on/off
  90.   local signal = rs.getOutput("bottom")
  91.   if spawn and signal then
  92.     rs.setOutput("bottom", false)
  93.     print("Turning spawner on")
  94.   end
  95.   if not spawn and not signal then
  96.     rs.setOutput("bottom", true)
  97.     print("Turning spawner off")
  98.   end
  99.  
  100.   --Connect to main comp, if link is not there
  101.   if not link then
  102.     modem.transmit(id, id, "Spawner ".. label)
  103.     spawn = false
  104.     if attempt == 1 then
  105.       x, y = term.getCursorPos()
  106.     else term.setCursorPos(x,y)
  107.     end
  108.     term.clearLine()
  109.     term.write("Attempting to ping main computer.".." Attempt: ".. attempt)
  110.     attempt = attempt + 1
  111.   end
  112.  
  113.   --receive a ping back or timeout after 10 sec
  114.   local event, side, sChannel, rChannel, message, sDist = receive(5)
  115.  
  116.   --look at the received message, respond accordingly
  117.   if sChannel == id then
  118.     if message == "loop" then link = true
  119.     elseif message == "true" then spawn = true
  120.     elseif message == "false" then spawn = false
  121.     elseif message == "ping" then link = false
  122.     end
  123.   end
  124.  
  125.  
  126. end
Advertisement
Add Comment
Please, Sign In to add comment