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

Decaying blackboard events


When we update our agent senses, we can use the deltaTimeInMillis to decrement the time-to-live value of each stored event. If the time-to-live drops below 0, we'll remove the stored event:

AgentSenses.lua:

local function PruneEvents(events, deltaTimeInMillis)
    local validEvents = {};
    
    for index = 1, #events do
        local event = events[index];
        event.ttl = event.ttl - deltaTimeInMillis;
        
        if (event.ttl > 0) then
            table.insert(validEvents, event);
        end
    end
    
    return validEvents;
end

Providing a helper function to wrap pruning events is useful, as most auditory events will require being pruned during the normal agent's update loop:

AgentSenses.lua:

local function PruneBlackboardEvents(
    blackboard, attribute, deltaTimeInMillis)
    
    local attributeValue = blackboard:Get(attribute);
    
    if (attributeValue) then
        blackboard:Set(
            attribute,
            PruneEvents(attributeValue...