Add breaks to AE2 stock iteration

Add GUI related stuff to graphics.lua
Implement autostocker code
First prototype of fetching files from github
This commit is contained in:
Sampsa 2020-11-03 02:53:42 +02:00
parent c5afc10bff
commit 9f86c28d46
7 changed files with 352 additions and 5 deletions

View File

@ -220,6 +220,7 @@ end
local function refreshDatabase(itemList)
filteredList = {}
for i = 1, #itemList, 1 do
if i % 200 == 0 then os.sleep() end
if itemList[i].size >= 100 then filteredList[itemList[i].label] = itemList[i].size end
itemList[i] = nil
end
@ -289,6 +290,7 @@ function ARWidgets.itemTicker(glasses, x, y, w)
local function getTotalItemCount(items)
local sum = 0
for i = 1, #items, 1 do
if i % 200 == 0 then os.sleep() end
sum = sum + items[i].size
end
return sum

View File

@ -69,10 +69,33 @@ function graphics.border(GPU, w, h, color)
draw.rect(GPU, 1, 1, 1, h*2, color)
draw.rect(GPU, w, 1, 1, h*2, color)
end
function copyWindow(GPU, x, y, page, destination)
currentWindows = {}
function graphics.checkCollision(GPU, x, y)
for window, params in pairs(currentWindows) do
if x >= params.x and x <= params.x+params.w-1 then
if y >= params.y and y <= params.y+math.ceil(params.h/2)-1 then
return window
end
end
end
return nil
end
function graphics.createWindow(GPU, width, height, name)
local pageNumber = GPU.allocateBuffer(width, math.ceil(height/2))
currentWindows[name] = {page=pageNumber, x=1, y=1, w=width, h=height, GPU = GPU}
return pageNumber
end
local function copyWindow(GPU, x, y, page, destination)
destination = 0 or destination
GPU.bitblt(destination, x, math.ceil(y/2), 160, 46, page, 1, 1)
GPU.bitblt(destination, x, y, 160, 50, page, 1, 1)
end
function graphics.refresh(GPU)
for window, params in pairs(currentWindows) do
if params.w > 0 then
copyWindow(GPU, params.x, params.y, params.page)
end
end
GPU.setActiveBuffer(0)
end
windows = {}
function graphics.update()
@ -89,5 +112,8 @@ function graphics.update()
redraw()
os.sleep()
end
function graphics.clear()
currentWindows = {}
end
return graphics

View File

@ -13,7 +13,7 @@ function checkCollision(x, y)
end
end
end
return "None "
return nil
end
local contextMenus = 0

View File

@ -15,7 +15,9 @@ fluidMap = {["fluid.molten.solderingalloy"] = {index = 81, size = 144},
["IC2 Coolant"] = {index = 79, size = 1000},
["fluid.molten.styrenebutadienerubber"] = {index = 78, size = 720},
["fluid.molten.niobiumtitanium"] = {index = 77, size = 144},
["fluid.molten.tritanium"] = {index = 76, size = 144}
["fluid.molten.tritanium"] = {index = 76, size = 144},
["fluid.Neon"] = {index = 75, size = 1000}
}
local function addRecipe(slot, source, sourceSide)
if source.getStackInSlot(sourceSide, slot) ~= nil then

View File

