All files / systems/ai AIPersonality.ts

83.33% Statements 10/12
100% Branches 4/4
75% Functions 6/8
83.33% Lines 10/12

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                                                                                                                                                                                        27x                                                                                                     27x                                                                                                                                                                                                                                                                         21x 21x                 37x 83x   37x             7x             3x                             4171x                                                                
/**
 * AI Personality System for Korean Martial Arts Combat
 * Defines behavioral archetypes that guide AI decision-making
 */
 
import { PlayerArchetype, TrigramStance } from "@/types";
 
/**
 * Movement pattern types for archetype behavior
 * 
 * @korean 이동 패턴 타입
 */
export type MovementPattern = "aggressive" | "evasive" | "analytical" | "unpredictable";
 
/**
 * Vital target priority for archetype-specific combat strategies
 * 
 * @korean 급소 우선순위
 */
export type VitalTargetPriority = "health" | "pain" | "consciousness" | "balanced";
 
/**
 * Technique category types for archetype preferences
 * 
 * @korean 기술 범주
 */
export type TechniqueCategory = 
  | "joint_manipulation" 
  | "bone_strikes" 
  | "nerve_strikes"
  | "silent_takedowns"
  | "anatomical_analysis"
  | "calculated_strikes"
  | "psychological_pressure"
  | "submission_induction"
  | "dirty_techniques"
  | "environmental_usage";
 
/**
 * Archetype-specific behavior profile
 * 
 * Defines combat preferences, movement patterns, and tactical decision-making
 * unique to each of the 5 player archetypes.
 * 
 * @korean 원형별 행동 프로필
 */
export interface ArchetypeBehavior {
  /** Preferred trigram stances for this archetype */
  readonly preferredStances: readonly TrigramStance[];
  /** Optimal combat range in grid cells (1 cell = ~40px) */
  readonly optimalRange: number;
  /** Health percentage threshold to trigger retreat behavior */
  readonly retreatThreshold: number;
  /** Technique categories this archetype favors */
  readonly techniqueSelectionBias: readonly TechniqueCategory[];
  /** Movement pattern characteristic of this archetype */
  readonly movementPattern: MovementPattern;
  /** Whether archetype follows honor code (affects retreat behavior) */
  readonly honorCode: boolean;
  /** Priority system for vital point targeting */
  readonly vitalTargetPriority: VitalTargetPriority;
}
 
/**
 * AI personality profile defining combat behavior
 */
export interface AIPersonality {
  readonly name: string;
  readonly koreanName: string;
  readonly archetype: PlayerArchetype;
  readonly aggressionLevel: number; // 0.0-1.0: How often AI attacks
  readonly defensePreference: number; // 0.0-1.0: Tendency to block/counter
  readonly comboTendency: number; // 0.0-1.0: Likelihood to continue combos
  readonly stanceSwitchFrequency: number; // 0.0-1.0: How often changes stance
  readonly feintChance: number; // 0.0-1.0: Probability of fake attacks
  readonly tacticalRetreatThreshold: number; // Health % to retreat
  readonly favoredStances: readonly TrigramStance[];
  readonly description: {
    readonly korean: string;
    readonly english: string;
  };
}
 
/**
 * Archetype-specific behavior profiles
 * 
 * Maps each of the 5 player archetypes to their unique combat behaviors,
 * movement patterns, and tactical preferences based on Korean martial arts
 * traditions and game design philosophy.
 * 
 * @korean 원형별 행동 프로필
 */
export const ARCHETYPE_BEHAVIORS: Record<PlayerArchetype, ArchetypeBehavior> = {
  [PlayerArchetype.MUSA]: {
    preferredStances: [TrigramStance.GEON, TrigramStance.JIN, TrigramStance.GAN], // Heaven, Thunder, Mountain
    optimalRange: 1, // Close quarters (1 cell = ~40px)
    retreatThreshold: 30, // Only retreats at very low health
    techniqueSelectionBias: ["joint_manipulation", "bone_strikes"],
    movementPattern: "aggressive",
    honorCode: true, // Never retreats above threshold
    vitalTargetPriority: "balanced",
  },
  [PlayerArchetype.AMSALJA]: {
    preferredStances: [TrigramStance.SON, TrigramStance.GAM], // Wind, Water
    optimalRange: 1, // Stealth melee (1 cell)
    retreatThreshold: 60, // Retreats early if detected
    techniqueSelectionBias: ["nerve_strikes", "silent_takedowns"],
    movementPattern: "evasive",
    honorCode: false,
    vitalTargetPriority: "consciousness",
  },
  [PlayerArchetype.HACKER]: {
    preferredStances: [TrigramStance.LI, TrigramStance.TAE], // Fire, Lake
    optimalRange: 3, // Mid-range analysis (3 cells = ~120px)
    retreatThreshold: 50,
    techniqueSelectionBias: ["anatomical_analysis", "calculated_strikes"],
    movementPattern: "analytical",
    honorCode: false,
    vitalTargetPriority: "balanced",
  },
  [PlayerArchetype.JEONGBO_YOWON]: {
    preferredStances: [TrigramStance.GAN, TrigramStance.GON], // Mountain, Earth
    optimalRange: 2, // Tactical mid-range (2 cells = ~80px)
    retreatThreshold: 40,
    techniqueSelectionBias: ["psychological_pressure", "submission_induction"],
    movementPattern: "analytical",
    honorCode: false,
    vitalTargetPriority: "pain",
  },
  [PlayerArchetype.JOJIK_POKRYEOKBAE]: {
    preferredStances: [TrigramStance.JIN, TrigramStance.GAM], // Thunder, Water (adaptable)
    optimalRange: 1, // Close brutal combat (1 cell)
    retreatThreshold: 70, // Retreats pragmatically
    techniqueSelectionBias: ["dirty_techniques", "environmental_usage"],
    movementPattern: "unpredictable",
    honorCode: false,
    vitalTargetPriority: "health",
  },
};
 
/**
 * Five AI personality archetypes inspired by Korean martial arts philosophy
 */
export const AI_PERSONALITIES: Record<string, AIPersonality> = {
  /**
   * 맹공자 (Maenggongja) - Fierce Attacker
   * Aggressive pressure fighter using Musa archetype
   */
  AGGRESSIVE_STRIKER: {
    name: "Aggressive Striker",
    koreanName: "맹공자",
    archetype: PlayerArchetype.MUSA,
    aggressionLevel: 0.85,
    defensePreference: 0.2,
    comboTendency: 0.7,
    stanceSwitchFrequency: 0.3,
    feintChance: 0.15,
    tacticalRetreatThreshold: 0.15,
    favoredStances: [
      TrigramStance.GEON, // Heaven - Direct force
      TrigramStance.JIN, // Thunder - Explosive power
      TrigramStance.LI, // Fire - Precision strikes
    ],
    description: {
      korean: "정면 돌파를 선호하는 공격적인 전사",
      english: "Aggressive warrior who prefers frontal assault",
    },
  },
 
  /**
   * 기술가 (Gisulga) - Technical Master
   * Precision fighter using Amsalja archetype
   */
  TECHNICAL_MASTER: {
    name: "Technical Master",
    koreanName: "기술가",
    archetype: PlayerArchetype.AMSALJA,
    aggressionLevel: 0.5,
    defensePreference: 0.6,
    comboTendency: 0.4,
    stanceSwitchFrequency: 0.7,
    feintChance: 0.35,
    tacticalRetreatThreshold: 0.35,
    favoredStances: [
      TrigramStance.SON, // Wind - Continuous pressure
      TrigramStance.GAM, // Water - Flow and adaptation
      TrigramStance.TAE, // Lake - Fluid manipulation
    ],
    description: {
      korean: "정밀한 기술로 약점을 노리는 달인",
      english: "Master who targets weaknesses with precise techniques",
    },
  },
 
  /**
   * 균형 잡힌 자 (Gyunhyeong Jabin-ja) - Balanced Fighter
   * All-around fighter using Jeongbo Yowon archetype
   */
  BALANCED_FIGHTER: {
    name: "Balanced Fighter",
    koreanName: "균형 잡힌 자",
    archetype: PlayerArchetype.JEONGBO_YOWON,
    aggressionLevel: 0.6,
    defensePreference: 0.5,
    comboTendency: 0.5,
    stanceSwitchFrequency: 0.5,
    feintChance: 0.25,
    tacticalRetreatThreshold: 0.25,
    favoredStances: [
      TrigramStance.GEON, // Heaven
      TrigramStance.GAM, // Water
      TrigramStance.GAN, // Mountain
      TrigramStance.GON, // Earth
    ],
    description: {
      korean: "공격과 방어의 조화를 추구하는 전략가",
      english: "Strategist seeking harmony between offense and defense",
    },
  },
 
  /**
   * 방어의 달인 (Bangeo-ui Dallin) - Defensive Specialist
   * Counter-attack focused using Hacker archetype
   */
  DEFENSIVE_SPECIALIST: {
    name: "Defensive Specialist",
    koreanName: "방어의 달인",
    archetype: PlayerArchetype.HACKER,
    aggressionLevel: 0.35,
    defensePreference: 0.8,
    comboTendency: 0.3,
    stanceSwitchFrequency: 0.4,
    feintChance: 0.4,
    tacticalRetreatThreshold: 0.4,
    favoredStances: [
      TrigramStance.GAN, // Mountain - Defensive mastery
      TrigramStance.GON, // Earth - Grounding
      TrigramStance.GAM, // Water - Adaptation
    ],
    description: {
      korean: "방어에서 반격의 기회를 찾는 전문가",
      english: "Expert who finds counter-attack opportunities through defense",
    },
  },
 
  /**
   * 혼돈의 전사 (Hondon-ui Jeonsa) - Chaos Warrior
   * Unpredictable fighter using Jojik Pokryeokbae archetype
   */
  CHAOS_WARRIOR: {
    name: "Chaos Warrior",
    koreanName: "혼돈의 전사",
    archetype: PlayerArchetype.JOJIK_POKRYEOKBAE,
    aggressionLevel: 0.75,
    defensePreference: 0.3,
    comboTendency: 0.6,
    stanceSwitchFrequency: 0.8,
    feintChance: 0.5,
    tacticalRetreatThreshold: 0.1,
    favoredStances: [
      TrigramStance.LI, // Fire - Unpredictable
      TrigramStance.SON, // Wind - Constant motion
      TrigramStance.JIN, // Thunder - Explosive
      TrigramStance.TAE, // Lake - Fluid
    ],
    description: {
      korean: "예측 불가능한 패턴으로 상대를 혼란시키는 전사",
      english: "Warrior who confuses opponents with unpredictable patterns",
    },
  },
};
 
