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

x050001_g_scriptId = 050001

--副本参数
x050001_g_CSParamIdxCurStage = 0              --大阶段
x050001_g_CSParamIdxCurSubStage = 1           --小阶段
x050001_g_CSParam1 = 5
x050001_g_CSParam2 = 6

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

x050001_g_MonstorList = {}
x050001_g_MonstorList[1] = { 490001, 490002, 490002, 490002, 490000, 490000, 490000, 490000, 490000, 490000, 490000, 490000 }  --小怪
x050001_g_MonstorList[2] = { 490004, 490005, 490003, 490003, 490003, 490003, 490003, 490003, 490003, 490003, 490003, 490003, 490003, 490003, 490003 }  --小怪
x050001_g_MonstorList[3] = { 490006, 490007, 490007, 490007 }  --小怪
x050001_g_MonstorList[4] = { 490008, 490009, 490003, 490003, 490003, 490003, 490003, 490003, 490003, 490003 }  --小怪
x050001_g_MonstorList[5] = { 490012, 490010, 490010, 490011, 490011 }  --小怪   麻烦把boss放在第一个
x050001_g_MonstorList[6] = { 490014, 490017, 490017, 490013, 490013, 490013, 490013, 490013, 490013 } -- 宝箱怪物

x050001_g_MonstorPosList = {}
x050001_g_MonstorPosList[1] = { { 62.26, 45.4, -1.57 }, { 58.85, 45.94, -1.57 }, { 62.28, 47.19, -1.57 }, { 65.63, 46.14, -1.57 }, { 57.35, 47.42, -1.57 }, { 59.57, 47.51, -1.57 }, { 64.36, 47.58, -1.57 }, { 66.79, 47.62, -1.57 }, { 58.13, 49.45, -1.57 }, { 60.89, 49.72, -1.57 }, { 63.05, 49.93, -1.57 }, { 65.96, 49.73, -1.57 } } --小怪坐标 和上边对应
x050001_g_MonstorPosList[2] = { { 63.35, 66.11, -1.57 }, { 60.81, 66, -1.57 }, { 61.24, 69.46, -1.57 }, { 62.14, 67.92, -1.57 }, { 63.32, 69.55, -1.57 }, { 59.31, 69.6, -1.57 }, { 57.12, 69.91, -1.57 }, { 65.23, 70.07, -1.57 }, { 67.22, 70.58, -1.57 }, { 60.03, 67.87, -1.57 }, { 57.97, 67.89, -1.57 }, { 64.02, 68.12, -1.57 }, { 65.79, 68.12, -1.57 }, { 60.1, 71.66, -1.57 }, { 65.15, 72.04, -1.57 } }
x050001_g_MonstorPosList[3] = { { 61.84, 105.1, -1.57 }, { 63.74, 105.1, -1.57 }, { 59.05, 105.1, -1.57 }, { 66.22, 105.1, -1.57 } }
x050001_g_MonstorPosList[4] = { { 33.85, 125.97, -0.11 }, { 33.85, 128.88, -0.11 }, { 30.08, 122.82, -0.11 }, { 30.08, 126.46, -0.11 }, { 27.5, 125.02, -0.11 }, { 27.64, 130.77, -0.11 }, { 30.08, 133.43, -0.11 }, { 30.08, 129.69, -0.11 }, { 27.64, 127.26, -0.11 }, { 27.64, 128.72, -0.11 } }
x050001_g_MonstorPosList[5] = { { 62.09, 174.56, -1.57 }, { 59.1, 176.81, -1.57 }, { 60.9, 176.81, -1.57 }, { 63.87, 176.81, -1.57 }, { 65.71, 176.81, -1.57 } }
x050001_g_MonstorPosList[6] = { { 61.82, 174.73, -1.57 }, { 58.46, 175.95, -1.57 }, { 65.98, 175.95, -1.57 }, { 53.63, 177.41, -1.57 }, { 56.92, 177.41, -1.57 }, { 60.38, 177.41, -1.57 }, { 63.7, 177.41, -1.57 }, { 67.67, 177.41, -1.57 }, { 71.22, 177.41, -1.57 } }

