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

x050004_g_scriptId = 050004

--副本参数
x050004_g_CSParamIdxCurStage = 0              --大阶段
x050004_g_CSParamIdxCurSubStage = 1           --小阶段
x050004_g_CSParam1 = 5
x050004_g_CSParam2 = 6

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

x050004_g_MonstorList = {}
x050004_g_MonstorList[1] = { 494212, 494212, 494212, 494212, 494201, 494201, 494201, 494201, 494201, 494201, 494201, 494201, 494201, 494201, 494201, 494201 }  --小怪
x050004_g_MonstorList[2] = { 494202, 494202, 494202, 494202, 494202, 494202, 494202, 494202, 494202, 494202, 494202, 494202, 494213, 494213, 494213, 494213 }  --小怪
x050004_g_MonstorList[3] = { 494204, 494203, 494203, 494203, 494203, 494203, 494204, 494203, 494203, 494203, 494203, 494203 }  --小怪
x050004_g_MonstorList[4] = { 494205, 494205, 494205, 494205 }  --小怪
x050004_g_MonstorList[5] = { 494208, 494207 }  --小怪   麻烦把boss放在第一个
x050004_g_MonstorList[6] = { 494210, 494211, 494211, 494209, 494209, 494209, 494209, 494209, 494209 }

x050004_g_MonstorPosList = {}
x050004_g_MonstorPosList[1] = { { 95.79, 45.84, -1.57 }, { 93.2, 46.89, -1.57 }, { 91.5, 42.9, -1.02 }, { 96.53, 40.82, -2.06 }, { 90.49, 45.81, -1.1 }, { 96.79, 42.98, -1.57 }, { 94.33, 44.02, -1.46 }, { 93.79, 41.49, -1.57 }, { 96.62, 49.21, -1.57 }, { 93, 48.97, -1.2 }, { 100.39, 45.58, -1.83 }, { 98.32, 47.57, -1.96 }, { 92.49, 45.08, -1.48 }, { 97.82, 45.09, -1.71 }, { 94.89, 47.67, -1.57 }, { 99.39, 42.6, -1.66 } } 
x050004_g_MonstorPosList[2] = { { 98.86, 89.32, -1.42 }, { 95.33, 89.69, -1.9 }, { 91.39, 88.65, -1.57 }, { 90.61, 85.38, -1.57 }, { 93.7, 81.84, -1.57 }, { 101.9, 88.39, -1.57 }, { 99.18, 83.25, -1.57 }, { 104.19, 85.9, -1.64 }, { 97.02, 80.39, -1.17 }, { 102.54, 83.83, -1.82 }, { 101.079, 81.756, -1.57 }, { 90.67, 81.74, -1.71 }, { 96.1, 84.08, -1.57 }, { 93.62, 85.4, -1.57 }, { 100.07, 86.33, -1.22 }, { 96.79, 86.96, -1.57 } }
x050004_g_MonstorPosList[3] = { { 47.22, 80.13, 0 }, { 49.49, 78.14, 0.09 }, { 49.58, 80.77, 0 }, { 51.72, 77.99, 0.28 }, { 53.48, 80.17, 0.21 }, { 49.54, 83.27, 0 }, { 46.92, 84.25, 0 }, { 49.52, 86.08, -0.29 }, { 51.62, 81.22, 0.07 }, { 51.22, 84.25, -0.36 }, { 51.83, 86.76, -0.52 }, { 53.65, 83.57, -0.3 } }
x050004_g_MonstorPosList[4] = { { 32.51, 101.64, -1.57 }, { 35.65, 101.64, -1.57 }, { 37.76, 105.35, -1.57 }, { 30.97, 105.35, -1.57 } }
x050004_g_MonstorPosList[5] = { { 34.69, 140.07, -1.57 }, { 41.91, 142.17, -3.14 } }
x050004_g_MonstorPosList[6] = { { 31.81, 141.68, -1.57 }, { 35.3, 141.01, -1.57 }, { 38.84, 140.51, -1.57 }, { 26.68, 144.95, -1.57 }, { 30.28, 144.99, -1.57 }, { 33.66, 144.72, -1.57 }, { 37.14, 144.61, -1.57 }, { 40.49, 144.34, -1.57 }, { 43.87, 143.53, -1.57 } }

-- 宝箱怪物寻路，路径表ID（NpcPatroPath）
--和 x050001_g_MonstorList[6] = { 490014, 490013, 490013 } 对应
x050004_g_MoveListId = {}
x050004_g_MoveListId[1] = { 4100, 4110, 4120, 4200, 4210, 4220, 4230, 4240, 4250 }

x050004_g_BoxNumEnumId = 138
x050004_g_BoxNumCount = 3
x050004_g_BoxCountDown = 11  -- 刷宝箱之前的倒数时间

x050004_g_ClearBoxTime = 42 -- 清除宝箱怪物时间

x050004_g_BigBoxId = 494210   -- 大包厢ID
x050004_g_BigBoxId2 = 494211   -- 大包厢ID
x050004_g_SmallBoxId = 494209    -- 小包厢ID


x050004_g_TargetList = {}                      --目标
x050004_g_TargetList[1] = { { 494212, 494201 } }  --{{第1阶段目标1},{第1阶段目标2}}
x050004_g_TargetList[2] = { { 494202, 494213 } }  --{{第2阶段目标1},{第2阶段目标2}}
x050004_g_TargetList[3] = { { 494204, 494203 } }
x050004_g_TargetList[4] = { { 494205 } }
x050004_g_TargetList[5] = { { 494208 }, { 494207 } }
x050004_g_TargetList[6] = { { 494210, 494211 }, { 494209 } }

x050004_g_BossNumEnumId = 987
x050004_g_BossNumCount = 3

x050004_g_BossPer = -1    -- 杀掉小怪技能触发血量
x050004_g_ImpactId1 = -1  -- 回血
x050004_g_ImpactId2 = -1     -- 加攻
x050004_g_ImpactId3 = -1     -- 减防
x050004_g_Monstor = 494207 -- 被杀掉的小怪
x050004_g_SkillId = 530001 -- 杀掉怪物的技能

x050004_g_KuangbaoCD = 5
x050004_g_KuangbaoImpactId = 30602
x050004_g_KuangbaoMonstorId1 = 496007
x050004_g_KuangbaoMonstorId2 = 496007

x050004_g_MovCdTime = 10

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

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

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

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

function x050004_OnCopySceneOpen(scene)
    --  这里加任务   五个ID务必按照顺序排列  任务加好之后把前边两个横杠去掉
    AddCSMission(scene, 1880)
    AddCSMission(scene, 1881)
    AddCSMission(scene, 1882)
    AddCSMission(scene, 1883)
    AddCSMission(scene, 1884)
end
function x050004_SyncBoxInfo(scene, objId)
    local CSevalue = GetCSEvaluaion(scene)
    local curStage = GetCopySceneParam(scene, x050004_g_CSParamIdxCurStage)
    local curSubStage = GetCopySceneParam(scene, x050004_g_CSParamIdxCurSubStage)
    if CSevalue == 0 and curStage == 5 and curSubStage > x050004_g_BoxCountDown and curSubStage <= x050004_g_ClearBoxTime then
        SyncSLBossBoxTime(scene, objId, x050004_g_ClearBoxTime - curSubStage, x050004_g_ClearBoxTime - x050004_g_BoxCountDown - 1)
    end
end

