-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
field_player_avatar.c
2222 lines (1932 loc) · 68.9 KB
/
field_player_avatar.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 "main.h"
#include "bike.h"
#include "event_data.h"
#include "event_object_movement.h"
#include "field_camera.h"
#include "field_effect.h"
#include "field_effect_helpers.h"
#include "field_player_avatar.h"
#include "fieldmap.h"
#include "menu.h"
#include "metatile_behavior.h"
#include "overworld.h"
#include "party_menu.h"
#include "random.h"
#include "rotating_gate.h"
#include "script.h"
#include "sound.h"
#include "sprite.h"
#include "strings.h"
#include "task.h"
#include "tv.h"
#include "wild_encounter.h"
#include "constants/abilities.h"
#include "constants/event_objects.h"
#include "constants/event_object_movement.h"
#include "constants/field_effects.h"
#include "constants/items.h"
#include "constants/moves.h"
#include "constants/songs.h"
#include "constants/trainer_types.h"
#define NUM_FORCED_MOVEMENTS 18
#define NUM_ACRO_BIKE_COLLISIONS 5
static EWRAM_DATA u8 sSpinStartFacingDir = 0;
EWRAM_DATA struct ObjectEvent gObjectEvents[OBJECT_EVENTS_COUNT] = {};
EWRAM_DATA struct PlayerAvatar gPlayerAvatar = {};
// static declarations
static u8 ObjectEventCB2_NoMovement2();
static bool8 TryInterruptObjectEventSpecialAnim(struct ObjectEvent *, u8);
static void npc_clear_strange_bits(struct ObjectEvent *);
static void MovePlayerAvatarUsingKeypadInput(u8, u16, u16);
static void PlayerAllowForcedMovementIfMovingSameDirection();
static bool8 TryDoMetatileBehaviorForcedMovement();
static u8 GetForcedMovementByMetatileBehavior();
static bool8 ForcedMovement_None(void);
static bool8 ForcedMovement_Slip(void);
static bool8 ForcedMovement_WalkSouth(void);
static bool8 ForcedMovement_WalkNorth(void);
static bool8 ForcedMovement_WalkWest(void);
static bool8 ForcedMovement_WalkEast(void);
static bool8 ForcedMovement_PushedSouthByCurrent(void);
static bool8 ForcedMovement_PushedNorthByCurrent(void);
static bool8 ForcedMovement_PushedWestByCurrent(void);
static bool8 ForcedMovement_PushedEastByCurrent(void);
static bool8 ForcedMovement_SlideSouth(void);
static bool8 ForcedMovement_SlideNorth(void);
static bool8 ForcedMovement_SlideWest(void);
static bool8 ForcedMovement_SlideEast(void);
static bool8 ForcedMovement_MatJump(void);
static bool8 ForcedMovement_MatSpin(void);
static bool8 ForcedMovement_MuddySlope(void);
static void MovePlayerNotOnBike(u8, u16);
static u8 CheckMovementInputNotOnBike(u8);
static void PlayerNotOnBikeNotMoving(u8, u16);
static void PlayerNotOnBikeTurningInPlace(u8, u16);
static void PlayerNotOnBikeMoving(u8, u16);
static u8 CheckForPlayerAvatarCollision(u8);
static u8 CheckForPlayerAvatarStaticCollision(u8);
static u8 CheckForObjectEventStaticCollision(struct ObjectEvent *, s16, s16, u8, u8);
static bool8 CanStopSurfing(s16, s16, u8);
static bool8 ShouldJumpLedge(s16, s16, u8);
static bool8 TryPushBoulder(s16, s16, u8);
static void CheckAcroBikeCollision(s16, s16, u8, u8 *);
static void DoPlayerAvatarTransition(void);
static void PlayerAvatarTransition_Dummy(struct ObjectEvent *);
static void PlayerAvatarTransition_Normal(struct ObjectEvent *);
static void PlayerAvatarTransition_MachBike(struct ObjectEvent *);
static void PlayerAvatarTransition_AcroBike(struct ObjectEvent *);
static void PlayerAvatarTransition_Surfing(struct ObjectEvent *);
static void PlayerAvatarTransition_Underwater(struct ObjectEvent *);
static void PlayerAvatarTransition_ReturnToField(struct ObjectEvent *);
static bool8 PlayerAnimIsMultiFrameStationary(void);
static bool8 PlayerAnimIsMultiFrameStationaryAndStateNotTurning(void);
static bool8 PlayerIsAnimActive(void);
static bool8 PlayerCheckIfAnimFinishedOrInactive(void);
static void PlayerRun(u8);
static void PlayerNotOnBikeCollide(u8);
static void PlayerNotOnBikeCollideWithFarawayIslandMew(u8);
static void PlayCollisionSoundIfNotFacingWarp(u8);
static void HideShowWarpArrow(struct ObjectEvent *);
static void StartStrengthAnim(u8, u8);
static void Task_PushBoulder(u8);
static bool8 PushBoulder_Start(struct Task *, struct ObjectEvent *, struct ObjectEvent *);
static bool8 PushBoulder_Move(struct Task *, struct ObjectEvent *, struct ObjectEvent *);
static bool8 PushBoulder_End(struct Task *, struct ObjectEvent *, struct ObjectEvent *);
static void DoPlayerMatJump(void);
static void DoPlayerAvatarSecretBaseMatJump(u8);
static u8 PlayerAvatar_DoSecretBaseMatJump(struct Task *, struct ObjectEvent *);
static void DoPlayerMatSpin(void);
static void PlayerAvatar_DoSecretBaseMatSpin(u8);
static bool8 PlayerAvatar_SecretBaseMatSpinStep0(struct Task *, struct ObjectEvent *);
static bool8 PlayerAvatar_SecretBaseMatSpinStep1(struct Task *, struct ObjectEvent *);
static bool8 PlayerAvatar_SecretBaseMatSpinStep2(struct Task *, struct ObjectEvent *);
static bool8 PlayerAvatar_SecretBaseMatSpinStep3(struct Task *, struct ObjectEvent *);
static void CreateStopSurfingTask(u8);
static void Task_StopSurfingInit(u8);
static void Task_WaitStopSurfing(u8);
static void Task_Fishing(u8);
static u8 Fishing_Init(struct Task *);
static u8 Fishing_GetRodOut(struct Task *);
static u8 Fishing_WaitBeforeDots(struct Task *);
static u8 Fishing_InitDots(struct Task *);
static u8 Fishing_ShowDots(struct Task *);
static u8 Fishing_CheckForBite(struct Task *);
static u8 Fishing_GotBite(struct Task *);
static u8 Fishing_WaitForA(struct Task *);
static u8 Fishing_CheckMoreDots(struct Task *);
static u8 Fishing_MonOnHook(struct Task *);
static u8 Fishing_StartEncounter(struct Task *);
static u8 Fishing_NotEvenNibble(struct Task *);
static u8 Fishing_GotAway(struct Task *);
static u8 Fishing_NoMon(struct Task *);
static u8 Fishing_PutRodAway(struct Task *);
static u8 Fishing_EndNoMon(struct Task *);
static void AlignFishingAnimationFrames(void);
static u8 TrySpinPlayerForWarp(struct ObjectEvent *, s16 *);
static bool8 (*const sForcedMovementTestFuncs[NUM_FORCED_MOVEMENTS])(u8) =
{
MetatileBehavior_IsTrickHouseSlipperyFloor,
MetatileBehavior_IsIce_2,
MetatileBehavior_IsWalkSouth,
MetatileBehavior_IsWalkNorth,
MetatileBehavior_IsWalkWest,
MetatileBehavior_IsWalkEast,
MetatileBehavior_IsSouthwardCurrent,
MetatileBehavior_IsNorthwardCurrent,
MetatileBehavior_IsWestwardCurrent,
MetatileBehavior_IsEastwardCurrent,
MetatileBehavior_IsSlideSouth,
MetatileBehavior_IsSlideNorth,
MetatileBehavior_IsSlideWest,
MetatileBehavior_IsSlideEast,
MetatileBehavior_IsWaterfall,
MetatileBehavior_IsSecretBaseJumpMat,
MetatileBehavior_IsSecretBaseSpinMat,
MetatileBehavior_IsMuddySlope,
};
// + 1 for ForcedMovement_None, which is excluded above
static bool8 (*const sForcedMovementFuncs[NUM_FORCED_MOVEMENTS + 1])(void) =
{
ForcedMovement_None,
ForcedMovement_Slip,
ForcedMovement_Slip,
ForcedMovement_WalkSouth,
ForcedMovement_WalkNorth,
ForcedMovement_WalkWest,
ForcedMovement_WalkEast,
ForcedMovement_PushedSouthByCurrent,
ForcedMovement_PushedNorthByCurrent,
ForcedMovement_PushedWestByCurrent,
ForcedMovement_PushedEastByCurrent,
ForcedMovement_SlideSouth,
ForcedMovement_SlideNorth,
ForcedMovement_SlideWest,
ForcedMovement_SlideEast,
ForcedMovement_PushedSouthByCurrent,
ForcedMovement_MatJump,
ForcedMovement_MatSpin,
ForcedMovement_MuddySlope,
};
static void (*const sPlayerNotOnBikeFuncs[])(u8, u16) =
{
[NOT_MOVING] = PlayerNotOnBikeNotMoving,
[TURN_DIRECTION] = PlayerNotOnBikeTurningInPlace,
[MOVING] = PlayerNotOnBikeMoving,
};
static bool8 (*const sAcroBikeTrickMetatiles[NUM_ACRO_BIKE_COLLISIONS])(u8) =
{
MetatileBehavior_IsBumpySlope,
MetatileBehavior_IsIsolatedVerticalRail,
MetatileBehavior_IsIsolatedHorizontalRail,
MetatileBehavior_IsVerticalRail,
MetatileBehavior_IsHorizontalRail,
};
static const u8 sAcroBikeTrickCollisionTypes[NUM_ACRO_BIKE_COLLISIONS] = {
COLLISION_WHEELIE_HOP,
COLLISION_ISOLATED_VERTICAL_RAIL,
COLLISION_ISOLATED_HORIZONTAL_RAIL,
COLLISION_VERTICAL_RAIL,
COLLISION_HORIZONTAL_RAIL,
};
static void (*const sPlayerAvatarTransitionFuncs[])(struct ObjectEvent *) =
{
[PLAYER_AVATAR_STATE_NORMAL] = PlayerAvatarTransition_Normal,
[PLAYER_AVATAR_STATE_MACH_BIKE] = PlayerAvatarTransition_MachBike,
[PLAYER_AVATAR_STATE_ACRO_BIKE] = PlayerAvatarTransition_AcroBike,
[PLAYER_AVATAR_STATE_SURFING] = PlayerAvatarTransition_Surfing,
[PLAYER_AVATAR_STATE_UNDERWATER] = PlayerAvatarTransition_Underwater,
[PLAYER_AVATAR_STATE_FIELD_MOVE] = PlayerAvatarTransition_ReturnToField,
[PLAYER_AVATAR_STATE_FISHING] = PlayerAvatarTransition_Dummy,
[PLAYER_AVATAR_STATE_WATERING] = PlayerAvatarTransition_Dummy,
};
static bool8 (*const sArrowWarpMetatileBehaviorChecks[])(u8) =
{
[DIR_SOUTH - 1] = MetatileBehavior_IsSouthArrowWarp,
[DIR_NORTH - 1] = MetatileBehavior_IsNorthArrowWarp,
[DIR_WEST - 1] = MetatileBehavior_IsWestArrowWarp,
[DIR_EAST - 1] = MetatileBehavior_IsEastArrowWarp,
};
static const u8 sRivalAvatarGfxIds[][2] =
{
[PLAYER_AVATAR_STATE_NORMAL] = {OBJ_EVENT_GFX_RIVAL_BRENDAN_NORMAL, OBJ_EVENT_GFX_RIVAL_MAY_NORMAL},
[PLAYER_AVATAR_STATE_MACH_BIKE] = {OBJ_EVENT_GFX_RIVAL_BRENDAN_MACH_BIKE, OBJ_EVENT_GFX_RIVAL_MAY_MACH_BIKE},
[PLAYER_AVATAR_STATE_ACRO_BIKE] = {OBJ_EVENT_GFX_RIVAL_BRENDAN_ACRO_BIKE, OBJ_EVENT_GFX_RIVAL_MAY_ACRO_BIKE},
[PLAYER_AVATAR_STATE_SURFING] = {OBJ_EVENT_GFX_RIVAL_BRENDAN_SURFING, OBJ_EVENT_GFX_RIVAL_MAY_SURFING},
[PLAYER_AVATAR_STATE_UNDERWATER] = {OBJ_EVENT_GFX_BRENDAN_UNDERWATER, OBJ_EVENT_GFX_MAY_UNDERWATER},
[PLAYER_AVATAR_STATE_FIELD_MOVE] = {OBJ_EVENT_GFX_RIVAL_BRENDAN_FIELD_MOVE, OBJ_EVENT_GFX_RIVAL_MAY_FIELD_MOVE},
[PLAYER_AVATAR_STATE_FISHING] = {OBJ_EVENT_GFX_BRENDAN_FISHING, OBJ_EVENT_GFX_MAY_FISHING},
[PLAYER_AVATAR_STATE_WATERING] = {OBJ_EVENT_GFX_BRENDAN_WATERING, OBJ_EVENT_GFX_MAY_WATERING}
};
static const u8 sPlayerAvatarGfxIds[][2] =
{
[PLAYER_AVATAR_STATE_NORMAL] = {OBJ_EVENT_GFX_BRENDAN_NORMAL, OBJ_EVENT_GFX_MAY_NORMAL},
[PLAYER_AVATAR_STATE_MACH_BIKE] = {OBJ_EVENT_GFX_BRENDAN_MACH_BIKE, OBJ_EVENT_GFX_MAY_MACH_BIKE},
[PLAYER_AVATAR_STATE_ACRO_BIKE] = {OBJ_EVENT_GFX_BRENDAN_ACRO_BIKE, OBJ_EVENT_GFX_MAY_ACRO_BIKE},
[PLAYER_AVATAR_STATE_SURFING] = {OBJ_EVENT_GFX_BRENDAN_SURFING, OBJ_EVENT_GFX_MAY_SURFING},
[PLAYER_AVATAR_STATE_UNDERWATER] = {OBJ_EVENT_GFX_BRENDAN_UNDERWATER, OBJ_EVENT_GFX_MAY_UNDERWATER},
[PLAYER_AVATAR_STATE_FIELD_MOVE] = {OBJ_EVENT_GFX_BRENDAN_FIELD_MOVE, OBJ_EVENT_GFX_MAY_FIELD_MOVE},
[PLAYER_AVATAR_STATE_FISHING] = {OBJ_EVENT_GFX_BRENDAN_FISHING, OBJ_EVENT_GFX_MAY_FISHING},
[PLAYER_AVATAR_STATE_WATERING] = {OBJ_EVENT_GFX_BRENDAN_WATERING, OBJ_EVENT_GFX_MAY_WATERING},
};
static const u8 sFRLGAvatarGfxIds[GENDER_COUNT] =
{
[MALE] = OBJ_EVENT_GFX_RED,
[FEMALE] = OBJ_EVENT_GFX_LEAF
};
static const u8 sRSAvatarGfxIds[GENDER_COUNT] =
{
[MALE] = OBJ_EVENT_GFX_LINK_RS_BRENDAN,
[FEMALE] = OBJ_EVENT_GFX_LINK_RS_MAY
};
static const u8 sPlayerAvatarGfxToStateFlag[GENDER_COUNT][5][2] =
{
[MALE] =
{
{OBJ_EVENT_GFX_BRENDAN_NORMAL, PLAYER_AVATAR_FLAG_ON_FOOT},
{OBJ_EVENT_GFX_BRENDAN_MACH_BIKE, PLAYER_AVATAR_FLAG_MACH_BIKE},
{OBJ_EVENT_GFX_BRENDAN_ACRO_BIKE, PLAYER_AVATAR_FLAG_ACRO_BIKE},
{OBJ_EVENT_GFX_BRENDAN_SURFING, PLAYER_AVATAR_FLAG_SURFING},
{OBJ_EVENT_GFX_BRENDAN_UNDERWATER, PLAYER_AVATAR_FLAG_UNDERWATER},
},
[FEMALE] =
{
{OBJ_EVENT_GFX_MAY_NORMAL, PLAYER_AVATAR_FLAG_ON_FOOT},
{OBJ_EVENT_GFX_MAY_MACH_BIKE, PLAYER_AVATAR_FLAG_MACH_BIKE},
{OBJ_EVENT_GFX_MAY_ACRO_BIKE, PLAYER_AVATAR_FLAG_ACRO_BIKE},
{OBJ_EVENT_GFX_MAY_SURFING, PLAYER_AVATAR_FLAG_SURFING},
{OBJ_EVENT_GFX_MAY_UNDERWATER, PLAYER_AVATAR_FLAG_UNDERWATER},
}
};
static bool8 (*const sArrowWarpMetatileBehaviorChecks2[])(u8) = //Duplicate of sArrowWarpMetatileBehaviorChecks
{
[DIR_SOUTH - 1] = MetatileBehavior_IsSouthArrowWarp,
[DIR_NORTH - 1] = MetatileBehavior_IsNorthArrowWarp,
[DIR_WEST - 1] = MetatileBehavior_IsWestArrowWarp,
[DIR_EAST - 1] = MetatileBehavior_IsEastArrowWarp,
};
static bool8 (*const sPushBoulderFuncs[])(struct Task *, struct ObjectEvent *, struct ObjectEvent *) =
{
PushBoulder_Start,
PushBoulder_Move,
PushBoulder_End,
};
static bool8 (*const sPlayerAvatarSecretBaseMatJump[])(struct Task *, struct ObjectEvent *) =
{
PlayerAvatar_DoSecretBaseMatJump,
};
static bool8 (*const sPlayerAvatarSecretBaseMatSpin[])(struct Task *, struct ObjectEvent *) =
{
PlayerAvatar_SecretBaseMatSpinStep0,
PlayerAvatar_SecretBaseMatSpinStep1,
PlayerAvatar_SecretBaseMatSpinStep2,
PlayerAvatar_SecretBaseMatSpinStep3,
};
void MovementType_Player(struct Sprite *sprite)
{
UpdateObjectEventCurrentMovement(&gObjectEvents[sprite->data[0]], sprite, ObjectEventCB2_NoMovement2);
}
static u8 ObjectEventCB2_NoMovement2(void)
{
return 0;
}
void PlayerStep(u8 direction, u16 newKeys, u16 heldKeys)
{
struct ObjectEvent *playerObjEvent = &gObjectEvents[gPlayerAvatar.objectEventId];
HideShowWarpArrow(playerObjEvent);
if (gPlayerAvatar.preventStep == FALSE)
{
Bike_TryAcroBikeHistoryUpdate(newKeys, heldKeys);
if (TryInterruptObjectEventSpecialAnim(playerObjEvent, direction) == 0)
{
npc_clear_strange_bits(playerObjEvent);
DoPlayerAvatarTransition();
if (TryDoMetatileBehaviorForcedMovement() == 0)
{
MovePlayerAvatarUsingKeypadInput(direction, newKeys, heldKeys);
PlayerAllowForcedMovementIfMovingSameDirection();
}
}
}
}
static bool8 TryInterruptObjectEventSpecialAnim(struct ObjectEvent *playerObjEvent, u8 direction)
{
if (ObjectEventIsMovementOverridden(playerObjEvent)
&& !ObjectEventClearHeldMovementIfFinished(playerObjEvent))
{
u8 heldMovementActionId = ObjectEventGetHeldMovementActionId(playerObjEvent);
if (heldMovementActionId > MOVEMENT_ACTION_WALK_FAST_RIGHT && heldMovementActionId < MOVEMENT_ACTION_WALK_IN_PLACE_NORMAL_DOWN)
{
if (direction == DIR_NONE)
{
return TRUE;
}
if (playerObjEvent->movementDirection != direction)
{
ObjectEventClearHeldMovement(playerObjEvent);
return FALSE;
}
if (CheckForPlayerAvatarStaticCollision(direction) == COLLISION_NONE)
{
ObjectEventClearHeldMovement(playerObjEvent);
return FALSE;
}
}
return TRUE;
}
return FALSE;
}
static void npc_clear_strange_bits(struct ObjectEvent *objEvent)
{
objEvent->inanimate = FALSE;
objEvent->disableAnim = FALSE;
objEvent->facingDirectionLocked = FALSE;
gPlayerAvatar.flags &= ~PLAYER_AVATAR_FLAG_DASH;
}
static void MovePlayerAvatarUsingKeypadInput(u8 direction, u16 newKeys, u16 heldKeys)
{
if (gPlayerAvatar.flags & (PLAYER_AVATAR_FLAG_MACH_BIKE | PLAYER_AVATAR_FLAG_ACRO_BIKE))
MovePlayerOnBike(direction, newKeys, heldKeys);
else
MovePlayerNotOnBike(direction, heldKeys);
}
static void PlayerAllowForcedMovementIfMovingSameDirection(void)
{
if (gPlayerAvatar.runningState == MOVING)
gPlayerAvatar.flags &= ~PLAYER_AVATAR_FLAG_CONTROLLABLE;
}
static bool8 TryDoMetatileBehaviorForcedMovement(void)
{
return sForcedMovementFuncs[GetForcedMovementByMetatileBehavior()]();
}
static u8 GetForcedMovementByMetatileBehavior(void)
{
u8 i;
if (!(gPlayerAvatar.flags & PLAYER_AVATAR_FLAG_CONTROLLABLE))
{
u8 metatileBehavior = gObjectEvents[gPlayerAvatar.objectEventId].currentMetatileBehavior;
for (i = 0; i < NUM_FORCED_MOVEMENTS; i++)
{
if (sForcedMovementTestFuncs[i](metatileBehavior))
return i + 1;
}
}
return 0;
}
static bool8 ForcedMovement_None(void)
{
if (gPlayerAvatar.flags & PLAYER_AVATAR_FLAG_FORCED_MOVE)
{
struct ObjectEvent *playerObjEvent = &gObjectEvents[gPlayerAvatar.objectEventId];
playerObjEvent->facingDirectionLocked = FALSE;
playerObjEvent->enableAnim = TRUE;
SetObjectEventDirection(playerObjEvent, playerObjEvent->facingDirection);
gPlayerAvatar.flags &= ~PLAYER_AVATAR_FLAG_FORCED_MOVE;
}
return FALSE;
}
static bool8 DoForcedMovement(u8 direction, void (*moveFunc)(u8))
{
struct PlayerAvatar *playerAvatar = &gPlayerAvatar;
u8 collision = CheckForPlayerAvatarCollision(direction);
playerAvatar->flags |= PLAYER_AVATAR_FLAG_FORCED_MOVE;
if (collision)
{
ForcedMovement_None();
if (collision < COLLISION_STOP_SURFING)
{
return FALSE;
}
else
{
if (collision == COLLISION_LEDGE_JUMP)
PlayerJumpLedge(direction);
playerAvatar->flags |= PLAYER_AVATAR_FLAG_FORCED_MOVE;
playerAvatar->runningState = MOVING;
return TRUE;
}
}
else
{
playerAvatar->runningState = MOVING;
moveFunc(direction);
return TRUE;
}
}
static bool8 DoForcedMovementInCurrentDirection(void (*moveFunc)(u8))
{
struct ObjectEvent *playerObjEvent = &gObjectEvents[gPlayerAvatar.objectEventId];
playerObjEvent->disableAnim = TRUE;
return DoForcedMovement(playerObjEvent->movementDirection, moveFunc);
}
static bool8 ForcedMovement_Slip(void)
{
return DoForcedMovementInCurrentDirection(PlayerWalkFast);
}
static bool8 ForcedMovement_WalkSouth(void)
{
return DoForcedMovement(DIR_SOUTH, PlayerWalkNormal);
}
static bool8 ForcedMovement_WalkNorth(void)
{
return DoForcedMovement(DIR_NORTH, PlayerWalkNormal);
}
static bool8 ForcedMovement_WalkWest(void)
{
return DoForcedMovement(DIR_WEST, PlayerWalkNormal);
}
static bool8 ForcedMovement_WalkEast(void)
{
return DoForcedMovement(DIR_EAST, PlayerWalkNormal);
}
static bool8 ForcedMovement_PushedSouthByCurrent(void)
{
return DoForcedMovement(DIR_SOUTH, PlayerRideWaterCurrent);
}
static bool8 ForcedMovement_PushedNorthByCurrent(void)
{
return DoForcedMovement(DIR_NORTH, PlayerRideWaterCurrent);
}
static bool8 ForcedMovement_PushedWestByCurrent(void)
{
return DoForcedMovement(DIR_WEST, PlayerRideWaterCurrent);
}
static bool8 ForcedMovement_PushedEastByCurrent(void)
{
return DoForcedMovement(DIR_EAST, PlayerRideWaterCurrent);
}
static bool8 ForcedMovement_Slide(u8 direction, void (*moveFunc)(u8))
{
struct ObjectEvent *playerObjEvent = &gObjectEvents[gPlayerAvatar.objectEventId];
playerObjEvent->disableAnim = TRUE;
playerObjEvent->facingDirectionLocked = TRUE;
return DoForcedMovement(direction, moveFunc);
}
static bool8 ForcedMovement_SlideSouth(void)
{
return ForcedMovement_Slide(DIR_SOUTH, PlayerWalkFast);
}
static bool8 ForcedMovement_SlideNorth(void)
{
return ForcedMovement_Slide(DIR_NORTH, PlayerWalkFast);
}
static bool8 ForcedMovement_SlideWest(void)
{
return ForcedMovement_Slide(DIR_WEST, PlayerWalkFast);
}
static bool8 ForcedMovement_SlideEast(void)
{
return ForcedMovement_Slide(DIR_EAST, PlayerWalkFast);
}
static bool8 ForcedMovement_MatJump(void)
{
DoPlayerMatJump();
return TRUE;
}
static bool8 ForcedMovement_MatSpin(void)
{
DoPlayerMatSpin();
return TRUE;
}
static bool8 ForcedMovement_MuddySlope(void)
{
struct ObjectEvent *playerObjEvent = &gObjectEvents[gPlayerAvatar.objectEventId];
if (playerObjEvent->movementDirection != DIR_NORTH || GetPlayerSpeed() < PLAYER_SPEED_FASTEST)
{
Bike_UpdateBikeCounterSpeed(0);
playerObjEvent->facingDirectionLocked = TRUE;
return DoForcedMovement(DIR_SOUTH, PlayerWalkFast);
}
else
{
return FALSE;
}
}
static void MovePlayerNotOnBike(u8 direction, u16 heldKeys)
{
sPlayerNotOnBikeFuncs[CheckMovementInputNotOnBike(direction)](direction, heldKeys);
}
static u8 CheckMovementInputNotOnBike(u8 direction)
{
if (direction == DIR_NONE)
return gPlayerAvatar.runningState = NOT_MOVING;
else if (direction != GetPlayerMovementDirection() && gPlayerAvatar.runningState != MOVING)
return gPlayerAvatar.runningState = TURN_DIRECTION;
else
return gPlayerAvatar.runningState = MOVING;
}
static void PlayerNotOnBikeNotMoving(u8 direction, u16 heldKeys)
{
PlayerFaceDirection(GetPlayerFacingDirection());
}
static void PlayerNotOnBikeTurningInPlace(u8 direction, u16 heldKeys)
{
PlayerTurnInPlace(direction);
}
static void PlayerNotOnBikeMoving(u8 direction, u16 heldKeys)
{
u8 collision = CheckForPlayerAvatarCollision(direction);
if (collision)
{
if (collision == COLLISION_LEDGE_JUMP)
{
PlayerJumpLedge(direction);
return;
}
else if (collision == COLLISION_OBJECT_EVENT && IsPlayerCollidingWithFarawayIslandMew(direction))
{
PlayerNotOnBikeCollideWithFarawayIslandMew(direction);
return;
}
else
{
u8 adjustedCollision = collision - COLLISION_STOP_SURFING;
if (adjustedCollision > 3)
PlayerNotOnBikeCollide(direction);
return;
}
}
if (gPlayerAvatar.flags & PLAYER_AVATAR_FLAG_SURFING)
{
// same speed as running
PlayerWalkFast(direction);
return;
}
if (!(gPlayerAvatar.flags & PLAYER_AVATAR_FLAG_UNDERWATER) && (heldKeys & B_BUTTON) && FlagGet(FLAG_SYS_B_DASH)
&& IsRunningDisallowed(gObjectEvents[gPlayerAvatar.objectEventId].currentMetatileBehavior) == 0)
{
PlayerRun(direction);
gPlayerAvatar.flags |= PLAYER_AVATAR_FLAG_DASH;
return;
}
else
{
PlayerWalkNormal(direction);
}
}
static u8 CheckForPlayerAvatarCollision(u8 direction)
{
s16 x, y;
struct ObjectEvent *playerObjEvent = &gObjectEvents[gPlayerAvatar.objectEventId];
x = playerObjEvent->currentCoords.x;
y = playerObjEvent->currentCoords.y;
MoveCoords(direction, &x, &y);
return CheckForObjectEventCollision(playerObjEvent, x, y, direction, MapGridGetMetatileBehaviorAt(x, y));
}
static u8 CheckForPlayerAvatarStaticCollision(u8 direction)
{
s16 x, y;
struct ObjectEvent *playerObjEvent = &gObjectEvents[gPlayerAvatar.objectEventId];
x = playerObjEvent->currentCoords.x;
y = playerObjEvent->currentCoords.y;
MoveCoords(direction, &x, &y);
return CheckForObjectEventStaticCollision(playerObjEvent, x, y, direction, MapGridGetMetatileBehaviorAt(x, y));
}
u8 CheckForObjectEventCollision(struct ObjectEvent *objectEvent, s16 x, s16 y, u8 direction, u8 metatileBehavior)
{
u8 collision = GetCollisionAtCoords(objectEvent, x, y, direction);
if (collision == COLLISION_ELEVATION_MISMATCH && CanStopSurfing(x, y, direction))
return COLLISION_STOP_SURFING;
if (ShouldJumpLedge(x, y, direction))
{
IncrementGameStat(GAME_STAT_JUMPED_DOWN_LEDGES);
return COLLISION_LEDGE_JUMP;
}
if (collision == COLLISION_OBJECT_EVENT && TryPushBoulder(x, y, direction))
return COLLISION_PUSHED_BOULDER;
if (collision == COLLISION_NONE)
{
if (CheckForRotatingGatePuzzleCollision(direction, x, y))
return COLLISION_ROTATING_GATE;
CheckAcroBikeCollision(x, y, metatileBehavior, &collision);
}
return collision;
}
static u8 CheckForObjectEventStaticCollision(struct ObjectEvent *objectEvent, s16 x, s16 y, u8 direction, u8 metatileBehavior)
{
u8 collision = GetCollisionAtCoords(objectEvent, x, y, direction);
if (collision == COLLISION_NONE)
{
if (CheckForRotatingGatePuzzleCollisionWithoutAnimation(direction, x, y))
return COLLISION_ROTATING_GATE;
CheckAcroBikeCollision(x, y, metatileBehavior, &collision);
}
return collision;
}
static bool8 CanStopSurfing(s16 x, s16 y, u8 direction)
{
if ((gPlayerAvatar.flags & PLAYER_AVATAR_FLAG_SURFING)
&& MapGridGetElevationAt(x, y) == 3
&& GetObjectEventIdByPosition(x, y, 3) == OBJECT_EVENTS_COUNT)
{
CreateStopSurfingTask(direction);
return TRUE;
}
else
{
return FALSE;
}
}
static bool8 ShouldJumpLedge(s16 x, s16 y, u8 direction)
{
if (GetLedgeJumpDirection(x, y, direction) != DIR_NONE)
return TRUE;
else
return FALSE;
}
static bool8 TryPushBoulder(s16 x, s16 y, u8 direction)
{
if (FlagGet(FLAG_SYS_USE_STRENGTH))
{
u8 objectEventId = GetObjectEventIdByXY(x, y);
if (objectEventId != OBJECT_EVENTS_COUNT && gObjectEvents[objectEventId].graphicsId == OBJ_EVENT_GFX_PUSHABLE_BOULDER)
{
x = gObjectEvents[objectEventId].currentCoords.x;
y = gObjectEvents[objectEventId].currentCoords.y;
MoveCoords(direction, &x, &y);
if (GetCollisionAtCoords(&gObjectEvents[objectEventId], x, y, direction) == COLLISION_NONE
&& MetatileBehavior_IsNonAnimDoor(MapGridGetMetatileBehaviorAt(x, y)) == FALSE)
{
StartStrengthAnim(objectEventId, direction);
return TRUE;
}
}
}
return FALSE;
}
static void CheckAcroBikeCollision(s16 x, s16 y, u8 metatileBehavior, u8 *collision)
{
u8 i;
for (i = 0; i < NUM_ACRO_BIKE_COLLISIONS; i++)
{
if (sAcroBikeTrickMetatiles[i](metatileBehavior))
{
*collision = sAcroBikeTrickCollisionTypes[i];
return;
}
}
}
bool8 IsPlayerCollidingWithFarawayIslandMew(u8 direction)
{
u8 mewObjectId;
struct ObjectEvent *object;
s16 playerX;
s16 playerY;
s16 mewPrevX;
object = &gObjectEvents[gPlayerAvatar.objectEventId];
playerX = object->currentCoords.x;
playerY = object->currentCoords.y;
MoveCoords(direction, &playerX, &playerY);
mewObjectId = GetObjectEventIdByLocalIdAndMap(1, MAP_NUM(FARAWAY_ISLAND_INTERIOR), MAP_GROUP(FARAWAY_ISLAND_INTERIOR));
if (mewObjectId == OBJECT_EVENTS_COUNT)
return FALSE;
object = &gObjectEvents[mewObjectId];
mewPrevX = object->previousCoords.x;
if (mewPrevX == playerX)
{
if (object->previousCoords.y != playerY
|| object->currentCoords.x != mewPrevX
|| object->currentCoords.y != object->previousCoords.y)
{
if (object->previousCoords.x == playerX &&
object->previousCoords.y == playerY)
return TRUE;
}
}
return FALSE;
}
void SetPlayerAvatarTransitionFlags(u16 transitionFlags)
{
gPlayerAvatar.transitionFlags |= transitionFlags;
DoPlayerAvatarTransition();
}
static void DoPlayerAvatarTransition(void)
{
u8 i;
u8 flags = gPlayerAvatar.transitionFlags;
if (flags != 0)
{
for (i = 0; i < ARRAY_COUNT(sPlayerAvatarTransitionFuncs); i++, flags >>= 1)
{
if (flags & 1)
sPlayerAvatarTransitionFuncs[i](&gObjectEvents[gPlayerAvatar.objectEventId]);
}
gPlayerAvatar.transitionFlags = 0;
}
}
static void PlayerAvatarTransition_Dummy(struct ObjectEvent *objEvent)
{
}
static void PlayerAvatarTransition_Normal(struct ObjectEvent *objEvent)
{
ObjectEventSetGraphicsId(objEvent, GetPlayerAvatarGraphicsIdByStateId(PLAYER_AVATAR_STATE_NORMAL));
ObjectEventTurn(objEvent, objEvent->movementDirection);
SetPlayerAvatarStateMask(PLAYER_AVATAR_FLAG_ON_FOOT);
}
static void PlayerAvatarTransition_MachBike(struct ObjectEvent *objEvent)
{
ObjectEventSetGraphicsId(objEvent, GetPlayerAvatarGraphicsIdByStateId(PLAYER_AVATAR_STATE_MACH_BIKE));
ObjectEventTurn(objEvent, objEvent->movementDirection);
SetPlayerAvatarStateMask(PLAYER_AVATAR_FLAG_MACH_BIKE);
BikeClearState(0, 0);
}
static void PlayerAvatarTransition_AcroBike(struct ObjectEvent *objEvent)
{
ObjectEventSetGraphicsId(objEvent, GetPlayerAvatarGraphicsIdByStateId(PLAYER_AVATAR_STATE_ACRO_BIKE));
ObjectEventTurn(objEvent, objEvent->movementDirection);
SetPlayerAvatarStateMask(PLAYER_AVATAR_FLAG_ACRO_BIKE);
BikeClearState(0, 0);
Bike_HandleBumpySlopeJump();
}
static void PlayerAvatarTransition_Surfing(struct ObjectEvent *objEvent)
{
u8 spriteId;
ObjectEventSetGraphicsId(objEvent, GetPlayerAvatarGraphicsIdByStateId(PLAYER_AVATAR_STATE_SURFING));
ObjectEventTurn(objEvent, objEvent->movementDirection);
SetPlayerAvatarStateMask(PLAYER_AVATAR_FLAG_SURFING);
gFieldEffectArguments[0] = objEvent->currentCoords.x;
gFieldEffectArguments[1] = objEvent->currentCoords.y;
gFieldEffectArguments[2] = gPlayerAvatar.objectEventId;
spriteId = FieldEffectStart(FLDEFF_SURF_BLOB);
objEvent->fieldEffectSpriteId = spriteId;
SetSurfBlob_BobState(spriteId, BOB_PLAYER_AND_MON);
}
static void PlayerAvatarTransition_Underwater(struct ObjectEvent *objEvent)
{
ObjectEventSetGraphicsId(objEvent, GetPlayerAvatarGraphicsIdByStateId(PLAYER_AVATAR_STATE_UNDERWATER));
ObjectEventTurn(objEvent, objEvent->movementDirection);
SetPlayerAvatarStateMask(PLAYER_AVATAR_FLAG_UNDERWATER);
objEvent->fieldEffectSpriteId = StartUnderwaterSurfBlobBobbing(objEvent->spriteId);
}
static void PlayerAvatarTransition_ReturnToField(struct ObjectEvent *objEvent)
{
gPlayerAvatar.flags |= PLAYER_AVATAR_FLAG_CONTROLLABLE;
}
void UpdatePlayerAvatarTransitionState(void)
{
gPlayerAvatar.tileTransitionState = T_NOT_MOVING;
if (PlayerIsAnimActive())
{
if (!PlayerCheckIfAnimFinishedOrInactive())
{
if (!PlayerAnimIsMultiFrameStationary())
gPlayerAvatar.tileTransitionState = T_TILE_TRANSITION;
}
else
{
if (!PlayerAnimIsMultiFrameStationaryAndStateNotTurning())
gPlayerAvatar.tileTransitionState = T_TILE_CENTER;
}
}
}
static bool8 PlayerAnimIsMultiFrameStationary(void)
{
u8 movementActionId = gObjectEvents[gPlayerAvatar.objectEventId].movementActionId;
if (movementActionId <= MOVEMENT_ACTION_FACE_RIGHT
|| (movementActionId >= MOVEMENT_ACTION_DELAY_1 && movementActionId <= MOVEMENT_ACTION_DELAY_16)
|| (movementActionId >= MOVEMENT_ACTION_WALK_IN_PLACE_SLOW_DOWN && movementActionId <= MOVEMENT_ACTION_WALK_IN_PLACE_FASTER_RIGHT)
|| (movementActionId >= MOVEMENT_ACTION_ACRO_WHEELIE_FACE_DOWN && movementActionId <= MOVEMENT_ACTION_ACRO_END_WHEELIE_FACE_RIGHT)
|| (movementActionId >= MOVEMENT_ACTION_ACRO_WHEELIE_IN_PLACE_DOWN && movementActionId <= MOVEMENT_ACTION_ACRO_WHEELIE_IN_PLACE_RIGHT))
return TRUE;
else
return FALSE;
}
static bool8 PlayerAnimIsMultiFrameStationaryAndStateNotTurning(void)
{
if (PlayerAnimIsMultiFrameStationary() && gPlayerAvatar.runningState != TURN_DIRECTION)
return TRUE;
else
return FALSE;
}
static bool8 PlayerIsAnimActive(void)
{
return ObjectEventIsMovementOverridden(&gObjectEvents[gPlayerAvatar.objectEventId]);
}
static bool8 PlayerCheckIfAnimFinishedOrInactive(void)
{
return ObjectEventCheckHeldMovementStatus(&gObjectEvents[gPlayerAvatar.objectEventId]);
}
static void PlayerSetCopyableMovement(u8 movement)
{
gObjectEvents[gPlayerAvatar.objectEventId].playerCopyableMovement = movement;
}
u8 PlayerGetCopyableMovement(void)
{
return gObjectEvents[gPlayerAvatar.objectEventId].playerCopyableMovement;
}
static void PlayerForceSetHeldMovement(u8 movementActionId)
{
ObjectEventForceSetHeldMovement(&gObjectEvents[gPlayerAvatar.objectEventId], movementActionId);
}
void PlayerSetAnimId(u8 movementActionId, u8 copyableMovement)
{
if (!PlayerIsAnimActive())
{
PlayerSetCopyableMovement(copyableMovement);
ObjectEventSetHeldMovement(&gObjectEvents[gPlayerAvatar.objectEventId], movementActionId);
}
}
void PlayerWalkNormal(u8 direction)
{
PlayerSetAnimId(GetWalkNormalMovementAction(direction), COPY_MOVE_WALK);
}
void PlayerWalkFast(u8 direction)
{
PlayerSetAnimId(GetWalkFastMovementAction(direction), COPY_MOVE_WALK);
}
void PlayerRideWaterCurrent(u8 direction)
{
PlayerSetAnimId(GetRideWaterCurrentMovementAction(direction), COPY_MOVE_WALK);
}
void PlayerWalkFaster(u8 direction)
{
PlayerSetAnimId(GetWalkFasterMovementAction(direction), COPY_MOVE_WALK);
}
static void PlayerRun(u8 direction)
{
PlayerSetAnimId(GetPlayerRunMovementAction(direction), COPY_MOVE_WALK);
}
void PlayerOnBikeCollide(u8 direction)
{
PlayCollisionSoundIfNotFacingWarp(direction);
PlayerSetAnimId(GetWalkInPlaceNormalMovementAction(direction), COPY_MOVE_WALK);
}
void PlayerOnBikeCollideWithFarawayIslandMew(u8 direction)
{
PlayerSetAnimId(GetWalkInPlaceNormalMovementAction(direction), COPY_MOVE_WALK);
}
static void PlayerNotOnBikeCollide(u8 direction)
{
PlayCollisionSoundIfNotFacingWarp(direction);
PlayerSetAnimId(GetWalkInPlaceSlowMovementAction(direction), COPY_MOVE_WALK);
}
static void PlayerNotOnBikeCollideWithFarawayIslandMew(u8 direction)
{
PlayerSetAnimId(GetWalkInPlaceSlowMovementAction(direction), COPY_MOVE_WALK);
}