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

x050005_g_scriptId = 050005

--副本参数
x050005_g_CSParamIdxCurStage = 0              --大阶段
x050005_g_CSParamIdxCurSubStage = 1           --小阶段
x050005_g_CSParam1 = 5
x050005_g_CSParam2 = 6

x050005_g_CSParamAnimationID = 5033           --mov动画ID  暂时不用

x050005_g_CSParamIdxHasBox = 12 -- 该次副本是否有宝箱

x050005_g_MonstorList = {}
x050005_g_MonstorList[1] = { 496001, 496001, 496001, 496002, 496001, 496001, 496001, 496002, 496001, 496001, 496001, 496002, 496001, 496001, 496001, 496002 }  --小怪
x050005_g_MonstorList[2] = { 496003, 496003, 496003, 496004, 496003, 496003, 496003, 496004, 496003, 496003, 496003, 496004, 496003, 496003, 496003, 496004 }  --小怪
x050005_g_MonstorList[3] = { 496005, 496005, 496005, 496005, 496005, 496005, 496005, 496005, 496005, 496005, 496006, 496006 }  --小怪
x050005_g_MonstorList[4] = { 496007, 496007, 496008, 496008 }  --小怪
x050005_g_MonstorList[5] = { 496009 }  --小怪   麻烦把boss放在第一个
x050005_g_MonstorList[6] = { 496011, 496011, 496012, 496010, 496010, 496010, 496010, 496010, 496010 }

x050005_g_MonstorPosList = {}
x050005_g_MonstorPosList[1] = { { 61.37, 107.08, -1.14 }, { 55.6, 113.04, -1.23 }, { 57.38, 104.76, -0.89 }, { 54.3, 104.98, -0.38 }, { 57.07, 108.14, -1.08 }, { 54.33, 108.12, -0.77 }, { 59.4, 113.88, -1.46 }, { 52.19, 109.41, -0.82 }, { 55.58, 109.97, -1.12 }, { 53.24, 111.81, -0.97 }, { 57.88, 111.99, -1.17 }, { 62.41, 113.22, -1.73 }, { 59.48, 109.63, -1.18 }, { 60.7, 111.37, -1.33 }, { 62.77, 108.97, -1.75 }, { 59.64, 104.9, -1.14 } } --小怪坐标 和上边对应
x050005_g_MonstorPosList[2] = { { 47.01, 161.97, -1.93 }, { 39.26, 163.94, -1.75 }, { 40.49, 160.78, -1.95 }, { 49.62, 164.24, -2.28 }, { 46.81, 169.08, -2.06 }, { 42.19, 169.35, -1.52 }, { 47.2, 165.49, -1.93 }, { 49.91, 161.3, -2.3 }, { 46.23, 158.69, -2.14 }, { 44.23, 163.04, -2.06 }, { 44.45, 167.13, -1.94 }, { 37.71, 168.27, -1.52 }, { 42.44, 158.8, -1.93 }, { 40.4, 166.5, -1.71 }, { 36.17, 160.68, -1.2 }, { 35.54, 164.6, -1.32 } }
x050005_g_MonstorPosList[3] = { { 124.16, 163.35, -3.67 }, { 117.44, 164.4, -3.51 }, { 120.06, 172.56, -2.84 }, { 125.53, 170.21, -3.2 }, { 117.02, 170.58, -3.17 }, { 125.94, 166.38, -3.41 }, { 123.77, 172.49, -3.03 }, { 120.46, 162.83, -3.61 }, { 115.77, 167.43, -3.2 }, { 120.76, 167.74, -3.17 }, { 111.71, 171.66, -3.14 }, { 111.83, 166.7, -3.6 } }
x050005_g_MonstorPosList[4] = { { 149.17, 100.89, 1.52 }, { 151.67, 101.02, -4.36 }, { 154.53, 96.45, -4.38 }, { 147.48, 96.45, 1.23 } }
x050005_g_MonstorPosList[5] = { { 152.53, 51.08, 1.57 } }
x050005_g_MonstorPosList[6] = { { 146.4, 48.65, 1.57 }, { 152.71, 48.55, 1.57 }, { 159.52, 48.75, 1.57 }, { 142.34, 45.24, 1.57 }, { 146.72, 45.24, 1.57 }, { 150.65, 45.24, 1.57 }, { 154.69, 45.24, 1.57 }, { 159.05, 45.24, 1.57 }, { 163.22, 45.24, 1.57 } }

-- 宝箱怪物寻路，路径表ID（NpcPatroPath）
--和 x050001_g_MonstorList[6] = { 490014, 490013, 490013 } 对应
x050005_g_MoveListId = {}
x050005_g_MoveListId[1] = { 5100, 5110, 5120, 5200, 5210, 5220, 5230, 5240, 5250 }

x050005_g_BoxNumEnumId = 138
x050005_g_BoxNumCount = 3
x050005_g_BoxCountDown = 11  -- 刷宝箱之前的倒数时间

x050005_g_ClearBoxTime = 42 -- 清除宝箱怪物时间
x050005_g_BigBoxId = 496011   -- 大包厢ID
x050005_g_BigBoxId2 = 496012   -- 大包厢ID
x050005_g_SmallBoxId = 496010    -- 小包厢ID


x050005_g_TargetList = {}                      --目标
x050005_g_TargetList[1] = { { 496001, 496002 } }  --{{第1阶段目标1},{第1阶段目标2}}
x050005_g_TargetList[2] = { { 496003, 496004 } }  --{{第2阶段目标1},{第2阶段目标2}}
x050005_g_TargetList[3] = { { 496005, 496006 } }
x050005_g_TargetList[4] = { { 496007, 496008 } }
x050005_g_TargetList[5] = { { 496009 } }
x050005_g_TargetList[6] = { { 496011, 496012 }, { 496010 } }

x050005_g_DeleListId = {}
x050005_g_DeleListId[1] = { 496009 }    -- 最后需要删除的怪物Id

x050005_g_BossNumEnumId = 987
x050005_g_BossNumCount = 3

x050005_g_MovCdTime = 10

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

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

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

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

function x050005_SyncBoxInfo(scene, objId)
    local CSevalue = GetCSEvaluaion(scene)
    local curStage = GetCopySceneParam(scene, x050005_g_CSParamIdxCurStage)
    local curSubStage = GetCopySceneParam(scene, x050005_g_CSParamIdxCurSubStage)
    if CSevalue == 0 and curStage == 5 and curSubStage > x050005_g_BoxCountDown and curSubStage <= x050005_g_ClearBoxTime then
        SyncSLBossBoxTime(scene, objId, x050005_g_ClearBoxTime - curSubStage, x050005_g_ClearBoxTime - x050005_g_BoxCountDown - 1)
    end
end

function x050005_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, x050005_g_BoxNumEnumId) % 100 <= x050005_g_BoxNumCount) then
            if rolebaseId == x050005_g_BigBoxId or rolebaseId == x050005_g_BigBoxId2 then
                Flag = CreateSLBossBox(scene, nUserObjId, true, MonId, true)
            elseif rolebaseId == x050005_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 x050005_OnCopySceneOpen(scene)
    AddCSMission(scene, 1890)
    AddCSMission(scene, 1891)
    AddCSMission(scene, 1892)
    AddCSMission(scene, 1893)
    AddCSMission(scene, 1894)
