diff --git a/constants/radio_constants.asm b/constants/radio_constants.asm index 207a2ff60..8c7572910 100644 --- 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 diff --git a/engine/radio.asm b/engine/radio.asm index 54cee3894..06cc3a59a 100644 --- a/engine/radio.asm +++ b/engine/radio.asm @@ -348,11 +348,13 @@ OPT_MaryText1: 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 @@ OaksPkmnTalk8: 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 @@ OaksPkmnTalk8: 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 @@ OaksPkmnTalk9: .ok jp NextRadioLine -.Descriptors: +.Adjectives: +; there are NUM_OAKS_MON_TALK_ADJECTIVES entries dw .cute dw .weird dw .pleasant @@ -1158,7 +1164,7 @@ PnP_Text3: 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 @@ PnP_Text4: 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 @@ PeoplePlaces5: .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 @@ PnP_Text5: 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 @@ PeoplePlaces7: .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 @@ BuenasPassword4: ; 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 @@ BuenasPassword4: ; 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. diff --git a/macros/code.asm b/macros/code.asm index 5ec214d50..5a0b82a24 100644 --- a/macros/code.asm +++ b/macros/code.asm @@ -39,15 +39,17 @@ ENDM 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