AI_Diplomacy/ai_animation
Tyler Marques bad4ad37e3
FIX: Messages were triggering at the same time
Was calling the onMessageComplete function twice, fixed that
2025-06-16 09:39:30 -07:00
..
public added background music 2025-06-05 08:02:33 +00:00
src FIX: Messages were triggering at the same time 2025-06-16 09:39:30 -07:00
tests/e2e WIP: Mostly working version that uses the eventqueue 2025-06-16 09:39:30 -07:00
.dockerignore Fixing some issues with streaming and games not being accessible 2025-06-04 14:16:44 -04:00
.env.example fixing sounds and timing 2025-06-05 07:07:36 +00:00
.gitignore Initial attempt at some tests that help us determine if the game has basic functionality 2025-06-03 10:14:34 -07:00
CLAUDE.md Initial attempt at some tests that help us determine if the game has basic functionality 2025-06-03 10:14:34 -07:00
Dockerfile optimizations and sound for stream! 2025-06-05 07:29:43 +00:00
experiment_log.md webhooks 2025-06-02 20:33:36 -04:00
index.html FIX: Map jump bug, leaderboard misname 2025-06-16 09:39:29 -07:00
package-lock.json WIP: Majority converted to event queue, continuing to work on others. 2025-06-16 09:39:30 -07:00
package.json Initial attempt at some tests that help us determine if the game has basic functionality 2025-06-03 10:14:34 -07:00
playwright.config.ts Initial attempt at some tests that help us determine if the game has basic functionality 2025-06-03 10:14:34 -07:00
README.md Adding a GameId to allow moving through a list of games. 2025-05-28 10:38:16 -07:00
reorganize_games.sh Fixing some issues with streaming and games not being accessible 2025-06-04 14:16:44 -04:00
show_conversation.sh Adding a GameId to allow moving through a list of games. 2025-05-28 10:38:16 -07:00
tsconfig.json WIP: Working on fixing moving from game to game 2025-06-16 09:39:29 -07:00
vite.config.js Fixing some issues with streaming and games not being accessible 2025-06-04 14:16:44 -04:00
vitest.config.ts Added some initial tests for the functions present. 2025-05-28 10:38:16 -07:00

AI Diplomacy Animation

A Three.js-based visualization of Diplomacy game states showing animated conversations between AI players and unit movements.

Turn Animation System

The application uses a sophisticated turn-based animation system that coordinates multiple types of animations through game phases.

Architecture Overview

The turn animation system is built around several key components that work together to create smooth, coordinated transitions:

  1. Main Game Loop (src/main.ts): Continuous animation loop that monitors all animation states
  2. Game State Management (src/gameState.ts): Central state coordination with boolean locks
  3. Phase Management (src/phase.ts): Handles phase transitions and orchestration
  4. Unit Animation System (src/units/animate.ts): Creates and manages unit movement tweens

How Turn Animations Advance

The turn advancement follows a carefully orchestrated sequence:

1. Playback Initiation

When the user clicks Play, togglePlayback() is triggered, which:

  • Sets gameState.isPlaying = true
  • Hides the standings board
  • Starts the camera pan animation
  • Begins message display for the current phase

2. Message Animation Phase

If the current phase has messages:

  • updateChatWindows() displays messages word-by-word
  • Each message appears with typing animation
  • gameState.messagesPlaying tracks this state

3. Unit Animation Phase

Once messages complete (or if there are no messages):

  • displayPhaseWithAnimation() is called
  • createAnimationsForNextPhase() analyzes the previous phase's orders
  • Movement tweens are created for each unit based on order results
  • Animations are added to gameState.unitAnimations array

4. Animation Monitoring

The main animate() loop continuously:

  • Updates all active unit animations
  • Filters out completed animations
  • Detects when gameState.unitAnimations.length === 0

5. Phase Transition

When all animations complete:

  • advanceToNextPhase() is scheduled with a configurable delay
  • If the phase has a summary, text-to-speech is triggered
  • After speech completes, moveToNextPhase() increments the phase index
  • The cycle repeats for the next phase

State Coordination

The system uses several boolean flags to prevent race conditions and ensure proper sequencing:

  • messagesPlaying: Prevents unit animations from starting during message display
  • isAnimating: Tracks unit animation state
  • isSpeaking: Prevents phase advancement during text-to-speech
  • isPlaying: Overall playback state that gates all automatic progression
  • nextPhaseScheduled: Prevents multiple phase transitions from being scheduled

Animation Flow Diagram

flowchart TD
    A[User Clicks Play] --> B[togglePlayback]
    B --> C{Phase has messages?}
    
    C -->|Yes| D[updateChatWindows - Show messages word by word]
    C -->|No| E[displayPhaseWithAnimation]
    
    D --> F[Messages complete]
    F --> E
    
    E --> G[createAnimationsForNextPhase]
    G --> H[Create unit movement tweens based on previous phase orders]
    H --> I[Add animations to gameState.unitAnimations array]
    
    I --> J[Main animate loop monitors animations]
    J --> K{All animations complete?}
    
    K -->|No| J
    K -->|Yes| L[Schedule advanceToNextPhase with delay]
    
    L --> M{Phase has summary?}
    M -->|Yes| N[speakSummary - Text-to-speech]
    M -->|No| O[moveToNextPhase]
    
    N --> P[Speech complete]
    P --> O
    
    O --> Q[Increment gameState.phaseIndex]
    Q --> R[displayPhaseWithAnimation for next phase]
    R --> E
    
    style A fill:#e1f5fe
    style J fill:#fff3e0
    style K fill:#f3e5f5
    style O fill:#e8f5e8

Key Design Decisions

Centralized State Management: All animation states are tracked in the gameState object, making it easy to coordinate between different animation types and prevent conflicts.

Asynchronous Coordination: Rather than blocking operations, the system uses promises and callbacks to coordinate between message animations, unit movements, and speech synthesis.

Graceful Degradation: If text-to-speech fails or isn't available, the system continues with the next phase automatically.

Animation Filtering: The main loop actively filters completed animations from the tracking array, ensuring memory doesn't grow unbounded during long games.

Configurable Timing: Phase delays and animation durations are configurable through the config object, allowing easy adjustment of pacing.

This architecture ensures smooth, coordinated animations while maintaining clear separation of concerns between different animation systems.

Development

  • npm run dev - Start the development server
  • npm run build - Build for production
  • npm run lint - Run TypeScript linting

Game Data

Game data is loaded from JSON files in the public/games/ directory. The expected format includes phases with messages, orders, and state information for each turn of the Diplomacy game.

--TODO: Create something to combine the game data into a simpler place, Diary is in csv and I need that to display the thoughts of the LLM during the betrayal scenes