-- 宝箱怪物寻路，路径表ID（NpcPatroPath）
--和 x050001_g_MonstorList[6] = { 490014, 490013, 490013 } 对应
x050001_g_MoveListId = {}
x050001_g_MoveListId[1] = { 1100, 1110, 1120, 1210, 1220, 1230, 1240, 1250, 1260 }

x050001_g_TargetList = {}                      --目标
x050001_g_TargetList[1] = { { 490000, 490001, 490002 } }  --{{第1阶段目标},{第1阶段目标2}}
x050001_g_TargetList[2] = { { 490004, 490005, 490003 } }  --{{第2阶段目标1},{第2阶段目标2}}
x050001_g_TargetList[3] = { { 490006, 490007 } }
x050001_g_TargetList[4] = { { 490008, 490009, 490003 } }
x050001_g_TargetList[5] = { { 490012 } }
x050001_g_TargetList[6] = { { 490014, 490017 }, { 490013 } }

x050001_g_DeleListId = {}
x050001_g_DeleListId[1] = { 490010, 490010, 490011, 490011 }    -- 最后需要删除的怪物Id

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

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

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

x050001_g_BossNumEnumId = 987
x050001_g_BossNumCount = 3

x050001_g_BoxNumEnumId = 138
x050001_g_BoxNumCount = 3
x050001_g_BoxCountDown = 11  -- 刷宝箱之前的倒数时间（需要同时修改前端时间）

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

x050001_g_BigBoxId = 490014   -- 大包厢ID
x050001_g_BigBoxId2 = 490017   -- 大包厢ID

x050001_g_SmallBoxId = 490013   -- 小包厢ID

x050001_g_MovCdTime = 10

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

x050001_g_sSeedAddBuff = 1001 --加速buff
x050001_g_TargetId = 4 -- 完成3阶段加速

--任务
function x050001_OnCopySceneOpen(scene)
    AddCSMission(scene, 1850)
    AddCSMission(scene, 1851)
    AddCSMission(scene, 1852)
    AddCSMission(scene, 1853)
    AddCSMission(scene, 1854)
end

function x050001_StageControl(scene, nstage)
    SetCopySceneParam(scene, 2, 0)
    SetCopySceneParam(scene, 3, 0)
    SetCopySceneParam(scene, 5, 100)
    SetCopySceneParam(scene, 6, x050001_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 x050001_OnDieCountDown1(scene, objId, rolebaseId, stage)
    local count = #x050001_g_TargetList[stage + 1]
    for k = 1, count do
        local count2 = #x050001_g_TargetList[stage + 1][k]
        for j = 1, count2 do
            local TargetId = x050001_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 x050001_OnObjEnter(scene, objId)
    --CopySceneNPC(scene,1001101,51001,6,68,1,"XXX")--场景、角色ID、CharModel表ID、势力、出现的场景SceneID、等级、名称
    SynCsStageData(scene, objId, true)
    x050001_SyncBoxInfo(scene, objId)
end

--设置Boss时间
function x050001_SyncBoxInfo(scene, objId)
    local CSevalue = GetCSEvaluaion(scene)
    local curStage = GetCopySceneParam(scene, x050001_g_CSParamIdxCurStage)
    local curSubStage = GetCopySceneParam(scene, x050001_g_CSParamIdxCurSubStage)
    if CSevalue == 0 and curStage == 5 and curSubStage > x050001_g_BoxCountDown and curSubStage <= x050001_g_ClearBoxTime then
        SyncSLBossBoxTime(scene, objId, x050001_g_ClearBoxTime - curSubStage, x050001_g_ClearBoxTime - x050001_g_BoxCountDown - 1)
    end
end

--Boss死亡掉落
function x050001_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, x050001_g_BoxNumEnumId) % 100 <= x050001_g_BoxNumCount) then
            if rolebaseId == x050001_g_BigBoxId or rolebaseId == x050001_g_BigBoxId2 then
                Flag = CreateSLBossBox(scene, nUserObjId, true, MonId, true)
            elseif rolebaseId == x050001_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 x050001_OnObjDie(scene, objId, rolebaseId, killerId)
    local nCurStage = GetCopySceneParam(scene, x050001_g_CSParamIdxCurStage)
    if nCurStage == 5 and rolebaseId ~= x050001_g_BigBoxId and rolebaseId ~= x050001_g_SmallBoxId and rolebaseId ~= x050001_g_BigBoxId2 then
        return -- 不是宝箱怪物进入宝箱阶段的死亡回调直接return
    end
    local MonCount = GetHostileMonsterCount(scene)
    local oldStage = GetCopySceneParam(scene, 7)
    x050001_OnDieCountDown1(scene, objId, rolebaseId, oldStage)
    if (rolebaseId == x050001_g_BigBoxId or rolebaseId == x050001_g_SmallBoxId or rolebaseId == x050001_g_BigBoxId2) then
        x050001_OnBoxDie(scene, objId, rolebaseId)
    end
    if (rolebaseId == x050001_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, x050001_g_BossNumEnumId) % 1000 <= x050001_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
            x050001_Succeed(scene)
        end
        SetCopySceneParam(scene, x050001_g_CSParamIdxCurStage, nCurStage + 1)
        SetCopySceneParam(scene, x050001_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, x050001_g_CSParamIdxHasBox, 1)
        end
        return
    end
    if MonCount == 0 and nCurStage ~= 4 then
        SetCopySceneParam(scene, x050001_g_CSParamIdxCurStage, nCurStage + 1)
        SetCopySceneParam(scene, x050001_g_CSParamIdxCurSubStage, 0)

    end
