Barotrauma 潜渊症

Barotrauma 潜渊症

RuyiLuaComponent
whosyourdaddy  [开发者] 4 月 24 日 上午 7:09
Code Example
Enter the password to open the door
function inp(pin, value) -- If input pin 1 received signal if pin == 1 then -- If input pin 1 received signal value "9527" if value == "9527" then -- Open the keypad door out[1] = 1 end end end

At the start of the round, generate a random number
function onMapLoaded() -- send a generated random number ranged in 0~10000 from output pin 1 when all items have been loaded out[1] = math.random() * 10000.0 end

Record campaign playtime
local campaignPlaytime function update(deltaTime) campaignPlaytime = campaignPlaytime + deltaTime local hours = math.floor(campaignPlaytime / 3600) local minutes = math.floor(campaignPlaytime / 60) % 60 local seconds = math.floor(campaignPlaytime) % 60 -- outputs formatted playtime out[1] = ("%i:%i:%i"):format(hours, minutes, seconds) end function onItemLoaded() -- read the last saved campaign playtime when the item has finished loading -- note: if the stored data cannot be converted to a number, reset the playtime to 0.0 campaignPlaytime = tonumber(readMemory()) or 0.0 end function onSave() -- saved the current campaign playtime when the component starts being saved. writeMemory(tostring(campaignPlaytime)) end

Print welcome message in multiplayer

function update()
-- we do the initialization on update for ensure stability

if isMultiplayer then
if isClient then
-- When you load into the game, you send the message ​​"Hi everyone! I’m in!"​​ to the server.
netSend "Hi everyone! I’m in!"

-- When you receive the message from the server, it gets printed to your console.
function netReceive(message)
print(message)
end
end

if isServer then
function netReceive(message, client)
-- Upon receiving the message from the player, the server broadcasts their message to all other players.
netSend(client.Name .. " said: " .. message)
-- output to the text displayer
out[1] = client.Name .. " said: " .. message
end
end
end

-- set nil, do not need to execute the initialization again
update = nil
end

Teleport character
inp = {} inpSenders = {} function update() -- If the input pin 3 received a signal if inp[3] then local signalSource = inpSenders[3] -- If the signal takes a source if signalSource then -- Teleport the signal source to the position of this component signalSource.TeleportTo(ruyiLua.Item.WorldPosition) -- reset the signal source of the input pin 3 inpSenders[3] = nil end -- reset input pin 3 inp[3] = nil end end

Control engine set_force via key presses
inpSenders = {} -- Implementation Method 1 function inp(pin, value) -- When input pin 1 receives a control signal from the periscope if pin == 1 then -- Get the signal sender who is using the periscope local signalSource = inpSenders[pin] if signalSource then -- Detect the controller's key presses. if signalSource.IsKeyDown(InputType.Left) then -- If the controller is pressing the Left mouse button -- Output pin 1​ sends a value of ​​-100​ to the engine, making the submarine move ​backward. out[1] = -100 elseif signalSource.IsKeyDown(InputType.Right) then -- If the controller is pressing the Right mouse button -- Output pin 1​ sends a value of ​100​ to the engine, making the submarine move ​forward. out[1] = 100 end -- clear the table clear(inpSenders) end end end -- Implementation Method 2 inp = {} function update() local engineHasBeenControledViaKeyPresses = false if inp[1] then local signalSource = inpSenders[1] if signalSource then if signalSource.IsKeyDown(InputType.Left) then -- Set the signal sender of the output pin 1 as the signal source of that input pin 1 outSender = signalSource out[1] = -100 outSender = nil engineHasBeenControledViaKeyPresses = true elseif signalSource.IsKeyDown(InputType.Right) then outSender = signalSource out[1] = 100 outSender = nil engineHasBeenControledViaKeyPresses = true end clear(inpSenders) end clear(inp) end -- If there is not any controllers using the periscope to control the engine, reset its set_force if not engineHasBeenControledViaKeyPresses then out[1] = 0 end end
< >
正在显示第 1 - 1 条,共 1 条留言
-- 智能反应堆控制器
-- 输入1: 实时负载 (kW)
-- 输入2: 实时功率 (kW)
-- 输入3: 最大反应堆功率 (kW)
-- 输入4: 反应堆温度 (0-100000)
-- 输出1: 裂变速率控制 (%)
-- 输出2: 涡轮输出功率 (%)

