shithub: pokecrystal

Download patch

ref: cc95d1208d4a6b8291a5430a8d652a9337074788
parent: 22b607b664b1b350ad96340b39440c8e4296006c
author: Remy Oukaour <remy.oukaour@gmail.com>
date: Wed Jan 10 06:57:28 EST 2018

Use maskbits more, and clarify its usage

--- a/constants/radio_constants.asm
+++ b/constants/radio_constants.asm
@@ -105,9 +105,16 @@
 	const MAPRADIO_LETS_ALL_SING
 	const MAPRADIO_ROCKET
 
+; These tables in engine/radio.asm are all sized to a power of 2
+; so there's no need for a rejection sampling loop
+NUM_OAKS_MON_TALK_ADVERBS    EQU 16 ; OaksPkmnTalk8.Adverbs
+NUM_OAKS_MON_TALK_ADJECTIVES EQU 16 ; OaksPkmnTalk9.Adjectives
+NUM_PNP_PEOPLE_ADJECTIVES    EQU 16 ; PeoplePlaces5.Adjectives
+NUM_PNP_PLACES_ADJECTIVES    EQU 16 ; PeoplePlaces7.Adjectives
+
 ; BuenasPasswordTable sizes (see data/radio/buenas_passwords.asm)
-NUM_PASSWORD_CATEGORIES EQU 11
-NUM_PASSWORDS_PER_CATEGORY EQU 3
+NUM_PASSWORD_CATEGORIES    EQU 11
+NUM_PASSWORDS_PER_CATEGORY EQU  3
 
 ; GetBuenasPassword.StringFunctionJumpTable indexes (see engine/radio.asm)
 	const_def
--- a/engine/radio.asm
+++ b/engine/radio.asm
@@ -348,11 +348,13 @@
 	db "@"
 
 OaksPkmnTalk8:
+	; 0-15 are all valid indexes into .Adverbs,
+	; so no need for a retry loop
 	call Random
-	and $f ; 0-15 are all the valid indexes into .Descriptors
+	maskbits NUM_OAKS_MON_TALK_ADVERBS +- 1
 	ld e, a
 	ld d, 0
-	ld hl, .Descriptors
+	ld hl, .Adverbs
 	add hl, de
 	add hl, de
 	ld a, [hli]
@@ -361,7 +363,8 @@
 	ld a, OAKS_POKEMON_TALK_9
 	jp NextRadioLine
 
-.Descriptors:
+.Adverbs:
+; there are NUM_OAKS_MON_TALK_ADVERBS entries
 	dw .sweetadorably
 	dw .wigglyslickly
 	dw .aptlynamed
@@ -460,11 +463,13 @@
 	db "@"
 
 OaksPkmnTalk9:
+	; 0-15 are all valid indexes into .Adjectives,
+	; so no need for a retry loop
 	call Random
-	and $f ; 0-15 are all the valid indexes into .Descriptors
+	maskbits NUM_OAKS_MON_TALK_ADJECTIVES +- 1
 	ld e, a
 	ld d, 0
-	ld hl, .Descriptors
+	ld hl, .Adjectives
 	add hl, de
 	add hl, de
 	ld a, [hli]
@@ -481,7 +486,8 @@
 .ok
 	jp NextRadioLine
 
-.Descriptors:
+.Adjectives:
+; there are NUM_OAKS_MON_TALK_ADJECTIVES entries
 	dw .cute
 	dw .weird
 	dw .pleasant
@@ -1158,7 +1164,7 @@
 
 PeoplePlaces4: ; People
 	call Random
-	and $7f
+	maskbits NUM_TRAINER_CLASSES +- 1
 	inc a
 	cp NUM_TRAINER_CLASSES - 1
 	jr nc, PeoplePlaces4
@@ -1199,11 +1205,13 @@
 	db "@"
 
 PeoplePlaces5:
+	; 0-15 are all valid indexes into .Adjectives,
+	; so no need for a retry loop
 	call Random
-	and $f ; 0-15 are all the valid indexes into .Descriptors
+	maskbits NUM_PNP_PEOPLE_ADJECTIVES +- 1
 	ld e, a
 	ld d, 0
-	ld hl, .Descriptors
+	ld hl, .Adjectives
 	add hl, de
 	add hl, de
 	ld a, [hli]
@@ -1221,7 +1229,8 @@
 .ok
 	jp NextRadioLine
 
-.Descriptors:
+.Adjectives:
+; there are NUM_PNP_PEOPLE_ADJECTIVES entries
 	dw PnP_cute
 	dw PnP_lazy
 	dw PnP_happy
@@ -1346,11 +1355,13 @@
 	db "@"
 
 PeoplePlaces7:
+	; 0-15 are all valid indexes into .Adjectives,
+	; so no need for a retry loop
 	call Random
-	and $f ; 0-15 are all the valid indexes into .Descriptors
+	maskbits NUM_PNP_PLACES_ADJECTIVES +- 1
 	ld e, a
 	ld d, 0
-	ld hl, .Descriptors
+	ld hl, .Adjectives
 	add hl, de
 	add hl, de
 	ld a, [hli]
@@ -1369,7 +1380,8 @@
 .ok
 	jp PrintRadioLine
 
-.Descriptors:
+.Adjectives:
+; there are NUM_PNP_PLACES_ADJECTIVES entries
 	dw PnP_cute
 	dw PnP_lazy
 	dw PnP_happy
@@ -1553,7 +1565,7 @@
 ; There are only 11 groups to choose from.
 .greater_than_11
 	call Random
-	maskbits NUM_PASSWORD_CATEGORIES
+	maskbits NUM_PASSWORD_CATEGORIES +- 1
 	cp NUM_PASSWORD_CATEGORIES
 	jr nc, .greater_than_11
 ; Store it in the high nybble of e.
@@ -1562,7 +1574,7 @@
 ; For each group, choose one of the three passwords.
 .greater_than_three
 	call Random
-	maskbits NUM_PASSWORDS_PER_CATEGORY
+	maskbits NUM_PASSWORDS_PER_CATEGORY +- 1
 	cp NUM_PASSWORDS_PER_CATEGORY
 	jr nc, .greater_than_three
 ; The high nybble of wBuenasPassword will now contain the password group index, and the low nybble contains the actual password.
--- a/macros/code.asm
+++ b/macros/code.asm
@@ -39,15 +39,17 @@
 mobile EQUS "ret"
 
 maskbits: MACRO
+; masks just enough bits to cover the argument
+; e.g. "maskbits %00010100" becomes "and %00011111"
 ; example usage in rejection sampling:
 ; .loop
 ; 	call Random
-; 	maskbits 30
+; 	maskbits 30 +- 1
 ; 	cp 30
 ; 	jr nc, .loop
 x = 1
 rept 8
-if \1 > x
+if x < (\1)
 x = (x + 1) * 2 +- 1
 endc
 endr