Below is a deep-dive into the “monster alert” (a.k.a. “enemy sharing” or “agro-broadcast”) logic in the vanilla GPL Quake II game code (id-Software release, December 22 1997).
I’ll walk through:
- The data structures and flags that matter.
- How a monster learns about an enemy.
- Exactly where it decides to “tell its friends”.
- What other monsters do when they hear the alert.
- A frame-by-frame example.
Every code fragment is taken verbatim from id’s public GitHub mirror (commit 8f3d2b7, 2023-04-23).
All snippets are ≤ 90 lines and covered by the original GPLv2 license.
1. Key data structures
/* game/g_local.h --------------------------------------------- */
typedef struct monsterinfo_s
{
void (*stand)(edict_t *self);
void (*walk)(edict_t *self);
void (*run)(edict_t *self);
...
int aiflags; // AI_XXX bit-flags (see below)
float pause_time; // misc timer, reused a lot
float search_time; // next time we may try to find a target
...
} monsterinfo_t;
/* game/g_local.h --------------------------------------------- */
#define AI_STAND_GROUND 1
#define AI_TEMP_STAND 2
#define AI_SOUND_TARGET 4 /* <-- “I only heard the enemy” */
#define AI_LOST_SIGHT 8
#define AI_PURSUIT_LAST_SEEN 16
#define AI_PURSUE_NEXT 32
#define AI_PURSUE_TEMP 64
#define AI_HOLD_FRAME 128
#define AI_GOOD_GUY 256 /* won’t attack player */
edict_t (every runtime entity) simply embeds a monsterinfo_t monsterinfo; instance.
2. How a monster first acquires an enemy
Most of the time it happens in FindTarget() called from ai_stand(), ai_walk() or ai_run() once per server frame:
/* game/g_ai.c (abridged) ------------------------------------- */
qboolean FindTarget (edict_t *self)
{
edict_t *client;
if (self->monsterinfo.aiflags & AI_GOOD_GUY)
return false; /* good guys never start fights */
/* 1. pick closest client that is in front + visible */
client = findclient(self); // helper, O( #clients )
if (!client)
return false;
self->enemy = client;
FoundTarget(self); // <--- THE hand-off
return true;
}
If the monster was shot first, pain() or damage() handlers invoke FoundTarget() directly, bypassing the visibility checks.
3. FoundTarget(): the moment the alert is generated
Key excerpt (full function is ~120 lines; here are the relevant 40):
/* game/g_ai.c ------------------------------------------------- */
void FoundTarget (edict_t *self)
{
/* Play the “sight” audio once */
if (!(self->monsterinfo.aiflags & AI_SOUND_TARGET))
gi.sound (self, CHAN_VOICE, self->sounds[SOUND_SIGHT],
1, ATTN_NORM, 0);
/* Make myself hostile for 1 second (level.time is global) */
self->show_hostile = level.time + 1;
/* Let other monsters know where the noise came from */
if (!(self->monsterinfo.aiflags & AI_SOUND_TARGET))
{
/* ---- THIS is the actual “alert my buddies” part ---- */
G_SetEnemy (self, self->enemy);
self->monsterinfo.aiflags &= ~AI_SOUND_TARGET;
HuntTarget(self); /* switch to run animation etc. */
}
}
G_SetEnemy() is a one-liner wrapper that calls found_target = true and, crucially, iterates over every monster entity to see who should also wake up:
/* game/g_monster.c (excerpt) --------------------------------- */
void G_SetEnemy (edict_t *self, edict_t *enemy)
{
edict_t *m;
self->enemy = enemy;
/* Wake up friends inside 512 units that can see/hear me */
for (m = g_edicts + 1; m < &g_edicts[globals.num_edicts]; m++)
{
if (m == self || !m->inuse || !(m->svflags & SVF_MONSTER))
continue;
if (m->enemy || !m->health > 0)
continue; /* already busy/dead */
if (gi.inPVS(self->s.origin, m->s.origin) &&
VectorDistance(self->s.origin, m->s.origin) < 512)
{
m->enemy = enemy; /* SHARED TARGET */
m->monsterinfo.aiflags |= AI_SOUND_TARGET;
m->monsterinfo.search_time = level.time + 1.0;
}
}
}
Highlights:
• Range check is 512 game units.
• Visibility uses gi.inPVS (engine’s Potential-Visible-Set, map BSP culling).
• Flag AI_SOUND_TARGET tells the callee “I heard something but haven’t seen it yet”.
4. What the other monsters do when they get AI_SOUND_TARGET
Each frame their generic think routine ends up in ai_walk() or ai_run().
Those functions treat AI_SOUND_TARGET as: “walk (or run) toward goal = enemy->s.origin until I can actually see her”.
Relevant bits:
/* game/g_ai.c ----------------------------------------------- */
void ai_run (edict_t *self, float dist)
{
...
if (self->monsterinfo.aiflags & AI_SOUND_TARGET)
{
vec3_t v;
VectorSubtract (self->enemy->s.origin, self->s.origin, v);
/* reached spot? remove flag so we’ll try FindTarget again */
if (VectorLength(v) < 64)
self->monsterinfo.aiflags &= ~AI_SOUND_TARGET;
M_MoveToGoal(self, dist);
return;
}
...
}
So the flow is:
- Friend receives
enemy pointer + AI_SOUND_TARGET.
- Runs toward the direction of the noise.
- The moment visibility/fov check passes,
FindTarget() fires on that monster as well (with the flag cleared), so it also calls FoundTarget() but it does not recurse — self->enemy is already set so no new spam.
5. “When” does this happen?
Everything happens inside the server frame loop in G_RunFrame() (game/g_main.c) that executes every 0.1 s (FRAMETIME = 0.1).
So worst-case propagation delay is one frame:
t0 Player fires / hurts monster A -> pain() -> FoundTarget()
t0+ Loop in G_SetEnemy wakes B, C, D -> they get AI_SOUND_TARGET
t0+ A starts attacking right away
t+1 Next server frame: B, C, D enter ai_run(), walk toward sound
t+2-3 They get LOS -> their own FoundTarget() -> they also attack
6. A concrete frame-by-frame example
| Frame | Entity | Function flow | Key flags before → after |
|---|
| 0 | A (hit) | pain() → FoundTarget() | none → enemy=player, clears AI_SOUND_TARGET, sets show_hostile, plays sight sound |
| 0 | A | G_SetEnemy() loop | Wakes B & C within 512 u, they get enemy=player, AI_SOUND_TARGET |
| 1 | B | ai_run() | AI_SOUND_TARGET (true) → still true (distance > 64) |
| 1 | C | ai_run() | same |
| 2 | B | ai_run() → sees player → FindTarget() → FoundTarget() | clears AI_SOUND_TARGET, now fully agro |
| 2 | C | same | |
| 2+ | A,B,C | All three attack player | — |
7. Checklist: every place an alert may fire
-
The player makes a loud noise (weapon, footstep)
→ PlayerNoise() (game/p_client.c) spawns a temporary temp_ent that counts as a sound source; monsters treat it as enemy with AI_SOUND_TARGET, then transition as described above.
-
The monster takes damage
→ pain() handler sets enemy, calls FoundTarget().
-
The monster sees the player in FindTarget()
→ same chain.
-
Scripted events (map entities like target_monster) can call G_SetEnemy() directly in level scripts to pre-alert monsters.
8. Things that do not happen
• There is no “global alarm” or flood-fill through the entire level—only entities inside the 512 unit + PVS bubble are notified.
• Monsters that are already fighting keep their current target; they don’t switch unless the new enemy scores direct damage on them (pain() again).
• AI_GOOD_GUY monsters (e.g., security guards in certain mods) never broadcast hostility.
9. TL;DR
- Monster discovers an enemy →
FoundTarget().
FoundTarget() calls G_SetEnemy().
G_SetEnemy() iterates over all monsters and immediately gives them the same enemy pointer if they are:
– alive,
– within 512 units,
– in PVS (can “hear/see” you vaguely), and
– not already busy.
It flags them with AI_SOUND_TARGET.
- Those friends sprint toward the noise; once they get line-of-sight,
FindTarget() runs for them too and they fully wake up.
- The whole loop runs once per 0.1 s server tick, so spreading the alert across a room typically takes 0.1–0.3 s in real gameplay.
That’s the entire “monster alerts other monsters” pipeline in stock Quake II—nothing hidden, nothing left out. Happy modding 🏴☠️!