diff --git a/include/game.h b/include/game.h index 9bc727ef..4ffa403f 100644 --- a/include/game.h +++ b/include/game.h @@ -7,6 +7,7 @@ #include "gamealloc.h" #include "libu64/pad.h" #include "unk.h" +#include "m_controller.h" struct GameState; struct GraphicsContext; @@ -55,7 +56,9 @@ typedef struct GameState { /* 0x9C */ UNK_TYPE1 unk_9C[0x3]; /* 0x9F */ u8 running; /* 0xA0 */ s32 unk_A0; -} GameState; // size >= 0xA4, size <= 0xE0 + /* 0xA4 */ UNK_TYPE1 unk_A4[0x4]; + /* 0xA8 */ Controller controller; +} GameState; // size = 0xE0 void func_800D2E00_jp(GameState* gameState); void func_800D2E58_jp(u16 button, Gfx** gfxP); diff --git a/include/m_controller.h b/include/m_controller.h index fc5a6f87..5a8eaf87 100644 --- a/include/m_controller.h +++ b/include/m_controller.h @@ -6,17 +6,42 @@ #define CHECK_BTN_ALL(state, combo) (~((state) | ~(combo)) == 0) #define RESET_BUTTON 0x80 +#define STICK_MIN 9.899495f +#define STICK_MAX 61.0f +#define STICK_UNCORRECTED_SCALE (1.0f / STICK_MAX) +#define STICK_CORRECTED_SCALE (1.0f / (STICK_MAX - STICK_MIN)) + +typedef struct Controller { + /* 0x00 */ f32 moveX; + /* 0x04 */ f32 moveY; + /* 0x08 */ f32 moveR; + /* 0x0C */ s16 moveAngle; + + /* 0x10 */ f32 lastMoveX; + /* 0x14 */ f32 lastMoveY; + /* 0x18 */ f32 lastMoveR; + /* 0x1C */ s16 lastMoveAngle; + + /* 0x20 */ f32 adjustedX; + /* 0x24 */ f32 adjustedY; + /* 0x28 */ f32 adjustedR; + + /* 0x2C */ f32 lastAdjustedX; + /* 0x30 */ f32 lastAdjustedY; + /* 0x34 */ f32 lastAdjustedR; +} Controller; // size = 0x38 + struct GameState; void mCon_ct(struct GameState* gameState); void mCon_dt(struct GameState* gameState); -// void mCon_calc(); -// void mCon_main(); -// void chkButton(); -// void getButton(); -// void chkTrigger(); -// void getTrigger(); -// void getJoystick_X(); -// void getJoystick_Y(); +void mCon_calc(Controller* controller, f32 x, f32 y); +void mCon_main(struct GameState* game); +s32 chkButton(u16 mask); +u16 getButton(void); +s32 chkTrigger(u16 mask); +u16 getTrigger(void); +s32 getJoystick_X(void); +s32 getJoystick_Y(void); #endif diff --git a/include/6A0DE0.h b/include/m_event.h similarity index 98% rename from include/6A0DE0.h rename to include/m_event.h index 479f6001..03a59a1a 100644 --- a/include/6A0DE0.h +++ b/include/m_event.h @@ -1,5 +1,5 @@ -#ifndef C_6A0DE0_H -#define C_6A0DE0_H +#ifndef M_EVENT_H +#define M_EVENT_H #include "ultra64.h" @@ -39,7 +39,7 @@ // void func_8007D848_jp(); // void func_8007D880_jp(); // void func_8007D8D4_jp(); -// void mEv_CheckTitleDemo(); +s32 mEv_CheckTitleDemo(void); // void func_8007D91C_jp(); // void func_8007D930_jp(); // void func_8007D9C8_jp(); diff --git a/include/m_lib.h b/include/m_lib.h index 582c4cb3..4cc2b2f5 100644 --- a/include/m_lib.h +++ b/include/m_lib.h @@ -61,7 +61,7 @@ void *Lib_SegmentedToVirtual(void *ptr); //original name unknown, taken from maj // void func_8009ADE0_jp(); // void func_8009AE28_jp(); // void func_8009AE44_jp(); -// void check_percent_abs(); +f32 check_percent_abs(f32 x, f32 min, f32 max, f32 scale, int shift_by_min); // void get_percent_forAccelBrake(); // void Game_play_Projection_Trans(); // void get_percent(); diff --git a/include/sys_math_atan.h b/include/sys_math_atan.h index 59e1bd13..1a4cfa81 100644 --- a/include/sys_math_atan.h +++ b/include/sys_math_atan.h @@ -4,7 +4,7 @@ #include "ultra64.h" // void U_GetAtanTable(); -// void atans_table(); +s16 atans_table(f32 x, f32 y); // void atanf_table(); #endif diff --git a/src/code/m_controller.c b/src/code/m_controller.c new file mode 100644 index 00000000..1c108f5c --- /dev/null +++ b/src/code/m_controller.c @@ -0,0 +1,111 @@ +#include "m_controller.h" +#include "game.h" +#include "m_event.h" +#include "m_lib.h" +#include "sys_math_atan.h" +#include "attributes.h" + +void mCon_ct(UNUSED GameState* game) { +} + +void mCon_dt(UNUSED GameState* game) { +} + +void mCon_calc(Controller* controller, f32 x, f32 y) { + controller->lastMoveX = controller->moveX; + controller->lastMoveY = controller->moveY; + controller->lastMoveR = controller->moveR; + + controller->lastMoveAngle = controller->moveAngle; + + controller->lastAdjustedX = controller->adjustedX; + controller->lastAdjustedY = controller->adjustedY; + controller->lastAdjustedR = controller->adjustedR; + + { + f32 r = sqrtf((x * x) + (y * y)); + + if (r <= STICK_MIN) { + controller->moveX = 0.0f; + controller->moveY = 0.0f; + controller->moveR = 0.0f; + controller->adjustedX = 0.0f; + controller->adjustedY = 0.0f; + controller->adjustedR = 0.0f; + } else { + s16 theta = atans_table(x, y); + + if (r > STICK_MAX) { + x = cos_s(theta) * STICK_MAX; + y = sin_s(theta) * STICK_MAX; + r = STICK_MAX; + } + + controller->moveAngle = theta; + { UNUSED s32 requiredScopeTemp; } + controller->moveX = check_percent_abs(x, STICK_MIN, STICK_MAX, STICK_UNCORRECTED_SCALE, 0); + controller->moveY = check_percent_abs(y, STICK_MIN, STICK_MAX, STICK_UNCORRECTED_SCALE, 0); + controller->moveR = check_percent_abs(r, STICK_MIN, STICK_MAX, STICK_UNCORRECTED_SCALE, 0); + + controller->adjustedX = check_percent_abs(x, STICK_MIN, STICK_MAX, STICK_CORRECTED_SCALE, 1); + controller->adjustedY = check_percent_abs(y, STICK_MIN, STICK_MAX, STICK_CORRECTED_SCALE, 1); + controller->adjustedR = check_percent_abs(r, STICK_MIN, STICK_MAX, STICK_CORRECTED_SCALE, 1); + } + } +} + +void mCon_main(GameState* game) { + Controller* controller = &game->controller; + f32 x = getJoystick_X(); + f32 y = getJoystick_Y(); + + mCon_calc(controller, x, y); +} + +s32 chkButton(u16 mask) { + if (mEv_CheckTitleDemo() > 0) { + return 0; + } else { + return mask == (CONTROLLER1(gamePT)->cur.button & mask); + } +} + +u16 getButton(void) { + if (mEv_CheckTitleDemo() > 0) { + return 0; + } else { + return CONTROLLER1(gamePT)->cur.button; + } +} + +s32 chkTrigger(u16 mask) { + if (mEv_CheckTitleDemo() > 0) { + return 0; + } else { + return mask == (CONTROLLER1(gamePT)->press.button & mask); + } +} + +u16 getTrigger(void) { + if (mEv_CheckTitleDemo() > 0) { + return 0; + } else { + return CONTROLLER1(gamePT)->press.button; + } +} + +s32 getJoystick_X(void) { + if (mEv_CheckTitleDemo() > 0) { + return 0; + } else { + return CONTROLLER1(gamePT)->cur.stick_x; + } +} + +s32 getJoystick_Y(void) { + if (mEv_CheckTitleDemo() > 0) { + return 0; + } else { + return CONTROLLER1(gamePT)->cur.stick_y; + } +} diff --git a/src/overlays/gamestates/ovl__00743CD0/_00743CD0.h b/src/overlays/gamestates/ovl__00743CD0/_00743CD0.h index d91180bc..f4d7127d 100644 --- a/src/overlays/gamestates/ovl__00743CD0/_00743CD0.h +++ b/src/overlays/gamestates/ovl__00743CD0/_00743CD0.h @@ -7,7 +7,7 @@ typedef struct _00743CD0State { /* 0x00 */ GameState state; - /* 0xA4 */ UNK_TYPE1 unk_00[0x1B20-0xA4]; + /* 0xE0 */ UNK_TYPE1 unk_00[0x1B20-0xE0]; } _00743CD0State; // size = 0x1B20 void _00743CD0_init(GameState *thisx); diff --git a/src/overlays/gamestates/ovl_famicom_emu/famicom_emu.h b/src/overlays/gamestates/ovl_famicom_emu/famicom_emu.h index 60d71a07..13bda300 100644 --- a/src/overlays/gamestates/ovl_famicom_emu/famicom_emu.h +++ b/src/overlays/gamestates/ovl_famicom_emu/famicom_emu.h @@ -7,7 +7,6 @@ typedef struct FamicomEmuState { /* 0x00 */ GameState state; - /* 0xA4 */ UNK_TYPE1 unk_00[0x3C]; } FamicomEmuState; // size = 0xE0 void famicom_emu_init(GameState *thisx); diff --git a/src/overlays/gamestates/ovl_first_game/first_game.h b/src/overlays/gamestates/ovl_first_game/first_game.h index 35c7228e..f258e786 100644 --- a/src/overlays/gamestates/ovl_first_game/first_game.h +++ b/src/overlays/gamestates/ovl_first_game/first_game.h @@ -7,7 +7,6 @@ typedef struct FirstGameState { /* 0x00 */ GameState state; - /* 0xA4 */ UNK_TYPE1 unk_00[0x3C]; } FirstGameState; // size = 0xE0 void first_game_init(GameState *thisx); diff --git a/src/overlays/gamestates/ovl_play/m_play.h b/src/overlays/gamestates/ovl_play/m_play.h index d8324113..9cc4c118 100644 --- a/src/overlays/gamestates/ovl_play/m_play.h +++ b/src/overlays/gamestates/ovl_play/m_play.h @@ -7,7 +7,7 @@ typedef struct PlayState { /* 0x00 */ GameState state; - /* 0xA4 */ UNK_TYPE1 unk_00[0x236C]; + /* 0xE0 */ UNK_TYPE1 unk_00[0x2410 - 0xE0]; } PlayState; // size = 0x2410 void play_init(GameState *thisx); diff --git a/src/overlays/gamestates/ovl_player_select/player_select.h b/src/overlays/gamestates/ovl_player_select/player_select.h index 0ae08b37..dc5f3ff9 100644 --- a/src/overlays/gamestates/ovl_player_select/player_select.h +++ b/src/overlays/gamestates/ovl_player_select/player_select.h @@ -7,7 +7,7 @@ typedef struct PlayerSelectState { /* 0x00 */ GameState state; - /* 0xA4 */ UNK_TYPE1 unk_00[0x288-0xA4]; + /* 0xE0 */ UNK_TYPE1 unk_00[0x288-0xE0]; } PlayerSelectState; // size = 0x288 void player_select_init(GameState *thisx); diff --git a/src/overlays/gamestates/ovl_prenmi/m_prenmi.h b/src/overlays/gamestates/ovl_prenmi/m_prenmi.h index a55cfc41..4f7622ec 100644 --- a/src/overlays/gamestates/ovl_prenmi/m_prenmi.h +++ b/src/overlays/gamestates/ovl_prenmi/m_prenmi.h @@ -7,7 +7,7 @@ typedef struct PrenmiState { /* 0x00 */ GameState state; - /* 0xA4 */ UNK_TYPE1 unk_00[0xE8-0xA4]; + /* 0xE0 */ UNK_TYPE1 unk00[0xE8 - 0xE0]; } PrenmiState; // size = 0xE8 void prenmi_init(GameState *thisx); diff --git a/src/overlays/gamestates/ovl_save_menu/save_menu.h b/src/overlays/gamestates/ovl_save_menu/save_menu.h index 57ef8f91..92b4d677 100644 --- a/src/overlays/gamestates/ovl_save_menu/save_menu.h +++ b/src/overlays/gamestates/ovl_save_menu/save_menu.h @@ -7,7 +7,7 @@ typedef struct SaveMenuState { /* 0x00 */ GameState state; - /* 0xA4 */ UNK_TYPE1 unk_00[0x230-0xA4]; + /* 0xE0 */ UNK_TYPE1 unk_00[0x230-0xE0]; } SaveMenuState; // size = 0x230 void save_menu_init(GameState *thisx); diff --git a/src/overlays/gamestates/ovl_second_game/second_game.h b/src/overlays/gamestates/ovl_second_game/second_game.h index b3f76895..50478ff7 100644 --- a/src/overlays/gamestates/ovl_second_game/second_game.h +++ b/src/overlays/gamestates/ovl_second_game/second_game.h @@ -7,7 +7,6 @@ typedef struct SecondGameState { /* 0x00 */ GameState state; - /* 0xA4 */ UNK_TYPE1 unk_00[0xE0-0xA4]; } SecondGameState; // size = 0xE0 void second_game_init(GameState *thisx); diff --git a/src/overlays/gamestates/ovl_select/m_select.h b/src/overlays/gamestates/ovl_select/m_select.h index 94dc5347..1f4c7136 100644 --- a/src/overlays/gamestates/ovl_select/m_select.h +++ b/src/overlays/gamestates/ovl_select/m_select.h @@ -7,7 +7,7 @@ typedef struct MapSelectState { /* 0x00 */ GameState state; - /* 0xA4 */ UNK_TYPE1 unk_00[0x240-0xA4]; + /* 0xE0 */ UNK_TYPE1 unk_00[0x240-0xE0]; } MapSelectState; // size = 0x240 void select_init(GameState *thisx); diff --git a/src/overlays/gamestates/ovl_trademark/m_trademark.h b/src/overlays/gamestates/ovl_trademark/m_trademark.h index 38abf24d..708c98a0 100644 --- a/src/overlays/gamestates/ovl_trademark/m_trademark.h +++ b/src/overlays/gamestates/ovl_trademark/m_trademark.h @@ -7,7 +7,7 @@ typedef struct TrademarkState { /* 0x00 */ GameState state; - /* 0xA4 */ UNK_TYPE1 unk_00[0x25A78-0xA4]; + /* 0xE0 */ UNK_TYPE1 unk_00[0x25A78-0xE0]; } TrademarkState; // size = 0x25A78 void trademark_init(GameState *thisx); diff --git a/yamls/jp/code.yaml b/yamls/jp/code.yaml index 28a6da11..40d7d585 100644 --- a/yamls/jp/code.yaml +++ b/yamls/jp/code.yaml @@ -21,7 +21,7 @@ - [0x68B0D0, asm, code/m_collision_bg] - [0x69A7E0, asm, code/69A7E0] - [0x69C6B0, c, code/m_common_data] - - [0x69C770, asm, code/m_controller] + - [0x69C770, c, code/m_controller] - [0x69CB30, asm, code/69CB30] - [0x69DD60, c, code/m_debug] - [0x69DDF0, asm, code/m_debug_display] @@ -159,7 +159,7 @@ - [0x73A080, rodata, code/73A080] - [0x73A0F0, rodata, code/73A0F0] - [0x73A480, rodata, code/73A480] - - [0x73A490, rodata, code/73A490] + - [0x73A490, .rodata, code/m_controller] - [0x73A4A0, rodata, code/73A4A0] - [0x73A4D0, rodata, code/73A4D0] - [0x73A6A0, rodata, code/73A6A0]