Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- minStockedPercent = 90 -- at which percentage to start moving items
- sleeptime = 3 --- time in seconds for the computer to sleep between item requests
- scrollSpeed = 3 --- amount of items to scroll when touching monitor
- specialGuy = 1 -- id of turtle to save specialAmount items when receiving update
- specialAmount = 128
- --initialize
- if fs.exists("addItem") == false then
- shell.run("pastebin", "get", "YUtHDsCC", "addItem")
- end
- inventory = nil
- fromInventories = {}
- monitor = nil
- if fs.exists("wanteditems.lua") then
- wantedItems = dofile("wanteditems.lua")
- else shell.run("addItem") end
- readCount = false
- local scroll = 1
- deviceCount = 0
- if fs.exists("API") == false then
- shell.run("pastebin", "get", "EzkfU5ZM", "API")
- end
- shaka = require("API")
- local sleeperTable = {}
- local notifyCount = 0
- -- 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)
- if modem.isWireless() then
- wirelessModem = peripheral.wrap(side)
- rednet.open(side)
- else
- wiredModem = peripheral.wrap(side)
- rednet.close(side)
- end
- elseif device == "monitor" then
- monitor = peripheral.wrap(side)
- end
- end
- if monitor == nil then
- window = shaka.centerWindow(40, 5, colors.red, colors.black)
- term.clear()
- window.clear()
- shaka.centerText("No monitor connected!", 2, window)
- shaka.changeColors(colors.red, colors.gray)
- shaka.centerText("Connect one on any side to continue..", 4, window)
- event, side = os.pullEvent("peripheral")
- print("Rebooting.")
- sleep(2)
- os.reboot()
- end
- if rednet.isOpen() == false then
- window = shaka.centerWindow(40, 5, colors.red, colors.black)
- term.clear()
- window.clear()
- shaka.centerText("No wireless modem connected!", 2, window)
- shaka.changeColors(colors.red, colors.gray)
- shaka.centerText("Connect one on any side to continue..", 4, window)
- event, side = os.pullEvent("peripheral")
- print("Rebooting.")
- sleep(2)
- os.reboot()
- end
- --connect inventories
- function tableContains(t, item)
- for _, value in pairs(t) do
- if value == item then
- return true
- end
- end
- return false
- end
- if fs.exists("inventories") then
- fromInventories = shaka.readFile("inventories")
- end
- local inventories = wiredModem.getNamesRemote()
- for _, inventoryName in ipairs(inventories) do
- if shaka.stringFind(inventoryName, "turtle") == false and inventoryName ~= nil and shaka.stringFind(inventoryName, "computer") == false then
- if not tableContains(fromInventories, inventoryName) then
- table.insert(fromInventories, inventoryName)
- end
- end
- end
- shaka.writeFile("inventories", fromInventories)
- function centerText(monitor, y, text)
- local w, h = monitor.getSize()
- local x = math.floor((w - string.len(text) + 2) / 2)
- monitor.setCursorPos(x , y)
- print(text)
- end
- function showDesktop()
- term.clear()
- term.setCursorPos(1,1)
- term.setTextColor(colors.lightGray)
- centerText(term, 5, "Click screen or hit space to view or add items.\n")
- centerText(term, 7,"No requests will be made while in menu.")
- local count = 0
- for _, items in pairs(wantedItems) do
- count = count + #items
- end
- term.setTextColor(colors.green)
- centerText(term, 9, "Currently keeping " ..count.. " items in stock.")
- term.setTextColor(colors.white)
- end
- showDesktop()
- ----functions
- function split(s, delimiter)
- result = {};
- for match in (s..delimiter):gmatch("(.-)"..delimiter) do
- table.insert(result, match);
- end
- return result;
- end
- function showItems()
- monitor.setTextScale(0.5)
- local items = {}
- for channel, itemsData in pairs(wantedItems) do
- for _, itemData in pairs(itemsData) do
- local item = string.match(itemData[1], ":(.*)")
- if item then
- item = string.gsub(item, "_", " ")
- table.insert(items, item .. ": " .. itemData[2])
- end
- end
- end
- table.sort(items)
- local _, maxLines = monitor.getSize()
- while true do
- monitor.clear()
- monitor.setCursorPos(1,1)
- monitor.setTextColor(colors.yellow)
- monitor.setBackgroundColor(colors.gray)
- monitor.clearLine()
- monitor.write("Monitored items:")
- monitor.setBackgroundColor(colors.black)
- monitor.setTextColor(colors.lightGray)
- for i = scroll, scroll + maxLines - 2 do
- monitor.setCursorPos(1, i - scroll + 2)
- monitor.write(items[i] or "")
- end
- local event, side, x, y = os.pullEvent("monitor_touch")
- if #items >= maxLines then
- if y >= maxLines/2 then
- scroll = math.min(scroll + scrollSpeed, #items - maxLines + 2)
- else
- scroll = math.max(scroll - scrollSpeed, 1)
- end
- end
- end
- end
- function scanInventory(var)
- items = { }
- for i= 1, #fromInventories do
- invWrap = peripheral.wrap(fromInventories[i])
- if invWrap == nil then
- fromInventories[i] = nil
- shaka.writeFile("inventories", fromInventories)
- break
- end
- for k, v in pairs(invWrap.list()) do
- for prop, value in pairs(v) do
- if prop == "name" then
- for channel, itemsData in pairs(var) do
- for _, itemData in pairs(itemsData) do
- if itemData[1] == value then
- readCount = true
- itemName = value
- end
- end
- end
- end
- -----add itemcount to table
- if prop == "count" and readCount == true then
- readCount = false
- if items[itemName] == nil then
- items[itemName] = 0
- end
- items[itemName] = items[itemName] + value
- end
- end
- end
- end
- end
- function notifyComputer()
- for channel, itemsData in pairs(wantedItems) do
- for _, itemData in pairs(itemsData) do
- local item = itemData[1]
- local wantedCount = itemData[2]
- local currentCount = items[item] or 0
- local restockCount = 0
- if wantedCount > currentCount then
- restockCount = wantedCount - currentCount
- end
- local percent = 100 * (currentCount/wantedCount)
- if percent < minStockedPercent and sleeperTable[item] ~= true then
- msg = table.concat({item, restockCount, wantedCount}, " ")
- BRmsg = table.concat({item, restockCount, channel}, " ")
- rednet.send(channel, msg, "stockpile")
- -- print(channel, msg)
- -- sleep(2)
- rednet.broadcast(BRmsg, "listen")
- sleep(0.5)
- end
- end
- end
- end
- -- Function to receive rednet message
- function receiveRednetMessage(protocolName)
- local senderID, message, protocol = rednet.receive(protocolName)
- -- print(message, protocol)
- if protocol == "stockpileupdate" then
- if message:find("saveItem ") == 1 then
- local itemName = message:sub(10)
- local itemExists = false
- for id, items in pairs(wantedItems) do
- for _, item in ipairs(items) do
- if item[1] == itemName and id == senderID then
- itemExists = true
- break
- end
- end
- if itemExists then
- rednet.send(senderID, "nope", "stockPileConfirm")
- break
- end
- end
- if not itemExists then
- if senderID == specialGuy then
- addItem(senderID, itemName, specialAmount)
- else
- addItem(senderID, itemName, 1)
- end
- rednet.send(senderID, "gotcha", "stockPileConfirm")
- end
- end
- elseif protocol == "tableUpdate" and message == "give" then
- local serializedTable = textutils.serialize(wantedItems)
- rednet.broadcast(serializedTable, "tableUpdate")
- elseif protocol == "tableChange" then
- split(message, " ")
- local ID = tonumber(result[1])
- local itemName = result[2]
- local itemCount = tonumber(result[3])
- removeItem(wantedItems, ID, tostring(itemName))
- if itemCount ~= 0 then
- addItem(ID, itemName, itemCount)
- end
- saveWantedItems()
- local serializedTable = textutils.serialize(wantedItems)
- rednet.broadcast("done", "tableConfirm")
- elseif protocol == "giefIDplz" then
- rednet.send(senderID, "here", "disBeID")
- elseif protocol == "cantCraft" then
- sleeperTable[message] = true
- end
- end
- ---remove items from table
- function removeItem(tableData, id, item)
- local subTable = tableData[id]
- if not subTable then
- return
- end
- for i = #subTable, 1, -1 do
- if subTable[i][1] == item then
- table.remove(subTable, i)
- end
- end
- if #subTable == 0 then
- tableData[id] = nil
- end
- end
- -- Function to add an item to the wanted items table
- function addItem(computerID, itemName, itemCount)
- if wantedItems[computerID] == nil then
- wantedItems[computerID] = {}
- end
- table.insert(wantedItems[computerID], {itemName, itemCount})
- saveWantedItems()
- end
- -- Function to save the wanted items
- function saveWantedItems()
- for id, items in pairs(wantedItems) do
- for _, item in ipairs(items) do
- if not id or not item[1] or not item[2] then
- print("Computer ID, item name, and item count must not be empty. Hit any button to try again.")
- event, key = os.pullEvent("key")
- return
- end
- end
- end
- file = fs.open("wanteditems.lua", "w")
- file.write("return {\n")
- for id, items in pairs(wantedItems) do
- file.write((" [%d] = {\n"):format(id))
- for _, item in ipairs(items) do
- file.write((" {%q, %d},\n"):format(item[1], item[2]))
- end
- file.write(" },\n")
- end
- file.write("}\n")
- file.close()
- end
- function switchToMenu()
- local event = os.pullEvent("mouse_click")
- if event then
- switchmenu = true
- end
- end
- function switchToMenu2()
- local event, key = os.pullEvent("key")
- if key == keys.space or key == keys.enter then
- switchmenu = true
- end
- end
- function dostuff()
- sleep(sleeptime)
- scanInventory(wantedItems)
- notifyComputer()
- notifyCount = notifyCount + 1
- if notifyCount > 10 then
- sleeperTable = {}
- notifyCount = 0
- end
- end
- function receiveStockPile()
- receiveRednetMessage("stockpileupdate")
- end
- function receiveTableUpdate()
- receiveRednetMessage("tableUpdate")
- end
- function receiveTableChange()
- receiveRednetMessage("tableChange")
- end
- function receiveIdRequest()
- receiveRednetMessage("giefIDplz")
- end
- function receiveCraftUnable()
- receiveRednetMessage("cantCraft")
- -- print("cantCraft received")
- end
- while true do
- if switchmenu == true then
- shell.run("addItem")
- end
- parallel.waitForAny(
- --general stuff
- dostuff, showItems, switchToMenu, switchToMenu2,
- --rednet protocols
- receiveStockPile, receiveTableUpdate, receiveTableChange, receiveIdRequest, receiveCraftUnable)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement