Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require('component')
- local event = require('event')
- local keyboard = require('keyboard')
- local term = require('term')
- local reactor = require('reactor')
- local turbine = require('turbine')
- local STATUS_STARTUP = 1
- local STATUS_ACTIVE = 2
- local STATUS_COOL_DOWN = 3
- local STATUS_INACTIVE = 4
- local STEAM_FLOW_RATE = 2000
- local ROTOR_SPEED = 1800
- function round( num,idp )
- local mult = 10^(idp or 0)
- return math.floor(num * mult + 0.5) / mult
- end
- local function write_xy( row,col,s,... )
- term.setCursor( col,row )
- term.write( s:format(...) )
- end
- local loop = true
- local status = STATUS_INACTIVE
- local evt = {}
- local curr_control_rod = 1
- local last_steam_produced = 0
- local control_rod_step = 0.1
- local control_rod_lock = false
- local r = reactor.new('e74e3a')
- local turbines = {}
- turbines[1] = turbine.new('49b302')
- turbines[2] = turbine.new('b28bd5')
- local steam_needed = STEAM_FLOW_RATE * #turbines
- function evt.key_down( _,address,char,code,player )
- if code == keyboard.keys.s then
- if status == STATUS_INACTIVE then
- status = STATUS_STARTUP
- elseif status == STATUS_ACTIVE then
- status = STATUS_COOL_DOWN
- end
- elseif code == keyboard.keys.l then
- control_rod_lock = not control_rod_lock
- elseif code == keyboard.keys.q then
- loop = false
- return
- end
- end
- event.listen( 'key_down',evt.key_down )
- term.clear()
- --turn everything off to normalize state
- if r:is_running() then
- r:all_rods_level(100)
- r:stop()
- end
- for t_idx,t in ipairs(turbines) do
- t:disengage_inductor()
- t:stop()
- end
- write_xy(1,1,'Status: Inactive ')
- local STATUS_STEAM_INCREASE = 1
- local STATUS_STEAM_DECREASE = 2
- local STATUS_STEAM_STABLE = 3
- local steam_poll = nil
- local steam_status = nil
- local last_control_rod = 1
- while loop do
- if status == STATUS_STARTUP then
- write_xy(1,1,'Status: Starting Up')
- os.sleep(1)
- for t_idx,_ in ipairs(turbines) do
- turbines[t_idx]:start()
- end
- r:start()
- status = STATUS_ACTIVE
- elseif status == STATUS_ACTIVE then
- write_xy(1,1,'Status: Active ')
- --handle steam production
- local steam_produced = r:hot_fluid_produced()
- local rod_level = r:rod_level(1)
- --[[
- if steam_produced < ( steam_needed - 1 ) or steam_produced > ( steam_needed + 1 ) then
- local steam_diff = 0
- local increase_rod_level = true
- if steam_produced > last_steam_produced then
- --steam output increasing
- steam_diff = steam_produced - last_steam_produced
- increase_rod_level = true
- elseif steam_produced < last_steam_produced then
- --steam outout decreasing
- steam_diff = last_steam_produced - steam_produced
- increase_rod_level = false
- end
- local control_rod_step = 1
- if steam_diff > 200 then
- --rising quickly, move control rod more
- control_rod_step = control_rod_step * 3 --kind of pointless, but left in for more control
- elseif steam_diff >= 100 then
- control_rod_step = control_rod_step * 1
- elseif control_rod_step >= 25 then
- control_rod_step = control_rod_step * 0.3
- else
- control_rod_step = control_rod_step * 0.1
- end
- if increase_rod_level then
- r:increase_rod_level( control_rod_step )
- rod_level = rod_level + control_rod_step
- else
- r:decrease_rod_level( control_rod_step )
- rod_level = rod_level - control_rod_step
- end
- end
- --]]
- --[[
- if steam_produced < ( steam_needed - 100 ) then
- r:decrease_rod_level(0.8)
- elseif steam_produced > ( steam_needed + 100 ) then
- r:increase_rod_level(0.8)
- else
- --refine control to individual rods
- if steam_produced < ( steam_needed - 1 ) then
- local next_control_rod = ( last_control_rod + 1 )
- if next_control_rod > ( r:rod_count() - 1 ) then
- next_control_rod = 0
- end
- r:decrease_single_rod_level( next_control_rod,control_rod_step )
- last_control_rod = next_control_rod
- elseif steam_produced > ( steam_needed + 1 ) then
- local next_control_rod = ( last_control_rod - 1 )
- if next_control_rod < 0 then
- next_control_rod = ( r:rod_count() - 1 )
- end
- r:increase_single_rod_level( next_control_rod,control_rod_step )
- last_control_rod = next_control_rod
- end
- end
- --]]
- --[[
- if steam_produced < ( steam_needed - 100 ) then
- r:decrease_rod_level(0.8)
- elseif steam_produced > ( steam_needed + 100 ) then
- r:increase_rod_level(0.8)
- else
- --refine control to individual rods
- if steam_produced < ( steam_needed - 1 ) then
- local next_control_rod = ( last_control_rod + 1 )
- if next_control_rod > ( r:rod_count() - 1 ) then
- next_control_rod = 0
- end
- r:decrease_single_rod_level( next_control_rod,control_rod_step )
- last_control_rod = next_control_rod
- steam_status = STATUS_STEAM_DECREASE
- elseif steam_produced > ( steam_needed + 1 ) then
- local next_control_rod = ( last_control_rod - 1 )
- if next_control_rod < 0 then
- next_control_rod = ( r:rod_count() - 1 )
- end
- r:increase_single_rod_level( next_control_rod,control_rod_step )
- last_control_rod = next_control_rod
- steam_status = STATUS_STEAM_INCREASE
- else
- steam_status = STATUS_STEAM_STABLE
- end
- end
- --]]
- if not control_rod_lock then
- if steam_poll == nil or steam_poll == 20 then
- if steam_produced > ( steam_needed + 10 ) then --if producing too much steam, lower rods quickly to prevent build up
- r:increase_rod_level(1)
- elseif steam_produced < ( steam_needed - 100 ) then --if producing too little steam, raise rods fairly quickly to build output
- r:decrease_rod_level(0.8)
- elseif steam_produced < ( steam_needed - 1 ) then --if producing under 100mb from target output, slow rod rise speed to prevent overshooting
- local steam_diff = steam_needed - steam_produced
- local rod_step = 0.2
- if steam_diff <= 10 then
- rod_step = 0.05
- end
- local rod_idx = r:highest_rod()
- r:decrease_single_rod_level( rod_idx,rod_step )
- elseif steam_produced > ( steam_needed + 1 ) and steam_produced <= ( steam_needed + 10 ) then --if producing just over target, lower rods slowly to match goal
- local steam_diff = steam_produced - steam_needed
- local rod_step = 0.1
- local rod_idx = r:lowest_rod()
- r:increase_single_rod_level( rod_idx,rod_step )
- end
- steam_poll = 0
- else
- steam_poll = steam_poll + 1
- end
- end
- --[[
- if steam_produced > ( steam_needed + 10 ) then
- r:increase_rod_level(0.7)
- elseif steam_produced < ( steam_needed - 100 ) then
- r:decrease_rod_level(0.3)
- elseif steam_produced < ( steam_needed - 1 ) then
- local steam_diff = steam_needed - steam_produced
- local rod_step = 0.1
- --[[
- if steam_diff <= 100 then
- rod_step = 0.1
- end
- --]]--[[
- r:decrease_single_rod_level( curr_control_rod,rod_step )
- curr_control_rod = curr_control_rod + 1
- if curr_control_rod > r:rod_count() then
- curr_control_rod = 1
- end
- elseif steam_produced > steam_needed and steam_produced <= ( steam_needed + 10 ) then
- local steam_diff = steam_produced - steam_needed
- local rod_step = 0.1
- --[[
- if steam_diff <= 100 then
- rod_step = 0.1
- end
- --]]--[[
- r:increase_single_rod_level( curr_control_rod,rod_step )
- curr_control_rod = curr_control_rod - 1
- if curr_control_rod < 1 then
- curr_control_rod = r:rod_count()
- end
- end
- --]]
- --[[
- --refine control to individual rods
- if steam_produced < ( steam_needed - 1 ) then
- local next_control_rod = ( last_control_rod + 1 )
- if next_control_rod > ( r:rod_count() - 1 ) then
- next_control_rod = 0
- end
- local steam_diff = steam_needed - steam_produced
- local rod_step = 0.3
- if steam_diff <= 10 then
- rod_step = 0.1
- end
- r:decrease_single_rod_level( next_control_rod,rod_step )
- last_control_rod = next_control_rod
- elseif steam_produced > ( steam_needed + 1 ) then
- local next_control_rod = ( last_control_rod - 1 )
- if next_control_rod < 0 then
- next_control_rod = ( r:rod_count() - 1 )
- end
- local steam_diff = steam_produced - steam_needed
- local rod_step = 0.3
- if steam_diff <= 10 then
- rod_step = 0.1
- end
- r:increase_single_rod_level( next_control_rod,rod_step )
- last_control_rod = next_control_rod
- end
- end
- --]]
- last_steam_produced = steam_produced
- --local rod_levels = ''
- --for i=1,#r.rod_levels do
- -- rod_levels = rod_levels .. '|' .. round(r.rod_levels[i],2)
- --end
- --write_xy(8,1,rod_levels .. ' ')
- local rod_line = 'Rod Level: %g%%'
- if control_rod_lock then
- rod_line = rod_line .. ' [locked]'
- else
- rod_line = rod_line .. ' '
- end
- write_xy(2,1,rod_line,rod_level);
- write_xy(3,1,'Fluid Produced: %g mb ',steam_produced)
- local line_adjust = 0
- for t_idx,_ in ipairs(turbines) do
- local inductor_engaged = turbines[t_idx]:is_inductor_engaged()
- local rotor_speed = turbines[t_idx]:rotor_speed()
- if not inductor_engaged and rotor_speed >= ( ROTOR_SPEED + 5 ) then
- turbines[t_idx]:engage_inductor()
- elseif inductor_engaged and rotor_speed <= ( ROTOR_SPEED - 5 ) then
- turbines[t_idx]:disengage_inductor()
- end
- write_xy(4 + line_adjust,1,'Turbine #' .. t_idx)
- write_xy(5 + line_adjust,5,'Rotor Speed: %g RPM ',rotor_speed)
- write_xy(6 + line_adjust,5,' Power: %g RF ',turbines[t_idx]:energy_produced())
- line_adjust = line_adjust + 4
- end
- elseif status == STATUS_COOL_DOWN then
- term.clear()
- write_xy(1,1,'Status: Cool Down ')
- r:stop()
- r:all_rods_level(100)
- local fluid_amount = r:hot_fluid_amount()
- if fluid_amount == 0 then
- for t_idx,_ in ipairs(turbines) do
- turbines[t_idx]:stop()
- end
- status = STATUS_INACTIVE
- end
- elseif status == STATUS_INACTIVE then
- write_xy(1,1,'Status: Inactive ')
- end
- --[[
- local curr_energy = r1.getEnergyStored()
- local curr_percentage = ( ( curr_energy / 10000000 ) * 100 )
- write_xy( 2,1,'Energy %%: %g',curr_percentage )
- if last_energy ~= nil then
- if curr_energy < last_energy then
- write_xy( 3,1,' Usage: -%g RF ',( last_energy - curr_energy ) )
- elseif curr_energy > last_energy then
- write_xy( 3,1,' Usage: +%g RF ',( curr_energy - last_energy ) )
- end
- end
- last_energy = curr_energy
- if curr_percentage <= 15 then
- r1.setActive(true)
- write_xy(1,1,' Status: On ')
- elseif curr_percentage >= 90 then
- r1.setActive(false)
- write_xy(1,1,' Status: Off')
- end
- --]]
- --r:save_state('last')
- os.sleep(0)
- end
- term.clear()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement