AI_Diplomacy/ai_animation
Tyler Marques ecf8e1db06
Another pass at basic moments
Adding diary, name of powers, name of moment type, and some supporting
thoughts.

Added a debug menu item to disable or enable the eleven labs speech.
Useful for removing it when debugging
2025-05-30 11:33:16 -07:00
..
.github/workflows Claude - Adding workflows that will run on PR to catch failed tests 2025-05-28 10:38:16 -07:00
public Removing files that got added by accident 2025-05-30 11:33:15 -07:00
src Another pass at basic moments 2025-05-30 11:33:16 -07:00
.gitignore Updating phase state control to allow for reinit of units at any phase. 2025-05-28 10:38:16 -07:00
CLAUDE.md Power names are now shown as the model name 2025-05-30 11:33:16 -07:00
Dockerfile Full working stream. Fixed issue with chrome SIGILL'ing with base software renderer 2025-03-17 16:27:03 -04:00
experiment_log.md adding the rotating display but bugs still with the game loop 2025-05-06 20:25:40 -04:00
index.html Debug menu. 2025-05-30 11:33:15 -07:00
package-lock.json Added some initial tests for the functions present. 2025-05-28 10:38:16 -07:00
package.json Power names are now shown as the model name 2025-05-30 11:33:16 -07:00
README.md Adding a GameId to allow moving through a list of games. 2025-05-28 10:38:16 -07: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 Retreats now work as expected 2025-03-26 17:05:30 -04:00
vite.config.js fixed logging and elevan labs! 2025-03-17 14:49:40 -07: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