Advertisement
Pinkishu

retrieval

Jan 20th, 2013
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.96 KB | None | 0 0
  1. function systemError(errorStr)
  2.     local file = fs.open("errors","a")
  3.     if file then
  4.         file.writeLine(errorStr)
  5.         file.close()
  6.     end
  7.     os.reboot()
  8. end
  9.  
  10. if fs.exists("errors") then
  11.     local file = fs.open("errors","r")
  12.     if file then
  13.         local line = file.readLine()
  14.         print("The system has previously encountered an unexpected critical error that likely needs maintenance by a technical builder to fix.\nErrors:")
  15.         while line do
  16.             print(line)
  17.             line = file.readLine()
  18.         end
  19.         file.close()
  20.         error()
  21.     else
  22.         systemError("errors file located but could not be read.")
  23.     end
  24. end
  25.  
  26.  
  27.  
  28. shell.setDir("retrieval")
  29. dofile(shell.resolve("guiTools"))
  30. term.clear()
  31. activeGUI = nil
  32. db = {}
  33. entryString = ""
  34. dbAltNameCache = {}
  35. retrievalQueue = {}
  36. dbFile = "retDB"
  37.  
  38. chestValues = {colors.white,colors.orange,colors.magenta,colors.yellow,colors.lime}
  39. motors = { down = colors.gray, up = colors.pink }
  40. signals = { retriever = colors.lightGray, filter = colors.cyan, itemDetector = colors.purple}
  41.  
  42.  
  43. function loadDB()
  44.     db = {}
  45.     local file = fs.open(dbFile,"r")
  46.     db = textutils.unserialize(file.readAll())
  47.     file.close()
  48. end
  49.  
  50. function buildCache()
  51.     entryString = ""
  52.     dbAltNameCache = {}
  53.     for k,v in pairs(db) do
  54.         for i,n in pairs(v.altNames) do
  55.             dbAltNameCache[string.lower(n)] = k
  56.             entryString = entryString ..";"..string.lower(n)
  57.         end
  58.         dbAltNameCache[string.lower(k)] = k
  59.         entryString = entryString ..";"..string.lower(k)
  60.     end
  61. end
  62.  
  63. loadDB()
  64. buildCache()
  65.  
  66. function findEntries(str)
  67.     if str == "" then return {} end
  68.     local pat = "([^;]*"..string.lower(str).."[^;]*)"
  69.     local m = string.gmatch(entryString,pat)
  70.     local r = m()
  71.     local results = {}
  72.     while r do
  73.         if dbAltNameCache[r] == nil then error("No table index #"..r.."#") end
  74.         results[dbAltNameCache[r]] = true
  75.         r = m()
  76.     end
  77.     local realResults = {}
  78.     for k,v in pairs(results) do table.insert(realResults,k) end
  79.     return realResults
  80. end
  81.  
  82. guis = {}
  83. dofile(shell.resolve("gui_main"))
  84.  
  85. activeGUI = guis["main"]
  86.  
  87. function initGUI()
  88.     if type(activeGUI.init) == "function" then activeGUI:init() end
  89. end
  90.  
  91. function addRetrievalRequest(itemName,itemCount)
  92.     table.insert(retrievalQueue,{name=itemName,count=itemCount,dbData = db[itemName]})
  93.     if #retrievalQueue == 1 then os.queueEvent("retrieval_start") end
  94.     os.queueEvent("retrieval_update")
  95. end
  96.  
  97. initGUI()
  98.  
  99. function guiLoop()
  100.     while false do
  101.         local p = read()
  102.         if tonumber(p) ~= nil then
  103.             local pc = tonumber(p)
  104.             syncChests(pc)
  105.             sleep(2)
  106.         end
  107.     end
  108.     while true do
  109.         local t = {os.pullEvent()}
  110.         local cmd = table.remove(t,1)
  111.         if type(activeGUI.events[cmd]) == "function" then
  112.             activeGUI.events[cmd](activeGUI,unpack(t))
  113.         end
  114.     end
  115. end
  116.  
  117.  
  118.  
  119. function getActiveChestNum()
  120.     for k,v in pairs(chestValues) do
  121.         if rs.testBundledInput("bottom",v) then return k end
  122.     end
  123.     systemError("No active chest found. Likely some frames behind the wall messed up or a piece of cable broke.")
  124. end
  125.  
  126. function pulseMotor(col)
  127.     rs.setBundledOutput("bottom",col)
  128.     sleep(1)
  129.     rs.setBundledOutput("bottom",0)
  130.     sleep(0.3)
  131. end
  132.  
  133. function syncChests(chestNum)
  134.     local actChest = getActiveChestNum()
  135.     local mod = chestNum - actChest
  136.     local dir = "up"
  137.     if mod < 0 then dir = "down" end
  138.     local dangle = 0
  139.     while getActiveChestNum() ~= chestNum do
  140.         pulseMotor(motors[dir])
  141.         dangle = dangle + 1
  142.         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
  143.     end
  144. end
  145.  
  146. function processRetrievalStep(actRet)
  147.     syncChests(actRet.dbData.chest)
  148.     sleep(0.1)    
  149.  
  150.     local maxStackSize = actRet.dbData.chestStackSize
  151.     local stacks = math.floor(actRet.count/maxStackSize)
  152.     local items = actRet.count - (stacks*maxStackSize)    
  153.  
  154.     if items > 0 then
  155.         rs.setBundledOutput("back",items)
  156.         sleep(0.3)
  157.         rs.setBundledOutput("back",0)
  158.         sleep(0.3)
  159.         rs.setBundledOutput("back",actRet.dbData.slot)
  160.         sleep(0.3)
  161.         rs.setBundledOutput("back",0)
  162.         sleep(4)
  163.  
  164.         rs.setBundledOutput("bottom",signals.retriever)
  165.         sleep(0.3)
  166.         rs.setBundledOutput("bottom",0)
  167.     end
  168.  
  169.     if stacks > 0 then
  170.         local addRequest = maxStackSize - items
  171.  
  172.         if addRequest > 0 then
  173.            rs.setBundledOutput("back",addRequest)
  174.            sleep(0.3)
  175.            rs.setBundledOutput("back",0)
  176.             sleep(0.3)
  177.             rs.setBundledOutput("back",actRet.dbData.slot)
  178.             sleep(0.3)
  179.            rs.setBundledOutput("back",0)
  180.            sleep(4)
  181.         end
  182.  
  183.         for i=1,stacks,1 do
  184.             rs.setBundledOutput("bottom",signals.retriever)
  185.             sleep(0.3)
  186.             rs.setBundledOutput("bottom",0)
  187.             sleep(0.2)
  188.         end
  189.     end
  190.  
  191.     actRet.count = 0
  192.  
  193.     rs.setBundledOutput("bottom",signals.filter)
  194.     sleep(0.3)
  195.     rs.setBundledOutput("bottom",0)
  196.  
  197.     waitDetector()
  198.  
  199.     table.remove(retrievalQueue,1)
  200. end
  201.  
  202. function waitDetector()
  203.     while true do
  204.         local ev = os.pullEvent("redstone")
  205.         if rs.testBundledInput("bottom",signals.itemDetector) then break end
  206.     end
  207.     sleep(0.1)
  208. end
  209.  
  210. function retrievalLoop()
  211.     while true do
  212.         if #retrievalQueue == 0 then
  213.             local t = os.pullEvent("retrieval_start")
  214.         end
  215.  
  216.         local actRet = retrievalQueue[1]
  217.         while actRet.count > 0 do
  218.             processRetrievalStep(actRet)
  219.             os.queueEvent("retrieval_update")
  220.             sleep(0.5)
  221.         end
  222.  
  223.         sleep(0.2)
  224.  
  225.  
  226.     end
  227. end
  228.  
  229. parallel.waitForAny(guiLoop,retrievalLoop)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement