203 lines
10 KiB
Python
203 lines
10 KiB
Python
"""Verify owned character statistics against the original Classic 70334 wire layout."""
|
|
import math
|
|
import os
|
|
import struct
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
from native_codec_driver import MODES, ROOT, load, serialize
|
|
|
|
if not Path(os.environ.get("GW_TEST_CODEC_PATH", ROOT / "build/forever-schema/libforever_codec.so")).is_file():
|
|
sys.exit(77)
|
|
|
|
codec = load()
|
|
pack = struct.pack
|
|
guid = 4509
|
|
positive, negative, percent = 1201, 1208, 1215
|
|
|
|
|
|
def packed_guid(raw):
|
|
mask = sum(1 << i for i, byte in enumerate(raw) if byte)
|
|
return mask.to_bytes(len(raw) // 8, "little") + bytes(byte for byte in raw if byte)
|
|
|
|
|
|
def field_mask(fields):
|
|
blocks = [0] * (max(fields) // 32 + 1)
|
|
for index in fields:
|
|
blocks[index // 32] |= 1 << (index % 32)
|
|
return bytes([len(blocks)]) + pack(f"<{len(blocks)}I", *blocks) + b"".join(
|
|
pack("<I", fields[index]) for index in sorted(fields)
|
|
)
|
|
|
|
|
|
def create(fields):
|
|
header = pack("<QHIIIII", guid, 70, 0x46010046, 0, 12, 0, 0) + b"Fixture\0\1\1\0" + pack("<I", 0xA9)
|
|
body = pack("<IBB", 1, 0, 3) + packed_guid(pack("<Q", guid)) + bytes([4, 0x21])
|
|
body += pack("<II4fI6f", 0, 100, 1, 2, 3, 0, 0, 2.5, 7, 4.5, 4.72, 2.5, 3.14)
|
|
return serialize(codec, MODES.index(""), header + body + field_mask({22: 80, 28: 80, 34: 1, 35: 1, 36: 257, **fields}))
|
|
|
|
|
|
def delta(fields, owner=guid):
|
|
header = pack("<HIQBHI", 70, 0, guid, 1, 0, 0xA9)
|
|
body = pack("<IBB", 1, 0, 0) + packed_guid(pack("<Q", owner)) + field_mask(fields)
|
|
return serialize(codec, MODES.index("--unit-values-profile"), header + body)
|
|
|
|
|
|
def float_word(value):
|
|
return int.from_bytes(pack("<f", value), "little")
|
|
|
|
|
|
def expected_unit_delta(bits, values):
|
|
blocks = [0] * 8
|
|
for bit in bits:
|
|
blocks[bit // 32] |= 1 << (bit % 32)
|
|
presence = sum(1 << index for index, word in enumerate(blocks) if word)
|
|
mask = bytes([presence]) + b"".join(pack(">I", word) for word in blocks if word) + b"\0"
|
|
data = pack("<BBBI", 1, 0, 3, 1 << 5) + mask + pack(f"<{len(values)}i", *values)
|
|
block = b"\0" + packed_guid(pack("<QQ", guid, (2 << 58) | (70 << 42))) + pack("<I", len(data)) + data
|
|
return pack("<HIBI", 0, 1, 0x80, len(block)) + block
|
|
|
|
|
|
# 1.12 stores the effective stat in UNIT_FIELD_STAT0+i. POSSTAT/NEGSTAT
|
|
# are signed integer values on the wire, even though Tortoise stores floats.
|
|
# 70334 writes [effective, positive, negative, support] for each stat.
|
|
# Original rank 6 Power Word: Fortitude (10938), aura 29 / misc 2,
|
|
# supplies base points 53 + die 1; 240 is a synthetic effective stamina value.
|
|
stat_rows = [(101, 7, -2), (143, 13, -4), (240, 54, 0), (194, 23, -9), (177, 34, -17)]
|
|
stat_fields, stat_bits, stat_values = {}, {197}, []
|
|
for index, row in enumerate(stat_rows):
|
|
for first, bit, value in zip((150, 1177, 1182), (198, 203, 208), row):
|
|
field = first + index
|
|
stat_fields[field] = value & 0xffffffff
|
|
stat_bits.add(bit + index)
|
|
stat_values.append(value)
|
|
|
|
# Locate the independent totals in the owner create body; the only changed bytes
|
|
# when supplying the bonuses must be the positive and negative slots.
|
|
totals = create({150 + i: row[0] for i, row in enumerate(stat_rows)})
|
|
initial = b"".join(pack("<4i", row[0], 0, 0, 0) for row in stat_rows)
|
|
assert totals.count(initial) == 1, "Effective totals did not locate the native stat records"
|
|
with_bonuses = b"".join(pack("<4i", *row, 0) for row in stat_rows)
|
|
assert create(stat_fields) == totals.replace(initial, with_bonuses), "Owner create omitted stat bonuses"
|
|
assert delta(stat_fields) == expected_unit_delta(stat_bits, stat_values), "Combined stat update differs"
|
|
for source, child_bit in ((1177, 203), (1182, 208)):
|
|
for index in range(5):
|
|
for value in (13 if source == 1177 else -17, 0):
|
|
assert delta({source + index: value & 0xffffffff}) == expected_unit_delta(
|
|
{197, child_bit + index}, [value]), (source, index, value)
|
|
assert delta({1179: 54, 1184: 0}, owner=0xF130000001000001) == pack("<HIBI", 0, 0, 0x80, 0)
|
|
print("PASS all five owned stat totals, positive/negative slots, 20 sparse changes/clears and owner scope")
|
|
|
|
# Both attack-power modifiers are signed shorts in each legacy field. 70334
|
|
# has independent int32 slots; its melee pair crosses the 64/96 mask groups.
|
|
for source, bits in ((166, {64, 95, 96, 97}), (169, {96, 103, 104})):
|
|
for pos, neg in ((301, -54), (0, -16), (41, 0), (0, 0)):
|
|
word = (pos & 0xffff) | ((neg & 0xffff) << 16)
|
|
assert delta({source: word}) == expected_unit_delta(bits, [pos, neg]), (source, pos, neg)
|
|
# Distinct bases anchor each AP record during full create.
|
|
ap = create({165: 319, 166: 0xfff1000e, 168: 529, 169: 0xffe5001b})
|
|
assert ap.count(pack("<10i", 319, 14, -15, 0, 0, 0, 0, 529, 27, -27)) == 1
|
|
print("PASS melee and ranged signed attack-power halves on create, change and clear")
|
|
|
|
|
|
def expected_delta(fields):
|
|
# ClassicActivePlayerDataBit maps SDK group 275 and children 276..296
|
|
# to Classic group 317 and children 318..338. Original WriteUpdate
|
|
# emits positive, negative, percentage for each changed school in order.
|
|
changed = {317, *(318 + index - positive for index in fields)}
|
|
blocks = [sum(1 << (bit % 32) for bit in changed if bit // 32 == i) for i in range(14)]
|
|
bits = f"{sum(1 << i for i, block in enumerate(blocks) if block):014b}"
|
|
bits += "".join(f"{block:032b}" for block in blocks if block)
|
|
bits += "0" * (-len(bits) % 8)
|
|
contents = bytes([1, 0, 3]) + pack("<I", 1 << 7) + int(bits, 2).to_bytes(len(bits) // 8, "big")
|
|
for school in range(7):
|
|
for first in (positive, negative, percent):
|
|
if first + school in fields:
|
|
contents += pack("<I", fields[first + school])
|
|
block = bytes([0]) + packed_guid(pack("<QQ", guid, (2 << 58) | (70 << 42))) + pack("<I", len(contents)) + contents
|
|
return pack("<HIBI", 0, 1, 0x80, len(block)) + block
|
|
|
|
|
|
fields = {}
|
|
school_records = b""
|
|
for school in range(7):
|
|
pos, neg, factor = 101 + school, 47 + school, 0.5 + school / 8
|
|
fields.update({positive + school: pos, negative + school: neg, percent + school: float_word(factor)})
|
|
school_records += pack("<iiff", pos, neg, factor, 1.0)
|
|
after = create(fields)
|
|
assert after.count(school_records) == 1, "Full create omitted a school or changed its field order"
|
|
at = after.index(school_records)
|
|
empty = create({})
|
|
assert after[:at] == empty[:at] and after[at + 112:] == empty[at + 112:]
|
|
assert empty[at:at + 112] == pack("<iiff", 0, 0, 0.0, 1.0) * 7
|
|
assert after[at + 112:at + 124] == pack("<iff", 0, 1.0, 1.0), "Damage incorrectly altered healing"
|
|
assert delta(fields) == expected_delta(fields), "Multi-school update mask or payload order differs"
|
|
for index, value in fields.items():
|
|
for sample in (value, 0):
|
|
assert delta({index: sample}) == expected_delta({index: sample}), (index, sample)
|
|
assert delta(fields, owner=0xF130000001000001) == pack("<HIBI", 0, 0, 0x80, 0)
|
|
for mode in (create, delta):
|
|
try:
|
|
mode({percent: float_word(math.nan)})
|
|
except ValueError:
|
|
pass
|
|
else:
|
|
raise AssertionError("Nonfinite school multiplier accepted")
|
|
print("PASS initial and sparse school damage, 42 single-field values/clears, owner scope and healing independence")
|
|
|
|
# Original UnitData::WriteUpdate sends the ranged scalar before the main/offhand
|
|
# array. The 70334 mask uses scalar group 32, array group 194, and one reserved
|
|
# bit after its eight-block presence mask and selected uint32 mask blocks.
|
|
def expected_attack_delta(periods):
|
|
named_bits = {126: (194, 195), 127: (194, 196), 128: (32, 53)}
|
|
mask = [0] * 8
|
|
for index in periods:
|
|
for bit in named_bits[index]:
|
|
mask[bit // 32] |= 1 << (bit % 32)
|
|
presence = sum(1 << i for i, word in enumerate(mask) if word)
|
|
changed = bytes([presence]) + b"".join(pack(">I", word) for word in mask if word) + b"\x00"
|
|
values = b"".join(pack("<I", periods[i]) for i in (128, 126, 127) if i in periods)
|
|
data = pack("<BBBI", 1, 0, 3, 1 << 5) + changed + values
|
|
block = b"\x00" + packed_guid(pack("<QQ", guid, (2 << 58) | (70 << 42))) + pack("<I", len(data)) + data
|
|
return pack("<HIBI", 0, 1, 0x80, len(block)) + block
|
|
|
|
|
|
periods = {126: 1901, 127: 2302, 128: 2803}
|
|
initial_periods = pack("<III", *periods.values())
|
|
full = create(periods)
|
|
assert full.count(initial_periods) == 1
|
|
start = full.index(initial_periods)
|
|
assert full[:start] == empty[:start] and empty[start:start+12] == bytes(12) and full[start+12:] == empty[start+12:]
|
|
assert delta(periods) == expected_attack_delta(periods)
|
|
for index, period in periods.items():
|
|
for value in (period, 0):
|
|
assert delta({index: value}) == expected_attack_delta({index: value}), (index, value)
|
|
assert delta(periods, owner=0xF130000001000001) == pack("<HIBI", 0, 0, 0x80, 0)
|
|
print("PASS main, offhand and ranged period initial values, separate updates and zero clears, combined update and owner scope")
|
|
|
|
# Tortoise UNIT_MOD_CAST_SPEED is a duration factor. Classic owner UnitData
|
|
# has casting speed, negative casting speed (separate), then spell haste.
|
|
cast_speed = 145
|
|
hasted = create({cast_speed: float_word(.8)})
|
|
hasted_triplet = pack("<fff", .8, 1, .8)
|
|
assert hasted.count(hasted_triplet) == 1
|
|
offset = hasted.index(hasted_triplet)
|
|
assert empty[offset:offset+12] == pack("<fff", 1, 1, 1)
|
|
for factor in (.8, 1.25, 1.0, 0.0):
|
|
haste = factor if factor else 1.0
|
|
assert create({cast_speed:float_word(factor)}) == empty[:offset] + pack("<fff", factor, 1, haste) + empty[offset+12:]
|
|
# SDK bits 76,78 become 70334 bits 77,79, under group 64 and mask block 2.
|
|
changed = bytes([1 << 2]) + pack(">I", 1 | (1 << 13) | (1 << 15)) + b"\x00"
|
|
data = pack("<BBBI", 1, 0, 3, 1 << 5) + changed + pack("<ff",factor,haste)
|
|
block = b"\x00" + packed_guid(pack("<QQ", guid,(2 << 58) | (70 << 42))) + pack("<I",len(data)) + data
|
|
assert delta({cast_speed:float_word(factor)}) == pack("<HIBI",0,1,0x80,len(block)) + block
|
|
for mode in (create, delta):
|
|
for invalid in (-1.0, math.nan, math.inf):
|
|
try:
|
|
mode({cast_speed:float_word(invalid)})
|
|
except ValueError:
|
|
continue
|
|
raise AssertionError("Invalid cast-speed factor accepted")
|
|
assert delta({cast_speed:float_word(.8)}, owner=0xF130000001000001) == pack("<HIBI",0,0,0x80,0)
|
|
print("PASS owned spell haste and casting speed initial/updates, neutral reset, instant casts, and unit scope")
|