- See also: A_FindTracer
A_FindTarget is an action that searches through all Objects of a specified type, and sets the actor's target to the one nearest (or farthest from) it in the map. Var1 determines the type of Object to look for, and Var2 specifies whether to look for the closest or farthest one of that type (0 for closest, 1 for farthest). This action does not take Z positioning into account.
If the type of Object being searched for is a player, this action will ignore any spectators, players with the notarget
cheat enabled, and players with no health. This action will also ignore any enemies with no health.
Code – A_FindTarget
|
|
// Function: A_FindTarget
//
// Description: Finds the nearest/furthest mobj of the specified type and sets actor->target to it.
//
// var1 = mobj type
// var2 = if (0) nearest; else furthest;
//
void A_FindTarget(mobj_t *actor)
{
INT32 locvar1 = var1;
INT32 locvar2 = var2;
mobj_t *targetedmobj = NULL;
thinker_t *th;
mobj_t *mo2;
fixed_t dist1 = 0, dist2 = 0;
#ifdef HAVE_BLUA
if (LUA_CallAction("A_FindTarget", actor))
return;
#endif
CONS_Debug(DBG_GAMELOGIC, "A_FindTarget called from object type %d, var1: %d, var2: %d\n", actor->type, locvar1, locvar2);
// scan the thinkers
for (th = thlist[THINK_MOBJ].next; th != &thlist[THINK_MOBJ]; th = th->next)
{
if (th->function.acp1 == (actionf_p1)P_RemoveThinkerDelayed)
continue;
mo2 = (mobj_t *)th;
if (mo2->type == (mobjtype_t)locvar1)
{
if (mo2->player && (mo2->player->spectator || mo2->player->pflags & PF_INVIS))
continue; // Ignore spectators
if ((mo2->player || mo2->flags & MF_ENEMY) && mo2->health <= 0)
continue; // Ignore dead things
if (targetedmobj == NULL)
{
targetedmobj = mo2;
dist2 = R_PointToDist2(actor->x, actor->y, mo2->x, mo2->y);
}
else
{
dist1 = R_PointToDist2(actor->x, actor->y, mo2->x, mo2->y);
if ((!locvar2 && dist1 < dist2) || (locvar2 && dist1 > dist2))
{
targetedmobj = mo2;
dist2 = dist1;
}
}
}
}
if (!targetedmobj)
{
CONS_Debug(DBG_GAMELOGIC, "A_FindTarget: Unable to find the specified object to target.\n");
return; // Oops, nothing found..
}
CONS_Debug(DBG_GAMELOGIC, "A_FindTarget: Found a target.\n");
P_SetTarget(&actor->target, targetedmobj);
}
|
|