end
function x050005_StageControl(scene, nstage)
    SetCopySceneParam(scene, 2, 0)
    SetCopySceneParam(scene, 3, 0)
    SetCopySceneParam(scene, 4, 0)
    SetCopySceneParam(scene, 5, 100)
    SetCopySceneParam(scene, 6, x050005_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 x050005_Stage_Eff(scene, stage)
    if stage == 1 then
        ShowCopySceneGuideEffect(scene, 904101, 904102, 904103)
    elseif stage == 2 then
        ShowCopySceneGuideEffect(scene, 904201, 904202)
    elseif stage == 3 then
        ShowCopySceneGuideEffect(scene, 904301, 904302)
    elseif stage == 4 then
        ShowCopySceneGuideEffect(scene, 904401, 904402)
    end
end

function x050005_OnDieCountDown1(scene, rolebaseId, stage)
    local count = #x050005_g_TargetList[stage + 1]
    for k = 1, count do
        local count2 = #x050005_g_TargetList[stage + 1][k]
        for j = 1, count2 do
            local TargetId = x050005_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 x050005_OnObjEnter(scene, objId)
    SynCsStageData(scene, objId, true)
    x050005_SyncBoxInfo(scene, objId)
end

--死亡回调
function x050005_OnObjDie(scene, objId, rolebaseId, killerId)
    local nCurStage = GetCopySceneParam(scene, x050005_g_CSParamIdxCurStage)
    local MonCount = GetHostileMonsterCount(scene)
    if nCurStage == 5 and rolebaseId ~= x050005_g_BigBoxId and rolebaseId ~= x050005_g_SmallBoxId and rolebaseId ~= x050005_g_BigBoxId2 then
        return    -- 不是宝箱怪物进入宝箱阶段的死亡回调直接return
    end
    local oldStage = GetCopySceneParam(scene, 7)
    x050005_OnDieCountDown1(scene, rolebaseId, oldStage)
    if (rolebaseId == x050005_g_BigBoxId or rolebaseId == x050005_g_SmallBoxId or rolebaseId == x050005_g_BigBoxId2) then
        x050005_OnBoxDie(scene, objId, rolebaseId)
    end
    if (rolebaseId == x050005_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, x050005_g_BossNumEnumId) % 1000 <= x050005_g_BossNumCount) then
                local Flag = CreateDropBoxByShiLian(scene, nUserObjId, objId)
                if Flag == true then
                    IsEff = true
                end
            end
        end
        if IsEff == true then
            MonsterDropEff(scene, objId)
        end
        if GetCSEvaluaion(scene) ~= 0 then
            x050005_Succeed(scene)
        end
        SetCopySceneParam(scene, x050005_g_CSParamIdxCurStage, nCurStage + 1)
        SetCopySceneParam(scene, x050005_g_CSParamIdxCurSubStage, 0)
        x050005_ClearAllMonster(scene)
        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, x050005_g_CSParamIdxHasBox, 1)
        end
        return
    end
    if MonCount == 0 and nCurStage ~= 4 then
        SetCopySceneParam(scene, x050005_g_CSParamIdxCurStage, nCurStage + 1)
        SetCopySceneParam(scene, x050005_g_CSParamIdxCurSubStage, 0)

    end
end
function x050005_ClearAllMonster(scene)
    local count = #x050005_g_DeleListId[1]
    for i = 1, count do
        DeleMonsterByRoleId(scene, x050005_g_DeleListId[1][i])
    end
end
--动画/对话回调

--副本Tick
function x050005_OnCopySceneTick(scene, tickCount)
    local curStage = GetCopySceneParam(scene, x050005_g_CSParamIdxCurStage)
    local curSubStage = GetCopySceneParam(scene, x050005_g_CSParamIdxCurSubStage)
    SetCopySceneParam(scene, x050005_g_CSParamIdxCurSubStage, curSubStage + 1)
    -- x050005_BoxHpTick(scene)
    if curStage < 5 then
        x050005_Stage_0(scene, curStage, curSubStage)
    elseif curStage == 5 then
        -- x050005_HpCountDown1(scene)
        local LastCSEvalue = GetCopySceneParam(scene, x050005_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
                x050005_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, x050005_g_CSParamIdxHasBox, 1)
                x050005_Stage_Box(scene, curStage, curSubStage)
            end
        elseif LastCSEvalue ~= 0 then
            x050005_Stage_Box(scene, curStage, curSubStage)
        end
    elseif curStage == 6 then
        SetCopySceneSuccess(scene)
    end
end

