Raised This Month: $51 Target: $400
 12% 

Solved 2D Array in Enum | cs_get_weapon_id in FM_SetModel


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 01-20-2018 , 12:06   2D Array in Enum | cs_get_weapon_id in FM_SetModel
Reply With Quote #1

Hello, I recently noticed that I'm unable to use 2D arrays in Enums.
I get this error:
PHP Code:
error 001expected token"}"but found "[" 
Is it just a random error or it's impossible to use 2D arrays in Enums?
PHP Code:
#define MAX_WEAPONS 19
#define MAX_MODELS 3 // V_, P_, W_

enum _:ModelData
{
    
Weapon_ModelsMAX_WEAPONS ][ MAX_MODELS ]

Basically, I'm using Weapon_Models to store the new weapon models and replace them with the old ones.
Where the first dimension is the list of armoury_entity entities
And the second dimension is reserved for the V_, P_ and W_ model types of the weapon.



The second problem is, I found a thread where it said you couldn't retrieve the weapon ID with cs_get_weapon_id in FM_SetModel, a working method is checking the weapon model, but that's not what I need. I found this stock (GetWeaponBoxProps) but I'm not really sure how to use it.
PHP Code:
GetWeaponBoxPropsent, &weapon, &weaponType, &ammoCountammoName[], len 

    for(new 
1i<= 5i++) 
    { 
        
weapon get_pdata_cbase(entm_rgpPlayerItems_CWeaponBox[i], XO_CWEAPONBOX); 
        if( 
weapon 
        { 
            break; 
        } 
    } 

    if( 
weapon 
    { 
        
weaponType get_pdata_int(weaponm_iIdXO_CBASEPLAYERWEAPON); 
    } 
    else 
    { 
        
weaponType 0
    } 

    
ammoCount get_pdata_int(entm_rgAmmo_CWeaponBox[1], XO_CWEAPONBOX); 
    if( 
ammoCount 
    { 
        
global_get(glb_pStringBaseget_pdata_int(entm_rgiszAmmo[1], XO_CWEAPONBOX), ammoNamelen); 
    } 

Thanks in advance!
__________________

Last edited by edon1337; 01-21-2018 at 16:10.
edon1337 is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 01-20-2018 , 13:42   Re: 2D Array in Enum | cs_get_weapon_id in FM_SetModel
Reply With Quote #2

Quote:
The second problem is, I found a thread where it said you couldn't retrieve the weapon ID with cs_get_weapon_id in FM_SetModel
No offense, but if you do not start connecting what you learn you won't get anywhere.

Remember your previous thread when you asked about weaponbox?
This was my reply: https://forums.alliedmods.net/showpo...5&postcount=27 You could have figured this out based on that information.

Explanation:
What it said is that FM_SetModel is called 2 times on weaponbox entities. Firstly, when the box is created(no weapon yet) and it has weaponbox.mdl as model. Then the weapon is added and the model is changed.
It's obvious you won't be able to use cs_get_weapon_id in the first SetModel call, since there's no weapon to retrieve the id from.
All you have to do is check in SetModel if you have a weaponbox entity and the model is not weaponbox.mdl. Then you can use cs_get_weapon_id.


About your first issue: a better solution is to use a trie(again)
PHP Code:
enum _:ModelType
{
    
vModel[128],
    
pModel[128],
    
wModel[128]
}
    
new 
Models[ModelType]

TrieSetArray(TriekeyModels
__________________

Last edited by HamletEagle; 01-20-2018 at 16:29.
HamletEagle is offline
Black Rose
Veteran Member
Join Date: Feb 2011
Location: Stockholm, Sweden
Old 01-20-2018 , 14:03   Re: 2D Array in Enum | cs_get_weapon_id in FM_SetModel
Reply With Quote #3

Don't use Trie when the index is an integer. That's what Dynamic Arrays are for.
Black Rose is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 01-20-2018 , 14:30   Re: 2D Array in Enum | cs_get_weapon_id in FM_SetModel
Reply With Quote #4

Quote:
Originally Posted by Black Rose View Post
Don't use Trie when the index is an integer. That's what Dynamic Arrays are for.
I have to disagree. Firstly, how do you know his index will be an integer? He could store data like
Code:
armoury_m4a1 models
//or simply
ak47 models
And it would be much better to store data in a readable format for the end user. He does not know and does not have to know that armoury smokegrenade index is 18 to be able to make changes.

(assuming he is using a configuration file for models)

Even if it was an index, with a dynamic array you would have to iterate over the array and find the entry that match your desired index.
__________________

Last edited by HamletEagle; 01-20-2018 at 14:36.
HamletEagle is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 01-20-2018 , 14:53   Re: 2D Array in Enum | cs_get_weapon_id in FM_SetModel
Reply With Quote #5

Quote:
Originally Posted by HamletEagle View Post
Remember your previous thread when you asked about weaponbox?
This was my reply: https://forums.alliedmods.net/showpo...5&postcount=27 You could have figured this out based on that information.
How exactly could that solve my problem? I clearly said I was aware of the impossibility of retrieving Weapon ID in FM_SetModel and was asking for a solution to it.

Quote:
Originally Posted by HamletEagle View Post
I have to disagree. Firstly, how do you know his index will be an integer? He could store data like
Code:
armoury_m4a1 models
//or simply
ak47 models
And it would be much better to store data in a readable format for the end user. He does not know and does not have to know that armoury smokegrenade index is 18 to be able to make changes.

(assuming he is using a configuration file for models)

Even if it was an index, with a dynamic array you would have to iterate over the array and find the entry that match your desired index.
This is what my configuration file looks like, can Tries be useful in my case?
PHP Code:
V_KNIFE models/NewKnife_V.mdl
P_KNIFE 
=
W_KNIFE =

V_AK47 models/NewAk47_V.mdl
P_AK47 
=
W_AK47 =

V_AUG models/NewAug_V.mdl
P_AUG 
=
W_AUG =

V_AWP models/NewAwp_V.mdl
P_AWP 
=
W_AWP =

. . . 
EDIT: This is what I'm gonna do, store all the keys (V_AWP, P_AWP, W_AWP..) in a global string and then loop through all of them when using TrieGetString.
__________________

Last edited by edon1337; 01-20-2018 at 16:03.
edon1337 is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 01-20-2018 , 16:33   Re: 2D Array in Enum | cs_get_weapon_id in FM_SetModel
Reply With Quote #6

Quote:
Originally Posted by edon1337 View Post
How exactly could that solve my problem? I clearly said I was aware of the impossibility of retrieving Weapon ID in FM_SetModel and was asking for a solution to it.
I feel like people don't even listen anymore this days. There is no impossibility. I'll say it again:
->weaponbox is created, model is models/w_weaponbox.mdl
->at this point the box does not contain any weapon. It's just an "empty box".
->PackWeapon is called, weapon is added to the box, model is changed.

This means that the private data are set in the second FM_SetModel call, thus you can access them. All you need to do is to filter out the first call.

PHP Code:
#include <amxmodx>
#include <fakemeta>
#include <cstrike>

public plugin_init()
{
    
register_forward(FM_SetModel"pfnSetModel"false)
}

public 
pfnSetModel(const Entity)
{
    if(
pev_valid(Entity))
    {
        new 
EntityClassName[32]
        
pev(Entitypev_classnameEntityClassNamecharsmax(EntityClassName))
        
        if(
equal(EntityClassName"weaponbox"))
        {
            new 
EntityModel[32]
            
pev(Entitypev_modelEntityModelcharsmax(EntityModel))
            
            if(!
equal(EntityModel"models/w_weaponbox.mdl"))
            {
                
//do your stuff
            
}
        }
    }

Now, what probably confuses you is that when you do cs_get_weapon_id directly on the weaponbox it will most likely return 0. That's because the weaponbox is not the weapon.
You have to firstly get the weapon entity index from the weaponbox, then retrieve the weapon CSW_* index from the weapon entity index.
This is just how weaponbox entities work, not related with FM_SetModel or any other thing you said.

The stock you found is doing a lot of stuff that you probably don't need.
PHP Code:
GetWeaponBoxWeaponTypeent 

    new 
weapon
    for(new 
1i<= 5i++) 
    { 
        
weapon get_pdata_cbase(entm_rgpPlayerItems_CWeaponBox[i], XO_CWEAPONBOX); 
        if( 
weapon 
        { 
            return 
cs_get_weapon_id(weapon); 
        } 
    } 

    return 
0

This one is more straightforward. Given a weaponbox entity index, it will return the CSW_* index of the first(usually the only) weapon entity that's packed into the box.
__________________

Last edited by HamletEagle; 01-20-2018 at 16:35.
HamletEagle is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 01-20-2018 , 16:37   Re: 2D Array in Enum | cs_get_weapon_id in FM_SetModel
Reply With Quote #7

Seems like it is impossible, the only possible way is by changing the config file names, ex:
V_AK47 --> AK47_V

then, I can create 2 global strings, 1 containing all the config file names, and the other one containing the weapon classes, in Ham_Item_Deploy I would loop through both strings and check
Code:
if( containi( szString1[ sizeof( szString1 ) - 2 ], szString2 ) )
sizeof() - 2 because we have to remove the _V/_P postfix.

Correct me if I'm wrong or there's a better solution.
__________________

Last edited by edon1337; 01-20-2018 at 16:38.
edon1337 is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 01-20-2018 , 16:49   Re: 2D Array in Enum | cs_get_weapon_id in FM_SetModel
Reply With Quote #8

It's very possible and it's quite simple, you are overthinking and over complicating stuff.
__________________
HamletEagle is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 01-20-2018 , 16:51   Re: 2D Array in Enum | cs_get_weapon_id in FM_SetModel
Reply With Quote #9

Any idea?
PHP Code:
#include < amxmodx >
#include < amxmisc >
#include < hamsandwich >
#include < fakemeta >
#include < fun >

enum _:ModelData
{
    
Weapon_V128 ],
    
Weapon_P128 ],
    
Weapon_W128 ]
}

new 
Trie:g_tWeaponModels;

new const 
g_szTrieKeys[ ][ ] =
{
    
"KNIFE_V""KNIFE_P""KNIFE_W""AK47_V""AK47_P""AK47_W""AUG_V""AUG_P""AUG_W""AWP_V""AWP_P""AWP_W""FLASHBANG_V""FLASHBANG_P""FLASHBANG_W""G3SG1_V""G3SG1_P""G3SG1_W""HEGRENADE_V""HEGRENADE_P""HEGRENADE_W""M3_V""M3_P""M3_W""M4A1_V""M4A1_P""M4A1_W""M249_V""M249_P",
    
"M249_W""MAC10_V""MAC10_P""MAC10_W""MP5NAVY_V""MP5NAVY_P""MP5NAVY_W""P90_V""P90_P""P90_W""SCOUT_V""SCOUT_P""SCOUT_W""SG552_V""SG552_P""SG552_W""SMOKEGRENADE_V""SMOKEGRENADE_P""SMOKEGRENADE_W""TMP_V""TMP_P""TMP_W""UMP45_V""UMP45_P""UMP45_W""XM1014_V""XM1014_P""XM1014_W"
}

new const 
g_szWeaponClasses[ ][ ] =
{
    
"weapon_knife""weapon_ak47""weapon_aug""weapon_awp""weapon_flashbang""weapon_g3sg1""weapon_hegrenade""weapon_m3""weapon_m4a1""weapon_m249",
    
"weapon_mac10""weapon_mp5navy""weapon_p90""weapon_scout""weapon_sg552""weapon_smokegrenade""weapon_tmp""weapon_ump45""weapon_xm1014"
}

new const 
g_szFileIni[ ] = "GameModels.ini";

public 
plugin_init( )
{
    
register_plugin"CS Battle Royale Models""1.0""DoNii" );

    
g_tWeaponModels TrieCreate( );
    
    for( new 
isizeof g_szWeaponClassesi++ )
    
RegisterHamHam_Item_Deployg_szWeaponClasses], "fw_HamItemDeployPost");

    
ReadFile( );
}

