Monster AI

Monster AI mainly resides in the g_ai.c file. The mframe_t sequences of the monsters refers to functions in this file. These functions act upon the monsterinfo_t's aiflags, which tells the state of the monster's current "plan".

Each server frame the monster tries to find a valid target. To avoid spending too much time finding targets, the monster only checks one client each server frame. This is no problem in single player game, but in cooperative game the monster will notice players a bit slower. The procedure of finding just one of the clients is supported by a few attributes in the global level structure. It contains: a sight_client pointer, sight_entity, sight_entity_framenum, sound_entity; sound_entity_framenum, sound2_entity, and a sound2_entity_framenum. Every server frame, AI_SetSightClient is set to the next living client. This means that all monsters will try to target this client in this server frame. The next server frame it will be another client. The function FindTarget is used to find the client designated for sighting. FindTarget also tries to find the client by "listening" for sound. That is when sound_entity becomes useful. FindTarget is called from various AI-functions like: ai_stand, ai_walk, and ai_run.