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

Creating a behavior tree object


Creating a behavior tree object is simple, as it primarily consists of a root node, evaluation function, and update function:

BehaviorTree.lua:

require "BehaviorTreeNode"

BehaviorTree = {};

local _EvaluateSelector;
local _EvaluateSequence;

function BehaviorTree.SetNode(self, node)
    tree.node_ = node;
end

function BehaviorTree.new()
    local tree = {};
    
    -- The BehaviorTree's data members.
    tree.currentNode_ = nil;
    tree.node_ = nil;
    
    return tree;
end

Behavior tree helper functions

Four primary evaluators are used to process the behavior tree structure: selector evaluation, sequence evaluation, actual node evaluation, and finally, a continue evaluation function that continues where a sequence's child finishes:

BehaviorTree.lua:

local EvaluateSelector;
local EvaluateSequence;

Selector evaluation

As selectors only return false if all child nodes have executed without returning true, we can iterate over all children and return the first...