summaryrefslogtreecommitdiff
path: root/src/enemy/rollobite.c
blob: db6233e18745e6ae5dc483b06b0320677e4676b4 (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
/**
 * @file rollobite.c
 * @ingroup Enemies
 *
 * @brief Rollobite enemy
 */
#include "collision.h"
#include "enemy.h"
#include "physics.h"
#include "player.h"
#include "room.h"

typedef struct {
    /*0x00*/ Entity base;
    /*0x68*/ u8 unused1[28];
    /*0x84*/ u8 unk_84;
    /*0x85*/ u8 unk_85;
} RollobiteEntity;

extern void (*const Rollobite_Functions[])(RollobiteEntity*);
extern void (*const gRollobiteActions[])(RollobiteEntity*);
extern void (*const gUnk_080CA6A4[])(RollobiteEntity*);
extern void (*const gUnk_080CA6BC[])(RollobiteEntity*);

extern const u8 gUnk_080CA6CC[];
extern const s8 gUnk_080CA6D4[];

void sub_08020A30(RollobiteEntity* this);
void sub_08020A7C(RollobiteEntity* this);
bool32 Rollobite_TryToHoleUp(RollobiteEntity* this);
bool32 Rollobite_IsRolledUp(RollobiteEntity* this);

extern void RegisterCarryEntity(Entity*);

void Rollobite(RollobiteEntity* this) {
    EnemyFunctionHandler(super, (EntityActionArray)Rollobite_Functions);
}

void Rollobite_OnTick(RollobiteEntity* this) {
    Rollobite_TryToHoleUp(this);
    gRollobiteActions[super->action](this);
}

void Rollobite_OnCollision(RollobiteEntity* this) {
    if (super->hitType == 34 && super->health != 0xff) {
        super->action = 4;
        super->zVelocity = Q_16_16(2.0);
        super->direction = DIR_NONE;
        super->health = 0xff;
        super->hitType = 35;
        InitializeAnimation(super, super->animationState + 8);
    }

    if (super->contactFlags != CONTACT_TAKE_DAMAGE) {
        if (super->action == 4 || super->action == 5) {
            super->action = 4;
            super->timer = 180;
            super->direction = DIR_NONE;
            InitializeAnimation(super, super->animationState + 0x10);
        }
    }

    if (super->contactFlags == (CONTACT_TAKE_DAMAGE | 0x13))
        Rollobite_OnTick(this);
}

void Rollobite_OnKnockback(RollobiteEntity* this) {
    if (Rollobite_TryToHoleUp(this)) {
        super->knockbackDuration = 0;
    } else if (Rollobite_IsRolledUp(this)) {
        super->knockbackDuration--;
        CalculateEntityTileCollisions(super, super->knockbackDirection, 10);
        ProcessMovementInternal(super, super->knockbackSpeed, super->knockbackDirection, 10);
    } else {
        GenericKnockback(super);
    }
}

void Rollobite_OnGrabbed(RollobiteEntity* this) {
    if (super->subAction < 3 && !sub_0806F520(super)) {
        super->action = 4;
        COLLISION_ON(super);
        super->direction = DIR_NONE;
        InitializeAnimation(super, super->animationState + 0x10);
    } else {
        gUnk_080CA6A4[super->subAction](this);
    }
}

void sub_0802077C(RollobiteEntity* this) {
    super->subAction = 1;
    super->gustJarTolerance = 60;
}

void sub_08020788(RollobiteEntity* this) {
    sub_0806F4E8(super);
}

void sub_08020790(RollobiteEntity* this) {
    sub_0806F3E4(super);
}

void sub_08020798(RollobiteEntity* this) {
    COLLISION_OFF(super);
}

void nullsub_6(RollobiteEntity* this) {
    /* ... */
}

void sub_080207A8(RollobiteEntity* this) {
    super->action = 4;
    COLLISION_ON(super);
    super->spritePriority.b0 = 4;
    super->gustJarState &= 0xfb;
    super->direction ^= DirectionSouth;
    super->zVelocity = Q_16_16(1.5);
    super->speed = 0x80;
    InitializeAnimation(super, super->animationState + 0x10);
}

void Rollobite_Initialize(RollobiteEntity* this) {
    sub_0804A720(super);
    super->carryFlags = 0x30;
    super->gustJarFlags = 18;
    this->unk_84 = 0;
    super->direction = DirectionRound(Random());
    sub_08020A30(this);
}

void Rollobite_Walk(RollobiteEntity* this) {
    GetNextFrame(super);
    if (super->frame & 0x1) {
        super->frame &= ~0x1;
        if (!ProcessMovement0(super))
            super->timer = 1;
    }

    if (super->frame & 0x10) {
        super->frame &= ~0x10;
        if (--super->timer == 0) {
            super->action = 3;
            super->timer = 60;
        }
    }
}

void sub_08020874(RollobiteEntity* this) {
    gUnk_080CA6BC[super->subAction](this);
}

void sub_0802088C(RollobiteEntity* this) {
    super->subAction = 1;
    COLLISION_OFF(super);
    this->unk_85 = gPlayerEntity.base.animationState;
    super->spritePriority.b1 = 0;
}

void sub_080208B4(RollobiteEntity* this) {
    s8 uVar1 = (this->unk_85 - gPlayerEntity.base.animationState) / 2;
    if (uVar1) {
        super->animationState = (super->animationState + uVar1) & 3;
        InitializeAnimation(super, super->animationState + 0x10);
    }
    this->unk_85 = gPlayerEntity.base.animationState;
}

void sub_080208F0(RollobiteEntity* this) {
    super->spritePriority.b1 = 3;
}

void sub_08020904(RollobiteEntity* this) {
    super->action = 4;
    COLLISION_ON(super);
    super->direction = -1;
    InitializeAnimation(super, super->animationState + 0x10);
}

void Rollobite_Turn(RollobiteEntity* this) {
    if (--super->timer == 0)
        sub_08020A30(this);
}

void Rollobite_RolledUp(RollobiteEntity* this) {
    u32 unk;

    if ((super->frame & ANIM_DONE) == 0)
        GetNextFrame(super);

    unk = BounceUpdate(super, Q_8_8(40.0));

    if (unk == BOUNCE_DONE_ALL) {
        if (--super->timer == 0) {
            super->action = 5;
            InitializeAnimation(super, super->animationState + 12);
        }
        RegisterCarryEntity(super);
    } else {
        if (unk == BOUNCE_INIT_NEXT)
            EnqueueSFX(SFX_PLACE_OBJ);

        if (!(super->direction & DIR_NOT_MOVING_CHECK))
            ProcessMovement2(super);
    }
}

void Rollobite_Unroll(RollobiteEntity* this) {
    GetNextFrame(super);
    if (super->frame & ANIM_DONE) {
        COLLISION_ON(super);
        super->speed = 0x100;
        super->hitType = 34;
        sub_08020A30(this);
        super->direction = DirectionFromAnimationState(super->animationState);
        InitializeAnimation(super, super->animationState);
    } else {
        if ((super->frame & 1) == 0)
            RegisterCarryEntity(super);
    }
}

void Rollobite_LinedUp(RollobiteEntity* this) {
    if (GravityUpdate(super, Q_8_8(28.0)) == 0) {
        super->action = 7;
        super->spritePriority.b0 = 7;
    }

    if (super->frame == 0)
        GetNextFrame(super);
}

void Rollobite_Holed(RollobiteEntity* this) {
    GetNextFrame(super);
}

void sub_08020A30(RollobiteEntity* this) {
    if (this->unk_84 < 2) {
        super->timer = gUnk_080CA6CC[Random() & 7];
        if (super->timer == 0) {
            super->action = 3;
            super->timer = 60;
            this->unk_84++;
            return;
        }
    }
    super->action = 1;
    this->unk_84 = 0;
    sub_08020A7C(this);
}

void sub_08020A7C(RollobiteEntity* this) {
    int tmp = Random();
    u32 state = DirectionRound(super->direction + gUnk_080CA6D4[tmp % 3]);

    if (sub_08049FA0(super) == 0) {
        int tmp = DirectionRoundUp(sub_08049EE4(super));
        if ((state ^ DirectionSouth) == tmp)
            state ^= DirectionSouth;
    }

    super->direction = state;
    super->animationState = (u8)(state >> 3);
    InitializeAnimation(super, super->animationState);
}

bool32 Rollobite_TryToHoleUp(RollobiteEntity* this) {
    if (Rollobite_IsRolledUp(this) && super->z.HALF.HI == 0) {
        int tile = COORD_TO_TILE(super);
        int iVar1 = GetTileType(tile, super->collisionLayer);
        if ((iVar1 * 0x10000 - 0x710000U) >> 0x10 < 2) {
            super->action = 6;
            COLLISION_OFF(super);
            super->x.HALF.HI &= 0xfff0;
            super->x.HALF.HI += 8;
            super->y.HALF.HI &= 0xfff0;
            super->y.HALF.HI += 13;
            super->zVelocity = Q_16_16(2.0);
            InitializeAnimation(super, super->animationState + 0x14);
            SetBottomTile(0x4034, tile, super->collisionLayer);
            return TRUE;
        }
    }
    return FALSE;
}

bool32 Rollobite_IsRolledUp(RollobiteEntity* this) {
    u32 tmp = super->animIndex;
    if (16 <= tmp && tmp <= 19) {
        return TRUE;
    } else {
        return FALSE;
    }
}

// clang-format off
void (*const Rollobite_Functions[])(RollobiteEntity*) = {
    Rollobite_OnTick,
    Rollobite_OnCollision,
    Rollobite_OnKnockback,
    (void (*)(RollobiteEntity*))GenericDeath,
    (void (*)(RollobiteEntity*))GenericConfused,
    Rollobite_OnGrabbed,
};

void (*const gRollobiteActions[])(RollobiteEntity*) = {
    Rollobite_Initialize,
    Rollobite_Walk,
    sub_08020874,
    Rollobite_Turn,
    Rollobite_RolledUp,
    Rollobite_Unroll,
    Rollobite_LinedUp,
    Rollobite_Holed,
};

void (*const gUnk_080CA6A4[])(RollobiteEntity*) = {
    sub_0802077C,
    sub_08020788,
    sub_08020790,
    sub_08020798,
    nullsub_6,
    sub_080207A8,
};

void (*const gUnk_080CA6BC[])(RollobiteEntity*) = {
    sub_0802088C,
    sub_080208B4,
    sub_080208F0,
    sub_08020904,
};

const u8 gUnk_080CA6CC[] = {
    0, 6, 9, 0, 6, 6, 0, 7,
};
// clang-format on