A_CheckAmbush is an action that calls a state if the actor is "ambushing" its target or tracer – i.e. the actor is located behind the target Object. For example, this action could be used for enemies that attack only when the player is not looking at it. Var1 determines whether the actor's target (0) or tracer (1) is used, and Var2 determines the state to call when this action is successful; see List of states for a full list. If the chosen Object does not exist, this action will do nothing.
The actor is considered to be "behind" the target Object by this action when it is positioned such that it forms an angle within 112.5° either side from the back of the target Object. This can also be interpreted to mean that if the actor is positioned anywhere within 67.5° of the target Object's front, the state will not be called. This does not require the target Object to actually "see" the actor; the state call will fail regardless of whether it can be seen by the target Object or not.
This action originates from the v2.0 modification SRB2Morphed and was added to SRB2 itself in v2.1.
Code – A_CheckAmbush
// Function: A_CheckAmbush
// Description: Calls a state if the actor is behind its targeted player.
// 0 = target
// 1 = tracer
// var2 = state number
void A_CheckAmbush(mobj_t *actor)
INT32 locvar1 = var1;
INT32 locvar2 = var2;
angle_t at; // angle target is currently facing
angle_t atp; // actor to target angle
angle_t an; // angle between at and atp
if (LUA_CallAction("A_CheckAmbush", actor))
if ((!locvar1 && !actor->target) || (locvar1 && !actor->tracer))
if (!locvar1) // target
at = actor->target->angle;
atp = R_PointToAngle2(actor->x, actor->y, actor->target->x, actor->target->y);
else // tracer
at = actor->tracer->angle;
atp = R_PointToAngle2(actor->x, actor->y, actor->tracer->x, actor->tracer->y);
an = atp - at;
if (an > ANGLE_180) // flip angle if bigger than 180
an = InvAngle(an);
if (an < ANGLE_90+ANGLE_22h) // within an angle of 112.5 from each other?