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

optimize string array


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Celena Luna
Veteran Member
Join Date: Aug 2013
Location: Nagazora
Old 02-09-2021 , 01:52   optimize string array
Reply With Quote #1

Is there a better way to optimize this?

PHP Code:
new WeaponList_Pistol_CT[][] = { "weapon_deagle""weapon_fiveseven""weapon_fiveseven""weapon_fiveseven" }
new 
WeaponList_SMG_SG_CT[][] = { "weapon_tmp""weapon_ump45""weapon_m3"}
new 
WeaponList_SMG_SG2_CT[][] = { "weapon_mp5navy""weapon_p90""weapon_xm1014"}
new 
WeaponList_Rfile_CT[][] = { "weapon_famas""weapon_scout""weapon_famas""weapon_famas"}
new 
WeaponList_Rifle2_CT[][] = { "weapon_m4a1""weapon_aug""weapon_famas"}
new 
WeaponList_Random_CT[][] = { "weapon_m4a1""weapon_aug""weapon_famas""weapon_mp5navy""weapon_p90"}

new 
WeaponList_Pistol_TS[][] = { "weapon_deagle""weapon_elite""weapon_elite""weapon_elite" }
new 
WeaponList_SMG_SG_TS[][] = { "weapon_mac10""weapon_ump45""weapon_m3"}
new 
WeaponList_SMG_SG2_TS[][] = { "weapon_mp5navy""weapon_p90""weapon_xm1014"}
new 
WeaponList_Rfile_TS[][] = { "weapon_galil""weapon_scout"}
new 
WeaponList_Rifle2_TS[][] = { "weapon_ak47""weapon_sg552""weapon_galil"}
new 
WeaponList_Random_TS[][] = { "weapon_ak47""weapon_sg552""weapon_mp5navy""weapon_p90""weapon_galil"
__________________
My plugin:

Last edited by Celena Luna; 02-28-2021 at 00:45.
Celena Luna is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 02-09-2021 , 02:10   Re: optimize string array
Reply With Quote #2

Optimize it in what sense? Context matters (i.e. it matters on how you're using it). It's likely good enough. I think there are enumerations for weapons which you could use instead, that would use less memory. Then you can look up the string only when needed as a string. But again, it depends on how you are using these arrays.
__________________

Last edited by fysiks; 02-09-2021 at 02:13.
fysiks is offline
Celena Luna
Veteran Member
Join Date: Aug 2013
Location: Nagazora
Old 02-09-2021 , 02:25   Re: optimize string array
Reply With Quote #3

I plan on randomizing weapon in the array and give it depend on round.
I just questioning if is there any way to do it look "cleaner"

I tried as
PHP Code:
new WeaponListCT[][] = {
    { 
"weapon_deagle""weapon_fiveseven" },
    { 
"weapon_mp5navy""weapon_p90""weapon_xm1014"}

but it keep getting error
__________________
My plugin:

Last edited by Celena Luna; 02-09-2021 at 02:26.
Celena Luna is offline
+ARUKARI-
AlliedModders Donor
Join Date: Jul 2004
Location: Japan
Old 02-09-2021 , 02:40   Re: optimize string array
Reply With Quote #4

PHP Code:
enum _:WEAPON_LISTS
{
    
PISTOLS,
    
SMG_SG_1,
    
SMG_SG_2,
    
RIFLE_1,
    
RIFLE_2,
    
RANDOM,
};
WeaponListCT[WEAPON_LISTS][][] =
{
    { 
"weapon_deagle""weapon_fiveseven""" },                // PISTOLS
    
"weapon_mp5navy""weapon_p90""weapon_xm1014"},        // SMG_SG_1
    //...
}; 
It is a 3-dimensional array and the number of items must match or an error will occur.
__________________
GitHub
SteamWishlist

六四天安門事件

Last edited by +ARUKARI-; 02-09-2021 at 02:47.
+ARUKARI- is offline
Celena Luna
Veteran Member
Join Date: Aug 2013
Location: Nagazora
Old 02-09-2021 , 02:54   Re: optimize string array
Reply With Quote #5

That was what I tried to used but then I got
Code:
error 052: multi-dimensional arrays must be fully initialized
so I have to split it into smaller array.
When randomize with sizeof, it will also random into the blank one
__________________
My plugin:

Last edited by Celena Luna; 02-09-2021 at 02:55.
Celena Luna is offline
Napoleon_be
Veteran Member
Join Date: Jul 2011
Location: Belgium
Old 02-09-2021 , 07:52   Re: optimize string array
Reply With Quote #6

I made this a while ago, it might be helpful for you.

PHP Code:
/* Credits to
    ConnorMcLeod:
        - I got the Free For All code from his plugin: https://forums.alliedmods.net/showthread.php?t=80208
    Bugsy:
        - Helping me figure out how to set model to the player if the weapon can be bought by both teams.
        - I still need to work on blocking weapon pickup to avoid bugs.
*/

#include <amxmodx>
#include <cstrike>
#include <hamsandwich>
#include <fun>

#pragma semicolon 1

#define PLUGIN "FFA: WeaponBased PlayerModels"
#define VERSION "1.0.0"
#define AUTHOR "NapoleoN#"

enum _:ModelsInfo
{
    
szModelName[32],
    
iWeaponID,
    
CsTeams:csTeams
}

new const 
szWeapons[][ModelsInfo] =
{
    { 
"wbknife"CSW_KNIFE CS_TEAM_UNASSIGNED },             // Knife
    
"wbawp"CSW_AWP CS_TEAM_UNASSIGNED },            // AWP
    
"wbfamas"CSW_FAMASCS_TEAM_CT },                 // Famas
    
"wbm4a1"CSW_M4A1CS_TEAM_CT },                    // M4A1
    
