From 6cdd2296bb1913e3e41e62b48f66d7a195847f7e Mon Sep 17 00:00:00 2001 From: nmlgc Date: Sat, 21 Sep 2019 11:04:39 +0200 Subject: [PATCH] =?UTF-8?q?[Reverse-engineering]=20[th04/th05]=20Input=20?= =?UTF-8?q?=E2=86=92=20player=20movement?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Completes P0033, funded by zorg. --- Makefile.mak | 3 +- th04/hardware/input.inc | 11 +++- th04/player/move.asm | 117 +++++++++++++++++++++++++++++++++ th04/player/pos[bss].asm | 2 + th04/th04.inc | 3 + th04_main.asm | 123 +++-------------------------------- th05/playchar_speed[bss].asm | 2 +- th05/playermv.asm | 120 ++++++++++++++++++++++++++++++++++ th05_main.asm | 107 ++---------------------------- 9 files changed, 267 insertions(+), 221 deletions(-) create mode 100644 th04/player/move.asm create mode 100644 th04/player/pos[bss].asm create mode 100644 th05/playermv.asm diff --git a/Makefile.mak b/Makefile.mak index 0961c229..b752b5b8 100644 --- a/Makefile.mak +++ b/Makefile.mak @@ -92,9 +92,10 @@ bin\th04\main.exe: bin\th04\main.obj bin\th04\scoreupd.obj $** | +bin\th05\playermv.obj: th05\playermv.asm bin\th05\hud_bar.obj: th05\hud_bar.asm bin\th05\scoreupd.obj: th04\scoreupd.asm -bin\th05\main.exe: bin\th05\main.obj bin\th05\hud_bar.obj bin\th05\scoreupd.obj th05\main012.cpp +bin\th05\main.exe: bin\th05\main.obj bin\th05\playermv.obj bin\th05\hud_bar.obj bin\th05\scoreupd.obj th05\main012.cpp $(CC) $(CFLAGS) -ml -3 -Z -DGAME=5 -nbin\th05\ -eMAIN.EXE @&&| $** | diff --git a/th04/hardware/input.inc b/th04/hardware/input.inc index 753cfc42..e3ea9bef 100644 --- a/th04/hardware/input.inc +++ b/th04/hardware/input.inc @@ -14,8 +14,15 @@ INPUT_CANCEL = 1000h INPUT_OK = 2000h INPUT_Q = 4000h -INPUT_MOVEMENT = INPUT_UP or INPUT_DOWN or INPUT_LEFT or INPUT_RIGHT +INPUT_MOVEMENT = \ + INPUT_UP or INPUT_DOWN or INPUT_LEFT or INPUT_RIGHT or \ + INPUT_UP_LEFT or INPUT_UP_RIGHT or INPUT_DOWN_LEFT or INPUT_DOWN_RIGHT if GAME eq 5 -INPUT_REPLAY_END = 0F0F0h +INPUT_REPLAY_END = not INPUT_MOVEMENT endif + +; move_ret_t +MOVE_INVALID = 0 +MOVE_VALID = 1 +MOVE_NOINPUT = 2 diff --git a/th04/player/move.asm b/th04/player/move.asm new file mode 100644 index 00000000..e514618b --- /dev/null +++ b/th04/player/move.asm @@ -0,0 +1,117 @@ +; move_ret_t pascal near player_move(int input); +public PLAYER_MOVE +player_move proc near +@@input_local = word ptr -2 +@@input = word ptr 4 + + enter 2, 0 + mov dl, MOVE_VALID + mov ax, [bp+@@input] + mov [bp+@@input_local], ax + mov cx, @@num_cases + mov bx, offset @@switch_table + +@@case_lookup: + mov ax, cs:[bx] + cmp ax, [bp+@@input_local] + jz short @@switch + add bx, 2 + loop @@case_lookup + jmp short @@default +; --------------------------------------------------------------------------- + +@@switch: + jmp word ptr cs:[bx+@@case_to_target] ; switch jump + +@@standing_still: + mov dl, MOVE_NOINPUT + jmp short @@ret +; --------------------------------------------------------------------------- + +@@left: + mov player_pos.velocity.x, -playchar_speed_aligned + jmp short @@ret +; --------------------------------------------------------------------------- + +@@down_left: + mov player_pos.velocity.x, -playchar_speed_diagonal + mov player_pos.velocity.y, playchar_speed_diagonal + jmp short @@ret +; --------------------------------------------------------------------------- + +@@down: + mov player_pos.velocity.y, playchar_speed_aligned + jmp short @@ret +; --------------------------------------------------------------------------- + +@@down_right: + mov player_pos.velocity.x, playchar_speed_diagonal + mov player_pos.velocity.y, playchar_speed_diagonal + jmp short @@ret +; --------------------------------------------------------------------------- + +@@right: + mov player_pos.velocity.x, playchar_speed_aligned + jmp short @@ret +; --------------------------------------------------------------------------- + +@@up_right: + mov player_pos.velocity.x, playchar_speed_diagonal + jmp short @@up_left_y +; --------------------------------------------------------------------------- + +@@up: + mov player_pos.velocity.y, -playchar_speed_aligned + jmp short @@ret +; --------------------------------------------------------------------------- + +@@up_left: + mov player_pos.velocity.x, -playchar_speed_diagonal + +@@up_left_y: + mov player_pos.velocity.y, -playchar_speed_diagonal + jmp short @@ret +; --------------------------------------------------------------------------- + +@@default: + mov dl, MOVE_INVALID + +@@ret: + mov al, dl + leave + retn 2 + +; --------------------------------------------------------------------------- + db 0 +@@switch_table label word + ; value table for switch statement + dw INPUT_NONE + dw INPUT_UP + dw INPUT_DOWN + dw INPUT_LEFT + dw INPUT_UP or INPUT_LEFT + dw INPUT_DOWN or INPUT_LEFT + dw INPUT_RIGHT + dw INPUT_UP or INPUT_RIGHT + dw INPUT_DOWN or INPUT_RIGHT + dw INPUT_UP_LEFT + dw INPUT_UP_RIGHT + dw INPUT_DOWN_LEFT + dw INPUT_DOWN_RIGHT +@@case_to_target = $ - @@switch_table +@@num_cases = @@case_to_target shr 1 + ; jump table for switch statement + dw offset @@standing_still + dw offset @@up + dw offset @@down + dw offset @@left + dw offset @@up_left + dw offset @@down_left + dw offset @@right + dw offset @@up_right + dw offset @@down_right + dw offset @@up_left + dw offset @@up_right + dw offset @@down_left + dw offset @@down_right +player_move endp diff --git a/th04/player/pos[bss].asm b/th04/player/pos[bss].asm new file mode 100644 index 00000000..ebd787f4 --- /dev/null +++ b/th04/player/pos[bss].asm @@ -0,0 +1,2 @@ +public PLAYER_POS +player_pos motion_t diff --git a/th04/th04.inc b/th04/th04.inc index 921935d4..e80a4f04 100644 --- a/th04/th04.inc +++ b/th04/th04.inc @@ -1,2 +1,5 @@ GAME = 4 include th04/shared.inc + +playchar_speed_aligned = 4 shl 4 +playchar_speed_diagonal = 3 shl 4 diff --git a/th04_main.asm b/th04_main.asm index 98773718..5c70315d 100644 --- a/th04_main.asm +++ b/th04_main.asm @@ -11118,113 +11118,7 @@ loc_10894: retn sub_107E2 endp - -; =============== S U B R O U T I N E ======================================= - -; Attributes: bp-based frame - -sub_10898 proc near - -var_2 = word ptr -2 -arg_0 = word ptr 4 - - enter 2, 0 - mov dl, 1 - mov ax, [bp+arg_0] - mov [bp+var_2], ax - mov cx, 0Dh ; switch 13 cases - mov bx, offset table_10898 - -loc_108AA: - mov ax, cs:[bx] - cmp ax, [bp+var_2] - jz short loc_108B9 - add bx, 2 - loop loc_108AA - jmp short loc_10913 ; default -; --------------------------------------------------------------------------- - -loc_108B9: - jmp word ptr cs:[bx+1Ah] ; switch jump - -loc_108BD: - mov dl, 2 ; jumptable 000108B9 case 0 - jmp short loc_10915 -; --------------------------------------------------------------------------- - -loc_108C1: - mov player_pos.velocity.x, -64 ; jumptable 000108B9 case 4 - jmp short loc_10915 -; --------------------------------------------------------------------------- - -loc_108C9: - mov player_pos.velocity.x, -48 ; jumptable 000108B9 cases 6,1024 - mov player_pos.velocity.y, 48 - jmp short loc_10915 -; --------------------------------------------------------------------------- - -loc_108D7: - mov player_pos.velocity.y, 64 ; jumptable 000108B9 case 2 - jmp short loc_10915 -; --------------------------------------------------------------------------- - -loc_108DF: - mov player_pos.velocity.x, 48 ; jumptable 000108B9 cases 10,2048 - mov player_pos.velocity.y, 48 - jmp short loc_10915 -; --------------------------------------------------------------------------- - -loc_108ED: - mov player_pos.velocity.x, 64 ; jumptable 000108B9 case 8 - jmp short loc_10915 -; --------------------------------------------------------------------------- - -loc_108F5: - mov player_pos.velocity.x, 48 ; jumptable 000108B9 cases 9,512 - jmp short loc_1090B -; --------------------------------------------------------------------------- - -loc_108FD: - mov player_pos.velocity.y, -64 ; jumptable 000108B9 case 1 - jmp short loc_10915 -; --------------------------------------------------------------------------- - -loc_10905: - mov player_pos.velocity.x, -48 ; jumptable 000108B9 cases 5,256 - -loc_1090B: - mov player_pos.velocity.y, -48 - jmp short loc_10915 -; --------------------------------------------------------------------------- - -loc_10913: - mov dl, 0 ; default - -loc_10915: - mov al, dl - leave - retn 2 -sub_10898 endp - -; --------------------------------------------------------------------------- - db 0 -table_10898 dw 0, 1, 2, 4 ; value table for switch statement - dw 5, 6, 8, 9 - dw 0Ah, 100h, 200h, 400h - dw 800h - dw offset loc_108BD ; jump table for switch statement - dw offset loc_108FD - dw offset loc_108D7 - dw offset loc_108C1 - dw offset loc_10905 - dw offset loc_108C9 - dw offset loc_108ED - dw offset loc_108F5 - dw offset loc_108DF - dw offset loc_10905 - dw offset loc_108F5 - dw offset loc_108C9 - dw offset loc_108DF +include th04/player/move.asm ; =============== S U B R O U T I N E ======================================= @@ -11384,8 +11278,8 @@ sub_10988 endp sub_10ABF proc near -var_2 = byte ptr -2 -var_1 = byte ptr -1 +@@move_ret = byte ptr -2 +var_1 = byte ptr -1 enter 2, 0 push si @@ -11421,15 +11315,14 @@ loc_10B11: mov player_pos.velocity.x, 0 mov player_pos.velocity.y, 0 mov ax, _input - and ax, 0F0Fh + and ax, INPUT_MOVEMENT mov si, ax mov [bp+var_1], 1 loc_10B32: - push si - call main_01:sub_10898 - mov [bp+var_2], al - cmp [bp+var_2], 0 + call main_01:player_move pascal, si + mov [bp+@@move_ret], al + cmp [bp+@@move_ret], 0 jnz short loc_10B58 cmp [bp+var_1], 0 jz short loc_10B58 @@ -41529,7 +41422,7 @@ public _stage_vm _stage_vm dd ? word_2598A dw ? word_2598C dw ? -player_pos motion_t +include th04/player/pos[bss].asm word_2599A dw ? word_2599C dw ? word_2599E dw ? diff --git a/th05/playchar_speed[bss].asm b/th05/playchar_speed[bss].asm index d4c80772..f6db39ba 100644 --- a/th05/playchar_speed[bss].asm +++ b/th05/playchar_speed[bss].asm @@ -1,6 +1,6 @@ ; All values are signed. (Yes, allowing you to invert the controls with ; negative values!) -public _playchar_speed_aligned, _playchar_speed_diagonal +public _PLAYCHAR_SPEED_ALIGNED, _PLAYCHAR_SPEED_DIAGONAL _playchar_speed_aligned dw ? dw ? _playchar_speed_diagonal dw ? diff --git a/th05/playermv.asm b/th05/playermv.asm new file mode 100644 index 00000000..98dbc457 --- /dev/null +++ b/th05/playermv.asm @@ -0,0 +1,120 @@ + .186 + locals + +include libs/master.lib/macros.inc +include th04/math/motion.inc +include th04/hardware/input.inc + +extrn player_pos:motion_t +extrn _playchar_speed_aligned:word +extrn _playchar_speed_diagonal:word + +; ---------------------------------------------------------------------------- + +main_01_TEXT segment word public 'CODE' use16 + assume cs:main_01_TEXT + +; move_ret_t pascal near player_move(int input); +public PLAYER_MOVE +player_move proc near +@@input = word ptr ss:[bx+2] + +@@diagonal_x equ ax +@@diagonal_y equ cx +@@aligned equ dx + + mov bx, sp + mov bx, @@input + cmp bl, INPUT_RIGHT or INPUT_DOWN + ja short @@invalid + mov @@diagonal_y, _playchar_speed_diagonal + mov @@diagonal_x, @@diagonal_y + mov @@aligned, _playchar_speed_aligned + and bh, low INPUT_MOVEMENT + jz short @@switch + or bl, bl + jnz short @@invalid + shr bx, 8 + cmp bl, 8 + ja short @@invalid + add bl, 11 + +@@switch: + add bx, bx + jmp word ptr cs:@@switch_table[bx] + +@@up: + neg @@aligned + +@@down: + mov player_pos.velocity.y, @@aligned + jmp short @@moved +; --------------------------------------------------------------------------- + +@@left: + neg @@aligned + +@@right: + mov player_pos.velocity.x, @@aligned + jmp short @@moved +; --------------------------------------------------------------------------- + +@@up_right: + neg @@diagonal_y + jmp short @@down_right +; --------------------------------------------------------------------------- + +@@up_left: + neg @@diagonal_y + +@@down_left: + neg @@diagonal_x + +@@down_right: + mov player_pos.velocity.x, @@diagonal_x + mov player_pos.velocity.y, @@diagonal_y + jmp short @@moved +; --------------------------------------------------------------------------- + +@@standing_still: + mov al, MOVE_NOINPUT + jmp short @@ret +; --------------------------------------------------------------------------- + +@@invalid: + xor al, al + jmp short @@ret +; --------------------------------------------------------------------------- + +@@moved: + mov al, MOVE_VALID + +@@ret: + retn 2 + +; --------------------------------------------------------------------------- +@@switch_table label word + dw @@standing_still + dw @@up + dw @@down + dw @@invalid + dw @@left + dw @@up_left + dw @@down_left + dw @@invalid + dw @@right + dw @@up_right + dw @@down_right + dw @@invalid + dw @@up_left + dw @@up_right + dw @@invalid + dw @@down_left + dw @@invalid + dw @@invalid + dw @@invalid + dw @@down_right +player_move endp +main_01_TEXT ends + + end diff --git a/th05_main.asm b/th05_main.asm index 47ac9c0c..7c12d521 100644 --- a/th05_main.asm +++ b/th05_main.asm @@ -12973,13 +12973,12 @@ loc_12188: mov player_pos.velocity.x, 0 mov player_pos.velocity.y, 0 mov ax, _input - and ax, 0F0Fh + and ax, INPUT_MOVEMENT mov si, ax mov [bp+var_1], 1 loc_121A9: - push si - call sub_142F8 + call player_move pascal, si or al, al jnz short loc_121CA cmp [bp+var_1], 0 @@ -17557,106 +17556,10 @@ loc_142D4: locret_142F6: retn sub_14266 endp - -; --------------------------------------------------------------------------- nop -; =============== S U B R O U T I N E ======================================= - - -sub_142F8 proc near - mov bx, sp - mov bx, ss:[bx+2] - cmp bl, 0Ah - ja short loc_1434D - mov cx, _playchar_speed_diagonal - mov ax, cx - mov dx, _playchar_speed_aligned - and bh, 0Fh - jz short loc_14321 - or bl, bl - jnz short loc_1434D - shr bx, 8 - cmp bl, 8 - ja short loc_1434D - add bl, 0Bh - -loc_14321: - add bx, bx - jmp word ptr cs:table_14356[bx] - -loc_14328: - neg dx - -loc_1432A: - mov player_pos.velocity.y, dx - jmp short loc_14351 -; --------------------------------------------------------------------------- - -loc_14330: - neg dx - -loc_14332: - mov player_pos.velocity.x, dx - jmp short loc_14351 -; --------------------------------------------------------------------------- - -loc_14338: - neg cx - jmp short loc_14340 -; --------------------------------------------------------------------------- - -loc_1433C: - neg cx - -loc_1433E: - neg ax - -loc_14340: - mov player_pos.velocity.x, ax - mov player_pos.velocity.y, cx - jmp short loc_14351 -; --------------------------------------------------------------------------- - -loc_14349: - mov al, 2 - jmp short locret_14353 -; --------------------------------------------------------------------------- - -loc_1434D: - xor al, al - jmp short locret_14353 -; --------------------------------------------------------------------------- - -loc_14351: - mov al, 1 - -locret_14353: - retn 2 -sub_142F8 endp - -; --------------------------------------------------------------------------- -table_14356 dw loc_14349 - dw loc_14328 - dw loc_1432A - dw loc_1434D - dw loc_14330 - dw loc_1433C - dw loc_1433E - dw loc_1434D - dw loc_14332 - dw loc_14338 - dw loc_14340 - dw loc_1434D - dw loc_1433C - dw loc_14338 - dw loc_1434D - dw loc_1433E - dw loc_1434D - dw loc_1434D - dw loc_1434D - dw loc_14340 - + PLAYER_MOVE procdesc pascal near \ + input:word HUD_BAR_PUT procdesc near HUD_SCORE_PUT procdesc near SCORE_UPDATE_AND_RENDER procdesc near @@ -45141,7 +45044,7 @@ _stage_title dd ? _stage_bgm_title dd ? _boss_bgm_title dd ? word_2CE9E dw ? -player_pos motion_t +include th04/player/pos[bss].asm include th05/playchar_speed[bss].asm dword_2CEB4 dd ? dword_2CEB8 dd ?