Finite State Machine

Contents

[hide]

Description

This is a Deterministic Finite State Machine framework based on chapter 3.1 of Game Programming Gems 1 by Eric Dybsend. Therea are two classes and two enums. Include them in your project and follow the explanations to get the FSM working properly. There‘s also a complete example script at the end of this page.

Components

  • Transition enum: This enum contains the labels to the transitions that can be fired by the system. Don‘t change the first label, NullTransition, as the FSMSystem class uses it.
  • StateID enum: This is the ID of the states the game may have. You could use references to the real States‘ classes but using enums makes the system less susceptible to have code having access to objects it is not supposed to. All the states‘ ids should be placed here. Don‘t change the first label, NullStateID, as the FSMSystem class uses it.
  • FSMState class: This class has a Dictionary with pairs (Transition-StateID) indicating which new state S2 the FSM should go to when a transition T is fired and the current state is S1. It has methods to add and delete pairs (Transition-StateID), a method to check which state to go to if a transition is passed to it. Two methods are used in the example given to check which transition should be fired (Reason()) and which action(s) (Act()) the GameObject that has the FSMState attached should do. You don‘t have to use this schema, but some kind of transition-action code must be used in your game.
  • FSMSystem: This is the Finite State Machine class that each NPC or GameObject in your game must have in order to use the framework. It stores the NPC‘s States in a List, has methods to add and delete a state and a method to change the current state based on a transition passed to it (PerformTransition()). You can call this method anywhere within your code, as in a collision test, or within Update() or FixedUpdate().

C# - FSMSystem.cs

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

/// <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

Example

Here‘s an example that implements the above framework. The GameObject with this script follows a path of waypoints and starts chasing a target if it comes within a certain distance from it. Attach this class to your NPC. Besides the framework transition and stateid enums, you also have to setup a reference to the waypoints and to the target (player). I used FixedUpdate() in the MonoBehaviour because the NPC reasoning schema doesn‘t need to be called every frame, but you can change that and use Update().

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Text;
  4 using UnityEngine;
  5
  6 [RequireComponent(typeof(Rigidbody))]
  7 public class NPCControl : MonoBehaviour
  8 {
  9     public GameObject player;
 10     public Transform[] path;
 11     private FSMSystem fsm;
 12
 13     public void SetTransition(Transition t) { fsm.PerformTransition(t); }
 14
 15     public void Start()
 16     {
 17         MakeFSM();
 18     }
 19
 20     public void FixedUpdate()
 21     {
 22         fsm.CurrentState.Reason(player, gameObject);
 23         fsm.CurrentState.Act(player, gameObject);
 24     }
 25
 26     // The NPC has two states: FollowPath and ChasePlayer
 27     // If it‘s on the first state and SawPlayer transition is fired, it changes to ChasePlayer
 28     // If it‘s on ChasePlayerState and LostPlayer transition is fired, it returns to FollowPath
 29     private void MakeFSM()
 30     {
 31         FollowPathState follow = new FollowPathState(path);
 32         follow.AddTransition(Transition.SawPlayer, StateID.ChasingPlayer);
 33
 34         ChasePlayerState chase = new ChasePlayerState();
 35         chase.AddTransition(Transition.LostPlayer, StateID.FollowingPath);
 36
 37         fsm = new FSMSystem();
 38         fsm.AddState(follow);
 39         fsm.AddState(chase);
 40     }
 41 }
 42
 43 public class FollowPathState : FSMState
 44 {
 45     private int currentWayPoint;
 46     private Transform[] waypoints;
 47
 48     public FollowPathState(Transform[] wp)
 49     {
 50         waypoints = wp;
 51         currentWayPoint = 0;
 52         stateID = StateID.FollowingPath;
 53     }
 54
 55     public override void Reason(GameObject player, GameObject npc)
 56     {
 57         // If the Player passes less than 15 meters away in front of the NPC
 58         RaycastHit hit;
 59         if (Physics.Raycast(npc.transform.position, npc.transform.forward, out hit, 15F))
 60         {
 61             if (hit.transform.gameObject.tag == "Player")
 62                 npc.GetComponent<NPCControl>().SetTransition(Transition.SawPlayer);
 63         }
 64     }
 65
 66     public override void Act(GameObject player, GameObject npc)
 67     {
 68         // Follow the path of waypoints
 69         // Find the direction of the current way point
 70         Vector3 vel = npc.rigidbody.velocity;
 71         Vector3 moveDir = waypoints[currentWayPoint].position - npc.transform.position;
 72
 73         if (moveDir.magnitude < 1)
 74         {
 75             currentWayPoint++;
 76             if (currentWayPoint >= waypoints.Length)
 77             {
 78                 currentWayPoint = 0;
 79             }
 80         }
 81         else
 82         {
 83             vel = moveDir.normalized * 10;
 84
 85             // Rotate towards the waypoint
 86             npc.transform.rotation = Quaternion.Slerp(npc. transform.rotation,Quaternion.LookRotation(moveDir), 5 * Time.deltaTime);
 89             npc.transform.eulerAngles = new Vector3(0, npc.transform.eulerAngles.y, 0);
 90
 91         }
 92
 93         // Apply the Velocity
 94         npc.rigidbody.velocity = vel;
 95     }
 96
 97 } // FollowPathState
 98
 99 public class ChasePlayerState : FSMState
100 {
101     public ChasePlayerState()
102     {
103         stateID = StateID.ChasingPlayer;
104     }
105
106     public override void Reason(GameObject player, GameObject npc)
107     {
108         // If the player has gone 30 meters away from the NPC, fire LostPlayer transition
109         if (Vector3.Distance(npc.transform.position, player.transform.position) >= 30)
110             npc.GetComponent<NPCControl>().SetTransition(Transition.LostPlayer);
111     }
112
113     public override void Act(GameObject player, GameObject npc)
114     {
115         // Follow the path of waypoints
116         // Find the direction of the player
117         Vector3 vel = npc.rigidbody.velocity;
118         Vector3 moveDir = player.transform.position - npc.transform.position;
119
120         // Rotate towards the waypoint
121         npc.transform.rotation = Quaternion.Slerp(npc.transform.rotation,
122                                                   Quaternion.LookRotation(moveDir),
123                                                   5 * Time.deltaTime);
124         npc.transform.eulerAngles = new Vector3(0, npc.transform.eulerAngles.y, 0);
125
126         vel = moveDir.normalized * 10;
127
128         // Apply the new Velocity
129         npc.rigidbody.velocity = vel;
130     }
131
132 } // ChasePlayerState
 
时间: 2024-11-08 20:45:18

Finite State Machine的相关文章

Finite State Machine 是什么?

状态机(Finite State Machine):状态机由状态寄存器和组合逻辑电路构成,能够根据控制信号按照预先设定的状态进行状态转移,是协调相关信号动       作.完成特定操作的控制中心. 类别: ~ 若输出只和状态有关而与输入无关,则称为Moore状态机 ~ 输出不仅和状态有关而且和输入有关系,则称为Mealy状态机 关于状态机的一个极度确切的描述是它是一个有向图形,由一组节点和一组相应的转移函数组成.状态机通过响应一系列事件而"运行".每个事件都在属于"当前&qu

Finite State Machine 有限状态机

? ?????首先这是一篇FSM翻译,关于Finite State Machine 的架构赏析,如果项目对ai需求不是非常强,可以在此基础上扩展,keyle也是在学习中欢迎交流,后面两篇计划是在写一篇Behavior?Tree(行为树),最后一篇实现基于Lua的AI的热更新?QQ群交流:137728654 ? ? Finite State Machine Contents 1?Description 2?Components 3?C# - FSMSystem.cs 4?Example Descr

paper:synthesizable finite state machine design techniques using the new systemverilog 3.0 enhancements 之 standard verilog FSM conding styles(二段式)

1.Two always block style with combinational outputs(Good Style) 对应的代码如下: 2段式总结: (1)the combinational always block sensitivity list is sensitve to changes on the state variable and all of the inputs referenced in the combinaltional always block. 这个实际中

paper:synthesizable finite state machine design techniques using the new systemverilog 3.0 enhancements 之 standard verilog FSM conding styles(三段式)

Three always block style with registered outputs(Good style)

Java Secret: Using an enum to build a State machine(Java秘术:用枚举构建一个状态机)

近期在读Hadoop#Yarn部分的源代码.读到状态机那一部分的时候,感到enmu的使用方法实在是太灵活了,在给并发编程网翻译一篇文章的时候,正好碰到一篇这种文章.就赶紧翻译下来,涨涨姿势. 原文链接:http://www.javacodegeeks.com/2011/07/java-secret-using-enum-to-build-state.html 作者:Peter Lawrey    译者:陈振阳 综述 Java中的enum比其它的语言中的都强大,这产生了非常多令人吃惊的使用方法.

unity5, animator state machine, 无条件transition实现播放动画序列

今天遇到这样一个需求,我有一个名为happy的animation clip和一个名为speak的animation clip.想实现当主角胜利后播放动序列: happy->speak->happy->speak->... 这样无限循环. 走了一些弯路后发现直接在animator state machine里就可以搞定,如下图: transition{idle->happy}上condition设为triggerWin,由于idle到happy的transition不能太拖沓,

MITK中State machine和configuration详解和调用顺序

(学习笔记,错误难免,请指正:私人劳动,转载请注明出处) 下面以MITK自带的mitkPointSetDataInteractor.h.mitkPointSetDataInteractor.cpp.PointSet.xml.PointSetConfig.xml为例,解释MITK中的交互原理,以及State machine和configuration的调用顺序. PointSet.xml: <statemachine> <state name="start" start

Mina State machine状态机讲解

原文地址:Mina State machine(Apache Mina User guide Chapter14 State machine) 如果您使用的是Mina与复杂网络开发应用程序交互,你可能在某些时候发现自己达到的美好状态模式尝试解决一些复杂性.然而,在你这样做之前你可能想检查mina-statemachine试图解决一些状态模式的不足之处. 1.1.1. 一个简单的例子 让我们演示mina-statemachine如何以一个简单的例子工作.下图显示了一个典型的录音机一个状态机.椭圆形

(三)Unity5.0新特性------State Machine Behaviours

出处:http://blog.csdn.net/u010019717 author:孙广东      时间:2015.3.31 (State machine behaviours)状态机的行为在Animator Controller的脚本中是可以附加到动画状态或子状态机.每当你进入一种状态,就可以对其添加各种各样的状态依赖例如播放声音等行为,他们甚至可以独立于动画animation.而用于(logic state machines )逻辑状态机.要添加(State machine behavio