设为首页收藏本站新闻投稿

MOD中国同盟社

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1336|回复: 5

[转帖] 发现一个很有用的AMXX创建NPC的帖子。 [复制链接]

Rank: 7Rank: 7Rank: 7

帖子
863
精华
0
声望
2462 点
金币
584 Mold
被赞许
7 次
注册时间
2010-5-7
发表于 2010-12-15 17:25:47 |显示全部楼层
【来自AliedModder】看了之后,感觉不错,于是拿来和大家分享,大家自己去理解吧。。。
#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 Floatrigin[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 *** 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 ** 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 Floatrigin[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!

使用道具 举报

Rank: 5Rank: 5

帖子
500
精华
0
声望
1471 点
金币
499 Mold
被赞许
3 次
注册时间
2010-10-12

可爱小猫

发表于 2010-12-15 17:49:25 |显示全部楼层
......
能介绍下么。。。
用中文

使用道具 举报

Rank: 7Rank: 7Rank: 7

帖子
863
精华
0
声望
2462 点
金币
584 Mold
被赞许
7 次
注册时间
2010-5-7
发表于 2010-12-15 17:57:48 |显示全部楼层
回复 2# sk4z


    准备要翻译的,太长了,于是就放弃了,把英文部分说明用GOOGLE翻译着看吧

使用道具 举报

Honorary Member

在人海出现,同时也在人海中沉没

Rank: 7Rank: 7Rank: 7

帖子
4193
精华
3
声望
7989 点
金币
696 Mold
被赞许
151 次
注册时间
2009-12-11

黑暗使者 Modchina灌水王

发表于 2010-12-15 18:01:39 |显示全部楼层
我可耻的眼花了
已有 1 人评分声望 收起 理由
lyynyfy + 5

总评分: 声望 + 5   查看全部评分

使用道具 举报

Rank: 3Rank: 3

帖子
137
精华
0
声望
509 点
金币
67 Mold
被赞许
1 次
注册时间
2010-9-25

可爱小猫

发表于 2011-1-5 22:09:11 |显示全部楼层
哈哈  我理解不了   不发表意见了,就顶一下吧

使用道具 举报

Rank: 3Rank: 3

帖子
0
精华
0
声望
110 点
金币
40 Mold
被赞许
3 次
注册时间
2009-6-18
发表于 2011-1-26 13:33:50 |显示全部楼层
照样是没有加路点和计划

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

回顶部