The Source Engine AI system provides the base infrastructure that TF2 bots and any other NPCs build on. The core lives inDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/sr2echa/TF2-Source-Code/llms.txt
Use this file to discover all available pages before exploring further.
game/server/ and centers on CAI_BaseNPC — a large class that implements scheduling, sensing, navigation, and squad logic. TF2’s bot system (CTFBot) uses the newer NextBot framework (game/server/NextBot/) layered on top of the standard player class rather than CAI_BaseNPC.
CAI_BaseNPC
CAI_BaseNPC (game/server/ai_basenpc.h) is the foundation for all Source Engine NPCs. It provides:
- Schedule/task system — discrete AI behaviors broken into named sequences of tasks
- Senses —
CAI_Sensesmanages sight and hearing, populating a memory of enemies - Navigator —
CAI_Navigatorhandles pathfinding through the nav mesh to a goal - Motor —
CAI_Motortranslates path following into movement commands - Squad —
CAI_Squadgroups NPCs for coordinated behavior
Schedule and task system
Schedules are named sequences of tasks defined with theBEGIN_SCHEDULE / DEFINE_TASK macros and stored in ai_schedule.cpp. Each task has a start and run phase; when a task completes, the NPC advances to the next one.
Senses and memory
CAI_Senses (game/server/ai_senses.h) manages enemy detection. It runs a periodic scan, checking line-of-sight and hearing radius, then stores detected entities in CAI_Memory. Memory entries decay over time, allowing NPCs to “forget” enemies they’ve lost sight of.
The NextBot framework
TF2 bots use the NextBot framework (game/server/NextBot/) rather than CAI_BaseNPC. NextBot provides a component-based architecture:
INextBotComponent
Base interface for all NextBot components (locomotion, body, vision, intention)
ILocomotion
Handles movement — walking, jumping, climbing ladders, falling
IVision
Manages threat detection, field-of-view filtering, and threat prioritization
IIntention / ActionFactory
Runs a behavior tree of
Action<> objects that push/pop based on priorityThe NextBot framework was introduced with Left 4 Dead and retrofitted into TF2 for MvM bot support. It is strictly server-side; client-side rendering of bot players uses the normal
C_TFPlayer path.