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

NPC Entity (Chase & Kill Players)


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Astrox
Junior Member
Join Date: Apr 2023
Old 04-27-2023 , 14:07   NPC Entity (Chase & Kill Players)
Reply With Quote #1

Hi everyone !

I'm new to alliedmods, and I hope I'm in the right subforum.

I wrote a script which creates an entity with a gun and sound from this archived thread : https://forums.alliedmods.net/archiv...p/t-11756.html , but the main part that I implemented isn't working "npc_think" .

The rat spawns after 3 seconds but doesn't chase me and this is what I get from ingame console :

L 04/27/2023 - 151:15: [ENGINE] Invalid player 2 (not in-game)
L 04/27/2023 - 151:15: [AMXX] Displaying debug trace (plugin "nextbots.amxx")
L 04/27/2023 - 151:15: [AMXX] Run time error 10: native error (native "entity_range")
L 04/27/2023 - 151:15: [AMXX] [0] engine_stocks.inc::get_entity_distance (line 71)
L 04/27/2023 - 151:15: [AMXX] [1] nextbots.sma::get_closest_player (line 131)
L 04/27/2023 - 151:15: [AMXX] [2] nextbots.sma::npc_think (line 176)


Honestly, I don't know if the code will work even without the log error since I literally copied the think function to the end of script from this website : https://amxmodx-es.com/Thread-Proble...7747#pid227747 and since get_closest_player() function didn't work I had to use chatGPT and come up with content of that function since there is no includes for it ("I'm a beginner scripter ikr :/ ")

This is my script :

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

new Float:spawn_origin[3] = { 0.00.00.0 }
new 
Float:spawn_origin_sprite[3] = { 0.00.00.0 }


public 
plugin_init()
{
        
    
register_plugin("RatSpawner""1.0""DeadShot");

    
register_srvcmd("rat""spawn_rat");
    
    
register_srvcmd("think""npc_think");
    
    
register_think("npc_rat""npc_think");
     
    
register_logevent"RoundStart"2"1=Round_Start" ); 

}

public 
plugin_precache()
{
    
precache_model("models/scoo_rats/bigfatrat.mdl");
    
precache_model("models/p_m4a1.mdl");
    
precache_generic("sprites/ledglow.spr");
    
precache_sound("twitch.wav");
}

public 
RoundStart(id)
{

    
// Schedule the rat function to execute 3 seconds after the round start
    
set_task(3.0"Usecmd_rat");
}

public 
Usecmd_rat()
{
    
server_cmd("rat");
    
    return 
PLUGIN_HANDLED
}


public 
spawn_rat()
{
    
// Get a random spawn point in the map (you can replace this with your own logic)
    
new ent_spawn find_ent_by_class(0"info_player_start");
    if (
ent_spawn != 0)
    {
       
        
entity_get_vector(ent_spawnEV_VEC_originspawn_origin);
        
entity_get_vector(ent_spawnEV_VEC_originspawn_origin_sprite);
        
spawn_origin[2] -=60.0// Spawn the rat slightly above the ground
        
rat(0); // Spawn the rat at the given location
        
spawn_origin_sprite[2] +=60.0;
        
// Spawn the cycler_sprite entity
        
new ent_sprite create_entity("env_sprite");
        
entity_set_origin(ent_spritespawn_origin_sprite);
        
entity_set_model(ent_sprite"sprites/ledglow.spr");
        
set_pev(ent_sprite,pev_renderamt,255.0);
        
set_pev(ent_sprite,pev_rendermode,kRenderTransAdd);
        
set_pev(ent_sprite,pev_renderfx,kRenderFxNone);
        
set_pev(ent_sprite,pev_rendercolorFloat:{255.00.00.0});
        
set_pev(ent_spritepev_scale0.8)
        
// Framerate
        
set_pev(ent_sprite,pev_framerate,0.0);
        
drop_to_floor(ent_spawn);
        
emit_soundent_spawn2"twitch.wav"1.0ATTN_STATIC0PITCH_NORM );
    }
    return 
