# liyunfan1223/mod-playerbots architecture

> Architecture diagram of the GitHub repository liyunfan1223/mod-playerbots, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/liyunfan1223/mod-playerbots
- Repository: https://github.com/liyunfan1223/mod-playerbots

## Overview

##### Project Type and Purpose
- This is a C++ server-side module (“mod-playerbots”) for the AzerothCore MMORPG engine (World of Warcraft emulation).
- It plugs into the WorldServer, loading at runtime as a module, reads SQL schema/data to extend the database, registers AI bots, and implements a multi-tiered AI framework (triggers, strategies, actions, values).

##### Top-Level Components
a. AzerothCore WorldServer (host application)
b. Module Loader (AzerothCore plugin system)
c. mod-playerbots Module:
- Configuration subsystem (`conf/*.dist`, environment variables)
- SQL Schema/Data loader (`data/sql/...`)
- C++ AI core (src/*.cpp, src/strategy, src/factory, src/playerbots_loader.cpp)
- Database persistence (`PlayerbotDbStore`)
- Command server (`PlayerbotCommandServer`)
d. External dependencies:
- AzerothCore API (server events, world simulation, network I/O)
- DBC files (game data)
- MySQL/MariaDB

##### Relationships & Data Flows
1. Startup
- WorldServer initializes → plugin loader discovers mod-playerbots →
- `playerbots_loader.cpp` reads config/environment, executes SQL schema (data/sql/create) → registers DB tables/functions.
2. Runtime Loop
- WorldServer generates game events (player joined, NPC update tick, combat event) → mod-playerbots intercepts via `ServerFacade` and manager hooks (PlayerbotMgr, TravelMgr, FleeManager etc.).
- For each bot, `AiFactory`/`RandomPlayerbotFactory` instantiates a `PlayerbotAI` object.
- On each update tick:
- `TriggerContext` gathers world state → selects active triggers (src/strategy/triggers/*) →
- `StrategyContext` chooses appropriate strategy modules (class-specific, dungeon, raid strategies, etc.) →
- `ActionContext` picks concrete actions (movement, combat, chat) and computes `Value` objects as parameters →
- WorldServer API calls execute actions (move, cast spell, send chat).
- Any persistent state (quest progress, custom strategy) is read/written via `PlayerbotDbStore`.
- Chat commands received via `PlayerbotCommandServer` → parsed → invoke manager methods or AI updates.

##### Architectural Patterns
- Plugin architecture: module loaded into host server
- Factory pattern: `AiFactory`, `RandomPlayerbotFactory` for bot creation
- Strategy pattern: dynamic selection of `Strategy` classes (generic, class-specific, dungeon, raid)
- Command pattern: `Action` objects that encapsulate behavior
- Observer/Trigger pattern: `Trigger` objects that watch world events
- Data persistence layer: `PlayerbotDbStore` abstracts SQL operations

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Host Application
    subgraph "AzerothCore WorldServer"
        direction TB
        CoreEngine["Core Engine"]:::core
        ModuleLoader["Module Loader"]:::module
    end

    %% Plugin Module
    ModuleLoader --> Mod["mod-playerbots Module"]:::moduleBox

    subgraph "mod-playerbots Module"
        direction TB
        ConfigLoader["Configuration Subsystem"]:::module
        SQLLoader["SQL Schema & Data Loader"]:::module
        BotMgr1["PlayerbotMgr"]:::module
        BotMgr2["RandomPlayerbotMgr"]:::module

        subgraph "AI Core"
            direction TB
            AiFactoryNode["AiFactory"]:::module
            RandomFactory["RandomPlayerbotFactory"]:::module
            Triggers["Trigger Subsystem"]:::module
            Strategies["Strategy Subsystem"]:::module
            Actions["Action Subsystem"]:::module
            Values["Value Subsystem"]:::module
        end

        Persistence["PlayerbotDbStore"]:::module
        CommandServer["PlayerbotCommandServer"]:::module
        ServerFacade["ServerFacade"]:::module
    end

    %% External Systems
    Database["MySQL/MariaDB"]:::db
    Env["Environment Variables Script"]:::external
    DBC["DBC Files"]:::external

    %% Startup Flow
    Env -->|reads| ConfigLoader
    ConfigLoader -->|loads SQL| SQLLoader
    SQLLoader -->|executes schema/data| Database

    %% Runtime Event Loop
    CoreEngine -->|world events| Triggers
    Triggers -->|selects triggers| Strategies
    Strategies -->|chooses strategies| Actions
    Actions -->|executes actions| ServerFacade
    ServerFacade -->|calls API| CoreEngine

    %% Chat Commands
    CoreEngine -->|player chat| CommandServer
    CommandServer -->|invokes| BotMgr1

    %% Persistence
    AiFactoryNode <-->|reads/writes| Persistence
    Persistence <-->|stores data| Database

    %% External Data
    DBC --> Triggers
    DBC --> Strategies

    %% Click Events
    click ModuleLoader "https://github.com/liyunfan1223/mod-playerbots/blob/master/src/playerbots_loader.cpp"
    click ConfigLoader "https://github.com/liyunfan1223/mod-playerbots/blob/master/conf/playerbots.conf.dist"
    click Env "https://github.com/liyunfan1223/mod-playerbots/blob/master/conf/conf.sh.dist"
    click SQLLoader "https://github.com/liyunfan1223/mod-playerbots/blob/master/data/sql/playerbots/create/create_mysql.sql"
    click SQLLoader "https://github.com/liyunfan1223/mod-playerbots/blob/master/data/sql/playerbots/create/drop_mysql.sql"
    click SQLLoader "https://github.com/liyunfan1223/mod-playerbots/blob/master/data/sql/playerbots/base/*.sql"
    click AiFactoryNode "https://github.com/liyunfan1223/mod-playerbots/blob/master/src/AiFactory.cpp"
    click RandomFactory "https://github.com/liyunfan1223/mod-playerbots/blob/master/src/RandomPlayerbotFactory.cpp"
    click Triggers "https://github.com/liyunfan1223/mod-playerbots/blob/master/src/strategy/triggers/TriggerContext.h"
    click Strategies "https://github.com/liyunfan1223/mod-playerbots/blob/master/src/strategy/Strategy.cpp"
    click Actions "https://github.com/liyunfan1223/mod-playerbots/blob/master/src/strategy/actions/ActionContext.h"
    click Values "https://github.com/liyunfan1223/mod-playerbots/blob/master/src/strategy/values/ValueContext.h"
    click Persistence "https://github.com/liyunfan1223/mod-playerbots/blob/master/src/PlayerbotDbStore.cpp"
    click CommandServer "https://github.com/liyunfan1223/mod-playerbots/blob/master/src/PlayerbotCommandServer.cpp"
    click BotMgr1 "https://github.com/liyunfan1223/mod-playerbots/blob/master/src/PlayerbotMgr.cpp"
    click BotMgr2 "https://github.com/liyunfan1223/mod-playerbots/blob/master/src/RandomPlayerbotMgr.cpp"
    click ServerFacade "https://github.com/liyunfan1223/mod-playerbots/blob/master/src/ServerFacade.cpp"

    %% Styles
    classDef core fill:#dddddd,stroke:#333,stroke-width:1px
    classDef module fill:#cce5ff,stroke:#2366bd,stroke-width:1px
    classDef moduleBox fill:#b3d7ff,stroke:#1a5fa8,stroke-width:2px
    classDef db fill:#d4edda,stroke:#2e7d32,stroke-width:1px
    classDef external fill:#fff3cd,stroke:#856404,stroke-width:1px
```
