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

Updating decision evaluators


With a centralized data structure in place, we can refactor the decision evaluators in order to reference the blackboard directly. Removing the enemy evaluation from the SoldierEvaluators_CanShootAgent function solves a critical flaw in our previous implementation, which is the fact that no conditional evaluation should modify the agent. Logic evaluators should only make true or false calculations in a passive manner and never mutate the state of an agent:

SoldierEvaluators.lua:

function SoldierEvaluators_CanShootAgent(userData)
    local enemy = userData.blackboard:Get("enemy");
    
    if (enemy ~= nil and
        Agent.GetHealth(enemy) > 0 and
        Vector.Distance(
            userData.agent:GetPosition(),
            enemy:GetPosition()) < 3) then
        
        return true;
    end;
    return false;
end

function SoldierEvaluators_HasAmmo(userData)
    local ammo = userData.blackboard:Get("ammo");
    
    return ammo ~= nil and ammo > 0;...