PLUGIN_CONTINUE;
}

public 
rat(id)
{
    new 
ent create_entity("info_target");
    
give_weapon(ent);
    
    
entity_set_origin(ent,spawn_origin);
    
entity_set_origin(id,spawn_origin);
    
    
entity_set_float(entEV_FL_takedamage50.0);
    
entity_set_float(entEV_FL_health100.0);
    
    
entity_set_string(entEV_SZ_classname"npc_rat");
    
entity_set_model(ent"models/scoo_rats/bigfatrat.mdl");
    
entity_set_int(entEV_INT_solid2);

    
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);

    
entity_set_float(entEV_FL_animtime2.0);
    
entity_set_float(entEV_FL_framerate1.0);
    
entity_set_int(entEV_INT_sequence0);

    
entity_set_float(entEV_FL_nextthinkhalflife_time() + 0.01);
    
    return 
1;
}

public 
give_weapon(ent)
{
    new 
entWeapon create_entity("info_target");

    
entity_set_string(entWeaponEV_SZ_classname"npc_weapon");
    
entity_set_int(entWeaponEV_INT_movetypeMOVETYPE_FOLLOW);
    
entity_set_int(entWeaponEV_INT_solidSOLID_NOT);
    
entity_set_edict(entWeaponEV_ENT_aimentent);
    
entity_set_model(entWeapon"models/p_m4a1.mdl");
}
//Get closest player to NPC
get_closest_player(ent)
{
 
// iEntity = entity you are finding players closest to
    
    
new iPlayers32 ], iNum;
    
get_playersiPlayersiNum"a" );
    
    new 
iClosestPlayer 0Float:flClosestDist 9999.0;
    new 
iPlayerFloat:flDist;
    
    for( new 
0iNumi++ )
    {
        
iPlayer iPlayers];
        
flDist entity_rangeiPlayerent );
        
        if( 
flDist flClosestDist )
        {
            
iClosestPlayer iPlayer;
            
flClosestDist flDist;
        }
    }
    
    if( 
iClosestPlayer )
    {
        
client_print0print_chat"%i is the #1 closest at %i units"iClosestPlayerfloatroundflClosestDist ) );
    }
    return 
flClosestDist;
}

