Book Image

Learning Game AI Programming with Lua

By : David Young
Book Image

Learning Game AI Programming with Lua

By: David Young

Overview of this book

Table of Contents (16 chapters)
Learning Game AI Programming with Lua
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Free Chapter
1
Getting Started with AI Sandbox
Index

Behavior trees


With decision trees focusing on the if…else style of action selection and state machines focusing on the statefulness of actions, behavior trees fill a nice middle ground with reaction-based decision making.

The behavior tree node

Behavior trees are composed solely of different types of nodes. Based on the node type, the behavior tree will interpret child nodes as actions and evaluators. As we'll need to distinguish each node instance type from one another, we can create an enumeration of all supported node types: actions, conditions, selectors, and sequences.

Creating an instance of a behavior tree node merely sets the node type and the name of the node:

BehaviorTreeNode.lua:

BehaviorTreeNode = {};

BehaviorTreeNode.Type = {
    ACTION = "ACTION",
    CONDITION = "CONDITION",
    SELECTOR = "SELECTOR",
    SEQUENCE = "SEQUENCE"
};

function BehaviorTreeNode.new(name, type)
    local node = {};
    
    -- The BehaviorTreeNode's data members.
    node.action_ = nil;
    node.children_...