From 53f76460b4a99f9b4c307543cc239a8dfcadae57 Mon Sep 17 00:00:00 2001 From: scawful Date: Thu, 18 Jan 2024 20:06:30 -0500 Subject: [PATCH] Compute room size and room size pointers in Room class --- src/app/zelda3/dungeon/room.cc | 64 ++++++++++++++++++++++++++++++++++ src/app/zelda3/dungeon/room.h | 8 +++++ 2 files changed, 72 insertions(+) diff --git a/src/app/zelda3/dungeon/room.cc b/src/app/zelda3/dungeon/room.cc index 03e484b7..38e1d1a1 100644 --- a/src/app/zelda3/dungeon/room.cc +++ b/src/app/zelda3/dungeon/room.cc @@ -56,6 +56,70 @@ void Room::LoadHeader() { staircase_rooms_[1] = (rom()->data()[header_location + 11]); staircase_rooms_[2] = (rom()->data()[header_location + 12]); staircase_rooms_[3] = (rom()->data()[header_location + 13]); + + // Calculate the size of the room based on how many objects are used per room + // Some notes from hacker Zarby89 + // vanilla rooms are using in average ~0x80 bytes + // a "normal" person who wants more details than vanilla will use around 0x100 + // bytes per rooms you could fit 128 rooms like that in 1 bank + // F8000 I don't remember if that's PC or snes tho + // Check last rooms + // F8000+(roomid*3) + // So we want to search the rom() object at this addressed based on the room + // ID since it's the roomid * 3 we will by pulling 3 bytes at a time We can do + // this with the rom()->ReadByteVector(addr, size) + try { + // Existing room size address calculation... + auto room_size_address = 0xF8000 + (room_id_ * 3); + + std::cout << "Room #" << room_id_ << " Address: " << std::hex + << room_size_address << std::endl; + + // Reading bytes for long address construction + uint8_t low = rom()->data()[room_size_address]; + uint8_t high = rom()->data()[room_size_address + 1]; + uint8_t bank = rom()->data()[room_size_address + 2]; + + // Constructing the long address + int long_address = (bank << 16) | (high << 8) | low; + std::cout << std::hex << std::setfill('0') << std::setw(6) << long_address + << std::endl; + room_size_pointer_ = long_address; + + if (long_address == 0x0A8000) { + // Blank room disregard in size calculation + std::cout << "Size of Room #" << room_id_ << ": 0 bytes" << std::endl; + room_size_ = 0; + } else { + // use the long address to calculate the size of the room + // we will use the room_id_ to calculate the next room's address + // and subtract the two to get the size of the room + + int next_room_address = 0xF8000 + ((room_id_ + 1) * 3); + + std::cout << "Next Room Address: " << std::hex << next_room_address + << std::endl; + + // Reading bytes for long address construction + uint8_t next_low = rom()->data()[next_room_address]; + uint8_t next_high = rom()->data()[next_room_address + 1]; + uint8_t next_bank = rom()->data()[next_room_address + 2]; + + // Constructing the long address + int next_long_address = (next_bank << 16) | (next_high << 8) | next_low; + + std::cout << std::hex << std::setfill('0') << std::setw(6) + << next_long_address << std::endl; + + // Calculate the size of the room + int room_size = next_long_address - long_address; + std::cout << "Size of Room #" << room_id_ << ": " << std::dec << room_size + << " bytes" << std::endl; + room_size_ = room_size; + } + } catch (const std::exception& e) { + std::cout << "Error: " << e.what() << std::endl; + } } void Room::LoadRoomFromROM() { diff --git a/src/app/zelda3/dungeon/room.h b/src/app/zelda3/dungeon/room.h index 96c9ad31..29699654 100644 --- a/src/app/zelda3/dungeon/room.h +++ b/src/app/zelda3/dungeon/room.h @@ -138,6 +138,9 @@ class Room : public SharedROM { auto layer1() const { return background_bmps_[0]; } auto layer2() const { return background_bmps_[1]; } auto layer3() const { return background_bmps_[2]; } + auto room_size() const { return room_size_; } + auto room_size_ptr() const { return room_size_pointer_; } + auto set_room_size(uint64_t size) { room_size_ = size; } RoomObject AddObject(short oid, uint8_t x, uint8_t y, uint8_t size, uint8_t layer) { @@ -182,6 +185,9 @@ class Room : public SharedROM { uint8_t floor2_graphics_; uint8_t layer2_mode_; + uint64_t room_size_; + int64_t room_size_pointer_; + std::array blocks_; std::array chest_list_; @@ -198,6 +204,8 @@ class Room : public SharedROM { std::vector chests_in_room_; std::vector tile_objects_; + + std::vector room_addresses_; }; } // namespace dungeon