Ai has been a part of the game development process that has been repeatedly modified. Planning short-term needs as the game content more and more rich, will inevitably adjust AI behavior, so I have long wanted to have a completely to the planning comrades edit the game AI Editor.
let's consider the actual logic AI of the game, such as the need for a character to find a wild monster that he can play and to avoid some big wild monsters that can't be beaten, or a character to achieve a certain level of behavior. The combination of these behavior and conditions must be dependent on the role itself has implemented some basic functions, such as walking to the specified location, play the specified action, and so on, the logic needs to be implemented in advance, here does not explain. Then leave the logic in the AI to invoke the appropriate user API. Is there a state machine or a behavior tree to achieve? I feel that the behavior tree is better. The state machine can be used when the requirements are fixed and the state is less, and once the state is added or deleted it will be a big fuss, and the behavior tree is very extensible. Behavior Tree People can find a lot of information, the general content will not say. Let's talk about some of the differences here.
I'm dividing the nodes into these 4 categories .
• Leaf Nodes: The node in the executable state, which is the primary behavior logic, needs to be implemented under this node. The other 3 types of nodes are control-type nodes
• Sequential Nodes: Executes its child nodes sequentially
• Select Nodes: Randomly select a node by calculating the weights of the child nodes
• Concurrent Nodes: Perform all of their own points the 4 types of nodes themselves have a detection of entry conditions.In order to better cooperate with the planning development, I use Xlua to invoke the user API in the game. This is the leaf node of the Xlua file, the control node of the Xlua file is similar to just no update () and trigger (). The script is called externally, and detect () is executed first to determine the entry condition of the node, and enter () is called when True is returned.
function Detect ()--The entry condition of the node
Function Enter ()--Call after entering
Function Update ()--Node in progress
(only leaf nodes)
function trigger ()--Response after external event triggering
(only leaf nodes)
function exit ()--Exit node
function detect () return trueendfunction enter () endfunction update (DT) Endfunction trigger (Type,obj) endfunction exit () End
If there is a need now:
there is a person to go to work, and then work till work, there will be two options, go home or girlfriend's home, go to the girlfriend's house must not empty hands, should first go to the gift shop to buy some gifts, have a gift to girlfriend home, and girlfriend dating. This is the end of the day, the beginning of another day of life.
Let 's take a look at this behavior tree:
with a list of this tree we can construct the following structure under the editor:
This tree structure is finally exported in JSON format for constructing nodes
The effect of this tree is the following:
here is the source download: Resources download (Baidu Cloud)
Game AI Customization--Behavior Tree +xlua+json