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

#include "d/dolzel_rel.h" // IWYU pragma: keep
#include "d/actor/d_a_kytag02.h"
#include "d/d_com_inf_game.h"
#include "d/d_kankyo_rain.h"
#include "f_op/f_op_actor_mng.h"

/* 00000078-000000C0       .text set_path_info__FP10fopAc_ac_c */
dPath* set_path_info(fopAc_ac_c* i_this) {
    dPath* ret = NULL;
    if (((fopAcM_GetParam(i_this) >> 16) & 0xFF) != 0xFF)
        ret = dPath_GetRoomPath(((fopAcM_GetParam(i_this) >> 16) & 0xFF), fopAcM_GetRoomNo(i_this));
    return ret;
}

/* 000000C0-000000F0       .text set_next_path_info__FP13kytag02_classP5dPath */
dPath* set_next_path_info(kytag02_class* i_this, dPath* path) {
    fopAc_ac_c* actor = &i_this->actor;
    return dPath_GetNextRoomPath(path, fopAcM_GetRoomNo(actor));
}

/* 000000F0-0000017C       .text get_railwind_vec__FP5dPathi */
cXyz get_railwind_vec(dPath* path, int i_no) {
    cXyz p0;
    p0.x = path->m_points[i_no].m_position.x;
    p0.y = path->m_points[i_no].m_position.y;
    p0.z = path->m_points[i_no].m_position.z;
    cXyz p1;
    p1.x = path->m_points[i_no + 1].m_position.x;
    p1.y = path->m_points[i_no + 1].m_position.y;
    p1.z = path->m_points[i_no + 1].m_position.z;
    cXyz ret;
    dKyr_get_vectle_calc(&p0, &p1, &ret);
    return ret;
}

/* 0000017C-000002E8       .text get_nearpos_rail__FP13kytag02_classP5dPathP4cXyzPi */
dPath* get_nearpos_rail(kytag02_class* i_this, dPath* i_path, cXyz* pos, int* i_no) {
    dPath* path;
    dPath* bestPath;
    f32 best;
    int bestIdx;

    bestIdx = 0;
    path = i_path;
    best = 1000000000.0f; // This is not G_CM3D_F_INF
    bestPath = i_path;

    while (true) {
        for (s32 i = 0; i < path->m_num; i++) {
            f32 dx = path->m_points[i].m_position.x - pos->x;
            f32 dz = path->m_points[i].m_position.z - pos->z;
            f32 dist = std::sqrtf(dx*dx + dz*dz);
            if (best > dist) {
                bestPath = path;
                bestIdx = i;
                best = dist;
            }
        }

        if (path->m_nextID == 0xFFFF)
            break;
        path = set_next_path_info(i_this, path);
    }
    if (bestIdx == i_path->m_num - 1)
        bestIdx--;
    *i_no = bestIdx;
    return bestPath;
}

/* 000002E8-000003D4       .text windtag_move__FP13kytag02_class */
void windtag_move(kytag02_class* i_this) {
    fopAc_ac_c* player = dComIfGp_getPlayer(0);
    int i_no;

    if (i_this->mpPath != NULL) {
        dPath* path = get_nearpos_rail(i_this, i_this->mpPath, &player->current.pos, &i_no);
        i_this->mWindVec = get_railwind_vec(path, i_no);
        g_env_light.mWind.mpWindVecOverride = &i_this->mWindVec;
        dPnt* pnt = &path->m_points[i_no];
        u32 strength = (s32)pnt->mArg3 != 0xFF ? pnt->mArg3 : path->mArg0;
        g_env_light.mWind.mWindStrengthOverride = (f32)strength / 100.0f;
    }
}

/* 000003D4-000003DC       .text daKytag02_Draw__FP13kytag02_class */
static BOOL daKytag02_Draw(kytag02_class* i_this) {
    return TRUE;
}

/* 000003DC-00000400       .text daKytag02_Execute__FP13kytag02_class */
static BOOL daKytag02_Execute(kytag02_class* i_this) {
    windtag_move(i_this);
    return TRUE;
}

/* 00000400-00000408       .text daKytag02_IsDelete__FP13kytag02_class */
static BOOL daKytag02_IsDelete(kytag02_class* i_this) {
    UNUSED(i_this);
    return TRUE;
}

/* 00000408-00000420       .text daKytag02_Delete__FP13kytag02_class */
static BOOL daKytag02_Delete(kytag02_class* i_this) {
    g_env_light.mWind.mpWindVecOverride = NULL;
    return TRUE;
}

/* 00000420-0000047C       .text daKytag02_Create__FP10fopAc_ac_c */
static cPhs_State daKytag02_Create(fopAc_ac_c* i_this) {
    kytag02_class* a_this = (kytag02_class*)i_this;
    fopAcM_ct(i_this, kytag02_class);
    a_this->mpPath = set_path_info(i_this);
    return cPhs_COMPLEATE_e;
}

static actor_method_class l_daKytag02_Method = {
    (process_method_func)daKytag02_Create,
    (process_method_func)daKytag02_Delete,
    (process_method_func)daKytag02_Execute,
    (process_method_func)daKytag02_IsDelete,
    (process_method_func)daKytag02_Draw,
};

actor_process_profile_definition g_profile_KYTAG02 = {
    /* Layer ID     */ fpcLy_CURRENT_e,
    /* List ID      */ 0x0007,
    /* List Prio    */ fpcPi_CURRENT_e,
    /* Proc Name    */ fpcNm_KYTAG02_e,
    /* Proc SubMtd  */ &g_fpcLf_Method.base,
    /* Size         */ sizeof(kytag02_class),
    /* Size Other   */ 0,
    /* Parameters   */ 0,
    /* Leaf SubMtd  */ &g_fopAc_Method.base,
    /* Draw Prio    */ fpcDwPi_KYTAG02_e,
    /* Actor SubMtd */ &l_daKytag02_Method,
    /* Status       */ fopAcStts_UNK4000_e | fopAcStts_UNK40000_e,
    /* Group        */ fopAc_ACTOR_e,
    /* Cull Type    */ fopAc_CULLBOX_0_e,
};
