summaryrefslogtreecommitdiff
path: root/src/bike.c
blob: 58a4f38f4a51cfdf5caf46bef17ad2ef4d1c40cd (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
#include "global.h"
#include "bike.h"
#include "field_map_obj.h"
#include "field_player_avatar.h"
#include "fieldmap.h"
#include "flags.h"
#include "global.fieldmap.h"
#include "metatile_behavior.h"
#include "rom4.h"
#include "songs.h"
#include "sound.h"

extern u8 sub_80608A4(u8);

extern u8 gUnknown_02039250;
extern u8 gUnknown_02039251;
extern u8 gUnknown_0202E854;

static void MovePlayerOnMachBike(u8, u16, u16);
static u8 GetMachBikeTransition(u8 *);
static void MachBikeTransition_FaceDirection(u8);
static void MachBikeTransition_80E517C(u8);
static void MachBikeTransition_80E51C4(u8);
static void MachBikeTransition_80E5270(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 AcroBikeHandleInputState5(u8 *, u16, u16);
static u8 AcroBikeHandleInputState6(u8 *, u16, u16);
static void AcroBikeTransition_FaceDirection(u8);
static void AcroBikeTransition_80E5708(u8);
static void AcroBikeTransition_80E5744(u8);
static void AcroBikeTransition_NormalToWheelie(u8);
static void AcroBikeTransition_80E57F8(u8);
static void AcroBikeTransition_80E5834(u8);
static void AcroBikeTransition_80E5870(u8);
static void AcroBikeTransition_80E58AC(u8);
static void AcroBikeTransition_80E5920(u8);
static void AcroBikeTransition_80E5990(u8);
static void AcroBikeTransition_80E59A0(u8);
static void AcroBikeTransition_80E5A30(u8);
static void AcroBikeTransition_80E5AC0(u8);
static void sub_80E5B60(u16, u16);
static u8 sub_80E5C2C(void);
static void sub_80E5C7C(u8);
static void sub_80E5CB8(u8);
static u8 sub_80E5CF4(u16);
static u8 get_some_collision(u8);
static u8 sub_80E5DA0(struct MapObject *, s16, s16, u8, u8);
static bool8 IsRunningDisallowedByMetatile(u8);
static void sub_80E5E4C();
static u8 CanBikeFaceDirOnMetatile(u8, u8);
static bool8 sub_80E5EC0(u8, u8);
static void sub_80E6024(void);

static void (*const sMachBikeTransitions[])(u8) =
{
    MachBikeTransition_FaceDirection,
    MachBikeTransition_80E517C,
    MachBikeTransition_80E51C4,
    MachBikeTransition_80E5270,
};

static void (*const gUnknown_083DB5A4[])(u8) =
{
    PlayerGoSpeed0,
    sub_80593C4,
    sub_80593F4,
};

static void (*const sAcroBikeTransitions[])(u8) =
{
    AcroBikeTransition_FaceDirection,
    AcroBikeTransition_80E5708,
    AcroBikeTransition_80E5744,
    AcroBikeTransition_NormalToWheelie,
    AcroBikeTransition_80E57F8,
    AcroBikeTransition_80E5834,
    AcroBikeTransition_80E5870,
    AcroBikeTransition_80E58AC,
    AcroBikeTransition_80E5920,
    AcroBikeTransition_80E5990,
    AcroBikeTransition_80E59A0,
    AcroBikeTransition_80E5A30,
    AcroBikeTransition_80E5AC0,
};

static u8 (*const sAcroBikeInputHandlers[])(u8 *, u16, u16) =
{
    AcroBikeHandleInputNormal,
    AcroBikeHandleInputTurning,
    AcroBikeHandleInputWheelieStanding,
    AcroBikeHandleInputBunnyHop,
    AcroBikeHandleInputWheelieMoving,
    AcroBikeHandleInputState5,
    AcroBikeHandleInputState6,
};

const u16 gMachBikeSpeeds[] = {SPEED_NORMAL, SPEED_FAST, SPEED_FASTEST};
static const u8 Unknown_3DB606[] = {4, 0};

static const struct UnknownStruct1 gUnknown_083DB608[] =
{
    {1, 2, 15, 15, Unknown_3DB606, Unknown_3DB606, 1},
    {2, 2, 15, 15, Unknown_3DB606, Unknown_3DB606, 2},
    {3, 2, 15, 15, Unknown_3DB606, Unknown_3DB606, 3},
    {4, 2, 15, 15, Unknown_3DB606, Unknown_3DB606, 4},
};

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);
}

