summaryrefslogtreecommitdiff
path: root/src/object/minecart.c
blob: 64fb239d151cb3d00dcaeda799fbc18d2ac7c78f (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
/**
 * @file minecart.c
 * @ingroup Objects
 *
 * @brief Minecart object
 */
#include "functions.h"
#include "hitbox.h"
#include "object.h"

typedef struct {
    Entity base;
    u8 filler[28];
    MinecartData* minecartData;
} MinecartEntity;

extern void sub_08017744(Entity*);

u32 sub_08091DDC(MinecartEntity* this);
void Minecart_Init(MinecartEntity* this);
void Minecart_Action1(MinecartEntity* this);
void Minecart_Action2(MinecartEntity* this);
void Minecart_Action3(MinecartEntity* this);
void Minecart_Action4(MinecartEntity* this);
void Minecart_Action5(MinecartEntity* this);
void Minecart_Action6(MinecartEntity* this);
void Minecart_Action7(MinecartEntity* this);

extern const u16* const gUnk_081223D8[];

void Minecart(Entity* this) {
    static void (*const Minecart_Actions[])(MinecartEntity*) = {
        Minecart_Init,    Minecart_Action1, Minecart_Action2, Minecart_Action3,
        Minecart_Action4, Minecart_Action5, Minecart_Action6, Minecart_Action7,
    };
    Minecart_Actions[this->action]((MinecartEntity*)this);
    this->contactFlags = 0;
}

void Minecart_Init(MinecartEntity* this) {
    MinecartData* unk = &gRoomTransition.minecart_data[super->timer];

    this->minecartData = unk;
    if ((gRoomControls.room != unk->room) || (gPlayerState.flags & PL_IN_MINECART) != 0) {
        DeleteThisEntity();
    }
    super->x.HALF.HI = gRoomControls.origin_x + ((unk->tilePos & 0x3f) << 4) + 8;
    super->y.HALF.HI = gRoomControls.origin_y + ((unk->tilePos & 0xfc << 4) >> 2) + 8;
    super->animationState = unk->animationState;
    super->type2 = unk->field_0x6;
    super->action = 1;
    super->hitbox = (Hitbox*)&gUnk_080FD310;
    COLLISION_ON(super);
    super->hitType = 1;
    super->collisionFlags = 0x47;
    super->hurtType = 0x44;
    super->flags2 = 0x80;
    super->direction = DirectionFromAnimationState(super->animationState);
    super->speed = 0x700;
    super->spritePriority.b1 = 3;
    InitAnimationForceUpdate(super, super->type2 + 4 + super->animationState);
    SetTile(0x4022, COORD_TO_TILE(super), super->collisionLayer);
}

void Minecart_Action1(MinecartEntity* this) {
    if ((super->contactFlags & 0x7f) == 0x1d) {
        super->zVelocity = Q_16_16(2.625);
        super->action = 7;
        InitAnimationForceUpdate(super, super->type2 + 4 + super->animationState);
        SoundReq(SFX_13B);
    } else {
        if (sub_0800445C(super) != 0) {
            if (!((gPlayerState.flags & (PL_MINISH | PL_ROLLING)) || gPlayerState.field_0x1c ||
                  gPlayerState.heldObject || gPlayerState.jump_status)) {
                super->timer++;
            } else {
                super->timer = 0;
            }
        } else {
            super->timer = 0;
        }
        if (super->type2 == 0) {
            if (super->timer > 8) {
                super->action++;
                gPlayerState.jump_status = 0x81;
                gPlayerState.flags |= PL_ENTER_MINECART;
                gPlayerEntity.base.zVelocity = Q_16_16(2.0);
                gPlayerEntity.base.speed = 0x100;
                gPlayerEntity.base.flags &= ~PL_MINISH;
                ResetActiveItems();
                DeleteClones();
                SoundReq(SFX_PLY_JUMP);
            }
        } else {
            super->timer = 0;
        }
    }
}

void Minecart_Action2(MinecartEntity* this) {
    if (EntityInRectRadius(super, &gPlayerEntity.base, 2, 2) != 0) {
        gPlayerEntity.base.x.HALF.HI = super->x.HALF.HI;
        gPlayerEntity.base.y.HALF.HI = super->y.HALF.HI;
        if (gPlayerEntity.base.z.HALF.HI > -0x10) {
            if (gPlayerEntity.base.zVelocity >= 0) {
                return;
            }
            gPlayerEntity.base.animationState = super->animationState << 1;
            gPlayerState.flags = (gPlayerState.flags ^ PL_ENTER_MINECART) | PL_IN_MINECART;
            super->action++;
            super->subtimer = 1;
            super->flags |= ENT_PERSIST;
            super->hitType = 0x97;
            super->collisionFlags = (gPlayerEntity.base.collisionFlags + 1) | 0x20;
            super->flags2 = gPlayerEntity.base.flags2;
            super->hurtType = 0x18;
            super->damage = 8;
            sub_0801766C(super);
            RestorePrevTileEntity(COORD_TO_TILE(super), super->collisionLayer);
            SoundReq(SFX_137);
        }
    } else {
        gPlayerEntity.base.direction = GetFacingDirection(&gPlayerEntity.base, super);
    }
    if (gPlayerEntity.base.zVelocity < 0) {
        gPlayerEntity.base.spritePriority.b0 = super->spritePriority.b0 - 1;
    }
}

void Minecart_Action3(MinecartEntity* this) {
    static const s8 gUnk_081223C8[] = { 0, -7, 7, 0, 0, 7, -7, 0 };

    u32 iVar2;
    u32 uVar3;

    gRoomControls.scrollSpeed = 7;
    if ((gPlayerState.flags & PL_IN_MINECART) == 0) {
        super->action = 1;
        return;
    }

    if ((gPlayerEntity.base.frame & 0xf) == 0) {
        COLLISION_OFF(super);
        CopyPosition(super, &gPlayerEntity.base);
        if ((gPlayerEntity.base.frame & 0xf0) == 0x10) {
            super->spriteOffsetY = 1;
        } else {
            super->spriteOffsetY = 0;
        }
    } else {
        COLLISION_ON(super);
        gPlayerEntity.base.speed = 0;
        LinearMoveUpdate(super);
        CopyPosition(super, &gPlayerEntity.base);
        gPlayerEntity.base.spritePriority.b0 = super->spritePriority.b0 - 1;
        if (!sub_08091DDC(this)) {
            if ((gRoomTransition.frameCount & 0xf) == 0) {
                SoundReq(SFX_138);
            }

            if (super->subtimer-- == 0) {
                SoundReq(SFX_PLY_VO7);
                super->subtimer = 60;
            }

            uVar3 = GetRelativeCollisionTile(super, gUnk_081223C8[super->animationState * 2],
                                             gUnk_081223C8[super->animationState * 2 + 1]);
            iVar2 = sub_08007DD6(uVar3, gUnk_081223D8[super->animationState]);
            if (iVar2 == 0) {
                super->direction = DirectionTurnAround(super->direction);
                super->animationState = AnimationStateFlip90(super->animationState);
            } else {
                switch (uVar3) {
                    case 0x64:
                        super->flags &= ~ENT_PERSIST;
                        super->hitType = 1;
                        super->collisionFlags = 0x47;
                        super->hurtType = 0x44;
                        super->flags2 = 0x80;
                        super->action = 6;
                        sub_08017744(super);
                        gPlayerState.jump_status = 0x41;
                        gPlayerState.flags = (gPlayerState.flags ^ PL_IN_MINECART) | PL_ENTER_MINECART;
                        gPlayerEntity.base.zVelocity = Q_16_16(2.0);
                        gPlayerEntity.base.speed = 0x200;
                        gPlayerEntity.base.animationState = super->animationState << 1;
                        gPlayerEntity.base.direction = super->direction;
                        gPlayerEntity.base.flags |= PL_MINISH;
                        SnapToTile(super);
                        InitAnimationForceUpdate(super, super->animationState + 0xc);
                        SoundReq(SFX_PLY_VO4);
                        SoundReq(SFX_139);
                        return;
                    case 0x67:
                    case 0x68:
                    case 0x69:
                    case 0x6a:
                    case 0x6c:
                    case 0x6d:
                    case 0x6e:
                    case 0x6f:
                        if (uVar3 == GetActTile(super)) {
                            Minecart_Action4(this);
                            gPlayerEntity.base.animationState = super->animationState << 1;
                            return;
                        }
                        break;
                }
            }

            gPlayerEntity.base.animationState = super->animationState << 1;
            if (super->animIndex == super->animationState) {
                UpdateAnimationSingleFrame(super);
            } else {
                InitAnimationForceUpdate(super, super->animationState);
            }
        }
    }
}

void Minecart_Action4(MinecartEntity* this) {
    SnapToTile(super);
    CopyPosition(super, &gPlayerEntity.base);
    switch (GetActTile(super)) {
        case 0x67:
            if (super->direction == DirectionWest) {
                super->direction = DirectionNorth;
                super->animationState = 0;
            } else {
                super->direction = DirectionEast;
                super->animationState = 1;
            }
            break;
        case 0x68:
            if (super->direction == DirectionEast) {
                super->direction = DirectionSouth;
                super->animationState = 2;
            } else {
                super->direction = DirectionWest;
                super->animationState = 3;
            }
            break;
        case 0x69:
            if (super->direction == DirectionEast) {
                super->direction = DirectionNorth;
                super->animationState = 0;
            } else {
                super->direction = DirectionWest;
                super->animationState = 3;
            }
            break;
        case 0x6a:
            if (super->direction == DirectionWest) {
                super->direction = DirectionSouth;
                super->animationState = 2;
            } else {
                super->direction = DirectionEast;
                super->animationState = 1;
            }
            break;
        default:
            super->action = 3;
            return;
    }

    super->action = 3;
    gPlayerEntity.base.animationState = super->animationState << 1;
}

void Minecart_Action5(MinecartEntity* this) {
    LinearMoveUpdate(super);
    CopyPosition(super, &gPlayerEntity.base);
    if (gRoomControls.reload_flags == 0) {
        super->action = 3;
        super->speed = 0x700;
        gRoomControls.camera_target = &gPlayerEntity.base;
    }
}

void Minecart_Action6(MinecartEntity* this) {
    MinecartData* minecartData;

    if (gPlayerState.jump_status == 0) {
        super->action = 1;
        super->direction = DirectionTurnAround(super->direction);
        super->animationState ^= 2;
        gPlayerState.flags ^= PL_ENTER_MINECART;
        minecartData = this->minecartData;
        minecartData->tilePos = COORD_TO_TILE(super);
        minecartData->room = gRoomControls.room;
        minecartData->animationState = super->animationState;
        InitAnimationForceUpdate(super, super->animationState + 0x10);
        SetTile(0x4022, COORD_TO_TILE(super), super->collisionLayer);
    } else {
        UpdateAnimationSingleFrame(super);
        gPlayerEntity.base.spritePriority.b0 = super->spritePriority.b0 - 1;
    }
}

void Minecart_Action7(MinecartEntity* this) {
    if (super->zVelocity < 0) {
        UpdateAnimationSingleFrame(super);
    }
    if (((super->frame & 0x7f) == 0) && GravityUpdate(super, Q_8_8(32.0)) == 0) {
        super->type2 ^= 4;
        super->action = 1;
        this->minecartData->field_0x6 = super->type2;
        SoundReq(SFX_13C);
    }
}

bool32 sub_08091DDC(MinecartEntity* this) {
    static const s8 gUnk_081223D0[] = { 0, -8, 8, 0, 0, 8, -8, 0 };
    if ((sub_080B1AF0(super, gUnk_081223D0[super->animationState * 2], gUnk_081223D0[super->animationState * 2 + 1]) ==
         0xff) &&
        (sub_0807BD14(&gPlayerEntity.base, super->animationState))) {
        super->updatePriority = 6;
        super->action = 5;
        switch (super->direction) {
            case 0:
            case 8:
            case 0x10:
            case 0x18:
                super->speed = 0xa0;
                break;
        }
        gPlayerEntity.base.direction = super->direction;
        gRoomControls.camera_target = super;
        return 1;
    } else {
        return 0;
    }
}