summaryrefslogtreecommitdiff
path: root/src/lottery_corner.c
blob: c807237af8e2eddce147b1b83c3e5b840555d95e (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
154
155
156
157
158
159
160
161
162
163
164
#include "global.h"
#include "pokemon.h"
#include "rng.h"
#include "string_util.h"
#include "var.h"

extern u16 gScriptResult;
extern u16 gSpecialVar_0x8004;
extern struct Pokemon gPlayerParty[6];
extern struct PokemonStorage gPokemonStorage;
extern u16 gSpecialVar_0x8005;
extern u16 gUnknown_0840CB04[];
extern u16 gSpecialVar_0x8006;
static EWRAM_DATA u16 sWinNumberDigit = 0;
static EWRAM_DATA u16 sOtIdDigit = 0;

void sub_8145D14(u32);
u32 sub_8145D3C(void);
static u8 GetMatchingDigits(u16, u16);

void sub_8145A78(void)
{
    u16 rand = Random();

    sub_8145D14((Random() << 16) | rand);
    VarSet(0x4045, 0);
}

void sub_8145AA4(u16 a)
{
    u32 var = Random();
    
    while(--a != 0xFFFF)
    {
        var = var * 1103515245 + 12345;
    }
    sub_8145D14(var);    
}

void sub_8145AEC(void)
{
    u16 a = sub_8145D3C();
    gScriptResult = a;
}

//Script special function
void PickLotteryCornerTicket(void)
{
    u16 i;
    u16 j;
    u32 box;
    u32 slot;
    
    gSpecialVar_0x8004 = 0;
    slot = 0;
    box = 0;
    for(i = 0; i < 6; i++)
    {
        struct Pokemon *pkmn = &gPlayerParty[i];
        
        // UB: Too few arguments for function GetMonData
        if(GetMonData(pkmn, MON_DATA_SPECIES) != 0)
        {
            if(!GetMonData(pkmn, MON_DATA_IS_EGG))
            {
                u32 otId = GetMonData(pkmn, MON_DATA_OT_ID);
                u8 a = GetMatchingDigits(gScriptResult, otId);
                
                if(a > gSpecialVar_0x8004 && a > 1)
                {
                    gSpecialVar_0x8004 = a - 1;
                    box = 14;
                    slot = i;
                }
            }
        }
        else
            break;
    }
    
    for(i = 0; i < 14; i++)
    {
        for(j = 0; j < 0x1E; j++)
        {
            struct BoxPokemon *pkmn = &gPokemonStorage.boxes[i][j];
            
            // UB: Too few arguments for function GetMonData
            if(GetBoxMonData(pkmn, MON_DATA_SPECIES) != 0 &&
            !GetBoxMonData(pkmn, MON_DATA_IS_EGG))
            {
                u32 otId = GetBoxMonData(pkmn, MON_DATA_OT_ID);
                u8 a = GetMatchingDigits(gScriptResult, otId);
                
                if(a > gSpecialVar_0x8004 && a > 1)
                {
                    gSpecialVar_0x8004 = a - 1;
                    box = i;
                    slot = j;
                }
            }
        }
    }
    
    if(gSpecialVar_0x8004 != 0)
    {
        gSpecialVar_0x8005 = gUnknown_0840CB04[gSpecialVar_0x8004 - 1];
        
        if(box == 14)
        {
            gSpecialVar_0x8006 = 0;
            GetMonData(&gPlayerParty[slot], MON_DATA_NICKNAME, gStringVar1);
        }
        else
        {
            gSpecialVar_0x8006 = 1;
            GetBoxMonData(&gPokemonStorage.boxes[box][slot], MON_DATA_NICKNAME, gStringVar1);
        }
        StringGetEnd10(gStringVar1);
    }
}

static u8 GetMatchingDigits(u16 winNumber, u16 otId)
{
    u8 i;
    u8 matchingDigits = 0;  //Why not just use i?
    
    for(i = 0; i < 5; i++)
    {
        sWinNumberDigit = winNumber % 10;
        sOtIdDigit = otId % 10;
        
        if(sWinNumberDigit == sOtIdDigit)
        {
            winNumber = winNumber / 10;
            otId = otId / 10;
            matchingDigits++;
        }
        else
            break;
    }
    return matchingDigits;
}

void sub_8145D14(u32 a)
{
    u16 b = a >> 16;
    u16 c = a;
    
    VarSet(0x404B, c);
    VarSet(0x404C, b);
}

u32 sub_8145D3C(void)
{
    u16 var1 = VarGet(0x404B);
    u16 var2 = VarGet(0x404C);
    
    return (var2 << 16) | var1;
}

void unref_sub_8145D64(u16 a)
{
    sub_8145D14(a);
}