Skip to content

Commit

Permalink
[Decompilation] [th05] Bosses: Smooth random movement
Browse files Browse the repository at this point in the history
Part of P0109, funded by [Anonymous] and Blue Bolt.
  • Loading branch information
nmlgc committed Aug 16, 2020
1 parent 023417a commit 18a8ecc
Show file tree
Hide file tree
Showing 13 changed files with 175 additions and 209 deletions.
3 changes: 2 additions & 1 deletion th02/math/randring.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ extern uint8_t randring[RANDRING_SIZE];
uint16_t pascal near randring1_next16(void);
uint8_t pascal near randring2_next8(void);
uint8_t pascal near randring2_next8_and(uint8_t mask);
uint16_t pascal near randring2_next16(void);
#endif

uint16_t pascal near randring2_next16(void);
4 changes: 4 additions & 0 deletions th03/math/randring.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#include "th02/math/randring.h"

uint16_t pascal near randring1_next16(void);
uint16_t pascal near randring1_next16_and(uint16_t mask);
uint16_t pascal near randring2_next16_and(uint16_t mask);
uint16_t pascal near randring2_next16_mod(uint16_t mask);

inline char randring_angle(char random_range, char offset)
{
Expand Down
3 changes: 3 additions & 0 deletions th04/main/boss/boss.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// Bosses
// ------
static const int BOSS_W = 64;
static const int BOSS_H = 64;

typedef struct {
motion_t pos;
int hp;
Expand Down
4 changes: 3 additions & 1 deletion th04/math/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
// (different calling convention than the TH03 one)
int pascal vector1_at(int origin, int length, int angle);

int pascal near vector2_near(Point near &ret, unsigned char angle, int length);
int pascal near vector2_near(
SPPoint near &ret, unsigned char angle, int length
);

int pascal vector2_at(
Point near &ret, int origin_x, int origin_y, int length, int angle
Expand Down
2 changes: 2 additions & 0 deletions th04/shared.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include "th04/score.h"
/// ------

#include "th04/main/playfld.h"

/// Stages
/// ------
extern nearfunc_t_near stage_invalidate;
Expand Down
2 changes: 2 additions & 0 deletions th04/shared.inc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ STAGE_START_INVINCIBILITY_FRAMES = 64

; Bosses
; ------
BOSS_W = 64
BOSS_H = 64
BOSS_DEFEAT_INVINCIBILITY_FRAMES = 255
; ------

Expand Down
83 changes: 83 additions & 0 deletions th05/main/boss/boss.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,89 @@ extern boss_stuff_t boss2;
/// --------
/// Call these on subsequent frames for a smooth flying movement.

typedef enum {
Y_ANY,
Y_UP,
Y_DOWN,
} y_direction_t;

extern area_t<Subpixel> boss_flystep_random_clamp;
static const subpixel_t BOSS_FLYSTEP_RANDOM_FIELD_LEFT = TO_SP(
(PLAYFIELD_W / 2) - (PLAYFIELD_W / 8)
);
static const subpixel_t BOSS_FLYSTEP_RANDOM_FIELD_RIGHT = TO_SP(
(PLAYFIELD_W / 2) + (PLAYFIELD_W / 8)
);

static const int BOSS_FLYSTEP_RANDOM_FRAMES = 28;

// Decides a random [boss.angle] on [frame] 0, based on the current position
// within the playfield and clamping areas declared above. Then, steps the
// [boss] into this direction with a hardcoded, [frame]-dependent speed for
// all [frame]s between 0 and [BOSS_FLYSTEP_RANDOM_FRAMES], after which the
// function returns true.
bool pascal near boss_flystep_random(int frame);

#define flystep_random_for( \
boss, \
next_y_direction, \
speed, \
frames, \
clamp_left, \
clamp_right, \
clamp_top, \
clamp_bottom, \
sprite_left, \
sprite_right, \
sprite_still, \
frame \
) \
if(frame == 0) { \
if(boss.pos.cur.x.v < BOSS_FLYSTEP_RANDOM_FIELD_LEFT) { \
boss.angle = (randring2_next16_mod(0x60) - 0x30); \
} else if(boss.pos.cur.x.v > BOSS_FLYSTEP_RANDOM_FIELD_RIGHT) { \
boss.angle = (randring2_next16_and(0x60) + 0x30); \
} else { \
boss.angle = randring2_next16(); \
} \
if( \
(next_y_direction == Y_UP ) && (boss.angle < 0x80) || \
(next_y_direction == Y_DOWN) && (boss.angle >= 0x80) \
) { \
boss.angle = -boss.angle; \
} \
next_y_direction = Y_ANY; \
} \
if(frame >= 0) { \
vector2_near( \
boss.pos.velocity, boss.angle, (to_sp(speed) - (frame * 2)) \
); \
boss.pos.cur.x.v += boss.pos.velocity.x.v; \
boss.pos.cur.y.v += boss.pos.velocity.y.v; \
if(boss.pos.velocity.x.v < 0) { \
boss.sprite = sprite_left; \
} else { \
boss.sprite = sprite_right; \
} \
if(boss.pos.cur.y.v < clamp_top) { \
boss.pos.cur.y.v = clamp_top; \
next_y_direction = Y_DOWN; \
} else if(boss.pos.cur.y.v > clamp_bottom) { \
boss.pos.cur.y.v = clamp_bottom; \
next_y_direction = Y_UP; \
} \
if(boss.pos.cur.x.v < clamp_left) { \
boss.pos.cur.x.v = clamp_left; \
} else if(boss.pos.cur.x.v > clamp_right) { \
boss.pos.cur.x.v = clamp_right; \
} \
if(frame >= frames) { \
boss.sprite = sprite_still; \
return true; \
} \
} \
return false;

// Steps the [boss] from its current position towards the target point, moving
// it by a hardcoded fraction of the distance. Returns true once the [boss]
// has reached the target point.
Expand Down
20 changes: 20 additions & 0 deletions th05/main/boss/move.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
extern y_direction_t boss_flystep_random_next_y_direction;

bool pascal near boss_flystep_random(int frame)
{
flystep_random_for(
boss,
boss_flystep_random_next_y_direction,
4.0f,
BOSS_FLYSTEP_RANDOM_FRAMES,
boss_flystep_random_clamp.left.v,
boss_flystep_random_clamp.right.v,
boss_flystep_random_clamp.top.v,
boss_flystep_random_clamp.bottom.v,
boss_sprite_left,
boss_sprite_right,
boss_sprite_stay,
frame
);
}

static const int TOWARDS_FRACTION = 16; // higher = slower

inline int towards(int val, int speed = 1) {
Expand Down
8 changes: 8 additions & 0 deletions th05/main/boss/move[data].asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
public _boss_flystep_random_clamp
_boss_flystep_random_clamp label area_t
dw ((BOSS_W / 2) shl 4)
dw ((PLAYFIELD_W - (BOSS_W / 2)) shl 4)
dw (24 shl 4)
dw (128 shl 4)

dw ?
4 changes: 4 additions & 0 deletions th05/main/boss/sprites[bss].asm
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ public _boss_sprite_left, _boss_sprite_right, _boss_sprite_stay
_boss_sprite_left dw ?
_boss_sprite_right dw ?
_boss_sprite_stay dw ?

public _boss_flystep_random_next_y_direc
_boss_flystep_random_next_y_direc db ?
evendata
1 change: 1 addition & 0 deletions th05/main032.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@

extern "C" {
#include "th05/th05.hpp"
#include "th04/math/vector.hpp"
#include "th05/main/boss/move.cpp"
}
1 change: 1 addition & 0 deletions th05/th05.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

#include "ReC98.h"
#include "th01/math/area.hpp"
#include "th04/shared.hpp"
#include "th05/resident.hpp"

Expand Down
Loading

0 comments on commit 18a8ecc

Please sign in to comment.