forked from pret/pokeemerald
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcontest_link_util.c
344 lines (300 loc) · 10.3 KB
/
contest_link_util.c
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include "global.h"
#include "contest.h"
#include "contest_link.h"
#include "event_data.h"
#include "link.h"
#include "random.h"
#include "task.h"
/*
The functions in this file handle preliminary communication
for Emerald-only link contests. If the link contest has an RS
player linked, none of these functions are used.
The equivalent functions for RS-linked contests are spread
between contest_link.c and contest_util.c, and are suffixed RS
instead of Em
*/
static void Task_LinkContest_CommunicateMonsEm(u8);
static void Task_LinkContest_StartCommunicateRngEm(u8);
static void Task_LinkContest_CommunicateRngEm(u8);
static void Task_LinkContest_StartCommunicateLeaderIdsEm(u8);
static void Task_LinkContest_CommunicateLeaderIdsEm(u8);
static void Task_LinkContest_StartCommunicateCategoryEm(u8);
static void Task_LinkContest_CommunicateCategoryEm(u8);
static void Task_LinkContest_SetUpContestEm(u8);
static void Task_LinkContest_CommunicateAIMonsEm(u8);
static void Task_LinkContest_CalculateRound1Em(u8);
static void Task_LinkContest_CalculateTurnOrderEm(u8);
#define tCategory data[9]
void Task_LinkContest_StartCommunicationEm(u8 taskId)
{
int gameCleared;
switch (gTasks[taskId].tCategory)
{
case CONTEST_CATEGORY_COOL:
gHighestRibbonRank = GetMonData(&gPlayerParty[gContestMonPartyIndex], MON_DATA_COOL_RIBBON);
break;
case CONTEST_CATEGORY_BEAUTY:
gHighestRibbonRank = GetMonData(&gPlayerParty[gContestMonPartyIndex], MON_DATA_BEAUTY_RIBBON);
break;
case CONTEST_CATEGORY_CUTE:
gHighestRibbonRank = GetMonData(&gPlayerParty[gContestMonPartyIndex], MON_DATA_CUTE_RIBBON);
break;
case CONTEST_CATEGORY_SMART:
gHighestRibbonRank = GetMonData(&gPlayerParty[gContestMonPartyIndex], MON_DATA_SMART_RIBBON);
break;
case CONTEST_CATEGORY_TOUGH:
default:
gHighestRibbonRank = GetMonData(&gPlayerParty[gContestMonPartyIndex], MON_DATA_TOUGH_RIBBON);
break;
}
gContestMons[gContestPlayerMonIndex].highestRank = gHighestRibbonRank;
gameCleared = FlagGet(FLAG_SYS_GAME_CLEAR) > 0;
gContestMons[gContestPlayerMonIndex].gameCleared = gameCleared;
SetTaskFuncWithFollowupFunc(taskId, Task_LinkContest_CommunicateMonsEm, Task_LinkContest_StartCommunicateRngEm);
}
static void Task_LinkContest_StartCommunicateRngEm(u8 taskId)
{
SetTaskFuncWithFollowupFunc(taskId, Task_LinkContest_CommunicateRngEm, Task_LinkContest_StartCommunicateLeaderIdsEm);
}
static void Task_LinkContest_StartCommunicateLeaderIdsEm(u8 taskId)
{
SetTaskFuncWithFollowupFunc(taskId, Task_LinkContest_CommunicateLeaderIdsEm, Task_LinkContest_StartCommunicateCategoryEm);
}
static void Task_LinkContest_StartCommunicateCategoryEm(u8 taskId)
{
SetTaskFuncWithFollowupFunc(taskId, Task_LinkContest_CommunicateCategoryEm, Task_LinkContest_SetUpContestEm);
}
static void Task_LinkContest_SetUpContestEm(u8 taskId)
{
u8 i;
u8 rank;
int gameCleared;
u8 categories[CONTESTANT_COUNT];
u8 leaderIds[CONTESTANT_COUNT];
memset(categories, 0, sizeof(categories));
memset(leaderIds, 0, sizeof(leaderIds));
for (i = 0; i < gNumLinkContestPlayers; i++)
categories[i] = gTasks[taskId].data[i + 1];
// Ensure all players are doing the same category
for (i = 0; i < gNumLinkContestPlayers && categories[0] == categories[i]; i++)
;
if (i == gNumLinkContestPlayers)
gSpecialVar_0x8004 = FALSE; // Category choices the same
else
gSpecialVar_0x8004 = TRUE; // Category choices differ
for (i = 0; i < gNumLinkContestPlayers; i++)
leaderIds[i] = gTasks[taskId].data[i + 5];
// If < 4 players and player is leader, set AI contestants based on rank and game clear
if (gNumLinkContestPlayers != CONTESTANT_COUNT && GetMultiplayerId() == 0)
{
rank = gContestMons[0].highestRank;
for (i = 1; i < gNumLinkContestPlayers; i++)
{
if (rank < gContestMons[i].highestRank)
rank = gContestMons[i].highestRank;
}
if (rank)
rank--;
gameCleared = TRUE;
for (i = 0; i < gNumLinkContestPlayers; i++)
{
if (!gContestMons[i].gameCleared)
{
gameCleared = FALSE;
break;
}
}
SetLinkAIContestants(categories[0], rank, gameCleared);
}
// Assign link leader. After initial communication all players will read data only from them
gContestLinkLeaderIndex = LinkContest_GetLeaderIndex(leaderIds);
if (gNumLinkContestPlayers < CONTESTANT_COUNT)
SetTaskFuncWithFollowupFunc(taskId, Task_LinkContest_CommunicateAIMonsEm, Task_LinkContest_CalculateRound1Em);
else
gTasks[taskId].func = Task_LinkContest_CalculateRound1Em;
}
static void Task_LinkContest_CalculateRound1Em(u8 taskId)
{
CalculateRound1Points(gSpecialVar_ContestCategory);
SetTaskFuncWithFollowupFunc(taskId, Task_LinkContest_CommunicateRound1Points, Task_LinkContest_CalculateTurnOrderEm);
}
static void Task_LinkContest_CalculateTurnOrderEm(u8 taskId)
{
SortContestants(FALSE);
SetTaskFuncWithFollowupFunc(taskId, Task_LinkContest_CommunicateTurnOrder, Task_LinkContest_FinalizeConnection);
}
static void Task_LinkContest_CommunicateMonsEm(u8 taskId)
{
int i;
if (!LinkContest_TryLinkStandby(&gTasks[taskId].data[12]))
return;
switch (gTasks[taskId].data[0])
{
default:
gTasks[taskId].data[0] = 0;
gTasks[taskId].data[12] = 0;
SwitchTaskToFollowupFunc(taskId);
break;
case 0:
if (IsLinkTaskFinished())
{
if (LinkContest_SendBlock(&gContestMons[gContestPlayerMonIndex], sizeof(struct ContestPokemon)) == 1)
gTasks[taskId].data[0]++;
}
break;
case 1:
if (LinkContest_GetBlockReceivedFromAllPlayers())
{
for (i = 0; i < gNumLinkContestPlayers; i++)
{
memcpy(&gContestMons[i], gBlockRecvBuffer[i], sizeof(struct ContestPokemon));
StripPlayerAndMonNamesForLinkContest(&gContestMons[i], gLinkPlayers[i].language);
}
gTasks[taskId].data[0]++;
}
break;
}
}
static void Task_LinkContest_CommunicateRngEm(u8 taskId)
{
if (!LinkContest_TryLinkStandby(&gTasks[taskId].data[12]))
return;
switch (gTasks[taskId].data[0])
{
default:
gTasks[taskId].data[0] = 0;
gTasks[taskId].data[12] = 0;
SwitchTaskToFollowupFunc(taskId);
break;
case 0:
if (GetMultiplayerId() == 0)
{
// Only the leader sends the RNG seed
if (!IsLinkTaskFinished())
return;
if (LinkContest_SendBlock(&gRngValue, sizeof(gRngValue)) == 1)
gTasks[taskId].data[0]++;
}
else
{
// Other link members skip to waiting
gTasks[taskId].data[0]++;
}
break;
case 1:
// Wait to receive RNG data
if (LinkContest_GetBlockReceived(0))
{
memcpy(&gRngValue, gBlockRecvBuffer[0], sizeof(gRngValue));
memcpy(&gContestRngValue, gBlockRecvBuffer[0], sizeof(gContestRngValue));
gTasks[taskId].data[0]++;
}
break;
}
}
static void Task_LinkContest_CommunicateLeaderIdsEm(u8 taskId)
{
int i;
u16 data[CONTESTANT_COUNT];
u16 leaderId;
if (!LinkContest_TryLinkStandby(&gTasks[taskId].data[12]))
return;
switch (gTasks[taskId].data[0])
{
default:
gTasks[taskId].data[0] = 0;
gTasks[taskId].data[12] = 0;
SwitchTaskToFollowupFunc(taskId);
break;
case 0:
if (IsLinkTaskFinished())
{
leaderId = 0x6E;
if (LinkContest_SendBlock(&leaderId, sizeof(leaderId)) == 1)
gTasks[taskId].data[0]++;
}
break;
case 1:
if (LinkContest_GetBlockReceivedFromAllPlayers())
{
for (i = 0; i < gNumLinkContestPlayers; i++)
{
data[i] = gBlockRecvBuffer[i][0];
gTasks[taskId].data[i + 5] = data[i];
}
gTasks[taskId].data[0]++;
}
break;
}
}
static void Task_LinkContest_CommunicateCategoryEm(u8 taskId)
{
int i;
u16 data[CONTESTANT_COUNT];
u16 category;
if (!LinkContest_TryLinkStandby(&gTasks[taskId].data[12]))
return;
switch (gTasks[taskId].data[0])
{
default:
gTasks[taskId].data[0] = 0;
gTasks[taskId].data[12] = 0;
SwitchTaskToFollowupFunc(taskId);
break;
case 0:
if (IsLinkTaskFinished())
{
category = gTasks[taskId].tCategory;
if (LinkContest_SendBlock(&category, sizeof(category)) == 1)
gTasks[taskId].data[0]++;
}
break;
case 1:
if (LinkContest_GetBlockReceivedFromAllPlayers())
{
for (i = 0; i < gNumLinkContestPlayers; i++)
{
data[i] = gBlockRecvBuffer[i][0];
gTasks[taskId].data[i + 1] = data[i];
}
gTasks[taskId].data[0]++;
}
break;
}
}
static void Task_LinkContest_CommunicateAIMonsEm(u8 taskId)
{
int i;
if (!LinkContest_TryLinkStandby(&gTasks[taskId].data[12]))
return;
switch (gTasks[taskId].data[0])
{
default:
gTasks[taskId].data[0] = 0;
gTasks[taskId].data[12] = 0;
SwitchTaskToFollowupFunc(taskId);
break;
case 0:
if (GetMultiplayerId() == 0)
{
if (!IsLinkTaskFinished())
return;
if (LinkContest_SendBlock(&gContestMons[gNumLinkContestPlayers], (CONTESTANT_COUNT - gNumLinkContestPlayers) * sizeof(struct ContestPokemon)) == 1)
gTasks[taskId].data[0]++;
}
else
{
gTasks[taskId].data[0]++;
}
break;
case 1:
if (LinkContest_GetBlockReceived(0))
{
memcpy(&gContestMons[gNumLinkContestPlayers], gBlockRecvBuffer[0], (CONTESTANT_COUNT - gNumLinkContestPlayers) * sizeof(struct ContestPokemon));
for (i = gNumLinkContestPlayers; i < CONTESTANT_COUNT; i++)
StripPlayerAndMonNamesForLinkContest(&gContestMons[i], gLinkPlayers[0].language);
gTasks[taskId].data[0]++;
}
break;
}
}