AliquotMesozoic

Mob Spawner Startup

Jan 31st, 2015
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.31 KB | None | 0 0
  1. local wireSide = ...
  2.  
  3. assert(wireSide, "Argument (wireSide) missing")
  4. local lightControlID = 155
  5. local myID = 154
  6.  
  7. local modem = peripheral.find("modem")
  8.  
  9. modem.open(myID)
  10.  
  11. local function getMachineSetup(onByDefault, color, func)
  12.     return {
  13.         current = onByDefault and color or 0,
  14.         color = color,
  15.         func = func
  16.     }
  17. end
  18.  
  19.  
  20. local machines = {
  21.     lights = getMachineSetup(true, colors.brown, function(currentStatus) modem.transmit(lightControlID, myID, currentStatus) end),
  22.     spawner = getMachineSetup(true, colors.red),
  23.     grinders = getMachineSetup(false, colors.green)
  24. }
  25.  
  26. local function getMachineStatus(machine)
  27.     if (machines[machine]) then
  28.         return machines[machine].current
  29.     end
  30. end
  31.  
  32. local function update()
  33.     local color = 0
  34.     for name, machine in pairs(machines) do
  35.         color = colors.combine(color, machine.current)
  36.         if machine.func then
  37.             machine.func(machine.current ~= 0)
  38.         end
  39.     end
  40.     rs.setBundledOutput(wireSide, color)
  41. end
  42.  
  43. local function getStatus(id)
  44.     modem.transmit(id, myID, machines)
  45. end
  46.  
  47. local function onMessage(message)
  48.     local query = message[5]
  49.     if query.machine then
  50.         machines[query.machine].current = query.status and machines[query.machine].color or 0
  51.     end
  52.  
  53.     update()
  54.     getStatus(message[3])
  55. end
  56.  
  57.  
  58. update()
  59.  
  60. while true do
  61.     onMessage({os.pullEvent("modem_message")})
  62. end
Advertisement
Add Comment
Please, Sign In to add comment