Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- ============================================================
- -- MineColonies Research Supplier v3.2
- -- ATM10 / Minecraft 1.21.1
- -- CC:Tweaked + Advanced Peripherals + AE2 + MineColonies
- -- ============================================================
- --
- -- PURPOSE
- -- 1. Finds the ME Bridge whose custom name is "Research".
- -- 2. Finds the inventory directly touching that ME Bridge.
- -- 3. Reads MineColonies research through Colony Integrator.
- -- 4. Finds research which is currently available to start:
- -- status == NOT_STARTED
- -- all requirements are fulfilled
- -- parent research path is already finished
- -- 5. Totals the item costs for ALL currently-available research.
- -- 6. Pulls those items from AE2 into the Research inventory.
- -- 7. If AE2 is short but an item is craftable, requests an
- -- AE2 autocraft and stages it on a later pass.
- -- 8. When research availability changes, items previously staged
- -- by this program are pulled back into AE2 and the exact new
- -- requirement set is staged.
- --
- -- IMPORTANT
- -- The inventory touching the Research ME Bridge should be used as
- -- a dedicated research-material buffer. The program only pulls back
- -- quantities that IT previously exported, so unrelated items are
- -- left alone.
- --
- -- The program does NOT start research. MineColonies/Advanced
- -- Peripherals exposes research information, but no research-start
- -- method. You still click the research manually in the University.
- --
- -- ============================================================
- -- ============================================================
- -- CONFIG
- -- ============================================================
- local RESEARCH_BRIDGE_NAME = "Research"
- -- nil = automatically probe all six faces of the Research ME Bridge.
- -- If automatic detection ever chooses the wrong face, set this to:
- -- "north", "south", "east", "west", "up", or "down"
- local OUTPUT_SIDE = nil
- -- How often to completely reconcile the research buffer.
- -- This includes reclaiming the program's prior staged quantities and
- -- restaging the exact current requirement set.
- local POLL_SECONDS = 30
- -- Prevents one pass from submitting an absurd number of simultaneous
- -- new AE2 craft jobs. Existing jobs do not count against this limit.
- local MAX_NEW_CRAFTS_PER_PASS = 12
- -- Probe items used only to discover which ME Bridge face touches an
- -- inventory. One item is exported and immediately imported back.
- local PROBE_ITEMS = {
- "minecraft:cobblestone",
- "minecraft:stone",
- "minecraft:dirt",
- "minecraft:iron_ingot",
- "minecraft:oak_planks",
- "minecraft:glass"
- }
- local SIDES = {
- "north",
- "south",
- "east",
- "west",
- "up",
- "down"
- }
- local STATE_FILE = "research_supplier_state"
- -- ============================================================
- -- GENERIC HELPERS
- -- ============================================================
- local function parseTransferred(result)
- if type(result) == "number" then
- return result
- end
- if type(result) == "table" then
- if type(result.count) == "number" then
- return result.count
- end
- if type(result.amount) == "number" then
- return result.amount
- end
- if type(result.transferred) == "number" then
- return result.transferred
- end
- end
- return 0
- end
- local function storedCount(result)
- if type(result) ~= "table" then
- return 0
- end
- if type(result.count) == "number" then
- return result.count
- end
- if type(result.amount) == "number" then
- return result.amount
- end
- return 0
- end
- local function cleanDisplayName(value)
- if value == nil then
- return "Unknown Item"
- end
- local text = tostring(value)
- text = text:gsub("^%[", "")
- text = text:gsub("%]$", "")
- return text
- end
- -- Makes a serialization-safe independent copy of nested AP tables.
- -- This avoids CC:Tweaked's "Cannot serialize table with repeated
- -- entries" problem.
- local function serializableCopy(value, ancestors)
- local valueType = type(value)
- if valueType == "nil"
- or valueType == "boolean"
- or valueType == "number"
- or valueType == "string" then
- return value
- end
- if valueType ~= "table" then
- return nil
- end
- ancestors = ancestors or {}
- if ancestors[value] then
- return nil
- end
- ancestors[value] = true
- local result = {}
- for key, child in pairs(value) do
- local keyType = type(key)
- if keyType == "string"
- or keyType == "number"
- or keyType == "boolean" then
- local cleanChild =
- serializableCopy(
- child,
- ancestors
- )
- if cleanChild ~= nil then
- result[key] = cleanChild
- end
- end
- end
- ancestors[value] = nil
- return result
- end
- local function snapshotItem(item)
- if type(item) ~= "table" then
- return nil
- end
- local result = {
- name = item.name,
- fingerprint = item.fingerprint
- }
- if type(item.components) == "table"
- and next(item.components) then
- result.components =
- serializableCopy(
- item.components
- )
- end
- -- Some AP/MineColonies versions expose legacy NBT rather than
- -- components. Preserve it if present.
- if item.nbt ~= nil then
- result.nbt =
- serializableCopy(
- item.nbt
- )
- end
- return result
- end
- local function itemKey(item)
- if type(item) ~= "table"
- or not item.name then
- return nil
- end
- -- Research costs are normally ordinary items. Prefer a stable
- -- name/components/NBT identity rather than a MineColonies
- -- fingerprint so AE2 autocrafting can use it.
- local extra = ""
- if type(item.components) == "table"
- and next(item.components) then
- extra =
- ":components="
- .. textutils.serialize(
- serializableCopy(
- item.components
- )
- )
- elseif item.nbt ~= nil then
- extra =
- ":nbt="
- .. textutils.serialize(
- serializableCopy(
- item.nbt
- )
- )
- end
- return tostring(item.name) .. extra
- end
- local function makeStorageFilter(item, count)
- local filter = {}
- -- Exact fingerprint is useful when AP provides one for a stored
- -- stack. Research costs usually do not, so name/components is the
- -- normal route.
- if item.fingerprint then
- filter.fingerprint = item.fingerprint
- else
- filter.name = item.name
- if type(item.components) == "table"
- and next(item.components) then
- filter.components = item.components
- elseif item.nbt ~= nil then
- filter.nbt = item.nbt
- end
- end
- if count then
- filter.count = count
- end
- return filter
- end
- local function makeCraftFilter(item, count)
- -- IMPORTANT:
- -- In this ATM10/AP setup, craftability/crafting should use the
- -- registry name/components rather than a MineColonies fingerprint.
- local filter = {
- name = item.name
- }
- if type(item.components) == "table"
- and next(item.components) then
- filter.components = item.components
- elseif item.nbt ~= nil then
- filter.nbt = item.nbt
- end
- if count then
- filter.count = count
- end
- return filter
- end
- -- ============================================================
- -- PERSISTENT STATE
- -- ============================================================
- local STATE = {
- outputSides = {},
- -- [itemKey] = {
- -- item = serialization-safe item identity,
- -- displayName = string,
- -- count = amount this program most recently staged
- -- }
- staged = {}
- }
- local function loadState()
- if not fs.exists(STATE_FILE) then
- return
- end
- local handle = fs.open(STATE_FILE, "r")
- if not handle then
- return
- end
- local raw = handle.readAll()
- handle.close()
- local ok, decoded =
- pcall(
- textutils.unserialize,
- raw
- )
- if ok and type(decoded) == "table" then
- STATE.outputSides =
- type(decoded.outputSides) == "table"
- and decoded.outputSides
- or (
- decoded.outputSide
- and {decoded.outputSide}
- or {}
- )
- STATE.staged =
- type(decoded.staged) == "table"
- and decoded.staged
- or {}
- else
- print("[STATE] Old/invalid state ignored.")
- end
- end
- local function saveState()
- local clean =
- serializableCopy(
- STATE
- )
- local ok, encoded =
- pcall(
- textutils.serialize,
- clean,
- {compact = true}
- )
- if not ok then
- print(
- "[STATE ERROR] "
- .. tostring(encoded)
- )
- return
- end
- local temp = STATE_FILE .. ".tmp"
- local handle = fs.open(temp, "w")
- if not handle then
- print("[STATE ERROR] Could not open temp state file.")
- return
- end
- handle.write(encoded)
- handle.close()
- if fs.exists(STATE_FILE) then
- fs.delete(STATE_FILE)
- end
- fs.move(temp, STATE_FILE)
- end
- loadState()
- -- ============================================================
- -- FIND COLONY INTEGRATOR
- -- ============================================================
- local colony = peripheral.find("colony_integrator")
- if not colony then
- error("No colony_integrator found on the wired network.")
- end
- -- ============================================================
- -- FIND CUSTOM-NAMED RESEARCH ME BRIDGE
- -- ============================================================
- local function findResearchBridge()
- local target =
- string.lower(
- RESEARCH_BRIDGE_NAME
- )
- for _, name in ipairs(peripheral.getNames()) do
- if peripheral.getType(name) == "me_bridge" then
- local bridge = peripheral.wrap(name)
- local ok, customName =
- pcall(
- bridge.getName
- )
- if ok
- and type(customName) == "string"
- and string.lower(customName) == target then
- return bridge, name, customName
- end
- end
- end
- return nil
- end
- local me,
- mePeripheralName,
- meCustomName =
- findResearchBridge()
- if not me then
- error(
- 'Could not find an ME Bridge custom-named "'
- .. RESEARCH_BRIDGE_NAME
- .. '".'
- )
- end
- -- ============================================================
- -- OUTPUT-INVENTORY FACE DETECTION (ALL VALID FACES)
- -- ============================================================
- local function getProbeItem()
- for _, itemName in ipairs(PROBE_ITEMS) do
- local ok, result =
- pcall(
- me.getItem,
- {name = itemName}
- )
- if ok
- and storedCount(result) > 0 then
- return itemName
- end
- end
- return nil
- end
- local function probeSide(side, probeItem)
- local ok, result =
- pcall(
- me.exportItem,
- {
- name = probeItem,
- count = 1
- },
- side
- )
- if not ok then
- return false
- end
- local moved =
- parseTransferred(result)
- if moved <= 0 then
- return false
- end
- -- Immediately put the probe back into AE2.
- local importOK, importResult =
- pcall(
- me.importItem,
- {
- name = probeItem,
- count = moved
- },
- side
- )
- local restored =
- importOK
- and parseTransferred(importResult)
- or 0
- if restored < moved then
- print(
- "[PROBE WARNING] "
- .. tostring(moved - restored)
- .. " probe item(s) remained on "
- .. side
- .. "."
- )
- end
- return true
- end
- local function detectOutputSides()
- if OUTPUT_SIDE then
- return {OUTPUT_SIDE}
- end
- local probeItem =
- getProbeItem()
- if not probeItem then
- error(
- "Could not auto-detect the Research inventories because "
- .. "none of the configured probe items exist in AE2. "
- .. "Either put cobblestone/stone/dirt in AE2 or set "
- .. "OUTPUT_SIDE manually near the top of the script."
- )
- end
- local found = {}
- for _, side in ipairs(SIDES) do
- if probeSide(side, probeItem) then
- table.insert(found, side)
- end
- end
- if #found == 0 then
- error(
- "No inventory detected directly beside the Research ME Bridge."
- )
- end
- STATE.outputSides = found
- saveState()
- return found
- end
- local outputSides =
- detectOutputSides()
- -- ============================================================
- -- RESEARCH TREE
- -- ============================================================
- local function normalizedStatus(status)
- local text =
- string.upper(
- tostring(status or "")
- )
- text =
- text:gsub(
- "%s+",
- "_"
- )
- return text
- end
- local function requirementsFulfilled(research)
- for _, requirement
- in pairs(research.requirements or {}) do
- if requirement.fulfilled ~= true then
- return false
- end
- end
- return true
- end
- local function collectAvailableResearch()
- local tree =
- colony.getResearch()
- or {}
- local available = {}
- local unknownStatuses = {}
- local function walk(branch, list, depth)
- if type(list) ~= "table" then
- return
- end
- for _, research in pairs(list) do
- if type(research) == "table" then
- local status =
- normalizedStatus(
- research.status
- )
- if status == "FINISHED" then
- -- Only walk further down a research chain once
- -- the parent research is actually complete.
- walk(
- branch,
- research.children,
- depth + 1
- )
- elseif status == "NOT_STARTED"
- or status == "NOTSTARTED" then
- if requirementsFulfilled(research) then
- table.insert(
- available,
- {
- branch = branch,
- depth = depth,
- research = research
- }
- )
- end
- elseif status == "IN_PROGRESS"
- or status == "INPROGRESS" then
- -- Do not stage its cost. MineColonies has already
- -- accepted/consumed what was required to start it.
- else
- unknownStatuses[status] = true
- end
- end
- end
- end
- for branch, list in pairs(tree) do
- walk(
- tostring(branch),
- list,
- 0
- )
- end
- return available,
- unknownStatuses
- end
- -- ============================================================
- -- TOTAL CURRENT RESEARCH COSTS
- -- ============================================================
- -- Build ONE AE2 snapshot per polling pass.
- --
- -- The previous version queried me.getItem()/isCraftable() once for
- -- every entry in every MineColonies validItems list. Some research
- -- costs have very large validItems lists, so that could make the
- -- program appear frozen immediately after "Starting...".
- --
- -- On the 1.21.1 AP storage API, getItems({}) and
- -- getCraftableItems({}) let us fetch the whole picture once.
- local function buildAESnapshot()
- local snapshot = {
- storedByName = {},
- craftableByName = {},
- hasStoredSnapshot = false,
- hasCraftableSnapshot = false
- }
- -- Current 1.21.1 / AP storage-system API.
- if type(me.getItems) == "function" then
- local ok, items =
- pcall(
- me.getItems,
- {}
- )
- if ok
- and type(items) == "table" then
- snapshot.hasStoredSnapshot = true
- for _, item in pairs(items) do
- if type(item) == "table"
- and item.name then
- local count =
- storedCount(item)
- snapshot.storedByName[item.name] =
- (snapshot.storedByName[item.name] or 0)
- + count
- if item.isCraftable == true then
- snapshot.craftableByName[item.name] = true
- end
- end
- end
- end
- end
- if type(me.getCraftableItems) == "function" then
- local ok, items =
- pcall(
- me.getCraftableItems,
- {}
- )
- if ok
- and type(items) == "table" then
- snapshot.hasCraftableSnapshot = true
- for _, item in pairs(items) do
- if type(item) == "table"
- and item.name then
- snapshot.craftableByName[item.name] = true
- end
- end
- end
- end
- -- Compatibility with older bridge method names, just in case.
- if not snapshot.hasStoredSnapshot
- and type(me.listItems) == "function" then
- local ok, items =
- pcall(
- me.listItems
- )
- if ok
- and type(items) == "table" then
- snapshot.hasStoredSnapshot = true
- for _, item in pairs(items) do
- if type(item) == "table"
- and item.name then
- snapshot.storedByName[item.name] =
- (snapshot.storedByName[item.name] or 0)
- + storedCount(item)
- if item.isCraftable == true then
- snapshot.craftableByName[item.name] = true
- end
- end
- end
- end
- end
- if not snapshot.hasCraftableSnapshot
- and type(me.listCraftableItems) == "function" then
- local ok, items =
- pcall(
- me.listCraftableItems
- )
- if ok
- and type(items) == "table" then
- snapshot.hasCraftableSnapshot = true
- for _, item in pairs(items) do
- if type(item) == "table"
- and item.name then
- snapshot.craftableByName[item.name] = true
- end
- end
- end
- end
- return snapshot
- end
- local function chooseCostItem(validItems, aeSnapshot)
- if type(validItems) ~= "table" then
- return nil
- end
- local candidates = {}
- for _, candidate in pairs(validItems) do
- if type(candidate) == "table"
- and candidate.name then
- table.insert(
- candidates,
- snapshotItem(candidate)
- )
- end
- end
- if #candidates == 0 then
- return nil
- end
- -- Prefer the acceptable alternative with the greatest quantity
- -- already in AE2. This is now just local table lookup, not a
- -- peripheral call for every candidate.
- local bestStored = nil
- local bestCount = 0
- if aeSnapshot
- and aeSnapshot.hasStoredSnapshot then
- for _, candidate in ipairs(candidates) do
- local count =
- aeSnapshot.storedByName[candidate.name]
- or 0
- if count > bestCount then
- bestCount = count
- bestStored = candidate
- end
- end
- end
- if bestStored
- and bestCount > 0 then
- return bestStored
- end
- -- If none are currently stored, prefer one AE2 can autocraft.
- if aeSnapshot then
- for _, candidate in ipairs(candidates) do
- if aeSnapshot.craftableByName[candidate.name] then
- return candidate
- end
- end
- end
- -- Last fallback: choose the first MineColonies-valid option.
- -- The normal reconcile stage will still perform the live
- -- getItem()/isCraftable() check for this ONE selected item.
- return candidates[1]
- end
- local function collectDesiredCosts(available, aeSnapshot)
- local desired = {}
- local function addChosenItem(item, count, research)
- if not item
- or not item.name then
- return
- end
- local key =
- itemKey(item)
- count =
- tonumber(count)
- or 1
- if not key
- or count <= 0 then
- return
- end
- local demand =
- desired[key]
- if not demand then
- demand = {
- item = item,
- displayName =
- cleanDisplayName(
- item.displayName
- or item.name
- ),
- count = 0,
- researches = {}
- }
- desired[key] = demand
- end
- demand.count =
- demand.count
- + count
- table.insert(
- demand.researches,
- cleanDisplayName(
- research.name
- or research.id
- or "Unknown Research"
- )
- )
- end
- for _, entry in ipairs(available) do
- local research =
- entry.research
- local cost =
- research.cost
- if type(cost) == "table" then
- -- Live ATM10 / MineColonies structure:
- --
- -- research.cost = {
- -- {
- -- validItems = {
- -- { name=..., fingerprint=..., components=..., ... },
- -- ...
- -- },
- -- count = 16
- -- },
- -- ...
- -- }
- --
- -- validItems are alternatives. We need ONE valid item for
- -- each cost entry, not every item in the list.
- if type(cost.validItems) == "table" then
- local chosen =
- chooseCostItem(
- cost.validItems,
- aeSnapshot
- )
- addChosenItem(
- chosen,
- cost.count,
- research
- )
- elseif cost.name then
- addChosenItem(
- snapshotItem(cost),
- cost.count,
- research
- )
- else
- for _, costEntry in pairs(cost) do
- if type(costEntry) == "table" then
- if type(costEntry.validItems) == "table" then
- local chosen =
- chooseCostItem(
- costEntry.validItems,
- aeSnapshot
- )
- addChosenItem(
- chosen,
- costEntry.count,
- research
- )
- elseif type(costEntry.item) == "table" then
- addChosenItem(
- snapshotItem(costEntry.item),
- costEntry.count
- or costEntry.item.count,
- research
- )
- elseif costEntry.name then
- addChosenItem(
- snapshotItem(costEntry),
- costEntry.count,
- research
- )
- end
- end
- end
- end
- end
- end
- return desired
- end
- -- ============================================================
- -- AE2 HELPERS
- -- ============================================================
- local function getStored(item)
- local ok, result =
- pcall(
- me.getItem,
- makeStorageFilter(item)
- )
- if not ok then
- return 0
- end
- return storedCount(result)
- end
- local function isCraftable(item)
- local ok, result =
- pcall(
- me.isCraftable,
- makeCraftFilter(item)
- )
- return ok
- and result == true
- end
- local function isCrafting(item)
- local ok, result =
- pcall(
- me.isCrafting,
- makeCraftFilter(item)
- )
- return ok
- and result == true
- end
- local function startCraft(item, count)
- if count <= 0 then
- return false, "Nothing to craft"
- end
- local ok, result, err =
- pcall(
- me.craftItem,
- makeCraftFilter(
- item,
- count
- )
- )
- if not ok then
- return false,
- tostring(result)
- end
- if result == true then
- return true
- end
- return false,
- tostring(err or result)
- end
- local function importPriorStage(entry)
- local remaining =
- tonumber(entry.count)
- or 0
- if remaining <= 0 then
- return true, 0
- end
- local totalMoved = 0
- local allOK = true
- for _, side in ipairs(outputSides) do
- if remaining <= 0 then
- break
- end
- local ok, result =
- pcall(
- me.importItem,
- makeStorageFilter(
- entry.item,
- remaining
- ),
- side
- )
- if ok then
- local moved =
- parseTransferred(result)
- totalMoved =
- totalMoved
- + moved
- remaining =
- math.max(
- 0,
- remaining - moved
- )
- else
- allOK = false
- print(
- "[RECALL ERROR]["
- .. side
- .. "] "
- .. tostring(result)
- )
- end
- end
- -- Whatever was actually present has now been reconciled. If a
- -- player already took some of the staged material, it simply
- -- will not be found and therefore will not be pulled back.
- entry.count = 0
- return allOK,
- totalMoved
- end
- local function exportDesired(item, count)
- local remaining =
- count
- local totalMoved = 0
- for _, side in ipairs(outputSides) do
- if remaining <= 0 then
- break
- end
- local ok, result =
- pcall(
- me.exportItem,
- makeStorageFilter(
- item,
- remaining
- ),
- side
- )
- if ok then
- local moved =
- parseTransferred(result)
- totalMoved =
- totalMoved
- + moved
- remaining =
- math.max(
- 0,
- remaining - moved
- )
- else
- print(
- "[EXPORT ERROR]["
- .. side
- .. "] "
- .. tostring(result)
- )
- end
- end
- return totalMoved
- end
- -- ============================================================
- -- RECONCILE RESEARCH BUFFER
- -- ============================================================
- local lastActions = {}
- local function addAction(text)
- table.insert(
- lastActions,
- text
- )
- if #lastActions > 12 then
- table.remove(
- lastActions,
- 1
- )
- end
- end
- local function reconcile(desired)
- lastActions = {}
- -- --------------------------------------------------------
- -- 1. Pull back exactly what this script believes it staged
- -- last pass.
- --
- -- This serves three purposes:
- -- * cleans materials for research no longer available
- -- * detects items the player already removed
- -- * lets us restage exact current totals without duplicates
- -- --------------------------------------------------------
- for key, entry in pairs(STATE.staged) do
- local ok, moved =
- importPriorStage(entry)
- if ok then
- if moved > 0 then
- addAction(
- "RECALL "
- .. entry.displayName
- .. " x"
- .. tostring(moved)
- )
- end
- if not desired[key] then
- STATE.staged[key] = nil
- end
- else
- addAction(
- "RECALL ERROR "
- .. entry.displayName
- )
- end
- end
- -- --------------------------------------------------------
- -- 2. Stage what exists now. Ask AE2 to craft shortages.
- -- --------------------------------------------------------
- local craftStarts = 0
- local sorted = {}
- for key, demand in pairs(desired) do
- demand.key = key
- table.insert(sorted, demand)
- end
- table.sort(
- sorted,
- function(a, b)
- return string.lower(a.displayName)
- < string.lower(b.displayName)
- end
- )
- for _, demand in ipairs(sorted) do
- local wanted =
- demand.count
- local stored =
- getStored(
- demand.item
- )
- local attempt =
- math.min(
- wanted,
- stored
- )
- local moved =
- exportDesired(
- demand.item,
- attempt
- )
- STATE.staged[demand.key] = {
- item =
- snapshotItem(
- demand.item
- ),
- displayName =
- demand.displayName,
- count =
- moved
- }
- if moved > 0 then
- addAction(
- "STAGE "
- .. demand.displayName
- .. " x"
- .. tostring(moved)
- .. "/"
- .. tostring(wanted)
- )
- end
- local missing =
- math.max(
- 0,
- wanted - moved
- )
- if missing > 0 then
- if isCraftable(demand.item) then
- if isCrafting(demand.item) then
- addAction(
- "CRAFTING "
- .. demand.displayName
- .. " x"
- .. tostring(missing)
- )
- elseif craftStarts
- < MAX_NEW_CRAFTS_PER_PASS then
- local started,
- err =
- startCraft(
- demand.item,
- missing
- )
- if started then
- craftStarts =
- craftStarts + 1
- addAction(
- "CRAFT "
- .. demand.displayName
- .. " x"
- .. tostring(missing)
- )
- else
- addAction(
- "CRAFT FAIL "
- .. demand.displayName
- .. " x"
- .. tostring(missing)
- )
- if err then
- print(
- "[CRAFT FAIL] "
- .. demand.displayName
- .. ": "
- .. tostring(err)
- )
- end
- end
- else
- addAction(
- "CRAFT QUEUED NEXT PASS "
- .. demand.displayName
- .. " x"
- .. tostring(missing)
- )
- end
- else
- addAction(
- "MISSING "
- .. demand.displayName
- .. " x"
- .. tostring(missing)
- )
- end
- end
- end
- saveState()
- end
- -- ============================================================
- -- TERMINAL DASHBOARD
- -- ============================================================
- local function drawDashboard(
- available,
- desired,
- unknownStatuses
- )
- term.clear()
- term.setCursorPos(1, 1)
- local colonyName =
- colony.getColonyName()
- or "Unknown Colony"
- print("========================================")
- print(" MINECOLONIES RESEARCH SUPPLIER v3.2")
- print("========================================")
- print("Colony: " .. tostring(colonyName))
- print("ME Bridge: " .. tostring(meCustomName))
- print("Output sides: " .. table.concat(outputSides, ", "))
- print("Poll: " .. tostring(POLL_SECONDS) .. "s")
- print("")
- print(
- "Available research: "
- .. tostring(#available)
- )
- for _, entry in ipairs(available) do
- local name =
- cleanDisplayName(
- entry.research.name
- or entry.research.id
- )
- print(" + " .. name)
- end
- print("")
- local itemKinds = 0
- local totalItems = 0
- for _, demand in pairs(desired) do
- itemKinds = itemKinds + 1
- totalItems =
- totalItems
- + demand.count
- end
- print(
- "Required item types: "
- .. tostring(itemKinds)
- )
- print(
- "Total research items: "
- .. tostring(totalItems)
- )
- print("")
- print("Last actions:")
- if #lastActions == 0 then
- print(" No item movement needed.")
- else
- for _, action in ipairs(lastActions) do
- print(" " .. action)
- end
- end
- local unknown = {}
- for status in pairs(unknownStatuses or {}) do
- if status ~= "" then
- table.insert(
- unknown,
- status
- )
- end
- end
- if #unknown > 0 then
- table.sort(unknown)
- print("")
- print(
- "Unknown research status value(s): "
- .. table.concat(unknown, ", ")
- )
- end
- print("")
- print("Ctrl+T to stop.")
- end
- -- ============================================================
- -- STARTUP
- -- ============================================================
- print("MineColonies Research Supplier v3.2")
- print("Research bridge: " .. mePeripheralName)
- print("Custom name: " .. meCustomName)
- print("Output sides: " .. table.concat(outputSides, ", "))
- print("Starting...")
- sleep(1)
- -- ============================================================
- -- MAIN LOOP
- -- ============================================================
- while true do
- local ok, err =
- pcall(
- function()
- term.clear()
- term.setCursorPos(1, 1)
- print("Research Supplier v3.2")
- print("")
- print("Reading MineColonies research...")
- local available,
- unknownStatuses =
- collectAvailableResearch()
- print(
- "Found "
- .. tostring(#available)
- .. " available research entries."
- )
- print("Reading AE2 inventory/craftable snapshot...")
- local aeSnapshot =
- buildAESnapshot()
- print("Resolving research costs...")
- local desired =
- collectDesiredCosts(
- available,
- aeSnapshot
- )
- print("Staging/crafting materials...")
- reconcile(desired)
- drawDashboard(
- available,
- desired,
- unknownStatuses
- )
- end
- )
- if not ok then
- term.clear()
- term.setCursorPos(1, 1)
- print("RESEARCH SUPPLIER ERROR")
- print("")
- print(tostring(err))
- print("")
- print(
- "Retrying in "
- .. tostring(POLL_SECONDS)
- .. " seconds..."
- )
- end
- sleep(POLL_SECONDS)
- end
Advertisement