summaryrefslogtreecommitdiff
path: root/engine/overworld/cable_club_npc.asm
blob: 8f27975953bf1d670293389d60d0bf1af3ea6291 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
CableClubNPC: ; 7035 (1:7035)
	ld hl, CableClubNPCWelcomeText
	call PrintText
	call Func_154a
	jr nz, .asm_7048
	CheckEvent EVENT_GOT_POKEDEX
	jp nz, .receivedPokedex
; if the player hasn't received the pokedex
.asm_7048
	ld c, 60
	call DelayFrames
	ld hl, CableClubNPCMakingPreparationsText
	call PrintText
	jp .didNotConnect
.receivedPokedex
	ld a, $1
	ld [wMenuJoypadPollCount], a
	ld a, 90
	ld [wLinkTimeoutCounter], a
.establishConnectionLoop
	ld a, [hSerialConnectionStatus]
	cp USING_INTERNAL_CLOCK
	jr z, .establishedConnection
	cp USING_EXTERNAL_CLOCK
	jr z, .establishedConnection
	ld a, CONNECTION_NOT_ESTABLISHED
	ld [hSerialConnectionStatus], a
	ld a, ESTABLISH_CONNECTION_WITH_EXTERNAL_CLOCK
	ld [rSB], a
	xor a
	ld [hSerialReceiveData], a
	ld a, START_TRANSFER_EXTERNAL_CLOCK
	ld [rSC], a
	ld a, [wLinkTimeoutCounter]
	dec a
	ld [wLinkTimeoutCounter], a
	jr z, .failedToEstablishConnection
	ld a, ESTABLISH_CONNECTION_WITH_INTERNAL_CLOCK
	ld [rSB], a
	ld a, START_TRANSFER_INTERNAL_CLOCK
	ld [rSC], a
	call DelayFrame
	jr .establishConnectionLoop
.establishedConnection
	call Serial_SendZeroByte
	call DelayFrame
	call Serial_SendZeroByte
	ld c, 50
	call DelayFrames
	ld hl, CableClubNPCPleaseApplyHereHaveToSaveText
	call PrintText
	xor a
	ld [wMenuJoypadPollCount], a
	call YesNoChoice
	ld a, $1
	ld [wMenuJoypadPollCount], a
	ld a, [wCurrentMenuItem]
	and a
	jr nz, .choseNo
	callab SaveSAVtoSRAM
	call WaitForSoundToFinish
	ld a, SFX_SAVE
	call PlaySoundWaitForCurrent
	ld hl, CableClubNPCPleaseWaitText
	call PrintText
	ld hl, wUnknownSerialCounter
	ld a, $3
	ld [hli], a
	xor a
	ld [hl], a
	ld [hSerialReceivedNewData], a
	ld [wSerialExchangeNybbleSendData], a
	call Serial_SyncAndExchangeNybble
	ld hl, wUnknownSerialCounter
	ld a, [hli]
	inc a
	jr nz, .connected
	ld a, [hl]
	inc a
	jr nz, .connected
	ld b, 10
.syncLoop
	call DelayFrame
	call Serial_SendZeroByte
	dec b
	jr nz, .syncLoop
	call CloseLinkConnection
	ld hl, CableClubNPCLinkClosedBecauseOfInactivityText
	call PrintText
	jr .didNotConnect
.failedToEstablishConnection
	ld hl, CableClubNPCAreaReservedFor2FriendsLinkedByCableText
	call PrintText
	jr .didNotConnect
.choseNo
	call CloseLinkConnection
	ld hl, CableClubNPCPleaseComeAgainText
	call PrintText
.didNotConnect
	xor a
	ld hl, wUnknownSerialCounter
	ld [hli], a
	ld [hl], a
	ld hl, wd72e
	res 6, [hl]
	xor a
	ld [wMenuJoypadPollCount], a
	ret
.connected
	xor a
	ld [hld], a
	ld [hl], a
	ld a, [wLetterPrintingDelayFlags]
	push af
	callab LinkMenu
	pop af
	ld [wLetterPrintingDelayFlags], a
	ret

; seems to be similar of Serial_SyncAndExchangeNybble
Serial_SyncAndExchangeNybbleDouble: ; 7131 (1:7131)
	ld a, $ff
	ld [wSerialExchangeNybbleReceiveData], a
.loop
	call Serial_ExchangeNybble
	call DelayFrame
	push hl
	ld hl, wUnknownSerialCounter + 1
	dec [hl]
	jr nz, .next
	dec hl
	dec [hl]
	jr nz, .next
	pop hl
	jr .setUnknownSerialCounterToFFFF
.next
	pop hl
	ld a, [wSerialExchangeNybbleReceiveData]
	inc a
	jr z, .loop
	call DelayFrame
	ld a, $ff
	ld [wSerialExchangeNybbleReceiveData], a
	call Serial_ExchangeNybble
	ld a, [wSerialExchangeNybbleReceiveData]
	inc a
	jr z, .loop
	ld b, 10
.syncLoop1
	call DelayFrame
	call Serial_ExchangeNybble
	dec b
	jr nz, .syncLoop1
	ld b, 10
.syncLoop2
	call DelayFrame
	call Serial_SendZeroByte
	dec b
	jr nz, .syncLoop2
	ld a, [wSerialExchangeNybbleReceiveData]
	ld [wSerialSyncAndExchangeNybbleReceiveData], a
	ret
.setUnknownSerialCounterToFFFF
	ld a, $ff
	ld [wUnknownSerialCounter], a
	ld [wUnknownSerialCounter + 1], a
	ret
	
CableClubNPCAreaReservedFor2FriendsLinkedByCableText: ; 7188 (1:7188)
	TX_FAR _CableClubNPCAreaReservedFor2FriendsLinkedByCableText
	db "@"

CableClubNPCWelcomeText: ; 718d (1:718d)
	TX_FAR _CableClubNPCWelcomeText
	db "@"

CableClubNPCPleaseApplyHereHaveToSaveText: ; 7192 (1:7192)
	TX_FAR _CableClubNPCPleaseApplyHereHaveToSaveText
	db "@"

CableClubNPCPleaseWaitText: ; 7197 (1:7197)
	TX_FAR _CableClubNPCPleaseWaitText
	db $a, "@"

CableClubNPCLinkClosedBecauseOfInactivityText: ; 719d (1:719d)
	TX_FAR _CableClubNPCLinkClosedBecauseOfInactivityText
	db "@"

CableClubNPCPleaseComeAgainText: ; 71a2 (1:71a2)
	TX_FAR _CableClubNPCPleaseComeAgainText
	db "@"

CableClubNPCMakingPreparationsText: ; 71a7 (1:71a7)
	TX_FAR _CableClubNPCMakingPreparationsText
	db "@"

CloseLinkConnection: ; 71ac (1:71ac)
	call Delay3
	ld a, CONNECTION_NOT_ESTABLISHED
	ld [hSerialConnectionStatus], a
	ld a, ESTABLISH_CONNECTION_WITH_EXTERNAL_CLOCK
	ld [rSB], a
	xor a
	ld [hSerialReceiveData], a
	ld a, START_TRANSFER_EXTERNAL_CLOCK
	ld [rSC], a
	ret