Advertisement
drpepper240

turtle reactor controller

Dec 13th, 2013
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.76 KB | None | 0 0
  1. --computercraft
  2. --reactor controller for AE fission reactor by drPepper
  3. --Requires a turtle below the reactor block, a chest below it, and an empty space in front of it
  4.  
  5. -- "THE BEER-WARE LICENSE"
  6. -- drPepper@KOPROKUBACH wrote this file. As long as you retain this notice you
  7. -- can do whatever you want with this stuff. If we meet some day, in-game or IRL,
  8. -- and you think this stuff is worth it, you can give me a beer in return
  9.  
  10.  
  11. --resume table
  12. rt = {}
  13. rt.reactorEmpty = false
  14. rt.turtleEmpty = false
  15. rt.chestEmpty = false
  16. rt.needAssistance = false
  17. --make sure reactor is working
  18. rt.setReactorWorking = false
  19. rt.guiMessage = ""
  20.  
  21. monitor = nil
  22.  
  23. PASTEBIN = "SVKJqCE0"
  24.  
  25. --time to re-check reactor state
  26. REFRESH_TIME = 1.0
  27.  
  28.  
  29.  
  30. function DetectPeripheral(name)  
  31.     local sides = {"top" , "bottom" , "front" , "left" , "right" , "back"} 
  32.     for i = 1, 6 do
  33.         if peripheral.isPresent(sides[i]) and peripheral.getType(sides[i]) == name then
  34.             print("found "..tostring(name)..": "..tostring(sides[i]))
  35.             return sides[i]
  36.         end
  37.     end
  38.     return nil
  39. end
  40.  
  41.  
  42. function WriteResume()
  43.     local file = fs.open("resume.f","w")
  44.     local sT = textutils.serialize(rt)
  45.     file.write(sT)
  46.     file.close()
  47.     return
  48. end
  49.  
  50.  
  51. function ReadResume()
  52.     if not fs.exists("resume.f") then
  53.         WriteResume()
  54.         return
  55.     end
  56.     local file = fs.open("resume.f","r")
  57.     local sT = file.readAll()
  58.     rt = textutils.unserialize(sT)
  59.     file.close()
  60.     return
  61. end
  62.  
  63. --params are boolean
  64. function setVarReactorEmpty(empty)
  65.     rt.reactorEmpty = empty
  66.     WriteResume()
  67.     print("ReactorEmpty: "..tostring(empty))
  68. end
  69.  
  70. function setVarTurtleEmpty(empty)
  71.     rt.turtleEmpty = empty
  72.     WriteResume()
  73.     print("TurtleEmpty: "..tostring(empty))
  74. end
  75.  
  76. function setVarChestEmpty(empty)
  77.     rt.chestEmpty = empty
  78.     WriteResume()
  79.     print("ChestEmpty: "..tostring(empty))
  80. end
  81.  
  82. --fills reactor chamber and fully prepares for the next fill
  83. --returns true on success: reactor filled
  84. function fillReactor()
  85.     turtle.select(1)
  86.     if turtle.getItemCount(1) ~= 0 then
  87.         setVarTurtleEmpty(false)
  88.         if turtle.dropUp() then
  89.             setVarReactorEmpty(false)
  90.             setVarTurtleEmpty(true)
  91.             if turtle.suckDown() then
  92.                 setVarTurtleEmpty(false)
  93.                 return true
  94.             else
  95.                 setVarChestEmpty(true)
  96.                 return true
  97.             end
  98.         else
  99.             setVarReactorEmpty(false)
  100.             return true
  101.         end
  102.     else
  103.         setVarTurtleEmpty(true)
  104.         if turtle.suckDown() then
  105.             setVarTurtleEmpty(false)
  106.         else
  107.             setVarChestEmpty(true)
  108.         end
  109.         return false --to start again
  110.     end
  111. end
  112.  
  113. --empties the reactor and tries to preserve used (if any) in the turtle and return unused cell to the chest (in this case)
  114. function emptyReactor()
  115.     turtle.select(1)
  116.     if turtle.getItemCount(1) == 0 then
  117.         setVarTurtleEmpty(true)
  118.         if turtle.suckUp() then
  119.             setVarReactorEmpty(true)
  120.             setVarTurtleEmpty(false)
  121.             return
  122.         else
  123.             setVarReactorEmpty(true)
  124.             return
  125.         end
  126.     else
  127.         setVarTurtleEmpty(false)
  128.         if turtle.dropDown() then
  129.             setVarTurtleEmpty(true)
  130.             if turtle.suckUp() then
  131.                 setVarTurtleEmpty(false)
  132.             end
  133.             setVarReactorEmpty(true)
  134.             return
  135.         else
  136.             print("chest overfilled")
  137.             rt.needAssistance = true
  138.             turtle.drop()
  139.             setVarTurtleEmpty(true)
  140.             if turtle.suckUp() then
  141.                 setVarTurtleEmpty(false)
  142.             end
  143.                 setVarReactorEmpty(true)
  144.             return
  145.         end
  146.     end
  147. end
  148.  
  149.  
  150. function ProcessClick(cx, cy)
  151.     if cy == 3 and cx < 5 then
  152.         rt.chestEmpty = false
  153.     elseif cy == 4 and rt.needAssistance then
  154.         rt.needAssistance = false
  155.     elseif cy ~= 4 and cx > 5 and cy < 10 then
  156.         --start
  157.         rt.guiMessage = "  starting... "
  158.     elseif cy ~= 4 and cx > 11 then
  159.         --stop
  160.         rt.guiMessage = "  stopping... "
  161.     end
  162.    
  163. end
  164.  
  165.  
  166. function DrawGUI()
  167. --RCTR #### ####
  168. --TRTL #### ####
  169. --CHST #### ####
  170. --messagemessage
  171.  
  172.     if monitor == nil then
  173.         return
  174.     end
  175.  
  176.     monitor.setBackgroundColor(colors.black)
  177.     monitor.clear()
  178.     monitor.setTextColor(colors.white)
  179.    
  180.     monitor.setCursorPos(1,1)
  181.     if rt.reactorEmpty then
  182.         monitor.setBackgroundColor(colors.red)
  183.     else
  184.         monitor.setBackgroundColor(colors.green)
  185.     end
  186.     monitor.write("RCTR")
  187.    
  188.     monitor.setCursorPos(1,2)
  189.     if rt.turtleEmpty then
  190.         monitor.setBackgroundColor(colors.red)
  191.     else
  192.         monitor.setBackgroundColor(colors.green)
  193.     end
  194.     monitor.write("TRTL")
  195.    
  196.     monitor.setCursorPos(1,3)
  197.     if rt.chestEmpty then
  198.         monitor.setBackgroundColor(colors.red)
  199.     else
  200.         monitor.setBackgroundColor(colors.green)
  201.     end
  202.     monitor.write("CHST")
  203.    
  204.     local i = 1
  205.     monitor.setBackgroundColor(colors.green)
  206.     for i=1,3 do
  207.         monitor.setCursorPos(6,i)
  208.         monitor.write("    ")
  209.     end
  210.  
  211.     monitor.setBackgroundColor(colors.red)
  212.     for i=1,3 do
  213.         monitor.setCursorPos(11,i)
  214.         monitor.write("    ")
  215.     end
  216.    
  217.     if rt.needAssistance then
  218.         monitor.setBackgroundColor(colors.orange)
  219.         monitor.setTextColor(colors.black)
  220.     end
  221.     monitor.setCursorPos(1,4)
  222.     monitor.write(rt.guiMessage)
  223. end
  224.  
  225.  
  226. ReadResume()
  227.  
  228. monitor = peripheral.wrap(DetectPeripheral("monitor"))
  229.  
  230. termSizeX, termSizeY = term.getSize()
  231.  
  232. while true do
  233.     DrawGUI()
  234.  
  235.     --text UI
  236.     term.clear()
  237.     term.setCursorPos(1,1)
  238.     term.write("REACTOR  [")
  239.     if rt.reactorEmpty then
  240.         term.write(" ]")
  241.     else
  242.         term.write("X]")
  243.     end
  244.     term.setCursorPos(1,2)
  245.     term.write("TURTLE   [")
  246.     if rt.turtleEmpty then
  247.         term.write(" ]")
  248.     else
  249.         term.write("X]")
  250.     end
  251.     term.setCursorPos(1,3)
  252.     term.write("CHEST    [")
  253.     if rt.chestEmpty then
  254.         print(" ]")
  255.     else
  256.         print("X]")
  257.     end
  258.     if rt.needAssistance then
  259.         term.setCursorPos(1,4)
  260.         term.write("\nGET A HAZMAT SUIT AND GO CHECK THE REACTOR!\n")
  261.     end
  262.    
  263.     term.setCursorPos(14,1)
  264.     term.write("last update:")
  265.     term.setCursorPos(14,2)
  266.     term.write(tostring(os.time()))
  267.     term.setCursorPos(14,3)
  268.     if rt.setReactorWorking then
  269.         print("ONLINE")
  270.     else
  271.         print("STOPPED")
  272.     end
  273.    
  274.     term.setCursorPos(1,termSizeY)
  275.    
  276.     os.startTimer(REFRESH_TIME)
  277.    
  278.     print("H - help")
  279.     local sEvent, param = os.pullEvent()
  280.     if sEvent == "key" then
  281.         if param == 31 then
  282.             --TODO start
  283.             if not rt.needAssistance then
  284.                 print("Starting...")
  285.                 if fillReactor() then
  286.                     print("Success!")
  287.                     sleep(1)
  288.                 else
  289.                     print("Failure!")
  290.                     sleep(1)
  291.                 end
  292.             end
  293.         elseif param == 30 then
  294.             --TODO abort
  295.             print("Stopping...")
  296.             emptyReactor()
  297.             print("Done.")
  298.             sleep(1)
  299.         elseif param == 22 then
  300.             --update
  301.             fs.delete("resume.f")
  302.             shell.run("rm", "startup")
  303.             shell.run("pastebin", "get "..PASTEBIN.." startup")
  304.             os.reboot()
  305.            
  306.         elseif param == 35 then
  307.             --help
  308.             print("S - start, A - abort, R - reset all warnings. Always leave some space free in the chest.")
  309.         elseif param == 19 then
  310.             --reset
  311.             setVarChestEmpty(false)
  312.             rt.needAssistance = false
  313.         end
  314.  
  315.     elseif sEvent == "timer" then
  316.         if rt.setReactorWorking then
  317.             if not rt.needAssistance then
  318.                 fillReactor()
  319.             end
  320.         else
  321.             emptyReactor()
  322.         end
  323.     end
  324.    
  325. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement