yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
room_layout.cc
Go to the documentation of this file.
1#include "room_layout.h"
2
3#include "absl/strings/str_format.h"
4#include "rom/snes.h"
5#include "util/log.h"
8
9namespace yaze::zelda3 {
10
11namespace {
12
13// Returns true if the object ID is a pit or layer mask that should be on BG2.
14// These objects create transparency holes in BG1 to show BG2 content through.
15// Based on ALTTP object IDs and draw routine analysis.
16bool IsPitOrMaskObject(int16_t id) {
17 // BigHole4x4 (0xA4)
18 if (id == 0xA4)
19 return true;
20
21 // Pit edge objects (0x9B - 0xA6 range)
22 if (id >= 0x9B && id <= 0xA6)
23 return true;
24
25 // Type 3 pit objects (0xFE6)
26 if (id == 0xFE6)
27 return true;
28
29 // Layer 2 pit mask objects (Type 3: 0xFBE, 0xFBF)
30 if (id == 0xFBE || id == 0xFBF)
31 return true;
32
33 return false;
34}
35
36} // namespace
37
38absl::StatusOr<int> RoomLayout::GetLayoutAddress(int layout_id) const {
39 if (!rom_ || !rom_->is_loaded()) {
40 return absl::FailedPreconditionError("ROM not loaded");
41 }
42
43 if (layout_id < 0 ||
44 layout_id >= static_cast<int>(kRoomLayoutPointers.size())) {
45 return absl::InvalidArgumentError(
46 absl::StrFormat("Invalid layout id %d", layout_id));
47 }
48
49 uint32_t snes_addr = kRoomLayoutPointers[layout_id];
50 int pc_addr = SnesToPc(static_cast<int>(snes_addr));
51 if (pc_addr < 0 || pc_addr >= static_cast<int>(rom_->size())) {
52 return absl::OutOfRangeError(
53 absl::StrFormat("Layout pointer %d out of range", layout_id));
54 }
55 return pc_addr;
56}
57
58absl::Status RoomLayout::LoadLayout(int layout_id) {
59 objects_.clear();
60
61 auto addr_result = GetLayoutAddress(layout_id);
62 if (!addr_result.ok()) {
63 return addr_result.status();
64 }
65
66 int pos = addr_result.value();
67 const auto& rom_data = rom_->data();
68 int layer = 0;
69
70 LOG_DEBUG("RoomLayout", "Loading layout %d from PC address 0x%05X", layout_id,
71 pos);
72
73 int obj_index = 0;
74 while (pos + 2 < static_cast<int>(rom_->size())) {
75 uint8_t b1 = rom_data[pos];
76 uint8_t b2 = rom_data[pos + 1];
77
78 if (b1 == 0xFF && b2 == 0xFF) {
79 // $FF $FF is the END terminator for this layout
80 LOG_DEBUG("RoomLayout",
81 "Layout %d terminated at pos=0x%05X after %d objects",
82 layout_id, pos, obj_index);
83 break;
84 }
85
86 if (pos + 2 >= static_cast<int>(rom_->size())) {
87 break;
88 }
89
90 uint8_t b3 = rom_data[pos + 2];
91 pos += 3;
92
94 b1, b2, b3, static_cast<uint8_t>(layer));
95
96 // Pit/mask objects should be on BG2 layer to create transparency holes.
97 // This allows them to show through BG1 content (walls, floor).
98 if (IsPitOrMaskObject(obj.id_)) {
100 LOG_DEBUG("RoomLayout", "Pit/mask object 0x%03X assigned to BG2 layer",
101 obj.id_);
102 }
103
104 obj.SetRom(rom_);
105 obj.EnsureTilesLoaded();
106 objects_.push_back(obj);
107
108 LOG_DEBUG("RoomLayout",
109 "Layout %d obj[%d]: bytes=[%02X,%02X,%02X] -> id=0x%03X x=%d "
110 "y=%d size=%d tiles=%zu",
111 layout_id, obj_index, b1, b2, b3, obj.id_, obj.x_, obj.y_,
112 obj.size_, obj.tiles().size());
113 obj_index++;
114 }
115
116 LOG_DEBUG("RoomLayout", "Layout %d loaded with %zu objects", layout_id,
117 objects_.size());
118
119 return absl::OkStatus();
120}
121
122absl::Status RoomLayout::Draw(int room_id, const uint8_t* gfx_data,
125 const gfx::PaletteGroup& palette_group,
126 DungeonState* state) const {
127 if (!rom_ || !rom_->is_loaded()) {
128 return absl::FailedPreconditionError("ROM not loaded");
129 }
130
131 if (objects_.empty()) {
132 return absl::OkStatus();
133 }
134
135 ObjectDrawer drawer(rom_, room_id, gfx_data);
136 // Pass bg1 as layout_bg1 so BG2 objects (pits/masks) will mark the
137 // corresponding area in BG1 as transparent via MarkBG1Transparent().
138 // This creates "holes" in BG1 that allow BG2 content to show through.
139 return drawer.DrawObjectList(objects_, bg1, bg2, palette_group, state, &bg1);
140}
141
142} // namespace yaze::zelda3
auto data() const
Definition rom.h:135
auto size() const
Definition rom.h:134
bool is_loaded() const
Definition rom.h:128
Interface for accessing dungeon game state.
Draws dungeon objects to background buffers using game patterns.
absl::Status DrawObjectList(const std::vector< RoomObject > &objects, gfx::BackgroundBuffer &bg1, gfx::BackgroundBuffer &bg2, const gfx::PaletteGroup &palette_group, const DungeonState *state=nullptr, gfx::BackgroundBuffer *layout_bg1=nullptr)
Draw all objects in a room.
std::vector< RoomObject > objects_
Definition room_layout.h:37
absl::Status Draw(int room_id, const uint8_t *gfx_data, gfx::BackgroundBuffer &bg1, gfx::BackgroundBuffer &bg2, const gfx::PaletteGroup &palette_group, DungeonState *state) const
absl::StatusOr< int > GetLayoutAddress(int layout_id) const
absl::Status LoadLayout(int layout_id)
static RoomObject DecodeObjectFromBytes(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t layer)
const std::vector< gfx::TileInfo > & tiles() const
Definition room_object.h:88
void SetRom(Rom *rom)
Definition room_object.h:68
#define LOG_DEBUG(category, format,...)
Definition log.h:103
Zelda 3 specific classes and functions.
Definition editor.h:35
uint32_t SnesToPc(uint32_t addr) noexcept
Definition snes.h:8
Represents a group of palettes.