summaryrefslogtreecommitdiff
path: root/src/overlays/actors/ovl_En_Dha/z_en_dha.c
blob: f06b0e6c687e7f060392c218e66b30c4e9347e8d (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
/*
 * File: z_en_dha.c
 * Overlay: ovl_En_Dha
 * Description: Dead Hand's Hand
 */

#include "z_en_dha.h"
#include "overlays/actors/ovl_En_Dh/z_en_dh.h"

#include "libc64/qrand.h"
#include "array_count.h"
#include "gfx.h"
#include "gfx_setupdl.h"
#include "ichain.h"
#include "sfx.h"
#include "sys_matrix.h"
#include "versions.h"
#include "z_en_item00.h"
#include "z_lib.h"
#include "play_state.h"
#include "player.h"
#include "save.h"

#include "assets/objects/object_dh/object_dh.h"

#define FLAGS (ACTOR_FLAG_ATTENTION_ENABLED | ACTOR_FLAG_HOSTILE | ACTOR_FLAG_UPDATE_CULLING_DISABLED)

void EnDha_Init(Actor* thisx, PlayState* play);
void EnDha_Destroy(Actor* thisx, PlayState* play);
void EnDha_Update(Actor* thisx, PlayState* play);
void EnDha_Draw(Actor* thisx, PlayState* play);

void EnDha_SetupWait(EnDha* this);
void EnDha_Wait(EnDha* this, PlayState* play);
void EnDha_SetupTakeDamage(EnDha* this);
void EnDha_TakeDamage(EnDha* this, PlayState* play);
void EnDha_SetupDeath(EnDha* this);
void EnDha_Die(EnDha* this, PlayState* play);
void EnDha_UpdateHealth(EnDha* this, PlayState* play);

ActorProfile En_Dha_Profile = {
    /**/ ACTOR_EN_DHA,
    /**/ ACTORCAT_ENEMY,
    /**/ FLAGS,
    /**/ OBJECT_DH,
    /**/ sizeof(EnDha),
    /**/ EnDha_Init,
    /**/ EnDha_Destroy,
    /**/ EnDha_Update,
    /**/ EnDha_Draw,
};

static DamageTable sDamageTable = {
    /* Deku nut      */ DMG_ENTRY(0, 0x0),
    /* Deku stick    */ DMG_ENTRY(2, 0xF),
    /* Slingshot     */ DMG_ENTRY(0, 0x0),
    /* Explosive     */ DMG_ENTRY(0, 0x0),
    /* Boomerang     */ DMG_ENTRY(0, 0x0),
    /* Normal arrow  */ DMG_ENTRY(0, 0x0),
    /* Hammer swing  */ DMG_ENTRY(0, 0x0),
    /* Hookshot      */ DMG_ENTRY(0, 0x0),
    /* Kokiri sword  */ DMG_ENTRY(2, 0xF),
    /* Master sword  */ DMG_ENTRY(2, 0xF),
    /* Giant's Knife */ DMG_ENTRY(4, 0xF),
    /* Fire arrow    */ DMG_ENTRY(0, 0x0),
    /* Ice arrow     */ DMG_ENTRY(0, 0x0),
    /* Light arrow   */ DMG_ENTRY(0, 0x0),
    /* Unk arrow 1   */ DMG_ENTRY(0, 0x0),
    /* Unk arrow 2   */ DMG_ENTRY(0, 0x0),
    /* Unk arrow 3   */ DMG_ENTRY(0, 0x0),
    /* Fire magic    */ DMG_ENTRY(0, 0x0),
    /* Ice magic     */ DMG_ENTRY(0, 0x0),
    /* Light magic   */ DMG_ENTRY(0, 0x0),
    /* Shield        */ DMG_ENTRY(0, 0x0),
    /* Mirror Ray    */ DMG_ENTRY(0, 0x0),
    /* Kokiri spin   */ DMG_ENTRY(2, 0xF),
    /* Giant spin    */ DMG_ENTRY(4, 0xF),
    /* Master spin   */ DMG_ENTRY(2, 0xF),
    /* Kokiri jump   */ DMG_ENTRY(4, 0xF),
    /* Giant jump    */ DMG_ENTRY(8, 0xF),
    /* Master jump   */ DMG_ENTRY(4, 0xF),
    /* Unknown 1     */ DMG_ENTRY(0, 0x0),
    /* Unblockable   */ DMG_ENTRY(0, 0x0),
    /* Hammer jump   */ DMG_ENTRY(4, 0xF),
    /* Unknown 2     */ DMG_ENTRY(0, 0x0),
};

static ColliderJntSphElementInit sJntSphElementsInit[] = {
    {
        {
            ELEM_MATERIAL_UNK0,
            { 0x00000000, HIT_SPECIAL_EFFECT_NONE, 0x00 },
            { 0xFFCFFFFF, HIT_BACKLASH_NONE, 0x00 },
            ATELEM_NONE,
            ACELEM_ON,
            OCELEM_NONE,
        },
        { 1, { { 0, 0, 0 }, 12 }, 100 },
    },
    {
        {
            ELEM_MATERIAL_UNK0,
            { 0x00000000, HIT_SPECIAL_EFFECT_NONE, 0x00 },
            { 0xFFCFFFFF, HIT_BACKLASH_NONE, 0x00 },
            ATELEM_NONE,
            ACELEM_ON,
            OCELEM_ON,
        },
        { 2, { { 3200, 0, 0 }, 10 }, 100 },
    },
    {
        {
            ELEM_MATERIAL_UNK0,
            { 0x00000000, HIT_SPECIAL_EFFECT_NONE, 0x00 },
            { 0xFFCFFFFF, HIT_BACKLASH_NONE, 0x00 },
            ATELEM_NONE,
            ACELEM_ON,
            OCELEM_ON,
        },
        { 3, { { 1200, 0, 0 }, 10 }, 100 },
    },
    {
        {
            ELEM_MATERIAL_UNK0,
            { 0x00000000, HIT_SPECIAL_EFFECT_NONE, 0x00 },
            { 0xFFCFFFFF, HIT_BACKLASH_NONE, 0x00 },
            ATELEM_NONE,
            ACELEM_ON,
            OCELEM_NONE,
        },
        { 4, { { 2700, 0, 0 }, 10 }, 100 },
    },
    {
        {
            ELEM_MATERIAL_UNK0,
            { 0x00000000, HIT_SPECIAL_EFFECT_NONE, 0x00 },
            { 0xFFCFFFFF, HIT_BACKLASH_NONE, 0x00 },
            ATELEM_NONE,
            ACELEM_ON,
            OCELEM_ON,
        },
        { 5, { { 1200, 0, 0 }, 10 }, 100 },
    },
};

static ColliderJntSphInit sJntSphInit = {
    {
        COL_MATERIAL_HIT6,
        AT_NONE,
        AC_ON | AC_TYPE_PLAYER,
        OC1_ON | OC1_TYPE_PLAYER | OC1_TYPE_1,
        OC2_TYPE_1,
        COLSHAPE_JNTSPH,
    },
    ARRAY_COUNT(sJntSphElementsInit),
    sJntSphElementsInit,
};

static InitChainEntry sInitChain[] = {
    ICHAIN_S8(naviEnemyId, NAVI_ENEMY_DEAD_HANDS_HAND, ICHAIN_CONTINUE),
    ICHAIN_F32(lockOnArrowOffset, 2000, ICHAIN_CONTINUE),
    ICHAIN_VEC3F_DIV1000(scale, 10, ICHAIN_STOP),
};

void EnDha_SetupAction(EnDha* this, EnDhaActionFunc actionFunc) {
    this->actionFunc = actionFunc;
}

void EnDha_Init(Actor* thisx, PlayState* play) {
    EnDha* this = (EnDha*)thisx;

    Actor_ProcessInitChain(&this->actor, sInitChain);
    this->actor.colChkInfo.damageTable = &sDamageTable;
    SkelAnime_InitFlex(play, &this->skelAnime, &object_dh_Skel_000BD8, &object_dh_Anim_0015B0, this->jointTable,
                       this->morphTable, 4);
    ActorShape_Init(&this->actor.shape, 0, ActorShadow_DrawFeet, 90.0f);
    this->actor.focus.pos = this->actor.world.pos;
    this->actor.focus.pos.y += 50.0f;
    this->actor.colChkInfo.mass = MASS_HEAVY;
    this->actor.colChkInfo.health = 8;
    this->limbAngleX[0] = -0x4000;
    Collider_InitJntSph(play, &this->collider);
    Collider_SetJntSph(play, &this->collider, &this->actor, &sJntSphInit, this->colliderElements);
    this->actor.flags &= ~ACTOR_FLAG_ATTENTION_ENABLED;

    EnDha_SetupWait(this);
}

void EnDha_Destroy(Actor* thisx, PlayState* play) {
    s32 pad;
    EnDha* this = (EnDha*)thisx;

    Collider_DestroyJntSph(play, &this->collider);
}

void EnDha_SetupWait(EnDha* this) {
    Animation_PlayLoop(&this->skelAnime, &object_dh_Anim_0015B0);
    this->unk_1C0 = 0;
    this->actionTimer = ((Rand_ZeroOne() * 10.0f) + 5.0f);
    this->actor.speed = 0.0f;
    this->actor.world.rot.y = this->actor.shape.rot.y;
    this->actor.home.rot.z = 1;
    EnDha_SetupAction(this, EnDha_Wait);
}

void EnDha_Wait(EnDha* this, PlayState* play) {
    Vec3f zeroVec = { 0.0f, 0.0f, 0.0f }; // unused
    Vec3f armPosMultiplier1 = { 0.0f, 0.0f, 55.0f };
    Vec3f armPosMultiplier2 = { 0.0f, 0.0f, -54.0f };
    Player* player = GET_PLAYER(play);
    s32 pad;
    s32 pad2;
    Vec3f playerPos = player->actor.world.pos;
    Vec3s angle;
    s16 yaw;

    playerPos.x += Math_SinS(player->actor.shape.rot.y) * -5.0f;
    playerPos.z += Math_CosS(player->actor.shape.rot.y) * -5.0f;

    if (!LINK_IS_ADULT) {
        playerPos.y += 38.0f;
    } else {
        playerPos.y += 56.0f;
    }

    if (this->actor.xzDistToPlayer <= 100.0f) {
        this->handAngle.y = this->handAngle.x = this->limbAngleY = 0;

        if (Math_Vec3f_DistXYZ(&playerPos, &this->handPos[0]) <= 12.0f) {
            if (this->unk_1CC == 0) {
                if (play->grabPlayer(play, player)) {
                    this->timer = 0;
                    this->unk_1CC++;

                    if (this->actor.parent != NULL) {
                        this->actor.parent->params = ENDH_START_ATTACK_GRAB;
                    }

                    Actor_PlaySfx(&this->actor, NA_SE_EN_DEADHAND_GRIP);
                }
            } else {
                this->timer += 0x1194;
                this->limbAngleY = Math_SinS(this->timer) * 1820.0f;

                if (!(player->stateFlags2 & PLAYER_STATE2_7)) {
                    this->unk_1CC = 0;
                    EnDha_SetupTakeDamage(this);
                    return;
                }

                if (this->timer < -0x6E6B) {
                    Actor_PlaySfx(&this->actor, NA_SE_EN_DEADHAND_GRIP);
                }
            }

            func_80035844(&this->handPos[1], &playerPos, &this->handAngle, 0);
            this->handAngle.y -= this->actor.shape.rot.y + this->limbAngleY;
            this->handAngle.x -= this->actor.shape.rot.x + this->limbAngleX[0] + this->limbAngleX[1];
        } else {
#if OOT_VERSION < NTSC_1_1
            // Empty
#elif OOT_VERSION < PAL_1_0
            if ((player->stateFlags2 & PLAYER_STATE2_7) && (&this->actor == player->actor.parent)) {
                player->stateFlags2 &= ~PLAYER_STATE2_7;
                player->actor.parent = NULL;
            }
#else
            if ((player->stateFlags2 & PLAYER_STATE2_7) && (&this->actor == player->actor.parent)) {
                player->stateFlags2 &= ~PLAYER_STATE2_7;
                player->actor.parent = NULL;
                player->av2.actionVar2 = 200;
            }
#endif

            if (this->actor.home.rot.z != 0) {
                Actor_PlaySfx(&this->actor, NA_SE_EN_DEADHAND_HAND_AT);
                this->actor.home.rot.z = 0;
            }
        }

        this->actor.shape.rot.y = Math_Vec3f_Yaw(&this->actor.world.pos, &playerPos);

        Math_SmoothStepToF(&this->handPos[0].x, playerPos.x, 1.0f, 16.0f, 0.0f);
        Math_SmoothStepToF(&this->handPos[0].y, playerPos.y, 1.0f, 16.0f, 0.0f);
        Math_SmoothStepToF(&this->handPos[0].z, playerPos.z, 1.0f, 16.0f, 0.0f);

        func_80035844(&this->armPos, &this->handPos[0], &angle, 0);
        Matrix_Translate(this->handPos[0].x, this->handPos[0].y, this->handPos[0].z, MTXMODE_NEW);
        Matrix_RotateZYX(angle.x, angle.y, 0, MTXMODE_APPLY);
        Matrix_MultVec3f(&armPosMultiplier2, &this->armPos);
        Matrix_Translate(this->actor.world.pos.x, this->actor.world.pos.y, this->actor.world.pos.z, MTXMODE_NEW);
        func_80035844(&this->actor.world.pos, &this->armPos, &angle, 0);
        Matrix_RotateZYX(angle.x, angle.y, 0, MTXMODE_APPLY);
        Matrix_MultVec3f(&armPosMultiplier1, &this->armPos);
        this->limbAngleX[0] = Math_Vec3f_Pitch(&this->actor.world.pos, &this->armPos);
        yaw = Math_Vec3f_Yaw(&this->actor.world.pos, &this->armPos) - this->actor.shape.rot.y;

        if (ABS(yaw) >= 0x4000) {
            this->limbAngleX[0] = -0x8000 - this->limbAngleX[0];
        }

        this->limbAngleX[1] = (Math_Vec3f_Pitch(&this->armPos, &this->handPos[0]) - this->limbAngleX[0]);

        if (this->limbAngleX[1] < 0) {
            this->limbAngleX[0] += this->limbAngleX[1] * 2;
            this->limbAngleX[1] *= -2;
        }
    } else {
        if ((player->stateFlags2 & PLAYER_STATE2_7) && (&this->actor == player->actor.parent)) {
            player->stateFlags2 &= ~PLAYER_STATE2_7;
            player->actor.parent = NULL;
#if OOT_VERSION >= PAL_1_0
            player->av2.actionVar2 = 200;
#endif
        }

        this->actor.home.rot.z = 1;
        Math_SmoothStepToS(&this->limbAngleX[1], 0, 1, 0x3E8, 0);
        Math_SmoothStepToS(&this->limbAngleX[0], -0x4000, 1, 0x3E8, 0);
        SkelAnime_Update(&this->skelAnime);
    }
}

void EnDha_SetupTakeDamage(EnDha* this) {
    this->actionTimer = 15;
    EnDha_SetupAction(this, EnDha_TakeDamage);
}

void EnDha_TakeDamage(EnDha* this, PlayState* play) {
    Player* player = GET_PLAYER(play);

    if ((player->stateFlags2 & PLAYER_STATE2_7) && (&this->actor == player->actor.parent)) {
        player->stateFlags2 &= ~PLAYER_STATE2_7;
        player->actor.parent = NULL;
#if OOT_VERSION >= PAL_1_0
        player->av2.actionVar2 = 200;
#endif
    }

    Math_SmoothStepToS(&this->limbAngleX[1], 0, 1, 2000, 0);
    Math_SmoothStepToS(&this->limbAngleY, 0, 1, 600, 0);
    Math_SmoothStepToS(&this->limbAngleX[0], -0x4000, 1, 2000, 0);
    SkelAnime_Update(&this->skelAnime);
    this->actionTimer--;

    if (this->actionTimer == 0) {
        EnDha_SetupWait(this);
    }
}

void EnDha_SetupDeath(EnDha* this) {
    this->unk_1C0 = 8;
    this->actionTimer = 300;

    if (this->actor.parent != NULL) {
        if (this->actor.parent->params != ENDH_DEATH) {
            Actor_PlaySfx(&this->actor, NA_SE_EN_DEADHAND_HAND_DEAD);
        }
        if (this->actor.parent->params <= ENDH_WAIT_UNDERGROUND) {
            this->actor.parent->params--;
        }
    }

    EnDha_SetupAction(this, EnDha_Die);
}

void EnDha_Die(EnDha* this, PlayState* play) {
    s16 angle;
    Vec3f vec;
    Player* player = GET_PLAYER(play);

    if ((player->stateFlags2 & PLAYER_STATE2_7) && (&this->actor == player->actor.parent)) {
        player->stateFlags2 &= ~PLAYER_STATE2_7;
        player->actor.parent = NULL;
#if OOT_VERSION >= PAL_1_0
        player->av2.actionVar2 = 200;
#endif
    }

    Math_SmoothStepToS(&this->limbAngleX[1], 0, 1, 0x7D0, 0);
    angle = Math_SmoothStepToS(&this->limbAngleX[0], -0x4000, 1, 0x7D0, 0);
    SkelAnime_Update(&this->skelAnime);

    if (angle == 0) {
        vec = this->actor.world.pos;

        if (this->actionTimer != 0) {
            if (-12000.0f < this->actor.shape.yOffset) {
                this->actor.shape.yOffset -= 1000.0f;
                func_80033480(play, &vec, 7.0f, 1, 0x5A, 0x14, 1);
            } else {
                this->actionTimer--;

                if ((this->actor.parent != NULL) && (this->actor.parent->params == ENDH_DEATH)) {
                    Actor_Kill(&this->actor);
                }
            }
        } else {
            this->actor.shape.yOffset += 500.0f;
            func_80033480(play, &vec, 7.0f, 1, 0x5A, 0x14, 1);

            if (this->actor.shape.yOffset == 0.0f) {
                EnDha_SetupWait(this);
            }
        }
    }
}

void EnDha_UpdateHealth(EnDha* this, PlayState* play) {
    if (!((this->unk_1C0 >= 8) || !(this->collider.base.acFlags & AC_HIT))) {
        this->collider.base.acFlags &= ~AC_HIT;

        if (this->actor.colChkInfo.damageReaction == 0 || this->actor.colChkInfo.damageReaction == 6) {
            return;
        } else {
            Actor_SetColorFilter(&this->actor, COLORFILTER_COLORFLAG_RED, 255, COLORFILTER_BUFFLAG_OPA, 8);
            if (Actor_ApplyDamage(&this->actor) == 0) {
                EnDha_SetupDeath(this);
                this->actor.colChkInfo.health = 8;
                Item_DropCollectibleRandom(play, &this->actor, &this->actor.world.pos,
                                           COLLECTIBLE_DROP_RANDOM_PARAMS(COLLECTIBLE_DROP_TABLE_14, false));
            } else {
                Actor_PlaySfx(&this->actor, NA_SE_EN_DEADHAND_DAMAGE);
                this->unk_1C0 = 9;
                EnDha_SetupTakeDamage(this);
            }
        }
    }

    if ((this->actor.parent != NULL) && (this->actor.parent->params == ENDH_DEATH)) {
        EnDha_SetupDeath(this);
    }
}

void EnDha_Update(Actor* thisx, PlayState* play) {
    s32 pad;
    EnDha* this = (EnDha*)thisx;

    if (this->actor.parent == NULL) {
        this->actor.parent = Actor_FindNearby(play, &this->actor, ACTOR_EN_DH, ACTORCAT_ENEMY, 10000.0f);
    }

    EnDha_UpdateHealth(this, play);
    this->actionFunc(this, play);
    CollisionCheck_SetAC(play, &play->colChkCtx, &this->collider.base);
    CollisionCheck_SetOC(play, &play->colChkCtx, &this->collider.base);
}

s32 EnDha_OverrideLimbDraw(PlayState* play, s32 limbIndex, Gfx** dList, Vec3f* pos, Vec3s* rot, void* thisx) {
    EnDha* this = (EnDha*)thisx;

    if (limbIndex == 1) {
        rot->y = -(s16)(this->limbAngleX[0] + 0x4000);
        rot->z += this->limbAngleY;
    } else if (limbIndex == 2) {
        rot->z = this->limbAngleX[1];
        rot->y -= this->limbAngleY;
    } else if (limbIndex == 3) {
        rot->y = -this->handAngle.y;
        rot->z = -this->handAngle.x;
    }

    return false;
}

void EnDha_PostLimbDraw(PlayState* play, s32 limbIndex, Gfx** dList, Vec3s* rot, void* thisx) {
    Vec3f handVec = { 1100.0f, 0.0f, 0.0f };
    Vec3f zeroVec = { 0.0f, 0.0f, 0.0f };
    EnDha* this = (EnDha*)thisx;

    switch (limbIndex) {
        case 1:
            Collider_UpdateSpheres(2, &this->collider);
            Collider_UpdateSpheres(3, &this->collider);
            break;
        case 2:
            Collider_UpdateSpheres(4, &this->collider);
            Collider_UpdateSpheres(5, &this->collider);
            Matrix_MultVec3f(&zeroVec, &this->armPos);
            break;
        case 3:
            Collider_UpdateSpheres(1, &this->collider);
            Matrix_MultVec3f(&handVec, &this->handPos[0]);
            Matrix_MultVec3f(&zeroVec, &this->handPos[1]);
            break;
    }
}

void EnDha_Draw(Actor* thisx, PlayState* play) {
    s32 pad;
    EnDha* this = (EnDha*)thisx;

    Gfx_SetupDL_25Opa(play->state.gfxCtx);
    SkelAnime_DrawFlexOpa(play, this->skelAnime.skeleton, this->skelAnime.jointTable, this->skelAnime.dListCount,
                          EnDha_OverrideLimbDraw, EnDha_PostLimbDraw, this);
}