Advertisement
jille_Jr

CC: Furnace control Computer 1

Nov 17th, 2013
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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: (this one) http://pastebin.com/06cjixf9
  10. -- Computer 2: 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. -- how often computer shall retry
  17. local updaterate = 2 -- times a sec
  18.  
  19. -- where the chests are in relation to the furnace
  20. local outputToFurnace = "north"
  21. local inputToFurnace = "west"
  22. -- directions: north,south,east,west,up,down
  23.  
  24. -- slots in the furnace inventory
  25. local inputSlot = 0
  26. local fuelSlot = 1
  27. local outputSlot = 2
  28.  
  29. -- 1 for normal furnace
  30. -- 1.25 for IC2 iron furnace
  31. local furnaceModifier = 1
  32.  
  33. local fuel = {
  34.     -- {id,dmg,burntime},
  35.     {id=263,dmg=0,burn=8},--coal
  36.     {id=263,dmg=1,burn=8},--charcoal
  37.     {id=5,dmg=-1,burn=1.5},--planks
  38.     --{id=17,dmg=-1,burn=1.5},--wood
  39.     {id=6,dmg=-1,burn=.5},--sapling
  40.     {id=280,dmg=0,burn=.5},--stick
  41. }
  42.  
  43. local input = peripheral.wrap("front")
  44. or error("Unable to wrap input chest!",0)
  45. local output = peripheral.wrap("right")
  46. or error("Unable to wrap output chest!",0)
  47.  
  48. local modemside = "bottom"
  49. local modem
  50. if peripheral.getType(modemside) == "modem" then
  51.     modem = peripheral.wrap(modemside)
  52.     or error("Unable to wrap modem!",0)
  53. else error("No modem found, please correct!",0) end
  54.  
  55. local function getAllStacks(inv)
  56.     local t = {}
  57.     local size = inv.getInventorySize()
  58.     for slot = 1,size do
  59.         local item = inv.getStackInSlot(slot) or {}
  60.        
  61.         item.rawName = item.rawName or ""
  62.         item.qty = item.qty or 0
  63.         item.dmg = item.dmg or 0
  64.         item.name = item.name or ""
  65.         item.id = item.id or 0
  66.        
  67.         t[slot] = item
  68.     end
  69.     return t
  70. end
  71.  
  72. -- statement = boolean
  73. -- fuel = table
  74. local function isFuel(item)
  75.     for _,fuel in pairs(fuel) do
  76.         if item.id == fuel.id
  77.         and (item.dmg == fuel.dmg
  78.         or fuel.dmg == -1) then
  79.             return true,fuel
  80.         end
  81.     end
  82.     return false
  83. end
  84.  
  85. -- returns slot,fuel,item
  86. local function findFuel()
  87.     local items = getAllStacks(input)
  88.     for slot,item in pairs(items) do
  89.         local state,fuel = isFuel(item)
  90.         if state then
  91.             return slot,fuel,item
  92.         end
  93.     end
  94. end
  95.  
  96. -- returns slot,item
  97. local function findItem()
  98.     local slot,fuel = findFuel()
  99.     if slot and fuel then
  100.         local items = getAllStacks(input)
  101.         for slot,item in pairs(items) do
  102.             local state = isFuel(item)
  103.             if not state then
  104.                 if item.qty >= fuel.burn then
  105.                     return slot,item
  106.                 end
  107.             end
  108.         end
  109.     end
  110. end
  111.  
  112. local function emptyFurnace()
  113.     output.pullItem(outputToFurnace,outputSlot,64)
  114. end
  115.  
  116. local function isFurnaceEmpty()
  117.     local timeout = os.startTimer(2)
  118.     modem.open(1)
  119.     local msg = textutils.serialize({task="furnace",value="question"})
  120.     modem.transmit(1,1,msg)
  121.    
  122.     local state = false
  123.    
  124.     while true do
  125.         local ev,p1,p2,p3,p4,p5 = os.pullEvent()
  126.         if ev == "modem_message" and p1 == modemside and p2 == 1 then
  127.             local msg = textutils.unserialize(p4)
  128.             if type(msg) == "table" then
  129.                 if msg.task == "furnace"
  130.                 and type(msg.value) == "boolean" then
  131.                     state = msg.value
  132.                     break
  133.                 end
  134.             end
  135.         elseif ev == "timer" then
  136.             if p1 == timeout then
  137.                 state = false
  138.                 break
  139.             end
  140.         end
  141.     end
  142.     modem.close(1)
  143.     return state
  144. end
  145.  
  146. local function cook()
  147.     -- check with computer
  148.     if isFurnaceEmpty() then
  149.         -- get fuel and item
  150.         local fuelslot,fuel,fuelitem = findFuel()
  151.         local itemslot,item = findItem()
  152.        
  153.         -- no fuel/item found: cancel out
  154.         if not fuel then return end
  155.         if not item then return end
  156.        
  157.         local maxburn = fuelitem.qty*fuel.burn
  158.         local fuelmove,itemmove
  159.         if item.qty > maxburn * furnaceModifier then
  160.             -- # of items are more then we can burn
  161.             fuelmove = fuelitem.qty
  162.             itemmove = maxburn
  163.         else
  164.             -- we have more or enough fuel for all items
  165.             fuelmove = math.floor(item.qty/(fuel.burn*furnaceModifier))
  166.             itemmove = item.qty - (item.qty%(fuel.burn*furnaceModifier))
  167.         end
  168.        
  169.         -- debugging
  170.         print("Moving "..tostring(fuelmove).." peices of "..tostring(fuelitem.name).." to fuel slot.")
  171.         print("Moving "..tostring(itemmove).." peices of "..tostring(item.name).." to input slot.")
  172.        
  173.         -- move items
  174.         -- pushItemItemIntoSlot(direction, fromSlot, maxQuantity, intoSlot)
  175.         input.pushItemIntoSlot(inputToFurnace,fuelslot,fuelmove,fuelSlot)
  176.         input.pushItemIntoSlot(inputToFurnace,itemslot,itemmove,inputSlot)
  177.     else
  178.         emptyFurnace()
  179.     end
  180. end
  181.  
  182. print("Checks input chest for valid fuel and items to burn.")
  183. print("If theres items in the furnace i shall not insert more.")
  184. print("Checks "..updaterate.." times a second,")
  185. print("i.e. "..1/updaterate.." seconds between each update.")
  186. while true do
  187.     cook()
  188.     sleep(1/updaterate)
  189. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement