Files
firestorm/MW4COMPARE/tools/decompile/damage.py
T
83478b7666 Add MW4COMPARE: .mw4 decompiler toolchain and V4H comparison harness
Tooling built to recover editable source for six 'Mech chassis that exist
in the parallel FS_Build_V4H build but not in this repo. Reverse-engineers
every compiled record type in the .mw4 package format back to the .data /
.instance / .subsystems / .damage / .contents / .torso / .engine /
.armature sources the content pipeline consumes.

Nothing here is wired into the game build. It is a standalone analysis
harness run from Linux.

Package format
--------------
"#VBD" container. Directory records are [len][name][FILETIME][origSize]
[storedSize][offset], payload base at dword 0x0C. A record is stored raw
when storedSize == origSize, otherwise LZW (9->12-bit LSB-first codes,
256=clear, 257=EOF, dict from 258), per Database.cpp:451.

GameModel records are flat /Zp4 structs following the C++ inheritance
chain Entity(0) -> Mover(28) -> MWObject(80) -> Vehicle(664) -> Mech(756),
1636 bytes total. CreateMessage records follow Replicator -> Entity ->
Mover -> MWMover -> MWObject -> Vehicle -> Mech from start=16 (the
undeclared Connection__Message header), ending at 341 and padded to 344.

tools/decompile/
----------------
  datamap.py        header-driven layout engine; CHAIN + ANCHORS
                    {Vehicle:664, Mech:756} assert the struct offsets
  mw4msg.py         CreateMessage reader/walker
  data.py           .data      constants.py  define/table symbol resolution
  damage.py         .damage    contents.py   .contents
  smallmodel.py     .torso + .engine         instance.py  .instance
  armature.py / armature_parts.py  .armature + armaturedata/armaturevideo
  assembly.py       joint hierarchy renderer
  make_generic_doll.py  builds generic MFD/Radar damage dolls
  verify_*.py       per-type round-trip verifiers

Verified round-trip across all 64 shared chassis:
  .armature      2938/2976 pages     .subsystems  7579/7585 keys
  .data map      6071/6071 values    .data trip   8291/8306 keys
  .damage        6605/6605 keys      .contents    7480/7480 keys
  .torso+.engine 1280/1280 keys      .instance     896/896 keys, 64/64 pages
  armature_parts 1202/1202 .data, 1149/1202 .video

Layout-discovery lessons (documented in DECOMPILING.md)
-------------------------------------------------------
- Never let a field map be discovered by the values that verify it. A
  value-matching pass reported 4288/4288 while mis-assigning 34 keys. The
  map was rebuilt from header declaration order, anchored on uniquely
  resolved fields.
- Read the factory, not the data. 12 .data fields and 5 Torso fields are
  declared plain Stuff::Scalar but multiplied by Radians_Per_Degree in
  Mech_Tool.cpp:889 / Torso_Tool.cpp.
- Strip typedefs before walking a header. A stray `typedef int AttributeID;`
  masked a missing ClassID - two 4-byte errors cancelling out, caught only
  by the ANCHORS assertion.
- A verifier that silently narrows its own input reports success. Braced
  blocks must be hidden before splitting pages, replacing both CR and LF,
  because a `Shadow={...}` block contains a line reading `[shadow]` and
  splitlines() also splits on bare CR.
- NSWIZZLE is undefined, so the #else branch is live and orders members
  differently. bool is 1 byte; char x[MaxStringLength] is 256.
- V4H carries stale Mech IDs (their Atlas is 5, ours 6), so 64 of 65 shared
  chassis are off by one; --retarget-ids emits $(M_<Chassis>)/$(IDS_<Chassis>).

reports/ holds generated diffs. The two ~5 MB manifest-*.tsv intermediates
are gitignored; regenerate everything with run-comparison.sh.

Co-authored-by: Claude Opus 5 (Anthropic) <noreply@anthropic.com>
Co-authored-by: GitHub Copilot <copilot@github.com>
2026-08-08 16:47:57 -05:00

238 lines
9.0 KiB
Python