function x050004_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, x050004_g_BoxNumEnumId) % 100 <= x050004_g_BoxNumCount) then
            if rolebaseId == x050004_g_BigBoxId or rolebaseId == x050004_g_BigBoxId2 then
                Flag = CreateSLBossBox(scene, nUserObjId, true, MonId, true)
            elseif rolebaseId == x050004_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 x050004_StageControl(scene, nstage)
    SetCopySceneParam(scene, 2, 0)
    SetCopySceneParam(scene, 3, 0)
    SetCopySceneParam(scene, 4, 0)
    SetCopySceneParam(scene, 5, 100)
    SetCopySceneParam(scene, 6, x050004_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 x050004_Stage_Eff(scene, EffStage)
    if EffStage == 1 then
        ShowCopySceneGuideEffect(scene, 903101, 903102)
    elseif EffStage == 2 then
        ShowCopySceneGuideEffect(scene, 903201)
    elseif EffStage == 3 then
        ShowCopySceneGuideEffect(scene, 903301)
    elseif EffStage == 4 then
        ShowCopySceneGuideEffect(scene, 903401)
    end
end

function x050004_OnDieCountDown1(scene, rolebaseId, stage)
    local count = #x050004_g_TargetList[stage + 1]
    for k = 1, count do
        local count2 = #x050004_g_TargetList[stage + 1][k]
        for j = 1, count2 do
            local TargetId = x050004_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 x050004_OnObjEnter(scene, objId)
    SynCsStageData(scene, objId, true)
    x050004_SyncBoxInfo(scene, objId)
end

--死亡回调
function x050004_OnObjDie(scene, objId, rolebaseId, killerId)
    local nCurStage = GetCopySceneParam(scene, x050004_g_CSParamIdxCurStage)
    local MonCount = GetHostileMonsterCount(scene)
    if nCurStage == 5 and rolebaseId ~= x050004_g_BigBoxId and rolebaseId ~= x050004_g_SmallBoxId and rolebaseId ~= x050004_g_BigBoxId2 then
        return    -- 不是宝箱怪物进入宝箱阶段的死亡回调直接return
    end
    local oldStage = GetCopySceneParam(scene, 7)
    x050004_OnDieCountDown1(scene, rolebaseId, oldStage)
    if (rolebaseId == x050004_g_BigBoxId or rolebaseId == x050004_g_SmallBoxId or rolebaseId == x050004_g_BigBoxId2) then
        x050004_OnBoxDie(scene, objId, rolebaseId)
    end
    if (rolebaseId == x050004_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, x050004_g_BossNumEnumId) % 1000 <= x050004_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
            x050004_Succeed(scene)
        end
        SetCopySceneParam(scene, x050004_g_CSParamIdxCurStage, nCurStage + 1)
        SetCopySceneParam(scene, x050004_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, x050004_g_CSParamIdxHasBox, 1)
        end
        return
    end
    if MonCount == 0 and nCurStage ~= 4 then
        SetCopySceneParam(scene, x050004_g_CSParamIdxCurStage, nCurStage + 1)
        SetCopySceneParam(scene, x050004_g_CSParamIdxCurSubStage, 0)

    end
end

--动画/对话回调
function x050004_AddKuangBaoImpact(scene, tick)
    if tick % x050004_g_KuangbaoCD == 0 then
        local MonObj1 = GetObjIdByRoleBase(scene, x050004_g_KuangbaoMonstorId1)
        local MonObj2 = GetObjIdByRoleBase(scene, x050004_g_KuangbaoMonstorId2)
        if (CalculatePercenHp(scene, MonObj1) > 0 or CalculatePercenHp(scene, MonObj2) > 0) then
            SendImpactToAllByRBID(scene, x050004_g_MonstorList[5][1], x050004_g_KuangbaoImpactId)
        end
    end
end

--副本Tick
function x050004_OnCopySceneTick(scene, tickCount)
    local curStage = GetCopySceneParam(scene, x050004_g_CSParamIdxCurStage)
    local curSubStage = GetCopySceneParam(scene, x050004_g_CSParamIdxCurSubStage)
    SetCopySceneParam(scene, x050004_g_CSParamIdxCurSubStage, curSubStage + 1)
    x050004_AddKuangBaoImpact(scene, tickCount)
    -- x050004_BoxHpTick(scene)
    if curStage == 4 then
        local Parm = GetCopySceneParam(scene, 10)
        local MonObj = GetObjIdByRoleBase(scene, x050004_g_MonstorList[5][1])
        if (MonIsBattle(scene, x050004_g_MonstorList[5][1]) and CalculatePercenHp(scene, MonObj) <= x050004_g_BossPer and Parm == 0) then
            SetCopySceneParam(scene, 10, 1)
        end
    end
    if curStage < 5 then
        x050004_Stage_0(scene, curStage, curSubStage)
    elseif curStage == 5 then
        -- x050004_HpCountDown1(scene)
        local LastCSEvalue = GetCopySceneParam(scene, x050004_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
                x050004_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, x050004_g_CSParamIdxHasBox, 1)
                x050004_Stage_Box(scene, curStage, curSubStage)
            end
        elseif LastCSEvalue ~= 0 then
            x050004_Stage_Box(scene, curStage, curSubStage)
        end
    elseif curStage == 6 then
        SetCopySceneSuccess(scene)
    end
end

--刷小怪阶段
function x050004_Stage_0(scene, stage, substage)
    if substage == 1 then
        OnCSMissionStage(scene, stage)
        x050004_StageControl(scene, stage)
        if stage ~= 0 then
            CSEvalueBeginNextStage(scene)
        end
        if stage == 4 then
            StoryAnimation(scene, 2009)
            SetCSEvaluateIsBossStage(scene, true)
        end
        x050004_Stage_Eff(scene, stage)
        local count = #x050004_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, x050004_g_MonstorList[stage + 1][i], x050004_g_MonstorPosList[stage + 1][i][3], x050004_g_MonstorPosList[stage + 1][i][1], x050004_g_MonstorPosList[stage + 1][i][2])
                SendImpactToUnit(scene, MonId, x050004_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 = #x050004_g_MonstorList[stage + 1]
        for i = 1, count do
            MonId = CreateMonster(scene, x050004_g_MonstorList[stage + 1][i], x050004_g_MonstorPosList[stage + 1][i][3], x050004_g_MonstorPosList[stage + 1][i][1], x050004_g_MonstorPosList[stage + 1][i][2])
            SendImpactToUnit(scene, MonId, x050004_g_LevelAddBuff[1][nUserCount])
            SetHpFull(scene, MonId)
            UpdateObjHpMax(scene, MonId)
        end
    end
end

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

    end
end

--最后阶段
function x050004_Stage_1(scene, stage, substage)
    if substage == 1 then
        SetCopySceneParam(scene, x050004_g_CSParamIdxCurStage, stage + 1)
        SetCopySceneParam(scene, x050004_g_CSParamIdxCurSubStage, 0)
    end
end

function x050004_Stage_Box(scene, stage, substage)
    local nUserCount = GetSceneUserCount(scene)
    if substage == 0 then
        x050004_StageControl(scene, stage)
        for i = 0, nUserCount - 1 do
            local nUserObjId = GetUserObjIdByIdx(scene, i)
            CSEvalueEnd(scene, nUserObjId, false)
            SetMissionMD(scene, nUserObjId, 4, 377, 1)
            x050004_CheckMissionMD(scene, nUserObjId, x050004_g_BoxNumEnumId)
            local oldTime = GetMissionMD(scene, nUserObjId, 4, x050004_g_BoxNumEnumId)
            SetMissionMD(scene, nUserObjId, 4, x050004_g_BoxNumEnumId, oldTime + 1)
        end

    elseif substage == x050004_g_BoxCountDown then
        for i = 0, nUserCount - 1 do
            local nUserObjId = GetUserObjIdByIdx(scene, i)
            x050004_SyncBoxInfo(scene, nUserObjId)
        end
        local MonId
        local nUserCount = GetSceneUserCount(scene)
        if nUserCount < 1 then
            nUserCount = 1
        end
        local count = #x050004_g_MonstorList[stage + 1]
        for i = 1, count do
            MonId = CreateMonster(scene, x050004_g_MonstorList[stage + 1][i], x050004_g_MonstorPosList[stage + 1][i][3], x050004_g_MonstorPosList[stage + 1][i][1], x050004_g_MonstorPosList[stage + 1][i][2])
            SendImpactToUnit(scene, MonId, x050004_g_LevelAddBuff[1][nUserCount])
            SetHpFull(scene, MonId)
            UpdateObjHpMax(scene, MonId)
            NpcStartPatrol(scene, MonId, x050004_g_MoveListId[1][i])--怪物寻路
        end
    elseif substage == x050004_g_ClearBoxTime then
        DeleHostileMonster(scene)
        SetCopySceneParam(scene, x050004_g_CSParamIdxCurStage, stage + 1)
        SetCopySceneParam(scene, x050004_g_CSParamIdxCurSubStage, 0)
    end
end

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

function x050004_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, x050004_g_BoxNumEnumId) % 100 <= x050004_g_BoxNumCount) then
            CreateSLBossBox(scene, nUserObjId, IsKill, MonId, false)
        end
    end
end

function x050004_HpCountDown1(scene)
    local OldHp = math.ceil(GetCopySceneParam(scene, x050004_g_CSParam1))
    local count = #x050004_g_MonstorList[6]
    for j = 1, count do
        local ZhobjId2 = GetObjIdByRoleBase(scene, x050004_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 = #x050004_g_DropHp[1]
        for i = 1, HpCount do
            if NewHp > x050004_g_DropHp[1][i] then
                TargetHp = x050004_g_DropHp[1][i]
                break
            end
        end
        SetCopySceneParam(scene, x050004_g_CSParam1, NewHp)
        SetCopySceneParam(scene, x050004_g_CSParam2, TargetHp)
        local nUserCount = GetSceneUserCount(scene)
        for i = 0, nUserCount - 1 do
            local nUserObjId = GetUserObjIdByIdx(scene, i)
            SynCsStageData(scene, nUserObjId)
        end
    end
end