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