All files / systems/ai TrainingAI.ts

96% Statements 48/50
87.5% Branches 14/16
94.11% Functions 16/17
96% Lines 48/50

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383                                                                                                      15x                                                                     31x               31x       31x   31x                           31x 31x   31x 31x                 32x   4x   23x   5x                   9x                     1x                                             27x 1x     26x     26x     26x 21x       5x                                                       5x         5x             5x     5x           5x                 5x     5x                       5x 5x 5x             5x 5x             1x                   1x                   3x 3x   3x                 3x             1x 1x 1x   1x                     19x             100x             100x                                               1x                
/**
 * TrainingAI - AI opponent system for training mode
 * 
 * Integrates AIPersonality, DecisionTree, and ComboSystem to create
 * a configurable AI opponent for training scenarios.
 * 
 * **Korean Philosophy (훈련 AI 철학)**:
 * - 단계적 난이도 (Stepped Difficulty): Progressive challenge levels
 * - 교육적 피드백 (Educational Feedback): AI teaches through behavior
 * - 적응형 전투 (Adaptive Combat): AI adjusts to player skill
 */
 
import { Position, TrigramStance } from "@/types";
import { PlayerState } from "../player";
import { AIPersonality, AI_PERSONALITIES } from "./AIPersonality";
import { AIDecisionTree, CombatContext, AIDecision } from "./DecisionTree";
import { AIComboSystem } from "./ComboSystem";
import { AdaptiveDifficulty } from "./AdaptiveDifficulty";
 
/**
 * Training AI difficulty levels
 */
export type AITrainingDifficulty = "easy" | "medium" | "hard";
 
/**
 * Training AI state
 */
export interface TrainingAIState {
  readonly difficulty: AITrainingDifficulty;
  readonly personality: AIPersonality;
  readonly currentAction: AIDecision | null;
  readonly position: Position;
  readonly stance: TrigramStance;
  readonly isActive: boolean;
  readonly reactionTime: number; // ms delay before responding
  readonly blockChance: number; // 0.0-1.0
  readonly counterChance: number; // 0.0-1.0
  readonly lastActionTime: number;
}
 
/**
 * Training AI configuration based on difficulty
 */
interface DifficultyConfig {
  readonly reactionTime: number;
  readonly blockChance: number;
  readonly counterChance: number;
  readonly aggressionMultiplier: number;
  readonly aiSkillLevel: number; // For DecisionTree
}
 
const DIFFICULTY_CONFIGS: Record<AITrainingDifficulty, DifficultyConfig> = {
  easy: {
    reactionTime: 500, // 500ms reaction delay
    blockChance: 0.3,
    counterChance: 0.1,
    aggressionMultiplier: 0.6,
    aiSkillLevel: 0.3,
  },
  medium: {
    reactionTime: 300, // 300ms reaction delay
    blockChance: 0.5,
    counterChance: 0.3,
    aggressionMultiplier: 0.8,
    aiSkillLevel: 0.6,
  },
  hard: {
    reactionTime: 150, // 150ms reaction delay
    blockChance: 0.7,
    counterChance: 0.5,
    aggressionMultiplier: 1.0,
    aiSkillLevel: 0.9,
  },
};
 
/**
 * TrainingAI System
 * 
 * Manages AI opponent behavior in training mode using existing AI systems.
 * Provides configurable difficulty and realistic martial arts behavior.
 */
export class TrainingAI {
  private state: TrainingAIState;
  private decisionTree: AIDecisionTree;
  private comboSystem: AIComboSystem;
  private adaptiveDifficulty: AdaptiveDifficulty;
  private actionDelayTimer: number = 0;
 
