rules54

Mob

Jul 18th, 2013
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.46 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.   end
  30. end
  31.  
  32. --prevent script from continuing if errors
  33. assert(label ~= nil, "Please label this computer as the spawner it will control. Ex: Creepers")
  34. assert(modem ~= nil, "Please attach a wireless modem to any side.")
  35.  
  36. --Print some debug
  37.   term.clear() term.setCursorPos(1,1)
  38.   print("Spawner type: ".. label)
  39.   print("Modem located on: ".. modemSide)
  40.   print("Link: ".. tostring(link))
  41.  
  42. --Open channels to receive messages
  43.   print("Opening channel: ".. id)
  44.   modem.open(id)
  45.  
  46. --Begin loop
  47. while true do
  48.  
  49.   --Look at spawn values and turn spawner on/off
  50.   local signal = rs.getOutput("bottom")
  51.   if spawn and signal then
  52.     rs.setOutput("bottom", false)
  53.     print("Turning spawner on")
  54.   end
  55.   if not spawn and not signal then
  56.     rs.setOutput("bottom", true)
  57.     print("Turning spawner off")
  58.   end
  59.  
  60.   --Connect to main comp, if link is not there
  61.   if not link then
  62.     modem.transmit(id, id, "Spawner ".. label)
  63.     spawn = false
  64.     if attempt == 1 then
  65.       x, y = term.getCursorPos()
  66.     else term.setCursorPos(x,y)
  67.     end
  68.     term.clearLine()
  69.     term.write("Attempting to ping main computer.".." Attempt: ".. attempt)
  70.     attempt = attempt + 1
  71.   end
  72.  
  73.   --receive a ping back or timeout after 10 sec
  74.   local event, side, sChannel, rChannel, message, sDist = receive(5)
  75.  
  76.   --look at the received message, respond accordingly
  77.   if sChannel == id then
  78.     if message == "loop" then link = true
  79.     elseif message == "true" then spawn = true
  80.     elseif message == "false" then spawn = false
  81.     elseif message == "ping" then link = false
  82.     end
  83.   end
  84.  
  85.  
  86. end
Advertisement
Add Comment
Please, Sign In to add comment