From 09f5292c0a67675898b30eb4b52bdd82ee44ecda Mon Sep 17 00:00:00 2001 From: nmlgc Date: Sat, 13 Apr 2024 17:06:53 +0200 Subject: [PATCH] [Decompilation] [th03] Player: Position update and clamping Part of P0280, funded by [Anonymous], Blue Bolt, and JonathKane. --- th01/math/clamp.hpp | 8 +++++ th03/main/player/move.cpp | 24 ++++++++++++++- th03/main/player/move.hpp | 2 ++ th03_main.asm | 65 ++++----------------------------------- 4 files changed, 39 insertions(+), 60 deletions(-) diff --git a/th01/math/clamp.hpp b/th01/math/clamp.hpp index 55213c91..9e42d13f 100644 --- a/th01/math/clamp.hpp +++ b/th01/math/clamp.hpp @@ -11,6 +11,14 @@ #define clamp_min_2(v, min) ((v) < min ? min : (v)) #define clamp_max_2_ge(v, max) ((v) >= max ? max : (v)) +#define clamp_minmax(v, min, max) { \ + if(v < min) { \ + v = min; \ + } else if(v > max) { \ + v = max; \ + } \ +} + #define clamp_inc(v, max) \ (v)++; \ if((v) > (max)) { \ diff --git a/th03/main/player/move.cpp b/th03/main/player/move.cpp index 19887c8c..705982f8 100644 --- a/th03/main/player/move.cpp +++ b/th03/main/player/move.cpp @@ -1,13 +1,35 @@ #pragma option -zPmain_01 -G +#include #include "platform.h" #include "pc98.h" -#include "th01/math/subpixel.hpp" +#include "th01/math/clamp.hpp" #include "th03/hardware/input.h" +#include "th03/common.h" #include "th03/playchar.hpp" +#include "th03/main/playfld.hpp" +#include "th03/main/collmap.hpp" +#include "th03/main/sprite16.hpp" #include "th03/main/chars/speed.hpp" +#include "th03/main/player/shot.hpp" +#include "th03/main/player/player.hpp" #include "th03/main/player/move.hpp" +void pascal near player_pos_update_and_clamp(player_t near& player) +{ + subpixel_t x = player.center.x; + subpixel_t y = player.center.y; + + x += player_velocity.x.v; + y += player_velocity.y.v; + + clamp_minmax(x, to_sp( 8), to_sp(PLAYFIELD_W - 8)); + clamp_minmax(y, to_sp(24), to_sp(PLAYFIELD_H - 16)); + + player.center.x.v = x; + player.center.y.v = y; +} + #pragma option -a2 inline void move(Subpixel8& dim, const Subpixel8& speed, int8_t direction) { diff --git a/th03/main/player/move.hpp b/th03/main/player/move.hpp index db3cafe5..169502aa 100644 --- a/th03/main/player/move.hpp +++ b/th03/main/player/move.hpp @@ -7,5 +7,7 @@ enum move_ret_t { extern speed_t player_speed_base; // of [player_cur] extern SPPoint8 player_velocity; // of [player_cur] +void pascal near player_pos_update_and_clamp(player_t near& player); + // Sets [player_velocity] according to [input]. move_ret_t pascal near player_move(input_t input); diff --git a/th03_main.asm b/th03_main.asm index d7f75133..369aa34f 100644 --- a/th03_main.asm +++ b/th03_main.asm @@ -4343,8 +4343,7 @@ loc_C4E8: call @player_move$qui pascal, di cmp al, MOVE_VALID jnz short loc_C505 - push si - call sub_D6D4 + call @player_pos_update_and_clamp$qr8player_t pascal, si loc_C505: call @player_hittest$qi pascal, (8 / 2) @@ -4627,8 +4626,7 @@ loc_C6C7: call @player_move$qui pascal, di cmp al, MOVE_VALID jnz short loc_C6D3 - push si - call sub_D6D4 + call @player_pos_update_and_clamp$qr8player_t pascal, si loc_C6D3: cmp [bp+var_B], 0 @@ -6663,61 +6661,12 @@ sub_D6C0 endp ; --------------------------------------------------------------------------- nop -; =============== S U B R O U T I N E ======================================= - -; Attributes: bp-based frame - -sub_D6D4 proc near - -arg_0 = word ptr 4 - - push bp - mov bp, sp - push si - mov si, [bp+arg_0] - mov dx, [si] - mov cx, [si+2] - mov al, _player_velocity.x8 - cbw - add dx, ax - mov al, _player_velocity.y8 - cbw - add cx, ax - cmp dx, 80h - jge short loc_D6F7 - mov dx, 80h - jmp short loc_D700 -; --------------------------------------------------------------------------- - -loc_D6F7: - cmp dx, 1180h - jle short loc_D700 - mov dx, 1180h - -loc_D700: - cmp cx, 180h - jge short loc_D70B - mov cx, 180h - jmp short loc_D714 -; --------------------------------------------------------------------------- - -loc_D70B: - cmp cx, 1600h - jle short loc_D714 - mov cx, 1600h - -loc_D714: - mov [si], dx - mov [si+2], cx - pop si - pop bp - retn 2 -sub_D6D4 endp - MOVE_INVALID = 0 MOVE_VALID = 1 MOVE_NOINPUT = 2 + @PLAYER_POS_UPDATE_AND_CLAMP$QR8PLAYER_T procdesc pascal near \ + player:word @PLAYER_MOVE$QUI procdesc pascal near \ input:word PLAYER_M_TEXT ends @@ -7143,8 +7092,7 @@ loc_DAC7: loc_DAEF: cmp [bp+var_5], MOVE_VALID jnz short loc_DAF9 - push si - call sub_D6D4 + call @player_pos_update_and_clamp$qr8player_t pascal, si loc_DAF9: cmp [bp+var_6], 0 @@ -7797,8 +7745,7 @@ loc_E036: mov _player_velocity.x8, al mov al, [bp+@@vector_y] mov _player_velocity.y8, al - push si - call sub_D6D4 + call @player_pos_update_and_clamp$qr8player_t pascal, si jmp short loc_E078 ; ---------------------------------------------------------------------------