SA-MP Forums Archive
Checking Ranges and respawning. - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Checking Ranges and respawning. (/showthread.php?tid=639379)



Checking Ranges and respawning. - iLearner - 16.08.2017

PHP код:
timer ZombieRespawnTimer[5000]()
{
    new 
bool:true_foundidFloat:x,Float:y,Float:zcount_zombiesNPCSS[MAX_PLAYERS];
    
count_zombies=0;
     
    for(new 
i=0i<MAX_PLAYERSi++)
    {
        
GetPlayerPos(i,x,y,z);
        for(new 
k=0k<MAX_PLAYERSk++)
        {
            if(
IsPlayerNPC(k))
            {
                if(
IsPlayerInRangeOfPoint(k50xyz))
                {
                    
true_found=true;
                    
id k;
                }
            }    
        }
        if(
true_found)
        {
            
count_zombies++;
            
NPCSS[count_zombies] = id;
        }
        
    }
    if(
count_zombies 20)
    {
        
printf("Found %d more zombies in a area, respawning...",count_zombies-20);
        for(new 
i=0i<count_zombies-20i++)
        {
            
FCNPC_Respawn(NPCSS[i]);
        }
    }

Basically this is the (unoptimized and badly made as i am still testing) code i am using to check with a 5 seconds timer whether there are 20+ npc's in a range, if yes count them and respawn the count-20 npc's.

I am using double loop as i want to check through every npc, and loop to all other npc's, but the problem is the server kinda hangs when the timer gets called and i also know the reason... I was here to ask if someone has a better idea?


Re: Checking Ranges and respawning. - iLearner - 16.08.2017

Tried this and it exceeds heap size:
PHP код:
timer ZombieRespawnTimer[5000]()
{
    new 
Float:x,Float:y,Float:zcount_zombiesNPCSS[MAX_PLAYERS][MAX_PLAYERS], NPCSSID[MAX_PLAYERS][MAX_PLAYERS];
    
count_zombies=0;
     
    for(new 
i=0i<MAX_PLAYERSi++)
    {
        
GetPlayerPos(i,x,y,z);
        for(new 
k=0k<MAX_PLAYERSk++)
        {
            if(
IsPlayerNPC(k))
            {
                if(
IsPlayerInRangeOfPoint(k50xyz))
                {
                    
NPCSS[i][k] ++;
                    
NPCSSID[i][k] = k;
                }
            }    
        }
    }
    for(new 
i=0i<MAX_PLAYERSi++)
    {
        for(new 
k=0k<MAX_PLAYERSk++)
        {
            if(
NPCSS[i][k] > 20)
            {
                
printf("Found %d more zombies in a area, respawning...",NPCSS[i][k]-20);
                
FCNPC_Respawn(NPCSSID[i][k]);
            }
        }
    }         




Re: Checking Ranges and respawning. - Paulice - 16.08.2017

One word (or 5?), AttachDynamicAreaToPlayer.

Edit: k isn't after i if you didn't know lol


Re: Checking Ranges and respawning. - ZiGGi - 16.08.2017

Agreed about Dynamic Areas.
Also I recommend to use foreach:
PHP код:
foreach (new FCNPC



Re: Checking Ranges and respawning. - AbyssMorgan - 16.08.2017

without timers/loops but for 300.0 range
PHP код:
new StreamedNPC[MAX_PLAYERS];
public 
OnPlayerStreamIn(playeridforplayerid){
    if(
IsPlayerNPC(playerid) && IsPlayerNPC(forplayerid)){ //npc for npc
        
StreamedNPC[forplayerid]++;
        if(
StreamedNPC[forplayerid] >= 20){ //reached max npc in 300.0 range
            
        
}
    }
    return 
1;
}
public 
OnPlayerStreamOut(playeridforplayerid){
    if(
IsPlayerNPC(playerid) && IsPlayerNPC(forplayerid)){ //npc for npc
        
StreamedNPC[forplayerid]--;
    }
    return 
1;




Re: Checking Ranges and respawning. - Paulice - 16.08.2017

Quote:
Originally Posted by AbyssMorgan
Посмотреть сообщение
without timers/loops but for 300.0 range
PHP код:
new StreamedNPC[MAX_PLAYERS];
public 
OnPlayerStreamIn(playeridforplayerid){
    if(
IsPlayerNPC(playerid) && IsPlayerNPC(forplayerid)){ //npc for npc
        
StreamedNPC[forplayerid]++;
        if(
StreamedNPC[forplayerid] >= 20){ //reached max npc in 300.0 range
            
        
}
    }
    return 
1;
}
public 
OnPlayerStreamOut(playeridforplayerid){
    if(
IsPlayerNPC(playerid) && IsPlayerNPC(forplayerid)){ //npc for npc
        
StreamedNPC[forplayerid]--;
    }
    return 
1;

He wants 50.0 radius!