Avoid "+- 1" at every maskbits

This commit is contained in:
Remy Oukaour 2018-01-16 17:27:50 -05:00
parent 8048082114
commit 7b3de85a06
32 changed files with 79 additions and 76 deletions

View File

@ -1304,7 +1304,7 @@ ParseSFXOrRest: ; e8698
ld [hl], a
; are we on the last channel? (noise sampling)
ld a, [CurChannel]
maskbits NUM_MUSIC_CHANS +- 1
maskbits NUM_MUSIC_CHANS
cp CHAN4
ret z
; update hi frequency from next param
@ -1668,7 +1668,7 @@ MusicEE; e883e
; if ????, jump
; get channel
ld a, [CurChannel]
maskbits NUM_MUSIC_CHANS +- 1
maskbits NUM_MUSIC_CHANS
ld e, a
ld d, 0
; hl = Channel1JumpCondition + channel id
@ -1966,7 +1966,7 @@ Music_NoteType: ; e8963
add hl, bc
ld [hl], a
ld a, [CurChannel]
maskbits NUM_MUSIC_CHANS +- 1
maskbits NUM_MUSIC_CHANS
cp CHAN4
ret z
; intensity
@ -2414,7 +2414,7 @@ SetLRTracks: ; e8b1b
push de
; store current channel in de
ld a, [CurChannel]
maskbits NUM_MUSIC_CHANS +- 1
maskbits NUM_MUSIC_CHANS
ld e, a
ld d, 0
; get this channel's lr tracks
@ -2449,7 +2449,7 @@ _PlayMusic:: ; e8b30
call LoadMusicByte ; store first byte of music header in a
rlca
rlca
maskbits NUM_MUSIC_CHANS +- 1
maskbits NUM_MUSIC_CHANS
inc a
.loop
; start playing channels
@ -2505,7 +2505,7 @@ _PlayCryHeader:: ; e8b79
; Top 2 bits contain the number of channels
rlca
rlca
maskbits NUM_MUSIC_CHANS +- 1
maskbits NUM_MUSIC_CHANS
; For each channel:
inc a
@ -2530,7 +2530,7 @@ _PlayCryHeader:: ; e8b79
; No tempo for channel 4
ld a, [CurChannel]
maskbits NUM_MUSIC_CHANS +- 1
maskbits NUM_MUSIC_CHANS
cp CHAN4
jr nc, .start
@ -2669,7 +2669,7 @@ _PlaySFX:: ; e8c04
call LoadMusicByte
rlca ; top 2
rlca ; bits
maskbits NUM_MUSIC_CHANS +- 1
maskbits NUM_MUSIC_CHANS
inc a ; # channels -> # loops
.startchannels
push af
@ -2722,7 +2722,7 @@ PlayStereoSFX:: ; e8ca6
call LoadMusicByte
rlca
rlca
maskbits NUM_MUSIC_CHANS +- 1
maskbits NUM_MUSIC_CHANS
inc a
.loop
@ -2736,7 +2736,7 @@ PlayStereoSFX:: ; e8ca6
push de
; get tracks for this channel
ld a, [CurChannel]
maskbits NUM_MUSIC_CHANS +- 1
maskbits NUM_MUSIC_CHANS
ld e, a
ld d, 0
call GetLRTracks

View File

