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

x050002_g_scriptId = 050002

--副本参数
x050002_g_CSParamIdxCurStage = 0              --大阶段
x050002_g_CSParamIdxCurSubStage = 1           --小阶段

x050002_g_CSParam1 = 5
x050002_g_CSParam2 = 6

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

x050002_g_MonstorList = {}
x050002_g_MonstorList[1] = { 490100, 490100, 490101, 490101, 490101, 490101, 490101, 490101, 490101, 490101, 490101, 490101, 490101, 490101, 490101, 490101 }  --小怪
x050002_g_MonstorList[2] = { 491101, 491101, 491102, 491102, 491102, 491102, 491102, 491102, 491102, 491102, 491102, 491102, 491102, 491102, 491102, 491102 }  --小怪
x050002_g_MonstorList[3] = { 493103, 493103, 492102, 492102, 492102, 492102, 492102, 492102, 492102, 492102 }  --小怪
x050002_g_MonstorList[4] = { 494104, 494104, 494105, 494105 }  --小怪
x050002_g_MonstorList[5] = { 495105 }  --小怪   麻烦把boss放在第一个
x050002_g_MonstorList[6] = { 497109, 497110, 497110, 496108, 496108, 496108, 496108, 496108, 496108 }

x050002_g_MonstorPosList = {}
x050002_g_MonstorPosList[1] = { { 30.11, 107.99, -1.97 }, { 31.22, 113.64, -1.97 }, { 33.56, 112.54, -1.97 }, { 29.16, 114.81, -1.97 }, { 35.97, 111.69, -1.97 }, { 27.21, 113.37, -1.97 }, { 29.05, 112.51, -1.97 }, { 31.58, 111.35, -1.97 }, { 34.24, 110.35, -1.97 }, { 36.71, 109.56, -1.97 }, { 26.34, 111.58, -1.97 }, { 29.84, 110.67, -1.97 }, { 32.7, 109.24, -1.97 }, { 36.01, 107.73, -1.97 }, { 27.75, 109.98, -1.97 }, { 33.96, 107.38, -1.97 } } --小怪坐标 和上边对应
x050002_g_MonstorPosList[2] = { { 63.65, 130.45, -3.37 }, { 68.66, 135.33, -3.37 }, { 70.09, 132.49, -3.37 }, { 70.03, 129.31, -3.37 }, { 69.17, 126.13, -3.37 }, { 67.36, 124.47, -3.37 }, { 67.02, 137.12, -3.37 }, { 67.02, 134.76, -3.37 }, { 66.86, 132.44, -3.37 }, { 68.18, 129.68, -3.37 }, { 66.23, 126.73, -3.37 }, { 66.23, 126.73, -3.37 }, { 65.7, 123.3, -3.37 }, { 66.12, 130.04, -3.37 }, { 65.14, 134.36, -3.37 }, { 64.4, 125.87, -3.37 } }
x050002_g_MonstorPosList[3] = { { 102.73, 113.24, -3.9 }, { 103.42, 110.45, -3.9 }, { 107.64, 114.08, -3.9 }, { 108.09, 111.74, -3.9 }, { 107.09, 109.42, -3.9 }, { 105.28, 107.98, -3.9 }, { 103.04, 107.81, -3.9 }, { 107.01, 116.73, -3.9 }, { 105.39, 112.92, -3.9 }, { 100.47, 107.61, -3.9 } }
x050002_g_MonstorPosList[4] = { { 105.25, 72.04, 1.04 }, { 103.7, 69.51, 1.04 }, { 108.4, 69.86, 1.04 }, { 101.57, 73.43, 1.04 } }
x050002_g_MonstorPosList[5] = { { 60.74, 34.54, 0.64 } }
x050002_g_MonstorPosList[6] = { { 59.09, 33.25, 0.66 }, { 59.48, 30.11, 0.66 }, { 56, 34.42, 0.66 }, { 60.53, 25.53, 0.66 }, { 58.42, 27.45, 0.66 }, { 56.54, 29.43, 0.66 }, { 54.68, 31.57, 0.66 }, { 52.67, 33.96, 0.66 }, { 50.84, 36.06, 0.66 } }

