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

x050003_g_scriptId = 050003

--副本参数
x050003_g_CSParamIdxCurStage = 0              --大阶段
x050003_g_CSParamIdxCurSubStage = 1           --小阶段

x050003_g_CSParam1 = 5
x050003_g_CSParam2 = 6

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

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

x050003_g_MonstorList = {}
x050003_g_MonstorList[1] = { 495201, 495200, 495200, 495200, 495200, 495200, 495200, 495200, 495200, 495200, 495200, 495200, 495200, 495200, 495200, 495200 }  --小怪
x050003_g_MonstorList[2] = { 495203, 495202, 495202, 495202, 495202, 495202, 495202, 495202, 495202, 495202, 495202, 495202, 495202, 495202, 495202, 495202, 495202, 495202 }  --小怪
x050003_g_MonstorList[3] = { 495205, 495204, 495204, 495204, 495204, 495204, 495204, 495204, 495204, 495204, 495204, 495204, 495204, 495204, 495204, 495204, 495204, 495204 }  --小怪
x050003_g_MonstorList[4] = { 495207, 495206, 495206, 495206 }  --小怪
x050003_g_MonstorList[5] = { 495208 }  --小怪   麻烦把boss放在第一个
x050003_g_MonstorList[6] = { 495252, 495253, 495253, 495251, 495251, 495251, 495251, 495251, 495251 }

x050003_g_MonstorPosList = {}
x050003_g_MonstorPosList[1] = { { 47.86, 68.59, -1.89 }, { 50.07, 69.43, -1.89 }, { 46.86, 70.41, -1.89 }, { 44.82, 70.99, -1.89 }, { 52.16, 69.16, -1.89 }, { 43.66, 72.92, -1.89 }, { 45.7, 72.34, -1.89 }, { 54.09, 70.47, -1.89 }, { 52.05, 71.05, -1.89 }, { 48.94, 71.74, -1.89 }, { 47.42, 74, -1.89 }, { 51.31, 72.87, -1.89 }, { 44.85, 75.28, -1.89 }, { 54.57, 72.89, -1.89 }, { 47.95, 76.05, -1.89 }, { 51.9, 75.08, -1.89 } } --小怪坐标 和上边对应
x050003_g_MonstorPosList[2] = { { 41.1, 111.74, -1.35 }, { 43.14, 113.63, -1.47 }, { 39.11, 112.66, -1.47 }, { 36.18, 114.11, -1.47 }, { 36.79, 111.9, -1.47 }, { 46.11, 114.42, -1.47 }, { 44.28, 116.11, -1.47 }, { 40.24, 115.1, -1.47 }, { 38.16, 114.7, -1.47 }, { 33.22, 115.39, -1.47 }, { 36.22, 116.39, -1.47 }, { 38.59, 116.95, -1.47 }, { 40.5, 117.33, -1.47 }, { 42.81, 117.76, -1.47 }, { 46.48, 118.63, -1.47 }, { 34.09, 113.04, -1.47 }, { 46.76, 116.52, -1.47 }, { 42.1, 115.51, -1.47 } }
x050003_g_MonstorPosList[3] = { { 63.31, 143.67, -2.5 }, { 63.08, 145.76, -2.5 }, { 62.33, 147.73, -2.5 }, { 60.08, 148.16, -2.5 }, { 60.31, 150.34, -2.5 }, { 63.15, 149.66, -2.5 }, { 65.82, 143.62, -2.5 }, { 68.1, 143.16, -2.5 }, { 68.99, 141.17, -2.5 }, { 69.29, 144.31, -2.5 }, { 71.9, 143.62, -2.5 }, { 66.7, 147.29, -2.5 }, { 65.37, 146.01, -2.5 }, { 65.21, 149.5, -2.5 }, { 69.76, 146.48, -2.5 }, { 72.58, 146.14, -2.5 }, { 68.29, 149.4, -2.5 }, { 63.63, 152.26, -2.5 } }
x050003_g_MonstorPosList[4] = { { 116.65, 147.54, -3.14 }, { 116.74, 144.71, -3.14 }, { 118.6, 150.23, -3.14 }, { 118.6, 142.27, -3.14 } }
x050003_g_MonstorPosList[5] = { { 157.2, 146.34, -3.14 } }
x050003_g_MonstorPosList[6] = { { 159.57, 146.32, -3.14 }, { 161.18, 150.33, -3.14 }, { 161.31, 142.15, -3.14 }, { 164, 155.02, -3.14 }, { 164.04, 151.62, -3.14 }, { 164, 148.24, -3.14 }, { 164.07, 143.92, -3.14 }, { 164.07, 140.91, -3.14 }, { 164.07, 137.36, -3.14 } }

