aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMiquel Sabaté Solà <mssola@mssola.com>2026-02-10 22:45:20 +0100
committerMiquel Sabaté Solà <mssola@mssola.com>2026-02-10 22:45:20 +0100
commit1b46ddb0486276de0b0691e10277d206a0763300 (patch)
treec85c159b92d8466291b74398c21f881625094014 /src
parent48f3a562c7d4d6e95c18d44e201a8c172f107a5e (diff)
downloadjetpac.nes-1b46ddb0486276de0b0691e10277d206a0763300.tar.gz
jetpac.nes-1b46ddb0486276de0b0691e10277d206a0763300.zip
Simplify the random_valid_y_coordinate function
This function relies on a pre-computed table to get the "random" numbers from, but most of the times this is used, it needs to be between some safe boundaries. Before this commit, this was done inside of the function, correcting the fetched value to be above or below these limits. But there was a bug on the below the grounds limit, in which the 'sbc' instruction could subtract too much from the fetched value and make enemies appear below the sky. Now, this is easy to correct, but since we are cheating with a pre-computed table, I thought it would be vastly easier to just get a random table with the "proper" values. That is, random but within the required boundaries already. Plus, it makes the 'random_valid_y_coordinate' function much faster. This commit adds a ruby script in bin/ which produces a pseudo-random table when called, making sure all the requirements are met. We have to make sure to call this script if we ever change the boundaries at some point. Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
Diffstat (limited to 'src')
-rw-r--r--src/background.s6
-rw-r--r--src/prng.s56
2 files changed, 24 insertions, 38 deletions
diff --git a/src/background.s b/src/background.s
index a9dfbaf..0facde6 100644
--- a/src/background.s
+++ b/src/background.s
@@ -3,9 +3,15 @@
.scope Background
;; Screen coordinate on the Y axis where elements can begin to appear (e.g.
;; upper bound for new enemies, starting point for falling items, etc.).
+ ;;
+ ;; NOTE: if you change this value, you should re-generate the random values
+ ;; from prng.s as well.
UPPER_MARGIN_Y_COORD = $1A
;; Screen coordinates on the Y axis for the ground.
+ ;;
+ ;; NOTE: if you change this value, you should re-generate the random values
+ ;; from prng.s as well.
GROUND_Y_COORD = $C8
;; Returns whether the given tile position collides with a background
diff --git a/src/prng.s b/src/prng.s
index e2653b7..8211957 100644
--- a/src/prng.s
+++ b/src/prng.s
@@ -15,48 +15,28 @@
.proc random_valid_y_coordinate
;; Get the new random number and store it right away.
ldx zp_rand
- lda rand_table, x
+ lda valid_y_rand_table, x
sta zp_rand
-
- ;; Is this value below ground?
- cmp #(Background::GROUND_Y_COORD - 16)
- bcc @check_sky
-
- ;; Yes! Return something that is at least above it.
- sec
- sbc #(Background::GROUND_Y_COORD - 16)
- rts
-
- @check_sky:
- ;; Is this value above the upper screen margin?
- cmp #Background::UPPER_MARGIN_Y_COORD
- bcs @end
-
- ;; Yes! Return something that is at least below it.
- clc
- adc #Background::UPPER_MARGIN_Y_COORD
-
- @end:
rts
.endproc
.endscope
;; The pre-computed table.
-rand_table:
- .byte $D7, $3A, $1C, $8F, $09, $B2, $E6, $54, $A3, $91, $2B, $F5, $78, $0D, $4C, $6E
- .byte $FF, $C0, $52, $33, $6A, $E9, $9B, $1A, $47, $88, $7D, $21, $0E, $F4, $B3, $9C
- .byte $15, $67, $A8, $41, $D2, $39, $80, $76, $C9, $E5, $0A, $1B, $5F, $22, $73, $DA
- .byte $B4, $96, $3C, $E0, $8D, $F7, $2A, $05, $9E, $43, $11, $6D, $A7, $58, $C1, $32
- .byte $28, $0F, $79, $BE, $51, $64, $9D, $A9, $3B, $71, $8E, $C6, $4A, $13, $F0, $27
- .byte $E2, $5C, $06, $D3, $95, $B8, $4F, $70, $19, $A4, $6B, $38, $82, $C7, $5E, $01
- .byte $F3, $2D, $9A, $65, $7C, $D1, $0B, $E8, $57, $36, $84, $1F, $B0, $92, $45, $AC
- .byte $60, $7E, $A1, $53, $C8, $29, $D4, $FB, $07, $42, $E3, $99, $16, $8A, $3D, $C5
- .byte $24, $B1, $6F, $03, $7A, $E7, $8C, $59, $D0, $46, $93, $1E, $A5, $2C, $B7, $F1
- .byte $89, $55, $C3, $30, $62, $98, $04, $D6, $7F, $A0, $E4, $12, $3B, $81, $F9, $23
- .byte $C4, $0D, $5A, $71, $9F, $B6, $2E, $85, $37, $A9, $18, $6C, $E1, $4B, $D9, $02
- .byte $F8, $63, $B5, $40, $97, $0C, $7A, $51, $A2, $3E, $8F, $D5, $14, $69, $E0, $B8
- .byte $4D, $77, $25, $9B, $0A, $F2, $3C, $86, $E9, $1F, $68, $A3, $50, $C1, $7D, $04
- .byte $B2, $8E, $56, $1D, $73, $9C, $F5, $2A, $61, $D7, $09, $3E, $84, $A0, $E6, $1B
- .byte $3F, $C8, $94, $05, $72, $D6, $A7, $4C, $1A, $5F, $B3, $29, $80, $E1, $6D, $9E
- .byte $0C, $43, $F7, $8B, $52, $16, $A8, $3D, $91, $2B, $E5, $70, $C6, $4A, $D9, $F8
+valid_y_rand_table:
+ .byte $25, $87, $B7, $6A, $23, $77, $6D, $71, $6D, $B6, $86, $93, $2B, $97, $A8, $39
+ .byte $26, $AE, $A6, $70, $9F, $2D, $74, $B2, $8E, $A5, $33, $3E, $6D, $75, $91, $6B
+ .byte $A1, $2E, $A4, $7C, $53, $28, $1E, $79, $1B, $6B, $A9, $7E, $76, $74, $90, $B3
+ .byte $26, $9B, $2C, $94, $9C, $86, $7A, $B7, $1D, $B4, $72, $82, $7C, $23, $33, $78
+ .byte $B0, $70, $B5, $A9, $1B, $70, $83, $28, $73, $24, $7E, $28, $B1, $8A, $75, $9B
+ .byte $20, $26, $22, $8D, $7C, $3F, $29, $3D, $1F, $72, $22, $A4, $86, $34, $6F, $9C
+ .byte $20, $A9, $3A, $77, $39, $6F, $3B, $86, $95, $3D, $96, $9F, $56, $84, $53, $77
+ .byte $B2, $23, $72, $1C, $99, $B4, $B8, $32, $8E, $A3, $21, $20, $25, $97, $92, $2C
+ .byte $3E, $96, $B5, $28, $84, $7E, $A7, $8A, $A3, $3E, $A4, $72, $A7, $A8, $6D, $A3
+ .byte $AE, $7E, $23, $6E, $6A, $72, $35, $3C, $99, $B3, $37, $22, $7F, $79, $1C, $97
+ .byte $52, $77, $77, $95, $B4, $7F, $2D, $28, $71, $9E, $76, $9A, $A0, $32, $23, $7F
+ .byte $94, $9F, $90, $3C, $AF, $A7, $9C, $B5, $87, $9A, $9E, $84, $B0, $AD, $7D, $9D
+ .byte $A2, $24, $89, $6A, $79, $81, $A2, $1E, $A9, $1E, $AB, $8D, $6E, $54, $90, $AE
+ .byte $A4, $B5, $1E, $B4, $53, $9D, $7D, $78, $74, $AD, $6A, $96, $2F, $6F, $29, $9F
+ .byte $98, $1D, $3B, $57, $6D, $B0, $1C, $88, $B0, $55, $99, $8F, $B1, $3F, $AD, $23
+ .byte $92, $7A, $26, $55, $A3, $75, $34, $23, $20, $79, $B0, $A6, $B6, $2A, $20, $8C