Raised This Month: $ Target: $400
 0% 

Problem with NPC code


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Apokalipsisa
Member
Join Date: May 2012
Old 05-12-2013 , 07:08   Problem with NPC code
Reply With Quote #1

Hi guys and sry for my bad english.I found an old npc code made by dias.But it has some problems.I've added this two lines:
PHP Code:
entity_set_int(entEV_INT_movetype,MOVETYPE_PUSHSTEP)
entity_set_int(entEV_INT_gamestate,1
So now the npc can walk and take damage.But i want to ask,how to made the function "FindClosesEnemy" to make follow every closest player?Becouse now if my friend acessed in the server first,the npc only follow him and not me.And i want to ask how to optimize this code,becouse when i shoot npc's i get error in server console : "channel overflow for player "some player" "and something like get_space overflow.Then the server starts to lag and the ping goes 300-400.Please help me fix it.Heres is the code:

PHP Code:
#include <amxmodx>
#include <engine>
#include <fakemeta>
#include <hamsandwich>

#define ZB_CLASSNAME "npc_zombie"
#define TASK_ATTACK 323423

new const zombie_model[] = "models/player/tank_zombi_host/tank_zombi_host.mdl"
new const zombie_hurt_sound[] = "zombie_npc/zombie_hurt.wav"
new const zombie_attack_sound[] = "zombie_npc/zombi_attack.wav"
new const zombie_die_sound[] = "zombie_npc/zombie_death.wav"

enum
{
    
ANIM_IDLE 1,
    
ANIM_WALK 3,
    
ANIM_ATTACK 76,
    
ANIM_DIE 104
}

new 
Float:g_spawn_point[3], Float:g_angles[3]
new 
pev_victim pev_enemy

new ent2

public plugin_init()
{
    
register_plugin("NPC Zombie""1.0""Dias")
    
    
register_think(ZB_CLASSNAME"fw_zb_think")
    
    
register_clcmd("say /set_spawn_point""get_spawn_point")
    
register_clcmd("say /create_zombie""create_zombie")
}

public 
client_PostThink(id)
{
    
//new Float:distance = entity_range(id, ent2)
    
    //client_print(id, print_chat, "%f", distance)
}

public 
plugin_precache()
{
    
engfunc(EngFunc_PrecacheModelzombie_model)
    
    
precache_sound(zombie_hurt_sound)
    
precache_sound(zombie_attack_sound)
    
precache_sound(zombie_die_sound)
}

public 
get_spawn_point(id)
{
    
entity_get_vector(idEV_VEC_origing_spawn_point)
    
entity_get_vector(idEV_VEC_anglesg_angles)
}

public 
create_zombie(id)
{    
    new 
ent create_entity("info_target")
    
ent2 ent
    
    entity_set_origin
(entg_spawn_point)
    
    
entity_set_float(entEV_FL_takedamage,1.0)
    
entity_set_float(entEV_FL_health100.0)
    
    
entity_set_string(entEV_SZ_classnameZB_CLASSNAME)
    
entity_set_model(entzombie_model)
    
    
entity_set_int(entEV_INT_solid2)
    
entity_set_int(entEV_INT_movetype,MOVETYPE_PUSHSTEP)
    
    
entity_set_int(entEV_INT_gamestate,1)
    
    
set_pev(entpev_victim0)
    
    
entity_set_byte(entEV_BYTE_controller1125)
    
entity_set_byte(entEV_BYTE_controller2125)
    
entity_set_byte(entEV_BYTE_controller3125)
    
entity_set_byte(entEV_BYTE_controller4125)
    
    new 
Float:maxs[3] = {16.016.036.0}
    new 
Float:mins[3] = {-16.0, -16.0, -36.0}
    
entity_set_size(entminsmaxs)
    
    
play_anim(entANIM_IDLE1.0)
    
    
entity_set_float(ent,EV_FL_nextthinkhalflife_time() + 0.01)
    
drop_to_floor(ent)
    
    
RegisterHamFromEntity(Ham_TakeDamageent"fw_zb_takedmg")
    
RegisterHamFromEntity(Ham_Killedent"fw_zb_killed")
    
    return 
1
}

public 
fw_zb_takedmg(victiminflictorattackerFloat:damagedamagebits)
{
    
emit_sound(victimCHAN_BODYzombie_hurt_sound1.0ATTN_NORM0PITCH_NORM)
}

public 
fw_zb_killed(ent2)
{
    new 
ent create_entity("info_target")
    new 
Float:Origin[3], Float:Angles[3]
    
    
pev(ent2pev_originOrigin)
    
pev(ent2pev_anglesAngles)
    
    
entity_set_origin(entOrigin)
    
entity_set_vector(entEV_VEC_anglesAngles)
    
    
entity_set_string(entEV_SZ_classname"temp_zb")
    
entity_set_model(entzombie_model)
    
    new 
Float:maxs[3] = {16.016.036.0}
    new 
Float:mins[3] = {-16.0, -16.0, -36.0}
    
entity_set_size(entminsmaxs)
    
    
drop_to_floor(ent)
    
    
play_anim(entANIM_IDLE1.0)    
    
    
play_anim(entANIM_DIE1.0)
    
emit_sound(entCHAN_BODYzombie_die_sound1.0ATTN_NORM0PITCH_NORM)
    
    
set_task(5.0"remove_temp_zb"ent)
}

public 
remove_temp_zb(ent)
{
    
remove_entity(ent)
}

public 
fw_zb_think(ent)
{
    if(!
is_valid_ent(ent))
        return 
FMRES_IGNORED
        
    
new victim FindClosesEnemy(ent)
    new 
Float:Origin[3], Float:VicOrigin[3], Float:distance
    
    pev
(entpev_originOrigin)
    
pev(victimpev_originVicOrigin)
    
    
distance get_distance_f(OriginVicOrigin)
    
    if(
distance <= 60.0)
    {
        
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.1)
    }
    
    return 
FMRES_HANDLED
}

