//
// Generated by dtk
// Translation Unit: d_a_sk.cpp
//

#include "d/dolzel_rel.h" // IWYU pragma: keep
#include "d/actor/d_a_sk.h"
#include "res/Object/Sk.h"
#include "m_Do/m_Do_ext.h"
#include "d/d_jnt_hit.h"
#include "d/d_cc_d.h"
#include "d/d_com_inf_game.h"
#include "f_op/f_op_actor_mng.h"

/* 00000078-00000240       .text nodeCallBack__FP7J3DNodei */
static BOOL nodeCallBack(J3DNode* node, int calcTiming) {
    if (calcTiming == J3DNodeCBCalcTiming_In) {
        J3DJoint* joint = (J3DJoint*)node;
        s32 jntNo = joint->getJntNo();
        J3DModel* model = j3dSys.getModel();
        sk_class* i_this = (sk_class*)model->getUserArea();
        cXyz offset;

        if (i_this != NULL) {
            MTXCopy(model->getAnmMtx(jntNo), *calc_mtx);

            cMtx_YrotM(*calc_mtx, i_this->m2C2[jntNo].y);
            cMtx_XrotM(*calc_mtx, i_this->m2C2[jntNo].x);
            cMtx_ZrotM(*calc_mtx, i_this->m2C2[jntNo].z);

            switch (jntNo) {
            case TURU_00_JNT_BO04_e:
                offset.x = 0.0f;
                offset.y = 0.0f;
                offset.z = 180.0f;
                MtxPosition(&offset, &i_this->m2DC[jntNo]);
                break;

            case TURU_00_JNT_BO03_e:
                offset.x = 0.0f;
                offset.y = 0.0f;
                offset.z = 250.0f;
                MtxPosition(&offset, &i_this->m2DC[jntNo]);
                break;

            case TURU_00_JNT_BO02_e:
                offset.x = 0.0f;
                offset.y = 0.0f;
                offset.z = 250.0f;
                MtxPosition(&offset, &i_this->m2DC[jntNo]);
                break;

            case TURU_00_JNT_BO01_e:
                offset.x = 0.0f;
                offset.y = 0.0f;
                offset.z = 180.0f;
                MtxPosition(&offset, &i_this->m2DC[jntNo]);
                break;
            }

            model->setAnmMtx(jntNo, *calc_mtx);
            MTXCopy(*calc_mtx, J3DSys::mCurrentMtx);
        }
    }
    return TRUE;
}

/* 0000027C-000002EC       .text daSk_Draw__FP8sk_class */
static BOOL daSk_Draw(sk_class* i_this) {
    J3DModel* model = i_this->mpMorf->getModel();

    g_env_light.settingTevStruct(TEV_TYPE_BG0_PLIGHT, &i_this->actor.current.pos, &i_this->actor.tevStr);
    g_env_light.setLightTevColorType(model, &i_this->actor.tevStr);
    i_this->mpMorf->entryDL();
    return TRUE;
}

/* 000002EC-00000498       .text dousa_move__FP8sk_class */
void dousa_move(sk_class* i_this) {
    for (s32 i = 1; i < 4; i++) {
        i_this->m2C2[i].x = cM_ssin(i_this->m2BE * 2000 + (i * 20000)) * 2000.0f;
        i_this->m2C2[i].y = cM_ssin(i_this->m2BE * 1300 + (i * 12000)) * 2000.0f;
        i_this->m2C2[i].z = cM_ssin(i_this->m2BE * 1000 + (i * 15000)) * 4000.0f;
    }

    s16 angle = cLib_distanceAngleS(i_this->m2C2[1].z, 0x1000);
    if (angle < 0x1000) {
        if (!i_this->m2BC) {
            fopAcM_seStart(&i_this->actor, JA_SE_OBJ_SHOKU_LIFT_MOVE, 0);
            i_this->m2BC = true;
        }
    } else {
        i_this->m2BC = false;
    }
    i_this->m2BE++;
}

