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

x050007_g_scriptId = 050007

--副本参数
x050007_g_CSParamIdxCurStage = 0              --大阶段
x050007_g_CSParamIdxCurSubStage = 1           --小阶段
x050007_g_CSParam1 = 5
x050007_g_CSParam2 = 6

x050007_g_CSParamIdxHasBox = 12 -- 该次副本是否有宝箱
x050007_g_CSParamIdxBossBuff = 13  -- bossbuff提升次数
x050007_g_CSParamAnimationID = 2012           --mov动画ID  暂时不用

x050007_g_MonstorList = {}
x050007_g_MonstorList[1] = { 498002, 498002, 498002, 498001, 498001, 498001, 498001, 498001, 498001, 498001, 498001, 498001 }  --小怪
x050007_g_MonstorList[2] = { 498004, 498004, 498004, 498003, 498003, 498003, 498003, 498003, 498003, 498003, 498003, 498003 }  --小怪
x050007_g_MonstorList[3] = { 498006, 498006, 498006, 498005, 498005, 498005, 498005, 498005, 498005 }  --小怪
x050007_g_MonstorList[4] = { 498008, 498008, 498007, 498007 }  --小怪
x050007_g_MonstorList[5] = { 498011 }  --小怪   麻烦把boss放在第一个

x050007_g_MonstorList[6] = { 498014, 498014, 498013, 498012, 498012, 498012, 498012, 498012, 498012 }

x050007_g_MonstorPosList = {}
x050007_g_MonstorPosList[1] = { {100.53,29.12,-1.11},{93.35,35.14,-0.94},{99.17,41.5,-1.3},{96.46,29.62,-0.88},{103.81,33.37,-1.3},{100.77,33.9,-1.23},{92.38,29.81,-0.79},{96.9,38.07,-1.1},{102.55,39.01,-1.43},{93.02,40.59,-1.1},{96.87,35.03,-0.97},{89.62,34.71,-0.76} } --小怪坐标 和上边对应
x050007_g_MonstorPosList[2] = { {132.65,49.02,-2.84},{136.03,45.23,-2.34},{130.91,53.66,-2.84},{139.9,47.33,-2.36},{137.91,51.91,-2.42},{133.92,54.77,-2.84},{141.66,51.03,-2.84},{135.16,51.42,-2.19},{131.74,59.26,-3.12},{141.23,55.78,-2.28},{137.49,60.28,-2.38},{130.84,48.7,-2.84} }
x050007_g_MonstorPosList[3] = { {164.45,82.42,-2.52},{170.53,79.93,-2.39},{166.85,87.14,-3.01},{173.63,82.21,-2.26},{170.09,85.27,-2.71},{162.95,87.15,-2.98},{164.68,91.71,-2.87},{173.37,86.9,-2.11},{169.61,89.82,-2.39} }
x050007_g_MonstorPosList[4] = { {129.08,118.85,-0.81},{132.12,121.63,-1.1},{130.14,128.46,-1.1},{122.43,123.31,-0.71} }
x050007_g_MonstorPosList[5] = { { 116.8, 198.7, -1.57 } }
x050007_g_MonstorPosList[6] = {{110.96,207.26,-1.57},{115.44,208.08,-1.57},{120.1,207.9,-1.57},{103.79,211.4,-1.57},{108.09,211.89,-1.57},{112.85,212.18,-1.57},{117.39,212.63,-1.57},{121.8,213.12,-1.57},{126.17,213.45,-1.57} }

x050007_g_TargetList = {}                      --目标
x050007_g_TargetList[1] = { { 498001, 498002 } }  --{{第1阶段目标},{第1阶段目标2}}
x050007_g_TargetList[2] = { { 498003, 498004 } }  --{{第2阶段目标1},{第2阶段目标2}}
x050007_g_TargetList[3] = { { 498005, 498006 } }
x050007_g_TargetList[4] = { { 498007, 498008 } }
x050007_g_TargetList[5] = { { 498011 } }
x050007_g_TargetList[6] = { { 498013 , 498014 }, { 498012 } }

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

x050007_g_AddBuffMon = {}
x050007_g_AddBuffMon[1] = { -1, -1, -1, -1 }        --每一波上buff的辅助怪物

x050007_g_AddBuffId = {}
x050007_g_AddBuffId[1] = { -1, -1, -1, -1 }        --每一波上buff的辅助怪物所对应的buffID  和上边对应

x050007_g_BossAddBuffMon = {}
x050007_g_BossAddBuffMon[1] = { -1, -1 }        --Boss关卡buff的辅助怪物

x050007_g_BossAddBuffTime = 5    -- 5秒上一次
x050007_g_BossAddBuffCount = 10        -- buff种类

x050007_g_BossAddBuffId = {}
x050007_g_BossAddBuffId[1] = { { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } }        --Boss关卡buff的辅助怪物所对应的buffID  和上边对应
x050007_g_BossAddBuffHpPer = 99        -- 触发百分比

x050007_g_BossNumEnumId = 987
x050007_g_BossNumCount = 3

x050007_g_BoxNumEnumId = 138
x050007_g_BoxNumCount = 3
x050007_g_BoxCountDown = 11  -- 刷宝箱之前的倒数时间（需要同时修改前端时间）

x050007_g_ClearBoxTime = 42 -- 清除宝箱怪物时间
x050007_g_BigBoxId = 498014   -- 大包厢ID
x050007_g_BigBoxId = 498013
x050007_g_SmallBoxId = 498012   -- 小包厢ID

x050007_g_MovCdTime = 10

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


-- 宝箱怪物寻路，路径表ID（NpcPatroPath）
--和 x050001_g_MonstorList[6] = { 490014, 490013, 490013 } 对应
x050007_g_MoveListId = {}
x050007_g_MoveListId[1] = { 7100, 7110, 7120, 7200, 7210, 7220, 7230, 7240, 7250 }




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

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

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

function x050007_OnCopySceneOpen(scene)
    AddCSMission(scene, 1920)
    AddCSMission(scene, 1921)
    AddCSMission(scene, 1922)
    AddCSMission(scene, 1923)
    AddCSMission(scene, 1924)
end

function x050007_StageControl(scene, nstage)
    SetCopySceneParam(scene, 2, 0)
    SetCopySceneParam(scene, 3, 0)
    SetCopySceneParam(scene, 4, 0)
    SetCopySceneParam(scene, 5, 100)
    SetCopySceneParam(scene, 6, x050007_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 x050007_OnDieCountDown1(scene, rolebaseId, stage)
    local count = #x050007_g_TargetList[stage + 1]
    for k = 1, count do
        local count2 = #x050007_g_TargetList[stage + 1][k]
        for j = 1, count2 do
            local TargetId = x050007_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 x050007_OnObjEnter(scene, objId)
    SynCsStageData(scene, objId, true)
    x050007_SyncBoxInfo(scene, objId)
end

function x050007_SyncBoxInfo(scene, objId)
    local CSevalue = GetCSEvaluaion(scene)
    local curStage = GetCopySceneParam(scene, x050007_g_CSParamIdxCurStage)
    local curSubStage = GetCopySceneParam(scene, x050007_g_CSParamIdxCurSubStage)
    if CSevalue == 0 and curStage == 5 and curSubStage > x050007_g_BoxCountDown and curSubStage <= x050007_g_ClearBoxTime then
        SyncSLBossBoxTime(scene, objId, x050007_g_ClearBoxTime - curSubStage, x050007_g_ClearBoxTime - x050007_g_BoxCountDown - 1)
    end
end

function x050007_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, x050007_g_BoxNumEnumId) % 100 <= x050007_g_BoxNumCount) then
            if rolebaseId == x050007_g_BigBoxId then
                Flag = CreateSLBossBox(scene, nUserObjId, true, MonId, true)
            elseif rolebaseId == x050007_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 x050007_OnObjDie(scene, objId, rolebaseId, killerId)
    local nCurStage = GetCopySceneParam(scene, x050007_g_CSParamIdxCurStage)
    if nCurStage == 5 and rolebaseId ~= x050007_g_BigBoxId and rolebaseId ~= x050007_g_SmallBoxId then
        return    -- 不是宝箱怪物进入宝箱阶段的死亡回调直接return
    end
    local MonCount = GetHostileMonsterCount(scene)
    local oldStage = GetCopySceneParam(scene, 7)
    x050007_OnDieCountDown1(scene, rolebaseId, oldStage)
    if (rolebaseId == x050007_g_BigBoxId or rolebaseId == x050007_g_SmallBoxId) then
        x050007_OnBoxDie(scene, objId, rolebaseId)
    end
    if nCurStage == 4 and MonCount == 0 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, x050007_g_BossNumEnumId) % 1000 <= x050007_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
            x050007_Succeed(scene)
        end
        SetCopySceneParam(scene, x050007_g_CSParamIdxCurStage, nCurStage + 1)
        SetCopySceneParam(scene, x050007_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, x050007_g_CSParamIdxHasBox, 1)
        end
        return
    end
    if MonCount == 0 and nCurStage ~= 4 then
        SetCopySceneParam(scene, x050007_g_CSParamIdxCurStage, nCurStage + 1)
        SetCopySceneParam(scene, x050007_g_CSParamIdxCurSubStage, 0)

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


