Skip to content

Commit

Permalink
[Decompilation] [th01] REIIDEN.EXE: Move already referenced literals …
Browse files Browse the repository at this point in the history
…to C land

Part of P0214, funded by Ember2528.
  • Loading branch information
nmlgc committed Aug 14, 2022
1 parent b60a23e commit d4140ad
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 132 deletions.
22 changes: 12 additions & 10 deletions th01/hardware/text.h
@@ -1,27 +1,29 @@
// Fills the text layer with spaces.
// PORTERS: Implement better, and more consistently.
#define text_fill_space(esc_move_topleft, space, tmp_x, tmp_y) { \
// ZUN bloat: The slowest imaginable version of this operation.
#define text_fill_space(esc_move_topleft, tmp_x, tmp_y) { \
printf(esc_move_topleft); \
for(tmp_y = 0; tmp_y < (RES_Y / GLYPH_H); tmp_y++) { \
for(tmp_x = 0; tmp_x < (RES_X / GLYPH_HALF_W); tmp_x++) { \
printf(space); \
printf(" "); \
} \
} \
}

// Fills the text layer with opaque black.
// MODDERS: This should maybe reset the current text mode color.
#define text_fill_black( \
esc_color_bg_black_fg_black, esc_move_topleft, space, tmp_x, tmp_y \
) { \
printf(esc_color_bg_black_fg_black); \
text_fill_space(esc_move_topleft, space, tmp_x, tmp_y); \
#define text_fill_black(tmp_x, tmp_y) { \
printf("\x1B[16;40m"); \
text_fill_space("\x1B[0;0H", tmp_x, tmp_y); \
}

inline void text_color_reset(void) {
printf("\x1B[0m");
}

// Fills the text layer with transparent spaces. Yes, this overwrites the
// perfectly suitable master.lib function with the same name.
#define text_clear_sloppy(tmp_x, tmp_y) { \
printf("\x1B[0m"); /* Reset text mode color */ \
text_color_reset(); \
/* (yes, this escape sequence is actually 1-based) */ \
text_fill_space("\x1B[1;1H", " ", tmp_x, tmp_y); \
text_fill_space("\x1B[1;1H", tmp_x, tmp_y); \
}
4 changes: 4 additions & 0 deletions th01/hardware/ztext.hpp
Expand Up @@ -37,4 +37,8 @@ void z_text_print(const char *cmd);
void z_text_vputsa(
tram_x_t x, tram_y_t y, int z_atrb, const sshiftjis_t *fmt, ...
);

inline void z_text_clear_inlined() {
printf("\x1B*");
}
// ------------------------------------------
2 changes: 1 addition & 1 deletion th01/main/boss/b20j.cpp
Expand Up @@ -1788,7 +1788,7 @@ void konngara_main(void)
// --------------------------------------------------------------------

printf("\x1B)3"); // Enter graph mode
text_fill_black("\x1B[16;40m", "\x1B[0;0H", " ", j, i);
text_fill_black(j, i);

grp_put_palette_show(SCROLL_BG_FN);
z_palette_set_black(j, i);
Expand Down
6 changes: 2 additions & 4 deletions th01/main/player/bomb.cpp
Expand Up @@ -22,10 +22,8 @@ static const screen_y_t SQUARE_CENTER_Y = ((RES_Y / 2) - (32 / 2));

void bomb_kuji_load(void)
{
extern const char kuzi1_grc[];
extern const char kuzi2_grc[];
grc_load(GRC_SLOT_BOMB_KUJI_1, kuzi1_grc);
grc_load(GRC_SLOT_BOMB_KUJI_2, kuzi2_grc);
grc_load(GRC_SLOT_BOMB_KUJI_1, "kuzi1.grc");
grc_load(GRC_SLOT_BOMB_KUJI_2, "kuzi2.grc");
}

bool16 bomb_update_and_render(int frame)
Expand Down
74 changes: 7 additions & 67 deletions th01/main/player/orb.cpp
Expand Up @@ -2,61 +2,19 @@

extern double orb_velocity_y;

extern const double ORB_VELOCITY_Y_MIN;
extern const float ORB_VELOCITY_Y_MAX;
extern const double ORB_COEFFICIENT_OF_RESTITUTION;
#define COEFFICIENT_OF_RESTITUTION 0.78

inline double gravity_for(const double& force) {
return ((orb_force_frame / 5) + orb_force);
}
/// Temporary data segment workarounds
/// ----------------------------------

// Also needs to be spelled out in ASM to avoid the unwanted WAIT instruction
// afterwards.
#define GRAVITY_FOR(force) \
_AX = orb_force_frame / 5; \
asm { mov [bp-2], ax; } \
asm { fild word ptr [bp-2]; } \
asm { fadd force; } \
/// ----------------------------------

pixel_t orb_velocity_y_update(void)
{
/* TODO: Proper decompilation, once data can be emitted here:
* ----------------------------------------------------------
orb_velocity_y = gravity_for(orb_force);
if(orb_velocity_y > ORB_VELOCITY_Y_MAX) {
orb_velocity_y = ORB_VELOCITY_Y_MAX;
} else if(orb_velocity_y < ORB_VELOCITY_Y_MIN) {
orb_velocity_y = ORB_VELOCITY_Y_MIN;
}
return gravity_for(orb_force);
* ----------------------------------------------------------
* Performing arithmetic or comparisons between a double (orb_velocity_y)
* and a float (ORB_VELOCITY_Y_MAX) variable always FLDs the float first,
* before emitting the corresponding FPU instruction with the double,
* which is not what we want here.
*/
GRAVITY_FOR(orb_force);
asm {
fstp orb_velocity_y;
fld orb_velocity_y;
fcomp ORB_VELOCITY_Y_MAX;
fstsw [bp-2];
FWAIT_EMU;
mov ax, [bp-2];
sahf;
jbe min_velocity_check;
fld ORB_VELOCITY_Y_MAX;
}
goto set_velocity;
min_velocity_check:
if(orb_velocity_y < ORB_VELOCITY_Y_MIN) _asm {
fld ORB_VELOCITY_Y_MIN;
set_velocity:
fstp orb_velocity_y;
FWAIT_EMU;
if(orb_velocity_y > 16.0f) {
orb_velocity_y = 16.0f;
} else if(orb_velocity_y < -16.0) {
orb_velocity_y = -16.0;
}
return gravity_for(orb_force);
}
Expand All @@ -70,11 +28,8 @@ pixel_t orb_velocity_y_update(void)

void orb_force_new(double immediate, orb_force_t force)
{
extern const float ORB_FORCE_2_0;
extern const double ORB_FORCE_SHOT_BASE;

if(force == OF_BOUNCE_FROM_SURFACE) {
orb_force = (-orb_velocity_y * ORB_COEFFICIENT_OF_RESTITUTION);
orb_force = (-orb_velocity_y * COEFFICIENT_OF_RESTITUTION);
if(orb_velocity_x == OVX_0) {
random_velocity_change(0, OVX_4_LEFT);
random_velocity_change(1, OVX_4_RIGHT);
Expand All @@ -84,22 +39,7 @@ void orb_force_new(double immediate, orb_force_t force)
orb_force = ((-orb_velocity_y) - (orb_force_frame / 4));
}
if(force == OF_SHOT) {
/* TODO: Proper decompilation, once data can be emitted here:
* ----------------------------------------------------------
orb_force = ((orb_velocity_y / ORB_FORCE_2_0) + ORB_FORCE_SHOT_BASE);
* ----------------------------------------------------------
* Performing arithmetic or comparisons between a double
* (orb_velocity_y) and a float (ORB_FORCE_2_0) variable always FLDs
* the float first, before emitting the corresponding FPU instruction
* with the double, which is not what we want here.
*/
_asm {
fld orb_velocity_y;
fdiv ORB_FORCE_2_0;
fadd ORB_FORCE_SHOT_BASE;
fstp orb_force;
FWAIT_EMU;
}
orb_force = (-10.0 + (orb_velocity_y / 2.0));
}
if(force == OF_IMMEDIATE) {
orb_force = immediate;
Expand Down
36 changes: 12 additions & 24 deletions th01/main_010.cpp
Expand Up @@ -20,6 +20,9 @@
#include "th01/hardware/palette.h"
#include "th01/hardware/text.h"
#include "th01/hardware/tram_x16.hpp"
}
#include "th01/hardware/ztext.hpp"
extern "C" {
#include "th01/snd/mdrv2.h"
#include "th01/formats/grp.h"
#include "th01/formats/pf.hpp"
Expand All @@ -41,7 +44,7 @@
#include "th01/shiftjis/entrance.hpp"
#include "th01/shiftjis/fns.hpp"

extern const char esc_cls[];
int8_t temporary_padding = 0;

inline void bomb_doubletap_update(uint8_t& pressed, uint8_t& other) {
if(bomb_doubletap_frames < BOMB_DOUBLETAP_WINDOW) {
Expand Down Expand Up @@ -217,26 +220,19 @@ void pascal stage_num_animate(unsigned int stage_num)
tram_cursor.putkanji_until_end(' ', TX_BLACK);

frame_delay(35);
printf(esc_cls);
z_text_clear_inlined();
}

void load_and_init_stuff_used_in_all_stages(void)
{
extern const char mask_grf[];
extern const char miko_ac_bos[];
extern const char miko_ac2_bos[];
#undef PTN_STG_CARDFLIP_FN
extern const char PTN_STG_CARDFLIP_FN[];
extern const char miko_ptn[];

int i;

scoredat_load_hiscore();
hud_bg_load(mask_grf);
player_48x48.load(miko_ac_bos);
player_48x32.load(miko_ac2_bos);
hud_bg_load("mask.grf");
player_48x48.load("miko_ac.bos");
player_48x32.load("miko_ac2.bos");
ptn_load(PTN_SLOT_STG, PTN_STG_CARDFLIP_FN);
ptn_load(PTN_SLOT_MIKO, miko_ptn);
ptn_load(PTN_SLOT_MIKO, "miko.ptn");
ptn_new(PTN_SLOT_BG_HUD, ((PTN_BG_last - PTN_BG_first) + 1));
bomb_kuji_load();
shootout_lasers_init(i);
Expand All @@ -249,18 +245,10 @@ void stage_entrance(int stage_id, const char* bg_fn, bool16 clear_vram_page_0)
int y;

if(first_stage_in_scene == true) {
extern const char esc_color_bg_black_fg_black[];
extern const char esc_cursor_to_x0_y0[];
extern const char space[];
extern const char esc_color_reset[];
extern const char empty_grf[];

text_fill_black(
esc_color_bg_black_fg_black, esc_cursor_to_x0_y0, space, x, y
);
printf(esc_color_reset);
text_fill_black(x, y);
text_color_reset();

if(strcmp(bg_fn, empty_grf)) {
if(strcmp(bg_fn, "empty.grf")) {
grp_put_palette_show(bg_fn);
}
/* TODO: Replace with the decompiled call
Expand Down
29 changes: 3 additions & 26 deletions th01_reiiden.asm
Expand Up @@ -3023,32 +3023,6 @@ main_38_TEXT ends

.data

db 0

public _esc_cls
_esc_cls db 1Bh,'*',0
public _mask_grf, _miko_ac_bos, _miko_ac2_bos, _PTN_STG_CARDFLIP_FN, _miko_ptn
_mask_grf db 'mask.grf',0
_miko_ac_bos db 'miko_ac.bos',0
_miko_ac2_bos db 'miko_ac2.bos',0
_PTN_STG_CARDFLIP_FN db 'stg.ptn',0
_miko_ptn db 'miko.ptn',0
public _esc_color_bg_black_fg_black, _esc_cursor_to_x0_y0, _space
public _esc_color_reset, _empty_grf, _kuzi1_grc, _kuzi2_grc
_esc_color_bg_black_fg_black db 1Bh,'[16;40m',0
_esc_cursor_to_x0_y0 db 1Bh,'[0;0H',0
_space db ' ',0
_esc_color_reset db 1Bh,'[0m',0
_empty_grf db 'empty.grf',0
_kuzi1_grc db 'kuzi1.grc',0
_kuzi2_grc db 'kuzi2.grc',0
public _ORB_VELOCITY_Y_MAX, _ORB_VELOCITY_Y_MIN
public _ORB_COEFFICIENT_OF_RESTITUTION, _ORB_FORCE_2_0, _ORB_FORCE_SHOT_BASE
_ORB_VELOCITY_Y_MAX dd 16.0
_ORB_VELOCITY_Y_MIN dq -16.0
_ORB_COEFFICIENT_OF_RESTITUTION dq 0.78
_ORB_FORCE_2_0 dd 2.0
_ORB_FORCE_SHOT_BASE dq -10.0
aVovVtvrvd db 'PAUSE',0
aB@nKjb@b@pic db ' 再開  終了',0
aBB@b@b@b@b@b@ db '●      ',0
Expand Down Expand Up @@ -3189,6 +3163,9 @@ OVX_8_RIGHT = 4
OF_BOUNCE_FROM_SURFACE = 0
OF_BOUNCE_FROM_TOP = 1

_esc_cls = 0159h
_PTN_STG_CARDFLIP_FN = 017Eh
_ORB_COEFFICIENT_OF_RESTITUTION = qword ptr ds:[01D0h]
extern _rank:byte
extern _bgm_mode:byte
extern _bombs:byte
Expand Down

0 comments on commit d4140ad

Please sign in to comment.