Skip to content

Commit

Permalink
loadout: Always reapply items on MvM respec
Browse files Browse the repository at this point in the history
See #15.
  • Loading branch information
nosoop committed Apr 11, 2021
1 parent b61925d commit ba60734
Showing 1 changed file with 43 additions and 2 deletions.
45 changes: 43 additions & 2 deletions scripting/cwx.sp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ int g_CurrentLoadoutEntity[MAXPLAYERS + 1][NUM_PLAYER_CLASSES][NUM_ITEMS];

Cookie g_ItemPersistCookies[NUM_PLAYER_CLASSES][NUM_ITEMS];

bool g_bForceReequipItems[MAXPLAYERS + 1];

#include "cwx/item_config.sp"
#include "cwx/item_entity.sp"
#include "cwx/item_export.sp"
Expand All @@ -75,7 +77,7 @@ public void OnPluginStart() {

delete hGameConf;


HookEvent("player_spawn", OnPlayerSpawnPost);
HookUserMessage(GetUserMessageId("PlayerLoadoutUpdated"), OnPlayerLoadoutUpdated,
.post = OnPlayerLoadoutUpdatedPost);

Expand Down Expand Up @@ -250,7 +252,7 @@ void OnPlayerLoadoutUpdatedPost(UserMsg msg_id, bool sent) {
// equip our item if it isn't already equipped, or if it's being killed
// the latter applies to items that are normally invalid for the class
int currentLoadoutItem = g_CurrentLoadoutEntity[client][playerClass][i];
if (!IsValidEntity(currentLoadoutItem)
if (g_bForceReequipItems[client] || !IsValidEntity(currentLoadoutItem)
|| GetEntityFlags(currentLoadoutItem) & FL_KILLME) {
// TODO validate that the player can access this item
CustomItemDefinition item;
Expand All @@ -267,6 +269,11 @@ void OnPlayerLoadoutUpdatedPost(UserMsg msg_id, bool sent) {
// as is the case again, this happens on non-valid-for-class items
}

void OnPlayerSpawnPost(Event event, const char[] name, bool dontBroadcast) {
int client = GetClientOfUserId(event.GetInt("userid"));
g_bForceReequipItems[client] = false;
}

/**
* Item persistence - we return our item's CEconItemView instance when the game looks up our
* inventory item. This prevents our custom item from being invalidated when touch resupply.
Expand Down Expand Up @@ -314,6 +321,40 @@ MRESReturn OnGetLoadoutItemPost(int client, Handle hReturn, Handle hParams) {
return MRES_Supercede;
}

/**
* Handles a special case where the player is refunding all of their upgrades, which may stomp
* on any existing runtime attributes applied to our weapon.
*/
public Action OnClientCommandKeyValues(int client, KeyValues kv) {
char cmd[64];
kv.GetSectionName(cmd, sizeof(cmd));

/**
* Mark the player to always invalidate our items so they get reequipped during respawn --
* this is fine since TF2 manages to reapply upgrades to plugin-granted items.
*
* The player gets their loadout changed multiple times during respec so we can't just
* invalidate the reference in g_CurrentLoadoutEntity (since it'll be valid after the first
* change).
*
* Hopefully nobody's blocking "MVM_Respec", because that would leave this flag set.
* Otherwise we should be able to hook CUpgrades::GrantOrRemoveAllUpgrades() directly,
* though that incurs a gamedata burden.
*/
if (StrEqual(cmd, "MVM_Respec")) {
g_bForceReequipItems[client] = true;
}
}

public void OnClientCommandKeyValues_Post(int client, KeyValues kv) {
char cmd[64];
kv.GetSectionName(cmd, sizeof(cmd));

if (StrEqual(cmd, "MVM_Respec")) {
g_bForceReequipItems[client] = false;
}
}

/**
* Saves the current item into the loadout for the specified class.
*/
Expand Down

0 comments on commit ba60734

Please sign in to comment.