--刷小怪阶段
function x050005_Stage_0(scene, stage, substage)
    if substage == 1 then
        x050005_StageControl(scene, stage)
        OnCSMissionStage(scene, stage)
        if stage ~= 0 then
            CSEvalueBeginNextStage(scene)
        end
        if stage == 4 then
            StoryAnimation(scene, 2010)
            SetCSEvaluateIsBossStage(scene, true)
        end
        x050005_Stage_Eff(scene, stage)
        local count = #x050005_g_MonstorList[stage + 1]
        local MonId
        local nUserCount = GetSceneUserCount(scene)
        if nUserCount < 1 then
            nUserCount = 1
        end
        for i = 1, count do
            if stage ~= 4 then
                MonId = CreateMonster(scene, x050005_g_MonstorList[stage + 1][i], x050005_g_MonstorPosList[stage + 1][i][3], x050005_g_MonstorPosList[stage + 1][i][1], x050005_g_MonstorPosList[stage + 1][i][2])
                SendImpactToUnit(scene, MonId, x050005_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 = #x050005_g_MonstorList[stage + 1]
        for i = 1, count do
            MonId = CreateMonster(scene, x050005_g_MonstorList[stage + 1][i], x050005_g_MonstorPosList[stage + 1][i][3], x050005_g_MonstorPosList[stage + 1][i][1], x050005_g_MonstorPosList[stage + 1][i][2])
            SendImpactToUnit(scene, MonId, x050005_g_LevelAddBuff[1][nUserCount])
            SetHpFull(scene, MonId)
            UpdateObjHpMax(scene, MonId)
        end
    end
end

function x050005_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 x050005_Succeed(scene)
    local nUserCount = GetSceneUserCount(scene)
    for i = 0, nUserCount - 1 do
        local nUserObjId = GetUserObjIdByIdx(scene, i)
        CSEvalueEnd(scene, nUserObjId)
    end
end

--最后阶段
function x050005_Stage_1(scene, stage, substage)
    if substage == 1 then
        SetCopySceneParam(scene, x050005_g_CSParamIdxCurStage, stage + 1)
        SetCopySceneParam(scene, x050005_g_CSParamIdxCurSubStage, 0)
    end
end

function x050005_Stage_Box(scene, stage, substage)
    local nUserCount = GetSceneUserCount(scene)
    if substage == 0 then
        x050005_StageControl(scene, stage)
        for i = 0, nUserCount - 1 do
            local nUserObjId = GetUserObjIdByIdx(scene, i)
            SetMissionMD(scene, nUserObjId, 4, 378, 1)
            CSEvalueEnd(scene, nUserObjId, false)
            x050005_CheckMissionMD(scene, nUserObjId, x050005_g_BoxNumEnumId)
            local oldTime = GetMissionMD(scene, nUserObjId, 4, x050005_g_BoxNumEnumId)
            SetMissionMD(scene, nUserObjId, 4, x050005_g_BoxNumEnumId, oldTime + 1)
        end
    elseif substage == x050005_g_BoxCountDown then
        for i = 0, nUserCount - 1 do
            local nUserObjId = GetUserObjIdByIdx(scene, i)
            x050005_SyncBoxInfo(scene, nUserObjId)
        end
        local MonId
        local nUserCount = GetSceneUserCount(scene)
        if nUserCount < 1 then
            nUserCount = 1
        end
        local count = #x050005_g_MonstorList[stage + 1]
        for i = 1, count do
            MonId = CreateMonster(scene, x050005_g_MonstorList[stage + 1][i], x050005_g_MonstorPosList[stage + 1][i][3], x050005_g_MonstorPosList[stage + 1][i][1], x050005_g_MonstorPosList[stage + 1][i][2])
            SendImpactToUnit(scene, MonId, x050005_g_LevelAddBuff[1][nUserCount])
            SetHpFull(scene, MonId)
            UpdateObjHpMax(scene, MonId)
            NpcStartPatrol(scene, MonId, x050005_g_MoveListId[1][i])--怪物寻路
        end
    elseif substage == x050005_g_ClearBoxTime then
        DeleHostileMonster(scene)
        SetCopySceneParam(scene, x050005_g_CSParamIdxCurStage, stage + 1)
        SetCopySceneParam(scene, x050005_g_CSParamIdxCurSubStage, 0)
    end
end

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

function x050005_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, x050005_g_BoxNumEnumId) % 100 <= x050005_g_BoxNumCount) then
            CreateSLBossBox(scene, nUserObjId, IsKill, MonId, false)
        end
    end
end

function x050005_HpCountDown1(scene)
    local OldHp = math.ceil(GetCopySceneParam(scene, x050005_g_CSParam1))
    local count = #x050005_g_MonstorList[6]
    for j = 1, count do
        local ZhobjId2 = GetObjIdByRoleBase(scene, x050005_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 = #x050005_g_DropHp[1]
        for i = 1, HpCount do
            if NewHp > x050005_g_DropHp[1][i] then
                TargetHp = x050005_g_DropHp[1][i]
                break
            end
        end
        SetCopySceneParam(scene, x050005_g_CSParam1, NewHp)
        SetCopySceneParam(scene, x050005_g_CSParam2, TargetHp)
        local nUserCount = GetSceneUserCount(scene)
        for i = 0, nUserCount - 1 do
            local nUserObjId = GetUserObjIdByIdx(scene, i)
            SynCsStageData(scene, nUserObjId)
        end
    end
end