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

Building a finite state machine


To build a finite state machine for our agent, we'll create the initial states that wrap each possible action. As the state machine needs a starting point, we'll set the state explicitly to the idle state to begin with. Once the idle state has finished, our state machine will automatically pick the most relevant action to execute afterward:

SoldierLogic.lua:

function SoldierLogic_FiniteStateMachine(userData)
    local fsm = FiniteStateMachine.new(userData);
    fsm:AddState("die", DieAction(userData));
    fsm:AddState("flee", FleeAction(userData));
    fsm:AddState("idle", IdleAction(userData));
    fsm:AddState("move", MoveAction(userData));
    fsm:AddState("pursue", PursueAction(userData));
    fsm:AddState("randomMove", RandomMoveAction(userData));
    fsm:AddState("reload", ReloadAction(userData));
    
    fsm:SetState("idle");
    return fsm;
end

The idle state

Creating the idle state consists of adding every possible transition from the idle, which also...