summaryrefslogtreecommitdiff
path: root/src/field_poison.c
blob: 6a519d90ca049fbb66a2f595955d0282e378e1ba (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
#include "global.h"
#include "string_util.h"
#include "party_menu.h"
#include "constants/species.h"
#include "task.h"
#include "field_message_box.h"
#include "strings.h"
#include "rom_818CFC8.h"
#include "battle_frontier_2.h"
#include "pokenav.h"
#include "event_data.h"
#include "script.h"
#include "battle.h"
#include "fldeff_80F9BCC.h"
#include "field_poison.h"

static bool32 sub_80F9568(struct Pokemon *pokemon)
{
    u16 species = GetMonData(pokemon, MON_DATA_SPECIES2);
    if (species == SPECIES_NONE || species == SPECIES_EGG)
    {
        return FALSE;
    }
    return TRUE;
}

static bool32 sub_80F958C(void)
{
    int i;
    struct Pokemon *pokemon;

    for (pokemon = gPlayerParty, i = 0; i < PARTY_SIZE; i++, pokemon++)
    {
        if (sub_80F9568(pokemon) && GetMonData(pokemon, MON_DATA_HP) != 0)
        {
            return FALSE;
        }
    }
    return TRUE;
}

static void sub_80F95C0(u8 partyIdx)
{
    struct Pokemon *pokemon = gPlayerParty + partyIdx;
    unsigned int status = STATUS1_NONE;
    AdjustFriendship(pokemon, 0x07);
    SetMonData(pokemon, MON_DATA_STATUS, &status);
    GetMonData(pokemon, MON_DATA_NICKNAME, gStringVar1);
    StringGetEnd10(gStringVar1);
}

static bool32 sub_80F960C(u8 partyIdx)
{
    struct Pokemon *pokemon = gPlayerParty + partyIdx;
    if (sub_80F9568(pokemon) && GetMonData(pokemon, MON_DATA_HP) == 0 && pokemon_ailments_get_primary(GetMonData(pokemon, MON_DATA_STATUS)) == AILMENT_PSN)
    {
        return TRUE;
    }
    return FALSE;
}

static void sub_80F9654(u8 taskId)
{
    s16 *data = gTasks[taskId].data;
    switch (data[0])
    {
        case 0:
            for (; data[1] < PARTY_SIZE; data[1]++)
            {
                if (sub_80F960C(data[1]))
                {
                    sub_80F95C0(data[1]);
                    ShowFieldMessage(gText_PkmnFainted3);
                    data[0]++;
                    return;
                }
            }
            data[0] = 2;
            break;
        case 1:
            if (IsFieldMessageBoxHidden())
            {
                data[0]--;
            }
            break;
        case 2:
            if (sub_80F958C())
            {
                if (InBattlePyramid() | InBattlePike() || sub_81D5C18())
                {
                    gSpecialVar_Result = 2;
                }
                else
                {
                    gSpecialVar_Result = 1;
                }
            }
            else
            {
                gSpecialVar_Result = 0;
            }
            EnableBothScriptContexts();
            DestroyTask(taskId);
            break;
    }
}

void sub_80F972C(void)
{
    CreateTask(sub_80F9654, 80);
    ScriptContext1_Stop();
}

int DoPoisonFieldEffect(void)
{
    int i;
    unsigned int hp;
    struct Pokemon *pokemon = gPlayerParty;
    unsigned int numPoisoned = 0;
    unsigned int numFainted = 0;
    for (i = 0; i < PARTY_SIZE; i++)
    {
        if (GetMonData(pokemon, MON_DATA_SANITY_BIT2) && pokemon_ailments_get_primary(GetMonData(pokemon, MON_DATA_STATUS)) == AILMENT_PSN)
        {
            hp = GetMonData(pokemon, MON_DATA_HP);
            if (hp == 0 || --hp == 0)
            {
                numFainted++;
            }
            SetMonData(pokemon, MON_DATA_HP, &hp);
            numPoisoned++;
        }
        pokemon++;
    }
    if (numFainted != 0 || numPoisoned != 0)
    {
        overworld_poison_effect();
    }
    if (numFainted != 0)
    {
        return FLDPSN_FNT;
    }
    if (numPoisoned != 0)
    {
        return FLDPSN_PSN;
    }
    return FLDPSN_NONE;
}