summaryrefslogtreecommitdiff
path: root/src/enemy/lakitu.c
blob: bff5b976d069fa4ea5d2fbc5f2945048aa99811f (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
/**
 * @file lakitu.c
 * @ingroup Enemies
 *
 * @brief Lakitu enemy
 */
#include "enemy.h"
#include "sound.h"
#include "effects.h"
#include "projectile.h"
#include "physics.h"
#include "player.h"
#include "asm.h"

typedef struct {
    /*0x00*/ Entity base;
    /*0x68*/ u8 unused1[12];
    /*0x74*/ u16 unk_74;
    /*0x76*/ u16 unk_76;
    /*0x78*/ u8 unk_78;
    /*0x79*/ u8 unk_79;
} LakituEntity;

extern void (*const LakituActionFuncs[])(LakituEntity*);

// Forward references to functions in lakitu.c
extern void sub_0803CAD0(LakituEntity* this);
extern void sub_0803CBAC(LakituEntity* this);
extern void sub_0803CA84(LakituEntity* this, u32);
extern bool32 sub_0803CA4C(LakituEntity* this);
extern bool32 sub_0803CB04(LakituEntity* this);
extern void Lakitu_SpawnLightning(LakituEntity* this);
extern void sub_0803CB34(LakituEntity* this);
extern void sub_0803CC08(LakituEntity* this);

enum {
    INIT,
    HIDDEN,
    END_HIDE,
    IDLE,
    BEGIN_HIDE,
    LIGHTNING_THROW,
    LIGHTNING_DELAY,
    CLOUDLESS,
};

typedef struct {
    s8 x;
    s8 y;
} PACKED OffsetCoords;

// Variables
extern void (*const Lakitu_Functions[])(LakituEntity*);
extern void (*const gUnk_080D0128[])(LakituEntity*);
extern void (*const gUnk_080D0148[])(LakituEntity*);
extern const OffsetCoords gUnk_080D0154[];

void Lakitu(LakituEntity* this) {
    EnemyFunctionHandler(super, (EntityActionArray)Lakitu_Functions);

    EnemySetFXOffset(super, 0, 1, -16);
}

void Lakitu_OnTick(LakituEntity* this) {
    if (super->action != 0 && super->action != 7) {
        sub_0803CAD0(this);
    }

    LakituActionFuncs[super->action](this);
}

void Lakitu_OnCollision(LakituEntity* this) {
    if ((super->contactFlags & 0x7f) == 0x1d) {
        super->zVelocity = Q_16_16(2.0);

        sub_0803CBAC(this);
    } else {
        if (super->hitType == 0x43) {
            Entity* fx = CreateFx(super, FX_DEATH, 0);

            if (fx != NULL) {
                u32 angle = (super->knockbackDirection ^ 0x10) << 3;
                s32 sine;

                sine = gSineTable[angle];
                if (sine < 0) {
                    sine += 0x1f;
                }

                fx->x.HALF.HI += sine >> 5;

                sine = gSineTable[angle + 0x40];
                if (sine < 0) {
                    sine += 0x1f;
                }

                fx->y.HALF.HI -= sine >> 5;
            }
        }
    }

    if (super->confusedTime) {
        EnemyCreateFX(super, FX_STARS);
    }

    EnemyFunctionHandlerAfterCollision(super, Lakitu_Functions);
}

void Lakitu_OnGrabbed(LakituEntity* this) {
    if (sub_0806F520(super)) {
        gUnk_080D0148[super->subAction](this);
    }
}

void sub_0803C844(LakituEntity* this) {
    super->subAction = 1;
    super->gustJarTolerance = 0x3c;
}

void sub_0803C850(LakituEntity* this) {
    Entity* cloud = super->child;
    if (cloud != NULL) {
        cloud->spriteOffsetX = super->spriteOffsetX;
    }

    sub_0806F4E8(super);
}

void sub_0803C86C(LakituEntity* this) {
    sub_0803CBAC(this);
    super->child = NULL;
}

void Lakitu_Initialize(LakituEntity* this) {
    Entity* cloud = EnemyCreateProjectile(super, 17, 0);
    if (cloud == NULL) {
        return;
    }

    cloud->parent = super;
    super->child = cloud;

    sub_0804A720(super);

    super->action = HIDDEN;

    super->z.HALF.HI = -2;

    super->spriteOffsetY = 0xff;

    this->unk_74 = super->x.HALF.HI;
    this->unk_76 = super->y.HALF.HI;
}

void Lakitu_Hide(LakituEntity* this) {
    sub_0803CA84(this, 0);

    if (sub_0803CA4C(this)) {
        super->action = END_HIDE;
        super->spriteSettings.draw = 1;
    }
}

void Lakitu_EndHide(LakituEntity* this) {
    UpdateAnimationSingleFrame(super);

    if (super->frame & ANIM_DONE) {
        super->action = IDLE;
        super->timer = 60;

        super->hitType = 0x42;

        InitAnimationForceUpdate(super, super->animationState + 4);
    }
}

void Lakitu_Idle(LakituEntity* this) {
    if (sub_0803CB04(this)) {
        return;
    }

    if (!sub_0803CA4C(this)) {
        super->action = BEGIN_HIDE;

        super->hitType = 0x43;
        InitAnimationForceUpdate(super, super->animationState + 0xc);
    } else {
        sub_0803CA84(this, 4);
    }
}

void Lakitu_BeginHide(LakituEntity* this) {
    UpdateAnimationSingleFrame(super);

    if (super->frame & ANIM_DONE) {
        super->action = HIDDEN;
        super->spriteSettings.draw = 0;

        InitAnimationForceUpdate(super, super->animationState);
    }
}

void Lakitu_Lightning(LakituEntity* this) {
    UpdateAnimationSingleFrame(super);

    if (!(super->frame & ANIM_DONE)) {
        return;
    }

    Lakitu_SpawnLightning(this);

    super->action = LIGHTNING_DELAY;
    super->hitType = 0x42;

    if ((Random() & 1) && !this->unk_79) {
        super->timer = 15;

        this->unk_79 = TRUE;
    } else {
        super->timer = 30;

        this->unk_79 = FALSE;

        InitAnimationForceUpdate(super->child, super->animationState);
    }
}

void Lakitu_LightningDelay(LakituEntity* this) {
    super->timer--;

    if (super->timer != 0) {
        return;
    }

    if (this->unk_79 == 1) {
        sub_0803CB34(this);
    } else {
        super->action = IDLE;
        super->timer = 180;

        InitAnimationForceUpdate(super, super->animationState + 4);
    }
}

void Lakitu_Cloudless(LakituEntity* this) {
    if (GravityUpdate(super, Q_8_8(24.0)) == 0 && super->animIndex <= 19) {
        InitAnimationForceUpdate(super, super->animationState + 20);

        super->spritePriority.b1 = 0;
    }

    UpdateAnimationSingleFrame(super);
    sub_0803CC08(this);
}

bool32 sub_0803CA4C(LakituEntity* this) {
    if (EntityWithinDistance(super, gPlayerEntity.base.x.HALF.HI, gPlayerEntity.base.y.HALF.HI, 0x28) == 0) {
        if (EntityInRectRadius(super, &gPlayerEntity.base, 0x70, 0x50)) {
            return TRUE;
        }
    }

    return FALSE;
}

void sub_0803CA84(LakituEntity* this, u32 unkParameter) {
    u32 altAnimState = GetFacingDirection(super, &gPlayerEntity.base);

    if (((altAnimState - 3) & 7) > 2 || ((super->animationState - (altAnimState >> 3)) & 3) > 1) {
        altAnimState = DirectionRoundUp(altAnimState) >> 3;

        if (altAnimState != super->animationState) {
            super->animationState = altAnimState;

            InitAnimationForceUpdate(super, altAnimState + unkParameter);
            InitAnimationForceUpdate(super->child, altAnimState);
        }
    }
}

void sub_0803CAD0(LakituEntity* this) {
    if (!EntityWithinDistance(super, this->unk_74, this->unk_76, 1)) {
        super->direction = CalculateDirectionTo(super->x.HALF.HI, super->y.HALF.HI, this->unk_74, this->unk_76);

        ProcessMovement2(super);
    }
}

bool32 sub_0803CB04(LakituEntity* this) {
    bool32 ret;
    u8 delay;

    delay = --super->timer;
    if (delay != 0) {
        ret = FALSE;
    } else {
        sub_0803CB34(this);
        this->unk_79 = delay;

        InitAnimationForceUpdate(super->child, super->animationState + 4);
        ret = TRUE;
    }

    return ret;
}

void sub_0803CB34(LakituEntity* this) {
    super->action = LIGHTNING_THROW;
    super->hitType = 0xa6;

    this->unk_78 = GetFacingDirection(super, &gPlayerEntity.base);

    InitAnimationForceUpdate(super, super->animationState + 8);
}

void Lakitu_SpawnLightning(LakituEntity* this) {
    Entity* lightning;
    const OffsetCoords* offset;

    lightning = EnemyCreateProjectile(super, LAKITU_LIGHTNING, 0);

    if (lightning == NULL) {
        return;
    }

    offset = &gUnk_080D0154[super->animationState];

    lightning->direction = this->unk_78;

    PositionRelative(super, lightning, Q_16_16(offset->x), Q_16_16(offset->y));

    EnqueueSFX(SFX_193);
}

void sub_0803CBAC(LakituEntity* this) {
    Entity* cloud;

    cloud = super->child;
    if (cloud != NULL) {
        cloud->flags |= ENT_COLLIDE;
        cloud->hitType = 0x43;
    }

    super->action = CLOUDLESS;
    super->spriteSettings.draw = 1;

    super->spritePriority.b1 = 1;

    super->collisionMask &= 0x7b;

    super->hitType = 0x42;

    InitAnimationForceUpdate(super, super->animationState + 16);
}

void sub_0803CC08(LakituEntity* this) {
    Entity* cloud;
    Entity* fx;

    const s32 diff = Q_8_8(3.0 / 128.0);

    cloud = super->child;
    if (cloud == NULL) {
        return;
    }

    if ((u32)(cloud->z.HALF.HI - super->z.HALF.HI) > 2) {
        return;
    }

    if (super->zVelocity >= 0) {
        return;
    }

    if (!EntityWithinDistance(super, cloud->x.HALF.HI, cloud->y.HALF.HI, 6)) {
        return;
    }

    fx = CreateFx(super, FX_DEATH, 0);
    if (fx != NULL) {
        fx->x.HALF.HI += diff;
        fx->y.HALF.HI += diff;
    }

    fx = CreateFx(super, FX_DEATH, 0);
    if (fx != NULL) {
        fx->x.HALF.HI -= diff;
        fx->y.HALF.HI += diff;
    }

    fx = CreateFx(super, FX_DEATH, 0);
    if (fx != NULL) {
        fx->x.HALF.HI += diff;
        fx->y.HALF.HI -= diff;
    }

    fx = CreateFx(super, FX_DEATH, 0);
    if (fx != NULL) {
        fx->x.HALF.HI -= diff;
        fx->y.HALF.HI -= diff;
    }

    super->child = NULL;
    DeleteEntity(cloud);
}

void (*const Lakitu_Functions[])(LakituEntity*) = {
    Lakitu_OnTick,
    Lakitu_OnCollision,
    (void (*)(LakituEntity*))GenericKnockback,
    (void (*)(LakituEntity*))GenericDeath,
    (void (*)(LakituEntity*))GenericConfused,
    Lakitu_OnGrabbed,
};

void (*const LakituActionFuncs[])(LakituEntity*) = {
    Lakitu_Initialize, Lakitu_Hide,      Lakitu_EndHide,        Lakitu_Idle,
    Lakitu_BeginHide,  Lakitu_Lightning, Lakitu_LightningDelay, Lakitu_Cloudless,
};

void (*const gUnk_080D0148[])(LakituEntity*) = {
    sub_0803C844,
    sub_0803C850,
    sub_0803C86C,
};

const OffsetCoords gUnk_080D0154[] = {
    { 0x00, 0xEE },
    { 0x12, 0x00 },
    { 0x00, 0x08 },
    { 0xEE, 0x00 },
};