游戏服务器AI 之行为树(基于behavior designer)(一)

前言
AI应该是MMO类游戏服务器的一个主要模块,AI设计的好坏直接影响着游戏的品质。在现行的AI实现方案中,主要包括下面几大类

方案一:有限状态机(FSM)
顾名思义,就是列举出所有的状态,根据不同的状态去处理相应的逻辑

例如游戏中的一般的小怪的AI:
  1. 没有敌人的时候就执行巡逻动作
  2. 发现敌人就攻击敌人
  3. 攻击距离不够则追击
  4. 超出最大的追击距离则返回活动范围继续执行待机巡逻
在状态机的世界里,我们首先罗列出怪的全部的状态,分别是待机 巡逻、攻击、追击、返回活动范围这么四个状态,伪代码如下所示:
switch(status)
{
    case IdleStatus:
    {
        if(find_enemy)
        {
            SetStatus(AttackStatus);
        }
        else
        {
            do_idle();
        }
        break;
    }
    case AttackStatus:
    {
        if(short_distance)
        {
            SetStatus(PursueStatus);
        }
        else
        {
            do_attack();
        }
        break;
    }
    case PursueStatus:
    {
        if(enough_distance)
        {
            SetStatus(AttackStatus);
        }
        else if(exceed_max_pursue_distance)
        {
            SetStatus(BackStatus);
        }
        else
        {
            do_pursue();
        }
        break;
    }
    case BackStatus:
    {
        if(readch_point)
        {
            SetStatus(IdleStatus);
        }
        else
        {
            do_back();
        }
        break;
    }
}
上面的代码大概就是一般的状态机的写法,优点很明显就是逻辑比较清楚,基本上是按照我们正常的思维来的。缺点也很明显,一旦状态过多,扩展性是个很大的问题。就以这个例子为例,当我们再加上血量低于多少的时候逃跑,逃跑到安全区域后回血再返回原始的活动范围,或者是待机的动作不光是巡逻,还会有几率地触发张望,睡觉等动作,你会发现你的状态机越写越复杂。


所以当你的AI比较简单,状态较少的时候可以用状态机,一旦AI状态过多就要考虑用其他方案了,比如说:行为树(下面会详细讲到)

方案二: 分层状态机(HFSM)
分层状态从字面上的意思也很好理解,当AI的状态比较多的时候,我们可以对AI的状态进行分类,每一个大类里面就是一个小的状态机的。
以刚才的例子为例:待机的状态可能不止是巡逻,还会有坐着、趴着、睡觉等状态, 这时候可以把待机作为最外层的一个大状态,用于最外层的switch-case。里面再switch-case各个小的状态。这种类似于软件设计的分层概念,虽然降低了状态过多引起的代码的复杂度,但扩展性还是有所限制。代码我就不写了

方案三:行为树(behavior tree)
从名字上看,行为树它是一种树结构。它具有树的所有特性,有根节点,父节点,孩子节点,叶子节点,无环性。行为树的执行原理就是: 每一帧都从根节点开始执行

可能初次听说这个大家可能首先想到的就是性能的问题,每一帧都从头来,那性能是不是很低。诚然,相比于状态机,行为树的性能会有所降低,但是带来的扩展性和可维护性确是非常高的,而且一旦行为树结构编写完成,后续的AI工作只需要交给策划来编辑配置,代码框架不会有任何改动,对于我们开发者来说只需要增加独立的节点而已。后面我会介绍一种很方便的行为树编辑软件: behavior designer,它的可视化效果做的比较好,而且也能很方便地导出行为树的配置文件。


针对性能问题:在行为树的节点设计中,我们也可以根据具体的情况,通过一些手段来优化效率。后面的章节我会分享在行为树编写的过程中遇到的问题和难点


本章完,待续~~~

下一章:行为树节点类型介绍



  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
This extension requires one license per seat Requires Unity 4.6.0 or higher. Behavior trees are used by AAA studios to create a lifelike AI. With Behavior Designer, you can bring the power of behaviour trees to Unity! Behavior Designer is a behaviour tree implementation designed for everyone - programmers, artists, designers. Behavior Designer offers an intuitive visual editor with a powerful API allowing you to easily create new tasks. It also includes hundreds of tasks, PlayMaker integration, and extensive third party integ ration making it possible to create complex AIs without having to write a single line of code! Behavior Designer was designed from the ground up to be as efficient as possible with zero allocations after initialization. As a result, it runs great on all platforms including mobile. Behavior Designer is dedicated to behavior trees, which means that each update will be a better behavior tree implementation. Features: - An intuitive visual editor - A powerful API - Visual runtime debugger - Variables to communicate between tasks - Conditional Aborts - Built in event system - Unity 5 multiplayer support - Use existing code with reflection tasks - Hundreds of tasks - Evaluate tasks using Utility Theory - Realtime error detection - Binary or JSON serialization - Data-oriented design - Zero runtime allocations after startup - Object drawers (property drawers) - Includes runtime source code - Extensive documentation and videos - Sample projects available online - And more Addon Packs: - Formations Pack - Movement Pack - Tactical Pack Complete Projects: - Deathmatch AI Kit Third Party Integrations: - 2D Toolkit - A* Pathfinding Project (Movement Pack) - Adventure Creator - Anti-Cheat Toolkit - Apex Path (Movement Pack) - Blox - Camera Path Animator - Chronos - Cinema Director - Control Freak - Core GameKit - Curvy - Dialogue System - DOTween - Final IK - Glow Effect - ICode - Inventory Pro - LeanTween - Love/Hate -

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值