shithub: pokered

Download patch

ref: 346ce9454aac5ed9bd000d637f146b31f5a6f29d
parent: 715496bf351a4141fb8a47977ef49c956857d26e
author: Rangi <remy.oukaour+rangi42@gmail.com>
date: Sun May 23 18:09:02 EDT 2021

Use macros for bit arrays

--- a/constants/item_constants.asm
+++ b/constants/item_constants.asm
@@ -3,7 +3,7 @@
 ; - ItemNames (see data/items/names.asm)
 ; - ItemPrices (see data/items/prices.asm)
 ; - TechnicalMachinePrices (see data/items/tm_prices.asm)
-; - KeyItemBitfield (see data/items/key_items.asm)
+; - KeyItemFlags (see data/items/key_items.asm)
 ; - ItemUsePtrTable (see engine/items/item_effects.asm)
 	const_def
 	const NO_ITEM       ; $00
--- a/data/items/key_items.asm
+++ b/data/items/key_items.asm
@@ -1,105 +1,86 @@
-key_item_bits: MACRO
-_bit = 0
-_byte = 0
-REPT _NARG
-_byte = _byte | ((\1) << _bit)
-_bit = _bit + 1
-IF _bit == 8
-	db _byte
-_byte = 0
-_bit = 0
-ENDC
-SHIFT
-ENDR
-IF _bit > 0
-	db _byte
-ENDC
-ENDM
-
-KeyItemBitfield:
-	table_width 1, KeyItemBitfield
-	key_item_bits \
-	FALSE, \ ; MASTER_BALL
-	FALSE, \ ; ULTRA_BALL
-	FALSE, \ ; GREAT_BALL
-	FALSE, \ ; POKE_BALL
-	TRUE,  \ ; TOWN_MAP
-	TRUE,  \ ; BICYCLE
-	TRUE,  \ ; SURFBOARD
-	TRUE,  \ ; SAFARI_BALL
-	TRUE,  \ ; POKEDEX
-	FALSE, \ ; MOON_STONE
-	FALSE, \ ; ANTIDOTE
-	FALSE, \ ; BURN_HEAL
-	FALSE, \ ; ICE_HEAL
-	FALSE, \ ; AWAKENING
-	FALSE, \ ; PARLYZ_HEAL
-	FALSE, \ ; FULL_RESTORE
-	FALSE, \ ; MAX_POTION
-	FALSE, \ ; HYPER_POTION
-	FALSE, \ ; SUPER_POTION
-	FALSE, \ ; POTION
-	TRUE,  \ ; BOULDERBADGE
-	TRUE,  \ ; CASCADEBADGE
-	TRUE,  \ ; THUNDERBADGE
-	TRUE,  \ ; RAINBOWBADGE
-	TRUE,  \ ; SOULBADGE
-	TRUE,  \ ; MARSHBADGE
-	TRUE,  \ ; VOLCANOBADGE
-	TRUE,  \ ; EARTHBADGE
-	FALSE, \ ; ESCAPE_ROPE
-	FALSE, \ ; REPEL
-	TRUE,  \ ; OLD_AMBER
-	FALSE, \ ; FIRE_STONE
-	FALSE, \ ; THUNDER_STONE
-	FALSE, \ ; WATER_STONE
-	FALSE, \ ; HP_UP
-	FALSE, \ ; PROTEIN
-	FALSE, \ ; IRON
-	FALSE, \ ; CARBOS
-	FALSE, \ ; CALCIUM
-	FALSE, \ ; RARE_CANDY
-	TRUE,  \ ; DOME_FOSSIL
-	TRUE,  \ ; HELIX_FOSSIL
-	TRUE,  \ ; SECRET_KEY
-	TRUE,  \ ; UNUSED_ITEM
-	TRUE,  \ ; BIKE_VOUCHER
-	FALSE, \ ; X_ACCURACY
-	FALSE, \ ; LEAF_STONE
-	TRUE,  \ ; CARD_KEY
-	FALSE, \ ; NUGGET
-	FALSE, \ ; PP_UP_2
-	FALSE, \ ; POKE_DOLL
-	FALSE, \ ; FULL_HEAL
-	FALSE, \ ; REVIVE
-	FALSE, \ ; MAX_REVIVE
-	FALSE, \ ; GUARD_SPEC
-	FALSE, \ ; SUPER_REPEL
-	FALSE, \ ; MAX_REPEL
-	FALSE, \ ; DIRE_HIT
-	FALSE, \ ; COIN
-	FALSE, \ ; FRESH_WATER
-	FALSE, \ ; SODA_POP
-	FALSE, \ ; LEMONADE
-	TRUE,  \ ; S_S_TICKET
-	TRUE,  \ ; GOLD_TEETH
-	FALSE, \ ; X_ATTACK
-	FALSE, \ ; X_DEFEND
-	FALSE, \ ; X_SPEED
-	FALSE, \ ; X_SPECIAL
-	TRUE,  \ ; COIN_CASE
-	TRUE,  \ ; OAKS_PARCEL
-	TRUE,  \ ; ITEMFINDER
-	TRUE,  \ ; SILPH_SCOPE
-	TRUE,  \ ; POKE_FLUTE
-	TRUE,  \ ; LIFT_KEY
-	FALSE, \ ; EXP_ALL
-	TRUE,  \ ; OLD_ROD
-	TRUE,  \ ; GOOD_ROD
-	TRUE,  \ ; SUPER_ROD
-	FALSE, \ ; PP_UP
-	FALSE, \ ; ETHER
-	FALSE, \ ; MAX_ETHER
-	FALSE, \ ; ELIXER
-	FALSE    ; MAX_ELIXER
-	assert_table_length (NUM_ITEMS + 7) / 8
+KeyItemFlags:
+	bit_array KeyItemFlags
+	dbit FALSE ; MASTER_BALL
+	dbit FALSE ; ULTRA_BALL
+	dbit FALSE ; GREAT_BALL
+	dbit FALSE ; POKE_BALL
+	dbit TRUE  ; TOWN_MAP
+	dbit TRUE  ; BICYCLE
+	dbit TRUE  ; SURFBOARD
+	dbit TRUE  ; SAFARI_BALL
+	dbit TRUE  ; POKEDEX
+	dbit FALSE ; MOON_STONE
+	dbit FALSE ; ANTIDOTE
+	dbit FALSE ; BURN_HEAL
+	dbit FALSE ; ICE_HEAL
+	dbit FALSE ; AWAKENING
+	dbit FALSE ; PARLYZ_HEAL
+	dbit FALSE ; FULL_RESTORE
+	dbit FALSE ; MAX_POTION
+	dbit FALSE ; HYPER_POTION
+	dbit FALSE ; SUPER_POTION
+	dbit FALSE ; POTION
+	dbit TRUE  ; BOULDERBADGE
+	dbit TRUE  ; CASCADEBADGE
+	dbit TRUE  ; THUNDERBADGE
+	dbit TRUE  ; RAINBOWBADGE
+	dbit TRUE  ; SOULBADGE
+	dbit TRUE  ; MARSHBADGE
+	dbit TRUE  ; VOLCANOBADGE
+	dbit TRUE  ; EARTHBADGE
+	dbit FALSE ; ESCAPE_ROPE
+	dbit FALSE ; REPEL
+	dbit TRUE  ; OLD_AMBER
+	dbit FALSE ; FIRE_STONE
+	dbit FALSE ; THUNDER_STONE
+	dbit FALSE ; WATER_STONE
+	dbit FALSE ; HP_UP
+	dbit FALSE ; PROTEIN
+	dbit FALSE ; IRON
+	dbit FALSE ; CARBOS
+	dbit FALSE ; CALCIUM
+	dbit FALSE ; RARE_CANDY
+	dbit TRUE  ; DOME_FOSSIL
+	dbit TRUE  ; HELIX_FOSSIL
+	dbit TRUE  ; SECRET_KEY
+	dbit TRUE  ; UNUSED_ITEM
+	dbit TRUE  ; BIKE_VOUCHER
+	dbit FALSE ; X_ACCURACY
+	dbit FALSE ; LEAF_STONE
+	dbit TRUE  ; CARD_KEY
+	dbit FALSE ; NUGGET
+	dbit FALSE ; PP_UP_2
+	dbit FALSE ; POKE_DOLL
+	dbit FALSE ; FULL_HEAL
+	dbit FALSE ; REVIVE
+	dbit FALSE ; MAX_REVIVE
+	dbit FALSE ; GUARD_SPEC
+	dbit FALSE ; SUPER_REPEL
+	dbit FALSE ; MAX_REPEL
+	dbit FALSE ; DIRE_HIT
+	dbit FALSE ; COIN
+	dbit FALSE ; FRESH_WATER
+	dbit FALSE ; SODA_POP
+	dbit FALSE ; LEMONADE
+	dbit TRUE  ; S_S_TICKET
+	dbit TRUE  ; GOLD_TEETH
+	dbit FALSE ; X_ATTACK
+	dbit FALSE ; X_DEFEND
+	dbit FALSE ; X_SPEED
+	dbit FALSE ; X_SPECIAL
+	dbit TRUE  ; COIN_CASE
+	dbit TRUE  ; OAKS_PARCEL
+	dbit TRUE  ; ITEMFINDER
+	dbit TRUE  ; SILPH_SCOPE
+	dbit TRUE  ; POKE_FLUTE
+	dbit TRUE  ; LIFT_KEY
+	dbit FALSE ; EXP_ALL
+	dbit TRUE  ; OLD_ROD
+	dbit TRUE  ; GOOD_ROD
+	dbit TRUE  ; SUPER_ROD
+	dbit FALSE ; PP_UP
+	dbit FALSE ; ETHER
+	dbit FALSE ; MAX_ETHER
+	dbit FALSE ; ELIXER
+	dbit FALSE ; MAX_ELIXER
+	end_bit_array NUM_ITEMS
--- a/engine/items/item_effects.asm
+++ b/engine/items/item_effects.asm
@@ -2614,9 +2614,10 @@
 	jr nc, .checkIfItemIsHM
 ; if the item is not an HM or TM
 	push af
-	ld hl, KeyItemBitfield
+	ld hl, KeyItemFlags
 	ld de, wBuffer
 	ld bc, 15 ; only 11 bytes are actually used
+	assert 15 >= (NUM_ITEMS + 7) / 8
 	call CopyData
 	pop af
 	dec a
--- a/macros/asserts.asm
+++ b/macros/asserts.asm
@@ -73,6 +73,41 @@
 ENDC
 ENDM
 
+bit_array: MACRO
+CURRENT_BIT_ARRAY_VALUE = 0
+CURRENT_BIT_ARRAY_LENGTH = 0
+IF _NARG == 1
+REDEF CURRENT_BIT_ARRAY_START EQUS "\1"
+ELSE
+REDEF CURRENT_BIT_ARRAY_START EQUS "._bit_array\@"
+{CURRENT_BIT_ARRAY_START}:
+ENDC
+ENDM
+
+dbit: MACRO
+	ASSERT (\1) == 0 || (\1) == 1, "bits must be 0 or 1"
+CURRENT_BIT_ARRAY_VALUE = CURRENT_BIT_ARRAY_VALUE | ((\1) << (CURRENT_BIT_ARRAY_LENGTH % 8))
+CURRENT_BIT_ARRAY_LENGTH = CURRENT_BIT_ARRAY_LENGTH + 1
+IF CURRENT_BIT_ARRAY_LENGTH % 8 == 0
+	db CURRENT_BIT_ARRAY_VALUE
+CURRENT_BIT_ARRAY_VALUE = 0
+ENDC
+ENDM
+
+end_bit_array: MACRO
+IF CURRENT_BIT_ARRAY_LENGTH % 8
+	db CURRENT_BIT_ARRAY_VALUE
+ENDC
+IF _NARG == 1
+x = \1
+	ASSERT x == CURRENT_BIT_ARRAY_LENGTH, \
+		"{CURRENT_BIT_ARRAY_START}: expected {d:x} bits, got {d:CURRENT_BIT_ARRAY_LENGTH}"
+x = (x + 7) / 8
+	ASSERT x == @ - {CURRENT_BIT_ARRAY_START}, \
+		"{CURRENT_BIT_ARRAY_START}: expected {d:x} bytes"
+ENDC
+ENDM
+
 def_grass_wildmons: MACRO
 ;\1: encounter rate
 CURRENT_GRASS_WILDMONS_RATE = \1