All files / systems/trigram TrigramCalculator.ts

62.96% Statements 34/54
74.28% Branches 26/35
75% Functions 6/8
64.15% Lines 34/53

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                    36x                                             96x 5x       91x 96x                                                                                                                                   1618x 1618x               10x 10x 10x   10x 80x       80x 19x 19x       10x                   2407x 9x       2398x     2398x 2398x 2398x   2398x         2398x           2398x 2398x                                                                     64x     64x     30x         34x 17x     17x 16x       1x          
import { TrigramStance } from "../../types/common";
import { StanceLaterality } from "./types";
 
/**
 * Enhanced stance effectiveness matrix for Korean martial arts
 * Based on traditional I-Ching trigram relationships
 */
const STANCE_EFFECTIVENESS_MATRIX: Record<
  TrigramStance,
  Partial<Record<TrigramStance, number>>
> = {
  [TrigramStance.GEON]: { [TrigramStance.GON]: 1.2, [TrigramStance.SON]: 0.8 },
  [TrigramStance.GON]: { [TrigramStance.GEON]: 0.8, [TrigramStance.GAM]: 1.2 },
  [TrigramStance.TAE]: { [TrigramStance.JIN]: 1.2, [TrigramStance.GAN]: 0.8 },
  [TrigramStance.JIN]: { [TrigramStance.TAE]: 0.8, [TrigramStance.SON]: 1.2 },
  [TrigramStance.LI]: { [TrigramStance.GAM]: 0.8, [TrigramStance.TAE]: 0.8 },
  [TrigramStance.GAM]: {
    // water extinguishes fire:
    [TrigramStance.LI]: 1.2,
    [TrigramStance.GON]: 0.8,
  },
  [TrigramStance.SON]: { [TrigramStance.GEON]: 1.2, [TrigramStance.JIN]: 0.8 },
  [TrigramStance.GAN]: { [TrigramStance.TAE]: 1.2, [TrigramStance.LI]: 0.8 },
};
 
export class TrigramCalculator {
  /**
   * Calculate effectiveness of one stance against another
   */
  calculateStanceEffectiveness(
    attackerStance: TrigramStance,
    defenderStance: TrigramStance
  ): number {
    if (attackerStance === defenderStance) {
      return 1.0; // Neutral when same stance
    }
 
    const effectiveness =
      STANCE_EFFECTIVENESS_MATRIX[attackerStance]?.[defenderStance];
    return effectiveness ?? 1.0; // Default to neutral if no specific relationship
  }
 
  /**
   * Get the optimal counter stance for a given stance
   */
  getCounterStance(targetStance: TrigramStance): TrigramStance {
    let bestCounter = TrigramStance.GEON;
    let bestEffectiveness = 0;
 
    // Find stance with highest effectiveness against target
    for (const stance of Object.values(TrigramStance)) {
      const effectiveness = this.calculateStanceEffectiveness(
        stance,
        targetStance
      );
      if (effectiveness > bestEffectiveness) {
        bestEffectiveness = effectiveness;
        bestCounter = stance;
      }
    }
 
    return bestCounter;
  }
 
  /**
   * Calculate transition difficulty between stances
   */
  calculateTransitionDifficulty(
    fromStance: TrigramStance,
    toStance: TrigramStance
  ): number {
    if (fromStance === toStance) return 0;
 
    // Base difficulty for any transition
    const baseDifficulty = 0.5;
 
    // Get stance order for adjacency calculation
    const stanceOrder = Object.values(TrigramStance);
    const fromIndex = stanceOrder.indexOf(fromStance);
    const toIndex = stanceOrder.indexOf(toStance);
 
    if (fromIndex === -1 || toIndex === -1) {
      return 1.0; // Unknown stances, high difficulty
    }
 
    // Calculate distance (adjacent stances are easier)
    const distance = Math.min(
      Math.abs(toIndex - fromIndex),
      stanceOrder.length - Math.abs(toIndex - fromIndex)
    );
 
    // Normalize distance to 0-1 range and add base difficulty
    const normalizedDistance = distance / (stanceOrder.length / 2);
    return baseDifficulty + normalizedDistance * 0.5;
  }
 
