Advertisement
HR_Shaft

Spectator for Phasor v2+, By 002 SAPP Conversion

Dec 7th, 2014
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.56 KB | None | 0 0
  1. -- Spectators by 002 v1.0
  2. -- Converted to Phasor v2 by H® Shaft
  3.  
  4. -- Script allows admins to spectate without taking or giving damage, and not visible to other players, type '/spectate' to start/stop
  5. -- Addition: show constant message to spectating players "You are spectating. Use /spectate to stop. Use /respawn to respawn. "
  6. -- Can be run as a persistent or non-persistent script
  7.  
  8.  
  9. -- don't edit --
  10. spectators = {}
  11. timer_id = {}
  12. map_reset = false
  13. game_started = false
  14.  
  15. function GetRequiredVersion()
  16.     return 200
  17. end
  18.  
  19. function OnScriptLoad(process, game, persistent)
  20.     Persistent = persistent
  21.     ScriptLoad()
  22. end
  23.  
  24. function OnNewGame(map)
  25.     game_started = false
  26.     ScriptLoad()
  27.     if not new_game_timer then
  28.         new_game_timer = registertimer(0, "NewGameTimer")
  29.     end
  30. end
  31.  
  32. function ScriptLoad()
  33.     if Persistent then
  34.         spectators = {}
  35.         timer_id = {}
  36.         console = {}
  37.         game_started = false
  38.         if not new_game_timer then
  39.             new_game_timer = registertimer(0, "NewGameTimer")
  40.         end        
  41.     end
  42.     spectators = {}
  43.     timer_id = {}  
  44.     spectator = registertimer(20, "Spectator")
  45.     game_started = false
  46.     console = {}
  47.     consoletimer = registertimer(100, "ConsoleTimer")
  48. end
  49.  
  50. function NewGameTimer(id, count)
  51.     game_started = true
  52.     if map_reset == true then
  53.         map_reset = false
  54.     end
  55.     new_game_timer = nil   
  56.     return false
  57. end
  58.  
  59. function OnGameEnd(stage)
  60.     if stage == 1 then
  61.         game_started = false
  62.         if spectator then
  63.             spectator = nil
  64.         end
  65.         for i = 0,15 do
  66.             if getplayer(i) then
  67.                 spectators[gethash(i)] = nil
  68.                 timer_id[i] = nil
  69.             end
  70.         end
  71.     end
  72. end
  73.  
  74. function OnPlayerLeave(player)
  75.     if getplayer(player) then
  76.         if spectators[gethash(player)] then
  77.             spectators[gethash(player)] = nil
  78.             timer_id[player] = nil
  79.         end
  80.     end
  81. end
  82.  
  83. function Spectator(id, count)
  84.     for i = 0,15 do            
  85.         if getplayer(i) then
  86.             if spectators[gethash(i)] and game_started then
  87.                 local m_player = getplayer(i)
  88.                 local z = readfloat(m_player + 0x100)
  89.                 writefloat(m_player + 0x100, z - 1000) 
  90.             end    
  91.         end
  92.     end
  93.     return true
  94. end
  95.  
  96. function OnServerChat(player, type, message)
  97.     local response = nil
  98.     if player then
  99.         if string.lower(message) == "/spectate" then
  100.             Spectate(player)
  101.             response = false
  102.         elseif string.lower(message) == "/respawn" then
  103.             Respawn(player)
  104.             response = false
  105.         end        
  106.     end
  107.     return response
  108. end
  109.  
  110. function OnPlayerSpawn(player, m_objectId)
  111.     if getplayer(player) and game_started then
  112.         if spectators[gethash(player)] then
  113.             local m_player = getplayer(player)
  114.             local m_objectId = getplayerobjectid(player)
  115.             if m_objectId then
  116.                 local m_object = getobject(m_objectId)
  117.                 if m_object then           
  118.                     writedword(m_player + 0x2C, 0) -- respawn time 0
  119.                     writebit(m_object + 0x10, 0, 1) -- uncollideable/invulnerable
  120.                     writebit(m_object + 0x106, 11, 1) -- undamageable except for shields w explosions
  121.                     writebit(m_object + 0x204, 20, 1) -- doesn't drop weapons/grenades on death        
  122.                 end
  123.             end
  124.         end
  125.     end
  126. end
  127.  
  128. function OnVehicleEntry(player, m_vehicleId, seat, mapId, relevant)
  129.     -- blocks spectating players from entering vehicles
  130.     if getplayer(player) and spectators[gethash(player)] then
  131.         return false
  132.     else
  133.         return true
  134.     end
  135.     return nil
  136. end
  137.  
  138. function OnObjectInteraction(player, objId, mapId)
  139.     -- blocks spectating players from picking up weapons and equipment
  140.     local Pass = nil
  141.     local name, type = gettaginfo(mapId)
  142.     if type == "weap" then
  143.         if spectators[gethash(player)] then
  144.             Pass = false
  145.         else
  146.             Pass = true
  147.         end
  148.     elseif type == "eqip" then
  149.         if spectators[gethash(player)] then
  150.             Pass = false
  151.         else
  152.             Pass = true
  153.         end
  154.     end
  155.     return Pass
  156. end
  157.  
  158. function OnDamageLookup(receiving, causing, tagid)
  159.     if receiving and causing and receiving ~= causing then
  160.         local c_player = objectidtoplayer(causing)
  161.         if c_player and spectators[gethash(c_player)] then
  162.             return false
  163.         end
  164.     end
  165. end
  166.  
  167. function Spectate(player)
  168.     if getplayer(player) then
  169.         if isadmin(player) then
  170.             if spectators[gethash(player)] ~= nil then
  171.                 KillPlayerSilently(player)
  172.                 say(getname(player) .. " is no longer spectating.")
  173.                 timer_id[player] = nil             
  174.             else
  175.                 local m_objectId = getplayerobjectid(player)
  176.                 if m_objectId then
  177.                     local m_object = getobject(m_objectId)
  178.                     local m_player = getplayer(player)
  179.                     if m_object then           
  180.                         say(getname(player) .. " is now spectating.")
  181.                         privatesay(player, "Use /spectate to stop. Use /respawn to respawn.")
  182.                         timer_id[player] = registertimer(1000, "TellEm", player)
  183.                         writedword(m_player + 0x2C, 0)
  184.                         KillPlayerSilently(player)
  185.                     end
  186.                 end
  187.             end
  188.             if spectators[gethash(player)] then
  189.                 spectators[gethash(player)] = nil
  190.             else
  191.                 spectators[gethash(player)] = true
  192.             end
  193.         else
  194.             privatesay(player, "You are not allowed to spectate.")
  195.         end
  196.     end
  197. end
  198.  
  199. function TellEm(id, count, player)
  200.     if getplayer(player) and game_started then
  201.         if spectators[gethash(player)] then
  202.             local m_objectId = getplayerobjectid(player)
  203.             if m_objectId then
  204.                 local m_object = getobject(m_objectId)
  205.                 if m_object then
  206.                     sendconsoletext(player, "You are spectating. Use /spectate to stop. Use /respawn to respawn. ",  0.11, 1)
  207.                 end
  208.             end
  209.             return true
  210.         else
  211.             return false
  212.         end
  213.     end
  214. end
  215.  
  216. function Respawn(player)
  217.     if getplayer(player) then
  218.         if (spectators[gethash(player)] == nil) then
  219.             privatesay(player, "You are not spectating.")
  220.             return
  221.         end
  222.         privatesay(player, "Now respawning...")
  223.         KillPlayerSilently(player)
  224.     end
  225. end
  226.  
  227. function KillPlayerSilently(player)
  228.     if getplayer(player) then
  229.         local m_player = getplayer(player)
  230.         local old_value = readword(m_player + 0xD4)    
  231.         writeword(m_player + 0xD4, 0xFFFF)
  232.         kill(player)
  233.         writeword(m_player + 0xD4, old_value)
  234.         writedword(m_player + 0x2C, 0)
  235.     end
  236. end
  237.  
  238. function OnServerCommand(player, command)
  239.     local allow = nil
  240.     local cmd = tokenizecmdstring(command)
  241.     local tokencount = #cmd
  242.     if tokencount > 0 then
  243.    
  244.         if cmd[1] == "sv_map_reset" then
  245.             map_reset = true       
  246.             ScriptLoad()
  247.             privatesay(player, "**RESET** The game has been reset and scripts reloaded. ")     
  248.             for i=0,15 do  
  249.                 if getplayer(i) then
  250.                     spectators[gethash(i)] = {}
  251.                     timer_id[i] = nil              
  252.                     kill(i)
  253.                     privatesay(i, "**RESET** The game has been reset. ")
  254.                 end
  255.             end
  256.        
  257.             allow = true
  258.            
  259.         elseif cmd[1] == "sv_script_reload" then
  260.             map_reset = true
  261.             ScriptLoad()
  262.             privatesay(player, "**RELOAD** The game has been reset and scripts reloaded. ")
  263.             for i=0,15 do  
  264.                 if getplayer(i) then
  265.                     spectators[gethash(i)] = {}
  266.                     timer_id[i] = nil              
  267.                     kill(i)
  268.                     privatesay(i, "**RELOAD** The game has been reset because scripts were reloaded, your score remains.")
  269.                 end
  270.             end        
  271.             allow = true
  272.         end
  273.        
  274.     end
  275.     return allow
  276. end
  277.  
  278. -- Nuggets Sendconsoletext
  279. console = {}
  280. console.__index = console
  281. consoletimer = registertimer(100, "ConsoleTimer")
  282. phasor_sendconsoletext = sendconsoletext
  283.  
  284. function sendconsoletext(player, message, time, order, align, func)
  285.     console[player] = console[player] or {}
  286.     local temp = {}
  287.     temp.player = player
  288.     temp.id = nextid(player, order)
  289.     temp.message = message or ""
  290.     temp.time = time or 5
  291.     temp.remain = temp.time
  292.     temp.align = align or "left"
  293.     temp.func = func
  294.     console[player][temp.id] = temp
  295.     setmetatable(console[player][temp.id], console)
  296.     return console[player][temp.id]
  297. end
  298.  
  299. function nextid(player, order)
  300.     if not order then
  301.         local x = 0
  302.         for k,v in pairs(console[player]) do
  303.             if k > x + 1 then
  304.                 return x + 1
  305.             end
  306.             x = x + 1
  307.         end
  308.         return x + 1
  309.     else
  310.         local original = order
  311.         while console[player][order] do
  312.             order = order + 0.001
  313.             if order == original + 0.999 then break end
  314.         end
  315.         return order
  316.     end
  317. end
  318.  
  319. function getmessage(player, order)
  320.     if console[player] then
  321.         if order then
  322.             return console[player][order]
  323.         end
  324.     end
  325. end
  326.  
  327. function getmessages(player)
  328.     return console[player]
  329. end
  330.  
  331. function getmessageblock(player, order)
  332.     local temp = {}
  333.     for k,v in pairs(console[player]) do
  334.         if k >= order and k < order + 1 then
  335.             table.insert(temp, console[player][k])
  336.         end
  337.     end
  338.     return temp
  339. end
  340.  
  341. function console:getmessage()
  342.     return self.message
  343. end
  344.  
  345. function console:append(message, reset)
  346.     if console[self.player] then
  347.         if console[self.player][self.id] then
  348.             if getplayer(self.player) then
  349.                 if reset then
  350.                     if reset == true then
  351.                         console[self.player][self.id].remain = console[self.player][self.id].time
  352.                     elseif tonumber(reset) then
  353.                         console[self.player][self.id].time = tonumber(reset)
  354.                         console[self.player][self.id].remain = tonumber(reset)
  355.                     end
  356.                 end
  357.                
  358.                 console[self.player][self.id].message = message or ""
  359.                 return true
  360.             end
  361.         end
  362.     end
  363. end
  364.  
  365. function console:shift(order)
  366.     local temp = console[self.player][self.id]
  367.     console[self.player][self.id] = console[self.player][order]
  368.     console[self.player][order] = temp
  369. end
  370.  
  371. function console:pause(time)
  372.     console[self.player][self.id].pausetime = time or 5
  373. end
  374.  
  375. function console:delete()
  376.     console[self.player][self.id] = nil
  377. end
  378.  
  379. function ConsoleTimer(id, count)
  380.     for i,_ in opairs(console) do
  381.         if tonumber(i) then
  382.             if getplayer(i) then
  383.                 for k,v in opairs(console[i]) do
  384.                     if console[i][k].pausetime then
  385.                         console[i][k].pausetime = console[i][k].pausetime - 0.1
  386.                         if console[i][k].pausetime <= 0 then
  387.                             console[i][k].pausetime = nil
  388.                         end
  389.                     else
  390.                         if console[i][k].func then
  391.                             if not console[i][k].func(i) then
  392.                                 console[i][k] = nil
  393.                             end
  394.                         else
  395.                             console[i][k].remain = console[i][k].remain - 0.1
  396.                             if console[i][k].remain <= 0 then
  397.                                 console[i][k] = nil
  398.                             end
  399.                         end
  400.                     end
  401.                 end
  402.                 if table.len(console[i]) > 0 then
  403.                    
  404.                     local paused = 0
  405.                     for k,v in pairs(console[i]) do
  406.                         if console[i][k].pausetime then
  407.                             paused = paused + 1
  408.                         end
  409.                     end
  410.                    
  411.                     if paused < table.len(console[i]) then
  412.                         local str = ""
  413.                         for i = 0,30 do
  414.                             str = str .. " \n"
  415.                         end
  416.                        
  417.                         phasor_sendconsoletext(i, str)
  418.                        
  419.                         for k,v in opairs(console[i]) do
  420.                             if not console[i][k].pausetime then
  421.                                 if console[i][k].align == "right" or console[i][k].align == "center" then
  422.                                     phasor_sendconsoletext(i, consolecenter(console[i][k].message))
  423.                                 else
  424.                                     phasor_sendconsoletext(i, console[i][k].message)
  425.                                 end
  426.                             end
  427.                         end
  428.                     end
  429.                 end
  430.             else
  431.                 console[i] = nil
  432.             end
  433.         end
  434.     end
  435.     return true
  436. end
  437.  
  438. function consolecenter(text)
  439.     if text then
  440.         local len = string.len(text)
  441.         for i = len + 1, 78 do
  442.             text = " " .. text
  443.         end
  444.        
  445.         return text
  446.     end
  447. end
  448.  
  449. function opairs(t)
  450.     local keys = {}
  451.     for k,v in pairs(t) do
  452.         table.insert(keys, k)
  453.     end    
  454.     table.sort(keys,
  455.     function(a,b)
  456.         if type(a) == "number" and type(b) == "number" then
  457.             return a < b
  458.         end
  459.         an = string.lower(tostring(a))
  460.         bn = string.lower(tostring(b))
  461.         if an ~= bn then
  462.             return an < bn
  463.         else
  464.             return tostring(a) < tostring(b)
  465.         end
  466.     end)
  467.     local count = 1
  468.     return function()
  469.         if table.unpack(keys) then
  470.             local key = keys[count]
  471.             local value = t[key]
  472.             count = count + 1
  473.             return key,value
  474.         end
  475.     end
  476. end
  477.  
  478. function table.len(t)
  479.     local count = 0
  480.     for k,v in pairs(t) do
  481.         count = count + 1
  482.     end
  483.     return count
  484. end
  485.  
  486. -- Created by H® Shaft.
  487. -- Thanks to Oxide, AelitePrime, Nugget & Wizard - and 002!
  488. -- Visit http://halorace.org/forum/index.php?topic=514.0 or
  489. -- Visit http://pastebin.com/u/HR_Shaft for more phasor scripts
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement