yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
overworld_entrance.cc
Go to the documentation of this file.
2#include <vector>
3#include "absl/status/status.h"
4#include "absl/status/statusor.h"
5#include "app/rom.h"
6#include "util/macro.h"
7
8namespace yaze::zelda3 {
9
10absl::StatusOr<std::vector<OverworldEntrance>> LoadEntrances(Rom* rom) {
11 std::vector<OverworldEntrance> entrances;
12 int ow_entrance_map_ptr = kOverworldEntranceMap;
13 int ow_entrance_pos_ptr = kOverworldEntrancePos;
14 int ow_entrance_id_ptr = kOverworldEntranceEntranceId;
15 int num_entrances = 129;
16
17 // Check if expanded entrance data is actually present in ROM
18 // The flag position should contain 0xB8 for vanilla, something else for expanded
19 if (rom->data()[kOverworldEntranceExpandedFlagPos] != 0xB8) {
20 // ROM has expanded entrance data - use expanded addresses
21 ow_entrance_map_ptr = kOverworldEntranceMapExpanded;
22 ow_entrance_pos_ptr = kOverworldEntrancePosExpanded;
23 ow_entrance_id_ptr = kOverworldEntranceEntranceIdExpanded;
24 num_entrances = 256; // Expanded entrance count
25 }
26 // Otherwise use vanilla addresses (already set above)
27
28 for (int i = 0; i < num_entrances; i++) {
29 ASSIGN_OR_RETURN(auto map_id, rom->ReadWord(ow_entrance_map_ptr + (i * 2)));
30 ASSIGN_OR_RETURN(auto map_pos,
31 rom->ReadWord(ow_entrance_pos_ptr + (i * 2)));
32 ASSIGN_OR_RETURN(auto entrance_id, rom->ReadByte(ow_entrance_id_ptr + i));
33 int p = map_pos >> 1;
34 int x = (p % 64);
35 int y = (p >> 6);
36 bool deleted = false;
37 if (map_pos == 0xFFFF) {
38 deleted = true;
39 }
40 entrances.emplace_back(
41 (x * 16) + (((map_id % 64) - (((map_id % 64) / 8) * 8)) * 512),
42 (y * 16) + (((map_id % 64) / 8) * 512), entrance_id, map_id, map_pos,
43 deleted);
44 }
45
46 return entrances;
47}
48
49absl::StatusOr<std::vector<OverworldEntrance>> LoadHoles(Rom* rom) {
50 constexpr int kNumHoles = 0x13;
51 std::vector<OverworldEntrance> holes;
52 for (int i = 0; i < kNumHoles; i++) {
53 ASSIGN_OR_RETURN(auto map_id, rom->ReadWord(kOverworldHoleArea + (i * 2)));
54 ASSIGN_OR_RETURN(auto map_pos, rom->ReadWord(kOverworldHolePos + (i * 2)));
55 ASSIGN_OR_RETURN(auto entrance_id,
57 int p = (map_pos + 0x400) >> 1;
58 int x = (p % 64);
59 int y = (p >> 6);
60 holes.emplace_back(
61 (x * 16) + (((map_id % 64) - (((map_id % 64) / 8) * 8)) * 512),
62 (y * 16) + (((map_id % 64) / 8) * 512), entrance_id, map_id,
63 (uint16_t)(map_pos + 0x400), true);
64 }
65 return holes;
66}
67
68absl::Status SaveEntrances(Rom* rom,
69 const std::vector<OverworldEntrance>& entrances, bool expanded_entrances) {
70
71 auto write_entrance = [&](int index, uint32_t map_addr, uint32_t pos_addr,
72 uint32_t id_addr) -> absl::Status {
73 // Mirrors ZeldaFullEditor/Save.cs::SaveOWEntrances (see lines ~1081-1085)
74 // where MapID and MapPos are written as 16-bit words and EntranceID as a byte.
75 RETURN_IF_ERROR(rom->WriteShort(map_addr, entrances[index].map_id_));
76 RETURN_IF_ERROR(rom->WriteShort(pos_addr, entrances[index].map_pos_));
77 RETURN_IF_ERROR(rom->WriteByte(id_addr, entrances[index].entrance_id_));
78 return absl::OkStatus();
79 };
80
81 // Always keep the legacy tables in sync for pure vanilla ROMs so e.g. Hyrule
82 // Magic expects them. ZScream does the same in SaveOWEntrances.
83 for (int i = 0; i < kNumOverworldEntrances; ++i) {
84 RETURN_IF_ERROR(write_entrance(i, kOverworldEntranceMap + (i * 2),
85 kOverworldEntrancePos + (i * 2),
87 }
88
89 if (expanded_entrances) {
90 // For ZS v3+ ROMs, mirror writes into the expanded tables the way
91 // ZeldaFullEditor does when the ASM patch is active.
92 for (int i = 0; i < kNumOverworldEntrances; ++i) {
93 RETURN_IF_ERROR(write_entrance(i, kOverworldEntranceMapExpanded + (i * 2),
96 }
97 }
98
99 return absl::OkStatus();
100}
101
102absl::Status SaveHoles(Rom* rom, const std::vector<OverworldEntrance>& holes) {
103 for (int i = 0; i < kNumOverworldHoles; ++i) {
105 rom->WriteShort(kOverworldHoleArea + (i * 2), holes[i].map_id_));
106
107 // ZeldaFullEditor/Data/Overworld.cs::LoadHoles() adds 0x400 when loading
108 // (see lines ~1006-1014). SaveOWEntrances subtracts it before writing
109 // (Save.cs lines ~1088-1092). We replicate that here so vanilla ROMs
110 // receive the expected values.
111 uint16_t rom_map_pos = static_cast<uint16_t>(holes[i].map_pos_ >= 0x400
112 ? holes[i].map_pos_ - 0x400
113 : holes[i].map_pos_);
114 RETURN_IF_ERROR(rom->WriteShort(kOverworldHolePos + (i * 2), rom_map_pos));
116 rom->WriteByte(kOverworldHoleEntrance + i, holes[i].entrance_id_));
117 }
118
119 return absl::OkStatus();
120}
121
122} // namespace yaze::zelda3
The Rom class is used to load, save, and modify Rom data.
Definition rom.h:74
absl::Status WriteByte(int addr, uint8_t value)
Definition rom.cc:728
absl::StatusOr< uint16_t > ReadWord(int offset)
Definition rom.cc:668
auto data() const
Definition rom.h:206
absl::StatusOr< uint8_t > ReadByte(int offset)
Definition rom.cc:661
absl::Status WriteShort(int addr, uint16_t value)
Definition rom.cc:753
#define RETURN_IF_ERROR(expression)
Definition macro.h:53
#define ASSIGN_OR_RETURN(type_variable_name, expression)
Definition macro.h:61
Zelda 3 specific classes and functions.
absl::Status SaveEntrances(Rom *rom, const std::vector< OverworldEntrance > &entrances, bool expanded_entrances)
constexpr int kOverworldHoleArea
constexpr int kOverworldEntrancePos
constexpr int kOverworldHoleEntrance
absl::StatusOr< std::vector< OverworldEntrance > > LoadEntrances(Rom *rom)
constexpr int kOverworldEntranceEntranceIdExpanded
constexpr int kNumOverworldHoles
constexpr int kOverworldEntranceEntranceId
constexpr int kOverworldHolePos
constexpr int kOverworldEntranceMap
constexpr int kOverworldEntrancePosExpanded
constexpr int kOverworldEntranceMapExpanded
absl::Status SaveHoles(Rom *rom, const std::vector< OverworldEntrance > &holes)
absl::StatusOr< std::vector< OverworldEntrance > > LoadHoles(Rom *rom)
constexpr int kOverworldEntranceExpandedFlagPos
constexpr int kNumOverworldEntrances