public 
zombie_attack(entvictim)
{
    new 
Float:Ent_Origin[3], Float:Vic_Origin[3]
    
    
pev(entpev_originEnt_Origin)
    
pev(victimpev_originVic_Origin)
    
    
npc_turntotarget(entEnt_OriginvictimVic_Origin)
    
    
play_anim(entANIM_ATTACK1.0)
    
ExecuteHam(Ham_TakeDamagevictim0victimrandom_float(5.010.0), DMG_BULLET)  
    
emit_sound(victimCHAN_BODYzombie_attack_sound1.0ATTN_NORM0PITCH_NORM)
    
    
remove_task(ent+TASK_ATTACK)
    
set_task(1.5"stop_attack"ent+TASK_ATTACK)
}

public 
stop_attack(ent)
{
    
ent -= TASK_ATTACK
    
    play_anim
(entANIM_IDLE1.0)
    
remove_task(ent+TASK_ATTACK)
}

public 
npc_turntotarget(entFloat:Ent_Origin[3], targetFloat:Vic_Origin[3]) 
{
    if(
target
    {
        new 
Float:newAngle[3]
        
entity_get_vector(entEV_VEC_anglesnewAngle)
        new 
Float:Vic_Origin[0] - Ent_Origin[0]
        new 
Float:Vic_Origin[1] - Ent_Origin[1]

        new 
Float:radians floatatan(z/xradian)
        
newAngle[1] = radians * (180 3.14)
        if (
Vic_Origin[0] < Ent_Origin[0])
            
newAngle[1] -= 180.0
        
        entity_set_vector
(entEV_VEC_anglesnewAngle)
    }
}

public 
hook_ent(entvictim)
{
    new 
Float:fl_Velocity[3]
    new 
Float:VicOrigin[3], Float:EntOrigin[3]

    
pev(entpev_originEntOrigin)
    
pev(victimpev_originVicOrigin)
    
    new 
Float:distance_f get_distance_f(EntOriginVicOrigin)

    if (
distance_f 60.0)
    {
        new 
Float:fl_Time distance_f 100.0

        fl_Velocity
[0] = (VicOrigin[0] - EntOrigin[0]) / fl_Time
        fl_Velocity
[1] = (VicOrigin[1] - EntOrigin[1]) / fl_Time
        fl_Velocity
[2] = (VicOrigin[2] - EntOrigin[2]) / fl_Time
    
} else
    {
        
fl_Velocity[0] = 0.0
        fl_Velocity
[1] = 0.0
        fl_Velocity
[2] = 0.0
    
}

    
entity_set_vector(entEV_VEC_velocityfl_Velocity)
}

stock bool:IsValidTarget(iTarget)
{
    if (!
iTarget || !(1<= iTarget <= get_maxplayers()) || !is_user_connected(iTarget) || !is_user_alive(iTarget))
        return 
false
    
return true
}

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
}

