--试练Boss1
--进入副本→刷五波波小怪→副本完成→传出副本

x050010_g_scriptId = 050010

--副本参数
x050010_g_CSParamIdxCurStage = 0              --大阶段
x050010_g_CSParamIdxCurSubStage = 1           --小阶段
x050010_g_CSParam1 = 5
x050010_g_CSParam2 = 6

x050010_g_CSParamIdxHasBox = 12 -- 该次副本是否有宝箱
x050010_g_CSParamAnimationID = 5033           --mov动画ID  暂时不用

x050010_g_MonstorList = {}
x050010_g_MonstorList[1] = { 498500, 498501, 498500, 498500, 498500, 498500, 498500, 498500, 498500, 498500, 498500, 498500, 498500, 498501 }  --小怪第一波
x050010_g_MonstorList[2] = { 498502, 498502, 498502, 498502, 498502, 498502, 498502, 498502, 498502, 498502, 498502, 498502, 498503, 498503 }  --小怪第二波
x050010_g_MonstorList[3] = { 498504, 498504, 498504, 498504, 498504, 498504, 498504, 498504, 498504, 498504, 498504, 498504, 498505, 498505 }  --小怪第三波
x050010_g_MonstorList[4] = { 498506, 498506, 498506, 498506 }  --小怪第四波
x050010_g_MonstorList[5] = { 498507 }  --小怪第五波   麻烦把boss放在第一个

x050010_g_MonstorList[6] = { 498509, 498509, 498509, 498508, 498508, 498508, 498508, 498508, 498508, 498508, 498508, 498508 }

x050010_g_MonstorPosList = {}
x050010_g_MonstorPosList[1] = { { 39.43, 21.08, -3.8 }, { 42.06, 22.94, -3.8 }, { 39.03, 16.08, -3.8 }, { 43.69, 14.66, -3.8 }, { 43.89, 18.75, -3.8 }, { 48.11, 22.26, -3.8 }, { 50, 17.2, -3.8 }, { 48.68, 13.35, -3.8 }, { 53.06, 17.67, -3.8 }, { 51.87, 21.23, -3.8 }, { 47.51, 17.92, -3.8 }, { 44.85, 24.58, -3.8 }, { 44.7, 21.37, -3.8 }, { 41.34, 11.75, -3.8 } } --小怪坐标 和上边对应
x050010_g_MonstorPosList[2] = { { 76.5, 17.24, -3.13 }, { 76.9, 22.24, -3.13 }, { 79.53, 24.1, -3.13 }, { 81.16, 15.82, -3.13 }, { 81.36, 19.91, -3.13 }, { 85.58, 23.42, -3.13 }, { 87.47, 18.36, -3.13 }, { 86.15, 14.51, -3.13 }, { 90.53, 18.83, -3.13 }, { 89.34, 22.39, -3.13 }, { 84.98, 19.08, -3.13 }, { 82.32, 25.74, -3.13 }, { 82.17, 22.53, -3.13 }, { 78.81, 12.91, -3.13 } }
x050010_g_MonstorPosList[3] = { { 92.86, 47.05, -1.51 }, { 87.7, 51.52, -1.51 }, { 88.1, 56.52, -1.51 }, { 89.76, 54.08, -1.51 }, { 92.36, 50.1, -1.51 }, { 92.56, 54.19, -1.51 }, { 96.78, 57.7, -1.51 }, { 98.67, 52.64, -1.51 }, { 95.62, 49.1, -1.51 }, { 97.94, 55.71, -1.51 }, { 96.18, 53.36, -1.51 }, { 94.24, 52.03, -1.51 }, { 93.37, 56.81, -1.51 }, { 89.59, 49.03, -1.51 } }
x050010_g_MonstorPosList[4] = { { 88.65, 72.15, -1.51 }, { 95.16, 72.12, -1.51 }, { 88, 66.8, -1.51 }, { 95.5, 66.8, -1.51 } }
x050010_g_MonstorPosList[5] = { { 92.09, 99.91, -1.57 } }
x050010_g_MonstorPosList[6] = { { 91.23, 116.29, 1.57 }, { 99.09, 102.9, 1.57 }, { 84.8, 96.45, 1.57 }, { 90.78, 100.93, 1.57 }, { 90.17, 106.74, 1.57 }, { 84.11, 104.38, 1.57 }, { 90.03, 93.86, 1.57 }, { 96.74, 96.45, 1.57 }, { 94.44, 90.63, 1.57 }, { 96.21, 107.01, 1.57 }, { 91.88, 111.76, 1.57 }, { 94.1, 120.13, 1.57 } }

x050010_g_TargetList = {}                      --目标
x050010_g_TargetList[1] = { { 498500, 498501 } }  --{{第1阶段目标},{第1阶段目标2}}
x050010_g_TargetList[2] = { { 498502, 498503 } }  --{{第2阶段目标1},{第2阶段目标2}}
x050010_g_TargetList[3] = { { 498504, 498505 } }
x050010_g_TargetList[4] = { { 498506 } }
x050010_g_TargetList[5] = { { 498507 } }
x050010_g_TargetList[6] = { { 498509 }, { 498508 } }


-- 下边为默认配置不需要策划进行配置

x050010_g_DeleListId = {}
x050010_g_DeleListId[1] = { 490010, 490010, 490011, 490011 }    -- 最后需要删除的怪物Id
x050010_g_BossNumEnumId = 987
x050010_g_BossNumCount = 3

x050010_g_BoxNumEnumId = 138
x050010_g_BoxNumCount = 3
x050010_g_BoxCountDown = 11  -- 刷宝箱之前的倒数时间（需要同时修改前端时间）

x050010_g_ClearBoxTime = 42 -- 清除宝箱怪物时间
x050010_g_BigBoxId = 498509   -- 大包厢ID
x050010_g_SmallBoxId = 498508   -- 小包厢ID

x050010_g_MovCdTime = 10

x050010_g_LevelAddBuff = {}
x050010_g_LevelAddBuff[1] = { -1, 30711, 30712, 30712, 30712 }

x050010_g_DropHp = {}   -- 宝箱掉落百分比血量，注意单调，由高到底，填写大于0的数 不包括0
x050010_g_DropHp[1] = { 90, 80, 70, 60, 40, 20 }

x050010_g_DropRew = {} -- 宝箱掉落血量百分比掉落大小宝箱控制 1 小宝箱 2 大宝箱  x050010_g_DropHp对应
x050010_g_DropRew[1] = { 1, 1, 1, 1, 1, 2 }

x050010_g_CSParamIdxBoxHpAward = 13 -- 该次副本血量奖励索引

function x050010_OnCopySceneOpen(scene)
    -- 更改寻路任务
    AddCSMission(scene, 1960)
    AddCSMission(scene, 1961)
    AddCSMission(scene, 1962)
    AddCSMission(scene, 1963)
    AddCSMission(scene, 1964)
end

function x050010_StageControl(scene, nstage)
    SetCopySceneParam(scene, 2, 0)
    SetCopySceneParam(scene, 3, 0)
    SetCopySceneParam(scene, 4, 0)
    SetCopySceneParam(scene, 5, 100)
    SetCopySceneParam(scene, 6, x050010_g_DropHp[1][1])
    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 x050010_OnDieCountDown1(scene, rolebaseId, stage)
    local count = #x050010_g_TargetList[stage + 1]
    for k = 1, count do
        local count2 = #x050010_g_TargetList[stage + 1][k]
        for j = 1, count2 do
            local TargetId = x050010_g_TargetList[stage + 1][k][j]
            if (TargetId == rolebaseId) then
                local OldValue = GetCopySceneParam(scene, 1 + k)
                SetCopySceneParam(scene, 1 + k, OldValue + 1)
                local nUserCount = GetSceneUserCount(scene)
                for i = 0, nUserCount - 1 do
                    local nUserObjId = GetUserObjIdByIdx(scene, i)
                    SynCsStageData(scene, nUserObjId)
                end
            end
        end
    end
end

function x050010_OnObjEnter(scene, objId)
    SynCsStageData(scene, objId, true)
    x050010_SyncBoxInfo(scene, objId)
end

function x050010_SyncBoxInfo(scene, objId)
    local CSevalue = GetCSEvaluaion(scene)
    local curStage = GetCopySceneParam(scene, x050010_g_CSParamIdxCurStage)
    local curSubStage = GetCopySceneParam(scene, x050010_g_CSParamIdxCurSubStage)
    if CSevalue == 0 and curStage == 5 and curSubStage > x050010_g_BoxCountDown and curSubStage <= x050010_g_ClearBoxTime then
        SyncSLBossBoxTime(scene, objId, x050010_g_ClearBoxTime - curSubStage, x050010_g_ClearBoxTime - x050010_g_BoxCountDown - 1)
    end
end

function x050010_OnBoxDie(scene, MonId, rolebaseId)
    local IsEff = false
    local nUserCount = GetSceneUserCount(scene)
    for i = 0, nUserCount - 1 do
        local nUserObjId = GetUserObjIdByIdx(scene, i)
        local Flag = false
        if (GetMissionMD(scene, nUserObjId, 4, x050010_g_BoxNumEnumId) % 100 <= x050010_g_BoxNumCount) then
            if rolebaseId == x050010_g_BigBoxId then
                Flag = CreateSLBossBox(scene, nUserObjId, true, MonId, true)
            elseif rolebaseId == x050010_g_SmallBoxId then
                Flag = CreateSLBossBox(scene, nUserObjId, false, MonId, true)
            end
        end
        if Flag == true then
            IsEff = true
        end
    end
    if IsEff == true then
        MonsterDropEff(scene, MonId)
    end
end

--死亡回调
function x050010_OnObjDie(scene, objId, rolebaseId, killerId)
    local nCurStage = GetCopySceneParam(scene, x050010_g_CSParamIdxCurStage)
    if nCurStage == 5 and rolebaseId ~= x050010_g_BigBoxId and rolebaseId ~= x050010_g_SmallBoxId then
        return    -- 不是宝箱怪物进入宝箱阶段的死亡回调直接return
    end
    local MonCount = GetHostileMonsterCount(scene)
    local oldStage = GetCopySceneParam(scene, 7)
    x050010_OnDieCountDown1(scene, rolebaseId, oldStage)
    if (rolebaseId == x050010_g_BigBoxId or rolebaseId == x050010_g_SmallBoxId) then
        x050010_OnBoxDie(scene, objId, rolebaseId)
    end
    if (rolebaseId == x050010_g_MonstorList[5][1]) then
        local IsEff = false
        AccoutByCopyScene(scene)
        DeleHostileMonster(scene)
        local nUserCount = GetSceneUserCount(scene)
        for i = 0, nUserCount - 1 do
            local nUserObjId = GetUserObjIdByIdx(scene, i)
            if (GetMissionMD(scene, nUserObjId, 4, x050010_g_BossNumEnumId) % 1000 <= x050010_g_BossNumCount) then
                local Flag = CreateDropBoxByShiLian(scene, nUserObjId, objId)
                print(Flag)
                if Flag == true then
                    IsEff = true
                end
            end
        end

        if IsEff == true then
            MonsterDropEff(scene, objId)
        end
        if GetCSEvaluaion(scene) ~= 0 then
            x050010_Succeed(scene)
        end
        SetCopySceneParam(scene, x050010_g_CSParamIdxCurStage, nCurStage + 1)
        SetCopySceneParam(scene, x050010_g_CSParamIdxCurSubStage, 0)
        if GetCSEvaluaion(scene) == 0 then
            local nUserCount = GetSceneUserCount(scene)
            for i = 0, nUserCount - 1 do
                local nUserObjId = GetUserObjIdByIdx(scene, i)
                CSEvalueEnd(scene, nUserObjId)
            end
            SetCopySceneParam(scene, x050010_g_CSParamIdxHasBox, 1)
        end
        return
    end
    if MonCount == 0 and nCurStage ~= 4 then
        SetCopySceneParam(scene, x050010_g_CSParamIdxCurStage, nCurStage + 1)
        SetCopySceneParam(scene, x050010_g_CSParamIdxCurSubStage, 0)

    end
end

--动画/对话回调
--副本Tick
function x050010_OnCopySceneTick(scene, tickCount)
    local curStage = GetCopySceneParam(scene, x050010_g_CSParamIdxCurStage)
    local curSubStage = GetCopySceneParam(scene, x050010_g_CSParamIdxCurSubStage)
    SetCopySceneParam(scene, x050010_g_CSParamIdxCurSubStage, curSubStage + 1)
    -- x050010_BoxHpTick(scene)
    if curStage < 5 then
        x050010_Stage_0(scene, curStage, curSubStage)
    elseif curStage == 5 then
        -- x050010_HpCountDown1(scene)
        local LastCSEvalue = GetCopySceneParam(scene, x050010_g_CSParamIdxHasBox)
        if LastCSEvalue == 0 then
            if GetCSEvaluaion(scene) ~= 0 then
                local nUserCount = GetSceneUserCount(scene)
                for i = 0, nUserCount - 1 do
                    local nUserObjId = GetUserObjIdByIdx(scene, i)
                    CSEvalueEnd(scene, nUserObjId)
                end
                x050010_Stage_1(scene, curStage, curSubStage)
            elseif GetCSEvaluaion(scene) == 0 then
                local nUserCount = GetSceneUserCount(scene)
                for i = 0, nUserCount - 1 do
                    local nUserObjId = GetUserObjIdByIdx(scene, i)
                    CSEvalueEnd(scene, nUserObjId)
                end
                SetCopySceneParam(scene, x050010_g_CSParamIdxHasBox, 1)
                x050010_Stage_Box(scene, curStage, curSubStage)
            end
        elseif LastCSEvalue ~= 0 then
            x050010_Stage_Box(scene, curStage, curSubStage)
        end
    elseif curStage == 6 then
        SetCopySceneSuccess(scene)
    end
