nomnuggetnom

Controller

Sep 8th, 2013
432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 23.40 KB | None | 0 0
  1. --local modem = peripheral.wrap("right")    modem.open(1)
  2. local terminalBridge = peripheral.wrap("back") terminalBridge.clear()
  3. local sysBridge = peripheral.wrap("right")
  4. local controller = peripheral.wrap("bottom")
  5. local modem = peripheral.wrap("top") for i=1, 51 do modem.open(i) end
  6.  
  7. -- AE Storage
  8. calcUsage = (controller.getUsedBytes()) / (controller.getTotalBytes())
  9. calcDisplay = math.floor(calcUsage * 75)
  10. calcPercent = math.floor(calcUsage * 100)
  11.  
  12. -- addBox(x,y,width,height,hexcolor,opacity)
  13. -- addText(x,y,text,color)
  14. function displayAE()
  15.     terminalBridge.clear()
  16.     local meter = terminalBridge.addBox(calcDisplay, 244, 1, 7, 0x000000, 1.0)
  17.     local box = terminalBridge.addBox(5, 245, 75, 5, 0xB3150C, 0.5)
  18.     box.setGradient(2)
  19.     box.setColor2(0x1A991C)
  20.     local meter = terminalBridge.addBox(calcDisplay, 244, 1, 7, 0x000000, 1.0)
  21.     local percentDisplay = terminalBridge.addText(83, 244, "" ..tostring(calcPercent).. "%", 0xFFFFFF)
  22. end
  23.  
  24. -- Converts 1000 into 1,000 for example
  25. function formatNumber(userInput)
  26.     local formatted = userInput
  27.     while true do  
  28.         formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
  29.         if (k==0) then break end
  30.     end
  31.     return formatted
  32.     --print("Function result: " ..tostring(formatted))
  33. end
  34. ---------------------------------------------------
  35. ----------------[[ Item System ]]------------------
  36. ---------------------------------------------------
  37. itemIndex = {}
  38. -- itemIndex[name] = id
  39. idIndex = {}
  40. -- idIndex[id] = name
  41. itemStock = {}
  42. -- itemStock[id] = number
  43.  
  44. function retrieveCrafts()
  45.     canCraft = sysBridge.listCraft()
  46. end
  47.  
  48. function retrieveStorage()
  49.     itemStock = sysBridge.listItems()
  50. end
  51.  
  52. function retrieveIDs()
  53.         fileRead = fs.open("itemIndex", "r")
  54.         lineText = fileRead.readLine()
  55.         while lineText ~= nil do
  56.         _, _, key, value = string.find(lineText, "(%a+)%s*=%s*(%d+)")
  57.         itemIndex[key]= tonumber(value)
  58.         _, _, key, value = string.find(lineText, "(%a+)%s*=%s*(%d+)")
  59.         idIndex[tonumber(value)]=key
  60.         lineText = fileRead.readLine()
  61.         end
  62.         fileRead.close()
  63. end
  64.  
  65. retrieveIDs()
  66.  
  67. function isIndexed(itemID)
  68.     if itemIndex[itemID] == nil then return false
  69.     else return true end
  70. end
  71.  
  72. function craftItem(reqID, reqQty, transfer)
  73.         sysBridge.craft(reqID, reqQty)
  74.         if idIndex[reqID] ~= nil then
  75.             sysStatus = "Crafting.."
  76.             displayText("Crafting " ..tostring(reqQty).. " " ..tostring(idIndex[reqID]).. "...")
  77.         else
  78.             sysStatus = "Crafting.."
  79.             displayText("Crafting " ..tostring(reqQty).. " " ..tostring(reqID).. "...")
  80.         end
  81.         if transfer ~= "n" then
  82.             print("Transfer command")
  83.             transferItem(reqID,reqQty, false)
  84.         end
  85. end
  86.  
  87. function indexID(itemName, itemID)
  88.     if itemIndex[itemName] == nil then
  89.         fileWrite = fs.open("itemIndex", "a")
  90.         fileWrite.writeLine(tostring(itemName).. "=" ..tonumber(itemID))
  91.         displayText("Item '" ..tostring(itemName).. "' indexed (" ..tostring(itemID) .. ").")
  92.         fileWrite.close()
  93.         retrieveIDs()
  94.     elseif itemIndex[itemName] ~= nil and itemID == itemIndex[itemName] then
  95.         displayText("Item already exists.")
  96.     end
  97. end
  98.  
  99. --[[
  100. function removeID(itemName)
  101.     if itemIndex[itemName] ~= nil then
  102.         print("Getting ready to look for the value.")
  103.         fileWrite = fs.open("itemIndex", "w")
  104.         --print("Alright, here we go. Looking for: '"..itemName=itemID.."'.")
  105.         findValue = ""..tostring(itemName).."="..tostring(itemIndex[itemName])
  106.         print(findValue)
  107.         --repeat lineText = fileWrite.readLine() until lineText == findValue
  108.         lineText = fileWrite.readLine()
  109.         --while not lineText == findValue do fileWrite.readLine() end
  110.         while not fileWrite.readLine() == findValue do fileWrite.readLine()
  111.         print("I could remove this: " ..tostring(lineText))
  112.     elseif itemIndex[itemName] == nil then
  113.         print("That item doesn't exist.")
  114.     end
  115.     fileWrite.close()
  116. end
  117. ]]--
  118.  
  119. --[[
  120. function displayText(textInput)
  121.     sysStatus = "dispText"  terminalBridge.clear()  displayAE()
  122.     stringL = math.floor(terminalBridge.getStringWidth(tostring(textInput)))
  123.     boxW = stringL + 10
  124.     -- addBox x, y, w, h, color, opacity
  125.     -- Fade In
  126.     dispLine = terminalBridge.addBox(7, 7, 1, 15, 0x347D0A, 0.0)
  127.     for i = 0, 1, 0.00003 do dispLine.setOpacity(i) end
  128.     dispBox = terminalBridge.addGradientBox(8, 7, boxW, 15, 0x000000, 0.0, 0x000000, 0.0, 2)
  129.     for i = 0, boxW, boxW/30000 do dispBox.setWidth(i) dispBox.setOpacity2((i/dispBox.getWidth())/2.5) end
  130.     dispText = terminalBridge.addText(13, 11, tostring(textInput), 0xFFFFFF)
  131.     dispText.setZIndex(1)
  132.     --sleep(2)
  133.     --print("About to start the timer.")
  134. end
  135. ]]--
  136.  
  137. function fadeIn(textInput)
  138.     sysStatus = "dispText"  terminalBridge.clear()  displayAE()
  139.     stringL = math.floor(terminalBridge.getStringWidth(tostring(textInput)))
  140.     boxW = stringL + 10
  141.     dispLine = terminalBridge.addBox(7, 7, 1, 15, 0x347D0A, 0.0)
  142.     for i = 0, 1, 0.00005 do dispLine.setOpacity(i) end
  143.     --for i = 0, 1, 0.001 do dispLine.setOpacity(i) end
  144.     dispBox = terminalBridge.addGradientBox(8, 7, boxW, 15, 0x000000, 0.0, 0x000000, 0.0, 2)
  145.     for i = 0, boxW, boxW/15000 do dispBox.setWidth(i) dispBox.setOpacity2((i/dispBox.getWidth())/2.5) end
  146.     dispText = terminalBridge.addText(13, 11, tostring(textInput), 0xFFFFFF)
  147.     dispText.setZIndex(1)
  148. end
  149.  
  150. function fadeOut(opacity)
  151.     print("fadeOut")
  152.     dispText.setText("")
  153.     for i = dispBox.getOpacity2(), 0, -0.00006 do dispBox.setOpacity2(i*2) dispLine.setOpacity(i*2) end
  154.     sysStatus = "idle""", 0xFFFFFF)
  155.     dispLine = terminalBridge.addBox(7, 7, 1, 15, 0x347D0A, 0.0)
  156.     if textInput ~= dispText.getText() then --What we're displaying isn't updated.
  157.         if dispLine.getOpacity2() == 0 then --Our display isn't up.
  158.             print("The display isn't up!")
  159.             fadeIn(textInput)
  160.         else --Our display is up, but not with the right text.
  161.             print("The display is up!")
  162.             fadeOut()
  163.             fadeIn(textInput)
  164.         end
  165.     elseif textInput == dispText.getText() then -- Our text is correct.
  166.         print("Our text is correct! Fade.")
  167.     end
  168.     if os.pullEvent("timer") or os.pullEvent("chat_command") then
  169.         fadeOut()
  170.     elseif os.pullEvent("chat_command") then
  171.         fadeOut()
  172.         getInput(p1)
  173.     end
  174.     --if event == "timer" or event == "chat_command" then
  175.     --  fadeOut()
  176.     --end
  177. end
  178.    
  179.     --[[
  180.         sysStatus = "dispText"  terminalBridge.clear()  displayAE()
  181.         stringL = math.floor(terminalBridge.getStringWidth(tostring(textInput)))
  182.         boxW = stringL + 10
  183.         -- addBox x, y, w, h, color, opacity
  184.         -- Fade In
  185.         dispLine = terminalBridge.addBox(7, 7, 1, 15, 0x347D0A, 0.0)
  186.         for i = 0, 1, 0.00003 do dispLine.setOpacity(i) end
  187.         dispBox = terminalBridge.addGradientBox(8, 7, boxW, 15, 0x000000, 0.0, 0x000000, 0.0, 2)
  188.         for i = 0, boxW, boxW/30000 do dispBox.setWidth(i) dispBox.setOpacity2((i/dispBox.getWidth())/2.5) end
  189.         dispText = terminalBridge.addText(13, 11, textInput, 0xFFFFFF)
  190.         dispText.setZIndex(1)
  191.         --sleep(2)
  192.         os.startTimer(2)
  193.         print("fadeOut")
  194.         dispText.setText("")
  195.         for i = 1, 0, -0.00003 do dispBox.setOpacity2(i/2.5) dispLine.setOpacity(i) end
  196. ]]--
  197.  
  198. function transferItem(itemID, itemQty, doCraft)
  199.     retrieveStorage()
  200.     if doCraft then
  201.         if itemStock[itemID] == nil then storedItems = 0 else storedItems = itemStock[itemID] end
  202.         neededItems = itemQty - storedItems
  203.         orderedCraft = false
  204.         if neededItems > 0 then -- We'll need to craft some items.
  205.             if not orderedCraft then
  206.                 sysBridge.craft(itemID, itemQty)
  207.                 orderedCraft = true
  208.                 fadeIn("Crafting "..tostring(neededItems).." of "..tostring(itemQty).." '"..tostring(idIndex[itemID]).."'...")
  209.                 while neededItems > 0 do -- Already ordered crafting. Waiting.
  210.                     retrieveStorage()
  211.                     if itemStock[itemID] == nil then storedItems = 0 else storedItems = itemStock[itemID] end
  212.                     neededItems = itemQty - storedItems
  213.                     print(storedItems)
  214.                     sleep(.5)
  215.                 end
  216.                 -- Once we get here, we have enough items.
  217.                 sysBridge.retrieve(itemID, itemQty, 4)
  218.                 --displayText("Finished crafting, moved "..tostring(itemQty).." '"..tostring(idIndex[itemID]).."'.")
  219.                 fadeOut()
  220.                 fadeIn("Finished crafting, moved "..tostring(itemQty).." '"..tostring(idIndex[itemID]).."'.")
  221.                 sysStatus = "dispText"
  222.                 os.startTimer(2)
  223.                 fadeOut()
  224.             end
  225.         else -- No need to craft, we have enough.
  226.             sysBridge.retrieve(itemID, itemQty, 4)
  227.             fadeIn("Moved "..tostring(itemQty).. " '"..tostring(idIndex[itemID]).."'.")
  228.             sysStatus = "dispText"
  229.             os.startTimer(2)
  230.             local event = os.pullEvent()
  231.             if event == "timer" or event == "chat_command" then fadeOut() end
  232.             --displayText("Moved "..tostring(itemQty).. " '"..tostring(idIndex[itemID]).."'.")
  233.         end
  234.     else
  235.         itemsMoved = sysBridge.retrieve(itemID, 1728, 4)
  236.         displayText("Moved "..tostring(itemsMoved).." '"..tostring(idIndex[itemID]).."'.")
  237.     end
  238. end
  239.  
  240. retrieveIDs()
  241. retrieveCrafts()
  242. retrieveStorage()
  243. -------------
  244. while true do
  245. -------------
  246. local event, p1, p2, p3, p4 = os.pullEvent()
  247. displayAE()
  248.  
  249. --[[ Input Handling ]]--
  250. --[[
  251. local chatEvent, input = os.pullEvent("chat_command")
  252. local userInput = {}
  253. for i in string.gmatch(input, "%S+") do
  254.     table.insert(userInput, i)
  255. end
  256. ]]--
  257.  
  258. function getInput(msg)
  259.     for i in string.gmatch(p1, "%S+") do
  260.         table.insert(userInput, i)
  261.     end
  262. end
  263.  
  264. local userInput = {}
  265. if event == "chat_command" then
  266.     for i in string.gmatch(p1, "%S+") do
  267.         table.insert(userInput, i)
  268.     end
  269. elseif event == "timer" then
  270.     fadeOut()
  271. end
  272.  
  273. --[[
  274. if event == "timer" and sysStatus == "dispText" then
  275.     fadeOut()
  276. end]]--
  277.  
  278.    
  279.    
  280. --[[ Crafting Handling ]]--
  281. if userInput[1] == "craft" or userInput[1] == "make" then
  282.     -- userInput[2] is quantity
  283.     -- userInput[3] is item
  284.     -- userInput[4] is optional, for transferring items
  285.     if not tonumber(userInput[3]) then  -- Received text input
  286.         if itemIndex[userInput[3]] ~= nil then
  287.             craftItem(tonumber(itemIndex[userInput[3]]), tonumber(userInput[2]), userInput[4])
  288.         else
  289.             displayText("Item '" ..tostring(userInput[3]).. "' not recognized.")
  290.         end
  291.     else
  292.         craftItem(tonumber(userInput[3]), tonumber(userInput[2]), userInput[4])
  293.     end
  294.     --[[ Transfer Handling ]]--
  295.  
  296. --[[ Item Transferring ]]
  297. elseif userInput[1] ==  "transfer" or userInput[1] == "move" then
  298.     -- userInput[2] is quantity, or all
  299.     -- userInput[3] is item name
  300.     if not tonumber(userInput[3]) then  -- Received text input
  301.         if itemIndex[userInput[3]] ~= nil then
  302.             if userInput[2] == "all" then
  303.                 transferItem(tonumber(itemIndex[userInput[3]]), tonumber(userInput[2]), false)
  304.             elseif tonumber(userInput[2]) then
  305.                 transferItem(tonumber(itemIndex[userInput[3]]), tonumber(userInput[2]), true)
  306.             end
  307.         else
  308.             displayText("Item '" ..tostring(userInput[3]).. "' not recognized.")
  309.         end
  310.     else
  311.         transferItem(tonumber(userInput[3]), tonumber(userInput[2]), true)
  312.     end
  313.     --[[ Manual Item Indexing ]]--
  314.  
  315. --[[ Item Indexing ]]--
  316. elseif userInput[1] == "index" then
  317.     -- userInput[2] is name
  318.     -- userInput[3] is ID
  319.     -- userInput[4] is metadata
  320.     if tonumber(userInput[2]) then displayText("Wrong format, second input should be the item name.")
  321.     elseif not tonumber(userInput[3]) then displayText("Wrong format, third input should be the item ID.")
  322.     elseif not tonumber(userInput[4]) and userInput[4] ~= nil then displayText("Wrong format, fourth input should be (optional) metadata.")
  323.     elseif userInput[4] == nil then
  324.         indexID(userInput[2],tonumber(userInput[3]))
  325.         retrieveIDs()
  326.     elseif userInput[4] ~= nil then
  327.         indexID(userInput[2], (tonumber(userInput[4]) * 32768)+userInput[3])
  328.         retrieveIDs()
  329.     --else
  330.         --indexID(userInput[2],tonumber(userInput[3]))
  331.         --retrieveIDs()
  332.     end
  333.    
  334. --[[ Index Removal ]]--
  335. elseif userInput[1] == "unindex" or userInput[1] == "remove" then
  336.     -- userInput[2] is the item name
  337.     print("I'll look for the value of "..tostring(userInput[2]))
  338.     removeID(userInput[2])
  339.    
  340. --[[ Storage Checking ]]-- 
  341.     elseif userInput[1] == "check" or userInput[1] == "num" or userInput[1] == "stock" then
  342.     -- userInput[2] is name or ID
  343.     retrieveStorage()
  344.     if not tonumber(userInput[2]) then -- Received text
  345.         if isIndexed(userInput[2]) then
  346.             displayText("Currently " ..tostring(itemStock[itemIndex[userInput[2]]]).. " '" ..tostring(userInput[2]).. "' stored.")
  347.         else
  348.             displayText("Item '" ..tostring(userInput[2]).. "' not recognized.")
  349.         end
  350.     end
  351.  
  352. --[[ Multiplacation ]]--
  353. elseif userInput[1] == "math" then
  354.     local eval, error = loadstring("return " ..userInput[2])
  355.     if error then
  356.         displayText("Error processing '" ..tostring(userInput[2]).. "'.")
  357.     else
  358.         displayText(tostring(formatNumber(eval())))
  359.     end
  360.  
  361. --[[ Display Request ]]--
  362. elseif userInput[1] == "display" then
  363.     for i in string.gmatch(p1, "display(.+)") do
  364.         --fadeIn(i, 15)
  365.         displayText(i)
  366.     end
  367.    
  368. elseif userInput[1] == "img" then
  369.     drawImage(cobble, userInput[3], userInput[4], 1, 1, 0xFFFFFF)
  370.  
  371. --[[ Modem Handling ]]--
  372. elseif userInput[1] == "toggle" or userInput[1] == "t" then
  373.     modem.transmit(50, 50, tostring(userInput[2]))
  374.     local modemMessage, p1, p2, p3, p4 = os.pullEvent("modem_message")
  375.     if p4 then spawnerStatus = "off" else spawnerStatus = "on" end
  376.     displayText("Toggled '"..tostring(userInput[2]).."' to "..tostring(spawnerStatus)..".")
  377.  
  378. --[[ Enderchest Clear ]]--
  379. elseif userInput[1] == "clear" or userInput[1] == "empty" then
  380.     modem.transmit(51, 51, "go")
  381.     displayText("Clearing, please wait for a few seconds...")
  382.     sleep(.5)
  383.     modem.transmit(51, 51, "stop")
  384.     local modemMessage, p1, p2, p3, p4 = os.pullEvent("modem_message")
  385.     if p4 == "cleared" then displayText("Cleared the enderchest.") end
  386.     retrieveStorage()
  387.    
  388. elseif userInput[1] == "format" then
  389.     formatNumber(userInput[2])
  390. end -- End of first if
  391. ---
  392. end
  393. ---
Advertisement
Add Comment
Please, Sign In to add comment