Skip to content

Commit

Permalink
Use a lookup table for y position rather than a random function in asm
Browse files Browse the repository at this point in the history
  • Loading branch information
wtjones committed Sep 18, 2018
1 parent 5a373b3 commit 395c31d
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 32 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

## [0.0.3] - 2018-09-17

### Changed

- Use a lookup table for y position rather than a random function in asm

## [0.0.2] - 2018-07-03

### Added
Expand Down
69 changes: 44 additions & 25 deletions src/droplet.asm
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
INCLUDE "gbhw.inc"

MAX_DROPLETS EQU 10
MAX_DROPLETS EQU 20
INITIAL_DROPLETS EQU 20
INITIAL_SPAWN_SPRITE_Y EQU 8
IDLE_SPRITE_Y EQU 0

Expand All @@ -25,29 +26,53 @@ init_droplets::
call mem_Set

xor a
ld [total_droplets],a
ld [total_droplets], a
ld [spawn_delay], a
ret

; spawn max
jr .skip
.loop
call spawn_droplet
.skip
ld a, [total_droplets]
cp MAX_DROPLETS
jr nz, .loop

spawn_droplet::
; has the spawn delay reached zero?
ld a, [spawn_delay]
cp 0
jr z, .skip_return
dec a
ld [spawn_delay], a
call init_droplets_y
ret
.skip_return
; reset the spawn delay with a random range


init_droplets_y:
ld hl, droplets

ld a, [total_droplets]
ld c, a
inc c
jp .skip

.loop
push hl
push bc
call fast_random
call get_random_sprite_y
pop bc
pop hl

ld [hl+], a
inc hl
inc hl
inc hl
.skip
dec c
jp nz, .loop
ret

and %00000011
add 16
ld [spawn_delay], a

spawn_droplet::
ld a, [total_droplets]
cp MAX_DROPLETS
jr nz, .skip_return
ret
.skip_return
; advance to the next idle droplet
ld hl, droplets

Expand All @@ -61,12 +86,10 @@ spawn_droplet::
cp IDLE_SPRITE_Y
jr nz, .next


; found an idle droplet
; set y
ld [hl], INITIAL_SPAWN_SPRITE_Y


; set x to random 0-19 multiplied by 8 to align with tiles
inc hl
push hl
Expand Down Expand Up @@ -107,7 +130,7 @@ spawn_droplet::
ret


; Move each active droplet down and rotate the tile
; Move each active droplet down and cycle the tile
; Speed is based on memory position for variety
move_droplets::
ld hl, droplets
Expand Down Expand Up @@ -181,13 +204,13 @@ move_droplets::
.inc_y
ld a, [droplet_sprite_y]
add e
; if we are now offscreen, set the droplet to idle
; if we are now offscreen, reset the droplet
cp a, SCRN_Y + 16 ; carry flag set if 160 > y
jp c, .inc_y_set_idle_skip
ld a, [total_droplets]
dec a
ld [total_droplets], a
ld a, IDLE_SPRITE_Y
ld a, INITIAL_SPAWN_SPRITE_Y
.inc_y_set_idle_skip
ld [droplet_sprite_y], a
.dont_move:
Expand All @@ -197,9 +220,6 @@ move_droplets::
cp %00000011
jp nz, .dont_cycle_character

ld a, [droplet_sprite_y]
cp IDLE_SPRITE_Y ; is the droplet idle?
jp nc, .skip_tile_reset
push bc
call fast_random
pop bc
Expand All @@ -222,7 +242,6 @@ move_droplets::
inc hl

.skip

dec c
jp nz, .loop
ret
Expand Down
1 change: 0 additions & 1 deletion src/main.asm
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ start::
xor a
ld [tile_command_list_length], a
call move_droplets
call spawn_droplet
call set_droplets_to_bg
call update_tile_fade
call update_tile_fade
Expand Down
48 changes: 42 additions & 6 deletions src/random.asm
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
RANDOM_SEED EQU 10
RANDOM_LENGTH EQU 40
RANDOM_X_LENGTH EQU 40
RANDOM_Y_LENGTH EQU 36

SECTION "random vars", WRAM0

seed:: DS 1
next_random_offset:: DS 1
next_random_x_offset:: DS 1
next_random_y_offset:: DS 1

SECTION "random", ROM0

init_random::
xor a
ld [next_random_offset], a
ld [next_random_x_offset], a
ld [next_random_y_offset], a
ld a, RANDOM_SEED
ld [seed],a
ret
Expand All @@ -22,7 +25,7 @@ init_random::
get_random_sprite_x::
ld hl, random_sprite_x ; get start of random LUT
ld b, 0
ld a, [next_random_offset] ; current offset in LUT
ld a, [next_random_x_offset] ; current offset in LUT
ld c, a
add hl, bc
ld b, [hl]
Expand All @@ -31,11 +34,37 @@ get_random_sprite_x::
ld a, c
inc a

cp a, RANDOM_LENGTH
cp a, RANDOM_X_LENGTH
jp nz, .no_reset
ld a, 0
.no_reset
ld [next_random_offset], a
ld [next_random_x_offset], a
ld a, b
ret



; Output:
; A - random tile-aligned x coord
; Destroys:
; BC, HL
get_random_sprite_y::
ld hl, random_sprite_y ; get start of random LUT
ld b, 0
ld a, [next_random_y_offset] ; current offset in LUT
ld c, a
add hl, bc
ld b, [hl]

; inc offset
ld a, c
inc a

cp a, RANDOM_X_LENGTH
jp nz, .no_reset
ld a, 0
.no_reset
ld [next_random_y_offset], a
ld a, b
ret

Expand Down Expand Up @@ -74,6 +103,13 @@ fast_random::

; Table of tile-aligned sprite x values. Two sequences are used to improve
; randomness.
; PowerShell: (0..19 | get-random -count 20 | %{'{0:X}' -f (8 + ($_ * 8)) }) -join ',$'
random_sprite_x:
DB $78,$40,$50,$58,$90,$70,$20,$48,$A0,$18,$88,$68,$8,$60,$80,$98,$28,$38,$30,$10
DB $60,$78,$48,$18,$88,$50,$68,$90,$8,$58,$38,$20,$70,$40,$80,$30,$28,$10,$98,$A0

; Table of tile-aligned sprite y values. Two sequences are used to improve
; randomness.
random_sprite_y:
DB $50,$88,$60,$20,$10,$58,$70,$78,$80,$38,$68,$48,$40,$90,$30,$28,$98,$18
DB $20,$50,$48,$30,$68,$88,$10,$38,$70,$60,$98,$80,$18,$78,$58,$40,$28,$90

0 comments on commit 395c31d

Please sign in to comment.