How lincao-3d/grass works
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: SceneFactory.py Scene factory · ChatScene.py AI chat · CombatScene.py Combat UI · Game.py Game engine
Components 18
Desktop Runtime
- Executable entry
src/main.py - Game engine · Owns the loop, scene lifecycle, and dispatch.
src/engine/Game.py - Scene factory
src/engine/scene/SceneFactory.py
Scenes
- Scene contract
src/engine/scene/Scene.py - Menu and configuration
src/engine/scene/MainMenu.py - AI chat · Presents generated or streamed narrative.
src/engine/scene/ChatScene.py - Combat UI
src/engine/scene/CombatScene.py - Scenario assistant · Generates settings and mechanic-refactoring guidance.
src/engine/scene/ScenarioAssistant.py
AI Integration
- LLM client · External API boundary requiring an API key.
src/engine/ai/chat.py - Game AI tools · Exposes game-facing capabilities to the model.
src/engine/ai/tools.py
RPG Domain
- Player and entities
src/model/player.py - Scenario model
src/model/scenario.py - Character choices
src/model/race.py - Items and inventory
src/model/Item.py - Combat mechanics · Applies effects and skills to combat resolution.
src/model/combat.py
Presentation
- Reusable UI components · Shared scene controls and character-sheet presentation.
src/engine/ui/UIElement.py - ASCII background player · Plays frame-based backgrounds from ASCII assets.
src/engine/ui/ASCIIBgPlayer.py - Dice and interaction effects
src/engine/ui/DiceRollAnimation.py
Connections 21
- 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