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

93.04% Statements 107/115
85.71% Branches 24/28
87.5% Functions 7/8
93.04% Lines 107/115

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 1781x   1x 1x                         1x 1x 13x     1x   13x         1x 1x 1x         1x 4x 4x 4x 4x 4x 4x 4x 4x     4x 1x 1x 1x 1x 1x 1x 1x 1x     4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     2x 2x 2x 2x 2x 2x 2x 2x   2x 2x 2x 2x 2x 4x         1x 7x 7x 7x 7x 7x     7x 2x 2x     7x 7x 1x 1x     7x       4x 7x         1x 15x 15x 15x 15x 15x 3x 3x   12x 12x 12x 12x 12x 12x     12x 15x 15x   15x 15x 15x 15x 15x 15x 15x 15x 15x 15x         1x 2x   1x 1x     1x 2x   2x 2x         1x             1x  
import { TrigramStance } from "../../types/common";
import { PlayerState } from "../player";
import { PLAYER_ARCHETYPES_DATA } from "../types";
import { TrigramCalculator } from "./TrigramCalculator";
import { TrigramTransitionCost } from "./types";
 
export interface StanceChangeResult {
  readonly success: boolean;
  readonly updatedPlayer: PlayerState;
  readonly cost: TrigramTransitionCost;
  readonly message?: string;
}
 
/**
 * Manager for trigram stance changes and state
 */
export class StanceManager {
  private readonly minTransitionInterval = 500;
  private currentStance?: TrigramStance;
 
  // Fix: Remove constructor parameter requirement
  constructor() {
    // No initialization needed
  }
 
  /**
   * Return the last stance we set (undefined if none yet)
   */
  public getCurrent(): TrigramStance | undefined {
    return this.currentStance;
  }
 
  /**
   * Attempt to change stance
   */
  changeStance(
    player: PlayerState,
    newStance: TrigramStance
  ): StanceChangeResult {
    const cost = this.getStanceTransitionCost(
      player.currentStance,
      newStance,
      player
    );
 
    // Check if transition is possible
    if (!this.canChangeStance(player, newStance)) {
      return {
        success: false,
        updatedPlayer: player,
        cost,
        message:
          "Cannot change stance - insufficient resources or cooldown active",
      };
    }
 
    // Same stance - no cost
    if (player.currentStance === newStance) {
      this.currentStance = newStance;
      return {
        success: true,
        updatedPlayer: {
          ...player,
          lastStanceChangeTime: Date.now(),
        },
        cost: { ki: 0, stamina: 0, timeMilliseconds: 0 }, // Fix: Use timeMilliseconds
      };
    }
 
    // Apply stance change
    const updatedPlayer: PlayerState = {
      ...player,
      currentStance: newStance,
      ki: Math.max(0, player.ki - cost.ki),
      stamina: Math.max(0, player.stamina - cost.stamina),
      lastStanceChangeTime: Date.now(),
    };
    this.currentStance = newStance;
 
    return {
      success: true,
      updatedPlayer,
      cost,
    };
  }
 
  /**
   * Check if player can change to the specified stance
   */
  canChangeStance(player: PlayerState, newStance: TrigramStance): boolean {
    const cost = this.getStanceTransitionCost(
      player.currentStance,
      newStance,
      player
    );
 
    // Check resources
    if (player.ki < cost.ki || player.stamina < cost.stamina) {
      return false;
    }
 
    // Check cooldown
    const timeSinceLastChange = Date.now() - (player.lastStanceChangeTime || 0);
    if (timeSinceLastChange < this.minTransitionInterval) {
      return false;
    }
 
    // Check if player is stunned or incapacitated
    if (player.isStunned || player.consciousness < 50) {
      return false;
    }
 
    return true;
  }
 
  /**
   * Calculate the cost of transitioning between stances
   */
  getStanceTransitionCost(
    fromStance: TrigramStance,
    toStance: TrigramStance,
    player: PlayerState
  ): TrigramTransitionCost {
    if (fromStance === toStance) {
      return { ki: 0, stamina: 0, timeMilliseconds: 0 };
    }
 
    const baseCost = { ki: 10, stamina: 15, timeMilliseconds: 500 };
    const difficulty = TrigramCalculator.calculateTransitionDifficulty(
      fromStance,
      toStance
    );
    const difficultyMultiplier = 1 + difficulty;
 
    // Apply archetype modifiers
    const archetypeData = PLAYER_ARCHETYPES_DATA[player.archetype];
    const favoredStances = archetypeData.favoredStances || [];
    const archetypeModifier = favoredStances.includes(toStance) ? 0.8 : 1.0;
 
    return {
      ki: Math.floor(baseCost.ki * difficultyMultiplier * archetypeModifier),
      stamina: Math.floor(
        baseCost.stamina * difficultyMultiplier * archetypeModifier
      ),
      timeMilliseconds: Math.floor(
        baseCost.timeMilliseconds * difficultyMultiplier
      ),
    };
  }
 
  /**
   * Get optimal stance recommendation
   */
  getOptimalStance(player: PlayerState, opponent?: PlayerState): TrigramStance {
    if (opponent) {
      // Get counter stance against opponent
      return TrigramCalculator.getCounterStance(opponent.currentStance);
    }
 
    // Default to archetype preferred stance
    const archetypeData = PLAYER_ARCHETYPES_DATA[player.archetype];
    const favoredStances = archetypeData.favoredStances || [];
 
    return favoredStances.length > 0 ? favoredStances[0] : TrigramStance.GEON; // Fix: Use enum value
  }
 
  /**
   * Get all valid transitions from current stance
   */
  getValidTransitions(player: PlayerState): TrigramStance[] {
    return Object.values(TrigramStance).filter(
      (
        stance // Fix: Use enum values
      ) => this.canChangeStance(player, stance)
    );
  }
}