Gabriel Moreira Minossi 4c11905281 Capitalizing or encapsulating global variables
Making the linter happy
2021-01-06 13:03:57 -03:00

158 lines
5.1 KiB
Lua

Component = require("component")
Event = require("event")
Term = require("term")
local assemblyUtil = {}
local function addEntries(file, prompt, amount, type)
local a, b, c
for i = 1, amount, 1 do
Term.write(prompt .. " " .. i .. " ")
a, b, c = Event.pull()
while a ~= "component_added" do
a, b, c = Event.pull()
os.sleep()
end
file:write(type .. i .. "," .. b .. "\n")
Term.write(b .. "\n")
end
end
local function addAuxilary(file, proxy, type)
if proxy == nil then
Term.write("Cant find a valid " .. type .. "! Exiting...\n")
os.exit()
else
file:write(type .. "," .. proxy.address .. "\n")
end
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
local function proxy(address)
local machineAddress = Component.get(address)
if (machineAddress ~= nil) then
return Component.proxy(machineAddress)
else
return nil
end
end
local function configureClient()
local file = io.open("addresses", "w")
addEntries(file, "Add item interface", 15, "input")
addEntries(file, "Add fluid interface", 4, "fluid")
addEntries(file, "Add item transposer", 15, "inputTransposer")
addEntries(file, "Add fluid transposer", 4, "fluidTransposer")
addAuxilary(file, Component.me_interface, "items")
addAuxilary(file, Component.database, "database")
addAuxilary(file, Component.gt_machine, "controller")
end
function assemblyUtil.buildClient()
Term.write("Starting Assembly Line initalization...")
local assemblyStructure = {}
local file = io.open("addresses", "r")
if file == nil then
Term.write(" no address configuration found, configuring:\n")
configureClient()
file = io.lines("addresses")
else
file = io.lines("addresses")
end
for line in file do
Term.write(".")
local tokens = split(line, ",")
assemblyStructure[tokens[1]] = proxy(tokens[2])
end
Term.write("\n")
return assemblyStructure
end
local function voltageToTier(voltage)
local maxTier = 15
local tier = maxTier
voltage = voltage - 1
local tierVoltage = 32 * math.pow(4, tier - 1)
while voltage % tierVoltage == voltage do
tier = tier - 1
tierVoltage = 32 * math.pow(4, tier - 1)
end
return tier + 1
end
--[[
function copyPattern(interface, slot, recipe, database)
for i = 1, recipe.inputs, 1 do
local item = recipe["input" .. i]
local name = item.name
if dictionary[name] ~= nil then
name = dictionary[name]
end
interface.setInterfacePatternInput(slot, database, databaseMap[name], item.amount, i)
end
end
--]]
local function getControllerTier(assemblyData)
local controller = assemblyData["controller"]
return voltageToTier(
math.floor(
string.gsub(
string.sub(
controller.getSensorInformation()[4],
1,
string.find(controller.getSensorInformation()[4], "/") - 1
),
"([^0-9]+)",
""
) + 0
)
)
end
local function addRecipe(recipelist, slot, source, sourceSide)
if source.getStackInSlot(sourceSide, slot) ~= nil then
local pattern = source.getStackInSlot(sourceSide, slot)
recipelist[pattern.output] = {}
recipelist[pattern.output]["label"] = pattern.output
recipelist[pattern.output]["time"] = pattern.time
recipelist[pattern.output]["inputs"] = 0
recipelist[pattern.output]["fluids"] = 0
recipelist[pattern.output]["tier"] = voltageToTier(pattern.eu)
if pattern.inputItems ~= nil then
local items = pattern.inputItems
for i = 1, #items, 1 do
recipelist[pattern.output]["input" .. i] = {name = items[i][1], amount = items[i][2]}
recipelist[pattern.output]["inputs"] = recipelist[pattern.output]["inputs"] + 1
end
end
if pattern.inputFluids ~= nil then
local fluids = pattern.inputFluids
for i = 1, #fluids do
recipelist[pattern.output]["fluid" .. i] = {name = fluids[i][1], amount = fluids[i][2]}
recipelist[pattern.output]["fluids"] = recipelist[pattern.output]["fluids"] + 1
end
end
end
end
function assemblyUtil.getRecipes(recipelist)
for address, type in pairs(Component.list()) do
if type == "transposer" then
local dataSource = proxy(address)
for side = 0, 5 do
if dataSource.getInventorySize(side) ~= nil then
local slots = dataSource.getInventorySize(side)
for slot = 1, slots, 1 do
addRecipe(recipelist, slot, dataSource, side)
end
end
end
end
end
end
return assemblyUtil