Make Sandstorm raise the Special Defense of Rock type Pokémon by 50% - pret/pokecrystal GitHub Wiki

From generation 4 onwards, the Sandstorm weather raises the Special Defense of Rock-type Pokémon by 50%, so in this simple tutorial we'll implement this feature into Pokémon Crystal.

Contents

  1. Create the new function
  2. Call the function while getting the damage stats

1. Create the new function

In engine/battle/effect_commands.asm, create a new function called SandstormSpDefBoost:

+SandstormSpDefBoost: 
+; First, check if Sandstorm is active.
+	ld a, [wBattleWeather]
+	cp WEATHER_SANDSTORM
+	ret nz
+
+; Then, check the opponent's types.
+	ld hl, wEnemyMonType1
+	ldh a, [hBattleTurn]
+	and a
+	jr z, .ok
+	ld hl, wBattleMonType1
+.ok
+	ld a, [hli]
+	cp ROCK
+	jr z, .start_boost
+	ld a, [hl]
+	cp ROCK
+	ret nz
+
+.start_boost
+	ld h, b
+	ld l, c
+	srl b
+	rr c
+	add hl, bc
+	ld b, h
+	ld c, l
+	ret

You might've noticed that we're using the opponent's types instead of the user's to apply the boost. This is because we're gonna make the check while the game is getting the damage stats, i.e., when the user is attacking the opponent.

2. Call the function while getting the damage stats

In the same file, call the newly created function in both PlayerAttackDamage and EnemyAttackDamage:

PlayerAttackDamage:
	...
.special
	ld hl, wEnemyMonSpclDef
	ld a, [hli]
	ld b, a
	ld c, [hl]

+	call SandstormSpDefBoost
	...
EnemyAttackDamage:
	...
.special
	ld hl, wBattleMonSpclDef
	ld a, [hli]
	ld b, a
	ld c, [hl]

+	call SandstormSpDefBoost
	...

NOTE: It's highly recommended that you also apply this bugfix where the (Special) Defense could overflow if it was 1024 or higher.

And that's it! Now the game will try to apply the boost while getting the Special Defense of the opponent by first checking the weather and then its types. If the check is successful, the opponent will get the 50% boost from the Sandstorm.