AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Code Snippets/Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=83)
-   -   How To: Make a perfect NPC (https://forums.alliedmods.net/showthread.php?t=11756)

Twilight Suzuka 03-27-2005 21:48

How To: Make a perfect NPC
 
I will discuss how to make the perfect NPC.

It'll be broken down into two sections:
1. Setting up an NPC
2. Setting up a bot.

Setting Up An NPC

Steps:
1. Making the ent
2. Making it solid
3. Making it take damage
4. Setting its model
5. Animating
6. Making NPC think correctly
7. Giving it a weapon

Ok, here we go.

1. First lets make a function:

Code:
public spawn_npc(id)

Then lets make the entity:

Code:
    new ent = create_entity("info_target")
Note: This creates the entity.

We also need to give the entity an origin, and also make the player *jump*, just for testing purposes.

Code:
    new Float:origin[3]     entity_get_vector(id,EV_VEC_origin,origin)     entity_set_origin(ent,origin);     origin[2] += 300.0     entity_set_origin(id,origin)
Note: Sets the origin

2.
Ok the player is out of the way. Lets make it solid.

Code:
    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_int(ent,EV_INT_solid, 2)
Note: Sets it BBOX solid, and gives it a bbox

3.
Its solid, but invinsible. Lets also make it take damage:

Code:
    entity_set_float(ent,EV_FL_takedamage,1.0)     entity_set_float(ent,EV_FL_health,100.0)
Note: Changes the takedamage to 1, and gives it HP.

The health IS the health of the entity, but you should set it higher then normal. Ents seem to have less HP somehow.

4.
Setting the model is as easy as this:

Code:
    entity_set_string(ent,EV_SZ_classname,"npc_onna");     entity_set_model(ent,"models/onna.mdl");
Note: Sets its model and classname

I know setting the classname is not setting the model, but you should set it with a similar classname.

5.
Ok, now lets give it a default animation.

Code:
    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);
Note: Sets the frameRate (framerate for ent), animation time (max time for an animation, and sequence (the actual animation). You do not really need animtime, but I think better safe then sorry.

6.
Making it think is SLIGHTLY harder.

First you have to register its think in plugin_init:

Code:
    register_think("npc_onna","npc_think");
Note: The first parameter must be the same as the classname you set.

Now then, we need to make the entity start thinking:
In the create NPC function:
Code:
    entity_set_float(ent,EV_FL_nextthink,halflife_time() + 0.01)
Note: This makes the entity think REALLY soon. 1/100th of a second.

It needs to think VERY fast, in order to out run the server frames. This wont cause a whole lot of lag, since it is mostly handled in the engine.

Now then, the npc_think function will be called whenever it thinks. You can do all sorts of shit in here.

For instance, you can check if the HP has gone down, and make it bleed, play the right animation, etc.

Or, you could make it walk around by giving it velocity and changing the animation.

Above all, you must call the think function again:
Code:
public npc_think(id) {     // Put your think stuff here.     entity_set_float(id,EV_FL_nextthink,halflife_time() + 0.01) }

Everything else you do in here is your business. I'll put up some examples later of how to make it walk or shoot.

7.
I'll show you how to add a weapon as well:

Code:
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_gauss.mdl") }
Make sure the model you set is precached, or it will fuck up.
the P model is the PLAYER model, which will generally work. I used an HL default weapon because my model was HLDM.

Now then, lets put it all together:
Code:
#include <amxmodx> #include <engine> public plugin_init() {     register_clcmd("onna", "onna")     register_think("npc_onna","npc_think"); } public plugin_precache() {     precache_model("models/onna.mdl")     precache_model("models/p_gauss.mdl") } public onna(id) {     new Float:origin[3]     entity_get_vector(id,EV_VEC_origin,origin)     new ent = create_entity("info_target")     give_weapon(ent)     entity_set_origin(ent,origin);     origin[2] += 300.0     entity_set_origin(id,origin)     entity_set_float(ent,EV_FL_takedamage,1.0)     entity_set_float(ent,EV_FL_health,100.0)     entity_set_string(ent,EV_SZ_classname,"npc_onna");     entity_set_model(ent,"models/onna.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)     drop_to_floor(ent)     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_gauss.mdl") } public npc_think(id) {     // Put your think stuff here.     entity_set_float(id,EV_FL_nextthink,halflife_time() + 0.01) }

This is a working test function. You can easily take alter it to make an NPC of your choosing.

Have fun with it!


Making A Bot

Making a bot, or an NPC that can move easily and takes up a slot, is as simple as 1 2 3.

1. Follow the guide above
2. replace "create_entity("info_target"" with engfunc(EngFunc_CreateFakeClient,"Onna")

3. Set the model of the new bot using the mod specific functions.

You can now:
Use the FM engfunc command, EngFunc_RunPlayerMove to make them move
Act as if they are a normal player.

Very simple!

Have fun with it

- Mel

v3x 03-27-2005 21:58

What's an NPC? :oops:

Twilight Suzuka 03-27-2005 22:09

Non-Playable-Character.

You can use them in plugins to make like, a gunshop owner, or a store owner.

v3x 03-27-2005 22:12

Oh, that's awesome! I'll be sure to use this. :D

KyleD 03-27-2005 23:42

Thanks Twilight Suzuka!!

This helped me out a lot!

v3x 03-27-2005 23:43

1 Attachment(s)
Could I set it to make the NPC 'killable', or at least make it take damage?

Edit: Look @ what I did with it, hehe..

Twilight Suzuka 03-28-2005 10:53

Yes, you can! I'll figure it out and post it soon. I just prefer friendly NPC's, ones that you cant kill for TSXMOD.

There is also a way to make your NPC act like an actual player, I'll get into that later.

The real thing was making the aniimations work, the rest of it is extra.

Hell, I made a realistic Scientist that walked around pretty good.

nightscreem 03-28-2005 15:03

nice job

v3x 03-28-2005 15:41

Quote:

Originally Posted by Twilight Suzuka
Yes, you can! I'll figure it out and post it soon. I just prefer friendly NPC's, ones that you cant kill for TSXMOD.

There is also a way to make your NPC act like an actual player, I'll get into that later.

The real thing was making the aniimations work, the rest of it is extra.

Hell, I made a realistic Scientist that walked around pretty good.

Awesome, can't wait for that! I'll have to look more into entitys and how they work. :)

XxAvalanchexX 03-28-2005 17:23

Set his takedamage field, give him some max health and health, and I think that'll about do it. Now you have to figure out how to make him flinch when we gets shot and how to make him die. ;-)


All times are GMT -4. The time now is 16:32.

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