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.

CTFBot (game/server/tf/bot/tf_bot.h) is TF2’s bot player class. It inherits from NextBotPlayer<CTFPlayer>, combining the full player entity with the NextBot component system. CTFBot drives all AI-controlled players: Mann vs. Machine robots, training mode bots, and tournament practice bots.

CTFBot class structure

// game/server/tf/bot/tf_bot.h
class CTFBot : public NextBotPlayer< CTFPlayer >, public CGameEventListener
{
public:
    virtual void Spawn();
    virtual void PhysicsSimulate( void );
    virtual void Touch( CBaseEntity *pOther );
    virtual bool IsAllowedToPickUpFlag( void ) const;
    virtual void InitClass( void );

    // Component accessors
    CTFBotLocomotion *GetLocomotionInterface() const;
    CTFBotVision     *GetVisionInterface() const;
    CTFBotBody       *GetBodyInterface() const;

    // Squad
    CTFBotSquad *GetSquad() const     { return m_squad; }
    bool IsInASquad() const           { return m_squad != NULL; }
};

NextBot components

CTFBot uses four specialized components declared in game/server/tf/bot/:
ComponentFileResponsibility
CTFBotLocomotiontf_bot_locomotion.hMovement, jumping, ladder climbing, stuck detection
CTFBotVisiontf_bot_vision.hEnemy detection, FOV filtering, threat prioritization
CTFBotBodytf_bot_body.hAim direction, look-at targets, crouch control
Intention / Behaviorbehavior/tf_bot_behavior.hBehavior tree root, dispatches to action classes

Behavior action tree

The bot’s decision-making is a stack of Action<CTFBot> objects. The top action runs each tick; it can return Continue(), SuspendFor(newAction), Done(), or ChangeTo(newAction). Actions are organized by scenario role:
CTFBotMainAction is the root. It delegates to scenario-specific actions:
  • CTFBotSeekAndDestroy — general offensive behavior
  • CTFBotAttack — engage a specific threat
  • CTFBotRetreatToCover — disengage and find cover
  • CTFBotGetHealth — navigate to a health pickup
  • CTFBotGetAmmo — navigate to an ammo pickup
Each TF2 class has a subfolder under behavior/ with override actions:
  • behavior/engineer/ — sentry placement, dispenser building, teleporter setup
  • behavior/medic/ — healing target selection, ÜberCharge management
  • behavior/spy/ — disguise selection, backstab approach, sapper placement
  • behavior/sniper/ — vantage point selection, scope timing
  • behavior/demoman/ — sticky trap placement, grenade arc targeting
  • CTFBotMvMDeployBomb — carrying and deploying the bomb at the hatch
  • CTFBotEscortFlag — following the bomb carrier in a squad
  • behavior/missions/ — tank escort, sentry buster, spy mission behaviors

Vision system

CTFBotVision (tf_bot_vision.h/cpp) extends IVision and filters detected entities by team, distance, and line-of-sight. It maintains a sorted threat list and exposes GetPrimaryKnownThreat() to behavior actions.
// game/server/tf/bot/tf_bot_vision.h
class CTFBotVision : public IVision
{
public:
    virtual bool IsIgnored( CBaseEntity *subject ) const OVERRIDE;
    virtual bool IsVisibleEntityNoticed( CBaseEntity *who ) const OVERRIDE;
    virtual float GetMaxVisionRange() const OVERRIDE;
    virtual float GetMinRecognizeTime() const OVERRIDE;
};

Difficulty attributes

CTFBot difficulty is controlled by a set of integer attributes (m_iSkill) that scale reaction time, aim spread, and target-switching speed. MvM popfiles can also override individual bot attributes such as MaxVisionRange and AimTrackingInterval per bot generator.
Bot ignore flags (e.g., TFBOT_IGNORE_ENEMY_SENTRY_GUNS) can be set per bot in popfiles to create specialized robots that focus only on certain objectives while ignoring specific threats.

Build docs developers (and LLMs) love