Game Development Reference
In-Depth Information
Has enemy evaluator
Has enemy is the first evaluator that actually modifies the userData table passed in to
the evaluator. The HasEnemy function calculates the best enemy the agent should consider
to be its enemy. Iterating over all agents in the sandbox, the closest pathable enemy will be
selected. If no enemy that meets these requirements is found, the HasEnemy evaluator re-
turns false.
Note
As HasEnemy modifies and performs non-simple calculations, it should be used sparingly
within any logic structure.
Since our agents will need to know when they have an enemy as well as when there are no
valid enemies, we'll create a normal HasEnemy function evaluator and the inverse
HasNoEnemy function.
SoldierEvaluators.lua :
function SoldierEvaluators_HasEnemy(userData)
local sandbox = userData.agent:GetSandbox();
local position = Agent.GetPosition(userData.agent);
local agents =
Sandbox.GetAgents(userData.agent:GetSandbox());
local closestEnemy;
local distanceToEnemy;
for index=1, #agents do
local agent = agents[index];
if (Agent.GetId(agent) ~=
Agent.GetId(userData.agent) and
Agent.GetHealth(agent) > 0) then
-- Find the closest enemy.
local distanceToAgent = Vector.DistanceSquared(
position, Agent.GetPosition(agent));
if (closestEnemy == nil or
distanceToAgent < distanceToEnemy) then
Search WWH ::




Custom Search