-- 控制器配置参数
local config = {
minFissionRate = 10, -- 最小裂变速率(%)
maxFissionRate = 100, -- 最大裂变速率(%)
responseDelay = 0.5, -- 响应延迟(秒)
powerSmoothing = 0.1, -- 功率平滑系数
loadStableThreshold = 5, -- 负载稳定阈值(kW)
efficiencyTarget = 0.95, -- 目标效率值
tempEmergencyThreshold = 5100,-- 温度紧急阈值,超过此值裂变速率设为0
tempStableRange = 5000 -- 温度稳定范围
}

-- 状态变量
local currentLoad = 0
local currentPower = 0
local currentTemperature = 0
local maxReactorPower = 3100 -- 默认值3100kW
local targetFissionRate = config.minFissionRate
local currentFissionRate = config.minFissionRate
local lastUpdateTime = 0
local loadHistory = {}
local powerHistory = {}
local maxHistorySize = 10
local emergencyShutdown = false -- 紧急关闭状态

-- 初始化控制器
function initialize()
print("反应堆控制器启动完成 - 最大功率: " .. maxReactorPower .. "kW")
print("温度紧急阈值: " .. config.tempEmergencyThreshold)
print("温度稳定范围: " .. config.tempStableRange)
end

-- 输入处理函数
function inp(pin, value)
if pin == 1 then
-- 实时负载输入 (kW)
currentLoad = tonumber(value) or 0
recordHistory(loadHistory, currentLoad)
elseif pin == 2 then
-- 实时功率输入 (kW)
currentPower = tonumber(value) or 0
recordHistory(powerHistory, currentPower)
elseif pin == 3 then
-- 最大反应堆功率输入 (kW)
local newMaxPower = tonumber(value) or 3100
if newMaxPower ~= maxReactorPower then
maxReactorPower = newMaxPower
print("更新最大反应堆功率: " .. maxReactorPower .. "kW")
end
elseif pin == 4 then
-- 反应堆温度输入 (0-100000)
currentTemperature = tonumber(value) or 0

-- 检查温度紧急阈值
if currentTemperature >= config.tempEmergencyThreshold then
if not emergencyShutdown then
emergencyShutdown = true
print("紧急关闭: 温度超过" .. config.tempEmergencyThreshold .. "!")
end
elseif currentTemperature < config.tempEmergencyThreshold then
if emergencyShutdown then
emergencyShutdown = false
print("紧急关闭解除: 温度低于" .. config.tempEmergencyThreshold)
end
end
end
end

-- 主更新函数
function update(deltaTime)
-- 控制逻辑执行
local currentTime = os.time()
if currentTime - lastUpdateTime >= config.responseDelay then
-- 如果处于紧急关闭状态,强制裂变速率为0
if emergencyShutdown then
out[1] = 0 -- 裂变速率设为0
out[2] = 0 -- 涡轮输出设为0
print("紧急关闭状态: 裂变速率和涡轮输出已设为0")
else
-- 正常控制逻辑
if performSafetyCheck() then
adjustFissionRate()
local turbineOutputPercent = calculateTurbineOutputPercent()

-- 输出控制信号
out[1] = currentFissionRate
out[2] = turbineOutputPercent

-- 调试输出
print(string.format("负载: %.1fkW, 功率: %.1fkW, 温度: %.0f, 裂变: %.1f%%, 涡轮: %.1f%%",
currentLoad, currentPower, currentTemperature, currentFissionRate, turbineOutputPercent))
else
-- 安全模式:最低输出
out[1] = config.minFissionRate
out[2] = 5 -- 5% 最低涡轮输出
end
end

lastUpdateTime = currentTime
end
end

-- 记录历史数据用于趋势分析
function recordHistory(historyTable, value)
table.insert(historyTable, value)
if #historyTable > maxHistorySize then
table.remove(historyTable, 1)
end
end

-- 计算负载变化趋势 (kW/秒)
function calculateLoadTrend()
if #loadHistory < 2 then return 0 end

