forked from pret/pokeemerald
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlottery_corner.c
168 lines (143 loc) · 4.35 KB
/
lottery_corner.c
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
165
166
167
168
#include "global.h"
#include "lottery_corner.h"
#include "event_data.h"
#include "pokemon.h"
#include "constants/items.h"
#include "random.h"
#include "string_util.h"
#include "text.h"
#include "pokemon_storage_system.h"
static EWRAM_DATA u16 sWinNumberDigit = 0;
static EWRAM_DATA u16 sOtIdDigit = 0;
static const u16 sLotteryPrizes[] =
{
ITEM_PP_UP,
ITEM_EXP_SHARE,
ITEM_MAX_REVIVE,
ITEM_MASTER_BALL,
};
static u8 GetMatchingDigits(u16, u16);
void ResetLotteryCorner(void)
{
u16 rand = Random();
SetLotteryNumber((Random() << 16) | rand);
VarSet(VAR_POKELOT_PRIZE_ITEM, 0);
}
void SetRandomLotteryNumber(u16 i)
{
u32 var = Random();
while (--i != 0xFFFF)
var = ISO_RANDOMIZE2(var);
SetLotteryNumber(var);
}
void RetrieveLotteryNumber(void)
{
u16 lottoNumber = GetLotteryNumber();
gSpecialVar_Result = lottoNumber;
}
void PickLotteryCornerTicket(void)
{
u16 i;
u16 j;
u32 box;
u32 slot;
gSpecialVar_0x8004 = 0;
slot = 0;
box = 0;
for (i = 0; i < PARTY_SIZE; i++)
{
struct Pokemon *mon = &gPlayerParty[i];
if (GetMonData(mon, MON_DATA_SPECIES) != SPECIES_NONE)
{
// do not calculate ticket values for eggs.
if (!GetMonData(mon, MON_DATA_IS_EGG))
{
u32 otId = GetMonData(mon, MON_DATA_OT_ID);
u8 numMatchingDigits = GetMatchingDigits(gSpecialVar_Result, otId);
if (numMatchingDigits > gSpecialVar_0x8004 && numMatchingDigits > 1)
{
gSpecialVar_0x8004 = numMatchingDigits - 1;
box = TOTAL_BOXES_COUNT;
slot = i;
}
}
}
else // Pokémon are always arranged from populated spots first to unpopulated, so the moment a NONE species is found, that's the end of the list.
{
break;
}
}
for (i = 0; i < TOTAL_BOXES_COUNT; i++)
{
for (j = 0; j < IN_BOX_COUNT; j++)
{
if (GetBoxMonData(&gPokemonStoragePtr->boxes[i][j], MON_DATA_SPECIES) != SPECIES_NONE &&
!GetBoxMonData(&gPokemonStoragePtr->boxes[i][j], MON_DATA_IS_EGG))
{
u32 otId = GetBoxMonData(&gPokemonStoragePtr->boxes[i][j], MON_DATA_OT_ID);
u8 numMatchingDigits = GetMatchingDigits(gSpecialVar_Result, otId);
if (numMatchingDigits > gSpecialVar_0x8004 && numMatchingDigits > 1)
{
gSpecialVar_0x8004 = numMatchingDigits - 1;
box = i;
slot = j;
}
}
}
}
if (gSpecialVar_0x8004 != 0)
{
gSpecialVar_0x8005 = sLotteryPrizes[gSpecialVar_0x8004 - 1];
if (box == TOTAL_BOXES_COUNT)
{
gSpecialVar_0x8006 = 0;
GetMonData(&gPlayerParty[slot], MON_DATA_NICKNAME, gStringVar1);
}
else
{
gSpecialVar_0x8006 = 1;
GetBoxMonData(&gPokemonStoragePtr->boxes[box][slot], MON_DATA_NICKNAME, gStringVar1);
}
StringGet_Nickname(gStringVar1);
}
}
static u8 GetMatchingDigits(u16 winNumber, u16 otId)
{
u8 i;
u8 matchingDigits = 0;
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;
}
// lottery numbers go from 0 to 99999, not 65535 (0xFFFF). interestingly enough, the function that calls GetLotteryNumber shifts to u16, so it cant be anything above 65535 anyway.
void SetLotteryNumber(u32 lotteryNum)
{
u16 lowNum = lotteryNum >> 16;
u16 highNum = lotteryNum;
VarSet(VAR_POKELOT_RND1, highNum);
VarSet(VAR_POKELOT_RND2, lowNum);
}
u32 GetLotteryNumber(void)
{
u16 highNum = VarGet(VAR_POKELOT_RND1);
u16 lowNum = VarGet(VAR_POKELOT_RND2);
return (lowNum << 16) | highNum;
}
// interestingly, this may have been the original lottery number set function, but GF tried to change it to 32-bit later but didnt finish changing all calls as one GetLotteryNumber still shifts to u16.
void SetLotteryNumber16_Unused(u16 lotteryNum)
{
SetLotteryNumber(lotteryNum);
}