-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
item_use.c
executable file
·1127 lines (1012 loc) · 32.7 KB
/
item_use.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 "item_use.h"
#include "battle.h"
#include "battle_pyramid.h"
#include "battle_pyramid_bag.h"
#include "berry.h"
#include "berry_powder.h"
#include "bike.h"
#include "coins.h"
#include "data.h"
#include "event_data.h"
#include "event_object_lock.h"
#include "event_object_movement.h"
#include "event_scripts.h"
#include "fieldmap.h"
#include "field_effect.h"
#include "field_player_avatar.h"
#include "field_screen_effect.h"
#include "field_weather.h"
#include "item.h"
#include "item_menu.h"
#include "item_use.h"
#include "mail.h"
#include "main.h"
#include "menu.h"
#include "menu_helpers.h"
#include "metatile_behavior.h"
#include "overworld.h"
#include "palette.h"
#include "party_menu.h"
#include "pokeblock.h"
#include "pokemon.h"
#include "script.h"
#include "sound.h"
#include "strings.h"
#include "string_util.h"
#include "task.h"
#include "text.h"
#include "constants/event_bg.h"
#include "constants/event_objects.h"
#include "constants/item_effects.h"
#include "constants/items.h"
#include "constants/songs.h"
static void SetUpItemUseCallback(u8);
static void FieldCB_UseItemOnField(void);
static void Task_CallItemUseOnFieldCallback(u8);
static void Task_UseItemfinder(u8);
static void Task_CloseItemfinderMessage(u8);
static void Task_HiddenItemNearby(u8);
static void Task_StandingOnHiddenItem(u8);
static bool8 ItemfinderCheckForHiddenItems(const struct MapEvents *, u8);
static u8 GetDirectionToHiddenItem(s16, s16);
static void PlayerFaceHiddenItem(u8);
static void CheckForHiddenItemsInMapConnection(u8);
static void Task_OpenRegisteredPokeblockCase(u8);
static void ItemUseOnFieldCB_Bike(u8);
static void ItemUseOnFieldCB_Rod(u8);
static void ItemUseOnFieldCB_Itemfinder(u8);
static void ItemUseOnFieldCB_Berry(u8);
static void ItemUseOnFieldCB_WailmerPailBerry(u8);
static void ItemUseOnFieldCB_WailmerPailSudowoodo(u8);
static bool8 TryToWaterSudowoodo(void);
static void BootUpSoundTMHM(u8);
static void Task_ShowTMHMContainedMessage(u8);
static void UseTMHMYesNo(u8);
static void UseTMHM(u8);
static void Task_StartUseRepel(u8);
static void Task_UseRepel(u8);
static void Task_CloseCantUseKeyItemMessage(u8);
static void SetDistanceOfClosestHiddenItem(u8, s16, s16);
static void CB2_OpenPokeblockFromBag(void);
// EWRAM variables
EWRAM_DATA static void(*sItemUseOnFieldCB)(u8 taskId) = NULL;
// Below is set TRUE by UseRegisteredKeyItemOnField
#define tUsingRegisteredKeyItem data[3]
// UB here if an item with type ITEM_USE_MAIL or ITEM_USE_BAG_MENU uses SetUpItemUseCallback
// Never occurs in vanilla, but can occur with improperly created items
static const MainCallback sItemUseCallbacks[] =
{
[ITEM_USE_PARTY_MENU - 1] = CB2_ShowPartyMenuForItemUse,
[ITEM_USE_FIELD - 1] = CB2_ReturnToField,
[ITEM_USE_PBLOCK_CASE - 1] = NULL,
};
static const u8 sClockwiseDirections[] = {DIR_NORTH, DIR_EAST, DIR_SOUTH, DIR_WEST};
static const struct YesNoFuncTable sUseTMHMYesNoFuncTable =
{
.yesFunc = UseTMHM,
.noFunc = CloseItemMessage,
};
#define tEnigmaBerryType data[4]
static void SetUpItemUseCallback(u8 taskId)
{
u8 type;
if (gSpecialVar_ItemId == ITEM_ENIGMA_BERRY)
type = gTasks[taskId].tEnigmaBerryType - 1;
else
type = ItemId_GetType(gSpecialVar_ItemId) - 1;
if (!InBattlePyramid())
{
gBagMenu->newScreenCallback = sItemUseCallbacks[type];
Task_FadeAndCloseBagMenu(taskId);
}
else
{
gPyramidBagMenu->newScreenCallback = sItemUseCallbacks[type];
CloseBattlePyramidBag(taskId);
}
}
static void SetUpItemUseOnFieldCallback(u8 taskId)
{
if (gTasks[taskId].tUsingRegisteredKeyItem != TRUE)
{
gFieldCallback = FieldCB_UseItemOnField;
SetUpItemUseCallback(taskId);
}
else
sItemUseOnFieldCB(taskId);
}
static void FieldCB_UseItemOnField(void)
{
FadeInFromBlack();
CreateTask(Task_CallItemUseOnFieldCallback, 8);
}
static void Task_CallItemUseOnFieldCallback(u8 taskId)
{
if (IsWeatherNotFadingIn() == 1)
sItemUseOnFieldCB(taskId);
}
static void DisplayCannotUseItemMessage(u8 taskId, bool8 isUsingRegisteredKeyItemOnField, const u8 *str)
{
StringExpandPlaceholders(gStringVar4, str);
if (!isUsingRegisteredKeyItemOnField)
{
if (!InBattlePyramid())
DisplayItemMessage(taskId, FONT_NORMAL, gStringVar4, CloseItemMessage);
else
DisplayItemMessageInBattlePyramid(taskId, gText_DadsAdvice, Task_CloseBattlePyramidBagMessage);
}
else
DisplayItemMessageOnField(taskId, gStringVar4, Task_CloseCantUseKeyItemMessage);
}
static void DisplayDadsAdviceCannotUseItemMessage(u8 taskId, bool8 isUsingRegisteredKeyItemOnField)
{
DisplayCannotUseItemMessage(taskId, isUsingRegisteredKeyItemOnField, gText_DadsAdvice);
}
static void DisplayCannotDismountBikeMessage(u8 taskId, bool8 isUsingRegisteredKeyItemOnField)
{
DisplayCannotUseItemMessage(taskId, isUsingRegisteredKeyItemOnField, gText_CantDismountBike);
}
static void Task_CloseCantUseKeyItemMessage(u8 taskId)
{
ClearDialogWindowAndFrame(0, TRUE);
DestroyTask(taskId);
ScriptUnfreezeObjectEvents();
UnlockPlayerFieldControls();
}
u8 CheckIfItemIsTMHMOrEvolutionStone(u16 itemId)
{
if (ItemId_GetFieldFunc(itemId) == ItemUseOutOfBattle_TMHM)
return 1;
else if (ItemId_GetFieldFunc(itemId) == ItemUseOutOfBattle_EvolutionStone)
return 2;
else
return 0;
}
// Mail in the bag menu can't have a message but it can be checked (view the mail background, no message)
static void CB2_CheckMail(void)
{
struct Mail mail;
mail.itemId = gSpecialVar_ItemId;
ReadMail(&mail, CB2_ReturnToBagMenuPocket, FALSE);
}
void ItemUseOutOfBattle_Mail(u8 taskId)
{
gBagMenu->newScreenCallback = CB2_CheckMail;
Task_FadeAndCloseBagMenu(taskId);
}
void ItemUseOutOfBattle_Bike(u8 taskId)
{
s16 *data = gTasks[taskId].data;
s16 coordsY;
s16 coordsX;
u8 behavior;
PlayerGetDestCoords(&coordsX, &coordsY);
behavior = MapGridGetMetatileBehaviorAt(coordsX, coordsY);
if (FlagGet(FLAG_SYS_CYCLING_ROAD) == TRUE || MetatileBehavior_IsVerticalRail(behavior) == TRUE || MetatileBehavior_IsHorizontalRail(behavior) == TRUE || MetatileBehavior_IsIsolatedVerticalRail(behavior) == TRUE || MetatileBehavior_IsIsolatedHorizontalRail(behavior) == TRUE)
DisplayCannotDismountBikeMessage(taskId, tUsingRegisteredKeyItem);
else
{
if (Overworld_IsBikingAllowed() == TRUE && IsBikingDisallowedByPlayer() == 0)
{
sItemUseOnFieldCB = ItemUseOnFieldCB_Bike;
SetUpItemUseOnFieldCallback(taskId);
}
else
DisplayDadsAdviceCannotUseItemMessage(taskId, tUsingRegisteredKeyItem);
}
}
static void ItemUseOnFieldCB_Bike(u8 taskId)
{
if (ItemId_GetSecondaryId(gSpecialVar_ItemId) == MACH_BIKE)
GetOnOffBike(PLAYER_AVATAR_FLAG_MACH_BIKE);
else // ACRO_BIKE
GetOnOffBike(PLAYER_AVATAR_FLAG_ACRO_BIKE);
ScriptUnfreezeObjectEvents();
UnlockPlayerFieldControls();
DestroyTask(taskId);
}
static bool32 CanFish(void)
{
s16 x, y;
u16 tileBehavior;
GetXYCoordsOneStepInFrontOfPlayer(&x, &y);
tileBehavior = MapGridGetMetatileBehaviorAt(x, y);
if (MetatileBehavior_IsWaterfall(tileBehavior))
return FALSE;
if (TestPlayerAvatarFlags(PLAYER_AVATAR_FLAG_UNDERWATER))
return FALSE;
if (!TestPlayerAvatarFlags(PLAYER_AVATAR_FLAG_SURFING))
{
if (IsPlayerFacingSurfableFishableWater())
return TRUE;
}
else
{
if (MetatileBehavior_IsSurfableWaterOrUnderwater(tileBehavior) && MapGridGetCollisionAt(x, y) == 0)
return TRUE;
if (MetatileBehavior_IsBridgeOverWaterNoEdge(tileBehavior) == TRUE)
return TRUE;
}
return FALSE;
}
void ItemUseOutOfBattle_Rod(u8 taskId)
{
if (CanFish() == TRUE)
{
sItemUseOnFieldCB = ItemUseOnFieldCB_Rod;
SetUpItemUseOnFieldCallback(taskId);
}
else
DisplayDadsAdviceCannotUseItemMessage(taskId, gTasks[taskId].tUsingRegisteredKeyItem);
}
static void ItemUseOnFieldCB_Rod(u8 taskId)
{
StartFishing(ItemId_GetSecondaryId(gSpecialVar_ItemId));
DestroyTask(taskId);
}
void ItemUseOutOfBattle_Itemfinder(u8 var)
{
IncrementGameStat(GAME_STAT_USED_ITEMFINDER);
sItemUseOnFieldCB = ItemUseOnFieldCB_Itemfinder;
SetUpItemUseOnFieldCallback(var);
}
static void ItemUseOnFieldCB_Itemfinder(u8 taskId)
{
if (ItemfinderCheckForHiddenItems(gMapHeader.events, taskId) == TRUE)
gTasks[taskId].func = Task_UseItemfinder;
else
DisplayItemMessageOnField(taskId, gText_ItemFinderNothing, Task_CloseItemfinderMessage);
}
// Define itemfinder task data
#define tItemDistanceX data[0]
#define tItemDistanceY data[1]
#define tItemFound data[2]
#define tCounter data[3] // Used to count delay between beeps and rotations during player spin
#define tItemfinderBeeps data[4]
#define tFacingDir data[5]
static void Task_UseItemfinder(u8 taskId)
{
u8 playerDir;
u8 playerDirToItem;
u8 i;
s16 *data = gTasks[taskId].data;
if (tCounter == 0)
{
if (tItemfinderBeeps == 4)
{
playerDirToItem = GetDirectionToHiddenItem(tItemDistanceX, tItemDistanceY);
if (playerDirToItem != DIR_NONE)
{
PlayerFaceHiddenItem(sClockwiseDirections[playerDirToItem - 1]);
gTasks[taskId].func = Task_HiddenItemNearby;
}
else
{
// Player is standing on hidden item
playerDir = GetPlayerFacingDirection();
for (i = 0; i < ARRAY_COUNT(sClockwiseDirections); i++)
{
if (playerDir == sClockwiseDirections[i])
tFacingDir = (i + 1) & 3;
}
gTasks[taskId].func = Task_StandingOnHiddenItem;
tCounter = 0;
tItemFound = 0;
}
return;
}
PlaySE(SE_ITEMFINDER);
tItemfinderBeeps++;
}
tCounter = (tCounter + 1) & 0x1F;
}
static void Task_CloseItemfinderMessage(u8 taskId)
{
ClearDialogWindowAndFrame(0, TRUE);
ScriptUnfreezeObjectEvents();
UnlockPlayerFieldControls();
DestroyTask(taskId);
}
static bool8 ItemfinderCheckForHiddenItems(const struct MapEvents *events, u8 taskId)
{
int itemX, itemY;
s16 playerX, playerY, i, distanceX, distanceY;
PlayerGetDestCoords(&playerX, &playerY);
gTasks[taskId].tItemFound = FALSE;
for (i = 0; i < events->bgEventCount; i++)
{
// Check if there are any hidden items on the current map that haven't been picked up
if (events->bgEvents[i].kind == BG_EVENT_HIDDEN_ITEM && !FlagGet(events->bgEvents[i].bgUnion.hiddenItem.hiddenItemId + FLAG_HIDDEN_ITEMS_START))
{
itemX = (u16)events->bgEvents[i].x + MAP_OFFSET;
distanceX = itemX - playerX;
itemY = (u16)events->bgEvents[i].y + MAP_OFFSET;
distanceY = itemY - playerY;
// Player can see 7 metatiles on either side horizontally
// and 5 metatiles on either side vertically
if (distanceX >= -7 && distanceX <= 7 && distanceY >= -5 && distanceY <= 5)
SetDistanceOfClosestHiddenItem(taskId, distanceX, distanceY);
}
}
CheckForHiddenItemsInMapConnection(taskId);
if (gTasks[taskId].tItemFound == TRUE)
return TRUE;
else
return FALSE;
}
static bool8 IsHiddenItemPresentAtCoords(const struct MapEvents *events, s16 x, s16 y)
{
u8 bgEventCount = events->bgEventCount;
const struct BgEvent *bgEvent = events->bgEvents;
int i;
for (i = 0; i < bgEventCount; i++)
{
if (bgEvent[i].kind == BG_EVENT_HIDDEN_ITEM && x == (u16)bgEvent[i].x && y == (u16)bgEvent[i].y) // hidden item and coordinates matches x and y passed?
{
if (!FlagGet(bgEvent[i].bgUnion.hiddenItem.hiddenItemId + FLAG_HIDDEN_ITEMS_START))
return TRUE;
else
return FALSE;
}
}
return FALSE;
}
static bool8 IsHiddenItemPresentInConnection(const struct MapConnection *connection, int x, int y)
{
s16 connectionX, connectionY;
struct MapHeader const *const connectionHeader = GetMapHeaderFromConnection(connection);
// To convert our x/y into coordinates that are relative to the connected map, we must:
// - Subtract the virtual offset used for the border buffer (MAP_OFFSET).
// - Subtract the horizontal offset between North/South connections, or the vertical offset for East/West
// - Account for map size. (0,0) is in the NW corner of our map, so when looking North/West we have to add the height/width of the connected map,
// and when looking South/East we have to subtract the height/width of our current map.
#define localX (x - MAP_OFFSET)
#define localY (y - MAP_OFFSET)
switch (connection->direction)
{
case CONNECTION_NORTH:
connectionX = localX - connection->offset;
connectionY = connectionHeader->mapLayout->height + localY;
break;
case CONNECTION_SOUTH:
connectionX = localX - connection->offset;
connectionY = localY - gMapHeader.mapLayout->height;
break;
case CONNECTION_WEST:
connectionX = connectionHeader->mapLayout->width + localX;
connectionY = localY - connection->offset;
break;
case CONNECTION_EAST:
connectionX = localX - gMapHeader.mapLayout->width;
connectionY = localY - connection->offset;
break;
default:
return FALSE;
}
return IsHiddenItemPresentAtCoords(connectionHeader->events, connectionX, connectionY);
}
#undef localX
#undef localY
static void CheckForHiddenItemsInMapConnection(u8 taskId)
{
s16 playerX, playerY;
s16 x, y;
s16 width = gMapHeader.mapLayout->width + MAP_OFFSET;
s16 height = gMapHeader.mapLayout->height + MAP_OFFSET;
s16 var1 = MAP_OFFSET;
s16 var2 = MAP_OFFSET;
PlayerGetDestCoords(&playerX, &playerY);
// Player can see 7 metatiles on either side horizontally
// and 5 metatiles on either side vertically
for (x = playerX - 7; x <= playerX + 7; x++)
{
for (y = playerY - 5; y <= playerY + 5; y++)
{
if (var1 > x
|| x >= width
|| var2 > y
|| y >= height)
{
const struct MapConnection *conn = GetMapConnectionAtPos(x, y);
if (conn && IsHiddenItemPresentInConnection(conn, x, y) == TRUE)
SetDistanceOfClosestHiddenItem(taskId, x - playerX, y - playerY);
}
}
}
}
static void SetDistanceOfClosestHiddenItem(u8 taskId, s16 itemDistanceX, s16 itemDistanceY)
{
s16 *data = gTasks[taskId].data;
s16 oldItemAbsX, oldItemAbsY, newItemAbsX, newItemAbsY;
if (tItemFound == FALSE)
{
// No other items found yet, set this one
tItemDistanceX = itemDistanceX;
tItemDistanceY = itemDistanceY;
tItemFound = TRUE;
}
else
{
// Other items have been found, check if this one is closer
// Get absolute x distance of the already-found item
if (tItemDistanceX < 0)
oldItemAbsX = tItemDistanceX * -1; // WEST
else
oldItemAbsX = tItemDistanceX; // EAST
// Get absolute y distance of the already-found item
if (tItemDistanceY < 0)
oldItemAbsY = tItemDistanceY * -1; // NORTH
else
oldItemAbsY = tItemDistanceY; // SOUTH
// Get absolute x distance of the newly-found item
if (itemDistanceX < 0)
newItemAbsX = itemDistanceX * -1;
else
newItemAbsX = itemDistanceX;
// Get absolute y distance of the newly-found item
if (itemDistanceY < 0)
newItemAbsY = itemDistanceY * -1;
else
newItemAbsY = itemDistanceY;
if (oldItemAbsX + oldItemAbsY > newItemAbsX + newItemAbsY)
{
// New item is closer
tItemDistanceX = itemDistanceX;
tItemDistanceY = itemDistanceY;
}
else
{
if (oldItemAbsX + oldItemAbsY == newItemAbsX + newItemAbsY
&& (oldItemAbsY > newItemAbsY || (oldItemAbsY == newItemAbsY && tItemDistanceY < itemDistanceY)))
{
// If items are equal distance, use whichever is closer on the Y axis or further south
tItemDistanceX = itemDistanceX;
tItemDistanceY = itemDistanceY;
}
}
}
}
static u8 GetDirectionToHiddenItem(s16 itemDistanceX, s16 itemDistanceY)
{
s16 absX, absY;
if (itemDistanceX == 0 && itemDistanceY == 0)
return DIR_NONE; // player is standing on the item.
// Get absolute X distance.
if (itemDistanceX < 0)
absX = itemDistanceX * -1;
else
absX = itemDistanceX;
// Get absolute Y distance.
if (itemDistanceY < 0)
absY = itemDistanceY * -1;
else
absY = itemDistanceY;
if (absX > absY)
{
if (itemDistanceX < 0)
return DIR_EAST;
else
return DIR_NORTH;
}
else
{
if (absX < absY)
{
if (itemDistanceY < 0)
return DIR_SOUTH;
else
return DIR_WEST;
}
if (absX == absY)
{
if (itemDistanceY < 0)
return DIR_SOUTH;
else
return DIR_WEST;
}
return DIR_NONE; // Unreachable
}
}
static void PlayerFaceHiddenItem(u8 direction)
{
ObjectEventClearHeldMovementIfFinished(&gObjectEvents[GetObjectEventIdByLocalIdAndMap(OBJ_EVENT_ID_PLAYER, 0, 0)]);
ObjectEventClearHeldMovement(&gObjectEvents[GetObjectEventIdByLocalIdAndMap(OBJ_EVENT_ID_PLAYER, 0, 0)]);
UnfreezeObjectEvent(&gObjectEvents[GetObjectEventIdByLocalIdAndMap(OBJ_EVENT_ID_PLAYER, 0, 0)]);
PlayerTurnInPlace(direction);
}
static void Task_HiddenItemNearby(u8 taskId)
{
if (ObjectEventCheckHeldMovementStatus(&gObjectEvents[GetObjectEventIdByLocalIdAndMap(OBJ_EVENT_ID_PLAYER, 0, 0)]) == TRUE)
DisplayItemMessageOnField(taskId, gText_ItemFinderNearby, Task_CloseItemfinderMessage);
}
static void Task_StandingOnHiddenItem(u8 taskId)
{
s16 *data = gTasks[taskId].data;
if (ObjectEventCheckHeldMovementStatus(&gObjectEvents[GetObjectEventIdByLocalIdAndMap(OBJ_EVENT_ID_PLAYER, 0, 0)]) == TRUE
|| tItemFound == FALSE)
{
// Spin player around on item
PlayerFaceHiddenItem(sClockwiseDirections[tFacingDir]);
tItemFound = TRUE;
tFacingDir = (tFacingDir + 1) & 3;
tCounter++;
if (tCounter == 4)
DisplayItemMessageOnField(taskId, gText_ItemFinderOnTop, Task_CloseItemfinderMessage);
}
}
// Undefine itemfinder task data
#undef tItemDistanceX
#undef tItemDistanceY
#undef tItemFound
#undef tCounter
#undef tItemfinderBeeps
#undef tFacingDir
void ItemUseOutOfBattle_PokeblockCase(u8 taskId)
{
if (MenuHelpers_IsLinkActive() == TRUE)
{
DisplayDadsAdviceCannotUseItemMessage(taskId, gTasks[taskId].tUsingRegisteredKeyItem);
}
else if (gTasks[taskId].tUsingRegisteredKeyItem != TRUE)
{
gBagMenu->newScreenCallback = CB2_OpenPokeblockFromBag;
Task_FadeAndCloseBagMenu(taskId);
}
else
{
gFieldCallback = FieldCB_ReturnToFieldNoScript;
FadeScreen(FADE_TO_BLACK, 0);
gTasks[taskId].func = Task_OpenRegisteredPokeblockCase;
}
}
static void CB2_OpenPokeblockFromBag(void)
{
OpenPokeblockCase(PBLOCK_CASE_FIELD, CB2_ReturnToBagMenuPocket);
}
static void Task_OpenRegisteredPokeblockCase(u8 taskId)
{
if (!gPaletteFade.active)
{
CleanupOverworldWindowsAndTilemaps();
OpenPokeblockCase(PBLOCK_CASE_FIELD, CB2_ReturnToField);
DestroyTask(taskId);
}
}
void ItemUseOutOfBattle_CoinCase(u8 taskId)
{
ConvertIntToDecimalStringN(gStringVar1, GetCoins(), STR_CONV_MODE_LEFT_ALIGN, 4);
StringExpandPlaceholders(gStringVar4, gText_CoinCase);
if (!gTasks[taskId].tUsingRegisteredKeyItem)
{
DisplayItemMessage(taskId, FONT_NORMAL, gStringVar4, CloseItemMessage);
}
else
{
DisplayItemMessageOnField(taskId, gStringVar4, Task_CloseCantUseKeyItemMessage);
}
}
void ItemUseOutOfBattle_PowderJar(u8 taskId)
{
ConvertIntToDecimalStringN(gStringVar1, GetBerryPowder(), STR_CONV_MODE_LEFT_ALIGN, 5);
StringExpandPlaceholders(gStringVar4, gText_PowderQty);
if (!gTasks[taskId].tUsingRegisteredKeyItem)
{
DisplayItemMessage(taskId, FONT_NORMAL, gStringVar4, CloseItemMessage);
}
else
{
DisplayItemMessageOnField(taskId, gStringVar4, Task_CloseCantUseKeyItemMessage);
}
}
void ItemUseOutOfBattle_Berry(u8 taskId)
{
if (IsPlayerFacingEmptyBerryTreePatch() == TRUE)
{
sItemUseOnFieldCB = ItemUseOnFieldCB_Berry;
gFieldCallback = FieldCB_UseItemOnField;
gBagMenu->newScreenCallback = CB2_ReturnToField;
Task_FadeAndCloseBagMenu(taskId);
}
else
{
ItemId_GetFieldFunc(gSpecialVar_ItemId)(taskId);
}
}
static void ItemUseOnFieldCB_Berry(u8 taskId)
{
RemoveBagItem(gSpecialVar_ItemId, 1);
LockPlayerFieldControls();
ScriptContext_SetupScript(BerryTree_EventScript_ItemUsePlantBerry);
DestroyTask(taskId);
}
void ItemUseOutOfBattle_WailmerPail(u8 taskId)
{
if (TryToWaterSudowoodo() == TRUE)
{
sItemUseOnFieldCB = ItemUseOnFieldCB_WailmerPailSudowoodo;
SetUpItemUseOnFieldCallback(taskId);
}
else if (TryToWaterBerryTree() == TRUE)
{
sItemUseOnFieldCB = ItemUseOnFieldCB_WailmerPailBerry;
SetUpItemUseOnFieldCallback(taskId);
}
else
{
DisplayDadsAdviceCannotUseItemMessage(taskId, gTasks[taskId].tUsingRegisteredKeyItem);
}
}
static void ItemUseOnFieldCB_WailmerPailBerry(u8 taskId)
{
LockPlayerFieldControls();
ScriptContext_SetupScript(BerryTree_EventScript_ItemUseWailmerPail);
DestroyTask(taskId);
}
static bool8 TryToWaterSudowoodo(void)
{
s16 x, y;
u8 elevation;
u8 objId;
GetXYCoordsOneStepInFrontOfPlayer(&x, &y);
elevation = PlayerGetElevation();
objId = GetObjectEventIdByPosition(x, y, elevation);
if (objId == OBJECT_EVENTS_COUNT || gObjectEvents[objId].graphicsId != OBJ_EVENT_GFX_SUDOWOODO)
return FALSE;
else
return TRUE;
}
static void ItemUseOnFieldCB_WailmerPailSudowoodo(u8 taskId)
{
LockPlayerFieldControls();
ScriptContext_SetupScript(BattleFrontier_OutsideEast_EventScript_WaterSudowoodo);
DestroyTask(taskId);
}
void ItemUseOutOfBattle_Medicine(u8 taskId)
{
gItemUseCB = ItemUseCB_Medicine;
SetUpItemUseCallback(taskId);
}
void ItemUseOutOfBattle_ReduceEV(u8 taskId)
{
gItemUseCB = ItemUseCB_ReduceEV;
SetUpItemUseCallback(taskId);
}
void ItemUseOutOfBattle_SacredAsh(u8 taskId)
{
gItemUseCB = ItemUseCB_SacredAsh;
SetUpItemUseCallback(taskId);
}
void ItemUseOutOfBattle_PPRecovery(u8 taskId)
{
gItemUseCB = ItemUseCB_PPRecovery;
SetUpItemUseCallback(taskId);
}
void ItemUseOutOfBattle_PPUp(u8 taskId)
{
gItemUseCB = ItemUseCB_PPUp;
SetUpItemUseCallback(taskId);
}
void ItemUseOutOfBattle_RareCandy(u8 taskId)
{
gItemUseCB = ItemUseCB_RareCandy;
SetUpItemUseCallback(taskId);
}
void ItemUseOutOfBattle_TMHM(u8 taskId)
{
if (gSpecialVar_ItemId >= ITEM_HM01)
DisplayItemMessage(taskId, FONT_NORMAL, gText_BootedUpHM, BootUpSoundTMHM); // HM
else
DisplayItemMessage(taskId, FONT_NORMAL, gText_BootedUpTM, BootUpSoundTMHM); // TM
}
static void BootUpSoundTMHM(u8 taskId)
{
PlaySE(SE_PC_LOGIN);
gTasks[taskId].func = Task_ShowTMHMContainedMessage;
}
static void Task_ShowTMHMContainedMessage(u8 taskId)
{
if (JOY_NEW(A_BUTTON | B_BUTTON))
{
StringCopy(gStringVar1, gMoveNames[ItemIdToBattleMoveId(gSpecialVar_ItemId)]);
StringExpandPlaceholders(gStringVar4, gText_TMHMContainedVar1);
DisplayItemMessage(taskId, FONT_NORMAL, gStringVar4, UseTMHMYesNo);
}
}
static void UseTMHMYesNo(u8 taskId)
{
BagMenu_YesNo(taskId, ITEMWIN_YESNO_HIGH, &sUseTMHMYesNoFuncTable);
}
static void UseTMHM(u8 taskId)
{
gItemUseCB = ItemUseCB_TMHM;
SetUpItemUseCallback(taskId);
}
static void RemoveUsedItem(void)
{
RemoveBagItem(gSpecialVar_ItemId, 1);
CopyItemName(gSpecialVar_ItemId, gStringVar2);
StringExpandPlaceholders(gStringVar4, gText_PlayerUsedVar2);
if (!InBattlePyramid())
{
UpdatePocketItemList(ItemId_GetPocket(gSpecialVar_ItemId));
UpdatePocketListPosition(ItemId_GetPocket(gSpecialVar_ItemId));
}
else
{
UpdatePyramidBagList();
UpdatePyramidBagCursorPos();
}
}
void ItemUseOutOfBattle_Repel(u8 taskId)
{
if (VarGet(VAR_REPEL_STEP_COUNT) == 0)
gTasks[taskId].func = Task_StartUseRepel;
else if (!InBattlePyramid())
DisplayItemMessage(taskId, FONT_NORMAL, gText_RepelEffectsLingered, CloseItemMessage);
else
DisplayItemMessageInBattlePyramid(taskId, gText_RepelEffectsLingered, Task_CloseBattlePyramidBagMessage);
}
static void Task_StartUseRepel(u8 taskId)
{
s16 *data = gTasks[taskId].data;
if (++data[8] > 7)
{
data[8] = 0;
PlaySE(SE_REPEL);
gTasks[taskId].func = Task_UseRepel;
}
}
static void Task_UseRepel(u8 taskId)
{
if (!IsSEPlaying())
{
VarSet(VAR_REPEL_STEP_COUNT, ItemId_GetHoldEffectParam(gSpecialVar_ItemId));
RemoveUsedItem();
if (!InBattlePyramid())
DisplayItemMessage(taskId, FONT_NORMAL, gStringVar4, CloseItemMessage);
else
DisplayItemMessageInBattlePyramid(taskId, gStringVar4, Task_CloseBattlePyramidBagMessage);
}
}
static void Task_UsedBlackWhiteFlute(u8 taskId)
{
if(++gTasks[taskId].data[8] > 7)
{
PlaySE(SE_GLASS_FLUTE);
if (!InBattlePyramid())
DisplayItemMessage(taskId, FONT_NORMAL, gStringVar4, CloseItemMessage);
else
DisplayItemMessageInBattlePyramid(taskId, gStringVar4, Task_CloseBattlePyramidBagMessage);
}
}
void ItemUseOutOfBattle_BlackWhiteFlute(u8 taskId)
{
CopyItemName(gSpecialVar_ItemId, gStringVar2);
if (gSpecialVar_ItemId == ITEM_WHITE_FLUTE)
{
FlagSet(FLAG_SYS_ENC_UP_ITEM);
FlagClear(FLAG_SYS_ENC_DOWN_ITEM);
StringExpandPlaceholders(gStringVar4, gText_UsedVar2WildLured);
}
else
{
FlagSet(FLAG_SYS_ENC_DOWN_ITEM);
FlagClear(FLAG_SYS_ENC_UP_ITEM);
StringExpandPlaceholders(gStringVar4, gText_UsedVar2WildRepelled);
}
gTasks[taskId].data[8] = 0;
gTasks[taskId].func = Task_UsedBlackWhiteFlute;
}
void Task_UseDigEscapeRopeOnField(u8 taskId)
{
ResetInitialPlayerAvatarState();
StartEscapeRopeFieldEffect();
DestroyTask(taskId);
}
static void ItemUseOnFieldCB_EscapeRope(u8 taskId)
{
Overworld_ResetStateAfterDigEscRope();
RemoveUsedItem();
gTasks[taskId].data[0] = 0;
DisplayItemMessageOnField(taskId, gStringVar4, Task_UseDigEscapeRopeOnField);
}
bool8 CanUseDigOrEscapeRopeOnCurMap(void)
{
if (gMapHeader.allowEscaping)
return TRUE;
else
return FALSE;
}
void ItemUseOutOfBattle_EscapeRope(u8 taskId)
{
if (CanUseDigOrEscapeRopeOnCurMap() == TRUE)
{
sItemUseOnFieldCB = ItemUseOnFieldCB_EscapeRope;
SetUpItemUseOnFieldCallback(taskId);
}
else
{
DisplayDadsAdviceCannotUseItemMessage(taskId, gTasks[taskId].tUsingRegisteredKeyItem);
}
}
void ItemUseOutOfBattle_EvolutionStone(u8 taskId)
{
gItemUseCB = ItemUseCB_EvolutionStone;
SetUpItemUseCallback(taskId);
}
void ItemUseInBattle_PokeBall(u8 taskId)
{
if (IsPlayerPartyAndPokemonStorageFull() == FALSE) // have room for mon?
{
RemoveBagItem(gSpecialVar_ItemId, 1);
if (!InBattlePyramid())
Task_FadeAndCloseBagMenu(taskId);
else
CloseBattlePyramidBag(taskId);
}
else if (!InBattlePyramid())
{
DisplayItemMessage(taskId, FONT_NORMAL, gText_BoxFull, CloseItemMessage);
}
else
DisplayItemMessageInBattlePyramid(taskId, gText_BoxFull, Task_CloseBattlePyramidBagMessage);
}
static void Task_CloseStatIncreaseMessage(u8 taskId)
{
if (JOY_NEW(A_BUTTON | B_BUTTON))
{
if (!InBattlePyramid())
Task_FadeAndCloseBagMenu(taskId);
else
CloseBattlePyramidBag(taskId);
}
}
static void Task_UseStatIncreaseItem(u8 taskId)
{
if(++gTasks[taskId].data[8] > 7)
{
PlaySE(SE_USE_ITEM);
RemoveBagItem(gSpecialVar_ItemId, 1);
if (!InBattlePyramid())
DisplayItemMessage(taskId, FONT_NORMAL, UseStatIncreaseItem(gSpecialVar_ItemId), Task_CloseStatIncreaseMessage);
else
DisplayItemMessageInBattlePyramid(taskId, UseStatIncreaseItem(gSpecialVar_ItemId), Task_CloseStatIncreaseMessage);
}
}
// e.g. X Attack, Guard Spec
void ItemUseInBattle_StatIncrease(u8 taskId)
{
u16 partyId = gBattlerPartyIndexes[gBattlerInMenuId];
if (ExecuteTableBasedItemEffect(&gPlayerParty[partyId], gSpecialVar_ItemId, partyId, 0) != FALSE)
{
if (!InBattlePyramid())
DisplayItemMessage(taskId, FONT_NORMAL, gText_WontHaveEffect, CloseItemMessage);
else
DisplayItemMessageInBattlePyramid(taskId, gText_WontHaveEffect, Task_CloseBattlePyramidBagMessage);
}
else
{
gTasks[taskId].func = Task_UseStatIncreaseItem;
gTasks[taskId].data[8] = 0;
}
}
static void ItemUseInBattle_ShowPartyMenu(u8 taskId)