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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
|
Credits to Citrus Bolt for this. Without them, this wouldn't be possible.
When making a hack, you may or may not want to preserve link features. This tutorial will walk you through how to add a new identifier that gets sent through the link whilst also being able to keep compatibility with vanilla Pokémon games.
First, we want to open [include/constants/global.h](https://github.com/pret/pokeemerald/blob/master/include/constants/global.h) and add the following defines:
```diff
+ // Version ID modifiers used for identifying unofficial games.
+ // The idea is that each unofficial game will have its own number they can use in conjunction with one of the official origin game IDs
+ // so that they do not have to requisition a new ID for every project
+ #define MODIFIER_NONE 0 // official games
+ #define MODIFIER_HELIODOR 1 // Heliodor.
+ #define MODIFIER_DX 2 // 4-2 is FireRed DX and 5-2 is LeafGreen DX
+ #define MODIFIER_CRYSTALDUST 3 // 4-3 is modified CrystalDust
```
These are constant values for the different possible IDs we're gonna use. 1, 2 and 3 have already been reserved but the tutorial will allow for 255 different IDs. Think of a number and define it like the following:
```c
#define VERSION_MODIFIER (MODIFIER_DX)
```
For this tutorial, I will be using `MODIFIER_DX` but as mentioned previously, you can use any value you want.
So next, we want to open [include/link.h](https://github.com/pret/pokeemerald/blob/master/include/link.h) and [include/link_rfu.h](https://github.com/pret/pokeemerald/blob/master/include/link_rfu.h) as we will be editing a couple structs. In `include/link.h`, we need to find `struct LinkPlayer` and make the following change:
```diff
struct LinkPlayer
{
/* 0x00 */ u16 version;
/* 0x02 */ u16 lp_field_2;
/* 0x04 */ u32 trainerId;
/* 0x08 */ u8 name[PLAYER_NAME_LENGTH + 1];
/* 0x10 */ u8 progressFlags; // (& 0x0F) is hasNationalDex, (& 0xF0) is hasClearedGame
- /* 0x11 */ u8 neverRead;
+ /* 0x11 */ u8 versionModifier;
/* 0x12 */ u8 progressFlagsCopy;
/* 0x13 */ u8 gender;
/* 0x14 */ u32 linkType;
/* 0x18 */ u16 id; // battler id in battles
/* 0x1A */ u16 language;
};
```
And in `include/link_rfu.h`, we need to find `struct __attribute__((packed, aligned(2))) GFtgtGname` and make the following change:
```diff
struct __attribute__((packed, aligned(2))) GFtgtGname
{
struct GFtgtGnameSub unk_00;
u8 child_sprite_gender[RFU_CHILD_MAX]; // u8 sprite_idx:3;
// u8 gender:1;
// u8 unk_4:3
// u8 active:1
u16 species:10;
u16 type:6;
u8 activity:7;
u8 started:1;
u8 playerGender:1;
u8 level:7;
- u8 padding;
+ u8 versionModifier;
}; // size: RFU_GNAME_SIZE
```
If you notice, we now have a `versionModifier` in both structs which we will be using to signal hacks that the game is in fact an unofficial game.
Next, we want to open [src/link.c](https://github.com/pret/pokeemerald/blob/master/src/link.c) and find the function labelled `InitLocalLinkPlayer` and make the following change:
```diff
static void InitLocalLinkPlayer(void)
{
gLocalLinkPlayer.trainerId = gSaveBlock2Ptr->playerTrainerId[0] | (gSaveBlock2Ptr->playerTrainerId[1] << 8) | (gSaveBlock2Ptr->playerTrainerId[2] << 16) | (gSaveBlock2Ptr->playerTrainerId[3] << 24);
StringCopy(gLocalLinkPlayer.name, gSaveBlock2Ptr->playerName);
gLocalLinkPlayer.gender = gSaveBlock2Ptr->playerGender;
gLocalLinkPlayer.linkType = gLinkType;
gLocalLinkPlayer.language = gGameLanguage;
gLocalLinkPlayer.version = gGameVersion + 0x4000;
+ gLocalLinkPlayer.versionModifier = VERSION_MODIFIER;
gLocalLinkPlayer.lp_field_2 = 0x8000;
gLocalLinkPlayer.progressFlags = IsNationalPokedexEnabled();
if (FlagGet(FLAG_IS_CHAMPION))
{
gLocalLinkPlayer.progressFlags |= 0x10;
}
}
```
Here, `VERSION_MODIFIER` is our specific number we set in `include/constants/global.h`.
In that same file (src/link.c), go to the function labelled `ProcessRecvCmds`, find the following and change:
```c
if ((linkPlayer->version & 0xFF) == VERSION_RUBY || (linkPlayer->version & 0xFF) == VERSION_SAPPHIRE)
{
linkPlayer->progressFlagsCopy = 0;
- linkPlayer->neverRead = 0;
linkPlayer->progressFlags = 0;
}
ConvertLinkPlayerName(linkPlayer);
if (strcmp(block->magic1, sASCIIGameFreakInc) != 0
|| strcmp(block->magic2, sASCIIGameFreakInc) != 0)
{
SetMainCallback2(CB2_LinkError);
}
else
{
HandleReceiveRemoteLinkPlayer(i);
}
```
This removes the reference to `linkPlayer->neverRead`, which was changed to our new `versionModifier` label. Don't worry, that byte is written to but never read hence it being named `neverRead`.
Now, we need to open [src/link_rfu_3](https://github.com/pret/pokeemerald/blob/master/src/link_rfu_3.c) and find the function named `InitHostRFUtgtGname`and make changes as follows:
```diff
void InitHostRFUtgtGname(struct GFtgtGname *data, u8 activity, bool32 started, s32 child_sprite_genders)
{
s32 i;
for (i = 0; i < 2; i++)
{
data->unk_00.playerTrainerId[i] = gSaveBlock2Ptr->playerTrainerId[i];
}
for (i = 0; i < RFU_CHILD_MAX; i++)
{
data->child_sprite_gender[i] = child_sprite_genders;
child_sprite_genders >>= 8;
}
data->playerGender = gSaveBlock2Ptr->playerGender;
data->activity = activity;
data->started = started;
+ data->versionModifier = VERSION_MODIFIER;
data->unk_00.language = GAME_LANGUAGE;
data->unk_00.version = GAME_VERSION;
data->unk_00.hasNews = FALSE;
data->unk_00.hasCard = FALSE;
- data->unk_00.unknown = FALSE;
data->unk_00.isChampion = FlagGet(FLAG_IS_CHAMPION);
data->unk_00.hasNationalDex = IsNationalPokedexEnabled();
data->unk_00.gameClear = FlagGet(FLAG_SYS_GAME_CLEAR);
}
```
Now, our game will send our `versionModifier` ID through both wireless and cable link. Now, we need to make a few edits to the trainer card. This is probably the most difficult step in this tutorial so follow carefully.
Okay so, we need to open [include/trainer_card.h](https://github.com/pret/pokeemerald/blob/master/include/trainer_card.h) and find `struct TrainerCard`. In that struct, make the following change:
```diff
struct TrainerCard
{
/*0x00*/ u8 gender;
/*0x01*/ u8 stars;
/*0x02*/ bool8 hasPokedex;
/*0x03*/ bool8 caughtAllHoenn;
/*0x04*/ bool8 hasAllPaintings;
/*0x06*/ u16 hofDebutHours;
/*0x08*/ u16 hofDebutMinutes;
/*0x0A*/ u16 hofDebutSeconds;
/*0x0C*/ u16 caughtMonsCount;
/*0x0E*/ u16 trainerId;
/*0x10*/ u16 playTimeHours;
/*0x12*/ u16 playTimeMinutes;
/*0x14*/ u16 linkBattleWins;
/*0x16*/ u16 linkBattleLosses;
/*0x18*/ u16 battleTowerWins;
/*0x1A*/ u16 battleTowerStraightWins;
/*0x1C*/ u16 contestsWithFriends;
/*0x1E*/ u16 pokeblocksWithFriends;
/*0x20*/ u16 pokemonTrades;
/*0x24*/ u32 money;
/*0x28*/ u16 easyChatProfile[TRAINER_CARD_PROFILE_LENGTH];
/*0x30*/ u8 playerName[PLAYER_NAME_LENGTH + 1];
/*0x38*/ u8 version;
/*0x3A*/ bool16 hasAllFrontierSymbols;
/*0x3C*/ u32 berryCrushPoints;
/*0x40*/ u32 unionRoomNum;
/*0x44*/ u8 filler[8];
/*0x4C*/ bool8 shouldDrawStickers; // FRLG only
/*0x4D*/ u8 unused;
/*0x4E*/ u8 monIconTint; // FRLG only
/*0x4F*/ u8 facilityClass;
/*0x50*/ u8 stickers[TRAINER_CARD_STICKER_TYPES]; // FRLG only
/*0x54*/ u16 monSpecies[PARTY_SIZE]; // FRLG only
/*0x60*/ bool16 hasAllSymbols;
/*0x62*/ u16 frontierBP;
+ /*0x64*/ u8 versionModifier;
};
```
This is so the game can use the `versionModifier` value when loading the trainer card.
Next, find the definition of `CopyTrainerCardData` and change it to the following:
```c
void CopyTrainerCardData(struct TrainerCard *dst, u16 *src, u8 gameVersion, u8 versionModifier);
```
Okay, now we actually need to use that new byte! Open [src/trainer_card.c](https://github.com/pret/pokeemerald/blob/master/src/trainer_card.c) and find the functions `CopyTrainerCardData`, `TrainerCard_GenerateCardForLinkPlayer` and `TrainerCard_GenerateCardForPlayer`.
In `CopyTrainerCardData, make the following changes:
```diff
- void CopyTrainerCardData(struct TrainerCard *dst, u16 *src, u8 gameVersion)
+ void CopyTrainerCardData(struct TrainerCard *dst, u16 *src, u8 gameVersion, u8 versionModifier)
{
memset(dst, 0, sizeof(struct TrainerCard));
dst->version = gameVersion;
- switch (VersionToCardType(gameVersion))
+ switch (VersionToCardType(gameVersion, versionModifier))
{
case CARD_TYPE_FRLG:
memcpy(dst, src, 0x60);
break;
case CARD_TYPE_RS:
memcpy(dst, src, 0x38);
break;
case CARD_TYPE_EMERALD:
memcpy(dst, src, 0x60);
dst->berryCrushPoints = 0;
dst->hasAllSymbols = src[29];
dst->frontierBP = src[30];
break;
}
}
```
In `TrainerCard_GenerateCardForLinkPlayer`, make the following changes:
```diff
static void TrainerCard_GenerateCardForLinkPlayer(struct TrainerCard *trainerCard)
{
memset(trainerCard, 0, sizeof(struct TrainerCard));
trainerCard->version = GAME_VERSION;
+ trainerCard->versionModifier = VERSION_MODIFIER;
- SetPlayerCardData(trainerCard, CARD_TYPE_EMERALD);
+ SetPlayerCardData(trainerCard, VersionToCardType(GAME_VERSION, VERSION_MODIFIER));
trainerCard->hasAllSymbols = HasAllFrontierSymbols();
trainerCard->frontierBP = gSaveBlock2Ptr->frontier.cardBattlePoints;
if (trainerCard->hasAllSymbols)
trainerCard->stars++;
if (trainerCard->gender == FEMALE)
trainerCard->facilityClass = gLinkPlayerFacilityClasses[(trainerCard->trainerId % NUM_FEMALE_LINK_FACILITY_CLASSES) + NUM_MALE_LINK_FACILITY_CLASSES];
else
trainerCard->facilityClass = gLinkPlayerFacilityClasses[trainerCard->trainerId % NUM_MALE_LINK_FACILITY_CLASSES];
}
```
In `TrainerCard_GenerateCardForPlayer`, make the following changes:
```diff
void TrainerCard_GenerateCardForPlayer(struct TrainerCard *trainerCard)
{
- memset(trainerCard, 0, 0x60);
+ memset(trainerCard, 0, sizeof(struct TrainerCard));
trainerCard->version = GAME_VERSION;
+ trainerCard->versionModifier = VERSION_MODIFIER;
- SetPlayerCardData(trainerCard, CARD_TYPE_EMERALD);
+ SetPlayerCardData(trainerCard, VersionToCardType(GAME_VERSION, VERSION_MODIFIER));
trainerCard->hasAllFrontierSymbols = HasAllFrontierSymbols();
*((u16*)&trainerCard->berryCrushPoints) = gSaveBlock2Ptr->frontier.cardBattlePoints;
if (trainerCard->hasAllFrontierSymbols)
trainerCard->stars++;
if (trainerCard->gender == FEMALE)
trainerCard->facilityClass = gLinkPlayerFacilityClasses[(trainerCard->trainerId % NUM_FEMALE_LINK_FACILITY_CLASSES) + NUM_MALE_LINK_FACILITY_CLASSES];
else
trainerCard->facilityClass = gLinkPlayerFacilityClasses[trainerCard->trainerId % NUM_MALE_LINK_FACILITY_CLASSES];
}
```
Now, we need to go to the function `GetSetCardType`. You can use custom cardtype IDs but for demonstration purposes, we're gonna use the vanilla ones with one example. So in `GetSetCardType`, make the following change:
```diff
static u8 GetSetCardType(void)
{
if (sData == NULL)
{
if (gGameVersion == VERSION_FIRE_RED || gGameVersion == VERSION_LEAF_GREEN)
return CARD_TYPE_FRLG;
else if (gGameVersion == VERSION_EMERALD)
return CARD_TYPE_EMERALD;
else
return CARD_TYPE_RS;
}
else
{
if (sData->trainerCard.version == VERSION_FIRE_RED || sData->trainerCard.version == VERSION_LEAF_GREEN)
{
sData->isHoenn = FALSE;
if (sData->trainerCard.versionModifier == MODIFIER_DX)
return CARD_TYPE_EMERALD;
else
return CARD_TYPE_FRLG;
}
else if (sData->trainerCard.version == VERSION_EMERALD)
{
sData->isHoenn = TRUE;
return CARD_TYPE_EMERALD;
}
else
{
sData->isHoenn = TRUE;
return CARD_TYPE_RS;
}
}
}
```
Of course, `CARD_TYPE_EMERALD` uses different gfx and stuff to `CARD_TYPE_FRLG` but it was simply to demonstrate how `versionModifier` could be used in that function.
Okay, now we need to go the function `VersionToCardType` and make the following changes:
```diff
- static u8 VersionToCardType(u8 version)
+ static u8 VersionToCardType(u8 version, u8 versionModifier)
{
if (version == VERSION_FIRE_RED || version == VERSION_LEAF_GREEN)
- return CARD_TYPE_FRLG;
+ {
+ if (versionModifier == MODIFIER_DX)
+ return CARD_TYPE_EMERALD;
+ else
+ return CARD_TYPE_FRLG;
+ }
else if (version == VERSION_EMERALD)
return CARD_TYPE_EMERALD;
else
return CARD_TYPE_RS;
}
```
Same reasoning behind the `GetSetCardType` changes.
There are some static declarations in `src/trainer_card.c`. We need to change `VersionToCardType`'s declaration which is `static u8 VersionToCardType(u8);`, we need to change it to `static u8 VersionToCardType(u8, u8);`
Once I can get around to it, I'll be updating this tutorial so it will show how to add new card types and show how to update the player graphics for the games you're linking in both the overworld and in battle. But for now, this is all I have. If I didn't mess anything up, your game should now use its own unique identifier when linking!
|