forked from pret/pokeemerald
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdaycare.c
1298 lines (1117 loc) · 37.6 KB
/
daycare.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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "global.h"
#include "pokemon.h"
#include "battle.h"
#include "daycare.h"
#include "string_util.h"
#include "mail.h"
#include "pokemon_storage_system.h"
#include "event_data.h"
#include "random.h"
#include "main.h"
#include "egg_hatch.h"
#include "text.h"
#include "menu.h"
#include "international_string_util.h"
#include "script.h"
#include "strings.h"
#include "task.h"
#include "window.h"
#include "party_menu.h"
#include "list_menu.h"
#include "overworld.h"
#include "constants/items.h"
#include "constants/moves.h"
#include "constants/region_map_sections.h"
extern const struct Evolution gEvolutionTable[][EVOS_PER_MON];
static void ClearDaycareMonMail(struct DaycareMail *mail);
static void SetInitialEggData(struct Pokemon *mon, u16 species, struct DayCare *daycare);
static u8 GetDaycareCompatibilityScore(struct DayCare *daycare);
static void DaycarePrintMonInfo(u8 windowId, u32 daycareSlotId, u8 y);
// RAM buffers used to assist with BuildEggMoveset()
EWRAM_DATA static u16 sHatchedEggLevelUpMoves[EGG_LVL_UP_MOVES_ARRAY_COUNT] = {0};
EWRAM_DATA static u16 sHatchedEggFatherMoves[MAX_MON_MOVES] = {0};
EWRAM_DATA static u16 sHatchedEggFinalMoves[MAX_MON_MOVES] = {0};
EWRAM_DATA static u16 sHatchedEggEggMoves[EGG_MOVES_ARRAY_COUNT] = {0};
EWRAM_DATA static u16 sHatchedEggMotherMoves[MAX_MON_MOVES] = {0};
#include "data/pokemon/egg_moves.h"
static const struct WindowTemplate sDaycareLevelMenuWindowTemplate =
{
.bg = 0,
.tilemapLeft = 15,
.tilemapTop = 1,
.width = 14,
.height = 6,
.paletteNum = 15,
.baseBlock = 8
};
// Indices here are assigned by Task_HandleDaycareLevelMenuInput to VAR_RESULT,
// which is copied to VAR_0x8004 and used as an index for GetDaycareCost
static const struct ListMenuItem sLevelMenuItems[] =
{
{gText_ExpandedPlaceholder_Empty, 0},
{gText_ExpandedPlaceholder_Empty, 1},
{gText_Exit, DAYCARE_LEVEL_MENU_EXIT}
};
static const struct ListMenuTemplate sDaycareListMenuLevelTemplate =
{
.items = sLevelMenuItems,
.moveCursorFunc = ListMenuDefaultCursorMoveFunc,
.itemPrintFunc = DaycarePrintMonInfo,
.totalItems = 3,
.maxShowed = 3,
.windowId = 0,
.header_X = 0,
.item_X = 8,
.cursor_X = 0,
.upText_Y = 1,
.cursorPal = 2,
.fillValue = 1,
.cursorShadowPal = 3,
.lettersSpacing = 1,
.itemVerticalPadding = 0,
.scrollMultiple = LIST_NO_MULTIPLE_SCROLL,
.fontId = FONT_NORMAL,
.cursorKind = CURSOR_BLACK_ARROW
};
static const u8 *const sCompatibilityMessages[] =
{
gDaycareText_GetAlongVeryWell,
gDaycareText_GetAlong,
gDaycareText_DontLikeOther,
gDaycareText_PlayOther
};
static const u8 sJapaneseEggNickname[] = _("タマゴ"); // "tamago" ("egg" in Japanese)
u8 *GetMonNickname2(struct Pokemon *mon, u8 *dest)
{
u8 nickname[POKEMON_NAME_BUFFER_SIZE];
GetMonData(mon, MON_DATA_NICKNAME, nickname);
return StringCopy_Nickname(dest, nickname);
}
u8 *GetBoxMonNickname(struct BoxPokemon *mon, u8 *dest)
{
u8 nickname[POKEMON_NAME_BUFFER_SIZE];
GetBoxMonData(mon, MON_DATA_NICKNAME, nickname);
return StringCopy_Nickname(dest, nickname);
}
u8 CountPokemonInDaycare(struct DayCare *daycare)
{
u8 i, count;
count = 0;
for (i = 0; i < DAYCARE_MON_COUNT; i++)
{
if (GetBoxMonData(&daycare->mons[i].mon, MON_DATA_SPECIES) != 0)
count++;
}
return count;
}
void InitDaycareMailRecordMixing(struct DayCare *daycare, struct RecordMixingDaycareMail *mixMail)
{
u8 i;
u8 numDaycareMons = 0;
for (i = 0; i < DAYCARE_MON_COUNT; i++)
{
if (GetBoxMonData(&daycare->mons[i].mon, MON_DATA_SPECIES) != SPECIES_NONE)
{
numDaycareMons++;
if (GetBoxMonData(&daycare->mons[i].mon, MON_DATA_HELD_ITEM) == ITEM_NONE)
mixMail->cantHoldItem[i] = FALSE;
else
mixMail->cantHoldItem[i] = TRUE;
}
else
{
// Daycare slot empty
mixMail->cantHoldItem[i] = TRUE;
}
}
mixMail->numDaycareMons = numDaycareMons;
}
static s8 Daycare_FindEmptySpot(struct DayCare *daycare)
{
u8 i;
for (i = 0; i < DAYCARE_MON_COUNT; i++)
{
if (GetBoxMonData(&daycare->mons[i].mon, MON_DATA_SPECIES) == SPECIES_NONE)
return i;
}
return -1;
}
static void StorePokemonInDaycare(struct Pokemon *mon, struct DaycareMon *daycareMon)
{
if (MonHasMail(mon))
{
u8 mailId;
StringCopy(daycareMon->mail.otName, gSaveBlock2Ptr->playerName);
GetMonNickname2(mon, daycareMon->mail.monName);
StripExtCtrlCodes(daycareMon->mail.monName);
daycareMon->mail.gameLanguage = GAME_LANGUAGE;
daycareMon->mail.monLanguage = GetMonData(mon, MON_DATA_LANGUAGE);
mailId = GetMonData(mon, MON_DATA_MAIL);
daycareMon->mail.message = gSaveBlock1Ptr->mail[mailId];
TakeMailFromMon(mon);
}
daycareMon->mon = mon->box;
BoxMonRestorePP(&daycareMon->mon);
daycareMon->steps = 0;
ZeroMonData(mon);
CompactPartySlots();
CalculatePlayerPartyCount();
}
static void StorePokemonInEmptyDaycareSlot(struct Pokemon *mon, struct DayCare *daycare)
{
s8 slotId = Daycare_FindEmptySpot(daycare);
StorePokemonInDaycare(mon, &daycare->mons[slotId]);
}
void StoreSelectedPokemonInDaycare(void)
{
u8 monId = GetCursorSelectionMonId();
StorePokemonInEmptyDaycareSlot(&gPlayerParty[monId], &gSaveBlock1Ptr->daycare);
}
// Shifts the second daycare Pokémon slot into the first slot.
static void ShiftDaycareSlots(struct DayCare *daycare)
{
// This condition is only satisfied when the player takes out the first Pokémon from the daycare.
if (GetBoxMonData(&daycare->mons[1].mon, MON_DATA_SPECIES) != SPECIES_NONE
&& GetBoxMonData(&daycare->mons[0].mon, MON_DATA_SPECIES) == SPECIES_NONE)
{
daycare->mons[0].mon = daycare->mons[1].mon;
ZeroBoxMonData(&daycare->mons[1].mon);
daycare->mons[0].mail = daycare->mons[1].mail;
daycare->mons[0].steps = daycare->mons[1].steps;
daycare->mons[1].steps = 0;
ClearDaycareMonMail(&daycare->mons[1].mail);
}
}
static void ApplyDaycareExperience(struct Pokemon *mon)
{
s32 i;
bool8 firstMove;
u16 learnedMove;
for (i = 0; i < MAX_LEVEL; i++)
{
// Add the mon's gained daycare experience level by level until it can't level up anymore.
if (TryIncrementMonLevel(mon))
{
// Teach the mon new moves it learned while in the daycare.
firstMove = TRUE;
while ((learnedMove = MonTryLearningNewMove(mon, firstMove)) != 0)
{
firstMove = FALSE;
if (learnedMove == MON_HAS_MAX_MOVES)
DeleteFirstMoveAndGiveMoveToMon(mon, gMoveToLearn);
}
}
else
{
break;
}
}
// Re-calculate the mons stats at its new level.
CalculateMonStats(mon);
}
static u16 TakeSelectedPokemonFromDaycare(struct DaycareMon *daycareMon)
{
u16 species;
u32 experience;
struct Pokemon pokemon;
GetBoxMonNickname(&daycareMon->mon, gStringVar1);
species = GetBoxMonData(&daycareMon->mon, MON_DATA_SPECIES);
BoxMonToMon(&daycareMon->mon, &pokemon);
if (GetMonData(&pokemon, MON_DATA_LEVEL) != MAX_LEVEL)
{
experience = GetMonData(&pokemon, MON_DATA_EXP) + daycareMon->steps;
SetMonData(&pokemon, MON_DATA_EXP, &experience);
ApplyDaycareExperience(&pokemon);
}
gPlayerParty[PARTY_SIZE - 1] = pokemon;
if (daycareMon->mail.message.itemId)
{
GiveMailToMon(&gPlayerParty[PARTY_SIZE - 1], &daycareMon->mail.message);
ClearDaycareMonMail(&daycareMon->mail);
}
ZeroBoxMonData(&daycareMon->mon);
daycareMon->steps = 0;
CompactPartySlots();
CalculatePlayerPartyCount();
return species;
}
static u16 TakeSelectedPokemonMonFromDaycareShiftSlots(struct DayCare *daycare, u8 slotId)
{
u16 species = TakeSelectedPokemonFromDaycare(&daycare->mons[slotId]);
ShiftDaycareSlots(daycare);
return species;
}
u16 TakePokemonFromDaycare(void)
{
return TakeSelectedPokemonMonFromDaycareShiftSlots(&gSaveBlock1Ptr->daycare, gSpecialVar_0x8004);
}
static u8 GetLevelAfterDaycareSteps(struct BoxPokemon *mon, u32 steps)
{
struct BoxPokemon tempMon = *mon;
u32 experience = GetBoxMonData(mon, MON_DATA_EXP) + steps;
SetBoxMonData(&tempMon, MON_DATA_EXP, &experience);
return GetLevelFromBoxMonExp(&tempMon);
}
static u8 GetNumLevelsGainedFromSteps(struct DaycareMon *daycareMon)
{
u8 levelBefore;
u8 levelAfter;
levelBefore = GetLevelFromBoxMonExp(&daycareMon->mon);
levelAfter = GetLevelAfterDaycareSteps(&daycareMon->mon, daycareMon->steps);
return levelAfter - levelBefore;
}
static u8 GetNumLevelsGainedForDaycareMon(struct DaycareMon *daycareMon)
{
u8 numLevelsGained = GetNumLevelsGainedFromSteps(daycareMon);
ConvertIntToDecimalStringN(gStringVar2, numLevelsGained, STR_CONV_MODE_LEFT_ALIGN, 2);
GetBoxMonNickname(&daycareMon->mon, gStringVar1);
return numLevelsGained;
}
static u32 GetDaycareCostForSelectedMon(struct DaycareMon *daycareMon)
{
u32 cost;
u8 numLevelsGained = GetNumLevelsGainedFromSteps(daycareMon);
GetBoxMonNickname(&daycareMon->mon, gStringVar1);
cost = 100 + 100 * numLevelsGained;
ConvertIntToDecimalStringN(gStringVar2, cost, STR_CONV_MODE_LEFT_ALIGN, 5);
return cost;
}
static u16 GetDaycareCostForMon(struct DayCare *daycare, u8 slotId)
{
return GetDaycareCostForSelectedMon(&daycare->mons[slotId]);
}
void GetDaycareCost(void)
{
gSpecialVar_0x8005 = GetDaycareCostForMon(&gSaveBlock1Ptr->daycare, gSpecialVar_0x8004);
}
static void UNUSED Debug_AddDaycareSteps(u16 numSteps)
{
gSaveBlock1Ptr->daycare.mons[0].steps += numSteps;
gSaveBlock1Ptr->daycare.mons[1].steps += numSteps;
}
u8 GetNumLevelsGainedFromDaycare(void)
{
if (GetBoxMonData(&gSaveBlock1Ptr->daycare.mons[gSpecialVar_0x8004].mon, MON_DATA_SPECIES) != 0)
return GetNumLevelsGainedForDaycareMon(&gSaveBlock1Ptr->daycare.mons[gSpecialVar_0x8004]);
return 0;
}
static void ClearDaycareMonMail(struct DaycareMail *mail)
{
s32 i;
for (i = 0; i < PLAYER_NAME_LENGTH + 1; i++)
mail->otName[i] = 0;
for (i = 0; i < POKEMON_NAME_LENGTH + 1; i++)
mail->monName[i] = 0;
ClearMail(&mail->message);
}
static void ClearDaycareMon(struct DaycareMon *daycareMon)
{
ZeroBoxMonData(&daycareMon->mon);
daycareMon->steps = 0;
ClearDaycareMonMail(&daycareMon->mail);
}
static void UNUSED ClearAllDaycareData(struct DayCare *daycare)
{
u8 i;
for (i = 0; i < DAYCARE_MON_COUNT; i++)
ClearDaycareMon(&daycare->mons[i]);
daycare->offspringPersonality = 0;
daycare->stepCounter = 0;
}
// Determines what the species of an Egg would be based on the given species.
// It determines this by working backwards through the evolution chain of the
// given species.
static u16 GetEggSpecies(u16 species)
{
int i, j, k;
bool8 found;
// Working backwards up to 5 times seems arbitrary, since the maximum number
// of times would only be 3 for 3-stage evolutions.
for (i = 0; i < EVOS_PER_MON; i++)
{
found = FALSE;
for (j = 1; j < NUM_SPECIES; j++)
{
for (k = 0; k < EVOS_PER_MON; k++)
{
if (gEvolutionTable[j][k].targetSpecies == species)
{
species = j;
found = TRUE;
break;
}
}
if (found)
break;
}
if (j == NUM_SPECIES)
break;
}
return species;
}
static s32 GetParentToInheritNature(struct DayCare *daycare)
{
u32 species[DAYCARE_MON_COUNT];
s32 i;
s32 dittoCount;
s32 parent = -1;
// search for female gender
for (i = 0; i < DAYCARE_MON_COUNT; i++)
{
if (GetBoxMonGender(&daycare->mons[i].mon) == MON_FEMALE)
parent = i;
}
// search for ditto
for (dittoCount = 0, i = 0; i < DAYCARE_MON_COUNT; i++)
{
species[i] = GetBoxMonData(&daycare->mons[i].mon, MON_DATA_SPECIES);
if (species[i] == SPECIES_DITTO)
dittoCount++, parent = i;
}
// coin flip on ...two Dittos
if (dittoCount == DAYCARE_MON_COUNT)
{
if (Random() >= USHRT_MAX / 2)
parent = 0;
else
parent = 1;
}
// Don't inherit nature if not holding Everstone
if (GetBoxMonData(&daycare->mons[parent].mon, MON_DATA_HELD_ITEM) != ITEM_EVERSTONE
|| Random() >= USHRT_MAX / 2)
{
return -1;
}
return parent;
}
static void _TriggerPendingDaycareEgg(struct DayCare *daycare)
{
s32 parent;
s32 natureTries = 0;
SeedRng2(gMain.vblankCounter2);
parent = GetParentToInheritNature(daycare);
// don't inherit nature
if (parent < 0)
{
daycare->offspringPersonality = (Random2() << 16) | ((Random() % 0xfffe) + 1);
}
// inherit nature
else
{
u8 wantedNature = GetNatureFromPersonality(GetBoxMonData(&daycare->mons[parent].mon, MON_DATA_PERSONALITY, NULL));
u32 personality;
do
{
personality = (Random2() << 16) | (Random());
if (wantedNature == GetNatureFromPersonality(personality) && personality != 0)
break; // found a personality with the same nature
natureTries++;
} while (natureTries <= 2400);
daycare->offspringPersonality = personality;
}
FlagSet(FLAG_PENDING_DAYCARE_EGG);
}
// Functionally unused
static void _TriggerPendingDaycareMaleEgg(struct DayCare *daycare)
{
daycare->offspringPersonality = (Random()) | (EGG_GENDER_MALE);
FlagSet(FLAG_PENDING_DAYCARE_EGG);
}
void TriggerPendingDaycareEgg(void)
{
_TriggerPendingDaycareEgg(&gSaveBlock1Ptr->daycare);
}
static void UNUSED TriggerPendingDaycareMaleEgg(void)
{
_TriggerPendingDaycareMaleEgg(&gSaveBlock1Ptr->daycare);
}
// Removes the selected index from the given IV list and shifts the remaining
// elements to the left.
static void RemoveIVIndexFromList(u8 *ivs, u8 selectedIv)
{
s32 i, j;
u8 temp[NUM_STATS];
ivs[selectedIv] = 0xFF;
for (i = 0; i < NUM_STATS; i++)
{
temp[i] = ivs[i];
}
j = 0;
for (i = 0; i < NUM_STATS; i++)
{
if (temp[i] != 0xFF)
ivs[j++] = temp[i];
}
}
static void InheritIVs(struct Pokemon *egg, struct DayCare *daycare)
{
u8 i;
u8 selectedIvs[INHERITED_IV_COUNT];
u8 availableIVs[NUM_STATS];
u8 whichParents[INHERITED_IV_COUNT];
u8 iv;
// Initialize a list of IV indices.
for (i = 0; i < NUM_STATS; i++)
{
availableIVs[i] = i;
}
// Select the 3 IVs that will be inherited.
for (i = 0; i < INHERITED_IV_COUNT; i++)
{
// Randomly pick an IV from the available list and stop from being chosen again.
// BUG: Instead of removing the IV that was just picked, this
// removes position 0 (HP) then position 1 (DEF), then position 2. This is why HP and DEF
// have a lower chance to be inherited in Emerald and why the IV picked for inheritance can
// be repeated. Amusingly, FRLG and RS also got this wrong. They remove selectedIvs[i], which
// is not an index! This means that it can sometimes remove the wrong stat.
#ifndef BUGFIX
selectedIvs[i] = availableIVs[Random() % (NUM_STATS - i)];
RemoveIVIndexFromList(availableIVs, i);
#else
u8 index = Random() % (NUM_STATS - i);
selectedIvs[i] = availableIVs[index];
RemoveIVIndexFromList(availableIVs, index);
#endif
}
// Determine which parent each of the selected IVs should inherit from.
for (i = 0; i < INHERITED_IV_COUNT; i++)
{
whichParents[i] = Random() % DAYCARE_MON_COUNT;
}
// Set each of inherited IVs on the egg mon.
for (i = 0; i < INHERITED_IV_COUNT; i++)
{
switch (selectedIvs[i])
{
case 0:
iv = GetBoxMonData(&daycare->mons[whichParents[i]].mon, MON_DATA_HP_IV);
SetMonData(egg, MON_DATA_HP_IV, &iv);
break;
case 1:
iv = GetBoxMonData(&daycare->mons[whichParents[i]].mon, MON_DATA_ATK_IV);
SetMonData(egg, MON_DATA_ATK_IV, &iv);
break;
case 2:
iv = GetBoxMonData(&daycare->mons[whichParents[i]].mon, MON_DATA_DEF_IV);
SetMonData(egg, MON_DATA_DEF_IV, &iv);
break;
case 3:
iv = GetBoxMonData(&daycare->mons[whichParents[i]].mon, MON_DATA_SPEED_IV);
SetMonData(egg, MON_DATA_SPEED_IV, &iv);
break;
case 4:
iv = GetBoxMonData(&daycare->mons[whichParents[i]].mon, MON_DATA_SPATK_IV);
SetMonData(egg, MON_DATA_SPATK_IV, &iv);
break;
case 5:
iv = GetBoxMonData(&daycare->mons[whichParents[i]].mon, MON_DATA_SPDEF_IV);
SetMonData(egg, MON_DATA_SPDEF_IV, &iv);
break;
}
}
}
// Counts the number of egg moves a Pokémon learns and stores the moves in
// the given array.
static u8 GetEggMoves(struct Pokemon *pokemon, u16 *eggMoves)
{
u16 eggMoveIdx;
u16 numEggMoves;
u16 species;
u16 i;
numEggMoves = 0;
eggMoveIdx = 0;
species = GetMonData(pokemon, MON_DATA_SPECIES);
for (i = 0; i < ARRAY_COUNT(gEggMoves) - 1; i++)
{
if (gEggMoves[i] == species + EGG_MOVES_SPECIES_OFFSET)
{
eggMoveIdx = i + 1;
break;
}
}
for (i = 0; i < EGG_MOVES_ARRAY_COUNT; i++)
{
if (gEggMoves[eggMoveIdx + i] > EGG_MOVES_SPECIES_OFFSET)
break;
eggMoves[i] = gEggMoves[eggMoveIdx + i];
numEggMoves++;
}
return numEggMoves;
}
static void BuildEggMoveset(struct Pokemon *egg, struct BoxPokemon *father, struct BoxPokemon *mother)
{
u16 numSharedParentMoves;
u32 numLevelUpMoves;
u16 numEggMoves;
u16 i, j;
numSharedParentMoves = 0;
for (i = 0; i < MAX_MON_MOVES; i++)
{
sHatchedEggMotherMoves[i] = MOVE_NONE;
sHatchedEggFatherMoves[i] = MOVE_NONE;
sHatchedEggFinalMoves[i] = MOVE_NONE;
}
for (i = 0; i < EGG_MOVES_ARRAY_COUNT; i++)
sHatchedEggEggMoves[i] = MOVE_NONE;
for (i = 0; i < EGG_LVL_UP_MOVES_ARRAY_COUNT; i++)
sHatchedEggLevelUpMoves[i] = MOVE_NONE;
numLevelUpMoves = GetLevelUpMovesBySpecies(GetMonData(egg, MON_DATA_SPECIES), sHatchedEggLevelUpMoves);
for (i = 0; i < MAX_MON_MOVES; i++)
{
sHatchedEggFatherMoves[i] = GetBoxMonData(father, MON_DATA_MOVE1 + i);
sHatchedEggMotherMoves[i] = GetBoxMonData(mother, MON_DATA_MOVE1 + i);
}
numEggMoves = GetEggMoves(egg, sHatchedEggEggMoves);
for (i = 0; i < MAX_MON_MOVES; i++)
{
if (sHatchedEggFatherMoves[i] != MOVE_NONE)
{
for (j = 0; j < numEggMoves; j++)
{
if (sHatchedEggFatherMoves[i] == sHatchedEggEggMoves[j])
{
if (GiveMoveToMon(egg, sHatchedEggFatherMoves[i]) == MON_HAS_MAX_MOVES)
DeleteFirstMoveAndGiveMoveToMon(egg, sHatchedEggFatherMoves[i]);
break;
}
}
}
else
{
break;
}
}
for (i = 0; i < MAX_MON_MOVES; i++)
{
if (sHatchedEggFatherMoves[i] != MOVE_NONE)
{
for (j = 0; j < NUM_TECHNICAL_MACHINES + NUM_HIDDEN_MACHINES; j++)
{
if (sHatchedEggFatherMoves[i] == ItemIdToBattleMoveId(ITEM_TM01 + j) && CanMonLearnTMHM(egg, j))
{
if (GiveMoveToMon(egg, sHatchedEggFatherMoves[i]) == MON_HAS_MAX_MOVES)
DeleteFirstMoveAndGiveMoveToMon(egg, sHatchedEggFatherMoves[i]);
}
}
}
}
for (i = 0; i < MAX_MON_MOVES; i++)
{
if (sHatchedEggFatherMoves[i] == MOVE_NONE)
break;
for (j = 0; j < MAX_MON_MOVES; j++)
{
if (sHatchedEggFatherMoves[i] == sHatchedEggMotherMoves[j] && sHatchedEggFatherMoves[i] != MOVE_NONE)
sHatchedEggFinalMoves[numSharedParentMoves++] = sHatchedEggFatherMoves[i];
}
}
for (i = 0; i < MAX_MON_MOVES; i++)
{
if (sHatchedEggFinalMoves[i] == MOVE_NONE)
break;
for (j = 0; j < numLevelUpMoves; j++)
{
if (sHatchedEggLevelUpMoves[j] != MOVE_NONE && sHatchedEggFinalMoves[i] == sHatchedEggLevelUpMoves[j])
{
if (GiveMoveToMon(egg, sHatchedEggFinalMoves[i]) == MON_HAS_MAX_MOVES)
DeleteFirstMoveAndGiveMoveToMon(egg, sHatchedEggFinalMoves[i]);
break;
}
}
}
}
static void RemoveEggFromDayCare(struct DayCare *daycare)
{
daycare->offspringPersonality = 0;
daycare->stepCounter = 0;
}
void RejectEggFromDayCare(void)
{
RemoveEggFromDayCare(&gSaveBlock1Ptr->daycare);
}
static void AlterEggSpeciesWithIncenseItem(u16 *species, struct DayCare *daycare)
{
u16 motherItem, fatherItem;
if (*species == SPECIES_WYNAUT || *species == SPECIES_AZURILL)
{
motherItem = GetBoxMonData(&daycare->mons[0].mon, MON_DATA_HELD_ITEM);
fatherItem = GetBoxMonData(&daycare->mons[1].mon, MON_DATA_HELD_ITEM);
if (*species == SPECIES_WYNAUT && motherItem != ITEM_LAX_INCENSE && fatherItem != ITEM_LAX_INCENSE)
{
*species = SPECIES_WOBBUFFET;
}
if (*species == SPECIES_AZURILL && motherItem != ITEM_SEA_INCENSE && fatherItem != ITEM_SEA_INCENSE)
{
*species = SPECIES_MARILL;
}
}
}
static void GiveVoltTackleIfLightBall(struct Pokemon *mon, struct DayCare *daycare)
{
u32 motherItem = GetBoxMonData(&daycare->mons[0].mon, MON_DATA_HELD_ITEM);
u32 fatherItem = GetBoxMonData(&daycare->mons[1].mon, MON_DATA_HELD_ITEM);
if (motherItem == ITEM_LIGHT_BALL || fatherItem == ITEM_LIGHT_BALL)
{
if (GiveMoveToMon(mon, MOVE_VOLT_TACKLE) == MON_HAS_MAX_MOVES)
DeleteFirstMoveAndGiveMoveToMon(mon, MOVE_VOLT_TACKLE);
}
}
static u16 DetermineEggSpeciesAndParentSlots(struct DayCare *daycare, u8 *parentSlots)
{
u16 i;
u16 species[DAYCARE_MON_COUNT];
u16 eggSpecies;
for (i = 0; i < DAYCARE_MON_COUNT; i++)
{
species[i] = GetBoxMonData(&daycare->mons[i].mon, MON_DATA_SPECIES);
if (species[i] == SPECIES_DITTO)
{
parentSlots[0] = i ^ 1;
parentSlots[1] = i;
}
else if (GetBoxMonGender(&daycare->mons[i].mon) == MON_FEMALE)
{
parentSlots[0] = i;
parentSlots[1] = i ^ 1;
}
}
eggSpecies = GetEggSpecies(species[parentSlots[0]]);
if (eggSpecies == SPECIES_NIDORAN_F && daycare->offspringPersonality & EGG_GENDER_MALE)
{
eggSpecies = SPECIES_NIDORAN_M;
}
if (eggSpecies == SPECIES_ILLUMISE && daycare->offspringPersonality & EGG_GENDER_MALE)
{
eggSpecies = SPECIES_VOLBEAT;
}
// Make Ditto the "mother" slot if the other daycare mon is male.
if (species[parentSlots[1]] == SPECIES_DITTO && GetBoxMonGender(&daycare->mons[parentSlots[0]].mon) != MON_FEMALE)
{
u8 ditto = parentSlots[1];
parentSlots[1] = parentSlots[0];
parentSlots[0] = ditto;
}
return eggSpecies;
}
static void _GiveEggFromDaycare(struct DayCare *daycare)
{
struct Pokemon egg;
u16 species;
u8 parentSlots[DAYCARE_MON_COUNT];
bool8 isEgg;
species = DetermineEggSpeciesAndParentSlots(daycare, parentSlots);
AlterEggSpeciesWithIncenseItem(&species, daycare);
SetInitialEggData(&egg, species, daycare);
InheritIVs(&egg, daycare);
BuildEggMoveset(&egg, &daycare->mons[parentSlots[1]].mon, &daycare->mons[parentSlots[0]].mon);
if (species == SPECIES_PICHU)
GiveVoltTackleIfLightBall(&egg, daycare);
isEgg = TRUE;
SetMonData(&egg, MON_DATA_IS_EGG, &isEgg);
gPlayerParty[PARTY_SIZE - 1] = egg;
CompactPartySlots();
CalculatePlayerPartyCount();
RemoveEggFromDayCare(daycare);
}
void CreateEgg(struct Pokemon *mon, u16 species, bool8 setHotSpringsLocation)
{
u8 metLevel;
u16 ball;
u8 language;
u8 metLocation;
u8 isEgg;
CreateMon(mon, species, EGG_HATCH_LEVEL, USE_RANDOM_IVS, FALSE, 0, OT_ID_PLAYER_ID, 0);
metLevel = 0;
ball = ITEM_POKE_BALL;
language = LANGUAGE_JAPANESE;
SetMonData(mon, MON_DATA_POKEBALL, &ball);
SetMonData(mon, MON_DATA_NICKNAME, sJapaneseEggNickname);
SetMonData(mon, MON_DATA_FRIENDSHIP, &gSpeciesInfo[species].eggCycles);
SetMonData(mon, MON_DATA_MET_LEVEL, &metLevel);
SetMonData(mon, MON_DATA_LANGUAGE, &language);
if (setHotSpringsLocation)
{
metLocation = METLOC_SPECIAL_EGG;
SetMonData(mon, MON_DATA_MET_LOCATION, &metLocation);
}
isEgg = TRUE;
SetMonData(mon, MON_DATA_IS_EGG, &isEgg);
}
static void SetInitialEggData(struct Pokemon *mon, u16 species, struct DayCare *daycare)
{
u32 personality;
u16 ball;
u8 metLevel;
u8 language;
personality = daycare->offspringPersonality;
CreateMon(mon, species, EGG_HATCH_LEVEL, USE_RANDOM_IVS, TRUE, personality, OT_ID_PLAYER_ID, 0);
metLevel = 0;
ball = ITEM_POKE_BALL;
language = LANGUAGE_JAPANESE;
SetMonData(mon, MON_DATA_POKEBALL, &ball);
SetMonData(mon, MON_DATA_NICKNAME, sJapaneseEggNickname);
SetMonData(mon, MON_DATA_FRIENDSHIP, &gSpeciesInfo[species].eggCycles);
SetMonData(mon, MON_DATA_MET_LEVEL, &metLevel);
SetMonData(mon, MON_DATA_LANGUAGE, &language);
}
void GiveEggFromDaycare(void)
{
_GiveEggFromDaycare(&gSaveBlock1Ptr->daycare);
}
static bool8 TryProduceOrHatchEgg(struct DayCare *daycare)
{
u32 i, validEggs = 0;
for (i = 0; i < DAYCARE_MON_COUNT; i++)
{
if (GetBoxMonData(&daycare->mons[i].mon, MON_DATA_SANITY_HAS_SPECIES))
daycare->mons[i].steps++, validEggs++;
}
// Check if an egg should be produced
if (daycare->offspringPersonality == 0 && validEggs == DAYCARE_MON_COUNT && (daycare->mons[1].steps & 0xFF) == 0xFF)
{
u8 compatibility = GetDaycareCompatibilityScore(daycare);
if (compatibility > (Random() * 100u) / USHRT_MAX)
TriggerPendingDaycareEgg();
}
// Try to hatch Egg
if (++daycare->stepCounter == 255)
{
u32 eggCycles;
u8 toSub = GetEggCyclesToSubtract();
for (i = 0; i < gPlayerPartyCount; i++)
{
if (!GetMonData(&gPlayerParty[i], MON_DATA_IS_EGG))
continue;
if (GetMonData(&gPlayerParty[i], MON_DATA_SANITY_IS_BAD_EGG))
continue;
eggCycles = GetMonData(&gPlayerParty[i], MON_DATA_FRIENDSHIP);
if (eggCycles != 0)
{
if (eggCycles >= toSub)
eggCycles -= toSub;
else
eggCycles -= 1;
SetMonData(&gPlayerParty[i], MON_DATA_FRIENDSHIP, &eggCycles);
}
else
{
gSpecialVar_0x8004 = i;
return TRUE;
}
}
}
return FALSE;
}
bool8 ShouldEggHatch(void)
{
return TryProduceOrHatchEgg(&gSaveBlock1Ptr->daycare);
}
static bool8 IsEggPending(struct DayCare *daycare)
{
return (daycare->offspringPersonality != 0);
}
// gStringVar1 = first mon's nickname
// gStringVar2 = second mon's nickname
// gStringVar3 = first mon trainer's name
static void _GetDaycareMonNicknames(struct DayCare *daycare)
{
u8 otName[max(12, PLAYER_NAME_LENGTH + 1)];
if (GetBoxMonData(&daycare->mons[0].mon, MON_DATA_SPECIES) != 0)
{
GetBoxMonNickname(&daycare->mons[0].mon, gStringVar1);
GetBoxMonData(&daycare->mons[0].mon, MON_DATA_OT_NAME, otName);
StringCopy(gStringVar3, otName);
}
if (GetBoxMonData(&daycare->mons[1].mon, MON_DATA_SPECIES) != 0)
{
GetBoxMonNickname(&daycare->mons[1].mon, gStringVar2);
}
}
u16 GetSelectedMonNicknameAndSpecies(void)
{
GetBoxMonNickname(&gPlayerParty[GetCursorSelectionMonId()].box, gStringVar1);
return GetBoxMonData(&gPlayerParty[GetCursorSelectionMonId()].box, MON_DATA_SPECIES);
}
void GetDaycareMonNicknames(void)
{
_GetDaycareMonNicknames(&gSaveBlock1Ptr->daycare);
}
u8 GetDaycareState(void)
{
u8 numMons;
if (IsEggPending(&gSaveBlock1Ptr->daycare))
{
return DAYCARE_EGG_WAITING;
}
numMons = CountPokemonInDaycare(&gSaveBlock1Ptr->daycare);
if (numMons != 0)
{
return numMons + 1; // DAYCARE_ONE_MON or DAYCARE_TWO_MONS
}
return DAYCARE_NO_MONS;
}
static u8 UNUSED GetDaycarePokemonCount(void)
{
u8 ret = CountPokemonInDaycare(&gSaveBlock1Ptr->daycare);
if (ret)
return ret;
return 0;
}
// Determine if the two given egg group lists contain any of the
// same egg groups.
static bool8 EggGroupsOverlap(u16 *eggGroups1, u16 *eggGroups2)
{