pokeemerald/src/field_poison.c

151 lines
3.8 KiB
C
Raw Normal View History

2017-11-23 02:10:35 +01:00
#include "global.h"
2018-10-17 02:11:44 +02:00
#include "battle.h"
2018-11-14 01:01:50 +01:00
#include "battle_pike.h"
2019-02-08 00:41:44 +01:00
#include "battle_pyramid.h"
2017-11-23 02:10:35 +01:00
#include "event_data.h"
#include "field_message_box.h"
#include "field_poison.h"
2018-12-20 04:19:54 +01:00
#include "fldeff_misc.h"
2018-10-30 22:17:03 +01:00
#include "frontier_util.h"
2018-10-17 02:11:44 +02:00
#include "party_menu.h"
2017-11-23 02:10:35 +01:00
#include "pokenav.h"
#include "script.h"
2018-10-17 02:11:44 +02:00
#include "string_util.h"
#include "strings.h"
#include "task.h"
2019-01-13 20:50:08 +01:00
#include "trainer_hill.h"
2019-10-07 02:04:30 +02:00
#include "constants/field_poison.h"
2019-10-18 01:22:03 +02:00
#include "constants/party_menu.h"
2018-11-01 21:31:10 +01:00
2018-10-17 02:11:44 +02:00
static bool32 IsMonValidSpecies(struct Pokemon *pokemon)
2017-11-23 02:10:35 +01:00
{
u16 species = GetMonData(pokemon, MON_DATA_SPECIES2);
if (species == SPECIES_NONE || species == SPECIES_EGG)
return FALSE;
2022-07-07 03:04:54 +02:00
2017-11-23 02:10:35 +01:00
return TRUE;
}
2018-10-17 02:11:44 +02:00
static bool32 AllMonsFainted(void)
2017-11-23 02:10:35 +01:00
{
int i;
2018-10-17 02:11:44 +02:00
struct Pokemon *pokemon = gPlayerParty;
2017-11-23 02:10:35 +01:00
2018-10-17 02:11:44 +02:00
for (i = 0; i < PARTY_SIZE; i++, pokemon++)
2017-11-23 02:10:35 +01:00
{
2018-10-17 02:11:44 +02:00
if (IsMonValidSpecies(pokemon) && GetMonData(pokemon, MON_DATA_HP) != 0)
2017-11-23 02:10:35 +01:00
return FALSE;
}
return TRUE;
}
2018-10-17 02:11:44 +02:00
static void FaintFromFieldPoison(u8 partyIdx)
2017-11-23 02:10:35 +01:00
{
2022-07-07 03:04:54 +02:00
struct Pokemon *pokemon = &gPlayerParty[partyIdx];
2018-10-17 02:11:44 +02:00
u32 status = STATUS1_NONE;
2020-02-02 19:28:54 +01:00
AdjustFriendship(pokemon, FRIENDSHIP_EVENT_FAINT_FIELD_PSN);
2017-11-23 02:10:35 +01:00
SetMonData(pokemon, MON_DATA_STATUS, &status);
GetMonData(pokemon, MON_DATA_NICKNAME, gStringVar1);
StringGet_Nickname(gStringVar1);
2017-11-23 02:10:35 +01:00
}
2018-10-17 02:11:44 +02:00
static bool32 MonFaintedFromPoison(u8 partyIdx)
2017-11-23 02:10:35 +01:00
{
2022-07-07 03:04:54 +02:00
struct Pokemon *pokemon = &gPlayerParty[partyIdx];
2019-10-18 01:22:03 +02:00
if (IsMonValidSpecies(pokemon) && GetMonData(pokemon, MON_DATA_HP) == 0 && GetAilmentFromStatus(GetMonData(pokemon, MON_DATA_STATUS)) == AILMENT_PSN)
2017-11-23 02:10:35 +01:00
return TRUE;
2022-07-07 03:04:54 +02:00
2017-11-23 02:10:35 +01:00
return FALSE;
}
2022-07-07 03:04:54 +02:00
#define tState data[0]
#define tPartyIdx data[1]
2019-10-07 02:04:30 +02:00
static void Task_TryFieldPoisonWhiteOut(u8 taskId)
2017-11-23 02:10:35 +01:00
{
s16 *data = gTasks[taskId].data;
2022-07-07 03:04:54 +02:00
switch (tState)
2017-11-23 02:10:35 +01:00
{
case 0:
for (; tPartyIdx < PARTY_SIZE; tPartyIdx++)
{
if (MonFaintedFromPoison(tPartyIdx))
2017-11-23 02:10:35 +01:00
{
FaintFromFieldPoison(tPartyIdx);
ShowFieldMessage(gText_PkmnFainted_FldPsn);
tState++;
return;
2017-11-23 02:10:35 +01:00
}
}
tState = 2; // Finished checking party
break;
case 1:
// Wait for "{mon} fainted" message, then return to party loop
if (IsFieldMessageBoxHidden())
tState--;
break;
case 2:
if (AllMonsFainted())
{
// Battle facilities have their own white out script to handle the challenge loss
if (InBattlePyramid() | InBattlePike() || InTrainerHillChallenge())
gSpecialVar_Result = FLDPSN_FRONTIER_WHITEOUT;
2017-11-23 02:10:35 +01:00
else
gSpecialVar_Result = FLDPSN_WHITEOUT;
}
else
{
gSpecialVar_Result = FLDPSN_NO_WHITEOUT;
}
ScriptContext_Enable();
DestroyTask(taskId);
break;
2017-11-23 02:10:35 +01:00
}
}
2022-07-07 03:04:54 +02:00
#undef tState
#undef tPartyIdx
2019-10-07 02:04:30 +02:00
void TryFieldPoisonWhiteOut(void)
2017-11-23 02:10:35 +01:00
{
2019-10-07 02:04:30 +02:00
CreateTask(Task_TryFieldPoisonWhiteOut, 80);
ScriptContext_Stop();
2017-11-23 02:10:35 +01:00
}
s32 DoPoisonFieldEffect(void)
2017-11-23 02:10:35 +01:00
{
int i;
2018-10-17 02:11:44 +02:00
u32 hp;
struct Pokemon *pokemon = gPlayerParty;
2018-10-17 02:11:44 +02:00
u32 numPoisoned = 0;
u32 numFainted = 0;
2022-07-07 03:04:54 +02:00
2017-11-23 02:10:35 +01:00
for (i = 0; i < PARTY_SIZE; i++)
{
2019-10-18 01:22:03 +02:00
if (GetMonData(pokemon, MON_DATA_SANITY_HAS_SPECIES) && GetAilmentFromStatus(GetMonData(pokemon, MON_DATA_STATUS)) == AILMENT_PSN)
2017-11-23 02:10:35 +01:00
{
2022-07-07 03:04:54 +02:00
// Apply poison damage
2017-11-23 02:10:35 +01:00
hp = GetMonData(pokemon, MON_DATA_HP);
if (hp == 0 || --hp == 0)
numFainted++;
2022-07-07 03:04:54 +02:00
2017-11-23 02:10:35 +01:00
SetMonData(pokemon, MON_DATA_HP, &hp);
numPoisoned++;
2017-11-23 02:10:35 +01:00
}
pokemon++;
}
2022-07-07 03:04:54 +02:00
// Do screen flash effect
if (numFainted != 0 || numPoisoned != 0)
2018-12-20 04:19:54 +01:00
FldEffPoison_Start();
2022-07-07 03:04:54 +02:00
if (numFainted != 0)
return FLDPSN_FNT;
2022-07-07 03:04:54 +02:00
if (numPoisoned != 0)
return FLDPSN_PSN;
2022-07-07 03:04:54 +02:00
return FLDPSN_NONE;
2017-11-23 02:10:35 +01:00
}