end

function x050010_Stage_Eff(scene, stage)
    -- 更改特效ID
    if stage == 1 then
        ShowCopySceneGuideEffect(scene, 900101)
    elseif stage == 2 then
        ShowCopySceneGuideEffect(scene, 900201)
    elseif stage == 3 then
        ShowCopySceneGuideEffect(scene, 900301)
    elseif stage == 4 then
        ShowCopySceneGuideEffect(scene, 900401)
    end
end

--刷小怪阶段
function x050010_Stage_0(scene, stage, substage)
    if substage == 1 then
        x050010_StageControl(scene, stage)
        OnCSMissionStage(scene, stage)
        if stage ~= 0 then
            CSEvalueBeginNextStage(scene)
        end
        x050010_Stage_Eff(scene, stage)

        if stage == 4 then
            StoryAnimation(scene, 6050)
            SetCSEvaluateIsBossStage(scene, true)
        end
        local MonId
        local nUserCount = GetSceneUserCount(scene)
        if nUserCount < 1 then
            nUserCount = 1
        end
        local count = #x050010_g_MonstorList[stage + 1]
        for i = 1, count do
            if stage ~= 4 then
                MonId = CreateMonster(scene, x050010_g_MonstorList[stage + 1][i], x050010_g_MonstorPosList[stage + 1][i][3], x050010_g_MonstorPosList[stage + 1][i][1], x050010_g_MonstorPosList[stage + 1][i][2])
                SendImpactToUnit(scene, MonId, x050010_g_LevelAddBuff[1][nUserCount])
                SetHpFull(scene, MonId)
                UpdateObjHpMax(scene, MonId)
            end
        end
    elseif (substage == 2) and stage == 4 then
        local MonId
        local nUserCount = GetSceneUserCount(scene)
        for i = 0, nUserCount - 1 do
            local nUserObjId = GetUserObjIdByIdx(scene, i)
            AutoCombat(scene, nUserObjId, true)
        end
        if nUserCount < 1 then
            nUserCount = 1
        end
        local count = #x050010_g_MonstorList[stage + 1]
        for i = 1, count do
            MonId = CreateMonster(scene, x050010_g_MonstorList[stage + 1][i], x050010_g_MonstorPosList[stage + 1][i][3], x050010_g_MonstorPosList[stage + 1][i][1], x050010_g_MonstorPosList[stage + 1][i][2])
            SendImpactToUnit(scene, MonId, x050010_g_LevelAddBuff[1][nUserCount])
            SetHpFull(scene, MonId)
            UpdateObjHpMax(scene, MonId)
        end
    end
end

function x050010_CheckMissionMD(scene, objId, type)
    local nLastData = GetMissionMD(scene, objId, 4, type)
    local nNewData = CheckRefreshTime(scene, nLastData, objId, 0)
    if math.floor(nLastData / 100) ~= nNewData then
        SetMissionMD(scene, objId, 4, type, nNewData * 100)
    end
end

function x050010_Succeed(scene)

    local nUserCount = GetSceneUserCount(scene)
    for i = 0, nUserCount - 1 do
        local nUserObjId = GetUserObjIdByIdx(scene, i)
        CSEvalueEnd(scene, nUserObjId)
    end
end

--最后阶段
function x050010_Stage_1(scene, stage, substage)
    if substage == 1 then
        SetCopySceneParam(scene, x050010_g_CSParamIdxCurStage, stage + 1)
        SetCopySceneParam(scene, x050010_g_CSParamIdxCurSubStage, 0)
    end
end

function x050010_Stage_Box(scene, stage, substage)
    local nUserCount = GetSceneUserCount(scene)
    if substage == 0 then
        x050010_StageControl(scene, stage)
        for i = 0, nUserCount - 1 do
            local nUserObjId = GetUserObjIdByIdx(scene, i)
            SetMissionMD(scene, nUserObjId, 4, 1077, 1)
            CSEvalueEnd(scene, nUserObjId, false)
            x050010_CheckMissionMD(scene, nUserObjId, x050010_g_BoxNumEnumId)
            local oldTime = GetMissionMD(scene, nUserObjId, 4, x050010_g_BoxNumEnumId)
            SetMissionMD(scene, nUserObjId, 4, x050010_g_BoxNumEnumId, oldTime + 1)
        end
    elseif substage == x050010_g_BoxCountDown then
        for i = 0, nUserCount - 1 do
            local nUserObjId = GetUserObjIdByIdx(scene, i)
            x050010_SyncBoxInfo(scene, nUserObjId)
        end
        local MonId
        local nUserCount = GetSceneUserCount(scene)
        if nUserCount < 1 then
            nUserCount = 1
        end
        local count = #x050010_g_MonstorList[stage + 1]
        for i = 1, count do
            MonId = CreateMonster(scene, x050010_g_MonstorList[stage + 1][i], x050010_g_MonstorPosList[stage + 1][i][3], x050010_g_MonstorPosList[stage + 1][i][1], x050010_g_MonstorPosList[stage + 1][i][2])
            SendImpactToUnit(scene, MonId, x050010_g_LevelAddBuff[1][nUserCount])
            SetHpFull(scene, MonId)
            UpdateObjHpMax(scene, MonId)
        end
    elseif substage == x050010_g_ClearBoxTime then
        DeleHostileMonster(scene)
        SetCopySceneParam(scene, x050010_g_CSParamIdxCurStage, stage + 1)
        SetCopySceneParam(scene, x050010_g_CSParamIdxCurSubStage, 0)
    end
end

----------------怪物掉血掉落宝箱---------------------------------
function x050010_BoxHpTick(scene)
    -- 宝箱怪物掉血掉落宝箱
    local count = #x050010_g_MonstorList[6]
    for j = 1, count do
        local ZhobjId2 = GetObjIdByRoleBase(scene, x050010_g_MonstorList[6][j])
        local HpCount = #x050010_g_DropHp[1]
        for i = 1, HpCount do
            if math.ceil(CalculatePercenHp(scene, ZhobjId2)) <= x050010_g_DropHp[1][i] and CalculatePercenHp(scene, ZhobjId2) >= 0 then
                local Flag = GetCopySceneParam(scene, x050010_g_CSParamIdxBoxHpAward - 1 + i)
                if Flag == 0 then
                    SetCopySceneParam(scene, x050010_g_CSParamIdxBoxHpAward - 1 + i, 1)
                    local boxIdFlag = x050010_g_DropRew[1][i]
                    if boxIdFlag == 1 then
                        x050010_OnBoxDrop(scene, ZhobjId2, x050010_g_MonstorList[6][j], false)
                    elseif boxIdFlag == 2 then
                        x050010_OnBoxDrop(scene, ZhobjId2, x050010_g_MonstorList[6][j], true)
                    end
                end
            end
        end
    end
end

function x050010_OnBoxDrop(scene, MonId, rolebaseId, IsKill)
    local nUserCount = GetSceneUserCount(scene)
    for i = 0, nUserCount - 1 do
        local nUserObjId = GetUserObjIdByIdx(scene, i)
        if (GetMissionMD(scene, nUserObjId, 4, x050010_g_BoxNumEnumId) % 100 <= x050010_g_BoxNumCount) then
            CreateSLBossBox(scene, nUserObjId, IsKill, MonId, false)
        end
    end
end

function x050010_HpCountDown1(scene)
    local OldHp = math.ceil(GetCopySceneParam(scene, x050010_g_CSParam1))
    local count = #x050010_g_MonstorList[6]
    for j = 1, count do
        local ZhobjId2 = GetObjIdByRoleBase(scene, x050010_g_MonstorList[6][j])
        local NewHp = math.ceil(CalculatePercenHp(scene, ZhobjId2))
        if NewHp < 0 or NewHp == OldHp then
            return
        end
        local TargetHp = 0
        local HpCount = #x050010_g_DropHp[1]
        for i = 1, HpCount do
            if NewHp > x050010_g_DropHp[1][i] then
                TargetHp = x050010_g_DropHp[1][i]
                break
            end
        end
        SetCopySceneParam(scene, x050010_g_CSParam1, NewHp)
        SetCopySceneParam(scene, x050010_g_CSParam2, TargetHp)
        local nUserCount = GetSceneUserCount(scene)
        for i = 0, nUserCount - 1 do
            local nUserObjId = GetUserObjIdByIdx(scene, i)
            SynCsStageData(scene, nUserObjId)
        end
    end
end