diff options
| author | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-21 22:09:26 -0500 |
|---|---|---|
| committer | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-21 22:09:26 -0500 |
| commit | 3d4f0df6c4d1560e3117d5df1e37e186d53473a5 (patch) | |
| tree | 3385a2e12d6ffe694f1c4aaf983ec072837d4b0e /tools | |
| parent | ec4840ab361f512e027a8f43248f2983800348bc (diff) | |
| parent | cd6ade80c9ca72a3296cf52177f9a715349b7c8c (diff) | |
Merge pull request #787 from 465d6a0eac
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/gamedata_maps.py | 77 |
1 files changed, 73 insertions, 4 deletions
diff --git a/tools/gamedata_maps.py b/tools/gamedata_maps.py index 9efce56a5..c490d4e9e 100644 --- a/tools/gamedata_maps.py +++ b/tools/gamedata_maps.py | |||
| @@ -24,9 +24,11 @@ match the published GTA V `heightmap.dat` (`HMAP`) layout: | |||
| 24 | 24 | ||
| 25 | `PAMH` (`HMAP` read little-endian) | 25 | `PAMH` (`HMAP` read little-endian) |
| 26 | 0x00 "PAMH" (the `HMAP` magic stored little-endian) | 26 | 0x00 "PAMH" (the `HMAP` magic stored little-endian) |
| 27 | 0x04 u8 version major, u8 version minor | 27 | 0x04 u8 version major, u8 version minor (1.2 in `common.rpf`) |
| 28 | 0x05 in the `levels.rpf` tiles 0x05..0x0B also hold leftover text of the | ||
| 29 | tile name; 0x04 stays 1 and the real fields start at 0x0C (see below) | ||
| 28 | 0x06 u16 pad | 30 | 0x06 u16 pad |
| 29 | 0x08 u32 compressed/flag word (GTA V writes 1; measured 0x1BBC8CED here) | 31 | 0x08 u32: GTA V's `compressed` word in `common.rpf`, padding in the tiles |
| 30 | 0x0C u16 width | 32 | 0x0C u16 width |
| 31 | 0x0E u16 height | 33 | 0x0E u16 height |
| 32 | 0x10 six f32 bounds (min xyz, max xyz) | 34 | 0x10 six f32 bounds (min xyz, max xyz) |
| @@ -36,6 +38,14 @@ match the published GTA V `heightmap.dat` (`HMAP`) layout: | |||
| 36 | On the measured file the first array is a flat 110 (2.5 m) sea-surface | 38 | On the measured file the first array is a flat 110 (2.5 m) sea-surface |
| 37 | layer and the second is the ground; min <= max in all 23,616 cells. | 39 | layer and the second is the ground; min <= max in all 23,616 cells. |
| 38 | 40 | ||
| 41 | In the 1,476 64 x 64 tiles of `levels.rpf`, bytes 0x05..0x0B hold leftover text, | ||
| 42 | not a version minor and a flag: 0x04 stays 1, 0x05.. hold the tile name `n_m` | ||
| 43 | (n = column + 1, m = row + 1), and the remaining bytes keep the right-aligned | ||
| 44 | tail of the string `height` (`ght`, `ht`, or a zero byte when the name is | ||
| 45 | longer). The 28 distinct 0x08..0x0B words are all text and none of them changes | ||
| 46 | the arrays, so 0x08 is padding there. The `common.rpf` payload happens to hold | ||
| 47 | 0x1BBC8CED, which is why it was once reported as a `compressed` flag. | ||
| 48 | |||
| 39 | Both formats scale a u8 value into the header's Z box: | 49 | Both formats scale a u8 value into the header's Z box: |
| 40 | 50 | ||
| 41 | z = min.z + value / 255 * (max.z - min.z) | 51 | z = min.z + value / 255 * (max.z - min.z) |
| @@ -178,11 +188,14 @@ def parse_pamh(blob: bytes) -> dict: | |||
| 178 | half = width * height | 188 | half = width * height |
| 179 | maximum = bytearray(half) | 189 | maximum = bytearray(half) |
| 180 | minimum = bytearray(half) | 190 | minimum = bytearray(half) |
| 191 | full_width = 0 | ||
| 181 | for y in range(height): | 192 | for y in range(height): |
| 182 | start, count = struct.unpack_from("<HH", rows, y * 8) | 193 | start, count = struct.unpack_from("<HH", rows, y * 8) |
| 183 | offset = struct.unpack_from("<i", rows, y * 8 + 4)[0] | 194 | offset = struct.unpack_from("<i", rows, y * 8 + 4)[0] |
| 184 | if start + count > width: | 195 | if start + count > width: |
| 185 | raise ValueError(f"PAMH row {y} leaves the {width}-cell width") | 196 | raise ValueError(f"PAMH row {y} leaves the {width}-cell width") |
| 197 | if start == 0 and count == width: | ||
| 198 | full_width += 1 | ||
| 186 | for i in range(count): | 199 | for i in range(count): |
| 187 | x = start + i | 200 | x = start + i |
| 188 | at = offset + x | 201 | at = offset + x |
| @@ -197,7 +210,7 @@ def parse_pamh(blob: bytes) -> dict: | |||
| 197 | "header": { | 210 | "header": { |
| 198 | "version_major": blob[0x04], | 211 | "version_major": blob[0x04], |
| 199 | "version_minor": blob[0x05], | 212 | "version_minor": blob[0x05], |
| 200 | "flag": _u32(blob, 0x08), | 213 | "padding08": _u32(blob, 0x08), |
| 201 | "width": width, | 214 | "width": width, |
| 202 | "height": height, | 215 | "height": height, |
| 203 | "bounds": bounds, | 216 | "bounds": bounds, |
| @@ -210,6 +223,7 @@ def parse_pamh(blob: bytes) -> dict: | |||
| 210 | "height": height, | 223 | "height": height, |
| 211 | "cells": width * height, | 224 | "cells": width * height, |
| 212 | "row_records": height, | 225 | "row_records": height, |
| 226 | "full_width_rows": full_width, | ||
| 213 | "max_min": min(maximum), | 227 | "max_min": min(maximum), |
| 214 | "max_max": max(maximum), | 228 | "max_max": max(maximum), |
| 215 | "min_min": min(minimum), | 229 | "min_min": min(minimum), |
| @@ -221,6 +235,52 @@ def parse_pamh(blob: bytes) -> dict: | |||
| 221 | } | 235 | } |
| 222 | 236 | ||
| 223 | 237 | ||
| 238 | def pamh_census(payloads: list[dict]) -> list[dict]: | ||
| 239 | """Group decoded PAMH payloads by grid size and count what varies inside a group. | ||
| 240 | |||
| 241 | The 0x08 word is padding, so it is counted, not used as a key. Byte 0x05 is the HMAP | ||
| 242 | version-minor byte in the common.rpf payload but holds text in the levels.rpf tiles, so it is | ||
| 243 | reported as a distribution instead of a group key.""" | ||
| 244 | groups = {} | ||
| 245 | for p in payloads: | ||
| 246 | if p["type"] != "PAMH": | ||
| 247 | continue | ||
| 248 | h, g = p["header"], p["grid"] | ||
| 249 | grp = groups.get((g["width"], g["height"])) | ||
| 250 | if grp is None: | ||
| 251 | grp = groups[(g["width"], g["height"])] = { | ||
| 252 | "width": g["width"], | ||
| 253 | "height": g["height"], | ||
| 254 | "tiles": 0, | ||
| 255 | "cells": 0, | ||
| 256 | "bounds_min": list(h["bounds"]["min"]), | ||
| 257 | "bounds_max": list(h["bounds"]["max"]), | ||
| 258 | "full_width_row_tiles": 0, | ||
| 259 | "min_above_max": 0, | ||
| 260 | "version_minor": {}, | ||
| 261 | "padding08_zero": 0, | ||
| 262 | "_padding": set(), | ||
| 263 | } | ||
| 264 | grp["tiles"] += 1 | ||
| 265 | grp["cells"] += g["cells"] | ||
| 266 | for i in range(3): | ||
| 267 | grp["bounds_min"][i] = min(grp["bounds_min"][i], h["bounds"]["min"][i]) | ||
| 268 | grp["bounds_max"][i] = max(grp["bounds_max"][i], h["bounds"]["max"][i]) | ||
| 269 | if g["full_width_rows"] == g["height"]: | ||
| 270 | grp["full_width_row_tiles"] += 1 | ||
| 271 | grp["min_above_max"] += g["min_above_max"] | ||
| 272 | minor = str(h["version_minor"]) | ||
| 273 | grp["version_minor"][minor] = grp["version_minor"].get(minor, 0) + 1 | ||
| 274 | if h["padding08"] == 0: | ||
| 275 | grp["padding08_zero"] += 1 | ||
| 276 | grp["_padding"].add(h["padding08"]) | ||
| 277 | out = list(groups.values()) | ||
| 278 | for grp in out: | ||
| 279 | grp["padding08_distinct"] = len(grp.pop("_padding")) | ||
| 280 | out.sort(key=lambda x: (-x["tiles"], x["width"], x["height"])) | ||
| 281 | return out | ||
| 282 | |||
| 283 | |||
| 224 | def parse_payload(blob: bytes) -> dict: | 284 | def parse_payload(blob: bytes) -> dict: |
| 225 | if blob[:4] == b"MHIU": | 285 | if blob[:4] == b"MHIU": |
| 226 | return parse_mhiu(blob) | 286 | return parse_mhiu(blob) |
| @@ -310,9 +370,18 @@ def main() -> int: | |||
| 310 | f'{g["min_above_max"]} cells min>max') | 370 | f'{g["min_above_max"]} cells min>max') |
| 311 | if not payloads: | 371 | if not payloads: |
| 312 | print("no MHIU or PAMH payloads") | 372 | print("no MHIU or PAMH payloads") |
| 373 | census = pamh_census(payloads) | ||
| 374 | for grp in census: | ||
| 375 | print(f'PAMH census {grp["width"]}x{grp["height"]}: {grp["tiles"]} tiles, ' | ||
| 376 | f'{grp["full_width_row_tiles"]} with full-width rows, ' | ||
| 377 | f'{grp["min_above_max"]} cells min>max, padding 08 zero {grp["padding08_zero"]}, ' | ||
| 378 | f'{grp["padding08_distinct"]} distinct') | ||
| 313 | if a.json: | 379 | if a.json: |
| 380 | out = {"payloads": [_public(p) for p in payloads]} | ||
| 381 | if census: | ||
| 382 | out["pamh_census"] = census | ||
| 314 | with open(a.json, "w", encoding="utf-8") as f: | 383 | with open(a.json, "w", encoding="utf-8") as f: |
| 315 | json.dump({"payloads": [_public(p) for p in payloads]}, f, indent=1) | 384 | json.dump(out, f, indent=1) |
| 316 | print("wrote", a.json) | 385 | print("wrote", a.json) |
| 317 | return 0 | 386 | return 0 |
| 318 | 387 | ||