public 
plugin_precache( )
{
    new 
iDataModelData ];
    
    for( new 
isizeof g_szTrieKeysi++ )
    {
        if( 
TrieGetArrayg_tWeaponModelsg_szTrieKeys], iDatasizeofiData ) ) )
        {
            
precache_modeliDataWeapon_V ] );
            
precache_modeliDataWeapon_P ] );
            
precache_modeliDataWeapon_W ] );
        }
    }
}

public 
fw_HamItemDeployPostiEnt )
{
    if( 
pev_validiEnt ) != )
    return 
HAM_IGNORED;
    
    new 
szClassName32 ], szTrieKey32 ];
    
peviEntpev_classnameszClassNamecharsmaxszClassName ) );

    for( new 
isizeof g_szTrieKeysi++ )
    {
        
copyszTrieKeycharsmaxszTrieKey ), g_szTrieKeys] );
        
szTrieKeysizeofszTrieKey ) - ] = EOS;
        
        if( 
equaliszTrieKeyszClassName ) )
        
// i dont know...
    
}
}

ReadFile( )
{
    new 
szConfigsName256 ], szFilename512 ];
    
get_configsdirszConfigsNamecharsmaxszConfigsName ) );
    
    
formatexszFilenamecharsmaxszFilename ), "%s/%s"szConfigsNameg_szFileIni );
    
    new 
