summaryrefslogtreecommitdiff
path: root/src/home/effect_commands.asm
blob: 9446d33b8d92da4edb965428c4c65a0098a96281 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
; Checks if the command type at a is one of the commands of the attack or
; card effect currently in use, and executes its associated function if so.
; input:
   ; a = command type to check
   ; [wLoadedAttackEffectCommands] = pointer to list of commands of current attack or trainer card
TryExecuteEffectCommandFunction:
	push af
	; grab pointer to command list from wLoadedAttackEffectCommands
	ld hl, wLoadedAttackEffectCommands
	ld a, [hli]
	ld h, [hl]
	ld l, a
	pop af
	call CheckMatchingCommand
	jr nc, .execute_function
	; return if no matching command was found
	or a
	ret

.execute_function
	; execute the function at [wEffectFunctionsBank]:hl
	ldh a, [hBankROM]
	push af
	ld a, [wEffectFunctionsBank]
	call BankswitchROM
	or a
	call CallHL
	push af
	; restore original bank and return
	pop bc
	pop af
	call BankswitchROM
	push bc
	pop af
	ret

; input:
  ; a = command type to check
  ; hl = list of commands of current attack or trainer card
; return nc if command type matching a is found, carry otherwise
CheckMatchingCommand:
	ld c, a
	ld a, l
	or h
	jr nz, .not_null_pointer
	; return carry if pointer is NULL
	scf
	ret

.not_null_pointer
	ldh a, [hBankROM]
	push af
	ld a, BANK(EffectCommands)
	call BankswitchROM
	; store the bank number of command functions ($b) in wEffectFunctionsBank
	ld a, BANK("Effect Functions")
	ld [wEffectFunctionsBank], a
.check_command_loop
	ld a, [hli]
	or a
	jr z, .no_more_commands
	cp c
	jr z, .matching_command_found
	; skip function pointer for this command and move to the next one
	inc hl
	inc hl
	jr .check_command_loop

.matching_command_found
	; load function pointer for this command
	ld a, [hli]
	ld h, [hl]
	ld l, a
	; restore bank and return nc
	pop af
	call BankswitchROM
	or a
	ret

.no_more_commands
	; restore bank and return c
	pop af
	call BankswitchROM
	scf
	ret