-- 宝箱怪物寻路，路径表ID（NpcPatroPath）
--和 x050001_g_MonstorList[6] = { 490014, 490013, 490013 } 对应
x050003_g_MoveListId = {}
x050003_g_MoveListId[1] = { 3100, 3110, 3120, 3210, 3220, 3230, 3240, 3250, 3260 }

x050003_g_TargetList = {}                      --目标
x050003_g_TargetList[1] = { { 495201, 495200 } }  --{{第1阶段目标1},{第1阶段目标2}}
x050003_g_TargetList[2] = { { 495203, 495202 } }  --{{第2阶段目标1},{第2阶段目标2}}
x050003_g_TargetList[3] = { { 495205, 495204 } }
x050003_g_TargetList[4] = { { 495207, 495206 } }
x050003_g_TargetList[5] = { { 495208 } }
x050003_g_TargetList[6] = { { 495252, 495253 }, { 495251 } }

x050003_g_BoxNumEnumId = 138
x050003_g_BoxNumCount = 3
x050003_g_BoxCountDown = 11  -- 刷宝箱之前的倒数时间

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

x050003_g_BigBoxId = 495252   -- 大包厢ID
x050003_g_BigBoxId2 = 495253   -- 大包厢ID

x050003_g_SmallBoxId = 495251   -- 小包厢ID

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

x050003_g_BossNumEnumId = 987
x050003_g_BossNumCount = 3

x050003_g_MovCdTime = 10

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

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

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

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

function x050003_OnCopySceneOpen(scene)
    AddCSMission(scene, 1870)
    AddCSMission(scene, 1871)
    AddCSMission(scene, 1872)
    AddCSMission(scene, 1873)
    AddCSMission(scene, 1874)
end
function x050003_SyncBoxInfo(scene, objId)
    local CSevalue = GetCSEvaluaion(scene)
    local curStage = GetCopySceneParam(scene, x050003_g_CSParamIdxCurStage)
    local curSubStage = GetCopySceneParam(scene, x050003_g_CSParamIdxCurSubStage)
    if CSevalue == 0 and curStage == 5 and curSubStage > x050003_g_BoxCountDown and curSubStage <= x050003_g_ClearBoxTime then
        SyncSLBossBoxTime(scene, objId, x050003_g_ClearBoxTime - curSubStage, x050003_g_ClearBoxTime - x050003_g_BoxCountDown - 1)
    end
end

