Advertisement
giintv

Copro Reactor 2

Aug 10th, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.    
  2.     reactor_pos = nil
  3.     sides = {"top" , "bottom" , "front" , "left" , "right" , "back"}    
  4.     reactor_file = "reactor.txt"
  5.     state_file = "state.txt"
  6.     command = nil
  7.     id = nil
  8.      
  9.     function clear()
  10.             term.setBackgroundColor(colors.green)
  11.             term.clear()
  12.     end
  13.  
  14.     function nextLine()
  15.     local hx, hy = term.getCursorPos()
  16.     term.setCursorPos(hx, hy + 1)  
  17.     end
  18.      
  19.     function setReactorPos()
  20.             clear()
  21.             term.setCursorPos(15, 7)
  22.             term.write("Select reactor possition")
  23.             term.setCursorPos(15, 8)
  24.             term.write("1: Top, 2: Bot, 3: Front")
  25.             term.setCursorPos(15, 9)
  26.             term.write("4: Left, 5: Right, 6: Back")
  27.             term.setCursorPos(15, 10)
  28.             reactor_pos = tonumber(read())
  29.             local file = fs.open(reactor_file, "w")
  30.             file.write(reactor_pos)
  31.             file.close()
  32.     end
  33.      
  34.     function saveStateOnDisk()
  35.         local file = fs.open(state_file, "w")
  36.         file.write(command)
  37.         file.close()
  38.     end
  39.  
  40.     function loadStateFromDisk()
  41.         if fs.exists(state_file) then
  42.             local file = fs.open(state_file, "r")
  43.             result = tostring(file.readLine())
  44.             file.close()
  45.             return result
  46.         end
  47.         return nil
  48.     end
  49.  
  50.     function DetectPeripheral(name)      
  51.             for i = 1, 6 do
  52.                     if peripheral.isPresent(sides[i]) and peripheral.getType(sides[i]) == name then
  53.                             return sides[i]
  54.                     end
  55.             end
  56.             return nil      
  57.     end
  58.      
  59.     function rednetControll()
  60.             while true do
  61.                     wifi = DetectPeripheral("modem")
  62.                     rednet.open(wifi)
  63.                     a, b, c = rednet.receive()
  64.                     id = a
  65.                     command = b
  66.                     return b;
  67.             end
  68.     end
  69.      
  70.     function keyControll()
  71.             while true do
  72.                 local sEvent, param = os.pullEvent("monitor_touch")
  73.         if redstone.getOutput(sides[reactor_pos]) == true then
  74.             command = "stop"
  75.             return 0
  76.         else
  77.             command = "start"
  78.             return 0
  79.         end
  80.         end
  81.     end
  82.    
  83.    
  84.     function Menu()
  85.             clear()
  86.             term.setCursorPos(screen_width/4 , screen_height * 0.3)
  87.             local temp = tostring(sides[reactor_pos])
  88.             if redstone.getOutput(temp) then
  89.                     term.write("ALL WORKS!")
  90.             else
  91.                     term.write("NOTHING WORKS!")
  92.             end
  93.         term.setCursorPos(screen_width/4 ,1 + (screen_height * 0.3) )
  94.             term.write("REACTOR IS ON "..sides[reactor_pos])
  95.         term.setCursorPos(screen_width/4 ,2 + (screen_height * 0.3) )
  96.             term.write("PRESS X TO CHANGE")
  97.         return 0
  98.     end
  99.     screen = peripheral.wrap(DetectPeripheral("monitor"))
  100.     --logout protection, reactor position
  101.     if not fs.exists(reactor_file) then
  102.     setReactorPos()
  103.     else
  104.             local file = fs.open(reactor_file, "r")
  105.             reactor_pos = tonumber(file.readLine())
  106.             file.close()
  107.     end
  108.     --logout protection, reactor output
  109.     command = loadStateFromDisk()
  110.     if command == "start" then
  111.         redstone.setOutput(sides[reactor_pos], true)
  112.     end
  113.     --mainloop
  114.     term.redirect(screen)
  115.     screen_width, screen_height = screen.getSize()
  116.     while true do
  117.             Menu()
  118.             parallel.waitForAny(rednetControll, keyControll)
  119.             if command == "start" then
  120.                     redstone.setOutput(sides[reactor_pos], true)
  121.                     saveStateOnDisk()
  122.                             if not id == nil then
  123.                                 rednet.send(id, true)
  124.                                 id = nil
  125.                     end
  126.             elseif command == "stop" then
  127.                     redstone.setOutput(sides[reactor_pos], false)
  128.                     saveStateOnDisk()
  129.                             if not id == nil then
  130.                                 rednet.send(id, false)
  131.                                 id = nil
  132.                     end
  133.             elseif command == "state" then
  134.                     rednet.send(id, redstone.getOutput(sides[reactor_pos]))
  135.             end
  136.     end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement