Free MORE some space in the Home BANK - pret/pokered GitHub Wiki

Follow this tutorial too: Free some space in the Home BANK

Code by Rangi42 (in Red Star/Blue Star).

home.asm:

...
INCLUDE "home/timer.asm"
INCLUDE "home/audio.asm"
INCLUDE "home/update_sprites.asm"

-INCLUDE "data/items/marts.asm"

INCLUDE "home/overworld_text.asm"
INCLUDE "home/uncompress.asm"
INCLUDE "home/reset_player_sprite.asm"
...

scripts\ViridianMart.asm:

...
ViridianMartText3:
    text_far _ViridianMartText3
    text_end

+ViridianClerkText:
+   script_mart POKE_BALL, ANTIDOTE, PARLYZ_HEAL, BURN_HEAL

scripts\PewterMart.asm:

...
PewterMartText3:
...
.Text
	text_far _PewterMartText3
	text_end

+PewterClerkText:
+	script_mart POKE_BALL, POTION, ESCAPE_ROPE, ANTIDOTE, BURN_HEAL, AWAKENING, PARLYZ_HEAL

scripts\CeruleanMart.asm:

...
CeruleanMartText3:
	text_far _CeruleanMartText3
	text_end

+CeruleanClerkText:
+	script_mart POKE_BALL, POTION, REPEL, ANTIDOTE, BURN_HEAL, AWAKENING, PARLYZ_HEAL

scripts\VermilionMart.asm:

...
VermilionMartText3:
	text_far _VermilionMartText3
	text_end

+VermilionClerkText:
+	script_mart POKE_BALL, SUPER_POTION, ICE_HEAL, AWAKENING, PARLYZ_HEAL, REPEL

scripts\LavenderMart.asm:

...
LavenderMartText3:
...
.NuggetText
	text_far _LavenderMartNuggetText
	text_end

+LavenderClerkText:
+	script_mart GREAT_BALL, SUPER_POTION, REVIVE, ESCAPE_ROPE, SUPER_REPEL, ANTIDOTE, BURN_HEAL, ICE_HEAL, PARLYZ_HEAL

scripts\CeladonMart2F.asm:

...
CeladonMart2Text5:
	text_far _CeladonMart2Text5
	text_end

+CeladonMart2Clerk1Text:
+	script_mart GREAT_BALL, SUPER_POTION, REVIVE, SUPER_REPEL, ANTIDOTE, BURN_HEAL, ICE_HEAL, AWAKENING, PARLYZ_HEAL

+CeladonMart2Clerk2Text:
+	script_mart TM_DOUBLE_TEAM, TM_REFLECT, TM_RAZOR_WIND, TM_HORN_DRILL, TM_EGG_BOMB, TM_MEGA_PUNCH, TM_MEGA_KICK, TM_TAKE_DOWN, TM_SUBMISSION

scripts\CeladonMart4F.asm:

...
CeladonMart4Text4:
	text_far _CeladonMart4Text4
	text_end

+CeladonMart4ClerkText:
+	script_mart POKE_DOLL, FIRE_STONE, THUNDER_STONE, WATER_STONE, LEAF_STONE

scripts\CeladonMart5F.asm:

...
CeladonMart5Text5:
	text_far _CeladonMart5Text5
	text_end

+CeladonMart5Clerk1Text:
+	script_mart X_ACCURACY, GUARD_SPEC, DIRE_HIT, X_ATTACK, X_DEFEND, X_SPEED, X_SPECIAL

+CeladonMart5Clerk2Text:
+	script_mart HP_UP, PROTEIN, IRON, CARBOS, CALCIUM

scripts\FuchsiaMart.asm:

...
FuchsiaMartText3:
	text_far _FuchsiaMartText3
	text_end

+FuchsiaClerkText:
+	script_mart ULTRA_BALL, GREAT_BALL, SUPER_POTION, REVIVE, FULL_HEAL, SUPER_REPEL

scripts\CinnabarMart.asm:

...
CinnabarMartText3:
	text_far _CinnabarMartText3
	text_end

+CinnabarClerkText:
+	script_mart ULTRA_BALL, GREAT_BALL, HYPER_POTION, MAX_REPEL, ESCAPE_ROPE, FULL_HEAL, REVIVE

scripts\SaffronMart.asm:

...
SaffronMartText3:
	text_far _SaffronMartText3
	text_end

+SaffronClerkText:
+	script_mart GREAT_BALL, HYPER_POTION, MAX_REPEL, ESCAPE_ROPE, FULL_HEAL, REVIVE

scripts\IndigoPlateauLobby.asm:

...
IndigoTradeNurseText:
	script_cable_club_receptionist

+IndigoClerkText:
+	script_mart ULTRA_BALL, GREAT_BALL, FULL_RESTORE, MAX_POTION, FULL_HEAL, REVIVE, MAX_REPEL

Free even MORE space by moving CalcStats

By ZetaPhoenix with the help of Vortiene and Engezerstorung.

home\move_mon.asm

CalcStats

...
; calculates all 5 stats of current mon and writes them to [de]
CalcStats::
+	homecall _CalcStats
-	ld c, $0
-.statsLoop
-	inc c
-	call CalcStat
-	ldh a, [hMultiplicand+1]
-	ld [de], a
-	inc de
-	ldh a, [hMultiplicand+2]
-	ld [de], a
-	inc de
-	ld a, c
-	cp NUM_STATS
-	jr nz, .statsLoop
	ret

; calculates stat c of current mon
; c: stat to calc (HP=1,Atk=2,Def=3,Spd=4,Spc=5)
; b: consider stat exp?
; hl: base ptr to stat exp values ([hl + 2*c - 1] and [hl + 2*c])
CalcStat::
+	homecall _CalcStat
-	push hl
-	push de
-	push bc
-	ld a, b
-	ld d, a
-	push hl
-	ld hl, wMonHeader
-	ld b, $0
-	add hl, bc
-	ld a, [hl]          ; read base value of stat
-	ld e, a
-	pop hl
-	push hl
-	sla c
-	ld a, d
-	and a
-	jr z, .statExpDone  ; consider stat exp?
-	add hl, bc          ; skip to corresponding stat exp value
-.statExpLoop            ; calculates ceil(Sqrt(stat exp)) in b
-	xor a
-	ldh [hMultiplicand], a
-	ldh [hMultiplicand+1], a
-	inc b               ; increment current stat exp bonus
-	ld a, b
-	cp $ff
-	jr z, .statExpDone
-	ldh [hMultiplicand+2], a
-	ldh [hMultiplier], a
-	call Multiply
-	ld a, [hld]
-	ld d, a
-	ldh a, [hProduct + 3]
-	sub d
-	ld a, [hli]
-	ld d, a
-	ldh a, [hProduct + 2]
-	sbc d               ; test if (current stat exp bonus)^2 < stat exp
-	jr c, .statExpLoop
-.statExpDone
-	srl c
-	pop hl
-	push bc
-	ld bc, wPartyMon1DVs - (wPartyMon1HPExp - 1) ; also wEnemyMonDVs - wEnemyMonHP
-	add hl, bc
-	pop bc
-	ld a, c
-	cp $2
-	jr z, .getAttackIV
-	cp $3
-	jr z, .getDefenseIV
-	cp $4
-	jr z, .getSpeedIV
-	cp $5
-	jr z, .getSpecialIV
-.getHpIV
-	push bc
-	ld a, [hl]  ; Atk IV
-	swap a
-	and $1
-	sla a
-	sla a
-	sla a
-	ld b, a
-	ld a, [hli] ; Def IV
-	and $1
-	sla a
-	sla a
-	add b
-	ld b, a
-	ld a, [hl] ; Spd IV
-	swap a
-	and $1
-	sla a
-	add b
-	ld b, a
-	ld a, [hl] ; Spc IV
-	and $1
-	add b      ; HP IV: LSB of the other 4 IVs
-	pop bc
-	jr .calcStatFromIV
-.getAttackIV
-	ld a, [hl]
-	swap a
-	and $f
-	jr .calcStatFromIV
-.getDefenseIV
-	ld a, [hl]
-	and $f
-	jr .calcStatFromIV
-.getSpeedIV
-	inc hl
-	ld a, [hl]
-	swap a
-	and $f
-	jr .calcStatFromIV
-.getSpecialIV
-	inc hl
-	ld a, [hl]
-	and $f
-.calcStatFromIV
-	ld d, $0
-	add e
-	ld e, a
-	jr nc, .noCarry
-	inc d                     ; de = Base + IV
-.noCarry
-	sla e
-	rl d                      ; de = (Base + IV) * 2
-	srl b
-	srl b                     ; b = ceil(Sqrt(stat exp)) / 4
-	ld a, b
-	add e
-	jr nc, .noCarry2
-	inc d                     ; de = (Base + IV) * 2 + ceil(Sqrt(stat exp)) / 4
-.noCarry2
-	ldh [hMultiplicand+2], a
-	ld a, d
-	ldh [hMultiplicand+1], a
-	xor a
-	ldh [hMultiplicand], a
-	ld a, [wCurEnemyLevel]
-	ldh [hMultiplier], a
-	call Multiply            ; ((Base + IV) * 2 + ceil(Sqrt(stat exp)) / 4) * Level
-	ldh a, [hMultiplicand]
-	ldh [hDividend], a
-	ldh a, [hMultiplicand+1]
-	ldh [hDividend+1], a
-	ldh a, [hMultiplicand+2]
-	ldh [hDividend+2], a
-	ld a, $64
-	ldh [hDivisor], a
-	ld a, $3
-	ld b, a
-	call Divide             ; (((Base + IV) * 2 + ceil(Sqrt(stat exp)) / 4) * Level) / 100
-	ld a, c
-	cp $1
-	ld a, 5 ; + 5 for non-HP stat
-	jr nz, .notHPStat
-	ld a, [wCurEnemyLevel]
-	ld b, a
-	ldh a, [hMultiplicand+2]
-	add b
-	ldh [hMultiplicand+2], a
-	jr nc, .noCarry3
-	ldh a, [hMultiplicand+1]
-	inc a
-	ldh [hMultiplicand+1], a ; HP: (((Base + IV) * 2 + ceil(Sqrt(stat exp)) / 4) * Level) / 100 + Level
-.noCarry3
-	ld a, 10 ; +10 for HP stat
-.notHPStat
-	ld b, a
-	ldh a, [hMultiplicand+2]
-	add b
-	ldh [hMultiplicand+2], a
-	jr nc, .noCarry4
-	ldh a, [hMultiplicand+1]
-	inc a                    ; non-HP: (((Base + IV) * 2 + ceil(Sqrt(stat exp)) / 4) * Level) / 100 + 5
-	ldh [hMultiplicand+1], a ; HP: (((Base + IV) * 2 + ceil(Sqrt(stat exp)) / 4) * Level) / 100 + Level + 10
-.noCarry4
-	ldh a, [hMultiplicand+1] ; check for overflow (>999)
-	cp HIGH(MAX_STAT_VALUE) + 1
-	jr nc, .overflow
-	cp HIGH(MAX_STAT_VALUE)
-	jr c, .noOverflow
-	ldh a, [hMultiplicand+2]
-	cp LOW(MAX_STAT_VALUE) + 1
-	jr c, .noOverflow
-.overflow
-	ld a, HIGH(MAX_STAT_VALUE) ; overflow: cap at 999
-	ldh [hMultiplicand+1], a
-	ld a, LOW(MAX_STAT_VALUE)
-	ldh [hMultiplicand+2], a
-.noOverflow
-	pop bc
-	pop de
-	pop hl
	ret
