yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
overworld_item.cc
Go to the documentation of this file.
2
3#include <algorithm>
4#include <cstdint>
5#include <vector>
6
7#include "absl/status/status.h"
8#include "absl/status/statusor.h"
9#include "app/rom.h"
10#include "app/snes.h"
11#include "util/log.h"
12#include "util/macro.h"
13#include "zelda3/common.h"
15
16namespace yaze::zelda3 {
17
18absl::StatusOr<std::vector<OverworldItem>> LoadItems(
19 Rom* rom, std::vector<OverworldMap>& overworld_maps) {
20 std::vector<OverworldItem> items;
21
22 // Version 0x03 of the OW ASM added item support for the SW.
23 uint8_t asm_version = (*rom)[OverworldCustomASMHasBeenApplied];
24
25 // Determine max number of overworld maps based on actual ASM version
26 // Only use expanded maps (0xA0) if v3+ ASM is actually applied
27 int max_ow =
28 (asm_version >= 0x03 && asm_version != 0xFF) ? kNumOverworldMaps : 0x80;
29
30 ASSIGN_OR_RETURN(uint32_t pointer_snes,
32 uint32_t item_pointer_address =
33 SnesToPc(pointer_snes); // 0x1BC2F9 -> 0x0DC2F9
34
35 for (int i = 0; i < max_ow; i++) {
36 ASSIGN_OR_RETURN(uint8_t bank_byte,
38 int bank = bank_byte & 0x7F;
39
40 ASSIGN_OR_RETURN(uint8_t addr_low,
41 rom->ReadByte(item_pointer_address + (i * 2)));
42 ASSIGN_OR_RETURN(uint8_t addr_high,
43 rom->ReadByte(item_pointer_address + (i * 2) + 1));
44
45 uint32_t addr = (bank << 16) + // 1B
46 (addr_high << 8) + // F9
47 addr_low; // 3C
48 addr = SnesToPc(addr);
49
50 // Check if this is a large map and skip if not the parent
51 if (overworld_maps[i].area_size() != zelda3::AreaSizeEnum::SmallArea) {
52 if (overworld_maps[i].parent() != (uint8_t)i) {
53 continue;
54 }
55 }
56
57 while (true) {
58 ASSIGN_OR_RETURN(uint8_t b1, rom->ReadByte(addr));
59 ASSIGN_OR_RETURN(uint8_t b2, rom->ReadByte(addr + 1));
60 ASSIGN_OR_RETURN(uint8_t b3, rom->ReadByte(addr + 2));
61
62 if (b1 == 0xFF && b2 == 0xFF) {
63 break;
64 }
65
66 int p = (((b2 & 0x1F) << 8) + b1) >> 1;
67
68 int x = p % 0x40; // Use 0x40 instead of 64 to match ZS
69 int y = p >> 6;
70
71 int fakeID = i % 0x40; // Use modulo 0x40 to match ZS
72
73 int sy = fakeID / 8;
74 int sx = fakeID - (sy * 8);
75
76 items.emplace_back(b3, (uint16_t)i, (x * 16) + (sx * 512),
77 (y * 16) + (sy * 512), false);
78 auto size = items.size();
79
80 items[size - 1].game_x_ = (uint8_t)x;
81 items[size - 1].game_y_ = (uint8_t)y;
82 addr += 3;
83 }
84 }
85 return items;
86}
87
88absl::Status SaveItems(Rom* rom, const std::vector<OverworldItem>& items) {
89 const int pointer_count = zelda3::kNumOverworldMaps;
90
91 std::vector<std::vector<OverworldItem>> room_items(pointer_count);
92
93 // Reset bomb door lookup table used by special item (0x86)
94 for (int i = 0; i < zelda3::kNumOverworldMaps; ++i) {
97 }
98
99 for (const OverworldItem& item : items) {
100 if (item.deleted)
101 continue;
102
103 const int map_index = static_cast<int>(item.room_map_id_);
104 if (map_index < 0 || map_index >= pointer_count) {
105 LOG_WARN(
106 "Overworld::SaveItems",
107 "Skipping item with map index %d outside pointer table (size=%d)",
108 map_index, pointer_count);
109 continue;
110 }
111
112 room_items[map_index].push_back(item);
113
114 if (item.id_ == 0x86) {
115 const int lookup_index =
116 std::min(map_index, zelda3::kNumOverworldMaps - 1);
119 static_cast<uint16_t>((item.game_x_ + (item.game_y_ * 64)) * 2)));
120 }
121 }
122
123 // Prepare pointer reuse cache
124 std::vector<int> item_pointers(pointer_count, -1);
125 std::vector<int> item_pointers_reuse(pointer_count, -1);
126
127 for (int i = 0; i < pointer_count; ++i) {
128 item_pointers_reuse[i] = -1;
129 for (int ci = 0; ci < i; ++ci) {
130 if (room_items[i].empty()) {
131 item_pointers_reuse[i] = -2; // reuse empty terminator
132 break;
133 }
134
135 if (CompareItemsArrays(room_items[i], room_items[ci])) {
136 item_pointers_reuse[i] = ci;
137 break;
138 }
139 }
140 }
141
142 // Item data always lives in the vanilla data block
144 int empty_pointer = -1;
145
146 for (int i = 0; i < pointer_count; ++i) {
147 if (item_pointers_reuse[i] == -1) {
148 item_pointers[i] = data_pos;
149 for (const OverworldItem& item : room_items[i]) {
150 const uint16_t map_pos =
151 static_cast<uint16_t>(((item.game_y_ << 6) + item.game_x_) << 1);
152 const uint32_t data =
153 static_cast<uint32_t>(map_pos & 0xFF) |
154 (static_cast<uint32_t>((map_pos >> 8) & 0xFF) << 8) |
155 (static_cast<uint32_t>(item.id_) << 16);
156
157 RETURN_IF_ERROR(rom->WriteLong(data_pos, data));
158 data_pos += 3;
159 }
160
161 empty_pointer = data_pos;
162 RETURN_IF_ERROR(rom->WriteShort(data_pos, 0xFFFF));
163 data_pos += 2;
164 } else if (item_pointers_reuse[i] == -2) {
165 if (empty_pointer < 0) {
166 item_pointers[i] = data_pos;
167 empty_pointer = data_pos;
168 RETURN_IF_ERROR(rom->WriteShort(data_pos, 0xFFFF));
169 data_pos += 2;
170 } else {
171 item_pointers[i] = empty_pointer;
172 }
173 } else {
174 item_pointers[i] = item_pointers[item_pointers_reuse[i]];
175 }
176 }
177
178 if (data_pos > kOverworldItemsEndData) {
179 return absl::AbortedError("Too many items");
180 }
181
182 // Update pointer table metadata to the expanded location used by ZScream
187 static_cast<uint8_t>(
189
190 // Clear pointer table (write zero) to avoid stale values when pointer count shrinks
191 for (int i = 0; i < zelda3::kNumOverworldMaps; ++i) {
193 rom->WriteShort(zelda3::kOverworldItemsPointersNew + (i * 2), 0x0000));
194 }
195
196 for (int i = 0; i < pointer_count; ++i) {
197 const uint32_t snes_addr = PcToSnes(item_pointers[i]);
200 static_cast<uint16_t>(snes_addr & 0xFFFF)));
201 }
202
203 util::logf("End of Items : %d", data_pos);
204
205 return absl::OkStatus();
206}
207
208} // 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< uint32_t > ReadLong(int offset)
Definition rom.cc:676
absl::StatusOr< uint8_t > ReadByte(int offset)
Definition rom.cc:661
absl::Status WriteShort(int addr, uint16_t value)
Definition rom.cc:753
absl::Status WriteLong(uint32_t addr, uint32_t value)
Definition rom.cc:766
#define LOG_WARN(category, format,...)
Definition log.h:108
#define RETURN_IF_ERROR(expression)
Definition macro.h:53
#define ASSIGN_OR_RETURN(type_variable_name, expression)
Definition macro.h:61
void logf(const absl::FormatSpec< Args... > &format, Args &&... args)
Definition log.h:116
Zelda 3 specific classes and functions.
absl::Status SaveItems(Rom *rom, const std::vector< OverworldItem > &items)
absl::StatusOr< std::vector< OverworldItem > > LoadItems(Rom *rom, std::vector< OverworldMap > &overworld_maps)
constexpr int overworldItemsAddressBank
constexpr int kNumOverworldMaps
Definition common.h:46
bool CompareItemsArrays(std::vector< OverworldItem > item_array1, std::vector< OverworldItem > item_array2)
constexpr int kOverworldItemsEndData
constexpr int kOverworldItemsStartDataNew
constexpr int overworldItemsAddress
constexpr int OverworldCustomASMHasBeenApplied
Definition common.h:50
constexpr int kOverworldBombDoorItemLocationsNew
constexpr int kOverworldItemsPointersNew
uint32_t PcToSnes(uint32_t addr)
Definition snes.h:17
uint32_t SnesToPc(uint32_t addr) noexcept
Definition snes.h:8