This repository has been archived on 2025-07-31. You can view files and clone it, but cannot push or open issues or pull requests.
Files
barotrauma-custom-components/Lua/enginecontrollercomponent.lua

91 lines
2.8 KiB
Lua

local allItems = {}
local defaultTable = {
velocity_x_in = 0,
current_velocity_x_in = 0,
set_trm_target = 20,
set_trm_aggression = 200,
set_mode = 1,
set_silent = 0,
previous_velocity_x_in = 0,
acceleration = 0,
}
Hook.Add("item.created", "enginecontrollercomponent.init", function(item)
if item.Prefab.Identifier == "enginecontrollercomponent" 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]
local set_mode = mem.set_mode
if
mem[connection.Name] == signalNum or
connection.Name == "current_velocity_x_in" and set_mode ~= 0
then
return
end
mem[connection.Name] = signalNum
local main_force_out
local boost_force_out
local velocity_x_in = mem.velocity_x_in
if set_mode == 1 then
main_force_out = velocity_x_in
boost_force_out = 0
elseif set_mode == 2 then
main_force_out = velocity_x_in
boost_force_out = velocity_x_in
elseif set_mode == 0 then
local current_velocity_x_in = mem.current_velocity_x_in
local set_trm_target = mem.set_trm_target
if connection.Name == "current_velocity_x_in" then
mem.acceleration = (mem.previous_velocity_x_in - current_velocity_x_in)
mem.previous_velocity_x_in = current_velocity_x_in
end
local absolute_current_velocity = math.abs(current_velocity_x_in)
if set_trm_target - 0.25 < absolute_current_velocity and velocity_x_in * current_velocity_x_in > 0 then
local set_trm_aggression = mem.set_trm_aggression
local target_acceleration = - (absolute_current_velocity * set_trm_aggression) + (set_trm_target * set_trm_aggression)
if math.abs(velocity_x_in) > math.abs(target_acceleration) then
if current_velocity_x_in > 0 then
main_force_out = target_acceleration
else
main_force_out = -target_acceleration
end
else
main_force_out = velocity_x_in
end
else
main_force_out = velocity_x_in
end
boost_force_out = 0
end
if mem.set_silent == 1 then
main_force_out = main_force_out * 0.2
boost_force_out = boost_force_out * 0.2
end
this.SendSignal(main_force_out, "main_force_out")
this.SendSignal(boost_force_out, "boost_force_out")
end
Hook.Add("signalreceived.enginecontrollercomponent", "enginecontrollercomponent.signalReceived", signalReceived)