...

Make a new file called "calc_stats.asm" in engine\pokemon

engine\pokemon\calc_stats.asm

_CalcStats::
	ld c, $0
.statsLoop
	inc c
	call _CalcStat
	ldh a, [hMultiplicand+1]
	ld [de], a
	inc de
	ldh a, [hMultiplicand+2]
	ld [de], a
	inc de
	ld a, c
	cp NUM_STATS
	jr nz, .statsLoop
	ret

_CalcStat::
	push hl
	push de
	push bc
	ld a, b
	ld d, a
	push hl
	ld hl, wMonHeader
	ld b, $0
	add hl, bc
	ld a, [hl]          ; read base value of stat
	ld e, a
	pop hl
	push hl
	sla c
	ld a, d
	and a
	jr z, .statExpDone  ; consider stat exp?
	add hl, bc          ; skip to corresponding stat exp value
.statExpLoop            ; calculates ceil(Sqrt(stat exp)) in b
	xor a
	ldh [hMultiplicand], a
	ldh [hMultiplicand+1], a
	inc b               ; increment current stat exp bonus
	ld a, b
	cp $ff
	jr z, .statExpDone
	ldh [hMultiplicand+2], a
	ldh [hMultiplier], a
	call Multiply
	ld a, [hld]
	ld d, a
	ldh a, [hProduct + 3]
	sub d
	ld a, [hli]
	ld d, a
	ldh a, [hProduct + 2]
	sbc d               ; test if (current stat exp bonus)^2 < stat exp
	jr c, .statExpLoop
.statExpDone
	srl c
	pop hl
	push bc
	ld bc, wPartyMon1DVs - (wPartyMon1HPExp - 1) ; also wEnemyMonDVs - wEnemyMonHP
	add hl, bc
	pop bc
	ld a, c
	cp $2
	jr z, .getAttackIV
	cp $3
	jr z, .getDefenseIV
	cp $4
	jr z, .getSpeedIV
	cp $5
	jr z, .getSpecialIV
.getHpIV
	push bc
	ld a, [hl]  ; Atk IV
	swap a
	and $1
	sla a
	sla a
	sla a
	ld b, a
	ld a, [hli] ; Def IV
	and $1
	sla a
	sla a
	add b
	ld b, a
	ld a, [hl] ; Spd IV
	swap a
	and $1
	sla a
	add b
	ld b, a
	ld a, [hl] ; Spc IV
	and $1
	add b      ; HP IV: LSB of the other 4 IVs
	pop bc
	jr .calcStatFromIV
.getAttackIV
	ld a, [hl]
	swap a
	and $f
	jr .calcStatFromIV
.getDefenseIV
	ld a, [hl]
	and $f
	jr .calcStatFromIV
.getSpeedIV
	inc hl
	ld a, [hl]
	swap a
	and $f
	jr .calcStatFromIV
.getSpecialIV
	inc hl
	ld a, [hl]
	and $f
.calcStatFromIV
	ld d, $0
	add e
	ld e, a
	jr nc, .noCarry
	inc d                     ; de = Base + IV
.noCarry
	sla e
	rl d                      ; de = (Base + IV) * 2
	srl b
	srl b                     ; b = ceil(Sqrt(stat exp)) / 4
	ld a, b
	add e
	jr nc, .noCarry2
	inc d                     ; de = (Base + IV) * 2 + ceil(Sqrt(stat exp)) / 4
.noCarry2
	ldh [hMultiplicand+2], a
	ld a, d
	ldh [hMultiplicand+1], a
	xor a
	ldh [hMultiplicand], a
	ld a, [wCurEnemyLevel]
	ldh [hMultiplier], a
	call Multiply            ; ((Base + IV) * 2 + ceil(Sqrt(stat exp)) / 4) * Level
	ldh a, [hMultiplicand]
	ldh [hDividend], a
	ldh a, [hMultiplicand+1]
	ldh [hDividend+1], a
	ldh a, [hMultiplicand+2]
	ldh [hDividend+2], a
	ld a, $64
	ldh [hDivisor], a
	ld a, $3
	ld b, a
	call Divide             ; (((Base + IV) * 2 + ceil(Sqrt(stat exp)) / 4) * Level) / 100
	ld a, c
	cp $1
	ld a, 5 ; + 5 for non-HP stat
	jr nz, .notHPStat
	ld a, [wCurEnemyLevel]
	ld b, a
	ldh a, [hMultiplicand+2]
	add b
	ldh [hMultiplicand+2], a
	jr nc, .noCarry3
	ldh a, [hMultiplicand+1]
	inc a
	ldh [hMultiplicand+1], a ; HP: (((Base + IV) * 2 + ceil(Sqrt(stat exp)) / 4) * Level) / 100 + Level
.noCarry3
	ld a, 10 ; +10 for HP stat
.notHPStat
	ld b, a
	ldh a, [hMultiplicand+2]
	add b
	ldh [hMultiplicand+2], a
	jr nc, .noCarry4
	ldh a, [hMultiplicand+1]
	inc a                    ; non-HP: (((Base + IV) * 2 + ceil(Sqrt(stat exp)) / 4) * Level) / 100 + 5
	ldh [hMultiplicand+1], a ; HP: (((Base + IV) * 2 + ceil(Sqrt(stat exp)) / 4) * Level) / 100 + Level + 10
.noCarry4
	ldh a, [hMultiplicand+1] ; check for overflow (>999)
	cp HIGH(MAX_STAT_VALUE) + 1
	jr nc, .overflow
	cp HIGH(MAX_STAT_VALUE)
	jr c, .noOverflow
	ldh a, [hMultiplicand+2]
	cp LOW(MAX_STAT_VALUE) + 1
	jr c, .noOverflow
.overflow
	ld a, HIGH(MAX_STAT_VALUE) ; overflow: cap at 999
	ldh [hMultiplicand+1], a
	ld a, LOW(MAX_STAT_VALUE)
	ldh [hMultiplicand+2], a
.noOverflow
	pop bc
	pop de
	pop hl
	ret

main.asm

SECTION "Vending Machine", ROMX

...
SECTION "Vending Machine", ROMX

INCLUDE "engine/events/vending_machine.asm"
+INCLUDE "engine/pokemon/calc_stats.asm"


SECTION "Itemfinder 2", ROMX
...

OPTIONAL: To make even more space we're gonna edit the Homecall MACRO.

macros/farcall.asm

MACRO homecall

...
MACRO homecall
	ldh a, [hLoadedROMBank]
	push af
	ld a, BANK(\1)
-	ldh [hLoadedROMBank], a
-	ld [MBC1RomBank], a
+	call SetCurBank
	call \1
	pop af
-	ldh [hLoadedROMBank], a
-	ld [MBC1RomBank], a
+	call SetCurBank
ENDM


MACRO homecall_sf ; homecall but save flags by popping into bc instead of af
	ldh a, [hLoadedROMBank]
	push af
	ld a, BANK(\1)
-	ldh [hLoadedROMBank], a
-	ld [MBC1RomBank], a
+	call SetCurBank
	call \1
	pop bc
	ld a, b
-	ldh [hLoadedROMBank], a
-	ld [MBC1RomBank], a
+	call SetCurBank
ENDM

Add this code at the end of home/bankswitch.asm

SetCurBank::
	ldh [hLoadedROMBank], a
	ld [MBC1RomBank], a
	ret