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 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 | 34x 34x 78x 78x 78x 78x 78x 78x 78x 78x 317x 317x 317x 317x 317x 317x 15x 317x 317x 317x 317x 317x 317x 317x 317x 9x 317x 317x 317x 317x 317x 317x 317x 636x 636x 636x 566x 70x 40x 40x 40x 30x 16x 16x 16x 14x 14x 636x 318x 320x 320x 320x 318x 318x 7x 7x 7x 7x 7x 311x 9x 9x 9x 9x 9x 302x 21x 21x 21x 2x 2x 21x 2x 2x 21x 281x 4x 277x 2x 2x 2x 2x 1x 1x 1x 1x 1x 34x | /**
* Injury-Based Movement Modifier System
*
* **Korean**: 손상 기반 이동 시스템 (Injury-Based Movement System)
*
* Dynamically calculates movement speed modifiers based on leg injuries, torso
* damage, stance configuration, and pain levels. Part of the 12 combat realism
* systems for authentic Korean martial arts gameplay.
*
* ## Movement Speed Calculation
*
* Base speed is modified by multiple factors:
* - **Leg Injuries**: 0-100% penalty based on health
* - **Torso Injuries**: 0-30% minor penalty
* - **Both Legs Injured**: Additional 20% cumulative penalty
* - **Stance Modifiers**: -20% (defensive) to +25% (offensive)
* - **Pain Overload**: -15% when pain ≥ 80
*
* ## Injury Severity Thresholds
*
* | Leg Health | State | Speed Penalty |
* |-----------|-------|---------------|
* | 70-100% | Normal | 0% |
* | 30-69% | Limping | 0-40% |
* | 10-29% | Severe Limp | 40-80% |
* | 0-9% | Critical | 80-100% |
*
* @module systems/movement/InjuryMovementModifier
* @category Movement System
* @korean 손상기반이동
*/
import type { TrigramStance } from "@/types/common";
import type { BodyPartHealth } from "../bodypart/types";
import { STANCE_SPEED_MODIFIERS } from "../physics/MovementPhysics";
/**
* Configuration for injury-based movement calculations.
*
* **Korean**: 손상 이동 설정
*
* @public
* @category Movement System
*/
export interface InjuryMovementConfig {
/** Leg health thresholds for injury states */
readonly legThresholds: {
readonly normal: number; // 70% - no penalty
readonly limping: number; // 30% - start of severe penalty
readonly critical: number; // 10% - near-maximum penalty
};
/** Maximum torso injury penalty (0.3 = 30%) */
readonly maxTorsoPenalty: number;
/** Both legs injured cumulative penalty multiplier (0.8 = -20%) */
readonly bothLegsInjuredMultiplier: number;
/** Pain overload threshold (80) */
readonly painOverloadThreshold: number;
/** Pain overload penalty multiplier (0.85 = -15%) */
readonly painOverloadMultiplier: number;
/** Minimum speed multiplier (0.1 = 10% of base speed) */
readonly minSpeedMultiplier: number;
}
/**
* Deep partial type for InjuryMovementConfig to support partial overrides.
* Allows callers to override individual threshold values without providing all.
*
* @public
* @category Movement System
*/
export type PartialInjuryMovementConfig = {
readonly legThresholds?: Partial<InjuryMovementConfig["legThresholds"]>;
readonly maxTorsoPenalty?: number;
readonly bothLegsInjuredMultiplier?: number;
readonly painOverloadThreshold?: number;
readonly painOverloadMultiplier?: number;
readonly minSpeedMultiplier?: number;
};
/**
* Default configuration matching acceptance criteria.
*/
export const DEFAULT_INJURY_MOVEMENT_CONFIG: InjuryMovementConfig = {
legThresholds: {
normal: 70,
limping: 30,
critical: 10,
},
maxTorsoPenalty: 0.3,
bothLegsInjuredMultiplier: 0.8,
painOverloadThreshold: 80,
painOverloadMultiplier: 0.85,
minSpeedMultiplier: 0.1,
} as const;
/**
* Result of injury-based movement calculation.
*
* **Korean**: 손상 이동 결과
*
* @public
* @category Movement System
*/
export interface InjuryMovementResult {
/** Final speed in units/second */
readonly finalSpeed: number;
/** Combined speed multiplier (0.1-1.0+) */
readonly speedMultiplier: number;
/** Breakdown of penalty sources */
readonly penalties: {
readonly leftLegPenalty: number;
readonly rightLegPenalty: number;
readonly torsoPenalty: number;
readonly bothLegsInjured: boolean;
readonly stanceModifier: number;
readonly painOverload: boolean;
};
/** Whether player is limping */
readonly isLimping: boolean;
/** Whether player has severe limp */
readonly isSevereLimp: boolean;
/** Bilingual status text */
readonly statusText: {
readonly korean: string;
readonly english: string;
};
}
/**
* Injury-Based Movement Modifier System.
*
* **Korean**: 손상 기반 이동 수정자 시스템
*
* Calculates dynamic movement speed based on injuries, stance, and pain.
* Integrates with MovementPhysics to apply realistic combat trauma effects.
*
* @example
* ```typescript
* const modifier = new InjuryMovementModifier();
*
* const result = modifier.calculateMovementSpeed(
* 5.0, // base speed
* bodyPartHealth, // current health
* TrigramStance.GEON, // current stance
* 65 // pain level
* );
*
* console.log(`Speed: ${result.finalSpeed} m/s`);
* console.log(`Status: ${result.statusText.korean} | ${result.statusText.english}`);
* ```
*
* @public
* @category Movement System
* @korean 손상기반이동수정자
*/
export class InjuryMovementModifier {
private readonly config: InjuryMovementConfig;
/**
* Threshold for determining if both legs are significantly injured.
* Uses epsilon to avoid floating-point rounding issues at boundary.
*
* @private
*/
private static readonly BOTH_LEGS_THRESHOLD = 0.3 + 1e-10;
/**
* Creates a new InjuryMovementModifier with optional configuration.
*
* @param config - Optional configuration overrides (supports partial threshold overrides)
*/
constructor(config?: PartialInjuryMovementConfig) {
// Deep merge leg thresholds to prevent partial overrides from breaking the config.
// Use ?? to avoid undefined values overwriting defaults when exactOptionalPropertyTypes is disabled.
const mergedLegThresholds = {
normal:
config?.legThresholds?.normal ??
DEFAULT_INJURY_MOVEMENT_CONFIG.legThresholds.normal,
limping:
config?.legThresholds?.limping ??
DEFAULT_INJURY_MOVEMENT_CONFIG.legThresholds.limping,
critical:
config?.legThresholds?.critical ??
DEFAULT_INJURY_MOVEMENT_CONFIG.legThresholds.critical,
};
const mergedConfig: InjuryMovementConfig = {
legThresholds: mergedLegThresholds,
maxTorsoPenalty:
config?.maxTorsoPenalty ??
DEFAULT_INJURY_MOVEMENT_CONFIG.maxTorsoPenalty,
bothLegsInjuredMultiplier:
config?.bothLegsInjuredMultiplier ??
DEFAULT_INJURY_MOVEMENT_CONFIG.bothLegsInjuredMultiplier,
painOverloadThreshold:
config?.painOverloadThreshold ??
DEFAULT_INJURY_MOVEMENT_CONFIG.painOverloadThreshold,
painOverloadMultiplier:
config?.painOverloadMultiplier ??
DEFAULT_INJURY_MOVEMENT_CONFIG.painOverloadMultiplier,
minSpeedMultiplier:
config?.minSpeedMultiplier ??
DEFAULT_INJURY_MOVEMENT_CONFIG.minSpeedMultiplier,
};
// Development-mode sanity check: ensure thresholds are finite and ordered
Eif (process.env.NODE_ENV !== "production") {
const { normal, limping, critical } = mergedConfig.legThresholds;
const allFinite =
Number.isFinite(normal) &&
Number.isFinite(limping) &&
Number.isFinite(critical);
const ordered = normal >= limping && limping >= critical;
Iif (!allFinite || !ordered) {
// Non-throwing warning to avoid breaking existing behavior
console.warn(
"[InjuryMovementModifier] Invalid legThresholds configuration detected:",
mergedConfig.legThresholds
);
}
}
this.config = mergedConfig;
}
/**
* Calculate modified movement speed based on all injury factors.
*
* **Korean**: 이동 속도 계산
*
* Applies penalties from:
* 1. Leg injuries (primary factor)
* 2. Torso injuries (minor factor)
* 3. Both legs injured (cumulative penalty)
* 4. Stance modifiers
* 5. Pain overload
*
* @param baseSpeed - Base movement speed (m/s)
* @param bodyPartHealth - Current body part health values
* @param stance - Current trigram stance
* @param painLevel - Current pain level (0-100)
* @returns Complete movement calculation result
*
* @public
* @korean 이동속도계산
*/
public calculateMovementSpeed(
baseSpeed: number,
bodyPartHealth: BodyPartHealth,
stance: TrigramStance,
painLevel: number
): InjuryMovementResult {
// Calculate individual leg penalties
const leftLegPenalty = this.calculateLegPenalty(bodyPartHealth.legLeft);
const rightLegPenalty = this.calculateLegPenalty(bodyPartHealth.legRight);
// Use worst leg penalty as base
const baseLegPenalty = Math.max(leftLegPenalty, rightLegPenalty);
let speedMultiplier = 1.0 - baseLegPenalty;
// Check if both legs are significantly injured using centralized method
const bothLegsInjured = this.areBothLegsInjured(leftLegPenalty, rightLegPenalty);
if (bothLegsInjured) {
// Additional 20% penalty when both legs injured
speedMultiplier *= this.config.bothLegsInjuredMultiplier;
}
// Calculate torso penalty (minor effect, 0-30% max)
const avgTorsoHealth = (bodyPartHealth.torsoUpper + bodyPartHealth.torsoLower) / 2;
// Clamp torso health to prevent negative penalties or exceeding max penalty
const clampedAvgTorsoHealth = Math.min(100, Math.max(0, avgTorsoHealth));
const torsoPenalty = ((100 - clampedAvgTorsoHealth) / 100) * this.config.maxTorsoPenalty;
speedMultiplier *= (1 - torsoPenalty);
// Apply stance modifier
const stanceModifier = this.getStanceSpeedModifier(stance);
speedMultiplier *= stanceModifier;
// Apply pain overload penalty if applicable (>= threshold, not just >)
const painOverload = painLevel >= this.config.painOverloadThreshold;
if (painOverload) {
speedMultiplier *= this.config.painOverloadMultiplier; // -15%
}
// Clamp to minimum speed (10% of base)
speedMultiplier = Math.max(speedMultiplier, this.config.minSpeedMultiplier);
// Calculate final speed
const finalSpeed = baseSpeed * speedMultiplier;
// Determine injury state for status text based on worst leg health
// This ensures status matches the actual movement penalty (which uses worst leg)
// Note: isLimping = true when leg health is in range [30-70) (limping range with 0-40% penalty)
// isSevereLimp = true when leg health is < 30 (severe/critical with 40-100% penalty)
const worstLegHealth = Math.min(bodyPartHealth.legLeft, bodyPartHealth.legRight);
const isLimping =
worstLegHealth < this.config.legThresholds.normal &&
worstLegHealth >= this.config.legThresholds.limping;
const isSevereLimp = worstLegHealth < this.config.legThresholds.limping;
// Generate status text
const statusText = this.generateStatusText(worstLegHealth, bothLegsInjured, painOverload);
return {
finalSpeed,
speedMultiplier,
penalties: {
leftLegPenalty,
rightLegPenalty,
torsoPenalty,
bothLegsInjured,
stanceModifier,
painOverload,
},
isLimping,
isSevereLimp,
statusText,
};
}
/**
* Calculate movement penalty from leg injury severity.
*
* **Korean**: 다리 부상 페널티 계산
*
* Progressive penalty scaling:
* - 100-70%: No penalty (0%)
* - 70-30%: Linear scaling (0-40%)
* - 30-10%: Accelerated scaling (40-80%)
* - 10-0%: Critical scaling (80-100%)
*
* @param legHealth - Leg health (0-100)
* @returns Penalty factor (0.0-1.0), clamped to valid range
*
* @private
*/
private calculateLegPenalty(legHealth: number): number {
const { normal, limping, critical } = this.config.legThresholds;
// Clamp input health to valid range to prevent runaway penalties
const clampedHealth = Math.min(Math.max(legHealth, 0), 100);
let penalty: number;
if (clampedHealth >= normal) {
// 70-100%: No penalty
penalty = 0;
} else if (clampedHealth >= limping) {
// 30-70%: 0-40% penalty (linear)
const healthRange = normal - limping;
// Guard against division by zero if thresholds are misconfigured
const healthFactor = healthRange > 0
? (normal - clampedHealth) / healthRange
: 0;
penalty = healthFactor * 0.4;
} else if (clampedHealth >= critical) {
// 10-30%: 40-80% penalty (accelerated)
const healthRange = limping - critical;
// Guard against division by zero if thresholds are misconfigured
const healthFactor = healthRange > 0
? (limping - clampedHealth) / healthRange
: 0;
penalty = 0.4 + (healthFactor * 0.4);
} else {
// 0-10%: 80-100% penalty (critical)
// Guard against division by zero if critical is 0
const healthFactor = critical > 0
? (critical - clampedHealth) / critical
: 1;
penalty = 0.8 + (healthFactor * 0.2);
}
// Ensure returned penalty is always within [0, 1]
return Math.min(Math.max(penalty, 0), 1);
}
/**
* Checks if both legs are significantly injured (>30% penalty each).
* Uses epsilon-based comparison for floating-point robustness.
*
* @param leftLegPenalty - Penalty for left leg (0-1)
* @param rightLegPenalty - Penalty for right leg (0-1)
* @returns True if both legs are significantly injured
* @private
*/
private areBothLegsInjured(leftLegPenalty: number, rightLegPenalty: number): boolean {
return leftLegPenalty >= InjuryMovementModifier.BOTH_LEGS_THRESHOLD &&
rightLegPenalty >= InjuryMovementModifier.BOTH_LEGS_THRESHOLD;
}
/**
* Get stance-based speed modifier.
*
* **Korean**: 자세 속도 배수 가져오기
*
* @param stance - Current trigram stance
* @returns Speed multiplier (0.8-1.25), defaults to 1.0 for unknown stances
*
* @public
*/
public getStanceSpeedModifier(stance: TrigramStance): number {
const modifier = STANCE_SPEED_MODIFIERS[stance];
// Defensive fallback for unknown stances (e.g., test/invalid values)
Iif (modifier == null) {
// Runtime-safe environment check (works in both Node and browser/Vite environments)
if (
typeof process !== "undefined" &&
process.env?.NODE_ENV === "development"
) {
console.warn(
`[InjuryMovementModifier] Unknown stance "${String(
stance
)}" received in getStanceSpeedModifier; defaulting to 1.0.`
);
}
return 1.0;
}
return modifier;
}
/**
* Generate bilingual status text based on injury state.
*
* **Korean**: 상태 텍스트 생성
*
* @param legHealthForStatus - Leg health percentage used for status determination (typically worst leg)
* @param bothLegsInjured - Whether both legs are significantly injured
* @param painOverload - Whether pain is over threshold
* @returns Bilingual status text
*
* @private
*/
private generateStatusText(
legHealthForStatus: number,
bothLegsInjured: boolean,
painOverload: boolean
): { korean: string; english: string } {
const { normal, limping, critical } = this.config.legThresholds;
if (legHealthForStatus < critical) {
const painText = painOverload ? " | 고통 과부하" : "";
const painTextEn = painOverload ? " | Pain Overload" : "";
const baseKorean = bothLegsInjured ? "심각한 부상 | 양 다리" : "심각한 부상";
const baseEnglish = bothLegsInjured ? "Critical Injury | Both Legs" : "Critical Injury";
return {
korean: `${baseKorean}${painText}`,
english: `${baseEnglish}${painTextEn}`,
};
} else if (legHealthForStatus < limping) {
const painText = painOverload ? " | 고통 과부하" : "";
const painTextEn = painOverload ? " | Pain Overload" : "";
const baseKorean = bothLegsInjured ? "중증 절름거림 | 양 다리" : "중증 절름거림";
const baseEnglish = bothLegsInjured ? "Severe Limping | Both Legs" : "Severe Limping";
return {
korean: `${baseKorean}${painText}`,
english: `${baseEnglish}${painTextEn}`,
};
} else if (legHealthForStatus < normal) {
const statusKrParts = ["절름거림"];
const statusEnParts = ["Limping"];
if (bothLegsInjured) {
statusKrParts.push("양 다리");
statusEnParts.push("Both Legs");
}
if (painOverload) {
statusKrParts.push("고통 과부하");
statusEnParts.push("Pain Overload");
}
return {
korean: statusKrParts.join(" | "),
english: statusEnParts.join(" | "),
};
} else if (painOverload) {
return {
korean: "고통 과부하",
english: "Pain Overload",
};
} else {
return {
korean: "정상",
english: "Normal",
};
}
}
/**
* Check if player should display limping animation.
*
* **Korean**: 절름거림 확인
*
* Uses worst leg health to match movement penalty behavior.
*
* @param bodyPartHealth - Current body part health
* @returns True if limping animation should play
*
* @public
*/
public shouldLimp(bodyPartHealth: BodyPartHealth): boolean {
const worstLegHealth = Math.min(bodyPartHealth.legLeft, bodyPartHealth.legRight);
return worstLegHealth < this.config.legThresholds.normal;
}
/**
* Check if player has severe limp.
*
* **Korean**: 중증 절름거림 확인
*
* Uses worst leg health to match movement penalty behavior.
*
* @param bodyPartHealth - Current body part health
* @returns True if severe limp (leg health < 30%)
*
* @public
*/
public hasSevereLimp(bodyPartHealth: BodyPartHealth): boolean {
const worstLegHealth = Math.min(bodyPartHealth.legLeft, bodyPartHealth.legRight);
return worstLegHealth < this.config.legThresholds.limping;
}
/**
* Get current injury state description.
*
* **Korean**: 부상 상태 설명
*
* Uses worst leg health to match movement penalty behavior.
*
* @param bodyPartHealth - Current body part health
* @returns Bilingual injury description
*
* @public
*/
public getInjuryDescription(bodyPartHealth: BodyPartHealth): {
korean: string;
english: string;
} {
const worstLegHealth = Math.min(bodyPartHealth.legLeft, bodyPartHealth.legRight);
const leftLegPenalty = this.calculateLegPenalty(bodyPartHealth.legLeft);
const rightLegPenalty = this.calculateLegPenalty(bodyPartHealth.legRight);
const bothLegsInjured = this.areBothLegsInjured(leftLegPenalty, rightLegPenalty);
return this.generateStatusText(worstLegHealth, bothLegsInjured, false);
}
}
/**
* Singleton instance for global access.
*
* @public
*/
export const injuryMovementModifier = new InjuryMovementModifier();
|