yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
object_parser.cc
Go to the documentation of this file.
1#include "object_parser.h"
2
3#include <algorithm>
4#include <cstring>
5#include <tuple>
6
7#include "absl/strings/str_format.h"
8#include "util/log.h"
11
12// ROM addresses for object data are defined in room_object.h:
13// - kRoomObjectSubtype1 = 0x8000 (SNES $01:8000)
14// - kRoomObjectSubtype2 = 0x83F0 (SNES $01:83F0)
15// - kRoomObjectSubtype3 = 0x84F0 (SNES $01:84F0)
16// - kRoomObjectTileAddress = 0x1B52 (SNES $00:9B52)
17
18// Subtype 1 tile count lookup table.
19//
20// Each entry specifies how many tiles the corresponding `RoomDraw_*`
21// routine reads from the per-object tile stream. Index directly by
22// `(object_id & 0xFF)` for subtype 1 objects.
23//
24// Audit history:
25// - Initial table sourced from ZScream's `DungeonObjectData.cs`.
26// - 2026-04-25 audit (zelda3-hacking-expert): IDs `0x47` and `0x48`
27// stored as 0 (fallback to 8) but routine bodies in
28// `special_routines.cc::DrawWaterfall47` and `DrawWaterfall48`
29// deterministically index tile slots 0..14 and 0..8 respectively
30// (three 1x5 / 1x3 columns). The under-fetched 8-tile vector is
31// not a bounds bug — `TileAtWrapped` wraps `index % tiles.size()`
32// — but the wrap quietly substitutes the wrong tile for the right
33// column (e.g. Waterfall47 column 3 indexes tile 12, which wraps
34// to tile 4 with size 8). Updated to the real counts so the wrap
35// never fires for vanilla data.
36// - IDs `0xD3..0xD6` are routed to `DrawNothing` (logic-only;
37// `CheckIfWallIsMoved` flag tests). Their tile-count entry is `0`
38// because those routines have no tile payload. Other uncataloged zero
39// entries still use the conservative 8-tile fallback.
40// - IDs `0xCD` and `0xCE` consume 24 words: top corner (9), vertical
41// pattern (6), and bottom corner (9). ZScream's count of 28 crosses into
42// the next object's data block, so yaze uses the routine-proven count.
43// - ID `0x3C` consumes 8 words for two 2x2 stamps; ZScream's count of 4
44// under-fetches the second stamp.
45// - Bar `0x4C` consumes 9 words: opening, repeated middle, and closing 1x3
46// columns ($0194BD, obj099E at $00A4F0). Its smallest raster is 4x3, but
47// the repeated middle column does not require additional source words.
48// - Bar `0x8F` consumes 4 words: a 2-tile top row and repeated 2-tile body
49// ($0197B5, obj09B0 at $00A502). Reading 6 crosses into the next corner.
50// - 2026-04-25 ZScream parity diff: full byte-for-byte comparison
51// against ZScream's `subtype1Lengths` (`DungeonObjectData.cs:184`)
52// remains pinned, with routine-body-proven corrections allowlisted.
53// The pin lives in `object_parser_test.cc::Subtype1TileLengths`
54// `MatchesZScreamReferenceExceptAtKnownDivergences` and detects
55// future drift in either direction.
56// clang-format off
57static constexpr uint8_t kSubtype1TileLengths[0xF8] = {
58 4, 8, 8, 8, 8, 8, 8, 4, 4, 5, 5, 5, 5, 5, 5, 5, // 0x00-0x0F
59 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, // 0x10-0x1F
60 5, 9, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 6, // 0x20-0x2F
61 6, 1, 1, 16, 1, 1, 16, 16, 6, 8, 12, 12, 8, 8, 4, 3, // 0x30-0x3F (0x3C=Doubled2x2:8)
62 3, 3, 3, 3, 3, 3, 3, 15, 9, 8, 8, 4, 9, 16, 16, 16, // 0x40-0x4F (0x47=Waterfall47:15, 0x48=Waterfall48:9, 0x4C=Bar4x3:9)
63 1, 18, 18, 4, 1, 8, 8, 1, 1, 1, 1, 18, 18, 15, 4, 3, // 0x50-0x5F
64 4, 8, 8, 8, 8, 8, 8, 4, 4, 3, 1, 1, 6, 6, 1, 1, // 0x60-0x6F
65 16, 1, 1, 16, 16, 8, 16, 16, 4, 1, 1, 4, 1, 4, 1, 8, // 0x70-0x7F
66 8, 12, 12, 12, 12, 18, 18, 8, 12, 4, 3, 3, 3, 1, 1, 4, // 0x80-0x8F (0x8F=Bar2x5:4)
67 8, 8, 4, 4, 16, 4, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x90-0x9F
68 1, 1, 1, 1, 24, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0xA0-0xAF
69 1, 1, 16, 3, 3, 8, 8, 8, 4, 4, 16, 4, 4, 4, 1, 1, // 0xB0-0xBF
70 1, 68, 1, 1, 8, 8, 8, 8, 8, 8, 8, 1, 1, 24, 24, 1, // 0xC0-0xCF (0xCD/0xCE use 24-word platform payloads)
71 1, 8, 8, 0, 0, 0, 0, 1, 8, 8, 8, 8, 21, 16, 4, 8, // 0xD0-0xDF (0xD3..0xD6: DrawNothing logic; tiles unused)
72 8, 8, 8, 8, 8, 8, 8, 8, 8, 1, 1, 1, 1, 1, 1, 1, // 0xE0-0xEF
73 1, 1, 1, 1, 1, 1, 1, 1 // 0xF0-0xF7
74};
75// clang-format on
76
77static inline bool IsSubtype1LogicOnlyZeroTileObject(int object_id) {
78 int index = object_id & 0xFF;
79 return index >= 0xD3 && index <= 0xD6;
80}
81
82// Helper function to get tile count for Subtype 1 objects.
83static inline int GetSubtype1TileCount(int object_id) {
84 int index = object_id & 0xFF;
85 if (index < 0xF8) {
86 int count = kSubtype1TileLengths[index];
87 if (count > 0) {
88 return count;
89 }
90 if (IsSubtype1LogicOnlyZeroTileObject(index)) {
91 return 0;
92 }
93 return 8; // Default to 8 if an uncataloged table entry has 0.
94 }
95 return 8; // Default for IDs >= 0xF8
96}
97
98namespace yaze {
99namespace zelda3 {
100
101absl::StatusOr<std::vector<gfx::TileInfo>> ObjectParser::ParseObject(
102 int16_t object_id) {
103 if (rom_ == nullptr) {
104 return absl::InvalidArgumentError("ROM is null");
105 }
106
107 // Validate object ID is non-negative
108 if (object_id < 0) {
109 return absl::InvalidArgumentError(
110 absl::StrFormat("Invalid object ID: %d", object_id));
111 }
112
113 int subtype = DetermineSubtype(object_id);
114
115 switch (subtype) {
116 case 1:
117 return ParseSubtype1(object_id);
118 case 2:
119 return ParseSubtype2(object_id);
120 case 3:
121 return ParseSubtype3(object_id);
122 default:
123 return absl::InvalidArgumentError(
124 absl::StrFormat("Invalid object subtype for ID: %#04x", object_id));
125 }
126}
127
128absl::StatusOr<std::vector<ObjectTileReadRange>>
130 if (tile_read_range_collector_ != nullptr) {
131 return absl::FailedPreconditionError(
132 "Object tile source resolution is already active");
133 }
134
135 std::vector<ObjectTileReadRange> ranges;
137 const auto parsed_or = ParseObject(object_id);
139 if (!parsed_or.ok()) {
140 return parsed_or.status();
141 }
142
143 std::sort(ranges.begin(), ranges.end(),
144 [](const ObjectTileReadRange& lhs, const ObjectTileReadRange& rhs) {
145 return std::tie(lhs.begin, lhs.end) <
146 std::tie(rhs.begin, rhs.end);
147 });
148 ranges.erase(std::unique(ranges.begin(), ranges.end()), ranges.end());
149 return ranges;
150}
151
152absl::StatusOr<ObjectRoutineInfo> ObjectParser::ParseObjectRoutine(
153 int16_t object_id) {
154 if (rom_ == nullptr) {
155 return absl::InvalidArgumentError("ROM is null");
156 }
157
158 auto subtype_info = GetObjectSubtype(object_id);
159 if (!subtype_info.ok()) {
160 return subtype_info.status();
161 }
162
163 ObjectRoutineInfo routine_info;
164 routine_info.routine_ptr = subtype_info->routine_ptr;
165 routine_info.tile_ptr = subtype_info->subtype_ptr;
166 routine_info.tile_count = subtype_info->max_tile_count;
167 routine_info.is_repeatable = true;
168 routine_info.is_orientation_dependent = true;
169
170 return routine_info;
171}
172
173absl::StatusOr<ObjectSubtypeInfo> ObjectParser::GetObjectSubtype(
174 int16_t object_id) {
176 info.subtype = DetermineSubtype(object_id);
177
178 switch (info.subtype) {
179 case 1: {
180 int index = object_id & 0xFF;
181 info.subtype_ptr = kRoomObjectSubtype1 + (index * 2);
182 info.routine_ptr = kRoomObjectSubtype1 + 0x200 + (index * 2);
183 break;
184 }
185 case 2: {
186 // Type 2 objects: 0x100-0x13F (64 objects only)
187 // Index mask 0x3F ensures we stay within 64-entry table bounds
188 int index = (object_id - 0x100) & 0x3F;
189 info.subtype_ptr = kRoomObjectSubtype2 + (index * 2);
190 // Routine table starts 128 bytes (64 entries * 2 bytes) after data table
191 info.routine_ptr = kRoomObjectSubtype2 + 0x80 + (index * 2);
192 break;
193 }
194 case 3: {
195 // Type 3 object IDs are 0xF80-0xFFF (128 objects)
196 // Table index should be 0-127
197 int index = (object_id - 0xF80) & 0x7F;
198 info.subtype_ptr = kRoomObjectSubtype3 + (index * 2);
199 info.routine_ptr = kRoomObjectSubtype3 + 0x100 + (index * 2);
200 break;
201 }
202 default:
203 return absl::InvalidArgumentError(
204 absl::StrFormat("Invalid object subtype for ID: %#04x", object_id));
205 }
206
208
209 return info;
210}
211
212absl::StatusOr<ObjectSizeInfo> ObjectParser::ParseObjectSize(
213 int16_t object_id, uint8_t size_byte) {
214 ObjectSizeInfo info;
215
216 // Extract size bits (0-3 for X, 4-7 for Y)
217 int size_x = size_byte & 0x03;
218 int size_y = (size_byte >> 2) & 0x03;
219
220 info.width_tiles = (size_x + 1) * 2; // Convert to tile count
221 info.height_tiles = (size_y + 1) * 2;
222
223 // Determine orientation based on object ID and size
224 // This is a heuristic based on the object naming patterns
225 if (object_id >= 0x80 && object_id <= 0xFF) {
226 // Objects 0x80-0xFF are typically vertical
227 info.is_horizontal = false;
228 } else {
229 // Objects 0x00-0x7F are typically horizontal
230 info.is_horizontal = true;
231 }
232
233 // Determine if object is repeatable
234 info.is_repeatable = (size_byte != 0);
235 info.repeat_count = size_byte == 0 ? 32 : size_byte;
236
237 return info;
238}
239
240absl::StatusOr<std::vector<gfx::TileInfo>> ObjectParser::ParseSubtype1(
241 int16_t object_id) {
242 int index = object_id & 0xFF;
243 int tile_ptr = kRoomObjectSubtype1 + (index * 2);
244
245 if (tile_ptr + 1 >= (int)rom_->size()) {
246 return absl::OutOfRangeError(
247 absl::StrFormat("Tile pointer out of range: %#06x", tile_ptr));
248 }
249
250 // Read tile data pointer (16-bit little-endian offset)
251 uint8_t low = rom_->data()[tile_ptr];
252 uint8_t high = rom_->data()[tile_ptr + 1];
253 int16_t offset = (int16_t)((high << 8) | low); // Signed offset!
254 int tile_data_ptr = kRoomObjectTileAddress + offset;
255
256 // DEBUG: Log wall objects 0x61/0x62 and ceiling 0xC0 to verify tile data
257 bool is_debug_object = (object_id == 0x61 || object_id == 0x62 ||
258 object_id == 0xC0 || object_id == 0xC2);
259 static int debug_count = 0;
260 if (debug_count < 10 || is_debug_object) {
261 LOG_DEBUG("ObjectParser",
262 "ParseSubtype1: obj=0x%02X%s tile_ptr=0x%04X (SNES $01:%04X)",
263 object_id, is_debug_object ? " (DEBUG)" : "", tile_ptr, tile_ptr);
264 LOG_DEBUG("ObjectParser",
265 " ROM[0x%04X..0x%04X]=0x%02X 0x%02X offset=%d (0x%04X)",
266 tile_ptr, tile_ptr + 1, low, high, offset, (uint16_t)offset);
267 LOG_DEBUG("ObjectParser",
268 " tile_data_ptr=0x%04X+0x%04X=0x%04X (SNES $00:%04X)",
269 kRoomObjectTileAddress, (uint16_t)offset, tile_data_ptr,
270 tile_data_ptr + 0x8000);
271
272 // Fix: Check for negative tile_data_ptr to prevent SIGSEGV on corrupted ROMs
273 if (tile_data_ptr >= 0 && tile_data_ptr + 8 < (int)rom_->size()) {
274 uint16_t tw0 =
275 rom_->data()[tile_data_ptr] | (rom_->data()[tile_data_ptr + 1] << 8);
276 uint16_t tw1 = rom_->data()[tile_data_ptr + 2] |
277 (rom_->data()[tile_data_ptr + 3] << 8);
278 LOG_DEBUG("ObjectParser", " First 2 tiles: $%04X(id=%d) $%04X(id=%d)",
279 tw0, tw0 & 0x3FF, tw1, tw1 & 0x3FF);
280 } else {
281 LOG_DEBUG("ObjectParser", " Tile data at 0x%04X: <OUT OF BOUNDS>",
282 tile_data_ptr);
283 }
284 if (!is_debug_object)
285 debug_count++;
286 }
287
288 // Use lookup table for correct tile count per object ID
289 int tile_count = GetSubtype1TileCount(object_id);
290 return ReadTileData(tile_data_ptr, tile_count);
291}
292
293absl::StatusOr<std::vector<gfx::TileInfo>> ObjectParser::ParseSubtype2(
294 int16_t object_id) {
295 constexpr int kDamFloodGateOpenTileOffset = 0x13E8;
296 constexpr int kDamFloodGateTileCount = 40;
297
298 // Type 2 objects: 0x100-0x13F (64 objects only)
299 int index = (object_id - 0x100) & 0x3F;
300 int tile_ptr = kRoomObjectSubtype2 + (index * 2);
301
302 if (tile_ptr + 1 >= (int)rom_->size()) {
303 return absl::OutOfRangeError(
304 absl::StrFormat("Tile pointer out of range: %#06x", tile_ptr));
305 }
306
307 // Read tile data pointer
308 uint8_t low = rom_->data()[tile_ptr];
309 uint8_t high = rom_->data()[tile_ptr + 1];
310 int tile_data_ptr = kRoomObjectTileAddress + ((high << 8) | low);
311
312 // Determine tile count based on object ID
313 int tile_count = GetSubtype2TileCount(object_id);
314
315 // DEBUG: Log corner tile loading (0x100-0x103)
316 bool is_corner = (object_id >= 0x100 && object_id <= 0x103);
317 if (is_corner) {
318 LOG_DEBUG("ObjectParser",
319 "ParseSubtype2: CORNER obj=0x%03X index=%d tile_ptr=0x%04X",
320 object_id, index, tile_ptr);
321 LOG_DEBUG("ObjectParser",
322 " ROM[0x%04X..0x%04X]=0x%02X 0x%02X offset=0x%04X", tile_ptr,
323 tile_ptr + 1, low, high, (high << 8) | low);
324 LOG_DEBUG(
325 "ObjectParser", " tile_data_ptr=0x%04X+0x%04X=0x%04X (tile_count=%d)",
326 kRoomObjectTileAddress, (high << 8) | low, tile_data_ptr, tile_count);
327
328 // Show first 2 tile words
329 if (tile_data_ptr >= 0 && tile_data_ptr + 4 < (int)rom_->size()) {
330 uint16_t tw0 =
331 rom_->data()[tile_data_ptr] | (rom_->data()[tile_data_ptr + 1] << 8);
332 uint16_t tw1 = rom_->data()[tile_data_ptr + 2] |
333 (rom_->data()[tile_data_ptr + 3] << 8);
334 LOG_DEBUG("ObjectParser", " First 2 tiles: $%04X(id=%d) $%04X(id=%d)",
335 tw0, tw0 & 0x3FF, tw1, tw1 & 0x3FF);
336 }
337 }
338
339 if (object_id == 0x137) {
340 auto closed_tiles = ReadTileData(tile_data_ptr, kDamFloodGateTileCount);
341 if (!closed_tiles.ok()) {
342 return closed_tiles.status();
343 }
344 auto open_tiles =
345 ReadTileData(kRoomObjectTileAddress + kDamFloodGateOpenTileOffset,
346 kDamFloodGateTileCount);
347 if (!open_tiles.ok()) {
348 return open_tiles.status();
349 }
350 closed_tiles->insert(closed_tiles->end(), open_tiles->begin(),
351 open_tiles->end());
352 return closed_tiles;
353 }
354
355 return ReadTileData(tile_data_ptr, tile_count);
356}
357
358absl::StatusOr<std::vector<gfx::TileInfo>> ObjectParser::ParseSubtype3(
359 int16_t object_id) {
360 constexpr int kBombableFloorOpenTileOffset = 0x05BA;
361 constexpr int kBombableFloorStateTileCount = 16;
362 constexpr int kPrisonCellTileOffset = 0x1488;
363 constexpr int kPrisonCellTileCount = 6;
364 constexpr int kRupeeFloorTileOffset = 0x1DD6;
365 constexpr int kRupeeFloorTileCount = 2;
366 constexpr int kBigKeyLockTileOffset = 0x1494;
367 constexpr int kClosedChestTileOffset = 0x149C;
368 constexpr int kOpenChestTileOffset = 0x14A4;
369 constexpr int kChestStateTileCount = 4;
370 constexpr int kClosedBigChestTileOffset = 0x14AC;
371 constexpr int kOpenBigChestTileOffset = 0x14C4;
372 constexpr int kBigChestStateTileCount = 12;
373 constexpr int kBigGrayRockTileOffset = 0x0E62;
374 constexpr int kBigGrayRockTileCount = 16;
375 constexpr int kAgahnimsAltarTileOffset = 0x1B4A;
376 constexpr int kAgahnimsAltarTileCount = 84;
377 constexpr int kFortuneTellerRoomTileOffset = 0x202E;
378 constexpr int kFortuneTellerRoomTileCount = 26;
379 constexpr int kSmithyFurnaceTileOffset = 0x1F92;
380 constexpr int kSmithyFurnaceTileCount = 48;
381
382 // Type 3 object IDs are 0xF80-0xFFF (128 objects)
383 // Table index should be 0-127, calculated by subtracting base offset 0xF80
384 int index = (object_id - 0xF80) & 0x7F;
385 int tile_ptr = kRoomObjectSubtype3 + (index * 2);
386
387 if (tile_ptr + 1 >= (int)rom_->size()) {
388 return absl::OutOfRangeError(
389 absl::StrFormat("Tile pointer out of range: %#06x", tile_ptr));
390 }
391
392 // Read tile data pointer
393 uint8_t low = rom_->data()[tile_ptr];
394 uint8_t high = rom_->data()[tile_ptr + 1];
395 int tile_data_ptr = kRoomObjectTileAddress + ((high << 8) | low);
396
397 // Determine tile count based on object ID
398 int tile_count = GetSubtype3TileCount(object_id);
399
400 // BombableFloor's intact 4x4 block starts at the object's pointer, while
401 // USDASM's replacement 4x4 block lives separately at obj05BA. Keep the two
402 // 16-word states adjacent in the parsed payload for stateful rendering.
403 if (object_id == 0xFC7) {
404 auto intact_tiles =
405 ReadTileData(tile_data_ptr, kBombableFloorStateTileCount);
406 if (!intact_tiles.ok()) {
407 return intact_tiles.status();
408 }
409 auto open_tiles =
410 ReadTileData(kRoomObjectTileAddress + kBombableFloorOpenTileOffset,
411 kBombableFloorStateTileCount);
412 if (!open_tiles.ok()) {
413 return open_tiles.status();
414 }
415 intact_tiles->insert(intact_tiles->end(), open_tiles->begin(),
416 open_tiles->end());
417 return intact_tiles;
418 }
419
420 // Both PrisonCell IDs dispatch to RoomDraw_PrisonCell, which ignores the
421 // subtype table pointer and loads literal obj1488. F8D's vanilla table entry
422 // does not point there, so following the table would render the two aliases
423 // differently even though the game does not.
424 if (object_id == 0xF8D || object_id == 0xF97) {
425 return ReadTileData(kRoomObjectTileAddress + kPrisonCellTileOffset,
426 kPrisonCellTileCount);
427 }
428
429 // RupeeFloor replaces the table-derived pointer with literal obj1DD6 and
430 // repeats those two words across its fixed footprint. Following the table
431 // here would hide a real shared-source overlap from both rendering and the
432 // source-impact resolver.
433 if (object_id == 0xF92) {
434 return ReadTileData(kRoomObjectTileAddress + kRupeeFloorTileOffset,
435 kRupeeFloorTileCount);
436 }
437
438 // BigKeyLock overwrites the table-derived X register with obj1494 before
439 // drawing. Follow the runtime literal so a relocated F98 table entry does
440 // not change the editor while the game continues using obj1494.
441 if (object_id == 0xF98) {
442 return ReadTileData(kRoomObjectTileAddress + kBigKeyLockTileOffset,
443 kChestStateTileCount);
444 }
445
446 // Chest overwrites X with obj149C/obj14A4 for its closed/open branches.
447 if (object_id == 0xF99) {
448 auto closed_tiles = ReadTileData(
449 kRoomObjectTileAddress + kClosedChestTileOffset, kChestStateTileCount);
450 if (!closed_tiles.ok()) {
451 return closed_tiles.status();
452 }
453 auto open_tiles = ReadTileData(
454 kRoomObjectTileAddress + kOpenChestTileOffset, kChestStateTileCount);
455 if (!open_tiles.ok()) {
456 return open_tiles.status();
457 }
458 closed_tiles->insert(closed_tiles->end(), open_tiles->begin(),
459 open_tiles->end());
460 return closed_tiles;
461 }
462
463 // BigChest likewise overwrites X with obj14AC/obj14C4. F9A/FB2 remain
464 // direct-open routines and legitimately use their table-derived pointers.
465 if (object_id == 0xFB1) {
466 auto closed_tiles =
467 ReadTileData(kRoomObjectTileAddress + kClosedBigChestTileOffset,
468 kBigChestStateTileCount);
469 if (!closed_tiles.ok()) {
470 return closed_tiles.status();
471 }
472 auto open_tiles =
473 ReadTileData(kRoomObjectTileAddress + kOpenBigChestTileOffset,
474 kBigChestStateTileCount);
475 if (!open_tiles.ok()) {
476 return open_tiles.status();
477 }
478 closed_tiles->insert(closed_tiles->end(), open_tiles->begin(),
479 open_tiles->end());
480 return closed_tiles;
481 }
482
483 // BigGrayRock replaces the table-derived data pointer with literal obj0E62,
484 // then draws its four contiguous 2x2 quadrants. Follow the runtime literal
485 // so a relocated FAC table entry cannot change only the editor rendering.
486 if (object_id == 0xFAC) {
487 return ReadTileData(kRoomObjectTileAddress + kBigGrayRockTileOffset,
488 kBigGrayRockTileCount);
489 }
490
491 // AgahnimsAltar replaces the table-derived pointer with literal obj1B4A,
492 // then mirrors its six source columns into a fixed 14x14 destination.
493 if (object_id == 0xFAD) {
494 return ReadTileData(kRoomObjectTileAddress + kAgahnimsAltarTileOffset,
495 kAgahnimsAltarTileCount);
496 }
497
498 // FortuneTellerRoom ignores its table-derived pointer and loads literal
499 // obj202E before writing a sparse, fixed 14x14 room facade.
500 if (object_id == 0xFD4) {
501 return ReadTileData(kRoomObjectTileAddress + kFortuneTellerRoomTileOffset,
502 kFortuneTellerRoomTileCount);
503 }
504
505 // SmithyFurnace replaces the table-derived data pointer with literal
506 // obj1F92 before drawing. Mirror the runtime source rather than allowing a
507 // relocated FCC table entry to change only the editor's rendering.
508 if (object_id == 0xFCC) {
509 return ReadTileData(kRoomObjectTileAddress + kSmithyFurnaceTileOffset,
510 kSmithyFurnaceTileCount);
511 }
512
513 return ReadTileData(tile_data_ptr, tile_count);
514}
515
516absl::StatusOr<std::vector<gfx::TileInfo>> ObjectParser::ReadTileData(
517 int address, int tile_count) {
518 // Each tile is stored as a 16-bit word (2 bytes), not 8 bytes!
519 // ZScream: tiles.Add(new Tile(ROM.DATA[pos + ((i * 2))], ROM.DATA[pos + ((i *
520 // 2)) + 1]));
521 if (tile_count < 0) {
522 return absl::InvalidArgumentError(
523 absl::StrFormat("Invalid tile count: %d", tile_count));
524 }
525 if (tile_count == 0) {
526 return std::vector<gfx::TileInfo>{};
527 }
528 if (address < 0 || address + (tile_count * 2) >= (int)rom_->size()) {
529 return absl::OutOfRangeError(
530 absl::StrFormat("Tile data address out of range: %#06x", address));
531 }
532
533 if (tile_read_range_collector_ != nullptr) {
535 {static_cast<uint32_t>(address),
536 static_cast<uint32_t>(address + tile_count * 2)});
537 }
538
539 std::vector<gfx::TileInfo> tiles;
540 tiles.reserve(tile_count);
541
542 // DEBUG: Log first tile read
543 static int debug_read_count = 0;
544 bool should_log = (debug_read_count < 3);
545
546 for (int i = 0; i < tile_count; i++) {
547 int tile_offset = address + (i * 2); // 2 bytes per tile word
548
549 // Read 1 word (2 bytes) per tile - this is the SNES tile format
550 uint16_t tile_word =
551 rom_->data()[tile_offset] | (rom_->data()[tile_offset + 1] << 8);
552
553 auto tile_info = gfx::WordToTileInfo(tile_word);
554 tiles.push_back(tile_info);
555
556 // DEBUG: Log first few tiles
557 if (should_log && i < 4) {
558 LOG_DEBUG("ObjectParser",
559 "ReadTile[%d]: addr=0x%06X word=0x%04X id=0x%03X pal=%d "
560 "mirror=(h:%d,v:%d)",
561 i, tile_offset, tile_word, tile_info.id_, tile_info.palette_,
562 tile_info.horizontal_mirror_, tile_info.vertical_mirror_);
563 }
564 }
565
566 if (should_log) {
567 LOG_DEBUG("ObjectParser",
568 "ReadTileData: addr=0x%06X count=%d loaded %zu tiles", address,
569 tile_count, tiles.size());
570 debug_read_count++;
571 }
572
573 return tiles;
574}
575
576int ObjectParser::GetSubtype2TileCount(int16_t object_id) const {
577 // 4x4 corners (0x100-0x10F): 16 tiles (32 bytes)
578 // These are RoomDraw_4x4 and RoomDraw_4x4Corner_BothBG routines
579 if (object_id >= 0x100 && object_id <= 0x10F) {
580 return 16;
581 }
582 // Weird corners (0x110-0x117): 12 tiles (24 bytes)
583 // These are RoomDraw_WeirdCornerBottom_BothBG and RoomDraw_WeirdCornerTop_BothBG
584 if (object_id >= 0x110 && object_id <= 0x117) {
585 return 12;
586 }
587 // Enabled star switch and lit torch are fixed 2x2 patterns.
588 if (object_id == 0x11F || object_id == 0x120) {
589 return 4;
590 }
591 // 4x4 fixed patterns (stairs/walls)
592 if (object_id == 0x11C || object_id == 0x124 || object_id == 0x125 ||
593 object_id == 0x129 || (object_id >= 0x12D && object_id <= 0x133)) {
594 return 16;
595 }
596 // Sanctuary wall: two 1x6 facade columns plus a 4x3 center (obj1458).
597 if (object_id == 0x13C) {
598 return 24;
599 }
600 // Magic Bat altar is subtype-2 object 0x13F (table index 0x3F).
601 // RoomDraw_MagicBatAltar consumes eight columns of seven words from obj2086.
602 if (object_id == 0x13F) {
603 return 56;
604 }
605 // Water hop stairs use a fixed 4x2 pattern.
606 if (object_id == 0x135 || object_id == 0x136) {
607 return 8;
608 }
609 // Dam floodgate preserves both closed and water-open 10x4 tile blocks.
610 if (object_id == 0x137) {
611 return 80;
612 }
613 // Beds (4x5)
614 if (object_id == 0x122 || object_id == 0x128) {
615 return 20;
616 }
617 // Tables (4x3)
618 if (object_id == 0x123 || object_id == 0x13D) {
619 return 12;
620 }
621 // 3x6 and 6x3 utility patterns
622 if (object_id == 0x12C || object_id == 0x13E) {
623 return 18;
624 }
625 // Spiral stairs (4x3)
626 if (object_id >= 0x138 && object_id <= 0x13B) {
627 return 12;
628 }
629 // Default: 8 tiles for other subtype 2 objects
630 return 8;
631}
632
633int ObjectParser::GetSubtype3TileCount(int16_t object_id) const {
634 // Water face variants:
635 // - 0xF80 (Empty) may branch to the 0xF81 tile block at runtime, so we load
636 // both spans to preserve parity for state-dependent rendering.
637 // - 0xF81 (Spitting): 4x5
638 // - 0xF82 (Drenching): 4x7
639 if (object_id == 0xF80) {
640 return 32;
641 }
642 if (object_id == 0xF81) {
643 return 20;
644 }
645 if (object_id == 0xF82) {
646 return 28;
647 }
648
649 // RoomDraw_PrisonCell loads literal obj1488 and consumes its six words for
650 // both object aliases.
651 if (object_id == 0xF8D || object_id == 0xF97) {
652 return 6;
653 }
654
655 // RupeeFloor (0xF92 = ASM 0x212) reads the two words at obj1DD6 and
656 // repeats them at fixed positions.
657 if (object_id == 0xF92) {
658 return 2;
659 }
660
661 // HammerPegSingle (0xF96 = ASM 0x216) jumps to
662 // RoomDraw_Rightwards2x2 and consumes one fixed 2x2 tile block.
663 if (object_id == 0xF96) {
664 return 4;
665 }
666
667 // BigKeyLock (0xF98 = ASM 0x218) loads literal obj1494 and consumes exactly
668 // one fixed 2x2 block. The following four words belong to obj149C (Chest),
669 // not to an alternate lock state.
670 if (object_id == 0xF98) {
671 return 4;
672 }
673
674 // Chest (F99) carries both closed/open 2x2 states. OpenChest (F9A) is
675 // already the fixed open state and consumes one 2x2 payload.
676 if (object_id == 0xF99) {
677 return 8;
678 }
679 if (object_id == 0xF9A) {
680 return 4;
681 }
682
683 // BombableFloor (0xFC7 = ASM 0x247) has two non-contiguous 4x4 states:
684 // 16 intact words from the object pointer and 16 replacement words at
685 // obj05BA. ParseSubtype3 loads and concatenates both spans.
686 if (object_id == 0xFC7) {
687 return 32;
688 }
689
690 // Somaria path pieces (ASM objects 0x203-0x20C, 0x20E, and 0x20F) each
691 // point to one tile word. RoomDraw_SomariaLine writes that word once.
692 if ((object_id >= 0xF83 && object_id <= 0xF8C) || object_id == 0xF8E ||
693 object_id == 0xF8F) {
694 return 1;
695 }
696
697 // BigChest (FB1) carries closed/open 4x3 states. OpenBigChest (FB2) is the
698 // fixed open state. Both draw through RoomDraw_1x3N_rightwards with N=4.
699 if (object_id == 0xFB1) {
700 return 24;
701 }
702 if (object_id == 0xFB2) {
703 return 12;
704 }
705 // TableRock4x3 variants (0xF94, 0xFCE, 0xFE7-0xFE8, 0xFEC-0xFED): 12 tiles
706 if (object_id == 0xF94 || object_id == 0xFCE ||
707 (object_id >= 0xFE7 && object_id <= 0xFE8) ||
708 (object_id >= 0xFEC && object_id <= 0xFED)) {
709 return 12;
710 }
711 // 4x4 pattern objects: 16 tiles
712 // (0xFC8 = 0x248, 0xFE6 = 0x266, 0xFEB = 0x26B, 0xFFA = 0x27A)
713 if (object_id == 0xFC8 || object_id == 0xFE6 || object_id == 0xFEB ||
714 object_id == 0xFFA) {
715 return 16;
716 }
717 // Boss shells (4x4)
718 if (object_id == 0xF95 || object_id == 0xFF2) {
719 return 16;
720 }
721 // Vitreous goo damage reuses one 8-word 4x2 source stamp across a fixed
722 // 5x2 grid of 4x4 destinations.
723 if (object_id == 0xFFB) {
724 return 8;
725 }
726 // Auto stairs (4x4)
727 if ((object_id >= 0xF9B && object_id <= 0xF9D) || object_id == 0xFB3) {
728 return 16;
729 }
730 // Straight inter-room stairs (4x4)
731 if ((object_id >= 0xF9E && object_id <= 0xFA1) ||
732 (object_id >= 0xFA6 && object_id <= 0xFA9)) {
733 return 16;
734 }
735 // BigGrayRock (0xFAC = ASM 0x22C) loads obj0E62 and draws four fixed
736 // 2x2 quadrants, consuming 16 contiguous tile words.
737 if (object_id == 0xFAC) {
738 return 16;
739 }
740 // AgahnimsAltar (0xFAD = ASM 0x22D) consumes six 14-tile source columns
741 // from obj1B4A before mirroring them into a 14x14 destination.
742 if (object_id == 0xFAD) {
743 return 84;
744 }
745 // FortuneTellerRoom (0xFD4 = ASM 0x254) consumes all 26 words from obj202E
746 // while writing a sparse, fixed 14x14 BG1 footprint.
747 if (object_id == 0xFD4) {
748 return 26;
749 }
750 // Bar corners (0xFD6-0xFD9 = ASM 0x256-0x259) point at contiguous 4-word
751 // blocks obj09B8/obj09C0/obj09C8/obj09D0 and draw through
752 // RoomDraw_Rightwards2x2. The default 8 would read into the neighbor's
753 // block and make adjacent corners falsely alias in source-impact analysis.
754 if (object_id >= 0xFD6 && object_id <= 0xFD9) {
755 return 4;
756 }
757 // SmithyFurnace (0xFCC = ASM 0x24C) loads obj1F92 and draws a fixed
758 // 6-column x 8-row block through RoomDraw_SomeBigDecors.
759 if (object_id == 0xFCC) {
760 return 48;
761 }
762 // 4x4 single-pattern objects
763 if (object_id == 0xFAA || object_id == 0xFAE ||
764 (object_id >= 0xFB4 && object_id <= 0xFB9) || object_id == 0xFE2) {
765 return 16;
766 }
767 // Big wall decor aliases: RoomDraw_BigWallDecor calls
768 // RoomDraw_1x3N_rightwards with A=8, consuming 8 columns x 3 rows.
769 if (object_id == 0xFCB || object_id == 0xFF6 || object_id == 0xFF7) {
770 return 24;
771 }
772 // Turtle Rock pipes: 24 tiles (proven from routine bodies, not just
773 // dimensions table). DrawVerticalTurtleRockPipe (0xFBA, 0xFBB) draws two
774 // stacked 4x3 sections, reading tiles[0..11] then tiles[12..23].
775 // DrawHorizontalTurtleRockPipe (0xFBC, 0xFBD, 0xFDC / ASM 0x25C) calls
776 // DrawNx4 with columns=6, which reads a 6x4 column-major block
777 // (tiles[0..23]).
778 // Without this case the parser fell through to the default 8 and
779 // TileAtWrapped substituted tiles[0..7] for indices 8..23 -- same
780 // failure mode Waterfall47/48 had before e9938002.
781 if ((object_id >= 0xFBA && object_id <= 0xFBD) || object_id == 0xFDC) {
782 return 24;
783 }
784 // Utility 6x3 (18 tiles)
785 if (object_id == 0xFCD || object_id == 0xFDD) {
786 return 18;
787 }
788 // Utility 3x5 (15 tiles)
789 if (object_id == 0xFD5 || object_id == 0xFDB) {
790 return 15;
791 }
792 // Archery game target door (3x6, 18 tiles)
793 if (object_id >= 0xFE0 && object_id <= 0xFE1) {
794 return 18;
795 }
796 // Solid wall decor 3x4 (12 tiles)
797 if (object_id == 0xFE9 || object_id == 0xFEA || object_id == 0xFEE ||
798 object_id == 0xFEF) {
799 return 12;
800 }
801 // LightBeamOnFloor (0xFF0 / ASM 0x270) reads two unique 4x4 blocks:
802 // obj2376 is reused for the top and overlapping middle stamps, then
803 // obj2396 supplies the bottom stamp.
804 if (object_id == 0xFF0) {
805 return 32;
806 }
807 // BigLightBeamOnFloor (0xFF1 / ASM 0x271) draws four unique 4x4 blocks.
808 if (object_id == 0xFF1) {
809 return 64;
810 }
811 // FloorLight (0xFF4 / ASM 0x274) unconditionally draws four unique 4x4
812 // blocks in an 8x8 grid.
813 if (object_id == 0xFF4) {
814 return 64;
815 }
816 // Ganon Triforce floor decor (two 4x4 blocks -> 32 tiles)
817 if (object_id == 0xFF8) {
818 return 32;
819 }
820 // Table rock 4x3
821 if (object_id == 0xFF9) {
822 return 12;
823 }
824 // Default: 8 tiles for most subtype 3 objects
825 return 8;
826}
827
828int ObjectParser::DetermineSubtype(int16_t object_id) const {
829 // Type 3 IDs from decoding are 0xF80-0xFFF (b3 0xF8-0xFF shifted).
830 if (object_id >= 0xF80) {
831 return 3;
832 } else if (object_id >= 0x100) {
833 return 2;
834 } else {
835 return 1;
836 }
837}
838
839int ObjectParser::ResolveTileCountForObject(int16_t object_id) const {
840 if (object_id < 0) {
841 return 1;
842 }
843
844 switch (DetermineSubtype(object_id)) {
845 case 1:
846 return GetSubtype1TileCount(object_id);
847 case 2:
848 return GetSubtype2TileCount(object_id);
849 case 3:
850 return GetSubtype3TileCount(object_id);
851 default:
852 return 1;
853 }
854}
855
857 ObjectDrawInfo info;
858
859 // Keep draw-info tile counts sourced from subtype tables (ROM-facing data),
860 // while routine selection comes from DrawRoutineRegistry (renderer-facing
861 // canonical mapping).
862 info.tile_count = ResolveTileCountForObject(object_id);
863 const bool is_vertical_band = (object_id >= 0x60 && object_id <= 0x6F);
864 info.is_horizontal = !is_vertical_band;
865 info.is_vertical = is_vertical_band;
866
868 info.routine_name = "DefaultSolid";
869 info.both_layers = false;
870
871 auto& registry = DrawRoutineRegistry::Get();
872 // Feature flags (custom objects) can be toggled at runtime by project
873 // context, so keep parser metadata aligned with current registry mappings.
874 registry.RefreshFeatureFlagMappings();
875
876 int registry_routine = registry.GetRoutineIdForObject(object_id);
877 if (registry_routine < 0) {
878 return info;
879 }
880
881 info.draw_routine_id = registry_routine;
882 if (const DrawRoutineInfo* routine_info =
883 registry.GetRoutineInfo(registry_routine);
884 routine_info != nullptr) {
885 info.routine_name = routine_info->name;
886 info.both_layers = routine_info->draws_to_both_bgs;
887
888 switch (routine_info->category) {
890 info.is_horizontal = false;
891 info.is_vertical = true;
892 break;
894 info.is_horizontal = false;
895 info.is_vertical = false;
896 break;
897 default:
898 info.is_horizontal = true;
899 info.is_vertical = false;
900 break;
901 }
902 }
903
904 return info;
905}
906
907} // namespace zelda3
908} // namespace yaze
auto data() const
Definition rom.h:169
auto size() const
Definition rom.h:168
static DrawRoutineRegistry & Get()
absl::StatusOr< ObjectSubtypeInfo > GetObjectSubtype(int16_t object_id)
Get object subtype information.
absl::StatusOr< ObjectRoutineInfo > ParseObjectRoutine(int16_t object_id)
Parse object routine data.
absl::StatusOr< ObjectSizeInfo > ParseObjectSize(int16_t object_id, uint8_t size_byte)
Parse object size and orientation.
absl::StatusOr< std::vector< ObjectTileReadRange > > ResolveTileReadRanges(int16_t object_id)
Resolve every tile-data range consumed by ParseObject.
int GetSubtype3TileCount(int16_t object_id) const
Get tile count for subtype 3 objects.
std::vector< ObjectTileReadRange > * tile_read_range_collector_
absl::StatusOr< std::vector< gfx::TileInfo > > ParseSubtype2(int16_t object_id)
Parse subtype 2 objects (0x100-0x1FF)
absl::StatusOr< std::vector< gfx::TileInfo > > ParseObject(int16_t object_id)
Parse object data directly from ROM.
int GetSubtype2TileCount(int16_t object_id) const
Get tile count for subtype 2 objects.
absl::StatusOr< std::vector< gfx::TileInfo > > ParseSubtype1(int16_t object_id)
Parse subtype 1 objects (0x00-0xFF)
absl::StatusOr< std::vector< gfx::TileInfo > > ParseSubtype3(int16_t object_id)
Parse subtype 3 objects (0x200+)
absl::StatusOr< std::vector< gfx::TileInfo > > ReadTileData(int address, int tile_count)
Read tile data from ROM.
ObjectDrawInfo GetObjectDrawInfo(int16_t object_id) const
Get draw routine information for an object.
int ResolveTileCountForObject(int16_t object_id) const
int DetermineSubtype(int16_t object_id) const
Determine object subtype from ID.
#define LOG_DEBUG(category, format,...)
Definition log.h:103
TileInfo WordToTileInfo(uint16_t word)
Definition snes_tile.cc:378
constexpr int kRoomObjectSubtype3
Definition room_object.h:47
constexpr int kRoomObjectSubtype1
Definition room_object.h:45
constexpr int kRoomObjectSubtype2
Definition room_object.h:46
constexpr int kRoomObjectTileAddress
Definition room_object.h:48
Metadata about a draw routine.
Draw routine information for object rendering.
Object routine information.
Object size and orientation information.
Object subtype information.
One half-open PC range consumed while parsing an object's tile data.