  constructor(
    difficulty: AITrainingDifficulty = "medium",
    initialPosition: Position = { x: 5, y: 0 },
    personalityKey?: string
  ) {
    // Select personality based on difficulty or use provided key
    const personality = personalityKey
      ? AI_PERSONALITIES[personalityKey] ?? AI_PERSONALITIES.BALANCED_FIGHTER
      : this.selectPersonalityForDifficulty(difficulty);
 
    const config = DIFFICULTY_CONFIGS[difficulty];
 
    this.state = {
      difficulty,
      personality,
      currentAction: null,
      position: initialPosition,
      stance: personality.favoredStances[0] ?? TrigramStance.GEON,
      isActive: false,
      reactionTime: config.reactionTime,
      blockChance: config.blockChance,
      counterChance: config.counterChance,
      lastActionTime: 0,
    };
 
    // Initialize AI systems
    this.decisionTree = new AIDecisionTree();
    this.decisionTree.setDifficultyLevel(config.aiSkillLevel);
 
    this.comboSystem = new AIComboSystem();
    this.adaptiveDifficulty = new AdaptiveDifficulty();
  }
 
  /**
   * Select appropriate personality for difficulty level
   */
  private selectPersonalityForDifficulty(
    difficulty: AITrainingDifficulty
  ): AIPersonality {
    switch (difficulty) {
      case "easy":
        return AI_PERSONALITIES.DEFENSIVE_SPECIALIST; // Less aggressive
      case "medium":
        return AI_PERSONALITIES.BALANCED_FIGHTER; // Balanced approach
      case "hard":
        return AI_PERSONALITIES.AGGRESSIVE_STRIKER; // More aggressive
      default:
        return AI_PERSONALITIES.BALANCED_FIGHTER;
    }
  }
 
  /**
   * Activate AI opponent
   */
  activate(): void {
    this.state = {
      ...this.state,
      isActive: true,
      lastActionTime: Date.now(),
    };
  }
 
  /**
   * Deactivate AI opponent
   */
  deactivate(): void {
    this.state = {
      ...this.state,
      isActive: false,
      currentAction: null,
    };
  }
 
  /**
   * Update AI behavior (60fps game loop)
   * 
   * Internal method called each frame by the TrainingAI system to process AI decision-making and update state.
   * Respects reaction time delays based on difficulty level.
   * 
   * @param deltaTime - Time since last frame in seconds
   * @param playerState - Current player state
   * @param aiPlayerState - Current AI player state (for combat systems)
   * @returns Updated AI decision or null if inactive/delayed
   */
  update(
    deltaTime: number,
    playerState: PlayerState,
    aiPlayerState: PlayerState
  ): AIDecision | null {
    if (!this.state.isActive) {
      return null;
    }
 
    const now = Date.now();
 
    // Update action delay timer
    this.actionDelayTimer = Math.max(0, this.actionDelayTimer - deltaTime * 1000);
 
    // Check if enough time has passed since last decision (reaction time)
    if (this.actionDelayTimer > 0) {
      return this.state.currentAction;
    }
 
    // Build combat context
    const context: CombatContext = {
      playerPosition: this.state.position,
      opponentPosition: playerState.position,
      playerHealth: aiPlayerState.health,
      playerMaxHealth: aiPlayerState.maxHealth,
      playerKi: aiPlayerState.ki,
      playerMaxKi: aiPlayerState.maxKi,
      playerStamina: aiPlayerState.stamina,
      playerMaxStamina: aiPlayerState.maxStamina,
      opponentHealth: playerState.health,
      opponentStance: playerState.currentStance,
      playerStance: this.state.stance,
      distanceToOpponent: this.calculateDistance(
        this.state.position,
        playerState.position
      ),
      timeInMatch: (now - this.state.lastActionTime) / 1000,
      isOpponentAttacking: this.isPlayerAttacking(playerState),
      recentDamageTaken: aiPlayerState.totalDamageReceived,
      arenaBounds: {
        x: -8,
        y: -6,
        width: 16,
        height: 12,
      },
    };
 
    // Get adjusted personality from adaptive difficulty
    const adjustedPersonality = this.adjustPersonalityForTraining(
      this.state.personality
    );
 
    // Make decision using decision tree
    const decision = this.decisionTree.makeDecision(
      context,
      adjustedPersonality,
      this.comboSystem
    );
 
    // Apply reaction time delay for next action
    this.actionDelayTimer = this.state.reactionTime;
 
    // Update state with new decision
    this.state = {
      ...this.state,
      currentAction: decision,
      lastActionTime: now,
    };
 
    return decision;
  }
 
