Guest User

Untitled

a guest
Sep 24th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[ Gate Connection ]]--
  2. --[[    Countdown    ]]--
  3. --[[      Timer      ]]--
  4. --[[     by  Dog     ]]--
  5. --[[       aka       ]]--
  6. --[[  HydrantHunter  ]]--
  7. --[[   pastebin:     ]]--
  8. --[[     T6xmhsJB    ]]--
  9.  
  10. local nHours = 0    --# leave this at 0
  11. local nMinutes = 1  --# 59 or less
  12. local nSeconds = 0 --# 59 or less
  13. local totalTime, hours, minutes, seconds, sHours, sMinutes, sSeconds, sTenths, countdownTimer
  14. local timer, sgEvent, event, _, newState, oldState, direction
  15. local lcGate, continue, SG_OutgoingOnly = false, false, false
  16. local gate = peripheral.find("stargate")
  17. if not gate then lcGate = true gate = peripheral.find("StargateBase") end
  18. if not gate then error("No Stargate located") end
  19. local mon = {
  20.   peripheral.find("monitor",
  21.     function(name, object)
  22.       object.setTextScale(4)
  23.       local x, y = object.getSize()
  24.       if x == 7 and y == 1 then
  25.         object.clear()
  26.         object.setCursorPos(1, 1)
  27.         if object.isColor() then object.setTextColor(colors.red) end
  28.         object.write("00:00:0")
  29.         return true
  30.       else
  31.         return false
  32.       end
  33.     end
  34.   )
  35. }
  36. term.clear()
  37. term.setCursorPos(1, 1)
  38. if term.isColor() then term.setTextColor(colors.red) end
  39. term.write("00:00:0")
  40.  
  41. while true do
  42.   if lcGate then
  43.     os.pullEvent("connect")
  44.     continue = true
  45.   else
  46.     sgEvent, _, newState, oldState = os.pullEvent("sgStargateStateChange")
  47.     if newState == "Connected" then
  48.       if SG_OutgoingOnly then
  49.         newState, _, direction = gate.stargateState()
  50.         if newState == "Connected" and direction == "Outgoing" then
  51.           continue = true
  52.         end
  53.       else
  54.         continue = true
  55.       end
  56.     end
  57.   end
  58.   if continue then
  59.     if mon[1] then
  60.       for i = 1, #mon do
  61.         mon[i].setTextColor(colors.white)
  62.       end
  63.     end
  64.     term.setTextColor(colors.white)
  65.     totalTime = (nHours * 36000) + (nMinutes * 600) + (nSeconds * 10)
  66.     for i = totalTime, 0, -1 do
  67.       hours = totalTime > 35999 and math.floor(totalTime / 36000) or 0
  68.       sHours = tostring(hours)
  69.       minutes = totalTime > 599 and math.floor((totalTime - (hours * 36000)) / 600) or 0
  70.       sMinutes = minutes > 9 and tostring(minutes) or "0" .. tostring(minutes)
  71.       seconds = totalTime > 9 and math.floor((totalTime - ((hours * 36000) + (minutes * 600))) / 10) or 0
  72.       sSeconds = seconds > 9 and tostring(seconds) or "0" .. tostring(seconds)
  73.       sTenths = tostring(math.floor(totalTime - ((hours * 36000) + (minutes * 600) + (seconds * 10))))
  74.       term.setCursorPos(1, 1)
  75.       if totalTime == 0 and term.isColor() then term.setTextColor(colors.red) end
  76.       term.write(sMinutes .. ":" .. sSeconds .. ":" .. sTenths)
  77.       if mon[1] then
  78.         for i = 1, #mon do
  79.           mon[i].setCursorPos(1, 1)
  80.           if totalTime == 0 and mon[i].isColor() then mon[i].setTextColor(colors.red) end
  81.           mon[i].write(sMinutes .. ":" .. sSeconds .. ":" .. sTenths)
  82.         end
  83.       end
  84.       if totalTime > 0 then
  85.         countdownTimer = os.startTimer(0.1)
  86.         while true do
  87.           event, timer, newState = os.pullEvent()
  88.           if event == "timer" and timer == countdownTimer then
  89.             totalTime = totalTime - 1
  90.             break
  91.           elseif event == "disconnect" or (event == "sgStargateStateChange" and newState == "Idle") then
  92.             totalTime = 0
  93.             if term.isColor() then term.setTextColor(colors.red) end
  94.             term.setCursorPos(1, 1)
  95.             term.write("00:00:0")
  96.             for i = 1, #mon do
  97.               if mon[i].isColor() then mon[i].setTextColor(colors.red) end
  98.               mon[i].setCursorPos(1, 1)
  99.               mon[i].write("00:00:0")
  100.             end
  101.             break
  102.           end
  103.         end
  104.       end
  105.       if totalTime == 0 then
  106.         continue = false
  107.         if lcGate then
  108.           pcall(gate.disengageStargate)
  109.         else
  110.           pcall(gate.disconnect)
  111.         end
  112.       end
  113.     end
  114.   end
  115. end
Add Comment
Please, Sign In to add comment