Naddox

DOMUM ORNAMENTUM MATERIAL STAGER

Aug 31st, 2026 (edited)
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 37.39 KB | Gaming | 0 0
  1. -- ============================================================
  2. -- MineColonies Domum Ornamentum Material Stager v3
  3. -- Multi-Builder Edition
  4. -- ============================================================
  5. --
  6. -- PURPOSE
  7. --   Scans every active/staffed Builder's Hut in the colony,
  8. --   reads Domum Ornamentum requirements from BOTH:
  9. --
  10. --     1. Builder Hut Required Resources
  11. --     2. The Builder citizen's personal requests
  12. --
  13. --   For each Builder, duplicate demand is merged using MAX:
  14. --
  15. --     Hut needs Framed Block x25
  16. --     Builder requests Framed Block x25
  17. --     => that Builder contributes x25, NOT x50
  18. --
  19. --   Demand from DIFFERENT Builders is then ADDED:
  20. --
  21. --     Builder A needs x25
  22. --     Builder B needs x10
  23. --     => stage materials for x35
  24. --
  25. -- OUTPUT
  26. --   One shared inventory directly ABOVE the dedicated
  27. --   Architect-materials ME Bridge.
  28. --
  29. -- ME BRIDGE DISCOVERY
  30. --   All ME Bridges named builder_# are ignored.
  31. --   If exactly one other ME Bridge is present, it is used.
  32. --
  33. --   Optional: set OUTPUT_BRIDGE_CUSTOM_NAME below if you want
  34. --   to explicitly select a bridge by its anvil/custom name.
  35. --
  36. -- AE2
  37. --   BEFORE staging raw materials, the script checks AE2 for
  38. --   the exact requested Domum Ornamentum finished block.
  39. --   Existing finished blocks reduce the amount that needs
  40. --   raw materials staged.
  41. --
  42. --   Stored raw materials are exported immediately.
  43. --   Missing raw materials are autocrafted when possible.
  44. --
  45. -- IMPORTANT
  46. --   The material calculation is deliberately conservative:
  47. --   requested DO output count x occurrences of each material.
  48. --   This can stage extra material for Cutter recipes that yield
  49. --   multiple output blocks, but it should not under-supply them.
  50. --
  51. -- ============================================================
  52.  
  53.  
  54. -- ============================================================
  55. -- CONFIG
  56. -- ============================================================
  57.  
  58. local OUTPUT_SIDE = "up"
  59. local POLL_INTERVAL = 5
  60. local CRAFT_RETRY_SECONDS = 30
  61.  
  62. -- When the Colony Supplier removes a finished DO block from
  63. -- AE2, MineColonies may take a few seconds to notice it in the
  64. -- Builder storage. Keep temporary credit for recently-removed
  65. -- finished blocks so this script does not immediately stage
  66. -- unnecessary raw materials during that update window.
  67. local FINISHED_ITEM_HOLD_SECONDS = 20
  68.  
  69. -- nil = automatically use the one ME Bridge that is NOT named
  70. -- builder_#, e.g. the bridge dedicated to this Architect setup.
  71. --
  72. -- Or set:
  73. -- local OUTPUT_BRIDGE_CUSTOM_NAME = "architect_materials"
  74. local OUTPUT_BRIDGE_CUSTOM_NAME = nil
  75.  
  76.  
  77. -- ============================================================
  78. -- PERIPHERALS
  79. -- ============================================================
  80.  
  81. local colony = peripheral.find("colony_integrator")
  82.  
  83. if not colony then
  84.     error("Colony Integrator not found!")
  85. end
  86.  
  87. local colonyName =
  88.     colony.getColonyName()
  89.     or "Unknown Colony"
  90.  
  91.  
  92. -- ============================================================
  93. -- FIND THE ARCHITECT ME BRIDGE
  94. -- ============================================================
  95.  
  96. local function findArchitectBridge()
  97.  
  98.     local candidates = {}
  99.  
  100.     for _, peripheralName
  101.     in ipairs(peripheral.getNames()) do
  102.  
  103.         if peripheral.getType(peripheralName)
  104.             == "me_bridge" then
  105.  
  106.             local bridge =
  107.                 peripheral.wrap(peripheralName)
  108.  
  109.             local customName =
  110.                 bridge.getName
  111.                 and bridge.getName()
  112.                 or ""
  113.  
  114.             customName =
  115.                 tostring(customName or "")
  116.  
  117.  
  118.             if OUTPUT_BRIDGE_CUSTOM_NAME then
  119.  
  120.                 if customName
  121.                     == OUTPUT_BRIDGE_CUSTOM_NAME then
  122.  
  123.                     return bridge,
  124.                            peripheralName,
  125.                            customName
  126.  
  127.                 end
  128.  
  129.             else
  130.  
  131.                 -- Colony supplier bridges are deliberately
  132.                 -- named builder_1, builder_2, builder_3, ...
  133.                 if not customName:match(
  134.                     "^builder_%d+$"
  135.                 ) then
  136.  
  137.                     table.insert(
  138.                         candidates,
  139.                         {
  140.                             bridge = bridge,
  141.                             peripheralName =
  142.                                 peripheralName,
  143.                             customName =
  144.                                 customName
  145.                         }
  146.                     )
  147.  
  148.                 end
  149.  
  150.             end
  151.         end
  152.     end
  153.  
  154.  
  155.     if OUTPUT_BRIDGE_CUSTOM_NAME then
  156.  
  157.         error(
  158.             "No ME Bridge found with custom name '"
  159.             .. OUTPUT_BRIDGE_CUSTOM_NAME
  160.             .. "'."
  161.         )
  162.  
  163.     end
  164.  
  165.  
  166.     if #candidates == 0 then
  167.  
  168.         error(
  169.             "No non-builder ME Bridge found for "
  170.             .. "architect_materials."
  171.         )
  172.  
  173.     end
  174.  
  175.  
  176.     if #candidates > 1 then
  177.  
  178.         local names = {}
  179.  
  180.         for _, candidate
  181.         in ipairs(candidates) do
  182.  
  183.             table.insert(
  184.                 names,
  185.                 candidate.peripheralName
  186.                 .. "='"
  187.                 .. candidate.customName
  188.                 .. "'"
  189.             )
  190.  
  191.         end
  192.  
  193.         error(
  194.             "Multiple non-builder ME Bridges found: "
  195.             .. table.concat(names, ", ")
  196.             .. ". Set OUTPUT_BRIDGE_CUSTOM_NAME."
  197.         )
  198.  
  199.     end
  200.  
  201.  
  202.     local selected =
  203.         candidates[1]
  204.  
  205.     return selected.bridge,
  206.            selected.peripheralName,
  207.            selected.customName
  208. end
  209.  
  210.  
  211. local me,
  212.       mePeripheralName,
  213.       meCustomName =
  214.     findArchitectBridge()
  215.  
  216.  
  217. -- ============================================================
  218. -- RUNTIME STATE
  219. -- ============================================================
  220.  
  221. -- staged[exact_DO_variant].materials[raw_material] = number sent
  222. --
  223. -- State is retained while that DO requirement remains active.
  224. -- When the requirement disappears completely, its state is
  225. -- forgotten so a future new request can be staged again.
  226.  
  227. local staged = {}
  228.  
  229. -- Do not repeatedly hammer AE2 with the same craft request.
  230. local craftRetry = {}
  231.  
  232. -- Suppress duplicate terminal warnings.
  233. local warned = {}
  234.  
  235. -- Tracks exact finished DO item stock changes in AE2.
  236. -- If stock suddenly drops, it was likely pulled by the Colony
  237. -- Supplier, so the previous amount remains credited briefly.
  238. local finishedItemState = {}
  239.  
  240.  
  241. -- ============================================================
  242. -- BASIC HELPERS
  243. -- ============================================================
  244.  
  245. local function now()
  246.     return os.epoch("utc") / 1000
  247. end
  248.  
  249.  
  250. local function num(value)
  251.  
  252.     if type(value) == "number" then
  253.         return value
  254.     end
  255.  
  256.     if value == true then
  257.         return 1
  258.     end
  259.  
  260.     return 0
  261. end
  262.  
  263.  
  264. local function posKey(pos)
  265.  
  266.     return tostring(pos.x)
  267.         .. ","
  268.         .. tostring(pos.y)
  269.         .. ","
  270.         .. tostring(pos.z)
  271. end
  272.  
  273.  
  274. local function samePos(a, b)
  275.  
  276.     return a
  277.        and b
  278.        and a.x == b.x
  279.        and a.y == b.y
  280.        and a.z == b.z
  281. end
  282.  
  283.  
  284. local function cleanName(name)
  285.  
  286.     if not name then
  287.         return "Unknown Item"
  288.     end
  289.  
  290.     name = tostring(name)
  291.     name = name:gsub("^%[", "")
  292.     name = name:gsub("%]$", "")
  293.  
  294.     return name
  295. end
  296.  
  297.  
  298. local function getDisplayName(
  299.     item,
  300.     fallback
  301. )
  302.  
  303.     if fallback
  304.     and fallback ~= "" then
  305.  
  306.         return cleanName(fallback)
  307.  
  308.     end
  309.  
  310.     if item
  311.     and item.displayName then
  312.  
  313.         return cleanName(
  314.             item.displayName
  315.         )
  316.  
  317.     end
  318.  
  319.     if item
  320.     and item.name then
  321.  
  322.         return tostring(item.name)
  323.  
  324.     end
  325.  
  326.     return "Unknown Item"
  327. end
  328.  
  329.  
  330. local function isDomum(item)
  331.  
  332.     return item
  333.        and type(item.name) == "string"
  334.        and item.name:match(
  335.             "^domum_ornamentum:"
  336.        ) ~= nil
  337. end
  338.  
  339.  
  340. local function variantKey(item)
  341.  
  342.     if not item then
  343.         return nil
  344.     end
  345.  
  346.     if item.fingerprint then
  347.  
  348.         return "fp:"
  349.             .. tostring(
  350.                 item.fingerprint
  351.             )
  352.  
  353.     end
  354.  
  355.     return tostring(item.name)
  356.         .. ":"
  357.         .. textutils.serialize(
  358.             item.components or {}
  359.         )
  360. end
  361.  
  362.  
  363. -- ============================================================
  364. -- AE2 HELPERS
  365. -- ============================================================
  366.  
  367. local function itemFilter(
  368.     name,
  369.     count
  370. )
  371.  
  372.     local filter = {
  373.         name = name
  374.     }
  375.  
  376.     if count then
  377.         filter.count = count
  378.     end
  379.  
  380.     return filter
  381. end
  382.  
  383.  
  384. local function storedCount(result)
  385.  
  386.     if not result then
  387.         return 0
  388.     end
  389.  
  390.     if type(result.count)
  391.         == "number" then
  392.  
  393.         return result.count
  394.     end
  395.  
  396.     if type(result.amount)
  397.         == "number" then
  398.  
  399.         return result.amount
  400.     end
  401.  
  402.     return 0
  403. end
  404.  
  405.  
  406. local function getStored(name)
  407.  
  408.     local ok, result =
  409.         pcall(
  410.             me.getItem,
  411.             itemFilter(name)
  412.         )
  413.  
  414.     if not ok
  415.     or not result then
  416.  
  417.         return 0
  418.     end
  419.  
  420.     return storedCount(result)
  421. end
  422.  
  423.  
  424. -- ============================================================
  425. -- EXACT FINISHED DOMUM ITEM LOOKUP
  426. -- ============================================================
  427. --
  428. -- Domum Ornamentum variants can share the same registry ID while
  429. -- differing by fingerprint/components. For the FINISHED block
  430. -- check, match the exact requested stack instead of only its name.
  431. -- ============================================================
  432.  
  433. local function exactItemFilter(item)
  434.  
  435.     if item
  436.     and item.fingerprint then
  437.  
  438.         return {
  439.             fingerprint =
  440.                 item.fingerprint
  441.         }
  442.     end
  443.  
  444.  
  445.     local filter = {
  446.         name = item.name
  447.     }
  448.  
  449.  
  450.     if item.components
  451.     and next(item.components) then
  452.  
  453.         filter.components =
  454.             item.components
  455.     end
  456.  
  457.  
  458.     return filter
  459. end
  460.  
  461.  
  462. local function getExactFinishedStored(item)
  463.  
  464.     local ok, result =
  465.         pcall(
  466.             me.getItem,
  467.             exactItemFilter(item)
  468.         )
  469.  
  470.  
  471.     if not ok
  472.     or not result then
  473.  
  474.         return 0
  475.     end
  476.  
  477.  
  478.     return storedCount(result)
  479. end
  480.  
  481.  
  482. local function getEffectiveFinishedStored(
  483.     key,
  484.     item
  485. )
  486.  
  487.     local stored =
  488.         getExactFinishedStored(
  489.             item
  490.         )
  491.  
  492.  
  493.     local currentTime =
  494.         now()
  495.  
  496.  
  497.     local state =
  498.         finishedItemState[key]
  499.  
  500.  
  501.     if not state then
  502.  
  503.         state = {
  504.             lastStored = stored,
  505.             holdAmount = 0,
  506.             holdUntil = 0
  507.         }
  508.  
  509.  
  510.         finishedItemState[key] =
  511.             state
  512.     end
  513.  
  514.  
  515.     -- If exact finished stock drops, assume the Colony Supplier
  516.     -- may just have exported it to Builder storage. Preserve the
  517.     -- previous stock count briefly while MineColonies catches up.
  518.     if stored < state.lastStored then
  519.  
  520.         state.holdAmount =
  521.             math.max(
  522.                 state.holdAmount,
  523.                 state.lastStored
  524.             )
  525.  
  526.  
  527.         state.holdUntil =
  528.             currentTime
  529.             + FINISHED_ITEM_HOLD_SECONDS
  530.     end
  531.  
  532.  
  533.     state.lastStored =
  534.         stored
  535.  
  536.  
  537.     local effective =
  538.         stored
  539.  
  540.  
  541.     if currentTime
  542.        < state.holdUntil then
  543.  
  544.         effective =
  545.             math.max(
  546.                 effective,
  547.                 state.holdAmount
  548.             )
  549.  
  550.     else
  551.  
  552.         state.holdAmount = 0
  553.         state.holdUntil = 0
  554.     end
  555.  
  556.  
  557.     return effective
  558. end
  559.  
  560.  
  561. local function isCraftable(name)
  562.  
  563.     local ok, result =
  564.         pcall(
  565.             me.isCraftable,
  566.             itemFilter(name)
  567.         )
  568.  
  569.     return ok
  570.        and result == true
  571. end
  572.  
  573.  
  574. local function isCrafting(name)
  575.  
  576.     local ok, result =
  577.         pcall(
  578.             me.isCrafting,
  579.             itemFilter(name)
  580.         )
  581.  
  582.     return ok
  583.        and result == true
  584. end
  585.  
  586.  
  587. local function parseTransferred(result)
  588.  
  589.     if type(result) == "number" then
  590.         return result
  591.     end
  592.  
  593.     if type(result) == "table" then
  594.  
  595.         return result.count
  596.             or result.amount
  597.             or result.transferred
  598.             or 0
  599.  
  600.     end
  601.  
  602.     return 0
  603. end
  604.  
  605.  
  606. local function exportItem(
  607.     name,
  608.     count
  609. )
  610.  
  611.     if count <= 0 then
  612.         return 0
  613.     end
  614.  
  615.  
  616.     local ok, result, err =
  617.         pcall(
  618.             me.exportItem,
  619.             itemFilter(
  620.                 name,
  621.                 count
  622.             ),
  623.             OUTPUT_SIDE
  624.         )
  625.  
  626.  
  627.     if not ok then
  628.  
  629.         print(
  630.             "[EXPORT ERROR] "
  631.             .. name
  632.             .. ": "
  633.             .. tostring(result)
  634.         )
  635.  
  636.         return 0
  637.     end
  638.  
  639.  
  640.     local moved =
  641.         parseTransferred(result)
  642.  
  643.  
  644.     if moved <= 0
  645.     and err then
  646.  
  647.         print(
  648.             "[EXPORT ERROR] "
  649.             .. name
  650.             .. ": "
  651.             .. tostring(err)
  652.         )
  653.  
  654.     end
  655.  
  656.  
  657.     return moved
  658. end
  659.  
  660.  
  661. local function craftItem(
  662.     name,
  663.     count
  664. )
  665.  
  666.     if count <= 0 then
  667.         return false
  668.     end
  669.  
  670.  
  671.     local ok, result, err =
  672.         pcall(
  673.             me.craftItem,
  674.             itemFilter(
  675.                 name,
  676.                 count
  677.             )
  678.         )
  679.  
  680.  
  681.     if not ok then
  682.  
  683.         print(
  684.             "[CRAFT ERROR] "
  685.             .. name
  686.             .. ": "
  687.             .. tostring(result)
  688.         )
  689.  
  690.         return false
  691.     end
  692.  
  693.  
  694.     if result then
  695.         return true
  696.     end
  697.  
  698.  
  699.     if err then
  700.  
  701.         print(
  702.             "[CRAFT ERROR] "
  703.             .. name
  704.             .. ": "
  705.             .. tostring(err)
  706.         )
  707.  
  708.     end
  709.  
  710.  
  711.     return false
  712. end
  713.  
  714.  
  715. -- ============================================================
  716. -- FIND EVERY RELEVANT BUILDER HUT
  717. -- ============================================================
  718. --
  719. -- We avoid hardcoding Builder counts or hut coordinates.
  720. --
  721. -- Builder huts are discovered from:
  722. --
  723. --   A. Citizens whose job is MineColonies Builder
  724. --   B. Active BUILD / UPGRADE / REPAIR work orders
  725. --
  726. -- This captures every staffed Builder Hut plus any hut actively
  727. -- handling a construction order.
  728. -- ============================================================
  729.  
  730. local function findBuilderHuts()
  731.  
  732.     local huts = {}
  733.  
  734.  
  735.     -- --------------------------------------------------------
  736.     -- STAFFED BUILDER HUTS
  737.     -- --------------------------------------------------------
  738.  
  739.     local citizens =
  740.         colony.getCitizens()
  741.         or {}
  742.  
  743.  
  744.     for _, citizen
  745.     in ipairs(citizens) do
  746.  
  747.         local work =
  748.             citizen.work
  749.  
  750.  
  751.         if work
  752.         and work.location
  753.         and (
  754.             tostring(work.job)
  755.                 == "com.minecolonies.job.builder"
  756.  
  757.             or tostring(work.name)
  758.                 == "com.minecolonies.building.builder"
  759.         ) then
  760.  
  761.             local key =
  762.                 posKey(
  763.                     work.location
  764.                 )
  765.  
  766.  
  767.             if not huts[key] then
  768.  
  769.                 huts[key] = {
  770.                     pos = work.location,
  771.                     builder = citizen
  772.                 }
  773.  
  774.             else
  775.  
  776.                 huts[key].builder =
  777.                     citizen
  778.  
  779.             end
  780.  
  781.         end
  782.  
  783.     end
  784.  
  785.  
  786.     -- --------------------------------------------------------
  787.     -- ACTIVE CONSTRUCTION WORK ORDERS
  788.     -- --------------------------------------------------------
  789.  
  790.     local orders =
  791.         colony.getWorkOrders()
  792.         or {}
  793.  
  794.  
  795.     for _, order
  796.     in ipairs(orders) do
  797.  
  798.         local workType =
  799.             tostring(
  800.                 order.workOrderType
  801.             )
  802.  
  803.  
  804.         if (
  805.             workType == "BUILD"
  806.             or workType == "UPGRADE"
  807.             or workType == "REPAIR"
  808.         )
  809.         and type(order.builder)
  810.             == "table" then
  811.  
  812.             local key =
  813.                 posKey(
  814.                     order.builder
  815.                 )
  816.  
  817.  
  818.             if not huts[key] then
  819.  
  820.                 huts[key] = {
  821.                     pos = order.builder,
  822.                     builder = nil
  823.                 }
  824.  
  825.             end
  826.  
  827.  
  828.             huts[key].order =
  829.                 order
  830.  
  831.         end
  832.  
  833.     end
  834.  
  835.  
  836.     -- --------------------------------------------------------
  837.     -- IF A WORK-ORDER HUT DID NOT ALREADY HAVE A CITIZEN,
  838.     -- MATCH ITS POSITION AGAINST ALL CITIZENS.
  839.     -- --------------------------------------------------------
  840.  
  841.     for _, hut
  842.     in pairs(huts) do
  843.  
  844.         if not hut.builder then
  845.  
  846.             for _, citizen
  847.             in ipairs(citizens) do
  848.  
  849.                 if citizen.work
  850.                 and citizen.work.location
  851.                 and samePos(
  852.                     citizen.work.location,
  853.                     hut.pos
  854.                 ) then
  855.  
  856.                     hut.builder =
  857.                         citizen
  858.  
  859.                     break
  860.                 end
  861.             end
  862.  
  863.         end
  864.  
  865.     end
  866.  
  867.  
  868.     local result = {}
  869.  
  870.     for _, hut
  871.     in pairs(huts) do
  872.  
  873.         table.insert(
  874.             result,
  875.             hut
  876.         )
  877.  
  878.     end
  879.  
  880.  
  881.     table.sort(
  882.         result,
  883.         function(a, b)
  884.  
  885.             local aName =
  886.                 a.builder
  887.                 and a.builder.name
  888.                 or posKey(a.pos)
  889.  
  890.             local bName =
  891.                 b.builder
  892.                 and b.builder.name
  893.                 or posKey(b.pos)
  894.  
  895.             return string.lower(
  896.                 tostring(aName)
  897.             )
  898.             <
  899.             string.lower(
  900.                 tostring(bName)
  901.             )
  902.  
  903.         end
  904.     )
  905.  
  906.  
  907.     return result
  908. end
  909.  
  910.  
  911. local function requestIsClosed(request)
  912.  
  913.     local state =
  914.         string.upper(
  915.             tostring(
  916.                 request.state or ""
  917.             )
  918.         )
  919.  
  920.  
  921.     return state:find(
  922.         "COMPLETE",
  923.         1,
  924.         true
  925.     ) ~= nil
  926.  
  927.     or state:find(
  928.         "CANCEL",
  929.         1,
  930.         true
  931.     ) ~= nil
  932.  
  933.     or state == "DONE"
  934.  
  935.     or state == "RESOLVED"
  936. end
  937.  
  938.  
  939. -- ============================================================
  940. -- DOMUM MATERIAL EXTRACTION
  941. -- ============================================================
  942. --
  943. -- Domum Ornamentum stores selected input textures/materials
  944. -- inside the requested item's component/custom-data structure.
  945. --
  946. -- We recursively locate branches whose key contains "texture"
  947. -- and collect resource-location strings from their VALUES.
  948. --
  949. -- Repeated material IDs are counted. If the same raw block is
  950. -- used in two Cutter material slots, it is staged twice per
  951. -- requested output.
  952. -- ============================================================
  953.  
  954. local function validResourceId(value)
  955.  
  956.     return type(value) == "string"
  957.        and value:match(
  958.             "^[%w_.-]+:[%w_./-]+$"
  959.        ) ~= nil
  960. end
  961.  
  962.  
  963. local function collectTextureValues(
  964.     value,
  965.     result
  966. )
  967.  
  968.     if type(value) == "string" then
  969.  
  970.         if validResourceId(value) then
  971.  
  972.             result[value] =
  973.                 (result[value] or 0)
  974.                 + 1
  975.  
  976.         end
  977.  
  978.         return
  979.     end
  980.  
  981.  
  982.     if type(value) ~= "table" then
  983.         return
  984.     end
  985.  
  986.  
  987.     for _, child
  988.     in pairs(value) do
  989.  
  990.         collectTextureValues(
  991.             child,
  992.             result
  993.         )
  994.  
  995.     end
  996. end
  997.  
  998.  
  999. local function findTextureData(
  1000.     value,
  1001.     result
  1002. )
  1003.  
  1004.     if type(value) ~= "table" then
  1005.         return
  1006.     end
  1007.  
  1008.  
  1009.     for key, child
  1010.     in pairs(value) do
  1011.  
  1012.         local keyName =
  1013.             string.lower(
  1014.                 tostring(key)
  1015.             )
  1016.  
  1017.  
  1018.         if keyName:find(
  1019.             "texture",
  1020.             1,
  1021.             true
  1022.         ) then
  1023.  
  1024.             collectTextureValues(
  1025.                 child,
  1026.                 result
  1027.             )
  1028.  
  1029.  
  1030.         elseif type(child)
  1031.             == "table" then
  1032.  
  1033.             findTextureData(
  1034.                 child,
  1035.                 result
  1036.             )
  1037.  
  1038.         end
  1039.  
  1040.     end
  1041. end
  1042.  
  1043.  
  1044. local function materialsFor(item)
  1045.  
  1046.     local result = {}
  1047.  
  1048.  
  1049.     if not item
  1050.     or type(item.components)
  1051.         ~= "table" then
  1052.  
  1053.         return result
  1054.     end
  1055.  
  1056.  
  1057.     findTextureData(
  1058.         item.components,
  1059.         result
  1060.     )
  1061.  
  1062.  
  1063.     -- Requested DO output is never its own raw material.
  1064.     if item.name then
  1065.         result[item.name] = nil
  1066.     end
  1067.  
  1068.  
  1069.     return result
  1070. end
  1071.  
  1072.  
  1073. -- ============================================================
  1074. -- DEMAND HELPERS
  1075. -- ============================================================
  1076.  
  1077. local function ensureVariant(
  1078.     map,
  1079.     item,
  1080.     name
  1081. )
  1082.  
  1083.     local key =
  1084.         variantKey(item)
  1085.  
  1086.  
  1087.     if not key then
  1088.         return nil
  1089.     end
  1090.  
  1091.  
  1092.     local entry =
  1093.         map[key]
  1094.  
  1095.  
  1096.     if not entry then
  1097.  
  1098.         entry = {
  1099.             key = key,
  1100.             item = item,
  1101.             name =
  1102.                 getDisplayName(
  1103.                     item,
  1104.                     name
  1105.                 ),
  1106.  
  1107.             hut = 0,
  1108.             builder = 0
  1109.         }
  1110.  
  1111.  
  1112.         map[key] =
  1113.             entry
  1114.  
  1115.     end
  1116.  
  1117.  
  1118.     return entry
  1119. end
  1120.  
  1121.  
  1122. -- ============================================================
  1123. -- COLLECT ONE BUILDER HUT'S DO DEMAND
  1124. -- ============================================================
  1125.  
  1126. local function collectHutDemand(
  1127.     hut,
  1128.     allRequests
  1129. )
  1130.  
  1131.     local localDemand = {}
  1132.  
  1133.  
  1134.     -- --------------------------------------------------------
  1135.     -- BUILDER HUT REQUIRED RESOURCES
  1136.     -- --------------------------------------------------------
  1137.  
  1138.     local resources =
  1139.         colony.getBuilderResources(
  1140.             hut.pos
  1141.         )
  1142.         or {}
  1143.  
  1144.  
  1145.     for _, resource
  1146.     in ipairs(resources) do
  1147.  
  1148.         local item =
  1149.             resource.item
  1150.  
  1151.  
  1152.         if isDomum(item) then
  1153.  
  1154.             local shortage =
  1155.                 math.max(
  1156.                     0,
  1157.  
  1158.                     num(resource.needs)
  1159.                     - num(resource.available)
  1160.                     - num(resource.delivering)
  1161.                 )
  1162.  
  1163.  
  1164.             if shortage > 0 then
  1165.  
  1166.                 local entry =
  1167.                     ensureVariant(
  1168.                         localDemand,
  1169.                         item,
  1170.                         resource.displayName
  1171.                     )
  1172.  
  1173.  
  1174.                 if entry then
  1175.  
  1176.                     entry.hut =
  1177.                         entry.hut
  1178.                         + shortage
  1179.  
  1180.                 end
  1181.  
  1182.             end
  1183.  
  1184.         end
  1185.  
  1186.     end
  1187.  
  1188.  
  1189.     -- --------------------------------------------------------
  1190.     -- THIS BUILDER'S PERSONAL REQUESTS
  1191.     -- --------------------------------------------------------
  1192.  
  1193.     if hut.builder
  1194.     and hut.builder.name then
  1195.  
  1196.         for _, request
  1197.         in ipairs(allRequests) do
  1198.  
  1199.             if not requestIsClosed(request)
  1200.             and tostring(
  1201.                 request.target or ""
  1202.             ):find(
  1203.                 hut.builder.name,
  1204.                 1,
  1205.                 true
  1206.             )
  1207.             and type(request.items)
  1208.                 == "table" then
  1209.  
  1210.  
  1211.                 -- Prefer a DO variant already present in this
  1212.                 -- Hut's Required Resources list.
  1213.                 local chosen = nil
  1214.  
  1215.  
  1216.                 for _, item
  1217.                 in ipairs(request.items) do
  1218.  
  1219.                     if isDomum(item)
  1220.                     and localDemand[
  1221.                         variantKey(item)
  1222.                     ] then
  1223.  
  1224.                         chosen = item
  1225.                         break
  1226.                     end
  1227.  
  1228.                 end
  1229.  
  1230.  
  1231.                 if not chosen then
  1232.  
  1233.                     for _, item
  1234.                     in ipairs(request.items) do
  1235.  
  1236.                         if isDomum(item) then
  1237.  
  1238.                             chosen = item
  1239.                             break
  1240.  
  1241.                         end
  1242.  
  1243.                     end
  1244.  
  1245.                 end
  1246.  
  1247.  
  1248.                 if chosen then
  1249.  
  1250.                     local entry =
  1251.                         ensureVariant(
  1252.                             localDemand,
  1253.                             chosen,
  1254.                             request.name
  1255.                         )
  1256.  
  1257.  
  1258.                     if entry then
  1259.  
  1260.                         entry.builder =
  1261.                             entry.builder
  1262.                             + (
  1263.                                 tonumber(
  1264.                                     request.count
  1265.                                 )
  1266.                                 or 1
  1267.                             )
  1268.  
  1269.                     end
  1270.  
  1271.                 end
  1272.  
  1273.             end
  1274.  
  1275.         end
  1276.  
  1277.     end
  1278.  
  1279.  
  1280.     -- --------------------------------------------------------
  1281.     -- MERGE WITHIN THIS BUILDER
  1282.     -- --------------------------------------------------------
  1283.  
  1284.     for _, entry
  1285.     in pairs(localDemand) do
  1286.  
  1287.         entry.quantity =
  1288.             math.max(
  1289.                 entry.hut,
  1290.                 entry.builder
  1291.             )
  1292.  
  1293.     end
  1294.  
  1295.  
  1296.     return localDemand
  1297. end
  1298.  
  1299.  
  1300. -- ============================================================
  1301. -- COLLECT ALL BUILDERS
  1302. -- ============================================================
  1303.  
  1304. local function collectAllDemand()
  1305.  
  1306.     local huts =
  1307.         findBuilderHuts()
  1308.  
  1309.     local requests =
  1310.         colony.getRequests()
  1311.         or {}
  1312.  
  1313.     local totalDemand = {}
  1314.  
  1315.  
  1316.     for _, hut
  1317.     in ipairs(huts) do
  1318.  
  1319.         local localDemand =
  1320.             collectHutDemand(
  1321.                 hut,
  1322.                 requests
  1323.             )
  1324.  
  1325.  
  1326.         -- Demand from DIFFERENT Builder Huts is additive.
  1327.         for key, localEntry
  1328.         in pairs(localDemand) do
  1329.  
  1330.             local total =
  1331.                 totalDemand[key]
  1332.  
  1333.  
  1334.             if not total then
  1335.  
  1336.                 total = {
  1337.                     key = key,
  1338.                     item =
  1339.                         localEntry.item,
  1340.  
  1341.                     name =
  1342.                         localEntry.name,
  1343.  
  1344.                     quantity = 0,
  1345.                     builders = 0
  1346.                 }
  1347.  
  1348.  
  1349.                 totalDemand[key] =
  1350.                     total
  1351.  
  1352.             end
  1353.  
  1354.  
  1355.             if localEntry.quantity > 0 then
  1356.  
  1357.                 total.quantity =
  1358.                     total.quantity
  1359.                     + localEntry.quantity
  1360.  
  1361.                 total.builders =
  1362.                     total.builders + 1
  1363.  
  1364.             end
  1365.  
  1366.         end
  1367.  
  1368.     end
  1369.  
  1370.  
  1371.     return totalDemand,
  1372.            huts
  1373. end
  1374.  
  1375.  
  1376. -- ============================================================
  1377. -- STAGING STATE
  1378. -- ============================================================
  1379.  
  1380. local function syncStagingState(demand)
  1381.  
  1382.     local active = {}
  1383.  
  1384.  
  1385.     for key, entry
  1386.     in pairs(demand) do
  1387.  
  1388.         active[key] = true
  1389.  
  1390.  
  1391.         local materials =
  1392.             materialsFor(
  1393.                 entry.item
  1394.             )
  1395.  
  1396.  
  1397.         local state =
  1398.             staged[key]
  1399.  
  1400.  
  1401.         if not state then
  1402.  
  1403.             state = {
  1404.                 requested =
  1405.                     entry.quantity,
  1406.  
  1407.                 name =
  1408.                     entry.name,
  1409.  
  1410.                 materials = {}
  1411.             }
  1412.  
  1413.  
  1414.             staged[key] =
  1415.                 state
  1416.  
  1417.         end
  1418.  
  1419.  
  1420.         -- --------------------------------------------------------
  1421.         -- CHECK FOR THE FINISHED DO BLOCK IN AE2 FIRST
  1422.         -- --------------------------------------------------------
  1423.         --
  1424.         -- Example:
  1425.         --   Builders currently need Framed Block x25
  1426.         --   AE2 already contains exact Framed Block x20
  1427.         --
  1428.         -- Only stage raw materials for the missing x5.
  1429.         --
  1430.         -- The lookup is exact by fingerprint/components so another
  1431.         -- DO variant with the same registry name does not count.
  1432.         local finishedStored =
  1433.             getEffectiveFinishedStored(
  1434.                 key,
  1435.                 entry.item
  1436.             )
  1437.  
  1438.  
  1439.         local missingFinished =
  1440.             math.max(
  1441.                 0,
  1442.                 entry.quantity
  1443.                 - finishedStored
  1444.             )
  1445.  
  1446.  
  1447.         -- Current aggregate across all Builders, minus exact
  1448.         -- finished blocks already available in AE2.
  1449.         --
  1450.         -- If demand rises, additional material is staged.
  1451.         -- If it falls below what has already been sent, nothing
  1452.         -- further is staged.
  1453.         state.requested =
  1454.             missingFinished
  1455.  
  1456.         state.name =
  1457.             entry.name
  1458.  
  1459.         state.totalDemand =
  1460.             entry.quantity
  1461.  
  1462.         state.finishedStored =
  1463.             finishedStored
  1464.  
  1465.  
  1466.         if missingFinished <= 0 then
  1467.  
  1468.             local coveredKey =
  1469.                 "covered:" .. key
  1470.  
  1471.  
  1472.             if not warned[
  1473.                 coveredKey
  1474.             ] then
  1475.  
  1476.                 print(
  1477.                     "[COVERED] "
  1478.                     .. entry.name
  1479.                     .. " x"
  1480.                     .. tostring(
  1481.                         entry.quantity
  1482.                     )
  1483.                     .. " already exists in AE2."
  1484.                 )
  1485.  
  1486.  
  1487.                 warned[
  1488.                     coveredKey
  1489.                 ] = true
  1490.             end
  1491.  
  1492.         else
  1493.  
  1494.             warned[
  1495.                 "covered:" .. key
  1496.             ] = nil
  1497.  
  1498.         end
  1499.  
  1500.  
  1501.         for materialName,
  1502.             perOutput
  1503.         in pairs(materials) do
  1504.  
  1505.             if not state.materials[
  1506.                 materialName
  1507.             ] then
  1508.  
  1509.                 state.materials[
  1510.                     materialName
  1511.                 ] = {
  1512.                     perOutput =
  1513.                         perOutput,
  1514.  
  1515.                     sent = 0
  1516.                 }
  1517.  
  1518.             else
  1519.  
  1520.                 state.materials[
  1521.                     materialName
  1522.                 ].perOutput =
  1523.                     perOutput
  1524.  
  1525.             end
  1526.  
  1527.         end
  1528.  
  1529.  
  1530.         if next(materials)
  1531.             == nil then
  1532.  
  1533.             local warningKey =
  1534.                 "parse:" .. key
  1535.  
  1536.  
  1537.             if not warned[
  1538.                 warningKey
  1539.             ] then
  1540.  
  1541.                 print("")
  1542.                 print(
  1543.                     "[PARSE ERROR] "
  1544.                     .. entry.name
  1545.                     .. " x"
  1546.                     .. tostring(
  1547.                         entry.quantity
  1548.                     )
  1549.                 )
  1550.  
  1551.                 print(
  1552.                     "Could not extract DO texture materials."
  1553.                 )
  1554.  
  1555.                 print(
  1556.                     "Inspect this requested item's components "
  1557.                     .. "before changing the parser."
  1558.                 )
  1559.  
  1560.  
  1561.                 warned[
  1562.                     warningKey
  1563.                 ] = true
  1564.  
  1565.             end
  1566.  
  1567.         else
  1568.  
  1569.             warned[
  1570.                 "parse:" .. key
  1571.             ] = nil
  1572.  
  1573.         end
  1574.  
  1575.     end
  1576.  
  1577.  
  1578.     -- Fully vanished requirement = finished.
  1579.     for key
  1580.     in pairs(staged) do
  1581.  
  1582.         if not active[key] then
  1583.             staged[key] = nil
  1584.             finishedItemState[key] = nil
  1585.             warned["covered:" .. key] = nil
  1586.         end
  1587.  
  1588.     end
  1589. end
  1590.  
  1591.  
  1592. -- ============================================================
  1593. -- PROCESS ONE RAW MATERIAL
  1594. -- ============================================================
  1595.  
  1596. local function stageMaterial(
  1597.     variant,
  1598.     materialName,
  1599.     materialState
  1600. )
  1601.  
  1602.     local target =
  1603.         variant.requested
  1604.         * materialState.perOutput
  1605.  
  1606.  
  1607.     local remaining =
  1608.         target
  1609.         - materialState.sent
  1610.  
  1611.  
  1612.     if remaining <= 0 then
  1613.         return
  1614.     end
  1615.  
  1616.  
  1617.     -- --------------------------------------------------------
  1618.     -- EXPORT STORED MATERIAL
  1619.     -- --------------------------------------------------------
  1620.  
  1621.     local stored =
  1622.         getStored(
  1623.             materialName
  1624.         )
  1625.  
  1626.  
  1627.     if stored > 0 then
  1628.  
  1629.         local amount =
  1630.             math.min(
  1631.                 stored,
  1632.                 remaining
  1633.             )
  1634.  
  1635.  
  1636.         local moved =
  1637.             exportItem(
  1638.                 materialName,
  1639.                 amount
  1640.             )
  1641.  
  1642.  
  1643.         if moved > 0 then
  1644.  
  1645.             materialState.sent =
  1646.                 materialState.sent
  1647.                 + moved
  1648.  
  1649.  
  1650.             remaining =
  1651.                 remaining - moved
  1652.  
  1653.  
  1654.             print(
  1655.                 "[STAGED] "
  1656.                 .. materialName
  1657.                 .. " x"
  1658.                 .. tostring(moved)
  1659.                 .. " for "
  1660.                 .. variant.name
  1661.             )
  1662.  
  1663.         end
  1664.  
  1665.  
  1666.         -- Output container did not accept the entire attempted
  1667.         -- transfer. Do not craft more until there is room.
  1668.         if moved < amount then
  1669.             return
  1670.         end
  1671.  
  1672.     end
  1673.  
  1674.  
  1675.     if remaining <= 0 then
  1676.         return
  1677.     end
  1678.  
  1679.  
  1680.     -- --------------------------------------------------------
  1681.     -- ALREADY CRAFTING
  1682.     -- --------------------------------------------------------
  1683.  
  1684.     if isCrafting(
  1685.         materialName
  1686.     ) then
  1687.  
  1688.         return
  1689.     end
  1690.  
  1691.  
  1692.     -- --------------------------------------------------------
  1693.     -- AUTOCRAFT
  1694.     -- --------------------------------------------------------
  1695.  
  1696.     if isCraftable(
  1697.         materialName
  1698.     ) then
  1699.  
  1700.         local retryAt =
  1701.             craftRetry[
  1702.                 materialName
  1703.             ]
  1704.             or 0
  1705.  
  1706.  
  1707.         if now() >= retryAt then
  1708.  
  1709.             local started =
  1710.                 craftItem(
  1711.                     materialName,
  1712.                     remaining
  1713.                 )
  1714.  
  1715.  
  1716.             craftRetry[
  1717.                 materialName
  1718.             ] =
  1719.                 now()
  1720.                 + CRAFT_RETRY_SECONDS
  1721.  
  1722.  
  1723.             if started then
  1724.  
  1725.                 print(
  1726.                     "[CRAFT] "
  1727.                     .. materialName
  1728.                     .. " x"
  1729.                     .. tostring(
  1730.                         remaining
  1731.                     )
  1732.                 )
  1733.  
  1734.  
  1735.                 warned[
  1736.                     "missing:"
  1737.                     .. materialName
  1738.                 ] = nil
  1739.  
  1740.             end
  1741.  
  1742.         end
  1743.  
  1744.  
  1745.         return
  1746.     end
  1747.  
  1748.  
  1749.     -- --------------------------------------------------------
  1750.     -- MISSING
  1751.     -- --------------------------------------------------------
  1752.  
  1753.     local warningKey =
  1754.         "missing:"
  1755.         .. materialName
  1756.  
  1757.  
  1758.     if not warned[
  1759.         warningKey
  1760.     ] then
  1761.  
  1762.         print(
  1763.             "[MISSING] "
  1764.             .. materialName
  1765.             .. " x"
  1766.             .. tostring(
  1767.                 remaining
  1768.             )
  1769.             .. " is not stored or craftable in AE2."
  1770.         )
  1771.  
  1772.  
  1773.         warned[
  1774.             warningKey
  1775.         ] = true
  1776.  
  1777.     end
  1778. end
  1779.  
  1780.  
  1781. -- ============================================================
  1782. -- PROCESS ALL ACTIVE DO VARIANTS
  1783. -- ============================================================
  1784.  
  1785. local function processStaging()
  1786.  
  1787.     for _, variant
  1788.     in pairs(staged) do
  1789.  
  1790.         for materialName,
  1791.             materialState
  1792.         in pairs(
  1793.             variant.materials
  1794.         ) do
  1795.  
  1796.             local ok, err =
  1797.                 pcall(
  1798.                     stageMaterial,
  1799.                     variant,
  1800.                     materialName,
  1801.                     materialState
  1802.                 )
  1803.  
  1804.  
  1805.             if not ok then
  1806.  
  1807.                 print(
  1808.                     "[ERROR] "
  1809.                     .. materialName
  1810.                     .. ": "
  1811.                     .. tostring(err)
  1812.                 )
  1813.  
  1814.             end
  1815.  
  1816.         end
  1817.  
  1818.     end
  1819. end
  1820.  
  1821.  
  1822. -- ============================================================
  1823. -- STARTUP DISPLAY
  1824. -- ============================================================
  1825.  
  1826. term.clear()
  1827. term.setCursorPos(
  1828.     1,
  1829.     1
  1830. )
  1831.  
  1832.  
  1833. print(
  1834.     "========================================"
  1835. )
  1836.  
  1837. print(
  1838.     " ARCHITECT MATERIAL STAGER v3"
  1839. )
  1840.  
  1841. print(
  1842.     "========================================"
  1843. )
  1844.  
  1845. print("")
  1846.  
  1847. print(
  1848.     "Colony: "
  1849.     .. colonyName
  1850. )
  1851.  
  1852. print(
  1853.     "ME Bridge: "
  1854.     .. mePeripheralName
  1855. )
  1856.  
  1857. print(
  1858.     "Custom Name: "
  1859.     .. tostring(
  1860.         meCustomName
  1861.     )
  1862. )
  1863.  
  1864. print(
  1865.     "Output Side: UP"
  1866. )
  1867.  
  1868. print("")
  1869.  
  1870. print(
  1871.     "Scanning all Builder Huts dynamically."
  1872. )
  1873.  
  1874. print(
  1875.     "Builder + Hut DO demand is merged per Hut."
  1876. )
  1877.  
  1878. print(
  1879.     "Demand from separate Builders is added."
  1880. )
  1881.  
  1882. print(
  1883.     "Finished DO stock in AE2 is checked first."
  1884. )
  1885.  
  1886. print("")
  1887.  
  1888. print(
  1889.     "Press Ctrl+T to stop."
  1890. )
  1891.  
  1892. print("")
  1893.  
  1894.  
  1895. -- ============================================================
  1896. -- MAIN LOOP
  1897. -- ============================================================
  1898.  
  1899. local lastBuilderCount = nil
  1900.  
  1901.  
  1902. while true do
  1903.  
  1904.     local ok,
  1905.           demand,
  1906.           huts =
  1907.         pcall(
  1908.             collectAllDemand
  1909.         )
  1910.  
  1911.  
  1912.     if not ok then
  1913.  
  1914.         print(
  1915.             "[ERROR] "
  1916.             .. tostring(demand)
  1917.         )
  1918.  
  1919.  
  1920.     else
  1921.  
  1922.         if lastBuilderCount
  1923.             ~= #huts then
  1924.  
  1925.             print(
  1926.                 "[SCAN] Builder Huts: "
  1927.                 .. tostring(
  1928.                     #huts
  1929.                 )
  1930.             )
  1931.  
  1932.  
  1933.             for index, hut
  1934.             in ipairs(huts) do
  1935.  
  1936.                 local name =
  1937.                     hut.builder
  1938.                     and hut.builder.name
  1939.                     or "Unassigned Builder"
  1940.  
  1941.  
  1942.                 print(
  1943.                     "  "
  1944.                     .. tostring(index)
  1945.                     .. ". "
  1946.                     .. name
  1947.                     .. " @ "
  1948.                     .. posKey(
  1949.                         hut.pos
  1950.                     )
  1951.                 )
  1952.  
  1953.             end
  1954.  
  1955.  
  1956.             lastBuilderCount =
  1957.                 #huts
  1958.  
  1959.         end
  1960.  
  1961.  
  1962.         syncStagingState(
  1963.             demand
  1964.         )
  1965.  
  1966.  
  1967.         processStaging()
  1968.  
  1969.     end
  1970.  
  1971.  
  1972.     sleep(
  1973.         POLL_INTERVAL
  1974.     )
  1975. end
  1976.  
Add Comment
Please, Sign In to add comment