/**
 * Get a random AI personality
 */
export function getRandomPersonality(): AIPersonality {
  const personalities = Object.values(AI_PERSONALITIES);
  return personalities[Math.floor(Math.random() * personalities.length)];
}
 
/**
 * Get personality by archetype
 */
export function getPersonalityByArchetype(
  archetype: PlayerArchetype
): AIPersonality {
  const personality = Object.values(AI_PERSONALITIES).find(
    (p) => p.archetype === archetype
  );
  return personality ?? AI_PERSONALITIES.BALANCED_FIGHTER;
}
 
/**
 * Get personality by name key
 */
export function getPersonalityByName(name: string): AIPersonality {
  return AI_PERSONALITIES[name] ?? AI_PERSONALITIES.BALANCED_FIGHTER;
}
 
/**
 * List all available personalities
 */
export function getAllPersonalities(): readonly AIPersonality[] {
  return Object.values(AI_PERSONALITIES);
}
 
/**
 * Get archetype-specific behavior profile
 * 
 * Retrieves the unique combat behavior configuration for a given archetype,
 * including movement patterns, optimal ranges, and tactical preferences.
 * 
 * @param archetype - Player archetype to get behavior for
 * @returns Archetype behavior profile
 * 
 * @korean 원형별 행동 프로필 가져오기
 */
export function getArchetypeBehavior(archetype: PlayerArchetype): ArchetypeBehavior {
  return ARCHETYPE_BEHAVIORS[archetype];
}
 
/**
 * Check if archetype follows honor code
 * 
 * Honor code affects retreat behavior - honor-bound archetypes like Musa
 * will not retreat above their health threshold.
 * 
 * @param archetype - Player archetype to check
 * @returns True if archetype follows honor code
 * 
 * @korean 명예 규범 확인
 */
export function followsHonorCode(archetype: PlayerArchetype): boolean {
  return ARCHETYPE_BEHAVIORS[archetype].honorCode;
}
 
/**
 * Get optimal combat range for archetype
 * 
 * Returns the preferred distance in grid cells (1 cell = ~40px) where
 * the archetype is most effective in combat.
 * 
 * @param archetype - Player archetype
 * @returns Optimal range in grid cells
 * 
 * @korean 최적 전투 거리 가져오기
 */
export function getOptimalRange(archetype: PlayerArchetype): number {
  return ARCHETYPE_BEHAVIORS[archetype].optimalRange;
}