function x050003_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, x050003_g_BoxNumEnumId) % 100 <= x050003_g_BoxNumCount) then
            if rolebaseId == x050003_g_BigBoxId or rolebaseId == x050003_g_BigBoxId2 then
                Flag = CreateSLBossBox(scene, nUserObjId, true, MonId, true)
            elseif rolebaseId == x050003_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 x050003_StageControl(scene, nstage)
    SetCopySceneParam(scene, 2, 0)
    SetCopySceneParam(scene, 3, 0)
    SetCopySceneParam(scene, 4, 0)
    SetCopySceneParam(scene, 5, 100)
    SetCopySceneParam(scene, 6, x050003_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 x050003_OnDieCountDown1(scene, rolebaseId, stage)
    local count = #x050003_g_TargetList[stage + 1]
    for k = 1, count do
        local count2 = #x050003_g_TargetList[stage + 1][k]
        for j = 1, count2 do

            local TargetId = x050003_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 x050003_OnObjEnter(scene, objId)
    --CopySceneNPC(scene,1001101,51001,6,70,1,"XXX")--场景、角色ID、CharModel表ID、势力、出现的场景SceneID、等级、名称
    SynCsStageData(scene, objId, true)
    x050003_SyncBoxInfo(scene, objId)
end

function x050003_Stage_Eff(scene, stage)
    if stage == 1 then
        ShowCopySceneGuideEffect(scene, 902101, 902102)
    elseif stage == 2 then
        ShowCopySceneGuideEffect(scene, 902201)
    elseif stage == 3 then
        ShowCopySceneGuideEffect(scene, 902301)
    elseif stage == 4 then
        ShowCopySceneGuideEffect(scene, 902401)
    end
end

--死亡回调
function x050003_OnObjDie(scene, objId, rolebaseId, killerId)
    local nCurStage = GetCopySceneParam(scene, x050003_g_CSParamIdxCurStage)
    if nCurStage == 5 and rolebaseId ~= x050003_g_BigBoxId and rolebaseId ~= x050003_g_SmallBoxId and rolebaseId ~= x050003_g_BigBoxId2 then
        return    -- 不是宝箱怪物进入宝箱阶段的死亡回调直接return
    end
    local MonCount = GetHostileMonsterCount(scene)
    local oldStage = GetCopySceneParam(scene, 7)
    x050003_OnDieCountDown1(scene, rolebaseId, oldStage)
    if (rolebaseId == x050003_g_BigBoxId or rolebaseId == x050003_g_SmallBoxId or rolebaseId == x050003_g_BigBoxId2) then
        x050003_OnBoxDie(scene, objId, rolebaseId)
    end
    if (rolebaseId == x050003_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, x050003_g_BossNumEnumId) % 1000 <= x050003_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
            x050003_Succeed(scene)
        end
        SetCopySceneParam(scene, x050003_g_CSParamIdxCurStage, nCurStage + 1)
        SetCopySceneParam(scene, x050003_g_CSParamIdxCurSubStage, 0)
        x050003_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, x050003_g_CSParamIdxHasBox, 1)
        end
        return
    end
    if MonCount == 0 and nCurStage ~= 4 then
        SetCopySceneParam(scene, x050003_g_CSParamIdxCurStage, nCurStage + 1)
        SetCopySceneParam(scene, x050003_g_CSParamIdxCurSubStage, 0)

    end
end
function x050003_ClearAllMonster(scene)
    local count = #x050003_g_DeleListId[1]
    for i = 1, count do
        DeleMonsterByRoleId(scene, x050003_g_DeleListId[1][i])
    end
end

--动画/对话回调
--副本Tick
function x050003_OnCopySceneTick(scene, tickCount)
    local curStage = GetCopySceneParam(scene, x050003_g_CSParamIdxCurStage)
    local curSubStage = GetCopySceneParam(scene, x050003_g_CSParamIdxCurSubStage)
    SetCopySceneParam(scene, x050003_g_CSParamIdxCurSubStage, curSubStage + 1)
    -- x050003_BoxHpTick(scene)
    if curStage < 5 then
        x050003_Stage_0(scene, curStage, curSubStage)
    elseif curStage == 5 then
        -- x050003_HpCountDown1(scene)
        local LastCSEvalue = GetCopySceneParam(scene, x050003_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
                x050003_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, x050003_g_CSParamIdxHasBox, 1)
                x050003_Stage_Box(scene, curStage, curSubStage)
            end
        elseif LastCSEvalue ~= 0 then
            x050003_Stage_Box(scene, curStage, curSubStage)
        end
    elseif curStage == 6 then
        SetCopySceneSuccess(scene)
    end
end


--刷小怪阶段
function x050003_Stage_0(scene, stage, substage)
    if substage == 1 then
        TellPlayerAutoCombat(scene, true)
        x050003_StageControl(scene, stage)
        OnCSMissionStage(scene, stage)
        if stage ~= 0 then
            CSEvalueBeginNextStage(scene)
        end
        if stage == 4 then
            StoryAnimation(scene, 2006)
            SetCSEvaluateIsBossStage(scene, true)
        end
        x050003_Stage_Eff(scene, stage)
        local count = #x050003_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, x050003_g_MonstorList[stage + 1][i], x050003_g_MonstorPosList[stage + 1][i][3], x050003_g_MonstorPosList[stage + 1][i][1], x050003_g_MonstorPosList[stage + 1][i][2])
                SendImpactToUnit(scene, MonId, x050003_g_LevelAddBuff[1][nUserCount])
                SetHpFull(scene, MonId)
                UpdateObjHpMax(scene, MonId)
            end
        end
    elseif (substage == 2) and stage == 4 then
        --print("刷出Boss")
        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 = #x050003_g_MonstorList[stage + 1]
        for i = 1, count do
            MonId = CreateMonster(scene, x050003_g_MonstorList[stage + 1][i], x050003_g_MonstorPosList[stage + 1][i][3], x050003_g_MonstorPosList[stage + 1][i][1], x050003_g_MonstorPosList[stage + 1][i][2])
            SendImpactToUnit(scene, MonId, x050003_g_LevelAddBuff[1][nUserCount])
            SetHpFull(scene, MonId)
            UpdateObjHpMax(scene, MonId)
        end
    end
end

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

--最后阶段
function x050003_Stage_1(scene, stage, substage)
    if substage == 1 then
        SetCopySceneParam(scene, x050003_g_CSParamIdxCurStage, stage + 1)
        SetCopySceneParam(scene, x050003_g_CSParamIdxCurSubStage, 0)
    end
end

function x050003_Stage_Box(scene, stage, substage)
    local nUserCount = GetSceneUserCount(scene)
    if substage == 0 then
        x050003_StageControl(scene, stage)
        for i = 0, nUserCount - 1 do
            local nUserObjId = GetUserObjIdByIdx(scene, i)
            SetMissionMD(scene, nUserObjId, 4, 376, 1)
            CSEvalueEnd(scene, nUserObjId, false)
            x050003_CheckMissionMD(scene, nUserObjId, x050003_g_BoxNumEnumId)
            local oldTime = GetMissionMD(scene, nUserObjId, 4, x050003_g_BoxNumEnumId)
            SetMissionMD(scene, nUserObjId, 4, x050003_g_BoxNumEnumId, oldTime + 1)
        end
    elseif substage == x050003_g_BoxCountDown then
        for i = 0, nUserCount - 1 do
            local nUserObjId = GetUserObjIdByIdx(scene, i)
            x050003_SyncBoxInfo(scene, nUserObjId)
        end
        local MonId
        local nUserCount = GetSceneUserCount(scene)
        if nUserCount < 1 then
            nUserCount = 1
        end
        local count = #x050003_g_MonstorList[stage + 1]
        for i = 1, count do
            MonId = CreateMonster(scene, x050003_g_MonstorList[stage + 1][i], x050003_g_MonstorPosList[stage + 1][i][3], x050003_g_MonstorPosList[stage + 1][i][1], x050003_g_MonstorPosList[stage + 1][i][2])
            SendImpactToUnit(scene, MonId, x050003_g_LevelAddBuff[1][nUserCount])
            SetHpFull(scene, MonId)
            UpdateObjHpMax(scene, MonId)
            NpcStartPatrol(scene, MonId, x050003_g_MoveListId[1][i])--怪物寻路
        end
    elseif substage == x050003_g_ClearBoxTime then
        DeleHostileMonster(scene)
        SetCopySceneParam(scene, x050003_g_CSParamIdxCurStage, stage + 1)
        SetCopySceneParam(scene, x050003_g_CSParamIdxCurSubStage, 0)
    end
end

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

function x050003_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, x050003_g_BoxNumEnumId) % 100 <= x050003_g_BoxNumCount) then
            CreateSLBossBox(scene, nUserObjId, IsKill, MonId, false)
        end
    end
end

function x050003_HpCountDown1(scene)
    local OldHp = math.ceil(GetCopySceneParam(scene, x050003_g_CSParam1))
    local count = #x050003_g_MonstorList[6]
    for j = 1, count do
        local ZhobjId2 = GetObjIdByRoleBase(scene, x050003_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 = #x050003_g_DropHp[1]
        for i = 1, HpCount do
            if NewHp > x050003_g_DropHp[1][i] then
                TargetHp = x050003_g_DropHp[1][i]
                break
            end
        end
        SetCopySceneParam(scene, x050003_g_CSParam1, NewHp)
        SetCopySceneParam(scene, x050003_g_CSParam2, TargetHp)
        local nUserCount = GetSceneUserCount(scene)
        for i = 0, nUserCount - 1 do
            local nUserObjId = GetUserObjIdByIdx(scene, i)
            SynCsStageData(scene, nUserObjId)
        end
    end
end