mirror of
https://github.com/S4mpsa/InfOS.git
synced 2025-08-03 18:06:04 -04:00
165 lines
4.7 KiB
Lua
165 lines
4.7 KiB
Lua
Event = require("event")
|
|
Term = require("term")
|
|
local AU = require("util")
|
|
local AT = require("transport")
|
|
local S = require("serialization")
|
|
local uc = require("unicode")
|
|
local fluidMap = (require("dictionary"))
|
|
local network = Component.modem
|
|
local id, AD
|
|
local function requestID()
|
|
network.broadcast(100, "requestID")
|
|
local _, _, _, _, _, messageType, value = Event.pull("modem_message")
|
|
while messageType ~= "sendID" do
|
|
_, _, _, _, _, messageType, value = Event.pull("modem_message")
|
|
os.sleep(0.2)
|
|
end
|
|
return value
|
|
end
|
|
|
|
local function checkRepeat(recipe)
|
|
local correctItems = 0
|
|
for i = 1, recipe.inputs, 1 do
|
|
if AT.isEmpty(AD["inputTransposer" .. i], 16) then
|
|
if AT.check(AD["inputTransposer" .. i], recipe["input" .. i].name, recipe["input" .. i].amount) then
|
|
correctItems = correctItems + 1
|
|
else
|
|
return false
|
|
end
|
|
else
|
|
return false
|
|
end
|
|
end
|
|
for i = 1, recipe.fluids, 1 do
|
|
if AT.isEmpty(AD["fluidTransposer" .. i], 1) then
|
|
if
|
|
AT.check(
|
|
AD["fluidTransposer" .. i],
|
|
fluidMap[recipe["fluid" .. i].name].name,
|
|
recipe["fluid" .. i].amount / fluidMap[recipe["fluid" .. i].name].size
|
|
)
|
|
then
|
|
correctItems = correctItems + 1
|
|
else
|
|
return false
|
|
end
|
|
else
|
|
return false
|
|
end
|
|
end
|
|
if correctItems == recipe.inputs + recipe.fluids then
|
|
return true
|
|
else
|
|
return false
|
|
end
|
|
end
|
|
|
|
local function processRecipe(recipe)
|
|
local database, interface = AD.database, AD.input1
|
|
local function insert()
|
|
for i = 1, recipe.fluids, 1 do
|
|
AT.move(
|
|
AD["fluidTransposer" .. i],
|
|
recipe["fluid" .. i].amount / fluidMap[recipe["fluid" .. i].name].size,
|
|
1
|
|
)
|
|
end
|
|
for i = 1, recipe.inputs, 1 do
|
|
AT.move(AD["inputTransposer" .. i], recipe["input" .. i].amount, 16)
|
|
end
|
|
for i = 1, recipe.fluids, 1 do
|
|
AT.empty(AD["fluidTransposer" .. i])
|
|
end
|
|
end
|
|
for i = 1, recipe.inputs, 1 do
|
|
database.clear(i)
|
|
interface.store({label = recipe["input" .. i].name}, database.address, i, 1)
|
|
end
|
|
for i = 1, recipe.fluids, 1 do
|
|
database.clear(20 + i)
|
|
interface.store({label = fluidMap[recipe["fluid" .. i].name].name}, database.address, 20 + i, 1)
|
|
end
|
|
for i = 1, recipe.inputs, 1 do
|
|
AT.set(AD["input" .. i], database, i, recipe["input" .. i].amount)
|
|
end
|
|
for i = 1, recipe.fluids, 1 do
|
|
AT.set(
|
|
AD["fluid" .. i],
|
|
database,
|
|
20 + i,
|
|
recipe["fluid" .. i].amount / fluidMap[recipe["fluid" .. i].name].size
|
|
)
|
|
end
|
|
::insertRecipes::
|
|
insert()
|
|
while checkRepeat(recipe) do
|
|
insert()
|
|
end
|
|
local wait = Computer.uptime()
|
|
while not AD.controller.hasWork() and Computer.uptime() < wait + 5 do
|
|
os.sleep(0.2)
|
|
end
|
|
if not AD.controller.hasWork() then
|
|
Term.write(" ... Error with starting assembly!")
|
|
network.broadcast(id, "jammed")
|
|
else
|
|
Term.write(" ... Assembly Started")
|
|
while AD.controller.hasWork() do
|
|
os.sleep(0.1)
|
|
end
|
|
if checkRepeat(recipe) then
|
|
goto insertRecipes
|
|
end
|
|
end
|
|
AT.clearAll(AD)
|
|
Term.write(" ... finished task!\n")
|
|
network.broadcast(id, "complete")
|
|
end
|
|
|
|
local function processMessage(localAddress, remoteAddress, port, distance, type, eventType, value2, value3)
|
|
if eventType == "startAssembly" then
|
|
local recipe = S.unserialize(value2)
|
|
Term.write("Starting assembly of " .. recipe.label)
|
|
processRecipe(recipe)
|
|
elseif eventType == "clear" then
|
|
AT.clearAll(AD)
|
|
end
|
|
end
|
|
local processKey = {}
|
|
|
|
local function quit()
|
|
Term.write("Quitting...")
|
|
Event.ignore("modem_message", processMessage)
|
|
Event.ignore("key_up", processKey)
|
|
os.exit()
|
|
end
|
|
|
|
local function processKey(event, address, key, code, player)
|
|
local value = uc.char(key)
|
|
if value == "." then
|
|
quit()
|
|
end
|
|
end
|
|
|
|
local function startAssembly()
|
|
network.open(100)
|
|
local storedID = io.open("ID", "r")
|
|
if storedID == nil then
|
|
id = requestID()
|
|
storedID = io.open("ID", "w")
|
|
storedID:write(id)
|
|
else
|
|
for line in io.lines("ID") do
|
|
id = line + 0
|
|
end
|
|
end
|
|
storedID:close()
|
|
network.open(id)
|
|
AD = AU.buildClient()
|
|
AT.clearAll(AD)
|
|
end
|
|
|
|
startAssembly()
|
|
Event.listen("modem_message", processMessage)
|
|
Event.listen("key_up", processKey)
|