Raised This Month: $2 Target: $400
 0% 

How To: Make a perfect NPC


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Twilight Suzuka
bad
Join Date: Jul 2004
Location: CS lab
Old 03-27-2005 , 21:48   How To: Make a perfect NPC
Reply With Quote #1

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
__________________
Twilight Suzuka is offline
Send a message via AIM to Twilight Suzuka Send a message via MSN to Twilight Suzuka
v3x
Veteran Member
Join Date: Oct 2004
Location: US
Old 03-27-2005 , 21:58  
Reply With Quote #2

What's an NPC?
__________________
What am I doing these days? Well, I run my own Rust server. It's heavily modded. If you'd like to join, the ip is 167.114.101.67:28116

I also created a website called Rust Tools. It will calculate and tell you the raw amounts of resources needed to craft items.
v3x is offline
Twilight Suzuka
bad
Join Date: Jul 2004
Location: CS lab
Old 03-27-2005 , 22:09  
Reply With Quote #3

Non-Playable-Character.

You can use them in plugins to make like, a gunshop owner, or a store owner.
__________________
Twilight Suzuka is offline
Send a message via AIM to Twilight Suzuka Send a message via MSN to Twilight Suzuka
v3x
Veteran Member
Join Date: Oct 2004
Location: US
Old 03-27-2005 , 22:12  
Reply With Quote #4

Oh, that's awesome! I'll be sure to use this.
__________________
What am I doing these days? Well, I run my own Rust server. It's heavily modded. If you'd like to join, the ip is 167.114.101.67:28116

I also created a website called Rust Tools. It will calculate and tell you the raw amounts of resources needed to craft items.
v3x is offline
KyleD
Member
Join Date: Mar 2005
Location: Anchorage, AK
Old 03-27-2005 , 23:42  
Reply With Quote #5

Thanks Twilight Suzuka!!

This helped me out a lot!
__________________
KyleD is offline
Send a message via MSN to KyleD
v3x
Veteran Member
Join Date: Oct 2004
Location: US
Old 03-27-2005 , 23:43  
Reply With Quote #6

Could I set it to make the NPC 'killable', or at least make it take damage?

Edit: Look @ what I did with it, hehe..
Attached Thumbnails
Click image for larger version

Name:	scream.jpg
Views:	4464
Size:	112.7 KB
ID:	2634  
__________________
What am I doing these days? Well, I run my own Rust server. It's heavily modded. If you'd like to join, the ip is 167.114.101.67:28116

I also created a website called Rust Tools. It will calculate and tell you the raw amounts of resources needed to craft items.
v3x is offline
Twilight Suzuka
bad
Join Date: Jul 2004
Location: CS lab
Old 03-28-2005 , 10:53  
Reply With Quote #7

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.
__________________
Twilight Suzuka is offline
Send a message via AIM to Twilight Suzuka Send a message via MSN to Twilight Suzuka
nightscreem
Veteran Member
Join Date: Jul 2004
Location: Belgium
Old 03-28-2005 , 15:03  
Reply With Quote #8

nice job
__________________
- Bye bye!
nightscreem is offline
v3x
Veteran Member
Join Date: Oct 2004
Location: US
Old 03-28-2005 , 15:41  
Reply With Quote #9

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.
__________________
What am I doing these days? Well, I run my own Rust server. It's heavily modded. If you'd like to join, the ip is 167.114.101.67:28116

I also created a website called Rust Tools. It will calculate and tell you the raw amounts of resources needed to craft items.
v3x is offline
XxAvalanchexX
Veteran Member
Join Date: Oct 2004
Location: abort73.com
Old 03-28-2005 , 17:23  
Reply With Quote #10

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. ;-)
__________________
No longer around. Thanks your support, everyone! As always:
THIS ONES FOR YOU
3000 PTS
XxAvalanchexX 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 10:04.


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