summaryrefslogtreecommitdiff
path: root/engine/battle/move_effects/teleport.asm
blob: a97ac8a194fa18cfb818b089d1c92dae67f7d530 (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
86
87
88
89
90
91
92
93
94
95
96
97
BattleCommand_Teleport:
; teleport

	ld a, [wBattleType]
	cp BATTLETYPE_SHINY
	jr z, .failed
	cp BATTLETYPE_TRAP
	jr z, .failed

	ld a, BATTLE_VARS_SUBSTATUS5_OPP
	call GetBattleVar
	bit SUBSTATUS_CANT_RUN, a
	jr nz, .failed
	ldh a, [hBattleTurn]
	and a
	jr nz, .enemy_turn

	; Can't teleport from a trainer battle
	ld a, [wBattleMode]
	dec a
	jr nz, .failed
	; b = player level
	ld a, [wCurPartyLevel]
	ld b, a
	; If player level >= enemy level, Teleport will succeed
	ld a, [wBattleMonLevel]
	cp b
	jr nc, .run_away
	; c = player level + enemy level + 1
	add b
	ld c, a
	inc c
	; Generate a number less than c
.loop_player
	call BattleRandom
	cp c
	jr nc, .loop_player
	; b = enemy level / 4
	srl b
	srl b
	; If the random number >= enemy level / 4, Teleport will succeed
	cp b
	jr nc, .run_away

.failed
	call AnimateFailedMove
	jp PrintButItFailed

.enemy_turn
	; Can't teleport from a trainer battle
	ld a, [wBattleMode]
	dec a
	jr nz, .failed
	; b = enemy level
	ld a, [wBattleMonLevel]
	ld b, a
	; If enemy level >= player level, Teleport will succeed
	ld a, [wCurPartyLevel]
	cp b
	jr nc, .run_away
	; c = enemy level + player level + 1
	add b
	ld c, a
	inc c
	; Generate a number less than c
.loop_enemy
	call BattleRandom
	cp c
	jr nc, .loop_enemy
	; b = player level / 4
	srl b
	srl b
	; This should be "jr c, .failed"
	; As written, it makes enemy use of Teleport always succeed if able
	cp b
	jr nc, .run_away

.run_away
	call UpdateBattleMonInParty
	xor a
	ld [wNumHits], a
	inc a
	ld [wForcedSwitch], a
	; set battle draw
	inc a
	ld [wBattleResult], a
	ld a, 1
	ld [wBattleAnimParam], a
	call BattleCommand_LowerSub
	call LoadMoveAnim
	ld c, 20
	call DelayFrames
	ld a, DRAW
	ld [wBattleResult], a

	ld hl, FledFromBattleText
	jp StdBattleTextbox