--副本Tick
function x050007_OnCopySceneTick(scene, tickCount)
    local curStage = GetCopySceneParam(scene, x050007_g_CSParamIdxCurStage)
    local curSubStage = GetCopySceneParam(scene, x050007_g_CSParamIdxCurSubStage)
    SetCopySceneParam(scene, x050007_g_CSParamIdxCurSubStage, curSubStage + 1)
    x050007_AddBossZYBuff(scene, tickCount)
    -- x050007_BoxHpTick(scene)
    if curStage < 5 then
        x050007_Stage_0(scene, curStage, curSubStage)
    elseif curStage == 5 then
        -- x050007_HpCountDown1(scene)
        local LastCSEvalue = GetCopySceneParam(scene, x050007_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
                x050007_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, x050007_g_CSParamIdxHasBox, 1)
                x050007_Stage_Box(scene, curStage, curSubStage)
            end
        elseif LastCSEvalue ~= 0 then
            x050007_Stage_Box(scene, curStage, curSubStage)
        end
    elseif curStage == 6 then
        SetCopySceneSuccess(scene)
    end
end

function x050007_Stage_Eff(scene, stage)
    if stage == 1 then
        ShowCopySceneGuideEffect(scene, 906101, 906102)
    elseif stage == 2 then
        ShowCopySceneGuideEffect(scene, 906201, 906202)
    elseif stage == 3 then
        ShowCopySceneGuideEffect(scene, 906301, 906302)
    elseif stage == 4 then
        ShowCopySceneGuideEffect(scene, 906401, 906402)
    end
end
--刷小怪阶段
function x050007_Stage_0(scene, stage, substage)
    if substage == 1 then
        x050007_StageControl(scene, stage)
        OnCSMissionStage(scene, stage)
        if stage ~= 0 then
            CSEvalueBeginNextStage(scene)
        end
        x050007_Stage_Eff(scene, stage)
        if stage == 4 then
            StoryAnimation(scene, 2012)
            SetCSEvaluateIsBossStage(scene, true)
        end
        local MonId
        local nUserCount = GetSceneUserCount(scene)
        if nUserCount < 1 then
            nUserCount = 1
        end
        local count = #x050007_g_MonstorList[stage + 1]
        for i = 1, count do
            if stage ~= 4 then
                MonId = CreateMonster(scene, x050007_g_MonstorList[stage + 1][i], x050007_g_MonstorPosList[stage + 1][i][3], x050007_g_MonstorPosList[stage + 1][i][1], x050007_g_MonstorPosList[stage + 1][i][2])
                SendImpactToUnit(scene, MonId, x050007_g_LevelAddBuff[1][nUserCount])
                x050007_AddZYBuff(scene, stage)
                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 = #x050007_g_MonstorList[stage + 1]
        for i = 1, count do
            MonId = CreateMonster(scene, x050007_g_MonstorList[stage + 1][i], x050007_g_MonstorPosList[stage + 1][i][3], x050007_g_MonstorPosList[stage + 1][i][1], x050007_g_MonstorPosList[stage + 1][i][2])
            SendImpactToUnit(scene, MonId, x050007_g_LevelAddBuff[1][nUserCount])
            x050007_AddZYBuff(scene, stage)
            SetHpFull(scene, MonId)
            UpdateObjHpMax(scene, MonId)
        end
    elseif (substage == 4) and stage == 4 then
        local MonId
        local nUserCount = GetSceneUserCount(scene)
        for i = 0, nUserCount - 1 do
            local nUserObjId = GetUserObjIdByIdx(scene, i)
            SetCameraPos(scene, nUserObjId)
        end
    end
end

function x050007_AddZYBuff(scene, nstage)
    local MonNum = #x050007_g_AddBuffMon[1]
    for i = 1, MonNum do
        if i == nstage + 1 then
            SendImpactToAllByRBID(scene, x050007_g_AddBuffMon[1][i], x050007_g_AddBuffId[1][i])
        end
    end
end

