All files / systems VitalPointSystem.ts

93.33% Statements 42/45
80% Branches 16/20
100% Functions 11/11
95.34% Lines 41/43

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                                                                                                          185x                 185x                                                                                               21x 2x 2x 1x         20x 20x                   20x       20x   20x 17x                   3x                                                   517x                                   11x                                                                                 2x   2x                   2x       2x   2x 1x                 1x                                       22x   22x 22x   22x 1540x 1540x 35x 35x       22x                           1585x 1585x 1585x                                 1x 1x   1x                                                     19x 19x 19x                           185x          
/**
 * System for managing Korean martial arts vital point (급소) targeting and damage calculation.
 * 
 * **Korean**: 급소 시스템 (Vital Point System)
 * 
 * The VitalPointSystem implements anatomical targeting mechanics based on traditional
 * Korean martial arts knowledge of 70 vital points (급소). It handles hit detection,
 * damage calculation, and status effect application for precise strikes.
 * 
 * ## Vital Point Philosophy
 * 
 * Korean martial arts identify specific anatomical locations that, when struck precisely,
 * can cause disproportionate effects. The system categorizes points by:
 * 
 * - **Location**: Head, neck, torso, limbs, and core
 * - **Category**: Neurological, vascular, respiratory, muscular, skeletal, etc.
 * - **Severity**: Minor, moderate, major, critical, lethal
 * - **Effects**: Unconsciousness, paralysis, pain, stunning, etc.
 * 
 * ## Key Features
 * 
 * - Distance-based hit detection with accuracy falloff
 * - Severity multipliers for damage calculation
 * - Targeted vs. proximity-based strikes
 * - Bilingual Korean-English vital point names
 * - Realistic anatomical positioning
 * 
 * @example
 * ```typescript
 * const vitalPointSystem = new VitalPointSystem();
 * 
 * // Process a strike at specific position
 * const result = vitalPointSystem.processHit(
 *   { x: 100, y: 50 }, // Target position
 *   { width: 10, height: 10 }, // Hit box
 *   "GB-20" // Optional: target specific vital point
 * );
 * 
 * if (result.hit && result.vitalPointHit) {
 *   console.log(`Hit ${result.vitalPointHit.names.english}!`);
 *   console.log(`Damage: ${result.damage}, Severity: ${result.severity}`);
 * }
 * ```
 * 
 * @public
 * @category Vital Point System
 * @korean 급소시스템
 */
import { Position, VitalPointSeverity } from "../types/common";
import { VitalPoint, VitalPointHitResult } from "./vitalpoint/types";
import { VITAL_POINTS_DATA } from "./vitalpoint/VitalPointsData";
 
export class VitalPointSystem {
  private vitalPoints: VitalPoint[] = [];
 
  /**
   * Creates a new VitalPointSystem instance.
   * 
   * Initializes the system with comprehensive Korean vital points database.
   */
  constructor() {
    // Initialize with comprehensive Korean vital points database
    this.initializeVitalPoints();
  }
 
