yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
object_dimensions.cc
Go to the documentation of this file.
1#include "object_dimensions.h"
2
3#include <limits>
4
5#include "core/features.h"
9
10namespace yaze {
11namespace zelda3 {
12
13namespace {
14bool GetSomariaLineDimensions(int object_id, int* width, int* height) {
15 if (!width || !height) {
16 return false;
17 }
18 if (!((object_id >= 0xF83 && object_id <= 0xF8C) || object_id == 0xF8E ||
19 object_id == 0xF8F)) {
20 return false;
21 }
22
23 *width = 1;
24 *height = 1;
25 return true;
26}
27
28std::pair<int, int> GetClosedChestPlatformDimensions(int size) {
29 const int size_x = (size >> 2) & 0x03;
30 const int size_y = size & 0x03;
31 return {size_x * 2 + 14, size_y * 2 + 8};
32}
33
34std::pair<int, int> GetOpenChestPlatformDimensions(int size) {
35 const int size_x = (size >> 2) & 0x03;
36 const int size_y = size & 0x03;
37 return {size_x * 2 + 10, size_y * 2 + 7};
38}
39
40std::pair<int, int> GetMovingWallDimensions(int size) {
41 const int direction = moving_wall::DirectionForSize(size);
42 return {moving_wall::ObjectCountForSize(size) + 3, direction * 2 + 6};
43}
44} // namespace
45
47 static ObjectDimensionTable instance;
48 return instance;
49}
50
52 if (!rom || !rom->is_loaded()) {
53 return absl::FailedPreconditionError("ROM not loaded");
54 }
55
56 dimensions_.clear();
58
59 // Parse ROM tables for refinement
63
64 loaded_ = true;
65 return absl::OkStatus();
66}
67
69 int object_id) const {
70 auto it = dimensions_.find(object_id);
71 if (it != dimensions_.end()) {
72 return {it->second.base_width, it->second.base_height};
73 }
74 return {2, 2}; // Default 16x16 pixels (2x2 tiles)
75}
76
78 int size) const {
79 if (size != 0) {
80 return size;
81 }
82 if (entry.zero_size_override > 0) {
83 return entry.zero_size_override;
84 }
85 if (entry.use_32_when_zero) {
86 return 32;
87 }
88 return 0;
89}
90
91std::pair<int, int> ObjectDimensionTable::GetDimensions(int object_id,
92 int size) const {
93 int somaria_width = 0;
94 int somaria_height = 0;
95 if (GetSomariaLineDimensions(object_id, &somaria_width, &somaria_height)) {
96 return {somaria_width, somaria_height};
97 }
98 if (object_id == 0xC1) {
99 return GetClosedChestPlatformDimensions(size);
100 }
101 if (object_id == 0xDC) {
102 return GetOpenChestPlatformDimensions(size);
103 }
104 if (object_id == 0xCD || object_id == 0xCE) {
105 return GetMovingWallDimensions(size);
106 }
107 if (object_id == 0xD8 || object_id == 0xDA) {
108 int size_x = ((size >> 2) & 0x03);
109 int size_y = (size & 0x03);
110 int width = (size_x + 2) * 4;
111 int height = (size_y + 2) * 4;
112 return {width, height};
113 }
114 if (object_id == 0xDD) {
115 int size_x = ((size >> 2) & 0x03);
116 int size_y = (size & 0x03);
117 int width = 4 + (size_x * 2);
118 int height = 4 + (size_y * 2);
119 return {width, height};
120 }
121 auto it = dimensions_.find(object_id);
122 if (it == dimensions_.end()) {
123 // Unknown object - estimate from size
124 // ASM: When size is 0, default to 32 (not 1)
125 int s = (size == 0) ? 32 : size + 1;
126 return {2 * s, 2};
127 }
128
129 const auto& entry = it->second;
130 int w = entry.base_width;
131 int h = entry.base_height;
132
133 int effective_size = ResolveEffectiveSize(entry, size);
134
135 switch (entry.extend_dir) {
137 w += effective_size * entry.extend_multiplier;
138 break;
140 h += effective_size * entry.extend_multiplier;
141 break;
143 w += effective_size * entry.extend_multiplier;
144 h += effective_size * entry.extend_multiplier;
145 break;
147 // Diagonals extend both dimensions based on count
148 // Width = base_width + size, Height = base_height + size
149 // ASM: Each iteration draws 5 tiles vertically and advances X by 1
150 // Bounding box height = 5 + (count - 1) = count + 4
151 w += effective_size * entry.extend_multiplier;
152 h += effective_size * entry.extend_multiplier;
153 break;
155 // SuperSquare objects use subtype1 size's 2-bit X/Y fields
156 // size_x = ((size >> 2) & 0x03) + 1, size_y = (size & 0x03) + 1
157 int size_x = ((size >> 2) & 0x03) + 1;
158 int size_y = (size & 0x03) + 1;
159 w = size_x * entry.extend_multiplier;
160 h = size_y * entry.extend_multiplier;
161 break;
162 }
164 default:
165 break;
166 }
167
168 return {w, h};
169}
170
172 int object_id, int size) const {
173 int somaria_width = 0;
174 int somaria_height = 0;
175 if (GetSomariaLineDimensions(object_id, &somaria_width, &somaria_height)) {
176 return {somaria_width, somaria_height};
177 }
178 if (object_id == 0xC1) {
179 return GetClosedChestPlatformDimensions(size);
180 }
181 if (object_id == 0xDC) {
182 return GetOpenChestPlatformDimensions(size);
183 }
184 if (object_id == 0xCD || object_id == 0xCE) {
185 return GetMovingWallDimensions(size);
186 }
187 if (object_id == 0xD8 || object_id == 0xDA) {
188 int size_x = ((size >> 2) & 0x03);
189 int size_y = (size & 0x03);
190 int width = (size_x + 2) * 4;
191 int height = (size_y + 2) * 4;
192 return {width, height};
193 }
194 if (object_id == 0xDD) {
195 int size_x = ((size >> 2) & 0x03);
196 int size_y = (size & 0x03);
197 int width = 4 + (size_x * 2);
198 int height = 4 + (size_y * 2);
199 return {width, height};
200 }
201 auto it = dimensions_.find(object_id);
202 if (it == dimensions_.end()) {
203 // Unknown object - use reasonable default based on size
204 int s = std::max(1, size + 1);
205 return {std::min(s * 2, 16), 2}; // Cap at 16 tiles wide for selection
206 }
207
208 const auto& entry = it->second;
209 int w = entry.base_width;
210 int h = entry.base_height;
211
212 // Selection bounds should match draw-size semantics.
213 int effective_size = ResolveEffectiveSize(entry, size);
214
215 switch (entry.extend_dir) {
217 w += effective_size * entry.extend_multiplier;
218 break;
220 h += effective_size * entry.extend_multiplier;
221 break;
223 w += effective_size * entry.extend_multiplier;
224 h += effective_size * entry.extend_multiplier;
225 break;
227 // Diagonals: both dimensions scale with size
228 w += effective_size * entry.extend_multiplier;
229 h += effective_size * entry.extend_multiplier;
230 break;
232 // SuperSquare: subtype1 size uses 2-bit X/Y fields
233 int size_x = ((size >> 2) & 0x03) + 1;
234 int size_y = (size & 0x03) + 1;
235 w = size_x * entry.extend_multiplier;
236 h = size_y * entry.extend_multiplier;
237 break;
238 }
240 default:
241 break;
242 }
243
244 // Ensure minimum visible size (1 tile)
245 w = std::max(w, 1);
246 h = std::max(h, 1);
247
248 return {w, h};
249}
250
252 int object_id, int size) const {
253 if (core::FeatureFlags::get().kEnableCustomObjects) {
254 int subtype = size & 0x1F;
255 auto custom_or =
256 CustomObjectManager::Get().GetObjectInternal(object_id, subtype);
257 if (custom_or.ok()) {
258 auto custom = custom_or.value();
259 if (custom && !custom->IsEmpty()) {
260 int min_x = std::numeric_limits<int>::max();
261 int min_y = std::numeric_limits<int>::max();
262 int max_x = std::numeric_limits<int>::min();
263 int max_y = std::numeric_limits<int>::min();
264
265 for (const auto& entry : custom->tiles) {
266 min_x = std::min(min_x, entry.rel_x);
267 min_y = std::min(min_y, entry.rel_y);
268 max_x = std::max(max_x, entry.rel_x);
269 max_y = std::max(max_y, entry.rel_y);
270 }
271
272 if (min_x != std::numeric_limits<int>::max()) {
273 SelectionBounds bounds;
274 bounds.offset_x = min_x;
275 bounds.offset_y = min_y;
276 bounds.width = (max_x - min_x) + 1;
277 bounds.height = (max_y - min_y) + 1;
278 return bounds;
279 }
280 }
281 }
282 }
283
284 auto [w, h] = GetSelectionDimensions(object_id, size);
285 SelectionBounds bounds{0, 0, w, h};
286
287 switch (object_id) {
288 // Offset +3 (1x1 solid +3)
289 case 0x34:
290 case 0x11F:
291 case 0x120:
292 bounds.offset_x = 3;
293 break;
294
295 // Rightwards corners +13
296 case 0x2F:
297 bounds.offset_x = 13;
298 break;
299 case 0x30:
300 bounds.offset_x = 13;
301 bounds.offset_y = 1;
302 break;
303
304 // Downwards corners +12
305 case 0x6C:
306 case 0x6D:
307 bounds.offset_x = 12;
308 break;
309
310 // Downwards solid +3 writes from y+3 to y+(size+6).
311 case 0x71:
312 bounds.offset_y = 3;
313 break;
314
315 // Moving wall west grows its fill left from the three-column platform.
316 case 0xCD:
317 bounds.offset_x = -moving_wall::ObjectCountForSize(size);
318 break;
319
320 default:
321 break;
322 }
323
324 // Acute diagonals extend upward relative to origin.
325 if (object_id == 0x09 || object_id == 0x0C || object_id == 0x0D ||
326 object_id == 0x10 || object_id == 0x11 || object_id == 0x14) {
327 bounds.offset_y = -(bounds.width - 1);
328 }
329 if (object_id == 0x15 || object_id == 0x18 || object_id == 0x19 ||
330 object_id == 0x1C || object_id == 0x1D || object_id == 0x20) {
331 bounds.offset_y = -(bounds.width - 1);
332 }
333
334 // Diagonal ceilings: offsets depend on which corner is the origin.
335 // TopLeft (0xA0, 0xA5, 0xA9): extends down-right - no offset needed.
336 // BottomLeft (0xA1, 0xA6, 0xAA): extends up-right - offset_y negative.
337 if (object_id == 0xA1 || object_id == 0xA6 || object_id == 0xAA) {
338 bounds.offset_y = -(bounds.width - 1);
339 }
340 // TopRight (0xA2, 0xA7, 0xAB): extends down-left - offset_x negative.
341 if (object_id == 0xA2 || object_id == 0xA7 || object_id == 0xAB) {
342 bounds.offset_x = -(bounds.width - 1);
343 }
344 // BottomRight (0xA3, 0xA8, 0xAC): extends up-left - both offsets negative.
345 if (object_id == 0xA3 || object_id == 0xA8 || object_id == 0xAC) {
346 bounds.offset_x = -(bounds.width - 1);
347 bounds.offset_y = -(bounds.width - 1);
348 }
349
350 return bounds;
351}
352
353std::tuple<int, int, int, int> ObjectDimensionTable::GetHitTestBounds(
354 const RoomObject& obj) const {
355 auto bounds = GetSelectionBounds(obj.id_, obj.size_);
356 return {obj.x_ + bounds.offset_x, obj.y_ + bounds.offset_y, bounds.width,
357 bounds.height};
358}
359
361 using Dir = DimensionEntry::ExtendDir;
362
363 // ============================================================================
364 // Subtype 1 objects (0x00-0xF7)
365 // ============================================================================
366
367 // 0x00: Ceiling 2x2 - uses GetSize_1to15or32
368 dimensions_[0x00] = {0, 2, Dir::Horizontal, 2, true};
369
370 // 0x01-0x02: Wall 2x4 - uses GetSize_1to15or26
371 for (int id = 0x01; id <= 0x02; id++) {
372 dimensions_[id] = {0, 4, Dir::Horizontal, 2, false}; // Use 26 when zero
373 }
374 for (int id = 0x01; id <= 0x02; id++) {
375 dimensions_[id].zero_size_override = 26;
376 }
377
378 // 0x03-0x04: Wall 2x4 spaced 4 - GetSize_1to16
379 for (int id = 0x03; id <= 0x04; id++) {
380 dimensions_[id] = {2, 4, Dir::Horizontal, 2, false};
381 }
382
383 // 0x05-0x06: Wall 2x4 spaced 4 BothBG - step is 6 tiles between starts
384 for (int id = 0x05; id <= 0x06; id++) {
385 dimensions_[id] = {2, 4, Dir::Horizontal, 6, false};
386 }
387
388 // 0x07-0x08: Floor 2x2 - GetSize_1to16
389 for (int id = 0x07; id <= 0x08; id++) {
390 dimensions_[id] = {2, 2, Dir::Horizontal, 2, false};
391 }
392
393 // 0x09-0x14: Diagonal walls - non-BothBG (count = size + 7)
394 // Height = count + 4 tiles (5 tiles per column + diagonal extent)
395 for (int id = 0x09; id <= 0x14; id++) {
396 // Diagonal pattern: width = count tiles, height = count + 4 tiles
397 dimensions_[id] = {7, 11, Dir::Diagonal, 1,
398 false}; // base 7 + size*1, height 11 + size*1
399 }
400
401 // 0x15-0x20: Diagonal walls - BothBG (count = size + 6)
402 for (int id = 0x15; id <= 0x20; id++) {
403 dimensions_[id] = {6, 10, Dir::Diagonal, 1,
404 false}; // base 6 + size*1, height 10 + size*1
405 }
406
407 // 0x21: Edge 1x3 +2 (width = size*2 + 4)
408 dimensions_[0x21] = {4, 3, Dir::Horizontal, 2, false};
409
410 // 0x22: Edge 1x1 +3 (width = size + 4)
411 dimensions_[0x22] = {4, 1, Dir::Horizontal, 1, false};
412
413 // 0x23-0x2E: Edge 1x1 +2 (width = size + 3)
414 for (int id = 0x23; id <= 0x2E; id++) {
415 dimensions_[id] = {3, 1, Dir::Horizontal, 1, false};
416 }
417
418 // 0x2F: Top corners 1x2 +13
419 dimensions_[0x2F] = {10, 2, Dir::Horizontal, 1, false};
420
421 // 0x30: Bottom corners 1x2 +13
422 dimensions_[0x30] = {10, 2, Dir::Horizontal, 1, false};
423
424 // 0x31-0x32: Nothing
425 dimensions_[0x31] = {1, 1, Dir::None, 0, false};
426 dimensions_[0x32] = {1, 1, Dir::None, 0, false};
427
428 // 0x33: 4x4 block - GetSize_1to16
429 dimensions_[0x33] = {4, 4, Dir::Horizontal, 4, false};
430
431 // 0x34: Solid 1x1 +3 (count = size + 4)
432 dimensions_[0x34] = {4, 1, Dir::Horizontal, 1, false};
433
434 // 0x35: Door switcher - uses a single tile in ROM data
435 dimensions_[0x35] = {1, 1, Dir::None, 0, false};
436
437 // 0x36-0x37: Decor 4x4 spaced 2 - spacing 6 tiles
438 for (int id = 0x36; id <= 0x37; id++) {
439 dimensions_[id] = {4, 4, Dir::Horizontal, 6, false};
440 }
441
442 // 0x38: Statue 2x3 spaced 2 - spacing 4 tiles
443 dimensions_[0x38] = {2, 3, Dir::Horizontal, 4, false};
444
445 // 0x39: Pillar 2x4 spaced 4 - step is 6 tiles between starts
446 dimensions_[0x39] = {2, 4, Dir::Horizontal, 6, false};
447
448 // 0x3A-0x3B: Decor 4x3 spaced 4 - step is 8 tiles between starts
449 for (int id = 0x3A; id <= 0x3B; id++) {
450 dimensions_[id] = {4, 3, Dir::Horizontal, 8, false};
451 }
452
453 // 0x3C: Doubled 2x2 (rendered as 4x2) with 6-tile horizontal step
454 dimensions_[0x3C] = {4, 2, Dir::Horizontal, 6, false};
455
456 // 0x3D: Pillar 2x4 spaced 4 - step is 6 tiles between starts
457 dimensions_[0x3D] = {2, 4, Dir::Horizontal, 6, false};
458
459 // 0x3E: Decor 2x2 spaced 12 - spacing 14 tiles
460 dimensions_[0x3E] = {2, 2, Dir::Horizontal, 14, false};
461
462 // 0x3F-0x46: Edge 1x1 +2 (width = size + 3)
463 for (int id = 0x3F; id <= 0x46; id++) {
464 dimensions_[id] = {3, 1, Dir::Horizontal, 1, false};
465 }
466
467 // 0x47: Waterfall47 - 1x5 columns, width = 2 + (size+1)*2
468 dimensions_[0x47] = {4, 5, Dir::Horizontal, 2, false};
469
470 // 0x48: Waterfall48 - 1x3 columns, width = 2 + (size+1)*2
471 dimensions_[0x48] = {4, 3, Dir::Horizontal, 2, false};
472
473 // 0x49-0x4A: Floor Tile 4x2 - GetSize_1to16
474 for (int id = 0x49; id <= 0x4A; id++) {
475 dimensions_[id] = {4, 2, Dir::Horizontal, 4, false};
476 }
477
478 // 0x4B: Decor 2x2 spaced 12 - spacing 14 tiles
479 dimensions_[0x4B] = {2, 2, Dir::Horizontal, 14, false};
480
481 // 0x4C: Bar 4x3 - count=(size+1), step=4
482 dimensions_[0x4C] = {4, 3, Dir::Horizontal, 4, false};
483
484 // 0x4D-0x4F: Shelf 4x4 - count=(size+1), step=4
485 for (int id = 0x4D; id <= 0x4F; id++) {
486 dimensions_[id] = {4, 4, Dir::Horizontal, 4, false};
487 }
488
489 // 0x50: Line 1x1 +1 (count = size + 2)
490 dimensions_[0x50] = {2, 1, Dir::Horizontal, 1, false};
491
492 // 0x51-0x52: Cannon Hole 4x3 (left segment repeats by 2 tiles, right cap once)
493 for (int id = 0x51; id <= 0x52; id++) {
494 dimensions_[id] = {4, 3, Dir::Horizontal, 2, false};
495 }
496
497 // 0x53: Floor 2x2 - GetSize_1to16
498 dimensions_[0x53] = {2, 2, Dir::Horizontal, 2, false};
499
500 // 0x54-0x5A: Mostly unused, but 0x55-0x56 are wall torches.
501 for (int id = 0x54; id <= 0x5A; id++) {
502 dimensions_[id] = {1, 1, Dir::None, 0, false};
503 }
504 // 0x55-0x56: Decor 4x2 spaced 12
505 for (int id = 0x55; id <= 0x56; id++) {
506 dimensions_[id] = {4, 2, Dir::Horizontal, 12, false};
507 }
508
509 // 0x5B-0x5C: Cannon Hole 4x3 (same as 0x51-0x52)
510 for (int id = 0x5B; id <= 0x5C; id++) {
511 dimensions_[id] = {4, 3, Dir::Horizontal, 2, false};
512 }
513
514 // 0x5D: Big Rail 1x3 +5 (count = size + 6)
515 dimensions_[0x5D] = {6, 3, Dir::Horizontal, 1, false};
516
517 // 0x5E: Block 2x2 spaced 2
518 dimensions_[0x5E] = {2, 2, Dir::Horizontal, 4, false};
519
520 // 0x5F: Edge 1x1 +23 (width = size + 23)
521 dimensions_[0x5F] = {23, 1, Dir::Horizontal, 1, false};
522
523 // 0x60: Downwards 2x2 - GetSize_1to15or32
524 dimensions_[0x60] = {2, 0, Dir::Vertical, 2, true};
525
526 // 0x61-0x62: Downwards 4x2 - GetSize_1to15or26
527 for (int id = 0x61; id <= 0x62; id++) {
528 dimensions_[id] = {4, 0, Dir::Vertical, 2, false};
529 }
530 for (int id = 0x61; id <= 0x62; id++) {
531 dimensions_[id].zero_size_override = 26;
532 }
533
534 // 0x63-0x64: Downwards 4x2 - GetSize_1to15or26 (BothBG)
535 for (int id = 0x63; id <= 0x64; id++) {
536 dimensions_[id] = {4, 0, Dir::Vertical, 2, false};
537 dimensions_[id].zero_size_override = 26;
538 }
539 // 0x65-0x66: Downwards Decor 4x2 spaced 4 (6-tile spacing)
540 for (int id = 0x65; id <= 0x66; id++) {
541 dimensions_[id] = {4, 2, Dir::Vertical, 6, false};
542 }
543 // 0x67-0x68: Downwards 2x2 - GetSize_1to16
544 for (int id = 0x67; id <= 0x68; id++) {
545 dimensions_[id] = {2, 2, Dir::Vertical, 2, false};
546 }
547
548 // 0x69: Downwards edge +3 (height = size + 4, matching horizontal 0x22).
549 // ASM RoomDraw_DownwardsHasEdge1x1_1to16_plus3 ($01:8EC3) uses A=2 in
550 // GetSize_1to16_timesA so middle count = size + 2; total span = corner +
551 // (size+2) middles + end = size + 4 tiles.
552 dimensions_[0x69] = {1, 4, Dir::Vertical, 1, false};
553
554 // 0x6A-0x6B: Downwards edge
555 for (int id = 0x6A; id <= 0x6B; id++) {
556 dimensions_[id] = {1, 1, Dir::Vertical, 1, false};
557 }
558
559 // 0x6C-0x6D: Downwards corners (+12 offset in draw routine).
560 // Canonical empty-canvas render:
561 // - 2 opening rows
562 // - (size + 10) body rows
563 // - 2 closing rows
564 // => total height = size + 14
565 for (int id = 0x6C; id <= 0x6D; id++) {
566 dimensions_[id] = {2, 14, Dir::Vertical, 1, false};
567 }
568
569 // 0x6E-0x6F: Nothing
570 dimensions_[0x6E] = {1, 1, Dir::None, 0, false};
571 dimensions_[0x6F] = {1, 1, Dir::None, 0, false};
572
573 // 0x70: Downwards Floor 4x4
574 dimensions_[0x70] = {4, 4, Dir::Vertical, 4, false};
575
576 // 0x71: Downwards Solid 1x1 +3
577 dimensions_[0x71] = {1, 4, Dir::Vertical, 1, false};
578
579 // 0x72: Nothing
580 dimensions_[0x72] = {1, 1, Dir::None, 0, false};
581
582 // 0x73-0x74: Downwards Decor 4x4 spaced 2
583 for (int id = 0x73; id <= 0x74; id++) {
584 dimensions_[id] = {4, 4, Dir::Vertical, 6, false};
585 }
586
587 // 0x75: Downwards Pillar 2x4 spaced 2
588 dimensions_[0x75] = {2, 4, Dir::Vertical, 6, false};
589
590 // 0x76-0x77: Downwards Decor 3x4 spaced 4 (8-tile spacing)
591 for (int id = 0x76; id <= 0x77; id++) {
592 dimensions_[id] = {3, 4, Dir::Vertical, 8, false};
593 }
594
595 // 0x78, 0x7B: Downwards Decor 2x2 spaced 12
596 dimensions_[0x78] = {2, 2, Dir::Vertical, 14, false};
597 dimensions_[0x7B] = {2, 2, Dir::Vertical, 14, false};
598
599 // 0x79-0x7A: Downwards Edge 1x1
600 for (int id = 0x79; id <= 0x7A; id++) {
601 dimensions_[id] = {1, 1, Dir::Vertical, 1, false};
602 }
603
604 // 0x7C: Downwards Line 1x1 +1
605 dimensions_[0x7C] = {1, 2, Dir::Vertical, 1, false};
606
607 // 0x7D: Downwards 2x2
608 dimensions_[0x7D] = {2, 2, Dir::Vertical, 2, false};
609
610 // 0x7E: Nothing
611 dimensions_[0x7E] = {1, 1, Dir::None, 0, false};
612
613 // 0x7F-0x80: Downwards Decor 2x4 spaced 8 (12-tile step)
614 for (int id = 0x7F; id <= 0x80; id++) {
615 dimensions_[id] = {2, 4, Dir::Vertical, 12, false};
616 }
617
618 // 0x81-0x84: Downwards Decor 3x4 spaced 2 (6-tile spacing)
619 for (int id = 0x81; id <= 0x84; id++) {
620 dimensions_[id] = {3, 4, Dir::Vertical, 6, false};
621 }
622
623 // 0x85-0x86: Downwards Cannon Hole 3x4 (height = 2*(size+2))
624 for (int id = 0x85; id <= 0x86; id++) {
625 dimensions_[id] = {3, 4, Dir::Vertical, 2, false};
626 }
627
628 // 0x87: Downwards Pillar 2x4 spaced 2
629 dimensions_[0x87] = {2, 4, Dir::Vertical, 6, false};
630
631 // 0x88: Downwards Big Rail 3x1 +5
632 dimensions_[0x88] = {2, 6, Dir::Vertical, 1, false};
633
634 // 0x89: Downwards Block 2x2 spaced 2
635 dimensions_[0x89] = {2, 2, Dir::Vertical, 4, false};
636
637 // 0x8A: long rail with corner/middle/end (+23 total base span).
638 dimensions_[0x8A] = {1, 23, Dir::Vertical, 1, false};
639
640 // 0x8B-0x8C: single-tile jump ledges (+7 => size + 8 rows).
641 for (int id = 0x8B; id <= 0x8C; id++) {
642 dimensions_[id] = {1, 8, Dir::Vertical, 1, false};
643 }
644
645 // 0x8D-0x8E: Downwards Edge 1x1
646 for (int id = 0x8D; id <= 0x8E; id++) {
647 dimensions_[id] = {1, 1, Dir::Vertical, 1, false};
648 }
649
650 // 0x8F: Downwards Bar 2x5 (height = (2*size)+5)
651 dimensions_[0x8F] = {2, 5, Dir::Vertical, 2, false};
652
653 // 0x90-0x91: Downwards 4x2 - GetSize_1to15or26
654 for (int id = 0x90; id <= 0x91; id++) {
655 dimensions_[id] = {4, 0, Dir::Vertical, 2, false};
656 dimensions_[id].zero_size_override = 26;
657 }
658 // 0x92-0x93: Downwards 2x2 - GetSize_1to15or32
659 for (int id = 0x92; id <= 0x93; id++) {
660 dimensions_[id] = {2, 0, Dir::Vertical, 2, true};
661 }
662 // 0x94: Downwards Floor 4x4 - GetSize_1to16
663 dimensions_[0x94] = {4, 4, Dir::Vertical, 4, false};
664
665 // 0x95: Downwards Pots 2x2
666 dimensions_[0x95] = {2, 2, Dir::Vertical, 2, false};
667
668 // 0x96: Downwards Hammer Pegs 2x2
669 dimensions_[0x96] = {2, 2, Dir::Vertical, 2, false};
670
671 // 0x97-0x9F: Nothing
672 for (int id = 0x97; id <= 0x9F; id++) {
673 dimensions_[id] = {1, 1, Dir::None, 0, false};
674 }
675
676 // ============================================================================
677 // Diagonal ceilings (0xA0-0xAC)
678 // ============================================================================
679 // ASM: These have fixed patterns, count = (size & 0x0F) + 4
680 // TopLeft: 0xA0, 0xA5, 0xA9
681 for (int id : {0xA0, 0xA5, 0xA9}) {
682 dimensions_[id] = {4, 4, Dir::Diagonal, 1, false};
683 }
684 // BottomLeft: 0xA1, 0xA6, 0xAA
685 for (int id : {0xA1, 0xA6, 0xAA}) {
686 dimensions_[id] = {4, 4, Dir::Diagonal, 1, false};
687 }
688 // TopRight: 0xA2, 0xA7, 0xAB
689 for (int id : {0xA2, 0xA7, 0xAB}) {
690 dimensions_[id] = {4, 4, Dir::Diagonal, 1, false};
691 }
692 // BottomRight: 0xA3, 0xA8, 0xAC
693 for (int id : {0xA3, 0xA8, 0xAC}) {
694 dimensions_[id] = {4, 4, Dir::Diagonal, 1, false};
695 }
696 // BigHole4x4: 0xA4 - extends both directions
697 dimensions_[0xA4] = {4, 4, Dir::Both, 1, false};
698
699 // 0xAD-0xAF, 0xBE-0xBF: Nothing
700 for (int id : {0xAD, 0xAE, 0xAF, 0xBE, 0xBF}) {
701 dimensions_[id] = {1, 1, Dir::None, 0, false};
702 }
703
704 // 0xB0-0xB1: Rightwards Edge 1x1 +7 (count = size + 8)
705 for (int id = 0xB0; id <= 0xB1; id++) {
706 dimensions_[id] = {8, 1, Dir::Horizontal, 1, false};
707 }
708
709 // 0xB2: 4x4 block
710 dimensions_[0xB2] = {4, 4, Dir::Horizontal, 4, false};
711
712 // 0xB3-0xB4: Edge 1x1 (+2 variant, width = size + 3)
713 for (int id = 0xB3; id <= 0xB4; id++) {
714 dimensions_[id] = {3, 1, Dir::Horizontal, 1, false};
715 }
716
717 // 0xB5: Weird 2x4 - GetSize_1to16, repeated horizontally.
718 dimensions_[0xB5] = {2, 4, Dir::Horizontal, 2, false};
719
720 // 0xB6-0xB7: Rightwards 2x4
721 for (int id = 0xB6; id <= 0xB7; id++) {
722 dimensions_[id] = {0, 4, Dir::Horizontal, 2, false};
723 }
724 for (int id = 0xB6; id <= 0xB7; id++) {
725 dimensions_[id].zero_size_override = 26;
726 }
727
728 // 0xB8-0xB9: Rightwards 2x2
729 for (int id = 0xB8; id <= 0xB9; id++) {
730 dimensions_[id] = {0, 2, Dir::Horizontal, 2, true};
731 }
732
733 // 0xBA: 4x4 block
734 dimensions_[0xBA] = {4, 4, Dir::Horizontal, 4, false};
735
736 // 0xBB: Rightwards Block 2x2 spaced 2
737 dimensions_[0xBB] = {2, 2, Dir::Horizontal, 4, false};
738
739 // 0xBC: Rightwards Pots 2x2
740 dimensions_[0xBC] = {2, 2, Dir::Horizontal, 2, false};
741
742 // 0xBD: Rightwards Hammer Pegs 2x2
743 dimensions_[0xBD] = {2, 2, Dir::Horizontal, 2, false};
744
745 // ============================================================================
746 // SuperSquare objects (0xC0-0xCF, 0xD0-0xDF, 0xE0-0xEF)
747 // These use both size nibbles for independent X/Y sizing.
748 // RoomDraw_4x4BlocksIn4x4SuperSquare, RoomDraw_4x4FloorIn4x4SuperSquare, etc.
749 // width = ((size & 0x0F) + 1) * 4, height = (((size >> 4) & 0x0F) + 1) * 4
750 // ============================================================================
751
752 // 0xC0: Large ceiling (4x4 blocks in super squares)
753 // ASM: RoomDraw_4x4BlocksIn4x4SuperSquare ($018B94)
754 dimensions_[0xC0] = {0, 0, Dir::SuperSquare, 4, false};
755
756 // 0xC2: 4x4 blocks variant (same routine as 0xC0)
757 dimensions_[0xC2] = {0, 0, Dir::SuperSquare, 4, false};
758
759 // 0xC3, 0xD7: 3x3 floor in super square (3x3 spacing)
760 dimensions_[0xC3] = {0, 0, Dir::SuperSquare, 3, false};
761 dimensions_[0xD7] = {0, 0, Dir::SuperSquare, 3, false};
762
763 // 0xC4: 4x4 floor one
764 dimensions_[0xC4] = {0, 0, Dir::SuperSquare, 4, false};
765
766 // 0xC5-0xCA: 4x4 floor patterns
767 for (int id = 0xC5; id <= 0xCA; id++) {
768 dimensions_[id] = {0, 0, Dir::SuperSquare, 4, false};
769 }
770
771 // 0xCB-0xCC: Nothing (RoomDraw_Nothing_E)
772 dimensions_[0xCB] = {1, 1, Dir::None, 0, false};
773 dimensions_[0xCC] = {1, 1, Dir::None, 0, false};
774 // 0xCD-0xCE: Moving walls. Size zero selects count=8 and direction=5,
775 // producing an 11x16 footprint; nonzero dimensions use the split selector
776 // tables in GetMovingWallDimensions.
777 dimensions_[0xCD] = {11, 16, Dir::None, 0, false};
778 dimensions_[0xCE] = {11, 16, Dir::None, 0, false};
779
780 // 0xD1-0xD2: 4x4 floor patterns
781 dimensions_[0xD1] = {0, 0, Dir::SuperSquare, 4, false};
782 dimensions_[0xD2] = {0, 0, Dir::SuperSquare, 4, false};
783
784 // 0xD9: 4x4 floor pattern
785 dimensions_[0xD9] = {0, 0, Dir::SuperSquare, 4, false};
786
787 // 0xDB: 4x4 floor two
788 dimensions_[0xDB] = {0, 0, Dir::SuperSquare, 4, false};
789
790 // 0xDD: Table rock 4x4 rightwards
791 dimensions_[0xDD] = {4, 4, Dir::Horizontal, 4, false};
792 // 0xDE: Spike 2x2 tiling
793 dimensions_[0xDE] = {0, 0, Dir::SuperSquare, 2, false};
794
795 // 0xDF-0xE8: 4x4 floor patterns
796 for (int id = 0xDF; id <= 0xE8; id++) {
797 dimensions_[id] = {0, 0, Dir::SuperSquare, 4, false};
798 }
799
800 // ============================================================================
801 // Chests - fixed 4x4 (matches DrawChest bounding size)
802 // ============================================================================
803 for (int id : {0xF9, 0xFA, 0xFB, 0xFC, 0xFD}) {
804 dimensions_[id] = {4, 4, Dir::None, 0, false};
805 }
806
807 // ============================================================================
808 // Subtype 2 objects (0x100-0x13F)
809 // ============================================================================
810 // Layout corners - 4x4 repeated horizontally
811 for (int id = 0x100; id <= 0x107; id++) {
812 dimensions_[id] = {4, 4, Dir::Horizontal, 4, false};
813 }
814
815 // Other 4x4 patterns
816 for (int id = 0x108; id <= 0x10F; id++) {
817 dimensions_[id] = {4, 4, Dir::None, 0, false};
818 }
819
820 // Weird corners - match 3x4 / 4x3 patterns
821 for (int id = 0x110; id <= 0x113; id++) {
822 dimensions_[id] = {3, 4, Dir::None, 0, false};
823 }
824 for (int id = 0x114; id <= 0x117; id++) {
825 dimensions_[id] = {4, 3, Dir::None, 0, false};
826 }
827 // 0x118-0x11B: Rightwards 2x2 (repeatable)
828 for (int id = 0x118; id <= 0x11B; id++) {
829 dimensions_[id] = {2, 2, Dir::Horizontal, 2, false};
830 }
831 // 0x11C: Rightwards 4x4 (repeatable)
832 dimensions_[0x11C] = {4, 4, Dir::Horizontal, 4, false};
833 // 0x11D: 2x3 pillar (repeated)
834 dimensions_[0x11D] = {2, 3, Dir::Horizontal, 4, false};
835 // 0x11E: Single 2x2
836 dimensions_[0x11E] = {2, 2, Dir::Horizontal, 2, false};
837 // 0x11F-0x120: Star switch / Torch (1x1 +3)
838 dimensions_[0x11F] = {4, 1, Dir::Horizontal, 1, false};
839 dimensions_[0x120] = {4, 1, Dir::Horizontal, 1, false};
840 // 0x121: 2x3 pillar (repeated)
841 dimensions_[0x121] = {2, 3, Dir::Horizontal, 4, false};
842
843 // Tables, beds, etc
844 dimensions_[0x122] = {4, 5, Dir::None, 0, false}; // Bed
845 dimensions_[0x123] = {4, 3, Dir::Horizontal, 8,
846 false}; // Table (8-tile spacing)
847 // 0x124-0x125: 4x4
848 dimensions_[0x124] = {4, 4, Dir::Horizontal, 4, false};
849 dimensions_[0x125] = {4, 4, Dir::Horizontal, 4, false};
850 // 0x126: 2x3 pillar (repeated)
851 dimensions_[0x126] = {2, 3, Dir::Horizontal, 4, false};
852 // 0x127: Rightwards 2x2 (repeatable)
853 dimensions_[0x127] = {2, 2, Dir::Horizontal, 2, false};
854 dimensions_[0x128] = {4, 5, Dir::None, 0, false}; // Bed variant
855 // 0x129: 4x4
856 dimensions_[0x129] = {4, 4, Dir::Horizontal, 4, false};
857 // 0x12A-0x12B: Rightwards 2x2 (repeatable)
858 dimensions_[0x12A] = {2, 2, Dir::Horizontal, 2, false};
859 dimensions_[0x12B] = {2, 2, Dir::Horizontal, 2, false};
860 // 0x134: Rightwards 2x2 (repeatable)
861 dimensions_[0x134] = {2, 2, Dir::Horizontal, 2, false};
862 dimensions_[0x12C] = {6, 3, Dir::None, 0, false}; // 6x3 pattern
863 // 0x12D-0x133: Inter-room fat stairs + auto stairs (fixed 4x4)
864 for (int id = 0x12D; id <= 0x133; id++) {
865 dimensions_[id] = {4, 4, Dir::None, 0, false};
866 }
867 // 0x135-0x136: Water hop stairs (fixed 4x2)
868 dimensions_[0x135] = {4, 2, Dir::None, 0, false};
869 dimensions_[0x136] = {4, 2, Dir::None, 0, false};
870 // 0x137: Dam floodgate (fixed 10x4)
871 dimensions_[0x137] = {10, 4, Dir::None, 0, false};
872 // 0x138-0x13B: Spiral stairs (fixed 4x3)
873 for (int id = 0x138; id <= 0x13B; id++) {
874 dimensions_[id] = {4, 3, Dir::None, 0, false};
875 }
876 // 0x13C: Sanctuary wall (repeatable 4x4)
877 dimensions_[0x13C] = {4, 4, Dir::Horizontal, 4, false};
878 // 0x13D: Table 4x3 (repeatable with 8-tile spacing)
879 dimensions_[0x13D] = {4, 3, Dir::Horizontal, 8, false};
880 dimensions_[0x13E] = {6, 3, Dir::None, 0, false}; // Utility 6x3
881 // 0x13F: Magic Bat Altar (repeatable 4x4)
882 dimensions_[0x13F] = {4, 4, Dir::Horizontal, 4, false};
883
884 // ============================================================================
885 // Subtype 3 objects (0xF80-0xFFF)
886 // ============================================================================
887 // Default for Type 3: most are 2x2 fixed objects
888 for (int id = 0xF80; id <= 0xFFF; id++) {
889 dimensions_[id] = {2, 2, Dir::None, 0, false};
890 }
891
892 // Override specific Type 3 objects with known sizes
893 // Water face family:
894 // - Empty face can extend to 4x5 at runtime; use the larger stable
895 // footprint so selection/hit-testing matches the active branch.
896 // - Spitting face is 4x5
897 // - Drenching face is 4x7
898 dimensions_[0xF80] = {4, 5, Dir::None, 0, false};
899 dimensions_[0xF81] = {4, 5, Dir::None, 0, false};
900 dimensions_[0xF82] = {4, 7, Dir::None, 0, false};
901
902 // Somaria path pieces each write one tile at the object anchor.
903 for (int id = 0xF83; id <= 0xF8C; id++) {
904 dimensions_[id] = {1, 1, Dir::None, 0, false};
905 }
906 dimensions_[0xF8E] = {1, 1, Dir::None, 0, false};
907 dimensions_[0xF8F] = {1, 1, Dir::None, 0, false};
908
909 // Prison cell bars ($019C44 spans x..x+15 and four rows)
910 dimensions_[0xF8D] = {16, 4, Dir::None, 0, false};
911 dimensions_[0xF97] = {16, 4, Dir::None, 0, false};
912 // Rupee floor pattern
913 dimensions_[0xF92] = {5, 8, Dir::None, 0, false};
914 // Table/rock 4x3 repeated with 8-tile spacing
915 dimensions_[0xF94] = {4, 3, Dir::Horizontal, 8, false};
916 // Single hammer peg (fixed 2x2)
917 dimensions_[0xF96] = {2, 2, Dir::None, 0, false};
918 // Bombable floor (fixed 4x4 in both intact and open states)
919 dimensions_[0xFC7] = {4, 4, Dir::None, 0, false};
920 // Boss shells (single 4x4)
921 dimensions_[0xF95] = {4, 4, Dir::None, 0, false};
922 dimensions_[0xFF2] = {4, 4, Dir::None, 0, false};
923 dimensions_[0xFFB] = {4, 4, Dir::None, 0, false};
924 // Auto/straight stairs (fixed 4x4)
925 for (int id = 0xF9B; id <= 0xFA1; id++) {
926 dimensions_[id] = {4, 4, Dir::None, 0, false};
927 }
928 for (int id = 0xFA6; id <= 0xFA9; id++) {
929 dimensions_[id] = {4, 4, Dir::None, 0, false};
930 }
931 dimensions_[0xFB3] = {4, 4, Dir::None, 0, false};
932 // Repeatable 4x4 patterns
933 for (int id = 0xFB4; id <= 0xFB9; id++) {
934 dimensions_[id] = {4, 4, Dir::Horizontal, 4, false};
935 }
936 dimensions_[0xFAA] = {4, 4, Dir::Horizontal, 4, false};
937 dimensions_[0xFAD] = {4, 4, Dir::Horizontal, 4, false};
938 dimensions_[0xFAE] = {4, 4, Dir::Horizontal, 4, false};
939 dimensions_[0xFCB] = {4, 4, Dir::Horizontal, 4, false};
940 dimensions_[0xFCC] = {4, 4, Dir::Horizontal, 4, false};
941 dimensions_[0xFD4] = {4, 4, Dir::Horizontal, 4, false};
942 dimensions_[0xFE2] = {4, 4, Dir::Horizontal, 4, false};
943 dimensions_[0xFF6] = {4, 4, Dir::Horizontal, 4, false};
944 dimensions_[0xFF7] = {4, 4, Dir::Horizontal, 4, false};
945 // Utility + archery patterns
946 dimensions_[0xFCD] = {6, 3, Dir::None, 0, false};
947 dimensions_[0xFDD] = {6, 3, Dir::None, 0, false};
948 dimensions_[0xFD5] = {3, 5, Dir::None, 0, false};
949 dimensions_[0xFDB] = {3, 5, Dir::None, 0, false};
950 dimensions_[0xFE0] = {3, 6, Dir::None, 0, false};
951 dimensions_[0xFE1] = {3, 6, Dir::None, 0, false};
952 // Solid wall decor 3x4
953 dimensions_[0xFE9] = {3, 4, Dir::None, 0, false};
954 dimensions_[0xFEA] = {3, 4, Dir::None, 0, false};
955 dimensions_[0xFEE] = {3, 4, Dir::None, 0, false};
956 dimensions_[0xFEF] = {3, 4, Dir::None, 0, false};
957 // Light beams + Triforce floor
958 dimensions_[0xFF0] = {4, 10, Dir::None, 0, false};
959 dimensions_[0xFF1] = {8, 8, Dir::None, 0, false};
960 dimensions_[0xFF4] = {8, 8, Dir::None, 0, false};
961 dimensions_[0xFF8] = {8, 8, Dir::None, 0, false};
962 // Table rock 4x3 (repeatable with 8-tile spacing)
963 dimensions_[0xFF9] = {4, 3, Dir::Horizontal, 8, false};
964 // Rightwards 4x4 repeated
965 dimensions_[0xFC8] = {4, 4, Dir::Horizontal, 4, false};
966 // Table/rock 4x3 repeated with 8-tile spacing
967 dimensions_[0xFCE] = {4, 3, Dir::Horizontal, 8, false};
968 // Actual 4x4 (no repetition)
969 dimensions_[0xFE6] = {4, 4, Dir::None, 0, false};
970 dimensions_[0xFE7] = {4, 3, Dir::Horizontal, 8, false};
971 dimensions_[0xFE8] = {4, 3, Dir::Horizontal, 8, false};
972 // Single 4x4 tile8 (large decor)
973 dimensions_[0xFEB] = {4, 4, Dir::None, 0, false};
974 // Single 4x3
975 dimensions_[0xFEC] = {4, 3, Dir::None, 0, false};
976 dimensions_[0xFED] = {4, 3, Dir::None, 0, false};
977 // Turtle Rock pipes
978 dimensions_[0xFBA] = {4, 6, Dir::None, 0, false};
979 dimensions_[0xFBB] = {4, 6, Dir::None, 0, false};
980 dimensions_[0xFBC] = {6, 4, Dir::None, 0, false};
981 dimensions_[0xFBD] = {6, 4, Dir::None, 0, false};
982 dimensions_[0xFDC] = {6, 4, Dir::None, 0, false};
983 // Rightwards 4x4 repeated
984 dimensions_[0xFFA] = {4, 4, Dir::Horizontal, 4, false};
985 // 0xFB1-0xFB2: Big Chest 4x3
986 dimensions_[0xFB1] = {4, 3, Dir::None, 0, false};
987 dimensions_[0xFB2] = {4, 3, Dir::None, 0, false};
988}
989
991 // ROM addresses from ZScream:
992 // Tile data offset table: $018000
993 // Routine pointer table: $018200
994 // These tables help determine object sizes based on tile counts
995
996 constexpr int kSubtype1TileOffsets = 0x8000;
997 constexpr int kSubtype1Routines = 0x8200;
998
999 // Read tile count for each object to refine dimensions
1000 for (int id = 0; id < 0xF8; id++) {
1001 auto offset_result = rom->ReadWord(kSubtype1TileOffsets + id * 2);
1002 if (!offset_result.ok())
1003 continue;
1004
1005 // Tile count can inform base size
1006 // This is a simplified heuristic - full accuracy requires parsing
1007 // the actual tile data
1008 }
1009}
1010
1012 // Subtype 2 data offset: $0183F0
1013 // Subtype 2 routine ptr: $018470
1014 constexpr int kSubtype2TileOffsets = 0x83F0;
1015 (void)kSubtype2TileOffsets;
1016 (void)rom;
1017 // Similar parsing for subtype 2 objects
1018}
1019
1021 // Subtype 3 data offset: $0184F0
1022 // Subtype 3 routine ptr: $0185F0
1023 constexpr int kSubtype3TileOffsets = 0x84F0;
1024 (void)kSubtype3TileOffsets;
1025 (void)rom;
1026 // Similar parsing for subtype 3 objects
1027}
1028
1029} // namespace zelda3
1030} // namespace yaze
The Rom class is used to load, save, and modify Rom data. This is a generic SNES ROM container and do...
Definition rom.h:28
absl::StatusOr< uint16_t > ReadWord(int offset) const
Definition rom.cc:526
bool is_loaded() const
Definition rom.h:144
static Flags & get()
Definition features.h:119
static CustomObjectManager & Get()
absl::StatusOr< std::shared_ptr< CustomObject > > GetObjectInternal(int object_id, int subtype)
ROM-based object dimension lookup table.
std::pair< int, int > GetBaseDimensions(int object_id) const
std::tuple< int, int, int, int > GetHitTestBounds(const RoomObject &obj) const
std::pair< int, int > GetDimensions(int object_id, int size) const
std::unordered_map< int, DimensionEntry > dimensions_
SelectionBounds GetSelectionBounds(int object_id, int size) const
int ResolveEffectiveSize(const DimensionEntry &entry, int size) const
static ObjectDimensionTable & Get()
std::pair< int, int > GetSelectionDimensions(int object_id, int size) const
std::pair< int, int > GetClosedChestPlatformDimensions(int size)
bool GetSomariaLineDimensions(int object_id, int *width, int *height)
constexpr int DirectionForSize(int size)
constexpr int ObjectCountForSize(int size)