pokeemerald/src/contest_link_util.c

345 lines
10 KiB
C
Raw Normal View History

2019-01-03 18:54:22 +01:00
#include "global.h"
#include "contest.h"
2020-07-12 02:25:56 +02:00
#include "contest_link.h"
2019-01-03 18:54:22 +01:00
#include "event_data.h"
#include "link.h"
#include "random.h"
#include "task.h"
2020-08-13 09:09:47 +02:00
/*
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);
2019-01-03 18:54:22 +01:00
2020-07-12 02:25:56 +02:00
#define tCategory data[9]
2020-08-13 09:09:47 +02:00
void Task_LinkContest_StartCommunicationEm(u8 taskId)
2019-01-03 18:54:22 +01:00
{
int gameCleared;
2020-07-12 02:25:56 +02:00
switch (gTasks[taskId].tCategory)
2019-01-03 18:54:22 +01:00
{
2020-07-12 02:25:56 +02:00
case CONTEST_CATEGORY_COOL:
2019-01-03 18:54:22 +01:00
gHighestRibbonRank = GetMonData(&gPlayerParty[gContestMonPartyIndex], MON_DATA_COOL_RIBBON);
break;
2020-07-12 02:25:56 +02:00
case CONTEST_CATEGORY_BEAUTY:
2019-01-03 18:54:22 +01:00
gHighestRibbonRank = GetMonData(&gPlayerParty[gContestMonPartyIndex], MON_DATA_BEAUTY_RIBBON);
break;
2020-07-12 02:25:56 +02:00
case CONTEST_CATEGORY_CUTE:
2019-01-03 18:54:22 +01:00
gHighestRibbonRank = GetMonData(&gPlayerParty[gContestMonPartyIndex], MON_DATA_CUTE_RIBBON);
break;
2020-07-12 02:25:56 +02:00
case CONTEST_CATEGORY_SMART:
2019-01-03 18:54:22 +01:00
gHighestRibbonRank = GetMonData(&gPlayerParty[gContestMonPartyIndex], MON_DATA_SMART_RIBBON);
break;
2020-07-12 02:25:56 +02:00
case CONTEST_CATEGORY_TOUGH:
2019-01-03 18:54:22 +01:00
default:
gHighestRibbonRank = GetMonData(&gPlayerParty[gContestMonPartyIndex], MON_DATA_TOUGH_RIBBON);
break;
}
2020-07-12 02:25:56 +02:00
gContestMons[gContestPlayerMonIndex].highestRank = gHighestRibbonRank;
2019-01-03 18:54:22 +01:00
gameCleared = FlagGet(FLAG_SYS_GAME_CLEAR) > 0;
2020-07-12 02:25:56 +02:00
gContestMons[gContestPlayerMonIndex].gameCleared = gameCleared;
2020-08-13 09:09:47 +02:00
SetTaskFuncWithFollowupFunc(taskId, Task_LinkContest_CommunicateMonsEm, Task_LinkContest_StartCommunicateRngEm);
2019-01-03 18:54:22 +01:00
}
2020-08-13 09:09:47 +02:00
static void Task_LinkContest_StartCommunicateRngEm(u8 taskId)
2019-01-03 18:54:22 +01:00
{
2020-08-13 09:09:47 +02:00
SetTaskFuncWithFollowupFunc(taskId, Task_LinkContest_CommunicateRngEm, Task_LinkContest_StartCommunicateLeaderIdsEm);
2019-01-03 18:54:22 +01:00
}
2020-08-13 09:09:47 +02:00
static void Task_LinkContest_StartCommunicateLeaderIdsEm(u8 taskId)
2019-01-03 18:54:22 +01:00
{
2020-08-13 09:09:47 +02:00
SetTaskFuncWithFollowupFunc(taskId, Task_LinkContest_CommunicateLeaderIdsEm, Task_LinkContest_StartCommunicateCategoryEm);
2019-01-03 18:54:22 +01:00
}
2020-08-13 09:09:47 +02:00
static void Task_LinkContest_StartCommunicateCategoryEm(u8 taskId)
2019-01-03 18:54:22 +01:00
{
2020-08-13 09:09:47 +02:00
SetTaskFuncWithFollowupFunc(taskId, Task_LinkContest_CommunicateCategoryEm, Task_LinkContest_SetUpContestEm);
2019-01-03 18:54:22 +01:00
}
2020-08-13 09:09:47 +02:00
static void Task_LinkContest_SetUpContestEm(u8 taskId)
2019-01-03 18:54:22 +01:00
{
u8 i;
u8 rank;
int gameCleared;
2020-08-13 09:09:47 +02:00
u8 categories[CONTESTANT_COUNT];
u8 leaderIds[CONTESTANT_COUNT];
2019-01-03 18:54:22 +01:00
2020-08-13 09:09:47 +02:00
memset(categories, 0, sizeof(categories));
memset(leaderIds, 0, sizeof(leaderIds));
2019-01-03 18:54:22 +01:00
for (i = 0; i < gNumLinkContestPlayers; i++)
2020-08-13 09:09:47 +02:00
categories[i] = gTasks[taskId].data[i + 1];
2019-01-03 18:54:22 +01:00
2020-07-24 06:14:53 +02:00
// Ensure all players are doing the same category
2020-08-13 09:09:47 +02:00
for (i = 0; i < gNumLinkContestPlayers && categories[0] == categories[i]; i++)
;
2019-01-03 18:54:22 +01:00
if (i == gNumLinkContestPlayers)
2020-08-13 09:09:47 +02:00
gSpecialVar_0x8004 = FALSE; // Category choices the same
2019-01-03 18:54:22 +01:00
else
2020-08-13 09:09:47 +02:00
gSpecialVar_0x8004 = TRUE; // Category choices differ
2019-01-03 18:54:22 +01:00
for (i = 0; i < gNumLinkContestPlayers; i++)
2020-08-13 09:09:47 +02:00
leaderIds[i] = gTasks[taskId].data[i + 5];
2019-01-03 18:54:22 +01:00
2020-08-13 09:09:47 +02:00
// If < 4 players and player is leader, set AI contestants based on rank and game clear
2020-07-12 02:25:56 +02:00
if (gNumLinkContestPlayers != CONTESTANT_COUNT && GetMultiplayerId() == 0)
2019-01-03 18:54:22 +01:00
{
2020-07-12 02:25:56 +02:00
rank = gContestMons[0].highestRank;
2019-01-03 18:54:22 +01:00
for (i = 1; i < gNumLinkContestPlayers; i++)
{
2020-07-12 02:25:56 +02:00
if (rank < gContestMons[i].highestRank)
rank = gContestMons[i].highestRank;
2019-01-03 18:54:22 +01:00
}
if (rank)
rank--;
2020-08-13 09:09:47 +02:00
gameCleared = TRUE;
2019-01-03 18:54:22 +01:00
for (i = 0; i < gNumLinkContestPlayers; i++)
{
2020-07-12 02:25:56 +02:00
if (!gContestMons[i].gameCleared)
2019-01-03 18:54:22 +01:00
{
2020-08-13 09:09:47 +02:00
gameCleared = FALSE;
2019-01-03 18:54:22 +01:00
break;
}
}
2020-08-13 09:09:47 +02:00
SetLinkAIContestants(categories[0], rank, gameCleared);
2019-01-03 18:54:22 +01:00
}
2020-08-13 09:09:47 +02:00
// Assign link leader. After initial communication all players will read data only from them
gContestLinkLeaderIndex = LinkContest_GetLeaderIndex(leaderIds);
2020-07-12 02:25:56 +02:00
if (gNumLinkContestPlayers < CONTESTANT_COUNT)
2020-08-13 09:09:47 +02:00
SetTaskFuncWithFollowupFunc(taskId, Task_LinkContest_CommunicateAIMonsEm, Task_LinkContest_CalculateRound1Em);
2019-01-03 18:54:22 +01:00
else
2020-08-13 09:09:47 +02:00
gTasks[taskId].func = Task_LinkContest_CalculateRound1Em;
2019-01-03 18:54:22 +01:00
}
2020-08-13 09:09:47 +02:00
static void Task_LinkContest_CalculateRound1Em(u8 taskId)
2019-01-03 18:54:22 +01:00
{
2020-08-13 09:09:47 +02:00
CalculateRound1Points(gSpecialVar_ContestCategory);
SetTaskFuncWithFollowupFunc(taskId, Task_LinkContest_CommunicateRound1Points, Task_LinkContest_CalculateTurnOrderEm);
2019-01-03 18:54:22 +01:00
}
2020-08-13 09:09:47 +02:00
static void Task_LinkContest_CalculateTurnOrderEm(u8 taskId)
2019-01-03 18:54:22 +01:00
{
SortContestants(FALSE);
2020-08-13 09:09:47 +02:00
SetTaskFuncWithFollowupFunc(taskId, Task_LinkContest_CommunicateTurnOrder, Task_LinkContest_FinalizeConnection);
2019-01-03 18:54:22 +01:00
}
2020-08-13 09:09:47 +02:00
static void Task_LinkContest_CommunicateMonsEm(u8 taskId)
2019-01-03 18:54:22 +01:00
{
int i;
2020-08-13 09:09:47 +02:00
if (!LinkContest_TryLinkStandby(&gTasks[taskId].data[12]))
2019-01-03 18:54:22 +01:00
return;
switch (gTasks[taskId].data[0])
{
default:
gTasks[taskId].data[0] = 0;
gTasks[taskId].data[12] = 0;
SwitchTaskToFollowupFunc(taskId);
break;
case 0:
if (IsLinkTaskFinished())
{
2020-07-12 02:25:56 +02:00
if (LinkContest_SendBlock(&gContestMons[gContestPlayerMonIndex], sizeof(struct ContestPokemon)) == 1)
2019-01-03 18:54:22 +01:00
gTasks[taskId].data[0]++;
}
break;
case 1:
2020-07-12 02:25:56 +02:00
if (LinkContest_GetBlockReceivedFromAllPlayers())
2019-01-03 18:54:22 +01:00
{
for (i = 0; i < gNumLinkContestPlayers; i++)
{
memcpy(&gContestMons[i], gBlockRecvBuffer[i], sizeof(struct ContestPokemon));
2020-07-12 02:25:56 +02:00
StripPlayerAndMonNamesForLinkContest(&gContestMons[i], gLinkPlayers[i].language);
2019-01-03 18:54:22 +01:00
}
gTasks[taskId].data[0]++;
}
break;
}
}
2020-08-13 09:09:47 +02:00
static void Task_LinkContest_CommunicateRngEm(u8 taskId)
2019-01-03 18:54:22 +01:00
{
2020-08-13 09:09:47 +02:00
if (!LinkContest_TryLinkStandby(&gTasks[taskId].data[12]))
2019-01-03 18:54:22 +01:00
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)
{
2020-08-13 09:09:47 +02:00
// Only the leader sends the RNG seed
2019-01-03 18:54:22 +01:00
if (!IsLinkTaskFinished())
return;
2020-07-12 02:25:56 +02:00
if (LinkContest_SendBlock(&gRngValue, sizeof(gRngValue)) == 1)
2019-01-03 18:54:22 +01:00
gTasks[taskId].data[0]++;
}
else
{
2020-08-13 09:09:47 +02:00
// Other link members skip to waiting
2019-01-03 18:54:22 +01:00
gTasks[taskId].data[0]++;
}
break;
case 1:
2020-08-13 09:09:47 +02:00
// Wait to receive RNG data
2020-07-12 02:25:56 +02:00
if (LinkContest_GetBlockReceived(0))
2019-01-03 18:54:22 +01:00
{
memcpy(&gRngValue, gBlockRecvBuffer[0], sizeof(gRngValue));
memcpy(&gContestRngValue, gBlockRecvBuffer[0], sizeof(gContestRngValue));
gTasks[taskId].data[0]++;
}
break;
}
}
2020-08-13 09:09:47 +02:00
static void Task_LinkContest_CommunicateLeaderIdsEm(u8 taskId)
2019-01-03 18:54:22 +01:00
{
int i;
2020-08-13 09:09:47 +02:00
u16 data[CONTESTANT_COUNT];
u16 leaderId;
2019-01-03 18:54:22 +01:00
2020-08-13 09:09:47 +02:00
if (!LinkContest_TryLinkStandby(&gTasks[taskId].data[12]))
2019-01-03 18:54:22 +01:00
return;
switch (gTasks[taskId].data[0])
{
default:
gTasks[taskId].data[0] = 0;
gTasks[taskId].data[12] = 0;
SwitchTaskToFollowupFunc(taskId);
break;
case 0:
if (IsLinkTaskFinished())
{
2020-08-13 09:09:47 +02:00
leaderId = 0x6E;
if (LinkContest_SendBlock(&leaderId, sizeof(leaderId)) == 1)
2019-01-03 18:54:22 +01:00
gTasks[taskId].data[0]++;
}
break;
case 1:
2020-07-12 02:25:56 +02:00
if (LinkContest_GetBlockReceivedFromAllPlayers())
2019-01-03 18:54:22 +01:00
{
for (i = 0; i < gNumLinkContestPlayers; i++)
{
data[i] = gBlockRecvBuffer[i][0];
gTasks[taskId].data[i + 5] = data[i];
}
gTasks[taskId].data[0]++;
}
break;
}
}
2020-08-13 09:09:47 +02:00
static void Task_LinkContest_CommunicateCategoryEm(u8 taskId)
2019-01-03 18:54:22 +01:00
{
int i;
2020-07-12 02:25:56 +02:00
u16 data[CONTESTANT_COUNT];
u16 category;
2019-01-03 18:54:22 +01:00
2020-08-13 09:09:47 +02:00
if (!LinkContest_TryLinkStandby(&gTasks[taskId].data[12]))
2019-01-03 18:54:22 +01:00
return;
switch (gTasks[taskId].data[0])
{
default:
gTasks[taskId].data[0] = 0;
gTasks[taskId].data[12] = 0;
SwitchTaskToFollowupFunc(taskId);
break;
case 0:
if (IsLinkTaskFinished())
{
2020-07-12 02:25:56 +02:00
category = gTasks[taskId].tCategory;
if (LinkContest_SendBlock(&category, sizeof(category)) == 1)
2019-01-03 18:54:22 +01:00
gTasks[taskId].data[0]++;
}
break;
case 1:
2020-07-12 02:25:56 +02:00
if (LinkContest_GetBlockReceivedFromAllPlayers())
2019-01-03 18:54:22 +01:00
{
for (i = 0; i < gNumLinkContestPlayers; i++)
{
data[i] = gBlockRecvBuffer[i][0];
gTasks[taskId].data[i + 1] = data[i];
}
gTasks[taskId].data[0]++;
}
break;
}
}
2020-08-13 09:09:47 +02:00
static void Task_LinkContest_CommunicateAIMonsEm(u8 taskId)
2019-01-03 18:54:22 +01:00
{
int i;
2020-08-13 09:09:47 +02:00
if (!LinkContest_TryLinkStandby(&gTasks[taskId].data[12]))
2019-01-03 18:54:22 +01:00
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;
2020-07-12 02:25:56 +02:00
if (LinkContest_SendBlock(&gContestMons[gNumLinkContestPlayers], (CONTESTANT_COUNT - gNumLinkContestPlayers) * sizeof(struct ContestPokemon)) == 1)
2019-01-03 18:54:22 +01:00
gTasks[taskId].data[0]++;
}
else
{
gTasks[taskId].data[0]++;
}
break;
case 1:
2020-07-12 02:25:56 +02:00
if (LinkContest_GetBlockReceived(0))
2019-01-03 18:54:22 +01:00
{
2020-07-12 02:25:56 +02:00
memcpy(&gContestMons[gNumLinkContestPlayers], gBlockRecvBuffer[0], (CONTESTANT_COUNT - gNumLinkContestPlayers) * sizeof(struct ContestPokemon));
for (i = gNumLinkContestPlayers; i < CONTESTANT_COUNT; i++)
StripPlayerAndMonNamesForLinkContest(&gContestMons[i], gLinkPlayers[0].language);
2019-01-03 18:54:22 +01:00
gTasks[taskId].data[0]++;
}
break;
}
}