@ -216,6 +216,8 @@ NUM_WATERMON EQU 3 ; data/wild/*_water.asm table size
GRASS_WILDDATA_LENGTH EQU (NUM_GRASSMON * 2 + 1) * 3 + 2
WATER_WILDDATA_LENGTH EQU (NUM_WATERMON * 2 + 1) * 1 + 2
NUM_ROAMMON_MAPS EQU 16 ; RoamMaps table size (see data/wild/roammon_maps.asm)
; swarms

View File

@ -13,6 +13,7 @@ endr
ENDM
RoamMaps: ; 2a40f
; there are NUM_ROAMMON_MAPS entries
roam_map ROUTE_29, 2, ROUTE_30, ROUTE_46
roam_map ROUTE_30, 2, ROUTE_29, ROUTE_31
roam_map ROUTE_31, 3, ROUTE_30, ROUTE_32, ROUTE_36

View File

@ -186,7 +186,7 @@ AIChooseMove: ; 440ce
.ChooseMove:
ld hl, Buffer1
call Random
maskbits NUM_MOVES +- 1
maskbits NUM_MOVES
ld c, a
ld b, 0
add hl, bc

View File

@ -640,7 +640,7 @@ StartTrainerBattle_LoadPokeBallGraphics: ; 8c5dc (23:45dc)
.cgb
ld hl, .daypals
ld a, [TimeOfDayPal]
maskbits NUM_DAYTIMES +- 1
maskbits NUM_DAYTIMES
cp DARKNESS_F
jr nz, .daytime
ld hl, .nightpals

View File

@ -6031,7 +6031,7 @@ ParseEnemyAction: ; 3e7c1
.loop2
ld hl, EnemyMonMoves
call BattleRandom
maskbits NUM_MOVES +- 1
maskbits NUM_MOVES
ld c, a
ld b, 0
add hl, bc

View File

@ -932,7 +932,7 @@ BattleCommand_CheckObedience: ; 343db
.RandomMove:
call BattleRandom
maskbits NUM_MOVES +- 1
maskbits NUM_MOVES
cp b
jr nc, .RandomMove
@ -4387,7 +4387,7 @@ BattleCommand_SleepTalk: ; 35b33
.sample_move
push hl
call BattleRandom
maskbits NUM_MOVES +- 1
maskbits NUM_MOVES
ld c, a
ld b, 0
add hl, bc
@ -8204,7 +8204,7 @@ BattleCommand_Conversion: ; 3707f
.done
.loop3
call BattleRandom
maskbits NUM_MOVES +- 1
maskbits NUM_MOVES
ld c, a
ld b, 0
ld hl, StringBuffer1

View File

@ -1206,7 +1206,7 @@ BattleAnimCmd_Sound: ; cc7cd (33:47cd)
srl a
ld [wSFXDuration], a
call .GetCryTrack
maskbits NUM_NOISE_CHANS +- 1
maskbits NUM_NOISE_CHANS
ld [CryTracks], a ; CryTracks
ld e, a
@ -1243,7 +1243,7 @@ BattleAnimCmd_Sound: ; cc7cd (33:47cd)
BattleAnimCmd_Cry: ; cc807 (33:4807)
call GetBattleAnimByte
maskbits NUM_NOISE_CHANS +- 1
maskbits NUM_NOISE_CHANS
ld e, a
ld d, 0
ld hl, .CryData

View File

@ -1230,7 +1230,7 @@ LoadMapPals:
ld l, a
; Futher refine by time of day
ld a, [TimeOfDayPal]
maskbits NUM_DAYTIMES +- 1
maskbits NUM_DAYTIMES
add a
add a
add a
@ -1276,7 +1276,7 @@ LoadMapPals:
.got_pals
ld a, [TimeOfDayPal]
maskbits NUM_DAYTIMES +- 1
maskbits NUM_DAYTIMES
ld bc, 8 palettes
ld hl, MapObjectPals
call AddNTimes
@ -1300,7 +1300,7 @@ LoadMapPals:
ld de, RoofPals
add hl, de
ld a, [TimeOfDayPal]
maskbits NUM_DAYTIMES +- 1
maskbits NUM_DAYTIMES
cp NITE_F
jr c, .morn_day
rept 4

View File

@ -27,12 +27,12 @@ Function_LoadOpponentTrainerAndPokemons: ; 1f8000
add b
ld b, a ; b contains the nr of the trainer
if DEF(_CRYSTAL11)
maskbits BATTLETOWER_NUM_UNIQUE_TRAINERS +- 1
maskbits BATTLETOWER_NUM_UNIQUE_TRAINERS
cp BATTLETOWER_NUM_UNIQUE_TRAINERS
else
; Crystal 1.0 used the wrong constant here, so only the first 21
; trainers in BattleTowerTrainers can be sampled.
maskbits BATTLETOWER_NUM_UNIQUE_PKMN +- 1
maskbits BATTLETOWER_NUM_UNIQUE_PKMN
cp BATTLETOWER_NUM_UNIQUE_PKMN
endc
jr nc, .resample
@ -115,7 +115,7 @@ Function_LoadRandomBattleTowerPkmn: ; 1f8081
ld a, [hRandomAdd]
add b
ld b, a
maskbits BATTLETOWER_NUM_UNIQUE_PKMN +- 1
maskbits BATTLETOWER_NUM_UNIQUE_PKMN
cp BATTLETOWER_NUM_UNIQUE_PKMN
jr nc, .resample
; in register 'a' is the chosen Pkmn of the LevelGroup

View File

@ -17,7 +17,7 @@ Special_BuenasPassword: ; 8af6b
ld a, [MenuSelection]
ld c, a
ld a, [wBuenasPassword]
maskbits NUM_PASSWORDS_PER_CATEGORY +- 1
maskbits NUM_PASSWORDS_PER_CATEGORY
cp c
jr nz, .wrong
ld b, $1

View File

@ -80,7 +80,7 @@ rept 4
endr
ld a, [TimeOfDay]
maskbits NUM_DAYTIMES +- 1
maskbits NUM_DAYTIMES
cp NITE_F
jr c, .time_species
inc hl

View File

@ -435,7 +435,7 @@ MagnetTrain_Jumptable_FirstRunThrough: ; 8ceae
ld a, [wEnvironment]
push af
ld a, [TimeOfDay]
maskbits NUM_DAYTIMES +- 1
maskbits NUM_DAYTIMES
ld [TimeOfDayPal], a
ld a, $1
ld [wEnvironment], a

View File

@ -10,7 +10,7 @@ LoadPoisonBGPals: ; cbcdd
and a
jr nz, .cgb
ld a, [TimeOfDayPal]
maskbits NUM_DAYTIMES +- 1
maskbits NUM_DAYTIMES
cp DARKNESS_F
ld a, %00000000
jr z, .convert_pals

View File

@ -775,7 +775,7 @@ GetPokedexEntryBank:
ld a, [EnemyMonSpecies]
rlca
rlca
maskbits NUM_DEX_ENTRY_BANKS +- 1
maskbits NUM_DEX_ENTRY_BANKS
ld hl, .PokedexEntryBanks
ld d, 0
ld e, a

View File

@ -82,7 +82,7 @@ _LoadFontsBattleExtra:: ; fb4be
LoadFrame: ; fb4cc
ld a, [TextBoxFrame]
maskbits NUM_FRAMES +- 1
maskbits NUM_FRAMES
ld bc, 6 * LEN_1BPP_TILE
ld hl, Frames
call AddNTimes

View File

@ -893,7 +893,7 @@ MapObjectMovementPattern: ; 47dd
ld hl, OBJECT_DIRECTION_WALKING
add hl, de
ld a, [hl]
maskbits NUM_DIRECTIONS +- 1
maskbits NUM_DIRECTIONS
ld d, 1 * 8 + 6
cp DOWN
jr z, .ok_13

View File

@ -106,7 +106,7 @@ Function6f3e: ; 6f3e
ld hl, OBJECT_DIRECTION_WALKING
add hl, bc
ld a, [hl]
maskbits NUM_DIRECTIONS +- 1
maskbits NUM_DIRECTIONS
ld e, a
ld d, 0
ld hl, .data_6f5b
@ -131,7 +131,7 @@ Function6f5f: ; 6f5f
push af
ld hl, OBJECT_DIRECTION_WALKING
add hl, bc
maskbits NUM_DIRECTIONS +- 1
maskbits NUM_DIRECTIONS
ld e, a
ld d, 0
ld hl, .data_6f7b
@ -178,7 +178,7 @@ Function6fa1: ; 6fa1
ld hl, OBJECT_DIRECTION_WALKING
add hl, bc
ld a, [hl]
maskbits NUM_DIRECTIONS +- 1
maskbits NUM_DIRECTIONS
jr z, .down
dec a
jr z, .up

View File

@ -954,7 +954,7 @@ InitPackBuffers: ; 1068a
ld [wJumptableIndex], a
; pocket id -> jumptable index
ld a, [wLastPocket]
maskbits NUM_POCKETS +- 1
maskbits NUM_POCKETS
ld [wCurrPocket], a
inc a
add a
@ -1099,7 +1099,7 @@ DepositSellTutorial_InterpretJoypad: ; 1076f
.d_left
ld a, [wJumptableIndex]
dec a
maskbits NUM_POCKETS +- 1
maskbits NUM_POCKETS
ld [wJumptableIndex], a
push de
ld de, SFX_SWITCH_POCKETS
@ -1111,7 +1111,7 @@ DepositSellTutorial_InterpretJoypad: ; 1076f
.d_right
ld a, [wJumptableIndex]
inc a
maskbits NUM_POCKETS +- 1
maskbits NUM_POCKETS
ld [wJumptableIndex], a
push de
ld de, SFX_SWITCH_POCKETS
@ -1281,7 +1281,7 @@ WaitBGMap_DrawPackGFX: ; 1089a (4:489a)
call WaitBGMap
DrawPackGFX: ; 1089d
ld a, [wCurrPocket]
maskbits NUM_POCKETS +- 1
maskbits NUM_POCKETS
ld e, a
ld d, $0
ld a, [BattleType]

View File

@ -140,7 +140,7 @@ DoPlayerMovement:: ; 80000
.water
ld a, c
maskbits NUM_DIRECTIONS +- 1
maskbits NUM_DIRECTIONS
ld c, a
ld b, 0
ld hl, .water_table
@ -244,7 +244,7 @@ DoPlayerMovement:: ; 80000
ld a, [PlayerDirection]
rrca
rrca
maskbits NUM_DIRECTIONS +- 1
maskbits NUM_DIRECTIONS
cp e
jr z, .not_turning
@ -430,7 +430,7 @@ DoPlayerMovement:: ; 80000
ld a, [PlayerDirection]
rrca
rrca
maskbits NUM_DIRECTIONS +- 1
maskbits NUM_DIRECTIONS
cp e
jr nz, .not_warp
call WarpCheck
@ -559,7 +559,7 @@ DoPlayerMovement:: ; 80000
cp 0
ret z
maskbits NUM_DIRECTIONS +- 1
maskbits NUM_DIRECTIONS
ld e, a
ld d, 0
ld hl, .forced_dpad

View File

@ -589,7 +589,7 @@ Special_SurfStartStep: ; 8379
ld a, [PlayerDirection]
srl a
srl a
maskbits NUM_DIRECTIONS +- 1
maskbits NUM_DIRECTIONS
ld e, a
ld d, 0
ld hl, .movement_data

View File

@ -230,7 +230,7 @@ GetDexEntryPointer: ; 44333
push de
rlca
rlca
maskbits NUM_DEX_ENTRY_BANKS +- 1
maskbits NUM_DEX_ENTRY_BANKS
ld hl, .PokedexEntryBanks
ld d, 0
ld e, a

View File

@ -251,7 +251,7 @@ InitPokegearTilemap: ; 90da8 (24:4da8)
ld a, $4f
call ByteFill
ld a, [wPokegearCard]
maskbits NUM_POKEGEAR_CARDS +- 1
maskbits NUM_POKEGEAR_CARDS
add a
ld e, a
ld d, 0
@ -289,7 +289,7 @@ InitPokegearTilemap: ; 90da8 (24:4da8)
ld [hWY], a
; swap region maps
ld a, [wPokegearMapRegion]
maskbits NUM_REGIONS +- 1
maskbits NUM_REGIONS
xor 1
ld [wPokegearMapRegion], a
ret

View File

@ -247,7 +247,7 @@ endr
; Generate a number, either 0, 1, or 2, to choose a time of day.
.loop2
call Random
maskbits NUM_DAYTIMES +- 1
maskbits NUM_DAYTIMES
cp DARKNESS_F
jr z, .loop2
@ -351,7 +351,7 @@ OaksPkmnTalk8:
; 0-15 are all valid indexes into .Adverbs,
; so no need for a retry loop
call Random
maskbits NUM_OAKS_MON_TALK_ADVERBS +- 1
maskbits NUM_OAKS_MON_TALK_ADVERBS
ld e, a
ld d, 0
ld hl, .Adverbs
@ -466,7 +466,7 @@ OaksPkmnTalk9:
; 0-15 are all valid indexes into .Adjectives,
; so no need for a retry loop
call Random
maskbits NUM_OAKS_MON_TALK_ADJECTIVES +- 1
maskbits NUM_OAKS_MON_TALK_ADJECTIVES
ld e, a
ld d, 0
ld hl, .Adjectives
@ -692,7 +692,7 @@ PokedexShow_GetDexEntryBank:
dec a
rlca
rlca
maskbits NUM_DEX_ENTRY_BANKS +- 1
maskbits NUM_DEX_ENTRY_BANKS
ld hl, .PokedexEntryBanks
ld d, 0
ld e, a
@ -1164,7 +1164,7 @@ PnP_Text3:
PeoplePlaces4: ; People
call Random
maskbits NUM_TRAINER_CLASSES +- 1
maskbits NUM_TRAINER_CLASSES
inc a
cp NUM_TRAINER_CLASSES - 1
jr nc, PeoplePlaces4
@ -1208,7 +1208,7 @@ PeoplePlaces5:
; 0-15 are all valid indexes into .Adjectives,
; so no need for a retry loop
call Random
maskbits NUM_PNP_PEOPLE_ADJECTIVES +- 1
maskbits NUM_PNP_PEOPLE_ADJECTIVES
ld e, a
ld d, 0
ld hl, .Adjectives
@ -1358,7 +1358,7 @@ PeoplePlaces7:
; 0-15 are all valid indexes into .Adjectives,
; so no need for a retry loop
call Random
maskbits NUM_PNP_PLACES_ADJECTIVES +- 1
maskbits NUM_PNP_PLACES_ADJECTIVES
ld e, a
ld d, 0
ld hl, .Adjectives
@ -1565,7 +1565,7 @@ BuenasPassword4:
; There are only 11 groups to choose from.
.greater_than_11
call Random
maskbits NUM_PASSWORD_CATEGORIES +- 1
maskbits NUM_PASSWORD_CATEGORIES
cp NUM_PASSWORD_CATEGORIES
jr nc, .greater_than_11
; Store it in the high nybble of e.
@ -1574,7 +1574,7 @@ BuenasPassword4:
; For each group, choose one of the three passwords.
.greater_than_three
call Random
maskbits NUM_PASSWORDS_PER_CATEGORY +- 1
maskbits NUM_PASSWORDS_PER_CATEGORY
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.

View File

@ -614,7 +614,7 @@ GetPocketName:
ld a, [wItemAttributeParamBuffer]
dec a
ld hl, .Pockets
maskbits NUM_POCKETS +- 1
maskbits NUM_POCKETS
add a
ld e, a
ld d, 0
@ -2474,7 +2474,7 @@ Script_warpfacing:
; parameters: facing, map_group, map_id, x, y
call GetScriptByte
maskbits NUM_DIRECTIONS +- 1
maskbits NUM_DIRECTIONS
ld c, a
ld a, [wPlayerSpriteSetupFlags]
set 5, a

View File

@ -283,7 +283,7 @@ StatsScreen_GetJoypad: ; 4de2c (13:5e2c)
StatsScreen_JoypadAction: ; 4de54 (13:5e54)
push af
ld a, [wcf64]
maskbits NUM_STAT_PAGES +- 1
maskbits NUM_STAT_PAGES
ld c, a
pop af
bit B_BUTTON_F, a
@ -512,7 +512,7 @@ StatsScreen_LoadGFX: ; 4dfb6 (13:5fb6)
.ClearBox: ; 4dfda (13:5fda)
ld a, [wcf64]
maskbits NUM_STAT_PAGES +- 1
maskbits NUM_STAT_PAGES
ld c, a
call StatsScreen_LoadPageIndicators
hlcoord 0, 8
@ -522,7 +522,7 @@ StatsScreen_LoadGFX: ; 4dfb6 (13:5fb6)
.LoadPals: ; 4dfed (13:5fed)
ld a, [wcf64]
maskbits NUM_STAT_PAGES +- 1
maskbits NUM_STAT_PAGES
ld c, a
farcall LoadStatsScreenPals
call DelayFrame
@ -532,7 +532,7 @@ StatsScreen_LoadGFX: ; 4dfb6 (13:5fb6)
.PageTilemap: ; 4e002 (13:6002)
ld a, [wcf64]
maskbits NUM_STAT_PAGES +- 1
maskbits NUM_STAT_PAGES
dec a
ld hl, .Jumptable
rst JumpTable

View File

@ -552,7 +552,7 @@ TradeAnim_GetTrademonSFX: ; 29277
; 29281
TradeAnim_TubeAnimJumptable: ; 29281
maskbits TRADEANIMJUMPTABLE_LENGTH +- 1
maskbits TRADEANIMJUMPTABLE_LENGTH
ld e, a
ld d, 0
ld hl, .Jumptable

View File

@ -835,7 +835,7 @@ INCBIN "gfx/unown_puzzle/tile_borders.2bpp"
LoadUnownPuzzlePiecesGFX: ; e17a3
ld a, [ScriptVar]
maskbits NUM_UNOWN_PUZZLES +- 1
maskbits NUM_UNOWN_PUZZLES
ld e, a
ld d, 0
ld hl, .LZPointers

View File

@ -739,11 +739,11 @@ JumpRoamMons: ; 2a394
JumpRoamMon: ; 2a3cd
.loop
ld hl, RoamMaps
.innerloop1 ; This loop is completely unnecessary.
call Random ; Choose a random number.
maskbits $10 - 1 ; Mask the number to limit it between 0 and 15.
cp $10 ; If the number is not less than 16, try again.
jr nc, .innerloop1 ; I'm sure you can guess why this check is bogus.
.innerloop1 ; This loop happens to be unnecessary.
call Random ; Choose a random number.
maskbits NUM_ROAMMON_MAPS ; Mask the number to limit it between 0 and 15.
cp NUM_ROAMMON_MAPS ; If the number is not less than 16, try again.
jr nc, .innerloop1 ; I'm sure you can guess why this check is bogus.
inc a
ld b, a
.innerloop2 ; Loop to get hl to the address of the chosen roam map.

View File

@ -48,17 +48,17 @@ GetClock:: ; 5b7
ld [hl], RTC_S
ld a, [de]
maskbits 60 - 1
maskbits 60
ld [hRTCSeconds], a
ld [hl], RTC_M
ld a, [de]
maskbits 60 - 1
maskbits 60
ld [hRTCMinutes], a
ld [hl], RTC_H
ld a, [de]
maskbits 24 - 1
maskbits 24
ld [hRTCHours], a
ld [hl], RTC_DL

View File

@ -37,17 +37,17 @@ ENDM
maskbits: MACRO
; masks just enough bits to cover the argument
; e.g. "maskbits %00010100" becomes "and %00011111"
; e.g. "maskbits 26" becomes "and %00011111" (since 26 - 1 = %00011001)
; example usage in rejection sampling:
; .loop
; call Random
; maskbits 30 +- 1
; maskbits 30
; cp 30
; jr nc, .loop
x = 1
rept 8
if x < (\1)
x = (x + 1) * 2 +- 1
if x + 1 < (\1)
x = x << 1 | 1
endc
endr
and x

View File

@ -1072,7 +1072,7 @@ Function106464:: ; 106464
Function10649b: ; 10649b
ld a, [TextBoxFrame]
maskbits NUM_FRAMES +- 1
maskbits NUM_FRAMES
ld bc, 6 * LEN_1BPP_TILE
ld hl, Frames
call AddNTimes