FSM(Finite State Machine)有限状态机

简称状态机(State Machine),表示“有限个“状态以及在这些状态之间进行转换(Transation)
和动作(Action)等行为的数学模型。

image.png

(以上解释来自百度百科)

wiki page:
https://en.wikipedia.org/wiki/Finite-state_machine

http://wiki.unity3d.com/index.php/Finite_State_Machine
这是unity3d的wiki,里面有实现的例子

状态机的应用无处不在,比如敌人的AI,角色的状态,或是现实生活中我们坐地铁
的验票闸门(turnstile).

对于简单的有限状态机,我们通过switch case就可以实现,有些很简单的状态控制
确实可以如此,这样可以简少不必要的代码量。

但对于复杂的状态机,如游戏中角色的行为,我会有多种状态,比如Idle,Attack,Run,Injured等等,并且状态之间切换是有条件的,在Idle状态时可以切换到Run,Attack,但主角在Injured攻攻击状态时,是不可以直接切换到idle的,这需要满足某些条件,比如受击动画结束后,才可以进行idle状态切换。

当状态机要控制的逻辑比较复杂时,使用switch就不行了,他有哪些问题?
1.不易于维护和扩展,我任何修改都会影响到其它的状态
2.容易出错,代码都在一起,我们可能会改出新的问题。
3.耦合性太高,如果多人开发这个功能,就得频率解决冲突的问题。
这显然不是科学的设计方案。

我们看一下来自于unity wiki上的实现:

首先要有一个状态的基类,FSMState为“状态”的抽象基类,且每一个状态都包含一套独立的逻辑,也都要由一个单独的FSMState派生类来实现。派生类和FSMState基类是"is a"的关系。

当状态比较多的时候,我们需要去管理这些状态,所以要有一个状态的管理器FSMSystem,管理着每一个状态。那么每一个我们具体要操作的对象都
拥有一个FSMSystem,比如NPC,当然,对于某些复杂的情况,我们可能会拥有多个FSMSystem,比如NPC是有情绪的,情绪也可以是一套复杂的状态机,攻击的行为可能会受到情绪的影响,那么状态机之间还会交互使用。
FSMSystem和拥有他的对象是“has a"的关系。

一个简单的FSMState示意图:


image.png

状态与状态之间可以相互切换,但相互切换是需要条件的,比如说从HappyState切换到AngryState状态,开心到愤怒的状态变化一定是有原因的,比如点的外卖孜然羊肉里面,没有羊肉,并且在切换前和切换后都要执行一些操作,比如资源的加载和释放,相关参数的初始化和重置等等。

所以具体的State类中,要有一个列表保存着各种他可以Transition到的状态。比如
Happy状态是可以切换到AngryState以及TiredState等等.

https://en.wikipedia.org/wiki/Finite-state_machine中提供了一个Example:
NPC的默认状态是在巡逻,按照固定的路径点,如果角色Player靠近NPC,那么
NPC就会切换到追逐的状态,当在追逐的过程中,角色跑远了或者跑得比较快,离NPC达到一定的距离,那么NPC就放弃追逐,继续回来巡逻,如此反复。
这里的Transation是Enum枚举,他是状态转移的原由。

上代码,并做解读:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/**
A Finite State Machine System based on Chapter 3.1 of Game Programming Gems 1 by Eric Dybsand

Written by Roberto Cezar Bianchini, July 2010


How to use:
  1. Place the labels for the transitions and the states of the Finite State System
      in the corresponding enums.

  2. Write new class(es) inheriting from FSMState and fill each one with pairs (transition-state).
      These pairs represent the state S2 the FSMSystem should be if while being on state S1, a
      transition T is fired and state S1 has a transition from it to S2. Remember this is a Deterministic FSM. 
      You can't have one transition leading to two different states.

     Method Reason is used to determine which transition should be fired.
     You can write the code to fire transitions in another place, and leave this method empty if you
     feel it's more appropriate to your project.

     Method Act has the code to perform the actions the NPC is supposed do if it's on this state.
     You can write the code for the actions in another place, and leave this method empty if you
     feel it's more appropriate to your project.

  3. Create an instance of FSMSystem class and add the states to it.

  4. Call Reason and Act (or whichever methods you have for firing transitions and making the NPCs
       behave in your game) from your Update or FixedUpdate methods.

  Asynchronous transitions from Unity Engine, like OnTriggerEnter, SendMessage, can also be used, 
  just call the Method PerformTransition from your FSMSystem instance with the correct Transition 
  when the event occurs.



THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE 
AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/


/// <summary>
/// Place the labels for the Transitions in this enum.
/// Don't change the first label, NullTransition as FSMSystem class uses it.
/// </summary>
public enum Transition
{
  NullTransition = 0, // Use this transition to represent a non-existing transition in your system
}

/// <summary>
/// Place the labels for the States in this enum.
/// Don't change the first label, NullTransition as FSMSystem class uses it.
/// </summary>
public enum StateID
{
  NullStateID = 0, // Use this ID to represent a non-existing State in your system    
}

/// <summary>
/// This class represents the States in the Finite State System.
/// Each state has a Dictionary with pairs (transition-state) showing
/// which state the FSM should be if a transition is fired while this state
/// is the current state.
/// Method Reason is used to determine which transition should be fired .
/// Method Act has the code to perform the actions the NPC is supposed do if it's on this state.
/// </summary>
public abstract class FSMState
{
  protected Dictionary<Transition, StateID> map = new Dictionary<Transition, StateID>();
  protected StateID stateID;
  public StateID ID { get { return stateID; } }

  public void AddTransition(Transition trans, StateID id)
  {
      // Check if anyone of the args is invalid
      if (trans == Transition.NullTransition)
      {
          Debug.LogError("FSMState ERROR: NullTransition is not allowed for a real transition");
          return;
      }

      if (id == StateID.NullStateID)
      {
          Debug.LogError("FSMState ERROR: NullStateID is not allowed for a real ID");
          return;
      }

      // Since this is a Deterministic FSM,
      //   check if the current transition was already inside the map
      if (map.ContainsKey(trans))
      {
          Debug.LogError("FSMState ERROR: State " + stateID.ToString() + " already has transition " + trans.ToString() + 
                         "Impossible to assign to another state");
          return;
      }

      map.Add(trans, id);
  }

  /// <summary>
  /// This method deletes a pair transition-state from this state's map.
  /// If the transition was not inside the state's map, an ERROR message is printed.
  /// </summary>
  public void DeleteTransition(Transition trans)
  {
      // Check for NullTransition
      if (trans == Transition.NullTransition)
      {
          Debug.LogError("FSMState ERROR: NullTransition is not allowed");
          return;
      }

      // Check if the pair is inside the map before deleting
      if (map.ContainsKey(trans))
      {
          map.Remove(trans);
          return;
      }
      Debug.LogError("FSMState ERROR: Transition " + trans.ToString() + " passed to " + stateID.ToString() + 
                     " was not on the state's transition list");
  }

  /// <summary>
  /// This method returns the new state the FSM should be if
  ///    this state receives a transition and 
  /// </summary>
  public StateID GetOutputState(Transition trans)
  {
      // Check if the map has this transition
      if (map.ContainsKey(trans))
      {
          return map[trans];
      }
      return StateID.NullStateID;
  }

  /// <summary>
  /// This method is used to set up the State condition before entering it.
  /// It is called automatically by the FSMSystem class before assigning it
  /// to the current state.
  /// </summary>
  public virtual void DoBeforeEntering() { }

  /// <summary>
  /// This method is used to make anything necessary, as reseting variables
  /// before the FSMSystem changes to another one. It is called automatically
  /// by the FSMSystem before changing to a new state.
  /// </summary>
  public virtual void DoBeforeLeaving() { } 

  /// <summary>
  /// This method decides if the state should transition to another on its list
  /// NPC is a reference to the object that is controlled by this class
  /// </summary>
  public abstract void Reason(GameObject player, GameObject npc);

  /// <summary>
  /// This method controls the behavior of the NPC in the game World.
  /// Every action, movement or communication the NPC does should be placed here
  /// NPC is a reference to the object that is controlled by this class
  /// </summary>
  public abstract void Act(GameObject player, GameObject npc);

} // class FSMState

说明:
Transition:
枚举常量,定义了转移的条件或原因,比如例子中,SawPlayer,表示看到玩家以后,LostPlayer玩家丢失以后等等,表明了转换的条件,该条件发生后,要转换到哪个状态。

StateID:
枚举常量,各种State状态的标识,每一个具体的State类,都对应一个StateID,在添加Transation时,也需要使用StateID,表示转移对应的状态。

protected Dictionary<Transition, StateID> map = new Dictionary<Transition, StateID>();
一个具体的State他会包含多个转移条件,所以使用Dic保存,通过通过key查找即可。不需要遍历搜索。

protected StateID stateID;
表示当前State的ID
public void AddTransition(Transition trans, StateID id)
为当前的状态添加转移条件,参数是转移条件以及转换对应的状态ID
public void DeleteTransition(Transition trans)
删除当前状态的转移条件,比如说有些转移条件只触发一次的情况
public StateID GetOutputState(Transition trans)
根据转移条件,获取转移条件对应的StateID,在调用performTransition时,
要通过该方法返回的StateID,在States的List中找到指定的State对象并执行

public virtual void DoBeforeEntering() { }
State切换后,做的初始化操作
public virtual void DoBeforeLeaving() { }
State切换前,对资源进行释放或是重置

public abstract void Reason(GameObject player, GameObject npc);
Reason,在当前的状态中,我要时刻判断我是否满足转移到其它条件的逻辑,比如
我在巡逻的时候,我要时刻检测主角是否靠近了我的可视范围内,如果满足,就进行条件的转移,所以通过Reason都是在Update中执行

public abstract void Act(GameObject player, GameObject npc);
Act表示当前状态的行为,动作,即当前状态要做的事儿,比如巡逻的逻辑。状态的行为部分都在这里执行。

以上就是FSMState类,Transition的添加删除,切换前后的资源处理,以及处理转换和当前状态的行为方法等等

FSMSystem

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// FSMSystem class represents the Finite State Machine class.
///  It has a List with the States the NPC has and methods to add,
///  delete a state, and to change the current state the Machine is on.
/// </summary>
public class FSMSystem
{
  private List<FSMState> states;

  // The only way one can change the state of the FSM is by performing a transition
  // Don't change the CurrentState directly
  private StateID currentStateID;
  public StateID CurrentStateID { get { return currentStateID; } }
  private FSMState currentState;
  public FSMState CurrentState { get { return currentState; } }

  public FSMSystem()
  {
      states = new List<FSMState>();
  }

  /// <summary>
  /// This method places new states inside the FSM,
  /// or prints an ERROR message if the state was already inside the List.
  /// First state added is also the initial state.
  /// </summary>
  public void AddState(FSMState s)
  {
      // Check for Null reference before deleting
      if (s == null)
      {
          Debug.LogError("FSM ERROR: Null reference is not allowed");
      }

      // First State inserted is also the Initial state,
      //   the state the machine is in when the simulation begins
      if (states.Count == 0)
      {
          states.Add(s);
          currentState = s;
          currentStateID = s.ID;
          return;
      }

      // Add the state to the List if it's not inside it
      foreach (FSMState state in states)
      {
          if (state.ID == s.ID)
          {
              Debug.LogError("FSM ERROR: Impossible to add state " + s.ID.ToString() + 
                             " because state has already been added");
              return;
          }
      }
      states.Add(s);
  }

  /// <summary>
  /// This method delete a state from the FSM List if it exists, 
  ///   or prints an ERROR message if the state was not on the List.
  /// </summary>
  public void DeleteState(StateID id)
  {
      // Check for NullState before deleting
      if (id == StateID.NullStateID)
      {
          Debug.LogError("FSM ERROR: NullStateID is not allowed for a real state");
          return;
      }

      // Search the List and delete the state if it's inside it
      foreach (FSMState state in states)
      {
          if (state.ID == id)
          {
              states.Remove(state);
              return;
          }
      }
      Debug.LogError("FSM ERROR: Impossible to delete state " + id.ToString() + 
                     ". It was not on the list of states");
  }

  /// <summary>
  /// This method tries to change the state the FSM is in based on
  /// the current state and the transition passed. If current state
  ///  doesn't have a target state for the transition passed, 
  /// an ERROR message is printed.
  /// </summary>
  public void PerformTransition(Transition trans)
  {
      // Check for NullTransition before changing the current state
      if (trans == Transition.NullTransition)
      {
          Debug.LogError("FSM ERROR: NullTransition is not allowed for a real transition");
          return;
      }

      // Check if the currentState has the transition passed as argument
      StateID id = currentState.GetOutputState(trans);
      if (id == StateID.NullStateID)
      {
          Debug.LogError("FSM ERROR: State " + currentStateID.ToString() +  " does not have a target state " + 
                         " for transition " + trans.ToString());
          return;
      }

      // Update the currentStateID and currentState       
      currentStateID = id;
      foreach (FSMState state in states)
      {
          if (state.ID == currentStateID)
          {
              // Do the post processing of the state before setting the new one
              currentState.DoBeforeLeaving();

              currentState = state;

              // Reset the state to its desired condition before it can reason or act
              currentState.DoBeforeEntering();
              break;
          }
      }

  } // PerformTransition()

} //class FSMSystem

说明:
private List<FSMState> states;
状态系统中要绘制所有的状态。通过List来保存,如果状态比较多的话,也建议使用
Dic字典来保存,这样就不需要遍历,提高查找效率。

private StateID currentStateID;
当前正在执行的StateID

private FSMState currentState;
当前正在执行的State对象

public void AddState(FSMState s)
添加状态到System中,不可以重复添加,并且第一次添加,
要初始化currentState

public void DeleteState(StateID id)
将状态从System中移除

public void PerformTransition(Transition trans)
核心方法,执行状态的转换操作

核心的类就两个,FSMState和FSMSystem

下面是官方提供例子的代码:

using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
public class NPCControl : MonoBehaviour
{
  public GameObject player;
  public Transform[] path;
  private FSMSystem fsm;

  public void SetTransition(Transition t) { fsm.PerformTransition(t); }

  public void Start()
  {
      MakeFSM();
  }

  public void FixedUpdate()
  {
      fsm.CurrentState.Reason(player, gameObject);
      fsm.CurrentState.Act(player, gameObject);
  }

  // The NPC has two states: FollowPath and ChasePlayer
  // If it's on the first state and SawPlayer transition is fired, it changes to ChasePlayer
  // If it's on ChasePlayerState and LostPlayer transition is fired, it returns to FollowPath
  private void MakeFSM()
  {
      FollowPathState follow = new FollowPathState(path);
      follow.AddTransition(Transition.SawPlayer, StateID.ChasingPlayer);

      ChasePlayerState chase = new ChasePlayerState();
      chase.AddTransition(Transition.LostPlayer, StateID.FollowingPath);

      fsm = new FSMSystem();
      fsm.AddState(follow);
      fsm.AddState(chase);
  }
}

说明:
NPC类,即使用FSM的对象,包含了一个FSMSystem的引用,以及多个状态操作
要完成状态的Transition添加以及State的添加
在FixedUpdate中要实时的执行两个方法,当前状态转移的逻辑Reason以及当前状态的行为Act。
MakeFSM完成状态的初始化操作。

FollowPathState

public class FollowPathState : FSMState
{
  private int currentWayPoint;
  private Transform[] waypoints;

  public FollowPathState(Transform[] wp) 
  { 
      waypoints = wp;
      currentWayPoint = 0;
      stateID = StateID.FollowingPath;
  }

  public override void Reason(GameObject player, GameObject npc)
  {
      // If the Player passes less than 15 meters away in front of the NPC
      RaycastHit hit;
      if (Physics.Raycast(npc.transform.position, npc.transform.forward, out hit, 15F))
      {
          if (hit.transform.gameObject.tag == "Player")
              npc.GetComponent<NPCControl>().SetTransition(Transition.SawPlayer);
      }
  }

  public override void Act(GameObject player, GameObject npc)
  {
      // Follow the path of waypoints
      // Find the direction of the current way point 
      Vector3 vel = npc.rigidbody.velocity;
      Vector3 moveDir = waypoints[currentWayPoint].position - npc.transform.position;

      if (moveDir.magnitude < 1)
      {
          currentWayPoint++;
          if (currentWayPoint >= waypoints.Length)
          {
              currentWayPoint = 0;
          }
      }
      else
      {
          vel = moveDir.normalized * 10;

          // Rotate towards the waypoint
          npc.transform.rotation = Quaternion.Slerp(npc.transform.rotation,
                                                    Quaternion.LookRotation(moveDir),
                                                    5 * Time.deltaTime);
          npc.transform.eulerAngles = new Vector3(0, npc.transform.eulerAngles.y, 0);

      }

      // Apply the Velocity
      npc.rigidbody.velocity = vel;
  }

} // FollowPathState

说明:
FollowPathState 巡逻的状态,在巡逻的逻辑中我们可以Transition到追逐Player的状态。
所以Reason方法,一直在判断Player是否在靠近它。
而Act则一直是在处理巡逻的动作。

ChasePlayerState

public class ChasePlayerState : FSMState
{
  public ChasePlayerState()
  {
      stateID = StateID.ChasingPlayer;
  }

  public override void Reason(GameObject player, GameObject npc)
  {
      // If the player has gone 30 meters away from the NPC, fire LostPlayer transition
      if (Vector3.Distance(npc.transform.position, player.transform.position) >= 30)
          npc.GetComponent<NPCControl>().SetTransition(Transition.LostPlayer);
  }

  public override void Act(GameObject player, GameObject npc)
  {
      // Follow the path of waypoints
      // Find the direction of the player         
      Vector3 vel = npc.rigidbody.velocity;
      Vector3 moveDir = player.transform.position - npc.transform.position;

      // Rotate towards the waypoint
      npc.transform.rotation = Quaternion.Slerp(npc.transform.rotation,
                                                Quaternion.LookRotation(moveDir),
                                                5 * Time.deltaTime);
      npc.transform.eulerAngles = new Vector3(0, npc.transform.eulerAngles.y, 0);

      vel = moveDir.normalized * 10;

      // Apply the new Velocity
      npc.rigidbody.velocity = vel;
  }

} // ChasePlayerState

说明:
追逐主角的状态,追逐主角可以Transition到巡逻状态,因为主角离得太远,跑得太快,跟丢了,我要恢复到继续巡逻的状态
那么Reason一直在判断,我是否把Player给跟丢了,而Act则执行当前的动作,
跟住Player。

这是个清晰简单的FSM状态机Example,很容易理解。

闲言碎语:
今天就到这里。现在是20:55,我在雕刻时光,都车已经充好了电,今天是阴历的生日,约了去看首富,我这是第二次刷,一会儿出去吃点儿面,明天再工作一天,后天天要去济南一趟,答应朋友帮忙(要是我问清楚了,我会拒绝的,因为时间4天,最近时间特别紧张,回来要忙着新工作面试,既然答应 了,不好拒绝就好好帮忙吧,以后再碰到,多考虑考虑自己的立场,肩膀写得有点疼,这咖啡馆的椅子快和桌子一样高了。
我准备回去把他录成视频,我最近在录一系列的课程,以前从来没有录过,尝试了几天,被无数次的NG给搞疯了,不过发现这有一定的帮忙,无数的NG加深了对知识的记忆,再坚持一段时间,起码把这系列的课程给做出来!

2018.8.18 30岁的生日,雕刻时光(北苑店)

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,445评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,889评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,047评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,760评论 1 276
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,745评论 5 367
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,638评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,011评论 3 398
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,669评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,923评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,655评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,740评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,406评论 4 320
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,995评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,961评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,197评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,023评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,483评论 2 342

推荐阅读更多精彩内容