Advertisement
neuroticfox

CCDC v1

Jul 2nd, 2023 (edited)
473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.58 KB | None | 0 0
  1. local tArgs = {...}
  2.  
  3.  bgColor = colors.black
  4.  txtColor = colors.white
  5.  numColor = colors.red
  6.  flowGates = {}
  7.  inputFlux = nil
  8.  outputFlux = nil
  9.  reactor = nil
  10.  tickCount = 00
  11.  
  12.  -- Safety Checks
  13.  
  14. local deviceList = peripheral.getNames()
  15.  for i = 1, #deviceList do
  16.   local device = peripheral.getType(deviceList[i])
  17.   if device == "flow_gate" then
  18.    table.insert(flowGates, deviceList[i])
  19.   elseif device == "draconic_reactor" then
  20.    reactor = peripheral.wrap(deviceList[i])
  21.   elseif device == "monitor" then
  22.    mon = peripheral.wrap(deviceList[i])
  23.   end
  24.  end
  25.  
  26. function clear()
  27.  if mon.isColor() == true then
  28.   mon.setBackgroundColor(bgColor)
  29.  end
  30.  mon.clear()
  31.  mon.setCursorPos(1,1)
  32. end
  33. clear()
  34.  
  35. local info = reactor.getReactorInfo()
  36.  flux_in = peripheral.wrap(flowGates[1])
  37.  flux_out = peripheral.wrap(flowGates[2])
  38.  
  39.  -- Set Values
  40.  
  41. local targetTemp = tonumber(tArgs[1]) or 8000  -- Temperature at which to hold the reactor
  42. local targetShield = tonumber(tArgs[2]) or 5 -- Desired shield strength to hold the reactor at. Ranges from 0.01 for 1% shield, to 1 for 100%
  43. local reactorOutputMultiplier = tonumber(tArgs[3]) or 10 -- Check "Mod Options > Draconic Evolution > Tweaks > reactorOutputMultiplier" to find what it is and change the variable below to match.
  44.  
  45.  -- Yield
  46. local function yield()
  47.   os.queueEvent("someFakeEventName") -- queue the event
  48.   os.pullEvent("someFakeEventName") -- pull it
  49. end
  50.  
  51.  -- AutoSet Gates
  52.     reactor.chargeReactor()
  53.     satOne = info.energySaturation
  54.     fieldOne = info.fieldStrength
  55.     flux_in.setSignalLowFlow(1.0)
  56.     os.sleep(0.5)
  57.     satTwo = info.energySaturation
  58.     fieldTwo = info.fieldStrength
  59.     flux_in.setSignalLowFlow(0.0)
  60.     reactor.stopReactor()
  61. if satTwo <= satOne or fieldTwo <= fieldOne then
  62. flux_in = peripheral.wrap(flowGates[2])
  63. flux_out = peripheral.wrap(flowGates[1])
  64. end
  65.  
  66.  -- Functions
  67.  reactor.chargeReactor()
  68.  
  69. function exit_msg(msg)
  70.   term.clear()
  71.   print(msg)
  72.   os.exit()
  73. end
  74.  
  75. function modify_temp(offset)
  76.   local new_temp = ideal_temp + offset
  77.   if new_temp > 8000 then
  78.     new_temp = 8000
  79.   elseif new_temp < 2500 then
  80.     new_temp = 2500
  81.   end
  82.   ideal_temp = new_temp
  83. end
  84.  
  85. function modify_field(offset)
  86.   local new_strength = ideal_strength + offset
  87.   if new_strength > 90 then
  88.     new_strength = 90
  89.   elseif new_strength < 1 then
  90.     new_strength = 1
  91.   end
  92.   ideal_strength = new_strength
  93. end
  94.  
  95.  -- main code
  96.  
  97. flux_in.setSignalLowFlow(0)
  98. flux_out.setSignalLowFlow(0)
  99.  
  100. local condition = reactor.getReactorInfo()
  101. if not condition then
  102.   print("Reactor not initialized, please ensure the stabilizers are properly laid out.")
  103.   os.exit()
  104. end
  105.  
  106. ideal_strength = 50
  107.  
  108. ideal_temp = 8000
  109. cutoff_temp = 8100
  110.  
  111.  -- tweakable pid gains
  112.  
  113. inflow_P_gain = 1
  114. inflow_I_gain = 0.04
  115. inflow_D_gain = 0.1
  116.  
  117. outflow_P_gain = 500
  118. outflow_I_gain = 0.5
  119. outflow_II_gain = 0.0000003
  120. outflow_D_gain = 60000
  121.  
  122.  -- initialize main loop
  123.  
  124. inflow_I_sum = 0
  125. inflow_D_last = 0
  126.  
  127. outflow_I_sum = 0
  128. outflow_II_sum = 0
  129. outflow_D_last = 0
  130.  
  131. state = "Standby"
  132. shutting_down = false
  133.  
  134. if condition.temperature > 25 then
  135.   state = "Cooling"
  136. end
  137. if condition.temperature > 2000 then
  138.   state = "Active"
  139. end
  140.  
  141.  -- Possible states:
  142.   --Standby
  143.   --Charging
  144.   --Active
  145.   --Manual Shutdown
  146.   --Emergency Shutdown
  147.   --Cooling
  148.  
  149. event_loop = true
  150. while event_loop do
  151. reactor.getReactorInfo()
  152. if info.status == warming_up and info.temperature >= 2000 then
  153. reactor.activateReactor()
  154. end
  155.  
  156.   local info = reactor.getReactorInfo()
  157.  
  158.   local inflow = 0
  159.   local outflow = 0
  160.  
  161.   shutting_down = state == "Manual Shutdown" or state == "Emergency Shutdown"
  162.   running = state == "Charging" or state == "Active"
  163.   safe = state == "Standby" or state == "Cooling"
  164.  
  165.   if state == "Charging" then
  166.     inflow = 200000
  167.  
  168.     if info.temperature > 2000 then
  169.       reactor.activateReactor()
  170.       state = "Active"
  171.     end
  172.   elseif state == "Cooling" then
  173.     if info.temperature < 25 then
  174.       state = "Standby"
  175.     end
  176.     inflow = 10
  177.     outflow = 20
  178.   elseif state == "Standby" then
  179.     inflow = 10
  180.     outflow = 20
  181.   else
  182.     -- adjust inflow rate based on field strength
  183.    
  184.     local field_error = (info.maxFieldStrength * (ideal_strength / 100)) - info.fieldStrength
  185.     local proportional_field_error = field_error * inflow_P_gain
  186.     inflow_I_sum = inflow_I_sum + field_error
  187.     local integral_field_error = inflow_I_sum * inflow_I_gain
  188.     local derivative_field_error = (field_error - inflow_D_last) * inflow_D_gain
  189.     inflow_D_last = field_error
  190.     local inflow_correction = proportional_field_error + integral_field_error + derivative_field_error
  191.     if inflow_correction < 0 then
  192.       inflow_I_sum = inflow_I_sum - field_error
  193.     end
  194.     inflow = inflow_correction
  195.  
  196.     if not shutting_down then
  197.  
  198.       -- adjust outflow rate based on core temperature
  199.  
  200.       local temp_error = ideal_temp - info.temperature
  201.       local proportional_temp_error = temp_error * outflow_P_gain
  202.       outflow_I_sum = outflow_I_sum + temp_error
  203.       local integral_temp_error = outflow_I_sum * outflow_I_gain
  204.       if math.abs(temp_error) < 100 then
  205.         outflow_II_sum = outflow_II_sum + integral_temp_error
  206.       else
  207.         outflow_II_sum = 0
  208.       end
  209.       local second_integral_temp_error = outflow_II_sum * outflow_II_gain
  210.       local derivative_temp_error = (temp_error - outflow_D_last) * outflow_D_gain
  211.       outflow_D_last = temp_error
  212.       local outflow_correction = proportional_temp_error + integral_temp_error + second_integral_temp_error + derivative_temp_error
  213.       if outflow_correction < 0 then
  214.         outflow_I_sum = outflow_I_sum - temp_error
  215.       end
  216.       outflow = outflow_correction
  217.  
  218.       -- cut off reactor in case of emergency
  219.  
  220.       if info.temperature > cutoff_temp then
  221.         print("Reactor temperature greater than", cutoff_temp, ", failsafe activated, shutting down")
  222.         outflow = 0
  223.         state = "Emergency Shutdown"
  224.         reactor.stopReactor()
  225.       end
  226.     else
  227.       if info.temperature < 2000 then
  228.         state = "Cooling"
  229.       end
  230.     end
  231.   end
  232.  
  233.   if state ~= "Active" and not shutting_down then
  234.     inflow_I_sum = 0
  235.     inflow_D_last = 0
  236.     outflow_I_sum = 0
  237.     outflow_II_sum = 0
  238.     outflow_D_last = 0
  239.   end
  240.  
  241.   if inflow < 0 then
  242.     inflow = 0
  243.   end
  244.   if outflow < 0 then
  245.     outflow = 0
  246.   end
  247.  
  248.   inflow = math.floor(inflow*reactorOutputMultiplier)
  249.   outflow = math.floor(outflow*reactorOutputMultiplier)
  250.  
  251.   flux_in.setSignalLowFlow(inflow*reactorOutputMultiplier)
  252.   flux_out.setSignalLowFlow(outflow*reactorOutputMultiplier)
  253.  
  254.   yield()
  255. end
  256.  
  257. term.clear()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement