summaryrefslogtreecommitdiff
path: root/src/field_poison.c
blob: 596bc6e89f6de5e43272b7cfe366515fb54c6065 (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
#include "global.h"
#include "field_poison.h"
#include "field_message_box.h"
#include "fldeff_poison.h"
#include "pokemon.h"
#include "pokemon_summary_screen.h"
#include "script.h"
#include "string_util.h"
#include "task.h"
#include "text.h"
#include "constants/species.h"

extern u16 gSpecialVar_Result;
extern u8 fieldPoisonText_PokemonFainted[];

static bool32 IsMonValidSpecies(struct Pokemon *mon)
{
    // UB: Too few arguments for function 'GetMonData'
    u16 species = GetMonData(mon, MON_DATA_SPECIES2);
    if (species == SPECIES_NONE || species == SPECIES_EGG)
        return FALSE;
    else
        return TRUE;
}

static bool32 AllMonsFainted(void)
{
    int i;
    struct Pokemon *mon = gPlayerParty;

    for (i = 0; i < PARTY_SIZE; i++, mon++)
    {
        // UB: Too few arguments for function 'GetMonData'
        if (IsMonValidSpecies(mon) && GetMonData(mon, MON_DATA_HP) != 0)
            return FALSE;
    }

    return TRUE;
}

static void FaintFromFieldPoison(u8 monIndex)
{
    struct Pokemon *mon = &gPlayerParty[monIndex];
    u32 status = 0;

    AdjustFriendship(mon, FRIENDSHIP_EVENT_FAINT_OUTSIDE_BATTLE);
    SetMonData(mon, MON_DATA_STATUS, &status);
    GetMonData(mon, MON_DATA_NICKNAME, gStringVar1);
    StringGetEnd10(gStringVar1);
}

static bool32 MonFaintedFromPoison(u8 monIndex)
{
    struct Pokemon *mon = &gPlayerParty[monIndex];

    // UB: Too few arguments for function 'GetMonData'
    if (IsMonValidSpecies(mon) && GetMonData(mon, MON_DATA_HP) == 0
     && GetPrimaryStatus(GetMonData(mon, MON_DATA_STATUS)) == STATUS_PRIMARY_POISON)
        return TRUE;
    else
        return FALSE;
}

#define tState       data[0]
#define tPartyMember data[1]

static void Task_WhiteOut(u8 taskId)
{
    s16 *data = gTasks[taskId].data;

    switch (tState)
    {
    case 0:
        // Check if any Pokemon have fainted due to poison
        while (tPartyMember < PARTY_SIZE)
        {
            if (MonFaintedFromPoison(tPartyMember))
            {
                // Show message about fainted mon
                FaintFromFieldPoison(tPartyMember);
                ShowFieldMessage(fieldPoisonText_PokemonFainted);
                tState++;
                return;
            }
            tPartyMember++;
        }
        tState = 2;
        break;
    case 1:  // Wait for message box to disappear
        if (IsFieldMessageBoxHidden())
            tState--;  // Go to previous step and check next party member
        break;
    case 2:  // done checking all mons
        if (AllMonsFainted())
            gSpecialVar_Result = 1;
        else
            gSpecialVar_Result = 0;
        EnableBothScriptContexts();
        DestroyTask(taskId);
        break;
    }
}

#undef tState
#undef tPartyMember

void ExecuteWhiteOut(void)
{
    CreateTask(Task_WhiteOut, 0x50);
    ScriptContext1_Stop();
}

s32 DoPoisonFieldEffect(void)
{
    struct Pokemon *mon = &gPlayerParty[0];
    u32 numPoisoned = 0;
    u32 numFainting = 0;
    int i;

    // count the number of mons that are poisoned and fainting from poison,
    // and decrement HP of all poisoned mons
    for (i = 0; i < PARTY_SIZE; i++)
    {
        u32 hp;

        if (GetMonData(mon, MON_DATA_SANITY_BIT2) != 0
         && GetPrimaryStatus(GetMonData(mon, MON_DATA_STATUS)) == STATUS_PRIMARY_POISON)
        {
            // decrement HP of poisoned mon
            hp = GetMonData(mon, MON_DATA_HP);
            if (hp != 0)
                hp--;
            if (hp == 0)
                numFainting++;
            SetMonData(mon, MON_DATA_HP, &hp);
            numPoisoned++;
        }
        mon++;
    }
    if (numFainting != 0 || numPoisoned != 0)
    {
        FldeffPoison_Start();
    }
    if (numFainting != 0)
    {
        return 2;
    }
    if (numPoisoned != 0)
    {
        return 1;
    }
    return 0;
}