How alisakkaf/clash-of-clans-bot-auto-farmer works
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: bot_engine.py Bot engine · v2_orchestrator.py Smart V2 orchestrator · main_window.py Main window · screen_reader.py Screen recognition
Components 22
Desktop control
- Application startup · Initializes the PyQt5 UI and bot runtime.
main.py - Main window · Configuration, controls, logs, and interactive assistance.
ui/main_window.py - Smart V2 controls
ui/smart_v2_panel.py
Runtime orchestration
- Bot engine · Owns recurring automation ticks outside the UI thread.
core/bot_engine.py - Game state machine · Guards progression across game screens and phases.
core/state_machine.py - Settings and profile · User choices and performance tuning.
core/settings.py - Default profile
profiles/default_profile.json - Shared logging
core/logger.py
Vision pipeline
- Screen recognition · Interprets frames with multi-scale template recognition.
vision/screen_reader.py - OCR reader · Extracts loot and UI values.
vision/ocr_reader.py - Smart V2 vision · Finds targets, red zones, and safe deployment regions.
vision/smart_vision_v2.py - Template manager · Loads and caches template assets from the manifest.
vision/template_manager.py - Template manifest
assets/templates/manifest.json
Game and tactics
- Home Village controller · Runs farming and home-village operations.
logic/home_village.py - Builder Base controller · Runs the multi-stage Builder Base attack sequence.
logic/builder_base.py - Smart V2 orchestrator · Selects attack rules and returns tactical steps.
logic/v2_orchestrator.py - Smart V2 planners · Coordinates deployment, funnel, hero, spell, and corridor planning.
logic/smart_v2_logic.py - Attack rules · Includes resource raid, ground funnel, and fallback tactics.
logic/rules/base_rule.py - Tactical policy profiles · Attack rules, troop profiles, and spell profiles.
config/v2_attack_rules.json
Android boundary
- ADB handler · Captures frames, discovers display metadata, and sends device commands.
core/adb_handler.py - ADB gestures · Supports taps, swipes, recorded gestures, and macros.
core/adb_gestures.py - Bundled ADB · Sole live-system boundary to the Android target.
2adb.exe
Connections 28
- 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