static u8 GetMachBikeTransition(u8 *ptr)
{
    u8 direction = player_get_direction_upper_nybble();

    if (*ptr == 0)
    {
        *ptr = direction;
        if (gPlayerAvatar.unkB == 0)
        {
            gPlayerAvatar.running2 = 0;
            return 0;
        }
        gPlayerAvatar.running2 = 2;
        return 3;
    }

    if (*ptr != direction && gPlayerAvatar.running2 != 2)
    {
        if (gPlayerAvatar.unkB != 0)
        {
            *ptr = direction;
            gPlayerAvatar.running2 = 2;
            return 3;
        }
        gPlayerAvatar.running2 = 1;
        return 1;
    }
    else
    {
        gPlayerAvatar.running2 = 2;
        return 2;
    }
}

static void MachBikeTransition_FaceDirection(u8 direction)
{
    PlayerFaceDirection(direction);
    sub_80E6024();
}

static void MachBikeTransition_80E517C(u8 direction)
{
    struct MapObject *playerMapObj = &gMapObjects[gPlayerAvatar.mapObjectId];

    if (CanBikeFaceDirOnMetatile(direction, playerMapObj->mapobj_unk_1E))
    {
        PlayerTurnInPlace(direction);
        sub_80E6024();
    }
    else
    {
        MachBikeTransition_FaceDirection(playerMapObj->mapobj_unk_18);
    }
}

static void MachBikeTransition_80E51C4(u8 direction)
{
    struct MapObject *playerMapObj = &gMapObjects[gPlayerAvatar.mapObjectId];
    u8 collision;

    if (CanBikeFaceDirOnMetatile(direction, playerMapObj->mapobj_unk_1E) == 0)
    {
        if (gPlayerAvatar.unkB)
            MachBikeTransition_80E5270(playerMapObj->placeholder18);
        else
            MachBikeTransition_FaceDirection(playerMapObj->placeholder18);
    }
    else
    {
        collision = get_some_collision(direction);
        if (collision > 0 && collision < 12)
        {
            if (collision == COLLISION_LEDGE_JUMP)
            {
                PlayerJumpLedge(direction);
            }
            else
            {
                sub_80E6024();
                if (collision < 5 || collision > 8)
                    PlayerOnBikeCollide(direction);
            }
        }
        else
        {
            gUnknown_083DB5A4[gPlayerAvatar.bikeFrameCounter](direction);
            gPlayerAvatar.unkB = 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_80E5270(u8 var)
{
    u8 collision;

    if (gPlayerAvatar.unkB != 0)
        gPlayerAvatar.bikeFrameCounter = --gPlayerAvatar.unkB;

    collision = get_some_collision(var);

    if (collision > 0 && collision < 12)
    {
        if (collision == COLLISION_LEDGE_JUMP)
        {
            PlayerJumpLedge(var);
        }
        else
        {
            sub_80E6024();
            if (collision < 5 || collision > 8)
                PlayerOnBikeCollide(var);
        }
    }
    else
    {
        gUnknown_083DB5A4[gPlayerAvatar.bikeFrameCounter](var);
    }
}

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 = player_get_direction_upper_nybble();

    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.running2 = 0;
            gPlayerAvatar.acroBikeState = ACRO_STATE_WHEELIE_STANDING;
            return ACRO_TRANS_NORMAL_TO_WHEELIE;
        }
        else
        {
            *newDirection = direction;
            gPlayerAvatar.running2 = 0;
            return ACRO_TRANS_FACE_DIRECTION;
        }
    }
    if (*newDirection == direction && (heldKeys & B_BUTTON) && gPlayerAvatar.unkB == 0)
    {
        gPlayerAvatar.unkB++;
        gPlayerAvatar.acroBikeState = ACRO_STATE_WHEELIE_MOVING;
        return 11;
    }
    if (*newDirection != direction && gPlayerAvatar.running2 != 2)
    {
        gPlayerAvatar.acroBikeState = ACRO_STATE_TURNING;
        gPlayerAvatar.unk9 = *newDirection;
        gPlayerAvatar.running2 = 0;
        return CheckMovementInputAcroBike(newDirection, newKeys, heldKeys);
    }
    gPlayerAvatar.running2 = 2;
    return 2;
}