-- 宝箱怪物寻路，路径表ID（NpcPatroPath）
--和 x050002_g_MonstorList[6] = { 490014, 490013, 490013 } 对应
x050002_g_MoveListId = {}
x050002_g_MoveListId[1] = { 2100, 2110, 2120, 2210, 2220, 2230, 2240, 2250, 2260 }

x050002_g_TargetList = {}                      --目标
x050002_g_TargetList[1] = { { 490100, 490101 } }  --{{第1阶段目标1},{第1阶段目标2}}e
x050002_g_TargetList[2] = { { 491101, 491102 } }  --{{第2阶段目标1},{第2阶段目标2}}
x050002_g_TargetList[3] = { { 493103, 492102 } }
x050002_g_TargetList[4] = { { 494104, 494105 } }
x050002_g_TargetList[5] = { { 495105 } }
x050002_g_TargetList[6] = { { 497109, 497110 }, { 496108 } }

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

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

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

x050002_g_BossNumEnumId = 987
x050002_g_BossNumCount = 3

x050002_g_BoxNumEnumId = 138
x050002_g_BoxNumCount = 3
x050002_g_BoxCountDown = 11  -- 刷宝箱之前的倒数时间（需要同时修改前端时间）

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

x050002_g_BigBoxId = 497109   -- 大包厢ID
x050002_g_BigBoxId2 = 497110   -- 大包厢ID
x050002_g_SmallBoxId = 496108   -- 小包厢ID

x050002_g_MovCdTime = 10

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

x050002_g_sSeedAddBuff = 19547002--角色BUFF

function x050002_OnCopySceneOpen(scene)
    --  这里加任务   五个ID务必按照顺序排列  任务加好之后把前边两个横杠去掉
    AddCSMission(scene, 1860)
    AddCSMission(scene, 1861)
    AddCSMission(scene, 1862)
    AddCSMission(scene, 1863)
    AddCSMission(scene, 1864)
end

function x050002_OnCopySceneQuit(scene)
    RemoveAllPlayerImpact(scene, x050002_g_sSeedAddBuff)
end

function x050002_OnCopySceneTimeOut(scene)
    RemoveAllPlayerImpact(scene, x050002_g_sSeedAddBuff)
end

function x050002_SyncBoxInfo(scene, objId)
    local CSevalue = GetCSEvaluaion(scene)
    local curStage = GetCopySceneParam(scene, x050002_g_CSParamIdxCurStage)
    local curSubStage = GetCopySceneParam(scene, x050002_g_CSParamIdxCurSubStage)
    if CSevalue == 0 and curStage == 5 and curSubStage > x050002_g_BoxCountDown and curSubStage <= x050002_g_ClearBoxTime then
        SyncSLBossBoxTime(scene, objId, x050002_g_ClearBoxTime - curSubStage, x050002_g_ClearBoxTime - x050002_g_BoxCountDown - 1)
    end
end

