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 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 | 23x 236x 267x 1x 266x 1x 265x 1x 264x 1x 263x 263x 263x 263x 263x 150x 150x 113x 9x 3x 6x 6x 6x 1x 6x 6x 6x 1x 5x 5x 5x 4x 1x 3x 1x 2x 2x 2x 2x 2x 263x 263x 163x 100x 263x 3x 3x 253x 253x 1x 1x 1x 1x 2x 2x 3x 3x 263x 263x 263x 263x 263x 163x 263x 263x 263x 3x 3x 3x 3x 2x 2x 263x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 150x 150x 2x 2x 144x 144x 1x 1x 1x 1x 1x 1x 1x 1x 150x 150x 150x 4x 1x 3x 1x 2x 2x 2x 3x 3x 1x 2x 2x 8x 8x 4x 4x 5x 5x | /**
* Grappling System for Korean Martial Arts Combat
*
* **Korean**: 잡기 시스템 (Grapple System)
*
* Implements realistic grappling and takedown mechanics based on:
* - **Hapkido (합기도)**: Joint locks and control techniques
* - **Ssireum (씨름)**: Traditional Korean wrestling
* - **GON Stance (곤괘)**: Earth stance grounding and control
*
* ## Core Mechanics
*
* 1. **Grab Initiation**: Establish grip on opponent's limb/body
* 2. **Control Duration**: Maintain control with stamina cost
* 3. **Grip Strength**: Affects escape difficulty and follow-up options
* 4. **Escape Mechanics**: Based on strength, technique, and stamina
* 5. **Follow-up Techniques**: Throws, takedowns, joint locks from control
*
* ## Phase 2 Enhancement
*
* Enhanced with Gon (Earth) trigram metadata integration:
* - Uses `controlDuration` from ExtendedGonTechnique
* - Supports authentic Ssireum/Hapkido control durations
* - Maintains backward compatibility with non-Gon techniques
*
* @module systems/combat/GrappleSystem
* @category Combat System
* @korean 잡기시스템
*/
import type { PlayerState } from "@/systems/player";
import {
CombatState,
GrappleControl,
GrappleState,
GrappleTarget,
TrigramStance,
} from "@/types";
import { asExtendedGonTechnique } from "../trigram/types/GonTechniqueExtensions";
import type { KoreanTechnique } from "../vitalpoint/types";
/**
* Configuration for grappling mechanics.
*
* **Korean**: 잡기 설정 (Grapple Configuration)
*/
export interface GrappleConfig {
/** Base stamina cost per second to maintain control */
readonly baseStaminaCostPerSecond: number;
/** Minimum grip strength to maintain control */
readonly minGripStrength: number;
/** Maximum grip strength value */
readonly maxGripStrength: number;
/** Base escape difficulty multiplier */
readonly baseEscapeDifficulty: number;
/** Stamina cost to attempt escape */
readonly escapeStaminaCost: number;
/** Minimum duration before escape is possible (ms) */
readonly minControlDuration: number;
}
/**
* Default grappling configuration values.
*/
export const DEFAULT_GRAPPLE_CONFIG: GrappleConfig = {
baseStaminaCostPerSecond: 5,
minGripStrength: 20,
maxGripStrength: 100,
baseEscapeDifficulty: 1.5,
escapeStaminaCost: 15,
minControlDuration: 500, // 0.5 seconds
};
/**
* Grapple attempt result.
*
* **Korean**: 잡기 시도 결과 (Grapple Attempt Result)
*/
export interface GrappleAttemptResult {
/** Whether the grapple was successful */
readonly success: boolean;
/** New grapple control state if successful */
readonly grappleControl?: GrappleControl;
/** Reason for failure if unsuccessful */
readonly reason?: string;
/** Stamina cost for the attempt */
readonly staminaCost: number;
}
/**
* Escape attempt result.
*
* **Korean**: 탈출 시도 결과 (Escape Attempt Result)
*/
export interface EscapeAttemptResult {
/** Whether the escape was successful */
readonly success: boolean;
/** Updated grapple control state */
readonly grappleControl: GrappleControl | null;
/** Reason for failure if unsuccessful */
readonly reason?: string;
/** Stamina cost for the attempt */
readonly staminaCost: number;
}
/**
* Grappling System for control and hold mechanics.
*
* **Korean**: 잡기 시스템 (Grapple System)
*
* Manages grappling state between combatants, including grip strength,
* control duration, escape mechanics, and follow-up techniques.
*/
export class GrappleSystem {
private readonly config: GrappleConfig;
constructor(config: Partial<GrappleConfig> = {}) {
this.config = { ...DEFAULT_GRAPPLE_CONFIG, ...config };
}
/**
* Attempt to initiate a grapple on target.
*
* **Korean**: 잡기 시도 (Attempt Grapple)
*
* @param attacker - Player attempting the grapple
* @param defender - Target player
* @param target - Body part to grapple
* @param currentTime - Current game time in milliseconds
* @returns Result of the grapple attempt
*/
attemptGrapple(
attacker: PlayerState,
defender: PlayerState,
target: GrappleTarget,
currentTime: number
): GrappleAttemptResult {
// Check if attacker can grapple
if (attacker.combatState === CombatState.STUNNED) {
return {
success: false,
reason: "Attacker is stunned",
staminaCost: 0,
};
}
// Check if attacker is already grappling someone else
if (attacker.combatState === CombatState.GRAPPLING) {
return {
success: false,
reason: "Attacker is already grappling another opponent",
staminaCost: 0,
};
}
if (attacker.stamina < this.config.escapeStaminaCost) {
return {
success: false,
reason: "Insufficient stamina",
staminaCost: 0,
};
}
// Check if defender is already being grappled
if (defender.combatState === CombatState.GRAPPLED) {
return {
success: false,
reason: "Target is already being grappled",
staminaCost: 0,
};
}
// Calculate grip strength based on attacker attributes and stance
const gripStrength = this.calculateGripStrength(attacker, target);
// Calculate success chance based on attributes
const successChance = this.calculateGrappleSuccessChance(
attacker,
defender,
target
);
const roll = Math.random();
const success = roll < successChance;
if (success) {
// Create grapple control - start with GRABBING state during initiation
const grappleControl: GrappleControl = {
state: GrappleState.GRABBING,
target,
controllerId: attacker.id,
targetId: defender.id,
gripStrength,
duration: 0,
startTime: currentTime,
canEscape: false, // Initially cannot escape
staminaCostPerSecond: this.calculateStaminaCost(target, gripStrength),
};
return {
success: true,
grappleControl,
staminaCost: this.config.escapeStaminaCost,
};
}
return {
success: false,
reason: `Grapple attempt failed (${Math.round(successChance * 100)}% chance)`,
staminaCost: this.config.escapeStaminaCost * 0.5, // Half cost on failure
};
}
/**
* Update grapple control state over time.
*
* **Korean**: 잡기 업데이트 (Update Grapple)
*
* @param grappleControl - Current grapple control state
* @param controller - Player maintaining control
* @param target - Player being controlled
* @param deltaTime - Time elapsed since last update (seconds)
* @param currentTime - Current game time in milliseconds
* @returns Updated grapple control or null if broken
*/
updateGrapple(
grappleControl: GrappleControl,
controller: PlayerState,
_target: PlayerState,
deltaTime: number,
currentTime: number
): GrappleControl | null {
// Check if controller can maintain control
if (
controller.stamina < grappleControl.staminaCostPerSecond * deltaTime ||
controller.combatState === CombatState.STUNNED
) {
// Control broken due to stamina or state
return null;
}
// Update duration
const newDuration = currentTime - grappleControl.startTime;
// Transition from GRABBING to CONTROLLING after initial establishment
let newState = grappleControl.state;
if (grappleControl.state === GrappleState.GRABBING && newDuration > 0) {
newState = GrappleState.CONTROLLING;
}
// Decay grip strength slightly over time
const gripDecayRate = 2; // points per second
const rawGripStrength = grappleControl.gripStrength - gripDecayRate * deltaTime;
// Check if grip strength has dropped too low
if (rawGripStrength < this.config.minGripStrength) {
return null;
}
const newGripStrength = rawGripStrength;
// Allow escape attempts after minimum duration
const canEscape = newDuration >= this.config.minControlDuration;
return {
...grappleControl,
state: newState,
gripStrength: newGripStrength,
duration: newDuration,
canEscape,
};
}
/**
* Attempt to escape from a grapple.
*
* **Korean**: 탈출 시도 (Attempt Escape)
*
* @param grappleControl - Current grapple control state
* @param controller - Player maintaining control
* @param target - Player attempting to escape
* @returns Result of the escape attempt
*/
attemptEscape(
grappleControl: GrappleControl,
controller: PlayerState,
target: PlayerState
): EscapeAttemptResult {
// Check if escape is possible
if (!grappleControl.canEscape) {
return {
success: false,
grappleControl,
reason: "Cannot escape yet",
staminaCost: 0,
};
}
// Check stamina
if (target.stamina < this.config.escapeStaminaCost) {
return {
success: false,
grappleControl,
reason: "Insufficient stamina",
staminaCost: 0,
};
}
// Calculate escape chance
const escapeChance = this.calculateEscapeChance(
grappleControl,
controller,
target
);
const roll = Math.random();
const success = roll < escapeChance;
Eif (success) {
return {
success: true,
grappleControl: null, // Escape successful, control broken
staminaCost: this.config.escapeStaminaCost,
};
}
// Escape failed, weaken grip slightly
const weakenedGripStrength = Math.max(
this.config.minGripStrength,
grappleControl.gripStrength - 10
);
return {
success: false,
grappleControl: {
...grappleControl,
gripStrength: weakenedGripStrength,
},
reason: `Escape failed (${Math.round(escapeChance * 100)}% chance)`,
staminaCost: this.config.escapeStaminaCost,
};
}
/**
* Calculate grip strength based on attacker attributes and target.
*
* **Korean**: 악력 계산 (Calculate Grip Strength)
*
* @private
*/
private calculateGripStrength(
attacker: PlayerState,
target: GrappleTarget
): number {
// Base strength from attack power
let strength = attacker.attackPower * 3;
// Stance modifiers
if (attacker.currentStance === TrigramStance.GON) {
strength *= 1.3; // GON stance excels at grappling
I} else if (attacker.currentStance === TrigramStance.GAN) {
strength *= 1.15; // GAN stance is also good for control
}
// Target modifiers
switch (target) {
case GrappleTarget.HAND:
strength *= 1.1; // Easier to control hand
break;
case GrappleTarget.ARM:
strength *= 1.0; // Standard difficulty
break;
case GrappleTarget.LEG:
strength *= 0.9; // Harder to control leg
break;
case GrappleTarget.TORSO:
strength *= 0.85; // Harder to control torso
break;
case GrappleTarget.NECK:
strength *= 1.2; // High control but risky
break;
case GrappleTarget.BOTH_ARMS:
strength *= 0.7; // Very hard to control both arms
break;
}
return Math.min(this.config.maxGripStrength, strength);
}
/**
* Calculate grapple success chance.
*
* **Korean**: 잡기 성공률 계산 (Calculate Grapple Success Chance)
*
* @private
*/
private calculateGrappleSuccessChance(
attacker: PlayerState,
defender: PlayerState,
target: GrappleTarget
): number {
// Base chance from technique vs defense
const attributeDiff = attacker.technique - defender.defense;
let baseChance = 0.5 + attributeDiff * 0.02; // ±2% per point difference
// Speed advantage
Iif (attacker.speed > defender.speed) {
baseChance += (attacker.speed - defender.speed) * 0.01;
}
// Stance modifiers
if (attacker.currentStance === TrigramStance.GON) {
baseChance += 0.15; // GON stance excels at grappling
}
Iif (defender.combatState === CombatState.ATTACKING) {
baseChance += 0.2; // Easier to grapple during attack
I} else if (defender.combatState === CombatState.DEFENDING) {
baseChance -= 0.15; // Harder to grapple defensive opponent
}
// Target difficulty
switch (target) {
case GrappleTarget.HAND:
baseChance += 0.1; // Easier to grab hand
break;
case GrappleTarget.BOTH_ARMS:
baseChance -= 0.2; // Much harder to grab both arms
break;
case GrappleTarget.NECK:
baseChance -= 0.1; // Risky target
break;
}
// Clamp between 0.05 and 0.95
return Math.max(0.05, Math.min(0.95, baseChance));
}
/**
* Calculate escape chance from grapple.
*
* **Korean**: 탈출 성공률 계산 (Calculate Escape Chance)
*
* @private
*/
private calculateEscapeChance(
grappleControl: GrappleControl,
controller: PlayerState,
target: PlayerState
): number {
// Base chance from strength vs grip
const gripFactor = grappleControl.gripStrength / this.config.maxGripStrength;
let baseChance = 0.5 - gripFactor * 0.3; // Stronger grip = harder escape
// Attribute comparison
const strengthDiff = target.attackPower - controller.attackPower;
baseChance += strengthDiff * 0.02;
// Speed advantage helps escape
Eif (target.speed > controller.speed) {
baseChance += (target.speed - controller.speed) * 0.015;
}
// Technique helps find weaknesses in grip
baseChance += target.technique * 0.005;
// Stance modifiers - certain stances are better at escaping grapples
// **Korean**: 곤(坤) 자세는 탈출에 유리 (+30%), 간(艮) 자세는 방어적인 탈출 (+15%)
Iif (target.currentStance === TrigramStance.GON) {
baseChance += 0.3; // GON stance excels at grounding and escape
I} else if (target.currentStance === TrigramStance.GAN) {
baseChance += 0.15; // GAN stance has defensive escape advantage
}
// Time factor - longer control makes escape harder
const durationSeconds = grappleControl.duration / 1000;
const timePenalty = Math.min(0.2, durationSeconds * 0.02);
baseChance -= timePenalty;
// Clamp between 0.05 and 0.85
return Math.max(0.05, Math.min(0.85, baseChance));
}
/**
* Calculate stamina cost to maintain control.
*
* **Korean**: 체력 소모 계산 (Calculate Stamina Cost)
*
* @private
*/
private calculateStaminaCost(
target: GrappleTarget,
gripStrength: number
): number {
let cost = this.config.baseStaminaCostPerSecond;
// Target affects stamina cost
switch (target) {
case GrappleTarget.HAND:
cost *= 0.8; // Easier to maintain
break;
case GrappleTarget.ARM:
cost *= 1.0; // Standard
break;
case GrappleTarget.LEG:
cost *= 1.2; // Harder to maintain
break;
case GrappleTarget.TORSO:
cost *= 1.3; // Much harder to maintain
break;
case GrappleTarget.NECK:
cost *= 1.5; // Very demanding
break;
case GrappleTarget.BOTH_ARMS:
cost *= 1.8; // Extremely demanding
break;
}
// Higher grip strength costs more stamina
const gripFactor = gripStrength / this.config.maxGripStrength;
cost *= 0.7 + gripFactor * 0.6; // 70-130% based on grip
return cost;
}
/**
* Check if a grapple can be transitioned to a throw.
*
* **Korean**: 던지기 전환 확인 (Check Throw Transition)
*
* @param grappleControl - Current grapple control state
* @param controller - Player attempting throw
* @returns Whether throw transition is possible
*/
canTransitionToThrow(
grappleControl: GrappleControl,
controller: PlayerState
): boolean {
// Need sufficient control duration
if (grappleControl.duration < 1000) {
return false; // Need at least 1 second of control
}
// Need sufficient grip strength
if (grappleControl.gripStrength < 60) {
return false;
}
// Need sufficient stamina for throw
Iif (controller.stamina < 20) {
return false;
}
// Certain targets are better for throws
const goodThrowTargets = [
GrappleTarget.ARM,
GrappleTarget.TORSO,
GrappleTarget.BOTH_ARMS,
];
return goodThrowTargets.includes(grappleControl.target);
}
/**
* Check if a grapple can be transitioned to a joint lock.
*
* **Korean**: 관절기 전환 확인 (Check Joint Lock Transition)
*
* @param grappleControl - Current grapple control state
* @param controller - Player attempting lock
* @returns Whether joint lock transition is possible
*/
canTransitionToJointLock(
grappleControl: GrappleControl,
controller: PlayerState
): boolean {
// Need sufficient control duration
Iif (grappleControl.duration < 800) {
return false;
}
// Need high technique for joint locks
if (controller.technique < 10) {
return false;
}
// Joint locks work on limbs
const lockTargets = [
GrappleTarget.HAND,
GrappleTarget.ARM,
GrappleTarget.LEG,
];
return lockTargets.includes(grappleControl.target);
}
/**
* Get technique-specific control duration from Gon metadata.
*
* **Korean**: 기술별 제어 시간 조회 (Get Technique Control Duration)
*
* Retrieves the post-throw positional advantage duration from
* ExtendedGonTechnique metadata. Falls back to default duration
* if technique doesn't have Gon-specific enhancements.
*
* **Control Duration Philosophy**:
* - Traditional Ssireum techniques (800-2000ms) provide longer control
* - Aggressive slam techniques (800-1200ms) provide brief control
* - Sacrifice throws (1500-2000ms) provide extended ground control
*
* **Use Cases**:
* - Determining follow-up attack windows after throws
* - Calculating defender recovery time before counterattack
* - Applying post-throw positional advantage in game state
*
* @param technique - Korean martial arts technique (checks for ExtendedGonTechnique)
* @param defaultDuration - Fallback duration if technique has no metadata (default: 1000ms)
* @returns Control duration in milliseconds
*
* @example
* ```typescript
* const controlTime = grappleSystem.getTechniqueControlDuration(
* ssireumThrowTechnique, // controlDuration: 1800
* 1000 // default fallback
* );
* // Result: 1800ms (uses technique metadata)
* ```
*
* @public
* @korean 기술별제어시간조회
*/
getTechniqueControlDuration(
technique: KoreanTechnique,
defaultDuration: number = 1000,
): number {
// Use helper function for safe type casting
const gonTechnique = asExtendedGonTechnique(technique);
if (!gonTechnique) {
// Non-Gon technique: use default duration
return defaultDuration;
}
// Extract control duration from validated Gon technique
return gonTechnique.controlDuration;
}
/**
* Apply post-throw control advantage window.
*
* **Korean**: 던지기 후 우세 적용 (Apply Post-Throw Advantage)
*
* Creates a control advantage state after a successful throw,
* using the technique's `controlDuration` to determine how long
* the attacker maintains positional dominance.
*
* **Advantage Effects**:
* - Attacker gains priority for follow-up attacks
* - Defender must wait for control duration to expire
* - Applies to throw transitions from grapple control
*
* @param technique - Throw technique executed
* @param attackerId - Player who executed throw
* @param defenderId - Player who was thrown
* @param currentTime - Current game time in milliseconds
* @returns Control advantage state with duration from technique metadata
*
* @example
* ```typescript
* const advantage = grappleSystem.applyPostThrowAdvantage(
* ssireumThrowTechnique,
* "player1",
* "player2",
* Date.now()
* );
* // advantage.duration = 1800ms (from technique metadata)
* ```
*
* @public
* @korean 던지기후우세적용
*/
applyPostThrowAdvantage(
technique: KoreanTechnique,
attackerId: string,
defenderId: string,
currentTime: number,
): {
controllerId: string;
targetId: string;
duration: number;
startTime: number;
endTime: number;
} {
// Get technique-specific control duration with 1200ms fallback
const controlDuration = this.getTechniqueControlDuration(technique, 1200);
return {
controllerId: attackerId,
targetId: defenderId,
duration: controlDuration,
startTime: currentTime,
endTime: currentTime + controlDuration,
};
}
}
export default GrappleSystem;
|