Advertisement
Guest User

Untitled

a guest
Nov 5th, 2024
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.21 KB | None | 0 0
  1. local c = require("component")
  2. local side = require("sides")
  3. local co = require("computer")
  4.  
  5. --Edit these for your i/o locations
  6. local coolantBox = side.east
  7. local fuelBox = side.west
  8. local outputBox = side.north
  9. local reactorSide = side.south
  10.  
  11. --Name of your coolant and fuel
  12. local coolantType = "gregtech:gt.360k_NaK_Coolantcell"
  13. local fuelType = "gregtech:gt.reactorMOXQuad"
  14. local depletedFuelType = "IC2:reactorMOXQuaddepleted"
  15.  
  16. --Coolant heat replacement threshold in %
  17. local heatLevel = 15
  18.  
  19. --Don't edit anything past here
  20. local rs = c.redstone
  21. local ts = c.transposer
  22. local gpu = c.gpu
  23.  
  24. local coolant = {}
  25. local fuel = {}
  26. local nukeDesign = {
  27. 0001, 2048, 2048, 2048, 0001, 2048, 2048, 0001, 2048,
  28. 2048, 2048, 0001, 2048, 2048, 2048, 2048, 0001, 2048,
  29. 0001, 2048, 2048, 2048, 2048, 0001, 2048, 2048, 2048,
  30. 2048, 2048, 2048, 0001, 2048, 2048, 2048, 2048, 0001,
  31. 2048, 0001, 2048, 2048, 2048, 2048, 0001, 2048, 2048,
  32. 2048, 0001, 2048, 2048, 0001, 2048, 2048, 2048, 0001
  33. }
  34.  
  35. local processedDesign = {}
  36. for i,j in ipairs(nukeDesign) do table.insert(processedDesign, {j, 0}) end
  37.  
  38. function tprint (tbl, indent)
  39. if not indent then indent = 0 end
  40. local toprint = string.rep(" ", indent) .. "{\r\n"
  41. indent = indent + 2
  42. for k, v in pairs(tbl) do
  43. toprint = toprint .. string.rep(" ", indent)
  44. if (type(k) == "number") then
  45. toprint = toprint .. "[" .. k .. "] = "
  46. elseif (type(k) == "string") then
  47. toprint = toprint .. k .. "= "
  48. end
  49. if (type(v) == "number") then
  50. toprint = toprint .. v .. ",\r\n"
  51. elseif (type(v) == "string") then
  52. toprint = toprint .. "\"" .. v .. "\",\r\n"
  53. elseif (type(v) == "table") then
  54. toprint = toprint .. tprint(v, indent + 2) .. ",\r\n"
  55. else
  56. toprint = toprint .. "\"" .. tostring(v) .. "\",\r\n"
  57. end
  58. end
  59. toprint = toprint .. string.rep(" ", indent-2) .. "}"
  60. return toprint
  61. end
  62.  
  63.  
  64. function watchdog()
  65. rs.setOutput(side.down, 15)
  66. os.sleep(.05)
  67. rs.setOutput(side.down, 0)
  68. rs.setOutput(side.west, 0)
  69. end
  70.  
  71. function resetWatchdog()
  72. rs.setOutput(side.east, 0)
  73. rs.setOutput(side.west, 15)
  74. end
  75.  
  76. function checkDesign()
  77. local design = {}
  78. for i in ts.getAllStacks(side.south) do
  79.  
  80. if i.name == coolantType then
  81. table.insert(design, {1, 100 * i.damage / i.maxDamage})
  82. elseif i.name == fuelType then
  83. table.insert(design, {2048, 100 * i.damage / i.maxDamage})
  84. elseif i.name == depletedFuelType then
  85. table.insert(design, {4096, 0})
  86. elseif i.name == nil then
  87. table.insert(design, {0, 0})
  88. else
  89. table.insert(design, {-1, 0})
  90. end
  91. end
  92. return design
  93. end
  94.  
  95. function printDesign(design)
  96. if #design ~= 54 then
  97. print("Invalid design!")
  98. return
  99. end
  100.  
  101. local w, h = gpu.getResolution()
  102. gpu.set(1, 1, "Durability of components:")
  103. for j, i in ipairs(design) do
  104. local text = " "
  105. i[2] = math.ceil(i[2])
  106. if i[1] == -1 then --Unknown item
  107. gpu.setBackground(0xFF0000)
  108. gpu.setForeground(0x000000)
  109. text = "NUL"
  110. elseif i[1] == 0 then --Blank space
  111. gpu.setBackground(0x1E1E1E)
  112. gpu.setForeground(0x3C3C3C)
  113. text = "BLN"
  114. elseif i[1] == 1 then --Coolant cell
  115. gpu.setBackground(0x0049FF)
  116. gpu.setForeground(0xFFFFFF)
  117. text = getText(i[2])
  118. elseif i[1] == 2048 then --Fuel
  119. gpu.setBackground(0x00FF00)
  120. gpu.setForeground(0x000000)
  121. text = getText(i[2])
  122. elseif i[1] == 4096 then --Depleted rod
  123. gpu.setBackground(0x006D00)
  124. gpu.setForeground(0xFFFFFF)
  125. text = "DEP"
  126. else --Some invalid state
  127. gpu.setBackground(0xFF00FF)
  128. end
  129.  
  130. gpu.set((((j - 1) % 9) * 3) + 1, (math.ceil(j / 9) + 1), text)
  131. gpu.setBackground(0x000000)
  132. gpu.setForeground(0xFFFFFF)
  133. end
  134. end
  135.  
  136. function getText(value)
  137. local text = ""
  138. local value = 100 - value
  139. if value == 100 then
  140. text = "100"
  141. elseif value < 10 then
  142. gpu.setForeground(0xFF0000)
  143. text = "0" .. value .. "%"
  144. else
  145. text = value .. "%"
  146. end
  147. return text
  148. end
  149.  
  150. function initReactor()
  151. for i,j in ipairs(processedDesign) do
  152. if j[1] == 1 then
  153. table.insert(coolant, i)
  154. elseif j[1] == 2048 then
  155. table.insert(fuel, i)
  156. end
  157. end
  158. local w, h = gpu.getResolution()
  159. gpu.fill(1, 1, w, h, " ")
  160. end
  161.  
  162. function checkCoolant()
  163. local design = checkDesign()
  164. local validator = true
  165. --hotCells = {}
  166. gpu.set(1, 9, "Coolant Status:")
  167.  
  168. for i,j in ipairs(coolant) do
  169. local text = "BAD" --init to BAD state
  170. gpu.setBackground(0xFF0000)
  171. gpu.setForeground(0xFFFFFF)
  172. k = design[j][1]
  173. l = 100 - design[j][2]
  174.  
  175. if k == 1 and l > heatLevel then --if we have coolant we're happy
  176. gpu.setBackground(0x00FF00)
  177. gpu.setForeground(0x000000)
  178. text = "OK!"
  179. elseif k == 1 and l <= heatLevel then
  180. while rs.getInput(side.north) > 0 do
  181. rs.setOutput(side.east, 0)
  182. os.sleep(1.1)
  183. end
  184. gpu.setBackground(0xFFB600)
  185. gpu.setForeground(0x000000)
  186. text = "HOT"
  187. --table.insert(hotCells, j)
  188. validator = false --if the cell is too hot don't run reactor
  189. else
  190. while rs.getInput(side.north) > 0 do
  191. rs.setOutput(side.east, 0)
  192. os.sleep(1.1)
  193. end
  194. validator = false --if there's not coolant don't run reactor
  195. end
  196.  
  197. gpu.set((((j - 1) % 9) * 3) + 1, (math.ceil(j / 9) + 9), text)
  198. end
  199. gpu.setBackground(0x000000)
  200. gpu.setForeground(0xFFFFFF)
  201. return validator
  202. end
  203.  
  204. function replaceFuel()
  205. local design = checkDesign()
  206. local firstValidInputStack = 0
  207. local firstValidOutputStack = 0
  208. local fuelAmount = 0
  209.  
  210. local counter = 1
  211. for i in ts.getAllStacks(fuelBox) do
  212. if i.name == fuelType then
  213. firstValidInputStack = counter
  214. fuelAmount = i.size
  215. break
  216. counter = counter + 1
  217. end
  218. end
  219.  
  220. local counter = 1
  221. for i in ts.getAllStacks(outputBox) do
  222. if i.name == nil then
  223. firstValidOutputStack = counter
  224. break
  225. counter = counter + 1
  226. end
  227. end
  228.  
  229. if firstValidOutputStack == 0 then
  230. gpu.set(1, 17, "Output is full! Cannot remove depleted fuel.")
  231. else
  232. gpu.set(1, 17, " ")
  233. end
  234.  
  235. if firstValidInputStack == 0 then
  236. gpu.set(1, 18, "Fuel input is empty! Cannot insert fresh fuel.")
  237. else
  238. gpu.set(1, 18, " ")
  239. end
  240.  
  241. for i, j in ipairs(fuel) do
  242. k = design[j][1]
  243.  
  244. if k == 4096 and firstValidOutputStack ~= 0 then
  245. ts.transferItem(reactorSide, outputBox, 1, j, firstValidOutputStack)
  246. if firstValidInputStack ~= 0 and fuelAmount > 0 then
  247. ts.transferItem(fuelBox, reactorSide, 1, firstValidInputStack, j)
  248. fuelAmount = fuelAmount - 1
  249. if fuelAmount == 0 then break end
  250. end
  251. elseif k == 0 and firstValidInputStack ~= 0 and fuelAmount > 0 then
  252. ts.transferItem(fuelBox, reactorSide, 1, firstValidInputStack, j)
  253. fuelAmount = fuelAmount - 1
  254. if fuelAmount == 0 then break end
  255. end
  256. end
  257. return design
  258. end
  259.  
  260. function replaceCoolant()
  261. local safe = true
  262. local design = checkDesign()
  263. local firstValidOutputStack = 0
  264. local coolantSlots = {}
  265. local outputSlots = {}
  266.  
  267. local counter = 1
  268. for i in ts.getAllStacks(outputBox) do
  269. if i.name == nil then
  270. table.insert(outputSlots, counter)
  271. end
  272. counter = counter + 1
  273. end
  274.  
  275. local counter = 1
  276. for i in ts.getAllStacks(coolantBox) do
  277. if i.name == coolantType then
  278. table.insert(coolantSlots, counter)
  279. end
  280. counter = counter + 1
  281. end
  282.  
  283. if #outputSlots == 0 then
  284. gpu.set(1, 19, "No coolant output available! Reactor may stop.")
  285. elseif #outputSlots > 0 then
  286. gpu.set(1, 19, " ")
  287. end
  288.  
  289. if #coolantSlots == 0 then
  290. gpu.set(1, 20, "No fresh coolant available! Reactor may stop.")
  291. elseif #coolantSlots > 0 then
  292. gpu.set(1, 20, " ")
  293. end
  294.  
  295. for i, j in ipairs(coolant) do
  296. k = design[j][1]
  297. l = 100 - design[j][2]
  298.  
  299. if k == 0 and #coolantSlots > 0 then
  300. rs.setOutput(side.east, 0)
  301. if(rs.getInput(side.north) == 0) then
  302. ts.transferItem(coolantBox, reactorSide, 1, coolantSlots[1], j)
  303. table.remove(coolantSlots, 1)
  304. end
  305. safe = false
  306.  
  307. elseif k == 1 and l <= heatLevel and #outputSlots > 0 then
  308. while rs.getInput(side.north) > 0 do
  309. rs.setOutput(side.east, 0)
  310. os.sleep(.2)
  311. end
  312. ts.transferItem(reactorSide, outputBox, 1, j, outputSlots[1])
  313. table.remove(outputSlots, 1)
  314. safe = false
  315. end
  316. end
  317.  
  318. return safe
  319. end
  320.  
  321. function freeMem()
  322. local max = 0
  323. for i=1,10 do
  324. max = math.max(max, co.freeMemory())
  325. os.sleep(0)
  326. end
  327. gpu.set(32, 6, "Memory:")
  328. gpu.set(32, 7, tostring(max))
  329. end
  330.  
  331. gpu.setResolution(50, 20)
  332. initReactor()
  333. replaceCoolant()
  334. checkCoolant()
  335. replaceFuel()
  336. printDesign(checkDesign())
  337. resetWatchdog()
  338.  
  339. while true do
  340. local shouldReactorRun = false
  341. a = checkCoolant()
  342. b = replaceCoolant()
  343. shouldReactorRun = a and b
  344.  
  345. local design = replaceFuel()
  346. printDesign(design)
  347.  
  348. gpu.setBackground(0x000000)
  349. gpu.setForeground(0xFFFFFF)
  350. gpu.set(32, 1, "Status:")
  351.  
  352. if rs.getInput(side.top) == 0 then
  353. shouldReactorRun = false
  354. gpu.setBackground(0XFFFF00)
  355. gpu.set(32, 3, " ")
  356. gpu.setBackground(0xFF0000)
  357. gpu.setForeground(0x000000)
  358. gpu.set(34, 3, "ESTOP")
  359. end
  360.  
  361. if shouldReactorRun then
  362. rs.setOutput(side.east, 15)
  363. gpu.setBackground(0x00FF00)
  364. gpu.setForeground(0x000000)
  365. gpu.set(32, 2, " WORKING ")
  366. gpu.setBackground(0x000000)
  367. gpu.set(32, 3, " ")
  368. else
  369. rs.setOutput(side.east, 0)
  370. gpu.setBackground(0xFF0000)
  371. gpu.setForeground(0x000000)
  372. gpu.set(32, 2, " STOPPED ")
  373. end
  374.  
  375. gpu.setBackground(0x000000)
  376. gpu.setForeground(0xFFFFFF)
  377.  
  378. watchdog()
  379. if (rs.getInput(side.west)) == 0 then
  380. resetWatchdog()
  381. else
  382. os.sleep(.05)
  383. end
  384.  
  385. freeMem()
  386. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement