behavior tree

==
AI




Finite State Machine (FSM), Hierarchical Finite State Machine (HFSM), Decision Tree, Behavior Tree

======
Finite State Machine (FSM)

//Enter a character from The current state moves to the next state
// a 6*6 table.

//The vertical is the current state
//The horizontal is the next state
//The value in the table is the condition

//new 6 state states, and then assign the value to the dic dictionary inside
//The key of the dictionary is the input character, and the value is the corresponding output state
//

const int range = CHAR_MAX + 1;

class State
{
public:
    State();
    State* transition[range];
};

struct TransGraph // use triple to describe map
{
    int current_state;
    char input_char;
    int next_state;
} ;

// fill in all keys and values ​​before use

    static TransGraph graph[] =
    {
        {1, 'A', 2}, {1, 'B', 3}, {1, 'C', 4}, {1, 'D', 5},
        {2, 'E', 2}, {2, 'I', 0},
        {3, 'F', 3}, {3, 'J', 0}, {3, 'M', 4},
        {4, 'G', 4}, {4, 'K', 0},
        {5, 'H', 5}, {5, 'L', 0}, {5, 'O', 2}, {5, 'N', 4},
        {0, 0, 0}
    };

    for (TransGraph* p_tg = graph; p_tg->current_state != 0; ++p_tg)
        state[p_tg->current_state].transition[p_tg->input_char ] = &state[p_tg->next_state];

//Enter a value to get the next value

void Fsm::Advance(char c)
{
    if (p_current ! = NULL)
        p_current = p_current->transition[c];
}




//Simple Finite State Machine – When Unity3d (C#)
initializes, bind all methods according to the method name of the class
and then switch the state.

If you need to switch according to the input, you need to define another method to switch the state
only according to the input









====
points Layer Finite State Machine (HFSM)

//Because sometimes the source state is the same. Enter the same. But the output is different

1. Each state is a class, they inherit from a public class, which contains virtual methods for entering, exiting, and handling events;
2. The state machine has a state stack, which is implemented here using List;
3. When the state machine is initialized, there is an initial state, usually the idle state, which becomes the first element of the stack;
4. The state transition is divided into three cases: a enters the target state, b exits the current state, and c switches to the target state (ie Exit the current state first, and then enter the target state);
5. The current valid state is the state at the top of the stack in the state stack, namely: _smList[0];


//Another explanation In
simple terms, it is when there are too many states in the FSM When the state is not easy to maintain, the state is classified and extracted, and the
state as a state machine, and then a large state machine is made to maintain these sub-state machines.





====
Behavior tree

It has only 4 types of Node:
* Composite Node combination node
* Decorator Node decoration node
* Condition Node condition node (leaf node)
* Action Node Action Node (leaf node)

After any Node is executed, it must report the execution result to its Parent Node: success/failure.
This simple success/failure reporting principle is cleverly used to control the decision direction of the entire tree.

———————————————————————

First look at the composite node Composite Node, in fact, it can be subdivided into 4 types according to the composite nature:

1 Selector Node Select node / priority Select node
When executing this type of Node, it will execute its own Child Node iteratively from begin to end:
if it encounters a Child Node and returns True after execution, then stop the iteration, and
this Node also returns True to its own Parent Node; otherwise, all Child Node returns False,
that Node returns False to its Parent Node.

2 Sequence Node
When executing this type of Node, it iteratively executes its own Child Node from begin to end:
if it encounters a Child Node and returns False after execution, then stop the iteration, and
this Node also returns False to its own Parent Node ; otherwise, all Child Nodes return True, and
that Node returns True to its own Parent Node.

3 Parallel Node Parallel Node Executes all its Child Nodes
concurrently .
The value returned to the Parent Node is related to the specific strategy adopted by the Parallel Node:
Parallel Selector Node: A False returns False, and all True returns True.
Parallel Sequence Node: One True returns True, all False returns False.
Parallel Hybird Node: The result is determined only after the specified number of Child Nodes return True or False.
There is no need to prejudge which Child Node should be placed before and which should be placed after, as Selector/Sequence does.
Common situations are:
(1) It is used for multiple Action subtrees in parallel.
(2) Hang a subtree under Parallel Node and hang multiple Condition Nodes
to provide real-time and performance.

4 Decorator decorates the nodes
Until Success and Until Failure
to execute the child nodes in a loop until "success" or "failure" is returned.
Limit
executes the child nodes for a certain number of times and then forces the return of "failure"
Time
child nodes will not be executed immediately, but will be executed at the specified time Execute after arrival.
TimeLimit
specifies the longest running time of the child node. If the child node is still running after the specified time, it will be forced to return "failure".
Invert
takes "not" for the return result of the child node, that is, the child node returns "success" and the node returns "Failure", if the child node returns "Failure", the node returns success


Leaf node
1 Condition node (Condition)
2 Action node (Action)


takes tick as a variable
Each time the update function is called
, the same tick is passed from top to bottom

All nodes inherit basenode
basenode contains a sublist, unique id

https://github .com/chenjd/TTBT-Framework Jianyu


state
actionstate
is divided into ABCDEF
, each corresponding actionstate has different cans, which are
divided into 1 2 3 4 5 6 and cur

is basically 1 corresponding to canA

Set the previous cur before each conversion For false
, the current cur is set to true

1 2 3 4 5 6 is achieved by 2x processing, and then superimposed. Can get whether to transition to a certain state

Anti- lock dead processing. Add time deadline



====
https://github.com/Tencent/behaviac
==
unity Behavior Designer

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326013256&siteId=291194637