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 :
, 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: [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)
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 :
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 :/ ")
PHP 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 }
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_spawn, EV_VEC_origin, spawn_origin);
entity_get_vector(ent_spawn, EV_VEC_origin, spawn_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_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(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(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_FOLLOW);
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
get_closest_player(ent)
{
// iEntity = entity you are finding players closest to
new iPlayers[ 32 ], iNum;
get_players( iPlayers, iNum, "a" );
new iClosestPlayer = 0, Float:flClosestDist = 9999.0;
new iPlayer, Float:flDist;
for( new i = 0; i < iNum; i++ )
{
iPlayer = iPlayers[ i ];
flDist = entity_range( iPlayer, ent );
if( flDist < flClosestDist )
{
iClosestPlayer = iPlayer;
flClosestDist = flDist;
}
}
if( iClosestPlayer )
{
client_print( 0, print_chat, "%i is the #1 closest at %i units", iClosestPlayer, floatround( flClosestDist ) );
}
return flClosestDist;
}
//Think Function
public npc_think(ent)
{
static victim, Float:dist;
if (entity_get_int(ent, EV_INT_iuser1))
{
entity_set_int(ent, EV_INT_iuser1, 0);
entity_set_int(ent, EV_INT_sequence, 5);
entity_set_float(ent, EV_FL_nextthink, halflife_time()+0.01);
return
}
victim = entity_get_edict(ent, EV_ENT_enemy);
if (!victim)
{
victim = get_closest_player(ent);
}
else
{
attack(ent);
entity_set_edict(ent, EV_ENT_enemy, 0);
entity_set_int(ent, EV_INT_iuser1, 1);
entity_set_float(ent, EV_FL_nextthink, halflife_time()+0.01);
return
}
if (dist < 200.0)
{
entity_set_edict(ent, EV_ENT_enemy, victim);
entity_set_int(ent, EV_INT_sequence, 5);
entity_set_vector(ent, EV_VEC_velocity, Float:{0.0, 0.0, 0.0});
entity_set_float(ent, EV_FL_nextthink, halflife_time()+0.01);
return
}
if (victim)
{
entity_set_int(ent, EV_INT_sequence, 5);
static Float:origin[3], Float:originT[3];
entity_get_vector(ent, EV_VEC_origin, origin);
entity_get_vector(victim, EV_VEC_origin, originT);
xs_vec_sub(originT, origin, originT);
xs_vec_normalize(originT, originT);
xs_vec_mul_scalar(originT, 250.0, originT);
entity_set_vector(ent, EV_VEC_velocity, originT);
originT[2] = 0.0;
vector_to_angle(originT, originT);
entity_set_vector(ent, EV_VEC_angles, originT);
}
else
{
entity_set_int(ent, EV_INT_sequence, 5);
entity_set_vector(ent, EV_VEC_velocity, Float:{0.0, 0.0, 0.0});
entity_set_float(ent, EV_FL_nextthink, halflife_time()+0.01);
}
entity_set_float(ent, EV_FL_nextthink, halflife_time()+0.01);
}
attack(ent)
{
new Float:origin[3], Float:originT[3];
entity_get_vector(ent, EV_VEC_angles, originT);
entity_get_vector(ent, EV_VEC_origin, origin);
angle_vector(originT, ANGLEVECTOR_FORWARD, originT);
xs_vec_normalize(originT, originT);
xs_vec_mul_scalar(originT, 10.0, originT);
xs_vec_add(origin, originT, originT);
new victim = -1;
while ((victim = find_ent_in_sphere(victim, originT, 200.0)) > 0)
{
if (!(1 <= victim <= 32))
continue;
if (!is_user_alive(victim))
continue;
entity_set_int(ent, EV_INT_sequence, 6);
ExecuteHamB(Ham_TakeDamage, victim, ent, ent, 70.0, DMG_CLUB);
}
}