AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   [HELP] Switch to closest enemy (https://forums.alliedmods.net/showthread.php?t=221978)

Apokalipsisa 07-28-2013 17:28

[HELP] Switch to closest enemy
 
How to make the etnity switch to closest player and not following the one who catched first?Here is the code in the entity think:

PHP Code:

public fw_zb_think(ent)
{
    if(!
is_valid_ent(ent))
        return 
FMRES_IGNORED
    
    
if(g_think[ent])
    {
        static 
victim
        
static Float:Origin[3], Float:VicOrigin[3], Float:distance

        victim 
FindClosesEnemy(ent)
        
        
pev(entpev_originOrigin)
        
pev(victimpev_originVicOrigin)
        
        
distance get_distance_f(OriginVicOrigin)
        
        if(
is_user_alive(victim))
        {
            if(
distance <= 60.0)
            {
                if(!
is_valid_ent(ent))
                    return 
FMRES_IGNORED    
            
                
new Float:Ent_Origin[3], Float:Vic_Origin[3]
                
                
pev(entpev_originEnt_Origin)
                
pev(victimpev_originVic_Origin)            
            
                
npc_turntotarget(entEnt_OriginvictimVic_Origin)
                
                
zombie_attack(entvictim)
                
entity_set_float(entEV_FL_nextthinkget_gametime() + 2.5)
            } else {
                
                if(
get_anim(ent) != ANIM_WALK)
                    
play_anim(entANIM_WALK1.0)
                    
                new 
Float:Ent_Origin[3], Float:Vic_Origin[3]
                
                
pev(entpev_originEnt_Origin)
                
pev(victimpev_originVic_Origin)
                
                
npc_turntotarget(entEnt_OriginvictimVic_Origin)
                
hook_ent(entvictim)
                
                
entity_set_float(entEV_FL_nextthinkget_gametime() + 0.5)
            }
            
            
current_target[ent] = victim
        
} else {
            
//hook_ent(ent, ent)
            
            
if(get_anim(ent) != ANIM_IDLE)
                
play_anim(entANIM_IDLE1.0)
            
            
entity_set_float(entEV_FL_nextthinkget_gametime() + 1.0)
        }
    } else {
        if(
get_anim(ent) != ANIM_IDLE)
            
play_anim(entANIM_IDLE1.0)
            
        
entity_set_float(entEV_FL_nextthinkget_gametime() + 1.0)        
    }
    
    return 
FMRES_HANDLED



Now the FindClosesEnemy function is set to public:

PHP Code:

public FindClosesEnemy(entid)
{
    new 
Float:Dist
    
new Float:maxdistance=4000.0
    
new indexid=0    
    
for(new i=1;i<=get_maxplayers();i++){
        if(
is_user_alive(i) && is_valid_ent(i) && can_see_fm(entidi))
        {
            
Dist entity_range(entidi)
            if(
Dist <= maxdistance)
            {
                
maxdistance=Dist
                indexid
=i
                
                
return indexid
            
}
        }    
    }    
    return 
0



How to make it switch targets and not store the ID to only first one who catch?If i connect first in the server the entity will follow only me even if there is close targets around him.How to make it switch targets?


All times are GMT -4. The time now is 15:57.

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