在用Lua写AI脚本这一块,这是我从网上找到的唯一一篇文章,而且写的非常好,读后受宜菲浅!让我少走很多弯路。但文章源出处找不到了,在这里对这篇文章的作者表示非常的感谢!
LUA实现角色AI的新方法
怪物AI只需要提供3种条件集:
1. 无目标的条件集
2. 有目标的条件集
3. 无论是有目标还是无目标, 都必须检测的条件集. 或者叫做定时器条件集.
假设目前我们为游戏提供如下类型的怪物表现: [来自ai_define.lua]
--ai类型 : 废材型 表现为被打也不还手, 继续随机走动
ai_useless = { no_target = as_idle, has_target = as_forget, tick = as_tick}
--ai类型 : 标准型 表现为被打了就还手并追击, 自己不会主动搜敌
ai_standard = { no_target = as_idle, has_target = as_chase , tick = as_tick}
--ai类型 : 进攻型 表现为保持警戒搜敌,有目标就追击
ai_attacker = { no_target = as_guard, has_target = as_chase , tick = as_tick}
--ai类型 : 胆小型 表现为被打了就反方向逃跑了
ai_flee = { no_target = as_idle, has_target = as_flee , tick = as_tick}
--ai类型 : 机智型 表现为保持警戒搜敌, 如果目标靠近, 就躲远点继续攻击,
用于高级远程攻击怪
ai_smart = { no_target = as_guard,has_target = as_around, tick = as_tick}
--ai类型 : 巡逻型 表现为两点间巡逻, 并保持警戒搜敌, 有目标就追击
ai_patrol = { no_target = as_patrol, has_target = as_chase, tick = as_tick}
然后写出如下两个AI运行的核心函数
function handle_state(c, state) --处理条件集(state), 参数c表示角色对象
if state==nil then return end
for con, event in state do --遍历条件集中的所有条件
if con==1 then
event(c) --永为真的条件, 必然执行事件
else
local r &#
LUA实现角色AI的新方法
怪物AI只需要提供3种条件集:
1. 无目标的条件集
2. 有目标的条件集
3. 无论是有目标还是无目标, 都必须检测的条件集. 或者叫做定时器条件集.
假设目前我们为游戏提供如下类型的怪物表现: [来自ai_define.lua]
--ai类型 : 废材型 表现为被打也不还手, 继续随机走动
ai_useless = { no_target = as_idle, has_target = as_forget, tick = as_tick}
--ai类型 : 标准型 表现为被打了就还手并追击, 自己不会主动搜敌
ai_standard = { no_target = as_idle, has_target = as_chase , tick = as_tick}
--ai类型 : 进攻型 表现为保持警戒搜敌,有目标就追击
ai_attacker = { no_target = as_guard, has_target = as_chase , tick = as_tick}
--ai类型 : 胆小型 表现为被打了就反方向逃跑了
ai_flee = { no_target = as_idle, has_target = as_flee , tick = as_tick}
--ai类型 : 机智型 表现为保持警戒搜敌, 如果目标靠近, 就躲远点继续攻击,
用于高级远程攻击怪
ai_smart = { no_target = as_guard,has_target = as_around, tick = as_tick}
--ai类型 : 巡逻型 表现为两点间巡逻, 并保持警戒搜敌, 有目标就追击
ai_patrol = { no_target = as_patrol, has_target = as_chase, tick = as_tick}
然后写出如下两个AI运行的核心函数
function handle_state(c, state) --处理条件集(state), 参数c表示角色对象
if state==nil then return end
for con, event in state do --遍历条件集中的所有条件
if con==1 then
event(c) --永为真的条件, 必然执行事件
else
local r &#