Skip to main content

Documentation 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.

The Source Engine AI system provides the base infrastructure that TF2 bots and any other NPCs build on. The core lives in 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
  • SensesCAI_Senses manages sight and hearing, populating a memory of enemies
  • NavigatorCAI_Navigator handles pathfinding through the nav mesh to a goal
  • MotorCAI_Motor translates path following into movement commands
  • SquadCAI_Squad groups NPCs for coordinated behavior
// game/server/ai_basenpc.h (simplified)
class CAI_BaseNPC : public CBaseCombatCharacter, public CAI_DefMovementSink
{
public:
    virtual int  SelectSchedule();
    virtual int  TranslateSchedule( int scheduleType );
    virtual void BuildScheduleTestBits();
    virtual bool IsJumpLegal( const Vector &startPos, const Vector &apex,
                              const Vector &endPos ) const;

    CAI_Navigator   *GetNavigator()  { return m_pNavigator; }
    CAI_Motor       *GetMotor()      { return m_pMotor; }
    CAI_Senses      *GetSenses()     { return m_pSenses; }
    CAI_Squad       *GetSquad()      { return m_pSquad; }
};

Schedule and task system

Schedules are named sequences of tasks defined with the BEGIN_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.
// Example schedule definition pattern
BEGIN_SCHEDULE( SCHED_CHASE_ENEMY )
    DEFINE_TASK( TASK_SET_FAIL_SCHEDULE, (float)SCHED_CHASE_ENEMY_FAILED )
    DEFINE_TASK( TASK_GET_PATH_TO_ENEMY, 0 )
    DEFINE_TASK( TASK_RUN_PATH, 0 )
    DEFINE_TASK( TASK_WAIT_FOR_MOVEMENT, 0 )
END_SCHEDULE()

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 priority
The 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.

Build docs developers (and LLMs) love