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

Better way to check if exist other than for loop


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Celena Luna
Veteran Member
Join Date: Aug 2013
Location: Nagazora
Old 04-16-2021 , 06:02   Better way to check if exist other than for loop
Reply With Quote #1

PHP Code:
new g_PlayerSkill[33][30];

public 
SkillGain(idlevel)
{
    static 
nextskillnextskill random_num(,g_TotalSkill)
    if(!
IsPlayerHasSkill(idnextskill))
    {
        
Set_Skill(idnextskilltrue)
        
g_PlayerSkill[id][level-1] = nextskill
    
}
    else
        
SkillGain(idlevel)    
}

public 
IsPlayerHasSkill(idskillid)
{
    for(new 
i=0i<30i++)
    {
        if(
g_PlayerSkill[id][i] == skillid)
        {
            return 
true
        
}
    }

    return 
false

While running this part, when the number of player's skill obtained is closer to g_TotalSkill, the random might run into the existed skill id multiple time and costing a lot of resource + lag
Is there better way to check it?
__________________
My plugin:

Last edited by Celena Luna; 04-16-2021 at 06:11.
Celena Luna is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 04-16-2021 , 06:13   Re: Better way to check if exist other than for loop
Reply With Quote #2

You should first find which skills the player doesn't have yet and then select only among those randomly.
__________________
klippy is offline
Celena Luna
Veteran Member
Join Date: Aug 2013
Location: Nagazora
Old 04-16-2021 , 06:58   Re: Better way to check if exist other than for loop
Reply With Quote #3

Quote:
Originally Posted by KliPPy View Post
You should first find which skills the player doesn't have yet and then select only among those randomly.
how should I do it?
I knew few way but if those way run on 32 player at the same time, It didn't work out too well
__________________
My plugin:

Last edited by Celena Luna; 04-16-2021 at 06:58.
Celena Luna is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 04-16-2021 , 08:30   Re: Better way to check if exist other than for loop
Reply With Quote #4

I would load an array with the skills he doesn't have and then to get a new skill, use random() to index it.

Or if you don't need random, use an array that maintains which skills a player doesn't have, as he gets a new one, set the index to true. When he needs a new skill, cycle through this array and any that are false are assignable.
__________________

Last edited by Bugsy; 04-16-2021 at 08:36.
Bugsy is offline
Shadows Adi
AlliedModders Donor
Join Date: Aug 2019
Location: Romania
Old 04-16-2021 , 13:09   Re: Better way to check if exist other than for loop
Reply With Quote #5

Not sure about this:
PHP Code:
new g_PlayerSkill[33][30];

public 
SkillGain(idlevel)
{
    static 
nextskillnextskill random_num(,g_TotalSkill)
    if(!
IsPlayerHasSkill(idnextskill))
    {
        
Set_Skill(idnextskilltrue)
        
g_PlayerSkill[id][level-1] = nextskill
    
}
    else
        
SkillGain(idlevel)    
}

public 
IsPlayerHasSkill(idskillid)
{
    
// I assume that "skillid" is the index of skill in array
    
return g_PlayerSkill[id][skillid] != true false

__________________


Accepting Paid Requests, contact PM.

MVP Of The Round View project on GITHUB / AlliedModders
CSGO REMAKE ~ CSGO MOD [STABLE + SOURCE CODE]
Shadows Adi is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 04-16-2021 , 22:20   Re: Better way to check if exist other than for loop
Reply With Quote #6

PHP Code:

#include <amxmodx>

#define MAX_PLAYERS 32

enum Skills
{
    
HasAllSkills,
    
HighJump,
    
Gravity,
    
NoClip,
    
UnlimitedMoney,
    
UnlimitedHealth,
    
UnlimitedAmmo,
    
OneShotKill
}
new 
bool:g_PlayerSkillsMAX_PLAYERS ][ Skills ];

public 
SkillGainid )
{
    new 
Skills:sRandomSkill GetRandomSkillid );
    
    if( 
sRandomSkill == HasAllSkills )
    {
        
//Player has all skills
    
}
    else
    {
        
//Player got skill sRandomSkill
        //Set_Skill( id , sRandomSkill , true)
        
g_PlayerSkillsid ][ sRandomSkill ] = true;
        
        
//Give the player the stuff
        
switch ( sRandomSkill )
        {
            case 
HighJump
            {
            }
            case 
Gravity
            {
            }
            case 
NoClip
            {
            }
            case 
UnlimitedMoney
            {
            }
            case 
UnlimitedHealth
            {
            }
            case 
UnlimitedAmmo
            {
            }
            case 
OneShotKill
            {
            }
        }
    }
}

public 
Skills:GetRandomSkillid )
{
    new 
Skills:sGetRandomSkills ] , iSkillIndex;

    for ( new 
Skills:sSkillID HighJump sSkillID Skills sSkillID++ )
    {
        if ( 
g_PlayerSkillsid ][ sSkillID ] == false )
        {
            
sGetRandomSkills:iSkillIndex++ ] = sSkillID;
        }
    }
    
    return 
iSkillIndex sGetRandom[ ( Skills:randomiSkillIndex ) ) ] : HasAllSkills;

__________________

Last edited by Bugsy; 04-16-2021 at 22:44.
Bugsy is offline
Celena Luna
Veteran Member
Join Date: Aug 2013
Location: Nagazora
Old 04-19-2021 , 04:03   Re: Better way to check if exist other than for loop
Reply With Quote #7

Sorry for late reply
I did thing of this but the number of skill (g_TotalSkill) increase via register native like Extra Item or Zombie Class in ZP
Because of that, I can't create the Array with the size of total skill
Also, I want it to be random too so check by order is not gonna work.
P?S: Sorry if I am being dumb
__________________
My plugin:

Last edited by Celena Luna; 04-19-2021 at 04:10.
Celena Luna is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 04-19-2021 , 09:10   Re: Better way to check if exist other than for loop
Reply With Quote #8

It will assign a new skill at random regardless of order as defined in the enum. I am not sure what you mean by the number of skills increasing by register native. You'd need to show more code
__________________
Bugsy is offline
Celena Luna
Veteran Member
Join Date: Aug 2013
Location: Nagazora
Old 04-19-2021 , 22:28   Re: Better way to check if exist other than for loop
Reply With Quote #9

Quote:
Originally Posted by Bugsy View Post
It will assign a new skill at random regardless of order as defined in the enum. I am not sure what you mean by the number of skills increasing by register native. You'd need to show more code
PHP Code:
Public Native_Register_Skill(plugin_idplugin_param)
{
    new 
s_Name[32], s_Desc[32], funcid_get;
        
get_string(1s_Namecharsmax(s_Name);
    
get_string(2s_Desccharsmax(s_Desc);

    
funcid_get get_param(3)

    
ArrayPushString(Skill_Names_Name)
    ... 
    ...

    
g_TotalSkill++
    return 
g_TotalSkill-1

This is what I mean. The skill is from a separate plugin and will register to main plugins via this native.
So I can't use enum as it is predetermine.
__________________
My plugin:

Last edited by Celena Luna; 04-19-2021 at 22:31. Reason: grammar x4
Celena Luna is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 04-20-2021 , 00:06   Re: Better way to check if exist other than for loop
Reply With Quote #10

Ok, how do I determine what skills have already been used and which are free? Which variables store this?

I don't want to waste time (again) with assumptions.
__________________
Bugsy 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 11:54.


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