//Think Function
public npc_think(ent)
{

    static 
victimFloat:dist;

    if (
entity_get_int(entEV_INT_iuser1))
    {
        
entity_set_int(entEV_INT_iuser10);
        
entity_set_int(entEV_INT_sequence5);
        
entity_set_float(entEV_FL_nextthinkhalflife_time()+0.01);
        return
    }

    
victim entity_get_edict(entEV_ENT_enemy);
    if (!
victim)
    {
    
       
victim get_closest_player(ent);        
    }
 
    else
    {
        
attack(ent);
        
entity_set_edict(entEV_ENT_enemy0);
        
entity_set_int(entEV_INT_iuser11);
        
entity_set_float(entEV_FL_nextthinkhalflife_time()+0.01); 
        return
    }

    if (
dist 200.0)
    {
        
entity_set_edict(entEV_ENT_enemyvictim);
        
entity_set_int(entEV_INT_sequence5);
        
entity_set_vector(entEV_VEC_velocityFloat:{0.00.00.0});
        
entity_set_float(entEV_FL_nextthinkhalflife_time()+0.01); 

        return
    }

    if (
victim)
    {
        
entity_set_int(entEV_INT_sequence5);
        static 
Float:origin[3], Float:originT[3];
        
entity_get_vector(entEV_VEC_originorigin);
        
entity_get_vector(victimEV_VEC_originoriginT);

        
xs_vec_sub(originToriginoriginT);
        
xs_vec_normalize(originToriginT);
        
xs_vec_mul_scalar(originT250.0originT);

        
entity_set_vector(entEV_VEC_velocityoriginT);
        
originT[2] = 0.0;
        
vector_to_angle(originToriginT);
        
entity_set_vector(entEV_VEC_anglesoriginT);
    }
    else
    {
       
entity_set_int(entEV_INT_sequence5);
       
entity_set_vector(entEV_VEC_velocityFloat:{0.00.00.0});
       
entity_set_float(entEV_FL_nextthinkhalflife_time()+0.01); 
    }
    
entity_set_float(entEV_FL_nextthinkhalflife_time()+0.01); 
}
attack(ent)
{
    new 
Float:origin[3], Float:originT[3];
    
    
entity_get_vector(entEV_VEC_anglesoriginT);
    
entity_get_vector(entEV_VEC_originorigin);
    
angle_vector(originTANGLEVECTOR_FORWARDoriginT);
    
xs_vec_normalize(originToriginT);
    
xs_vec_mul_scalar(originT10.0originT);
    
xs_vec_add(originoriginToriginT);
        
    new 
victim = -1;
    while ((
victim find_ent_in_sphere(victimoriginT200.0)) > 0)
    {
        if (!(
<= victim <= 32))
            continue;
        
        if (!
is_user_alive(victim))
            continue;
        
        
entity_set_int(entEV_INT_sequence6);
        
ExecuteHamB(Ham_TakeDamagevictimentent70.0DMG_CLUB);
    }

Thanks in advance !

Last edited by Astrox; 04-27-2023 at 16:11.
Astrox is offline
Astrox
Junior Member
Join Date: Apr 2023
Old 04-27-2023 , 16:36   Re: NPC Entity (Chase & Kill Players)
Reply With Quote #2

I've used this instead .

now the rat looks at me whenever (maybe only those that types follow) I go but not moving :
PHP Code:
#include <amxmodx>
#include <engine>
#include <fakemeta>
#include <hamsandwich>
#include <xs>
#include <cstrike>

new Float:spawn_origin[3] = { 0.00.00.0 }
new 
Float:spawn_origin_sprite[3] = { 0.00.00.0 }
new 
g_follow;

public 
plugin_init()
{
        
    
register_plugin("RatSpawner""1.0""DeadShot");

    
register_srvcmd("rat""spawn_rat");
    
    
register_srvcmd("think""npc_think");
    
    
register_clcmd("say /follow""clcmd_follow")
    
    
register_think("npc_rat""npc_think");
     
    
register_logevent"RoundStart"2"1=Round_Start" ); 

}

public 
plugin_precache()
{
    
precache_model("models/scoo_rats/bigfatrat.mdl");
    
precache_model("models/p_m4a1.mdl");
    
precache_generic("sprites/ledglow.spr");
    
precache_sound("twitch.wav");
}

public 
RoundStart(id)
{

    
// Schedule the rat function to execute 3 seconds after the round start
    
set_task(3.0"Usecmd_rat");
}

public 
Usecmd_rat()
{
    
server_cmd("rat");
    
    return 
PLUGIN_HANDLED
}


public 
spawn_rat()
{
    
// Get a random spawn point in the map (you can replace this with your own logic)
    
new ent_spawn find_ent_by_class(0"info_player_start");
    if (
ent_spawn != 0)
    {
       
        
entity_get_vector(ent_spawnEV_VEC_originspawn_origin);
        
entity_get_vector(ent_spawnEV_VEC_originspawn_origin_sprite);
        
spawn_origin[2] -=60.0// Spawn the rat slightly above the ground
        
rat(0); // Spawn the rat at the given location
        
spawn_origin_sprite[2] +=60.0;
        
// Spawn the cycler_sprite entity
        
new ent_sprite create_entity("env_sprite");
        
entity_set_origin(ent_spritespawn_origin_sprite);
        
entity_set_model(ent_sprite"sprites/ledglow.spr");
        
set_pev(ent_sprite,pev_renderamt,255.0);
        
set_pev(ent_sprite,pev_rendermode,kRenderTransAdd);
        
set_pev(ent_sprite,pev_renderfx,kRenderFxNone);
        
set_pev(ent_sprite,pev_rendercolorFloat:{255.00.00.0});
        
set_pev(ent_spritepev_scale0.8)
        
// Framerate
        
set_pev(ent_sprite,pev_framerate,0.0);
        
drop_to_floor(ent_spawn);
        
emit_soundent_spawn2"twitch.wav"1.0ATTN_STATIC0PITCH_NORM );
    }
    return 