function x050002_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, x050002_g_BoxNumEnumId) % 100 <= x050002_g_BoxNumCount) then
            if rolebaseId == x050002_g_BigBoxId or rolebaseId == x050002_g_BigBoxId2 then
                Flag = CreateSLBossBox(scene, nUserObjId, true, MonId, true)
            elseif rolebaseId == x050002_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 x050002_StageControl(scene, nstage)
    SetCopySceneParam(scene, 2, 0)
    SetCopySceneParam(scene, 3, 0)
    SetCopySceneParam(scene, 4, 0)
    SetCopySceneParam(scene, 5, 100)
    SetCopySceneParam(scene, 6, x050002_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 x050002_OnDieCountDown1(scene, rolebaseId, stage)
    local count = #x050002_g_TargetList[stage + 1]
    for k = 1, count do
        local count2 = #x050002_g_TargetList[stage + 1][k]
        for j = 1, count2 do
            local TargetId = x050002_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 x050002_OnObjEnter(scene, objId)
    --CopySceneNPC(scene,1001101,51001,6,69,1,"XXX")--场景、角色ID、CharModel表ID、势力、出现的场景SceneID、等级、名称
    SynCsStageData(scene, objId, true)
    x050002_SyncBoxInfo(scene, objId)
    SendAllPlayerImpact(scene, x050002_g_sSeedAddBuff)
end

function x050002_Stage_Eff(scene, stage)
    if stage == 1 then
        ShowCopySceneGuideEffect(scene, 901101)
    elseif stage == 2 then
        ShowCopySceneGuideEffect(scene, 901102)
    elseif stage == 3 then
        ShowCopySceneGuideEffect(scene, 901103)
    elseif stage == 4 then
        ShowCopySceneGuideEffect(scene, 901104)
    end
end

--死亡回调
function x050002_OnObjDie(scene, objId, rolebaseId, killerId)
    local nCurStage = GetCopySceneParam(scene, x050002_g_CSParamIdxCurStage)
    local MonCount = GetHostileMonsterCount(scene)
    if nCurStage == 5 and rolebaseId ~= x050002_g_BigBoxId and rolebaseId ~= x050002_g_SmallBoxId and rolebaseId ~= x050002_g_BigBoxId2 then
        return    -- 不是宝箱怪物进入宝箱阶段的死亡回调直接return
    end
    local oldStage = GetCopySceneParam(scene, 7)
    x050002_OnDieCountDown1(scene, rolebaseId, oldStage)
    if (rolebaseId == x050002_g_BigBoxId or rolebaseId == x050002_g_SmallBoxId or rolebaseId == x050002_g_BigBoxId2) then
        x050002_OnBoxDie(scene, objId, rolebaseId)
    end
    if objId == GetUserObjIdByIdx(scene, 0) then
        RemoveAllPlayerImpact(scene, x050002_g_sSeedAddBuff)
    end
    if (rolebaseId == x050002_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, x050002_g_BossNumEnumId) % 1000 <= x050002_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
            x050002_Succeed(scene)
        end
        SetCopySceneParam(scene, x050002_g_CSParamIdxCurStage, nCurStage + 1)
        SetCopySceneParam(scene, x050002_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, x050002_g_CSParamIdxHasBox, 1)
        end
        return
    end
    if MonCount == 0 and nCurStage ~= 4 then
        SetCopySceneParam(scene, x050002_g_CSParamIdxCurStage, nCurStage + 1)
        SetCopySceneParam(scene, x050002_g_CSParamIdxCurSubStage, 0)
    end
end


--副本Tick
function x050002_OnCopySceneTick(scene, tickCount)
    local curStage = GetCopySceneParam(scene, x050002_g_CSParamIdxCurStage)
    local curSubStage = GetCopySceneParam(scene, x050002_g_CSParamIdxCurSubStage)
    SetCopySceneParam(scene, x050002_g_CSParamIdxCurSubStage, curSubStage + 1)
    -- x050002_BoxHpTick(scene)
    if curStage < 5 then
        x050002_Stage_0(scene, curStage, curSubStage)
    elseif curStage == 5 then
        -- x050002_HpCountDown1 (scene)
        local LastCSEvalue = GetCopySceneParam(scene, x050002_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
                x050002_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, x050002_g_CSParamIdxHasBox, 1)
                x050002_Stage_Box(scene, curStage, curSubStage)
            end
        elseif LastCSEvalue ~= 0 then
            x050002_Stage_Box(scene, curStage, curSubStage)
        end
    elseif curStage == 6 then
        RemoveAllPlayerImpact(scene, x050002_g_sSeedAddBuff)
        SetCopySceneSuccess(scene)
    end
end


--刷小怪阶段
function x050002_Stage_0(scene, stage, substage)
    if substage == 1 then
        TellPlayerAutoCombat(scene, true)
        OnCSMissionStage(scene, stage)
        x050002_StageControl(scene, stage)
        if stage ~= 0 then
            CSEvalueBeginNextStage(scene)
        end
        if stage == 4 then
            StoryAnimation(scene, 2002)
            SetCSEvaluateIsBossStage(scene, true)
        end
        x050002_Stage_Eff(scene, stage)
        local count = #x050002_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, x050002_g_MonstorList[stage + 1][i], x050002_g_MonstorPosList[stage + 1][i][3], x050002_g_MonstorPosList[stage + 1][i][1], x050002_g_MonstorPosList[stage + 1][i][2])
                SendImpactToUnit(scene, MonId, x050002_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)
        TellPlayerAutoCombat(scene, true)
        if nUserCount < 1 then
            nUserCount = 1
        end
        local count = #x050002_g_MonstorList[stage + 1]
        for i = 1, count do
            MonId = CreateMonster(scene, x050002_g_MonstorList[stage + 1][i], x050002_g_MonstorPosList[stage + 1][i][3], x050002_g_MonstorPosList[stage + 1][i][1], x050002_g_MonstorPosList[stage + 1][i][2])
            SendImpactToUnit(scene, MonId, x050002_g_LevelAddBuff[1][nUserCount])
            SetHpFull(scene, MonId)
            UpdateObjHpMax(scene, MonId)
        end
    end
end

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

    end
end

--最后阶段
function x050002_Stage_1(scene, stage, substage)
    if substage == 1 then
        SetCopySceneParam(scene, x050002_g_CSParamIdxCurStage, stage + 1)
        SetCopySceneParam(scene, x050002_g_CSParamIdxCurSubStage, 0)
    end
end

function x050002_Stage_Box(scene, stage, substage)
    local nUserCount = GetSceneUserCount(scene)
    if substage == 0 then
        x050002_StageControl(scene, stage)
        for i = 0, nUserCount - 1 do
            local nUserObjId = GetUserObjIdByIdx(scene, i)
            SetMissionMD(scene, nUserObjId, 4, 375, 1)
            CSEvalueEnd(scene, nUserObjId, false)
            x050002_CheckMissionMD(scene, nUserObjId, x050002_g_BoxNumEnumId)
            local oldTime = GetMissionMD(scene, nUserObjId, 4, x050002_g_BoxNumEnumId)
            SetMissionMD(scene, nUserObjId, 4, x050002_g_BoxNumEnumId, oldTime + 1)
        end
    elseif substage == x050002_g_BoxCountDown then
        for i = 0, nUserCount - 1 do
            local nUserObjId = GetUserObjIdByIdx(scene, i)
            x050002_SyncBoxInfo(scene, nUserObjId)
        end
        local MonId
        local nUserCount = GetSceneUserCount(scene)
        if nUserCount < 1 then
            nUserCount = 1
        end
        local count = #x050002_g_MonstorList[stage + 1]
        for i = 1, count do
            MonId = CreateMonster(scene, x050002_g_MonstorList[stage + 1][i], x050002_g_MonstorPosList[stage + 1][i][3], x050002_g_MonstorPosList[stage + 1][i][1], x050002_g_MonstorPosList[stage + 1][i][2])
            SendImpactToUnit(scene, MonId, x050002_g_LevelAddBuff[1][nUserCount])
            SetHpFull(scene, MonId)
            UpdateObjHpMax(scene, MonId)
            NpcStartPatrol(scene, MonId, x050002_g_MoveListId[1][i])--怪物寻路
        end
    elseif substage == x050002_g_ClearBoxTime then
        DeleHostileMonster(scene)
        SetCopySceneParam(scene, x050002_g_CSParamIdxCurStage, stage + 1)
        SetCopySceneParam(scene, x050002_g_CSParamIdxCurSubStage, 0)
    end
end

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

function x050002_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, x050002_g_BoxNumEnumId) % 100 <= x050002_g_BoxNumCount) then
            CreateSLBossBox(scene, nUserObjId, IsKill, MonId, false)
        end
    end
end

function x050002_HpCountDown1(scene)
    local OldHp = math.ceil(GetCopySceneParam(scene, x050002_g_CSParam1))
    local count = #x050002_g_MonstorList[6]
    for j = 1, count do
        local ZhobjId2 = GetObjIdByRoleBase(scene, x050002_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 = #x050002_g_DropHp[1]
        for i = 1, HpCount do
            if NewHp > x050002_g_DropHp[1][i] then
                TargetHp = x050002_g_DropHp[1][i]
                break
            end
        end
        SetCopySceneParam(scene, x050002_g_CSParam1, NewHp)
        SetCopySceneParam(scene, x050002_g_CSParam2, TargetHp)
        local nUserCount = GetSceneUserCount(scene)
        for i = 0, nUserCount - 1 do
            local nUserObjId = GetUserObjIdByIdx(scene, i)
            SynCsStageData(scene, nUserObjId)
        end
    end
end