wolfdale276

Capacitor Storage

Apr 2nd, 2015
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.69 KB | None | 0 0
  1. -----BigReactor Control
  2. -----by jaranvil aka jared314
  3. -----Edited by Wolfdale276
  4.  
  5. -----feel free to use and/or mondify this code
  6. -----------------------------------------------
  7.  
  8. version = 2.2
  9. term.clear()
  10. -------------------FORMATTING-------------------------------
  11. function clear()
  12. mon.setBackgroundColor(colors.black)
  13. mon.clear()
  14. mon.setCursorPos(1,1)
  15. end
  16.  
  17. function draw_text_term(x, y, text, text_color, bg_color)
  18. term.setTextColor(text_color)
  19. term.setBackgroundColor(bg_color)
  20. term.setCursorPos(x,y)
  21. write(text)
  22. end
  23.  
  24. function draw_text(x, y, text, text_color, bg_color)
  25. mon.setBackgroundColor(bg_color)
  26. mon.setTextColor(text_color)
  27. mon.setCursorPos(x,y)
  28. mon.write(text)
  29. end
  30.  
  31. function draw_line(x, y, length, color)
  32. mon.setBackgroundColor(color)
  33. mon.setCursorPos(x,y)
  34. mon.write(string.rep(" ", length))
  35. end
  36.  
  37. function draw_line_term(x, y, length, color)
  38. term.setBackgroundColor(color)
  39. term.setCursorPos(x,y)
  40. term.write(string.rep(" ", length))
  41. end
  42.  
  43. function progress_bar(x, y, length, minVal, maxVal, bar_color, bg_color)
  44. draw_line(x, y, length, bg_color) --backgoround bar
  45. local barSize = math.floor((minVal/maxVal) * length)
  46. draw_line(x, y, barSize, bar_color) --progress so far
  47. end
  48.  
  49. function progress_bar_term(x, y, length, minVal, maxVal, bar_color, bg_color)
  50. draw_line_term(x, y, length, bg_color) --backgoround bar
  51. local barSize = math.floor((minVal/maxVal) * length)
  52. draw_line_term(x, y, barSize, bar_color) --progress so far
  53. end
  54.  
  55. function button(x, y, length, text, txt_color, bg_color)
  56. draw_line(x, y, length, bg_color)
  57. draw_text((x+2), y, text, txt_color, bg_color)
  58. end
  59.  
  60. function menu_bar()
  61. draw_line(1, 1, monX, colors.purple)
  62. draw_text(2, 1, "Power Tools Settings", colors.white, colors.purple)
  63. draw_line(1, 19, monX, colors.purple)
  64. draw_text(2, 19, " Reactor Control", colors.white, colors.purple)
  65. end
  66.  
  67. function power_menu()
  68. draw_line(1, 2, 9, colors.gray)
  69. draw_line(1, 3, 9, colors.gray)
  70. draw_line(1, 4, 9, colors.gray)
  71. if active then
  72. draw_text(2, 2, "ON", colors.lightGray, colors.gray)
  73. draw_text(2, 3, "OFF", colors.white, colors.gray)
  74. else
  75. draw_text(2, 2, "ON", colors.white, colors.gray)
  76. draw_text(2, 3, "OFF", colors.lightGray, colors.gray)
  77. end
  78. draw_text(2, 4, "Auto", colors.white, colors.gray)
  79. end
  80.  
  81. function tools_menu()
  82. draw_line(10, 2, 14, colors.gray)
  83. draw_line(10, 3, 14, colors.gray)
  84. draw_line(10, 4, 14, colors.gray)
  85. draw_line(10, 5, 14, colors.gray)
  86. draw_text(11, 2, "Control Rods", colors.white, colors.gray)
  87. draw_text(11, 3, "Efficiency", colors.white, colors.gray)
  88. draw_text(11, 4, "Fuel", colors.white, colors.gray)
  89. draw_text(11, 5, "Waste", colors.white, colors.gray)
  90.  
  91.  
  92. end
  93.  
  94. function settings_menu()
  95. draw_line(12, 2, 18, colors.gray)
  96. draw_line(12, 3, 18, colors.gray)
  97. draw_text(13, 2, "Check for Updates", colors.white, colors.gray)
  98. draw_text(13, 3, "Reset peripherals", colors.white, colors.gray)
  99. end
  100.  
  101. function popup_screen(y, title, height)
  102. clear()
  103. menu_bar()
  104.  
  105. draw_line(4, y, 22, colors.purple)
  106. draw_line(25, y, 1, colors.red)
  107.  
  108. for counter = y+1, height+y do
  109. draw_line(4, counter, 22, colors.white)
  110. end
  111.  
  112. draw_text(25, y, "X", colors.white, colors.red)
  113. draw_text(5, y, title, colors.white, colors.purple)
  114. end
  115.  
  116. function save_config()
  117. sw = fs.open("config.txt", "w")
  118. sw.writeLine(version)
  119. sw.writeLine(side)
  120. sw.writeLine(name)
  121. sw.writeLine(auto_string)
  122. sw.writeLine(on)
  123. sw.writeLine(off)
  124. sw.writeLine(auto_rods)
  125. sw.writeLine(auto_rf)
  126. sw.close()
  127. end
  128.  
  129. function load_config()
  130. sr = fs.open("config.txt", "r")
  131. version = tonumber(sr.readLine())
  132. side = sr.readLine()
  133. name = sr.readLine()
  134. auto_string = sr.readLine()
  135. on = tonumber(sr.readLine())
  136. off = tonumber(sr.readLine())
  137. auto_rods = sr.readLine()
  138. auto_rf = tonumber(sr.readLine())
  139. sr.close()
  140. end
  141.  
  142. --------------------------------------------------
  143.  
  144.  
  145.  
  146. function homepage()
  147. clear()
  148. menu_bar()
  149. terminal_screen()
  150.  
  151. energy_stored = peripheral.getEnergyStored(capacitor_bank)
  152.  
  153.  
  154. ------------STORAGE------------
  155.  
  156. draw_text(2, 15, "RF Stored:", colors.yellow, colors.black)
  157. energy_stored_percent = math.floor((energy_stored/10000000)*100)
  158. draw_text(13, 15, energy_stored_percent.."% ("..energy_stored.." RF)", colors.white, colors.black)
  159.  
  160.  
  161. end
  162.  
  163. sleep(0.5)
  164. end
  165.  
  166. --------------MENU SCREENS--------------
  167.  
  168. -----------------------Settings--------------------------------
  169.  
  170.  
  171. function rf_mode()
  172. wait = read()
  173. end
  174.  
  175. function steam_mode()
  176. wait = read()
  177. end
  178.  
  179. function install_update(program, pastebin)
  180. clear()
  181. draw_line(4, 5, 22, colors.blue)
  182.  
  183. for counter = 6, 10 do
  184. draw_line(4, counter, 22, colors.white)
  185. end
  186.  
  187. draw_text(5, 5, "Updating...", colors.white, colors.blue)
  188. draw_text(5, 7, "Open computer", colors.black, colors.white)
  189. draw_text(5, 8, "terminal.", colors.black, colors.white)
  190.  
  191. if fs.exists("install") then fs.delete("install") end
  192. shell.run("pastebin get p4zeq7Ma install")
  193. shell.run("install")
  194. end
  195.  
  196. function update()
  197. popup_screen(5, "Updates", 4)
  198. draw_text(5, 7, "Connecting to", colors.black, colors.white)
  199. draw_text(5, 8, "pastebin...", colors.black, colors.white)
  200.  
  201. sleep(0.5)
  202.  
  203. shell.run("pastebin get MkF2QQjH current_version.txt")
  204. sr = fs.open("current_version.txt", "r")
  205. current_version = tonumber(sr.readLine())
  206. sr.close()
  207. fs.delete("current_version.txt")
  208. terminal_screen()
  209.  
  210. if current_version > version then
  211.  
  212. popup_screen(5, "Updates", 7)
  213. draw_text(5, 7, "Update Available!", colors.black, colors.white)
  214. draw_text(11, 9, " Intall ", colors.white, colors.black)
  215. draw_text(11, 11, " Ignore ", colors.white, colors.black)
  216.  
  217. local event, side, xPos, yPos = os.pullEvent("monitor_touch")
  218.  
  219. --Instatll button
  220. if yPos == 9 and xPos >= 11 and xPos <= 17 then
  221. install_update()
  222. end
  223.  
  224. --Exit button
  225. if yPos == 5 and xPos == 25 then
  226. call_homepage()
  227. end
  228. call_homepage()
  229.  
  230. else
  231. popup_screen(5, "Updates", 5)
  232. draw_text(5, 7, "You are up to date!", colors.black, colors.white)
  233. draw_text(11, 9, " Okay ", colors.white, colors.black)
  234.  
  235. local event, side, xPos, yPos = os.pullEvent("monitor_touch")
  236.  
  237. --Okay button
  238. if yPos == 9 and xPos >= 11 and xPos <= 17 then
  239. call_homepage()
  240. end
  241.  
  242. --Exit button
  243. if yPos == 5 and xPos == 25 then
  244. call_homepage()
  245. end
  246. call_homepage()
  247. end
  248.  
  249.  
  250.  
  251. end
  252.  
  253. function reset_peripherals()
  254. clear()
  255. draw_line(4, 5, 22, colors.blue)
  256.  
  257. for counter = 6, 10 do
  258. draw_line(4, counter, 22, colors.white)
  259. end
  260.  
  261. draw_text(5, 5, "Reset Peripherals", colors.white, colors.blue)
  262. draw_text(5, 7, "Open computer", colors.black, colors.white)
  263. draw_text(5, 8, "terminal.", colors.black, colors.white)
  264. setup_wizard()
  265.  
  266. end
  267.  
  268. --stop running status screen if monitors was touched
  269. function stop()
  270. while true do
  271. local event, side, xPos, yPos = os.pullEvent("monitor_touch")
  272. x = xPos
  273. y = yPos
  274. stop_function = "monitor_touch"
  275. return
  276. end
  277. end
  278.  
  279. function mon_touch()
  280. --when the monitor is touch on the homepage
  281. if y == 1 then
  282. if x < monX/3 then
  283. power_menu()
  284. local event, side, xPos, yPos = os.pullEvent("monitor_touch")
  285. if xPos < 9 then
  286. if yPos == 2 then
  287. reactor1.setActive(true)
  288. timer = 0 --reset anytime the reactor is turned on/off
  289. call_homepage()
  290. else if yPos == 3 then
  291. reactor1.setActive(false)
  292. timer = 0 --reset anytime the reactor is turned on/off
  293. call_homepage()
  294. else if yPos == 4 then
  295. auto_off()
  296. else
  297. call_homepage()
  298. end
  299. end
  300. end
  301. else
  302. call_homepage()
  303. end
  304.  
  305. else if x < 20 then
  306. tools_menu()
  307. local event, side, xPos, yPos = os.pullEvent("monitor_touch")
  308. if xPos < 25 and xPos > 10 then
  309. if yPos == 2 then
  310. control_rods()
  311. else if yPos == 3 then
  312. efficiency()
  313. else if yPos == 4 then
  314. fuel()
  315. else if yPos == 5 then
  316. waste()
  317. else
  318. call_homepage()
  319. end
  320. end
  321. end
  322. end
  323. else
  324. call_homepage()
  325. end
  326. else if x < monX then
  327. settings_menu()
  328. local event, side, xPos, yPos = os.pullEvent("monitor_touch")
  329. if xPos > 13 then
  330. if yPos == 2 then
  331. update()
  332. else if yPos == 3 then
  333. reset_peripherals()
  334. else
  335. call_homepage()
  336. end
  337. end
  338. else
  339. call_homepage()
  340. end
  341. end
  342. end
  343. end
  344. else
  345. call_homepage()
  346. end
  347. end
  348.  
  349. function terminal_screen()
  350. term.clear()
  351. draw_line_term(1, 1, 55, colors.blue)
  352. draw_text_term(13, 1, "BigReactor Controls", colors.white, colors.blue)
  353. draw_line_term(1, 19, 55, colors.blue)
  354. draw_text_term(13, 19, "by jaranvil aka jared314", colors.white, colors.blue)
  355.  
  356. draw_text_term(1, 3, "Current program:", colors.white, colors.black)
  357. draw_text_term(1, 4, "Reactor Control v"..version, colors.blue, colors.black)
  358.  
  359. draw_text_term(1, 6, "Installer:", colors.white, colors.black)
  360. draw_text_term(1, 7, "pastebin.com/p4zeq7Ma", colors.blue, colors.black)
  361.  
  362. draw_text_term(1, 9, "Please give me your feedback, suggestions,", colors.white, colors.black)
  363. draw_text_term(1, 10, "and errors!", colors.white, colors.black)
  364.  
  365. draw_text_term(1, 11, "reddit.com/r/br_controls", colors.blue, colors.black)
  366. end
  367.  
  368. --run both homepage() and stop() until one returns
  369. function call_homepage()
  370. clear()
  371. parallel.waitForAny(homepage, stop)
  372.  
  373. if stop_function == "terminal_screen" then
  374. stop_function = "nothing"
  375. setup_wizard()
  376. else if stop_function == "monitor_touch" then
  377. stop_function = "nothing"
  378. mon_touch()
  379. end
  380. end
  381. end
  382.  
  383. --try to wrap peripherals
  384. --catch any errors
  385. function test_reactor_connection()
  386. reactor1 = peripheral.wrap(name) --wrap reactor
  387. c = reactor1.getConnected()
  388. if unexpected_condition then error() end
  389. end
  390.  
  391. function test_monitor_connection()
  392. mon = peripheral.wrap(side) --wrap mon
  393. monX, monY = mon.getSize() --get mon size ()
  394. if unexpected_condition then error() end
  395. end
  396.  
  397. --test if the entered monitor and reactor can be wrapped
  398. function test_configs()
  399.  
  400. term.clear()
  401. draw_line_term(1, 1, 55, colors.blue)
  402. draw_text_term(10, 1, "BigReactor Controls v"..version, colors.white, colors.blue)
  403. draw_line_term(1, 19, 55, colors.blue)
  404. draw_text_term(10, 19, "by jaranvil aka jared314", colors.white, colors.blue)
  405.  
  406. draw_text_term(1, 3, "Wrapping peripherals...", colors.blue, colors.black)
  407. draw_text_term(2, 5, "wrap montior...", colors.white, colors.black)
  408. sleep(0.1)
  409. if pcall(test_monitor_connection) then
  410. draw_text_term(18, 5, "success", colors.lime, colors.black)
  411. else
  412. draw_text_term(1, 4, "Error:", colors.red, colors.black)
  413. draw_text_term(1, 8, "Could not connect to monitor on "..side.." side", colors.red, colors.black)
  414. draw_text_term(1, 9, "Valid sides are 'left', 'right', 'top', 'bottom' and 'back'", colors.white, colors.black)
  415. draw_text_term(1, 11, "Press Enter to continue...", colors.gray, colors.black)
  416. wait = read()
  417. setup_wizard()
  418. end
  419. sleep(0.1)
  420. draw_text_term(2, 6, "wrap reactor...", colors.white, colors.black)
  421. sleep(0.1)
  422. if pcall(test_reactor_connection) then
  423. draw_text_term(18, 6, "success", colors.lime, colors.black)
  424. else
  425. draw_text_term(1, 8, "Error:", colors.red, colors.black)
  426. draw_text_term(1, 9, "Could not connect to "..name, colors.red, colors.black)
  427. draw_text_term(1, 10, "Reactor must be connected with networking cable and wired modem", colors.white, colors.black)
  428. draw_text_term(1, 12, "Press Enter to continue...", colors.gray, colors.black)
  429. wait = read()
  430. setup_wizard()
  431. end
  432. sleep(0.1)
  433. draw_text_term(2, 8, "saving settings to file...", colors.white, colors.black)
  434.  
  435. save_config()
  436.  
  437. sleep(0.1)
  438. draw_text_term(1, 10, "Setup Complete!", colors.lime, colors.black)
  439. sleep(1)
  440.  
  441. auto = auto_string == "true"
  442. call_homepage()
  443.  
  444. end
  445. ----------------SETUP-------------------------------
  446.  
  447. function setup_wizard()
  448. term.clear()
  449. draw_text_term(1, 1, "BigReactor Controls v"..version, colors.lime, colors.black)
  450. draw_text_term(1, 2, "Peripheral setup wizard", colors.white, colors.black)
  451. draw_text_term(1, 4, "Step 1:", colors.lime, colors.black)
  452. draw_text_term(1, 5, "-Place 3x3 advanced monitors next to computer.", colors.white, colors.black)
  453. draw_text_term(1, 7, "Step 2:", colors.lime, colors.black)
  454. draw_text_term(1, 8, "-Place a wired modem on this computer and on the ", colors.white, colors.black)
  455. draw_text_term(1, 9, " computer port of the reactor.", colors.white, colors.black)
  456. draw_text_term(1, 10, "-connect modems with network cable.", colors.white, colors.black)
  457. draw_text_term(1, 11, "-right click modems to activate.", colors.white, colors.black)
  458. draw_text_term(1, 13, "Press Enter when ready...", colors.gray, colors.black)
  459.  
  460. wait = read()
  461.  
  462. term.clear()
  463. draw_text_term(1, 1, "Peripheral Setup", colors.lime, colors.black)
  464. draw_text_term(1, 3, "What side is your monitor on?", colors.yellow, colors.black)
  465.  
  466. term.setTextColor(colors.white)
  467. term.setCursorPos(1,4)
  468. side = read()
  469.  
  470. term.clear()
  471. draw_text_term(1, 1, "Peripheral Setup", colors.lime, colors.black)
  472. draw_text_term(1, 3, "What is the reactor's name?", colors.yellow, colors.black)
  473. draw_text_term(1, 4, "type 'default' for 'BigReactors-Reactor_0'", colors.gray, colors.black)
  474.  
  475. term.setTextColor(colors.white)
  476. term.setCursorPos(1,5)
  477. name = read()
  478. w
  479. if name == "default" then name = "BigReactors-Reactor_0" end
  480. auto_string = false
  481. on = 0
  482. off = 99
  483. auto_rods = false
  484. auto_rf = 0
  485.  
  486. test_configs()
  487. end
  488.  
  489. function start()
  490. --if configs exists, load values and test
  491. if fs.exists("config.txt") then
  492. load_config()
  493.  
  494. test_configs()
  495. else
  496. setup_wizard()
  497. end
  498. end
  499.  
  500. start()
Add Comment
Please, Sign In to add comment