CalcMonAbility::
; species in [wCurSpecies]
; dvs pointer in hl

; If we're in a link battle with a non-Prism game, disable abilities.
	push bc
	push de
	call GetMonAbilities ; preserves hl
	; Determine the parity of the DVs
	; Use the DVs to determine which of the two abilities to use.
	ld b, 2
	call CountSetBits

CalcMonAbility_Done:
	rra
	ld hl, wBaseAbilities ; First 2 bytes of the 4-byte padding
	jr nc, .load
	inc hl
.load
	ld a, [hl]
	pop de
	pop bc
	ret

CalcTrainerMonAbility::
; Trainer 'mon's ability is also depends on class, ID and party position
	push bc
	call GetMonAbilities ; preserves hl
	ld a, [hli]
	ld b, a
	ld a, [wOtherTrainerClass]
	add b
	ld b, a
	ld a, [hl]
	ld c, a
	ld a, [wOtherTrainerID]
	add c
	xor b
	ld b, a
	bit 6, b
	jr z, .noswap
	swap a
.noswap
	bit 7, b
	jr z, .nocpl
	cpl
.nocpl
	ld c, a
	ld a, [wCurOTMon]
	and a
	jr z, .skiprrc
	ld b, a
.rrcloop
	rrc c
	dec b
	jr nz, .rrcloop
.skiprrc
	ld a, c
	jr CalcMonAbility_Done
	
GetMonAbilities:
	push hl
	ld hl, BaseData + (wBaseAbilities - wCurBaseData)
	ld bc, BaseData1 - BaseData0
	ld a, [wCurSpecies]
	rst AddNTimes
	ld a, [hli]
	ld [wBaseAbility1], a
	ld a, [hl]
	ld [wBaseAbility2], a
	pop hl
	ret

INCLUDE "battle/ability_names.asm"
INCLUDE "battle/pickup.asm"
