giintv

CoproReactor

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