#if VERSION > VERSION_DEMO
/* 00000498-00000758       .text body_atari_check__FP8sk_class */
void body_atari_check(sk_class* i_this) {
    fopAc_ac_c* a_this = (fopAc_ac_c*)&i_this->actor;

    if (i_this->m2C0 > 0) {
        i_this->m2C0--;
        return;
    }

    for (s32 i = 0; i < 4; i++) {
        if (i_this->m348[i].ChkTgHit()) {
            cCcD_Obj* hitObj = i_this->m348[i].GetTgHitObj();
            if (hitObj == NULL) {
                break;
            }
            i_this->m2C0 = 8;

            switch (hitObj->GetAtType()) {
            case AT_TYPE_MACHETE:
            case AT_TYPE_SWORD:
            case AT_TYPE_UNK800:
            case AT_TYPE_DARKNUT_SWORD:
            case AT_TYPE_MOBLIN_SPEAR:
            case AT_TYPE_PGANON_SWORD:
                fopAcM_seStart(a_this, JA_SE_LK_SW_HIT_S, 0x44);
                break;

            case AT_TYPE_BOOMERANG:
            case AT_TYPE_BOKO_STICK:
            case AT_TYPE_UNK2000:
            case AT_TYPE_STALFOS_MACE:
                fopAcM_seStart(a_this, JA_SE_LK_W_WEP_HIT, 0x44);
                break;

            case AT_TYPE_SKULL_HAMMER:
                fopAcM_seStart(a_this, JA_SE_LK_HAMMER_HIT, 0x44);
                break;

            case AT_TYPE_FIRE_ARROW:
            case AT_TYPE_NORMAL_ARROW:
            case AT_TYPE_LIGHT_ARROW:
            case AT_TYPE_ICE_ARROW:
            case AT_TYPE_GRAPPLING_HOOK:
                fopAcM_seStart(a_this, JA_SE_LK_MS_WEP_HIT, 0x44);
                break;

            case AT_TYPE_BOMB:
            case AT_TYPE_WIND:
                break;
            }
        }
    }
}
#endif

/* 00000758-000008D4       .text daSk_Execute__FP8sk_class */
static BOOL daSk_Execute(sk_class* i_this) {
    dousa_move(i_this);

    J3DModel* model = i_this->mpMorf->getModel();
    model->setBaseScale(i_this->actor.scale);
    mDoMtx_stack_c::transS(i_this->actor.current.pos.x, i_this->actor.current.pos.y, i_this->actor.current.pos.z);
    mDoMtx_stack_c::YrotM(i_this->actor.current.angle.y);
    mDoMtx_stack_c::XrotM(i_this->actor.current.angle.x);
    mDoMtx_stack_c::ZrotM(i_this->actor.current.angle.z);
    model->setBaseTRMtx(mDoMtx_stack_c::get());
    i_this->mpMorf->calc();

    for (s32 i = 0; i < 4; i++) {
        i_this->m348[i].SetC(i_this->m2DC[i]);
        switch (i) {
        case 0:
            i_this->m348[i].SetR(220.0f);
            break;

        case 1:
            i_this->m348[i].SetR(180.0f);
            break;

        case 2:
            i_this->m348[i].SetR(120.0f);
            break;

        case 3:
            i_this->m348[i].SetR(70.0f);
            break;
        }
        dComIfG_Ccsp()->Set(&i_this->m348[i]);
    }
#if VERSION > VERSION_DEMO
    body_atari_check(i_this);
#endif
    return TRUE;
}

/* 000008D4-000008DC       .text daSk_IsDelete__FP8sk_class */
static BOOL daSk_IsDelete(sk_class*) {
    return TRUE;
}

/* 000008DC-0000090C       .text daSk_Delete__FP8sk_class */
static BOOL daSk_Delete(sk_class* i_this) {
    dComIfG_resDeleteDemo(&i_this->mPhase, "Sk");
    return TRUE;
}

/* 0000090C-00000A6C       .text useHeapInit__FP10fopAc_ac_c */
static BOOL useHeapInit(fopAc_ac_c* a_this) {
    sk_class* i_this = (sk_class*)a_this;
    J3DModelData* pModelData = (J3DModelData*)dComIfG_getObjectRes("Sk", dRes_INDEX_SK_BDL_TURU_00_e);

    i_this->mpMorf = new mDoExt_McaMorf(pModelData, NULL, NULL, NULL, J3DFrameCtrl::EMode_NULL, 1.0f, 0, -1, 1, NULL, 0x80000, 0x11000022);
    if (i_this->mpMorf == NULL || i_this->mpMorf->getModel() == NULL) {
        return FALSE;
    }

    i_this->mpMorf->getModel()->setUserArea((u32)i_this);

    for (u16 i = 0; i < i_this->mpMorf->getModel()->getModelData()->getJointNum(); i++) {
        i_this->mpMorf->getModel()->getModelData()->getJointNodePointer(i)->setCallBack(nodeCallBack);
    }

#if VERSION > VERSION_DEMO
    static Vec sph_offset = {0.0f, 0.0f, 0.0f};
    static __jnt_hit_data_c search_data[] = {
        {
            /* mShapeType  */ JntHitType_SPH_DELETE_e,
            /* mJointIndex */ TURU_00_JNT_BO03_e,
            /* mRadius     */ 140.0f,
            /* mpOffsets   */ &sph_offset,
        },
        {
            /* mShapeType  */ JntHitType_SPH_DELETE_e,
            /* mJointIndex */ TURU_00_JNT_BO02_e,
            /* mRadius     */ 90.0f,
            /* mpOffsets   */ &sph_offset,
        },
        {
            /* mShapeType  */ JntHitType_SPH_DELETE_e,
            /* mJointIndex */ TURU_00_JNT_BO01_e,
            /* mRadius     */ 50.0f,
            /* mpOffsets   */ &sph_offset,
        },
    };

    i_this->m924 = JntHit_create(i_this->mpMorf->getModel(), search_data, ARRAY_SIZE(search_data));
    if (i_this->m924 != NULL) {
        i_this->actor.jntHit = i_this->m924;
    } else {
        return FALSE;
    }
#endif

    return TRUE;
}