static u8 AcroBikeHandleInputTurning(u8 *newDirection, u16 newKeys, u16 heldKeys)
{
    u8 direction;

    *newDirection = gPlayerAvatar.unk9;
    gPlayerAvatar.bikeFrameCounter++;

    //Wait 6 frames before actually changing direction
    if (gPlayerAvatar.bikeFrameCounter > 6)
    {
        gPlayerAvatar.running2 = 1;
        gPlayerAvatar.acroBikeState = ACRO_STATE_NORMAL;
        sub_80E6024();
        return 1;
    }
    direction = player_get_direction_upper_nybble();
    if (*newDirection == sub_80E5C2C())
    {
        sub_80E6024();
        gPlayerAvatar.unkB = 1;
        if (*newDirection == GetOppositeDirection(direction))
        {
            gPlayerAvatar.acroBikeState = ACRO_STATE_6;
            return 9;
        }
        else
        {
            gPlayerAvatar.running2 = 2;
            gPlayerAvatar.acroBikeState = ACRO_STATE_5;
            return 8;
        }
    }
    *newDirection = direction;
    return 0;
}

static u8 AcroBikeHandleInputWheelieStanding(u8 *ptr, u16 newKeys, u16 heldKeys)
{
    u8 direction;
    struct MapObject *playerMapObj;

    direction = player_get_direction_upper_nybble();
    playerMapObj = &gMapObjects[gPlayerAvatar.mapObjectId];
    gPlayerAvatar.running2 = 0;

    if (heldKeys & B_BUTTON)
        gPlayerAvatar.bikeFrameCounter++;
    else
    {
        //B button was released.
        gPlayerAvatar.bikeFrameCounter = 0;
        if (!MetatileBehavior_IsBumpySlope(playerMapObj->mapobj_unk_1E))
        {
            //Go back to normal on flat ground
            *ptr = direction;
            gPlayerAvatar.acroBikeState = ACRO_STATE_NORMAL;
            sub_80E6024();
            return 4;
        }
    }
    if (gPlayerAvatar.bikeFrameCounter >= 40)
    {
        *ptr = direction;
        gPlayerAvatar.acroBikeState = ACRO_STATE_BUNNY_HOP;
        sub_80E6024();
        return 6;
    }
    if (*ptr == direction)
    {
        gPlayerAvatar.running2 = 2;
        gPlayerAvatar.acroBikeState = ACRO_STATE_WHEELIE_MOVING;
        sub_80E6024();
        return 10;
    }
    if (*ptr == 0)
    {
        *ptr = direction;
        return 5;
    }
    gPlayerAvatar.running2 = 1;
    return 5;
}

static u8 AcroBikeHandleInputBunnyHop(u8 *ptr, u16 newKeys, u16 heldKeys)
{
    u8 direction;
    struct MapObject *playerMapObj;

    direction = player_get_direction_upper_nybble();
    playerMapObj = &gMapObjects[gPlayerAvatar.mapObjectId];
    if (!(heldKeys & B_BUTTON))
    {
        //B button was released
        sub_80E6024();
        if (MetatileBehavior_IsBumpySlope(playerMapObj->mapobj_unk_1E))
        {
            //Do a standing wheelie on a bumpy slope
            gPlayerAvatar.acroBikeState = ACRO_STATE_WHEELIE_STANDING;
            return CheckMovementInputAcroBike(ptr, newKeys, heldKeys);
        }
        else
        {
            //Go back to normal on flat ground
            *ptr = direction;
            gPlayerAvatar.running2 = 0;
            gPlayerAvatar.acroBikeState = ACRO_STATE_NORMAL;
            return 4;
        }
    }

    //B Button is still held

    if (*ptr == DIR_NONE)
    {
        *ptr = direction;
        gPlayerAvatar.running2 = 0;
        return 6;
    }
    if (*ptr != direction && gPlayerAvatar.running2 != 2)
    {
        gPlayerAvatar.running2 = 1;
        return 6;
    }
    gPlayerAvatar.running2 = 2;
    return 7;
}

static u8 AcroBikeHandleInputWheelieMoving(u8 *ptr, u16 newKeys, u16 heldKeys)
{
    u8 direction;
    struct MapObject *playerMapObj;

    direction = player_get_direction_lower_nybble();
    playerMapObj = &gMapObjects[gPlayerAvatar.mapObjectId];
    if (!(heldKeys & B_BUTTON))
    {
        sub_80E6024();
        if (!MetatileBehavior_IsBumpySlope(playerMapObj->mapobj_unk_1E))
        {
            gPlayerAvatar.acroBikeState = ACRO_STATE_NORMAL;
            if (*ptr == 0)
            {
                *ptr = direction;
                gPlayerAvatar.running2 = 0;
                return 4;
            }
            if (*ptr != direction && gPlayerAvatar.running2 != 2)
            {
                gPlayerAvatar.running2 = 0;
                return 4;
            }
            gPlayerAvatar.running2 = 2;
            return 12;
        }
        gPlayerAvatar.acroBikeState = ACRO_STATE_WHEELIE_STANDING;
        return CheckMovementInputAcroBike(ptr, newKeys, heldKeys);
    }
    if (*ptr == 0)
    {
        *ptr = direction;
        gPlayerAvatar.acroBikeState = ACRO_STATE_WHEELIE_STANDING;
        gPlayerAvatar.running2 = 0;
        sub_80E6024();
        return 5;
    }
    if (direction != *ptr && gPlayerAvatar.running2 != 2)
    {
        gPlayerAvatar.running2 = 0;
        return 5;
    }
    gPlayerAvatar.running2 = 2;
    return 10;
}

static u8 AcroBikeHandleInputState5(u8 *ptr, u16 newKeys, u16 heldKeys)
{
    struct MapObject *playerMapObj = &gMapObjects[gPlayerAvatar.mapObjectId];

    playerMapObj->mapobj_bit_9 = 0;
    FieldObjectSetDirection(playerMapObj, playerMapObj->mapobj_unk_18);
    gPlayerAvatar.acroBikeState = ACRO_STATE_NORMAL;
    return CheckMovementInputAcroBike(ptr, newKeys, heldKeys);
}

static u8 AcroBikeHandleInputState6(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_80E5708(u8 direction)
{
    struct MapObject *playerMapObj = &gMapObjects[gPlayerAvatar.mapObjectId];

    if (CanBikeFaceDirOnMetatile(direction, playerMapObj->mapobj_unk_1E) == 0)
        direction = playerMapObj->placeholder18;
    PlayerFaceDirection(direction);
}

static void AcroBikeTransition_80E5744(u8 direction)
{
    u8 collision;
    struct MapObject *playerMapObj = &gMapObjects[gPlayerAvatar.mapObjectId];

    if (CanBikeFaceDirOnMetatile(direction, playerMapObj->mapobj_unk_1E) == 0)
    {
        AcroBikeTransition_FaceDirection(playerMapObj->placeholder18);
        return;
    }
    collision = get_some_collision(direction);
    if (collision > 0 && collision < 12)
    {
        if (collision == COLLISION_LEDGE_JUMP)
            PlayerJumpLedge(direction);
        else if (collision < 5 || collision > 8)
            PlayerOnBikeCollide(direction);
    }
    else
    {
        npc_use_some_d2s(direction);
    }
}

static void AcroBikeTransition_NormalToWheelie(u8 direction)
{
    struct MapObject *playerMapObj = &gMapObjects[gPlayerAvatar.mapObjectId];

    if (CanBikeFaceDirOnMetatile(direction, playerMapObj->mapobj_unk_1E) == 0)
        direction = playerMapObj->placeholder18;
    PlayerStartWheelie(direction);
}

static void AcroBikeTransition_80E57F8(u8 direction)
{
    struct MapObject *playerMapObj = &gMapObjects[gPlayerAvatar.mapObjectId];

    if (CanBikeFaceDirOnMetatile(direction, playerMapObj->mapobj_unk_1E) == 0)
        direction = playerMapObj->placeholder18;
    sub_8059534(direction);
}

static void AcroBikeTransition_80E5834(u8 direction)
{
    struct MapObject *playerMapObj = &gMapObjects[gPlayerAvatar.mapObjectId];

    if (CanBikeFaceDirOnMetatile(direction, playerMapObj->mapobj_unk_1E) == 0)
        direction = playerMapObj->placeholder18;
    sub_8059504(direction);
}

static void AcroBikeTransition_80E5870(u8 direction)
{
    struct MapObject *playerMapObj = &gMapObjects[gPlayerAvatar.mapObjectId];

    if (CanBikeFaceDirOnMetatile(direction, playerMapObj->mapobj_unk_1E) == 0)
        direction = playerMapObj->placeholder18;
    sub_805954C(direction);
}

static void AcroBikeTransition_80E58AC(u8 direction)
{
    u8 var;
    struct MapObject *playerMapObj = &gMapObjects[gPlayerAvatar.mapObjectId];

    if (CanBikeFaceDirOnMetatile(direction, playerMapObj->mapobj_unk_1E) == 0)
    {
        AcroBikeTransition_80E5870(playerMapObj->placeholder18);
        return;
    }
    var = get_some_collision(direction);
    //TODO: Try to get rid of this goto
    if (var == 0 || var == 9)
    {
        goto derp;
    }
    else if (var == 6)
    {
        sub_8059594(direction);
    }
    else if (var < 5 || var > 8)
    {
        if (var <= 11)
        {
            AcroBikeTransition_80E5870(direction);
        }
        else
        {
          derp:
            sub_8059570(direction);
        }
    }
}

static void AcroBikeTransition_80E5920(u8 direction)
{
    u8 var;
    struct MapObject *playerMapObj;

    var = get_some_collision(direction);
    if (var != 0)
    {
        if (var == 7)
            return;
        if (var < 10)
        {
            AcroBikeTransition_80E5708(direction);
            return;
        }
        if (sub_80E5EC0(var, direction) == 0)
        {
            AcroBikeTransition_80E5708(direction);
            return;
        }
    }
    playerMapObj = &gMapObjects[gPlayerAvatar.mapObjectId];
    PlaySE(SE_JITE_PYOKO);
    playerMapObj->mapobj_bit_9 = 1;
    PlayerSetAnimId(sub_80608A4(direction), 2);
}

static void AcroBikeTransition_80E5990(u8 direction)
{
    sub_80595B8(direction);
}

static void AcroBikeTransition_80E59A0(u8 direction)
{
    u8 var;
    struct MapObject *playerMapObj = &gMapObjects[gPlayerAvatar.mapObjectId];

    if (CanBikeFaceDirOnMetatile(direction, playerMapObj->mapobj_unk_1E) == 0)
    {
        sub_8059504(playerMapObj->placeholder18);
        return;
    }
    var = get_some_collision(direction);
    if (var > 0 && var < 12)
    {
        if (var == 6)
        {
            sub_8059594(direction);
        }
        else if (var == 9)
        {
            sub_8059504(direction);
        }
        else if (var <= 4)
        {
            if (MetatileBehavior_IsBumpySlope(playerMapObj->mapobj_unk_1E))
                sub_8059504(direction);
            else
                sub_80595DC(direction);  //hit wall?
        }
        return;
    }
    sub_8059618(direction);
    gPlayerAvatar.running2 = 2;
}

static void AcroBikeTransition_80E5A30(u8 direction)
{
    u8 var;
    struct MapObject *playerMapObj = &gMapObjects[gPlayerAvatar.mapObjectId];

    if (CanBikeFaceDirOnMetatile(direction, playerMapObj->mapobj_unk_1E) == 0)
    {
        PlayerStartWheelie(playerMapObj->placeholder18);
        return;
    }
    var = get_some_collision(direction);
    if (var > 0 && var < 12)
    {
        if (var == 6)
        {
            sub_8059594(direction);
        }
        else if (var == 9)
        {
            sub_8059504(direction);
        }
        else if (var <= 4)
        {
            if (MetatileBehavior_IsBumpySlope(playerMapObj->mapobj_unk_1E))
                sub_8059504(direction);
            else
                sub_80595DC(direction);  //hit wall?
        }
        return;
    }
    sub_8059600(direction);
    gPlayerAvatar.running2 = 2;
}

static void AcroBikeTransition_80E5AC0(u8 direction)
{
    u8 var;
    struct MapObject *playerMapObj = &gMapObjects[gPlayerAvatar.mapObjectId];

    if (CanBikeFaceDirOnMetatile(direction, playerMapObj->mapobj_unk_1E) == 0)
    {
        sub_8059534(playerMapObj->placeholder18);
        return;
    }
    var = get_some_collision(direction);
    if (var > 0 && var < 12)
    {
        if (var == 6)
            PlayerJumpLedge(direction);
        else if (var < 5 || var > 8)
            sub_8059534(direction);
        return;
    }
    sub_8059630(direction);
}

void sub_80E5B38(u16 a, u16 b)
{
    if (gPlayerAvatar.flags & PLAYER_AVATAR_FLAG_ACRO_BIKE)
        sub_80E5B60(a, b);
}

static void sub_80E5B60(u16 unused, u16 b)
{
    u8 var;

    var = sub_80E5CF4(b);
    if (var == (gPlayerAvatar.unkC & 0xF))
    {
        if (gPlayerAvatar.unk14[0] < 0xFF)
            gPlayerAvatar.unk14[0]++;
    }
    else
    {
        sub_80E5C7C(var);
        gPlayerAvatar.unkB = 0;
    }

    var = b & 0xF;
    if (var == (gPlayerAvatar.unk10 & 0xF))
    {
        if (gPlayerAvatar.unk1C[0] < 0xFF)
            gPlayerAvatar.unk1C[0]++;
    }
    else
    {
        sub_80E5CB8(var);
        gPlayerAvatar.unkB = 0;
    }
}

static bool8 sub_80E5BC8(const u8 *a, const u8 *b)
{
    u8 i;

    for (i = 0; a[i] != 0; i++)
    {
        if (gPlayerAvatar.unk14[i] > a[i])
            return FALSE;
    }
    for (i = 0; b[i] != 0; i++)
    {
        if (gPlayerAvatar.unk1C[i] > b[i])
            return FALSE;
    }
    return TRUE;
}

static u8 sub_80E5C2C(void)
{
    u32 i;

    for (i = 0; i < 4; i++)
    {
        const struct UnknownStruct1 *s = &gUnknown_083DB608[i];
        u32 r1 = gPlayerAvatar.unkC;
        u32 r2 = gPlayerAvatar.unk10;

        r1 &= s->unk8;
        r2 &= s->unkC;
        if (r1 == s->unk0 && r2 == s->unk4 && sub_80E5BC8(s->unk10, s->unk14))
            return s->unk18;
    }
    return 0;
}

static void sub_80E5C7C(u8 a)
{
    u8 i;

    gPlayerAvatar.unkC = (gPlayerAvatar.unkC << 4) | (a & 0xF);

    for (i = 7; i != 0; i--)
        gPlayerAvatar.unk14[i] = gPlayerAvatar.unk14[i - 1];
    gPlayerAvatar.unk14[0] = 1;
}

static void sub_80E5CB8(u8 a)
{
    u8 i;

    gPlayerAvatar.unk10 = (gPlayerAvatar.unk10 << 4) | (a & 0xF);

    for (i = 7; i != 0; i--)
        gPlayerAvatar.unk1C[i] = gPlayerAvatar.unk1C[i - 1];
    gPlayerAvatar.unk1C[0] = 1;
}

static u8 sub_80E5CF4(u16 a)
{
    if (a & 0x40)
        return 2;
    if (a & 0x80)
        return 1;
    if (a & 0x20)
        return 3;
    if (a & 0x10)
        return 4;
    return 0;
}

static u8 get_some_collision(u8 direction)
{
    s16 x;
    s16 y;
    u8 metatitleBehavior;
    struct MapObject *playerMapObj = &gMapObjects[gPlayerAvatar.mapObjectId];

    x = playerMapObj->coords2.x;
    y = playerMapObj->coords2.y;
    MoveCoords(direction, &x, &y);
    metatitleBehavior = MapGridGetMetatileBehaviorAt(x, y);
    return sub_80E5DA0(playerMapObj, x, y, direction, metatitleBehavior);
}

static u8 sub_80E5DA0(struct MapObject *mapObject, s16 x, s16 y, u8 direction, u8 metatitleBehavior)
{
    u8 collision = CheckForFieldObjectCollision(mapObject, x, y, direction, metatitleBehavior);

    if (collision > 4)
        return collision;

    if (collision == 0 && IsRunningDisallowedByMetatile(metatitleBehavior))
        collision = 2;

    if (collision)
        sub_80E5E4C();

    return collision;
}

bool8 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) && (PlayerGetZCoord() & 1) == 0)
        return TRUE;
    return FALSE;
}

static void sub_80E5E4C(void)
{
    if (gUnknown_02039250 != 0 && gUnknown_02039251 < 100)
        gUnknown_02039251++;
}

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 sub_80E5EC0(u8 var1, u8 direction)
{
    if (direction == DIR_NORTH || direction == DIR_SOUTH)
    {
        if (var1 == 10 || var1 == 12)
            return FALSE;
    }
    else if (var1 == 11 || var1 == 13)
    {
        return FALSE;
    }

    return TRUE;
}

bool8 IsBikingDisallowedByPlayer(void)
{
    s16 x, y;
    u8 tileBehavior;

    if (!(gPlayerAvatar.flags & (PLAYER_AVATAR_FLAG_SURFING | PLAYER_AVATAR_FLAG_4)))
    {
        PlayerGetDestCoords(&x, &y);
        tileBehavior = MapGridGetMetatileBehaviorAt(x, y);
        if (!IsRunningDisallowedByMetatile(tileBehavior))
            return FALSE;
    }
    return TRUE;
}

bool8 player_should_look_direction_be_enforced_upon_movement(void)
{
    if (TestPlayerAvatarFlags(PLAYER_AVATAR_FLAG_ACRO_BIKE) != FALSE && MetatileBehavior_IsBumpySlope(gMapObjects[gPlayerAvatar.mapObjectId].mapobj_unk_1E) != FALSE)
        return FALSE;
    else
        return TRUE;
}

void GetOnOffBike(u8 var)
{
    gUnknown_0202E854 = 0;

    if (gPlayerAvatar.flags & (PLAYER_AVATAR_FLAG_MACH_BIKE | PLAYER_AVATAR_FLAG_ACRO_BIKE))
    {
        SetPlayerAvatarTransitionFlags(PLAYER_AVATAR_FLAG_ON_FOOT);
        sav1_reset_battle_music_maybe();
        sub_8053E90();
    }
    else
    {
        SetPlayerAvatarTransitionFlags(var);
        sav1_set_battle_music_maybe(BGM_CYCLING);
        sub_8053FB0(BGM_CYCLING);
    }
}

void BikeClearState(int var1, int var2)
{
    u8 i;

    gPlayerAvatar.acroBikeState = ACRO_STATE_NORMAL;
    gPlayerAvatar.unk9 = 0;
    gPlayerAvatar.bikeFrameCounter = 0;
    gPlayerAvatar.unkB = 0;
    gPlayerAvatar.unkC = var1;
    gPlayerAvatar.unk10 = var2;

    for (i = 0; i < 8; i++)
        gPlayerAvatar.unk14[i] = 0;

    for (i = 0; i < 8; i++)
        gPlayerAvatar.unk1C[i] = 0;
}

void sub_80E6010(u8 var)
{
    gPlayerAvatar.bikeFrameCounter = var;
    gPlayerAvatar.unkB = gPlayerAvatar.bikeFrameCounter + (gPlayerAvatar.bikeFrameCounter >> 1); // lazy way of multiplying by 1.5.
}

static void sub_80E6024(void)
{
    gPlayerAvatar.bikeFrameCounter = 0;
    gPlayerAvatar.unkB = 0;
}

s16 GetPlayerSpeed(void)
{
    // because the player pressed a direction, it won't ever return a speed of 0 since this function returns the player's current speed.
    s16 machSpeeds[3];

    memcpy(machSpeeds, gMachBikeSpeeds, sizeof(machSpeeds));

    if (gPlayerAvatar.flags & PLAYER_AVATAR_FLAG_MACH_BIKE)
        return machSpeeds[gPlayerAvatar.bikeFrameCounter];
    else if (gPlayerAvatar.flags & PLAYER_AVATAR_FLAG_ACRO_BIKE)
        return SPEED_FASTER;
    else if (gPlayerAvatar.flags & (PLAYER_AVATAR_FLAG_SURFING | PLAYER_AVATAR_FLAG_DASH))
        return SPEED_FAST;
    else
        return SPEED_NORMAL;
}

void sub_80E6084(void)
{
    s16 x, y;
    u8 tileBehavior;

    if (gPlayerAvatar.flags & PLAYER_AVATAR_FLAG_ACRO_BIKE)
    {
        PlayerGetDestCoords(&x, &y);
        tileBehavior = MapGridGetMetatileBehaviorAt(x, y);
        if (MetatileBehavior_IsBumpySlope(tileBehavior))
        {
            gPlayerAvatar.acroBikeState = ACRO_STATE_WHEELIE_STANDING;
            sub_8059C94(player_get_direction_upper_nybble());
        }
    }
}