All files / blacktrigram/src/systems/trigram TrigramCalculator.ts

66.35% Statements 71/107
84.61% Branches 11/13
66.66% Functions 4/6
66.35% Lines 71/107

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 1681x           1x     1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x   1x       1x 7x 7x 7x 7x       7x 7x 7x 7x         1x                                           1x                                                               1x 287x 287x 287x   287x 287x 287x 287x         1x   10x 10x 10x   10x 80x 80x 80x 80x 80x 19x 19x 19x 10x   10x 10x         1x 92x 92x 92x 92x 9x 9x     83x     83x 83x 83x   92x         83x 83x 83x 83x     83x 83x 92x 1x      
import { TrigramStance } from "../../types/common";
 
/**
 * 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);
 
    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;
  }
} // end of class
 
export { STANCE_EFFECTIVENESS_MATRIX };