@ -0,0 +1,221 @@
comp=require("component");screen=require("term");computer=require("computer");event=require("event"); thread = require("thread"); sides = require("sides")
get=require("utility"); ARG=require("ARGraphics"); G = require("graphics")
S = require("stockerUtil")
local uc = require("unicode"); tx = require("transforms")
config = require("config")
local GPU = comp.gpu
interface = comp.me_interface
transposer = comp.transposer
itemsToStock = {}
craftables = {}
local currentlyCrafting = {}
local drawerItem
local number = ""
function mouseListener()
function processClick(event, address, x, y, key, player)
local activeWindow = G.checkCollision(nil, x, y)
if activeWindow ~= nil then
if activeWindow == "Button" then
GPU.setActiveBuffer(0)
if drawerItem == nil or number == "" then
G.rect(GPU, currentWindows["Button"].x+2, currentWindows["Button"].y*2+1, 6, 6, negativeEUColour)
else
G.rect(GPU, currentWindows["Button"].x+2, currentWindows["Button"].y*2+1, 6, 6, positiveEUColour)
if itemsToStock[drawerItem] ~= nil then
S.update(drawerItem, itemsToStock[drawerItem], number)
itemsToStock[drawerItem] = number
elseif number+0.0 > 0 then
S.addPattern(drawerItem, number)
itemsToStock[drawerItem] = number
else
itemsToStock[drawerItem] = nil
end
end
number = ""
os.sleep(0.3)
G.refresh(GPU)
elseif activeWindow == "Number" then
GPU.setActiveBuffer(0)
if drawerItem == nil then
G.centeredText(GPU, currentWindows["Number"].x+25, currentWindows["Number"].y*2+3, 0xFFFFFF, "Pattern refresh requested...")
S.refreshCraftables()
end
inNumberBox = true
G.rect(GPU, currentWindows["Number"].x+2, currentWindows["Number"].y*2+1, 46, 6, 0x333333)
else
inNumberBox = false
number = ""
G.refresh(GPU)
end
else
inNumberBox = false
number = ""
G.refresh(GPU)
end
end
return event.listen("touch", processClick)
end
function keyboardListener()
function processKey(event, address, key, code, player)
if inNumberBox then
local value = uc.char(key)
if key == 10 then
inNumberBox = false
G.refresh(GPU)
end
if key == 8 then number = string.sub(number, 1, #number-1) elseif (key >= 48 and key <= 57) then
number = number..value
end
G.rect(GPU, currentWindows["Number"].x+2, currentWindows["Number"].y*2+1, 46, 6, 0x333333)
G.text(GPU, currentWindows["Number"].x+4, currentWindows["Number"].y*2+3, workingColour, number)
end
end
return event.listen("key_down", processKey)
end
function getNewItem(GPU, x, y)
if currentWindows["Item"] == nil then
local itemWindow = G.createWindow(GPU, 60, 6, "Item")
currentWindows["Item"].x = x
currentWindows["Item"].y = y
GPU.setActiveBuffer(itemWindow)
G.rect(GPU, 2, 2, 58, 4, hudColour)
G.rect(GPU, 3, 3, 56, 2, 0x000000)
GPU.setActiveBuffer(0)
end
local newDrawerItem = transposer.getStackInSlot(sides.top, 2)
if newDrawerItem ~= nil then
if craftables[newDrawerItem] ~= nil then
GPU.setForeground(workingColour)
else
GPU.setActiveBuffer(negativeEUColour)
end
if drawerItem == nil then
drawerItem = newDrawerItem.label
GPU.setActiveBuffer(currentWindows["Item"].page)
G.rect(GPU, 3, 3, 56, 2, 0x000000)
if craftables[drawerItem] ~= nil then
G.centeredText(GPU, 30, 3, positiveEUColour, drawerItem)
else
G.centeredText(GPU, 30, 3, negativeEUColour, drawerItem)
end
GPU.setActiveBuffer(0)
if itemsToStock[drawerItem] ~= nil then
G.rect(GPU, currentWindows["Item"].x, currentWindows["Item"].y*2-3, 60, 2, 0x000000)
G.centeredText(GPU, currentWindows["Item"].x+30, currentWindows["Item"].y*2-3, 0xFFFFFF, "Configured: "..itemsToStock[drawerItem])
end
G.refresh(GPU)
else
if drawerItem ~= newDrawerItem.label then
drawerItem = newDrawerItem.label
GPU.setActiveBuffer(currentWindows["Item"].page)
G.rect(GPU, 3, 3, 56, 2, 0x000000)
if craftables[drawerItem] ~= nil then
G.centeredText(GPU, 30, 3, positiveEUColour, drawerItem)
else
G.centeredText(GPU, 30, 3, negativeEUColour, drawerItem)
end
GPU.setActiveBuffer(0)
if itemsToStock[drawerItem] ~= nil then
G.rect(GPU, currentWindows["Item"].x, currentWindows["Item"].y*2-3, 60, 2, 0x000000)
G.centeredText(GPU, currentWindows["Item"].x+30, currentWindows["Item"].y*2-3, 0xFFFFFF, "Configured: "..itemsToStock[drawerItem])
end
G.refresh(GPU)
end
end
else
if drawerItem ~= nil then
drawerItem = nil
GPU.setActiveBuffer(currentWindows["Item"].page)
G.rect(GPU, 3, 3, 56, 2, 0x000000)
G.centeredText(GPU, 30, 3, 0xFFFFFF, "")
GPU.setActiveBuffer(0)
G.rect(GPU, currentWindows["Item"].x, currentWindows["Item"].y*2-3, 60, 2, 0x000000)
G.refresh(GPU)
end
end
end
function numberBox(GPU, x, y)
if currentWindows["Number"] == nil then
local itemWindow = G.createWindow(GPU, 50, 10, "Number")
currentWindows["Number"].x = x
currentWindows["Number"].y = y
GPU.setActiveBuffer(itemWindow)
G.rect(GPU, 2, 2, 48, 8, hudColour)
G.rect(GPU, 3, 3, 46, 6, 0x000000)
GPU.setActiveBuffer(0)
end
end
function button(GPU, x, y)
if currentWindows["Button"] == nil then
local button = G.createWindow(GPU, 10, 10, "Button")
currentWindows["Button"].x = x
currentWindows["Button"].y = y
GPU.setActiveBuffer(button)
G.rect(GPU, 2, 2, 8, 8, hudColour)
G.rect(GPU, 3, 3, 6, 6, workingColour)
GPU.setActiveBuffer(0)
end
end
function craftableBox(GPU, x, y)
if currentWindows["Craft"] == nil then
local crafts = G.createWindow(GPU, 72, 100, "Craft")
currentWindows["Craft"].x = x
currentWindows["Craft"].y = y
GPU.setActiveBuffer(crafts)
G.rect(GPU, 2, 2, 70, 94, hudColour)
GPU.setActiveBuffer(0)
end
GPU.setActiveBuffer(currentWindows["Craft"].page)
G.rect(GPU, 3, 4, 68, 90, 0x000000)
G.rect(GPU, 48, 2, 1, 94, hudColour)
local i = 1
S.updateCache()
for label, amount in pairs(itemsToStock) do
local stockedAmount = S.getAmount(label)
local stockedString = string.sub(stockedAmount.."", 1, #(stockedAmount.."")-2)
local toStock = amount+0.0
if toStock > 0 then
if drawerItem == label then
G.text(GPU, 4, 3+2*i, workingColour, label);
elseif craftables[label] == nil then
G.text(GPU, 4, 3+2*i, negativeEUColour, label);
else
G.text(GPU, 4, 3+2*i, 0xFFFFFF, label);
end
if stockedAmount >= toStock then --In stock
G.text(GPU, 59 - (#stockedString + 1), 3+2*i, 0xFFFFFF, stockedString)
elseif stockedAmount >= toStock * 0.85 then --Edit hysteresis here, slightly below stock
G.text(GPU, 59 - (#stockedString + 1), 3+2*i, workingColour, stockedString)
else --Needs to be ordered
--Add crafting request loop here
if craftables[label] ~= nil then
if currentlyCrafting[label] == nil then
currentlyCrafting[label] = craftables[label](toStock - stockedAmount)
elseif currentlyCrafting[label].isDone() or currentlyCrafting[label].isCanceled() then
currentlyCrafting[label] = nil
end
end
G.text(GPU, 59 - (#stockedString + 1), 3+2*i, negativeEUColour, stockedString)
end
G.text(GPU, 59, 3+2*i, 0xFFFFFF, "| "..amount)
i = math.min(i + 1, 43)
end
end
GPU.setActiveBuffer(0)
G.refresh(GPU)
end
mouseListener(); keyboardListener(); GPU.setResolution(160, 46); screen.clear()
G.clear()
numberBox(GPU, 100, 41); button(GPU, 150, 41); craftableBox(GPU, 0, 0)
G.refresh(GPU)
S.refreshCraftables(); S.loadPatterns()
local timeSinceRefresh = computer.uptime()
while true do
getNewItem(GPU, 100, 38)
if computer.uptime() - timeSinceRefresh > 900 then
timeSinceRefresh = computer.uptime()
craftableBox(GPU, 0, 0)
end
os.sleep(0.5)
end

View File

@ -0,0 +1,77 @@
comp=require("component");screen=require("term");computer=require("computer");event=require("event"); thread = require("thread"); sides = require("sides")
get=require("utility"); ARG=require("ARGraphics"); G = require("graphics")
local uc = require("unicode"); tx = require("transforms")
config = require("config")
local GPU = comp.gpu
local sUtil = {}
function sUtil.refreshCraftables()
local c = comp.me_interface.getCraftables()
local max = 0
if max > 0 then
c = tx.sub(c, 1, max)
end
for i, craftable in pairs(c) do
if i ~= "n" then
if i % 10 == 0 then
G.centeredText(GPU, currentWindows["Number"].x+25, currentWindows["Number"].y*2+3, 0xFFFFFF, "Discovering Patterns: "..i.." / "..#c)
end
end
if craftable ~= #c then
craftables[craftable.getItemStack().label] = craftable.request
end
end
G.centeredText(GPU, 86, 85, 0xFFFFFF, "Patterns in memory: "..#c)
end
local cachedAmounts = {}
function sUtil.updateCache()
local itemList = interface.getItemsInNetwork()
for i = 1, #itemList, 1 do
if i % 200 == 0 then os.sleep() end
cachedAmounts[itemList[i].label] = itemList[i].size
itemList[i] = nil
end
end
function sUtil.getAmount(itemLabel)
if cachedAmounts[itemLabel] == nil then return 0 else return cachedAmounts[itemLabel] end
end
function sUtil.update(label, oldAmount, newAmount)
local file = io.open("configured", 'r')
local fileContent = {}
local lineNumber = 0
local i = 1
for line in file:lines() do
if line == label..","..oldAmount then lineNumber = i end
table.insert (fileContent, line)
i = i + 1
end
io.close(file)
file = io.open("configured", 'w')
for index, value in ipairs(fileContent) do
if index ~= lineNumber then
file:write(value..'\n')
else
file:write(label..","..newAmount..'\n')
end
end
io.close(file)
end
function sUtil.addPattern(label, amount)
local file = io.open("configured", "a")
file:write(label..","..amount.."\n")
file:close()
itemsToStock[label] = amount
end
local function split(s, sep)
local fields = {}; local sep = sep or " "; local pattern = string.format("([^%s]+)", sep)
string.gsub(s, pattern, function(c) fields[#fields + 1] = c end)
return fields
end
function sUtil.loadPatterns()
local file = io.open("configured", "r")
for line in file:lines() do
local tokens = split(line, ",")
itemsToStock[tokens[1]] = tokens[2]
end
end
return sUtil

19
Programs/update.lua Normal file
View File

@ -0,0 +1,19 @@
local path = "/lib"
local shell = require("shell")
local download_list =
{
"https://raw.githubusercontent.com/S4mpsa/InfOS/master/Libraries/ARWidgets.lua",
"https://raw.githubusercontent.com/S4mpsa/InfOS/master/Libraries/config.lua",
"https://raw.githubusercontent.com/S4mpsa/InfOS/master/Libraries/graphics.lua",
"https://raw.githubusercontent.com/S4mpsa/InfOS/master/Libraries/utility.lua",
"https://raw.githubusercontent.com/S4mpsa/InfOS/master/Libraries/widgets.lua",
"https://raw.githubusercontent.com/S4mpsa/InfOS/master/Libraries/ARGraphics.lua"
}
shell.setWorkingDirectory(path)
print("Updating Files")
for k,v in pairs(download_list) do
print("Fetching ",v)
local command = "wget "..v.." -f"
shell.execute(command)
end
shell.setWorkingDirectory("/home")