From 09adb4a13e8ff6cdf9fe03717cf4f6d4b180e37d Mon Sep 17 00:00:00 2001 From: FortyTwoFortyTwo Date: Sun, 14 May 2023 19:30:41 +0100 Subject: [PATCH] Use different method to get viewmodels --- scripting/randomizer/viewmodels.sp | 52 ++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 14 deletions(-) diff --git a/scripting/randomizer/viewmodels.sp b/scripting/randomizer/viewmodels.sp index 425529b..e65acb7 100644 --- a/scripting/randomizer/viewmodels.sp +++ b/scripting/randomizer/viewmodels.sp @@ -19,31 +19,55 @@ enum ViewModels ViewModels_MAX, } -static int g_iViewModels[TF_MAXPLAYERS][ViewModels_MAX]; +bool ViewModels_Matches(int iClient, ViewModels nViewModels, int iViewmodel) +{ + if (GetEntPropEnt(iViewmodel, Prop_Data, "m_hOwnerEntity") != iClient) + return false; + + int iDefIndex = GetEntProp(iViewmodel, Prop_Send, "m_iItemDefinitionIndex", 2); + + switch (nViewModels) + { + case ViewModels_Arm: return iDefIndex == 0xFFFF; + case ViewModels_Weapon: return iDefIndex != 0xFFFF; + } + + ThrowError("Invalid ViewModels value '%d'", nViewModels); + return false; +} int ViewModels_GetFromClient(int iClient, ViewModels nViewModels, int iModelIndex, int iWeapon = INVALID_ENT_REFERENCE) { - if (!g_iViewModels[iClient][nViewModels] || !IsValidEntity(g_iViewModels[iClient][nViewModels])) - g_iViewModels[iClient][nViewModels] = INVALID_ENT_REFERENCE; + int iFound = INVALID_ENT_REFERENCE; - if (g_iViewModels[iClient][nViewModels] != INVALID_ENT_REFERENCE && GetEntProp(g_iViewModels[iClient][nViewModels], Prop_Send, "m_nModelIndex") != iModelIndex) + int iViewmodel = INVALID_ENT_REFERENCE; + while ((iViewmodel=FindEntityByClassname(iViewmodel, "tf_wearable_vm")) != INVALID_ENT_REFERENCE) { - RemoveEntity(g_iViewModels[iClient][nViewModels]); - g_iViewModels[iClient][nViewModels] = INVALID_ENT_REFERENCE; + if (!ViewModels_Matches(iClient, nViewModels, iViewmodel)) + continue; + + if (iFound != INVALID_ENT_REFERENCE || GetEntProp(iViewmodel, Prop_Send, "m_nModelIndex") != iModelIndex) + RemoveEntity(iViewmodel); // Delete any dupe to invalid model index + else + iFound = iViewmodel; } - if (g_iViewModels[iClient][nViewModels] == INVALID_ENT_REFERENCE) - g_iViewModels[iClient][nViewModels] = ViewModels_CreateWearable(iClient, iModelIndex, iWeapon); - - return g_iViewModels[iClient][nViewModels]; + if (iFound != INVALID_ENT_REFERENCE) + return iFound; + else + return ViewModels_CreateWearable(iClient, iModelIndex, iWeapon); } void ViewModels_DeleteFromClient(int iClient, ViewModels nViewModels) { - if (g_iViewModels[iClient][nViewModels] && IsValidEntity(g_iViewModels[iClient][nViewModels])) - RemoveEntity(g_iViewModels[iClient][nViewModels]); - - g_iViewModels[iClient][nViewModels] = INVALID_ENT_REFERENCE; + int iViewmodel = INVALID_ENT_REFERENCE; + while ((iViewmodel=FindEntityByClassname(iViewmodel, "tf_wearable_vm")) != INVALID_ENT_REFERENCE) + { + if (!ViewModels_Matches(iClient, nViewModels, iViewmodel)) + continue; + + RemoveEntity(iViewmodel); + } } void ViewModels_UpdateArms(int iClient, int iForceWeapon = INVALID_ENT_REFERENCE)