--任务副本1
--进入副本→播MOV→刷出一波怪→击杀后再刷出一波怪→击杀后刷出一个boss→击杀boss后完成

x040018_g_scriptId = 040018

--副本参数
x040018_g_CSParamIdxCurStage = 0              --大阶段
x040018_g_CSParamIdxCurSubStage = 1           --小阶段
x040018_g_CSParam1 = 2
x040018_g_CSParam2 = 3   -- 7为当前阶段
x040018_g_CSParamAnimationID = 3002           --mov动画ID

x040018_g_MonstorList = {}
x040018_g_MonstorList[1] = { 1003118, 1003118, 1003118, 1003118, 1003118, 1003118, 1003118, 1003118, 1003118, 1003118 }  --小怪
x040018_g_MonstorList[2] = { 1003119, 1003119, 1003119, 1003119, 1003119, 1003119, 1003119, 1003119, 1003119, 1003119 }  --小怪
x040018_g_MonstorList[3] = { 1003110 }

x040018_g_TargetList = {}                      --目标
x040018_g_TargetList[1] = { 1003118 }  --第一阶段目标1
x040018_g_TargetList[2] = { 1003119 }   --第2阶段目标1
x040018_g_TargetList[3] = { 1003110 }   --第3阶段目标1
x040018_g_TargetList[4] = {}

x040018_g_FirstMissionId = 2021    -- 第一个任务
x040018_g_SecMissionId = 2022 -- 第2个任务
x040018_g_SecMissionId2 = 2023 -- 第3个任务

x040018_g_MonstorPosList = {}
x040018_g_MonstorPosList[1] = { { 54.91, 63.14, -1.57 }, { 61.23, 63.37, -1.57 }, { 51.29, 63.64, -1.57 }, { 68.13, 61.42, -1.57 }, { 48.54, 61.53, -1.57 }, { 65.3, 64.03, -1.57 }, { 62.67, 65.63, -1.57 }, { 59.43, 66.08, -1.57 }, { 56.46, 66.05, -1.57 }, { 53.57, 65.66, -1.57 } } --小怪坐标
x040018_g_MonstorPosList[2] = { { 54.91, 63.14, -1.57 }, { 61.23, 63.37, -1.57 }, { 51.29, 63.64, -1.57 }, { 68.13, 61.42, -1.57 }, { 48.54, 61.53, -1.57 }, { 65.3, 64.03, -1.57 }, { 62.67, 65.63, -1.57 }, { 59.43, 66.08, -1.57 }, { 56.46, 66.05, -1.57 }, { 53.57, 65.66, -1.57 } }  --小怪坐标
x040018_g_MonstorPosList[3] = { { 58.6, 75.22, -1.57 } }

function x040018_OnCopySceneTimeOut(scene)
    ClearPlayer(scene)
end
function x040018_OnCopySceneOpen(scene)
    AddCSMission(scene, x040018_g_FirstMissionId)
    AddCSMission(scene, x040018_g_SecMissionId)
    AddCSMission(scene, x040018_g_SecMissionId2)
end
function x040018_ClearAllMonster(scene)
    DeleHostileMonster(scene)
end
function x040018_StageControl(scene, nstage)
    SetCopySceneParam(scene, 2, 0)
    SetCopySceneParam(scene, 3, 0)
    SetCopySceneParam(scene, 4, 0)
    SetCopySceneParam(scene, 7, nstage)
    local nUserCount = GetSceneUserCount(scene)
    for j = 0, nUserCount - 1 do
        local nUserObjId = GetUserObjIdByIdx(scene, j)
        SynCsStageData(scene, nUserObjId)
    end
end
function x040018_OnObjEnter(scene, objId)
    CopySceneNPC(scene, 1003020, 1000002, 8, 253, 99, "南凰澤兒")--场景、角色ID、CharModel表ID、势力、出现的场景SceneID、等级、名称
    SetCopySceneParam(scene, x040018_g_CSParamIdxCurStage, 0)
    SetCopySceneParam(scene, x040018_g_CSParamIdxCurSubStage, 0)
    SetCopySceneParam(scene, x040018_g_CSParam1, 0)
    SetCopySceneParam(scene, x040018_g_CSParam2, 0)
    x040018_ClearAllMonster(scene)
    SetCopySceneParam(scene, 7, 0)
    SynCsStageData(scene, objId, true)
end

function x040018_OnDieCountDown1(scene, rolebaseId, stage)
    local TargetId = x040018_g_TargetList[stage + 1][1]
    if (TargetId == rolebaseId) then
        local OldValue = GetCopySceneParam(scene, x040018_g_CSParam1)
        SetCopySceneParam(scene, x040018_g_CSParam1, OldValue + 1)
        local nUserCount = GetSceneUserCount(scene)
        for i = 0, nUserCount - 1 do
            local nUserObjId = GetUserObjIdByIdx(scene, i)
            SynCsStageData(scene, nUserObjId)
        end
    end
end

--死亡回调
function x040018_OnObjDie(scene, objId, rolebaseId, killerId)
    local nCurStage = GetCopySceneParam(scene, x040018_g_CSParamIdxCurStage)
    local oldStage = GetCopySceneParam(scene, 7)
    x040018_OnDieCountDown1(scene, rolebaseId, oldStage)
    if (rolebaseId == x040018_g_MonstorList[3][1]) then
        SetCopySceneSuccess(scene)
        x040018_StageControl(scene, 3)
        return
    end
    local MonCount = GetHostileMonsterCount(scene)
    if MonCount == 0 then
        SetCopySceneParam(scene, x040018_g_CSParamIdxCurStage, nCurStage + 1)
        SetCopySceneParam(scene, x040018_g_CSParamIdxCurSubStage, 0)
        x040018_CreateMonster(scene, nCurStage + 1, 1)
    end
end

--动画/对话回调
function x040018_OnStoryEventEnd(scene, nStoryId, nAnimationId)
    if nAnimationId == x040018_g_CSParamAnimationID then
        SetCopySceneParam(scene, x040018_g_CSParamIdxCurStage, 1)
        SetCopySceneParam(scene, x040018_g_CSParamIdxCurSubStage, 0)
        x040018_CreateMonster(scene, 1, 1)
    end
end

--副本Tick
function x040018_OnCopySceneTick(scene, tickCount)
    local curStage = GetCopySceneParam(scene, x040018_g_CSParamIdxCurStage)
    local curSubStage = GetCopySceneParam(scene, x040018_g_CSParamIdxCurSubStage)
    SetCopySceneParam(scene, x040018_g_CSParamIdxCurSubStage, curSubStage + 1)
    if curStage == 0 then
        x040018_Stage_0(scene, curStage, curSubStage + 1)
        if curSubStage >= 20 then
            SetCopySceneParam(scene, x040018_g_CSParamIdxCurStage, 1)
            SetCopySceneParam(scene, x040018_g_CSParamIdxCurSubStage, 0)
            x040018_CreateMonster(scene, 1, 1)
        end
    end
end


--第0阶段 播放MOV 刷第一波小怪
function x040018_Stage_0(scene, stage, substage)
    if substage == 1 then
        StoryAnimation(scene, x040018_g_CSParamAnimationID)
    end
end

function x040018_CreateMonster(scene, stage, substage)
    if substage == 1 then
        OnCSMissionStage(scene, stage - 1)
        TellPlayerAutoCombat(scene)
        x040018_StageControl(scene, stage - 1)
        local count = #x040018_g_MonstorList[stage]
        for i = 1, count do
            CreateMonster(scene, x040018_g_MonstorList[stage][i], x040018_g_MonstorPosList[stage][i][3], x040018_g_MonstorPosList[stage][i][1], x040018_g_MonstorPosList[stage][i][2])
        end
    end
end