zodiak707

test

Jun 19th, 2022 (edited)
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.12 KB | None | 0 0
  1. local filePath = "basalt.lua"
  2. if not(fs.exists(filePath))then
  3. shell.run("wget https://raw.githubusercontent.com/Pyroxenium/Basalt/master/basalt.lua "..filePath)
  4. end
  5. local basalt = dofile(filePath)
  6.  
  7. --value init start
  8. local reactor = peripheral.wrap("back")
  9. local energyStored = reactor.battery().stored()
  10. local energyCapacity = reactor.battery().capacity()
  11. local energyPerTick = 0
  12. local fuelBurnedLastTick = 0
  13. local fuelCapacity = reactor.fuelTank().capacity()
  14. local fuelStored = 0
  15. local fuelReactivity = 0
  16. local fuelReactant = 0
  17. local fuelWaste = 0
  18. local controlRodCount = reactor.controlRodCount()
  19.  
  20. local reactorRunning = false
  21. local burnTimerSeconds = 0
  22. local oldEnergyStored = energyStored
  23. local IOValue = {}
  24.  
  25. local isAutoModeEnabled = false
  26. local isManuelEnabled = false
  27. local fuelRodLevel = 100
  28. local startThreshold = 0
  29. local stopThreshold = 0
  30. --value init end
  31.  
  32. --settings start
  33. function saveSettings(var, value)
  34. settings.set(var, value)
  35. settings.save("reactor.settings")
  36. end
  37.  
  38. function save()
  39. saveSettings("isAutoModeEnabled", isAutoModeEnabled)
  40. saveSettings("isManuelEnabled", isManuelEnabled)
  41. saveSettings("fuelRodLevel", fuelRodLevel)
  42. saveSettings("startThreshold", startThreshold)
  43. saveSettings("stopThreshold", stopThreshold)
  44. end
  45.  
  46. if not(fs.exists("reactor.settings"))then
  47. save()
  48. end
  49.  
  50. function loadSettings(var)
  51. settings.load("reactor.settings")
  52. return settings.get(var)
  53. end
  54.  
  55. isAutoModeEnabled = loadSettings("isAutoModeEnabled")
  56. isManuelEnabled = loadSettings("isManuelEnabled")
  57. fuelRodLevel = loadSettings("fuelRodLevel")
  58. startThreshold = loadSettings("startThreshold")
  59. stopThreshold = loadSettings("stopThreshold")
  60. --settings end
  61.  
  62. --function to round falues on x decimal places
  63. function round(num, idp)
  64. local mult = 10^(idp or 0)
  65. return math.floor(num * mult + 0.5) / mult
  66. end
  67.  
  68. --function to convert seconds to time
  69. function SecondsToTime(seconds)
  70. if seconds <= 0 then
  71. return "0:00:00";
  72. else
  73. hours = math.floor(seconds/3600);
  74. mins = math.floor(seconds/60 - (hours*60));
  75. secs = math.floor(seconds - hours*3600 - mins*60);
  76. return hours..":"..mins..":"..secs
  77. end
  78. end
  79.  
  80. --get width and height of terminal
  81. local w, h = term.getSize()
  82.  
  83. --init frames start
  84. local main = basalt.createFrame("mainFrame"):show()
  85. local homeFrame = main:addFrame("homeFrame"):setPosition(1,2):setBackground(colors.lightGray):setSize(w, h-1):show()
  86. local fuelRodsFrame = main:addFrame("fuelRodsFrame"):setPosition(1,2):setBackground(colors.lightGray):setSize(w, h-1)
  87. local mainThread = main:addThread("mainThread")
  88.  
  89. local menuBar = main:addMenubar("mainMenuBar"):addItem("Overview"):addItem("Settings"):setBackground(colors.gray):setSize(w, 1):setSpace(4):show()
  90. menuBar:onChange(function(self)
  91. homeFrame:hide()
  92. fuelRodsFrame:hide()
  93. if(self:getValue().text=="Overview")then
  94. homeFrame:show()
  95. elseif(self:getValue().text=="Settings")then
  96. fuelRodsFrame:show()
  97. end
  98. end)
  99. --init frames end
  100.  
  101. --totaly awesome button animation start
  102. local function visualButton(btn)
  103. btn:onClick(function(self) btn:setBackground(colors.black) btn:setForeground(colors.lightGray) end)
  104. btn:onClickUp(function(self) btn:setBackground(colors.gray) btn:setForeground(colors.black) end)
  105. btn:onLoseFocus(function(self) btn:setBackground(colors.gray) btn:setForeground(colors.black) end)
  106. end
  107. --totaly awesome button animation end
  108.  
  109. --Overview start
  110. homeFrame:addLabel("energyLabel"):setText("Energy: "):setPosition(2,2):show()
  111. homeFrame:addLabel("energyStoredLabel"):setText("FE Stored: "):setPosition(3,3):show()
  112. energyStorage = homeFrame:addLabel("energyStoredValLabel"):setText(energyStored.." / "..energyCapacity.." FE"):setPosition(19,3):show()
  113. homeFrame:addLabel("energyProductionLabel"):setText("FE Pruduction: "):setPosition(3,4):show()
  114. energyTick = homeFrame:addLabel("energyProductionValLabel"):setText("0 FE/t"):setPosition(19,4):show()
  115. homeFrame:addLabel("energyIOLabel"):setText("Energy I/O: "):setPosition(3,5):show()
  116. ValueIOTick = homeFrame:addLabel("energyIOValLabel"):setText(0):setPosition(19,5):show()
  117. homeFrame:addLabel("fuelLabel"):setText("Fuel: "):setPosition(2,7):show()
  118. homeFrame:addLabel("fuelStoredLabel"):setText("Fuel Stored: "):setPosition(3,8):show()
  119. fuelStorage = homeFrame:addLabel("fuelStoredValLabel"):setText(fuelStored.." / "..fuelCapacity.." mB"):setPosition(19,8):show()
  120. homeFrame:addLabel("wasteStoredLabel"):setText("Waste Stored: "):setPosition(3,9):show()
  121. wasteStorage = homeFrame:addLabel("wasteStoredValLabel"):setText(fuelWaste.." / "..fuelCapacity.." mB"):setPosition(19,9):show()
  122. homeFrame:addLabel("fuelUsedLabel"):setText("Fuel Burned: "):setPosition(3,10):show()
  123. fuelUsed = homeFrame:addLabel("fuelUsedValLabel"):setText(fuelBurnedLastTick.." mB/t"):setPosition(19,10):show()
  124. homeFrame:addLabel("fuelUsedTimeLabel"):setText("Time per Ingot: "):setPosition(3,11):show()
  125. fuelUsedTime = homeFrame:addLabel("fuelUsedTimeValLabel"):setText(0):setPosition(19,11):show()
  126. homeFrame:addLabel("fuelBurnedTimeLabel"):setText("Ingot burned: "):setPosition(3,12):show()
  127. fuelBurnedTime = homeFrame:addLabel("fuelBurnedTimeValLabel"):setText(0):setPosition(19,12):show()
  128. homeFrame:addLabel("benchmarkLabel"):setText("Benchmark: "):setPosition(3,13):show()
  129. fuelBenchmark = homeFrame:addLabel("benchmarkValLabel"):setText(0):setPosition(19,13):show()
  130. homeFrame:addLabel("statusLabel"):setText("Status: "):setPosition(2,15):show()
  131. homeFrame:addLabel("activeLabel"):setText("Active: "):setPosition(3,16):show()
  132. activeLabel = homeFrame:addLabel("activeValLabel"):setText(0):setPosition(19,16):show()
  133. homeFrame:addLabel("autoModeLabel"):setText("Auto Mode: "):setPosition(3,17):show()
  134. autoModeLabel = homeFrame:addLabel("autoModeValLabel"):setText(0):setPosition(19,17):show()
  135. btnActivate = homeFrame:addButton("btnActivate"):setText("Activate"):setAnchor("right"):setSize(11,3):setPosition(2,2):onClick(function()
  136. reactor.setActive(not reactor.active())
  137. end):show()
  138. btnAutoMode = homeFrame:addButton("btnAutoMode"):setText("Auto Mode"):setAnchor("right"):setSize(11,3):setPosition(2,6):onClick(function()
  139. isManuelEnabled = false
  140. isAutoModeEnabled = not isAutoModeEnabled
  141. save()
  142. end):show()
  143. btnManuel = homeFrame:addButton("btnManuel"):setText("Manuel"):setAnchor("right"):setSize(11,3):setPosition(2,10):onClick(function()
  144. isAutoModeEnabled = false
  145. isManuelEnabled = not isManuelEnabled
  146. save()
  147. end):show()
  148. --Overview end
  149.  
  150. --Settings start
  151. fuelRodsFrame:addLabel("powerLabel"):setText("Fuelrod Level:"):setPosition(2,2):show()
  152. powerValLabel = fuelRodsFrame:addLabel("powerValLabel"):setText("0%"):setPosition(19,3):show()
  153. visualButton(fuelRodsFrame:addButton("rodLevelPlus1"):setText("+1"):setSize(2,1):setPosition(30,3):onClick(function()
  154. if((fuelRodLevel+1) < 100) then
  155. fuelRodLevel = fuelRodLevel+1
  156. else
  157. fuelRodLevel = 100
  158. end
  159. save()
  160. end):show())
  161. visualButton(fuelRodsFrame:addButton("rodLevelPlus5"):setText("+5"):setSize(2,1):setPosition(33,3):onClick(function()
  162. if((fuelRodLevel+5) < 100) then
  163. fuelRodLevel = fuelRodLevel+5
  164. else
  165. fuelRodLevel = 100
  166. end
  167. save()
  168. end):show())
  169. visualButton(fuelRodsFrame:addButton("rodLevelPlus19"):setText("+10"):setSize(3,1):setPosition(36,3):onClick(function()
  170. if((fuelRodLevel+10) < 100) then
  171. fuelRodLevel = fuelRodLevel+10
  172. else
  173. fuelRodLevel = 100
  174. end
  175. save()
  176. end):show())
  177. rodLevel = fuelRodsFrame:addProgressbar("rodLevelProgressbar"):setSize(16,1):setPosition(13,3):show()
  178. visualButton(fuelRodsFrame:addButton("rodLevelMinus1"):setText("-1"):setSize(2,1):setPosition(3,3):onClick(function()
  179. if((fuelRodLevel-1) > 0) then
  180. fuelRodLevel = fuelRodLevel-1
  181. else
  182. fuelRodLevel = 0
  183. end
  184. save()
  185. end):show())
  186. visualButton(fuelRodsFrame:addButton("rodLevelMinus5"):setText("-5"):setSize(2,1):setPosition(6,3):onClick(function()
  187. if((fuelRodLevel-5) > 0) then
  188. fuelRodLevel = fuelRodLevel-5
  189. else
  190. fuelRodLevel = 0
  191. end
  192. save()
  193. end):show())
  194. visualButton(fuelRodsFrame:addButton("rodLevelMinus19"):setText("-10"):setSize(3,1):setPosition(9,3):onClick(function()
  195. if((fuelRodLevel-10) > 0) then
  196. fuelRodLevel = fuelRodLevel-10
  197. else
  198. fuelRodLevel = 0
  199. end
  200. save()
  201. end):show())
  202. fuelRodsFrame:addLabel("startThresholdLabel"):setText("Start Threshold:"):setPosition(2,5):show()
  203. startThresholdValLabel = fuelRodsFrame:addLabel("startThresholdValLabel"):setText("0%"):setPosition(19,6):show()
  204. visualButton(fuelRodsFrame:addButton("startThresholdLevelPlus1"):setText("+1"):setSize(2,1):setPosition(30,6):onClick(function()
  205. if((startThreshold+1) < 100) then
  206. startThreshold = startThreshold+1
  207. else
  208. startThreshold = 100
  209. end
  210. save()
  211. end):show())
  212. visualButton(fuelRodsFrame:addButton("startThresholdLevelPlus5"):setText("+5"):setSize(2,1):setPosition(33,6):onClick(function()
  213. if((startThreshold+5) < 100) then
  214. startThreshold = startThreshold+5
  215. else
  216. startThreshold = 100
  217. end
  218. save()
  219. end):show())
  220. visualButton(fuelRodsFrame:addButton("startThresholdLevelPlus19"):setText("+10"):setSize(3,1):setPosition(36,6):onClick(function()
  221. if((startThreshold+10) < 100) then
  222. startThreshold = startThreshold+10
  223. else
  224. startThreshold = 100
  225. end
  226. save()
  227. end):show())
  228. startThresholdLevel = fuelRodsFrame:addProgressbar("startThresholdLevelProgressbar"):setSize(16,1):setPosition(13,6):show()
  229. visualButton(fuelRodsFrame:addButton("startThresholdLevelMinus1"):setText("-1"):setSize(2,1):setPosition(3,6):onClick(function()
  230. if((startThreshold-1) > 0) then
  231. startThreshold = startThreshold-1
  232. else
  233. startThreshold = 0
  234. end
  235. end):show())
  236. visualButton(fuelRodsFrame:addButton("startThresholdLevelMinus5"):setText("-5"):setSize(2,1):setPosition(6,6):onClick(function()
  237. if((startThreshold-5) > 0) then
  238. startThreshold = startThreshold-5
  239. else
  240. startThreshold = 0
  241. end
  242. save()
  243. end):show())
  244. visualButton(fuelRodsFrame:addButton("startThresholdLevelMinus19"):setText("-10"):setSize(3,1):setPosition(9,6):onClick(function()
  245. if((startThreshold-10) > 0) then
  246. startThreshold = startThreshold-10
  247. else
  248. startThreshold = 0
  249. end
  250. save()
  251. end):show())
  252. fuelRodsFrame:addLabel("stopThresholdLabel"):setText("Stop Threshold:"):setPosition(2,8):show()
  253. stopThresholdValLabel = fuelRodsFrame:addLabel("stopThresholdValLabel"):setText("0%"):setPosition(19,9):show()
  254. visualButton(fuelRodsFrame:addButton("stopThresholdLevelPlus1"):setText("+1"):setSize(2,1):setPosition(30,9):onClick(function()
  255. if((stopThreshold+1) < 100) then
  256. stopThreshold = stopThreshold+1
  257. else
  258. stopThreshold = 100
  259. end
  260. save()
  261. end):show())
  262. visualButton(fuelRodsFrame:addButton("stopThresholdLevelPlus5"):setText("+5"):setSize(2,1):setPosition(33,9):onClick(function()
  263. if((stopThreshold+5) < 100) then
  264. stopThreshold = stopThreshold+5
  265. else
  266. stopThreshold = 100
  267. end
  268. save()
  269. end):show())
  270. visualButton(fuelRodsFrame:addButton("stopThresholdLevelPlus19"):setText("+10"):setSize(3,1):setPosition(36,9):onClick(function()
  271. if((stopThreshold+10) < 100) then
  272. stopThreshold = stopThreshold+10
  273. else
  274. stopThreshold = 100
  275. end
  276. save()
  277. end):show())
  278. stopThresholdLevel = fuelRodsFrame:addProgressbar("stopThresholdLevelProgressbar"):setSize(16,1):setPosition(13,9):show()
  279. visualButton(fuelRodsFrame:addButton("stopThresholdLevelMinus1"):setText("-1"):setSize(2,1):setPosition(3,9):onClick(function()
  280. if((stopThreshold-1) > 0) then
  281. stopThreshold = stopThreshold-1
  282. else
  283. stopThreshold = 0
  284. end
  285. save()
  286. end):show())
  287. visualButton(fuelRodsFrame:addButton("stopThresholdLevelMinus5"):setText("-5"):setSize(2,1):setPosition(6,9):onClick(function()
  288. if((stopThreshold-5) > 0) then
  289. stopThreshold = stopThreshold-5
  290. else
  291. stopThreshold = 0
  292. end
  293. save()
  294. end):show())
  295. visualButton(fuelRodsFrame:addButton("stopThresholdLevelMinus19"):setText("-10"):setSize(3,1):setPosition(9,9):onClick(function()
  296. if((stopThreshold-10) > 0) then
  297. stopThreshold = stopThreshold-10
  298. else
  299. stopThreshold = 0
  300. end
  301. save()
  302. end):show())
  303. --Settings end
  304.  
  305. --Calculation Thread start
  306. rodsRunning = 0
  307. local function calcThread()
  308. while true do
  309. for i = 0, controlRodCount-1 do
  310. if(reactor.getControlRod(i).level() < 100) then
  311. rodsRunning = rodsRunning + 1
  312. end
  313. end
  314. if(rodsRunning > 0) then
  315. reactorRunning = true
  316. else
  317. reactorRunning = false
  318. end
  319. if(isManuelEnabled) then
  320. btnManuel:setBackground(colors.green)
  321. btnManuel:setForeground(colors.darkGray)
  322. else
  323. btnManuel:setBackground(colors.red)
  324. btnManuel:setForeground(colors.darkGray)
  325. end
  326. if(isAutoModeEnabled) then
  327. btnAutoMode:setBackground(colors.green)
  328. btnAutoMode:setForeground(colors.darkGray)
  329. autoModeLabel:setText("On")
  330. else
  331. btnAutoMode:setBackground(colors.red)
  332. btnAutoMode:setForeground(colors.darkGray)
  333. autoModeLabel:setText("Off")
  334. end
  335. if(reactor.active()) then
  336. btnActivate:setBackground(colors.green)
  337. btnActivate:setForeground(colors.darkGray)
  338. activeLabel:setText("On")
  339. else
  340. btnActivate:setBackground(colors.red)
  341. btnActivate:setForeground(colors.darkGray)
  342. activeLabel:setText("Off")
  343. end
  344. rodLevel:setProgress(fuelRodLevel)
  345. powerValLabel:setText(fuelRodLevel.."%")
  346. startThresholdLevel:setProgress(startThreshold)
  347. startThresholdValLabel:setText(startThreshold.."%")
  348. stopThresholdLevel:setProgress(stopThreshold)
  349. stopThresholdValLabel:setText(stopThreshold.."%")
  350. fuelStored = reactor.fuelTank().fuel()
  351. energyStored = reactor.battery().stored()
  352. energyPerTick = reactor.battery().producedLastTick()
  353. fuelWaste = reactor.fuelTank().waste()
  354. fuelBurnedLastTick = round(reactor.fuelTank().burnedLastTick(), 4)
  355. energyStorage:setText(energyStored.." / "..energyCapacity.." FE")
  356. energyTick:setText(energyPerTick.." FE/t")
  357. fuelStorage:setText(fuelStored.." / "..fuelCapacity.." mB")
  358. wasteStorage:setText(fuelWaste.." / "..fuelCapacity.." mB")
  359. fuelUsed:setText(fuelBurnedLastTick.." mB/t")
  360. if(reactorRunning) then
  361. burnTimerSeconds = (1000-fuelWaste)/(fuelBurnedLastTick*20)
  362. burnTimeSeconds = 1000/(fuelBurnedLastTick*20)
  363. benchmarkVal = math.floor(energyPerTick/reactor.fuelTank().burnedLastTick()/100)
  364. else
  365. burnTimerSeconds = 0
  366. burnTimeSeconds = 0
  367. benchmarkVal = 0
  368. end
  369. fuelBenchmark:setText(benchmarkVal.."")
  370. fuelBurnedTime:setText(SecondsToTime(burnTimerSeconds))
  371. fuelUsedTime:setText(SecondsToTime(burnTimeSeconds))
  372. if(#IOValue == 60) then
  373. table.remove(IOValue, 1)
  374. end
  375. table.insert(IOValue, (energyStored-oldEnergyStored))
  376. IOValueOutput = 0
  377. for i = 1, #IOValue do
  378. IOValueOutput = IOValueOutput + IOValue[i]
  379. end
  380. IOValueOutput = math.floor((IOValueOutput/#IOValue)/20)
  381. ValueIOTick:setText(IOValueOutput)
  382. oldEnergyStored = energyStored
  383. energyStoredPct = (energyStored*100)/energyCapacity
  384. if(reactor.active()) then
  385. if(isAutoModeEnabled) then
  386. if(energyStoredPct < startThreshold) then
  387. reactor.setAllControlRodLevels(fuelRodLevel)
  388. elseif(energyStoredPct > stopThreshold) then
  389. reactor.setAllControlRodLevels(100)
  390. end
  391. elseif(isManuelEnabled) then
  392. reactor.setAllControlRodLevels(fuelRodLevel)
  393. else
  394. reactor.setAllControlRodLevels(100)
  395. end
  396. end
  397. os.sleep(1)
  398. rodsRunning = 0
  399. end
  400. end
  401. mainThread:start(calcThread)
  402. --Calculation Thread end
  403.  
  404. basalt.autoUpdate()
Add Comment
Please, Sign In to add comment