  /**
   * Calculate stance effectiveness between attacker and defender
   */
  static calculateStanceEffectiveness(
    attackerStance: TrigramStance,
    defenderStance: TrigramStance
  ): number {
    // Use the effectiveness matrix from constants
    const effectiveness =
      STANCE_EFFECTIVENESS_MATRIX[attackerStance]?.[defenderStance];
    return effectiveness ?? 1.0; // Default neutral effectiveness
  }
 
  /**
   * Get optimal counter stance against opponent stance
   */
  static getCounterStance(opponentStance: TrigramStance): TrigramStance {
    // Find the stance that has highest effectiveness against opponent
    const stances = Object.values(TrigramStance);
    let bestCounter = TrigramStance.GEON;
    let bestEffectiveness = 0;
 
    stances.forEach((stance) => {
      const effectiveness = this.calculateStanceEffectiveness(
        stance,
        opponentStance
      );
      if (effectiveness > bestEffectiveness) {
        bestEffectiveness = effectiveness;
        bestCounter = stance;
      }
    });
 
    return bestCounter;
  }
 
  /**
   * Calculate difficulty of transitioning between stances
   */
  static calculateTransitionDifficulty(
    fromStance: TrigramStance,
    toStance: TrigramStance
  ): number {
    if (fromStance === toStance) {
      return 0; // No transition needed
    }
 
    // Base difficulty for any transition
    const baseDifficulty = 0.5;
 
    // Get stance order for adjacency calculation
    const stanceOrder = Object.values(TrigramStance);
    const fromIndex = stanceOrder.indexOf(fromStance);
    const toIndex = stanceOrder.indexOf(toStance);
 
    Iif (fromIndex === -1 || toIndex === -1) {
      return 1.0; // Unknown stances, high difficulty
    }
 
    // Calculate distance (adjacent stances are easier)
    const distance = Math.min(
      Math.abs(toIndex - fromIndex),
      stanceOrder.length - Math.abs(toIndex - fromIndex)
    );
 
    // Normalize distance to 0-1 range and add base difficulty
    const normalizedDistance = distance / (stanceOrder.length / 2);
    return baseDifficulty + normalizedDistance * 0.5;
  }
 
  /**
   * Calculate laterality modifier based on stance matching.
   * 
   * In Korean martial arts, matched stances (both fighters in same laterality)
   * create tactical advantages for mid-level attacks as centerlines are more exposed.
   * Mismatched stances (opposite laterality) provide defensive advantages as lead guards
   * naturally protect the centerline.
   * 
   * @param attackerLaterality - Attacker's stance laterality (left or right)
   * @param defenderLaterality - Defender's stance laterality (left or right)
   * @param attackLevel - Attack level: "high", "mid", or "low"
   * @returns Damage multiplier (1.0 = neutral, >1.0 = advantage, <1.0 = disadvantage)
   * 
   * @example
   * ```typescript
   * // Matched stances: attacker gains mid-level advantage
   * const modifier = TrigramCalculator.calculateLateralityModifier("left", "left", "mid");
   * // Returns 1.15 (+15% effectiveness)
   * 
   * // Mismatched stances: defender's guard protects centerline
   * const modifier = TrigramCalculator.calculateLateralityModifier("left", "right", "mid");
   * // Returns 0.90 (-10% effectiveness)
   * ```
   * 
   * @public
   * @korean 측면성수정자계산
   */
  static calculateLateralityModifier(
    attackerLaterality: StanceLaterality,
    defenderLaterality: StanceLaterality,
    attackLevel: "high" | "mid" | "low" = "mid"
  ): number {
    const isMatched = attackerLaterality === defenderLaterality;
 
    // Laterality primarily affects mid-level attacks (centerline attacks)
    if (attackLevel === "mid") {
      // Matched stances: Open centerline = offensive advantage
      // Mismatched stances: Protected centerline = defensive advantage
      return isMatched ? 1.15 : 0.90;
    }
 
    // High and low attacks less affected by laterality
    // Slight tactical variation still exists
    if (attackLevel === "high") {
      return isMatched ? 1.05 : 0.98;
    }
 
    if (attackLevel === "low") {
      return isMatched ? 1.03 : 0.99;
    }
 
    // Default neutral
    return 1.0;
  }
} // end of class
 
export { STANCE_EFFECTIVENESS_MATRIX };