jared314

dev_turbine

Dec 3rd, 2014
420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.29 KB | None | 0 0
  1. -----BigReactor Control
  2. -----by jaranvil aka jared314
  3.  
  4. -----feel free to use and/or mondify this code
  5. -----http://pastebin.com/RCPEHmxs
  6. -----------------------------------------------
  7.  
  8. version = 1
  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.blue)
  62. draw_text(2, 1, "Power Tools Settings", colors.white, colors.blue)
  63. draw_line(1, 19, monX, colors.blue)
  64. draw_text(2, 19, " Turbine Control", colors.white, colors.blue)
  65. end
  66.  
  67. function power_menu()
  68. draw_line(1, 2, 9, colors.gray)
  69. draw_line(1, 3, 9, colors.gray)
  70. if active then
  71. draw_text(2, 2, "ON", colors.lightGray, colors.gray)
  72. draw_text(2, 3, "OFF", colors.white, colors.gray)
  73. else
  74. draw_text(2, 2, "ON", colors.white, colors.gray)
  75. draw_text(2, 3, "OFF", colors.lightGray, colors.gray)
  76. end
  77. end
  78.  
  79. function tools_menu()
  80. draw_line(10, 2, 14, colors.gray)
  81. draw_line(10, 3, 14, colors.gray)
  82. draw_text(11, 2, "Flow Rate", colors.white, colors.gray)
  83. draw_text(11, 3, "Coils", colors.white, colors.gray)
  84.  
  85.  
  86. end
  87.  
  88. function settings_menu()
  89. draw_line(12, 2, 18, colors.gray)
  90. draw_line(12, 3, 18, colors.gray)
  91. draw_text(13, 2, "Check for Updates", colors.white, colors.gray)
  92. draw_text(13, 3, "Reset peripherals", colors.white, colors.gray)
  93. end
  94.  
  95. function popup_screen(y, title, height)
  96. clear()
  97. menu_bar()
  98.  
  99. draw_line(4, y, 22, colors.blue)
  100. draw_line(25, y, 1, colors.red)
  101.  
  102. for counter = y+1, height+y do
  103. draw_line(4, counter, 22, colors.white)
  104. end
  105.  
  106. draw_text(25, y, "X", colors.white, colors.red)
  107. draw_text(5, y, title, colors.white, colors.blue)
  108. end
  109.  
  110. function save_config()
  111. sw = fs.open("turbine_config.txt", "w")
  112. sw.writeLine(version)
  113. sw.writeLine(side)
  114. sw.writeLine(name)
  115. sw.writeLine(auto_string)
  116. sw.writeLine(on)
  117. sw.writeLine(off)
  118. sw.close()
  119. end
  120.  
  121. function load_config()
  122. sr = fs.open("turbine_config.txt", "r")
  123. version = tonumber(sr.readLine())
  124. side = sr.readLine()
  125. name = sr.readLine()
  126. auto_string = sr.readLine()
  127. on = tonumber(sr.readLine())
  128. off = tonumber(sr.readLine())
  129. sr.close()
  130. end
  131.  
  132. --------------------------------------------------
  133.  
  134.  
  135.  
  136. function homepage()
  137. while true do
  138. clear()
  139. menu_bar()
  140. terminal_screen()
  141.  
  142. active = turbine.getActive()
  143. energy_stored = turbine.getEnergyStored()
  144.  
  145.  
  146. --------POWER STAT--------------
  147. draw_text(2, 3, "Power: ", colors.yellow, colors.black)
  148.  
  149. if active then
  150. draw_text(10, 3, "ONLINE", colors.lime, colors.black)
  151. else
  152. draw_text(10, 3, "OFFLINE", colors.red, colors.black)
  153. end
  154.  
  155.  
  156. -----------Router speed---------------------
  157. draw_text(2, 5, "Router Speed: ", colors.yellow, colors.black)
  158. local maxVal = 2000
  159. local minVal = math.floor(turbine.getRotorSpeed())
  160. draw_text(19, 5, minVal.." rpm", colors.white, colors.black)
  161.  
  162. if minVal < 700 then
  163. progress_bar(2, 6, monX-2, minVal, maxVal, colors.lightBlue, colors.gray)
  164. else if minVal < 900 then
  165. progress_bar(2, 6, monX-2, minVal, maxVal, colors.lime, colors.gray)
  166. else if minVal < 1700 then
  167. progress_bar(2, 6, monX-2, minVal, maxVal, colors.lightBlue, colors.gray)
  168. else if minVal < 1900 then
  169. progress_bar(2, 6, monX-2, minVal, maxVal, colors.lime, colors.gray)
  170. else if minVal < 2000 then
  171. progress_bar(2, 6, monX-2, minVal, maxVal, colors.yellow, colors.gray)
  172. else if minVal >= 2000 then
  173. progress_bar(2, 6, monX-2, minVal, maxVal, colors.red, colors.gray)
  174. end
  175. end
  176. end
  177. end
  178. end
  179. end
  180.  
  181. -----------Steam Level---------------------
  182. draw_text(2, 8, "Steam Amount: ", colors.yellow, colors.black)
  183. local maxVal = 4000
  184. local minVal = math.floor(turbine.getInputAmount())
  185. draw_text(19, 8, minVal.." mB", colors.white, colors.black)
  186. progress_bar(2, 9, monX-2, minVal, maxVal, colors.lightGray, colors.gray)
  187.  
  188.  
  189. -----------Water Level---------------------
  190. draw_text(2, 11, "Water Amount: ", colors.yellow, colors.black)
  191. local maxVal = 4000
  192. local minVal = math.floor(turbine.getOutputAmount())
  193. draw_text(19, 11, minVal.." mB", colors.white, colors.black)
  194. progress_bar(2, 12, monX-2, minVal, maxVal, colors.blue, colors.gray)
  195.  
  196.  
  197. -------------OUTPUT-------------------
  198. draw_text(2, 14, "RF/tick: ", colors.yellow, colors.black)
  199. rft = math.floor(turbine.getEnergyProducedLastTick())
  200. draw_text(19, 14, rft.." RF/T", colors.white, colors.black)
  201.  
  202. -----------RF STORAGE---------------
  203. draw_text(2, 15, "RF Stored:", colors.yellow, colors.black)
  204. local maxVal = 1000000
  205. local minVal = energy_stored
  206. local percent = math.floor((energy_stored/maxVal)*100)
  207. draw_text(19, 15, percent.."%", colors.white, colors.black)
  208.  
  209. ------------FLOW RATE----------------
  210. draw_text(2, 16, "Flow Rate: ", colors.yellow, colors.black)
  211. flow_rate = turbine.getFluidFlowRateMax()
  212. draw_text(19, 16, flow_rate.." mB/t", colors.white, colors.black)
  213.  
  214. ------------COILS---------------------------
  215. engaged = turbine.getInductorEngaged()
  216. draw_text(2, 17, "Coils: ", colors.yellow, colors.black)
  217.  
  218. if engaged then
  219. draw_text(19, 17, "Engaged", colors.white, colors.black)
  220. else
  221. draw_text(19, 17, "Disengaged", colors.white, colors.black)
  222. end
  223.  
  224.  
  225.  
  226. sleep(0.5)
  227. end
  228. end
  229.  
  230. --------------MENU SCREENS--------------
  231.  
  232.  
  233.  
  234. -------------------Tools----------------------
  235.  
  236. function flow_rate_menu()
  237. popup_screen(3, "Flow Rate", 12)
  238. flow_rate = turbine.getFluidFlowRateMax()
  239. current_flow = turbine.getFluidFlowRate()
  240.  
  241. draw_text(5, 5, "Steam consumption", colors.lime, colors.white)
  242. draw_text(5, 6, "last tick:", colors.lime, colors.white)
  243. draw_text(13, 8, current_flow.." mB", colors.black, colors.white)
  244.  
  245. draw_text(5, 10, "Flow Rate Setting:", colors.lime, colors.white)
  246. draw_text(8, 12, " < ", colors.white, colors.black)
  247. draw_text(13, 12, flow_rate.." mB", colors.black, colors.white)
  248. draw_text(21, 12, " > ", colors.white, colors.black)
  249.  
  250. draw_text(13, 14, " Okay ", colors.white, colors.black)
  251.  
  252. local event, side, xPos, yPos = os.pullEvent("monitor_touch")
  253.  
  254. --decrease
  255. if yPos == 12 and xPos >= 8 and xPos <= 11 then
  256. turbine.setFluidFlowRateMax(flow_rate-10)
  257. flow_rate_menu()
  258. --increase
  259. else if yPos == 12 and xPos >= 21 and xPos <= 24 then
  260. turbine.setFluidFlowRateMax(flow_rate+10)
  261. flow_rate_menu()
  262. end
  263. end
  264.  
  265. --Okay button
  266. if yPos == 14 and xPos >= 13 and xPos <= 19 then
  267. call_homepage()
  268. end
  269.  
  270. --Exit button
  271. if yPos == 3 and xPos == 25 then
  272. call_homepage()
  273. end
  274. flow_rate_menu()
  275. end
  276.  
  277.  
  278. function coil_menu()
  279. engaged = turbine.getInductorEngaged()
  280.  
  281. popup_screen(3, "Induction Coils", 8)
  282. draw_text(5, 5, "Coil Status:", colors.black, colors.white)
  283.  
  284. if engaged then
  285. draw_text(5, 6, "Engaged", colors.lime, colors.white)
  286. draw_text(10, 8, " Disengage ", colors.white, colors.black)
  287. else
  288. draw_text(5, 6, "Disengaged", colors.red, colors.white)
  289. draw_text(10, 8, " Engage ", colors.white, colors.black)
  290. end
  291.  
  292. draw_text(12, 10, " Okay ", colors.white, colors.black)
  293.  
  294.  
  295. local event, side, xPos, yPos = os.pullEvent("monitor_touch")
  296.  
  297. if yPos == 8 and xPos >= 10 and xPos <= 21 then
  298. if engaged then
  299. turbine.setInductorEngaged(false)
  300. coil_menu()
  301. else
  302. turbine.setInductorEngaged(true)
  303. coil_menu()
  304. end
  305. end
  306.  
  307. --okay button
  308. if yPos == 10 and xPos >= 12 and xPos <= 18 then
  309. call_homepage()
  310. end
  311.  
  312.  
  313. coil_menu()
  314. end
  315.  
  316. -----------------------Settings--------------------------------
  317.  
  318.  
  319.  
  320. function install_update(program, pastebin)
  321.  
  322.  
  323. end
  324.  
  325. function update()
  326. popup_screen(5, "Updates", 4)
  327. draw_text(5, 7, "Connecting to", colors.black, colors.white)
  328. draw_text(5, 8, "pastebin...", colors.black, colors.white)
  329.  
  330. sleep(0.5)
  331.  
  332. shell.run("pastebin get QP3qrzNu current_version.txt")
  333. sr = fs.open("current_version.txt", "r")
  334. current_version = tonumber(sr.readLine())
  335. sr.close()
  336. fs.delete("current_version.txt")
  337. terminal_screen()
  338.  
  339. if current_version > version then
  340.  
  341. popup_screen(5, "Updates", 7)
  342. draw_text(5, 7, "Update Available!", colors.black, colors.white)
  343. draw_text(11, 9, " Intall ", colors.white, colors.black)
  344. draw_text(11, 11, " Ignore ", colors.white, colors.black)
  345.  
  346. local event, side, xPos, yPos = os.pullEvent("monitor_touch")
  347.  
  348. --Instatll button
  349. if yPos == 9 and xPos >= 11 and xPos <= 17 then
  350. install_update("turbine_control", "5B8h94V4")
  351. end
  352.  
  353. --Exit button
  354. if yPos == 5 and xPos == 25 then
  355. call_homepage()
  356. end
  357. call_homepage()
  358.  
  359. else
  360. popup_screen(5, "Updates", 5)
  361. draw_text(5, 7, "You are up to date!", colors.black, colors.white)
  362. draw_text(11, 9, " Okay ", colors.white, colors.black)
  363.  
  364. local event, side, xPos, yPos = os.pullEvent("monitor_touch")
  365.  
  366. --Okay button
  367. if yPos == 9 and xPos >= 11 and xPos <= 17 then
  368. call_homepage()
  369. end
  370.  
  371. --Exit button
  372. if yPos == 5 and xPos == 25 then
  373. call_homepage()
  374. end
  375. call_homepage()
  376. end
  377.  
  378.  
  379.  
  380. end
  381.  
  382. function reset_peripherals()
  383. clear()
  384. draw_line(4, 5, 22, colors.blue)
  385.  
  386. for counter = 6, 10 do
  387. draw_line(4, counter, 22, colors.white)
  388. end
  389.  
  390. draw_text(5, 5, "Reset Peripherals", colors.white, colors.blue)
  391. draw_text(5, 7, "Open computer", colors.black, colors.white)
  392. draw_text(5, 8, "terminal.", colors.black, colors.white)
  393. setup_wizard()
  394.  
  395. end
  396.  
  397. --stop running status screen if monitors was touched
  398. function stop()
  399. while true do
  400. local event, side, xPos, yPos = os.pullEvent("monitor_touch")
  401. x = xPos
  402. y = yPos
  403. stop_function = "monitor_touch"
  404. return
  405. end
  406. end
  407.  
  408. function mon_touch()
  409. --when the monitor is touch on the homepage
  410. if y == 1 then
  411. if x < monX/3 then
  412. power_menu()
  413. local event, side, xPos, yPos = os.pullEvent("monitor_touch")
  414. if xPos < 9 then
  415. if yPos == 2 then
  416. turbine.setActive(true)
  417. call_homepage()
  418. else if yPos == 3 then
  419. turbine.setActive(false)
  420. call_homepage()
  421. else
  422. call_homepage()
  423. end
  424. end
  425. else
  426. call_homepage()
  427. end
  428.  
  429. else if x < 20 then
  430. tools_menu()
  431. local event, side, xPos, yPos = os.pullEvent("monitor_touch")
  432. if xPos < 25 and xPos > 10 then
  433. if yPos == 2 then
  434. flow_rate_menu()
  435. else if yPos == 3 then
  436. coil_menu()
  437. else if yPos == 4 then
  438.  
  439. else if yPos == 5 then
  440.  
  441. else
  442. call_homepage()
  443. end
  444. end
  445. end
  446. end
  447. else
  448. call_homepage()
  449. end
  450. else if x < monX then
  451. settings_menu()
  452. local event, side, xPos, yPos = os.pullEvent("monitor_touch")
  453. if xPos > 13 then
  454. if yPos == 2 then
  455. update()
  456. else if yPos == 3 then
  457. reset_peripherals()
  458. else
  459. call_homepage()
  460. end
  461. end
  462. else
  463. call_homepage()
  464. end
  465. end
  466. end
  467. end
  468. else
  469. call_homepage()
  470. end
  471. end
  472.  
  473. function terminal_screen()
  474. term.clear()
  475. draw_line_term(1, 1, 55, colors.blue)
  476. draw_text_term(13, 1, "BigReactor Controls", colors.white, colors.blue)
  477. draw_line_term(1, 19, 55, colors.blue)
  478. draw_text_term(13, 19, "by jaranvil aka jared314", colors.white, colors.blue)
  479.  
  480. draw_text_term(1, 3, "Current program:", colors.white, colors.black)
  481. draw_text_term(1, 4, "Turbine Control v"..version, colors.blue, colors.black)
  482.  
  483. draw_text_term(1, 6, "Installer:", colors.white, colors.black)
  484. draw_text_term(1, 7, "pastebin.com/p4zeq7Ma", colors.blue, colors.black)
  485.  
  486. draw_text_term(1, 9, "Please give me your feedback, suggestions,", colors.white, colors.black)
  487. draw_text_term(1, 10, "and errors!", colors.white, colors.black)
  488.  
  489. draw_text_term(1, 11, "reddit.com/r/br_controls", colors.blue, colors.black)
  490. end
  491.  
  492. --run both homepage() and stop() until one returns
  493. function call_homepage()
  494. clear()
  495. parallel.waitForAny(homepage, stop)
  496.  
  497. if stop_function == "terminal_screen" then
  498. stop_function = "nothing"
  499. setup_wizard()
  500. else if stop_function == "monitor_touch" then
  501. stop_function = "nothing"
  502. mon_touch()
  503. end
  504. end
  505. end
  506.  
  507. --try to wrap peripherals
  508. --catch any errors
  509. function test_turbine_connection()
  510. turbine = peripheral.wrap(name) --wrap reactor
  511. c = turbine.getConnected()
  512. if unexpected_condition then error() end
  513. end
  514.  
  515. function test_monitor_connection()
  516. mon = peripheral.wrap(side) --wrap mon
  517. monX, monY = mon.getSize() --get mon size ()
  518. if unexpected_condition then error() end
  519. end
  520.  
  521. --test if the entered monitor and reactor can be wrapped
  522. function test_configs()
  523.  
  524. term.clear()
  525. draw_line_term(1, 1, 55, colors.blue)
  526. draw_text_term(10, 1, "BigReactor Controls v"..version, colors.white, colors.blue)
  527. draw_line_term(1, 19, 55, colors.blue)
  528. draw_text_term(10, 19, "by jaranvil aka jared314", colors.white, colors.blue)
  529.  
  530. draw_text_term(1, 3, "Wrapping peripherals...", colors.blue, colors.black)
  531. draw_text_term(2, 5, "wrap montior...", colors.white, colors.black)
  532. sleep(0.1)
  533. if pcall(test_monitor_connection) then
  534. draw_text_term(18, 5, "success", colors.lime, colors.black)
  535. else
  536. draw_text_term(1, 4, "Error:", colors.red, colors.black)
  537. draw_text_term(1, 8, "Could not connect to monitor on "..side.." side", colors.red, colors.black)
  538. draw_text_term(1, 9, "Valid sides are 'left', 'right', 'top', 'bottom' and 'back'", colors.white, colors.black)
  539. draw_text_term(1, 11, "Press Enter to continue...", colors.gray, colors.black)
  540. wait = read()
  541. setup_wizard()
  542. end
  543. sleep(0.1)
  544. draw_text_term(2, 6, "wrap turbine...", colors.white, colors.black)
  545. sleep(0.1)
  546. if pcall(test_turbine_connection) then
  547. draw_text_term(18, 6, "success", colors.lime, colors.black)
  548. else
  549. draw_text_term(1, 8, "Error:", colors.red, colors.black)
  550. draw_text_term(1, 9, "Could not connect to "..name, colors.red, colors.black)
  551. draw_text_term(1, 10, "Turbine must be connected with networking cable and wired modem", colors.white, colors.black)
  552. draw_text_term(1, 12, "Press Enter to continue...", colors.gray, colors.black)
  553. wait = read()
  554. setup_wizard()
  555. end
  556. sleep(0.1)
  557. draw_text_term(2, 8, "saving settings to file...", colors.white, colors.black)
  558.  
  559. save_config()
  560.  
  561. sleep(0.1)
  562. draw_text_term(1, 10, "Setup Complete!", colors.lime, colors.black)
  563. sleep(3)
  564.  
  565. auto = auto_string == "true"
  566. call_homepage()
  567.  
  568. end
  569. ----------------SETUP-------------------------------
  570.  
  571. function setup_wizard()
  572. term.clear()
  573. draw_text_term(1, 1, "BigReactor Controls v"..version, colors.lime, colors.black)
  574. draw_text_term(1, 2, "Peripheral setup wizard", colors.white, colors.black)
  575. draw_text_term(1, 4, "Step 1:", colors.lime, colors.black)
  576. draw_text_term(1, 5, "-Place 3x3 advanced monitors next to computer.", colors.white, colors.black)
  577. draw_text_term(1, 7, "Step 2:", colors.lime, colors.black)
  578. draw_text_term(1, 8, "-Place a wired modem on this computer and on the ", colors.white, colors.black)
  579. draw_text_term(1, 9, " computer port of the turbine.", colors.white, colors.black)
  580. draw_text_term(1, 10, "-connect modems with network cable.", colors.white, colors.black)
  581. draw_text_term(1, 11, "-right click modems to activate.", colors.white, colors.black)
  582. draw_text_term(1, 13, "Press Enter when ready...", colors.gray, colors.black)
  583.  
  584. wait = read()
  585.  
  586. term.clear()
  587. draw_text_term(1, 1, "Peripheral Setup", colors.lime, colors.black)
  588. draw_text_term(1, 3, "What side is your monitor on?", colors.yellow, colors.black)
  589.  
  590. term.setTextColor(colors.white)
  591. term.setCursorPos(1,4)
  592. side = read()
  593.  
  594. term.clear()
  595. draw_text_term(1, 1, "Peripheral Setup", colors.lime, colors.black)
  596. draw_text_term(1, 3, "What is the turbine's name?", colors.yellow, colors.black)
  597. draw_text_term(1, 4, "type 'default' for 'BigReactors-Turbine_0'", colors.gray, colors.black)
  598.  
  599. term.setTextColor(colors.white)
  600. term.setCursorPos(1,5)
  601. name = read()
  602.  
  603. if name == "default" then name = "BigReactors-Turbine_0" end
  604. auto_string = false
  605. on = 0
  606. off = 99
  607.  
  608. test_configs()
  609. end
  610.  
  611. function start()
  612. --if configs exists, load values and test
  613. if fs.exists("turbine_config.txt") then
  614. load_config()
  615.  
  616. test_configs()
  617. else
  618. setup_wizard()
  619. end
  620. end
  621.  
  622. start()
Add Comment
Please, Sign In to add comment