diff --git a/asm/bmmap.s b/asm/bmmap.s index 97da7b13..5da136a1 100644 --- a/asm/bmmap.s +++ b/asm/bmmap.s @@ -2,301 +2,6 @@ .SYNTAX UNIFIED - THUMB_FUNC_START sub_8019624 -sub_8019624: @ 0x08019624 - push {r4, r5, r6, r7, lr} - mov r7, sl - mov r6, r9 - mov r5, r8 - push {r5, r6, r7} - movs r6, #0 - ldr r1, _080196FC @ gBmMapSize - movs r2, #2 - ldrsh r0, [r1, r2] - mov r9, r1 - cmp r6, r0 - blt _0801963E - b _0801976A -_0801963E: - ldr r0, _08019700 @ gUnknown_0859A9D4 - mov sl, r0 -_08019642: - movs r4, #0 - movs r2, #0 - ldrsh r0, [r1, r2] - adds r1, r6, #1 - mov r8, r1 - cmp r4, r0 - blt _08019652 - b _0801975C -_08019652: - ldr r2, _08019704 @ gBmMapTerrain - mov ip, r2 - lsls r3, r6, #2 - mov r7, sl - movs r5, #0 -_0801965C: - mov r1, ip - ldr r0, [r1] - adds r0, r3, r0 - ldr r0, [r0] - adds r1, r0, r4 - ldrb r0, [r1] - cmp r0, #0x3c - bne _0801974C - movs r2, #0 - cmp r4, #0 - ble _0801968E - subs r0, r1, #1 - ldrb r0, [r0] - cmp r0, #0x17 - bne _0801967C - movs r2, #1 -_0801967C: - cmp r0, #0x2d - bne _08019682 - movs r2, #1 -_08019682: - cmp r0, #0x20 - bne _08019688 - movs r2, #1 -_08019688: - cmp r0, #0x21 - bne _0801968E - movs r2, #1 -_0801968E: - cmp r6, #0 - ble _080196B8 - mov r1, ip - ldr r0, [r1] - adds r0, r3, r0 - subs r0, #4 - ldr r0, [r0] - adds r0, r0, r4 - ldrb r0, [r0] - cmp r0, #0x17 - bne _080196A6 - adds r2, #2 -_080196A6: - cmp r0, #0x2d - bne _080196AC - adds r2, #2 -_080196AC: - cmp r0, #0x20 - bne _080196B2 - adds r2, #2 -_080196B2: - cmp r0, #0x21 - bne _080196B8 - adds r2, #2 -_080196B8: - cmp r4, #0 - ble _080196EC - cmp r6, #0 - ble _080196EC - mov r1, ip - ldr r0, [r1] - adds r1, r3, r0 - ldr r0, [r1] - adds r0, r4, r0 - subs r0, #1 - ldrb r0, [r0] - cmp r0, #0x17 - bne _080196EC - ldr r0, [r1, #4] - adds r0, r4, r0 - subs r0, #1 - ldrb r0, [r0] - cmp r0, #0x3c - bne _080196EC - subs r0, r1, #4 - ldr r0, [r0] - adds r0, r0, r4 - ldrb r0, [r0] - cmp r0, #0x17 - beq _080196EC - movs r2, #4 -_080196EC: - cmp r2, #2 - beq _08019720 - cmp r2, #2 - bgt _08019708 - cmp r2, #1 - beq _08019712 - b _0801974C - .align 2, 0 -_080196FC: .4byte gBmMapSize -_08019700: .4byte gUnknown_0859A9D4 -_08019704: .4byte gBmMapTerrain -_08019708: - cmp r2, #3 - beq _0801972E - cmp r2, #4 - beq _0801973C - b _0801974C -_08019712: - ldr r0, [r7] - adds r0, r3, r0 - ldr r0, [r0] - adds r0, r5, r0 - movs r2, #0xb7 - lsls r2, r2, #2 - b _08019748 -_08019720: - ldr r0, [r7] - adds r0, r3, r0 - ldr r0, [r0] - adds r0, r5, r0 - movs r2, #0xb6 - lsls r2, r2, #2 - b _08019748 -_0801972E: - ldr r0, [r7] - adds r0, r3, r0 - ldr r0, [r0] - adds r0, r5, r0 - movs r2, #0xd6 - lsls r2, r2, #2 - b _08019748 -_0801973C: - ldr r0, [r7] - adds r0, r3, r0 - ldr r0, [r0] - adds r0, r5, r0 - movs r2, #0xd7 - lsls r2, r2, #2 -_08019748: - adds r1, r2, #0 - strh r1, [r0] -_0801974C: - adds r5, #2 - adds r4, #1 - mov r1, r9 - movs r2, #0 - ldrsh r0, [r1, r2] - cmp r4, r0 - bge _0801975C - b _0801965C -_0801975C: - mov r6, r8 - mov r1, r9 - movs r2, #2 - ldrsh r0, [r1, r2] - cmp r6, r0 - bge _0801976A - b _08019642 -_0801976A: - pop {r3, r4, r5} - mov r8, r3 - mov r9, r4 - mov sl, r5 - pop {r4, r5, r6, r7} - pop {r0} - bx r0 - - THUMB_FUNC_START sub_8019778 -sub_8019778: @ 0x08019778 - push {lr} - ldr r0, _0801979C @ gUnknown_02001000 - ldr r1, _080197A0 @ gUnknown_0202BCF0 - ldrb r1, [r1, #0xe] - lsls r1, r1, #0x18 - asrs r1, r1, #0x18 - bl LoadChapterMap - bl FlushTilesFromBuffer - bl ApplyTrapMapChanges - bl FlushTerrainData - bl sub_8019624 - pop {r0} - bx r0 - .align 2, 0 -_0801979C: .4byte gUnknown_02001000 -_080197A0: .4byte gUnknown_0202BCF0 - - THUMB_FUNC_START SetupMapRowPointers -SetupMapRowPointers: @ 0x080197A4 - push {r4, r5, r6, r7, lr} - mov ip, r1 - adds r6, r2, #0 - adds r5, r3, #0 - ldr r2, _080197E0 @ gUnknown_03000808 - str r0, [r2] - adds r6, #2 - adds r5, #4 - lsls r1, r5, #2 - adds r4, r0, r1 - movs r3, #0 - adds r7, r2, #0 - cmp r3, r5 - bge _080197D0 -_080197C0: - ldr r1, [r2] - lsls r0, r3, #2 - adds r0, r0, r1 - str r4, [r0] - adds r4, r4, r6 - adds r3, #1 - cmp r3, r5 - blt _080197C0 -_080197D0: - ldr r0, [r7] - adds r0, #8 - mov r1, ip - str r0, [r1] - pop {r4, r5, r6, r7} - pop {r0} - bx r0 - .align 2, 0 -_080197E0: .4byte gUnknown_03000808 - - THUMB_FUNC_START ClearMapWith -ClearMapWith: @ 0x080197E4 - push {r4, r5, lr} - sub sp, #4 - adds r5, r0, #0 - adds r3, r1, #0 - ldr r0, _0801983C @ gBmMapSize - movs r2, #2 - ldrsh r1, [r0, r2] - adds r1, #4 - movs r2, #0 - ldrsh r0, [r0, r2] - adds r0, #2 - adds r4, r1, #0 - muls r4, r0, r4 - movs r0, #1 - ands r0, r4 - cmp r0, #0 - beq _08019808 - subs r4, #1 -_08019808: - movs r0, #0xff - ands r3, r0 - lsls r0, r3, #8 - adds r3, r3, r0 - mov r0, sp - strh r3, [r0] - adds r0, r5, #0 - subs r0, #8 - ldr r1, [r0] - lsrs r2, r4, #0x1f - adds r2, r4, r2 - lsls r2, r2, #0xa - lsrs r2, r2, #0xb - movs r0, #0x80 - lsls r0, r0, #0x11 - orrs r2, r0 - mov r0, sp - bl CpuSet - adds r0, r5, #0 - bl SetSubjectMap - add sp, #4 - pop {r4, r5} - pop {r0} - bx r0 - .align 2, 0 -_0801983C: .4byte gBmMapSize - THUMB_FUNC_START sub_8019840 sub_8019840: @ 0x08019840 push {r4, r5, r6, r7, lr} diff --git a/include/constants/terrains.h b/include/constants/terrains.h new file mode 100644 index 00000000..206dc698 --- /dev/null +++ b/include/constants/terrains.h @@ -0,0 +1,77 @@ +#ifndef GUARD_CONSTANTS_TERRAINS_H +#define GUARD_CONSTANTS_TERRAINS_H + +enum { + // Terrain identifiers + + // I'm going by the in-game names + some old FE7 nmm for this + // TODO: figure out in better details + + TERRAIN_TILE_00 = 0x00, + TERRAIN_PLAINS = 0x01, + TERRAIN_ROAD = 0x02, + TERRAIN_VILLAGE_03 = 0x03, + TERRAIN_VILLAGE_04 = 0x04, + TERRIAN_HOUSE = 0x05, + TERRAIN_ARMORY = 0x06, + TERRAIN_VENDOR = 0x07, + TERRAIN_ARENA_08 = 0x08, + TERRAIN_C_ROOM_09 = 0x09, + TERRAIN_FORT = 0x0A, + TERRAIN_GATE_0B = 0x0B, + TERRAIN_FOREST = 0x0C, + TERRAIN_THICKET = 0x0D, + TERRAIN_SAND = 0x0E, + TERRAIN_DESERT = 0x0F, + TERRAIN_RIVER = 0x10, + TERRAIN_MOUNTAIN = 0x11, + TERRAIN_PEAK = 0x12, + TERRAIN_BRIDGE_13 = 0x13, + TERRAIN_BRIDGE_14 = 0x14, + TERRAIN_SEA = 0x15, + TERRAIN_LAKE = 0x16, + TERRAIN_FLOOR_17 = 0x17, + TERRAIN_FLOOR_18 = 0x18, + TERRAIN_FENCE_19 = 0x19, + TERRAIN_WALL_1A = 0x1A, + TERRAIN_WALL_1B = 0x1B, + TERRAIN_RUBBLE = 0x1C, + TERRAIN_PILLAR = 0x1D, + TERRAIN_DOOR = 0x1E, + TERRAIN_THRONE = 0x1F, + TERRAIN_CHEST_20 = 0x20, + TERRAIN_CHEST_21 = 0x21, + TERRAIN_ROOF = 0x22, + TERRAIN_GATE_23 = 0x23, + TERRAIN_CHURCH = 0x24, + TERRAIN_RUINS_25 = 0x25, + TERRAIN_CLIFF = 0x26, + TERRAIN_BALLISTA_REGULAR = 0x27, + TERRAIN_BALLISTA_LONG = 0x28, + TERRAIN_BALLISTA_KILLER = 0x29, + TERRAIN_SHIP_FLAT = 0x2A, + TERRAIN_SHIP_WRECK = 0x2B, + TERRAIN_TILE_2C = 0x2C, + TERRAIN_STAIRS = 0x2D, + TERRAIN_TILE_2E = 0x2E, + TERRAIN_GLACIER = 0x2F, + TERRAIN_ARENA_30 = 0x30, + TERRAIN_VALLEY = 0x31, + TERRAIN_FENCE_32 = 0x32, + TERRAIN_SNAG = 0x33, + TERRAIN_BRIDGE_34 = 0x34, + TERRAIN_SKY = 0x35, + TERRAIN_DEEPS = 0x36, + TERRAIN_RUINS_37 = 0x37, + TERRAIN_INN = 0x38, + TERRAIN_BARREL = 0x39, + TERRAIN_BONE = 0x3A, + TERRAIN_DARK = 0x3B, + TERRAIN_WATER = 0x3C, + TERRAIN_GUNNELS = 0x3D, + TERRAIN_DECK = 0x3E, + TERRAIN_BRACE = 0x3F, + TERRAIN_MAST = 0x40, +}; + +#endif // GUARD_CONSTANTS_TERRAINS_H diff --git a/include/functions.h b/include/functions.h index 7fc4f459..ffbc9c36 100644 --- a/include/functions.h +++ b/include/functions.h @@ -859,7 +859,7 @@ void FillMovementMapSomehow(int x, int y, const s8* movCostLookup); // ??? FillMapStaffRangeForUnit(???); // ??? ApplyStuffToRangeMaps(???); // ??? sub_801B950(???); -// ??? SetSubjectMap(???); +void SetSubjectMap(u8** map); // ??? FillRangeMap(???); // ??? GetCurrentMovCostTable(???); // ??? sub_801B9EC(???); diff --git a/src/bmmap.c b/src/bmmap.c index ae001c9d..326ff8bb 100644 --- a/src/bmmap.c +++ b/src/bmmap.c @@ -1,9 +1,11 @@ #include "global.h" +#include "constants/terrains.h" + void InitChapterMap(int chapterId); -// ??? sub_80195BC(???); +void sub_80195BC(int chapterId); void sub_8019624(void); -// ??? sub_8019778(???); +void sub_8019778(void); void SetupMapRowPointers(void* buffer, u8*** outHandle, int width, int height); void ClearMapWith(u8** map, int value); // ??? sub_8019840(???); @@ -61,6 +63,9 @@ extern u8 gBmMapFogPool[]; extern u8 gBmMapHiddenPool[]; extern u8 gBmMapUnkPool[]; +extern u16** const gUnknown_0859A9D4; +extern u8** gUnknown_03000808; + void InitChapterMap(int chapterId) { LoadChapterMap(gUnknown_02001000, chapterId); LoadChapterMapGfx(chapterId); @@ -97,3 +102,117 @@ void sub_80195BC(int chapterId) { FlushTilesFromBuffer(); FlushTerrainData(); } + +void sub_8019624(void) { + int ix, iy; + + // Automatic water shadows? + + for (iy = 0; iy < gBmMapSize.height; ++iy) { + for (ix = 0; ix < gBmMapSize.width; ++ix) { + int connexion; + + if (gBmMapTerrain[iy][ix] != TERRAIN_WATER) + continue; + + connexion = 0; + + if (ix > 0) { + if (gBmMapTerrain[iy][ix - 1] == TERRAIN_FLOOR_17) + connexion = 1; + + if (gBmMapTerrain[iy][ix - 1] == TERRAIN_STAIRS) + connexion = 1; + + if (gBmMapTerrain[iy][ix - 1] == TERRAIN_CHEST_20) + connexion = 1; + + if (gBmMapTerrain[iy][ix - 1] == TERRAIN_CHEST_21) + connexion = 1; + } + + if (iy > 0) { + if (gBmMapTerrain[iy - 1][ix] == TERRAIN_FLOOR_17) + connexion += 2; + + if (gBmMapTerrain[iy - 1][ix] == TERRAIN_STAIRS) + connexion += 2; + + if (gBmMapTerrain[iy - 1][ix] == TERRAIN_CHEST_20) + connexion += 2; + + if (gBmMapTerrain[iy - 1][ix] == TERRAIN_CHEST_21) + connexion += 2; + } + + if (ix > 0 && iy > 0) + if ((gBmMapTerrain[iy] [ix - 1] == TERRAIN_FLOOR_17) && + (gBmMapTerrain[iy + 1][ix - 1] == TERRAIN_WATER) && + (gBmMapTerrain[iy - 1][ix] != TERRAIN_FLOOR_17)) + connexion = 4; + + switch (connexion) { + + case 1: // straight shadow on the left + gUnknown_0859A9D4[iy][ix] = 0x2DC; + break; + + case 2: // straight shadow on the top + gUnknown_0859A9D4[iy][ix] = 0x2D8; + break; + + case 3: // shadow on both the left and the top + gUnknown_0859A9D4[iy][ix] = 0x358; + break; + + case 4: // shadow on the left, but stronger at the top-left than bottom-left + gUnknown_0859A9D4[iy][ix] = 0x35C; + break; + + } // switch (connexion) + } + } +} + +void sub_8019778(void) { + LoadChapterMap(gUnknown_02001000, gUnknown_0202BCF0.chapterIndex); + + FlushTilesFromBuffer(); + ApplyTrapMapChanges(); + FlushTerrainData(); + sub_8019624(); +} + +void SetupMapRowPointers(void* buffer, u8*** outHandle, int width, int height) { + int i; + u8* itBuffer; + + gUnknown_03000808 = buffer; + + width += 2; // two tiles on each edge (shared) + height += 4; // two tiles on each edge + + itBuffer = buffer + height * sizeof(u8*); + + for (i = 0; i < height; ++i) { + gUnknown_03000808[i] = itBuffer; + itBuffer += width; + } + + // first row is actually the third, ensuring the top two map rows as safety + *outHandle = gUnknown_03000808 + 2; +} + +void ClearMapWith(u8** map, int value) { + int size = (gBmMapSize.height + 4) * (gBmMapSize.width + 2); + + if (size % 2) + size = size - 1; + + value = (0xFF & value); + value += value << 8; + + CpuFill16(value, map[-2], size); + + SetSubjectMap(map); +}