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 finite state machine agent


Now that we've created a finite state machine, we can simply replace the decision tree that powers our indirect soldier agent with a finite state machine. The initialization, updating, and termination of our FSM are identical to the decision tree.

With an abstract data structure, we can now create both decision-tree-based agents and finite state machine agents simultaneously:

IndirectSoldierAgent.lua:

local soldier;
local soldierController;
local soldierFSM;
local soldierUserData;

function Agent_Initialize(agent)

    ...
    
    soldierFSM = SoldierLogic_FiniteStateMachine(
        soldierUserData);
end

function Agent_Update(agent, deltaTimeInMillis)
    if (soldierUserData.alive) then
        soldierFSM:Update(deltaTimeInMillis);
    end

    soldierController:Update(agent, deltaTimeInMillis);
end