yaze 0.2.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
room.cc
Go to the documentation of this file.
1#include "room.h"
2
3#include <yaze.h>
4
5#include <cstdint>
6#include <vector>
7
8#include "absl/strings/str_cat.h"
9#include "app/rom.h"
12#include "util/log.h"
13
14namespace yaze {
15namespace zelda3 {
16
17RoomSize CalculateRoomSize(Rom *rom, int room_id) {
18 // Calculate the size of the room based on how many objects are used per room
19 // Some notes from hacker Zarby89
20 // vanilla rooms are using in average ~0x80 bytes
21 // a "normal" person who wants more details than vanilla will use around 0x100
22 // bytes per rooms you could fit 128 rooms like that in 1 bank
23 // F8000 I don't remember if that's PC or snes tho
24 // Check last rooms
25 // F8000+(roomid*3)
26 // So we want to search the rom() object at this addressed based on the room
27 // ID since it's the roomid * 3 we will by pulling 3 bytes at a time We can do
28 // this with the rom()->ReadByteVector(addr, size)
29 // Existing room size address calculation...
30 RoomSize room_size;
31 room_size.room_size_pointer = 0;
32 room_size.room_size = 0;
33
34 auto room_size_address = 0xF8000 + (room_id * 3);
35 util::logf("Room #%#03X Addresss: %#06X", room_id, room_size_address);
36
37 // Reading bytes for long address construction
38 uint8_t low = rom->data()[room_size_address];
39 uint8_t high = rom->data()[room_size_address + 1];
40 uint8_t bank = rom->data()[room_size_address + 2];
41
42 // Constructing the long address
43 int long_address = (bank << 16) | (high << 8) | low;
44 util::logf("%#06X", long_address);
45 room_size.room_size_pointer = long_address;
46
47 if (long_address == 0x0A8000) {
48 // Blank room disregard in size calculation
49 util::logf("Size of Room #%#03X: 0 bytes", room_id);
50 room_size.room_size = 0;
51 } else {
52 // use the long address to calculate the size of the room
53 // we will use the room_id_ to calculate the next room's address
54 // and subtract the two to get the size of the room
55
56 int next_room_address = 0xF8000 + ((room_id + 1) * 3);
57 util::logf("Next Room Address: %#06X", next_room_address);
58
59 // Reading bytes for long address construction
60 uint8_t next_low = rom->data()[next_room_address];
61 uint8_t next_high = rom->data()[next_room_address + 1];
62 uint8_t next_bank = rom->data()[next_room_address + 2];
63
64 // Constructing the long address
65 int next_long_address = (next_bank << 16) | (next_high << 8) | next_low;
66 util::logf("%#06X", next_long_address);
67
68 // Calculate the size of the room
69 int actual_room_size = next_long_address - long_address;
70 room_size.room_size = actual_room_size;
71 util::logf("Size of Room #%#03X: %d bytes", room_id, actual_room_size);
72 }
73
74 return room_size;
75}
76
77Room LoadRoomFromRom(Rom *rom, int room_id) {
78 Room room(room_id);
79
80 int header_pointer = (rom->data()[kRoomHeaderPointer + 2] << 16) +
81 (rom->data()[kRoomHeaderPointer + 1] << 8) +
82 (rom->data()[kRoomHeaderPointer]);
83 header_pointer = SnesToPc(header_pointer);
84
85 int address = (rom->data()[kRoomHeaderPointerBank] << 16) +
86 (rom->data()[(header_pointer + 1) + (room_id * 2)] << 8) +
87 rom->data()[(header_pointer) + (room_id * 2)];
88
89 auto header_location = SnesToPc(address);
90
91 room.bg2_ = (background2)((rom->data()[header_location] >> 5) & 0x07);
92 room.collision_ = (CollisionKey)((rom->data()[header_location] >> 2) & 0x07);
93 room.is_light_ = ((rom->data()[header_location]) & 0x01) == 1;
94
95 if (room.is_light_) {
96 room.bg2_ = background2::DarkRoom;
97 }
98
99 room.palette = ((rom->data()[header_location + 1] & 0x3F));
100 room.blockset = (rom->data()[header_location + 2]);
101 room.spriteset = (rom->data()[header_location + 3]);
102 room.effect_ = (EffectKey)((rom->data()[header_location + 4]));
103 room.tag1_ = (TagKey)((rom->data()[header_location + 5]));
104 room.tag2_ = (TagKey)((rom->data()[header_location + 6]));
105
106 room.staircase_plane_[0] = ((rom->data()[header_location + 7] >> 2) & 0x03);
107 room.staircase_plane_[1] = ((rom->data()[header_location + 7] >> 4) & 0x03);
108 room.staircase_plane_[2] = ((rom->data()[header_location + 7] >> 6) & 0x03);
109 room.staircase_plane_[3] = ((rom->data()[header_location + 8]) & 0x03);
110
111 room.holewarp = (rom->data()[header_location + 9]);
112 room.staircase_rooms_[0] = (rom->data()[header_location + 10]);
113 room.staircase_rooms_[1] = (rom->data()[header_location + 11]);
114 room.staircase_rooms_[2] = (rom->data()[header_location + 12]);
115 room.staircase_rooms_[3] = (rom->data()[header_location + 13]);
116
117 // =====
118
119 int header_pointer_2 = (rom->data()[kRoomHeaderPointer + 2] << 16) +
120 (rom->data()[kRoomHeaderPointer + 1] << 8) +
121 (rom->data()[kRoomHeaderPointer]);
122 header_pointer_2 = SnesToPc(header_pointer_2);
123
124 int address_2 = (rom->data()[kRoomHeaderPointerBank] << 16) +
125 (rom->data()[(header_pointer_2 + 1) + (room_id * 2)] << 8) +
126 rom->data()[(header_pointer_2) + (room_id * 2)];
127
128 room.message_id_ = messages_id_dungeon + (room_id * 2);
129
130 auto hpos = SnesToPc(address_2);
131 hpos++;
132 uint8_t b = rom->data()[hpos];
133
134 room.layer2_mode_ = (b >> 5);
135 room.layer_merging_ = kLayerMergeTypeList[(b & 0x0C) >> 2];
136
137 room.is_dark_ = (b & 0x01) == 0x01;
138 hpos++;
139 room.palette_ = rom->data()[hpos];
140 hpos++;
141
142 room.background_tileset_ = rom->data()[hpos];
143 hpos++;
144
145 room.sprite_tileset_ = rom->data()[hpos];
146 hpos++;
147
148 room.layer2_behavior_ = rom->data()[hpos];
149 hpos++;
150
151 room.tag1_ = (TagKey)rom->data()[hpos];
152 hpos++;
153
154 room.tag2_ = (TagKey)rom->data()[hpos];
155 hpos++;
156
157 b = rom->data()[hpos];
158
159 room.pits_.target_layer = (uint8_t)(b & 0x03);
160 room.stair1_.target_layer = (uint8_t)((b >> 2) & 0x03);
161 room.stair2_.target_layer = (uint8_t)((b >> 4) & 0x03);
162 room.stair3_.target_layer = (uint8_t)((b >> 6) & 0x03);
163 hpos++;
164 room.stair4_.target_layer = (uint8_t)(rom->data()[hpos] & 0x03);
165 hpos++;
166
167 room.pits_.target = rom->data()[hpos];
168 hpos++;
169 room.stair1_.target = rom->data()[hpos];
170 hpos++;
171 room.stair2_.target = rom->data()[hpos];
172 hpos++;
173 room.stair3_.target = rom->data()[hpos];
174 hpos++;
175 room.stair4_.target = rom->data()[hpos];
176 hpos++;
177
178 // Load room objects
179 int object_pointer = SnesToPc(room_object_pointer);
180 int room_address = object_pointer + (room_id * 3);
181 int objects_location = SnesToPc(room_address);
182
183 // Load sprites
184 int spr_ptr = 0x040000 | rooms_sprite_pointer;
185 int sprite_address = SnesToPc(dungeon_spr_ptrs | spr_ptr + (room_id * 2));
186
187 return room;
188}
189
190void Room::LoadRoomGraphics(uint8_t entrance_blockset) {
191 const auto &main_gfx = rom()->main_blockset_ids;
192 const auto &room_gfx = rom()->room_blockset_ids;
193 const auto &sprite_gfx = rom()->spriteset_ids;
194 current_gfx16_.reserve(0x4000);
195
196 for (int i = 0; i < 8; i++) {
197 blocks_[i] = main_gfx[blockset][i];
198 if (i >= 6 && i <= 6) {
199 // 3-6
200 if (entrance_blockset != 0xFF &&
201 room_gfx[entrance_blockset][i - 3] != 0) {
202 blocks_[i] = room_gfx[entrance_blockset][i - 3];
203 }
204 }
205 }
206
207 blocks_[8] = 115 + 0; // Static Sprites Blocksets (fairy,pot,ect...)
208 blocks_[9] = 115 + 10;
209 blocks_[10] = 115 + 6;
210 blocks_[11] = 115 + 7;
211 for (int i = 0; i < 4; i++) {
212 blocks_[12 + i] = (uint8_t)(sprite_gfx[spriteset + 64][i] + 115);
213 } // 12-16 sprites
214}
215
216constexpr int kGfxBufferOffset = 92 * 2048;
217constexpr int kGfxBufferStride = 512;
218constexpr int kGfxBufferAnimatedFrameOffset = 7 * 2048;
220constexpr int kGfxBufferRoomOffset = 2048;
221constexpr int kGfxBufferRoomSpriteOffset = 512;
222constexpr int kGfxBufferRoomSpriteStride = 2048;
224
226 auto gfx_buffer_data = rom()->graphics_buffer();
227
228 // Copy room graphics to buffer
229 int sheet_pos = 0;
230 for (int i = 0; i < 16; i++) {
231 int data = 0;
232 int block_offset = blocks_[i] * kGfxBufferRoomOffset;
233 while (data < kGfxBufferRoomOffset) {
234 uint8_t map_byte = gfx_buffer_data[data + block_offset];
235 if (i < 4) {
237 }
238
239 current_gfx16_[data + sheet_pos] = map_byte;
240 data++;
241 }
242
243 sheet_pos += kGfxBufferRoomOffset;
244 }
245
247}
248
250 int gfx_ptr = SnesToPc(rom()->version_constants().kGfxAnimatedPointer);
251
252 auto gfx_buffer_data = rom()->graphics_buffer();
253 auto rom_data = rom()->vector();
254 int data = 0;
255 while (data < 512) {
256 uint8_t map_byte =
257 gfx_buffer_data[data + (92 * 2048) + (512 * animated_frame_)];
258 current_gfx16_[data + (7 * 2048)] = map_byte;
259
260 map_byte =
261 gfx_buffer_data[data +
262 (rom_data[gfx_ptr + background_tileset_] * 2048) +
263 (512 * animated_frame_)];
264 current_gfx16_[data + (7 * 2048) - 512] = map_byte;
265 data++;
266 }
267}
268
270 auto rom_data = rom()->vector();
271 int object_pointer = (rom_data[room_object_pointer + 2] << 16) +
272 (rom_data[room_object_pointer + 1] << 8) +
273 (rom_data[room_object_pointer]);
274 object_pointer = SnesToPc(object_pointer);
275 int room_address = object_pointer + (room_id_ * 3);
276
277 int tile_address = (rom_data[room_address + 2] << 16) +
278 (rom_data[room_address + 1] << 8) + rom_data[room_address];
279
280 int objects_location = SnesToPc(tile_address);
281
282 if (objects_location == 0x52CA2) {
283 std::cout << "Room ID : " << room_id_ << std::endl;
284 }
285
286 if (is_floor_) {
287 floor1_graphics_ = static_cast<uint8_t>(rom_data[objects_location] & 0x0F);
289 static_cast<uint8_t>((rom_data[objects_location] >> 4) & 0x0F);
290 }
291
292 layout = static_cast<uint8_t>((rom_data[objects_location + 1] >> 2) & 0x07);
293
294 LoadChests();
295
296 z3_staircases_.clear();
297 int nbr_of_staircase = 0;
298
299 int pos = objects_location + 2;
300 uint8_t b1 = 0;
301 uint8_t b2 = 0;
302 uint8_t b3 = 0;
303 uint8_t posX = 0;
304 uint8_t posY = 0;
305 uint8_t sizeX = 0;
306 uint8_t sizeY = 0;
307 uint8_t sizeXY = 0;
308 short oid = 0;
309 int layer = 0;
310 bool door = false;
311 bool end_read = false;
312 while (!end_read) {
313 b1 = rom_data[pos];
314 b2 = rom_data[pos + 1];
315
316 if (b1 == 0xFF && b2 == 0xFF) {
317 pos += 2; // We jump to layer2
318 layer++;
319 door = false;
320 if (layer == 3) {
321 break;
322 }
323 continue;
324 }
325
326 if (b1 == 0xF0 && b2 == 0xFF) {
327 pos += 2; // We jump to layer2
328 door = true;
329 continue;
330 }
331
332 b3 = rom_data[pos + 2];
333 if (door) {
334 pos += 2;
335 } else {
336 pos += 3;
337 }
338
339 if (!door) {
340 if (b3 >= 0xF8) {
341 oid = static_cast<short>((b3 << 4) |
342 0x80 + (((b2 & 0x03) << 2) + ((b1 & 0x03))));
343 posX = static_cast<uint8_t>((b1 & 0xFC) >> 2);
344 posY = static_cast<uint8_t>((b2 & 0xFC) >> 2);
345 sizeXY = static_cast<uint8_t>((((b1 & 0x03) << 2) + (b2 & 0x03)));
346 } else {
347 oid = b3;
348 posX = static_cast<uint8_t>((b1 & 0xFC) >> 2);
349 posY = static_cast<uint8_t>((b2 & 0xFC) >> 2);
350 sizeX = static_cast<uint8_t>((b1 & 0x03));
351 sizeY = static_cast<uint8_t>((b2 & 0x03));
352 sizeXY = static_cast<uint8_t>(((sizeX << 2) + sizeY));
353 }
354
355 if (b1 >= 0xFC) {
356 oid = static_cast<short>((b3 & 0x3F) + 0x100);
357 posX = static_cast<uint8_t>(((b2 & 0xF0) >> 4) + ((b1 & 0x3) << 4));
358 posY = static_cast<uint8_t>(((b2 & 0x0F) << 2) + ((b3 & 0xC0) >> 6));
359 sizeXY = 0;
360 }
361
362 RoomObject r(oid, posX, posY, sizeXY, static_cast<uint8_t>(layer));
363 tile_objects_.push_back(r);
364
365 for (short stair : stairsObjects) {
366 if (stair == oid) {
367 if (nbr_of_staircase < 4) {
368 tile_objects_.back().set_options(ObjectOption::Stairs |
369 tile_objects_.back().options());
370 z3_staircases_.push_back(staircase(
371 posX, posY,
372 absl::StrCat("To ", staircase_rooms_[nbr_of_staircase])
373 .data()));
374 nbr_of_staircase++;
375 } else {
376 tile_objects_.back().set_options(ObjectOption::Stairs |
377 tile_objects_.back().options());
378 z3_staircases_.push_back(staircase(posX, posY, "To ???"));
379 }
380 }
381 }
382
383 if (oid == 0xF99) {
384 if (chests_in_room_.size() > 0) {
385 tile_objects_.back().set_options(ObjectOption::Chest |
386 tile_objects_.back().options());
387 // chest_list_.push_back(
388 // z3_chest(posX, posY, chests_in_room_.front().itemIn, false));
389 chests_in_room_.erase(chests_in_room_.begin());
390 }
391 } else if (oid == 0xFB1) {
392 if (chests_in_room_.size() > 0) {
393 tile_objects_.back().set_options(ObjectOption::Chest |
394 tile_objects_.back().options());
395 // chest_list_.push_back(
396 // z3_chest(posX + 1, posY, chests_in_room_.front().item_in,
397 // true));
398 chests_in_room_.erase(chests_in_room_.begin());
399 }
400 }
401 } else {
402 // tile_objects_.push_back(z3_object_door(static_cast<short>((b2 << 8) +
403 // b1),
404 // 0, 0, 0,
405 // static_cast<uint8_t>(layer)));
406 }
407 }
408}
409
411 auto rom_data = rom()->vector();
412 int sprite_pointer = (0x04 << 16) +
413 (rom_data[rooms_sprite_pointer + 1] << 8) +
414 (rom_data[rooms_sprite_pointer]);
415 int sprite_address_snes =
416 (0x09 << 16) + (rom_data[sprite_pointer + (room_id_ * 2) + 1] << 8) +
417 rom_data[sprite_pointer + (room_id_ * 2)];
418
419 int sprite_address = SnesToPc(sprite_address_snes);
420 bool sortsprites = rom_data[sprite_address] == 1;
421 sprite_address += 1;
422
423 while (true) {
424 uint8_t b1 = rom_data[sprite_address];
425 uint8_t b2 = rom_data[sprite_address + 1];
426 uint8_t b3 = rom_data[sprite_address + 2];
427
428 if (b1 == 0xFF) {
429 break;
430 }
431
432 sprites_.emplace_back(b3, (b2 & 0x1F), (b1 & 0x1F),
433 ((b2 & 0xE0) >> 5) + ((b1 & 0x60) >> 2),
434 (b1 & 0x80) >> 7);
435
436 if (sprites_.size() > 1) {
437 Sprite &spr = sprites_.back();
438 Sprite &prevSprite = sprites_[sprites_.size() - 2];
439
440 if (spr.id() == 0xE4 && spr.x() == 0x00 && spr.y() == 0x1E &&
441 spr.layer() == 1 && spr.subtype() == 0x18) {
442 prevSprite.set_key_drop(1);
443 sprites_.pop_back();
444 }
445
446 if (spr.id() == 0xE4 && spr.x() == 0x00 && spr.y() == 0x1D &&
447 spr.layer() == 1 && spr.subtype() == 0x18) {
448 prevSprite.set_key_drop(2);
449 sprites_.pop_back();
450 }
451 }
452
453 sprite_address += 3;
454 }
455}
456
458 auto rom_data = rom()->vector();
459 uint32_t cpos = SnesToPc((rom_data[chests_data_pointer1 + 2] << 16) +
460 (rom_data[chests_data_pointer1 + 1] << 8) +
461 (rom_data[chests_data_pointer1]));
462 size_t clength = (rom_data[chests_length_pointer + 1] << 8) +
463 (rom_data[chests_length_pointer]);
464
465 for (int i = 0; i < clength; i++) {
466 if ((((rom_data[cpos + (i * 3) + 1] << 8) + (rom_data[cpos + (i * 3)])) &
467 0x7FFF) == room_id_) {
468 // There's a chest in that room !
469 bool big = false;
470 if ((((rom_data[cpos + (i * 3) + 1] << 8) + (rom_data[cpos + (i * 3)])) &
471 0x8000) == 0x8000) {
472 big = true;
473 }
474
475 chests_in_room_.emplace_back(
476 chest_data(rom_data[cpos + (i * 3) + 2], big));
477 }
478 }
479}
480
481} // namespace zelda3
482} // namespace yaze
The Rom class is used to load, save, and modify Rom data.
Definition rom.h:59
auto data() const
Definition rom.h:163
auto rom()
Definition rom.h:384
destination pits_
Definition room.h:266
std::vector< RoomObject > tile_objects_
Definition room.h:254
EffectKey effect_
Definition room.h:261
uint8_t palette_
Definition room.h:245
uint8_t staircase_plane_[4]
Definition room.h:239
void LoadChests()
Definition room.cc:457
std::vector< zelda3::Sprite > sprites_
Definition room.h:255
std::vector< uint8_t > current_gfx16_
Definition room.h:229
void CopyRoomGraphicsToBuffer()
Definition room.cc:225
destination stair2_
Definition room.h:268
void LoadRoomGraphics(uint8_t entrance_blockset=0xFF)
Definition room.cc:190
uint8_t layer2_behavior_
Definition room.h:244
uint8_t layer2_mode_
Definition room.h:248
uint8_t holewarp
Definition room.h:220
destination stair4_
Definition room.h:270
int animated_frame_
Definition room.h:237
uint8_t blockset
Definition room.h:216
uint8_t layout
Definition room.h:219
uint8_t sprite_tileset_
Definition room.h:243
uint16_t message_id_
Definition room.h:224
uint8_t staircase_rooms_[4]
Definition room.h:240
std::array< uint8_t, 16 > blocks_
Definition room.h:250
uint8_t floor2_graphics_
Definition room.h:247
background2 bg2_
Definition room.h:265
uint8_t floor1_graphics_
Definition room.h:246
void LoadObjects()
Definition room.cc:269
uint8_t spriteset
Definition room.h:217
destination stair3_
Definition room.h:269
uint8_t palette
Definition room.h:218
void LoadAnimatedGraphics()
Definition room.cc:249
std::vector< chest_data > chests_in_room_
Definition room.h:257
LayerMergeType layer_merging_
Definition room.h:259
std::vector< staircase > z3_staircases_
Definition room.h:256
void LoadSprites()
Definition room.cc:410
destination stair1_
Definition room.h:267
uint8_t background_tileset_
Definition room.h:242
CollisionKey collision_
Definition room.h:260
A class for managing sprites in the overworld and underworld.
Definition sprite.h:279
auto id() const
Definition sprite.h:338
auto layer() const
Definition sprite.h:349
auto set_key_drop(int key)
Definition sprite.h:357
auto subtype() const
Definition sprite.h:350
auto y() const
Definition sprite.h:341
auto x() const
Definition sprite.h:340
Zelda 3 specific classes and functions.
constexpr int kGfxBufferAnimatedFrameStride
Definition room.cc:219
constexpr int kGfxBufferAnimatedFrameOffset
Definition room.cc:218
constexpr int chests_length_pointer
Definition room.h:44
constexpr int chests_data_pointer1
Definition room.h:45
constexpr int kGfxBufferRoomOffset
Definition room.cc:220
constexpr int rooms_sprite_pointer
Definition room.h:40
constexpr int kGfxBufferRoomSpriteOffset
Definition room.cc:221
RoomSize CalculateRoomSize(Rom *rom, int room_id)
Definition room.cc:17
constexpr int messages_id_dungeon
Definition room.h:47
constexpr int kGfxBufferRoomSpriteStride
Definition room.cc:222
constexpr uint16_t stairsObjects[]
Definition room.h:80
Room LoadRoomFromRom(Rom *rom, int room_id)
Definition room.cc:77
constexpr int kRoomHeaderPointer
Definition room.h:41
constexpr int kRoomHeaderPointerBank
Definition room.h:42
constexpr int kGfxBufferStride
Definition room.cc:217
constexpr int room_object_pointer
Definition room.h:35
constexpr int kGfxBufferRoomSpriteLastLineOffset
Definition room.cc:223
constexpr int dungeon_spr_ptrs
Definition room.h:76
constexpr int kGfxBufferOffset
Definition room.cc:216
Main namespace for the application.
Definition controller.cc:18
uint32_t SnesToPc(uint32_t addr) noexcept
Definition rom.h:329
uint8_t target_layer
Definition zelda.h:173
uint8_t target
Definition zelda.h:172
int64_t room_size_pointer
Definition room.h:277
struct chest_data chest_data
struct staircase staircase
background2
Definition zelda.h:130