PLUGIN_CONTINUE;
}

public 
rat(id)
{
    new 
ent create_entity("info_target");
    
give_weapon(ent);
    
    
entity_set_origin(ent,spawn_origin);
    
entity_set_origin(id,spawn_origin);
    
    
entity_set_float(entEV_FL_takedamage50.0);
    
entity_set_float(entEV_FL_health100.0);
    
    
entity_set_string(entEV_SZ_classname"npc_rat");
    
entity_set_model(ent"models/scoo_rats/bigfatrat.mdl");
    
entity_set_int(entEV_INT_solid2);

    
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);

    
entity_set_float(entEV_FL_animtime2.0);
    
entity_set_float(entEV_FL_framerate1.0);
    
entity_set_int(entEV_INT_sequence0);

    
entity_set_float(entEV_FL_nextthinkhalflife_time() + 0.01);
    
    return 
1;
}

public 
give_weapon(ent)
{
    new 
entWeapon create_entity("info_target");

    
entity_set_string(entWeaponEV_SZ_classname"npc_weapon");
    
entity_set_int(entWeaponEV_INT_movetypeMOVETYPE_FOLLOW);
    
entity_set_int(entWeaponEV_INT_solidSOLID_NOT);
    
entity_set_edict(entWeaponEV_ENT_aimentent);
    
entity_set_model(entWeapon"models/p_m4a1.mdl");
}
//Get closest player to NPC
stock entity_set_aim(entplayer)
{
    static 
Float:origin[3], Float:ent_origin[3], Float:angles[3]
    
entity_get_vector(playerEV_VEC_originorigin)
    
entity_get_vector(entEV_VEC_originent_origin)
    
    
xs_vec_sub(originent_originorigin)

    
xs_vec_normalize(originorigin)

    
vector_to_angle(originangles)
    
    
angles[0] = 0.0
    
    entity_set_vector
(entEV_VEC_anglesangles)
    
set_velocity(entangles)
}

