Advertisement
Tatantyler

Missile Silo Control

Nov 5th, 2012
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.00 KB | None | 0 0
  1. local oldPull = os.pullEvent
  2. os.pullEvent = os.pullEventRaw
  3. local silos = {}
  4. local selected = 1
  5.  
  6. print("Detecting silos...")
  7. for i,v in ipairs(rs.getSides()) do
  8.     if peripheral.getType(v) == "ICBMLauncher" then
  9.         print("Silo found on side:"..v)
  10.         table.insert(silos, {v, peripheral.wrap(v)})
  11.     end
  12. end
  13.  
  14. if #silos == 0 then
  15.     print("No silos detected.")
  16.     print("Please place a launcher control panel next to the computer.")
  17.         os.sleep(2)
  18.     return
  19. elseif #silos == 1 then
  20.     print("Found 1 silo.")
  21. else
  22.     print("Found "..#silos.." silos.")
  23. end
  24. os.sleep(0.75)
  25.  
  26. local function getPW()
  27.     if fs.exists("launchCode") then
  28.         term.clear()
  29.         term.setCursorPos(5, 8)
  30.         write("Please enter a launch code.")
  31.         local handle = fs.open("launchCode", "r")
  32.         local code = handle.readAll()
  33.         handle.close()
  34.         local userPW = read()
  35.         term.clear()
  36.         term.setCursorPos(1,1)
  37.         return (code == userPW)
  38.     else
  39.         return true
  40.     end
  41. end
  42.  
  43. local function printColored(...)
  44.     for i=1, arg["n"] do
  45.         if type(arg[i]) == "string" then
  46.             write(arg[i])
  47.         elseif type(arg[i]) == "number" then
  48.             if term.isColor() then
  49.                 term.setTextColor(arg[i])
  50.             end
  51.         end
  52.        
  53.     end
  54.     print("")
  55.     term.setTextColor(colors.white)
  56. end
  57.  
  58. while true do
  59.     term.clear()
  60.     term.setCursorPos(1,1)
  61.     printColored(colors.white, "Silo ", colors.lime, tostring(selected), colors.white, " selected.")
  62.     print("")
  63.     print("Details: ")
  64.     print("")
  65.     printColored("Frequency: ", colors.lime, tostring(silos[selected][2].getFrequency()))
  66.     local targetX, targetY, targetZ = silos[selected][2].getTarget()
  67.     print("Targeted: ")
  68.     printColored("X: ", colors.lime, tostring(targetX))
  69.     printColored("Y: ", colors.lime, tostring(targetY))
  70.     printColored("Z: ", colors.lime, tostring(targetZ))
  71.     print("")
  72.     print("Options: ")
  73.     printColored("1. ", colors.lime, "Select Silo")
  74.     printColored("2. ", colors.orange, "Set Silo Target")
  75.     printColored("3. ", colors.orange, "Set Silo Frequency")
  76.     printColored("4. ", colors.red, "Launch")
  77.     printColored("5. ", colors.red, "Launch All")
  78.     printColored("6. ", colors.orange, "Set All Silo Frequencies")
  79.     printColored("7. ", colors.orange, "Set All Silo Targets")
  80.     local event, key = os.pullEvent("key")
  81.     if key == 2 then
  82.         term.clear()
  83.         term.setCursorPos(1,1)
  84.         print("Detected silos:")
  85.         for i,v in ipairs(silos) do
  86.             print(i..". Silo on: "..v[1].." side")
  87.         end
  88.         while true do
  89.             local event, key = os.pullEvent("key")
  90.             if key >= 2 and key <=7 then
  91.                 if silos[key-1] then
  92.                     selected = key-1
  93.                     break
  94.                 end
  95.             end
  96.         end
  97.     elseif key == 3 then
  98.         local x, y, z = 0
  99.         while true do
  100.             term.setCursorPos(1,7)
  101.             term.clearLine()
  102.             write("X: ")
  103.             local selX = read()
  104.             if tonumber(selX) then
  105.                 x = tonumber(selX)
  106.                 break
  107.             end
  108.         end
  109.         while true do
  110.             term.setCursorPos(1,8)
  111.             term.clearLine()
  112.             write("Y: ")
  113.             local selY = read()
  114.             if tonumber(selY) then
  115.                 y = tonumber(selY)
  116.                 break
  117.             end
  118.         end
  119.         while true do
  120.             term.setCursorPos(1,9)
  121.             term.clearLine()
  122.             write("Z: ")
  123.             local selZ = read()
  124.             if tonumber(selZ) then
  125.                 z = tonumber(selZ)
  126.                 break
  127.             end
  128.         end
  129.         silos[selected][2].setTarget(x,y,z)
  130.     elseif key == 4 then
  131.         while true do
  132.             term.setCursorPos(1,5)
  133.             term.clearLine()
  134.             write("Frequency: ")
  135.             local selFreq = read()
  136.             if tonumber(selFreq) then
  137.                 silos[selected][2].setFrequency(tonumber(selFreq))
  138.                 break
  139.             end
  140.         end
  141.     elseif key == 5 then
  142.         if getPW() then
  143.             for i=10, 1, -1 do
  144.                 term.clear()
  145.                 term.setCursorPos(12,9)
  146.                 write("Launching in: "..i.." seconds...")
  147.                 os.sleep(1)
  148.             end
  149.             silos[selected][2].launch()
  150.         else
  151.             term.clear()
  152.             term.setCursorPos(12,9)
  153.             print("Incorrect code!")
  154.             os.sleep(2)
  155.         end
  156.     elseif key == 6 then
  157.         if getPW() then
  158.             for i=10, 1, -1 do
  159.                 term.clear()
  160.                 term.setCursorPos(12,9)
  161.                 write("Launching in: "..i.." seconds...")
  162.                 os.sleep(1)
  163.             end
  164.             for i,v in ipairs(silos) do
  165.                 v[2].launch()
  166.                 os.sleep(3)
  167.             end
  168.         else
  169.             term.clear()
  170.             term.setCursorPos(12,9)
  171.             print("Incorrect code!")
  172.             os.sleep(2)
  173.         end
  174.     elseif key == 7 then
  175.         while true do
  176.             term.setCursorPos(1,5)
  177.             term.clearLine()
  178.             write("Frequency: ")
  179.             local selFreq = read()
  180.             if tonumber(selFreq) then
  181.                 for i,v in ipairs(silos) do
  182.                     v[2].setFrequency(tonumber(selFreq))
  183.                 end
  184.                 break
  185.             end
  186.         end
  187.     elseif key == 8 then
  188.         local x, y, z = 0
  189.         while true do
  190.             term.setCursorPos(1,7)
  191.             term.clearLine()
  192.             write("X: ")
  193.             local selX = read()
  194.             if tonumber(selX) then
  195.                 x = tonumber(selX)
  196.                 break
  197.             end
  198.         end
  199.         while true do
  200.             term.setCursorPos(1,8)
  201.             term.clearLine()
  202.             write("Y: ")
  203.             local selY = read()
  204.             if tonumber(selY) then
  205.                 y = tonumber(selY)
  206.                 break
  207.             end
  208.         end
  209.         while true do
  210.             term.setCursorPos(1,9)
  211.             term.clearLine()
  212.             write("Z: ")
  213.             local selZ = read()
  214.             if tonumber(selZ) then
  215.                 z = tonumber(selZ)
  216.                 break
  217.             end
  218.         end
  219.         for i,v in ipairs(silos) do
  220.             v[2].setTarget(x,y,z)
  221.         end
  222.     end
  223. end
  224.  
  225. os.pullEvent = oldPull
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement