Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function systemError(errorStr)
- local file = fs.open("errors","a")
- if file then
- file.writeLine(errorStr)
- file.close()
- end
- os.reboot()
- end
- if fs.exists("errors") then
- local file = fs.open("errors","r")
- if file then
- local line = file.readLine()
- print("The system has previously encountered an unexpected critical error that likely needs maintenance by a technical builder to fix.\nErrors:")
- while line do
- print(line)
- line = file.readLine()
- end
- file.close()
- error()
- else
- systemError("errors file located but could not be read.")
- end
- end
- shell.setDir("retrieval")
- dofile(shell.resolve("guiTools"))
- term.clear()
- activeGUI = nil
- db = {}
- entryString = ""
- dbAltNameCache = {}
- retrievalQueue = {}
- dbFile = "retDB"
- chestValues = {colors.white,colors.orange,colors.magenta,colors.yellow,colors.lime}
- motors = { down = colors.gray, up = colors.pink }
- signals = { retriever = colors.lightGray, filter = colors.cyan, itemDetector = colors.purple}
- function loadDB()
- db = {}
- local file = fs.open(dbFile,"r")
- db = textutils.unserialize(file.readAll())
- file.close()
- end
- function buildCache()
- entryString = ""
- dbAltNameCache = {}
- for k,v in pairs(db) do
- for i,n in pairs(v.altNames) do
- dbAltNameCache[string.lower(n)] = k
- entryString = entryString ..";"..string.lower(n)
- end
- dbAltNameCache[string.lower(k)] = k
- entryString = entryString ..";"..string.lower(k)
- end
- end
- loadDB()
- buildCache()
- function findEntries(str)
- if str == "" then return {} end
- local pat = "([^;]*"..string.lower(str).."[^;]*)"
- local m = string.gmatch(entryString,pat)
- local r = m()
- local results = {}
- while r do
- if dbAltNameCache[r] == nil then error("No table index #"..r.."#") end
- results[dbAltNameCache[r]] = true
- r = m()
- end
- local realResults = {}
- for k,v in pairs(results) do table.insert(realResults,k) end
- return realResults
- end
- guis = {}
- dofile(shell.resolve("gui_main"))
- activeGUI = guis["main"]
- function initGUI()
- if type(activeGUI.init) == "function" then activeGUI:init() end
- end
- function addRetrievalRequest(itemName,itemCount)
- table.insert(retrievalQueue,{name=itemName,count=itemCount,dbData = db[itemName]})
- if #retrievalQueue == 1 then os.queueEvent("retrieval_start") end
- os.queueEvent("retrieval_update")
- end
- initGUI()
- function guiLoop()
- while false do
- local p = read()
- if tonumber(p) ~= nil then
- local pc = tonumber(p)
- syncChests(pc)
- sleep(2)
- end
- end
- while true do
- local t = {os.pullEvent()}
- local cmd = table.remove(t,1)
- if type(activeGUI.events[cmd]) == "function" then
- activeGUI.events[cmd](activeGUI,unpack(t))
- end
- end
- end
- function getActiveChestNum()
- for k,v in pairs(chestValues) do
- if rs.testBundledInput("bottom",v) then return k end
- end
- systemError("No active chest found. Likely some frames behind the wall messed up or a piece of cable broke.")
- end
- function pulseMotor(col)
- rs.setBundledOutput("bottom",col)
- sleep(1)
- rs.setBundledOutput("bottom",0)
- sleep(0.3)
- end
- function syncChests(chestNum)
- local actChest = getActiveChestNum()
- local mod = chestNum - actChest
- local dir = "up"
- if mod < 0 then dir = "down" end
- local dangle = 0
- while getActiveChestNum() ~= chestNum do
- pulseMotor(motors[dir])
- dangle = dangle + 1
- if dangle > 20 then systemError("Can't reach requested chest (gave up after 20 frame moves). Likely some frames messed up, got stuck, a piece of cable broke or some motor ran out of power.") end
- end
- end
- function processRetrievalStep(actRet)
- syncChests(actRet.dbData.chest)
- sleep(0.1)
- local maxStackSize = actRet.dbData.chestStackSize
- local stacks = math.floor(actRet.count/maxStackSize)
- local items = actRet.count - (stacks*maxStackSize)
- if items > 0 then
- rs.setBundledOutput("back",items)
- sleep(0.3)
- rs.setBundledOutput("back",0)
- sleep(0.3)
- rs.setBundledOutput("back",actRet.dbData.slot)
- sleep(0.3)
- rs.setBundledOutput("back",0)
- sleep(4)
- rs.setBundledOutput("bottom",signals.retriever)
- sleep(0.3)
- rs.setBundledOutput("bottom",0)
- end
- if stacks > 0 then
- local addRequest = maxStackSize - items
- if addRequest > 0 then
- rs.setBundledOutput("back",addRequest)
- sleep(0.3)
- rs.setBundledOutput("back",0)
- sleep(0.3)
- rs.setBundledOutput("back",actRet.dbData.slot)
- sleep(0.3)
- rs.setBundledOutput("back",0)
- sleep(4)
- end
- for i=1,stacks,1 do
- rs.setBundledOutput("bottom",signals.retriever)
- sleep(0.3)
- rs.setBundledOutput("bottom",0)
- sleep(0.2)
- end
- end
- actRet.count = 0
- rs.setBundledOutput("bottom",signals.filter)
- sleep(0.3)
- rs.setBundledOutput("bottom",0)
- waitDetector()
- table.remove(retrievalQueue,1)
- end
- function waitDetector()
- while true do
- local ev = os.pullEvent("redstone")
- if rs.testBundledInput("bottom",signals.itemDetector) then break end
- end
- sleep(0.1)
- end
- function retrievalLoop()
- while true do
- if #retrievalQueue == 0 then
- local t = os.pullEvent("retrieval_start")
- end
- local actRet = retrievalQueue[1]
- while actRet.count > 0 do
- processRetrievalStep(actRet)
- os.queueEvent("retrieval_update")
- sleep(0.5)
- end
- sleep(0.2)
- end
- end
- parallel.waitForAny(guiLoop,retrievalLoop)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement