sm-zombiereloaded-3/src/zr/spawnprotect.inc

177 lines
4.4 KiB
SourcePawn

/*
* ============================================================================
*
* Zombie:Reloaded
*
* File: spawnprotect.inc
* Type: Module
* Description: Protects late-joining players from zombies for x seconds.
*
* ============================================================================
*/
/**
* Array for storing spawn protect timer handles per client.
*/
new Handle:tSpawnProtect[MAXPLAYERS + 1];
/**
* Array for storing time left for spawn protection per client.
*/
new pSpawnProtectTime[MAXPLAYERS + 1];
/**
* Client is joining the server.
*
* @param client The client index.
*/
SpawnProtectClientInit(client)
{
// Reset timer handle.
tSpawnProtect[client] = INVALID_HANDLE;
}
/**
* Client is spawning into the game.
*
* @param client The client index.
*/
SpawnProtectOnClientSpawn(client)
{
// Disable spawn protection on client.
bInfectImmune[client][INFECT_TYPE_NORMAL] = false;
// If timer is currently running, kill it.
if (tSpawnProtect[client] != INVALID_HANDLE)
{
KillTimer(tSpawnProtect[client]);
}
// Reset timer handle.
tSpawnProtect[client] = INVALID_HANDLE;
}
/**
* Client is spawning into the game. *Post
*
* @param client The client index.
*/
SpawnProtectOnClientSpawnPost(client)
{
// If client isn't on a team, then stop.
if (!ZRIsClientOnTeam(client))
{
return;
}
// If zombie hasn't spawned, then stop.
if (!g_bZombieSpawned)
{
return;
}
// If protect cvar is disabled, then stop.
new bool:protect = GetConVarBool(g_hCvarsList[CVAR_SPAWNPROTECT]);
if (!protect)
{
return;
}
// If client is a zombie, then stop.
if (InfectIsClientInfected(client))
{
return;
}
// Set spawn protect flag on client.
bInfectImmune[client][INFECT_TYPE_NORMAL] = true;
// Get spawn protect attribute cvars.
new Float:speed = GetConVarFloat(g_hCvarsList[CVAR_SPAWNPROTECT_SPEED]);
new alpha = GetConVarInt(g_hCvarsList[CVAR_SPAWNPROTECT_ALPHA]);
// Set spawn protect attributes.
ToolsSetClientLMV(client, speed);
ToolsSetClientAlpha(client, alpha);
// Set time left to zr_protect_time's value.
new protect_time = GetConVarInt(g_hCvarsList[CVAR_SPAWNPROTECT_TIME]);
pSpawnProtectTime[client] = protect_time;
// Tell client they are being protected.
TranslationPrintToChat(client, "Spawn protection begin", protect_time);
// Send time left in a hud message.
TranslationPrintHUDText(client, "Spawn Protect", pSpawnProtectTime[client]);
// Start repeating timer.
tSpawnProtect[client] = CreateTimer(1.0, SpawnProtectTimer, client, TIMER_FLAG_NO_MAPCHANGE|TIMER_REPEAT);
}
/**
* Client has been killed.
*
* @param client The client index.
*/
SpawnProtectOnClientDeath(client)
{
// If timer is running, kill it.
if (tSpawnProtect[client] != INVALID_HANDLE)
{
KillTimer(tSpawnProtect[client]);
}
// Reset timer handle.
tSpawnProtect[client] = INVALID_HANDLE;
}
/**
* Timer callback function, countdown for spawn protection.
*
* @param timer The timer handle.
* @param client The client index.
*/
public Action:SpawnProtectTimer(Handle:timer, any:client)
{
// If client leaves, then stop timer.
if (!IsClientInGame(client))
{
return Plugin_Stop;
}
// If client has become a zombie, then stop timer.
if (!InfectIsClientHuman(client))
{
return Plugin_Stop;
}
// Decrement time left.
pSpawnProtectTime[client]--;
// Print time left to client.
TranslationPrintHUDText(client, "Spawn Protect", pSpawnProtectTime[client]);
// Time has expired.
if (pSpawnProtectTime[client] <= 0)
{
// Remove protect flag.
bInfectImmune[client][INFECT_TYPE_NORMAL] = false;
// Tell client spawn protection is over.
TranslationPrintHUDText(client, "Spawn protection end");
// Fix attributes.
ToolsSetClientAlpha(client, ClassGetAlphaInitial(client));
ToolsSetClientLMV(client, ClassGetSpeed(client));
// Clear timer handle.
tSpawnProtect[client] = INVALID_HANDLE;
// Stop timer.
return Plugin_Stop;
}
// Allow timer to continue repeating.
return Plugin_Continue;
}