#!/usr/bin/env python3
"""
damage.py - decompiler for a mech `<chassis>.damage` file.
The record is a bare concatenation of variable-length objects with no index and
no length prefixes: parsing means walking forward, reading a classID, and using
it to decide what follows. Written by `MWObject::CreateDamageStream`
(MWObject_Tool.cpp:1149), which iterates the source pages in order and dispatches
on whether a page carries a `DamageZone` entry.
Armour page -- DamageObject::ConstructDamageObjectStream (DamageObject.cpp:157):
classID, baseArmorValue, currentArmorValue, scaleSplashDamage,
damageObjectName (MString), internalDamageZoneID, armorZone, damageLevel,
armorType, maxArmorValue, attachedToZone
Internal page -- InternalDamageObject::ConstructInternalDamageObjectStream
(DamageObject.cpp:931) plus the MW4 subclass (MWDamageObject.cpp:88):
classID, baseInternalDamage, currentInternalDamage,
parentEntityName (MString), damageMode, damageZone, damagePropagationZone,
internalType, attachedTo, damageEffects[], missileSlots, projectileSlots,
beamSlots, omniSlots
MString is an int length NOT counting the terminator, the characters, then a NUL
-- the same encoding as the {FootSteps} stream.
Two traps:
* The armour page stores its own name, but the internal page does NOT. Internal
page names are reconstructed as `<Zone>Internal`, which every one of the 89
mech .damage files follows, using the damageZone that IS stored.
* ArmorZone and InternalZone are DIFFERENT enums. ArmorZone has
CenterRearTorso at 7 and Head at 8; InternalZone has Head at 7 and no rear
torso entry. Conflating them silently mislabels head and torso zones.
python3 damage.py <chassis-record-dir> [-o out.damage]
python3 damage.py --verify
"""
import argparse, collections, os, re, struct, subprocess, sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import subsystems
MANIFEST = "/home/rich/Repositories/FS_Ours_extracted/_manifest.tsv"
ARMOR_CLASS = 468 # Adept::DamageObject
INTERNAL_CLASS = 1162 # MechWarrior4::MWInternalDamageObject
# DamageObject.hpp:363 -- note CenterRearTorso, absent from the internal enum.
ARMOR_ZONE = {
-1: "NullZone", 0: "LeftLeg", 1: "RightLeg", 2: "LeftArm", 3: "RightArm",
4: "RightTorso", 5: "LeftTorso", 6: "CenterTorso", 7: "CenterRearTorso",
8: "Head", 9: "Special1", 10: "Special2", 11: "DefaultZone",
}
# DamageObject.hpp:204
INTERNAL_ZONE = {
-1: "NullZone", 0: "LeftLeg", 1: "RightLeg", 2: "LeftArm", 3: "RightArm",
4: "RightTorso", 5: "LeftTorso", 6: "CenterTorso", 7: "Head",
8: "Special1", 9: "Special2", 10: "VehicleHull", 11: "VehicleWeapon",
12: "VehicleSpecial", 13: "DefaultZone",
}
# DamageObject.hpp:185
DAMAGE_MODE = {
0: "GeneralDamageMode", 1: "GimpLeftDamageMode", 2: "GimpRightDamageMode",
3: "DestructionDamageMode", 4: "DetachableDamageMode", 5: "EngineDamageMode",
6: "NextDamageMode", 7: "HeadShotDamageMode", 8: "GyroHitDamageMode",
9: "TorsoLeftDamageMode", 10: "TorsoRightDamageMode",
11: "ArmLeftDamageMode", 12: "ArmRightDamageMode",
}
class Reader:
def __init__(self, blob):
self.b, self.o = blob, 0
def i32(self):
v = struct.unpack_from("<i", self.b, self.o)[0]
self.o += 4
return v
def u32(self):
v = struct.unpack_from("<I", self.b, self.o)[0]
self.o += 4
return v
def f32(self):
v = struct.unpack_from("<f", self.b, self.o)[0]
self.o += 4
return v
def mstring(self):
n = self.u32()
s = self.b[self.o:self.o + n].decode("latin-1")
self.o += n + 1 # length excludes the terminator
return s
def done(self):
return self.o >= len(self.b)
def parse(blob):
"""-> [(kind, {field: value})] in stream order."""
r = Reader(blob)
out = []
while not r.done():
class_id = r.i32()
if class_id == ARMOR_CLASS:
rec = {
"baseArmorValue": r.f32(),
"currentArmorValue": r.f32(),
"scaleSplashDamage": r.f32(),
"name": r.mstring(),
"internalDamageZone": r.i32(),
"armorZone": r.i32(),
"damageLevel": r.i32(),
"armorType": r.i32(),
"maxArmorValue": r.f32(),
"attachedToZone": r.i32(),
}
out.append(("armor", rec))
elif class_id == INTERNAL_CLASS:
rec = {
"baseInternalDamage": r.f32(),
"currentInternalDamage": r.f32(),
"parentEntityName": r.mstring(),
"damageMode": r.i32(),
"damageZone": r.i32(),
"damagePropagationZone": r.i32(),
"internalType": r.i32(),
"attachedTo": r.i32(),
}
count = r.i32()
rec["effects"] = [(r.u32(), r.f32()) for _ in range(count)]
rec["missileSlots"] = r.i32()
rec["projectileSlots"] = r.i32()
rec["beamSlots"] = r.i32()
rec["omniSlots"] = r.i32()
out.append(("internal", rec))
else:
raise ValueError(f"unknown classID {class_id} at offset {r.o - 4}")
return out
def fmt(x):
"""Match the authored style: 1.0, 80.0, 0.14, .99"""
if x == int(x):
return f"{x:.1f}"
return f"{x:g}"
def decompile(mech_dir, manifest=None):
"""-> [(pageName, [(key, value)])] in stream order."""
manifest = manifest if manifest is not None else subsystems.load_manifest(MANIFEST)
blob = record(mech_dir)
pages = []
for kind, r in parse(blob):
if kind == "armor":
kv = [
("BaseArmorValue", fmt(r["baseArmorValue"])),
("MaxArmorValue", fmt(r["maxArmorValue"])),
("ScaleSplashDamage", f"{r['scaleSplashDamage']:g}"),
("InternalDamageZone", INTERNAL_ZONE.get(r["internalDamageZone"])),
("ArmorZone", ARMOR_ZONE.get(r["armorZone"])),
]
if r["attachedToZone"] != -1:
kv.append(("SpecialAttachedToZone", ARMOR_ZONE.get(r["attachedToZone"])))
pages.append((r["name"], kv))
else:
zone = INTERNAL_ZONE.get(r["damageZone"], "Null")
kv = [
("DamageZone", zone),
("BaseInternalDamage", fmt(r["baseInternalDamage"])),
]
for rid, pct in r["effects"]:
path = manifest.get(rid >> 16, f"<unresolved:{rid}>")
kv.append(("DamageEffect", f"{path},{pct:g}"))
# no source writes GeneralDamageMode explicitly, so 0 means "omitted"
if r["damageMode"]:
kv.append(("DamageMode", DAMAGE_MODE.get(r["damageMode"])))
kv.append(("ParentEntityName", r["parentEntityName"]))
if r["damagePropagationZone"] != -1:
kv.append(("DamagePropagationZone",
INTERNAL_ZONE.get(r["damagePropagationZone"])))
if r["attachedTo"] != -1:
kv.append(("SpecialAttachedToZone", ARMOR_ZONE.get(r["attachedTo"])))
for key, field in (("MissileSlots", "missileSlots"),
("ProjectileSlots", "projectileSlots"),
("BeamSlots", "beamSlots"),
("OmniSlots", "omniSlots")):
if r[field]:
kv.append((key, str(r[field])))
pages.append((zone + "Internal", kv))
return pages
def record(mech_dir):
ch = os.path.basename(mech_dir.rstrip("/")).lower()
for fn in os.listdir(mech_dir):
if fn.lower().endswith(".damage"):
return open(os.path.join(mech_dir, fn), "rb").read()
raise SystemExit(f"no .damage record in {mech_dir}")
def emit(pages):
lines = []
for name, kv in pages:
lines.append(f"[{name}]")
lines.extend(f"{k}={v}" for k, v in kv)
lines.append("")
return "\r\n".join(lines)
def main():
ap = argparse.ArgumentParser()
ap.add_argument("mech_dir", nargs="?")
ap.add_argument("-o", "--output")
ap.add_argument("-m", "--manifest", default=MANIFEST)
ap.add_argument("--verify", action="store_true")
args = ap.parse_args()
if args.verify:
here = os.path.dirname(os.path.abspath(__file__))
raise SystemExit(subprocess.call([sys.executable,
os.path.join(here, "verify_damage.py")]))
if not args.mech_dir:
ap.error("mech_dir required")
text = emit(decompile(args.mech_dir, subsystems.load_manifest(args.manifest)))
if args.output:
with open(args.output, "wb") as fh:
fh.write(text.encode("latin-1"))
print(f"wrote {args.output}")
else:
sys.stdout.write(text)
if __name__ == "__main__":
main()