end

--删除怪物ID
function x050001_ClearAllMonster(scene)
    local count = #x050001_g_DeleListId[1]
    for i = 1, count do
        DeleMonsterByRoleId(scene, x050001_g_DeleListId[1][i])
    end
end

--副本Tick
function x050001_OnCopySceneTick(scene, tickCount)
    local curStage = GetCopySceneParam(scene, x050001_g_CSParamIdxCurStage)
    local curSubStage = GetCopySceneParam(scene, x050001_g_CSParamIdxCurSubStage)
    SetCopySceneParam(scene, x050001_g_CSParamIdxCurSubStage, curSubStage + 1)
    -- x050001_BoxHpTick(scene)
    if curStage < 5 then
        x050001_Stage_0(scene, curStage, curSubStage)
    elseif curStage == 5 then
        local LastCSEvalue = GetCopySceneParam(scene, x050001_g_CSParamIdxHasBox)
        --x050001_HpCountDown1(scene)
        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
                x050001_Stage_1(scene, curStage, curSubStage)
            elseif GetCSEvaluaion(scene) == 0 then
                --  判断是否是S级评价 S级评价刷宝箱
                local nUserCount = GetSceneUserCount(scene)
                for i = 0, nUserCount - 1 do
                    local nUserObjId = GetUserObjIdByIdx(scene, i)
                    CSEvalueEnd(scene, nUserObjId)
                end
                SetCopySceneParam(scene, x050001_g_CSParamIdxHasBox, 1)
                x050001_Stage_Box(scene, curStage, curSubStage)
            end
        elseif LastCSEvalue ~= 0 then
            x050001_Stage_Box(scene, curStage, curSubStage)
        end
    elseif curStage == 6 then
        RemoveAllPlayerImpact(scene, x050001_g_sSeedAddBuff)
        SetCopySceneSuccess(scene)
    end
end