"wbak47"CSW_AK47CS_TEAM_T },                     // AK47
    
"wbgalil"CSW_GALILCS_TEAM_T },                     // Galil
    
"wbtmp"CSW_TMPCS_TEAM_CT },                     // TMP
    
"wbump45"CSW_UMP45 CS_TEAM_UNASSIGNED},             // Ump45
    
"wbscout"CSW_SCOUT CS_TEAM_UNASSIGNED},             // Scout
    
"wbxm1014"CSW_XM1014 CS_TEAM_UNASSIGNED},            // XM1014
    
"wbaug"CSW_AUGCS_TEAM_CT },                    // AUG
    
"wbsg550"CSW_SG550CS_TEAM_CT },                // SG550
    
"wbmp5navy"CSW_MP5NAVY CS_TEAM_UNASSIGNED},            // MP5 Navy
    
"wbm249"CSW_M249 CS_TEAM_UNASSIGNED},                 // M249
    
"wbm3"CSW_M3 CS_TEAM_UNASSIGNED},                // M3
    
"wbg3sg1"CSW_G3SG1CS_TEAM_T },                    // G3SG1
    
"wbsg552"CSW_SG552CS_TEAM_T }                     // SG552
};

new 
iMsgRadar;
new 
iMaxPlayers;

public 
plugin_init()
{
    
register_plugin(PLUGINVERSIONAUTHOR);

    
register_event("CurWeapon""CheckWeapons""be""1=1");

    
RegisterHam(Ham_TakeDamage"player""PreTakeDamage");
    
RegisterHam(Ham_TraceAttack"player""TraceAttack");

    
iMsgRadar get_user_msgid("Radar");
    
register_message(iMsgRadar"RadarMessage");

    
server_cmd("mp_friendlyfire 1");

    
iMaxPlayers get_maxplayers();
}

precache_player_model(szModel[])
{
    static 
szFile[128];
    
formatex(szFilecharsmax(szFile), "models/player/%s/%s.mdl"szModelszModel);
    
precache_model(szFile);
    
replace(szFilecharsmax(szFile), ".mdl""T.mdl");

    if(
file_exists(szFile))
    {
        
precache_model(szFile);
    }
}

public 
plugin_precache()
{
    for(new 
isizeof(szWeapons); i++)
    {
        
precache_player_model(szWeapons[i][szModelName]);
    }
}

public 
client_putinserver(id)
{
    
client_cmd(id"hideradar");
}

public 
RadarMessage(iMsgIDMsgDestid)
{
    return 
PLUGIN_HANDLED;
}

public 
CheckWeapons(id)
{
    new 
iUserWeapon get_user_weapon(id);
    new 
CsTeams:csTeam cs_get_user_team(id);

    for (new 
isizeof(szWeapons) ; i++)
    {
        if (
iUserWeapon == szWeapons[i][iWeaponID])
        {
            if((
szWeapons[i][csTeams] == CS_TEAM_UNASSIGNED) || (csTeam == szWeapons[i][csTeams]))
            {
                
cs_set_user_model(id szWeapons[i][szModelName]);
            }
            else
            {
                
cs_reset_user_model(id);
            }

            break;
        }
    }
}

