move everything to lib

This commit is contained in:
Baipyrus 2023-11-03 20:23:46 +01:00
parent dcafc2d1b8
commit adb48bccdf
2 changed files with 84 additions and 78 deletions

View File

@ -6,85 +6,40 @@ local options = {
unit = 'FE'
}
-- set system seed
math.randomseed(os.time())
-- system options/components
local system = {}
-- initialize client library
dofile('inputClient.lua')
-- initialize components
wait_for_components(options, system, 'BiggerReactors_Reactor', 'Bigger Reactor')
-- generate induction matrix values
function generate_message(buffer)
-- read induction matrix values
local a_raw = system.main.active()
local p_raw = system.main.battery().producedLastTick()
local s_raw = system.main.battery().stored()
local m_raw = system.main.battery().capacity()
local f_raw = system.main.fuelTank().fuel()
local c_raw = system.main.fuelTank().capacity()
local ft_raw = system.main.fuelTemperature()
local ct_raw = system.main.casingTemperature()
-- generate print responsee message
function generate_message(width, component, buffer)
-- read bigger values
local a_raw = component.active()
local p_raw = component.battery().producedLastTick()
local s_raw = component.battery().stored()
local m_raw = component.battery().capacity()
local f_raw = component.fuelTank().fuel()
local c_raw = component.fuelTank().capacity()
local ft_raw = component.fuelTemperature()
local ct_raw = component.casingTemperature()
-- format and align reactor status and production
message_aligned(buffer, 'Status...:', a_raw and 'ACTIVE' or 'OFFLINE', system.width)
message_aligned(buffer, 'Producing:', format_energy(options, p_raw, true), system.width)
message_aligned(buffer, 'Status...:', a_raw and 'ACTIVE' or 'OFFLINE', width)
message_aligned(buffer, 'Producing:', format_energy(options, p_raw, true), width)
-- format and align reactor internal battery
message(buffer, '\n')
message_aligned(buffer, 'Stored...:', format_energy(options, s_raw), system.width)
message_aligned(buffer, 'Capacity.:', format_energy(options, m_raw), system.width)
message_aligned(buffer, 'Stored...:', format_energy(options, s_raw), width)
message_aligned(buffer, 'Capacity.:', format_energy(options, m_raw), width)
-- format and align fuel fill
message(buffer, '\n')
message_aligned(buffer, 'Fuel.....:', format_liquid(options, f_raw), system.width)
message_aligned(buffer, 'Capacity.:', format_liquid(options, c_raw), system.width)
message_aligned(buffer, 'Fuel.....:', format_liquid(options, f_raw), width)
message_aligned(buffer, 'Capacity.:', format_liquid(options, c_raw), width)
-- format and align temperatures
message(buffer, '\n')
message_aligned(buffer, 'Fuel Temp:', format_temp(options, ft_raw), system.width)
message_aligned(buffer, 'Case Temp:', format_temp(options, ct_raw), system.width)
message_aligned(buffer, 'Fuel Temp:', format_temp(options, ft_raw), width)
message_aligned(buffer, 'Case Temp:', format_temp(options, ct_raw), width)
end
function send_info()
-- buffer for messages
local buffer = {}
-- generate messages in buffer
generate_message(buffer)
-- transmit messages in form of buffer
transmit_messages(options, system, buffer)
end
function event_listener()
while true do
local _, _, senderChannel, _, message, _ = os.pullEvent('modem_message')
-- respond to corresponding message only
if senderChannel == system.channel and message == 'print' then
-- send message main display message
send_info()
end
end
end
-- main program
function main()
-- permanent runtime
while true do
-- reset terminal
term.clear()
-- run while all components are detected
while scan_components('BiggerReactors_Reactor').success and (not system.timeout) do
event_listener()
end
-- rescan components
wait_for_components(options, system, 'BiggerReactors_Reactor', 'Bigger Reactor')
end
end
main()
-- initialize client library
dofile('inputClient.lua')
run_client(options, {
print = generate_message
})

View File

@ -1,3 +1,10 @@
-- set system seed
math.randomseed(os.time())
-- system options/components
local system = {}
function set_converter(options)
return mekanismEnergyHelper[
string.format('joulesTo%s', options.unit)
@ -127,7 +134,7 @@ function split_string(text, delimiter)
end
-- initialize ender modem
function init_modem(options, system, name)
function init_modem(options)
system.channel = nil
-- open initializer port
system.modem.open(1)
@ -138,7 +145,7 @@ function init_modem(options, system, name)
local channel = math.random(2, 1 + options.channels)
-- open reply channel
system.modem.open(channel)
system.modem.transmit(1, channel, name)
system.modem.transmit(1, channel, options.name)
term.setCursorPos(1, 6)
print('\nTry #' .. count)
@ -177,7 +184,7 @@ function init_modem(options, system, name)
end
-- scan for components and retry on fail
function wait_for_components(options, system, component, name)
function wait_for_components(options)
system.timeout = false
local count = 1
-- fixed print out
@ -192,7 +199,7 @@ function wait_for_components(options, system, component, name)
count = count + 1
-- scan for components
local components = scan_components(component)
local components = scan_components(options.port)
if components.success then
-- output success message
print('Success!')
@ -200,9 +207,9 @@ function wait_for_components(options, system, component, name)
-- set system components
system.main = components.main
system.modem = components.modem
init_modem(options, system, name)
init_modem(options)
print('\nSuccess!')
return
return true
end
-- components not found, wait to retry
@ -224,7 +231,7 @@ function message_aligned(buffer, text, value, width)
end
-- transmit buffered messages over ender modem
function transmit_messages(options, system, buffer)
function transmit_messages(options, buffer)
system.modem.transmit(system.channel, system.channel, table.concat(buffer, '\n'))
local timer_id = os.startTimer(options.timeout)
@ -242,4 +249,48 @@ function transmit_messages(options, system, buffer)
return
end
end
end
function respond_print(options, name)
-- buffer for messages
local buffer = {}
-- generate messages in buffer
execute_funcs(system.width, system.main, buffer, name)
-- transmit messages in form of buffer
transmit_messages(options, buffer)
end
function event_listener()
while true do
local _, _, senderChannel, _, message, _ = os.pullEvent('modem_message')
-- respond to corresponding message only
if senderChannel == system.channel then
-- send main display message
respond_print(message)
end
end
end
function execute_funcs(width, component, buffer, name)
for key, func in ipairs(system.funcs) do
if type(func) == "function" and key == name then
func(width, component, buffer)
end
end
end
function run_client(options, funcs)
-- save response functions
system.funcs = funcs
-- get components and run permanently
while wait_for_components(options) do
-- reset terminal
term.clear()
-- run while all components are detected
while scan_components(options.port).success and (not system.timeout) do
event_listener()
end
end
end