jille_Jr icon

CC: Furnace control Computer 1

jille_Jr | PRO | 11/17/13 02:04:45 PM UTC | 0 ⭐ | 393 👁️ | Never ⏰ | []
Lua |

4.7 KB

|

None

|

0 👍

/

0 👎

-- Furnace control
-- Setup:
-- (from above)
-- Comp - Outp
-- Inpu - Furn
-- the second computer under the furnace,
-- and a wired modem connecting the 2 computers
 
-- Computer 1: (this one) http://pastebin.com/06cjixf9
-- Computer 2: http://pastebin.com/RjyREdT2
 
-- Screenshots of setup:
-- 1 http://i.imgur.com/t9GFE2r.png
-- 2 http://i.imgur.com/WjnunBC.png
 
-- how often computer shall retry
local updaterate = 2 -- times a sec
 
-- where the chests are in relation to the furnace
local outputToFurnace = "north"
local inputToFurnace = "west"
-- directions: north,south,east,west,up,down
 
-- slots in the furnace inventory
local inputSlot = 0
local fuelSlot = 1
local outputSlot = 2
 
-- 1 for normal furnace
-- 1.25 for IC2 iron furnace
local furnaceModifier = 1
 
local fuel = {
    -- {id,dmg,burntime},
    {id=263,dmg=0,burn=8},--coal
    {id=263,dmg=1,burn=8},--charcoal
    {id=5,dmg=-1,burn=1.5},--planks
    --{id=17,dmg=-1,burn=1.5},--wood
    {id=6,dmg=-1,burn=.5},--sapling
    {id=280,dmg=0,burn=.5},--stick
}
 
local input = peripheral.wrap("front")
or error("Unable to wrap input chest!",0)
local output = peripheral.wrap("right")
or error("Unable to wrap output chest!",0)
 
local modemside = "bottom"
local modem
if peripheral.getType(modemside) == "modem" then
    modem = peripheral.wrap(modemside)
    or error("Unable to wrap modem!",0)
else error("No modem found, please correct!",0) end
 
local function getAllStacks(inv)
    local t = {}
    local size = inv.getInventorySize()
    for slot = 1,size do
        local item = inv.getStackInSlot(slot) or {}
        
        item.rawName = item.rawName or ""
        item.qty = item.qty or 0
        item.dmg = item.dmg or 0
        item.name = item.name or ""
        item.id = item.id or 0
        
        t[slot] = item
    end
    return t
end
 
-- statement = boolean
-- fuel = table
local function isFuel(item)
    for _,fuel in pairs(fuel) do
        if item.id == fuel.id
        and (item.dmg == fuel.dmg
        or fuel.dmg == -1) then
            return true,fuel
        end
    end
    return false
end
 
-- returns slot,fuel,item
local function findFuel()
    local items = getAllStacks(input)
    for slot,item in pairs(items) do
        local state,fuel = isFuel(item)
        if state then
            return slot,fuel,item
        end
    end
end
 
-- returns slot,item
local function findItem()
    local slot,fuel = findFuel()
    if slot and fuel then
        local items = getAllStacks(input)
        for slot,item in pairs(items) do
            local state = isFuel(item)
            if not state then
                if item.qty >= fuel.burn then
                    return slot,item
                end
            end
        end
    end
end
 
local function emptyFurnace()
    output.pullItem(outputToFurnace,outputSlot,64)
end
 
local function isFurnaceEmpty()
    local timeout = os.startTimer(2)
    modem.open(1)
    local msg = textutils.serialize({task="furnace",value="question"})
    modem.transmit(1,1,msg)
    
    local state = false
    
    while true do
        local ev,p1,p2,p3,p4,p5 = os.pullEvent()
        if ev == "modem_message" and p1 == modemside and p2 == 1 then
            local msg = textutils.unserialize(p4)
            if type(msg) == "table" then
                if msg.task == "furnace"
                and type(msg.value) == "boolean" then
                    state = msg.value
                    break
                end
            end
        elseif ev == "timer" then
            if p1 == timeout then
                state = false
                break
            end
        end
    end
    modem.close(1)
    return state
end
 
local function cook()
    -- check with computer
    if isFurnaceEmpty() then
        -- get fuel and item
        local fuelslot,fuel,fuelitem = findFuel()
        local itemslot,item = findItem()
        
        -- no fuel/item found: cancel out
        if not fuel then return end
        if not item then return end
        
        local maxburn = fuelitem.qty*fuel.burn
        local fuelmove,itemmove
        if item.qty > maxburn * furnaceModifier then
            -- # of items are more then we can burn
            fuelmove = fuelitem.qty
            itemmove = maxburn
        else
            -- we have more or enough fuel for all items
            fuelmove = math.floor(item.qty/(fuel.burn*furnaceModifier))
            itemmove = item.qty - (item.qty%(fuel.burn*furnaceModifier))
        end
        
        -- debugging
        print("Moving "..tostring(fuelmove).." peices of "..tostring(fuelitem.name).." to fuel slot.")
        print("Moving "..tostring(itemmove).." peices of "..tostring(item.name).." to input slot.")
        
        -- move items
        -- pushItemItemIntoSlot(direction, fromSlot, maxQuantity, intoSlot)
        input.pushItemIntoSlot(inputToFurnace,fuelslot,fuelmove,fuelSlot)
        input.pushItemIntoSlot(inputToFurnace,itemslot,itemmove,inputSlot)
    else
        emptyFurnace()
    end
end
 
print("Checks input chest for valid fuel and items to burn.")
print("If theres items in the furnace i shall not insert more.")
print("Checks "..updaterate.." times a second,")
print("i.e. "..1/updaterate.." seconds between each update.")
while true do
    cook()
    sleep(1/updaterate)
end

Comments