Advertisement
Tatantyler

Missile Silo Control - Peripheral Cable Edition

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