neuroticfox

test18

Jul 11th, 2021 (edited)
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.57 KB | None | 0 0
  1. local component = require("component")
  2. local event = require("event")
  3. local term = require("term")
  4. local gpu = component.gpu
  5.  
  6. -- Safety Checks
  7.  
  8. if not component.isAvailable("draconic_reactor") then
  9. print("Reactor not connected. Please connect computer to reactor with an Adapter block.")
  10. os.exit()
  11. end
  12. reactor = component.draconic_reactor
  13. local flux_gates = {}
  14. for x,y in pairs(component.list("flux_gate")) do
  15. flux_gates[#flux_gates+1] = x
  16. end
  17. if #flux_gates < 2 then
  18. print("Not enough flux gates connected; please connect inflow and outflow flux gates with Adapter blocks.")
  19. os.exit()
  20. end
  21. flux_in = component.proxy(flux_gates[1])
  22. flux_out = component.proxy(flux_gates[2])
  23. if not flux_in or not flux_out then
  24. print("Not enough flux gates connected; please connect inflow and outflow flux gates with Adapter blocks.")
  25. os.exit()
  26. end
  27.  
  28. -- Functions
  29.  
  30. function exit_msg(msg)
  31. term.clear()
  32. print(msg)
  33. os.exit()
  34. end
  35.  
  36. function modify_temp(offset)
  37. local new_temp = ideal_temp + offset
  38. if new_temp > 8000 then
  39. new_temp = 8000
  40. elseif new_temp < 2500 then
  41. new_temp = 2500
  42. end
  43. ideal_temp = new_temp
  44. end
  45.  
  46. function modify_field(offset)
  47. local new_strength = ideal_strength + offset
  48. if new_strength > 90 then
  49. new_strength = 90
  50. elseif new_strength < 1 then
  51. new_strength = 1
  52. end
  53. ideal_strength = new_strength
  54. end
  55.  
  56. -- Buttons
  57.  
  58. local adj_button_width = 19
  59. local temp_adjust_x_offset = 68
  60. local temp_adjust_y_offset = 2
  61. local field_adjust_x_offset = temp_adjust_x_offset + adj_button_width + 2
  62. local field_adjust_y_offset = 2
  63.  
  64. local buttons = {
  65. start={
  66. x=42,
  67. y=2,
  68. width=24,
  69. height=7,
  70. text="Start",
  71. action=function()
  72. if safe then
  73. state = "Charging"
  74. reactor.chargeReactor()
  75. elseif shutting_down then
  76. state = "Active"
  77. reactor.activateReactor()
  78. end
  79. end,
  80. condition=function() return safe or shutting_down end
  81. },
  82. shutdown={
  83. x=42,
  84. y=12,
  85. width=24,
  86. height=7,
  87. text="Shutdown",
  88. action=function()
  89. state = "Manual Shutdown"
  90. reactor.stopReactor()
  91. end,
  92. condition=function() return running end
  93. },
  94. switch_gates={
  95. x=2,
  96. y=20,
  97. width=24,
  98. height=3,
  99. text="Swap flux gates",
  100. action=function()
  101. local old_addr = flux_in.address
  102. flux_in = component.proxy(flux_out.address)
  103. flux_out = component.proxy(old_addr)
  104. end,
  105. condition=function() return safe end
  106. },
  107. exit={
  108. x=42,
  109. y=12,
  110. width=24,
  111. height=7,
  112. text="Exit",
  113. action=function()
  114. event_loop = false
  115. end,
  116. condition=function() return safe end
  117. },
  118. temp_up_thousand={
  119. x=temp_adjust_x_offset,
  120. y=temp_adjust_y_offset,
  121. width=adj_button_width,
  122. height=1,
  123. text="+ 1000",
  124. action=function() modify_temp(1000) end
  125. },
  126. temp_up_hundred={
  127. x=temp_adjust_x_offset,
  128. y=temp_adjust_y_offset+2,
  129. width=adj_button_width,
  130. height=1,
  131. text="+ 100",
  132. action=function() modify_temp(100) end
  133. },
  134. temp_up_ten={
  135. x=temp_adjust_x_offset,
  136. y=temp_adjust_y_offset+4,
  137. width=adj_button_width,
  138. height=1,
  139. text="+ 10",
  140. action=function() modify_temp(10) end
  141. },
  142. temp_up_one={
  143. x=temp_adjust_x_offset,
  144. y=temp_adjust_y_offset+6,
  145. width=adj_button_width,
  146. height=1,
  147. text="+ 1",
  148. action=function() modify_temp(1) end
  149. },
  150. temp_down_thousand={
  151. x=temp_adjust_x_offset,
  152. y=temp_adjust_y_offset+16,
  153. width=adj_button_width,
  154. height=1,
  155. text="- 1000",
  156. action=function() modify_temp(-1000) end
  157. },
  158. temp_down_hundred={
  159. x=temp_adjust_x_offset,
  160. y=temp_adjust_y_offset+14,
  161. width=adj_button_width,
  162. height=1,
  163. text=" - 100",
  164. action=function() modify_temp(-100) end
  165. },
  166. temp_down_ten={
  167. x=temp_adjust_x_offset,
  168. y=temp_adjust_y_offset+12,
  169. width=adj_button_width,
  170. height=1,
  171. text="- 10",
  172. action=function() modify_temp(-10) end
  173. },
  174. temp_down_one={
  175. x=temp_adjust_x_offset,
  176. y=temp_adjust_y_offset+10,
  177. width=adj_button_width,
  178. height=1,
  179. text="- 1",
  180. action=function() modify_temp(-1) end
  181. },
  182. field_up_ten={
  183. x=field_adjust_x_offset,
  184. y=field_adjust_y_offset+4,
  185. width=adj_button_width,
  186. height=1,
  187. text="+ 10",
  188. action=function() modify_field(10) end
  189. },
  190. field_up_one={
  191. x=field_adjust_x_offset,
  192. y=field_adjust_y_offset+6,
  193. width=adj_button_width,
  194. height=1,
  195. text="+ 1",
  196. action=function() modify_field(1) end
  197. },
  198. field_down_ten={
  199. x=field_adjust_x_offset,
  200. y=field_adjust_y_offset+12,
  201. width=adj_button_width,
  202. height=1,
  203. text="- 10",
  204. action=function() modify_field(-10) end
  205. },
  206. field_down_one={
  207. x=field_adjust_x_offset,
  208. y=field_adjust_y_offset+10,
  209. width=adj_button_width,
  210. height=1,
  211. text="- 1",
  212. action=function() modify_field(-1) end
  213. }
  214. field_down_tenth={
  215. x=field_adjust_x_offset,
  216. y=field_adjust_y_offset+8,
  217. width=adj_button_width,
  218. height=1,
  219. text="- 0.1",
  220. action=function() modify_field(-0.1) end
  221. }
  222. }
  223.  
  224. -- main code
  225.  
  226. flux_in.setFlowOverride(0)
  227. flux_out.setFlowOverride(0)
  228. flux_in.setOverrideEnabled(true)
  229. flux_out.setOverrideEnabled(true)
  230.  
  231. local condition = reactor.getReactorInfo()
  232. if not condition then
  233. print("Reactor not initialized, please ensure the stabilizers are properly laid out.")
  234. os.exit()
  235. end
  236.  
  237. ideal_strength = 50
  238.  
  239. ideal_temp = 8000
  240. cutoff_temp = 8100
  241.  
  242. -- tweakable pid gains
  243.  
  244. inflow_P_gain = 1
  245. inflow_I_gain = 0.04
  246. inflow_D_gain = 0.1
  247.  
  248. outflow_P_gain = 500
  249. outflow_I_gain = 0.5
  250. outflow_II_gain = 0.0000003
  251. outflow_D_gain = 60000
  252.  
  253. -- initialize main loop
  254.  
  255. inflow_I_sum = 0
  256. inflow_D_last = 0
  257.  
  258. outflow_I_sum = 0
  259. outflow_II_sum = 0
  260. outflow_D_last = 0
  261.  
  262. state = "Standby"
  263. shutting_down = false
  264.  
  265. if condition.temperature > 25 then
  266. state = "Cooling"
  267. end
  268. if condition.temperature > 2000 then
  269. state = "Active"
  270. end
  271.  
  272. -- Possible states:
  273. --Standby
  274. --Charging
  275. --Active
  276. --Manual Shutdown
  277. --Emergency Shutdown
  278. --Cooling
  279.  
  280. event_loop = true
  281. while event_loop do
  282.  
  283. if not component.isAvailable("draconic_reactor") then
  284. exit_msg("Reactor disconnected, exiting")
  285. end
  286.  
  287. if not component.isAvailable("flux_gate") then
  288. exit_msg("Flux gates disconnected, exiting")
  289. end
  290.  
  291. local info = reactor.getReactorInfo()
  292.  
  293. local inflow = 0
  294. local outflow = 0
  295.  
  296. shutting_down = state == "Manual Shutdown" or state == "Emergency Shutdown"
  297. running = state == "Charging" or state == "Active"
  298. safe = state == "Standby" or state == "Cooling"
  299.  
  300. if state == "Charging" then
  301. inflow = 200000
  302.  
  303. if info.temperature > 2000 then
  304. reactor.activateReactor()
  305. state = "Active"
  306. end
  307. elseif state == "Cooling" then
  308. if info.temperature < 25 then
  309. state = "Standby"
  310. end
  311. inflow = 10
  312. outflow = 20
  313. elseif state == "Standby" then
  314. inflow = 10
  315. outflow = 20
  316. else
  317. -- adjust inflow rate based on field strength
  318.  
  319. local field_error = (info.maxFieldStrength * (ideal_strength / 100)) - info.fieldStrength
  320. local proportional_field_error = field_error * inflow_P_gain
  321. inflow_I_sum = inflow_I_sum + field_error
  322. local integral_field_error = inflow_I_sum * inflow_I_gain
  323. local derivative_field_error = (field_error - inflow_D_last) * inflow_D_gain
  324. inflow_D_last = field_error
  325. local inflow_correction = proportional_field_error + integral_field_error + derivative_field_error
  326. if inflow_correction < 0 then
  327. inflow_I_sum = inflow_I_sum - field_error
  328. end
  329. inflow = inflow_correction
  330.  
  331. if not shutting_down then
  332.  
  333. -- adjust outflow rate based on core temperature
  334.  
  335. local temp_error = ideal_temp - info.temperature
  336. local proportional_temp_error = temp_error * outflow_P_gain
  337. outflow_I_sum = outflow_I_sum + temp_error
  338. local integral_temp_error = outflow_I_sum * outflow_I_gain
  339. if math.abs(temp_error) < 100 then
  340. outflow_II_sum = outflow_II_sum + integral_temp_error
  341. else
  342. outflow_II_sum = 0
  343. end
  344. local second_integral_temp_error = outflow_II_sum * outflow_II_gain
  345. local derivative_temp_error = (temp_error - outflow_D_last) * outflow_D_gain
  346. outflow_D_last = temp_error
  347. local outflow_correction = proportional_temp_error + integral_temp_error + second_integral_temp_error + derivative_temp_error
  348. if outflow_correction < 0 then
  349. outflow_I_sum = outflow_I_sum - temp_error
  350. end
  351. outflow = outflow_correction
  352.  
  353. -- cut off reactor in case of emergency
  354.  
  355. if info.temperature > cutoff_temp then
  356. print("Reactor temperature greater than", cutoff_temp, ", failsafe activated, shutting down")
  357. outflow = 0
  358. state = "Emergency Shutdown"
  359. reactor.stopReactor()
  360. end
  361. else
  362. if info.temperature < 2000 then
  363. state = "Cooling"
  364. end
  365. end
  366. end
  367.  
  368. if state ~= "Active" and not shutting_down then
  369. inflow_I_sum = 0
  370. inflow_D_last = 0
  371. outflow_I_sum = 0
  372. outflow_II_sum = 0
  373. outflow_D_last = 0
  374. end
  375.  
  376. if inflow < 0 then
  377. inflow = 0
  378. end
  379. if outflow < 0 then
  380. outflow = 0
  381. end
  382.  
  383. inflow = math.floor(inflow)
  384. outflow = math.floor(outflow)
  385.  
  386. flux_in.setFlowOverride(inflow)
  387. flux_out.setFlowOverride(outflow)
  388.  
  389. -- Draw screen
  390.  
  391. if term.isAvailable() then
  392.  
  393. -- Draw Values
  394.  
  395. local left_margin = 2
  396. local spacing = 2
  397.  
  398. local values = {
  399. "Status: " .. state,
  400. "Field Strength: " .. ((info.fieldStrength / info.maxFieldStrength) * 100) .. "%",
  401. "Energy Saturation: " .. ((info.energySaturation / info.maxEnergySaturation) * 100) .. "%",
  402. "Fuel Concentration: " .. ((1 - info.fuelConversion / info.maxFuelConversion) * 100) .. "%",
  403. "Temperature: " .. info.temperature .. " F",
  404. "Reactor Efficiency: " .. info.fuelConversionRate .. " nb/t",
  405. "Energy Inflow Rate: " .. inflow .. " RF/t",
  406. "Energy Outflow Rate: " .. outflow .. " RF/t"
  407. }
  408.  
  409. if safe then
  410. values[#values+1] = "Click if flux gates need to be swapped"
  411. end
  412.  
  413. term.clear()
  414.  
  415. for i, v in ipairs(values) do
  416. term.setCursor(left_margin, i * spacing)
  417. term.write(v)
  418. end
  419.  
  420. -- Draw button values
  421.  
  422. term.setCursor(temp_adjust_x_offset, temp_adjust_y_offset+8)
  423. term.write("Target Temp: " .. ideal_temp .. " F")
  424. term.setCursor(field_adjust_x_offset, field_adjust_y_offset+8)
  425.  
  426.  
  427. -- Draw Buttons
  428.  
  429. gpu.setForeground(0x000000)
  430.  
  431. for bname, button in pairs(buttons) do
  432. if button.depressed then
  433.  
  434. button.depressed = button.depressed - 1
  435. if button.depressed == 0 then
  436. button.depressed = nil
  437. end
  438. end
  439. if button.condition == nil or button.condition() then
  440. local center_color = 0xAAAAAA
  441. local highlight_color = 0xCCCCCC
  442. local lowlight_color = 0x808080
  443. if button.depressed then
  444. center_color = 0x999999
  445. highlight_color = 0x707070
  446. lowlight_color = 0xBBBBBB
  447. end
  448. gpu.setBackground(center_color)
  449. gpu.fill(button.x, button.y, button.width, button.height, " ")
  450. if button.width > 1 and button.height > 1 then
  451. gpu.setBackground(lowlight_color)
  452. gpu.fill(button.x+1, button.y+button.height-1, button.width-1, 1, " ")
  453. gpu.fill(button.x+button.width-1, button.y, 1, button.height, " ")
  454. gpu.setBackground(highlight_color)
  455. gpu.fill(button.x, button.y, 1, button.height, " ")
  456. gpu.fill(button.x, button.y, button.width, 1, " ")
  457. end
  458. gpu.setBackground(center_color)
  459. term.setCursor(button.x + math.floor(button.width / 2 - #button.text / 2), button.y + math.floor(button.height / 2))
  460. term.write(button.text)
  461. end
  462. end
  463.  
  464. gpu.setBackground(0x000000)
  465. gpu.setForeground(0xFFFFFF)
  466. end
  467.  
  468. -- Wait for next tick, or manual shutdown
  469.  
  470. local event, id, op1, op2 = event.pull(0.05)
  471. if event == "interrupted" then
  472. if safe then
  473. break
  474. end
  475. elseif event == "touch" then
  476.  
  477. -- Handle Button Presses
  478.  
  479. local x = op1
  480. local y = op2
  481.  
  482. for bname, button in pairs(buttons) do
  483. if (button.condition == nil or button.condition()) and x >= button.x and x <= button.x + button.width and y >= button.y and y <= button.y + button.height then
  484. button.action()
  485. button.depressed = 3
  486. end
  487. end
  488. end
  489. end
  490.  
  491. term.clear()
Add Comment
Please, Sign In to add comment