Advertisement
jille_Jr

CC: Furnace control Computer 2

Nov 17th, 2013
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.65 KB | None | 0 0
  1. -- Furnace control
  2. -- Setup:
  3. -- (from above)
  4. -- Comp - Outp
  5. -- Inpu - Furn
  6. -- the second computer under the furnace,
  7. -- and a wired modem connecting the 2 computers
  8.  
  9. -- Computer 1: http://pastebin.com/06cjixf9
  10. -- Computer 2: (this one) http://pastebin.com/RjyREdT2
  11.  
  12. -- Screenshots of setup:
  13. -- 1 http://i.imgur.com/t9GFE2r.png
  14. -- 2 http://i.imgur.com/WjnunBC.png
  15.  
  16. local furnace = peripheral.wrap("top")
  17. or error("Unable to wrap furnace!",0)
  18.  
  19. local modemside = "bottom"
  20. local modem
  21. if peripheral.getType(modemside) == "modem" then
  22.     modem = peripheral.wrap(modemside)
  23.     or error("Unable to wrap modem!",0)
  24. else error("No modem found, please correct!",0) end
  25.  
  26. local function getAllStacks(inv)
  27.     local t = {}
  28.     local size = inv.getInventorySize()
  29.     for slot = 1,size do
  30.         local item = inv.getStackInSlot(slot) or {}
  31.        
  32.         item.rawName = item.rawName or ""
  33.         item.qty = item.qty or 0
  34.         item.dmg = item.dmg or 0
  35.         item.name = item.name or ""
  36.         item.id = item.id or 0
  37.        
  38.         t[slot] = item
  39.     end
  40.     return t
  41. end
  42.  
  43. local function isEmpty()
  44.     local items = getAllStacks(furnace)
  45.     for slot,item in pairs(items) do
  46.         if item.qty > 0 then
  47.             return false
  48.         end
  49.     end
  50.     return true
  51. end
  52.  
  53. modem.open(1)
  54. print("Waits for control computer to ask if the furnace is empty.")
  55. while true do
  56.     local ev,p1,p2,p3,p4,p5 = os.pullEvent()
  57.    
  58.     if ev == "modem_message" and p1 == modemside and p2 == 1 then
  59.         local msg = textutils.unserialize(p4)
  60.         if type(msg) == "table" then
  61.             if msg.task == "furnace"
  62.             and msg.value == "question" then
  63.                 local reply = textutils.serialize({task="furnace",value=isEmpty()})
  64.                 modem.transmit(1,1,reply)
  65.             end
  66.         end
  67.     end
  68. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement