-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy pathbike.c
1062 lines (950 loc) · 36.9 KB
/
bike.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 "bike.h"
#include "event_object_movement.h"
#include "field_player_avatar.h"
#include "fieldmap.h"
#include "field_specials.h"
#include "metatile_behavior.h"
#include "overworld.h"
#include "sound.h"
#include "constants/map_types.h"
#include "constants/songs.h"
// this file's functions
static void MovePlayerOnMachBike(u8, u16, u16);
static u8 GetMachBikeTransition(u8 *);
static void MachBikeTransition_FaceDirection(u8);
static void MachBikeTransition_TurnDirection(u8);
static void MachBikeTransition_TrySpeedUp(u8);
static void MachBikeTransition_TrySlowDown(u8);
static void MovePlayerOnAcroBike(u8, u16, u16);
static u8 CheckMovementInputAcroBike(u8 *, u16, u16);
static u8 AcroBikeHandleInputNormal(u8 *, u16, u16);
static u8 AcroBikeHandleInputTurning(u8 *, u16, u16);
static u8 AcroBikeHandleInputWheelieStanding(u8 *, u16, u16);
static u8 AcroBikeHandleInputBunnyHop(u8 *, u16, u16);
static u8 AcroBikeHandleInputWheelieMoving(u8 *, u16, u16);
static u8 AcroBikeHandleInputSidewaysJump(u8 *, u16, u16);
static u8 AcroBikeHandleInputTurnJump(u8 *, u16, u16);
static void AcroBikeTransition_FaceDirection(u8);
static void AcroBikeTransition_TurnDirection(u8);
static void AcroBikeTransition_Moving(u8);
static void AcroBikeTransition_NormalToWheelie(u8);
static void AcroBikeTransition_WheelieToNormal(u8);
static void AcroBikeTransition_WheelieIdle(u8);
static void AcroBikeTransition_WheelieHoppingStanding(u8);
static void AcroBikeTransition_WheelieHoppingMoving(u8);
static void AcroBikeTransition_SideJump(u8);
static void AcroBikeTransition_TurnJump(u8);
static void AcroBikeTransition_WheelieMoving(u8);
static void AcroBikeTransition_WheelieRisingMoving(u8);
static void AcroBikeTransition_WheelieLoweringMoving(u8);
static void AcroBike_TryHistoryUpdate(u16, u16);
static u8 AcroBike_GetJumpDirection(void);
static void Bike_UpdateDirTimerHistory(u8);
static void Bike_UpdateABStartSelectHistory(u8);
static u8 Bike_DPadToDirection(u16);
static u8 GetBikeCollision(u8);
static u8 GetBikeCollisionAt(struct ObjectEvent *, s16, s16, u8, u8);
static bool8 IsRunningDisallowedByMetatile(u8);
static void Bike_TryAdvanceCyclingRoadCollisions();
static u8 CanBikeFaceDirOnMetatile(u8, u8);
static bool8 WillPlayerCollideWithCollision(u8, u8);
static void Bike_SetBikeStill(void);
// const rom data
/*
A bike transition is a type of callback for the bike that actually
modifies the bicycle's direction or momentum or otherwise movement.
Alternatively, a bike may also have input handlers which process the
bike transition to call: the acro bike has input handlers while the mach
bike does not. This is because the Acro needs to know the button inputs
for its complex tricks and actions.
*/
static void (*const sMachBikeTransitions[])(u8) =
{
MachBikeTransition_FaceDirection, // Face vs Turn: Face has no anim while Turn does. Turn checks for collision because if you turn right as opposed to face right, if there is a wall there, turn will make a bonk sound effect while face will not.
MachBikeTransition_TurnDirection,
MachBikeTransition_TrySpeedUp,
MachBikeTransition_TrySlowDown,
};
// bikeFrameCounter is input which is represented by sMachBikeSpeeds in order
static void (*const sMachBikeSpeedCallbacks[])(u8) =
{
PlayerWalkNormal,
PlayerWalkFast,
PlayerWalkFaster,
};
static void (*const sAcroBikeTransitions[])(u8) =
{
AcroBikeTransition_FaceDirection,
AcroBikeTransition_TurnDirection,
AcroBikeTransition_Moving,
AcroBikeTransition_NormalToWheelie,
AcroBikeTransition_WheelieToNormal,
AcroBikeTransition_WheelieIdle,
AcroBikeTransition_WheelieHoppingStanding,
AcroBikeTransition_WheelieHoppingMoving,
AcroBikeTransition_SideJump,
AcroBikeTransition_TurnJump,
AcroBikeTransition_WheelieMoving,
AcroBikeTransition_WheelieRisingMoving,
AcroBikeTransition_WheelieLoweringMoving,
};
static u8 (*const sAcroBikeInputHandlers[])(u8 *, u16, u16) =
{
AcroBikeHandleInputNormal,
AcroBikeHandleInputTurning,
AcroBikeHandleInputWheelieStanding,
AcroBikeHandleInputBunnyHop,
AcroBikeHandleInputWheelieMoving,
AcroBikeHandleInputSidewaysJump,
AcroBikeHandleInputTurnJump,
};
// used with bikeFrameCounter from mach bike
static const u16 sMachBikeSpeeds[] = {PLAYER_SPEED_NORMAL, PLAYER_SPEED_FAST, PLAYER_SPEED_FASTEST};
// this is a list of timers to compare against later, terminated with 0. the only timer being compared against is 4 frames in this list.
static const u8 sAcroBikeJumpTimerList[] = {4, 0};
// this is a list of history inputs to do in order to do the check to retrieve a jump direction for acro bike. it seems to be an extensible list, so its possible that Game Freak may have intended for the Acro Bike to have more complex tricks at some point. The final list only has the acro jump.
static const struct BikeHistoryInputInfo sAcroBikeTricksList[] =
{
// the 0xF is a mask performed with each byte of the array in order to perform the check on only the last entry of the history list, otherwise the check wouldn't work as there can be 0xF0 as opposed to 0x0F.
{DIR_SOUTH, B_BUTTON, 0xF, 0xF, sAcroBikeJumpTimerList, sAcroBikeJumpTimerList, DIR_SOUTH},
{DIR_NORTH, B_BUTTON, 0xF, 0xF, sAcroBikeJumpTimerList, sAcroBikeJumpTimerList, DIR_NORTH},
{DIR_WEST, B_BUTTON, 0xF, 0xF, sAcroBikeJumpTimerList, sAcroBikeJumpTimerList, DIR_WEST},
{DIR_EAST, B_BUTTON, 0xF, 0xF, sAcroBikeJumpTimerList, sAcroBikeJumpTimerList, DIR_EAST},
};
// code
void MovePlayerOnBike(u8 direction, u16 newKeys, u16 heldKeys)
{
if (gPlayerAvatar.flags & PLAYER_AVATAR_FLAG_MACH_BIKE)
MovePlayerOnMachBike(direction, newKeys, heldKeys);
else
MovePlayerOnAcroBike(direction, newKeys, heldKeys);
}
static void MovePlayerOnMachBike(u8 direction, u16 newKeys, u16 heldKeys)
{
sMachBikeTransitions[GetMachBikeTransition(&direction)](direction);
}
// dirTraveling is a variable that is 0 when the player is standing still.
static u8 GetMachBikeTransition(u8 *dirTraveling)
{
// if the dir updated before this function, get the relevent new direction to check later.
u8 direction = GetPlayerMovementDirection();
// is the player standing still?
if (*dirTraveling == 0)
{
*dirTraveling = direction; // update the direction, since below we either faced a direction or we started moving.
if (gPlayerAvatar.bikeSpeed == PLAYER_SPEED_STANDING)
{
gPlayerAvatar.runningState = NOT_MOVING;
return MACH_TRANS_FACE_DIRECTION;
}
gPlayerAvatar.runningState = MOVING;
return MACH_TRANS_START_MOVING;
}
// we need to check if the last traveled direction changed from the new direction as well as ensuring that we dont update the state while the player is moving: see the else check.
if (*dirTraveling != direction && gPlayerAvatar.runningState != MOVING)
{
if (gPlayerAvatar.bikeSpeed != PLAYER_SPEED_STANDING)
{
*dirTraveling = direction; // implement the new direction
gPlayerAvatar.runningState = MOVING;
return MACH_TRANS_START_MOVING;
}
// if you didnt start moving but your dir was different, do a turn direction instead.
gPlayerAvatar.runningState = TURN_DIRECTION;
return MACH_TRANS_TURN_DIRECTION;
}
else // the player is either going in the current direction and hasnt changed or their state is currently moving.
{
gPlayerAvatar.runningState = MOVING;
return MACH_TRANS_KEEP_MOVING;
}
}
// the difference between face direction and turn direction is that one changes direction while the other does the animation of turning as well as changing direction.
static void MachBikeTransition_FaceDirection(u8 direction)
{
PlayerFaceDirection(direction);
Bike_SetBikeStill();
}
static void MachBikeTransition_TurnDirection(u8 direction)
{
struct ObjectEvent *playerObjEvent = &gObjectEvents[gPlayerAvatar.objectEventId];
if (CanBikeFaceDirOnMetatile(direction, playerObjEvent->currentMetatileBehavior))
{
PlayerTurnInPlace(direction);
Bike_SetBikeStill();
}
else
{
MachBikeTransition_FaceDirection(playerObjEvent->facingDirection);
}
}
static void MachBikeTransition_TrySpeedUp(u8 direction)
{
struct ObjectEvent *playerObjEvent = &gObjectEvents[gPlayerAvatar.objectEventId];
u8 collision;
if (CanBikeFaceDirOnMetatile(direction, playerObjEvent->currentMetatileBehavior) == FALSE)
{
// we cannot go forward, so either slow down or, if we are stopped, idle face direction.
if (gPlayerAvatar.bikeSpeed)
MachBikeTransition_TrySlowDown(playerObjEvent->movementDirection);
else
MachBikeTransition_FaceDirection(playerObjEvent->movementDirection);
}
else
{
collision = GetBikeCollision(direction);
if (collision > 0 && collision < COLLISION_VERTICAL_RAIL)
{
// we hit a solid object, but check to see if its a ledge and then jump.
if (collision == COLLISION_LEDGE_JUMP)
{
PlayerJumpLedge(direction);
}
else
{
// we hit a solid object that is not a ledge, so perform the collision.
Bike_SetBikeStill();
if (collision == COLLISION_OBJECT_EVENT && IsPlayerCollidingWithFarawayIslandMew(direction))
PlayerOnBikeCollideWithFarawayIslandMew(direction);
else if (collision < COLLISION_STOP_SURFING || collision > COLLISION_ROTATING_GATE)
PlayerOnBikeCollide(direction);
}
}
else
{
// we did not hit anything that can slow us down, so perform the advancement callback depending on the bikeFrameCounter and try to increase the mach bike's speed.
sMachBikeSpeedCallbacks[gPlayerAvatar.bikeFrameCounter](direction);
gPlayerAvatar.bikeSpeed = gPlayerAvatar.bikeFrameCounter + (gPlayerAvatar.bikeFrameCounter >> 1); // same as dividing by 2, but compiler is insistent on >> 1
if (gPlayerAvatar.bikeFrameCounter < 2) // do not go faster than the last element in the mach bike array
gPlayerAvatar.bikeFrameCounter++;
}
}
}
static void MachBikeTransition_TrySlowDown(u8 direction)
{
u8 collision;
if (gPlayerAvatar.bikeSpeed != PLAYER_SPEED_STANDING)
gPlayerAvatar.bikeFrameCounter = --gPlayerAvatar.bikeSpeed;
collision = GetBikeCollision(direction);
if (collision > 0 && collision < COLLISION_VERTICAL_RAIL)
{
if (collision == COLLISION_LEDGE_JUMP)
{
PlayerJumpLedge(direction);
}
else
{
Bike_SetBikeStill();
if (collision == COLLISION_OBJECT_EVENT && IsPlayerCollidingWithFarawayIslandMew(direction))
PlayerOnBikeCollideWithFarawayIslandMew(direction);
else if (collision < COLLISION_STOP_SURFING || collision > COLLISION_ROTATING_GATE)
PlayerOnBikeCollide(direction);
}
}
else
{
sMachBikeSpeedCallbacks[gPlayerAvatar.bikeFrameCounter](direction);
}
}
// the acro bike requires the input handler to be executed before the transition can.
static void MovePlayerOnAcroBike(u8 newDirection, u16 newKeys, u16 heldKeys)
{
sAcroBikeTransitions[CheckMovementInputAcroBike(&newDirection, newKeys, heldKeys)](newDirection);
}
static u8 CheckMovementInputAcroBike(u8 *newDirection, u16 newKeys, u16 heldKeys)
{
return sAcroBikeInputHandlers[gPlayerAvatar.acroBikeState](newDirection, newKeys, heldKeys);
}
static u8 AcroBikeHandleInputNormal(u8 *newDirection, u16 newKeys, u16 heldKeys)
{
u8 direction = GetPlayerMovementDirection();
gPlayerAvatar.bikeFrameCounter = 0;
if (*newDirection == DIR_NONE)
{
if (newKeys & B_BUTTON)
{
//We're standing still with the B button held.
//Do a wheelie.
*newDirection = direction;
gPlayerAvatar.runningState = NOT_MOVING;
gPlayerAvatar.acroBikeState = ACRO_STATE_WHEELIE_STANDING;
return ACRO_TRANS_NORMAL_TO_WHEELIE;
}
else
{
*newDirection = direction;
gPlayerAvatar.runningState = NOT_MOVING;
return ACRO_TRANS_FACE_DIRECTION;
}
}
if (*newDirection == direction && (heldKeys & B_BUTTON) && gPlayerAvatar.bikeSpeed == PLAYER_SPEED_STANDING)
{
gPlayerAvatar.bikeSpeed++;
gPlayerAvatar.acroBikeState = ACRO_STATE_WHEELIE_MOVING;
return ACRO_TRANS_WHEELIE_RISING_MOVING;
}
if (*newDirection != direction && gPlayerAvatar.runningState != MOVING)
{
gPlayerAvatar.acroBikeState = ACRO_STATE_TURNING;
gPlayerAvatar.newDirBackup = *newDirection;
gPlayerAvatar.runningState = NOT_MOVING;
return CheckMovementInputAcroBike(newDirection, newKeys, heldKeys);
}
gPlayerAvatar.runningState = MOVING;
return ACRO_TRANS_MOVING;
}
static u8 AcroBikeHandleInputTurning(u8 *newDirection, u16 newKeys, u16 heldKeys)
{
u8 direction;
*newDirection = gPlayerAvatar.newDirBackup;
gPlayerAvatar.bikeFrameCounter++;
// Wait 6 frames before actually changing direction
if (gPlayerAvatar.bikeFrameCounter > 6) // ... because it takes 6 frames to advance 1 tile.
{
gPlayerAvatar.runningState = TURN_DIRECTION;
gPlayerAvatar.acroBikeState = ACRO_STATE_NORMAL;
Bike_SetBikeStill();
return ACRO_TRANS_TURN_DIRECTION;
}
direction = GetPlayerMovementDirection();
if (*newDirection == AcroBike_GetJumpDirection())
{
Bike_SetBikeStill(); // Bike_SetBikeStill sets speed to standing, but the next line immediately overrides it. could have just reset acroBikeState to 0 here instead of wasting a jump.
gPlayerAvatar.bikeSpeed = PLAYER_SPEED_NORMAL;
if (*newDirection == GetOppositeDirection(direction))
{
// do a turn jump.
// no need to update runningState, didnt move.
gPlayerAvatar.acroBikeState = ACRO_STATE_TURN_JUMP;
return ACRO_TRANS_TURN_JUMP;
}
else
{
// do a sideways jump.
gPlayerAvatar.runningState = MOVING; // we need to move, set state to moving.
gPlayerAvatar.acroBikeState = ACRO_STATE_SIDE_JUMP;
return ACRO_TRANS_SIDE_JUMP;
}
}
*newDirection = direction;
return ACRO_TRANS_FACE_DIRECTION;
}
static u8 AcroBikeHandleInputWheelieStanding(u8 *newDirection, u16 newKeys, u16 heldKeys)
{
u8 direction;
struct ObjectEvent *playerObjEvent;
direction = GetPlayerMovementDirection();
playerObjEvent = &gObjectEvents[gPlayerAvatar.objectEventId];
gPlayerAvatar.runningState = NOT_MOVING;
if (heldKeys & B_BUTTON)
{
gPlayerAvatar.bikeFrameCounter++;
}
else
{
// B button was released.
gPlayerAvatar.bikeFrameCounter = 0;
if (!MetatileBehavior_IsBumpySlope(playerObjEvent->currentMetatileBehavior))
{
// Go back to normal on flat ground
*newDirection = direction;
gPlayerAvatar.acroBikeState = ACRO_STATE_NORMAL;
Bike_SetBikeStill();
return ACRO_TRANS_WHEELIE_TO_NORMAL;
}
}
if (gPlayerAvatar.bikeFrameCounter >= 40)
{
*newDirection = direction;
gPlayerAvatar.acroBikeState = ACRO_STATE_BUNNY_HOP;
Bike_SetBikeStill();
return ACRO_TRANS_WHEELIE_HOPPING_STANDING;
}
if (*newDirection == direction)
{
gPlayerAvatar.runningState = MOVING;
gPlayerAvatar.acroBikeState = ACRO_STATE_WHEELIE_MOVING;
Bike_SetBikeStill();
return ACRO_TRANS_WHEELIE_MOVING;
}
if (*newDirection == 0)
{
*newDirection = direction;
return ACRO_TRANS_WHEELIE_IDLE;
}
gPlayerAvatar.runningState = TURN_DIRECTION;
return ACRO_TRANS_WHEELIE_IDLE;
}
static u8 AcroBikeHandleInputBunnyHop(u8 *newDirection, u16 newKeys, u16 heldKeys)
{
u8 direction;
struct ObjectEvent *playerObjEvent;
direction = GetPlayerMovementDirection();
playerObjEvent = &gObjectEvents[gPlayerAvatar.objectEventId];
if (!(heldKeys & B_BUTTON))
{
// B button was released
Bike_SetBikeStill();
if (MetatileBehavior_IsBumpySlope(playerObjEvent->currentMetatileBehavior))
{
// even though B was released, dont undo the wheelie on the bumpy slope.
gPlayerAvatar.acroBikeState = ACRO_STATE_WHEELIE_STANDING;
return CheckMovementInputAcroBike(newDirection, newKeys, heldKeys);
}
else
{
// .. otherwise, go back to normal on flat ground
*newDirection = direction;
gPlayerAvatar.runningState = NOT_MOVING;
gPlayerAvatar.acroBikeState = ACRO_STATE_NORMAL;
return ACRO_TRANS_WHEELIE_TO_NORMAL;
}
}
// B Button is still held
if (*newDirection == DIR_NONE)
{
// we did not move, so keep hopping in place without moving.
*newDirection = direction;
gPlayerAvatar.runningState = NOT_MOVING;
return ACRO_TRANS_WHEELIE_HOPPING_STANDING;
}
if (*newDirection != direction && gPlayerAvatar.runningState != MOVING)
{
// we changed direction, so turn but do not move hop.
gPlayerAvatar.runningState = TURN_DIRECTION;
return ACRO_TRANS_WHEELIE_HOPPING_STANDING;
}
// otherwise, we started moving while hopping
gPlayerAvatar.runningState = MOVING;
return ACRO_TRANS_WHEELIE_HOPPING_MOVING;
}
static u8 AcroBikeHandleInputWheelieMoving(u8 *newDirection, u16 newKeys, u16 heldKeys)
{
u8 direction;
struct ObjectEvent *playerObjEvent;
direction = GetPlayerFacingDirection();
playerObjEvent = &gObjectEvents[gPlayerAvatar.objectEventId];
if (!(heldKeys & B_BUTTON))
{
// we were moving on a wheelie, but we let go while moving. reset bike still status
Bike_SetBikeStill();
if (!MetatileBehavior_IsBumpySlope(playerObjEvent->currentMetatileBehavior))
{
// we let go of B and arent on a bumpy slope, set state to normal because now we need to handle this
gPlayerAvatar.acroBikeState = ACRO_STATE_NORMAL;
if (*newDirection == DIR_NONE)
{
// we stopped moving but are turning, still try to lower the wheelie in place.
*newDirection = direction;
gPlayerAvatar.runningState = NOT_MOVING;
return ACRO_TRANS_WHEELIE_TO_NORMAL;
}
if (*newDirection != direction && gPlayerAvatar.runningState != MOVING)
{
// we did not turn while lowering wheelie, so do so without turning.
gPlayerAvatar.runningState = NOT_MOVING;
return ACRO_TRANS_WHEELIE_TO_NORMAL;
}
// if we are moving while lowering wheelie, put the acro into a lowering state while moving.
gPlayerAvatar.runningState = MOVING;
return ACRO_TRANS_WHEELIE_LOWERING_MOVING;
}
// please do not undo the wheelie on a bumpy slope
gPlayerAvatar.acroBikeState = ACRO_STATE_WHEELIE_STANDING;
return CheckMovementInputAcroBike(newDirection, newKeys, heldKeys);
}
// we are still holding B.
if (*newDirection == DIR_NONE)
{
// idle the wheelie in place because we're holding B without moving.
*newDirection = direction;
gPlayerAvatar.acroBikeState = ACRO_STATE_WHEELIE_STANDING;
gPlayerAvatar.runningState = NOT_MOVING;
Bike_SetBikeStill();
return ACRO_TRANS_WHEELIE_IDLE;
}
if (direction != *newDirection && gPlayerAvatar.runningState != MOVING)
{
gPlayerAvatar.runningState = NOT_MOVING;
return ACRO_TRANS_WHEELIE_IDLE;
}
gPlayerAvatar.runningState = MOVING;
return ACRO_TRANS_WHEELIE_MOVING;
}
static u8 AcroBikeHandleInputSidewaysJump(u8 *ptr, u16 newKeys, u16 heldKeys)
{
struct ObjectEvent *playerObjEvent = &gObjectEvents[gPlayerAvatar.objectEventId];
playerObjEvent->facingDirectionLocked = 0;
SetObjectEventDirection(playerObjEvent, playerObjEvent->facingDirection);
gPlayerAvatar.acroBikeState = ACRO_STATE_NORMAL;
return CheckMovementInputAcroBike(ptr, newKeys, heldKeys);
}
static u8 AcroBikeHandleInputTurnJump(u8 *ptr, u16 newKeys, u16 heldKeys)
{
gPlayerAvatar.acroBikeState = ACRO_STATE_NORMAL;
return CheckMovementInputAcroBike(ptr, newKeys, heldKeys);
}
static void AcroBikeTransition_FaceDirection(u8 direction)
{
PlayerFaceDirection(direction);
}
static void AcroBikeTransition_TurnDirection(u8 direction)
{
struct ObjectEvent *playerObjEvent = &gObjectEvents[gPlayerAvatar.objectEventId];
if (CanBikeFaceDirOnMetatile(direction, playerObjEvent->currentMetatileBehavior) == 0)
direction = playerObjEvent->movementDirection;
PlayerFaceDirection(direction);
}
static void AcroBikeTransition_Moving(u8 direction)
{
u8 collision;
struct ObjectEvent *playerObjEvent = &gObjectEvents[gPlayerAvatar.objectEventId];
if (CanBikeFaceDirOnMetatile(direction, playerObjEvent->currentMetatileBehavior) == 0)
{
AcroBikeTransition_FaceDirection(playerObjEvent->movementDirection);
return;
}
collision = GetBikeCollision(direction);
if (collision > 0 && collision < COLLISION_VERTICAL_RAIL)
{
if (collision == COLLISION_LEDGE_JUMP)
PlayerJumpLedge(direction);
else if (collision == COLLISION_OBJECT_EVENT && IsPlayerCollidingWithFarawayIslandMew(direction))
PlayerOnBikeCollideWithFarawayIslandMew(direction);
else if (collision < COLLISION_STOP_SURFING || collision > COLLISION_ROTATING_GATE)
PlayerOnBikeCollide(direction);
}
else
{
PlayerRideWaterCurrent(direction);
}
}
static void AcroBikeTransition_NormalToWheelie(u8 direction)
{
struct ObjectEvent *playerObjEvent = &gObjectEvents[gPlayerAvatar.objectEventId];
if (CanBikeFaceDirOnMetatile(direction, playerObjEvent->currentMetatileBehavior) == 0)
direction = playerObjEvent->movementDirection;
PlayerStartWheelie(direction);
}
static void AcroBikeTransition_WheelieToNormal(u8 direction)
{
struct ObjectEvent *playerObjEvent = &gObjectEvents[gPlayerAvatar.objectEventId];
if (CanBikeFaceDirOnMetatile(direction, playerObjEvent->currentMetatileBehavior) == 0)
direction = playerObjEvent->movementDirection;
PlayerEndWheelie(direction);
}
static void AcroBikeTransition_WheelieIdle(u8 direction)
{
struct ObjectEvent *playerObjEvent = &gObjectEvents[gPlayerAvatar.objectEventId];
if (CanBikeFaceDirOnMetatile(direction, playerObjEvent->currentMetatileBehavior) == 0)
direction = playerObjEvent->movementDirection;
PlayerIdleWheelie(direction);
}
static void AcroBikeTransition_WheelieHoppingStanding(u8 direction)
{
struct ObjectEvent *playerObjEvent = &gObjectEvents[gPlayerAvatar.objectEventId];
if (CanBikeFaceDirOnMetatile(direction, playerObjEvent->currentMetatileBehavior) == 0)
direction = playerObjEvent->movementDirection;
PlayerStandingHoppingWheelie(direction);
}
static void AcroBikeTransition_WheelieHoppingMoving(u8 direction)
{
u8 collision;
struct ObjectEvent *playerObjEvent = &gObjectEvents[gPlayerAvatar.objectEventId];
if (CanBikeFaceDirOnMetatile(direction, playerObjEvent->currentMetatileBehavior) == 0)
{
AcroBikeTransition_WheelieHoppingStanding(playerObjEvent->movementDirection);
return;
}
collision = GetBikeCollision(direction);
if (collision && collision != COLLISION_WHEELIE_HOP)
{
if (collision == COLLISION_LEDGE_JUMP)
{
PlayerLedgeHoppingWheelie(direction);
return;
}
if (collision >= COLLISION_STOP_SURFING && collision <= COLLISION_ROTATING_GATE)
{
return;
}
if (collision < COLLISION_VERTICAL_RAIL)
{
AcroBikeTransition_WheelieHoppingStanding(direction);
return;
}
}
PlayerMovingHoppingWheelie(direction);
}
static void AcroBikeTransition_SideJump(u8 direction)
{
u8 collision;
struct ObjectEvent *playerObjEvent;
collision = GetBikeCollision(direction);
if (collision)
{
if (collision == COLLISION_PUSHED_BOULDER)
return;
if (collision < COLLISION_ISOLATED_VERTICAL_RAIL)
{
AcroBikeTransition_TurnDirection(direction);
return;
}
if (WillPlayerCollideWithCollision(collision, direction) == FALSE)
{
AcroBikeTransition_TurnDirection(direction);
return;
}
}
playerObjEvent = &gObjectEvents[gPlayerAvatar.objectEventId];
PlaySE(SE_BIKE_HOP);
playerObjEvent->facingDirectionLocked = 1;
PlayerSetAnimId(GetJumpMovementAction(direction), COPY_MOVE_WALK);
}
static void AcroBikeTransition_TurnJump(u8 direction)
{
PlayerAcroTurnJump(direction);
}
static void AcroBikeTransition_WheelieMoving(u8 direction)
{
u8 collision;
struct ObjectEvent *playerObjEvent = &gObjectEvents[gPlayerAvatar.objectEventId];
if (CanBikeFaceDirOnMetatile(direction, playerObjEvent->currentMetatileBehavior) == 0)
{
PlayerIdleWheelie(playerObjEvent->movementDirection);
return;
}
collision = GetBikeCollision(direction);
if (collision > 0 && collision < COLLISION_VERTICAL_RAIL)
{
if (collision == COLLISION_LEDGE_JUMP)
{
PlayerLedgeHoppingWheelie(direction);
}
else if (collision == COLLISION_WHEELIE_HOP)
{
PlayerIdleWheelie(direction);
}
else if (collision < COLLISION_STOP_SURFING)
{
if (MetatileBehavior_IsBumpySlope(playerObjEvent->currentMetatileBehavior))
PlayerIdleWheelie(direction);
else
PlayerWheelieInPlace(direction); //hit wall?
}
return;
}
PlayerWheelieMove(direction);
gPlayerAvatar.runningState = MOVING;
}
static void AcroBikeTransition_WheelieRisingMoving(u8 direction)
{
u8 collision;
struct ObjectEvent *playerObjEvent = &gObjectEvents[gPlayerAvatar.objectEventId];
if (CanBikeFaceDirOnMetatile(direction, playerObjEvent->currentMetatileBehavior) == 0)
{
PlayerStartWheelie(playerObjEvent->movementDirection);
return;
}
collision = GetBikeCollision(direction);
if (collision > 0 && collision < COLLISION_VERTICAL_RAIL)
{
if (collision == COLLISION_LEDGE_JUMP)
{
PlayerLedgeHoppingWheelie(direction);
}
else if (collision == COLLISION_WHEELIE_HOP)
{
PlayerIdleWheelie(direction);
}
else if (collision < COLLISION_STOP_SURFING)
{
if (MetatileBehavior_IsBumpySlope(playerObjEvent->currentMetatileBehavior))
PlayerIdleWheelie(direction);
else
PlayerWheelieInPlace(direction); //hit wall?
}
return;
}
PlayerPopWheelieWhileMoving(direction);
gPlayerAvatar.runningState = MOVING;
}
static void AcroBikeTransition_WheelieLoweringMoving(u8 direction)
{
u8 collision;
struct ObjectEvent *playerObjEvent = &gObjectEvents[gPlayerAvatar.objectEventId];
if (CanBikeFaceDirOnMetatile(direction, playerObjEvent->currentMetatileBehavior) == 0)
{
PlayerEndWheelie(playerObjEvent->movementDirection);
return;
}
collision = GetBikeCollision(direction);
if (collision > 0 && collision < COLLISION_VERTICAL_RAIL)
{
if (collision == COLLISION_LEDGE_JUMP)
PlayerJumpLedge(direction);
else if (collision < COLLISION_STOP_SURFING || collision > COLLISION_ROTATING_GATE)
PlayerEndWheelie(direction);
return;
}
PlayerEndWheelieWhileMoving(direction);
}
void Bike_TryAcroBikeHistoryUpdate(u16 newKeys, u16 heldKeys)
{
if (gPlayerAvatar.flags & PLAYER_AVATAR_FLAG_ACRO_BIKE)
AcroBike_TryHistoryUpdate(newKeys, heldKeys);
}
static void AcroBike_TryHistoryUpdate(u16 newKeys, u16 heldKeys) // newKeys is unused
{
u8 direction = Bike_DPadToDirection(heldKeys);
if (direction == (gPlayerAvatar.directionHistory & 0xF))
{
// increment the timer for direction history since last input.
if (gPlayerAvatar.dirTimerHistory[0] < 0xFF)
gPlayerAvatar.dirTimerHistory[0]++;
}
else
{
Bike_UpdateDirTimerHistory(direction);
gPlayerAvatar.bikeSpeed = PLAYER_SPEED_STANDING;
}
direction = heldKeys & (A_BUTTON | B_BUTTON | SELECT_BUTTON | START_BUTTON); // directions is reused for some reason.
if (direction == (gPlayerAvatar.abStartSelectHistory & 0xF))
{
if (gPlayerAvatar.abStartSelectTimerHistory[0] < 0xFF)
gPlayerAvatar.abStartSelectTimerHistory[0]++;
}
else
{
Bike_UpdateABStartSelectHistory(direction);
gPlayerAvatar.bikeSpeed = PLAYER_SPEED_STANDING;
}
}
static bool8 HasPlayerInputTakenLongerThanList(const u8 *dirTimerList, const u8 *abStartSelectTimerList)
{
u8 i;
for (i = 0; dirTimerList[i] != 0; i++)
{
if (gPlayerAvatar.dirTimerHistory[i] > dirTimerList[i])
return FALSE;
}
for (i = 0; abStartSelectTimerList[i] != 0; i++)
{
if (gPlayerAvatar.abStartSelectTimerHistory[i] > abStartSelectTimerList[i])
return FALSE;
}
return TRUE;
}
static u8 AcroBike_GetJumpDirection(void)
{
u32 i;
for (i = 0; i < ARRAY_COUNT(sAcroBikeTricksList); i++)
{
const struct BikeHistoryInputInfo *historyInputInfo = &sAcroBikeTricksList[i];
u32 dirHistory = gPlayerAvatar.directionHistory;
u32 abStartSelectHistory = gPlayerAvatar.abStartSelectHistory;
dirHistory &= historyInputInfo->dirHistoryMask;
abStartSelectHistory &= historyInputInfo->abStartSelectHistoryMask;
if (dirHistory == historyInputInfo->dirHistoryMatch && abStartSelectHistory == historyInputInfo->abStartSelectHistoryMatch && HasPlayerInputTakenLongerThanList(historyInputInfo->dirTimerHistoryList, historyInputInfo->abStartSelectHistoryList))
return historyInputInfo->direction;
}
return 0;
}
static void Bike_UpdateDirTimerHistory(u8 dir)
{
u8 i;
gPlayerAvatar.directionHistory = (gPlayerAvatar.directionHistory << 4) | (dir & 0xF);
for (i = ARRAY_COUNT(gPlayerAvatar.dirTimerHistory) - 1; i != 0; i--)
gPlayerAvatar.dirTimerHistory[i] = gPlayerAvatar.dirTimerHistory[i - 1];
gPlayerAvatar.dirTimerHistory[0] = 1;
}
static void Bike_UpdateABStartSelectHistory(u8 input)
{
u8 i;
gPlayerAvatar.abStartSelectHistory = (gPlayerAvatar.abStartSelectHistory << 4) | (input & 0xF);
for (i = ARRAY_COUNT(gPlayerAvatar.abStartSelectTimerHistory) - 1; i != 0; i--)
gPlayerAvatar.abStartSelectTimerHistory[i] = gPlayerAvatar.abStartSelectTimerHistory[i - 1];
gPlayerAvatar.abStartSelectTimerHistory[0] = 1;
}
static u8 Bike_DPadToDirection(u16 heldKeys)
{
if (heldKeys & DPAD_UP)
return DIR_NORTH;
if (heldKeys & DPAD_DOWN)
return DIR_SOUTH;
if (heldKeys & DPAD_LEFT)
return DIR_WEST;
if (heldKeys & DPAD_RIGHT)
return DIR_EAST;
return DIR_NONE;
}
static u8 GetBikeCollision(u8 direction)
{
u8 metatileBehavior;
struct ObjectEvent *playerObjEvent = &gObjectEvents[gPlayerAvatar.objectEventId];
s16 x = playerObjEvent->currentCoords.x;
s16 y = playerObjEvent->currentCoords.y;
MoveCoords(direction, &x, &y);
metatileBehavior = MapGridGetMetatileBehaviorAt(x, y);
return GetBikeCollisionAt(playerObjEvent, x, y, direction, metatileBehavior);
}
static u8 GetBikeCollisionAt(struct ObjectEvent *objectEvent, s16 x, s16 y, u8 direction, u8 metatileBehavior)
{
u8 collision = CheckForObjectEventCollision(objectEvent, x, y, direction, metatileBehavior);
if (collision > COLLISION_OBJECT_EVENT)
return collision;
if (collision == COLLISION_NONE && IsRunningDisallowedByMetatile(metatileBehavior))
collision = COLLISION_IMPASSABLE;
if (collision)
Bike_TryAdvanceCyclingRoadCollisions();
return collision;
}
bool8 RS_IsRunningDisallowed(u8 tile)
{
if (IsRunningDisallowedByMetatile(tile) != FALSE || gMapHeader.mapType == MAP_TYPE_INDOOR)
return TRUE;
else
return FALSE;
}
static bool8 IsRunningDisallowedByMetatile(u8 tile)
{
if (MetatileBehavior_IsRunningDisallowed(tile))
return TRUE;
if (MetatileBehavior_IsFortreeBridge(tile) && (PlayerGetElevation() & 1) == 0)
return TRUE;
return FALSE;
}
static void Bike_TryAdvanceCyclingRoadCollisions(void)
{
if (gBikeCyclingChallenge != FALSE && gBikeCollisions < 100)
gBikeCollisions++;
}
static bool8 CanBikeFaceDirOnMetatile(u8 direction, u8 tile)
{
if (direction == DIR_EAST || direction == DIR_WEST)
{
// Bike cannot face east or west on a vertical rail
if (MetatileBehavior_IsIsolatedVerticalRail(tile)
|| MetatileBehavior_IsVerticalRail(tile))
return FALSE;
}
else
{
// Bike cannot face north or south on a horizontal rail
if (MetatileBehavior_IsIsolatedHorizontalRail(tile)
|| MetatileBehavior_IsHorizontalRail(tile))
return FALSE;
}
return TRUE;
}
static bool8 WillPlayerCollideWithCollision(u8 newTileCollision, u8 direction)
{
if (direction == DIR_NORTH || direction == DIR_SOUTH)
{
if (newTileCollision == COLLISION_ISOLATED_VERTICAL_RAIL || newTileCollision == COLLISION_VERTICAL_RAIL)
return FALSE;
}
else if (newTileCollision == COLLISION_ISOLATED_HORIZONTAL_RAIL || newTileCollision == COLLISION_HORIZONTAL_RAIL)
{
return FALSE;
}
return TRUE;
}
bool8 IsBikingDisallowedByPlayer(void)
{
s16 x, y;
u8 tileBehavior;
if (!(gPlayerAvatar.flags & (PLAYER_AVATAR_FLAG_SURFING | PLAYER_AVATAR_FLAG_UNDERWATER)))
{
PlayerGetDestCoords(&x, &y);
tileBehavior = MapGridGetMetatileBehaviorAt(x, y);
if (!IsRunningDisallowedByMetatile(tileBehavior))
return FALSE;
}
return TRUE;
}
bool8 IsPlayerNotUsingAcroBikeOnBumpySlope(void)
{
if (TestPlayerAvatarFlags(PLAYER_AVATAR_FLAG_ACRO_BIKE)
&& MetatileBehavior_IsBumpySlope(gObjectEvents[gPlayerAvatar.objectEventId].currentMetatileBehavior))
return FALSE;
else
return TRUE;
}
void GetOnOffBike(u8 transitionFlags)
{
gUnusedBikeCameraAheadPanback = FALSE;
if (gPlayerAvatar.flags & (PLAYER_AVATAR_FLAG_MACH_BIKE | PLAYER_AVATAR_FLAG_ACRO_BIKE))
{
SetPlayerAvatarTransitionFlags(PLAYER_AVATAR_FLAG_ON_FOOT);
Overworld_ClearSavedMusic();
Overworld_PlaySpecialMapMusic();
}
else
{
SetPlayerAvatarTransitionFlags(transitionFlags);
Overworld_SetSavedMusic(MUS_CYCLING);
Overworld_ChangeMusicTo(MUS_CYCLING);
}
}
void BikeClearState(int newDirHistory, int newAbStartHistory)
{
u8 i;
gPlayerAvatar.acroBikeState = ACRO_STATE_NORMAL;
gPlayerAvatar.newDirBackup = DIR_NONE;
gPlayerAvatar.bikeFrameCounter = 0;
gPlayerAvatar.bikeSpeed = PLAYER_SPEED_STANDING;
gPlayerAvatar.directionHistory = newDirHistory;