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