Write me in full detail, omit nothing, with code snippets from github, how exactly, when and what exactly happens when a monster alerts other monsters in quake2
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Alright, buckle up. This is a deep dive into the Quake II monster alerting system. We'll be dissecting the code, understanding the mechanics, and uncovering the details of how, when, and why monsters call for backup in id Software's classic FPS. Be prepared for a lot of C code and potentially confusing variable names!
Core Concepts and Data Structures
Before we dive into the functions, let's establish the fundamental concepts and data structures involved. Quake II's monster AI relies heavily on these:
edict_t (Entity Structure): This is the central structure. Everything in Quake II is an entity, including monsters, players, weapons, projectiles, etc. It contains a ton of information about the entity, including its position, velocity, model, health, AI state, and pointers to other entities it's interacting with. Key edict_t members for our topic include:
takedamage: A flag indicating if the entity can take damage (important for determining if a monster is vulnerable).
health: The entity's current health.
enemy: A pointer to the edict_t of the entity the monster is currently fighting.
monsterinfo: A structure containing monster-specific AI data.
monsterinfo.aiflags: Bit flags that control various AI aspects (e.g., if the monster has seen the enemy).
monsterinfo.trail_time: Time at which the monster started trailing the enemy. Used in search behavior.
monsterinfo.idle_time: Last time the monster did an idle animation.
flags: General flags for entity behavior.
svflags: Server-side flags controlling various aspects.
monsterinfo_t (Monster Information Structure): This structure is nested within edict_t and holds information specific to monster AI. It keeps track of the monster's state, behavior, and relationships with other entities.
AI_MeleeAttack(), AI_RangedAttack(), AI_Chase(), etc.: These are high-level AI functions called by the monsters' think functions to actually make the monster do something.
The Chain of Events: How an Alert Works
The monster alerting system in Quake II is interwoven throughout the monster's AI logic. Here's a step-by-step breakdown of the events:
Initial Trigger (Seeing or Hearing Something):
Line of Sight: A monster might visually detect a player (or another monster attacking a player). This is checked primarily by visible() calls. visible() performs line-of-sight tracing.
Hearing a Sound: Monsters can also react to sounds. This is handled by the sound() function, which is called whenever a sound is played in the game. If the sound is loud enough and within the monster's hearing range, the monster might investigate.
c
// From `g_combat.c` (simplified example):
void sound (edict_t *self, int mask, char *sound_name) {
// ... (code to find nearby monsters) ...
for (i=0 ; i<num_listeners ; i++) {
ent = listeners[i];
if (!ent->inuse)
continue;
// Check if the monster is in the PVS (potentially visible set) of the sound source.
if (!gi.inPVS (self->s.origin, ent->s.origin))
continue;
// ... (Check if the monster is close enough and hasn't already been alerted) ...
// The monster is alerted!
FoundTarget (ent, self);
}
}
FoundTarget() is the function that actually makes the monster react.
The FoundTarget() Function (Alerting the Monster): This function is critical. It's called when a monster "finds" a target (either by seeing or hearing something).
c
// From `m_move.c` (heavily simplified):
void FoundTarget (edict_t *self, edict_t *attacker) {
//If the monster has already seen its enemy, do not reset trailtime
if (!(self->monsterinfo.aiflags & AI_TARGET_VISIBLE))
{
self->monsterinfo.trail_time = level.time;
}
self->enemy = attacker;
self->monsterinfo.aiflags |= AI_TARGET_VISIBLE;
if (self->pain_debounce_time < level.time)
{
gi.sound (self, CHAN_VOICE, self->noise_sight, 1, ATTN_NORM, 0);
}
}
Sets the monster's enemy pointer to the attacker.
Sets the AI_TARGET_VISIBLE flag (to indicate that the monster has seen its target). This is important because it prevents the monster from repeatedly alerting other monsters every frame.
Plays the monster's "sight" sound (its alert sound).
The trail_time is updated so the monster remembers when he first found the target.
The AlertMonsters() Function (Spreading the Word): After a monster has found a target, it will attempt to alert nearby monsters. This happens within the monster's think function (the function that runs every frame to update the monster's behavior). The call to AlertMonsters is usually wrapped in a check to make sure it's only called under certain conditions (e.g., the monster is not currently in combat or is not already alerting others).
c
// From `m_move.c` (heavily simplified):
void AlertMonsters (edict_t *self) {
int i;
edict_t *ent;
for (i = 1, ent = &g_edicts[i]; i < globals.num_edicts; i++, ent++) {
if (!ent->inuse)
continue;
if (ent == self)
continue;
if (!ent->takedamage)
continue;
if (ent->svflags & SVF_MONSTER) {
if (!ent->enemy) { // The monster doesn't have an enemy yet
if (ent->health > 0) {
if (gi.inPVS(self->s.origin, ent->s.origin)) {
FoundTarget (ent, self->enemy);
}
}
}
}
}
}
The code iterates through all entities in the game (g_edicts).
It checks if the entity is a valid, living monster that doesn't already have an enemy (ent->enemy == NULL).
Crucially, it uses gi.inPVS() to check if the alerting monster and the potential alert recipient are in the same Potentially Visible Set (PVS). The PVS is a pre-calculated set of regions that can potentially see each other. This is a significant optimization because it avoids performing expensive line-of-sight checks on monsters that are in different parts of the level.
If the monster is in the PVS, FoundTarget() is called, effectively alerting the monster.
Where Does AlertMonsters() Get Called? This will vary per monster type, but it's generally inside the monster's think function. Let's look at an example from the soldier monster:
c
// from m_soldier.c
void soldier_idle (edict_t *self)
{
if (random() > 0.8)
return;
//added to check whether to alert
if ((self->monsterinfo.aiflags & AI_TARGET_VISIBLE) && (level.time > self->monsterinfo.idle_time + 10))
{
AlertMonsters(self);
}
self->monsterinfo.currentmove = &soldier_move_stand;
}
void soldier_run (edict_t *self)
{
if (random() > 0.8)
return;
//added to check whether to alert
if ((self->monsterinfo.aiflags & AI_TARGET_VISIBLE) && (level.time > self->monsterinfo.idle_time + 10))
{
AlertMonsters(self);
}
if (self->monsterinfo.aiflags & AI_TARGET_VISIBLE)
{
self->monsterinfo.currentmove = &soldier_move_run;
}
else
{
self->monsterinfo.currentmove = &soldier_move_run;
}
}
void soldier_think (edict_t *self)
{
//other AI routines go here
if (self->movetype == MOVETYPE_NONE)
{
self->nextthink = level.time + 1;
return;
}
self->nextthink = level.time + FRAMETIME;
if (self->monsterinfo.currentmove)
{
if ((self->monsterinfo.currentmove->think) )
(self->monsterinfo.currentmove->think) (self);
}
else
{
gi.dprintf ("%s doesn't have a currentmove\n", self->classname);
}
}
The Soldier's soldier_idle and soldier_run functions check for the AI_TARGET_VISIBLE flag and will alert nearby monsters that don't already have a target if it's been more than 10 seconds since the monster has been idle. This is to limit spamming the function.
Key Considerations and Optimizations:
gi.inPVS() (Potentially Visible Set): As mentioned above, this is a huge performance optimization. Instead of performing ray traces (line-of-sight checks) between every pair of monsters, it only considers monsters that are in the same PVS. The PVS is precomputed for each map.
AI_TARGET_VISIBLE Flag: This flag in monsterinfo.aiflags is used to prevent a monster from repeatedly alerting other monsters every frame. Without it, the AlertMonsters() function would be called repeatedly as long as the monster had line of sight to the player, leading to a flood of alerts.
self->enemy Check: The AlertMonsters() function specifically checks if a monster already has an enemy (ent->enemy). This is crucial to prevent monsters from "stealing" targets from each other unnecessarily.
Hearing Attenuation: The sound() function takes into account the distance and attenuation of sounds. Louder sounds and sounds closer to a monster are more likely to trigger an alert.
Monster Type Specificity: Different monster types may have different alert behaviors. Some might be more aggressive in alerting others, while others might be more cautious.
Code Location Summary
g_combat.c: Contains visible() and sound().
m_move.c: Contains FoundTarget() and AlertMonsters().
m_<monstername>.c (e.g., m_soldier.c, m_infantry.c): Contains the monster's specific AI logic, including its think function, movement functions (e.g., soldier_run, soldier_idle), and the call to AlertMonsters().
In Summary:
The monster alerting system in Quake II is a carefully designed system that balances responsiveness with performance. It relies on:
Visual detection (line of sight) using visible().
Auditory detection using sound().
The FoundTarget() function to establish the monster's enemy.
The AlertMonsters() function to broadcast the alert to nearby monsters.
The AI_TARGET_VISIBLE flag and the gi.inPVS() function for optimization.
The exact behavior varies slightly from monster to monster. It's a core component of the game's AI, and understanding it is key to appreciating the sophistication of Quake II's design. Good luck!