"""Findings about the Albion health-update protocol, verified against captures. These tests double as the specification for the Phase 1 decode work. They read raw ``photon_message`` records rather than the decoder's ``game_message`` stream, because several of the behaviours under test concern opcodes that ``src/decode/translate.py`` does not handle yet. Captures live outside the repository and are skipped when absent. """ import collections import statistics import pytest from capture_analysis import ( OPCODE_DAMAGE_BATCH, OPCODE_ENTITY_STATS, OPCODE_HEALTH_UPDATE, DamageHit, capture_paths, check_chain, damage_hits, damage_type, iter_messages, param, perfect_scalar_chains, ) pytestmark = pytest.mark.skipif( not capture_paths(), reason='discovery captures are not available') def _scalar_messages(): for path in capture_paths(): for message in iter_messages(path): if message.opcode == OPCODE_HEALTH_UPDATE: yield message def _all_hits(): for path in capture_paths(): for hit in damage_hits(path): yield hit # --- Opcode 7 is additional damage, not a repeat of opcode 6 --------------- def test_batched_damage_is_not_a_duplicate_of_scalar_damage(): """Echoing known hits destroys an exact health chain; real E7 does not. Each victim picked here has a scalar-opcode health chain that reconciles perfectly. Injecting a stream that merely repeats those same hits makes every step inconsistent, because the repeated delta is applied twice. If the batched opcode were such a repeat it would behave the same way. Rates are pooled across captures rather than compared per capture: a single-break sample from a two-step victim would otherwise dominate. """ real_breaks = real_steps = 0 synthetic_breaks = synthetic_steps = 0 for path in capture_paths(): for group in perfect_scalar_chains(path): base = check_chain(group, include_batched=False) actual = check_chain(group, include_batched=True) real_steps += actual.steps - base.steps real_breaks += actual.breaks - base.breaks echoed = list(group) + [ DamageHit(h.timestamp, OPCODE_DAMAGE_BATCH, h.victim, h.attacker, h.delta, h.health_after) for h in group] doubled = check_chain(echoed, include_batched=True) synthetic_steps += doubled.steps - base.steps synthetic_breaks += doubled.breaks - base.breaks assert real_steps > 20, f'only {real_steps} extra steps introduced' assert synthetic_steps > 20, f'only {synthetic_steps} synthetic steps' real_rate = real_breaks / real_steps synthetic_rate = synthetic_breaks / synthetic_steps assert real_rate < 0.25, \ f'real batched damage should not double-count, got {real_rate:.2%}' assert synthetic_rate > 0.75, \ f'a genuine duplicate should double-count, got {synthetic_rate:.2%}' def test_batched_damage_carries_substantial_volume(): """The batched opcode accounts for a large share of total damage. This is the size of the undercount that Phase 1 exists to fix. """ scalar = batched = 0.0 for hit in _all_hits(): if hit.delta >= 0: continue if hit.opcode == OPCODE_HEALTH_UPDATE: scalar += -hit.delta else: batched += -hit.delta assert scalar > 0 and batched > 0 share = batched / (scalar + batched) assert 0.3 < share < 0.5, f'batched share of damage drifted to {share:.2%}' def test_batched_damage_reports_health_after_each_hit(): """Every batched hit carries its own resulting health and attacker. These are the fields Phase 1 needs to credit damage without guessing. """ checked = 0 for hit in _all_hits(): if hit.opcode != OPCODE_DAMAGE_BATCH or hit.delta >= 0: continue assert hit.health_after is not None, 'batched hit without resulting health' assert hit.attacker is not None, 'batched hit without an attacker' checked += 1 assert checked > 100, f'only {checked} batched damage hits to check' # --- Health-update parameter layout --------------------------------------- def test_health_after_param_reconciles_with_deltas(): """Param 3 is the victim's health once the hit has been applied.""" report = check_chain(list(_all_hits()), include_batched=False) assert report.steps > 1000, f'only {report.steps} steps to reconcile' assert report.break_rate < 0.6, \ f'param 3 does not reconcile (break rate {report.break_rate:.2%})' def test_damage_type_separates_basic_hits_from_abilities(): """Param 5 splits basic attacks (0, 1) from ability hits (2+). The groups do not overlap and differ sharply in magnitude, which is what makes param 5 useful for splitting damage by hit type. """ magnitudes = collections.defaultdict(list) for message in _scalar_messages(): delta = param(message.params, 2) if not isinstance(delta, (int, float)) or delta >= 0: continue magnitudes[damage_type(message)].append(-delta) assert set(magnitudes) >= {0, 1, 2, 3}, f'unexpected param 5 domain: {sorted(magnitudes)}' basic = magnitudes[0] + magnitudes[1] abilities = magnitudes[2] + magnitudes[3] assert statistics.median(basic) < statistics.median(abilities) * 0.5, \ f'basic {statistics.median(basic):.0f} vs ability ' \ f'{statistics.median(abilities):.0f} are not separable' def test_max_health_param_presence_is_a_function_of_damage_type(): """Param 7 is populated for exactly the param 5 values 2 and above. "Populated" means a value of at least zero; basic hits carry -1 as a placeholder. Presence therefore carries no information beyond param 5, so param 7 adds no independent damage signal. Recorded so Phase 1 does not model the same partition twice. """ populated = collections.defaultdict(set) for message in _scalar_messages(): value = damage_type(message) raw = param(message.params, 7) populated[value].add(isinstance(raw, (int, float)) and raw >= 0) for value, flags in populated.items(): assert len(flags) == 1, \ f'param 7 presence varies within param 5={value}: {flags}' absent = {value for value, flags in populated.items() if flags == {False}} assert absent == {0, 1}, f'param 7 absent for {sorted(absent)}, expected 0 and 1' # --- Opcode 18 param 6 is not a damage multiplier -------------------------- def test_entity_stats_multiplier_does_not_track_damage(): """Damage does not change across opcode 18 multiplier transitions. This refutes the Phase 0 hypothesis that opcode 18 carries a live incoming damage multiplier. The multiplier moves while the median damage either side of the transition stays flat, so it cannot be used to infer mitigation state. Recorded here so Phase 2 does not build on it. """ import statistics as stats followed = 0 for path in capture_paths(): multipliers = collections.defaultdict(list) damage = collections.defaultdict(list) for message in iter_messages(path): if message.opcode == OPCODE_ENTITY_STATS: value = param(message.params, 6) if isinstance(value, (int, float)): multipliers[param(message.params, 0)].append( (message.timestamp, value)) for hit in damage_hits(path): damage[hit.victim].append((hit.timestamp, hit)) for entity, sequence in multipliers.items(): if not damage.get(entity): continue for index in range(1, len(sequence)): (before_ts, before_m), (after_ts, after_m) = sequence[index - 1], sequence[index] if before_m == after_m: continue earlier = [-hit.delta for ts, hit in damage[entity] if hit.delta < 0 and before_ts < ts <= after_ts] later = [-hit.delta for ts, hit in damage[entity] if hit.delta < 0 and ts > after_ts][:20] if len(earlier) < 3 or len(later) < 3: continue followed += 1 expected = abs(after_m / before_m) observed = abs(stats.median(later) / stats.median(earlier)) assert abs(observed - expected) > 0.2 * max(expected, 1e-9), \ f'damage tracked the multiplier x{expected:.2f} ' \ f'(observed x{observed:.2f}) -- it may be a damage multiplier' assert followed > 0, 'no opcode 18 multiplier transitions with surrounding damage' def test_entity_stats_multiplier_is_often_constant_per_entity(): """Where param 6 varies, it is a minority of samples for that entity. Consistent with a cosmetic or interpolation factor rather than a continuously tracked combat statistic. """ unstable = seen = 0 for path in capture_paths(): per_entity = collections.defaultdict(collections.Counter) for message in iter_messages(path): if message.opcode != OPCODE_ENTITY_STATS: continue value = param(message.params, 6) if isinstance(value, (int, float)): per_entity[param(message.params, 0)][round(value, 4)] += 1 for counts in per_entity.values(): seen += 1 if len(counts) > 1: unstable += 1 assert seen > 0, 'no opcode 18 records found' assert unstable / seen < 0.5, \ f'{unstable}/{seen} entities vary, too many for a cosmetic value'