forked from rh-hideout/pokeemerald-expansion
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdexnav.c
2689 lines (2381 loc) · 84.6 KB
/
dexnav.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 "battle_main.h"
#include "battle_setup.h"
#include "bg.h"
#include "data.h"
#include "daycare.h"
#include "decompress.h"
#include "dexnav.h"
#include "event_data.h"
#include "event_object_movement.h"
#include "event_scripts.h"
#include "field_effect.h"
#include "field_effect_helpers.h"
#include "field_message_box.h"
#include "field_player_avatar.h"
#include "field_screen_effect.h"
#include "fieldmap.h"
#include "gpu_regs.h"
#include "graphics.h"
#include "item.h"
#include "international_string_util.h"
#include "m4a.h"
#include "map_name_popup.h"
#include "main.h"
#include "malloc.h"
#include "menu.h"
#include "menu_helpers.h"
#include "metatile_behavior.h"
#include "move.h"
#include "overworld.h"
#include "palette.h"
#include "party_menu.h"
#include "pokedex.h"
#include "pokemon.h"
#include "pokemon_icon.h"
#include "pokemon_summary_screen.h"
#include "random.h"
#include "region_map.h"
#include "scanline_effect.h"
#include "script.h"
#include "script_pokemon_util.h"
#include "sound.h"
#include "sprite.h"
#include "start_menu.h"
#include "string_util.h"
#include "strings.h"
#include "task.h"
#include "text.h"
#include "text_window.h"
#include "wild_encounter.h"
#include "window.h"
#include "constants/map_types.h"
#include "constants/species.h"
#include "constants/maps.h"
#include "constants/field_effects.h"
#include "constants/items.h"
#include "constants/songs.h"
#include "constants/abilities.h"
#include "constants/rgb.h"
#include "constants/region_map_sections.h"
#include "gba/m4a_internal.h"
#if DEXNAV_ENABLED
STATIC_ASSERT(DN_FLAG_SEARCHING != 0, DNFlagSearching_Must_Not_Be_Zero);
STATIC_ASSERT(DN_FLAG_DETECTOR_MODE != 0, DNFlagDetectorMode_Must_Not_Be_Zero);
STATIC_ASSERT(DN_VAR_SPECIES != 0, DNVarSpecies_Must_Not_Be_Zero);
STATIC_ASSERT(DN_VAR_STEP_COUNTER != 0, DNVarStepCounter_Must_Not_Be_Zero);
#endif
// Defines
enum WindowIds
{
WINDOW_INFO,
WINDOW_REGISTERED,
WINDOW_COUNT,
};
enum Statuses
{
STATUS_INVALID_SEARCH,
STATUS_CHOOSE_MON,
STATUS_LOCKED,
STATUS_NO_DATA,
STATUS_INCORRECT_AREA,
};
struct DexNavSearch
{
u16 species;
u16 moves[MAX_MON_MOVES];
u16 heldItem;
u8 abilityNum;
u8 potential;
u8 searchLevel;
u8 monLevel;
u8 proximity;
u8 environment;
s16 tileX;
s16 tileY;
u8 fldEffSpriteId;
u8 fldEffId;
u8 movementCount;
u8 windowId;
u8 iconSpriteId;
u8 eyeSpriteId;
u8 itemSpriteId;
u8 starSpriteIds[3];
u8 ownedIconSpriteId;
u8 exclamationSpriteId;
u8 hiddenSearch:1;
u8 isHiddenMon:1;
u8 unk:6;
u16 palBuffer[16];
};
struct DexNavGUI
{
MainCallback savedCallback;
u8 state;
u8 cursorSpriteId;
u16 landSpecies[LAND_WILD_COUNT];
u16 waterSpecies[WATER_WILD_COUNT];
u16 hiddenSpecies[HIDDEN_WILD_COUNT];
u8 cursorRow;
u8 cursorCol;
u8 environment;
u8 potential;
u8 typeIconSpriteIds[2];
u8 starSpriteIds[3];
};
// RAM
EWRAM_DATA static struct DexNavSearch *sDexNavSearchDataPtr = NULL;
EWRAM_DATA static struct DexNavGUI *sDexNavUiDataPtr = NULL;
EWRAM_DATA static u8 *sBg1TilemapBuffer = NULL;
EWRAM_DATA bool8 gDexNavBattle = FALSE;
//// Function Declarations
//GUI
static void Task_DexNavWaitFadeIn(u8 taskId);
static void Task_DexNavMain(u8 taskId);
static void PrintCurrentSpeciesInfo(void);
// SEARCH
static bool8 TryStartHiddenMonFieldEffect(u8 environment, u8 xSize, u8 ySize, bool8 smallScan);
static void DexNavGenerateMoveset(u16 species, u8 searchLevel, u8 encounterLevel, u16* moveDst);
static u16 DexNavGenerateHeldItem(u16 species, u8 searchLevel);
static u8 DexNavGetAbilityNum(u16 species, u8 searchLevel);
static u8 DexNavGeneratePotential(u8 searchLevel);
static u8 DexNavTryGenerateMonLevel(u16 species, u8 environment);
static u8 GetEncounterLevelFromMapData(u16 species, u8 environment);
static void CreateDexNavWildMon(u16 species, u8 potential, u8 level, u8 abilityNum, u16 item, u16* moves);
static u8 GetPlayerDistance(s16 x, s16 y);
static u8 DexNavPickTile(u8 environment, u8 xSize, u8 ySize, bool8 smallScan);
static void DexNavProximityUpdate(void);
static void DexNavDrawIcons(void);
static void DexNavUpdateSearchWindow(u8 proximity, u8 searchLevel);
static void Task_DexNavSearch(u8 taskId);
static void EndDexNavSearchSetupScript(const u8 *script, u8 taskId);
// HIDDEN MONS
static void DexNavDrawHiddenIcons(void);
static void DrawHiddenSearchWindow(u8 width);
//// Const Data
// gui image data
static const u32 sDexNavGuiTiles[] = INCBIN_U32("graphics/dexnav/gui_tiles.4bpp.lz");
static const u32 sDexNavGuiTilemap[] = INCBIN_U32("graphics/dexnav/gui_tilemap.bin.lz");
static const u32 sDexNavGuiPal[] = INCBIN_U32("graphics/dexnav/gui.gbapal");
static const u32 sSelectionCursorGfx[] = INCBIN_U32("graphics/dexnav/cursor.4bpp.lz");
static const u16 sSelectionCursorPal[] = INCBIN_U16("graphics/dexnav/cursor.gbapal");
static const u32 sCapturedAllMonsTiles[] = INCBIN_U32("graphics/dexnav/captured_all.4bpp.lz"); //uses selection cursor pal
static const u32 sNoDataGfx[] = INCBIN_U32("graphics/dexnav/no_data.4bpp.lz");
// searching image data
static const u32 sPotentialStarGfx[] = INCBIN_U32("graphics/dexnav/star.4bpp.lz");
static const u32 sHiddenSearchIconGfx[] = INCBIN_U32("graphics/dexnav/hidden_search.4bpp.lz");
static const u32 sOwnedIconGfx[] = INCBIN_U32("graphics/dexnav/owned_icon.4bpp.lz");
static const u32 sHiddenMonIconGfx[] = INCBIN_U32("graphics/dexnav/hidden.4bpp.lz");
// strings
static const u8 sText_DexNav_NoInfo[] = _("--------");
static const u8 sText_DexNav_CaptureToSee[] = _("Capture first!");
static const u8 sText_DexNav_PressRToRegister[] = _("R TO REGISTER!");
static const u8 sText_DexNav_SearchForRegisteredSpecies[] = _("Search {STR_VAR_1}");
static const u8 sText_DexNav_NotFoundHere[] = _("This Pokémon cannot be found here!");
static const u8 sText_ThreeQmarks[] = _("???");
static const u8 sText_SearchLevel[] = _("SEARCH {LV}. {STR_VAR_1}");
static const u8 sText_MonLevel[] = _("{LV}. {STR_VAR_1}");
static const u8 sText_EggMove[] = _("MOVE: {STR_VAR_1}");
static const u8 sText_HeldItem[] = _("{STR_VAR_1}");
static const u8 sText_StartExit[] = _("{START_BUTTON} EXIT");
static const u8 sText_DexNavChain[] = _("{NO} {STR_VAR_1}");
static const u8 sText_DexNavChainLong[] = _("{NO}{STR_VAR_1}");
static const u8 sText_ArrowLeft[] = _("{LEFT_ARROW}");
static const u8 sText_ArrowRight[] = _("{RIGHT_ARROW}");
static const u8 sText_ArrowUp[] = _("{UP_ARROW}");
static const u8 sText_ArrowDown[] = _("{DOWN_ARROW}");
static const struct WindowTemplate sDexNavGuiWindowTemplates[] =
{
[WINDOW_INFO] =
{
.bg = 0,
.tilemapLeft = 21,
.tilemapTop = 5,
.width = 9,
.height = 15,
.paletteNum = 15,
.baseBlock = 1,
},
[WINDOW_REGISTERED] =
{
.bg = 0,
.tilemapLeft = 4,
.tilemapTop = 0,
.width = 26,
.height = 2,
.paletteNum = 15,
.baseBlock = 200,
},
DUMMY_WIN_TEMPLATE
};
//gui font
static const u8 sFontColor_Black[3] = {TEXT_COLOR_TRANSPARENT, TEXT_COLOR_DARK_GRAY, TEXT_COLOR_LIGHT_GRAY};
static const u8 sFontColor_White[3] = {TEXT_COLOR_TRANSPARENT, TEXT_COLOR_WHITE, TEXT_COLOR_DARK_GRAY};
//search window font
static const u8 sSearchFontColor[3] = {0, 15, 13};
static const struct OamData sNoDataIconOam =
{
.affineMode = ST_OAM_AFFINE_OFF,
.objMode = ST_OAM_OBJ_NORMAL,
.shape = SPRITE_SHAPE(32x32),
.size = SPRITE_SIZE(32x32),
.priority = 1,
};
static const struct OamData sHeldItemOam =
{
.affineMode = ST_OAM_AFFINE_OFF,
.objMode = ST_OAM_OBJ_NORMAL,
.shape = SPRITE_SHAPE(8x8),
.size = SPRITE_SIZE(8x8),
.priority = 0,
.paletteNum = 13,
};
static const struct OamData sCapturedAllOam =
{
.y = 0,
.affineMode = 1,
.objMode = 0,
.mosaic = 0,
.bpp = 0,
.shape = SPRITE_SHAPE(8x8),
.x = 0,
.matrixNum = 0,
.size = SPRITE_SIZE(8x8),
.tileNum = 0,
.priority = 0, //Highest
.paletteNum = 12,
.affineParam = 0,
};
static const struct OamData sSearchIconOam =
{
.y = 0,
.affineMode = 0,
.objMode = 0,
.mosaic = 0,
.bpp = 0,
.shape = 0,
.x = 0,
.matrixNum = 0,
.size = SPRITE_SIZE(32x32),
.tileNum = 0,
.priority = 0, // above BG layers
.paletteNum = 13,
.affineParam = 0
};
static const struct OamData sSelectionCursorOam =
{
.y = 0,
.affineMode = 0,
.objMode = 0,
.mosaic = 0,
.bpp = 0,
.shape = 0,
.x = 0,
.matrixNum = 0,
.size = SPRITE_SIZE(32x32),
.tileNum = 0,
.priority = 0, // above BG layers
.paletteNum = 12,
.affineParam = 0
};
static const struct OamData sSightOam =
{
.affineMode = ST_OAM_AFFINE_OFF,
.objMode = ST_OAM_OBJ_NORMAL,
.shape = SPRITE_SHAPE(16x8),
.size = SPRITE_SIZE(16x8),
.priority = 0,
};
static const union AnimCmd sAnimCmdSight0[] =
{
ANIMCMD_FRAME(0, 1),
ANIMCMD_END
};
static const union AnimCmd sAnimCmdSight1[] =
{
ANIMCMD_FRAME(2, 1),
ANIMCMD_END
};
static const union AnimCmd sAnimCmdSight2[] =
{
ANIMCMD_FRAME(4, 1),
ANIMCMD_END
};
static const union AnimCmd *const sAnimCmdTable_Sight[] =
{
sAnimCmdSight0,
sAnimCmdSight1,
sAnimCmdSight2,
};
// gui sprite templates
static const struct SpriteTemplate sNoDataIconTemplate =
{
.tileTag = ICON_GFX_TAG,
.paletteTag = ICON_PAL_TAG,
.oam = &sNoDataIconOam,
.anims = gDummySpriteAnimTable,
.images = NULL,
.affineAnims = gDummySpriteAffineAnimTable,
.callback = SpriteCallbackDummy,
};
static const struct SpriteTemplate sCaptureAllMonsSpriteTemplate =
{
.tileTag = CAPTURED_ALL_TAG,
.paletteTag = 0xFFFF,
.oam = &sCapturedAllOam,
.anims = gDummySpriteAnimTable,
.images = NULL,
.affineAnims = gDummySpriteAffineAnimTable,
.callback = SpriteCallbackDummy,
};
static const struct SpriteTemplate sSelectionCursorSpriteTemplate =
{
.tileTag = SELECTION_CURSOR_TAG,
.paletteTag = 0xFFFF,
.oam = &sSelectionCursorOam,
.anims = gDummySpriteAnimTable,
.images = NULL,
.affineAnims = gDummySpriteAffineAnimTable,
.callback = SpriteCallbackDummy,
};
// search window sprite templates
static const struct SpriteTemplate sHeldItemTemplate =
{
.tileTag = HELD_ITEM_TAG,
.paletteTag = 0xFFFF,
.oam = &sHeldItemOam,
.anims = gDummySpriteAnimTable,
.images = NULL,
.affineAnims = gDummySpriteAffineAnimTable,
.callback = SpriteCallbackDummy,
};
static const struct SpriteTemplate sPotentialStarTemplate =
{
.tileTag = LIT_STAR_TILE_TAG,
.paletteTag = 0xFFFF, //held item pal
.oam = &sHeldItemOam,
.anims = gDummySpriteAnimTable,
.images = NULL,
.affineAnims = gDummySpriteAffineAnimTable,
.callback = SpriteCallbackDummy,
};
static const struct SpriteTemplate sSearchIconSpriteTemplate =
{
.tileTag = HIDDEN_SEARCH_TAG,
.paletteTag = 0xFFFF, //held item pal
.oam = &sSearchIconOam,
.anims = gDummySpriteAnimTable,
.images = NULL,
.affineAnims = gDummySpriteAffineAnimTable,
.callback = SpriteCallbackDummy,
};
static const struct SpriteTemplate sOwnedIconTemplate =
{
.tileTag = OWNED_ICON_TAG,
.paletteTag = 0xFFFF, //held item pal
.oam = &sHeldItemOam,
.anims = gDummySpriteAnimTable,
.images = NULL,
.affineAnims = gDummySpriteAffineAnimTable,
.callback = SpriteCallbackDummy,
};
static const struct SpriteTemplate sHiddenMonIconTemplate =
{
.tileTag = HIDDEN_MON_ICON_TAG,
.paletteTag = 0xFFFF, //held item pal
.oam = &sHeldItemOam,
.anims = gDummySpriteAnimTable,
.images = NULL,
.affineAnims = gDummySpriteAffineAnimTable,
.callback = SpriteCallbackDummy,
};
// gui sprite sheets
static const struct CompressedSpriteSheet sNoDataIconSpriteSheet = {sNoDataGfx, (32 * 32) / 2, ICON_GFX_TAG};
static const struct CompressedSpriteSheet sCapturedAllPokemonSpriteSheet = {sCapturedAllMonsTiles, (8 * 8) / 2, CAPTURED_ALL_TAG};
// search sprite sheets
static const struct CompressedSpriteSheet sPotentialStarSpriteSheet = {sPotentialStarGfx, (8 * 8) / 2, LIT_STAR_TILE_TAG};
static const struct CompressedSpriteSheet sOwnedIconSpriteSheet = {sOwnedIconGfx, (8 * 8) / 2, OWNED_ICON_TAG};
static const struct CompressedSpriteSheet sHiddenMonIconSpriteSheet = {sHiddenMonIconGfx, (8 * 8) / 2, HIDDEN_MON_ICON_TAG};
//// functions
///////////////////////
//// DEXNAV SEARCH ////
///////////////////////
static s16 GetSearchWindowY(void)
{
return (GetWindowAttribute(sDexNavSearchDataPtr->windowId, WINDOW_TILEMAP_TOP) * 8);
}
#define SPECIES_ICON_X 28
static void DrawDexNavSearchMonIcon(u16 species, u8 *dst, bool8 owned)
{
u8 spriteId;
LoadMonIconPalette(species);
spriteId = CreateMonIcon(species, SpriteCB_MonIcon, SPECIES_ICON_X - 6, GetSearchWindowY() + 8, 0, 0xFFFFFFFF);
gSprites[spriteId].oam.priority = 0;
*dst = spriteId;
if (owned)
sDexNavSearchDataPtr->ownedIconSpriteId = CreateSprite(&sOwnedIconTemplate, SPECIES_ICON_X + 6, GetSearchWindowY() + 4, 0);
}
static void AddSearchWindow(u8 width)
{
struct WindowTemplate template;
u16 y = 16;
if (sDexNavSearchDataPtr->tileY > (gSaveBlock1Ptr->pos.y + 7))
y = 1; //draw at top if chosen tile is below
LoadDexNavWindowGfx(sDexNavSearchDataPtr->windowId, 0x1d5, 14 * 16);
SetWindowTemplateFields(&template, 0, 1, y, width, 3, 14, 8);
sDexNavSearchDataPtr->windowId = AddWindow(&template);
FillWindowPixelBuffer(sDexNavSearchDataPtr->windowId, PIXEL_FILL(1));
PutWindowTilemap(sDexNavSearchDataPtr->windowId);
CopyWindowToVram(sDexNavSearchDataPtr->windowId, 3);
DrawStdFrameWithCustomTileAndPalette(sDexNavSearchDataPtr->windowId, TRUE, 0x214, 14);
}
#define WINDOW_COL_0 (SPECIES_ICON_X + 4)
#define WINDOW_COL_1 (WINDOW_COL_0 + (GetFontAttribute(sDexNavSearchDataPtr->windowId, FONTATTR_MAX_LETTER_WIDTH) * (POKEMON_NAME_LENGTH)))
#define WINDOW_MOVE_NAME_X (WINDOW_COL_1 + (GetFontAttribute(sDexNavSearchDataPtr->windowId, FONTATTR_MAX_LETTER_WIDTH) * 6))
#define SEARCH_ARROW_X (WINDOW_MOVE_NAME_X + 90)
#define SEARCH_ARROW_Y 0
static void AddSearchWindowText(u16 species, u8 proximity, u8 searchLevel, bool8 hidden)
{
u8 windowId = sDexNavSearchDataPtr->windowId;
//species name - always present
if (hidden)
{
StringCopy(gStringVar4, sText_ThreeQmarks);
AddTextPrinterParameterized3(sDexNavSearchDataPtr->windowId, 0, WINDOW_COL_0, 0, sSearchFontColor, TEXT_SKIP_DRAW, gStringVar4);
return;
}
else
{
StringCopy(gStringVar1, GetSpeciesName(species));
AddTextPrinterParameterized3(sDexNavSearchDataPtr->windowId, 0, WINDOW_COL_0, 0, sSearchFontColor, TEXT_SKIP_DRAW, gStringVar1);
}
//level - always present
ConvertIntToDecimalStringN(gStringVar1, sDexNavSearchDataPtr->monLevel, STR_CONV_MODE_LEFT_ALIGN, 3);
StringExpandPlaceholders(gStringVar4, sText_MonLevel);
AddTextPrinterParameterized3(sDexNavSearchDataPtr->windowId, 0, WINDOW_COL_1, 0, sSearchFontColor, TEXT_SKIP_DRAW, gStringVar4);
if (proximity <= SNEAKING_PROXIMITY)
{
PlaySE(SE_POKENAV_ON);
// move
if (searchLevel > 1 && sDexNavSearchDataPtr->moves[0])
{
StringCopy(gStringVar1, GetMoveName(sDexNavSearchDataPtr->moves[0]));
StringExpandPlaceholders(gStringVar4, sText_EggMove);
AddTextPrinterParameterized3(windowId, 0, WINDOW_MOVE_NAME_X, 0, sSearchFontColor, TEXT_SKIP_DRAW, gStringVar4);
}
if (searchLevel > 2)
{
// ability name
StringCopy(gStringVar1, gAbilitiesInfo[GetAbilityBySpecies(species, sDexNavSearchDataPtr->abilityNum)].name);
AddTextPrinterParameterized3(windowId, 0, WINDOW_COL_1 + 16, 12, sSearchFontColor, TEXT_SKIP_DRAW, gStringVar1);
// item name
if (sDexNavSearchDataPtr->heldItem)
{
CopyItemName(sDexNavSearchDataPtr->heldItem, gStringVar1);
StringExpandPlaceholders(gStringVar4, sText_HeldItem);
AddTextPrinterParameterized3(windowId, 0, WINDOW_COL_0, 12, sSearchFontColor, TEXT_SKIP_DRAW, gStringVar4);
}
}
}
//chain level - always present
ConvertIntToDecimalStringN(gStringVar1, gSaveBlock3Ptr->dexNavChain, STR_CONV_MODE_LEFT_ALIGN, 3);
if (gSaveBlock3Ptr->dexNavChain > 99)
StringExpandPlaceholders(gStringVar4, sText_DexNavChainLong);
else
StringExpandPlaceholders(gStringVar4, sText_DexNavChain);
AddTextPrinterParameterized3(windowId, 0, SEARCH_ARROW_X - 16, 12, sSearchFontColor, TEXT_SKIP_DRAW, gStringVar4);
CopyWindowToVram(sDexNavSearchDataPtr->windowId, 2);
}
#define SEARCH_WINDOW_WIDTH 28
static void DrawSearchWindow(u16 species, u8 potential, bool8 hidden)
{
u8 searchLevel = sDexNavSearchDataPtr->searchLevel;
AddSearchWindow(SEARCH_WINDOW_WIDTH);
AddSearchWindowText(species, sDexNavSearchDataPtr->proximity, searchLevel, hidden);
}
#undef SEARCH_WINDOW_WIDTH
static void RemoveDexNavWindowAndGfx(void)
{
u32 i;
// try remove sprites
if (sDexNavSearchDataPtr->iconSpriteId != MAX_SPRITES)
DestroySprite(&gSprites[sDexNavSearchDataPtr->iconSpriteId]);
if (sDexNavSearchDataPtr->itemSpriteId != MAX_SPRITES)
DestroySprite(&gSprites[sDexNavSearchDataPtr->itemSpriteId]);
if (sDexNavSearchDataPtr->eyeSpriteId != MAX_SPRITES)
DestroySprite(&gSprites[sDexNavSearchDataPtr->eyeSpriteId]);
if (sDexNavSearchDataPtr->ownedIconSpriteId != MAX_SPRITES)
DestroySprite(&gSprites[sDexNavSearchDataPtr->ownedIconSpriteId]);
if (sDexNavSearchDataPtr->exclamationSpriteId != MAX_SPRITES)
DestroySprite(&gSprites[sDexNavSearchDataPtr->exclamationSpriteId]);
for (i = 0; i < NELEMS(sDexNavSearchDataPtr->starSpriteIds); i++)
{
if (sDexNavSearchDataPtr->starSpriteIds[i] != MAX_SPRITES)
DestroySprite(&gSprites[sDexNavSearchDataPtr->starSpriteIds[i]]);
}
FreeSpriteTilesByTag(HELD_ITEM_TAG);
FreeSpriteTilesByTag(OWNED_ICON_TAG);
FreeSpriteTilesByTag(HIDDEN_SEARCH_TAG);
FreeSpriteTilesByTag(HIDDEN_MON_ICON_TAG);
FreeSpriteTilesByTag(LIT_STAR_TILE_TAG);
FreeSpritePaletteByTag(HELD_ITEM_TAG);
SafeFreeMonIconPalette(sDexNavSearchDataPtr->species);
// remove window
ClearStdWindowAndFrameToTransparent(sDexNavSearchDataPtr->windowId, FALSE);
CopyWindowToVram(sDexNavSearchDataPtr->windowId, 3);
RemoveWindow(sDexNavSearchDataPtr->windowId);
}
//////////////////////
////DEXNAV SEARCH/////
//////////////////////
static u8 GetPlayerDistance(s16 x, s16 y)
{
u16 deltaX = abs(x - (gSaveBlock1Ptr->pos.x + 7));
u16 deltaY = abs(y - (gSaveBlock1Ptr->pos.y + 7));
return deltaX + deltaY;
}
static void DexNavProximityUpdate(void)
{
sDexNavSearchDataPtr->proximity = GetPlayerDistance(sDexNavSearchDataPtr->tileX, sDexNavSearchDataPtr->tileY);
}
//Pick a specific tile based on environment
static bool8 DexNavPickTile(u8 environment, u8 areaX, u8 areaY, bool8 smallScan)
{
// area of map to cover starting from camera position {-7, -7}
s16 topX = gSaveBlock1Ptr->pos.x - SCANSTART_X + (smallScan * 5);
s16 topY = gSaveBlock1Ptr->pos.y - SCANSTART_Y + (smallScan * 5);
s16 botX = topX + areaX;
s16 botY = topY + areaY;
u8 i;
bool8 nextIter;
u8 scale = 0;
u8 weight = 0;
u8 currMapType = GetCurrentMapType();
u8 tileBehaviour;
u8 tileBuffer = 2;
u8 *xPos = AllocZeroed((botX - topX) * (botY - topY) * sizeof(u8));
u8 *yPos = AllocZeroed((botX - topX) * (botY - topY) * sizeof(u8));
u32 iter = 0;
bool32 ret = FALSE;
// loop through every tile in area and evaluate
while (topY < botY)
{
while (topX < botX)
{
tileBehaviour = MapGridGetMetatileBehaviorAt(topX, topY);
//Check for objects
nextIter = FALSE;
if (TestPlayerAvatarFlags(PLAYER_AVATAR_FLAG_BIKE))
tileBuffer = SNEAKING_PROXIMITY + 3;
else if (TestPlayerAvatarFlags(PLAYER_AVATAR_FLAG_DASH))
tileBuffer = SNEAKING_PROXIMITY + 1;
if (GetPlayerDistance(topX, topY) <= tileBuffer)
{
// tile too close to player
topX++;
continue;
}
for (i = 0; i < OBJECT_EVENTS_COUNT; i++)
{
if (gObjectEvents[i].currentCoords.x == topX && gObjectEvents[i].currentCoords.y == topY)
{
// cannot be on a tile where an object exists
nextIter = TRUE;
break;
}
}
if (nextIter)
{
topX++;
continue;
}
weight = 0; // initiliaze weight
switch (environment)
{
case ENCOUNTER_TYPE_LAND:
if (MetatileBehavior_IsLandWildEncounter(tileBehaviour))
{
if (currMapType == MAP_TYPE_UNDERGROUND)
{
// inside (cave)
if (IsElevationMismatchAt(gObjectEvents[gPlayerAvatar.spriteId].currentElevation, topX, topY))
break; //occurs at same z coord
scale = 440 - (smallScan * 200) - (GetPlayerDistance(topX, topY) / 2) - (2 * (topX + topY));
weight = ((Random() % scale) < 1) && !MapGridGetCollisionAt(topX, topY);
}
else
{
// outdoors: grass
scale = 100 - (GetPlayerDistance(topX, topY) * 2);
weight = (Random() % scale <= 5) && !MapGridGetCollisionAt(topX, topY);
}
}
break;
case ENCOUNTER_TYPE_WATER:
if (MetatileBehavior_IsSurfableWaterOrUnderwater(tileBehaviour))
{
u8 scale = 320 - (smallScan * 200) - (GetPlayerDistance(topX, topY) / 2);
if (IsElevationMismatchAt(gObjectEvents[gPlayerAvatar.spriteId].currentElevation, topX, topY))
break;
weight = (Random() % scale <= 1) && !MapGridGetCollisionAt(topX, topY);
}
break;
default:
break;
}
if (weight > 0)
{
xPos[iter] = topX;
yPos[iter] = topY;
iter++;
}
topX++;
}
topY++;
topX = gSaveBlock1Ptr->pos.x - SCANSTART_X + (smallScan * 5);
}
if (iter > 0)
{
i = Random() % iter;
sDexNavSearchDataPtr->tileX = xPos[i];
sDexNavSearchDataPtr->tileY = yPos[i];
ret = TRUE;
}
Free(xPos);
Free(yPos);
return ret;
}
static bool8 TryStartHiddenMonFieldEffect(u8 environment, u8 xSize, u8 ySize, bool8 smallScan)
{
u8 currMapType = GetCurrentMapType();
u8 fldEffId = 0;
if (DexNavPickTile(environment, xSize, ySize, smallScan))
{
u8 metatileBehaviour = MapGridGetMetatileBehaviorAt(sDexNavSearchDataPtr->tileX, sDexNavSearchDataPtr->tileY);
switch (environment)
{
case ENCOUNTER_TYPE_LAND:
if (currMapType == MAP_TYPE_UNDERGROUND)
{
fldEffId = FLDEFF_CAVE_DUST;
}
else if (IsMapTypeIndoors(currMapType))
{
if (MetatileBehavior_IsTallGrass(metatileBehaviour)) //Grass in cave
fldEffId = FLDEFF_SHAKING_GRASS;
else if (MetatileBehavior_IsLongGrass(metatileBehaviour)) //Really tall grass
fldEffId = FLDEFF_SHAKING_LONG_GRASS;
else if (MetatileBehavior_IsSandOrDeepSand(metatileBehaviour))
fldEffId = FLDEFF_SAND_HOLE;
else
fldEffId = FLDEFF_CAVE_DUST;
}
else //outdoor, underwater
{
if (MetatileBehavior_IsTallGrass(metatileBehaviour)) //Regular grass
fldEffId = FLDEFF_SHAKING_GRASS;
else if (MetatileBehavior_IsLongGrass(metatileBehaviour)) //Really tall grass
fldEffId = FLDEFF_SHAKING_LONG_GRASS;
else if (MetatileBehavior_IsSandOrDeepSand(metatileBehaviour)) //Desert Sand
fldEffId = FLDEFF_SAND_HOLE;
else if (MetatileBehavior_IsMountain(metatileBehaviour)) //Rough Terrain
fldEffId = FLDEFF_CAVE_DUST;
else
fldEffId = FLDEFF_BERRY_TREE_GROWTH_SPARKLE; //default
}
break;
case ENCOUNTER_TYPE_WATER:
fldEffId = FLDEFF_WATER_SURFACING;
break;
default:
return FALSE;
}
if (fldEffId != 0)
{
gFieldEffectArguments[0] = sDexNavSearchDataPtr->tileX;
gFieldEffectArguments[1] = sDexNavSearchDataPtr->tileY;
gFieldEffectArguments[2] = 0xFF; // subpriority
gFieldEffectArguments[3] = 2; //priority
sDexNavSearchDataPtr->fldEffSpriteId = FieldEffectStart(fldEffId);
if (sDexNavSearchDataPtr->fldEffSpriteId == MAX_SPRITES)
return FALSE;
sDexNavSearchDataPtr->fldEffId = fldEffId;
return TRUE;
}
}
return FALSE;
}
static void DrawDexNavSearchHeldItem(u8* dst)
{
*dst = CreateSprite(&sHeldItemTemplate, SPECIES_ICON_X + 6, GetSearchWindowY() + 18, 0);
if (*dst != MAX_SPRITES)
gSprites[*dst].invisible = TRUE;
}
static void LoadSearchIconData(void)
{
// palettes clash with mon icon, so must load manually
LoadSpriteSheet(&gSpriteSheet_HeldItem);
LoadPalette(gHeldItemPalette, 0x100 + (16 * sHeldItemOam.paletteNum), 32);
LoadCompressedSpriteSheetUsingHeap(&sPotentialStarSpriteSheet);
//LoadCompressedSpriteSheetUsingHeap(&sSightSpriteSheet); //eye replaced with arrow
LoadCompressedSpriteSheetUsingHeap(&sOwnedIconSpriteSheet);
LoadCompressedSpriteSheetUsingHeap(&sHiddenMonIconSpriteSheet);
}
static u8 GetSearchLevel(u16 dexNum)
{
u8 searchLevel;
#if USE_DEXNAV_SEARCH_LEVELS == TRUE
searchLevel = gSaveBlock3Ptr->dexNavSearchLevels[dexNum];
#else
searchLevel = 0;
#endif
return searchLevel;
}
#define tProximity data[0]
#define tFrameCount data[1]
#define tSpecies data[2]
#define tEnvironment data[3]
#define tRevealed data[4]
static void Task_SetUpDexNavSearch(u8 taskId)
{
struct Task *task = &gTasks[taskId];
u16 species = sDexNavSearchDataPtr->species;
u8 searchLevel = GetSearchLevel(SpeciesToNationalPokedexNum(species));
// init sprites
sDexNavSearchDataPtr->iconSpriteId = MAX_SPRITES;
sDexNavSearchDataPtr->itemSpriteId = MAX_SPRITES;
sDexNavSearchDataPtr->eyeSpriteId = MAX_SPRITES;
sDexNavSearchDataPtr->starSpriteIds[0] = MAX_SPRITES;
sDexNavSearchDataPtr->starSpriteIds[1] = MAX_SPRITES;
sDexNavSearchDataPtr->starSpriteIds[2] = MAX_SPRITES;
sDexNavSearchDataPtr->ownedIconSpriteId = MAX_SPRITES;
sDexNavSearchDataPtr->exclamationSpriteId = MAX_SPRITES;
sDexNavSearchDataPtr->searchLevel = searchLevel;
DexNavGenerateMoveset(species, searchLevel, sDexNavSearchDataPtr->monLevel, &sDexNavSearchDataPtr->moves[0]);
sDexNavSearchDataPtr->heldItem = DexNavGenerateHeldItem(species, searchLevel);
sDexNavSearchDataPtr->abilityNum = DexNavGetAbilityNum(species, searchLevel);
sDexNavSearchDataPtr->potential = DexNavGeneratePotential(searchLevel);
DexNavProximityUpdate();
LoadSearchIconData();
if (sDexNavSearchDataPtr->hiddenSearch)
{
DexNavDrawHiddenIcons();
}
else
{
DexNavDrawIcons();
DexNavUpdateSearchWindow(sDexNavSearchDataPtr->proximity, searchLevel);
}
FlagSet(DN_FLAG_SEARCHING);
gPlayerAvatar.creeping = TRUE; //initialize as true in case mon appears beside you
task->tProximity = gSprites[gPlayerAvatar.spriteId].x;
task->tFrameCount = 0;
task->func = Task_DexNavSearch;
IncrementGameStat(GAME_STAT_DEXNAV_SCANNED);
}
static void DexNavSearchBail(u8 taskId, const u8 *script)
{
TRY_FREE_AND_SET_NULL(sDexNavSearchDataPtr);
FreeMonIconPalettes();
ScriptContext_SetupScript(script);
DestroyTask(taskId);
}
static void Task_InitDexNavSearch(u8 taskId)
{
struct Task *task = &gTasks[taskId];
u16 species = task->tSpecies;
u8 environment = task->tEnvironment;
sDexNavSearchDataPtr = AllocZeroed(sizeof(struct DexNavSearch));
if (sDexNavSearchDataPtr == NULL)
{
DexNavSearchBail(taskId, EventScript_NotFoundNearby);
return;
}
// assign non-objects to struct
sDexNavSearchDataPtr->species = species;
sDexNavSearchDataPtr->environment = environment; //updated in DexNavTryGenerateMonLevel if hidden mon
sDexNavSearchDataPtr->isHiddenMon = (environment == ENCOUNTER_TYPE_HIDDEN) ? TRUE : FALSE;
sDexNavSearchDataPtr->monLevel = DexNavTryGenerateMonLevel(species, environment);
if (GetFlashLevel() > 0)
{
DexNavSearchBail(taskId, EventScript_TooDark);
return;
}
if (sDexNavSearchDataPtr->monLevel == MON_LEVEL_NONEXISTENT || !TryStartHiddenMonFieldEffect(sDexNavSearchDataPtr->environment, 12, 12, FALSE))
{
DexNavSearchBail(taskId, EventScript_NotFoundNearby);
return;
}
sDexNavSearchDataPtr->hiddenSearch = FALSE;
task->tRevealed = TRUE; //search window revealed
task->func = Task_SetUpDexNavSearch;
}
static void DexNavDrawPotentialStars(u8 potential, u8* dst)
{
u8 spriteId;
u32 i;
for (i = 0; i < NELEMS(sDexNavSearchDataPtr->starSpriteIds); i++)
{
spriteId = MAX_SPRITES;
if (potential > i)
spriteId = CreateSprite(&sPotentialStarTemplate, SPECIES_ICON_X - 20, GetSearchWindowY() + 4 + (i * 8), 0);
dst[i] = spriteId;
if (spriteId != MAX_SPRITES)
gSprites[spriteId].invisible = TRUE;
}
}
static void DexNavUpdateDirectionArrow(void)
{
u16 tileX = sDexNavSearchDataPtr->tileX;
u16 tileY = sDexNavSearchDataPtr->tileY;
u16 playerX = gSaveBlock1Ptr->pos.x + 7;
u16 playerY = gSaveBlock1Ptr->pos.y + 7;
u16 deltaX = abs(tileX - playerX);
u16 deltaY = abs(tileY - playerY);
const u8 *str;
u8 windowId = sDexNavSearchDataPtr->windowId;
FillWindowPixelRect(windowId, PIXEL_FILL(1), SEARCH_ARROW_X, SEARCH_ARROW_Y, 12, 12);
if (deltaX <= 1 && deltaY <= 1)
{
str = gText_EmptyString2;
}
else if (deltaX > deltaY)
{
if (playerX > tileX)
str = sText_ArrowLeft; //player to right
else
str = sText_ArrowRight; //player to left
}
else //greater Y diff
{
if (playerY > tileY)
str = sText_ArrowUp; //player below
else
str = sText_ArrowDown; //player above
}
AddTextPrinterParameterized3(windowId, 1, SEARCH_ARROW_X, SEARCH_ARROW_Y, sSearchFontColor, TEXT_SKIP_DRAW, str);
CopyWindowToVram(windowId, 2);
}
static void DexNavDrawIcons(void)
{
u16 species = sDexNavSearchDataPtr->species;
DrawSearchWindow(species, sDexNavSearchDataPtr->potential, FALSE);
DrawDexNavSearchMonIcon(species, &sDexNavSearchDataPtr->iconSpriteId, GetSetPokedexFlag(SpeciesToNationalPokedexNum(species), FLAG_GET_CAUGHT));
DrawDexNavSearchHeldItem(&sDexNavSearchDataPtr->itemSpriteId);
DexNavDrawPotentialStars(sDexNavSearchDataPtr->potential, &sDexNavSearchDataPtr->starSpriteIds[0]);
DexNavUpdateDirectionArrow();
}
/////////////////////
//// SEARCH TASK ////
/////////////////////
bool8 TryStartDexNavSearch(void)
{
u8 taskId;
u16 val = VarGet(DN_VAR_SPECIES);
if (FlagGet(DN_FLAG_SEARCHING) || (val & DEXNAV_MASK_SPECIES) == SPECIES_NONE)
return FALSE;
HideMapNamePopUpWindow();
ChangeBgY_ScreenOff(0, 0, 0);
taskId = CreateTask(Task_InitDexNavSearch, 0);
gTasks[taskId].tSpecies = val & DEXNAV_MASK_SPECIES;
gTasks[taskId].tEnvironment = val >> 14;
PlaySE(SE_DEX_SEARCH);
return FALSE; //we dont actually want to enable the script context
}
void EndDexNavSearch(u8 taskId)
{
FlagClear(DN_FLAG_SEARCHING);
DestroyTask(taskId);