// Creature-by-creature combat metrics compatible with rust_base/monsters. // Quirks from the reference implementation are intentionally isolated here so // a future corrected model can coexist without changing report presentation. #import "../generation/creature-data.typ": master-creatures, player-balancing-rows #let compatibility-model = "rust_base" #let clamp(value, minimum, maximum) = calc.min(calc.max(value, minimum), maximum) #let number(value, fallback: 0) = if value == none or value == "" { fallback } else { int(str(value)) } #let tagged-number(value, suffix) = if value == none or value == "" { 0 } else { let cleaned = str(value).replace(suffix, "").trim() if cleaned.match(regex("^-?\\d+$")) == none { 0 } else { int(cleaned) } } #let damage-vector(value) = if value == none or value == "" { () } else { let cleaned = str(value).trim() // Rust collects parsed segments through Option>: one malformed // segment invalidates the complete damage profile instead of aborting. if cleaned.match(regex("^-?\\d+(?:/-?\\d+)*$")) == none { () } else { cleaned.split("/").map(int) } } #let category-or(value, fallback) = if value == none or value == "" { fallback } else { str(value) } #let binomial-distribution(threshold, dice) = { if dice <= 0 { return ((successes: 0, probability: 1.0),) } let probability = clamp(threshold / 10, 0.0000000001, 0.9999999999) range(dice + 1).map(successes => ( successes: successes, probability: calc.binom(dice, successes) * calc.pow(probability, successes) * calc.pow(1 - probability, dice - successes), )) } #let damage-at-successes(profile, successes) = { if successes <= 0 or profile.len() == 0 { 0 } else { profile.at(clamp(successes - 1, 0, profile.len() - 1)) } } #let expected-damage(profile, threshold, dice) = binomial-distribution(threshold, dice).map(item => ( damage-at-successes(profile, item.successes) * item.probability )).sum() #let resistance-value(value) = if value == none { 0 } else { str(value).clusters().map(char => if char == "+" { 2 } else if char == "-" { -2 } else { 0 }).sum() } #let worst-physical-resistance(part) = calc.min( resistance-value(part.slash), resistance-value(part.blunt), resistance-value(part.piercing), ) #let safe-ratio(numerator, denominator) = if denominator == 0 { none } else { numerator / denominator } #let safe-sum(values) = if none in values { none } else { values.sum() } #let rust-ratio(numerator, denominator) = if denominator == 0 and numerator == 0 { (kind: "nan", value: none) } else if denominator == 0 { (kind: "infinite", value: none) } else { (kind: "finite", value: numerator / denominator) } #let rust-sum(values) = if values.any(item => item.kind == "nan") { "NaN" } else if values.any(item => item.kind == "infinite") { none } else { values.map(item => item.value).sum() } #let damage-category(percent) = { if percent < 0.01 { "None" } else if percent < 0.25 { "Chip" } else if percent < 0.5 { "Moderate" } else if percent < 0.8 { "High" } else if percent < 1.0 { "NearDeath" } else if percent < 1.5 { "Kill" } else { "Overkill" } } // The overlapping 0.5..0.9 and 0.8..1.2 Rust ranges resolve at 0.9 because // match arms are evaluated in order. #let armor-category(percent) = { if percent < 0.01 { "None" } else if percent < 0.3 { "Chip" } else if percent < 0.5 { "Moderate" } else if percent < 0.9 { "High" } else if percent < 1.2 { "FullBreak" } else { "Overkill" } } #let profile-damage-to-part(part, player, parried, phase, creature-strength) = { let threshold = part.at("threshold_" + phase) + player.precision let protection = part.at("protection_" + phase) let resistance = worst-physical-resistance(part) // Rust uses 1..max_successes_off (exclusive upper bound). let damage-profile = range(1, player.max_successes_off).map(value => int(value * player.damage_per_s)) let dice = clamp(player.successes_off - parried, 0, player.successes_off) let damage-a = calc.max(expected-damage(damage-profile, threshold, dice) - resistance, 0) let damage-b1 = calc.max(expected-damage(damage-profile, threshold, calc.floor(dice / 2)) - resistance, 0) let damage-b2 = calc.max(expected-damage(damage-profile, threshold, dice - calc.floor(dice / 2)) - resistance, 0) let through-a = calc.max(damage-a - protection - creature-strength, 0) let through-b1 = calc.max(damage-b1 - protection - creature-strength, 0) let through-b2 = calc.max(damage-b2 - protection - creature-strength, 0) let ab-a = clamp(damage-a - protection, 0, player.armor_break) let ab-b1 = clamp(damage-b1 - protection, 0, player.armor_break) let ab-b2 = clamp(damage-b2 - protection, 0, player.armor_break) ( hp: calc.max(damage-a, damage-b1 + damage-b2), armor: calc.max(ab-a, ab-b1 + ab-b2), through: calc.max(through-a, through-b1 + through-b2), ) } #let scaled-damage(value, factor) = ( hp: value.hp * factor, armor: value.armor * factor, through: value.through * factor, ) #let add-damage(left, right) = ( hp: left.hp + right.hp, armor: left.armor + right.armor, through: left.through + right.through, ) #let player-profiles(profile-hse) = player-balancing-rows.filter(row => row.profile_hse == profile-hse) #let player-profile(profile-hse, stance) = player-profiles(profile-hse).find(row => row.profile_stance == stance) #let defense-threshold(attack, player) = calc.max( number(attack.threshold_parry) + player.specialty_parry, number(attack.threshold_dodge) + player.specialty_dodge, ) #let defense-row(creature, part, stance, showcase) = { let balancing = creature.raw.balancing.first() let player = player-profile(balancing.profile_hse, stance) let remaining-defense = balancing.average_defensive_successes let total-p1 = (hp: 0.0, armor: 0.0, through: 0.0) let total-p2 = (hp: 0.0, armor: 0.0, through: 0.0) let remaining-players = balancing.player_groupsize while remaining-players > 0 { let chunk = calc.min(remaining-players, 1.0) let parried = calc.min(remaining-defense, player.successes_off) let p1 = profile-damage-to-part(part, player, parried, "p1", creature.stats.strength) let p2 = profile-damage-to-part(part, player, parried, "p2", creature.stats.strength) total-p1 = add-damage(total-p1, scaled-damage(p1, chunk)) total-p2 = add-damage(total-p2, scaled-damage(p2, chunk)) remaining-defense = calc.max(remaining-defense - parried, 0) remaining-players -= chunk } let time-p1 = safe-ratio(part.life_points_p1, total-p1.hp) let time-p2 = safe-ratio(part.life_points_p2, total-p2.hp) let through-p1 = calc.max(total-p1.through - creature.stats.strength, 0) let through-p2 = calc.max(total-p2.through - creature.stats.strength, 0) ( part: part.body_part_name, stance: stance, showcase: showcase, armor-per-turn: total-p1.armor, integrity-p1: part.integrity_p1, time-armor: safe-ratio(part.integrity_p1, total-p1.armor), target-armor: balancing.time_to_armorbreak, damage-p1: total-p1.hp, health-p1: part.life_points_p1, time-p1: time-p1, target-p1: balancing.time_to_p2, damage-p2: total-p2.hp, health-p2: part.life_points_p2, time-p2: time-p2, target-p2: balancing.time_to_kill, through-p1: total-p1.through, through-p2: total-p2.through, time-through: rust-sum(( rust-ratio(part.life_points_p1, through-p1), rust-ratio(part.life_points_p2, through-p2), )), ) } #let creature-defense-report(creature, show-extra: true) = creature.raw.body-parts.map(part => { let stances = if show-extra { ((stance: "Neutral", showcase: "Display"), (stance: "Defensive", showcase: "Balancing"), (stance: "Offensive", showcase: "Balancing")) } else { ((stance: "Neutral", showcase: "Display"),) } stances.map(item => defense-row(creature, part, item.stance, item.showcase)) }).flatten() #let attack-report(attack, player, showcase) = { // Compatibility quirk: Best and Worst both use max(parry, dodge). let threshold = defense-threshold(attack, player) let successes = tagged-number(attack.success, "r") let distribution = binomial-distribution(threshold, player.successes_def) let successes-left = distribution.map(item => (successes - item.successes) * item.probability).sum() let damage = damage-vector(attack.damage_regular) let incoming = distribution.map(item => ( damage-at-successes(damage, successes - item.successes) * item.probability )).sum() let tanked = calc.max(incoming - player.protection - player.strength, 0) let armor-loss = clamp(incoming - player.protection, 0, number(attack.armor_break)) ( attack-id: attack.attack_id, name: attack.name, attack-type: category-or(attack.attack_type, "—"), phase: attack.phase, stance: player.profile_stance, showcase: showcase, incoming: incoming, fatigue: tagged-number(attack.damage_fatigue, "F"), successes-left: successes-left, threshold: threshold, attack-successes: successes, tanked: tanked, health: player.health, damage-category: damage-category(tanked / player.health), damage-expected: { let expected = category-or(attack.target_damage_category, "Malformed") if expected in ("Malformed", "None", "Chip", "Moderate", "High", "NearDeath", "Kill", "Overkill") { expected } else { "Malformed" } }, armor-loss: armor-loss, integrity: player.integrity, armor-category: armor-category(armor-loss / player.integrity), armor-expected: { let expected = category-or(attack.target_ab_category, "Malformed") if expected in ("Malformed", "None", "Chip", "Moderate", "High", "FullBreak", "Overkill") { expected } else { "Malformed" } }, ) } #let creature-attack-report(creature, show-extra: true, show-balanced: true) = { let balancing = creature.raw.balancing.first() let profiles = player-profiles(balancing.profile_hse) creature.raw.abilities.filter(attack => damage-vector(attack.damage_regular).len() > 0).map(attack => { let target-stance = category-or(attack.target_stance, "Neutral") let target = profiles.find(player => player.profile_stance == target-stance) let balancing-row = attack-report(attack, target, "Balancing") let rows = if show-balanced or balancing-row.damage-category != balancing-row.damage-expected or balancing-row.armor-category != balancing-row.armor-expected { (balancing-row,) } else { () } if show-extra { rows += profiles.filter(player => player.profile_stance != target-stance).map(player => attack-report(attack, player, "Display")) } rows }).flatten() } #let category-rank(category) = ( None: 0, Chip: 1, Moderate: 2, High: 3, NearDeath: 4, Kill: 5, Overkill: 6, Malformed: -1, ).at(category, default: -1) #let attack-type-labels = ( ATK: "Attaque", TRA: "Thrashing", TBU: "Tank buster", TBZ: "Tank buster zone", WIP: "Raid wipe", WID: "Raid-wide", AOE: "AOE", CLV: "Cleave", CMB: "Combo", FLR: "Flurry", GRB: "Saisie", BYP: "Bypass", TSH: "Tank shred", ASE: "Setup", BIS: "Best action", BAD: "Bad action", ) #let deterministic-hit(attack, player, remaining-successes) = { let incoming = damage-at-successes(damage-vector(attack.damage_regular), remaining-successes) let tanked = calc.max(incoming - player.protection - player.strength, 0) ( incoming: incoming, tanked: tanked, ratio: tanked / player.health, category: damage-category(tanked / player.health), survives: tanked < player.health, ) } #let defense-required-to-survive(attack, player) = { let successes = tagged-number(attack.success, "r") let found = range(successes + 1).find(defended => ( deterministic-hit(attack, player, calc.max(successes - defended, 0)).survives )) if found == none { successes + 1 } else { found } } #let survival-probability(attack, player, required) = { let threshold = defense-threshold(attack, player) let outcomes = binomial-distribution(threshold, player.successes_def).filter(item => item.successes >= required) if outcomes.len() == 0 { 0.0 } else { outcomes.map(item => item.probability).sum() } } #let attack-group-members(creature, attack) = { let group = if attack.attack_group == none { "" } else { str(attack.attack_group).trim() } if group == "" { (attack,) } else { creature.raw.abilities.filter(item => ( item.phase == attack.phase and item.attack_group != none and str(item.attack_group).trim() == group and damage-vector(item.damage_regular).len() > 0 )) } } #let diagnostic-base(creature, attack) = { let balancing = creature.raw.balancing.first() let profiles = player-profiles(balancing.profile_hse) let target-stance = category-or(attack.target_stance, "Neutral") let target = profiles.find(profile => profile.profile_stance == target-stance) let successes = tagged-number(attack.success, "r") let one = deterministic-hit(attack, target, 1) let two = deterministic-hit(attack, target, calc.min(2, successes)) let maximum = deterministic-hit(attack, target, successes) let survival = profiles.map(profile => ( stance: profile.profile_stance, ..deterministic-hit(attack, profile, successes), )) let survivors = survival.filter(item => item.survives).len() let required = defense-required-to-survive(attack, target) let threshold = defense-threshold(attack, target) let group = attack-group-members(creature, attack) let group-successes = group.map(item => tagged-number(item.success, "r")).sorted().rev() let expected = attack-report(attack, target, "Balancing") ( creature-id: creature.id, creature: creature.name, difficulty: balancing.profile_hse, attack-id: attack.attack_id, name: attack.name, phase: attack.phase, group: if attack.attack_group == none or str(attack.attack_group).trim() == "" { "—" } else { str(attack.attack_group) }, group-size: group.len(), group-rank: group-successes.position(value => value == successes) + 1, declared-type: category-or(attack.attack_type, "—"), declared-label: attack-type-labels.at(category-or(attack.attack_type, ""), default: category-or(attack.attack_type, "—")), target-stance: target-stance, successes: successes, threshold: threshold, defense-required: required, defense-dice: target.successes_def, survival-probability: survival-probability(attack, target, required), one: one, two: two, maximum: maximum, survivors: survivors, profile-count: profiles.len(), survival: survival, expected-category: expected.damage-category, ) } #let all-diagnostic-bases = master-creatures.map(creature => creature.raw.abilities.filter(attack => ( damage-vector(attack.damage_regular).len() > 0 )).map(attack => diagnostic-base(creature, attack))).flatten() #let diagnostic-cohort(item) = all-diagnostic-bases.filter(other => other.difficulty == item.difficulty) #let diagnostic-average(values) = if values.len() == 0 { none } else { values.sum() / values.len() } #let diagnostic-deviation(values) = if values.len() < 2 { 0 } else { let mean = diagnostic-average(values) calc.sqrt(values.map(value => calc.pow(value - mean, 2)).sum() / values.len()) } #let heuristic-tags(item) = { let tags = () let max-rank = category-rank(item.maximum.category) let expected-rank = category-rank(item.expected-category) if item.survivors == 0 or item.maximum.ratio >= 2 { tags.push("Nuke") } if ( item.survival.find(profile => profile.stance == "Defensive").survives and item.survivors <= calc.max(2, calc.floor(item.profile-count / 2)) and max-rank >= category-rank("Kill") ) { tags.push("Tank buster") } if max-rank >= category-rank("Kill") and expected-rank <= category-rank("Moderate") { tags.push("Contrôle de défense") } if item.one.ratio < 0.25 and item.maximum.ratio < 1 and item.maximum.ratio >= 0.25 { tags.push("Thrashing") } if item.one.ratio < 0.25 and item.two.ratio >= 0.25 { tags.push("Pression progressive") } if item.one.ratio >= 1 { tags.push("Exécution immédiate") } if item.defense-required >= 3 { tags.push("Défense en profondeur") } if item.threshold <= 5 and item.defense-required > 0 { tags.push("Défense difficile") } if item.maximum.ratio < 0.25 { tags.push("Faible menace directe") } if tags.len() == 0 { tags.push("Pression standard") } tags } #let attack-diagnostics = all-diagnostic-bases.map(item => { let cohort = diagnostic-cohort(item) let success-values = cohort.map(other => other.successes) let threshold-values = cohort.map(other => other.threshold) let success-deviation = diagnostic-deviation(success-values) let threshold-deviation = diagnostic-deviation(threshold-values) item + ( success-z: if success-deviation == 0 { 0 } else { (item.successes - diagnostic-average(success-values)) / success-deviation }, threshold-z: if threshold-deviation == 0 { 0 } else { (item.threshold - diagnostic-average(threshold-values)) / threshold-deviation }, tags: heuristic-tags(item), ) }) #let creature-attack-diagnostics(creature-id) = attack-diagnostics.filter(item => item.creature-id == creature-id) // Initiative pressure is deliberately descriptive: it compares action economy // and action grouping inside a Difficulty cohort without attempting to assign // damage to individual initiative steps. #let action-blocks(value) = if value == none { () } else { str(value).split(",").map(item => item.trim()).filter(item => item != "").map(item => { let found = item.match(regex("\\d+")) if found == none { 0 } else { int(found.text) } }) } #let initiative-phase-base(creature, phase) = { let stats = creature.raw.stats let blocks = action-blocks(stats.at(phase + "_actions")) let initiative-steps = str(stats.at(phase + "_init")).split(",").map(item => item.trim()).filter(item => item != "").len() let total = if blocks.len() == 0 { 0 } else { blocks.sum() } let maximum = if blocks.len() == 0 { 0 } else { blocks.sorted().last() } let minimum = if blocks.len() == 0 { 0 } else { blocks.sorted().first() } let mean = if blocks.len() == 0 { 0 } else { total / blocks.len() } let imbalance = if blocks.len() < 2 { 0 } else { calc.sqrt(blocks.map(value => calc.pow(value - mean, 2)).sum() / blocks.len()) } ( creature-id: creature.id, difficulty: creature.raw.balancing.first().profile_hse, phase: phase, blocks: blocks, total: total, steps: blocks.len(), initiative-steps: initiative-steps, maximum: maximum, spread: maximum - minimum, concentration: if total == 0 { 0 } else { maximum / total }, imbalance: imbalance, ) } #let all-initiative-bases = master-creatures.map(creature => ( initiative-phase-base(creature, "p1"), initiative-phase-base(creature, "p2"), )).flatten() #let initiative-z(value, cohort, field) = { let values = cohort.map(item => item.at(field)) let deviation = diagnostic-deviation(values) if deviation == 0 { 0 } else { (value - diagnostic-average(values)) / deviation } } #let initiative-tags(item, cohort) = { let tags = () let total-z = initiative-z(item.total, cohort, "total") let steps-z = initiative-z(item.steps, cohort, "steps") let maximum-z = initiative-z(item.maximum, cohort, "maximum") let imbalance-z = initiative-z(item.imbalance, cohort, "imbalance") let average-steps = diagnostic-average(cohort.map(other => other.steps)) if total-z >= 1.25 { tags.push("Surcharge d’actions") } if item.concentration >= 0.7 and (item.maximum >= 5 or maximum-z >= 1) { tags.push("Focus intense") } if item.steps >= 4 and item.maximum <= 1 { tags.push("Essaim d’initiatives") } else if steps-z >= 1.25 or item.steps >= average-steps + 2 { tags.push("Cadence morcelée") } if item.spread >= 2 and imbalance-z >= 1 { tags.push("Rythme asymétrique") } if item.steps != item.initiative-steps { tags.push("Déclaration désalignée") } if tags.len() == 0 { tags.push("Cadence conforme") } tags } #let initiative-profiles = all-initiative-bases.map(item => { let cohort = all-initiative-bases.filter(other => other.difficulty == item.difficulty and other.phase == item.phase) item + ( average-total: diagnostic-average(cohort.map(other => other.total)), average-steps: diagnostic-average(cohort.map(other => other.steps)), average-maximum: diagnostic-average(cohort.map(other => other.maximum)), total-z: initiative-z(item.total, cohort, "total"), steps-z: initiative-z(item.steps, cohort, "steps"), maximum-z: initiative-z(item.maximum, cohort, "maximum"), imbalance-z: initiative-z(item.imbalance, cohort, "imbalance"), tags: initiative-tags(item, cohort), ) }) #let creature-initiative-profiles(creature-id) = initiative-profiles.filter(item => item.creature-id == creature-id) #let advanced-creature-analysis(creature, show-extra: true, show-balanced: true) = ( creature: creature, model: compatibility-model, defense: creature-defense-report(creature, show-extra: show-extra), attacks: creature-attack-report(creature, show-extra: show-extra, show-balanced: show-balanced), diagnostics: creature-attack-diagnostics(creature.id), initiative: creature-initiative-profiles(creature.id), ) #let advanced-analysis(creature-ids: (), show-extra: true, show-balanced: true) = master-creatures.filter(creature => ( creature-ids.len() == 0 or creature.id in creature-ids )).map(creature => advanced-creature-analysis( creature, show-extra: show-extra, show-balanced: show-balanced, ))