# alisakkaf/clash-of-clans-bot-auto-farmer architecture

> Architecture diagram of the GitHub repository alisakkaf/clash-of-clans-bot-auto-farmer, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/alisakkaf/clash-of-clans-bot-auto-farmer
- Repository: https://github.com/alisakkaf/clash-of-clans-bot-auto-farmer

## Overview

#### Purpose and Entry Points
Windows/Python 3.10+ desktop automation bot for Clash of Clans. It drives an Android phone, tablet, or emulator through screenshots and ADB touch commands; it does not integrate with game internals. Application startup is `main.py`, which initializes the PyQt5 UI and bot runtime. The bundled device bridge is `2adb.exe`.

#### UI and Runtime Control
PyQt5 presents configuration, mode controls, logs, interactive assistance, and Smart V2 controls. UI actions start/stop and configure the engine rather than directly operating the device.
- `ui/main_window.py`
- `ui/smart_v2_panel.py`
- `core/bot_engine.py`

The engine is a QThread-style scheduler that owns the recurring bot tick. It coordinates state polling, vision, gameplay logic, and ADB actions, keeping long-running automation separate from the UI thread.

#### Device Automation Boundary
ADB is the sole boundary to the Android target: it discovers device display characteristics, captures screenshots, issues taps/swipes, and supports recorded gesture/macro operations. This makes device/emulator differences an adapter concern rather than a strategy concern.
- `core/adb_handler.py`
- `core/adb_gestures.py`
- `2adb.exe`

Control flow is: engine requests current frame/device metadata → vision and state logic interpret it → engine sends resulting gestures back through ADB.

#### State and Operational Modes
A finite-state layer tracks game phases and protects progression between searching, attacking, training, recovery, and related screens. Higher-level village controllers implement mode-specific operational flows, including home-village farming and Builder Base’s multi-stage attack sequence.
- `core/state_machine.py`
- `logic/home_village.py`
- `logic/builder_base.py`

This separates screen/game lifecycle handling from tactical deployment decisions. Profile/settings state provides performance tuning and persisted user choices.
- `core/settings.py`
- `profiles/default_profile.json`

#### Vision and Perception Pipeline
Frames are processed through multi-scale template recognition, OCR, and adaptive battlefield geometry detection. Template assets are centrally loaded/cached from a manifest; OCR extracts loot/UI values; Smart V2 vision identifies playable geometry such as red-zone constraints and targets.
- `vision/screen_reader.py`
- `vision/ocr_reader.py`
- `vision/smart_vision_v2.py`

Template-backed recognition is data-dependent on:
- `assets/templates/manifest.json`
- `vision/template_manager.py`

Vision outputs form the main contract into gameplay logic: recognized UI/state, resources, troop indicators, targets, and safe deployment regions.

#### Tactical Decision System
The Smart V2/CSR layer selects an attack rule based on detected army, target, and configuration, then coordinates planners for deployment order, funneling, heroes, spells, and safer corridors. It returns actionable tactical steps to the engine rather than touching ADB itself.
- `logic/v2_orchestrator.py`
- `logic/smart_v2_logic.py`
- `logic/rules/base_rule.py`

Rule implementations cover resource raids, ground funnels, air attacks, Town Hall snipes, and fallback smart behavior:
- `logic/rules/resource_raid_rule.py`
- `logic/rules/ground_funnel_rule.py`
- `logic/rules/smart_default_rule.py`

Planner inputs and policy thresholds are externally configurable and hot-reloadable:
- `config/v2_attack_rules.json`
- `config/v2_troop_profiles.json`
- `config/v2_spell_profiles.json`

#### Material Dependencies and Boundaries
The runtime combines PyQt5 (desktop UI/threading), OpenCV-based computer vision, EasyOCR, Python subprocess execution, and a local ADB executable. The only external live system is the connected Android device/emulator via ADB; no game-process, packet, or cloud-service integration is indicated. Logging is shared between engine and UI-facing diagnostics.
- `core/logger.py`
- `requirements.txt`

Main entry points: [`core/bot_engine.py`](https://github.com/alisakkaf/clash-of-clans-bot-auto-farmer/blob/main/core/bot_engine.py) (Bot engine), [`logic/v2_orchestrator.py`](https://github.com/alisakkaf/clash-of-clans-bot-auto-farmer/blob/main/logic/v2_orchestrator.py) (Smart V2 orchestrator), [`ui/main_window.py`](https://github.com/alisakkaf/clash-of-clans-bot-auto-farmer/blob/main/ui/main_window.py) (Main window), [`vision/screen_reader.py`](https://github.com/alisakkaf/clash-of-clans-bot-auto-farmer/blob/main/vision/screen_reader.py) (Screen recognition)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_desktop["Desktop control"]
  node_entry["Application startup<br/>Python entry point<br/>[main.py]"]
  node_ui["Main window<br/>PyQt5 UI<br/>[main_window.py]"]
  node_smart_panel["Smart V2 controls<br/>PyQt5 panel<br/>[smart_v2_panel.py]"]
end

subgraph group_runtime["Runtime orchestration"]
  node_engine["Bot engine<br/>QThread scheduler<br/>[bot_engine.py]"]
  node_state_machine["Game state machine<br/>finite-state layer<br/>[state_machine.py]"]
  node_settings["Settings and profile<br/>persisted settings<br/>[settings.py]"]
  node_default_profile["Default profile<br/>profile data"]
  node_logger["Shared logging<br/>diagnostics<br/>[logger.py]"]
end

subgraph group_perception["Vision pipeline"]
  node_screen_reader["Screen recognition<br/>OpenCV perception<br/>[screen_reader.py]"]
  node_ocr["OCR reader<br/>EasyOCR perception<br/>[ocr_reader.py]"]
  node_smart_vision["Smart V2 vision<br/>battlefield geometry<br/>[smart_vision_v2.py]"]
  node_templates["Template manager<br/>asset cache"]
  node_manifest["Template manifest<br/>recognition assets<br/>[manifest.json]"]
end

subgraph group_strategy["Game and tactics"]
  node_home_village["Home Village controller<br/>mode flow<br/>[home_village.py]"]
  node_builder_base["Builder Base controller<br/>mode flow<br/>[builder_base.py]"]
  node_v2_orchestrator["Smart V2 orchestrator<br/>tactical coordinator<br/>[v2_orchestrator.py]"]
  node_smart_logic["Smart V2 planners<br/>tactical planning<br/>[smart_v2_logic.py]"]
  node_attack_rules["Attack rules<br/>rule framework<br/>[base_rule.py]"]
  node_tactical_config["Tactical policy profiles<br/>hot-reload config"]
end

subgraph group_device["Android boundary"]
  node_adb_handler["ADB handler<br/>device adapter<br/>[adb_handler.py]"]
  node_gestures["ADB gestures<br/>gesture adapter<br/>[adb_gestures.py]"]
  node_adb_exe{{"Bundled ADB<br/>local executable<br/>[2adb.exe]"}}
end

node_entry -->|"starts"| node_ui
node_entry -->|"initializes"| node_engine
node_ui -->|"configures and controls"| node_engine
node_smart_panel -->|"configures Smart V2"| node_engine
node_ui -->|"hosts"| node_smart_panel
node_engine -->|"polls phase"| node_state_machine
node_engine -->|"requests frames and acts"| node_adb_handler
node_engine -->|"submits frames"| node_screen_reader
node_engine -->|"runs mode flow"| node_home_village
node_engine -->|"runs mode flow"| node_builder_base
node_state_machine -->|"routes phase"| node_home_village
node_state_machine -->|"routes phase"| node_builder_base
node_screen_reader -->|"uses templates"| node_templates
node_templates -->|"loads"| node_manifest
node_screen_reader -->|"extracts UI values"| node_ocr
node_screen_reader -->|"provides battlefield frame"| node_smart_vision
node_home_village -->|"requests attack plan"| node_v2_orchestrator
node_builder_base -->|"requests attack plan"| node_v2_orchestrator
node_v2_orchestrator -->|"coordinates planners"| node_smart_logic
node_v2_orchestrator -->|"selects rule"| node_attack_rules
node_tactical_config -.->|"hot-reloads policy"| node_v2_orchestrator
node_smart_vision -->|"provides targets and regions"| node_v2_orchestrator
node_settings -.->|"supplies runtime choices"| node_engine
node_default_profile -.->|"provides defaults"| node_settings
node_engine -->|"writes diagnostics"| node_logger
node_ui -->|"displays diagnostics"| node_logger
node_adb_handler -->|"issues gestures"| node_gestures
node_adb_handler -->|"executes"| node_adb_exe

click node_entry "https://github.com/alisakkaf/clash-of-clans-bot-auto-farmer/blob/main/main.py"
click node_ui "https://github.com/alisakkaf/clash-of-clans-bot-auto-farmer/blob/main/ui/main_window.py"
click node_smart_panel "https://github.com/alisakkaf/clash-of-clans-bot-auto-farmer/blob/main/ui/smart_v2_panel.py"
click node_engine "https://github.com/alisakkaf/clash-of-clans-bot-auto-farmer/blob/main/core/bot_engine.py"
click node_state_machine "https://github.com/alisakkaf/clash-of-clans-bot-auto-farmer/blob/main/core/state_machine.py"
click node_settings "https://github.com/alisakkaf/clash-of-clans-bot-auto-farmer/blob/main/core/settings.py"
click node_default_profile "https://github.com/alisakkaf/clash-of-clans-bot-auto-farmer/blob/main/profiles/default_profile.json"
click node_logger "https://github.com/alisakkaf/clash-of-clans-bot-auto-farmer/blob/main/core/logger.py"
click node_screen_reader "https://github.com/alisakkaf/clash-of-clans-bot-auto-farmer/blob/main/vision/screen_reader.py"
click node_ocr "https://github.com/alisakkaf/clash-of-clans-bot-auto-farmer/blob/main/vision/ocr_reader.py"
click node_smart_vision "https://github.com/alisakkaf/clash-of-clans-bot-auto-farmer/blob/main/vision/smart_vision_v2.py"
click node_templates "https://github.com/alisakkaf/clash-of-clans-bot-auto-farmer/blob/main/vision/template_manager.py"
click node_manifest "https://github.com/alisakkaf/clash-of-clans-bot-auto-farmer/blob/main/assets/templates/manifest.json"
click node_home_village "https://github.com/alisakkaf/clash-of-clans-bot-auto-farmer/blob/main/logic/home_village.py"
click node_builder_base "https://github.com/alisakkaf/clash-of-clans-bot-auto-farmer/blob/main/logic/builder_base.py"
click node_v2_orchestrator "https://github.com/alisakkaf/clash-of-clans-bot-auto-farmer/blob/main/logic/v2_orchestrator.py"
click node_smart_logic "https://github.com/alisakkaf/clash-of-clans-bot-auto-farmer/blob/main/logic/smart_v2_logic.py"
click node_attack_rules "https://github.com/alisakkaf/clash-of-clans-bot-auto-farmer/blob/main/logic/rules/base_rule.py"
click node_tactical_config "https://github.com/alisakkaf/clash-of-clans-bot-auto-farmer/blob/main/config/v2_attack_rules.json"
click node_adb_handler "https://github.com/alisakkaf/clash-of-clans-bot-auto-farmer/blob/main/core/adb_handler.py"
click node_gestures "https://github.com/alisakkaf/clash-of-clans-bot-auto-farmer/blob/main/core/adb_gestures.py"
click node_adb_exe "https://github.com/alisakkaf/clash-of-clans-bot-auto-farmer/blob/main/2adb.exe"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_entry,node_ui,node_smart_panel toneBlue
class node_engine,node_state_machine,node_settings,node_default_profile,node_logger toneAmber
class node_screen_reader,node_ocr,node_smart_vision,node_templates,node_manifest toneMint
class node_home_village,node_builder_base,node_v2_orchestrator,node_smart_logic,node_attack_rules,node_tactical_config toneRose
class node_adb_handler,node_gestures,node_adb_exe toneIndigo
```

## Components

### Desktop control

- [Application startup](https://github.com/alisakkaf/clash-of-clans-bot-auto-farmer/blob/main/main.py): Python entry point · Initializes the PyQt5 UI and bot runtime. · `main.py`
- [Main window](https://github.com/alisakkaf/clash-of-clans-bot-auto-farmer/blob/main/ui/main_window.py): PyQt5 UI · Configuration, controls, logs, and interactive assistance. · `ui/main_window.py`
- [Smart V2 controls](https://github.com/alisakkaf/clash-of-clans-bot-auto-farmer/blob/main/ui/smart_v2_panel.py): PyQt5 panel · `ui/smart_v2_panel.py`

### Runtime orchestration

- [Bot engine](https://github.com/alisakkaf/clash-of-clans-bot-auto-farmer/blob/main/core/bot_engine.py): QThread scheduler · Owns recurring automation ticks outside the UI thread. · `core/bot_engine.py`
- [Game state machine](https://github.com/alisakkaf/clash-of-clans-bot-auto-farmer/blob/main/core/state_machine.py): finite-state layer · Guards progression across game screens and phases. · `core/state_machine.py`
- [Settings and profile](https://github.com/alisakkaf/clash-of-clans-bot-auto-farmer/blob/main/core/settings.py): persisted settings · User choices and performance tuning. · `core/settings.py`
- [Default profile](https://github.com/alisakkaf/clash-of-clans-bot-auto-farmer/blob/main/profiles/default_profile.json): profile data · `profiles/default_profile.json`
- [Shared logging](https://github.com/alisakkaf/clash-of-clans-bot-auto-farmer/blob/main/core/logger.py): diagnostics · `core/logger.py`

### Vision pipeline

- [Screen recognition](https://github.com/alisakkaf/clash-of-clans-bot-auto-farmer/blob/main/vision/screen_reader.py): OpenCV perception · Interprets frames with multi-scale template recognition. · `vision/screen_reader.py`
- [OCR reader](https://github.com/alisakkaf/clash-of-clans-bot-auto-farmer/blob/main/vision/ocr_reader.py): EasyOCR perception · Extracts loot and UI values. · `vision/ocr_reader.py`
- [Smart V2 vision](https://github.com/alisakkaf/clash-of-clans-bot-auto-farmer/blob/main/vision/smart_vision_v2.py): battlefield geometry · Finds targets, red zones, and safe deployment regions. · `vision/smart_vision_v2.py`
- [Template manager](https://github.com/alisakkaf/clash-of-clans-bot-auto-farmer/blob/main/vision/template_manager.py): asset cache · Loads and caches template assets from the manifest. · `vision/template_manager.py`
- [Template manifest](https://github.com/alisakkaf/clash-of-clans-bot-auto-farmer/blob/main/assets/templates/manifest.json): recognition assets · `assets/templates/manifest.json`

### Game and tactics

- [Home Village controller](https://github.com/alisakkaf/clash-of-clans-bot-auto-farmer/blob/main/logic/home_village.py): mode flow · Runs farming and home-village operations. · `logic/home_village.py`
- [Builder Base controller](https://github.com/alisakkaf/clash-of-clans-bot-auto-farmer/blob/main/logic/builder_base.py): mode flow · Runs the multi-stage Builder Base attack sequence. · `logic/builder_base.py`
- [Smart V2 orchestrator](https://github.com/alisakkaf/clash-of-clans-bot-auto-farmer/blob/main/logic/v2_orchestrator.py): tactical coordinator · Selects attack rules and returns tactical steps. · `logic/v2_orchestrator.py`
- [Smart V2 planners](https://github.com/alisakkaf/clash-of-clans-bot-auto-farmer/blob/main/logic/smart_v2_logic.py): tactical planning · Coordinates deployment, funnel, hero, spell, and corridor planning. · `logic/smart_v2_logic.py`
- [Attack rules](https://github.com/alisakkaf/clash-of-clans-bot-auto-farmer/blob/main/logic/rules/base_rule.py): rule framework · Includes resource raid, ground funnel, and fallback tactics. · `logic/rules/base_rule.py`
- [Tactical policy profiles](https://github.com/alisakkaf/clash-of-clans-bot-auto-farmer/blob/main/config/v2_attack_rules.json): hot-reload config · Attack rules, troop profiles, and spell profiles. · `config/v2_attack_rules.json`

### Android boundary

- [ADB handler](https://github.com/alisakkaf/clash-of-clans-bot-auto-farmer/blob/main/core/adb_handler.py): device adapter · Captures frames, discovers display metadata, and sends device commands. · `core/adb_handler.py`
- [ADB gestures](https://github.com/alisakkaf/clash-of-clans-bot-auto-farmer/blob/main/core/adb_gestures.py): gesture adapter · Supports taps, swipes, recorded gestures, and macros. · `core/adb_gestures.py`
- [Bundled ADB](https://github.com/alisakkaf/clash-of-clans-bot-auto-farmer/blob/main/2adb.exe): local executable · Sole live-system boundary to the Android target. · `2adb.exe`

## Connections

- Application startup → Main window: starts
- Application startup → Bot engine: initializes
- Main window → Bot engine: configures and controls
- Smart V2 controls → Bot engine: configures Smart V2
- Main window → Smart V2 controls: hosts
- Bot engine → Game state machine: polls phase
- Bot engine → ADB handler: requests frames and acts
- Bot engine → Screen recognition: submits frames
- Bot engine → Home Village controller: runs mode flow
- Bot engine → Builder Base controller: runs mode flow
- Game state machine → Home Village controller: routes phase
- Game state machine → Builder Base controller: routes phase
- Screen recognition → Template manager: uses templates
- Template manager → Template manifest: loads
- Screen recognition → OCR reader: extracts UI values
- Screen recognition → Smart V2 vision: provides battlefield frame
- Home Village controller → Smart V2 orchestrator: requests attack plan
- Builder Base controller → Smart V2 orchestrator: requests attack plan
- Smart V2 orchestrator → Smart V2 planners: coordinates planners
- Smart V2 orchestrator → Attack rules: selects rule
- Tactical policy profiles → Smart V2 orchestrator: hot-reloads policy
- Smart V2 vision → Smart V2 orchestrator: provides targets and regions
- Settings and profile → Bot engine: supplies runtime choices
- Default profile → Settings and profile: provides defaults
- Bot engine → Shared logging: writes diagnostics
- Main window → Shared logging: displays diagnostics
- ADB handler → ADB gestures: issues gestures
- ADB handler → Bundled ADB: executes
