Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Get Wood, Cook, Deliver
- local ExoFlame = false
- local checkSourceTime = 120 --typically it takes 2 minute for a botany pot to grow 'something', so pointless checking the inventory more often than this checkSourceTime
- checkSourceTime = 20 --Override for dev/testing
- local fuelCookTime = 6.65 --Time it takes for a furnace to cook/make 1 piece of coal. This value will be used to attempt getting 9 pieces per round trip, per target device/chest/turtle
- fuelCookTime = 6.65 --Exoflame
- fuelCookTime = 6.65 --Reuse Charcoal
- --Each type for overflow and targetFuel MUST be exclusive types
- local overflowPeripheralType = {
- ['minecraft:barrel'] = { ['startSlot'] = 1 }
- }
- --Each type for overflow and targetFuel MUST be exclusive types
- local targetFuelPeripheralType = {
- ['minecraft:chest'] = { ['startSlot'] = 1 }
- }
- local fuelSourcePeripheralTypes = {
- ['botanypots:botany_pot'] = { ['startSlot'] = 3 },
- ['minecraft:trapped_chest'] = { ['startSlot'] = 1 }
- }
- local modemTypes = {
- ['modem'] = true
- }
- local fuelSourceItems = {
- ['minecraft:logs'] = true,
- ['minecraft:log'] = true
- }
- local furnacePeripheralTypes = {
- ['minecraft:furnace'] = true
- }
- local _furnaceSide = {
- ['top'] = 1,
- ['side'] = 2,
- ['bottom'] = 3
- }
- G_OBJ_TYPE = {
- ['table'] = 'table'
- }
- G_EVENT = {
- ['timer'] = 'timer',
- ['peripheral'] = 'peripheral',
- ['peripheral_detach'] = 'peripheral_detach'
- }
- local function writeTable(tbl, fileName)
- local fh = io.open(fileName, 'w')
- fh:write(textutils.serialise(tbl))
- fh:write("\n")
- fh:write("---------------------------------------------------- \n")
- for i, v in pairs(tbl) do
- if type(v) == G_OBJ_TYPE.table then
- fh:write(i.."\n")
- for j, n in pairs(v) do
- fh:write(j.." "..n.."\n")
- end
- else
- fh:write(i.." "..v.."\n")
- end
- end
- fh:flush()
- fh:close()
- end
- local function buildPeripheralTable(inTable)
- local retTable = inTable
- local pList = peripheral.getNames()
- for i, v in pairs(peripheral.getNames()) do
- --Get the type of peripheral and add it to the return table, with its id and fullname
- local strFullName = v
- local strType = peripheral.getType(v)
- local strId = string.gsub(v, strType.."_", "")
- if type(retTable[strType]) == G_OBJ_TYPE.table then
- retTable[strType][tonumber(strId) or 0] = strFullName
- else
- error("function: buildPeripheralTable: Unknown peripheral type: "..strType)
- end
- end
- return retTable
- end
- local function selectPeripheralType(tblAll, pType)
- -- Get all the furnaces from the generated peripheral table above
- local retTable = {}
- local idx = 1
- for i, v in pairs(pType) do
- if type(tblAll[i]) == G_OBJ_TYPE.table then
- for j, t in pairs(tblAll[i]) do
- retTable[idx] = t
- idx = idx + 1
- end
- else
- print("info:Type '"..i.."' not being used: Type:"..type(tblAll[i]))
- end
- end
- return retTable
- end
- local function initPeripherals()
- -- Initialise, at startup and also if a new peripheral is added, or some other major event (QED)
- --Build skeleton peripheral table
- local pAll = {}
- for i, _ in pairs(modemTypes) do
- pAll[i] = {}
- end
- for i, _ in pairs(fuelSourcePeripheralTypes) do
- pAll[i] = {}
- end
- for i, _ in pairs(furnacePeripheralTypes) do
- pAll[i] = {}
- end
- for i, _ in pairs(targetFuelPeripheralType) do
- pAll[i] = {}
- end
- for i, _ in pairs(overflowPeripheralType) do
- pAll[i] = {}
- end
- -- Get all the peripherals connected
- local peripheralTable = buildPeripheralTable(pAll)
- --build the return tables for each type
- local retSources = selectPeripheralType(pAll, fuelSourcePeripheralTypes)
- local retFurnaces = selectPeripheralType(pAll, furnacePeripheralTypes)
- local retOverflow = selectPeripheralType(pAll, overflowPeripheralType)
- local retTarget = selectPeripheralType(pAll, targetFuelPeripheralType)
- if #retOverflow < 1 then
- print("WARNING: No overflow inventory exists!!!")
- end
- if #retSources < 1 then
- print("WARNING: No source inventory (flowerpots) exist!!!")
- end
- if #retFurnaces < 1 then
- print("WARNING: No furnaces exist!!!")
- end
- if #retTarget < 1 then
- print("WARNING: No target inventory (chest attached to turtle) exists!!!")
- end
- return true, peripheralTable, retSources, retFurnaces, retOverflow, retTarget
- end
- local function processSource(tSource, tTarget, tType, tOverflow, furnaceSide)
- --For each inventory
- for _, v in pairs(tSource) do
- local p = peripheral.wrap(v)
- --Get slots
- local slotCount = p.size()
- --For each slot
- invType = peripheral.getType(p)
- startSlot = fuelSourcePeripheralTypes[invType].startSlot
- for s = startSlot, slotCount do
- local sent = 0
- --How many items in slot
- local item = p.getItemDetail(s)
- if item then
- local itemCount = item.count
- --For each Overflow storage
- for _, o in pairs(tOverflow) do
- --Move to overflow
- sent = sent + p.pushItems(o, s)
- if sent >= itemCount then break; end
- end
- end
- end
- end
- --For each overflow (future)
- for _, v in pairs(tOverflow) do
- local p = peripheral.wrap(v)
- --Get slots
- local slotCount = p.size()
- --For each slot
- for s = 1, slotCount do
- --How many items in slot
- local item = p.getItemDetail(s)
- if item then
- for t in pairs(tType) do
- if item.tags[t] then
- --For each target storage/furnace
- local amt, y = math.modf(item.count / #tTarget)
- amt = amt + math.ceil(y)
- for _, o in pairs(tTarget) do
- --Move to target/furnace
- p.pushItems(o, s, amt, furnaceSide.top) --only push to slot 1 (top of furnace)
- end
- end
- end
- end
- end
- end
- end
- local function processCoal(tSource, tTarget, furnaceSide, ExoFlame)
- -- for each furnace, take the product (coal, bottom slot) and deliver to target inventory (typically attached to the turtle)
- for _, f in pairs(tSource) do
- local p = peripheral.wrap(f)
- if not ExoFlame then
- local item = p.getItemDetail(furnaceSide.side)
- if not item then
- p.pushItems(f, furnaceSide.bottom, 1, furnaceSide.side)
- end
- end
- --How many items in slot
- local item = p.getItemDetail(furnaceSide.bottom)
- if item then
- local amt, y = math.modf(item.count / #tTarget)
- amt = amt + math.ceil(y)
- for _, t in pairs(tTarget) do
- p.pushItems(t, furnaceSide.bottom, amt)
- end
- end
- end
- end
- local function parallelSource(tSources, tFurnaces, fuelSourceItems, tOverflow, furnaceSide, waitTime)
- local waitTime = checkSourceTime
- local event
- local id = 0
- local timerId
- while true do
- print("Process source: "..id)
- processSource(tSources, tFurnaces, fuelSourceItems, tOverflow, furnaceSide)
- timerId = os.startTimer(waitTime)
- print("Next source: "..timerId.." for "..waitTime.." secs")
- repeat
- event, id = os.pullEvent(G_EVENT.timer)
- print("Event fired:"..event.." : "..id)
- until (id == timerId)
- end
- end
- local function parallelTarget(tFurnaces, tTarget, furnaceSide, waitTime, ExoFlame)
- local event
- local id = 0
- local timerId
- while true do
- print("Process furnace/target: "..id)
- processCoal(tFurnaces, tTarget, furnaceSide, ExoFlame)
- timerId = os.startTimer(waitTime)
- print("Next furnace: "..timerId.." for "..waitTime.." secs")
- repeat
- event, id = os.pullEvent(G_EVENT.timer)
- print("Event fired:"..event.." : "..id)
- until (id == timerId)
- end
- end
- local function parallelChange()
- --If any change is made to the connected peripherals,
- --we need this funtion to terminate and return control to the main function
- --so that the system is reinitialised and parallel can resume (or basically start again)
- local event
- repeat
- event = os.pullEvent()
- until (event == G_EVENT.peripheral or event == G_EVENT.peripheral_detach)
- print'Peripheral change on the network, re-initialising...'
- end
- local rc
- local _pAll = {}
- local _tSources = {}
- local _tFurnaces = {}
- local _tOverflow = {}
- local _tTarget = {}
- while true do
- rc, _pAll, _tSources, _tFurnaces, _tOverflow, _tTarget = initPeripherals()
- --Debug
- writeTable(_pAll, 'all.txt')
- writeTable(_tSources, 'sources.txt')
- writeTable(_tFurnaces, 'furnaces.txt')
- writeTable(_tOverflow, 'overflow.txt')
- writeTable(_tTarget, 'target.txt')
- --End Debug
- parallel.waitForAny(
- function() parallelSource(_tSources, _tFurnaces, fuelSourceItems, _tOverflow, _furnaceSide, checkSourceTime) end,
- function() parallelTarget(_tFurnaces, _tTarget, _furnaceSide, fuelCookTime, ExoFlame) end,
- parallelChange
- )
- end
Add Comment
Please, Sign In to add comment