iFile fopenszFilename"r" );

    if( 
iFile )
    {
        new 
szData128 ], szValue64 ], szKey32 ];
        
        while( ! 
feofiFile ) )
        {
            
fgetsiFileszDatacharsmaxszData ) );
            
trimszData );
            
            switch( 
szData] )
            {
            case 
EOS'#'';'
                continue;
                
            default:
                {
                    
strtokszDataszKeycharsmaxszKey ), szValuecharsmaxszValue ), '=' );
                    
trimszKey ); 
                    
trimszValue );
                    
                    if( 
szValue] == EOS )
                    continue;
                    
                    
TrieSetArrayg_tWeaponModelsszKeyszValuesizeof szValue );
                }
            }
        }
    }
    
fcloseiFile );

Regarding post #6, I'm sorry for ignoring it, I basically wasn't concentrated and didn't even notice you replied. Thanks for your time.
__________________

Last edited by edon1337; 01-20-2018 at 16:53.
edon1337 is offline
E1_531G
Senior Member
Join Date: Dec 2017
Old 01-20-2018 , 17:42   Re: 2D Array in Enum | cs_get_weapon_id in FM_SetModel
Reply With Quote #10

Here you write string not array. Don't you see?
Quote:
default:
{
strtok( szData, szKey, charsmax( szKey ), szValue, charsmax( szValue ), '=' );
trim( szKey );
trim( szValue );

if( szValue[ 0 ] == EOS )
continue;

TrieSetArray( g_tWeaponModels, szKey, szValue, sizeof szValue ); // < HERE
}
Go and read about how to use Trie***Array().

And, you have luck, Hamlet helps you, but you are incredible slow and blind...
__________________
My English is A0
E1_531G is offline
Reply



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 11:18.


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