OfficialStamper

botLog2Coal.lua

Feb 2nd, 2023 (edited)
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.68 KB | Gaming | 0 0
  1. --Get Wood, Cook, Deliver
  2.  
  3. local ExoFlame = false
  4.  
  5. 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
  6. checkSourceTime = 20 --Override for dev/testing
  7. 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
  8. fuelCookTime = 6.65 --Exoflame
  9. fuelCookTime = 6.65 --Reuse Charcoal
  10.  
  11.  
  12. --Each type for overflow and targetFuel MUST be exclusive types
  13. local overflowPeripheralType = {
  14.     ['minecraft:barrel'] = { ['startSlot'] = 1 }
  15.     }
  16.  
  17. --Each type for overflow and targetFuel MUST be exclusive types
  18. local targetFuelPeripheralType = {
  19.     ['minecraft:chest'] = { ['startSlot'] = 1 }
  20.     }
  21.  
  22.  
  23. local fuelSourcePeripheralTypes = {
  24.     ['botanypots:botany_pot'] = { ['startSlot'] = 3 },
  25.     ['minecraft:trapped_chest'] = { ['startSlot'] = 1 }
  26.     }
  27.  
  28. local modemTypes = {
  29.     ['modem'] = true
  30.     }
  31.  
  32. local fuelSourceItems = {
  33.     ['minecraft:logs'] = true,
  34.     ['minecraft:log'] = true
  35.     }
  36.  
  37. local furnacePeripheralTypes = {
  38.     ['minecraft:furnace'] = true
  39.     }
  40.  
  41. local _furnaceSide = {
  42.     ['top'] = 1,
  43.     ['side'] = 2,
  44.     ['bottom'] = 3
  45.     }
  46.  
  47. G_OBJ_TYPE = {
  48.     ['table'] = 'table'
  49.     }
  50.  
  51. G_EVENT = {
  52.     ['timer'] = 'timer',
  53.     ['peripheral'] = 'peripheral',
  54.     ['peripheral_detach'] = 'peripheral_detach'
  55.     }
  56.  
  57. local function writeTable(tbl, fileName)
  58.     local fh = io.open(fileName, 'w')
  59.     fh:write(textutils.serialise(tbl))
  60.     fh:write("\n")
  61.     fh:write("---------------------------------------------------- \n")
  62.     for i, v in pairs(tbl) do
  63.         if type(v) == G_OBJ_TYPE.table then
  64.             fh:write(i.."\n")
  65.             for j, n in pairs(v) do
  66.                 fh:write(j.." "..n.."\n")
  67.             end
  68.         else
  69.             fh:write(i.." "..v.."\n")
  70.         end
  71.     end
  72.     fh:flush()
  73.     fh:close()
  74. end
  75.  
  76. local function buildPeripheralTable(inTable)
  77.     local retTable = inTable
  78.     local pList = peripheral.getNames()
  79.     for i, v in pairs(peripheral.getNames()) do
  80.         --Get the type of peripheral and add it to the return table, with its id and fullname
  81.         local strFullName = v
  82.         local strType = peripheral.getType(v)
  83.         local strId = string.gsub(v, strType.."_", "")
  84.         if type(retTable[strType]) == G_OBJ_TYPE.table then
  85.             retTable[strType][tonumber(strId) or 0] = strFullName
  86.         else
  87.             error("function: buildPeripheralTable: Unknown peripheral type: "..strType)
  88.         end
  89.     end
  90.     return retTable
  91. end
  92.  
  93. local function selectPeripheralType(tblAll, pType)
  94.     -- Get all the furnaces from the generated peripheral table above
  95.     local retTable = {}
  96.     local idx = 1
  97.     for i, v in pairs(pType) do
  98.         if type(tblAll[i]) == G_OBJ_TYPE.table then
  99.             for j, t in pairs(tblAll[i]) do
  100.                 retTable[idx] = t
  101.                 idx = idx + 1
  102.             end
  103.         else
  104.             print("info:Type '"..i.."' not being used:  Type:"..type(tblAll[i]))
  105.         end
  106.     end
  107.     return retTable
  108. end
  109.  
  110. local function initPeripherals()
  111.     -- Initialise, at startup and also if a new peripheral is added, or some other major event (QED)
  112.     --Build skeleton peripheral table
  113.     local pAll = {}
  114.     for i, _ in pairs(modemTypes) do
  115.         pAll[i] = {}
  116.     end
  117.     for i, _ in pairs(fuelSourcePeripheralTypes) do
  118.         pAll[i] = {}
  119.     end
  120.     for i, _ in pairs(furnacePeripheralTypes) do
  121.         pAll[i] = {}
  122.     end
  123.     for i, _ in pairs(targetFuelPeripheralType) do
  124.         pAll[i] = {}
  125.     end
  126.     for i, _ in pairs(overflowPeripheralType) do
  127.         pAll[i] = {}
  128.     end
  129.  
  130.     -- Get all the peripherals connected
  131.     local peripheralTable = buildPeripheralTable(pAll)
  132.  
  133.     --build the return tables for each type
  134.     local retSources = selectPeripheralType(pAll, fuelSourcePeripheralTypes)
  135.     local retFurnaces = selectPeripheralType(pAll, furnacePeripheralTypes)
  136.     local retOverflow = selectPeripheralType(pAll, overflowPeripheralType)
  137.     local retTarget = selectPeripheralType(pAll, targetFuelPeripheralType)
  138.  
  139.     if #retOverflow < 1 then
  140.         print("WARNING: No overflow inventory exists!!!")
  141.     end
  142.     if #retSources < 1 then
  143.         print("WARNING: No source inventory (flowerpots) exist!!!")
  144.     end
  145.     if #retFurnaces < 1 then
  146.         print("WARNING: No furnaces exist!!!")
  147.     end
  148.     if #retTarget < 1 then
  149.         print("WARNING: No target inventory (chest attached to turtle) exists!!!")
  150.     end
  151.  
  152.     return true, peripheralTable, retSources, retFurnaces, retOverflow, retTarget
  153.  
  154. end
  155.  
  156. local function processSource(tSource, tTarget, tType, tOverflow, furnaceSide)
  157.  
  158.     --For each inventory
  159.     for _, v in pairs(tSource) do
  160.         local p = peripheral.wrap(v)
  161.         --Get slots
  162.         local slotCount = p.size()
  163.         --For each slot
  164.         invType = peripheral.getType(p)
  165.         startSlot = fuelSourcePeripheralTypes[invType].startSlot
  166.         for s = startSlot, slotCount do
  167.             local sent = 0
  168.             --How many items in slot
  169.             local item = p.getItemDetail(s)
  170.             if item then
  171.                 local itemCount = item.count
  172.                 --For each Overflow storage
  173.                 for _, o in pairs(tOverflow) do
  174.                     --Move to overflow
  175.                     sent = sent + p.pushItems(o, s)
  176.                     if sent >= itemCount then break; end
  177.                 end
  178.             end
  179.         end
  180.     end
  181.  
  182.     --For each overflow (future)
  183.     for _, v in pairs(tOverflow) do
  184.         local p = peripheral.wrap(v)
  185.         --Get slots
  186.         local slotCount = p.size()
  187.         --For each slot
  188.         for s = 1, slotCount do
  189.             --How many items in slot
  190.             local item = p.getItemDetail(s)
  191.             if item then
  192.                 for t in pairs(tType) do
  193.                     if item.tags[t] then
  194.                         --For each target storage/furnace
  195.                         local amt, y = math.modf(item.count / #tTarget)
  196.                         amt = amt + math.ceil(y)
  197.                         for _, o in pairs(tTarget) do
  198.                             --Move to target/furnace
  199.                             p.pushItems(o, s, amt, furnaceSide.top)  --only push to slot 1 (top of furnace)
  200.                         end
  201.                     end
  202.                 end
  203.             end
  204.         end
  205.     end
  206. end
  207.  
  208. local function processCoal(tSource, tTarget, furnaceSide, ExoFlame)
  209.     -- for each furnace, take the product (coal, bottom slot) and deliver to target inventory (typically attached to the turtle)
  210.     for _, f in pairs(tSource) do
  211.         local p = peripheral.wrap(f)
  212.  
  213.         if not ExoFlame then
  214.             local item = p.getItemDetail(furnaceSide.side)
  215.             if not item then
  216.                 p.pushItems(f, furnaceSide.bottom, 1, furnaceSide.side)
  217.             end
  218.         end
  219.  
  220.         --How many items in slot
  221.         local item = p.getItemDetail(furnaceSide.bottom)
  222.         if item then
  223.             local amt, y = math.modf(item.count / #tTarget)
  224.             amt = amt + math.ceil(y)
  225.             for _, t in pairs(tTarget) do
  226.                 p.pushItems(t, furnaceSide.bottom, amt)
  227.             end
  228.         end
  229.     end
  230. end
  231.  
  232. local function parallelSource(tSources, tFurnaces, fuelSourceItems, tOverflow, furnaceSide, waitTime)
  233.  
  234.     local waitTime = checkSourceTime
  235.     local event
  236.     local id = 0
  237.     local timerId
  238.  
  239.     while true do
  240.         print("Process source: "..id)
  241.         processSource(tSources, tFurnaces, fuelSourceItems, tOverflow, furnaceSide)
  242.         timerId = os.startTimer(waitTime)
  243.         print("Next source: "..timerId.." for "..waitTime.." secs")
  244.         repeat
  245.             event, id = os.pullEvent(G_EVENT.timer)
  246.             print("Event fired:"..event.." : "..id)
  247.         until (id == timerId)
  248.     end
  249.  
  250. end
  251.  
  252. local function parallelTarget(tFurnaces, tTarget, furnaceSide, waitTime, ExoFlame)
  253.  
  254.     local event
  255.     local id = 0
  256.     local timerId
  257.  
  258.     while true do
  259.         print("Process furnace/target: "..id)
  260.         processCoal(tFurnaces, tTarget, furnaceSide, ExoFlame)
  261.         timerId = os.startTimer(waitTime)
  262.         print("Next furnace: "..timerId.." for "..waitTime.." secs")
  263.         repeat
  264.             event, id = os.pullEvent(G_EVENT.timer)
  265.             print("Event fired:"..event.." : "..id)
  266.         until (id == timerId)
  267.     end
  268.  
  269. end
  270.  
  271. local function parallelChange()
  272.     --If any change is made to the connected peripherals,
  273.     --we need this funtion to terminate and return control to the main function
  274.     --so that the system is reinitialised and parallel can resume (or basically start again)
  275.     local event
  276.     repeat
  277.         event = os.pullEvent()
  278.     until (event == G_EVENT.peripheral or event == G_EVENT.peripheral_detach)
  279.  
  280.     print'Peripheral change on the network, re-initialising...'
  281.  
  282. end
  283.  
  284. local rc
  285. local _pAll = {}
  286. local _tSources = {}
  287. local _tFurnaces = {}
  288. local _tOverflow = {}
  289. local _tTarget = {}
  290.  
  291. while true do
  292.  
  293.     rc, _pAll, _tSources, _tFurnaces, _tOverflow, _tTarget = initPeripherals()
  294.     --Debug
  295.     writeTable(_pAll, 'all.txt')
  296.     writeTable(_tSources, 'sources.txt')
  297.     writeTable(_tFurnaces, 'furnaces.txt')
  298.     writeTable(_tOverflow, 'overflow.txt')
  299.     writeTable(_tTarget, 'target.txt')
  300.     --End Debug
  301.  
  302.     parallel.waitForAny(
  303.         function() parallelSource(_tSources, _tFurnaces, fuelSourceItems, _tOverflow, _furnaceSide, checkSourceTime) end,
  304.         function() parallelTarget(_tFurnaces, _tTarget, _furnaceSide, fuelCookTime, ExoFlame) end,
  305.         parallelChange
  306.         )
  307.  
  308. end
  309.  
Add Comment
Please, Sign In to add comment