  /**
   * Processes a hit at a specific position to determine vital point impact.
   * 
   * Evaluates whether a strike lands on or near a vital point, calculating
   * damage and effects based on accuracy and severity. Supports both targeted
   * strikes (specific vital point ID) and proximity-based detection.
   * 
   * ## Hit Detection Algorithm
   * 
   * 1. If targetedVitalPointId provided, validate that specific point
   * 2. Otherwise, find closest vital point to target position
   * 3. Calculate distance from strike to vital point
   * 4. Apply accuracy falloff based on distance (max 50px)
   * 5. Return hit result with damage multipliers
   * 
   * @param targetPosition - Position where strike lands
   * @param _hitBox - Size of hit box (currently unused, reserved for future)
   * @param targetedVitalPointId - Optional specific vital point being targeted
   * @returns Hit result with damage, effects, and severity
   * 
   * @example
   * ```typescript
   * // Proximity-based strike
   * const result1 = vitalPointSystem.processHit(
   *   { x: 100, y: 50 },
   *   { width: 10, height: 10 }
   * );
   * 
   * // Targeted strike at temple (태양혈)
   * const result2 = vitalPointSystem.processHit(
   *   { x: 100, y: 50 },
   *   { width: 10, height: 10 },
   *   "head_temple"
   * );
   * ```
   * 
   * @public
   * @korean 타격처리
   */
  processHit(
    targetPosition: Position,
    _hitBox: { width: number; height: number }, // Prefixed with underscore to indicate intentionally unused
    targetedVitalPointId?: string | null
  ): VitalPointHitResult {
    // If a specific vital point is targeted, check that one
    if (targetedVitalPointId) {
      const targetVitalPoint = this.getVitalPointById(targetedVitalPointId);
      if (targetVitalPoint) {
        return this.calculateVitalPointHit(targetVitalPoint, targetPosition);
      }
    }
 
    // Otherwise, find the closest vital point
    const closestVitalPoint = this.findClosestVitalPoint(targetPosition);
    Iif (!closestVitalPoint) {
      return {
        hit: false,
        damage: 0,
        effects: [],
        severity: VitalPointSeverity.MINOR,
      };
    }
 
    // Calculate distance to determine hit accuracy
    const distance = this.calculateDistance(
      targetPosition,
      closestVitalPoint.position
    );
    const maxHitDistance = 50; // pixels
 
    if (distance <= maxHitDistance) {
      return {
        hit: true,
        vitalPointHit: closestVitalPoint,
        damage: this.calculateBaseDamage(closestVitalPoint, distance),
        effects: [],
        severity: closestVitalPoint.severity,
        accuracy: Math.max(0, 1 - distance / maxHitDistance),
      };
    }
 
    return {
      hit: false,
      damage: 0,
      effects: [],
      severity: VitalPointSeverity.MINOR,
    };
  }
 
  /**
   * Retrieves a vital point by its unique identifier.
   * 
   * @param id - Vital point identifier (e.g., "GB-20", "head_temple")
   * @returns Vital point if found, null otherwise
   * 
   * @example
   * ```typescript
   * const temple = vitalPointSystem.getVitalPointById("head_temple");
   * if (temple) {
   *   console.log(temple.names.korean); // "태양혈"
   * }
   * ```
   * 
   * @public
   * @korean 급소조회
   */
  getVitalPointById(id: string): VitalPoint | null {
    return this.vitalPoints.find((vp) => vp.id === id) || null;
  }
 
  /**
   * Gets all registered vital points in the system.
   * 
   * @returns Read-only array of all vital points
   * 
   * @example
   * ```typescript
   * const allPoints = vitalPointSystem.getVitalPoints();
   * console.log(`${allPoints.length} vital points registered`);
   * ```
   * 
   * @public
   * @korean 급소목록조회
   */
  getVitalPoints(): readonly VitalPoint[] {
    return this.vitalPoints;
  }
 
  /**
   * Calculates hit result for a technique-based attack.
   * 
   * Determines whether a technique successfully lands on a vital point,
   * factoring in technique accuracy, attacker position, and randomness.
   * Used for AI and player technique execution.
   * 
   * @param technique - Technique being executed with accuracy property
   * @param attackerPosition - Position where attack originates
   * @param _defenderPosition - Defender position (reserved for future use)
   * @param _defenderStance - Defender stance (reserved for future use)
   * @returns Hit result with damage and effects
   * 
   * @example
   * ```typescript
   * const technique = {
   *   id: "geon-thunder-strike",
   *   accuracy: 0.85
   * };
   * 
   * const result = vitalPointSystem.calculateHit(
   *   technique,
   *   { x: 100, y: 50 },
   *   { x: 120, y: 50 },
   *   TrigramStance.GEON
   * );
   * ```
   * 
   * @public
   * @korean 기술타격계산
   */
  calculateHit(
    technique: any,
    attackerPosition: Position,
    _defenderPosition: Position, // Prefixed with underscore to indicate intentionally unused
    _defenderStance: any // Prefixed with underscore to indicate intentionally unused
  ): VitalPointHitResult {
    // Find closest vital point to attack
    const closestVitalPoint = this.findClosestVitalPoint(attackerPosition);
 
    Iif (!closestVitalPoint) {
      return {
        hit: false,
        damage: 0,
        effects: [],
        severity: VitalPointSeverity.MINOR,
      };
    }
 
    // Calculate hit based on technique accuracy and distance
    const distance = this.calculateDistance(
      attackerPosition,
      closestVitalPoint.position
    );
    const hitChance = technique.accuracy * (1 - distance / 100);
 
    if (Math.random() < hitChance) {
      return {
        hit: true,
        vitalPointHit: closestVitalPoint,
        damage: this.calculateBaseDamage(closestVitalPoint, distance),
        effects: [],
        severity: closestVitalPoint.severity,
      };
    }
 
    return {
      hit: false,
      damage: 0,
      effects: [],
      severity: VitalPointSeverity.MINOR,
    };
  }
 
