Naddox

MineColonies AE2 Builder Supplier

Aug 30th, 2026 (edited)
93
0
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 43.79 KB | Gaming | 0 0
  1. -- ============================================================
  2. -- MineColonies + AE2 Automatic Builder Supplier
  3. -- Dynamic Multi-Builder Edition v6
  4. -- ============================================================
  5. --
  6. -- WHAT CHANGED IN v6
  7. --   * No static Builder count.
  8. --   * Discovers every ME Bridge whose custom name matches:
  9. --         builder_1, builder_2, builder_3, ...
  10. --   * Discovers every MineColonies Builder's Hut.
  11. --   * Sorts Builder Huts by distance from COMPUTER_POS.
  12. --   * builder_1 is assigned to the closest Builder's Hut,
  13. --     builder_2 to the next closest, and so on.
  14. --   * Each ME Bridge automatically probes all six faces and
  15. --     remembers which faces can accept items.
  16. --   * Every assigned Builder is supplied independently.
  17. --   * One monitor automatically cycles through all Builders.
  18. --
  19. -- IMPORTANT
  20. --   Set COMPUTER_POS below before running the script.
  21. --
  22. -- STORAGE FACE DETECTION
  23. --   The script exports 1 probe item to each face and immediately
  24. --   imports 1 back. A face that successfully accepts the probe is
  25. --   treated as Builder storage. Previously proven faces remain
  26. --   remembered during later rescans, because a completely full
  27. --   inventory cannot accept a probe and would otherwise look
  28. --   disconnected.
  29. --
  30. -- ============================================================
  31.  
  32. -- ============================================================
  33. -- CONFIGURATION
  34. -- ============================================================
  35.  
  36. -- CHANGE THESE THREE VALUES to the world coordinates of the
  37. -- computer/control-room setup. These coordinates are only used
  38. -- to sort Builder Huts nearest-to-farthest.
  39. local COMPUTER_POS = {
  40.     x = nil,
  41.     y = nil,
  42.     z = nil
  43. }
  44.  
  45. -- ME Bridges are discovered by their Advanced Peripherals custom
  46. -- names. Rename them in an anvil to builder_1, builder_2, etc.
  47. local BUILDER_BRIDGE_PATTERN = "^builder_(%d+)$"
  48.  
  49. -- Items used to probe for adjacent Builder storage. The script
  50. -- uses the first one it finds stored in AE2.
  51. local STORAGE_PROBE_ITEMS = {
  52.     "minecraft:cobblestone",
  53.     "minecraft:stone",
  54.     "minecraft:dirt"
  55. }
  56.  
  57. local STORAGE_FACES = {
  58.     "north",
  59.     "south",
  60.     "east",
  61.     "west",
  62.     "up",
  63.     "down"
  64. }
  65.  
  66. local POLL_INTERVAL = 5
  67. local TOPOLOGY_RESCAN_SECONDS = 60
  68. local DELIVERY_HOLD_SECONDS = 15
  69. local CRAFT_RETRY_SECONDS = 30
  70. local CHAT_DELAY = 1.1
  71.  
  72. -- Monitor paging.
  73. local BUILDER_SCREEN_SECONDS = 8
  74. local ITEM_PAGE_SECONDS = 5
  75. local BUILDER_ROWS = 4
  76. local HUT_ROWS = 7
  77. local ACTIVITY_ROWS = 4
  78.  
  79. -- ============================================================
  80. -- VALIDATE CONFIG
  81. -- ============================================================
  82.  
  83. if type(COMPUTER_POS.x) ~= "number"
  84. or type(COMPUTER_POS.y) ~= "number"
  85. or type(COMPUTER_POS.z) ~= "number" then
  86.     error(
  87.         "Set COMPUTER_POS.x, COMPUTER_POS.y, and COMPUTER_POS.z "
  88.         .. "at the top of colony_supplier before running it."
  89.     )
  90. end
  91.  
  92. -- ============================================================
  93. -- CORE PERIPHERALS
  94. -- ============================================================
  95.  
  96. local colony = peripheral.find("colony_integrator")
  97. local chat = peripheral.find("chat_box")
  98. local monitor = peripheral.find("monitor")
  99.  
  100. if not colony then error("Colony Integrator not found!") end
  101. if not chat then error("Chat Box not found!") end
  102. if not monitor then error("Monitor not found!") end
  103.  
  104. local colonyName = colony.getColonyName() or "Unknown Colony"
  105. local CHAT_PREFIX = "Colony: " .. colonyName
  106.  
  107. local MON_W, MON_H = monitor.getSize()
  108.  
  109. if MON_W < 39 or MON_H < 26 then
  110.     error(
  111.         "Monitor is too small. Need at least 39x26, found "
  112.         .. tostring(MON_W) .. "x" .. tostring(MON_H)
  113.     )
  114. end
  115.  
  116. monitor.setBackgroundColor(colors.black)
  117. monitor.setTextColor(colors.white)
  118. monitor.clear()
  119.  
  120. -- ============================================================
  121. -- GLOBAL RUNTIME TABLES
  122. -- ============================================================
  123.  
  124. -- Rebuilt by discoverTopology().
  125. local BUILDERS = {}
  126.  
  127. -- Persistent state keyed by custom bridge name, e.g. builder_1.
  128. -- This survives topology rescans as long as the same custom name
  129. -- remains assigned to the same Builder Hut.
  130. local STATE_BY_BRIDGE = {}
  131.  
  132. local lastTopologyScan = 0
  133. local topologyMessage = "Starting discovery..."
  134.  
  135. -- ============================================================
  136. -- BASIC HELPERS
  137. -- ============================================================
  138.  
  139. local function now()
  140.     return os.epoch("utc") / 1000
  141. end
  142.  
  143. local function numberValue(v)
  144.     if type(v) == "number" then return v end
  145.     if v == true then return 1 end
  146.     return 0
  147. end
  148.  
  149. local function samePos(a, b)
  150.     return a
  151.        and b
  152.        and a.x == b.x
  153.        and a.y == b.y
  154.        and a.z == b.z
  155. end
  156.  
  157. local function posKey(pos)
  158.     if not pos then return "nil" end
  159.     return tostring(pos.x)
  160.         .. ","
  161.         .. tostring(pos.y)
  162.         .. ","
  163.         .. tostring(pos.z)
  164. end
  165.  
  166. local function distanceSq(a, b)
  167.     local dx = a.x - b.x
  168.     local dy = a.y - b.y
  169.     local dz = a.z - b.z
  170.     return dx * dx + dy * dy + dz * dz
  171. end
  172.  
  173. local function cleanName(name)
  174.     if not name then return "Unknown Item" end
  175.     name = tostring(name)
  176.     name = name:gsub("^%[", "")
  177.     name = name:gsub("%]$", "")
  178.     return name
  179. end
  180.  
  181. local function itemDisplayName(item, fallback)
  182.     if fallback and fallback ~= "" then
  183.         return cleanName(fallback)
  184.     end
  185.  
  186.     if item and item.displayName then
  187.         return cleanName(item.displayName)
  188.     end
  189.  
  190.     if item and item.name then
  191.         return tostring(item.name)
  192.     end
  193.  
  194.     return "Unknown Item"
  195. end
  196.  
  197. local function itemKey(item)
  198.     if not item then return nil end
  199.  
  200.     if item.fingerprint then
  201.         return "fp:" .. tostring(item.fingerprint)
  202.     end
  203.  
  204.     if item.name then
  205.         return "name:"
  206.             .. tostring(item.name)
  207.             .. ":"
  208.             .. textutils.serialize(item.components or {})
  209.     end
  210.  
  211.     return nil
  212. end
  213.  
  214. local function makeFilter(item, count)
  215.     local filter = {}
  216.  
  217.     -- Fingerprint works well for exact stored/exported stacks.
  218.     if item and item.fingerprint then
  219.         filter.fingerprint = item.fingerprint
  220.     elseif item then
  221.         filter.name = item.name
  222.  
  223.         if item.components and next(item.components) then
  224.             filter.components = item.components
  225.         end
  226.     end
  227.  
  228.     if count then
  229.         filter.count = count
  230.     end
  231.  
  232.     return filter
  233. end
  234.  
  235. local function makeCraftFilter(item, count)
  236.     -- Crafting lookups must use the registry ID/components rather
  237.     -- than MineColonies' fingerprint.
  238.     local filter = {
  239.         name = item.name
  240.     }
  241.  
  242.     if item.components and next(item.components) then
  243.         filter.components = item.components
  244.     end
  245.  
  246.     if count then
  247.         filter.count = count
  248.     end
  249.  
  250.     return filter
  251. end
  252.  
  253. local function storedCount(result)
  254.     if not result then return 0 end
  255.     if type(result.count) == "number" then return result.count end
  256.     if type(result.amount) == "number" then return result.amount end
  257.     return 0
  258. end
  259.  
  260. local function parseTransferred(result)
  261.     if type(result) == "number" then
  262.         return result
  263.     end
  264.  
  265.     if type(result) == "table" then
  266.         if type(result.count) == "number" then return result.count end
  267.         if type(result.amount) == "number" then return result.amount end
  268.         if type(result.transferred) == "number" then return result.transferred end
  269.     end
  270.  
  271.     return 0
  272. end
  273.  
  274. local function join(list, separator)
  275.     separator = separator or ","
  276.     local out = ""
  277.  
  278.     for i, value in ipairs(list) do
  279.         if i > 1 then out = out .. separator end
  280.         out = out .. tostring(value)
  281.     end
  282.  
  283.     return out
  284. end
  285.  
  286. -- ============================================================
  287. -- MONITOR HELPERS
  288. -- ============================================================
  289.  
  290. local function fit(text, width)
  291.     text = tostring(text or "")
  292.  
  293.     if #text <= width then
  294.         return text
  295.     end
  296.  
  297.     if width <= 3 then
  298.         return text:sub(1, width)
  299.     end
  300.  
  301.     return text:sub(1, width - 3) .. "..."
  302. end
  303.  
  304. local function monitorLine(y, text, color)
  305.     if y < 1 or y > MON_H then return end
  306.  
  307.     monitor.setCursorPos(1, y)
  308.     monitor.setBackgroundColor(colors.black)
  309.     monitor.setTextColor(color or colors.white)
  310.     monitor.write(string.rep(" ", MON_W))
  311.  
  312.     monitor.setCursorPos(1, y)
  313.     monitor.write(fit(text, MON_W))
  314. end
  315.  
  316. local function separator(y)
  317.     monitorLine(y, string.rep("-", MON_W), colors.gray)
  318. end
  319.  
  320. local function itemLine(name, qty)
  321.     local right = "x" .. tostring(qty)
  322.     local maxName = MON_W - #right - 1
  323.     local left = fit(name, maxName)
  324.  
  325.     return left
  326.         .. string.rep(" ", MON_W - #left - #right)
  327.         .. right
  328. end
  329.  
  330. local function activityColor(kind)
  331.     if kind == "PULL" then return colors.lime end
  332.     if kind == "CRAFT" then return colors.yellow end
  333.     if kind == "WAIT" then return colors.red end
  334.     if kind == "INFO" then return colors.lightBlue end
  335.     return colors.lightGray
  336. end
  337.  
  338. local function addActivity(state, kind, name, qty)
  339.     local text = kind .. " " .. tostring(name)
  340.  
  341.     if qty and qty > 0 then
  342.         text = text .. " x" .. tostring(qty)
  343.     end
  344.  
  345.     table.insert(state.activity, 1, {
  346.         kind = kind,
  347.         text = text
  348.     })
  349.  
  350.     while #state.activity > 20 do
  351.         table.remove(state.activity)
  352.     end
  353. end
  354.  
  355. local function pageSlice(list, rows)
  356.     local totalPages = math.max(1, math.ceil(#list / rows))
  357.     local page = (math.floor(now() / ITEM_PAGE_SECONDS) % totalPages) + 1
  358.     local first = ((page - 1) * rows) + 1
  359.     local last = math.min(#list, first + rows - 1)
  360.  
  361.     local out = {}
  362.  
  363.     for i = first, last do
  364.         table.insert(out, list[i])
  365.     end
  366.  
  367.     return out, page, totalPages
  368. end
  369.  
  370. local function getScreenBuilder()
  371.     local assigned = {}
  372.  
  373.     for _, ctx in ipairs(BUILDERS) do
  374.         if ctx.hut then
  375.             table.insert(assigned, ctx)
  376.         end
  377.     end
  378.  
  379.     if #assigned == 0 then
  380.         return nil, 0, 0
  381.     end
  382.  
  383.     local index = (math.floor(now() / BUILDER_SCREEN_SECONDS) % #assigned) + 1
  384.     return assigned[index], index, #assigned
  385. end
  386.  
  387. local function drawDashboard()
  388.     local ctx, pageIndex, pageCount = getScreenBuilder()
  389.  
  390.     monitor.setBackgroundColor(colors.black)
  391.     monitor.clear()
  392.  
  393.     monitorLine(1, "COLONY: " .. string.upper(colonyName), colors.purple)
  394.  
  395.     if not ctx then
  396.         separator(2)
  397.         monitorLine(3, "NO ASSIGNED BUILDER BRIDGES", colors.red)
  398.         monitorLine(5, topologyMessage, colors.lightGray)
  399.         monitorLine(26, "Status: waiting for topology", colors.lightGray)
  400.         return
  401.     end
  402.  
  403.     local state = ctx.state
  404.     local builderName = ctx.builderName or "Unassigned Worker"
  405.  
  406.     monitorLine(
  407.         2,
  408.         "BUILDER " .. tostring(pageIndex) .. "/" .. tostring(pageCount)
  409.         .. " | " .. ctx.customName,
  410.         colors.cyan
  411.     )
  412.  
  413.     monitorLine(3, builderName, colors.white)
  414.  
  415.     local builderList = state.builderDisplay or {}
  416.     local bPageItems, bPage, bPages = pageSlice(builderList, BUILDER_ROWS)
  417.  
  418.     monitorLine(
  419.         4,
  420.         "Requests: " .. tostring(#builderList)
  421.         .. "  Page " .. tostring(bPage) .. "/" .. tostring(bPages),
  422.         colors.lightGray
  423.     )
  424.  
  425.     for row = 1, BUILDER_ROWS do
  426.         local entry = bPageItems[row]
  427.  
  428.         if entry then
  429.             monitorLine(4 + row, itemLine(entry.name, entry.qty), colors.white)
  430.         else
  431.             monitorLine(4 + row, "", colors.white)
  432.         end
  433.     end
  434.  
  435.     separator(9)
  436.  
  437.     monitorLine(10, "HUT: " .. (ctx.activeBuildingName or "Builder's Hut"), colors.cyan)
  438.  
  439.     local hutList = state.hutDisplay or {}
  440.     local hPageItems, hPage, hPages = pageSlice(hutList, HUT_ROWS)
  441.  
  442.     monitorLine(
  443.         11,
  444.         "Required: " .. tostring(#hutList)
  445.         .. "  Page " .. tostring(hPage) .. "/" .. tostring(hPages),
  446.         colors.lightGray
  447.     )
  448.  
  449.     for row = 1, HUT_ROWS do
  450.         local entry = hPageItems[row]
  451.  
  452.         if entry then
  453.             monitorLine(11 + row, itemLine(entry.name, entry.qty), colors.white)
  454.         else
  455.             monitorLine(11 + row, "", colors.white)
  456.         end
  457.     end
  458.  
  459.     separator(19)
  460.     monitorLine(20, "ME SYSTEM ACTIVITY", colors.cyan)
  461.  
  462.     for row = 1, ACTIVITY_ROWS do
  463.         local entry = state.activity[row]
  464.  
  465.         if entry then
  466.             monitorLine(20 + row, entry.text, activityColor(entry.kind))
  467.         else
  468.             monitorLine(20 + row, "", colors.white)
  469.         end
  470.     end
  471.  
  472.     separator(25)
  473.     monitorLine(26, "Status: " .. state.statusText, colors.lightGray)
  474. end
  475.  
  476. -- ============================================================
  477. -- BUILDER STATE
  478. -- ============================================================
  479.  
  480. local function newBuilderState(hutKey)
  481.     return {
  482.         hutKey = hutKey,
  483.         requestState = {},
  484.         hutDeliveryHold = {},
  485.         alertState = {},
  486.         craftRetry = {},
  487.         activity = {},
  488.         builderDisplay = {},
  489.         hutDisplay = {},
  490.         statusText = "Discovered",
  491.         knownOutputs = {}
  492.     }
  493. end
  494.  
  495. local function getOrResetState(customName, hutKey)
  496.     local state = STATE_BY_BRIDGE[customName]
  497.  
  498.     if not state or state.hutKey ~= hutKey then
  499.         state = newBuilderState(hutKey)
  500.         STATE_BY_BRIDGE[customName] = state
  501.     end
  502.  
  503.     return state
  504. end
  505.  
  506. -- ============================================================
  507. -- DISCOVERY: ME BRIDGES
  508. -- ============================================================
  509.  
  510. local function discoverBuilderBridges()
  511.     local bridges = {}
  512.     local seenNumbers = {}
  513.  
  514.     for _, peripheralName in ipairs(peripheral.getNames()) do
  515.         if peripheral.getType(peripheralName) == "me_bridge" then
  516.             local bridge = peripheral.wrap(peripheralName)
  517.             local ok, customName = pcall(bridge.getName)
  518.  
  519.             if ok and type(customName) == "string" then
  520.                 local numberText = customName:match(BUILDER_BRIDGE_PATTERN)
  521.  
  522.                 if numberText then
  523.                     local number = tonumber(numberText)
  524.  
  525.                     if seenNumbers[number] then
  526.                         print(
  527.                             "[DISCOVERY] Duplicate custom bridge number: "
  528.                             .. customName
  529.                             .. " - ignoring "
  530.                             .. peripheralName
  531.                         )
  532.                     else
  533.                         seenNumbers[number] = true
  534.  
  535.                         table.insert(bridges, {
  536.                             number = number,
  537.                             customName = customName,
  538.                             peripheralName = peripheralName,
  539.                             bridge = bridge
  540.                         })
  541.                     end
  542.                 end
  543.             end
  544.         end
  545.     end
  546.  
  547.     table.sort(bridges, function(a, b)
  548.         return a.number < b.number
  549.     end)
  550.  
  551.     return bridges
  552. end
  553.  
  554. -- ============================================================
  555. -- DISCOVERY: BUILDER HUTS
  556. -- ============================================================
  557.  
  558. local function isBuilderBuilding(building)
  559.     local typeText = string.lower(tostring(building.type or ""))
  560.     local nameText = string.lower(tostring(building.name or ""))
  561.  
  562.     return typeText:find("building.builder", 1, true) ~= nil
  563.         or typeText:find("builder", 1, true) ~= nil
  564.         or nameText == "builder's hut"
  565.         or nameText == "builders hut"
  566. end
  567.  
  568. local function discoverBuilderHuts()
  569.     local huts = {}
  570.     local seen = {}
  571.     local buildings = colony.getBuildings() or {}
  572.  
  573.     for _, building in ipairs(buildings) do
  574.         if isBuilderBuilding(building)
  575.         and type(building.location) == "table" then
  576.             local key = posKey(building.location)
  577.  
  578.             if not seen[key] then
  579.                 seen[key] = true
  580.  
  581.                 table.insert(huts, {
  582.                     pos = building.location,
  583.                     building = building,
  584.                     distance = distanceSq(COMPUTER_POS, building.location)
  585.                 })
  586.             end
  587.         end
  588.     end
  589.  
  590.     table.sort(huts, function(a, b)
  591.         if a.distance ~= b.distance then
  592.             return a.distance < b.distance
  593.         end
  594.  
  595.         if a.pos.x ~= b.pos.x then return a.pos.x < b.pos.x end
  596.         if a.pos.z ~= b.pos.z then return a.pos.z < b.pos.z end
  597.         return a.pos.y < b.pos.y
  598.     end)
  599.  
  600.     return huts
  601. end
  602.  
  603. -- ============================================================
  604. -- DISCOVERY: STORAGE FACES
  605. -- ============================================================
  606.  
  607. local function chooseProbeItem(bridge)
  608.     for _, itemName in ipairs(STORAGE_PROBE_ITEMS) do
  609.         local ok, result = pcall(
  610.             bridge.getItem,
  611.             {name = itemName}
  612.         )
  613.  
  614.         if ok and storedCount(result) > 0 then
  615.             return itemName
  616.         end
  617.     end
  618.  
  619.     return nil
  620. end
  621.  
  622. local function containsValue(list, wanted)
  623.     for _, value in ipairs(list) do
  624.         if value == wanted then
  625.             return true
  626.         end
  627.     end
  628.  
  629.     return false
  630. end
  631.  
  632. local function scanStorageFaces(ctx)
  633.     local bridge = ctx.bridge
  634.     local state = ctx.state
  635.     local probeItem = chooseProbeItem(bridge)
  636.  
  637.     -- Once a face has been proven to be storage, retain it across
  638.     -- rescans. A full inventory cannot accept the probe, so a
  639.     -- failed later probe is not reliable evidence of removal.
  640.     local outputs = {}
  641.  
  642.     for _, oldFace in ipairs(state.knownOutputs or {}) do
  643.         table.insert(outputs, oldFace)
  644.     end
  645.  
  646.     if not probeItem then
  647.         print(
  648.             "[STORAGE] " .. ctx.customName
  649.             .. ": no probe item found in AE2; keeping previously known faces."
  650.         )
  651.  
  652.         return outputs
  653.     end
  654.  
  655.     for _, face in ipairs(STORAGE_FACES) do
  656.         local ok, result, err = pcall(
  657.             bridge.exportItem,
  658.             {name = probeItem, count = 1},
  659.             face
  660.         )
  661.  
  662.         local moved = ok and parseTransferred(result) or 0
  663.  
  664.         if moved > 0 then
  665.             if not containsValue(outputs, face) then
  666.                 table.insert(outputs, face)
  667.             end
  668.  
  669.             -- Put the probe back into AE2. If the inventory
  670.             -- already contained the same item, importing any one
  671.             -- matching item still leaves the inventory at the same
  672.             -- net count after the probe cycle.
  673.             local restored = 0
  674.             local attempts = 0
  675.  
  676.             while restored < moved and attempts < 3 do
  677.                 attempts = attempts + 1
  678.  
  679.                 local importOK, importResult = pcall(
  680.                     bridge.importItem,
  681.                     {name = probeItem, count = moved - restored},
  682.                     face
  683.                 )
  684.  
  685.                 if importOK then
  686.                     local imported = parseTransferred(importResult)
  687.                     restored = restored + imported
  688.                 end
  689.  
  690.                 if restored < moved then
  691.                     sleep(0.1)
  692.                 end
  693.             end
  694.  
  695.             if restored < moved then
  696.                 print(
  697.                     "[STORAGE] WARNING: "
  698.                     .. ctx.customName
  699.                     .. " left "
  700.                     .. tostring(moved - restored)
  701.                     .. " probe item(s) on "
  702.                     .. face
  703.                 )
  704.             end
  705.         elseif not ok and err then
  706.             -- Expected for non-inventory faces; no need to spam.
  707.         end
  708.     end
  709.  
  710.     -- Keep output order stable and human-readable.
  711.     local order = {
  712.         north = 1,
  713.         south = 2,
  714.         east = 3,
  715.         west = 4,
  716.         up = 5,
  717.         down = 6
  718.     }
  719.  
  720.     table.sort(outputs, function(a, b)
  721.         return (order[a] or 99) < (order[b] or 99)
  722.     end)
  723.  
  724.     state.knownOutputs = outputs
  725.     return outputs
  726. end
  727.  
  728. -- ============================================================
  729. -- DISCOVERY: COMPLETE TOPOLOGY
  730. -- ============================================================
  731.  
  732. local function discoverTopology()
  733.     local bridges = discoverBuilderBridges()
  734.     local huts = discoverBuilderHuts()
  735.     local newBuilders = {}
  736.  
  737.     print("")
  738.     print("========================================")
  739.     print(" TOPOLOGY DISCOVERY")
  740.     print("========================================")
  741.     print("Builder bridges: " .. tostring(#bridges))
  742.     print("Builder Huts:    " .. tostring(#huts))
  743.  
  744.     for _, bridgeInfo in ipairs(bridges) do
  745.         -- builder_N maps to the Nth closest Builder Hut.
  746.         local hut = huts[bridgeInfo.number]
  747.         local hutKey = hut and posKey(hut.pos) or "unassigned"
  748.         local state = getOrResetState(bridgeInfo.customName, hutKey)
  749.  
  750.         local ctx = {
  751.             number = bridgeInfo.number,
  752.             customName = bridgeInfo.customName,
  753.             peripheralName = bridgeInfo.peripheralName,
  754.             bridge = bridgeInfo.bridge,
  755.             hut = hut,
  756.             state = state,
  757.             outputs = {},
  758.             builderName = nil,
  759.             activeBuildingName = nil
  760.         }
  761.  
  762.         ctx.outputs = scanStorageFaces(ctx)
  763.  
  764.         table.insert(newBuilders, ctx)
  765.  
  766.         if hut then
  767.             print(
  768.                 ctx.customName
  769.                 .. " (" .. ctx.peripheralName .. ")"
  770.                 .. " -> Hut " .. posKey(hut.pos)
  771.                 .. " | outputs: "
  772.                 .. (#ctx.outputs > 0 and join(ctx.outputs, ",") or "NONE")
  773.             )
  774.         else
  775.             print(
  776.                 ctx.customName
  777.                 .. " (" .. ctx.peripheralName .. ")"
  778.                 .. " -> no " .. tostring(ctx.number)
  779.                 .. "th Builder Hut found"
  780.             )
  781.         end
  782.     end
  783.  
  784.     if #huts > #bridges then
  785.         print(
  786.             "[DISCOVERY] "
  787.             .. tostring(#huts - #bridges)
  788.             .. " Builder Hut(s) currently have no matching builder_# bridge."
  789.         )
  790.     end
  791.  
  792.     BUILDERS = newBuilders
  793.  
  794.     topologyMessage = tostring(#bridges)
  795.         .. " bridge(s), "
  796.         .. tostring(#huts)
  797.         .. " Builder Hut(s)"
  798.  
  799.     lastTopologyScan = now()
  800. end
  801.  
  802. -- ============================================================
  803. -- COLONY LOOKUPS
  804. -- ============================================================
  805.  
  806. local function findBuilderWorker(citizens, hutPos)
  807.     for _, citizen in ipairs(citizens) do
  808.         if citizen.work
  809.         and citizen.work.location
  810.         and samePos(citizen.work.location, hutPos) then
  811.             local job = string.lower(tostring(citizen.work.job or ""))
  812.             local workType = string.lower(tostring(citizen.work.type or ""))
  813.             local workName = string.lower(tostring(citizen.work.name or ""))
  814.  
  815.             if job:find("builder", 1, true)
  816.             or workType:find("builder", 1, true)
  817.             or workName:find("builder", 1, true) then
  818.                 return citizen
  819.             end
  820.         end
  821.     end
  822.  
  823.     return nil
  824. end
  825.  
  826. local function isConstructionOrder(order)
  827.     local t = tostring(order.workOrderType or "")
  828.  
  829.     return t == "BUILD"
  830.         or t == "UPGRADE"
  831.         or t == "REPAIR"
  832. end
  833.  
  834. local function findWorkOrderForHut(orders, hutPos)
  835.     local fallback = nil
  836.  
  837.     for _, order in ipairs(orders) do
  838.         if isConstructionOrder(order)
  839.         and type(order.builder) == "table"
  840.         and samePos(order.builder, hutPos) then
  841.             if order.isClaimed == true then
  842.                 return order
  843.             end
  844.  
  845.             fallback = fallback or order
  846.         end
  847.     end
  848.  
  849.     return fallback
  850. end
  851.  
  852. local function requestIsClosed(request)
  853.     local state = string.upper(tostring(request.state or ""))
  854.  
  855.     return state:find("COMPLETE", 1, true) ~= nil
  856.         or state:find("CANCEL", 1, true) ~= nil
  857.         or state == "DONE"
  858.         or state == "RESOLVED"
  859. end
  860.  
  861. local function getBuilderRequests(allRequests, builderName)
  862.     local out = {}
  863.  
  864.     if not builderName then
  865.         return out
  866.     end
  867.  
  868.     for _, request in ipairs(allRequests) do
  869.         if not requestIsClosed(request)
  870.         and tostring(request.target or ""):find(builderName, 1, true) then
  871.             table.insert(out, request)
  872.         end
  873.     end
  874.  
  875.     return out
  876. end
  877.  
  878. -- ============================================================
  879. -- BRIDGE-SPECIFIC AE2 HELPERS
  880. -- ============================================================
  881.  
  882. local function getStored(ctx, item)
  883.     local ok, result = pcall(
  884.         ctx.bridge.getItem,
  885.         makeFilter(item)
  886.     )
  887.  
  888.     if not ok or not result then
  889.         return nil, 0
  890.     end
  891.  
  892.     return result, storedCount(result)
  893. end
  894.  
  895. local function isCraftable(ctx, item)
  896.     local ok, result = pcall(
  897.         ctx.bridge.isCraftable,
  898.         makeCraftFilter(item)
  899.     )
  900.  
  901.     return ok and result == true
  902. end
  903.  
  904. local function isCrafting(ctx, item)
  905.     local ok, result = pcall(
  906.         ctx.bridge.isCrafting,
  907.         makeCraftFilter(item)
  908.     )
  909.  
  910.     return ok and result == true
  911. end
  912.  
  913. local function startCraft(ctx, item, quantity)
  914.     if quantity <= 0 then return false end
  915.  
  916.     local ok, result, err = pcall(
  917.         ctx.bridge.craftItem,
  918.         makeCraftFilter(item, quantity)
  919.     )
  920.  
  921.     if not ok then
  922.         print(
  923.             "[CRAFT ERROR][" .. ctx.customName .. "] "
  924.             .. tostring(result)
  925.         )
  926.         return false
  927.     end
  928.  
  929.     if result then
  930.         return true
  931.     end
  932.  
  933.     if err then
  934.         print(
  935.             "[CRAFT ERROR][" .. ctx.customName .. "] "
  936.             .. tostring(err)
  937.         )
  938.     end
  939.  
  940.     return false
  941. end
  942.  
  943. local function exportAcrossOutputs(ctx, item, quantity)
  944.     if quantity <= 0 then return 0 end
  945.  
  946.     local totalMoved = 0
  947.     local remaining = quantity
  948.  
  949.     for _, side in ipairs(ctx.outputs) do
  950.         if remaining <= 0 then break end
  951.  
  952.         local ok, result, err = pcall(
  953.             ctx.bridge.exportItem,
  954.             makeFilter(item, remaining),
  955.             side
  956.         )
  957.  
  958.         if ok then
  959.             local moved = parseTransferred(result)
  960.  
  961.             if moved > 0 then
  962.                 totalMoved = totalMoved + moved
  963.                 remaining = remaining - moved
  964.             elseif err then
  965.                 -- Destination may simply be full. Do not spam.
  966.             end
  967.         else
  968.             print(
  969.                 "[EXPORT ERROR][" .. ctx.customName .. "][" .. side .. "] "
  970.                 .. tostring(result)
  971.             )
  972.         end
  973.     end
  974.  
  975.     return totalMoved
  976. end
  977.  
  978. -- ============================================================
  979. -- CHAT ALERTS
  980. -- ============================================================
  981.  
  982. local function notifyMissing(ctx, demand, quantity)
  983.     if quantity <= 0 then return end
  984.  
  985.     local state = ctx.state
  986.  
  987.     if state.alertState[demand.key] == quantity then
  988.         return
  989.     end
  990.  
  991.     local who
  992.  
  993.     if ctx.builderName then
  994.         who = "Builder " .. ctx.builderName
  995.     else
  996.         who = "Builder's Hut " .. ctx.customName
  997.     end
  998.  
  999.     local message = who
  1000.         .. " has requested "
  1001.         .. demand.displayName
  1002.         .. " : x"
  1003.         .. tostring(quantity)
  1004.  
  1005.     local ok, result, err = pcall(
  1006.         chat.sendMessage,
  1007.         message,
  1008.         CHAT_PREFIX
  1009.     )
  1010.  
  1011.     if ok and result then
  1012.         state.alertState[demand.key] = quantity
  1013.         addActivity(state, "WAIT", demand.displayName, quantity)
  1014.         state.statusText = "Waiting: " .. demand.displayName
  1015.  
  1016.         print(
  1017.             "[ALERT][" .. ctx.customName .. "] "
  1018.             .. message
  1019.         )
  1020.  
  1021.         sleep(CHAT_DELAY)
  1022.     else
  1023.         print(
  1024.             "[CHAT ERROR] "
  1025.             .. tostring(err or result)
  1026.         )
  1027.     end
  1028. end
  1029.  
  1030. -- ============================================================
  1031. -- DEMAND OBJECTS
  1032. -- ============================================================
  1033.  
  1034. local function ensureDemand(demands, item, displayName)
  1035.     local key = itemKey(item)
  1036.     if not key then return nil end
  1037.  
  1038.     local demand = demands[key]
  1039.  
  1040.     if not demand then
  1041.         demand = {
  1042.             key = key,
  1043.             item = item,
  1044.             displayName = itemDisplayName(item, displayName),
  1045.             hutNeedRaw = 0,
  1046.             hutNeedEffective = 0,
  1047.             builderNeed = 0,
  1048.             totalNeed = 0,
  1049.             requestRefs = {}
  1050.         }
  1051.  
  1052.         demands[key] = demand
  1053.     end
  1054.  
  1055.     return demand
  1056. end
  1057.  
  1058. local function chooseRequestItem(ctx, request, demands)
  1059.     if type(request.items) ~= "table"
  1060.     or #request.items == 0 then
  1061.         return nil
  1062.     end
  1063.  
  1064.     -- Prefer an exact variant already present in Hut demand.
  1065.     for _, item in ipairs(request.items) do
  1066.         local key = itemKey(item)
  1067.  
  1068.         if key and demands[key] then
  1069.             return item
  1070.         end
  1071.     end
  1072.  
  1073.     -- Prefer a stored variant.
  1074.     local bestItem = nil
  1075.     local bestCount = 0
  1076.  
  1077.     for _, item in ipairs(request.items) do
  1078.         local _, count = getStored(ctx, item)
  1079.  
  1080.         if count > bestCount then
  1081.             bestCount = count
  1082.             bestItem = item
  1083.         end
  1084.     end
  1085.  
  1086.     if bestItem then return bestItem end
  1087.  
  1088.     -- Prefer something AE2 can craft.
  1089.     for _, item in ipairs(request.items) do
  1090.         if isCraftable(ctx, item) then
  1091.             return item
  1092.         end
  1093.     end
  1094.  
  1095.     return request.items[1]
  1096. end
  1097.  
  1098. local function addDisplay(displayMap, item, fallbackName, qty)
  1099.     local key = itemKey(item)
  1100.     if not key then return end
  1101.  
  1102.     local entry = displayMap[key]
  1103.  
  1104.     if not entry then
  1105.         entry = {
  1106.             name = itemDisplayName(item, fallbackName),
  1107.             qty = 0
  1108.         }
  1109.  
  1110.         displayMap[key] = entry
  1111.     end
  1112.  
  1113.     entry.qty = entry.qty + qty
  1114. end
  1115.  
  1116. local function mapToSortedList(map)
  1117.     local list = {}
  1118.  
  1119.     for _, entry in pairs(map) do
  1120.         table.insert(list, entry)
  1121.     end
  1122.  
  1123.     table.sort(list, function(a, b)
  1124.         return string.lower(a.name) < string.lower(b.name)
  1125.     end)
  1126.  
  1127.     return list
  1128. end
  1129.  
  1130. local function cleanupExpiredHolds(state)
  1131.     local t = now()
  1132.  
  1133.     for key, hold in pairs(state.hutDeliveryHold) do
  1134.         if (t - hold.time) >= DELIVERY_HOLD_SECONDS then
  1135.             state.hutDeliveryHold[key] = nil
  1136.         end
  1137.     end
  1138. end
  1139.  
  1140. -- ============================================================
  1141. -- COLLECT BUILDER + HUT DEMAND
  1142. -- ============================================================
  1143.  
  1144. local function collectDemands(ctx, allRequests)
  1145.     local state = ctx.state
  1146.     local demands = {}
  1147.     local builderDisplayMap = {}
  1148.     local hutDisplayMap = {}
  1149.  
  1150.     cleanupExpiredHolds(state)
  1151.  
  1152.     -- --------------------------------------------------------
  1153.     -- BUILDER HUT REQUIRED RESOURCES
  1154.     -- --------------------------------------------------------
  1155.  
  1156.     local hutResources = colony.getBuilderResources(ctx.hut.pos) or {}
  1157.  
  1158.     for _, resource in ipairs(hutResources) do
  1159.         local item = resource.item
  1160.  
  1161.         if type(item) == "table" then
  1162.             local rawShortage = math.max(
  1163.                 0,
  1164.                 numberValue(resource.needs)
  1165.                 - numberValue(resource.available)
  1166.                 - numberValue(resource.delivering)
  1167.             )
  1168.  
  1169.             if rawShortage > 0 then
  1170.                 local demand = ensureDemand(
  1171.                     demands,
  1172.                     item,
  1173.                     resource.displayName
  1174.                 )
  1175.  
  1176.                 if demand then
  1177.                     demand.hutNeedRaw = demand.hutNeedRaw + rawShortage
  1178.  
  1179.                     addDisplay(
  1180.                         hutDisplayMap,
  1181.                         item,
  1182.                         resource.displayName,
  1183.                         rawShortage
  1184.                     )
  1185.                 end
  1186.             end
  1187.         end
  1188.     end
  1189.  
  1190.     for key, demand in pairs(demands) do
  1191.         local held = 0
  1192.         local hold = state.hutDeliveryHold[key]
  1193.  
  1194.         if hold then
  1195.             held = hold.amount
  1196.         end
  1197.  
  1198.         demand.hutNeedEffective = math.max(
  1199.             0,
  1200.             demand.hutNeedRaw - held
  1201.         )
  1202.     end
  1203.  
  1204.     -- --------------------------------------------------------
  1205.     -- BUILDER PERSONAL REQUESTS
  1206.     -- --------------------------------------------------------
  1207.  
  1208.     local requests = getBuilderRequests(allRequests, ctx.builderName)
  1209.     local activeIds = {}
  1210.  
  1211.     for _, request in ipairs(requests) do
  1212.         if request.id then
  1213.             local requestId = tostring(request.id)
  1214.             activeIds[requestId] = true
  1215.  
  1216.             local item = chooseRequestItem(ctx, request, demands)
  1217.  
  1218.             if item then
  1219.                 local key = itemKey(item)
  1220.                 local count = tonumber(request.count) or 1
  1221.                 local requestState = state.requestState[requestId]
  1222.  
  1223.                 if not requestState
  1224.                 or requestState.count ~= count
  1225.                 or requestState.key ~= key then
  1226.                     requestState = {
  1227.                         count = count,
  1228.                         sent = 0,
  1229.                         key = key
  1230.                     }
  1231.  
  1232.                     state.requestState[requestId] = requestState
  1233.                 end
  1234.  
  1235.                 local outstanding = math.max(
  1236.                     0,
  1237.                     count - requestState.sent
  1238.                 )
  1239.  
  1240.                 addDisplay(
  1241.                     builderDisplayMap,
  1242.                     item,
  1243.                     nil,
  1244.                     count
  1245.                 )
  1246.  
  1247.                 if outstanding > 0 then
  1248.                     local demand = ensureDemand(demands, item)
  1249.  
  1250.                     if demand then
  1251.                         demand.builderNeed = demand.builderNeed + outstanding
  1252.  
  1253.                         table.insert(demand.requestRefs, {
  1254.                             id = requestId,
  1255.                             outstanding = outstanding
  1256.                         })
  1257.                     end
  1258.                 end
  1259.             end
  1260.         end
  1261.     end
  1262.  
  1263.     for requestId in pairs(state.requestState) do
  1264.         if not activeIds[requestId] then
  1265.             state.requestState[requestId] = nil
  1266.         end
  1267.     end
  1268.  
  1269.     -- --------------------------------------------------------
  1270.     -- MERGE
  1271.     -- --------------------------------------------------------
  1272.  
  1273.     for _, demand in pairs(demands) do
  1274.         -- One physical delivery can satisfy both MineColonies
  1275.         -- views, so do not add the two sources together.
  1276.         demand.totalNeed = math.max(
  1277.             demand.builderNeed,
  1278.             demand.hutNeedEffective
  1279.         )
  1280.     end
  1281.  
  1282.     state.builderDisplay = mapToSortedList(builderDisplayMap)
  1283.     state.hutDisplay = mapToSortedList(hutDisplayMap)
  1284.  
  1285.     return demands
  1286. end
  1287.  
  1288. -- ============================================================
  1289. -- DELIVERY ACCOUNTING
  1290. -- ============================================================
  1291.  
  1292. local function recordDelivery(ctx, demand, quantity)
  1293.     if quantity <= 0 then return end
  1294.  
  1295.     local state = ctx.state
  1296.     local left = quantity
  1297.  
  1298.     -- Distribute credit across personal request IDs.
  1299.     for _, ref in ipairs(demand.requestRefs) do
  1300.         if left <= 0 then break end
  1301.  
  1302.         local requestState = state.requestState[ref.id]
  1303.  
  1304.         if requestState then
  1305.             local outstanding = math.max(
  1306.                 0,
  1307.                 requestState.count - requestState.sent
  1308.             )
  1309.  
  1310.             local credit = math.min(left, outstanding)
  1311.             requestState.sent = requestState.sent + credit
  1312.             left = left - credit
  1313.         end
  1314.     end
  1315.  
  1316.     -- The same physical items also satisfy overlapping Hut demand.
  1317.     if demand.hutNeedEffective > 0 then
  1318.         local credit = math.min(quantity, demand.hutNeedEffective)
  1319.         local hold = state.hutDeliveryHold[demand.key]
  1320.  
  1321.         if not hold then
  1322.             hold = {
  1323.                 amount = 0,
  1324.                 time = now()
  1325.             }
  1326.  
  1327.             state.hutDeliveryHold[demand.key] = hold
  1328.         end
  1329.  
  1330.         hold.amount = hold.amount + credit
  1331.         hold.time = now()
  1332.     end
  1333. end
  1334.  
  1335. -- ============================================================
  1336. -- PROCESS ONE MERGED DEMAND
  1337. -- ============================================================
  1338.  
  1339. local function processDemand(ctx, demand)
  1340.     local state = ctx.state
  1341.     local need = demand.totalNeed
  1342.  
  1343.     if need <= 0 then
  1344.         state.alertState[demand.key] = nil
  1345.         state.craftRetry[demand.key] = nil
  1346.         return
  1347.     end
  1348.  
  1349.     if #ctx.outputs == 0 then
  1350.         state.statusText = "No Builder storage detected"
  1351.         return
  1352.     end
  1353.  
  1354.     -- --------------------------------------------------------
  1355.     -- PULL STORED ITEMS
  1356.     -- --------------------------------------------------------
  1357.  
  1358.     local _, available = getStored(ctx, demand.item)
  1359.  
  1360.     if available > 0 then
  1361.         local toSend = math.min(need, available)
  1362.         state.statusText = "Pulling " .. demand.displayName
  1363.  
  1364.         local moved = exportAcrossOutputs(
  1365.             ctx,
  1366.             demand.item,
  1367.             toSend
  1368.         )
  1369.  
  1370.         if moved > 0 then
  1371.             recordDelivery(ctx, demand, moved)
  1372.             addActivity(state, "PULL", demand.displayName, moved)
  1373.  
  1374.             print(
  1375.                 "[PULL][" .. ctx.customName .. "] "
  1376.                 .. demand.displayName
  1377.                 .. " x"
  1378.                 .. tostring(moved)
  1379.             )
  1380.  
  1381.             state.alertState[demand.key] = nil
  1382.             need = need - moved
  1383.         end
  1384.  
  1385.         -- If AE2 had the item but none/all could not fit, do not
  1386.         -- craft more until Builder storage has room.
  1387.         if moved < toSend then
  1388.             state.statusText = "Storage full/blocked"
  1389.             return
  1390.         end
  1391.     end
  1392.  
  1393.     if need <= 0 then
  1394.         state.statusText = "Supplied " .. demand.displayName
  1395.         return
  1396.     end
  1397.  
  1398.     -- --------------------------------------------------------
  1399.     -- CRAFTING
  1400.     -- --------------------------------------------------------
  1401.  
  1402.     if isCrafting(ctx, demand.item) then
  1403.         state.statusText = "Crafting " .. demand.displayName
  1404.         state.alertState[demand.key] = nil
  1405.         return
  1406.     end
  1407.  
  1408.     if isCraftable(ctx, demand.item) then
  1409.         local retryAt = state.craftRetry[demand.key] or 0
  1410.  
  1411.         if now() >= retryAt then
  1412.             state.statusText = "Starting craft: " .. demand.displayName
  1413.  
  1414.             local started = startCraft(
  1415.                 ctx,
  1416.                 demand.item,
  1417.                 need
  1418.             )
  1419.  
  1420.             state.craftRetry[demand.key] = now() + CRAFT_RETRY_SECONDS
  1421.  
  1422.             if started then
  1423.                 addActivity(state, "CRAFT", demand.displayName, need)
  1424.  
  1425.                 print(
  1426.                     "[CRAFT][" .. ctx.customName .. "] "
  1427.                     .. demand.displayName
  1428.                     .. " x"
  1429.                     .. tostring(need)
  1430.                 )
  1431.  
  1432.                 state.statusText = "Crafting " .. demand.displayName
  1433.                 state.alertState[demand.key] = nil
  1434.                 return
  1435.             end
  1436.  
  1437.             notifyMissing(ctx, demand, need)
  1438.             return
  1439.         end
  1440.  
  1441.         state.statusText = "Waiting for craft retry"
  1442.         return
  1443.     end
  1444.  
  1445.     -- --------------------------------------------------------
  1446.     -- NOT STORED / NOT CRAFTABLE
  1447.     -- --------------------------------------------------------
  1448.  
  1449.     notifyMissing(ctx, demand, need)
  1450. end
  1451.  
  1452. local function cleanupDemandState(ctx, demands)
  1453.     local state = ctx.state
  1454.  
  1455.     for key in pairs(state.alertState) do
  1456.         if not demands[key]
  1457.         or demands[key].totalNeed <= 0 then
  1458.             state.alertState[key] = nil
  1459.         end
  1460.     end
  1461.  
  1462.     for key in pairs(state.craftRetry) do
  1463.         if not demands[key]
  1464.         or demands[key].totalNeed <= 0 then
  1465.             state.craftRetry[key] = nil
  1466.         end
  1467.     end
  1468. end
  1469.  
  1470. -- ============================================================
  1471. -- UPDATE ONE BUILDER
  1472. -- ============================================================
  1473.  
  1474. local function updateBuilder(ctx, citizens, orders, allRequests)
  1475.     local state = ctx.state
  1476.  
  1477.     if not ctx.hut then
  1478.         state.statusText = "No assigned Builder Hut"
  1479.         state.builderDisplay = {}
  1480.         state.hutDisplay = {}
  1481.         return
  1482.     end
  1483.  
  1484.     local worker = findBuilderWorker(citizens, ctx.hut.pos)
  1485.     ctx.builderName = worker and worker.name or nil
  1486.  
  1487.     local order = findWorkOrderForHut(orders, ctx.hut.pos)
  1488.     ctx.activeBuildingName = order and order.buildingName or "Builder's Hut"
  1489.  
  1490.     if #ctx.outputs == 0 then
  1491.         state.statusText = "No writable storage faces"
  1492.     elseif not worker then
  1493.         -- Still supply Hut resources even if the worker is absent;
  1494.         -- only personal requests will be empty.
  1495.         state.statusText = "No Builder worker assigned"
  1496.     else
  1497.         state.statusText = "Checking colony demand..."
  1498.     end
  1499.  
  1500.     local ok, demands = pcall(
  1501.         collectDemands,
  1502.         ctx,
  1503.         allRequests
  1504.     )
  1505.  
  1506.     if not ok then
  1507.         state.statusText = "Demand read error"
  1508.         print(
  1509.             "[ERROR][" .. ctx.customName .. "] "
  1510.             .. tostring(demands)
  1511.         )
  1512.         return
  1513.     end
  1514.  
  1515.     for _, demand in pairs(demands) do
  1516.         if demand.totalNeed > 0 then
  1517.             local processOK, processErr = pcall(
  1518.                 processDemand,
  1519.                 ctx,
  1520.                 demand
  1521.             )
  1522.  
  1523.             if not processOK then
  1524.                 print(
  1525.                     "[ERROR][" .. ctx.customName .. "] "
  1526.                     .. demand.displayName
  1527.                     .. ": "
  1528.                     .. tostring(processErr)
  1529.                 )
  1530.  
  1531.                 state.statusText = "Error: " .. demand.displayName
  1532.             end
  1533.         end
  1534.     end
  1535.  
  1536.     cleanupDemandState(ctx, demands)
  1537.  
  1538.     if next(demands) == nil then
  1539.         state.statusText = "No current requirements"
  1540.     end
  1541. end
  1542.  
  1543. -- ============================================================
  1544. -- STARTUP DISPLAY
  1545. -- ============================================================
  1546.  
  1547. term.clear()
  1548. term.setCursorPos(1, 1)
  1549.  
  1550. print("========================================")
  1551. print(" MineColonies AE2 Builder Supplier v6")
  1552. print("========================================")
  1553. print("Colony: " .. colonyName)
  1554. print(
  1555.     "Computer anchor: "
  1556.     .. tostring(COMPUTER_POS.x) .. ","
  1557.     .. tostring(COMPUTER_POS.y) .. ","
  1558.     .. tostring(COMPUTER_POS.z)
  1559. )
  1560. print("Dynamic builder_# discovery: ON")
  1561. print("Dynamic Builder Hut assignment: ON")
  1562. print("Storage face probing: ON")
  1563. print("AE2 autocrafting: ON")
  1564. print("Chat alerts: ON")
  1565. print("Monitor dashboard: ON")
  1566. print("")
  1567. print("Press Ctrl+T to stop.")
  1568. print("")
  1569.  
  1570. -- ============================================================
  1571. -- INITIAL DISCOVERY
  1572. -- ============================================================
  1573.  
  1574. discoverTopology()
  1575.  
  1576. -- ============================================================
  1577. -- MAIN LOOP
  1578. -- ============================================================
  1579.  
  1580. while true do
  1581.     -- Periodically discover newly added builder_# bridges, huts,
  1582.     -- and newly writable storage faces.
  1583.     if (now() - lastTopologyScan) >= TOPOLOGY_RESCAN_SECONDS then
  1584.         local ok, err = pcall(discoverTopology)
  1585.  
  1586.         if not ok then
  1587.             print("[DISCOVERY ERROR] " .. tostring(err))
  1588.             lastTopologyScan = now()
  1589.         end
  1590.     end
  1591.  
  1592.     local citizens = colony.getCitizens() or {}
  1593.     local orders = colony.getWorkOrders() or {}
  1594.     local allRequests = colony.getRequests() or {}
  1595.  
  1596.     for _, ctx in ipairs(BUILDERS) do
  1597.         local ok, err = pcall(
  1598.             updateBuilder,
  1599.             ctx,
  1600.             citizens,
  1601.             orders,
  1602.             allRequests
  1603.         )
  1604.  
  1605.         if not ok then
  1606.             ctx.state.statusText = "Builder update error"
  1607.             print(
  1608.                 "[BUILDER ERROR][" .. ctx.customName .. "] "
  1609.                 .. tostring(err)
  1610.             )
  1611.         end
  1612.     end
  1613.  
  1614.     drawDashboard()
  1615.     sleep(POLL_INTERVAL)
  1616. end
  1617.  
Advertisement
Comments
  • User was banned
  • User was banned
Add Comment
Please, Sign In to add comment