Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- connect all inventories via wired modems
- -- moves items to one target inventory
- --------------------------------------------
- -- Initialize
- local fromInventories = {}
- local modem = nil
- local dailyMoved = 0
- local movedCounter = 0
- local yPos = 1
- -- Define the inventory to move items to
- local targetInv = ""
- -- Load target inventory from file
- if fs.exists("targetInv") then
- local file = fs.open("targetInv", "r")
- targetInv = file.readAll()
- file.close()
- end
- -- Prompt user to select target inventory if not set
- if targetInv == "" then
- -- Find and display available inventories
- local modem = nil
- for i, side in ipairs(peripheral.getNames()) do
- local device = peripheral.getType(side)
- if device == "modem" then
- modem = peripheral.wrap(side)
- break
- end
- end
- if modem then
- local options = modem.getNamesRemote()
- term.clear()
- term.setCursorPos(1, 1)
- term.setTextColor(colors.black)
- term.setBackgroundColor(colors.yellow)
- print("Select a target inventory:")
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- for i, option in ipairs(options) do
- print(i .. ". " .. option)
- end
- local choice = tonumber(read())
- if choice and choice >= 1 and choice <= #options then
- targetInv = options[choice]
- local file = fs.open("targetInv", "w")
- file.write(targetInv)
- file.close()
- term.clear()
- term.setTextColor(colors.green)
- term.setCursorPos(1, 1)
- term.write("Set ")
- term.setTextColor(colors.yellow)
- term.write(targetInv)
- term.setTextColor(colors.green)
- print(" as target inventory.")
- term.setTextColor(colors.gray)
- print("\n\nPress any key to continue..")
- event, key = os.pullEvent("key")
- else
- print("Invalid choice.")
- return
- end
- else
- print("No modem detected.")
- return
- end
- end
- local toInventory = peripheral.wrap(targetInv)
- term.clear()
- term.setCursorPos(1,1)
- -- Connect peripherals
- local sides = {"left", "right", "top", "bottom", "front", "back"}
- for i, side in ipairs(sides) do
- local device = peripheral.getType(side)
- if device == "modem" then
- modem = peripheral.wrap(side)
- rednet.open(side)
- elseif device then
- local wrapper = peripheral.wrap(side)
- end
- end
- --initialize inventories
- newInventories = modem.getNamesRemote()
- for _, inventoryName in ipairs(newInventories) do
- if inventoryName ~= targetInv then
- table.insert(fromInventories, inventoryName)
- end
- end
- ---------functions
- local function centerText(text, y, monitor) -- centers text on monitor or terminal
- if monitor == nil then
- monitor = term
- end
- local w, h = monitor.getSize()
- local x = math.floor((w - string.len(text) + 2) / 2)
- if y == nil then
- local a, b = monitor.getCursorPos()
- y = b
- end
- monitor.setCursorPos(x , y)
- monitor.write(text)
- end
- function moveAllItems(sourceName, targetName)
- local sourceInv = peripheral.wrap(sourceName)
- local targetInv = peripheral.wrap(targetName)
- local tasks = {}
- local invData = sourceInv.list()
- local taskCount = 0
- local totalMovedItems = 0 -- new variable to track the total moved items
- for k, v in pairs(invData) do
- if taskCount >= 200 then
- break
- end
- taskCount = taskCount + 1
- tasks[#tasks+1] = function()
- local itemData = sourceInv.getItemDetail(k)
- local movedItems = sourceInv.pushItems(targetName, k)
- if movedItems < itemData.count or movedItems == 0 then
- -- return false
- else
- totalMovedItems = totalMovedItems + movedItems -- increment total moved items
- end
- end
- end
- parallel.waitForAll(unpack(tasks))
- return totalMovedItems -- return the total moved items
- end
- function formatNumber(number)
- local formatted = tostring(number)
- local k = #formatted % 3
- if k == 0 then k = 3 end
- formatted = formatted:sub(1, k) .. formatted:sub(k+1):gsub("(%d%d%d)", ".%1")
- return formatted
- end
- local function moveItems()
- local allInvItems = 0
- for _, inventoryName in pairs(fromInventories) do
- local invAmount = 0
- invAmount = moveAllItems(inventoryName, targetInv)
- allInvItems = allInvItems + invAmount
- if invAmount > 0 then
- term.setTextColor(colors.green)
- term.setCursorPos(1, 6)
- term.clearLine()
- centerText("Moved " ..allInvItems.. " items.", 6)
- sleep(0.1)
- else
- term.setTextColor(colors.white)
- term.setCursorPos(1, 6)
- term.clearLine()
- centerText("Currently waiting for work", 6)
- sleep(0.1)
- end
- end
- return allInvItems
- end
- function showUptime()
- local uptime = os.clock()
- local hours = math.floor(uptime / 3600)
- local minutes = math.floor((uptime % 3600) / 60)
- local seconds = math.floor(uptime % 60)
- -- Create the uptime string
- local uptimeStr = ""
- if hours > 0 then
- uptimeStr = uptimeStr .. string.format("%d:", hours)
- end
- if minutes > 0 or hours > 0 then
- uptimeStr = uptimeStr .. string.format("%d:", minutes)
- end
- uptimeStr = uptimeStr .. string.format("%d", seconds)
- -- Position the cursor and display the uptime string
- local w, h = term.getSize()
- term.setCursorPos(w - #uptimeStr + 1, h)
- term.setTextColor(colors.gray)
- term.clearLine()
- term.write(uptimeStr)
- -- Wait for one second
- sleep(1)
- end
- local function all()
- if not rs.getInput("top") then
- local kk = moveItems()
- term.setTextColor(colors.gray)
- dailyMoved = dailyMoved + kk
- showDaily = formatNumber(dailyMoved)
- term.setCursorPos(1, 15)
- term.clearLine()
- centerText("Total items moved since last restart: " ..showDaily, 15)
- term.setTextColor(colors.white)
- end
- end
- while true do
- parallel.waitForAny(all, showUptime)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement