Post Pacific  

Go Back   Post Pacific > Lineage 2 > Lineage 2 General
FAQ Community Calendar Today's Posts Search

Notices

 
 
Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
Old 06.05.2006, 04:05   #1
Digital
Архивный участник
 
Join Date: Nov 2004
Posts: 1,405
Default [Tutorial] AI example SP3-4 [Chinese]

thx to ezme n coffeepot


AI教程--SP3 AI对事件的控制过程


前面我介绍了AI的基础知识以及最简单 的修改办法,从这部分开始,我将介ç» �AI的实际工作方法以及自己编写ai脚本 的方法。
首先介绍一下ai的handler。
在目前的ai里面,我们能提取出63个hand ler。那么handler起什么用呢?我们通过å ˆ†析一个完整ai的结构可以发现,一个
handler_begin ID
...
handler_end
所代表是一个事件的控制方法,它是å¤ ©堂中人工智能的事件管理者。
每一个hadler的ID代表的事件是不同的, 从逻辑上说独立事件不可能在逻辑上å� Œ时进行另外的事件,所以一个handler结 构内只能使用各种函数进行控制操作ï¼ Œ不能再引用别的handler。
比如如下的handler结构就是错误的
handler_begin ID1
...
handler_begin ID2
...
handler_end
...
handler_end
下面我公布ai中的63个handler以及代表的 处理事件
handler 0 // NO_DESIRE
handler 1 // ATTACKED
handler 2 // SPELLED
handler 3 // TALKED
handler 4 // TALK_SELECTED
handler 5 // SEE_CREATURE
handler 10 // MY_DYING
handler 11 // TIMER_FIRED
handler 12 // TIMER_FIRED_EX
handler 13 // CREATED
handler 16 // SEE_SPELL
handler 17 // OUT_OF_TERRITORY
handler 18 // DESIRE_MANIPULATION
handler 19 // PARTY_ATTACKED
handler 21 // PARTY_DIED
handler 23 // CLAN_ATTACKED
handler 25 // STATIC_OBJECT_CLAN_ATTACKED
handler 28 // TELEPORT_REQUESTED
handler 31 // QUEST_ACCEPTED
handler 32 // MENU_SELECTED
handler 33 // LEARN_SKILL_REQUESTED
handler 34 // ONE_SKILL_SELECTED
handler 36 // CLASS_CHANGE_REQUESTED
handler 42 // CREATE_PLEDGE
handler 43 // DISMISS_PLEDGE
handler 44 // REVIVE_PLEDGE
handler 45 // LEVEL_UP_PLEDGE
handler 48 // CREATE_ALLIANCE
handler 49 // SCRIPT_EVENT
handler 50 // TUTORIAL_EVENT
handler 51 // QUESTION_MARK_CLICKED
handler 52 // USER_CONNECTED
handler 61 // MOVE_TO_WAY_POINT_FINISHED
handler 62 // USE_SKILL_FINISHED
handler 63 // MOVE_TO_FINISHED
handler中可以使用的函数(204个):
func_call 0 // func[AddMoveAroundDesire]
func_call 1 // func[AddDoNothingDesire]
func_call 2 // func[AddAttackDesire]
func_call 3 // func[AddAttackDesireEx]
func_call 6 // func[AddFleeDesire]
func_call 7 // func[AddFollowDesire]
func_call 9 // func[AddMoveToWayPointDesire]
func_call 10 // func[AddUseSkillDesire]
func_call 11 // func[AddUseSkillDesireEx]
func_call 12 // func[AddMoveToDesire]
func_call 13 // func[AddEffectActionDesire]
func_call 14 // func[AddPetDefaultDesire_Follow]
func_call 15 // func[RemoveAllAttackDesire]
func_call 18 // func[MakeAttackEvent]
func_call 20 // func[GetLifeTime]
func_call 21 // func[GetTick]
func_call 23 // func[GetCurrentTick]
func_call 24 // func[GetDirection]
func_call 25 // func[SetCookie]
func_call 26 // func[GetCookie]
func_call 27 // func[SetTeleportPosOnLost]
func_call 28 // func[AddTimerEx]
func_call 29 // func[GetIndexFromCreature]
func_call 30 // func[GetCreatureFromIndex]
func_call 31 // func[RegisterGlobalMap]
func_call 32 // func[UnregisterGlobalMap]
func_call 33 // func[GetGlobalMap]
func_call 34 // func[SendScriptEvent]
func_call 36 // func[IsSpoiled]
func_call 37 // func[Say]
func_call 42 // func[Shout]
func_call 43 // func[ShowPage]
func_call 45 // func[Teleport]
func_call 46 // func[InstantTeleport]
func_call 47 // func[Sell]
func_call 48 // func[Buy]
func_call 51 // func[DistFromMe]
func_call 52 // func[SetTimerPeriodByTick]
func_call 53 // func[SetTimerPeriod]
func_call 54 // func[GetWayPointDelay]
func_call 55 // func[Rand]
func_call 57 // func[FloatToInt]
func_call 58 // func[GetMemoCount]
func_call 59 // func[HaveMemo]
func_call 60 // func[SetMemo]
func_call 61 // func[SetMemoState]
func_call 62 // func[GetMemoState]
func_call 63 // func[RemoveMemo]
func_call 64 // func[SetJournal]
func_call 65 // func[GetOneTimeQuestFlag]
func_call 66 // func[SetOneTimeQuestFlag]
func_call 67 // func[GetAbnormalLevel]
func_call 68 // func[SetDBValue]
func_call 74 // func[Pledge_GetLeader]
func_call 76 // func[GetPledge]
func_call 77 // func[GiveItem1]
func_call 80 // func[DeleteItem1]
func_call 81 // func[OwnItemCount]
func_call 83 // func[GetLastAttacker]
func_call 84 // func[FHTML_SetFileName]
func_call 85 // func[FHTML_SetInt]
func_call 86 // func[FHTML_SetFloat]
func_call 87 // func[FHTML_SetStr]
func_call 88 // func[ShowFHTML]
func_call 89 // func[MakeFString]
func_call 100 // func[IsMyLord]
func_call 103 // func[Castle_GetSiegeTime]
func_call 104 // func[Residence_GetTaxRate]
func_call 105 // func[Residence_GetTaxRateCurrent]
func_call 106 // func[Residence_SetTaxRate]
func_call 111 // func[Residence_VaultTakeOutMoney]
func_call 114 // func[Castle_GateOpenClose]
func_call 115 // func[Castle_GetPledgeState]
func_call 116 // func[Castle_IsUnderSiege]
func_call 117 // func[Castle_GetHPRegen]
func_call 118 // func[Castle_GetMPRegen]
func_call 119 // func[Castle_BanishOthers]
func_call 120 // func[Castle_SetHPRegen]
func_call 121 // func[Castle_SetMPRegen]
func_call 122 // func[Castle_GetRawSiegeTime]
func_call 123 // func[Castle_GetRawSystemTime]
func_call 125 // func[Castle_GateOpenClose2]
func_call 126 // func[Castle_GetPledgeId]
func_call 127 // func[Castle_GetLifeControlLevel]
func_call 128 // func[Agit_SetMaster]
func_call 129 // func[Agit_GetTeleportLevel]
func_call 130 // func[Agit_SetTeleportLevel]
func_call 131 // func[AddChoice]
func_call 132 // func[ShowChoicePage]
func_call 133 // func[ShowSkillList]
func_call 134 // func[ShowGrowSkillMessage]
func_call 143 // func[IsInCategory]
func_call 144 // func[ClassChange]
func_call 145 // func[Castle_GetCropRate]
func_call 146 // func[Castle_GetCropPrice]
func_call 148 // func[Residence_GetTaxIncome]
func_call 152 // func[Castle_PostCropPurchase]
func_call 154 // func[Skill_GetConsumeHP]
func_call 155 // func[Skill_GetConsumeMP]
func_call 156 // func[Skill_GetEffectPoint]
func_call 157 // func[Skill_GetAttribute]
func_call 158 // func[Skill_GetAbnormalType]
func_call 160 // func[Skill_InReuseDelay]
func_call 165 // func[CreatePrivates]
func_call 166 // func[CreateOnePrivate]
func_call 168 // func[Maker_GetNpcCount]
func_call 169 // func[Maker_FindNpcByKey]
func_call 170 // func[CreateOnePrivateEx]
func_call 171 // func[IncrementParam]
func_call 173 // func[SoundEffect]
func_call 174 // func[EffectMusic]
func_call 175 // func[Despawn]
func_call 177 // func[InMyTerritory]
func_call 178 // func[InstantTeleportInMyTerritory]
func_call 180 // func[EarthQuakeByNPC]
func_call 181 // func[SpecialCamera]
func_call 184 // func[PledgeLevelUp]
func_call 188 // func[ShowTutorialHTML]
func_call 189 // func[ShowTutorialHTML2]
func_call 190 // func[ShowQuestionMark]
func_call 192 // func[CloseTutorialHTML]
func_call 193 // func[EnableTutorialEvent]
func_call 194 // func[ShowRadar]
func_call 195 // func[DeleteRadar]
func_call 196 // func[CreatePet]
func_call 200 // func[OpenSiegeInfo]
func_call 201 // func[SetDoorHpLevel]
func_call 202 // func[GetDoorHpLevel]
func_call 203 // func[SetControlTowerLevel]
func_call 204 // func[GetControlTowerLevel]

这些函数只能独立在的handler中被使用ï ¼Œ而且各个函数有不同的变量格式。
举个例子:
我们从ai中提取出fun_call 43的运用方法:
shift_sp 1
push_event // talker
push_const 32
add
fetch_i
S0. "noquest.htm"
push_string S0
func_call 43 // func[ShowPage]
shift_sp -2
shift_sp -1
好了,开始分析
shift_sp 1 ---------堆栈指针加1,是为了空了放函数返回å €¼的空间
当前堆栈:
----------------------
空
----------------------
push_event // talker ---这是个对象指针
push_const 32 -----加上对象指针的偏移,相当于得到类ä¸ ­某个属性的值,相当于C++的 pTalker+32
当前堆栈:
-----------------------
32
event
空
----------------------
add
fetch_i 把前面talker+32的结果压入堆栈顶
当前堆栈:
-----------------------
event + 32 结果
空
----------------------
S0. "noquest.htm" -----S0标志,相当于一个define
push_string S0 ------压出字节,那么这些字节怎么处理呢ï¼ Ÿ往下看,这个就是函数来处理了
当前堆栈:
-----------------------
S0. "noquest.htm"
event + 32 结果
空
----------------------
func_call 43 // func[ShowPage] -----说明把S0所代表的用43这个函数处理
当前堆栈:
-----------------------
S0. "noquest.htm"
event + 32 结果
func_call 43 // func[ShowPage] 结果
当前堆栈:
----------------------
shift_sp -2 -----
-----------------------
func_call 43 // func[ShowPage] 结果
----------------------
shift_sp -1 --不需要用函数返回值,所以堆栈继续-1,清除函数调用结果
当前堆栈:
----------------------
----------------------
从这里我们就看出了这个函数工作时æ‰ €需要的变量string,也就是htm页面。
再深入一些,这个事件在客户段我们æ‰ §行将是什么效果呢?
我们点一个NPC,他是否跟我们说话,å° ±是它控制的^_^,好了,第一个秘密浮å ‡º水面了。

下面我写一段小handler来说明简单的hand ler应该怎么写。
假设我们要写一个npc的行为就是当人è· Ÿ他说话的时候他无意识的跟我们对话ã €‚
分析,这个事件其实就是一个对talk事ä »¶的处理,因为是无意识控制,那么我 们只要在handler中正确使用fun 43就好了。

handler 3 // TALKED ------声明处理的事件是当(玩家)跟"我"对 话的时候我该怎么做的事件处理
variable_begin -------开始
"talker"
"myself"
"_choiceN"
"_code"
"_from_choice"
variable_end -------------结束,从开始到结束这段是每个handlerå ›º定的,不同的handler有不同的这部分ï¼ Œ大家可以对应的提取出来,这样方便ä »¥后的编写

shift_sp 1 ---事件开始,给函数返回值留出空间
push_event // talker ---压入事件,说话者
push_const 32 ------声明压入的事件是32号事件,代表的是 说话者
add
fetch_i
S0. "hello.htm"
push_string S0 ----给下面的函数压入string S0,该怎么处理呢?往下面看
func_call 43 // func[ShowPage] -------说明的是展现给玩家S0所代表的页面
shift_sp -2 --------事件结束 小问题:这里为什么是-2呢?我们往前看,前面出现了push_const 32和push_string S0两个变量的负值行为,这里函数要清 空,那么自然就是-2了
shift_sp -1 ------主事件结束 函数返回
handler_end -----------handler结束

这样就清楚地写完了一个小小的功能〠‚

大家服务器都有自己的特色吧?!那ä¹ ˆ我们为何不用这段小小的程序来完成ä ¸€个巨大的功能呢?我们可以自己写一 个介绍自己服务器的htm文件,然后再æ– °手出生的地方刷一个NPC,NPC的行为就æ ˜¯无意识展现htm文件。该怎么做我想看 了这三部分已经没有问题了吧。

好了,这部分就介绍到这里,内容虽ç„ ¶少,但是需要大家提取的东西特别多ã €‚我多留一些时间,我再发SP4。
AI教程--SP4 AI的几种判断过程
在上一篇中我系统的介绍了ai的结构以 及ai对函数的调用。这一篇我简单介绍 下ai对事件的判断。

判断事件就是对事件对象进行有关数æ� ®的判断执行。这里的有关数据就是封å Œ…里的数据信息,可以是持有的物品, 可以是名字,可以是性别,可以是身ä¸ Š的状态以及属性,也可以是server对角è ‰²的控制过程等等。要想了解其中的信 息,大家可以截取一个客户段向serverå� ‘送的登陆封包分析下就出来了。

下面公布出一些常用的ID以及对应的意 思。

152 occupation(职业)
156 sex (性别)
160 name (名字)
412 karma (红名)
888 level (等级)

....

其中职业和性别的id大家自己整理,论å� ›上多的是.


下面说下修改人物属性的方法

函数原形
func_call 171 // func[IncrementParam]
那IncrementParam他的实际编写流程是这样 的
(CSharedCreatureData* c, int nType, float value)

看1个简单的例子
shift_sp 1
push_event // talker
push_const 32 (CSharedCreature)
add
fetch_i
push_const 1 ( nType)
push_const 30000(float value)
negate
func_call 171 // func[IncrementParam]
shift_sp -3
shift_sp -1
他的工作流程

shift_sp 1
push_event // talker(push_event 是把事件相关参数 (即CSharedCreature 指针)压栈)
push_const 32 (这边是说:指针偏移 32 字节,即移动到事件指针的第8个参数) (1)
add
fetch_i (fetch_i 是指取 CSharedCreature + 32 指针处的内容 )
push_const 1 (这边其实L2NPC提供的函数原形里type了) (2)
push_const 30000 (这边是float value) (3)
negate
func_call 171 // func[IncrementParam]
shift_sp -3 (平栈,上边1,2 3的位置我已经标出位置了 所以这边是shift_sp -3)
shift_sp -1
简单说一下这个func_call 171
他的几个type
[PARAM_EXP] = 0
[PARAM_SP] = 1
[PARAM_INT] = 2
[PARAM_STR] = 3
[PARAM_CON] = 4
[PARAM_MEN] = 5
[PARAM_DEX] = 6
[PARAM_WIT] = 7
[PARAM_LEVEL] = 8

这边的是INT ...WIT 他并非是DefaultINT .....DefaultWIT 这是有区别的

判断常用到的比较形式

equal 相等
greater_equal 大于等级
greater 大于
less 小于
less_equal 小于等于

下面就举例说下判断过程:
eg1:判断玩家是不是红名,是的话跳转,ä¸ �是的话继续
两种可能,一种是talker一种creature

talker

push_event // talker
push_const 32
add
fetch_i // karma
push_const 412
add
fetch_i
push_const 0
less_equal
branch_false L1
L0
..........
L1
..........

creature

push_event // creature
push_const 68
add
fetch_i // karma
push_const 412
add
fetch_i
push_const 0
less_equal
branch_false L1
L0
..........
L1
..........

这里要注意的是函数只能有一个返回å€ ¼,那么我们要在L0结束之后加一个jump L*,就是跳开L1段,到handler内其他段内.要 不然服务器就会挂掉.

再揭开一个秘密,我相信大家现在服务å ™¨里面都有自动加buff的NPC吧.具体的行ä ¸º就是看见白名主动加风走,看见红名ä¸ �加风走,而且喊话并且攻击,现在我们å° ±揭开这里面的秘密.

分析下,既然是主动判断,那么这里就要 用see creature这个handler.要喊话的话就要调用f string.txt里面语言的ID.这里假设我们在f string.txt里面定义了一段语言: 1001001:[杀人偿命欠债还钱!去死吧!!]

开始程序,首先写handler框架
handler 5 // SEE_CREATURE
variable_begin
"creature"
"myself"
"_choiceN"
"_code"
"_from_choice"
variable_end


handler_end

然后写判断
push_event // creature
push_const 68
add
fetch_i // karma
push_const 412
add
fetch_i
push_const 0
less_equal
branch_false L1 // 不是小于零(那就是红名了^^),跳转到L1, L1就是要发生喊话以及攻击,是则继续åˆ °L0,就要给玩家主动加风走.

主动加风走

shift_sp 1
push_event // creature
push_const 68
add
fetch_i
push_const 308226 //风走的ID.
push_const 0
push_const 1
push_const 1000000
func_call 10 // func[AddUseSkillDesire] 注明加风走,而且只加一次.
shift_sp -5
shift_sp -1

喊话行为的发生:
shift_sp 1
shift_sp 1
push_const 1001000 // [要说的话的ID]杀人偿命欠债还钱!去死吧!!
S0. ""
push_string S0
S1. ""
push_string S1
S2. ""
push_string S2
S3. ""
push_string S3
S4. ""
push_string S4
func_call 89 // func[MakeFString] 玩家聊天窗口显示字
shift_sp -6
func_call 42 // func[Shout] 发生喊话行为,说话者为NPC名称
shift_sp -1
shift_sp -1
攻击行为的发生

shift_sp 1
push_event // creature
push_const 68
add
fetch_i
push_const 0
push_const 2000
func_call 2 // func[AddAttackDesire] 对玩家发生攻击行为
shift_sp -3
shift_sp -1

好了现在融合下:
因为加风走是在判断为是的条件下的,è €Œ其他行为是在否的条件下的,那么我ä» ¬要在加了风走之后跳转到handler结束.è€ Œ喊话和攻击发生在判断为否的下的,那 么就要逻辑顺序进行,融合后的ai程序å¦ ‚下:
handler 5 // SEE_CREATURE
variable_begin
"creature"
"myself"
"_choiceN"
"_code"
"_from_choice"
variable_end

push_event // creature
push_const 68
add
fetch_i // karma
push_const 412
add
fetch_i
push_const 0
less_equal
branch_false L1 // 性向大于0
L0
shift_sp 1
push_event // creature
push_const 68
add
fetch_i
push_const 308226
push_const 0
push_const 1
push_const 1000000
func_call 10 // func[AddUseSkillDesire]
shift_sp -5
shift_sp -1
jump L3 // 结束
L1
shift_sp 1
shift_sp 1
push_const 1001001 // [要说的话的ID]杀人偿命欠债还钱!去死吧!!
S0. ""
push_string S0
S1. ""
push_string S1
S2. ""
push_string S2
S3. ""
push_string S3
S4. ""
push_string S4
func_call 89 // func[MakeFString]
shift_sp -6
func_call 42 // func[Shout]
shift_sp -1
shift_sp -1
L2 //这个L标签在这里可要可不要,要了还是 好点,一是方便查看,而是方便以后增加 功能调用.
shift_sp 1
push_event // creature
push_const 68
add
fetch_i
push_const 0
push_const 2000
func_call 2 // func[AddAttackDesire] 对玩家发生攻击行为
shift_sp -3
shift_sp -1
L3
handler_end