public 
bool:can_see_fm(entindex1entindex2)
{
    if (!
entindex1 || !entindex2)
        return 
false

    
if (pev_valid(entindex1) && pev_valid(entindex1))
    {
        new 
flags pev(entindex1pev_flags)
        if (
flags EF_NODRAW || flags FL_NOTARGET)
        {
            return 
false
        
}

        new 
Float:lookerOrig[3]
        new 
Float:targetBaseOrig[3]
        new 
Float:targetOrig[3]
        new 
Float:temp[3]

        
pev(entindex1pev_originlookerOrig)
        
pev(entindex1pev_view_ofstemp)
        
lookerOrig[0] += temp[0]
        
lookerOrig[1] += temp[1]
        
lookerOrig[2] += temp[2]

        
pev(entindex2pev_origintargetBaseOrig)
        
pev(entindex2pev_view_ofstemp)
        
targetOrig[0] = targetBaseOrig [0] + temp[0]
        
targetOrig[1] = targetBaseOrig [1] + temp[1]
        
targetOrig[2] = targetBaseOrig [2] + temp[2]

        
engfunc(EngFunc_TraceLinelookerOrigtargetOrig0entindex10//  checks the had of seen player
        
if (get_tr2(0TraceResult:TR_InOpen) && get_tr2(0TraceResult:TR_InWater))
        {
            return 
false
        

        else 
        {
            new 
Float:flFraction
            get_tr2
(0TraceResult:TR_flFractionflFraction)
            if (
flFraction == 1.0 || (get_tr2(0TraceResult:TR_pHit) == entindex2))
            {
                return 
true
            
}
            else
            {
                
targetOrig[0] = targetBaseOrig [0]
                
targetOrig[1] = targetBaseOrig [1]
                
targetOrig[2] = targetBaseOrig [2]
                
engfunc(EngFunc_TraceLinelookerOrigtargetOrig0entindex10//  checks the body of seen player
                
get_tr2(0TraceResult:TR_flFractionflFraction)
                if (
flFraction == 1.0 || (get_tr2(0TraceResult:TR_pHit) == entindex2))
                {
                    return 
true
                
}
                else
                {
                    
targetOrig[0] = targetBaseOrig [0]
                    
targetOrig[1] = targetBaseOrig [1]
                    
targetOrig[2] = targetBaseOrig [2] - 17.0
                    engfunc
(EngFunc_TraceLinelookerOrigtargetOrig0entindex10//  checks the legs of seen player
                    
get_tr2(0TraceResult:TR_flFractionflFraction)
                    if (
flFraction == 1.0 || (get_tr2(0TraceResult:TR_pHit) == entindex2))
                    {
                        return 
true
                    
}
                }
            }
        }
    }
    return 
false
}

stock get_anim(id)
{
    return 
pev(idpev_sequence)
}

stock play_anim(indexsequenceFloat:framerate 1.0)
{
    if(
is_valid_ent(index))
    {
        
entity_set_int(indexEV_INT_sequencesequence)
        
entity_set_float(indexEV_FL_animtimeget_gametime())
        
entity_set_float(indexEV_FL_framerate,  framerate)
        
entity_set_float(indexEV_FL_frame0.0)
    }
}  

stock DirectedVec(Float:start[3],Float:end[3],Float:reOri[3])
{
    new 
Float:v3[3]
    
v3[0]=start[0]-end[0]
    
v3[1]=start[1]-end[1]
    
v3[2]=start[2]-end[2]
    new 
Float:vl vector_length(v3)
    
reOri[0] = v3[0] / vl
    reOri
[1] = v3[1] / vl
    reOri
[2] = v3[2] / vl

Apokalipsisa 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 16:24.


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