  /**
   * Finds the closest vital point to a given position.
   * 
   * Uses Euclidean distance to determine proximity.
   * 
   * @param position - Target position
   * @returns Closest vital point or null if none registered
   * 
   * @private
   * @korean 최근접급소검색
   */
  private findClosestVitalPoint(position: Position): VitalPoint | null {
    Iif (this.vitalPoints.length === 0) return null;
 
    let closest = this.vitalPoints[0];
    let minDistance = this.calculateDistance(position, closest.position);
 
    for (const vitalPoint of this.vitalPoints) {
      const distance = this.calculateDistance(position, vitalPoint.position);
      if (distance < minDistance) {
        minDistance = distance;
        closest = vitalPoint;
      }
    }
 
    return closest;
  }
 
  /**
   * Calculates Euclidean distance between two positions.
   * 
   * @param pos1 - First position
   * @param pos2 - Second position
   * @returns Distance in pixels
   * 
   * @private
   * @korean 거리계산
   */
  private calculateDistance(pos1: Position, pos2: Position): number {
    const dx = pos1.x - pos2.x;
    const dy = pos1.y - pos2.y;
    return Math.sqrt(dx * dx + dy * dy);
  }
 
  /**
   * Calculates vital point hit result with accuracy and damage.
   * 
   * @param vitalPoint - Vital point being struck
   * @param hitPosition - Exact position of strike
   * @returns Hit result with calculated damage and accuracy
   * 
   * @private
   * @korean 급소타격결과계산
   */
  private calculateVitalPointHit(
    vitalPoint: VitalPoint,
    hitPosition: Position
  ): VitalPointHitResult {
    const distance = this.calculateDistance(hitPosition, vitalPoint.position);
    const baseDamage = this.calculateBaseDamage(vitalPoint, distance);
 
    return {
      hit: true,
      vitalPointHit: vitalPoint,
      damage: baseDamage,
      effects: [],
      severity: vitalPoint.severity,
      accuracy: Math.max(0, 1 - distance / 50),
    };
  }
 
  /**
   * Calculates base damage for a vital point strike.
   * 
   * Applies distance-based falloff to vital point's base damage value.
   * Closer strikes deal more damage up to maximum base damage.
   * 
   * @param vitalPoint - Vital point with base damage property
   * @param distance - Distance from strike to vital point center
   * @returns Calculated damage value
   * 
   * @private
   * @korean 기본피해계산
   */
  private calculateBaseDamage(
    vitalPoint: VitalPoint,
    distance: number
  ): number {
    const baseDamage = vitalPoint.baseDamage || 10;
    const distanceModifier = Math.max(0.1, 1 - distance / 100);
    return Math.floor(baseDamage * distanceModifier);
  }
 
  /**
   * Initializes the system with comprehensive Korean vital points.
   * 
   * Loads all 70 Korean vital points from the comprehensive database
   * covering head, torso, arms, and legs with proper categorization.
   * 
   * @private
   * @korean 급소초기화
   */
  private initializeVitalPoints(): void {
    // Load all 70 vital points from the comprehensive database
    this.vitalPoints = [...VITAL_POINTS_DATA];
  }
}
 
export default VitalPointSystem;