Raised This Month: $299 Target: $400
 74% 

[CSGO] Get active bonus weapon in deathmatch


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
shortguy
Member
Join Date: Jul 2009
Old 04-22-2020 , 02:42   [CSGO] Get active bonus weapon in deathmatch
Reply With Quote #1

Hi there,

Does anyone know how I can find out the classname of the current bonus weapon in deathmatch? I would like to run some code on the player_death if they died to the bonus weapon but not sure how I can fetch it.

Does anyone know where this is stored?

Thanks

Last edited by shortguy; 04-22-2020 at 02:43.
shortguy is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 04-22-2020 , 08:35   Re: [CSGO] Get active bonus weapon in deathmatch
Reply With Quote #2

Spoiler


There are two ways... I haven't found bonusweaponloadoutslot to second way.

But here is way to do it with events.
Event's fire always in same order.

This example have also trash code, ignore it.

PHP Code:
/*
Server event "player_death", Tick 28790:
- "userid" = "12"
- "attacker" = "2"
- "assister" = "0"
- "assistedflash" = "0"
- "weapon" = "elite"
- "weapon_itemid" = "7667212035"
- "weapon_fauxitemid" = "17293822569105719298"
- "weapon_originalowner_xuid" = "76561197988592905"
- "headshot" = "1"
- "dominated" = "0"
- "revenge" = "0"
- "wipe" = "0"
- "penetrated" = "0"
- "noreplay" = "1"
Server event "item_pickup_slerp", Tick 28790:
- "userid" = "2"
- "index" = "352"
- "behavior" = "1"
Server event "item_pickup", Tick 28790:
- "userid" = "2"
- "item" = "healthshot"
- "silent" = "1"
- "defindex" = "57"
Server event "gg_leader", Tick 28790:
- "playerid" = "2"
Server event "gg_killed_enemy", Tick 28790:
- "victimid" = "12"
- "attackerid" = "2"
- "dominated" = "0"
- "revenge" = "0"
- "bonus" = "1"
*/




#include <sdktools>

public void OnPluginStart()
{
    
HookEventEx("player_death",     gg_bonus_kill);
    
HookEventEx("gg_killed_enemy",    gg_bonus_kill);

    
RegConsoleCmd("sm_test"test);
}

public 
void gg_bonus_kill(Event event, const char[] namebool dontBroadcast)
{
    static 
int tick[MAXPLAYERS+1];
    static 
char weapon[MAXPLAYERS+1][MAX_NAME_LENGTH];

    if(
StrEqual(name"player_death"false))
    {
        
int attacker GetClientOfUserId(event.GetInt("attacker"));
        
tick[attacker] = GetGameTickCount();
        
event.GetString("weapon"weapon[attacker], sizeof(weapon[]));

        return;
    }

    
// not bonus kill
    
if(event.GetInt("bonus") != 1) return;


    
int attackerid GetClientOfUserId(event.GetInt("attackerid"));
    
    
// some odd reason tick doesn't match.
    
if(tick[attackerid] != GetGameTickCount()) return;
    
    
// success
    
BonusGun(attackeridweapon[attackerid]);
}

void BonusGun(int client, const char[] weapon)
{
    
PrintToServer("%N %s"clientweapon);
}



