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

Handling new agent sightings


As visibility can be updated very often, we only want to send out events for new agent sightings. We can create a helper function that will take a list of known agents indexed by their agent ID numbers and a list of currently visible agents. By storing the lastSeen time of a sighting, we can determine whether an event should be sent out.

Throttling events in this fashion prevents an abundance of redundant events from being propagated. As events are sent to every registered object, this can have an impact on performance when massive amounts of events are being sent:

AgentSenses.lua:

local function HandleNewAgentSightings(
    userData, knownAgents, spottedAgents)
    
    local newAgentSightings = {};
    
    for key, value in pairs(spottedAgents) do
        local agentId = value.agent:GetId();
        local lastSeen =
            value.lastSeen - knownAgents[agentId].lastSeen;
    
        if (knownAgents[agentId] == nil or lastSeen > 500) then
           ...