Created
September 30, 2024 15:03
-
-
Save Neboer/8d8d50a5e6013a412b9433fbd809cc42 to your computer and use it in GitHub Desktop.
使用高级电脑CC:Twicked控制Better-Fusion-Reactor中的核反应堆的主程序。背面接效率输入,左面减CR,右面加CR。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
StableTimeSec = 5.1 | |
function INFO(s) | |
write(s .. "\n") | |
end | |
function WAIT() | |
sleep(StableTimeSec) | |
end | |
function SetOutput(value) | |
if value > 0 then | |
redstone.setAnalogOutput("right", value) | |
sleep(0.1) | |
redstone.setAnalogOutput("right", 0) | |
else | |
redstone.setAnalogOutput("left", -value) | |
sleep(0.1) | |
redstone.setAnalogOutput("left", 0) | |
end | |
end | |
function GetEffLevel() | |
return redstone.getAnalogInput("back") | |
end | |
function IsRunning() | |
return redstone.getAnalogInput("back") ~= 0 | |
end | |
function IsGood() | |
return GetEffLevel() > 13 | |
end | |
function GetEffChangeStatus() -- get how does eff changed in StableTimeSec | |
local delta_time = 0.3 | |
local current_time = 0 | |
local last_value = GetEffLevel() | |
local first_change = 0 | |
local last_change = 0 | |
-- we record the first change as first_change, and every change after first_change is the last_change. | |
while current_time < StableTimeSec do | |
local value = GetEffLevel() | |
if value ~= last_value then | |
if first_change == 0 then | |
first_change = GetSign(value - last_value) | |
last_change = first_change -- if there is only one change, first_change is also the last_change. | |
else | |
last_change = GetSign(value - last_value) | |
end | |
end | |
last_value = value | |
sleep(delta_time) | |
current_time = current_time + delta_time | |
end | |
return first_change, last_change | |
end | |
function ClearCR() -- sync controller cr and reactor cr | |
local last_eff = -1 | |
local current_eff = 0 | |
repeat | |
INFO("clearing... -15") | |
SetOutput(-15) | |
WAIT() | |
INFO("last_eff: " .. last_eff .. ", current_eff: " .. current_eff .. "") | |
last_eff = current_eff | |
current_eff = GetEffLevel() | |
until current_eff == last_eff | |
INFO("last_eff is equal to current_eff") | |
SetOutput(-15) | |
WAIT() -- must clear cr | |
INFO("finished syncing...") | |
end | |
Controller = { | |
cr = -1, | |
set_output = function(value) | |
SetOutput(value) | |
-- we need to know will the eff change steadily or peakly. so we don't use wait, we use GetEffChangeStatus to get changes. | |
local first_change, last_change = GetEffChangeStatus() | |
Controller.cr = Controller.cr + value | |
if Controller.cr < 0 then | |
Controller.cr = 0 | |
elseif Controller.cr > 100 then | |
Controller.cr = 100 | |
end | |
return first_change, last_change | |
end, | |
get_eff_lv = GetEffLevel, | |
is_running = IsRunning, | |
is_good = IsGood, | |
sync = function() | |
ClearCR() | |
Controller.cr = 0 | |
end | |
} | |
-- libraries | |
function GetSign(number) | |
return (number > 0 and 1) or (number == 0 and 0) or -1 | |
end | |
-- below is the solution | |
function AdjustMain() | |
local minifier = 1 | |
while Controller.is_running() do | |
while Controller.is_running() and Controller.is_good() do | |
INFO("we are good.") | |
if minifier ~= 1 then | |
INFO("reset minifier to 1") | |
minifier = 1 | |
end | |
WAIT() | |
end | |
INFO("eff_lv is " .. Controller.get_eff_lv() .. ", not good, keep adjusting.") | |
local calculated_output = GetSign(minifier) * math.floor(15 / math.abs(minifier)) | |
INFO("adjust output: " .. calculated_output) | |
local first_change, last_change = Controller.set_output(calculated_output) | |
if first_change == last_change then | |
INFO(first_change .. "->" .. last_change .. " eff change steadily.") | |
if first_change < 0 then | |
minifier = -minifier | |
INFO("eff is decreasing, reverse the minifier to " .. minifier) | |
end | |
else | |
INFO(first_change .. "->" .. last_change .. " eff change peakly.") | |
if first_change < 0 and last_change > 0 then | |
INFO("eff is increasing now, keep going.") | |
elseif first_change > 0 and last_change < 0 then | |
local larger_minifier = GetSign(minifier) * (math.abs(minifier) + 1) | |
INFO("eff is increasing and then decreasing, to reach the peak, enlarge the minifier from " .. minifier .. " to " .. larger_minifier) | |
INFO("and we reverse minifier to " .. -larger_minifier) | |
minifier = -larger_minifier | |
end | |
end | |
if Controller.cr == 0 or Controller.cr == 100 then | |
INFO("cr has reached the limit " .. Controller.cr .. ", reverse minifier.") | |
minifier = -minifier | |
end | |
end | |
end | |
function Main() | |
while true do | |
if Controller.is_running() then | |
INFO("start syncing...") | |
Controller.sync() | |
INFO("controller synced, start adjusting...") | |
AdjustMain() | |
else | |
INFO("controller is not running, wait for 5 seconds.") | |
WAIT() | |
end | |
end | |
end | |
Main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment