ref: 64479cd64205d611a66698f447aa42c16c334b2d
parent: 8f8281fe6ed02d8235ab9f92fa6ae93f50a3bf8e
author: yenatch <yenatch@gmail.com>
date: Sat May 24 07:40:38 EDT 2014
Rename random number function and hram labels. GenRandom -> Random GenRandomInBattle -> BattleRandom H_RAND1 -> hRandomAdd H_RAND2 -> hRandomSub
--- a/engine/battle/4_2.asm
+++ b/engine/battle/4_2.asm
@@ -132,10 +132,10 @@
ld a, [W_GRASSRATE] ; $d887
.asm_138c4
ld b, a
- ld a, [H_RAND1] ; $ffd3
+ ld a, [hRandomAdd]
cp b
jr nc, .asm_13912
- ld a, [H_RAND2] ; $ffd4
+ ld a, [hRandomSub]
ld b, a
ld hl, WildMonEncounterSlotChances ; $7918
.asm_138d0
--- a/engine/battle/core.asm
+++ b/engine/battle/core.asm
@@ -280,7 +280,7 @@
jr nc, .asm_3c1a0
ld b, $ff
.asm_3c1a0
- call GenRandom
+ call Random
cp b
jr nc, .asm_3c14f
jr asm_3c202
@@ -475,12 +475,12 @@
ld a, [$ffaa]
cp $2
jr z, .invertOutcome
- call GenRandomInBattle
+ call BattleRandom
cp $80
jr c, .playerMovesFirst
jr .enemyMovesFirst
.invertOutcome
- call GenRandomInBattle
+ call BattleRandom
cp $80
jr c, .enemyMovesFirst
jr .playerMovesFirst
@@ -1595,7 +1595,7 @@
jr c, .asm_3cb5c
jr .asm_3cb2b
.asm_3cb39
- call GenRandomInBattle
+ call BattleRandom
ld b, a
ld a, [$ff98]
cp b
@@ -2992,7 +2992,7 @@
callab AIEnemyTrainerChooseMoves
.chooseRandomMove
push hl
- call GenRandomInBattle ; get random
+ call BattleRandom
ld b, $1
cp $3f ; select move 1 in [0,3e] (63/256 chance)
jr c, .moveChosen
@@ -3415,7 +3415,7 @@
ld [$CC5B],a
ld a,CONF_ANIM - 1
call PlayMoveAnimation
- call GenRandomInBattle
+ call BattleRandom
cp a,$80
jr c,.next3
ld hl,W_PLAYERBATTSTATUS1
@@ -3438,7 +3438,7 @@
ld hl,W_PLAYERMONSTATUS
bit PAR,[hl]
jr z,.next7 ; 5975
- call GenRandomInBattle ; random number
+ call BattleRandom
cp a,$3F
jr nc,.next7
ld hl,FullyParalyzedText
@@ -3526,7 +3526,7 @@
ld hl,W_PLAYERBATTSTATUS1
res 1,[hl]
set 7,[hl]
- call GenRandomInBattle ; random number
+ call BattleRandom
and a,3
inc a
inc a
@@ -3984,7 +3984,7 @@
cp d
jp nc, Func_3ddb0
.asm_3dcdf
- call GenRandomInBattle
+ call BattleRandom
swap a
cp b
jr nc, .asm_3dcdf
@@ -3991,7 +3991,7 @@
cp c
jp c, Func_3ddb0
.asm_3dceb
- call GenRandomInBattle
+ call BattleRandom
cp b
jr nc, .asm_3dceb
cp c
@@ -3999,7 +3999,7 @@
ld a, d
sub c
ld b, a
- call GenRandomInBattle
+ call BattleRandom
swap a
sub b
jr c, .asm_3dd0e
@@ -4010,7 +4010,7 @@
call PrintHurtItselfText
jp Func_3ddb4
.asm_3dd0e
- call GenRandomInBattle
+ call BattleRandom
add a
swap a
and $7
@@ -4019,7 +4019,7 @@
ld hl, BeganToNapText
jr .asm_3dd3a
.asm_3dd20
- call GenRandomInBattle
+ call BattleRandom
and $3
ld hl, LoafingAroundText
and a
@@ -4079,7 +4079,7 @@
ld a, [wCurrentMenuItem] ; $cc26
ld c, a
.asm_3dd86
- call GenRandomInBattle
+ call BattleRandom
and $3
cp b
jr nc, .asm_3dd86
@@ -4606,7 +4606,7 @@
jr nc, .SkipHighCritical
ld b, $ff
.SkipHighCritical
- call GenRandomInBattle ; generates a random value, in "a"
+ call BattleRandom ; generates a random value, in "a"
rlc a
rlc a
rlc a
@@ -4736,7 +4736,7 @@
ld b,a ; b = level * 1.5
; loop until a random number in the range [1, b) is found
.loop
- call GenRandomInBattle ; random number
+ call BattleRandom
and a
jr z,.loop
cp b
@@ -4859,7 +4859,7 @@
; this differs from the range when the player attacks, which is [1, b)
; it's possible for the enemy to do 0 damage with Psywave, but the player always does at least 1 damage
.loop
- call GenRandomInBattle ; random number
+ call BattleRandom
cp b
jr nc,.loop
ld b,a
@@ -5090,7 +5090,7 @@
ld hl,wEnemySelectedMove
; loop to pick a random number in the range [1, $a5) to be the move used by Metronome
.pickMoveLoop
- call GenRandomInBattle ; random number
+ call BattleRandom
and a
jr z,.pickMoveLoop
cp a,NUM_MOVES + 1 ; max normal move number + 1 (this is Struggle's move number)
@@ -5385,7 +5385,7 @@
.doAccuracyCheck
; if the random number generated is greater than or equal to the scaled accuracy, the move misses
; note that this means that even the highest accuracy is still just a 255/256 chance, not 100%
- call GenRandomInBattle ; random number
+ call BattleRandom
cp b
jr nc,.moveMissed
ret
@@ -5494,7 +5494,7 @@
ld a, [hl]
ld [$ff98], a
.asm_3e69c
- call GenRandomInBattle
+ call BattleRandom
rrca
cp $d9
jr c, .asm_3e69c
@@ -5821,7 +5821,7 @@
ld [$cc5b], a
ld a,CONF_ANIM
call PlayMoveAnimation
- call GenRandomInBattle
+ call BattleRandom
cp $80
jr c, Func_3e9aa
ld hl, W_ENEMYBATTSTATUS1 ; $d067
@@ -5884,7 +5884,7 @@
ld hl, W_ENEMYMONSTATUS ; $cfe9
bit 6, [hl]
jr z, asm_3e9f6
- call GenRandomInBattle
+ call BattleRandom
cp $3f
jr nc, asm_3e9f6
ld hl, FullyParalyzedText
@@ -5973,7 +5973,7 @@
ld hl, W_ENEMYBATTSTATUS1 ; $d067
res 1, [hl] ; mon is no longer using thrash or petal dance
set 7, [hl] ; mon is now confused
- call GenRandomInBattle
+ call BattleRandom
and $3
inc a
inc a
@@ -6062,9 +6062,9 @@
ld a, $98
ld b, $88
jr z, .asm_3eb33
- call GenRandomInBattle
+ call BattleRandom
ld b, a
- call GenRandomInBattle
+ call BattleRandom
.asm_3eb33
ld hl, $cff1
ld [hli], a
@@ -6578,34 +6578,39 @@
TerminatorText_3ee9a: ; 3ee9a (f:6e9a)
db "@"
-; generates a random number unless in link battle
-; stores random number in A
-GenRandomInBattle: ; 3ee9b (f:6e9b)
+
+BattleRandom:
+; Link battles use a shared PRNG.
+
ld a, [W_ISLINKBATTLE]
cp $4
- jp nz, GenRandom
+ jp nz, Random
+
push hl
push bc
ld a, [$ccde]
ld c, a
- ld b, $0
+ ld b, 0
ld hl, $d148
add hl, bc
inc a
ld [$ccde], a
- cp $9
+ cp 9
ld a, [hl]
pop bc
pop hl
ret c
+
push hl
push bc
push af
+
xor a
ld [$ccde], a
+
ld hl, $d148
- ld b, $9
-.asm_3eec5
+ ld b, 9
+.loop
ld a, [hl]
ld c, a
add a
@@ -6614,12 +6619,14 @@
inc a
ld [hli], a
dec b
- jr nz, .asm_3eec5 ; 0x3eecd $f6
+ jr nz, .loop
+
pop af
pop bc
pop hl
ret
+
Func_3eed3: ; 3eed3 (f:6ed3)
ld a, [H_WHOSETURN] ; $fff3
and a
@@ -7089,7 +7096,7 @@
and a
jr nz, .asm_3f242
.asm_3f231
- call GenRandomInBattle
+ call BattleRandom
and $7
jr z, .asm_3f231
ld [de], a
@@ -7145,7 +7152,7 @@
jr nz, .asm_3f2d7
jr .asm_3f295
.asm_3f290
- call GenRandomInBattle
+ call BattleRandom
cp b
ret nc
.asm_3f295
@@ -7253,7 +7260,7 @@
sub a, $1e ;subtract $1E to map to equivalent 10% chance effects
.next1
push af ;push effect...
- call GenRandomInBattle ;get random 8bit value for probability test
+ call BattleRandom ;get random 8bit value for probability test
cp b ;success?
pop bc ;...pop effect into C
ret nc ;do nothing if random value is >= 1A or 4D [no status applied]
@@ -7305,7 +7312,7 @@
sub a, $1e
.next1
push af
- call GenRandomInBattle
+ call BattleRandom
cp b
pop bc
ret nc
@@ -7571,7 +7578,7 @@
ld a, [W_ISLINKBATTLE] ; $d12b
cp $4
jr z, .asm_3f572
- call GenRandomInBattle
+ call BattleRandom
cp $40
jp c, Func_3f65a
.asm_3f572
@@ -7580,7 +7587,7 @@
ld a, [de]
cp ATTACK_DOWN_SIDE_EFFECT
jr c, .asm_3f58a
- call GenRandomInBattle
+ call BattleRandom
cp SPLASH_EFFECT
jp nc, Func_3f650
ld a, [de]
@@ -7813,7 +7820,7 @@
ld [de], a
ld [W_PLAYERMOVEEFFECT], a
ld [W_ENEMYMOVEEFFECT], a
- call GenRandomInBattle
+ call BattleRandom
and $1
inc a
inc a
@@ -7832,7 +7839,7 @@
ld de, $d06f
.asm_3f728
set 1, [hl] ; mon is now using thrash/petal dance
- call GenRandomInBattle
+ call BattleRandom
and $1
inc a
inc a
@@ -7857,7 +7864,7 @@
ld c, a
inc c
.asm_3f751
- call GenRandomInBattle
+ call BattleRandom
cp c
jr nc, .asm_3f751
srl b
@@ -7899,7 +7906,7 @@
ld c, a
inc c
.asm_3f7a4
- call GenRandomInBattle
+ call BattleRandom
cp c
jr nc, .asm_3f7a4
srl b
@@ -7982,11 +7989,11 @@
cp ATTACK_TWICE_EFFECT
ld a, $2
jr z, .asm_3f853
- call GenRandomInBattle
+ call BattleRandom
and $3
cp $2
jr c, .asm_3f851
- call GenRandomInBattle
+ call BattleRandom
and $3
.asm_3f851
inc a
@@ -8017,7 +8024,7 @@
jr z, .asm_3f879
ld b, $4d ; ~30% chance of flinch
.asm_3f879
- call GenRandomInBattle
+ call BattleRandom
cp b
ret nc
set 3, [hl] ; set mon's status to flinching
@@ -8124,11 +8131,11 @@
ret nz
call Func_3f9cf
set 5, [hl]
- call GenRandomInBattle
+ call BattleRandom
and $3
cp $2
jr c, .asm_3f93e
- call GenRandomInBattle
+ call BattleRandom
and $3
.asm_3f93e
inc a
@@ -8151,7 +8158,7 @@
jp Bankswitch
ConfusionSideEffect: ; 3f959 (f:7959)
- call GenRandomInBattle
+ call BattleRandom
cp $19
ret nc
jr Func_3f96f
@@ -8179,7 +8186,7 @@
jr nz, Func_3f9a6
set 7, [hl] ; mon is now confused
push af
- call GenRandomInBattle
+ call BattleRandom
and $3
inc a
inc a
@@ -8265,7 +8272,7 @@
jr nz, .asm_3fa74
.asm_3fa17
push hl
- call GenRandomInBattle
+ call BattleRandom
and $3
ld c, a
ld b, $0
@@ -8346,7 +8353,7 @@
jr nz, .asm_3fb06
.asm_3faa8
push hl
- call GenRandomInBattle
+ call BattleRandom
and $3
ld c, a
ld b, $0
@@ -8384,7 +8391,7 @@
and a
jr z, .asm_3faa8
.asm_3fae1
- call GenRandomInBattle
+ call BattleRandom
and $7
inc a
inc c
--- a/engine/battle/e.asm
+++ b/engine/battle/e.asm
@@ -898,7 +898,7 @@
ld a,[hli]
ld h,[hl]
ld l,a
- call GenRandom
+ call Random
jp [hl]
TrainerAIPointers: ; 3a55c (e:655c)
--- a/engine/cable_club.asm
+++ b/engine/cable_club.asm
@@ -37,7 +37,7 @@
jr nz, .asm_5357
ld b, $a
.asm_535d
- call GenRandom
+ call Random
cp $fd
jr nc, .asm_535d
ld [hli], a
--- a/engine/hidden_object_functions17.asm
+++ b/engine/hidden_object_functions17.asm
@@ -362,7 +362,7 @@
ld [$ffdb], a
push hl
- call GenRandom
+ call Random
swap a
ld b, a
ld a, [$ffdb]
@@ -390,7 +390,7 @@
; Reset the cans.
ld hl, $d773
res 1, [hl]
- call GenRandom
+ call Random
and $e
ld [$d743], a
--- a/engine/in_game_trades.asm
+++ b/engine/in_game_trades.asm
@@ -189,8 +189,8 @@
ld de, $cd4c
ld bc, $2
call Func_71d11
- call GenRandom
- ld hl, H_RAND1 ; $ffd3
+ call Random
+ ld hl, hRandomAdd
ld de, $cd59
jp CopyData
--- a/engine/items/items.asm
+++ b/engine/items/items.asm
@@ -156,7 +156,7 @@
; if not fighting ghost Marowak, loop until a random number in the current
; pokeball's allowed range is found
.loop ;$56fa
- call GenRandom
+ call Random
ld b,a
ld hl,$cf91
ld a,[hl]
@@ -243,7 +243,7 @@
ld a,[H_QUOTIENT + 2]
and a
jr nz,.BallSuccess ; if ((MaxHP * 255) / BallFactor) / (CurHP / 4) > 0x255, automatic success
- call GenRandom
+ call Random
ld b,a
ld a,[H_QUOTIENT + 3]
cp b
@@ -1344,7 +1344,7 @@
ld [H_WHOSETURN],a
ld [de],a ; zero escape factor (for bait), zero bait factor (for rock)
.randomLoop ; loop until a random number less than 5 is generated
- call GenRandom
+ call Random
and a,7
cp a,5
jr nc,.randomLoop
@@ -1767,7 +1767,7 @@
call FishingInit
jp c,ItemUseNotTime
.RandomLoop
- call GenRandom
+ call Random
srl a
jr c, .SetBite
and %11
@@ -2814,7 +2814,7 @@
ld e, $0 ; no bite yet
.RandomLoop ; 0xe90c
- call GenRandom
+ call Random
srl a
ret c ; 50% chance of no battle
--- a/engine/rng.asm
+++ b/engine/rng.asm
@@ -1,13 +1,13 @@
-GenRandom_: ; 13a8f (4:7a8f)
-; generate a random 16-bit integer and store it at $FFD3,$FFD4
- ld a,[rDIV]
- ld b,a
- ld a,[H_RAND1]
+Random_::
+; Generate a random 16-bit value.
+ ld a, [rDIV]
+ ld b, a
+ ld a, [hRandomAdd]
adc b
- ld [H_RAND1],a
- ld a,[rDIV]
- ld b,a
- ld a,[H_RAND2]
+ ld [hRandomAdd], a
+ ld a, [rDIV]
+ ld b, a
+ ld a, [hRandomSub]
sbc b
- ld [H_RAND2],a
+ ld [hRandomSub], a
ret
--- a/engine/slot_machine.asm
+++ b/engine/slot_machine.asm
@@ -179,7 +179,7 @@
ld a, [W_SUBANIMSUBENTRYADDR]
and a
jr nz, .skip1
- call GenRandom
+ call Random
and a
jr z, .skip2
ld b, a
@@ -578,7 +578,7 @@
call PrintText
ld a, (SFX_1f_3b - SFX_Headers_1f) / 3
call PlaySound
- call GenRandom
+ call Random
cp $80
ld a, $0
jr c, .skip
--- a/engine/titlescreen.asm
+++ b/engine/titlescreen.asm
@@ -256,7 +256,7 @@
.new
; Generate a new TitleMon.
- call GenRandom
+ call Random
and $f
ld c, a
ld b, 0
--- a/hram.asm
+++ b/hram.asm
@@ -98,8 +98,8 @@
H_SCREENEDGEREDRAWADDR EQU $FFD1
-H_RAND1 EQU $FFD3
-H_RAND2 EQU $FFD4
+hRandomAdd EQU $FFD3
+hRandomSub EQU $FFD4
H_FRAMECOUNTER EQU $FFD5 ; decremented every V-blank (used for delays)
--- a/main.asm
+++ b/main.asm
@@ -5033,7 +5033,7 @@
; VBlank-sensitive operations end.
- call GenRandom
+ call Random
ld a, [H_VBLANKOCCURRED]
and a
@@ -9907,12 +9907,12 @@
ld a,$45
jp Predef
-; hl points to where the color gets stored
-; e contains the number of pixels filled in the health bar (out of 48)
-GetHealthBarColor:: ; 3df9 (0:3df9)
+GetHealthBarColor::
+; Return at hl the palette of
+; an HP bar e pixels long.
ld a, e
cp 27
- ld d, $0 ; green
+ ld d, 0 ; green
jr nc, .gotColor
cp 10
inc d ; yellow
@@ -9940,9 +9940,11 @@
call LoadFontTilePatterns
jp UpdateSprites
-GiveItem:: ; 3e2e (0:3e2e)
-; Give player quantity c of item b, and copy item name to $cf4b.
-; Set carry on success. If no room in bag, reset carry.
+
+GiveItem::
+; Give player quantity c of item b,
+; and copy the item's name to $cf4b.
+; Return carry on success.
ld a, b
ld [$d11e], a
ld [$cf91], a
@@ -9951,12 +9953,13 @@
ld hl,wNumBagItems
call AddItemToInventory
ret nc
- call GetItemName ; $2fcf
+ call GetItemName
call CopyStringToCF4B
scf
ret
-GivePokemon:: ; 3e48 (0:3e48)
+GivePokemon::
+; Give the player monster b at level c.
ld a, b
ld [$cf91], a
ld a, c
@@ -9967,13 +9970,15 @@
ld hl, _GivePokemon
jp Bankswitch
-GenRandom:: ; 3e5c (0:3e5c)
-; store a random 8-bit value in a
+
+Random::
+; Return a random number in a.
+; For battles, use BattleRandom.
push hl
push de
push bc
- callba GenRandom_
- ld a,[H_RAND1]
+ callba Random_
+ ld a,[hRandomAdd]
pop bc
pop de
pop hl
@@ -11075,7 +11080,7 @@
jr .asm_4f5f
.asm_4f59
call getTileSpriteStandsOn
- call GenRandom
+ call Random
.asm_4f5f
ld b, a
ld a, [wCurSpriteMovement2]
@@ -11235,11 +11240,11 @@
ld [hl], $1 ; c1x1 = 1 (movement status ready)
ret
.initNextMovementCounter
- call GenRandom
+ call Random
ld a, [H_CURRENTSPRITEOFFSET]
add $8
ld l, a
- ld a, [H_RAND1] ; $ffd3
+ ld a, [hRandomAdd]
and $7f
ld [hl], a ; c2x8: set next movement delay to a random value in [0,$7f]
dec h ; note that value 0 actually makes the delay $100 (bug?)
@@ -11559,8 +11564,8 @@
ld a, [H_CURRENTSPRITEOFFSET]
add $8
ld l, a
- call GenRandom
- ld a, [H_RAND1] ; $ffd3
+ call Random
+ ld a, [hRandomAdd]
and $7f
ld [hl], a ; c2x8: set next movement delay to a random value in [0,$7f] (again with delay $100 if value is 0)
scf ; set carry (marking failure to walk)
@@ -14846,9 +14851,9 @@
ld a, [W_ISINBATTLE] ; $d057
and a
jr nz, .copyEnemyMonData
- call GenRandom ; generate random IVs
+ call Random ; generate random IVs
ld b, a
- call GenRandom
+ call Random
.writeFreshMonData ; f3b3
push bc
ld bc, $1b
@@ -15602,11 +15607,11 @@
ret
InitializePlayerData: ; f850 (3:7850)
- call GenRandom
- ld a, [H_RAND2]
+ call Random
+ ld a, [hRandomSub]
ld [wPlayerID], a ; set player trainer id
- call GenRandom
- ld a, [H_RAND1]
+ call Random
+ ld a, [hRandomAdd]
ld [wPlayerID + 1], a
ld a, $ff
ld [$d71b], a ; XXX what's this?
--- a/scripts/celadongamecorner.asm
+++ b/scripts/celadongamecorner.asm
@@ -11,7 +11,7 @@
bit 6, [hl]
res 6, [hl]
ret z
- call GenRandom
+ call Random
ld a, [$ffd3]
cp $7
jr nc, .asm_48be2
--- a/scripts/vermilioncity.asm
+++ b/scripts/vermilioncity.asm
@@ -14,7 +14,7 @@
jp CallFunctionInTable
VermilionCityScript_197c0: ; 197c0 (6:57c0)
- call GenRandom
+ call Random
ld a, [$ffd4]
and $e
ld [$d743], a