AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Better way to check if exist other than for loop (https://forums.alliedmods.net/showthread.php?t=331968)

Celena Luna 04-16-2021 06:02

Better way to check if exist other than for loop
 
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?

klippy 04-16-2021 06:13

Re: Better way to check if exist other than for loop
 
You should first find which skills the player doesn't have yet and then select only among those randomly.

Celena Luna 04-16-2021 06:58

Re: Better way to check if exist other than for loop
 
Quote:

Originally Posted by KliPPy (Post 2744221)
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

Bugsy 04-16-2021 08:30

Re: Better way to check if exist other than for loop
 
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.

Shadows Adi 04-16-2021 13:09

Re: Better way to check if exist other than for loop
 
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



Bugsy 04-16-2021 22:20

Re: Better way to check if exist other than for loop
 
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;



Celena Luna 04-19-2021 04:03

Re: Better way to check if exist other than for loop
 
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

Bugsy 04-19-2021 09:10

Re: Better way to check if exist other than for loop
 
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

Celena Luna 04-19-2021 22:28

Re: Better way to check if exist other than for loop
 
Quote:

Originally Posted by Bugsy (Post 2744511)
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.

Bugsy 04-20-2021 00:06

Re: Better way to check if exist other than for loop
 
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.


All times are GMT -4. The time now is 17:06.

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