diff --git a/asm/code_8083654.s b/asm/code_8083654.s index 854bf5259..5ccd444aa 100644 --- a/asm/code_8083654.s +++ b/asm/code_8083654.s @@ -5,257 +5,6 @@ .text - thumb_func_start sub_8083660 -sub_8083660: - push {r4-r7,lr} - mov r7, r10 - mov r6, r9 - mov r5, r8 - push {r5-r7} - ldr r4, _080836D4 - add sp, r4 - movs r1, 0xE0 - lsls r1, 5 - add r1, sp - str r0, [r1] - bl GetLeader - ldr r2, _080836D8 - add r2, sp - str r0, [r2] - movs r7, 0 - movs r0, 0 - ldr r1, _080836DC - add r1, sp - str r0, [r1] -_0808368A: - movs r0, 0x38 - bl DungeonRandInt - adds r4, r0, 0 - movs r0, 0x20 - bl DungeonRandInt - ldr r2, _080836E0 - add r2, sp - str r0, [r2] - ldr r0, _080836D8 - add r0, sp - ldr r0, [r0] - ldr r3, [r0, 0x4] - adds r5, r4, 0 - movs r1, 0 - ldr r4, [r2] - ldr r0, _080836E4 - cmp r7, r0 - ble _080836B4 - b _080837E6 -_080836B4: - movs r2, 0x3 - mov r10, r2 - lsls r0, r7, 2 - mov r2, sp - adds r6, r0, r2 -_080836BE: - movs r0, 0 - mov r8, r0 - adds r2, r5, 0x1 - ldr r0, _080836E8 - add r0, sp - str r2, [r0] - adds r1, 0x1 - ldr r2, _080836EC - add r2, sp - str r1, [r2] - b _0808379C - .align 2, 0 -_080836D4: .4byte 0xffffe3e4 -_080836D8: .4byte 0x00001c04 -_080836DC: .4byte 0x00001c0c -_080836E0: .4byte 0x00001c08 -_080836E4: .4byte 0x000006ff -_080836E8: .4byte 0x00001c14 -_080836EC: .4byte 0x00001c10 -_080836F0: - ldr r0, _08083738 - add r0, sp - ldr r0, [r0] - cmp r0, 0 - bne _0808373C - ldrh r1, [r2] - mov r0, r10 - ands r0, r1 - cmp r0, 0x1 - bne _08083782 - ldrb r0, [r2, 0x9] - cmp r0, 0xFF - beq _08083782 - ldr r0, [r2, 0x14] - cmp r0, 0 - bne _08083782 - ldr r0, [r2, 0x10] - cmp r0, 0 - bne _08083782 - lsls r0, r3, 16 - asrs r0, 16 - subs r0, r5 - cmp r0, 0 - bge _08083722 - negs r0, r0 -_08083722: - cmp r0, 0x5 - bgt _08083788 - asrs r0, r3, 16 - subs r0, r4 - cmp r0, 0 - bge _08083730 - negs r0, r0 -_08083730: - cmp r0, 0x5 - ble _08083782 - b _08083788 - .align 2, 0 -_08083738: .4byte 0x00001c0c -_0808373C: - ldr r1, _08083764 - add r1, sp - ldr r1, [r1] - cmp r1, 0x1 - bne _08083768 - ldrh r1, [r2] - mov r0, r10 - ands r0, r1 - cmp r0, 0x1 - bne _08083782 - ldrb r0, [r2, 0x9] - cmp r0, 0xFF - beq _08083782 - ldr r0, [r2, 0x14] - cmp r0, 0 - bne _08083782 - ldr r0, [r2, 0x10] - cmp r0, 0 - bne _08083782 - b _08083788 - .align 2, 0 -_08083764: .4byte 0x00001c0c -_08083768: - ldrh r1, [r2] - mov r0, r10 - ands r0, r1 - cmp r0, 0x1 - bne _08083782 - ldr r0, [r2, 0x14] - cmp r0, 0 - bne _08083782 - ldr r0, [r2, 0x10] - cmp r0, 0 - bne _08083782 - movs r2, 0x1 - mov r9, r2 -_08083782: - mov r0, r9 - cmp r0, 0 - beq _08083790 -_08083788: - strb r5, [r6] - strb r4, [r6, 0x1] - adds r6, 0x4 - adds r7, 0x1 -_08083790: - adds r4, 0x1 - cmp r4, 0x1F - ble _08083798 - movs r4, 0 -_08083798: - movs r1, 0x1 - add r8, r1 -_0808379C: - mov r2, r8 - cmp r2, 0x1F - bgt _080837C2 - movs r0, 0 - mov r9, r0 - adds r0, r5, 0 - adds r1, r4, 0 - ldr r2, _0808380C - add r2, sp - str r3, [r2] - bl GetTile - adds r2, r0, 0 - ldr r0, _08083810 - ldr r1, _0808380C - add r1, sp - ldr r3, [r1] - cmp r7, r0 - ble _080836F0 -_080837C2: - ldr r2, _08083814 - add r2, sp - ldr r5, [r2] - cmp r5, 0x37 - ble _080837CE - movs r5, 0 -_080837CE: - ldr r0, _08083818 - add r0, sp - ldr r1, [r0] - cmp r1, 0x37 - bgt _080837E6 - ldr r2, _0808381C - add r2, sp - ldr r4, [r2] - ldr r0, _08083810 - cmp r7, r0 - bgt _080837E6 - b _080836BE -_080837E6: - cmp r7, 0 - beq _08083820 - adds r0, r7, 0 - bl DungeonRandInt - lsls r0, 2 - mov r2, sp - adds r1, r2, r0 - ldrb r0, [r1] - movs r2, 0xE0 - lsls r2, 5 - add r2, sp - ldr r2, [r2] - strh r0, [r2] - ldrb r0, [r1, 0x1] - strh r0, [r2, 0x2] - movs r0, 0x1 - b _08083832 - .align 2, 0 -_0808380C: .4byte 0x00001c18 -_08083810: .4byte 0x000006ff -_08083814: .4byte 0x00001c14 -_08083818: .4byte 0x00001c10 -_0808381C: .4byte 0x00001c08 -_08083820: - ldr r0, _08083844 - add r0, sp - ldr r2, [r0] - adds r2, 0x1 - str r2, [r0] - cmp r2, 0x2 - bgt _08083830 - b _0808368A -_08083830: - movs r0, 0 -_08083832: - ldr r3, _08083848 - add sp, r3 - pop {r3-r5} - mov r8, r3 - mov r9, r4 - mov r10, r5 - pop {r4-r7} - pop {r1} - bx r1 - .align 2, 0 -_08083844: .4byte 0x00001c0c -_08083848: .4byte 0x00001c1c - thumb_func_end sub_8083660 - thumb_func_start sub_808384C sub_808384C: push {r4-r7,lr} diff --git a/include/structs/str_dungeon.h b/include/structs/str_dungeon.h index dd12783b3..10e356c7e 100644 --- a/include/structs/str_dungeon.h +++ b/include/structs/str_dungeon.h @@ -12,6 +12,7 @@ #define DUNGEON_MAX_SIZE_X 56 #define DUNGEON_MAX_SIZE_Y 32 +#define DUNGEON_MAX_SIZE_X_MUL_Y (DUNGEON_MAX_SIZE_X * DUNGEON_MAX_SIZE_Y) #define DUNGEON_MAX_WILD_POKEMON 16 #define DUNGEON_MAX_WILD_POKEMON_BODY_SIZE 16 #define DUNGEON_MAX_POKEMON (MAX_TEAM_MEMBERS + DUNGEON_MAX_WILD_POKEMON) diff --git a/include/structs/str_position.h b/include/structs/str_position.h index 42b312f96..599267924 100644 --- a/include/structs/str_position.h +++ b/include/structs/str_position.h @@ -1,6 +1,13 @@ #ifndef GUARD_STR_POSITION_H #define GUARD_STR_POSITION_H +// Used in dungeon generation and sub_8083660 +struct PositionU8 +{ + u8 x; + u8 y; +}; + // size: 0x4 typedef struct DungeonPos { diff --git a/src/code_8066D04.c b/src/code_8066D04.c index b70ae2035..86402d873 100644 --- a/src/code_8066D04.c +++ b/src/code_8066D04.c @@ -11,7 +11,7 @@ #include "code_806CD90.h" #include "code_8077274_1.h" #include "code_807CD9C.h" -#include "code_8084778.h" +#include "dungeon_jobs.h" #include "run_dungeon.h" #include "dungeon_items.h" #include "dungeon_leader.h" diff --git a/src/dungeon_8067A80.c b/src/dungeon_8067A80.c index 3bc816fa5..470fb78bb 100644 --- a/src/dungeon_8067A80.c +++ b/src/dungeon_8067A80.c @@ -11,7 +11,6 @@ #include "code_806CD90.h" #include "code_8077274_1.h" #include "code_807CD9C.h" -#include "code_8084778.h" #include "run_dungeon.h" #include "dungeon_items.h" #include "dungeon_leader.h" diff --git a/src/dungeon_generation.c b/src/dungeon_generation.c index 6ccf6a307..bc7aeb00f 100644 --- a/src/dungeon_generation.c +++ b/src/dungeon_generation.c @@ -4044,12 +4044,6 @@ static void ResetFloor(void) } } -struct PositionU8 -{ - u8 x; - u8 y; -}; - /* * ShuffleSpawnPositions - Randomly shuffle an array of spawn positions */ @@ -4083,7 +4077,7 @@ static void ShuffleSpawnPositions(struct PositionU8 *spawns, s32 count) */ static void SpawnNonEnemies(FloorProperties *floorProps, bool8 isEmptyMonsterHouse) { - struct PositionU8 validSpawns[DUNGEON_MAX_SIZE_X * DUNGEON_MAX_SIZE_Y]; + struct PositionU8 validSpawns[DUNGEON_MAX_SIZE_X_MUL_Y]; s32 count; s32 randIndex; s32 i; @@ -4419,7 +4413,7 @@ static void SpawnNonEnemies(FloorProperties *floorProps, bool8 isEmptyMonsterHou */ static void SpawnEnemies(FloorProperties *floorProps, bool8 isEmptyMonsterHouse) { - struct PositionU8 validSpawns[DUNGEON_MAX_SIZE_X * DUNGEON_MAX_SIZE_Y]; + struct PositionU8 validSpawns[DUNGEON_MAX_SIZE_X_MUL_Y]; s32 count; s32 randIndex; s32 i; diff --git a/src/dungeon_range.c b/src/dungeon_range.c index c08efcd31..d6b554c08 100644 --- a/src/dungeon_range.c +++ b/src/dungeon_range.c @@ -2,6 +2,7 @@ #include "globaldata.h" #include "dungeon_range.h" #include "dungeon_leader.h" +#include "dungeon_random.h" #include "dungeon_util.h" #include "sprite.h" #include "structs/str_dungeon.h" @@ -261,4 +262,89 @@ EntityInfo* GetLeaderInfo(void) return GetEntInfo(GetLeader()); } +bool8 sub_8083660(DungeonPos *posArg) +{ + struct PositionU8 positions[DUNGEON_MAX_SIZE_X_MUL_Y]; + s32 yLoop, xLoop; + Entity *leader = GetLeader(); + s32 randX, randY; + s32 i; + s32 x, y; + s32 count = 0; + + for (i = 0; i < 3; i++) { + DungeonPos leaderPos; + + randX = DungeonRandInt(DUNGEON_MAX_SIZE_X); + randY = DungeonRandInt(DUNGEON_MAX_SIZE_Y); + leaderPos = leader->pos; + x = randX; + + for (xLoop = 0; xLoop < DUNGEON_MAX_SIZE_X; xLoop++) { + y = randY; + if (count >= DUNGEON_MAX_SIZE_X_MUL_Y) + break; + + for (yLoop = 0; yLoop < DUNGEON_MAX_SIZE_Y; yLoop++) { + bool8 setPosition = FALSE; + const Tile *tile = GetTile(x, y); + if (count >= DUNGEON_MAX_SIZE_X_MUL_Y) + break; + if (i == 0) { + if (GetTerrainType(tile) == TERRAIN_TYPE_NORMAL + && tile->room != CORRIDOR_ROOM + && tile->object == NULL + && tile->monster == NULL + && (abs(leaderPos.x - x) >= 6 || abs(leaderPos.y - y) >= 6)) + { + setPosition = TRUE; + } + } + // No abs checks + else if (i == 1) { + if (GetTerrainType(tile) == TERRAIN_TYPE_NORMAL + && tile->room != CORRIDOR_ROOM + && tile->object == NULL + && tile->monster == NULL) + { + setPosition = TRUE; + } + } + // No abs checks and no CORRIDOR_ROOM check + else { + if (GetTerrainType(tile) == TERRAIN_TYPE_NORMAL + && tile->object == NULL + && tile->monster == NULL) + { + setPosition = TRUE; + } + } + + if (setPosition) { + positions[count].x = x; + positions[count].y = y; + count++; + } + + if (++y >= DUNGEON_MAX_SIZE_Y) { + y = 0; + } + } + + if (++x >= DUNGEON_MAX_SIZE_X) { + x = 0; + } + } + + if (count != 0) { + s32 posId = DungeonRandInt(count); + posArg->x = positions[posId].x; + posArg->y = positions[posId].y; + return TRUE; + } + } + + return FALSE; +} + // all of code_8083654.s belongs to this file