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