function x050001_Stage_Eff(scene, stage)
    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 x050001_Stage_0(scene, stage, substage)
    if substage == 1 then
        TellPlayerAutoCombat(scene, true)
        x050001_StageControl(scene, stage)
        OnCSMissionStage(scene, stage)
        if stage ~= 0 then
            CSEvalueBeginNextStage(scene)
        end
        x050001_Stage_Eff(scene, stage)
        if stage == 4 then
            StoryAnimation(scene, 2001)
            SetCSEvaluateIsBossStage(scene, true)
        end
        local MonId
        local nUserCount = GetSceneUserCount(scene)
        if nUserCount < 1 then
            nUserCount = 1
        end
        local count = #x050001_g_MonstorList[stage + 1]
        if stage == x050001_g_TargetId then
            --加buff
            SendAllPlayerImpact(scene, x050001_g_sSeedAddBuff)
        end
        for i = 1, count do
            if stage ~= 4 then
                MonId = CreateMonster(scene, x050001_g_MonstorList[stage + 1][i], x050001_g_MonstorPosList[stage + 1][i][3], x050001_g_MonstorPosList[stage + 1][i][1], x050001_g_MonstorPosList[stage + 1][i][2])
                SendImpactToUnit(scene, MonId, x050001_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 = #x050001_g_MonstorList[stage + 1]
        for i = 1, count do
            MonId = CreateMonster(scene, x050001_g_MonstorList[stage + 1][i], x050001_g_MonstorPosList[stage + 1][i][3], x050001_g_MonstorPosList[stage + 1][i][1], x050001_g_MonstorPosList[stage + 1][i][2])
            SendImpactToUnit(scene, MonId, x050001_g_LevelAddBuff[1][nUserCount])
            SetHpFull(scene, MonId)
            UpdateObjHpMax(scene, MonId)
        end
    end
end

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

--最后阶段
function x050001_Stage_1(scene, stage, substage)
    if substage == 1 then
        SetCopySceneParam(scene, x050001_g_CSParamIdxCurStage, stage + 1)
        SetCopySceneParam(scene, x050001_g_CSParamIdxCurSubStage, 0)
    end
end

--怪物宝箱
function x050001_Stage_Box(scene, stage, substage)
    local nUserCount = GetSceneUserCount(scene)
    if substage == 0 then
        x050001_StageControl(scene, stage)
        for i = 0, nUserCount - 1 do
            local nUserObjId = GetUserObjIdByIdx(scene, i)
            SetMissionMD(scene, nUserObjId, 4, 374, 1)
            CSEvalueEnd(scene, nUserObjId, false)
            x050001_CheckMissionMD(scene, nUserObjId, x050001_g_BoxNumEnumId)
            local oldTime = GetMissionMD(scene, nUserObjId, 4, x050001_g_BoxNumEnumId)
            SetMissionMD(scene, nUserObjId, 4, x050001_g_BoxNumEnumId, oldTime + 1)
        end
    elseif substage == x050001_g_BoxCountDown then
        RemoveAllPlayerImpact(scene, x050001_g_sSeedAddBuff)
        for i = 0, nUserCount - 1 do
            local nUserObjId = GetUserObjIdByIdx(scene, i)
            x050001_SyncBoxInfo(scene, nUserObjId)
        end
        local nUserCount = GetSceneUserCount(scene)
        if nUserCount < 1 then
            nUserCount = 1
        end
        local count = #x050001_g_MonstorList[stage + 1]
        for i = 1, count do
            local MonId = CreateMonster(scene, x050001_g_MonstorList[stage + 1][i], x050001_g_MonstorPosList[stage + 1][i][3], x050001_g_MonstorPosList[stage + 1][i][1], x050001_g_MonstorPosList[stage + 1][i][2])
            SendImpactToUnit(scene, MonId, x050001_g_LevelAddBuff[1][nUserCount])
            SetHpFull(scene, MonId)
            UpdateObjHpMax(scene, MonId)
            NpcStartPatrol(scene, MonId, x050001_g_MoveListId[1][i])--怪物寻路
        end
    elseif substage == x050001_g_ClearBoxTime then
        -- 删除宝箱
        DeleHostileMonster(scene)
        SetCopySceneParam(scene, x050001_g_CSParamIdxCurStage, stage + 1)
        SetCopySceneParam(scene, x050001_g_CSParamIdxCurSubStage, 0)
    end
end

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

function x050001_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, x050001_g_BoxNumEnumId) % 100 <= x050001_g_BoxNumCount) then
            CreateSLBossBox(scene, nUserObjId, IsKill, MonId, false)
        end
    end
end

function x050001_HpCountDown1(scene)
    local OldHp = math.ceil(GetCopySceneParam(scene, x050001_g_CSParam1))
    local count = #x050001_g_MonstorList[6]
    for j = 1, count do
        local ZhobjId2 = GetObjIdByRoleBase(scene, x050001_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 = #x050001_g_DropHp[1]
        for i = 1, HpCount do
            if NewHp > x050001_g_DropHp[1][i] then
                TargetHp = x050001_g_DropHp[1][i]
                break
            end
        end
        SetCopySceneParam(scene, x050001_g_CSParam1, NewHp)
        SetCopySceneParam(scene, x050001_g_CSParam2, TargetHp)
        local nUserCount = GetSceneUserCount(scene)
        for i = 0, nUserCount - 1 do
            local nUserObjId = GetUserObjIdByIdx(scene, i)
            SynCsStageData(scene, nUserObjId)
        end
    end
end