当然我们还能拓展下这个ai,即发生多åˆ ¤断,假如我们需要这个npc看到身上有命 运之翼头箍而且是白名的人喊话:英雄é ˜¿,我崇拜死你了!
那么我们第一步就要在fstring里面增加è ¿™个字段 1001002 : [英雄阿,我崇拜死你了!]
第二步,增加判断,因为发生条件为白名 前提,所以要紧加在判断性相之后,如下 :
push_event // creature
push_const 68
add
fetch_i // karma
push_const 412
add
fetch_i
push_const 0
less_equal
branch_false L1 // 性向大于0
L4
shift_sp 1
push_event // creature
push_const 68
add
fetch_i
push_const 6842 //物品ID
func_call 81 // func[OwnItemCount] 获得物品数量函数
shift_sp -2
push_const 1 //一个
greater_equal //大于等于
branch_false L5 //没有物品则跳转至L5段,有一个或者一ä¸ ª以上,继续进行
喊话

shift_sp 1
shift_sp 1
push_const 1001002 // [要说的话的ID]英雄阿,我崇拜死你了!
S5. ""
push_string S5
S6. ""
push_string S6
S7. ""
push_string S7
S8. ""
push_string S8
S9. ""
push_string S9
func_call 89 // func[MakeFString]
shift_sp -6
func_call 42 // func[Shout]
shift_sp -1
shift_sp -1

融合注意程序结构)
handler 5 // SEE_CREATURE
variable_begin
"creature"
"myself"
"_choiceN"
"_code"
"_from_choice"
variable_end

push_event // creature
push_const 68
add
fetch_i // karma
push_const 412
add
fetch_i
push_const 0
less_equal
branch_false L1 // 性向大于0
L4
shift_sp 1
push_event // creature
push_const 68
add
fetch_i
push_const 6842 //物品ID
func_call 81 // func[OwnItemCount] 获得物品数量函数
shift_sp -2
push_const 1 //一个
greater_equal //大于等于
branch_false L5 //没有物品则跳转至L5段,有一个或者一ä¸ ª以上,继续进行,喊话
L6
shift_sp 1
shift_sp 1
push_const 1001002 // [要说的话的ID]英雄阿,我崇拜死你了!
S5. ""
push_string S5
S6. ""
push_string S6
S7. ""
push_string S7
S8. ""
push_string S8
S9. ""
push_string S9
func_call 89 // func[MakeFString]
shift_sp -6
func_call 42 // func[Shout]
shift_sp -1
shift_sp -1
L5
L0
shift_sp 1
push_event // creature
push_const 68
add
fetch_i
push_const 308226
push_const 0
push_const 1
push_const 1000000
func_call 10 // func[AddUseSkillDesire]
shift_sp -5
shift_sp -1
jump L3 // 结束
L1
shift_sp 1
shift_sp 1
push_const 1001001 // [要说的话的ID]杀人偿命欠债还钱!去死吧!!
S0. ""
push_string S0
S1. ""
push_string S1
S2. ""
push_string S2
S3. ""
push_string S3
S4. ""
push_string S4
func_call 89 // func[MakeFString]
shift_sp -6
func_call 42 // func[Shout]
shift_sp -1
shift_sp -1
L2 //这个L标签在这里可要可不要,要了还是 好点,一是方便查看,而是方便以后增加 功能调用.
shift_sp 1
push_event // creature
push_const 68
add
fetch_i
push_const 0
push_const 2000
func_call 2 // func[AddAttackDesire] 对玩家发生攻击行为
shift_sp -3
shift_sp -1
L3
handler_end

好了,本篇就介绍到这里,谢谢大家的支 持.等ezme发出新的se我再继续发fina版,åœ ¨final版里面我将公布一些有用的程序æ® µ,并且详细介绍一些细节.届时希望大å® ¶通过我的教程能走出ai的盲区.

sp5--final预告:几个简单功能的ai控制实现æ •™本编写。
Digital is offline  
 


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. The time now is 23:50.