public 
Action test(int clientint args)
{
    
int m_iBonusProgress GetEntProp(clientProp_Send"m_iBonusProgress");
    
int m_iBonusChallenge GetEntProp(clientProp_Send"m_iBonusChallenge");
    
    
PrintToServer("m_iBonusProgress %i\nm_iBonusChallenge%i"m_iBonusProgressm_iBonusChallenge);



    
float m_flDMBonusStartTime GameRules_GetPropFloat("m_flDMBonusStartTime");
    
float m_flDMBonusTimeLength GameRules_GetPropFloat("m_flDMBonusTimeLength");
    
int m_unDMBonusWeaponLoadoutSlot GameRules_GetProp("m_unDMBonusWeaponLoadoutSlot");
    
int m_bDMBonusActive GameRules_GetProp("m_bDMBonusActive");


    
PrintToServer("m_flDMBonusStartTime %f\nm_flDMBonusTimeLength %f\nm_unDMBonusWeaponLoadoutSlot %i\nm_bDMBonusActive %i\n",
                    
m_flDMBonusStartTime,
                    
m_flDMBonusTimeLength,
                    
m_unDMBonusWeaponLoadoutSlot,
                    
m_bDMBonusActive);



    
PrintToServer("m_GGProgressiveWeaponOrderCT %i"GameRules_GetProp("m_GGProgressiveWeaponOrderCT"_m_unDMBonusWeaponLoadoutSlot));
    
PrintToServer("m_GGProgressiveWeaponKillUpgradeOrderT %i"GameRules_GetProp("m_GGProgressiveWeaponKillUpgradeOrderT"_m_unDMBonusWeaponLoadoutSlot));



    return 
Plugin_Continue;


*edit
I think I figured out how second way is working. It has to do with items_game.txt
I add that code later here.
__________________
Do not Private Message @me

Last edited by Bacardi; 04-22-2020 at 09:36.
Bacardi is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 04-23-2020 , 15:59   Re: [CSGO] Get active bonus weapon in deathmatch
Reply With Quote #3

I leave test code, of that second way to get bonus weapon in deathmatch.

- It read items_game.txt, to get weapon name with item definition index.
- Each player have own kind weapon load out, so you need look player m_EquippedLoadoutItemDefIndices with m_unDMBonusWeaponLoadoutSlot index
- in this sample, you get sm_test command and repeating timer.
Timer spam in chat 5 seconds before bonus start.

It's easier to get bonus weapon kill by using events, like I posted before.



PHP Code:


/*
    m_unDMBonusWeaponLoadoutSlot
    - weapon slot index, use in player m_EquippedLoadoutItemDefIndices
    - m_EquippedLoadoutItemDefIndices return -> m_iItemDefinitionIndex (items_game.txt)
    
    Note: CT and T have different bonus weapon load outs. Each player have different weapon load out
*/



#include <sdktools>

KeyValues csgo_items;


public 
void OnPluginStart()
{
    
csgo_items = new KeyValues("items_game");

    if(!
CSGO_ImportItems(csgo_items)) SetFailState("Fail - CSGO_ImportItems");

    
RegConsoleCmd("sm_test"test);
    
    
CreateTimer(1.0mytimer_TIMER_REPEAT);
}

public 
Action test(int clientint args)
{
    
float m_flDMBonusStartTime GameRules_GetPropFloat("m_flDMBonusStartTime");
    
float m_flDMBonusTimeLength GameRules_GetPropFloat("m_flDMBonusTimeLength");
    
int m_unDMBonusWeaponLoadoutSlot GameRules_GetProp("m_unDMBonusWeaponLoadoutSlot");
    
int m_bDMBonusActive GameRules_GetProp("m_bDMBonusActive");


    
PrintToServer("m_flDMBonusStartTime %f\nm_flDMBonusTimeLength %f\nm_unDMBonusWeaponLoadoutSlot %i\nm_bDMBonusActive %i\n",
                    
m_flDMBonusStartTime,
                    
m_flDMBonusTimeLength,
                    
m_unDMBonusWeaponLoadoutSlot,
                    
m_bDMBonusActive);


    
float timenow GetGameTime();
    
    
//int m_iItemDefinitionIndex;
    //char value[MAX_NAME_LENGTH];
/*
    for(int x = 0; x < GetEntPropArraySize(client, Prop_Send, "m_EquippedLoadoutItemDefIndices"); x++)
    {
        m_iItemDefinitionIndex = GetEntProp(client, Prop_Send, "m_EquippedLoadoutItemDefIndices", _, x);

        CSGO_GetItemDefinitionClassName(m_iItemDefinitionIndex, value, sizeof(value));
        PrintToServer("%i Item Class Name %i %s", x, m_iItemDefinitionIndex, value);

        CSGO_GetItemDefinitionName(m_iItemDefinitionIndex, value, sizeof(value));
        PrintToServer("%i Item       Name %i %s\n", x, m_iItemDefinitionIndex, value);
    }
*/
    
char value[MAX_NAME_LENGTH];
    
int m_iItemDefinitionIndex GetEntProp(clientProp_Send"m_EquippedLoadoutItemDefIndices"_m_unDMBonusWeaponLoadoutSlot);
    
CSGO_GetItemDefinitionName(m_iItemDefinitionIndexvaluesizeof(value));
    
PrintToChat(client"Next bonus weapon %s start %0.2f seconds"valuem_flDMBonusStartTime timenow);



    return 
Plugin_Handled;
}


public 
Action mytimer(Handle timer)
{
    
// skip current bonus weapon
    
if(GameRules_GetProp("m_bDMBonusActive")) return Plugin_Continue;


    
float m_flDMBonusStartTime GameRules_GetPropFloat("m_flDMBonusStartTime");
    
//float m_flDMBonusTimeLength = GameRules_GetPropFloat("m_flDMBonusTimeLength");
    
int m_unDMBonusWeaponLoadoutSlot GameRules_GetProp("m_unDMBonusWeaponLoadoutSlot");

    
float timenow GetGameTime();

    
    if(
0.0 < (m_flDMBonusStartTime timenow) <= 5.0)
    {

        
int m_iItemDefinitionIndex;
        
char value[MAX_NAME_LENGTH];

        for(
int i 1<= MaxClientsi++)
        {
            if(!
IsClientInGame(i) || GetClientTeam(i) <= 1) continue;
            
            
m_iItemDefinitionIndex GetEntProp(iProp_Send"m_EquippedLoadoutItemDefIndices"_m_unDMBonusWeaponLoadoutSlot);    
            
CSGO_GetItemDefinitionName(m_iItemDefinitionIndexvaluesizeof(value));
            
PrintToChat(i"Next bonus weapon is %s"value);
        }
    }

    return 
Plugin_Continue;
}










stock bool CSGO_ImportItems(KeyValues kv)
{

    if(
kv == null) return false;

    
KeyValues temp = new KeyValues("items_game");

    if(!
temp.ImportFromFile("scripts/items/items_game.txt"))
    {
        
LogError("Couldn't import file scripts/items/items_game.txt");
        
delete temp;
        return 
false;
    }
    
    if(!
temp.GotoFirstSubKey(true))
    {
        
LogError("KeyValues items_game not have sub key.");
        
delete temp;
        return 
false;
    }
    
    
char section[MAX_NAME_LENGTH];

    
// items_game.txt file have multiple "items" sections, all over the place.
    // - This loop gather all "items" sections and copy to given KeyValues handle.
    // - I may also need collect "prefabs", to get missing entity class name
    
do
    {
        if(!
temp.GetSectionName(sectionsizeof(section))) continue;

        if(!
StrEqual(section"items"false) && !StrEqual(section"prefabs"false)) continue;
        
//if(!StrEqual(section, "items", false)) continue;

        
if(!temp.GotoFirstSubKey(true))
        {
            continue;
        }
        
        
kv.JumpToKey(sectiontrue); // "items" or "prefabs"

        
do
        {
            if(!
temp.GetSectionName(sectionsizeof(section))) continue;

            
kv.JumpToKey(sectiontrue);
            
kv.Import(temp);
            
kv.GoBack(); // Go Top of "items" or "prefabs" Section
        
}
        while(
temp.GotoNextKey(true));

        
kv.Rewind(); // Go Top
        
temp.GoBack();
    }
    while(
temp.GotoNextKey(true))

    
delete temp;

    
//kv.ExportToFile("adidas.txt");
    
return true;
}

stock bool CSGO_GetItemDefinitionClassName(const int m_iItemDefinitionIndexchar[] item_classitem_class_size)
{
    
// Lets make sure, we are top of kv before go any further
    
csgo_items.Rewind();



    
char key[MAX_NAME_LENGTH];

    if(
m_iItemDefinitionIndex <= 0)
    {
        
Format(keysizeof(key), "default");
    }
    else
    {
        
Format(keysizeof(key), "%i"m_iItemDefinitionIndex);
    }
    
    
// no item definition index match
    
if(!csgo_items.JumpToKey("items"false) || !csgo_items.JumpToKey(keyfalse))
    {
        
Format(item_classitem_class_sizeNULL_STRING);
        
csgo_items.Rewind();
        return 
false;
    }


    
csgo_items.GetString("item_class"item_classitem_class_size"-1");

    
// item found, but couldn't find entity class name
    // - look class name from "prefabs"
    
if(StrEqual(item_class"-1"false))
    {
        
csgo_items.GetString("prefab"keysizeof(key), NULL_STRING);
        
csgo_items.Rewind();
        
        
// no item prefab found
        
if(!csgo_items.JumpToKey("prefabs"false) || !csgo_items.JumpToKey(keyfalse))
        {
            
Format(item_classitem_class_sizeNULL_STRING);
            
csgo_items.Rewind();
            return 
false;
        }

        
csgo_items.GetString("item_class"item_classitem_class_size"-1");

        
// no entity class name found, fail.
        
if(StrEqual(item_class"-1"false))
        {
            
Format(item_classitem_class_sizeNULL_STRING);
            
csgo_items.Rewind();
            return 
false;
        }
    }

    
// we have entity class name, great!
    
csgo_items.Rewind();
    return 
true;
}

// Translated #name works in chat/hint messages, and alone itself only. Translation stop working if you add extra text.
stock bool CSGO_GetItemDefinitionTranslatedName(const int m_iItemDefinitionIndexchar[] item_nameitem_name_size)
{
    
// Lets make sure, we are top of kv before go any further
    
csgo_items.Rewind();



    
char key[MAX_NAME_LENGTH];

    if(
m_iItemDefinitionIndex <= 0)
    {
        
Format(keysizeof(key), "default");
    }
    else
    {
        
Format(keysizeof(key), "%i"m_iItemDefinitionIndex);
    }
    
    
// no item definition index match
    
if(!csgo_items.JumpToKey("items"false) || !csgo_items.JumpToKey(keyfalse))
    {
        
Format(item_nameitem_name_sizeNULL_STRING);
        
csgo_items.Rewind();
        return 
false;
    }


    
csgo_items.GetString("item_name"item_nameitem_name_size"-1");

    
// item found, but couldn't find item translated name
    // - look translated name from "prefabs"
    
if(StrEqual(item_name"-1"false))
    {
        
csgo_items.GetString("prefab"keysizeof(key), NULL_STRING);
        
csgo_items.Rewind();
        
        
// no item prefab found
        
if(!csgo_items.JumpToKey("prefabs"false) || !csgo_items.JumpToKey(keyfalse))
        {
            
Format(item_nameitem_name_sizeNULL_STRING);
            
csgo_items.Rewind();
            return 
false;
        }

        
csgo_items.GetString("item_name"item_nameitem_name_size"-1");

        
// no item translated name found, fail.
        
if(StrEqual(item_name"-1"false))
        {
            
Format(item_nameitem_name_sizeNULL_STRING);
            
csgo_items.Rewind();
            return 
false;
        }
    }

    
// we have item translated name, great!
    
csgo_items.Rewind();
    return 
true;
}

stock bool CSGO_GetItemDefinitionName(const int m_iItemDefinitionIndexchar[] namename_size)
{
    
// Lets make sure, we are top of kv before go any further
    
csgo_items.Rewind();


    
char key[MAX_NAME_LENGTH];

    if(
m_iItemDefinitionIndex <= 0)
    {
        
Format(keysizeof(key), "default"); // it's weapon_knife
    
}
    else
    {
        
Format(keysizeof(key), "%i"m_iItemDefinitionIndex);
    }
    
    
// no item definition index match
    
if(!csgo_items.JumpToKey("items"false) || !csgo_items.JumpToKey(keyfalse))
    {
        
Format(namename_sizeNULL_STRING);
        
csgo_items.Rewind();
        return 
false;
    }


    
csgo_items.GetString("name"namename_size"-1");

    if(
StrEqual(name"-1"false))
    {
        
Format(namename_sizeNULL_STRING);
        
csgo_items.Rewind();
        return 
false;
    }

    
// we have item name, great!
    
csgo_items.Rewind();
    return 
true;

__________________
Do not Private Message @me
Bacardi is offline
shortguy
Member
Join Date: Jul 2009
Old 04-24-2020 , 00:44   Re: [CSGO] Get active bonus weapon in deathmatch
Reply With Quote #4

Thanks for the reply, I did end up looking into the m_unDMBonusWeaponLoadoutSlot thing, but saw that it was a bit difficult to convert that to a players loadout slot/item. I've actually made a PR on github to add a new native to convert a weapon ID to a loadoutslot so it would reduce the complexity of the second part. You can see my PR here: https://github.com/alliedmodders/sourcemod/pull/1241

As for now, I think I will go through with using the event method, thanks!

Last edited by shortguy; 04-24-2020 at 00:45.
shortguy is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 23:13.


Powered by vBulletin®
Copyright ©2000 - 2022, vBulletin Solutions, Inc.
Theme made by Freecode