From c604047688839cbbeb045f95c3ec3ff8f4fdc6d0 Mon Sep 17 00:00:00 2001 From: zaCade Date: Wed, 17 Jul 2019 15:47:38 +0200 Subject: [PATCH] PlayerRankings: Update rotating code, less prone to errors? Less global ints is gud yes? --- PlayerRankings/scripting/PlayerRankings.sp | 127 +++++++++------------ 1 file changed, 53 insertions(+), 74 deletions(-) diff --git a/PlayerRankings/scripting/PlayerRankings.sp b/PlayerRankings/scripting/PlayerRankings.sp index 8db68fd2..11440e89 100644 --- a/PlayerRankings/scripting/PlayerRankings.sp +++ b/PlayerRankings/scripting/PlayerRankings.sp @@ -1,9 +1,9 @@ +#include #include #include #include #include #include -#include #define SPECMODE_NONE 0 #define SPECMODE_FIRSTPERSON 4 @@ -28,10 +28,6 @@ ConVar g_hCVar_ProtectionMinimal2; ConVar g_hCVar_ProtectionMinimal3; /* INTERGERS */ -int g_iCrownEntity = INVALID_ENT_REFERENCE; -int g_iCrownParent = INVALID_ENT_REFERENCE; -int g_iSkullEntity = INVALID_ENT_REFERENCE; -int g_iSkullParent = INVALID_ENT_REFERENCE; int g_iDialogLevel = 100000; int g_iPlayerWinner[6]; @@ -82,7 +78,6 @@ public void OnPluginStart() CreateTimer(0.1, UpdateScoreboard, INVALID_HANDLE, TIMER_REPEAT); CreateTimer(0.1, UpdateDialog, INVALID_HANDLE, TIMER_REPEAT); - CreateTimer(0.1, UpdateCrowns, INVALID_HANDLE, TIMER_REPEAT); RegConsoleCmd("sm_togglecrown", OnToggleCrown); RegConsoleCmd("sm_toggleskull", OnToggleCrown); @@ -93,7 +88,6 @@ public void OnPluginStart() HookEvent("round_end", OnRoundEnding); HookEvent("player_hurt", OnClientHurt); HookEvent("player_spawn", OnClientSpawn); - HookEvent("player_death", OnClientDeath); SetCookieMenuItem(MenuHandler_CookieMenu, 0, "Player Rankings"); } @@ -383,34 +377,6 @@ public Action UpdateDialog(Handle timer) g_iDialogLevel--; } -//---------------------------------------------------------------------------------------------------- -// Purpose: -//---------------------------------------------------------------------------------------------------- -public Action UpdateCrowns(Handle timer) -{ - if (g_bEngineCSGO) - return; - - if (IsValidEntity(g_iCrownEntity) && IsValidEntity(g_iCrownParent) && IsClientInGame(g_iCrownParent)) - { - float fAngles[3]; - GetClientAbsAngles(g_iCrownParent, fAngles); - - fAngles[0] += 8.0; - fAngles[2] += 5.5; - - TeleportEntity(g_iCrownEntity, NULL_VECTOR, fAngles, NULL_VECTOR); - } - - if (IsValidEntity(g_iSkullEntity) && IsValidEntity(g_iSkullParent) && IsClientInGame(g_iSkullParent)) - { - float fAngles[3]; - GetClientAbsAngles(g_iSkullParent, fAngles); - - TeleportEntity(g_iSkullEntity, NULL_VECTOR, fAngles, NULL_VECTOR); - } -} - //---------------------------------------------------------------------------------------------------- // Purpose: //---------------------------------------------------------------------------------------------------- @@ -623,15 +589,10 @@ public void OnClientSpawn(Event hEvent, const char[] sEvent, bool bDontBroadcast int steamAccountID = GetSteamAccountID(client); if (g_iPlayerWinner[0] == steamAccountID) - { CreateTimer(7.0, OnClientSpawnPostCrown, GetClientSerial(client), TIMER_FLAG_NO_MAPCHANGE); - KillCrown(); - } + else if (g_iPlayerWinner[3] == steamAccountID) - { CreateTimer(7.0, OnClientSpawnPostSkull, GetClientSerial(client), TIMER_FLAG_NO_MAPCHANGE); - KillSkull(); - } } } @@ -644,10 +605,9 @@ public Action OnClientSpawnPostCrown(Handle timer, int serial) if((client = GetClientFromSerial(serial)) == -1) return; - if (!IsClientInGame(client) || IsFakeClient(client) || !IsPlayerAlive(client)) + if (!IsClientInGame(client) || !IsPlayerAlive(client)) return; - KillCrown(); SpawnCrown(client); } @@ -660,30 +620,12 @@ public Action OnClientSpawnPostSkull(Handle timer, int serial) if((client = GetClientFromSerial(serial)) == -1) return; - if (!IsClientInGame(client) || IsFakeClient(client) || !IsPlayerAlive(client)) + if (!IsClientInGame(client) || !IsPlayerAlive(client)) return; - KillSkull(); SpawnSkull(client); } -//---------------------------------------------------------------------------------------------------- -// Purpose: -//---------------------------------------------------------------------------------------------------- -public void OnClientDeath(Event hEvent, const char[] sEvent, bool bDontBroadcast) -{ - int client = GetClientOfUserId(hEvent.GetInt("userid")); - - if (!IsPlayerAlive(client)) - { - if (g_iCrownParent == client) - KillCrown(); - - if (g_iSkullParent == client) - KillSkull(); - } -} - //---------------------------------------------------------------------------------------------------- // Purpose: //---------------------------------------------------------------------------------------------------- @@ -723,8 +665,12 @@ void SpawnCrown(int client) SetVariantString("!activator"); AcceptEntityInput(entity, "SetParent", client); - g_iCrownEntity = entity; - g_iCrownParent = client; + DataPack data = new DataPack(); + data.WriteCell(entity); + data.WriteCell(client); + data.Reset(); + + CreateTimer(0.1, OnCrownUpdate, data, TIMER_REPEAT); } //---------------------------------------------------------------------------------------------------- @@ -753,6 +699,8 @@ void SpawnSkull(int client) TeleportEntity(entity, fVector, fAngles, NULL_VECTOR); + fVector[2] += 110.0; + float fDirection[3]; fDirection[0] = 0.0; fDirection[1] = 0.0; @@ -764,31 +712,62 @@ void SpawnSkull(int client) SetVariantString("!activator"); AcceptEntityInput(entity, "SetParent", client); - g_iSkullEntity = entity; - g_iSkullParent = client; + DataPack data = new DataPack(); + data.WriteCell(entity); + data.WriteCell(client); + data.Reset(); + + CreateTimer(0.1, OnSkullUpdate, data, TIMER_REPEAT); } //---------------------------------------------------------------------------------------------------- // Purpose: //---------------------------------------------------------------------------------------------------- -void KillCrown() +public Action OnCrownUpdate(Handle timer, DataPack data) { - if ((IsValidEntity(g_iCrownEntity) && AcceptEntityInput(g_iCrownEntity, "Kill")) || g_iCrownEntity != INVALID_ENT_REFERENCE) + int entity = data.ReadCell(); + int client = data.ReadCell(); + data.Reset(); + + if (!IsValidEntity(entity) || !IsClientInGame(client) || !IsPlayerAlive(client)) { - g_iCrownEntity = INVALID_ENT_REFERENCE; - g_iCrownParent = INVALID_ENT_REFERENCE; + AcceptEntityInput(entity, "Kill"); + return Plugin_Stop; + } + else + { + float fAngles[3]; + GetClientAbsAngles(client, fAngles); + + fAngles[0] += 8.0; + fAngles[2] += 5.5; + + TeleportEntity(entity, NULL_VECTOR, fAngles, NULL_VECTOR); + return Plugin_Continue; } } //---------------------------------------------------------------------------------------------------- // Purpose: //---------------------------------------------------------------------------------------------------- -void KillSkull() +public Action OnSkullUpdate(Handle timer, DataPack data) { - if ((IsValidEntity(g_iSkullEntity) && AcceptEntityInput(g_iSkullEntity, "Kill")) || g_iSkullEntity != INVALID_ENT_REFERENCE) + int entity = data.ReadCell(); + int client = data.ReadCell(); + data.Reset(); + + if (!IsValidEntity(entity) || !IsClientInGame(client) || !IsPlayerAlive(client)) { - g_iSkullEntity = INVALID_ENT_REFERENCE; - g_iSkullParent = INVALID_ENT_REFERENCE; + AcceptEntityInput(entity, "Kill"); + return Plugin_Stop; + } + else + { + float fAngles[3]; + GetClientAbsAngles(client, fAngles); + + TeleportEntity(entity, NULL_VECTOR, fAngles, NULL_VECTOR); + return Plugin_Continue; } }