diff --git a/asm/include/overlay_29_022E9298.inc b/asm/include/overlay_29_022E9298.inc index e4b2bebd..50cf22d5 100644 --- a/asm/include/overlay_29_022E9298.inc +++ b/asm/include/overlay_29_022E9298.inc @@ -1,14 +1,4 @@ #pragma once .public DUNGEON_PTR -.public EntityIsValid__022E95F4 .public GetTile .public GetVisibilityRange -.public LEADER_PTR -.public _020AFC4C -.public abs -.public ov29_0237C888 -.public ov29_0237C88A -.public ov29_0237C88E -.public ov29_0237C890 -.public ov29_0237C892 -.public sub_0201B9F8 diff --git a/asm/include/overlay_29_022E9488.inc b/asm/include/overlay_29_022E9488.inc new file mode 100644 index 00000000..a1b5af35 --- /dev/null +++ b/asm/include/overlay_29_022E9488.inc @@ -0,0 +1,11 @@ +#pragma once +.public DUNGEON_PTR +.public EntityIsValid__022E95F4 +.public LEADER_PTR +.public _020AFC4C +.public ov29_0237C888 +.public ov29_0237C88A +.public ov29_0237C88E +.public ov29_0237C890 +.public ov29_0237C892 +.public sub_0201B9F8 diff --git a/asm/overlay_29_022E9298.s b/asm/overlay_29_022E9298.s index eedc55fa..ae084564 100644 --- a/asm/overlay_29_022E9298.s +++ b/asm/overlay_29_022E9298.s @@ -62,200 +62,3 @@ _022E930C: .align 2, 0 _022E9358: .word DUNGEON_PTR arm_func_end ov29_022E9298 - - arm_func_start IsPositionWithinTwoTiles -IsPositionWithinTwoTiles: ; 0x022E935C - stmdb sp!, {r4, r5, r6, r7, r8, lr} - mov r8, r0 - mov r7, r1 - ldrsh r4, [r8] - ldrsh r0, [r7] - sub r0, r4, r0 - bl abs - mov r6, r0 - cmp r6, #1 - bgt _022E93A0 - ldrsh r1, [r8, #2] - ldrsh r0, [r7, #2] - sub r0, r1, r0 - bl abs - cmp r0, #1 - movle r0, #1 - ldmleia sp!, {r4, r5, r6, r7, r8, pc} -_022E93A0: - ldrsh r5, [r8, #2] - ldrsh r0, [r7, #2] - sub r0, r5, r0 - bl abs - cmp r6, r0 - movle r6, r0 - cmp r6, #2 - bne _022E9480 - mov r6, #0 - b _022E9410 -_022E93C8: - ldrsh r0, [r7] - cmp r4, r0 - addlt r4, r4, #1 - cmp r4, r0 - ldrsh r0, [r7, #2] - subgt r4, r4, #1 - cmp r5, r0 - addlt r5, r5, #1 - cmp r5, r0 - subgt r5, r5, #1 - mov r0, r4 - mov r1, r5 - bl GetTile - ldrh r0, [r0] - tst r0, #3 - moveq r0, #0 - ldmeqia sp!, {r4, r5, r6, r7, r8, pc} - add r6, r6, #1 -_022E9410: - cmp r6, #2 - blt _022E93C8 - ldrsh r4, [r7] - ldrsh r5, [r7, #2] - mov r6, #0 - b _022E9470 -_022E9428: - ldrsh r0, [r8] - cmp r4, r0 - addlt r4, r4, #1 - cmp r4, r0 - ldrsh r0, [r8, #2] - subgt r4, r4, #1 - cmp r5, r0 - addlt r5, r5, #1 - cmp r5, r0 - subgt r5, r5, #1 - mov r0, r4 - mov r1, r5 - bl GetTile - ldrh r0, [r0] - tst r0, #3 - moveq r0, #0 - ldmeqia sp!, {r4, r5, r6, r7, r8, pc} - add r6, r6, #1 -_022E9470: - cmp r6, #2 - blt _022E9428 - mov r0, #1 - ldmia sp!, {r4, r5, r6, r7, r8, pc} -_022E9480: - mov r0, #0 - ldmia sp!, {r4, r5, r6, r7, r8, pc} - arm_func_end IsPositionWithinTwoTiles - - arm_func_start ov29_022E9488 -ov29_022E9488: ; 0x022E9488 - stmdb sp!, {r3, r4, r5, lr} - ldr ip, _022E955C ; =DUNGEON_PTR - ldr r3, _022E9560 ; =0x0001A224 - ldr r4, [ip] - add ip, r3, #2 - ldrsh r3, [r4, r3] - ldrsh r4, [r4, ip] - mvn lr, #0xf - sub r3, r0, r3 - sub r0, r1, r4 - cmp r3, lr - cmpge r0, lr - blt _022E9554 - rsb r1, lr, #0xff - cmp r3, r1 - cmplt r0, #0xd0 - bge _022E9554 - mov r1, #0xc - mul ip, r2, r1 - ldr r4, _022E9564 ; =ov29_0237C88A - ldr r5, _022E9568 ; =ov29_0237C890 - ldrh r2, [r4, ip] - sub r1, r1, #0x20c - ldrsh r5, [r5, ip] - and r1, r2, r1 - strh r1, [r4, ip] - add r1, r3, r5 - ldrh r2, [r4, ip] - and r1, r1, lr, lsr #23 - ldr r3, _022E956C ; =ov29_0237C88E - orr r1, r2, r1 - strh r1, [r4, ip] - ldr r1, _022E9570 ; =ov29_0237C892 - ldrh r4, [r3, ip] - ldrsh lr, [r1, ip] - ldr r1, _022E9574 ; =0xFFFF000F - ldr r2, _022E9578 ; =_020AFC4C - and r1, r4, r1 - strh r1, [r3, ip] - add r0, r0, lr - ldr r1, _022E957C ; =ov29_0237C888 - mov lr, r0, lsl #0x14 - ldrh r4, [r3, ip] - ldr r0, [r2] - add r1, r1, ip - orr r4, r4, lr, lsr #16 - mov r2, #0 - strh r4, [r3, ip] - bl sub_0201B9F8 - mov r0, #1 - ldmia sp!, {r3, r4, r5, pc} -_022E9554: - mov r0, #0 - ldmia sp!, {r3, r4, r5, pc} - .align 2, 0 -_022E955C: .word DUNGEON_PTR -#ifdef JAPAN -_022E9560: .word 0x0001A180 -#else -_022E9560: .word 0x0001A224 -#endif -_022E9564: .word ov29_0237C88A -_022E9568: .word ov29_0237C890 -_022E956C: .word ov29_0237C88E -_022E9570: .word ov29_0237C892 -_022E9574: .word 0xFFFF000F -_022E9578: .word _020AFC4C -_022E957C: .word ov29_0237C888 - arm_func_end ov29_022E9488 - - arm_func_start GetLeader -GetLeader: ; 0x022E9580 - stmdb sp!, {r4, r5, r6, lr} - ldr r0, _022E95EC ; =LEADER_PTR - ldr r0, [r0] - cmp r0, #0 - ldmneia sp!, {r4, r5, r6, pc} - mov r5, #0 - ldr r4, _022E95F0 ; =DUNGEON_PTR - b _022E95DC -_022E95A0: - ldr r0, [r4] - add r0, r0, r5, lsl #2 - add r0, r0, #0x12000 -#ifdef JAPAN - ldr r6, [r0, #0xa84] -#else - ldr r6, [r0, #0xb28] -#endif - mov r0, r6 - bl EntityIsValid__022E95F4 - cmp r0, #0 - ldrne r0, [r6, #0xb4] - ldrneb r0, [r0, #7] - cmpne r0, #0 - ldrne r1, _022E95EC ; =LEADER_PTR - movne r0, r6 - strne r6, [r1] - ldmneia sp!, {r4, r5, r6, pc} - add r5, r5, #1 -_022E95DC: - cmp r5, #4 - blt _022E95A0 - mov r0, #0 - ldmia sp!, {r4, r5, r6, pc} - .align 2, 0 -_022E95EC: .word LEADER_PTR -_022E95F0: .word DUNGEON_PTR - arm_func_end GetLeader diff --git a/asm/overlay_29_022E9488.s b/asm/overlay_29_022E9488.s new file mode 100644 index 00000000..764af856 --- /dev/null +++ b/asm/overlay_29_022E9488.s @@ -0,0 +1,116 @@ + .include "asm/macros.inc" + .include "overlay_29_022E9488.inc" + + .text + + arm_func_start ov29_022E9488 +ov29_022E9488: ; 0x022E9488 + stmdb sp!, {r3, r4, r5, lr} + ldr ip, _022E955C ; =DUNGEON_PTR + ldr r3, _022E9560 ; =0x0001A224 + ldr r4, [ip] + add ip, r3, #2 + ldrsh r3, [r4, r3] + ldrsh r4, [r4, ip] + mvn lr, #0xf + sub r3, r0, r3 + sub r0, r1, r4 + cmp r3, lr + cmpge r0, lr + blt _022E9554 + rsb r1, lr, #0xff + cmp r3, r1 + cmplt r0, #0xd0 + bge _022E9554 + mov r1, #0xc + mul ip, r2, r1 + ldr r4, _022E9564 ; =ov29_0237C88A + ldr r5, _022E9568 ; =ov29_0237C890 + ldrh r2, [r4, ip] + sub r1, r1, #0x20c + ldrsh r5, [r5, ip] + and r1, r2, r1 + strh r1, [r4, ip] + add r1, r3, r5 + ldrh r2, [r4, ip] + and r1, r1, lr, lsr #23 + ldr r3, _022E956C ; =ov29_0237C88E + orr r1, r2, r1 + strh r1, [r4, ip] + ldr r1, _022E9570 ; =ov29_0237C892 + ldrh r4, [r3, ip] + ldrsh lr, [r1, ip] + ldr r1, _022E9574 ; =0xFFFF000F + ldr r2, _022E9578 ; =_020AFC4C + and r1, r4, r1 + strh r1, [r3, ip] + add r0, r0, lr + ldr r1, _022E957C ; =ov29_0237C888 + mov lr, r0, lsl #0x14 + ldrh r4, [r3, ip] + ldr r0, [r2] + add r1, r1, ip + orr r4, r4, lr, lsr #16 + mov r2, #0 + strh r4, [r3, ip] + bl sub_0201B9F8 + mov r0, #1 + ldmia sp!, {r3, r4, r5, pc} +_022E9554: + mov r0, #0 + ldmia sp!, {r3, r4, r5, pc} + .align 2, 0 +_022E955C: .word DUNGEON_PTR +#ifdef JAPAN +_022E9560: .word 0x0001A180 +#else +_022E9560: .word 0x0001A224 +#endif +_022E9564: .word ov29_0237C88A +_022E9568: .word ov29_0237C890 +_022E956C: .word ov29_0237C88E +_022E9570: .word ov29_0237C892 +_022E9574: .word 0xFFFF000F +_022E9578: .word _020AFC4C +_022E957C: .word ov29_0237C888 + arm_func_end ov29_022E9488 + + arm_func_start GetLeader +GetLeader: ; 0x022E9580 + stmdb sp!, {r4, r5, r6, lr} + ldr r0, _022E95EC ; =LEADER_PTR + ldr r0, [r0] + cmp r0, #0 + ldmneia sp!, {r4, r5, r6, pc} + mov r5, #0 + ldr r4, _022E95F0 ; =DUNGEON_PTR + b _022E95DC +_022E95A0: + ldr r0, [r4] + add r0, r0, r5, lsl #2 + add r0, r0, #0x12000 +#ifdef JAPAN + ldr r6, [r0, #0xa84] +#else + ldr r6, [r0, #0xb28] +#endif + mov r0, r6 + bl EntityIsValid__022E95F4 + cmp r0, #0 + ldrne r0, [r6, #0xb4] + ldrneb r0, [r0, #7] + cmpne r0, #0 + ldrne r1, _022E95EC ; =LEADER_PTR + movne r0, r6 + strne r6, [r1] + ldmneia sp!, {r4, r5, r6, pc} + add r5, r5, #1 +_022E95DC: + cmp r5, #4 + blt _022E95A0 + mov r0, #0 + ldmia sp!, {r4, r5, r6, pc} + .align 2, 0 +_022E95EC: .word LEADER_PTR +_022E95F0: .word DUNGEON_PTR + arm_func_end GetLeader diff --git a/include/dungeon_range_2.h b/include/dungeon_range_2.h new file mode 100644 index 00000000..32a134fc --- /dev/null +++ b/include/dungeon_range_2.h @@ -0,0 +1,9 @@ +#ifndef PMDSKY_DUNGEON_RANGE_2_H +#define PMDSKY_DUNGEON_RANGE_2_H + +#include "dungeon_mode.h" + +// Checks if a given target position is within two tiles of a given origin position. +bool8 IsPositionWithinTwoTiles(struct position *origin, struct position *target); + +#endif //PMDSKY_DUNGEON_RANGE_2_H diff --git a/main.lsf b/main.lsf index 3f009519..88edac50 100644 --- a/main.lsf +++ b/main.lsf @@ -388,6 +388,8 @@ Overlay OVY_29 Object asm/overlay_29_rodata_02351628.o Object src/dungeon_range.o Object asm/overlay_29_022E9298.o + Object src/dungeon_range_2.o + Object asm/overlay_29_022E9488.o Object src/dg_uty.o Object asm/overlay_29_022E9618.o Object src/overlay_29_022EA9EC.o diff --git a/src/dungeon_ai_movement.c b/src/dungeon_ai_movement.c index f7a5137d..d5f879e4 100644 --- a/src/dungeon_ai_movement.c +++ b/src/dungeon_ai_movement.c @@ -11,6 +11,7 @@ #include "dungeon_logic_8.h" #include "dungeon_map_access.h" #include "dungeon_pokemon_attributes_1.h" +#include "dungeon_range_2.h" #include "dungeon_util.h" #include "dungeon_util_1.h" #include "dungeon_util_static.h" @@ -48,7 +49,6 @@ struct can_move_in_direction_info const s32 FACING_DIRECTION_INCREMENTS[] = {0, 1, -1, 2, -2, 3, -3, 4}; extern bool8 CanTargetPosition(struct entity *monster, struct position *position); -extern bool8 IsPositionWithinTwoTiles(struct position *origin, struct position *target); extern s32 ov29_022FBE04(struct monster*); extern bool8 ShouldAvoidFirstHit(struct entity *monster, bool8 force_avoid); extern bool8 CanSeeTeammate(struct entity *monster); diff --git a/src/dungeon_range.c b/src/dungeon_range.c index c2b4dbfc..17499f7f 100644 --- a/src/dungeon_range.c +++ b/src/dungeon_range.c @@ -2,10 +2,9 @@ #include "dg_camera.h" #include "dungeon.h" #include "dungeon_map_access.h" +#include "dungeon_range_2.h" #include "main_0208655C.h" -extern bool8 IsPositionWithinTwoTiles(struct position *origin, struct position *target); - bool8 IsPositionActuallyInSight(struct position *origin, struct position *target, bool8 user_has_dropeye) { u8 origin_room; diff --git a/src/dungeon_range_2.c b/src/dungeon_range_2.c new file mode 100644 index 00000000..89fb256c --- /dev/null +++ b/src/dungeon_range_2.c @@ -0,0 +1,69 @@ +#include "dungeon_range_2.h" +#include "dungeon.h" +#include "dungeon_map_access.h" +#include "main_0208655C.h" + +bool8 IsPositionWithinTwoTiles(struct position *origin, struct position *target) +{ + s32 i; + s32 origin_x = origin->x; + s32 origin_y; + s32 diff = abs(origin_x - target->x); + if (diff <= 1) + { + s32 y_diff = abs(origin->y - target->y); + if (y_diff <= 1) + return TRUE; + } + + origin_y = origin->y; + s32 y_diff = abs(origin_y - target->y); + if (diff <= y_diff) + diff = y_diff; + + if (diff == 2) + { + for (i = 0; i < 2; i++) + { + if (origin_x < target->x) + origin_x++; + + if (origin_x > target->x) + origin_x--; + + if (origin_y < target->y) + origin_y++; + + if (origin_y > target->y) + origin_y--; + + const struct tile *tile = GetTile(origin_x, origin_y); + if (!(tile->terrain_flags & (TERRAIN_TYPE_NORMAL | TERRAIN_TYPE_SECONDARY))) + return FALSE; + } + + s32 target_x = target->x; + s32 target_y = target->y; + + for (i = 0; i < 2; i++) + { + if (target_x < origin->x) + target_x++; + + if (target_x > origin->x) + target_x--; + + if (target_y < origin->y) + target_y++; + + if (target_y > origin->y) + target_y--; + + const struct tile *tile = GetTile(target_x, target_y); + if (!(tile->terrain_flags & (TERRAIN_TYPE_NORMAL | TERRAIN_TYPE_SECONDARY))) + return FALSE; + } + return TRUE; + } + return FALSE; +}