--任务副本1
--接受任务220→进入副本853→刷新小怪→厄化庞弘出场动画→BOSS刷新→击杀BOSS→副本结束

x040016_g_scriptId = 040016

--副本参数
x040016_g_CSParamIdxCurStage = 0              --大阶段
x040016_g_CSParamIdxCurSubStage = 1           --小阶段
x040016_g_CSParam1 = 2
x040016_g_CSParam2 = 3   -- 7为当前阶段
x040016_g_CSParamAnimationID = 4001           --mov动画ID

x040016_g_MonstorList = {}
x040016_g_MonstorList[1] = { 1004109, 1004109, 1004109, 1004109, 1004109, 1004109, 1004109, 1004109, 1004109, 1004109, 1004109, 1004109 }  --小怪
x040016_g_MonstorList[2] = { 1004110 }

x040016_g_TargetList = {}                      --目标
x040016_g_TargetList[1] = { 1004109 }  --第一阶段目标1
x040016_g_TargetList[2] = { 1004110 }  --第2阶段目标1
x040016_g_TargetList[3] = {}

x040016_g_FirstMissionId1 = 2031    -- 第一个任务
x040016_g_SecMissionId2 = 2032 -- 第4个任务

x040016_g_MonstorPosList = {}
x040016_g_MonstorPosList[1] = { { 50.48, 27.35, -1.57 }, { 47.45, 28.76, -1.57 }, { 52.91, 28.91, -1.57 }, { 43.96, 28.1, -1.57 }, { 56.15, 28.06, -1.57 }, { 45.44, 30.73, -1.57 }, { 54.31, 30.91, -1.57 }, { 46.21, 25.54, -1.57 }, { 52.91, 25.8, -1.57 }, { 48.54, 31.39, -1.57 }, { 51.03, 31.42, -1.57 }, { 50.04, 24.02, -1.57 }} --小怪坐标
x040016_g_MonstorPosList[2] = { { 49.70, 55.86, -1.57 } }

function x040016_OnCopySceneTimeOut(scene)
    ClearPlayer(scene)
end

function x040016_OnCopySceneOpen(scene)
    AddCSMission(scene, x040016_g_FirstMissionId1)
    AddCSMission(scene, x040016_g_SecMissionId2)
end

function x040016_ClearAllMonster(scene)
    DeleHostileMonster(scene)
end
function x040016_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 x040016_OnObjEnter(scene, objId)
    SetCopySceneParam(scene, x040016_g_CSParamIdxCurStage, 0)
    SetCopySceneParam(scene, x040016_g_CSParamIdxCurSubStage, 0)
    SetCopySceneParam(scene, x040016_g_CSParam1, 0)
    SetCopySceneParam(scene, x040016_g_CSParam2, 0)
    x040016_ClearAllMonster(scene)
    SetCopySceneParam(scene, 7, 0)
    SynCsStageData(scene, objId, true)
end

function x040016_OnDieCountDown1(scene, rolebaseId, stage)
    local TargetId = x040016_g_TargetList[stage + 1][1]
    if (TargetId == rolebaseId) then
        local OldValue = GetCopySceneParam(scene, x040016_g_CSParam1)
        SetCopySceneParam(scene, x040016_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 x040016_OnObjDie(scene, objId, rolebaseId, killerId)
    local nCurStage = GetCopySceneParam(scene, x040016_g_CSParamIdxCurStage)
    local MonCount = GetHostileMonsterCount(scene)
    local oldStage = GetCopySceneParam(scene, 7)
    x040016_OnDieCountDown1(scene, rolebaseId, oldStage)
    if MonCount == 0 then
        if (rolebaseId == x040016_g_MonstorList[2][1]) then
            SetCopySceneSuccess(scene)
            x040016_StageControl(scene, oldStage + 1)
        elseif rolebaseId ~= x040016_g_MonstorList[2][1] then
            x040016_Stage_1(scene)
            x040016_StageControl(scene, oldStage + 1)
            x040016_CreateMonster(scene, nCurStage + 1)
        end
        SetCopySceneParam(scene, x040016_g_CSParamIdxCurStage, nCurStage + 1)
        SetCopySceneParam(scene, x040016_g_CSParamIdxCurSubStage, 0)
    end
end

--动画/对话回调
function x040016_OnStoryEventEnd(scene, nStoryId, nAnimationId)
    if nAnimationId == x040016_g_CSParamAnimationID then
        TellPlayerAutoCombat(scene, true)
    end
end

--副本Tick
function x040016_OnCopySceneTick(scene, tickCount)
    local curStage = GetCopySceneParam(scene, x040016_g_CSParamIdxCurStage)
    local curSubStage = GetCopySceneParam(scene, x040016_g_CSParamIdxCurSubStage)
    SetCopySceneParam(scene, x040016_g_CSParamIdxCurSubStage, curSubStage + 1)
    if curStage == 0 then
        SetCopySceneParam(scene, x040016_g_CSParamIdxCurStage, curStage + 1)
        SetCopySceneParam(scene, x040016_g_CSParamIdxCurSubStage, 0)
        x040016_Stage_0(scene, 1, 1)
        x040016_CreateMonster(scene, 1)
    end
end

function x040016_Stage_0(scene, stage, substage)
    if substage == 1 then
        OnCSMissionStage(scene, stage - 1)
        TellPlayerAutoCombat(scene)
        x040016_StageControl(scene, 0)
    end
end

function x040016_CreateMonster(scene, stage)
    local count = #x040016_g_MonstorList[stage]
    for i = 1, count do
        CreateMonster(scene, x040016_g_MonstorList[stage][i], x040016_g_MonstorPosList[stage][i][3], x040016_g_MonstorPosList[stage][i][1], x040016_g_MonstorPosList[stage][i][2])
    end
end

--第1阶段 播放MOV
function x040016_Stage_1(scene)
    TellPlayerAutoCombat(scene, false)
    StoryAnimation(scene, x040016_g_CSParamAnimationID)
end