Refactor overworld constructors to accept Rom pointers
This commit is contained in:
@@ -17,8 +17,8 @@
|
||||
namespace yaze {
|
||||
namespace zelda3 {
|
||||
|
||||
absl::Status Overworld::Load(Rom &rom) {
|
||||
if (rom.size() == 0) {
|
||||
absl::Status Overworld::Load(Rom *rom) {
|
||||
if (rom->size() == 0) {
|
||||
return absl::InvalidArgumentError("ROM file not loaded");
|
||||
}
|
||||
rom_ = rom;
|
||||
@@ -97,10 +97,10 @@ void Overworld::FetchLargeMaps() {
|
||||
|
||||
absl::StatusOr<uint16_t> Overworld::GetTile16ForTile32(
|
||||
int index, int quadrant, int dimension, const uint32_t *map32address) {
|
||||
ASSIGN_OR_RETURN(auto arg1,
|
||||
rom_.ReadByte(map32address[dimension] + quadrant + (index)));
|
||||
ASSIGN_OR_RETURN(auto arg2, rom_.ReadWord(map32address[dimension] + (index) +
|
||||
(quadrant <= 1 ? 4 : 5)));
|
||||
ASSIGN_OR_RETURN(
|
||||
auto arg1, rom()->ReadByte(map32address[dimension] + quadrant + (index)));
|
||||
ASSIGN_OR_RETURN(auto arg2, rom()->ReadWord(map32address[dimension] + (index) +
|
||||
(quadrant <= 1 ? 4 : 5)));
|
||||
return (uint16_t)(arg1 +
|
||||
(((arg2 >> (quadrant % 2 == 0 ? 4 : 0)) & 0x0F) * 256));
|
||||
}
|
||||
@@ -108,13 +108,13 @@ absl::StatusOr<uint16_t> Overworld::GetTile16ForTile32(
|
||||
absl::Status Overworld::AssembleMap32Tiles() {
|
||||
constexpr int kMap32TilesLength = 0x33F0;
|
||||
int num_tile32 = kMap32TilesLength;
|
||||
uint32_t map32address[4] = {rom_.version_constants().kMap32TileTL,
|
||||
rom_.version_constants().kMap32TileTR,
|
||||
rom_.version_constants().kMap32TileBL,
|
||||
rom_.version_constants().kMap32TileBR};
|
||||
uint32_t map32address[4] = {rom()->version_constants().kMap32TileTL,
|
||||
rom()->version_constants().kMap32TileTR,
|
||||
rom()->version_constants().kMap32TileBL,
|
||||
rom()->version_constants().kMap32TileBR};
|
||||
if (rom()->data()[kMap32ExpandedFlagPos] != 0x04 &&
|
||||
core::FeatureFlags::get().overworld.kLoadCustomOverworld) {
|
||||
map32address[0] = rom_.version_constants().kMap32TileTL;
|
||||
map32address[0] = rom()->version_constants().kMap32TileTL;
|
||||
map32address[1] = kMap32TileTRExpanded;
|
||||
map32address[2] = kMap32TileBLExpanded;
|
||||
map32address[3] = kMap32TileBRExpanded;
|
||||
@@ -338,10 +338,8 @@ absl::Status Overworld::LoadEntrances() {
|
||||
absl::Status Overworld::LoadHoles() {
|
||||
constexpr int kNumHoles = 0x13;
|
||||
for (int i = 0; i < kNumHoles; i++) {
|
||||
ASSIGN_OR_RETURN(auto map_id,
|
||||
rom()->ReadWord(kOverworldHoleArea + (i * 2)));
|
||||
ASSIGN_OR_RETURN(auto map_pos,
|
||||
rom()->ReadWord(kOverworldHolePos + (i * 2)));
|
||||
ASSIGN_OR_RETURN(auto map_id, rom()->ReadWord(kOverworldHoleArea + (i * 2)));
|
||||
ASSIGN_OR_RETURN(auto map_pos, rom()->ReadWord(kOverworldHolePos + (i * 2)));
|
||||
ASSIGN_OR_RETURN(auto entrance_id,
|
||||
rom()->ReadByte(kOverworldHoleEntrance + i));
|
||||
int p = (map_pos + 0x400) >> 1;
|
||||
@@ -412,8 +410,7 @@ absl::Status Overworld::LoadItems() {
|
||||
rom()->ReadLong(zelda3::kOverworldItemsAddress));
|
||||
uint32_t pointer_pc = SnesToPc(pointer); // 1BC2F9 -> 0DC2F9
|
||||
for (int i = 0; i < 128; i++) {
|
||||
ASSIGN_OR_RETURN(uint16_t word_address,
|
||||
rom()->ReadWord(pointer_pc + i * 2));
|
||||
ASSIGN_OR_RETURN(uint16_t word_address, rom()->ReadWord(pointer_pc + i * 2));
|
||||
uint32_t addr = (pointer & 0xFF0000) | word_address; // 1B F9 3C
|
||||
addr = SnesToPc(addr);
|
||||
|
||||
@@ -515,7 +512,7 @@ absl::Status Overworld::LoadSpritesFromMap(int sprites_per_gamestate_ptr,
|
||||
return absl::OkStatus();
|
||||
}
|
||||
|
||||
absl::Status Overworld::Save(Rom &rom) {
|
||||
absl::Status Overworld::Save(Rom *rom) {
|
||||
rom_ = rom;
|
||||
if (expanded_tile16_) RETURN_IF_ERROR(SaveMap16Expanded())
|
||||
RETURN_IF_ERROR(SaveMap16Tiles())
|
||||
@@ -676,8 +673,8 @@ absl::Status Overworld::SaveLargeMaps() {
|
||||
int parent_x_pos = overworld_maps_[i].parent() % 8;
|
||||
|
||||
// Always write the map parent since it should not matter
|
||||
RETURN_IF_ERROR(rom()->WriteByte(kOverworldMapParentId + i,
|
||||
overworld_maps_[i].parent()))
|
||||
RETURN_IF_ERROR(
|
||||
rom()->WriteByte(kOverworldMapParentId + i, overworld_maps_[i].parent()))
|
||||
|
||||
if (std::find(checked_map.begin(), checked_map.end(), i) !=
|
||||
checked_map.end()) {
|
||||
@@ -700,72 +697,72 @@ absl::Status Overworld::SaveLargeMaps() {
|
||||
RETURN_IF_ERROR(
|
||||
rom()->WriteByte(kOverworldScreenSize + i + offset + 64, 0x00));
|
||||
// Check 4
|
||||
RETURN_IF_ERROR(rom()->WriteByte(
|
||||
kOverworldScreenSizeForLoading + i + offset, 0x04));
|
||||
RETURN_IF_ERROR(
|
||||
rom()->WriteByte(kOverworldScreenSizeForLoading + i + offset, 0x04));
|
||||
RETURN_IF_ERROR(rom()->WriteByte(
|
||||
kOverworldScreenSizeForLoading + i + offset + kDarkWorldMapIdStart,
|
||||
0x04));
|
||||
RETURN_IF_ERROR(rom()->WriteByte(kOverworldScreenSizeForLoading + i +
|
||||
offset + kSpecialWorldMapIdStart,
|
||||
0x04));
|
||||
offset + kSpecialWorldMapIdStart,
|
||||
0x04));
|
||||
}
|
||||
|
||||
// Check 5 and 6
|
||||
RETURN_IF_ERROR(
|
||||
rom()->WriteShort(kTransitionTargetNorth + (i * 2),
|
||||
(uint16_t)((parent_y_pos * 0x200) - 0xE0)));
|
||||
(uint16_t)((parent_y_pos * 0x200) - 0xE0)));
|
||||
RETURN_IF_ERROR(
|
||||
rom()->WriteShort(kTransitionTargetWest + (i * 2),
|
||||
(uint16_t)((parent_x_pos * 0x200) - 0x100)));
|
||||
(uint16_t)((parent_x_pos * 0x200) - 0x100)));
|
||||
|
||||
RETURN_IF_ERROR(
|
||||
rom()->WriteShort(kTransitionTargetNorth + (i * 2) + 2,
|
||||
(uint16_t)((parent_y_pos * 0x200) - 0xE0)));
|
||||
(uint16_t)((parent_y_pos * 0x200) - 0xE0)));
|
||||
RETURN_IF_ERROR(
|
||||
rom()->WriteShort(kTransitionTargetWest + (i * 2) + 2,
|
||||
(uint16_t)((parent_x_pos * 0x200) - 0x100)));
|
||||
(uint16_t)((parent_x_pos * 0x200) - 0x100)));
|
||||
|
||||
RETURN_IF_ERROR(
|
||||
rom()->WriteShort(kTransitionTargetNorth + (i * 2) + 16,
|
||||
(uint16_t)((parent_y_pos * 0x200) - 0xE0)));
|
||||
(uint16_t)((parent_y_pos * 0x200) - 0xE0)));
|
||||
RETURN_IF_ERROR(
|
||||
rom()->WriteShort(kTransitionTargetWest + (i * 2) + 16,
|
||||
(uint16_t)((parent_x_pos * 0x200) - 0x100)));
|
||||
(uint16_t)((parent_x_pos * 0x200) - 0x100)));
|
||||
|
||||
RETURN_IF_ERROR(
|
||||
rom()->WriteShort(kTransitionTargetNorth + (i * 2) + 18,
|
||||
(uint16_t)((parent_y_pos * 0x200) - 0xE0)));
|
||||
(uint16_t)((parent_y_pos * 0x200) - 0xE0)));
|
||||
RETURN_IF_ERROR(
|
||||
rom()->WriteShort(kTransitionTargetWest + (i * 2) + 18,
|
||||
(uint16_t)((parent_x_pos * 0x200) - 0x100)));
|
||||
(uint16_t)((parent_x_pos * 0x200) - 0x100)));
|
||||
|
||||
// Check 7 and 8
|
||||
RETURN_IF_ERROR(rom()->WriteShort(kOverworldTransitionPositionX + (i * 2),
|
||||
(parent_x_pos * 0x200)));
|
||||
(parent_x_pos * 0x200)));
|
||||
RETURN_IF_ERROR(rom()->WriteShort(kOverworldTransitionPositionY + (i * 2),
|
||||
(parent_y_pos * 0x200)));
|
||||
(parent_y_pos * 0x200)));
|
||||
|
||||
RETURN_IF_ERROR(
|
||||
rom()->WriteShort(kOverworldTransitionPositionX + (i * 2) + 02,
|
||||
(parent_x_pos * 0x200)));
|
||||
(parent_x_pos * 0x200)));
|
||||
RETURN_IF_ERROR(
|
||||
rom()->WriteShort(kOverworldTransitionPositionY + (i * 2) + 02,
|
||||
(parent_y_pos * 0x200)));
|
||||
(parent_y_pos * 0x200)));
|
||||
|
||||
// problematic
|
||||
RETURN_IF_ERROR(
|
||||
rom()->WriteShort(kOverworldTransitionPositionX + (i * 2) + 16,
|
||||
(parent_x_pos * 0x200)));
|
||||
(parent_x_pos * 0x200)));
|
||||
RETURN_IF_ERROR(
|
||||
rom()->WriteShort(kOverworldTransitionPositionY + (i * 2) + 16,
|
||||
(parent_y_pos * 0x200)));
|
||||
(parent_y_pos * 0x200)));
|
||||
|
||||
RETURN_IF_ERROR(
|
||||
rom()->WriteShort(kOverworldTransitionPositionX + (i * 2) + 18,
|
||||
(parent_x_pos * 0x200)));
|
||||
(parent_x_pos * 0x200)));
|
||||
RETURN_IF_ERROR(
|
||||
rom()->WriteShort(kOverworldTransitionPositionY + (i * 2) + 18,
|
||||
(parent_y_pos * 0x200)));
|
||||
(parent_y_pos * 0x200)));
|
||||
|
||||
// Check 9
|
||||
RETURN_IF_ERROR(rom()->WriteShort(
|
||||
@@ -958,14 +955,14 @@ absl::Status Overworld::SaveLargeMaps() {
|
||||
}
|
||||
|
||||
RETURN_IF_ERROR(rom()->WriteShort(kTransitionTargetNorth + (i * 2),
|
||||
(uint16_t)((y_pos * 0x200) - 0xE0)));
|
||||
(uint16_t)((y_pos * 0x200) - 0xE0)));
|
||||
RETURN_IF_ERROR(rom()->WriteShort(kTransitionTargetWest + (i * 2),
|
||||
(uint16_t)((x_pos * 0x200) - 0x100)));
|
||||
(uint16_t)((x_pos * 0x200) - 0x100)));
|
||||
|
||||
RETURN_IF_ERROR(rom()->WriteShort(kOverworldTransitionPositionX + (i * 2),
|
||||
(x_pos * 0x200)));
|
||||
(x_pos * 0x200)));
|
||||
RETURN_IF_ERROR(rom()->WriteShort(kOverworldTransitionPositionY + (i * 2),
|
||||
(y_pos * 0x200)));
|
||||
(y_pos * 0x200)));
|
||||
|
||||
checked_map.emplace_back(i);
|
||||
}
|
||||
@@ -1352,17 +1349,13 @@ absl::Status Overworld::SaveMap16Tiles() {
|
||||
int tpos = kMap16Tiles;
|
||||
// 3760
|
||||
for (int i = 0; i < NumberOfMap16; i += 1) {
|
||||
RETURN_IF_ERROR(
|
||||
rom()->WriteShort(tpos, TileInfoToShort(tiles16_[i].tile0_)))
|
||||
RETURN_IF_ERROR(rom()->WriteShort(tpos, TileInfoToShort(tiles16_[i].tile0_)))
|
||||
tpos += 2;
|
||||
RETURN_IF_ERROR(
|
||||
rom()->WriteShort(tpos, TileInfoToShort(tiles16_[i].tile1_)))
|
||||
RETURN_IF_ERROR(rom()->WriteShort(tpos, TileInfoToShort(tiles16_[i].tile1_)))
|
||||
tpos += 2;
|
||||
RETURN_IF_ERROR(
|
||||
rom()->WriteShort(tpos, TileInfoToShort(tiles16_[i].tile2_)))
|
||||
RETURN_IF_ERROR(rom()->WriteShort(tpos, TileInfoToShort(tiles16_[i].tile2_)))
|
||||
tpos += 2;
|
||||
RETURN_IF_ERROR(
|
||||
rom()->WriteShort(tpos, TileInfoToShort(tiles16_[i].tile3_)))
|
||||
RETURN_IF_ERROR(rom()->WriteShort(tpos, TileInfoToShort(tiles16_[i].tile3_)))
|
||||
tpos += 2;
|
||||
}
|
||||
return absl::OkStatus();
|
||||
@@ -1383,11 +1376,11 @@ absl::Status Overworld::SaveEntrances() {
|
||||
|
||||
for (int i = 0; i < kNumOverworldEntrances; i++) {
|
||||
RETURN_IF_ERROR(rom()->WriteShort(kOverworldEntranceMap + (i * 2),
|
||||
all_entrances_[i].map_id_))
|
||||
all_entrances_[i].map_id_))
|
||||
RETURN_IF_ERROR(rom()->WriteShort(kOverworldEntrancePos + (i * 2),
|
||||
all_entrances_[i].map_pos_))
|
||||
all_entrances_[i].map_pos_))
|
||||
RETURN_IF_ERROR(rom()->WriteByte(kOverworldEntranceEntranceId + i,
|
||||
all_entrances_[i].entrance_id_))
|
||||
all_entrances_[i].entrance_id_))
|
||||
}
|
||||
|
||||
for (int i = 0; i < kNumOverworldHoles; i++) {
|
||||
@@ -1395,8 +1388,8 @@ absl::Status Overworld::SaveEntrances() {
|
||||
rom()->WriteShort(kOverworldHoleArea + (i * 2), all_holes_[i].map_id_))
|
||||
RETURN_IF_ERROR(
|
||||
rom()->WriteShort(kOverworldHolePos + (i * 2), all_holes_[i].map_pos_))
|
||||
RETURN_IF_ERROR(rom()->WriteByte(kOverworldHoleEntrance + i,
|
||||
all_holes_[i].entrance_id_))
|
||||
RETURN_IF_ERROR(
|
||||
rom()->WriteByte(kOverworldHoleEntrance + i, all_holes_[i].entrance_id_))
|
||||
}
|
||||
|
||||
return absl::OkStatus();
|
||||
@@ -1427,9 +1420,9 @@ absl::Status Overworld::SaveExits() {
|
||||
RETURN_IF_ERROR(
|
||||
rom()->WriteByte(OWExitUnk2 + i, all_exits_[i].scroll_mod_x_));
|
||||
RETURN_IF_ERROR(rom()->WriteShort(OWExitDoorType1 + (i * 2),
|
||||
all_exits_[i].door_type_1_));
|
||||
all_exits_[i].door_type_1_));
|
||||
RETURN_IF_ERROR(rom()->WriteShort(OWExitDoorType2 + (i * 2),
|
||||
all_exits_[i].door_type_2_));
|
||||
all_exits_[i].door_type_2_));
|
||||
}
|
||||
|
||||
return absl::OkStatus();
|
||||
@@ -1545,49 +1538,49 @@ absl::Status Overworld::SaveItems() {
|
||||
absl::Status Overworld::SaveMapProperties() {
|
||||
util::logf("Saving Map Properties");
|
||||
for (int i = 0; i < kDarkWorldMapIdStart; i++) {
|
||||
RETURN_IF_ERROR(rom()->WriteByte(kAreaGfxIdPtr + i,
|
||||
overworld_maps_[i].area_graphics()));
|
||||
RETURN_IF_ERROR(
|
||||
rom()->WriteByte(kAreaGfxIdPtr + i, overworld_maps_[i].area_graphics()));
|
||||
RETURN_IF_ERROR(rom()->WriteByte(kOverworldMapPaletteIds + i,
|
||||
overworld_maps_[i].area_palette()));
|
||||
overworld_maps_[i].area_palette()));
|
||||
RETURN_IF_ERROR(rom()->WriteByte(kOverworldSpriteset + i,
|
||||
overworld_maps_[i].sprite_graphics(0)));
|
||||
overworld_maps_[i].sprite_graphics(0)));
|
||||
RETURN_IF_ERROR(
|
||||
rom()->WriteByte(kOverworldSpriteset + kDarkWorldMapIdStart + i,
|
||||
overworld_maps_[i].sprite_graphics(1)));
|
||||
overworld_maps_[i].sprite_graphics(1)));
|
||||
RETURN_IF_ERROR(
|
||||
rom()->WriteByte(kOverworldSpriteset + kSpecialWorldMapIdStart + i,
|
||||
overworld_maps_[i].sprite_graphics(2)));
|
||||
overworld_maps_[i].sprite_graphics(2)));
|
||||
RETURN_IF_ERROR(rom()->WriteByte(kOverworldSpritePaletteIds + i,
|
||||
overworld_maps_[i].sprite_palette(0)));
|
||||
overworld_maps_[i].sprite_palette(0)));
|
||||
RETURN_IF_ERROR(
|
||||
rom()->WriteByte(kOverworldSpritePaletteIds + kDarkWorldMapIdStart + i,
|
||||
overworld_maps_[i].sprite_palette(1)));
|
||||
overworld_maps_[i].sprite_palette(1)));
|
||||
RETURN_IF_ERROR(rom()->WriteByte(
|
||||
kOverworldSpritePaletteIds + kSpecialWorldMapIdStart + i,
|
||||
overworld_maps_[i].sprite_palette(2)));
|
||||
}
|
||||
|
||||
for (int i = kDarkWorldMapIdStart; i < kSpecialWorldMapIdStart; i++) {
|
||||
RETURN_IF_ERROR(rom()->WriteByte(kAreaGfxIdPtr + i,
|
||||
overworld_maps_[i].area_graphics()));
|
||||
RETURN_IF_ERROR(
|
||||
rom()->WriteByte(kAreaGfxIdPtr + i, overworld_maps_[i].area_graphics()));
|
||||
RETURN_IF_ERROR(rom()->WriteByte(kOverworldSpriteset + i,
|
||||
overworld_maps_[i].sprite_graphics(0)));
|
||||
overworld_maps_[i].sprite_graphics(0)));
|
||||
RETURN_IF_ERROR(
|
||||
rom()->WriteByte(kOverworldSpriteset + kDarkWorldMapIdStart + i,
|
||||
overworld_maps_[i].sprite_graphics(1)));
|
||||
overworld_maps_[i].sprite_graphics(1)));
|
||||
RETURN_IF_ERROR(
|
||||
rom()->WriteByte(kOverworldSpriteset + kSpecialWorldMapIdStart + i,
|
||||
overworld_maps_[i].sprite_graphics(2)));
|
||||
overworld_maps_[i].sprite_graphics(2)));
|
||||
RETURN_IF_ERROR(rom()->WriteByte(kOverworldMapPaletteIds + i,
|
||||
overworld_maps_[i].area_palette()));
|
||||
overworld_maps_[i].area_palette()));
|
||||
RETURN_IF_ERROR(
|
||||
rom()->WriteByte(kOverworldSpritePaletteIds + kDarkWorldMapIdStart + i,
|
||||
overworld_maps_[i].sprite_palette(0)));
|
||||
overworld_maps_[i].sprite_palette(0)));
|
||||
RETURN_IF_ERROR(rom()->WriteByte(
|
||||
kOverworldSpritePaletteIds + kSpecialWorldMapIdStart + i,
|
||||
overworld_maps_[i].sprite_palette(1)));
|
||||
RETURN_IF_ERROR(rom()->WriteByte(kOverworldSpritePaletteIds + 192 + i,
|
||||
overworld_maps_[i].sprite_palette(2)));
|
||||
overworld_maps_[i].sprite_palette(2)));
|
||||
}
|
||||
|
||||
return absl::OkStatus();
|
||||
|
||||
Reference in New Issue
Block a user