FlipFlop, Counter, Reactor Controller Components

This commit is contained in:
2022-06-29 23:29:20 +01:00
parent e6ac9a8431
commit 8dd238ea38
11 changed files with 887 additions and 89 deletions

11
Lua/Autorun/autorun.lua Normal file
View File

@@ -0,0 +1,11 @@
local moduleNames = {
"flipflopcomponent",
"countercomponent",
"reactorcontrollercomponent",
}
modPath = table.pack(...)[1] .. "/Lua/"
for _, moduleName in ipairs(moduleNames) do
dofile(modPath .. moduleName .. ".lua")
end

36
Lua/countercomponent.lua Normal file
View File

@@ -0,0 +1,36 @@
local allItems = {}
Hook.Add("item.created", "countercomponent.init", function(item)
if item.Prefab.Identifier == "countercomponent" then
allItems[item] = true
end
end)
local signalReceived = function(signal, connection)
local this = connection.Item
local mem = this.Components[2]
if signal.value == "" then return end
local input = tonumber(signal.value) or 1
if input > 0 then
if tonumber(mem.value) >= this.Components[3].clampMax then
mem.value = tostring(this.Components[3].clampMin)
else
mem.value = tostring(tonumber(mem.value) + 1)
end
elseif input == 0 then
mem.value = "0"
elseif input < 0 then
mem.value = tostring(-input)
end
end
local think = function()
for component, _ in pairs(allItems) do
component.SendSignal(component.Components[2].value, "state_out")
end
end
Hook.Add("think", "countercomponent.think", think)
Hook.Add("signalreceived.countercomponent", "countercomponent.signalReceived", signalReceived)

32
Lua/flipflopcomponent.lua Normal file
View File

@@ -0,0 +1,32 @@
local allItems = {}
Hook.Add("item.created", "flipflopcomponent.init", function(item)
if item.Prefab.Identifier == "flipflopcomponent" then
allItems[item] = true
end
end)
local signalReceived = function(signal, connection)
local this = connection.Item
local mem = this.Components[2]
if signal.value == "" then return end
local input = tonumber(signal.value) or 1
if input == 1 then
if mem.value == "0" then mem.value = "1" else mem.value = "0" end
elseif input == 0 then
mem.value = "0"
elseif input == -1 then
mem.value = "1"
end
end
local think = function()
for component, _ in pairs(allItems) do
component.SendSignal(component.Components[2].value, "state_out")
end
end
Hook.Add("think", "flipflopcomponent.think", think)
Hook.Add("signalreceived.flipflopcomponent", "flipflopcomponent.signalReceived", signalReceived)

View File

@@ -0,0 +1,44 @@
local allItems = {}
local defaultTable = {
load_value_in = 0,
fuel_in = 100,
efficiency_in = 100,
fission_efficiency_in = 75,
max_power_in = 20000,
silent_in = 0
}
Hook.Add("item.created", "reactorcontrollercomponent.init", function(item)
if item.Prefab.Identifier == "reactorcontrollercomponent" then
allItems[item] = {}
for k, v in pairs(defaultTable) do
allItems[item][k] = v
end
end
end)
local signalReceived = function(signal, connection)
local this = connection.Item
local mem = allItems[this]
local signalNum = tonumber(signal.value) or defaultTable[connection.Name]
mem[connection.Name] = signalNum
local turbineoutput = (mem.load_value_in / mem.max_power_in) * mem.efficiency_in
local fissionrate = turbineoutput / (mem.fuel_in / mem.fission_efficiency_in)
if mem.silent_in == 1 then
if turbineoutput > 10 then
turbineoutput = 10
end
if fissionrate > 10 then
fissionrate = 10
end
end
this.SendSignal(tostring(turbineoutput), "turbine_output_out")
this.SendSignal(tostring(fissionrate), "fission_rate_out")
end
Hook.Add("signalreceived.reactorcontrollercomponent", "reactorcontrollercomponent.signalReceived", signalReceived)