Skip to content

Commit

Permalink
Cleaned up code extensively and added notes on attempted workaround o…
Browse files Browse the repository at this point in the history
…n my respawn delay hack
  • Loading branch information
MrOats committed Apr 7, 2017
1 parent 7b89a16 commit 96e6edc
Showing 1 changed file with 50 additions and 34 deletions.
84 changes: 50 additions & 34 deletions SpectateMode.as
Expand Up @@ -7,21 +7,23 @@ file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

/*
Current Status: Stable, but in development.
Documentation: https://github.com/MrOats/AngelScript_SC_Plugins/wiki/SpectateMode.as
Current Status: Stable.
Thank you to all those who have assisted me with this plugin!
*/
CScheduledFunction@ g_pKeepSpec = null;


//Globals

CScheduledFunction@ g_pSetRespawn = null;
const int g_MAXPLAYERS = g_Engine.maxClients;
array<bool> pSpectatePlease(g_MAXPLAYERS,false);
array<bool> bSpectatePlease(8, false);
const float MAX_FLOAT = Math.FLOAT_MAX;
CClientCommand spectate("spectate", "Say \"spectate on\" to turn on and \"spectate off\" to turn off", @toggleSpectate );

//Config

bool adminOnly = false;

//End Config
//Main Functions

void PluginInit()
{
Expand All @@ -31,14 +33,17 @@ void PluginInit()
g_Hooks.RegisterHook(Hooks::Player::ClientDisconnect,@RemoveSpecStatus);
g_Hooks.RegisterHook(Hooks::Game::MapChange,@EndTimerFuncs);
g_Hooks.RegisterHook(Hooks::Player::PlayerSpawn,@CheckSpectate);
/* g_Hooks.RegisterHook(Hooks::Player::PlayerCanRespawn,@PreventRespawn); <-- See function for notes */

}

void MapInit()
{

for (uint i = 0; i < pSpectatePlease.length(); i++) {
pSpectatePlease[i] = false;
bSpectatePlease.resize(g_Engine.maxClients);
for (uint i = 0; i < bSpectatePlease.length(); i++)
{
bSpectatePlease[i] = false;
}

if(g_pSetRespawn !is null)
Expand All @@ -53,32 +58,16 @@ void toggleSpectate(const CCommand@ pArguments)

CBasePlayer@ pPlayer = g_ConCommandSystem.GetCurrentPlayer();

if (pSpectatePlease[pPlayer.entindex()])
if (bSpectatePlease[pPlayer.entindex() - 1])
ExitSpectate(pPlayer);
else EnterSpectate(pPlayer);

}

void CheckObserver()
{

for (int i = 1; i <= g_MAXPLAYERS; i++)
{

CBasePlayer@ pPlayer = g_PlayerFuncs.FindPlayerByIndex(i);

if (pPlayer !is null)
if ( (!pPlayer.GetObserver().IsObserver()) && (pSpectatePlease[pPlayer.entindex() - 1]) )
pPlayer.GetObserver().StartObserver(pPlayer.pev.origin, pPlayer.pev.angles, false);

}

}

void SetRespawnTime()
{
/*

/*
The reason why we are still using a Scheduler that
runs constantly at a <1 second interval is because
the game isn't setting the player's respawn timer when
Expand All @@ -88,15 +77,15 @@ the player to Observer Mode.
This "hack" will remain until a proper "run-once" solution is set on a
per-player basis.
*/

for (int i = 1; i <= g_MAXPLAYERS; i++)

for (int i = 1; i <= g_Engine.maxClients; i++)
{

CBasePlayer@ pPlayer = g_PlayerFuncs.FindPlayerByIndex(i);

if ((pPlayer !is null) && (pSpectatePlease[pPlayer.entindex() - 1]))
if ((pPlayer !is null) && (bSpectatePlease[pPlayer.entindex() - 1]))
pPlayer.m_flRespawnDelayTime = MAX_FLOAT;

}
Expand All @@ -107,9 +96,9 @@ void EnterSpectate(CBasePlayer@ pPlayer)
{

if (adminOnly && (g_PlayerFuncs.AdminLevel(pPlayer) >= ADMIN_YES) )
pSpectatePlease[pPlayer.entindex() - 1] = true;
bSpectatePlease[pPlayer.entindex() - 1] = true;
else if (!adminOnly)
pSpectatePlease[pPlayer.entindex() - 1] = true;
bSpectatePlease[pPlayer.entindex() - 1] = true;

if(!pPlayer.GetObserver().IsObserver())
pPlayer.GetObserver().StartObserver( pPlayer.pev.origin, pPlayer.pev.angles, false );
Expand All @@ -119,8 +108,8 @@ void EnterSpectate(CBasePlayer@ pPlayer)
void ExitSpectate(CBasePlayer@ pPlayer)
{

//g_Game.AlertMessage(at_console, "Exiting SpectateMode");
pSpectatePlease[pPlayer.entindex() - 1] = false;
//g_Game.AlertMessage(at_console, "Exiting SpectateMode\n");
bSpectatePlease[pPlayer.entindex() - 1] = false;
//Reset the player's respawn time by respawning and killing.
g_PlayerFuncs.RespawnPlayer(pPlayer, true, true);
g_AdminControl.KillPlayer(pPlayer, 3);
Expand All @@ -135,10 +124,37 @@ HookReturnCode RemoveSpecStatus(CBasePlayer@ pPlayer)

}

/*
Having issues with below code as it still attempts to respawn player,
but they get sent back to being dead and so they are in
observer mode above their attempted respawn spot.
HookReturnCode PreventRespawn(CBasePlayer@ pPlayer, bool& out bCanRespawn)
{
if (bSpectatePlease[pPlayer.entindex() - 1])
{
bCanRespawn = false;
return HOOK_HANDLED;
}
else
{
bCanRespawn = true;
return HOOK_HANDLED;
}
}
*/

HookReturnCode CheckSpectate(CBasePlayer@ pPlayer)
{

if (pSpectatePlease[pPlayer.entindex() - 1])
if (bSpectatePlease[pPlayer.entindex() - 1])
{

pPlayer.GetObserver().StartObserver( pPlayer.pev.origin, pPlayer.pev.angles, false );
Expand Down

0 comments on commit 96e6edc

Please sign in to comment.