Advertisement
hbro

AE2AutoStock_FixedTabs

Jun 29th, 2020
1,556
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.91 KB | None | 0 0
  1. local component = require("component")
  2. local fs = require("filesystem")
  3. local serialization = require("serialization")
  4. local meController = component.proxy(component.me_controller.address)
  5. local gpu = component.gpu
  6. gpu.setResolution(160,50)
  7. local gui = require("gui")
  8. local event = require("event")
  9.  
  10. gui.checkVersion(2,5)
  11.  
  12. local prgName = "Applied Energistics 2 Auto Stock"
  13. local version = "v1.3"
  14. local lines = {}
  15. local items = {}
  16. local craftTasks = {}
  17. local maxCpuUsage = 1
  18. local currentCpuUsage = 0
  19.  
  20. local function LoadConfig()
  21.   local file,err = io.open("config.cfg", "r")
  22.   if err == nil then
  23.     local data = file:read("*n")
  24.     maxCpuUsage = tonumber(data)
  25.     gui.setText(mainGui, CpuMaxUsage, maxCpuUsage .. "")
  26.     file:close()
  27.   end
  28. end
  29.  
  30. local function SaveConfig()
  31.   local file,err = io.open("config.cfg", "w")
  32.   file:write(maxCpuUsage)
  33.   file:close()
  34. end
  35.  
  36. local function LoadItems()
  37.   local file,err = io.open("items.cfg", "r")
  38.   if err == nil then
  39.     local data = file:read("*a")
  40.     file:close()
  41.  
  42.     local itemsToLoad = serialization.unserialize(data)
  43.     items = {}
  44.     for index = 1, #itemsToLoad do
  45.       items[index] = itemsToLoad[index]
  46.     end
  47.  
  48.     for index = 1, #items do
  49.       items[index]["Name"] = ""
  50.       items[index]["CurrentCraftAmount"] = 0
  51.       items[index]["CurrentValue"] = 0
  52.       items[index]["Message"] = ""
  53.     end
  54.   end
  55. end
  56.  
  57. local function SaveItems()
  58.   local file,err = io.open("items.cfg", "w")
  59.   local itemsToSave = {}
  60.   for index = 1, #items do
  61.     itemsToSave[index] = {}
  62.     itemsToSave[index]["rawItemName"] = items[index]["rawItemName"]
  63.     itemsToSave[index]["rawItemDamage"] = items[index]["rawItemDamage"]
  64.     itemsToSave[index]["Setpoint"] = items[index]["Setpoint"]
  65.     itemsToSave[index]["CraftAmount"] = items[index]["CraftAmount"]
  66.   end
  67.   file:write(serialization.serialize(itemsToSave))
  68.   file:close()
  69.  
  70.   LoadItems()
  71. end
  72.  
  73. mainGui = gui.newGui(1, 2, 159, 48, true)
  74.  
  75. local function DrawHeaders()
  76.   Header_Name = gui.newLabel(mainGui, 4, 2, "Name", 0xc0c0c0, 0x0, 30)
  77.   Header_Current = gui.newLabel(mainGui, 35, 2, "Current (Setpoint)", 0xc0c0c0, 0x0, 18)
  78.   Header_Crafting = gui.newLabel(mainGui, 54, 2, "Crafting", 0xc0c0c0, 0x0, 8)
  79.   Header_Message = gui.newLabel(mainGui, 63, 2, "Message", 0xc0c0c0, 0x0, 15)
  80.   Header_Line = gui.newHLine(mainGui, 1, 3, 76)
  81.   Header2_Name = gui.newLabel(mainGui, 84, 2, "Name", 0xc0c0c0, 0x0, 30)
  82.   Header2_Current = gui.newLabel(mainGui, 115, 2, "Current (Setpoint)", 0xc0c0c0, 0x0, 18)
  83.   Header2_Crafting = gui.newLabel(mainGui, 134, 2, "Crafting", 0xc0c0c0, 0x0, 8)
  84.   Header2_Message = gui.newLabel(mainGui, 143, 2, "Message", 0xc0c0c0, 0x0, 15)
  85.   Header2_Line = gui.newHLine(mainGui, 81, 3, 76)
  86. end
  87.  
  88. local function DrawLines()
  89.   local rowCount = 1
  90.   for index = 1, 86 do
  91.     if index % 2 == 1 then
  92.       lines[index] = {}
  93.       lines[index]["Radio"] = gui.newRadio(mainGui, 1, 3 + rowCount)
  94.       lines[index]["Name"] = gui.newLabel(mainGui, 4, 3 + rowCount, "", 0xc0c0c0, 0x0, 30)
  95.       lines[index]["Current"] = gui.newLabel(mainGui, 35, 3 + rowCount, "", 0xc0c0c0, 0x0, 18)
  96.       lines[index]["Crafting"] = gui.newLabel(mainGui, 54, 3 + rowCount, "", 0xc0c0c0, 0x0, 8)
  97.       lines[index]["Message"] = gui.newLabel(mainGui, 63, 3 + rowCount, "", 0xc0c0c0, 0x0, 15)
  98.     else
  99.       lines[index] = {}
  100.       lines[index]["Radio"] = gui.newRadio(mainGui, 81, 3 + rowCount)
  101.       lines[index]["Name"] = gui.newLabel(mainGui, 84, 3 + rowCount, "", 0xc0c0c0, 0x0, 30)
  102.       lines[index]["Current"] = gui.newLabel(mainGui, 115, 3 + rowCount, "", 0xc0c0c0, 0x0, 18)
  103.       lines[index]["Crafting"] = gui.newLabel(mainGui, 134, 3 + rowCount, "", 0xc0c0c0, 0x0, 8)
  104.       lines[index]["Message"] = gui.newLabel(mainGui, 143, 3 + rowCount, "", 0xc0c0c0, 0x0, 15)
  105.       rowCount = rowCount + 1
  106.     end
  107.   end
  108.  
  109.   for index = 1, 86 do
  110.     gui.setVisible(mainGui, lines[index]["Radio"], false, true)
  111.   end
  112. end
  113.  
  114. local function EmptyLines()
  115.   for index = 1, 86 do
  116.     gui.setVisible(mainGui, lines[index]["Radio"], false, true)
  117.     gui.setText(mainGui, lines[index]["Name"], "")
  118.     gui.setText(mainGui, lines[index]["Current"], "")
  119.     gui.setText(mainGui, lines[index]["Crafting"], "")
  120.     gui.setText(mainGui, lines[index]["Message"], "")
  121.   end
  122. end
  123.  
  124. local function FillLines()
  125.   for index = 1, #items do
  126.     gui.setVisible(mainGui, lines[index]["Radio"], true, true)
  127.     gui.setEnable(mainGui, lines[index]["Radio"], true, true)
  128.     gui.setText(mainGui, lines[index]["Name"], items[index]["Name"])
  129.     gui.setText(mainGui, lines[index]["Current"], items[index]["CurrentValue"] .. " (" .. items[index]["Setpoint"] .. ")")
  130.     if items[index]["CurrentCraftAmount"] > 0 then
  131.       gui.setText(mainGui, lines[index]["Crafting"], items[index]["CurrentCraftAmount"] .. "")
  132.     else
  133.       gui.setText(mainGui, lines[index]["Crafting"], "")
  134.     end
  135.     gui.setText(mainGui, lines[index]["Message"], items[index]["Message"])
  136.   end
  137. end
  138.  
  139. local addGui_Open
  140. local changeGui_Open
  141. local addItem = {}
  142. local changeItemIndex
  143.  
  144. local function Item_Name_Callback(guiID, textID, text)
  145.    addItem["Name"] = text
  146. end
  147.  
  148. local function Setpoint_Callback(guiID, textID, text)
  149.    addItem["Setpoint"] = tonumber(text)
  150. end
  151.  
  152. local function ItemDamage_Callback(guiID, textID, text)
  153.    addItem["Damage"] = tonumber(text)
  154. end
  155.  
  156. local function CraftAmount_Callback(guiID, textID, text)
  157.    addItem["CraftAmount"] = tonumber(text)
  158. end
  159.  
  160. local function addButtonCallback(guiID, id)
  161.   index = #items + 1
  162.   if index <= 86 then
  163.     items[index] = {}
  164.     items[index]["rawItemName"] = addItem["Name"]
  165.     items[index]["rawItemDamage"] = addItem["Damage"]
  166.     items[index]["Setpoint"] = addItem["Setpoint"]
  167.     items[index]["CraftAmount"] = addItem["CraftAmount"]
  168.  
  169.     SaveItems()
  170.  
  171.     addGui_Open = false
  172.   else
  173.     addGui_Open = false
  174.     gui.showMsg("Maximum number of items reached (86 items).")
  175.   end
  176. end
  177.  
  178. local function changeButtonCallback(guiID, id)
  179.   index = changeItemIndex
  180.   items[index] = {}
  181.   items[index]["rawItemName"] = addItem["Name"]
  182.   items[index]["rawItemDamage"] = addItem["Damage"]
  183.   items[index]["Setpoint"] = addItem["Setpoint"]
  184.   items[index]["CraftAmount"] = addItem["CraftAmount"]
  185.  
  186.   SaveItems()
  187.  
  188.   changeGui_Open = false
  189. end
  190.  
  191. local function exitButtonCallback(guiID, id)
  192.   addGui_Open = false
  193.   changeGui_Open = false
  194. end
  195.  
  196. local function AddItem_Callback(guiID, buttonID)
  197.   local addGui = gui.newGui("center", "center", 62, 10, true, "Add Item")
  198.   Item_Name_Label = gui.newLabel(addGui, 1, 1, "   Item Name: ", 0xc0c0c0, 0x0, 7)
  199.   Item_Name = gui.newText(addGui, 15, 1, 30, "", Item_Name_Callback, 30, false)
  200.   Item_Damage_Label = gui.newLabel(addGui, 1, 3, " Item Damage: ", 0xc0c0c0, 0x0, 7)
  201.   Item_Damage = gui.newText(addGui, 15, 3, 8, "", ItemDamage_Callback, 8, false)
  202.   Item_Damage_Help = gui.newLabel(addGui, 24, 3, "(Metadata number of item)", 0xc0c0c0, 0x0, 7)
  203.   Setpoint_Label = gui.newLabel(addGui, 1, 5, "    Setpoint: ", 0xc0c0c0, 0x0, 7)
  204.   Setpoint = gui.newText(addGui, 15, 5, 8, "", Setpoint_Callback, 8, false)
  205.   Setpoint_Help = gui.newLabel(addGui, 24, 5, "(How many items to keep in stock)", 0xc0c0c0, 0x0, 7)  
  206.   CraftAmount_Label = gui.newLabel(addGui, 1, 7, "Craft Amount: ", 0xc0c0c0, 0x0, 7)
  207.   CraftAmount = gui.newText(addGui, 15, 7, 8, "", CraftAmount_Callback, 8, false)
  208.   CraftAmount_Help = gui.newLabel(addGui, 24, 7, "(How many items to craft max at once)", 0xc0c0c0, 0x0, 7)
  209.   addButton = gui.newButton(addGui, 41, 9, "Add Item", addButtonCallback)
  210.   exitButton = gui.newButton(addGui, 52, 9, "Cancel", exitButtonCallback)
  211.  
  212.   addGui_Open = true
  213.   addItem = {}
  214.  
  215.   gui.displayGui(addGui)
  216.   while addGui_Open do
  217.     gui.runGui(addGui)
  218.   end
  219.   gui.closeGui(addGui)
  220. end
  221.  
  222. local function RemoveItem_Callback(guiID, buttonID)
  223.    local radioIndex = gui.getRadio(guiID)
  224.    local removeIndex
  225.  
  226.    for index = 1, #lines do
  227.     if lines[index]["Radio"] == radioIndex then
  228.       removeIndex = index
  229.     end
  230.    end
  231.    
  232.    table.remove(items, removeIndex)
  233.    saveItems()
  234.    EmptyLines()
  235. end
  236.  
  237. local function ChangeItem_Callback(guiID, buttonID)
  238.   local radioIndex = gui.getRadio(guiID)
  239.   if radioIndex > 0 then
  240.     for index = 1, #lines do
  241.       if lines[index]["Radio"] == radioIndex then
  242.         changeItemIndex = index
  243.       end
  244.     end
  245.  
  246.     local changeGui = gui.newGui("center", "center", 62, 10, true, "Change Item")
  247.     Item_Name_Label = gui.newLabel(changeGui, 1, 1, "   Item Name: ", 0xc0c0c0, 0x0, 7)
  248.     Item_Name = gui.newText(changeGui, 15, 1, 30, items[changeItemIndex]["rawItemName"], Item_Name_Callback, 30, false)
  249.     Item_Damage_Label = gui.newLabel(changeGui, 1, 3, " Item Damage: ", 0xc0c0c0, 0x0, 7)
  250.     Item_Damage = gui.newText(changeGui, 15, 3, 8, items[changeItemIndex]["rawItemDamage"], ItemDamage_Callback, 8, false)
  251.     Item_Damage_Help = gui.newLabel(changeGui, 24, 3, "(Metadata number of item)", 0xc0c0c0, 0x0, 7)
  252.     Setpoint_Label = gui.newLabel(changeGui, 1, 5, "    Setpoint: ", 0xc0c0c0, 0x0, 7)
  253.     Setpoint = gui.newText(changeGui, 15, 5, 8, items[changeItemIndex]["Setpoint"], Setpoint_Callback, 8, false)
  254.     Setpoint_Help = gui.newLabel(changeGui, 24, 5, "(How many items to keep in stock)", 0xc0c0c0, 0x0, 7)  
  255.     CraftAmount_Label = gui.newLabel(changeGui, 1, 7, "Craft Amount: ", 0xc0c0c0, 0x0, 7)
  256.     CraftAmount = gui.newText(changeGui, 15, 7, 8, items[changeItemIndex]["CraftAmount"], CraftAmount_Callback, 8, false)
  257.     CraftAmount_Help = gui.newLabel(changeGui, 24, 7, "(How many items to craft max at once)", 0xc0c0c0, 0x0, 7)
  258.     changeButton = gui.newButton(changeGui, 38, 9, "Change Item", changeButtonCallback)
  259.     exitButton = gui.newButton(changeGui, 52, 9, "Cancel", exitButtonCallback)
  260.  
  261.     changeGui_Open = true
  262.     addItem = {}
  263.     addItem["Name"] = items[changeItemIndex]["rawItemName"]
  264.     addItem["Damage"] = items[changeItemIndex]["rawItemDamage"]
  265.     addItem["Setpoint"] = items[changeItemIndex]["Setpoint"]
  266.     addItem["CraftAmount"] = items[changeItemIndex]["CraftAmount"]
  267.  
  268.     gui.displayGui(changeGui)
  269.     while changeGui_Open do
  270.       gui.runGui(changeGui)
  271.     end
  272.     gui.closeGui(changeGui)
  273.   end
  274. end
  275.  
  276. local function CpuMaxUsage_Callback(guiID, textID, text)
  277.   maxCpuUsage = tonumber(text)
  278.   SaveConfig()
  279. end
  280.  
  281. local function DrawButtons()
  282.   AddButton = gui.newButton(mainGui, 1, 1, "Add Item", AddItem_Callback)
  283.   RemoveButton = gui.newButton(mainGui, 12, 1, "Remove Item", RemoveItem_Callback)
  284.   ChangeButton = gui.newButton(mainGui, 26, 1, "Change Item", ChangeItem_Callback)
  285.   CpuUsageLabel = gui.newLabel(mainGui, 118, 1, "CPU usage: ", 0xc0c0c0, 0x0, 13)
  286.   CpuMaxUsageLabel = gui.newLabel(mainGui, 134, 1, "Max CPU usage: ", 0xc0c0c0, 0x0, 15)
  287.   CpuMaxUsage = gui.newText(mainGui, 149, 1, 4, maxCpuUsage .. "", CpuMaxUsage_Callback, 4, false)
  288. end
  289.  
  290. function CheckItemsAndCraft()
  291.   for index = 1, #items do
  292.     items[index]["Message"] = ""
  293.     items[index]["CurrentValue"] = 0
  294.     items[index]["Name"] = ""
  295.    
  296.     local meItem = meController.getItemsInNetwork({ name = items[index]["rawItemName"], damage = items[index]["rawItemDamage"]})
  297.     if meItem.n >= 1 then
  298.       if not meItem[1].isCraftable then
  299.         items[index]["Message"] = "Not Craftable"
  300.       end
  301.  
  302.       items[index]["CurrentValue"] = meItem[1].size
  303.       items[index]["Name"] = meItem[1].label
  304.  
  305.       indexCraftTask = 1
  306.       for indexCraftTasks = 1, #craftTasks do
  307.         if craftTasks[indexCraftTasks].Id == index then indexCraftTask = indexCraftTasks end
  308.       end
  309.  
  310.       if craftTasks[indexCraftTask].task ~= nil and indexCraftTask > 1 then
  311.         if craftTasks[indexCraftTask].task.isDone() or craftTasks[indexCraftTask].task.isCanceled() then
  312.           currentCpuUsage = currentCpuUsage - 1
  313.           items[index]["CurrentCraftAmount"] = 0
  314.           table.remove(craftTasks, indexCraftTask)
  315.         end
  316.       else
  317.         if items[index]["CurrentCraftAmount"] == 0 and items[index]["CurrentValue"] < items[index]["Setpoint"] then
  318.           if currentCpuUsage < maxCpuUsage then
  319.             local meCpus = meController.getCpus()
  320.             local occupiedCpus = 0
  321.             for cpuIndex = 1, #meCpus do
  322.               if meCpus[cpuIndex].busy then occupiedCpus = occupiedCpus + 1 end
  323.             end
  324.          
  325.             if occupiedCpus < #meCpus then
  326.               local currentCraftAmount = items[index]["Setpoint"] - items[index]["CurrentValue"]
  327.               if currentCraftAmount > items[index]["CraftAmount"] then
  328.                 currentCraftAmount = items[index]["CraftAmount"]
  329.               end
  330.  
  331.               local craftables = meController.getCraftables({ name = items[index]["rawItemName"], damage = items[index]["rawItemDamage"]})
  332.               if craftables.n >= 1 then
  333.                 craftTask = craftables[1].request(currentCraftAmount)
  334.  
  335.                 if craftTask.isCanceled() then
  336.                   items[index]["Message"] = "No ingredients"
  337.                 else
  338.                   items[index]["CurrentCraftAmount"] = currentCraftAmount
  339.                   craftTaskWithId = { Id = index, task = craftTask }
  340.                   newIndex = #craftTasks + 1
  341.                   craftTasks[newIndex] = craftTaskWithId
  342.                   currentCpuUsage = currentCpuUsage + 1
  343.                 end
  344.               end
  345.             else
  346.               items[index]["Message"] = "All CPUs busy"
  347.             end
  348.           else
  349.             items[index]["Message"] = "All CPUs busy"
  350.           end
  351.         end
  352.       end
  353.     end
  354.   end
  355.  
  356.   gui.setText(mainGui, CpuUsageLabel, "CPU Usage: " .. currentCpuUsage)
  357. end
  358.  
  359. DrawHeaders()
  360. DrawLines()
  361. DrawButtons()
  362. LoadConfig()
  363. LoadItems()
  364.  
  365. gui.clearScreen()
  366. gui.setTop("Applied Energistics 2 Auto Stock")
  367. gui.setBottom("")
  368.  
  369. -- Create Empty craftTask
  370. craftTasks[1] = { Id = 0, task = "" }
  371.  
  372. -- Main loop
  373. local tickCount = 0
  374. while true do
  375.    gui.runGui(mainGui)
  376.    if tickCount <= 0 then
  377.      CheckItemsAndCraft()
  378.      tickCount = 20
  379.    else
  380.      tickCount = tickCount - 1
  381.    end
  382.    FillLines()
  383. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement