# lincao-3d/grass architecture

> Architecture diagram of the GitHub repository lincao-3d/grass, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/lincao-3d/grass
- Repository: https://github.com/lincao-3d/grass

## Overview

#### Purpose and Runtime

GRASS is a local Python 3.10+ desktop text RPG. It uses Pygame for a custom graphical interface and an OpenAI-compatible LLM service to generate narrative, react to player input, and support scenario rewriting. The executable entry point is `src/main.py`; application-level orchestration is centered in `src/engine/Game.py`.

#### Application Shell and Scene Control

The game engine owns the Pygame loop, current screen lifecycle, input/update/render dispatch, and transitions between gameplay modes. Scene construction is separated from scene behavior through `src/engine/scene/SceneFactory.py`.

Primary modes include menu/configuration, save selection, character creation, AI chat, combat, and scenario assistance:
- `src/engine/scene/MainMenu.py`
- `src/engine/scene/ChatScene.py`
- `src/engine/scene/CombatScene.py`
- `src/engine/scene/ScenarioAssistant.py`

`src/engine/scene/Scene.py` and `src/engine/scene/SceneElement.py` provide the common scene/UI boundary.

#### AI Narrative and Agent Boundary

LLM communication is isolated under `src/engine/ai/chat.py`, with tool definitions or callable game-facing capabilities in `src/engine/ai/tools.py`. This is the main external-service boundary: the runtime requires an API key (documented as `debug_api_key`) and an OpenAI-compatible API.

The chat scene presents streamed or generated narrative to the player while AI tools can connect model output to game operations. Scenario assistance uses the same AI layer to produce a new setting and a prompt intended to guide refactoring of game mechanics.

#### RPG Domain Model

The engine should consume domain objects and rules from `src/model/`, rather than embed RPG mechanics in scenes. Core state includes player/entity identity, character choices, items, effects, skills, and scenario definition:
- `src/model/player.py`
- `src/model/entity.py`
- `src/model/scenario.py`

Character creation maps user selections into race, class, and attribute models:
- `src/model/race.py`
- `src/model/classes.py`
- `src/model/attribs.py`

Inventory and item interactions are represented by `src/model/Item.py`; monster definitions are in `src/model/monster.py`.

#### Combat Stage

Combat is a distinct interaction mode combining domain rules with combat-specific UI. `src/model/combat.py` contains combat mechanics/state, while `src/engine/scene/CombatScene.py` controls the player-facing flow. Effects and character capabilities influence this layer through:
- `src/model/effects.py`
- `src/model/skills.py`

Combat outcomes can feed back into player/world narrative through the AI chat flow.

#### Presentation System and Assets

A reusable Pygame UI component layer supports scenes rather than relying on stock widgets. Important primitives include:
- `src/engine/ui/UIElement.py`
- `src/engine/ui/TextArea.py`
- `src/engine/ui/CharacterSheetPanel.py`

Visual atmosphere includes frame-based ASCII background playback in `src/engine/ui/ASCIIBgPlayer.py`, consuming `assets/ASCIItxt/`. Interaction/audio and animation elements include `src/engine/ui/DiceRollAnimation.py` and sound assets under `assets/sfx/`.

#### Configuration and Packaging

Global constants and runtime options are held in `src/constants.py` and `src/options.json`. Dependencies are declared in `requirements.txt`; `build_windows.sh` is the Windows packaging path. The intended deployment is a local desktop application, not a web service.

Main entry points: [`src/engine/scene/SceneFactory.py`](https://github.com/lincao-3d/grass/blob/main/src/engine/scene/SceneFactory.py) (Scene factory), [`src/engine/scene/ChatScene.py`](https://github.com/lincao-3d/grass/blob/main/src/engine/scene/ChatScene.py) (AI chat), [`src/engine/scene/CombatScene.py`](https://github.com/lincao-3d/grass/blob/main/src/engine/scene/CombatScene.py) (Combat UI), [`src/engine/Game.py`](https://github.com/lincao-3d/grass/blob/main/src/engine/Game.py) (Game engine)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Desktop Runtime"]
  node_main["Executable entry<br/>Python entry point<br/>[main.py]"]
  node_game["Game engine<br/>Pygame orchestrator<br/>[Game.py]"]
  node_scene_factory["Scene factory<br/>scene construction<br/>[SceneFactory.py]"]
end

subgraph group_scenes["Scenes"]
  node_scene_base["Scene contract<br/>scene/UI boundary<br/>[Scene.py]"]
  node_main_menu["Menu and configuration<br/>scene<br/>[MainMenu.py]"]
  node_chat_scene["AI chat<br/>narrative scene<br/>[ChatScene.py]"]
  node_combat_scene["Combat UI<br/>combat scene<br/>[CombatScene.py]"]
  node_scenario_assistant["Scenario assistant<br/>scenario scene"]
end

subgraph group_ai["AI Integration"]
  node_chat_client{{"LLM client<br/>OpenAI-compatible client<br/>[chat.py]"}}
  node_ai_tools["Game AI tools<br/>tool definitions<br/>[tools.py]"]
end

subgraph group_domain["RPG Domain"]
  node_player_entity["Player and entities<br/>core state<br/>[player.py]"]
  node_scenario["Scenario model<br/>world definition<br/>[scenario.py]"]
  node_character_build["Character choices<br/>race/class/attributes<br/>[race.py]"]
  node_inventory["Items and inventory<br/>item model<br/>[Item.py]"]
  node_combat_rules["Combat mechanics<br/>combat state and rules<br/>[combat.py]"]
end

subgraph group_presentation["Presentation"]
  node_ui_components["Reusable UI components<br/>Pygame UI layer<br/>[UIElement.py]"]
  node_ascii_background["ASCII background player<br/>atmosphere renderer<br/>[ASCIIBgPlayer.py]"]
  node_dice_animation["Dice and interaction effects<br/>Pygame animation"]
end

node_main -->|"starts"| node_game
node_game -->|"constructs scenes"| node_scene_factory
node_game -->|"runs lifecycle"| node_scene_base
node_scene_factory -->|"creates"| node_main_menu
node_scene_factory -->|"creates"| node_chat_scene
node_scene_factory -->|"creates"| node_combat_scene
node_scene_factory -->|"creates"| node_scenario_assistant
node_main_menu -->|"collects choices"| node_character_build
node_chat_scene -->|"requests narrative"| node_chat_client
node_scenario_assistant -->|"generates setting"| node_chat_client
node_chat_client -->|"uses tools"| node_ai_tools
node_ai_tools -.->|"operates on"| node_player_entity
node_chat_scene -->|"uses game state"| node_player_entity
node_chat_scene -->|"uses world context"| node_scenario
node_combat_scene -->|"drives combat"| node_combat_rules
node_combat_rules -->|"resolves participants"| node_player_entity
node_combat_rules -->|"applies items"| node_inventory
node_combat_scene -.->|"returns outcomes"| node_chat_scene
node_scene_base -->|"renders with"| node_ui_components
node_scene_base -.->|"uses atmosphere"| node_ascii_background
node_combat_scene -->|"shows rolls"| node_dice_animation

click node_main "https://github.com/lincao-3d/grass/blob/main/src/main.py"
click node_game "https://github.com/lincao-3d/grass/blob/main/src/engine/Game.py"
click node_scene_factory "https://github.com/lincao-3d/grass/blob/main/src/engine/scene/SceneFactory.py"
click node_scene_base "https://github.com/lincao-3d/grass/blob/main/src/engine/scene/Scene.py"
click node_main_menu "https://github.com/lincao-3d/grass/blob/main/src/engine/scene/MainMenu.py"
click node_chat_scene "https://github.com/lincao-3d/grass/blob/main/src/engine/scene/ChatScene.py"
click node_combat_scene "https://github.com/lincao-3d/grass/blob/main/src/engine/scene/CombatScene.py"
click node_scenario_assistant "https://github.com/lincao-3d/grass/blob/main/src/engine/scene/ScenarioAssistant.py"
click node_chat_client "https://github.com/lincao-3d/grass/blob/main/src/engine/ai/chat.py"
click node_ai_tools "https://github.com/lincao-3d/grass/blob/main/src/engine/ai/tools.py"
click node_player_entity "https://github.com/lincao-3d/grass/blob/main/src/model/player.py"
click node_scenario "https://github.com/lincao-3d/grass/blob/main/src/model/scenario.py"
click node_character_build "https://github.com/lincao-3d/grass/blob/main/src/model/race.py"
click node_inventory "https://github.com/lincao-3d/grass/blob/main/src/model/Item.py"
click node_combat_rules "https://github.com/lincao-3d/grass/blob/main/src/model/combat.py"
click node_ui_components "https://github.com/lincao-3d/grass/blob/main/src/engine/ui/UIElement.py"
click node_ascii_background "https://github.com/lincao-3d/grass/blob/main/src/engine/ui/ASCIIBgPlayer.py"
click node_dice_animation "https://github.com/lincao-3d/grass/blob/main/src/engine/ui/DiceRollAnimation.py"

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_main,node_game,node_scene_factory toneBlue
class node_scene_base,node_main_menu,node_chat_scene,node_combat_scene,node_scenario_assistant toneAmber
class node_chat_client,node_ai_tools toneMint
class node_player_entity,node_scenario,node_character_build,node_inventory,node_combat_rules toneRose
class node_ui_components,node_ascii_background,node_dice_animation toneIndigo
```

## Components

### Desktop Runtime

- [Executable entry](https://github.com/lincao-3d/grass/blob/main/src/main.py): Python entry point · `src/main.py`
- [Game engine](https://github.com/lincao-3d/grass/blob/main/src/engine/Game.py): Pygame orchestrator · Owns the loop, scene lifecycle, and dispatch. · `src/engine/Game.py`
- [Scene factory](https://github.com/lincao-3d/grass/blob/main/src/engine/scene/SceneFactory.py): scene construction · `src/engine/scene/SceneFactory.py`

### Scenes

- [Scene contract](https://github.com/lincao-3d/grass/blob/main/src/engine/scene/Scene.py): scene/UI boundary · `src/engine/scene/Scene.py`
- [Menu and configuration](https://github.com/lincao-3d/grass/blob/main/src/engine/scene/MainMenu.py): scene · `src/engine/scene/MainMenu.py`
- [AI chat](https://github.com/lincao-3d/grass/blob/main/src/engine/scene/ChatScene.py): narrative scene · Presents generated or streamed narrative. · `src/engine/scene/ChatScene.py`
- [Combat UI](https://github.com/lincao-3d/grass/blob/main/src/engine/scene/CombatScene.py): combat scene · `src/engine/scene/CombatScene.py`
- [Scenario assistant](https://github.com/lincao-3d/grass/blob/main/src/engine/scene/ScenarioAssistant.py): scenario scene · Generates settings and mechanic-refactoring guidance. · `src/engine/scene/ScenarioAssistant.py`

### AI Integration

- [LLM client](https://github.com/lincao-3d/grass/blob/main/src/engine/ai/chat.py): OpenAI-compatible client · External API boundary requiring an API key. · `src/engine/ai/chat.py`
- [Game AI tools](https://github.com/lincao-3d/grass/blob/main/src/engine/ai/tools.py): tool definitions · Exposes game-facing capabilities to the model. · `src/engine/ai/tools.py`

### RPG Domain

- [Player and entities](https://github.com/lincao-3d/grass/blob/main/src/model/player.py): core state · `src/model/player.py`
- [Scenario model](https://github.com/lincao-3d/grass/blob/main/src/model/scenario.py): world definition · `src/model/scenario.py`
- [Character choices](https://github.com/lincao-3d/grass/blob/main/src/model/race.py): race/class/attributes · `src/model/race.py`
- [Items and inventory](https://github.com/lincao-3d/grass/blob/main/src/model/Item.py): item model · `src/model/Item.py`
- [Combat mechanics](https://github.com/lincao-3d/grass/blob/main/src/model/combat.py): combat state and rules · Applies effects and skills to combat resolution. · `src/model/combat.py`

### Presentation

- [Reusable UI components](https://github.com/lincao-3d/grass/blob/main/src/engine/ui/UIElement.py): Pygame UI layer · Shared scene controls and character-sheet presentation. · `src/engine/ui/UIElement.py`
- [ASCII background player](https://github.com/lincao-3d/grass/blob/main/src/engine/ui/ASCIIBgPlayer.py): atmosphere renderer · Plays frame-based backgrounds from ASCII assets. · `src/engine/ui/ASCIIBgPlayer.py`
- [Dice and interaction effects](https://github.com/lincao-3d/grass/blob/main/src/engine/ui/DiceRollAnimation.py): Pygame animation · `src/engine/ui/DiceRollAnimation.py`

## Connections

- Executable entry → Game engine: starts
- Game engine → Scene factory: constructs scenes
- Game engine → Scene contract: runs lifecycle
- Scene factory → Menu and configuration: creates
- Scene factory → AI chat: creates
- Scene factory → Combat UI: creates
- Scene factory → Scenario assistant: creates
- Menu and configuration → Character choices: collects choices
- AI chat → LLM client: requests narrative
- Scenario assistant → LLM client: generates setting
- LLM client → Game AI tools: uses tools
- Game AI tools → Player and entities: operates on
- AI chat → Player and entities: uses game state
- AI chat → Scenario model: uses world context
- Combat UI → Combat mechanics: drives combat
- Combat mechanics → Player and entities: resolves participants
- Combat mechanics → Items and inventory: applies items
- Combat UI → AI chat: returns outcomes
- Scene contract → Reusable UI components: renders with
- Scene contract → ASCII background player: uses atmosphere
- Combat UI → Dice and interaction effects: shows rolls