local sum = 0
for i = 2, #loadHistory do
sum = sum + (loadHistory - loadHistory[i-1])
end
return sum / (#loadHistory - 1)
end

-- 计算当前功率效率
function calculateEfficiency()
if currentLoad <= 0 then return 0 end
return math.min(1.0, currentPower / currentLoad)
end

-- 检查负载是否稳定
function isLoadStable()
if #loadHistory < 2 then return true end

local variance = 0
local average = 0
for _, value in ipairs(loadHistory) do
average = average + value
end
average = average / #loadHistory

for _, value in ipairs(loadHistory) do
variance = variance + math.abs(value - average)
end
variance = variance / #loadHistory

return variance < config.loadStableThreshold
end

-- 温度稳定控制 - 维持温度在5000左右
function applyTemperatureStabilization(baseTarget)
-- 计算与目标温度的偏差
local tempDeviation = math.abs(currentTemperature - config.tempStableRange)

-- 如果温度偏离目标值,调整裂变速率
if tempDeviation > 100 then -- 允许100的偏差范围
local stabilizationFactor = 1.0

-- 温度过高,降低裂变速率
if currentTemperature > config.tempStableRange then
stabilizationFactor = 1.0 - (tempDeviation / 1000) * 0.5 -- 最大降低50%
print(string.format("温度稳定控制: 温度%.0f过高, 控制因子%.2f", currentTemperature, stabilizationFactor))
-- 温度过低,提高裂变速率
else
stabilizationFactor = 1.0 + (tempDeviation / 1000) * 0.3 -- 最大提高30%
print(string.format("温度稳定控制: 温度%.0f过低, 控制因子%.2f", currentTemperature, stabilizationFactor))
end

baseTarget = baseTarget * stabilizationFactor
end

return baseTarget
end

-- 自适应裂变速率控制
function adjustFissionRate()
local loadRatio = currentLoad / maxReactorPower
local efficiency = calculateEfficiency()
local trend = calculateLoadTrend()

-- 基础计算
local baseTarget = loadRatio * config.maxFissionRate

-- 效率修正
local efficiencyFactor = 1.0
if efficiency < config.efficiencyTarget - 0.1 then
efficiencyFactor = 1.05 -- 效率低,提高裂变速率
elseif efficiency > config.efficiencyTarget + 0.1 then
efficiencyFactor = 0.95 -- 效率过高,降低裂变速率
end

-- 趋势预测 (kW/秒)
local trendFactor = 1.0
if math.abs(trend) > 50 then -- 负载变化超过50kW/秒
trendFactor = trend > 0 and 1.03 or 0.97
end

-- 负载稳定性调整
local stabilityFactor = 1.0
if not isLoadStable() then
stabilityFactor = 0.98 -- 负载不稳定时保守调节
end

-- 合并所有因素
baseTarget = baseTarget * efficiencyFactor * trendFactor * stabilityFactor

-- 应用温度稳定控制 - 维持温度在5000左右
baseTarget = applyTemperatureStabilization(baseTarget)

-- 应用限制
targetFissionRate = math.max(config.minFissionRate,
math.min(config.maxFissionRate, baseTarget))

-- 平滑过渡到目标值
currentFissionRate = currentFissionRate + (targetFissionRate - currentFissionRate) * config.powerSmoothing
end

-- 计算涡轮输出功率百分比 (%)
function calculateTurbineOutputPercent()
local turbineOutputPercent = 0

if currentFissionRate > config.minFissionRate then
-- 基础输出基于裂变速率
local baseOutputPercent = currentFissionRate -- 直接使用裂变速率作为基础百分比

if currentLoad > 0 then
-- 根据负载需求微调
local loadAdjustment = ((currentLoad - currentPower) / maxReactorPower) * 20
local efficiencyAdjustment = (config.efficiencyTarget - calculateEfficiency()) * 10
turbineOutputPercent = baseOutputPercent + loadAdjustment + efficiencyAdjustment
else
-- 空载时降低输出
turbineOutputPercent = baseOutputPercent * 0.3
end

-- 限制输出范围 (0%到100%)
turbineOutputPercent = math.max(0, math.min(100, turbineOutputPercent))
end

return turbineOutputPercent
end

-- 安全检查
function performSafetyCheck()
local loadRatio = currentLoad / maxReactorPower

-- 负载安全检查
if loadRatio > 1.1 then
-- 严重过载 (超过最大功率10%),紧急降低输出
targetFissionRate = config.minFissionRate
print("警告: 反应堆严重过载! 紧急降低输出")
return false
elseif loadRatio > 0.95 then
-- 高负载 (超过最大功率95%),保守调节
targetFissionRate = math.min(targetFissionRate, config.maxFissionRate * 0.9)
end

return true
end

-- 保存和加载函数
function onItemLoaded()
local savedData = readMemory()
if savedData then
print("控制器状态已加载")
end
end

function onSave()
local saveData = string.format("%f,%f", config.minFissionRate, config.maxFissionRate)
writeMemory(saveData)
end

-- 初始化控制器
initialize()
最后由 天启洪流apocalyptic 编辑于; 10 月 16 日 上午 4:41
< >
正在显示第 1 - 1 条,共 1 条留言
每页显示数: 1530 50