stock set_velocity(entFloat:angles[3])
{
    static 
FloatDirection[3]
    
angle_vector(anglesANGLEVECTOR_FORWARDDirection)
    
xs_vec_mul_scalar(Direction215.0Direction// 215 speed
    
entity_set_vector(entEV_VEC_velocityDirection)
    
    
// Sequence 
    
if(entity_get_int(entEV_INT_sequence) != 4)
    {
        
entity_set_float(entEV_FL_nextthinkhalflife_time() + 0.01);
        
entity_set_int(entEV_INT_sequence4)
    }

public 
clcmd_follow(id)
{
    
g_follow id

public 
npc_think(ent)
{
    
// change by register_think
    
if(!pev_valid(ent)) return;
    
    static 
className[32]
    
pev(entpev_classnameclassNamecharsmax(className))
    
    if(!
equali(className"npc_rat")) return;
      
    
//Util_PlayAnimation(iEnt, NPC_IdleAnimations[random(sizeof NPC_IdleAnimations)])
    
    
if(is_user_alive(g_follow))
      
entity_set_aim(entg_follow)
    
entity_set_float(entEV_FL_nextthinkhalflife_time() + 0.01);

I don't know how to take use of this but the rat should follow the nearest players if there is more than 1 player while moving of course

Last edited by Astrox; 04-27-2023 at 16:40.
Astrox is offline
Jhob94
AMX Mod X Donor
Join Date: Jul 2012
Old 04-27-2023 , 16:49   Re: NPC Entity (Chase & Kill Players)
Reply With Quote #3

I will help you when i get home. I am with 5% battery on mobile phone
__________________
Jhob94 is offline
Astrox
Junior Member
Join Date: Apr 2023
Old 04-27-2023 , 18:21   Re: NPC Entity (Chase & Kill Players)
Reply With Quote #4

You are a legend bro, Thanks !

Be safe !
Astrox is offline
Jhob94
AMX Mod X Donor
Join Date: Jul 2012
Old 04-27-2023 , 18:36   Re: NPC Entity (Chase & Kill Players)
Reply With Quote #5

Code:
#include <amxmodx>
#include <engine>
#include <fakemeta>
#include <hamsandwich>
#include <xs>
#include <cstrike>

new Float:spawn_origin[3] = { 0.0, 0.0, 0.0 }
new Float:spawn_origin_sprite[3] = { 0.0, 0.0, 0.0 }
new g_follow;

public plugin_init()
{
	
	register_plugin("RatSpawner", "1.0", "DeadShot");
	
	register_srvcmd("rat", "spawn_rat");
	
	register_srvcmd("think", "npc_think");
	
	register_clcmd("say /follow", "clcmd_follow")
	
	register_think("npc_rat", "npc_think");
	
	register_logevent( "RoundStart", 2, "1=Round_Start" ); 
	
}

public plugin_precache()
{
	precache_model("models/scoo_rats/bigfatrat.mdl");
	precache_model("models/p_m4a1.mdl");
	precache_generic("sprites/ledglow.spr");
	precache_sound("twitch.wav");
}

public RoundStart()
{
	// Schedule the rat function to execute 3 seconds after the round start
	set_task(3.0, "Usecmd_rat");
}

public Usecmd_rat()
{
	server_cmd("rat");
	
	return PLUGIN_HANDLED
}


public spawn_rat()
{
	// Get a random spawn point in the map (you can replace this with your own logic)
	new ent_spawn = find_ent_by_class(0, "info_player_start");
	if (ent_spawn != 0)
	{
		
		entity_get_vector(ent_spawn, EV_VEC_origin, spawn_origin);
		spawn_origin[2] -=60.0; // Spawn the rat slightly above the ground
		rat(); // Spawn the rat at the given location
		spawn_origin_sprite[2] +=60.0;
		// Spawn the cycler_sprite entity
		new ent_sprite = create_entity("env_sprite");
		entity_set_origin(ent_sprite, spawn_origin_sprite);
		entity_set_model(ent_sprite, "sprites/ledglow.spr");
		set_pev(ent_sprite,pev_renderamt,255.0);
		set_pev(ent_sprite,pev_rendermode,kRenderTransAdd);
		set_pev(ent_sprite,pev_renderfx,kRenderFxNone);
		set_pev(ent_sprite,pev_rendercolor, Float:{255.0, 0.0, 0.0});
		set_pev(ent_sprite, pev_scale, 0.8)
		// Framerate
		set_pev(ent_sprite,pev_framerate,0.0);
		drop_to_floor(ent_spawn);
		emit_sound( ent_spawn, 2, "twitch.wav", 1.0, ATTN_STATIC, 0, PITCH_NORM );
	}
	return PLUGIN_CONTINUE;
}

public rat()
{
	new ent = create_entity("info_target");
	give_weapon(ent);
	
	entity_set_origin(ent,spawn_origin);
	
	entity_set_float(ent, EV_FL_takedamage, 50.0);
	entity_set_float(ent, EV_FL_health, 100.0);
	
	entity_set_string(ent, EV_SZ_classname, "npc_rat");
	entity_set_model(ent, "models/scoo_rats/bigfatrat.mdl");
	entity_set_int(ent, EV_INT_solid, 2);
	
	entity_set_byte(ent, EV_BYTE_controller1, 125);
	entity_set_byte(ent, EV_BYTE_controller2, 125);
	entity_set_byte(ent, EV_BYTE_controller3, 125);
	entity_set_byte(ent, EV_BYTE_controller4, 125);
	
	new Float:maxs[3] = { 16.0, 16.0, 36.0 };
	new Float:mins[3] = { -16.0, -16.0, -36.0 };
	entity_set_size(ent, mins, maxs);
	
	entity_set_float(ent, EV_FL_animtime, 2.0);
	entity_set_float(ent, EV_FL_framerate, 1.0);
	entity_set_int(ent, EV_INT_sequence, 0);
	
	entity_set_float(ent, EV_FL_nextthink, halflife_time() + 0.01);
	
	return 1;
}

public give_weapon(ent)
{
	new entWeapon = create_entity("info_target");
	
	entity_set_string(entWeapon, EV_SZ_classname, "npc_weapon");
	entity_set_int(entWeapon, EV_INT_movetype, MOVETYPE_STEP);
	entity_set_int(entWeapon, EV_INT_solid, SOLID_NOT);
	entity_set_edict(entWeapon, EV_ENT_aiment, ent);
	entity_set_model(entWeapon, "models/p_m4a1.mdl");
}
//Get closest player to NPC
stock entity_set_aim(ent, player)
{
	static Float:origin[3], Float:ent_origin[3], Float:angles[3]
	entity_get_vector(player, EV_VEC_origin, origin)
	entity_get_vector(ent, EV_VEC_origin, ent_origin)
	
	xs_vec_sub(origin, ent_origin, origin)
	
	xs_vec_normalize(origin, origin)
	
	vector_to_angle(origin, angles)
	
	angles[0] = 0.0
	
	entity_set_vector(ent, EV_VEC_angles, angles)
	set_velocity(ent, angles)
}

stock set_velocity(ent, Float:angles[3])
{
	static Float: Direction[3]
	angle_vector(angles, ANGLEVECTOR_FORWARD, Direction)
	xs_vec_mul_scalar(Direction, 215.0, Direction) // 215 speed
	entity_set_vector(ent, EV_VEC_velocity, Direction)
	
	// Sequence 
	if(entity_get_int(ent, EV_INT_sequence) != 4)
	{
		entity_set_float(ent, EV_FL_nextthink, halflife_time() + 0.01);
		entity_set_int(ent, EV_INT_sequence, 4)
	}
} 
public clcmd_follow(id)
{
	g_follow = id
} 
public npc_think(ent)
{
	// change by register_think
	if(!pev_valid(ent)) return;
	
	static className[32]
	pev(ent, pev_classname, className, charsmax(className))
	
	if(!equali(className, "npc_rat")) return;
	
	//Util_PlayAnimation(iEnt, NPC_IdleAnimations[random(sizeof NPC_IdleAnimations)])
	
	if(is_user_alive(g_follow))
		entity_set_aim(ent, g_follow)
	entity_set_float(ent, EV_FL_nextthink, halflife_time() + 0.01);
}
It was a very quick review. I removed id from unnecessary places and changed movetype.
If this still not works, tomorrow (or when i can) i will make one by myself
__________________

Last edited by Jhob94; 04-27-2023 at 18:37.
Jhob94 is offline
Astrox
Junior Member
Join Date: Apr 2023
Old 04-27-2023 , 19:06   Re: NPC Entity (Chase & Kill Players)
Reply With Quote #6

Appreciate the help (rat is still waiting for a bright future where he can walk xD).

Peace !

Last edited by Astrox; 04-27-2023 at 19:08.
Astrox is offline
Old 04-28-2023, 12:57
Astrox
This message has been deleted by Astrox.
Astrox
Junior Member
Join Date: Apr 2023
Old 04-28-2023 , 14:11   Re: NPC Entity (Chase & Kill Players)
Reply With Quote #7

I made some progress ; the rat follows me but there is still alot of rooms for optimization :
1- Rat doesn't lurk ( he needs a player to aim at it so it follows him)
2- It doesn't overcome obstacles (ramp, stairs, 1unit blocks, dust2 doors )
3- I think it may need to think when there is more than 1 player (follow nearest)
4- Need the rat to spawn at a random "info_player_start" with a cycler_sprite above rats head

This the updated code :
PHP Code:
#include <amxmodx>
#include <engine>
#include <fakemeta>
#include <hamsandwich>
#include <xs>

#define TASK_COOLDOWN 7208
#define TASK_REMOVE 8072
#define ID_REMOVE (taskid - TASK_REMOVE)


const MAX_SPAWN 32
new g_dead[256]
new 
g_cooldown
new g_spawncount
new g_follow

new const g_NpcClassName[] = "rat_npc"

new const g_NpcModel[] = "models/scoo_rats/bigfatrat.mdl"



public plugin_precache()
{
    
precache_generic("sprites/ledglow.spr");
    
precache_sound("twitch.wav");
    
 
    
precache_model(g_NpcModel)
}

public 
plugin_init()
{
    
register_plugin("RatSpawner""1.0""DeadShot");
    
    
register_logevent"RoundStart"2"1=Round_Start" ); 
    
register_srvcmd("rat""rat_npc")
    
register_srvcmd("follow""rat_follow")
    
    
RegisterHam(Ham_Think"info_target""npc_Think")

}

public 
RoundStart(id)
{

    
// Schedule the rat function to execute 3 seconds after the round start
    
set_task(3.0"Usecmd_rat");
}

public 
Usecmd_rat()
{
    
server_cmd("rat");
    
server_cmd("follow");
    
    return 
PLUGIN_HANDLED
}


public 
rat_npc(id)
{
    if(
g_cooldown) return PLUGIN_HANDLED;
    
    if(
g_spawncount >= MAX_SPAWN)
    {
        
client_print(idprint_chat"[EntTest] Max [%i] spawns has been reach."MAX_SPAWN)
        return 
PLUGIN_HANDLED;
    }
    
    
g_cooldown 1
    
    
new ent engfunc(EngFunc_CreateNamedEntityengfunc(EngFunc_AllocString"info_target"))
    if(!
pev_valid(ent)) return PLUGIN_HANDLED;
    
    
set_pev(entpev_classnameg_NpcClassName)
    
set_pev(entpev_netname"Twitch")
    new 
Floatorigin[3], Floatangle[3], Floatvelocity[3]
    
pev(idpev_originorigin)
    
origin[2] += 80.0
    engfunc
(EngFunc_SetOriginentorigin)
    
pev(idpev_anglesangle)
    
angle[0] = 0.0
    set_pev
(entpev_anglesangle)
    
set_pev(entpev_gravity1.0)
    
set_pev(entpev_movetypeMOVETYPE_PUSHSTEP)
    
set_pev(entpev_solidSOLID_SLIDEBOX)
    
set_pev(entpev_controller_0125)
    
set_pev(entpev_controller_1125)
    
set_pev(entpev_controller_2125)
    
set_pev(entpev_controller_3125)
    
engfunc(EngFunc_SetModelentg_NpcModel)
    
engfunc(EngFunc_SetSizeent, {-16.0, -16.0, -36.0 }, { 16.016.036.0 })
    
velocity_by_aim(id800velocity)
    
set_pev(entpev_velocityvelocity)
    
emit_soundent2"twitch.wav"1.0ATTN_STATIC0PITCH_NORM );
    
g_spawncount += 1
    
    set_pev
(entpev_nextthinkget_gametime() + 0.01)
    
set_task(1.0"ResetCooldown"TASK_COOLDOWN)
    
    return 
PLUGIN_HANDLED;
}

public 
rat_follow(id)

    
g_follow id
    
    
return PLUGIN_HANDLED;


public 
npc_Think(iEnt)
{
    if(!
pev_valid(iEnt)) return;
    
    static 
className[32]
    
pev(iEntpev_classnameclassNamecharsmax(className))
    
    if(!
equali(classNameg_NpcClassName)) return;
    
    if(
g_dead[iEnt]) return;
    
    if(
is_user_alive(g_follow)) entity_set_aim(iEntg_follow);
    
    
set_pev(iEntpev_nextthinkget_gametime() + 0.01)
}

public 
ResetCooldown(taskid)
{
    
g_cooldown 0
}

public 
RemoveDeadEntity(taskid)
{
    if(!
pev_valid(ID_REMOVE)) return;
    
    
engfunc(EngFunc_RemoveEntityID_REMOVE)
    
    
g_spawncount -= 1
    g_dead
[ID_REMOVE] = 0
}
//AIM AT PLAYER
entity_set_aim(entplayer

    static 
Float:origin[3], Float:ent_origin[3], Float:angles[3
    
pev(playerpev_originorigin
    
pev(entpev_originent_origin
     
    
xs_vec_sub(originent_originorigin
    
xs_vec_normalize(originorigin
    
vector_to_angle(originangles
     
    
angles[0] = 0.0 
     
    set_pev
(entpev_anglesangles)
    
set_velocity(entangles)

//RAT SPEED
set_velocity(entFloat:angles[3]) 

    static 
FloatDirection[3
    
angle_vector(anglesANGLEVECTOR_FORWARDDirection
    
xs_vec_mul_scalar(Direction350.0Direction// 350 speed
    
set_pev(entpev_velocityDirection)
    
    
// Run Sequence
    
if(pev(entpev_sequence) != 5Util_PlayAnimation(ent4);
}

Util_PlayAnimation(indexsequenceFloatframerate 1.0)
{
    
set_pev(indexpev_animtimeget_gametime())
    
set_pev(indexpev_framerate,  framerate)
    
set_pev(indexpev_frame0.0)
    
set_pev(indexpev_sequencesequence)

Astrox is offline
Astrox
Junior Member
Join Date: Apr 2023
Old 05-17-2023 , 06:04   Re: NPC Entity (Chase & Kill Players)
Reply With Quote #8

Any ideas :'( ?
Astrox is offline
Fuck For Fun
Veteran Member
Join Date: Nov 2013
Old 05-18-2023 , 08:21   Re: NPC Entity (Chase & Kill Players)
Reply With Quote #9

PHP Code:
get_closest_player(ent)
{
    new 
iPlayers[32], iNum;
    
get_players(iPlayersiNum"a");

    new 
iClosestPlayer 0Float:flClosestDist 9999.0;
    new 
iPlayerFloat:flDist;

    for (new 
0iNumi++)
    {
        
iPlayer iPlayers[i];
        
        if (!
is_user_alive(iPlayer))
            continue;

        
flDist entity_range(iPlayerent);

        if (
flDist flClosestDist)
        {
            
iClosestPlayer iPlayer;
            
flClosestDist flDist;
        }
    }

    if (
iClosestPlayer)
    {
        
client_print(0print_chat"%i is the #1 closest at %i units"iClosestPlayerfloatround(flClosestDist));
    }

    return 
flClosestDist;

It seems that the player with ID 2 is not recognized as an in-game player,
Fuck For Fun is offline
Send a message via Skype™ to Fuck For Fun
Astrox
Junior Member
Join Date: Apr 2023
Old 05-18-2023 , 14:43   Re: NPC Entity (Chase & Kill Players)
Reply With Quote #10

Thanks for the reply.
Yeh I got that ingame error (no player with id X) when trying to use that function inside the rat_follow() function
I may need some friends to test it out. At this rate, I may just give this plugin up .
Thanks for help tho <3
Astrox is offline
Reply



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 20:47.


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