  /**
   * Adjust personality based on training context
   */
  private adjustPersonalityForTraining(
    basePersonality: AIPersonality
  ): AIPersonality {
    const config = DIFFICULTY_CONFIGS[this.state.difficulty];
 
    // Apply difficulty multiplier to aggression
    return {
      ...basePersonality,
      aggressionLevel: basePersonality.aggressionLevel * config.aggressionMultiplier,
      defensePreference:
        basePersonality.defensePreference * (2 - config.aggressionMultiplier),
    };
  }
 
  /**
   * Calculate distance between two positions
   */
  private calculateDistance(pos1: Position, pos2: Position): number {
    const dx = pos2.x - pos1.x;
    const dy = pos2.y - pos1.y;
    return Math.sqrt(dx * dx + dy * dy);
  }
 
  /**
   * Determine if player is currently attacking
   */
  private isPlayerAttacking(playerState: PlayerState): boolean {
    const timeSinceLastAction = Date.now() - playerState.lastActionTime;
    return timeSinceLastAction < 500; // 500ms attack window
  }
 
  /**
   * Update AI position (for movement actions)
   */
  updatePosition(newPosition: Position): void {
    this.state = {
      ...this.state,
      position: newPosition,
    };
  }
 
  /**
   * Update AI stance
   */
  updateStance(newStance: TrigramStance): void {
    this.state = {
      ...this.state,
      stance: newStance,
    };
  }
 
  /**
   * Set difficulty
   */
  setDifficulty(difficulty: AITrainingDifficulty): void {
    const config = DIFFICULTY_CONFIGS[difficulty];
    const personality = this.selectPersonalityForDifficulty(difficulty);
 
    this.state = {
      ...this.state,
      difficulty,
      personality,
      reactionTime: config.reactionTime,
      blockChance: config.blockChance,
      counterChance: config.counterChance,
    };
 
    this.decisionTree.setDifficultyLevel(config.aiSkillLevel);
  }
 
  /**
   * Reset AI state
   */
  reset(): void {
    this.decisionTree.reset();
    this.comboSystem.resetCombo();
    this.actionDelayTimer = 0;
 
    this.state = {
      ...this.state,
      currentAction: null,
      lastActionTime: Date.now(),
    };
  }
 
  /**
   * Get current AI state (for display/debugging)
   */
  getState(): Readonly<TrainingAIState> {
    return this.state;
  }
 
  /**
   * Check if AI should block incoming attack
   */
  shouldBlock(): boolean {
    return Math.random() < this.state.blockChance;
  }
 
  /**
   * Check if AI should counter incoming attack
   */
  shouldCounter(): boolean {
    return Math.random() < this.state.counterChance;
  }
 
  /**
   * Get AI's current decision for debugging
   */
  getCurrentDecision(): AIDecision | null {
    return this.state.currentAction;
  }
 
  /**
   * Update adaptive difficulty based on match performance
   */
  updateAdaptiveDifficulty(matchData: {
    readonly hitsLanded: number;
    readonly totalAttacks: number;
    readonly combosExecuted: number;
    readonly perfectBlockCount: number;
    readonly avgReactionTimeMs: number;
    readonly vitalPointsHit: number;
    readonly effectiveStanceChanges: number;
    readonly damageDealt: number;
    readonly damageTaken: number;
  }): void {
    this.adaptiveDifficulty.updateSkillMetrics(matchData);
 
    // Optionally adjust difficulty based on adaptive system
    // For training, we might want to keep difficulty stable
  }
}
 
export default TrainingAI;