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 <algorithm>
4#include <utility>
5
6#include "absl/strings/str_format.h"
7#include "rom/snes.h"
8#include "util/log.h"
11
12namespace yaze::zelda3 {
13
14absl::StatusOr<int> RoomLayout::GetLayoutAddress(int layout_id) const {
15 if (!rom_ || !rom_->is_loaded()) {
16 return absl::FailedPreconditionError("ROM not loaded");
17 }
18
19 if (layout_id < 0 ||
20 layout_id >= static_cast<int>(kRoomLayoutPointers.size())) {
21 return absl::InvalidArgumentError(
22 absl::StrFormat("Invalid layout id %d", layout_id));
23 }
24
25 uint32_t snes_addr = kRoomLayoutPointers[layout_id];
26 int pc_addr = SnesToPc(static_cast<int>(snes_addr));
27 if (pc_addr < 0 || pc_addr >= static_cast<int>(rom_->vector().size())) {
28 return absl::OutOfRangeError(
29 absl::StrFormat("Layout pointer %d out of range", layout_id));
30 }
31 return pc_addr;
32}
33
34absl::Status RoomLayout::LoadLayout(int layout_id, util::StopToken stop_token) {
35 objects_.clear();
36 if (stop_token.stop_requested()) {
37 return absl::CancelledError("Room layout load cancelled");
38 }
39
40 auto addr_result = GetLayoutAddress(layout_id);
41 if (!addr_result.ok()) {
42 return addr_result.status();
43 }
44
45 int pos = addr_result.value();
46 const int next = layout_id + 1 < static_cast<int>(kRoomLayoutPointers.size())
47 ? kRoomLayoutPointers[layout_id + 1]
49 const int end = std::min(static_cast<int>(SnesToPc(next)),
50 static_cast<int>(rom_->vector().size()));
51 const auto& rom_data = rom_->data();
52 int layer = 0;
53 std::vector<RoomObject> decoded;
54
55 LOG_DEBUG("RoomLayout", "Loading layout %d from PC address 0x%05X", layout_id,
56 pos);
57
58 int obj_index = 0;
59 while (pos < end) {
60 if (stop_token.stop_requested()) {
61 return absl::CancelledError("Room layout load cancelled");
62 }
63 if (end - pos < 2) {
64 return absl::DataLossError(absl::StrFormat(
65 "Layout %d has a truncated terminator at PC 0x%05X", layout_id, pos));
66 }
67 uint8_t b1 = rom_data[pos];
68 uint8_t b2 = rom_data[pos + 1];
69
70 if (b1 == 0xFF && b2 == 0xFF) {
71 // $FF $FF is the END terminator for this layout
72 LOG_DEBUG("RoomLayout",
73 "Layout %d terminated at pos=0x%05X after %d objects",
74 layout_id, pos, obj_index);
75 objects_ = std::move(decoded);
76 return absl::OkStatus();
77 }
78
79 if (end - pos < 3) {
80 return absl::DataLossError(absl::StrFormat(
81 "Layout %d has a truncated object at PC 0x%05X", layout_id, pos));
82 }
83
84 uint8_t b3 = rom_data[pos + 2];
85 pos += 3;
86
88 b1, b2, b3, static_cast<uint8_t>(layer));
89
90 obj.SetRom(rom_);
92 if (stop_token.stop_requested()) {
93 return absl::CancelledError("Room layout load cancelled");
94 }
95 decoded.push_back(obj);
96
97 LOG_DEBUG("RoomLayout",
98 "Layout %d obj[%d]: bytes=[%02X,%02X,%02X] -> id=0x%03X x=%d "
99 "y=%d size=%d tiles=%zu",
100 layout_id, obj_index, b1, b2, b3, obj.id_, obj.x_, obj.y_,
101 obj.size_, obj.tiles().size());
102 obj_index++;
103 }
104
105 return absl::DataLossError(absl::StrFormat(
106 "Layout %d has no FF FF terminator within its fixed stream", layout_id));
107}
108
109absl::Status RoomLayout::Draw(int room_id, const uint8_t* gfx_data,
112 const gfx::PaletteGroup& palette_group,
113 DungeonState* state, uint8_t floor1_graphics,
114 uint8_t floor2_graphics) const {
115 if (!rom_ || !rom_->is_loaded()) {
116 return absl::FailedPreconditionError("ROM not loaded");
117 }
118
119 if (objects_.empty()) {
120 return absl::OkStatus();
121 }
122
123 ObjectDrawer drawer(rom_, room_id, gfx_data);
125 drawer.SetRoomFloorGraphics(floor1_graphics, floor2_graphics);
126
127 std::vector<RoomObject> render_objects = objects_;
128 for (auto& obj : render_objects) {
129 obj.layer_ = RoomObject::LayerType::BG1;
130 }
131
132 // ALTTP enters the room-layout stream with the upper tilemap pointer active.
133 // Object IDs do not reroute the destination tilemap; their draw routines use
134 // the current stream pointer.
135 return drawer.DrawObjectList(render_objects, bg1, bg2, palette_group, state);
136}
137
138} // namespace yaze::zelda3
const auto & vector() const
Definition rom.h:205
auto data() const
Definition rom.h:201
bool is_loaded() const
Definition rom.h:187
bool stop_requested() const noexcept
Definition stop_token.h:21
Interface for accessing dungeon game state.
Draws dungeon objects to background buffers using game patterns.
void SetRoomFloorGraphics(uint8_t floor1, uint8_t floor2)
void SetBG1RevealMaskSource(gfx::BG1RevealMaskSource source)
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, bool reset_room_event_indices=true, gfx::BackgroundBuffer *layout_bg2=nullptr)
Draw all objects in a room.
std::vector< RoomObject > objects_
Definition room_layout.h:40
absl::Status Draw(int room_id, const uint8_t *gfx_data, gfx::BackgroundBuffer &bg1, gfx::BackgroundBuffer &bg2, const gfx::PaletteGroup &palette_group, DungeonState *state, uint8_t floor1_graphics, uint8_t floor2_graphics) const
absl::StatusOr< int > GetLayoutAddress(int layout_id) const
absl::Status LoadLayout(int layout_id, util::StopToken stop_token={})
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:99
void SetRom(Rom *rom)
Definition room_object.h:78
#define LOG_DEBUG(category, format,...)
Definition log.h:133
Zelda 3 specific classes and functions.
constexpr int kRoomLayoutDataEnd
uint32_t SnesToPc(uint32_t addr) noexcept
Definition snes.h:8
Represents a group of palettes.