yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
dungeon_map.cc
Go to the documentation of this file.
1#include "dungeon_map.h"
2
3#include <fstream>
4#include <vector>
5
11#include "app/platform/window.h"
12#include "rom/snes.h"
13#include "util/file_util.h"
14#include "util/hex.h"
15#include "zelda3/game_data.h"
16
17namespace yaze::zelda3 {
18
19absl::StatusOr<std::vector<DungeonMap>> LoadDungeonMaps(
20 Rom& rom, DungeonMapLabels& dungeon_map_labels) {
21 std::vector<DungeonMap> dungeon_maps;
22 std::vector<std::array<uint8_t, kNumRooms>> current_floor_rooms_d;
23 std::vector<std::array<uint8_t, kNumRooms>> current_floor_gfx_d;
24 int total_floors_d;
25 uint8_t nbr_floor_d;
26 uint8_t nbr_basement_d;
27
28 for (int d = 0; d < kNumDungeons; d++) {
29 current_floor_rooms_d.clear();
30 current_floor_gfx_d.clear();
31 ASSIGN_OR_RETURN(int ptr, rom.ReadWord(kDungeonMapRoomsPtr + (d * 2)));
32 ASSIGN_OR_RETURN(int ptr_gfx, rom.ReadWord(kDungeonMapGfxPtr + (d * 2)));
33 ptr |= 0x0A0000; // Add bank to the short ptr
34 ptr_gfx |= 0x0A0000; // Add bank to the short ptr
35 int pc_ptr = SnesToPc(ptr); // Contains data for the next 25 rooms
36 int pc_ptr_gfx = SnesToPc(ptr_gfx); // Contains data for the next 25 rooms
37
38 ASSIGN_OR_RETURN(uint16_t boss_room_d,
39 rom.ReadWord(kDungeonMapBossRooms + (d * 2)));
40
41 ASSIGN_OR_RETURN(nbr_basement_d, rom.ReadByte(kDungeonMapFloors + (d * 2)));
42 nbr_basement_d &= 0x0F;
43
44 ASSIGN_OR_RETURN(nbr_floor_d, rom.ReadByte(kDungeonMapFloors + (d * 2)));
45 nbr_floor_d &= 0xF0;
46 nbr_floor_d = nbr_floor_d >> 4;
47
48 total_floors_d = nbr_basement_d + nbr_floor_d;
49
50 // for each floor in the dungeon
51 for (int i = 0; i < total_floors_d; i++) {
52 dungeon_map_labels[d].emplace_back();
53
54 std::array<uint8_t, kNumRooms> rdata;
55 std::array<uint8_t, kNumRooms> gdata;
56
57 // for each room on the floor
58 for (int j = 0; j < kNumRooms; j++) {
59 gdata[j] = 0xFF;
60 rdata[j] = rom.data()[pc_ptr + j + (i * kNumRooms)]; // Set the rooms
61
62 gdata[j] = rdata[j] == 0x0F ? 0xFF : rom.data()[pc_ptr_gfx++];
63
64 std::string label = util::HexByte(rdata[j]);
65 dungeon_map_labels[d][i][j] = label;
66 }
67
68 current_floor_gfx_d.push_back(gdata); // Add new floor gfx data
69 current_floor_rooms_d.push_back(rdata); // Add new floor data
70 }
71
72 dungeon_maps.emplace_back(boss_room_d, nbr_floor_d, nbr_basement_d,
73 current_floor_rooms_d, current_floor_gfx_d);
74 }
75
76 return dungeon_maps;
77}
78
79absl::Status SaveDungeonMaps(Rom& rom, std::vector<DungeonMap>& dungeon_maps) {
80 for (int d = 0; d < kNumDungeons; d++) {
81 int ptr = kDungeonMapRoomsPtr + (d * 2);
82 int ptr_gfx = kDungeonMapGfxPtr + (d * 2);
83 int pc_ptr = SnesToPc(ptr);
84 int pc_ptr_gfx = SnesToPc(ptr_gfx);
85
86 const int nbr_floors = dungeon_maps[d].nbr_of_floor;
87 const int nbr_basements = dungeon_maps[d].nbr_of_basement;
88 for (int i = 0; i < nbr_floors + nbr_basements; i++) {
89 for (int j = 0; j < kNumRooms; j++) {
90 RETURN_IF_ERROR(rom.WriteByte(pc_ptr + j + (i * kNumRooms),
91 dungeon_maps[d].floor_rooms[i][j]));
92 RETURN_IF_ERROR(rom.WriteByte(pc_ptr_gfx + j + (i * kNumRooms),
93 dungeon_maps[d].floor_gfx[i][j]));
94 pc_ptr_gfx++;
95 }
96 }
97 }
98
99 return absl::OkStatus();
100}
101
102absl::Status LoadDungeonMapTile16(gfx::Tilemap& tile16_blockset, Rom& rom,
103 GameData* game_data,
104 const std::vector<uint8_t>& gfx_data,
105 bool bin_mode) {
106 tile16_blockset.tile_size = {16, 16};
107 tile16_blockset.map_size = {186, 186};
108 tile16_blockset.atlas.Create(256, 192, 8,
109 std::vector<uint8_t>(256 * 192, 0x00));
110
111 for (int i = 0; i < kNumDungeonMapTile16; i++) {
112 int addr = kDungeonMapTile16;
113 if (rom.data()[kDungeonMapExpCheck] != 0xB9) {
115 }
116
117 ASSIGN_OR_RETURN(auto tl, rom.ReadWord(addr + (i * 8)));
118 gfx::TileInfo t1 = gfx::WordToTileInfo(tl); // Top left
119
120 ASSIGN_OR_RETURN(auto tr, rom.ReadWord(addr + 2 + (i * 8)));
121 gfx::TileInfo t2 = gfx::WordToTileInfo(tr); // Top right
122
123 ASSIGN_OR_RETURN(auto bl, rom.ReadWord(addr + 4 + (i * 8)));
124 gfx::TileInfo t3 = gfx::WordToTileInfo(bl); // Bottom left
125
126 ASSIGN_OR_RETURN(auto br, rom.ReadWord(addr + 6 + (i * 8)));
127 gfx::TileInfo t4 = gfx::WordToTileInfo(br); // Bottom right
128
129 int sheet_offset = 212;
130 if (bin_mode) {
131 sheet_offset = 0;
132 }
133 ComposeTile16(tile16_blockset, gfx_data, t1, t2, t3, t4, sheet_offset);
134 }
135
136 if (game_data) {
137 tile16_blockset.atlas.SetPalette(
139 }
140
141 // Queue texture creation via Arena's deferred system
143 &tile16_blockset.atlas);
144
145 return absl::OkStatus();
146}
147
148absl::Status SaveDungeonMapTile16(gfx::Tilemap& tile16_blockset, Rom& rom) {
149 for (int i = 0; i < kNumDungeonMapTile16; i++) {
150 int addr = kDungeonMapTile16;
151 if (rom.data()[kDungeonMapExpCheck] != 0xB9) {
153 }
154
155 gfx::TileInfo t1 = tile16_blockset.tile_info[i][0];
156 gfx::TileInfo t2 = tile16_blockset.tile_info[i][1];
157 gfx::TileInfo t3 = tile16_blockset.tile_info[i][2];
158 gfx::TileInfo t4 = tile16_blockset.tile_info[i][3];
159
160 auto tl = gfx::TileInfoToWord(t1);
161 RETURN_IF_ERROR(rom.WriteWord(addr + (i * 8), tl));
162
163 auto tr = gfx::TileInfoToWord(t2);
164 RETURN_IF_ERROR(rom.WriteWord(addr + 2 + (i * 8), tr));
165
166 auto bl = gfx::TileInfoToWord(t3);
167 RETURN_IF_ERROR(rom.WriteWord(addr + 4 + (i * 8), bl));
168
169 auto br = gfx::TileInfoToWord(t4);
170 RETURN_IF_ERROR(rom.WriteWord(addr + 6 + (i * 8), br));
171 }
172 return absl::OkStatus();
173}
174
175absl::Status LoadDungeonMapGfxFromBinary(Rom& rom, GameData* game_data,
176 gfx::Tilemap& tile16_blockset,
177 std::array<gfx::Bitmap, 4>& sheets,
178 std::vector<uint8_t>& gfx_bin_data) {
179 std::string bin_file = util::FileDialogWrapper::ShowOpenFileDialog();
180 if (bin_file.empty()) {
181 return absl::InternalError("No file selected");
182 }
183
184 std::ifstream file(bin_file, std::ios::binary);
185 if (!file.is_open()) {
186 return absl::InternalError("Failed to open file");
187 }
188
189 // Read the gfx data into a buffer
190 std::vector<uint8_t> bin_data((std::istreambuf_iterator<char>(file)),
191 std::istreambuf_iterator<char>());
192 auto converted_bin = gfx::SnesTo8bppSheet(bin_data, 4, 4);
193 gfx_bin_data = converted_bin;
194 if (LoadDungeonMapTile16(tile16_blockset, rom, game_data, converted_bin, true)
195 .ok()) {
196 std::vector<std::vector<uint8_t>> gfx_sheets;
197 for (int i = 0; i < 4; i++) {
198 gfx_sheets.emplace_back(converted_bin.begin() + (i * 0x1000),
199 converted_bin.begin() + ((i + 1) * 0x1000));
200 sheets[i] = gfx::Bitmap(128, 32, 8, gfx_sheets[i]);
201 if (game_data) {
202 sheets[i].SetPalette(
204 }
205
206 // Queue texture creation via Arena's deferred system
209 }
210 }
211 file.close();
212
213 return absl::OkStatus();
214}
215
216} // namespace yaze::zelda3
The Rom class is used to load, save, and modify Rom data. This is a generic SNES ROM container and do...
Definition rom.h:24
absl::Status WriteByte(int addr, uint8_t value)
Definition rom.cc:286
absl::StatusOr< uint16_t > ReadWord(int offset)
Definition rom.cc:228
auto data() const
Definition rom.h:135
absl::StatusOr< uint8_t > ReadByte(int offset)
Definition rom.cc:221
absl::Status WriteWord(int addr, uint16_t value)
Definition rom.cc:299
void QueueTextureCommand(TextureCommandType type, Bitmap *bitmap)
Definition arena.cc:34
static Arena & Get()
Definition arena.cc:19
Represents a bitmap image optimized for SNES ROM hacking.
Definition bitmap.h:67
void Create(int width, int height, int depth, std::span< uint8_t > data)
Create a bitmap with the given dimensions and data.
Definition bitmap.cc:199
void SetPalette(const SnesPalette &palette)
Set the palette for the bitmap using SNES palette format.
Definition bitmap.cc:382
SNES 16-bit tile metadata container.
Definition snes_tile.h:50
static std::string ShowOpenFileDialog()
ShowOpenFileDialog opens a file dialog and returns the selected filepath. Uses global feature flag to...
#define ASSIGN_OR_RETURN(type_variable_name, expression)
Definition macro.h:62
uint16_t TileInfoToWord(TileInfo tile_info)
Definition snes_tile.cc:304
std::vector< uint8_t > SnesTo8bppSheet(std::span< uint8_t > sheet, int bpp, int num_sheets)
Definition snes_tile.cc:131
TileInfo WordToTileInfo(uint16_t word)
Definition snes_tile.cc:321
std::string HexByte(uint8_t byte, HexStringParams params)
Definition hex.cc:30
Zelda 3 specific classes and functions.
Definition editor.h:35
absl::Status LoadDungeonMapTile16(gfx::Tilemap &tile16_blockset, Rom &rom, GameData *game_data, const std::vector< uint8_t > &gfx_data, bool bin_mode)
Load the dungeon map tile16 from the ROM.
constexpr int kDungeonMapExpCheck
Definition dungeon_map.h:25
constexpr int kDungeonMapFloors
Definition dungeon_map.h:17
constexpr int kDungeonMapTile16
Definition dungeon_map.h:26
constexpr int kNumRooms
Definition dungeon_map.h:37
constexpr int kDungeonMapTile16Expanded
Definition dungeon_map.h:27
absl::Status SaveDungeonMapTile16(gfx::Tilemap &tile16_blockset, Rom &rom)
Save the dungeon map tile16 to the ROM.
absl::Status LoadDungeonMapGfxFromBinary(Rom &rom, GameData *game_data, gfx::Tilemap &tile16_blockset, std::array< gfx::Bitmap, 4 > &sheets, std::vector< uint8_t > &gfx_bin_data)
Load the dungeon map gfx from binary.
constexpr int kDungeonMapBossRooms
Definition dungeon_map.h:30
constexpr int kDungeonMapRoomsPtr
Definition dungeon_map.h:16
constexpr int kNumDungeonMapTile16
Definition dungeon_map.h:38
constexpr int kNumDungeons
Definition dungeon_map.h:36
std::array< std::vector< std::array< std::string, kNumRooms > >, kNumDungeons > DungeonMapLabels
Definition dungeon_map.h:61
absl::StatusOr< std::vector< DungeonMap > > LoadDungeonMaps(Rom &rom, DungeonMapLabels &dungeon_map_labels)
Load the dungeon maps from the ROM.
constexpr int kDungeonMapGfxPtr
Definition dungeon_map.h:19
absl::Status SaveDungeonMaps(Rom &rom, std::vector< DungeonMap > &dungeon_maps)
Save the dungeon maps to the ROM.
uint32_t SnesToPc(uint32_t addr) noexcept
Definition snes.h:8
#define RETURN_IF_ERROR(expr)
Definition snes.cc:22
Tilemap structure for SNES tile-based graphics management.
Definition tilemap.h:118
Pair tile_size
Size of individual tiles (8x8 or 16x16)
Definition tilemap.h:123
Pair map_size
Size of tilemap in tiles.
Definition tilemap.h:124
Bitmap atlas
Master bitmap containing all tiles.
Definition tilemap.h:119
std::vector< std::array< gfx::TileInfo, 4 > > tile_info
Tile metadata (4 tiles per 16x16)
Definition tilemap.h:122
gfx::PaletteGroupMap palette_groups
Definition game_data.h:89