
#include "kpa_14.h"

API_CALLABLE(N(GetFloorBelow)) {
    Bytecode* args = script->ptrReadPos;
    s32 outVar = *args++;

    evt_set_variable(script, outVar, gCollisionStatus.floorBelow);
    return ApiStatus_DONE2;
}

API_CALLABLE(N(AddPlatformPushVelocity)) {
    Bytecode* args = script->ptrReadPos;
    s32 velX = evt_get_variable(script, *args++);
    s32 floor = evt_get_variable(script, *args++);
    s32 temp_a0 = evt_get_variable(script, *args++);
    PlayerStatus* playerStatus = &gPlayerStatus;

    if (gCollisionStatus.currentFloor == floor || gCollisionStatus.lastTouchedFloor == floor) {
        if (playerStatus->actionState != ACTION_STATE_TORNADO_JUMP &&
            playerStatus->actionState != ACTION_STATE_SPIN_JUMP &&
            temp_a0 == 0)
        {
            playerStatus->pushVelocity.x = velX;
        }
    }
    if (gPlayerData.currentPartner != PARTNER_NONE) {
        Npc* partner = get_npc_unsafe(NPC_PARTNER);

        if (partner->currentFloor == floor) {
            partner->pos.x += velX;
        }
    }
    return ApiStatus_DONE2;
}

API_CALLABLE(N(IsPartnerLakilester)) {
    if (gPartnerActionStatus.actingPartner == PARTNER_LAKILESTER) {
        script->varTable[9] = TRUE;
    } else {
        script->varTable[9] = FALSE;
    }
    return ApiStatus_DONE2;
}

EvtScript N(EVS_SetupPlatforms) = {
    EVT_SET_GROUP(EVT_GROUP_0B)
    EVT_CALL(ParentColliderToModel, COLLIDER_o852, MODEL_o860)
    EVT_SET(LVar0, 0)
    EVT_SET(LVar3, 0)
    EVT_LOOP(0)
        EVT_CALL(MakeLerp, LVar0, 175, 175, EASING_LINEAR)
        EVT_LOOP(0)
            EVT_CALL(UpdateLerp)
            EVT_CALL(TranslateGroup, MODEL_dai01, LVar0, 0, 0)
            EVT_CALL(UpdateColliderTransform, COLLIDER_o852)
            EVT_SET(LVar4, LVar0)
            EVT_SUB(LVar4, LVar3)
            EVT_SET(LVar3, LVar0)
            EVT_CALL(N(AddPlatformPushVelocity), LVar4, COLLIDER_o852, MV_TakingLavaFallDamage)
            EVT_IF_GT(LVar0, 145)
                EVT_CALL(N(IsPartnerLakilester))
                EVT_IF_EQ(LVar9, 1)
                    EVT_CALL(N(GetFloorBelow), LVar2)
                    EVT_IF_EQ(LVar2, COLLIDER_o858)
                        EVT_BREAK_LOOP
                    EVT_END_IF
                EVT_END_IF
            EVT_END_IF
            EVT_WAIT(1)
            EVT_IF_EQ(LVar1, 0)
                EVT_BREAK_LOOP
            EVT_END_IF
        EVT_END_LOOP
        EVT_WAIT(20)
        EVT_CALL(MakeLerp, LVar0, 0, 175, EASING_LINEAR)
        EVT_LOOP(0)
            EVT_CALL(UpdateLerp)
            EVT_CALL(TranslateGroup, MODEL_dai01, LVar0, 0, 0)
            EVT_CALL(UpdateColliderTransform, COLLIDER_o852)
            EVT_SET(LVar4, LVar0)
            EVT_SUB(LVar4, LVar3)
            EVT_SET(LVar3, LVar0)
            EVT_CALL(N(AddPlatformPushVelocity), LVar4, COLLIDER_o852, MV_TakingLavaFallDamage)
            EVT_IF_LT(LVar0, 30)
                EVT_CALL(N(IsPartnerLakilester))
                EVT_IF_EQ(LVar9, 1)
                    EVT_CALL(N(GetFloorBelow), LVar2)
                    EVT_IF_EQ(LVar2, COLLIDER_o857)
                        EVT_BREAK_LOOP
                    EVT_END_IF
                EVT_END_IF
            EVT_END_IF
            EVT_WAIT(1)
            EVT_IF_EQ(LVar1, 0)
                EVT_BREAK_LOOP
            EVT_END_IF
        EVT_END_LOOP
        EVT_WAIT(20)
    EVT_END_LOOP
    EVT_RETURN
    EVT_END
};