public 
PreTakeDamage(iVictimiInflictoriAttackerFloat:fDamageDamageBits)
{
    if(
iVictim != iAttacker && (<= iAttacker <= iMaxPlayers))
    {
        new 
CsTeams:csVictimTeam cs_get_user_team(iVictim);
        if(
csVictimTeam == cs_get_user_team(iAttacker))
        {
            
cs_set_user_team(iVictimcsVictimTeam == CS_TEAM_T CS_TEAM_CT CS_TEAM_T);
            
ExecuteHamB(Ham_TakeDamageiVictimiInflictoriAttackerfDamageDamageBits);
            
cs_set_user_team(iVictimcsVictimTeam);
            return 
HAM_SUPERCEDE;
        }
    }
    return 
HAM_IGNORED;
}

public 
TraceAttack(iVictimiAttackerFloat:fDamageFloat:fDirection[3], TraceHandleDamageBits)
{
    if( 
iVictim != iAttacker && (<= iAttacker <= iMaxPlayers))
    {
        new 
CsTeams:csVictimTeam cs_get_user_team(iVictim);
        if(
csVictimTeam == cs_get_user_team(iAttacker))
        {
            
cs_set_user_team(iVictimcsVictimTeam == CS_TEAM_T CS_TEAM_CT CS_TEAM_T);
            
ExecuteHamB(Ham_TraceAttackiVictimiAttackerfDamagefDirectionTraceHandleDamageBits);
            
cs_set_user_team(iVictimcsVictimTeam);
            return 
HAM_SUPERCEDE;
        }
    }
    return 
HAM_IGNORED;

__________________

Last edited by Napoleon_be; 02-12-2021 at 16:07.
Napoleon_be is offline
Send a message via Skype™ to Napoleon_be
Natsheh
Veteran Member
Join Date: Sep 2012
Old 02-11-2021 , 11:38   Re: optimize string array
Reply With Quote #7

add const to the array when initializing.

for example

PHP Code:
static const array[][] = { "Hello this will never change"":)" 
this is slightly better but if you're using alot of plugins which have alot of constant arrays it will make a difference.

and use static for file scope only.


also if you want just to save weapons you can use the weapon id rather than using its name, and use get_weaponname to get the weapon name if needed.
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !


Last edited by Natsheh; 02-11-2021 at 11:40.
Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 02-11-2021 , 22:15   Re: optimize string array
Reply With Quote #8

I think Napoleon_be's approach would be the cleanest if this works. How do you need to reference this data for each weapon within your code? We can most likely make it work by building a small function/macro to make it work either way.
__________________
Bugsy is offline
Napoleon_be
Veteran Member
Join Date: Jul 2011
Location: Belgium
Old 02-12-2021 , 16:03   Re: optimize string array
Reply With Quote #9

I might aswell just share the entire code. Previous post has been updated.

Take in mind this is just to show you an example of how i did something simular to what you need. The free for all part hasn't been tested and the code still needs a "weapon pickup blocker".

Also the free for all part needs a better integration as i could use my array instead of creating a new variable localy.
__________________

Last edited by Napoleon_be; 02-12-2021 at 16:09.
Napoleon_be is offline
Send a message via Skype™ to Napoleon_be
Celena Luna
Veteran Member
Join Date: Aug 2013
Location: Nagazora
Old 02-13-2021 , 10:18   Re: optimize string array
Reply With Quote #10

Quote:
Originally Posted by Natsheh View Post
add const to the array when initializing.

for example

PHP Code:
static const array[][] = { "Hello this will never change"":)" 
this is slightly better but if you're using alot of plugins which have alot of constant arrays it will make a difference.

and use static for file scope only.


also if you want just to save weapons you can use the weapon id rather than using its name, and use get_weaponname to get the weapon name if needed.
I actually forgot about get_weaponname can be use here. If only 2-dimension array then it might better.

Quote:
Originally Posted by Napoleon_be View Post
I might aswell just share the entire code. Previous post has been updated.

Take in mind this is just to show you an example of how i did something simular to what you need. The free for all part hasn't been tested and the code still needs a "weapon pickup blocker".

Also the free for all part needs a better integration as i could use my array instead of creating a new variable localy.
Quote:
Originally Posted by Bugsy View Post
I think Napoleon_be's approach would be the cleanest if this works. How do you need to reference this data for each weapon within your code? We can most likely make it work by building a small function/macro to make it work either way.
My base ideas is just make a multi-dimensional array for a set of weapon for each team each round but because number of weapon each round is different so some round will have "blank" weapon.
I can just fill it with random previous weapon in that set but it look kind of a mess.

@Napoleon_be thanks, I will try it
__________________
My plugin:
Celena Luna 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 00:03.


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