/* 00000A6C-00000E30       .text daSk_Create__FP10fopAc_ac_c */
static cPhs_State daSk_Create(fopAc_ac_c* a_this) {
    static dCcD_SrcSph body_co_sph_src = {
        // dCcD_SrcGObjInf
        {
            /* Flags             */ 0,
            /* SrcObjAt  Type    */ AT_TYPE_UNK2000,
            /* SrcObjAt  Atp     */ 1,
            /* SrcObjAt  SPrm    */ cCcD_AtSPrm_Set_e | cCcD_AtSPrm_GrpAll_e,
            /* SrcObjTg  Type    */ AT_TYPE_ALL & ~AT_TYPE_BOOMERANG & ~AT_TYPE_WATER & ~AT_TYPE_UNK20000 & ~AT_TYPE_WIND & ~AT_TYPE_UNK400000 & ~AT_TYPE_LIGHT,
            /* SrcObjTg  SPrm    */ cCcD_TgSPrm_Set_e | cCcD_TgSPrm_IsEnemy_e,
            /* SrcObjCo  SPrm    */ 0,
            /* SrcGObjAt Se      */ dCcG_SE_UNK6,
            /* SrcGObjAt HitMark */ dCcG_AtHitMark_None_e,
            /* SrcGObjAt Spl     */ dCcG_At_Spl_UNK9,
            /* SrcGObjAt Mtrl    */ 0,
            /* SrcGObjAt SPrm    */ 0,
            /* SrcGObjTg Se      */ 0,
            /* SrcGObjTg HitMark */ 0,
            /* SrcGObjTg Spl     */ dCcG_Tg_Spl_UNK0,
            /* SrcGObjTg Mtrl    */ 0,
            /* SrcGObjTg SPrm    */ 0,
            /* SrcGObjCo SPrm    */ 0,
        },
        // cM3dGSphS
        {{
            /* Center */ {0.0f, 0.0f, 0.0f},
            /* Radius */ 15.0f,
        }},
    };

    sk_class* i_this = (sk_class*)a_this;
    fopAcM_ct_Retail(a_this, sk_class);
    cPhs_State PVar2 = dComIfG_resLoad(&i_this->mPhase, "Sk");
    if (PVar2 == cPhs_COMPLEATE_e) {
        fopAcM_ct_Demo(a_this, sk_class);
        i_this->m2B4 = fopAcM_GetParam(i_this);
        if (i_this->m2B4 == 0xff) {
            i_this->m2B4 = 0;
        }

        i_this->m2B5 = fopAcM_GetParam(i_this) >> 0x18;
        if (i_this->m2B5 != 0xff && fopAcM_isSwitch(a_this, i_this->m2B5)) {
            return cPhs_ERROR_e;
        }

#if VERSION == VERSION_DEMO
        u32 heapSize = 0xC80;
#else
        u32 heapSize = 0xD00;
#endif
        if (!fopAcM_entrySolidHeap(a_this, useHeapInit, heapSize)) {
            return cPhs_ERROR_e;
        }

        fopAcM_SetMtx(a_this, i_this->mpMorf->getModel()->getBaseTRMtx());
        fopAcM_SetMin(a_this, -800.0f, -200.0f, -400.0f);
        fopAcM_SetMax(a_this, 500.0f, 1000.0f, 1000.0f);
        a_this->attention_info.flags = 0;
        i_this->m2BE = (u8)fopAcM_GetID(a_this) << 0xCu;

        for (s32 i = 1; i < 4; i++) {
            i_this->m2C2[i].x = cM_ssin(i_this->m2BE * 2000 + (i * 20000)) * 2000.0f;
            i_this->m2C2[i].y = cM_ssin(i_this->m2BE * 1300 + (i * 12000)) * 2000.0f;
            i_this->m2C2[i].z = cM_ssin(i_this->m2BE * 1000 + (i * 15000)) * 4000.0f;
        }

        J3DModel* model = i_this->mpMorf->getModel();
        model->setBaseScale(a_this->scale);
        mDoMtx_stack_c::transS(a_this->current.pos.x, a_this->current.pos.y, a_this->current.pos.z);
        i_this->mStts.Init(0x32, 0, a_this);
#if VERSION > VERSION_DEMO
        i_this->m2C0 = 8;
#endif
        mDoMtx_stack_c::YrotM(a_this->current.angle.y);
        mDoMtx_stack_c::XrotM(a_this->current.angle.x);
        mDoMtx_stack_c::ZrotM(a_this->current.angle.z);
        model->setBaseTRMtx(mDoMtx_stack_c::get());
        i_this->mpMorf->calc();

        for (s32 i = 0; i <= 4; i++) {
            i_this->m348[i].Set(body_co_sph_src);
            i_this->m348[i].SetStts(&i_this->mStts);
            dComIfG_Ccsp()->Set(&i_this->m348[i]);
        }
    }
    return PVar2;
}

static actor_method_class l_daSk_Method = {
    (process_method_func)daSk_Create,
    (process_method_func)daSk_Delete,
    (process_method_func)daSk_Execute,
    (process_method_func)daSk_IsDelete,
    (process_method_func)daSk_Draw,
};

actor_process_profile_definition g_profile_SK = {
    /* Layer ID     */ fpcLy_CURRENT_e,
    /* List ID      */ 0x0003,
    /* List Prio    */ fpcPi_CURRENT_e,
    /* Proc Name    */ fpcNm_SK_e,
    /* Proc SubMtd  */ &g_fpcLf_Method.base,
    /* Size         */ sizeof(sk_class),
    /* Size Other   */ 0,
    /* Parameters   */ 0,
    /* Leaf SubMtd  */ &g_fopAc_Method.base,
    /* Draw Prio    */ fpcDwPi_SK_e,
    /* Actor SubMtd */ &l_daSk_Method,
    /* Status       */ fopAcStts_UNK40000_e,
    /* Group        */ fopAc_ACTOR_e,
    /* Cull Type    */ fopAc_CULLBOX_CUSTOM_e,
};