function x050007_AddBossZYBuff(scene, tickcount)
    local MonNum = #x050007_g_BossAddBuffMon[1]
    local OldNum = GetCopySceneParam(scene, x050007_g_CSParamIdxBossBuff)
    local NowNum = OldNum
    for i = 1, MonNum do
        local ZhobjId1 = GetObjIdByRoleBase(scene, x050007_g_BossAddBuffMon[1][i])
        if CalculatePercenHp(scene, ZhobjId1) ~= -1 and CalculatePercenHp(scene, ZhobjId1) <= x050007_g_BossAddBuffHpPer then
        elseif CalculatePercenHp(scene, ZhobjId1) == 100 then
            if tickcount % x050007_g_BossAddBuffTime == 0 then
                NowNum = OldNum + 1
                if NowNum >= x050007_g_BossAddBuffCount then
                    NowNum = x050007_g_BossAddBuffCount
                end
                SendImpactToAllByRBID(scene, x050007_g_BossAddBuffMon[1][i], x050007_g_BossAddBuffId[1][i][NowNum])
            end
        end
    end
    SetCopySceneParam(scene, x050007_g_CSParamIdxBossBuff, NowNum)
end

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

--最后阶段
function x050007_Stage_1(scene, stage, substage)
    if substage == 1 then
        SetCopySceneParam(scene, x050007_g_CSParamIdxCurStage, stage + 1)
        SetCopySceneParam(scene, x050007_g_CSParamIdxCurSubStage, 0)
    end
end

function x050007_Stage_Box(scene, stage, substage)
    local nUserCount = GetSceneUserCount(scene)
    if substage == 0 then
        x050007_StageControl(scene, stage)
        for i = 0, nUserCount - 1 do
            local nUserObjId = GetUserObjIdByIdx(scene, i)
            SetMissionMD(scene, nUserObjId, 4, 1073, 1)
            CSEvalueEnd(scene, nUserObjId, false)
            x050007_CheckMissionMD(scene, nUserObjId, x050007_g_BoxNumEnumId)
            local oldTime = GetMissionMD(scene, nUserObjId, 4, x050007_g_BoxNumEnumId)
            SetMissionMD(scene, nUserObjId, 4, x050007_g_BoxNumEnumId, oldTime + 1)
        end
    elseif substage == x050007_g_BoxCountDown then
        for i = 0, nUserCount - 1 do
            local nUserObjId = GetUserObjIdByIdx(scene, i)
            x050007_SyncBoxInfo(scene, nUserObjId)
        end
        local MonId
        local nUserCount = GetSceneUserCount(scene)
        if nUserCount < 1 then
            nUserCount = 1
        end
        local count = #x050007_g_MonstorList[stage + 1]
        for i = 1, count do
            MonId = CreateMonster(scene, x050007_g_MonstorList[stage + 1][i], x050007_g_MonstorPosList[stage + 1][i][3], x050007_g_MonstorPosList[stage + 1][i][1], x050007_g_MonstorPosList[stage + 1][i][2])
            SendImpactToUnit(scene, MonId, x050007_g_LevelAddBuff[1][nUserCount])
            SetHpFull(scene, MonId)
            UpdateObjHpMax(scene, MonId)
            NpcStartPatrol(scene, MonId, x050007_g_MoveListId[1][i])--怪物寻路
        end
    elseif substage == x050007_g_ClearBoxTime then
        DeleHostileMonster(scene)
        SetCopySceneParam(scene, x050007_g_CSParamIdxCurStage, stage + 1)
        SetCopySceneParam(scene, x050007_g_CSParamIdxCurSubStage, 0)
    end
end

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

function x050007_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, x050007_g_BoxNumEnumId) % 100 <= x050007_g_BoxNumCount) then
            CreateSLBossBox(scene, nUserObjId, IsKill, MonId, false)
        end
    end
end

function x050007_HpCountDown1(scene)
    local OldHp = math.ceil(GetCopySceneParam(scene, x050007_g_CSParam1))
    local count = #x050007_g_MonstorList[6]
    for j = 1, count do
        local ZhobjId2 = GetObjIdByRoleBase(scene, x050007_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 = #x050007_g_DropHp[1]
        for i = 1, HpCount do
            if NewHp > x050007_g_DropHp[1][i] then
                TargetHp = x050007_g_DropHp[1][i]
                break
            end
        end
        SetCopySceneParam(scene, x050007_g_CSParam1, NewHp)
        SetCopySceneParam(scene, x050007_g_CSParam2, TargetHp)
        local nUserCount = GetSceneUserCount(scene)
        for i = 0, nUserCount - 1 do
            local nUserObjId = GetUserObjIdByIdx(scene, i)
            SynCsStageData(scene, nUserObjId)
        end
    end
end