--任务副本1
--进入副本→播放MOV→刷第一波小怪→击杀第一波小怪→刷BOSS→击杀BOSS→副本完成→传出副本

x061000_g_scriptId = 061000

--副本参数
x061000_g_CSParamIdxCurStage = 0              --大阶段
x061000_g_CSParamIdxCurSubStage = 1           --小阶段
x061000_g_CSParam1 = 2                          --
x061000_g_CSParam2 = 3
x061000_g_CSSkillZh50 = 4
x061000_g_CSSkill30 = 5
x061000_g_CSSkill10 = 6

x061000_g_AdapteLvl = 100
x061000_g_CSZhaohuanHp = {}
x061000_g_CSZhaohuanHp[1] = { 101, 101, -2, 50, -2, -2 }

x061000_g_CSParamAnimationID = 5038           --mov动画ID

x061000_g_MonstorList = {}
x061000_g_MonstorList[1] = { { 510100, 510100, 510100 }, { 510200, 510200, 510200 }, { 510200, 510200, 510200 }, { 510000, 510000, 510000 }, { 510000, 510000, 510000 }, { 510000, 510000, 510000 }, { 510000, 510000, 510000 }, { 510000, 510000, 510000 }, { 510000, 510000, 510000 }, { 510000, 510000, 510000 }, { 510000, 510000, 510000 }, { 510000, 510000, 510000 } }  --小怪
x061000_g_MonstorList[2] = { { 510100, 510100, 510100 }, { 510200, 510200, 510200 }, { 510200, 510200, 510200 }, { 510000, 510000, 510000 }, { 510000, 510000, 510000 }, { 510000, 510000, 510000 }, { 510000, 510000, 510000 }, { 510000, 510000, 510000 }, { 510000, 510000, 510000 }, { 510000, 510000, 510000 }, { 510000, 510000, 510000 }, { 510000, 510000, 510000 } }  --小怪
x061000_g_MonstorList[3] = { { 510100, 510100, 510100 }, { 510200, 510200, 510200 }, { 510200, 510200, 510200 }, { 510000, 510000, 510000 }, { 510000, 510000, 510000 }, { 510000, 510000, 510000 }, { 510000, 510000, 510000 }, { 510000, 510000, 510000 }, { 510000, 510000, 510000 }, { 510000, 510000, 510000 }, { 510000, 510000, 510000 }, { 510000, 510000, 510000 } }  --小怪


x061000_g_Boss1 = 510300
x061000_g_Boss2 = 510500
x061000_g_Boss3 = {}
x061000_g_Boss3[1] = { 510700, 510700, 510700 }

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

x061000_g_TarMonstorList = {}
x061000_g_TarMonstorList[1] = { 510100, 510200, 510000 }  --小怪
x061000_g_TarMonstorList[2] = { 510100, 510200, 510000 }  --小怪
x061000_g_TarMonstorList[3] = { 510100, 510200, 510000 }  --小怪
x061000_g_TarMonstorList[4] = { 510300, 510500, 510700 }

x061000_g_Delete = {}
x061000_g_Delete[1] = { 510401, 510601, 510801 }

x061000_g_BossZhaohuanSkillId = 509100    -- 第一种的加强buff
x061000_g_BossZhaohuanSkillId2 = 509200-- 第二种的加强buff
x061000_g_ZhongjiSkillId = -1    -- 第一种的重击技能
x061000_g_ZhongjiSkillIdTick = 999    -- 重击技能延迟

x061000_g_AddImpactId = 75003    --  第三种的互相加持buff

x061000_g_FirstMissionId = 1813    -- 第一个任务
x061000_g_SecMissionId = 1814 -- 第2个任务

x061000_g_MonstorPosList = {}
x061000_g_MonstorPosList[1] = { { 41.13, 68.5, -1.57 }, { 37.79, 71.3, -1.57 }, { 45.13, 71.37, -1.57 }, { 38.46, 68.54, -1.57 }, { 43.75, 68.52, -1.57 }, { 41.22, 70.92, -1.57 }, { 35.56, 70.34, -1.57 }, { 47.01, 69.96, -1.57 }, { 42.27, 66.36, -1.57 }, { 39.88, 72.71, -1.57 }, { 42.96, 72.88, -1.57 }, { 39.96, 66.43, -1.57 } } --小怪坐标
x061000_g_MonstorPosList[2] = { { 68.37, 96.49, -2.37 }, { 66.4, 100.05, -2.37 }, { 72.72, 95.87, -2.37 }, { 67.51, 98.02, -2.37 }, { 69.99, 96.11, -2.37 }, { 65.91, 96.26, -2.37 }, { 69.09, 100.27, -2.37 }, { 71.67, 98.32, -2.37 }, { 68.78, 93.97, -2.37 }, { 63.91, 98.68, -2.37 }, { 71.1, 92.67, -2.37 }, { 66.51, 94.52, -2.37 } } --小怪坐标
x061000_g_MonstorPosList[3] = { { 68.21, 136.45, -1.57 }, { 65.11, 141.91, -1.57 }, { 71.12, 142.36, -1.57 }, { 68.22, 138.94, -1.57 }, { 70.26, 139.19, -1.57 }, { 71.95, 139.12, -1.57 }, { 66.54, 139.22, -1.57 }, { 64.72, 138.81, -1.57 }, { 66.13, 136.82, -1.57 }, { 67.02, 142.19, -1.57 }, { 69.26, 142.45, -1.57 }, { 70.48, 137.01, -1.57 } } --小怪坐标

x061000_g_BossPosList = {}
x061000_g_BossPosList[1] = { { 68.86, 187.46, -1.57 } }
x061000_g_BossPosList[2] = { { 68.86, 187.46, -1.57 } }
x061000_g_BossPosList[3] = { { 68.74, 191.93, -1.57 }, { 64.08, 186.94, -1.57 }, { 73.61, 187.62, -1.57 } }

x061000_g_KuangbaoCD = 5
x061000_g_KuangbaoImpactId = 75002
x061000_g_KuangbaoMonstorId1 = 510401
x061000_g_KuangbaoMonstorId2 = 510401

function x061000_OnCopySceneOpen(scene)
    AddCSMission(scene, 1910)
    AddCSMission(scene, 1911)
    AddCSMission(scene, 1912)
    AddCSMission(scene, 1913)
    local Rand = RandomMon(1, 90) % 3 + 1
    SetCopySceneParam(scene, 10, Rand)
	SetCopySceneParam(scene, 7, Rand * 10)
end
function x061000_ClearAllMonster(scene)
    --print(count)
    local nAdaptedLvl = math.floor(GetAdaptLvl(scene) / 10)
    if nAdaptedLvl > x061000_g_AdapteLvl then
        nAdaptedLvl = x061000_g_AdapteLvl
    end
    for i = 1, 3 do
        local count = #x061000_g_MonstorList[i]
        for j = 1, count do
            for k = 1, 3 do
                DeleMonsterByRoleId(scene, x061000_g_MonstorList[i][j][k] + nAdaptedLvl)
            end
        end
    end
    DeleMonsterByRoleId(scene, x061000_g_Boss1 + nAdaptedLvl)
    DeleMonsterByRoleId(scene, x061000_g_Boss2 + nAdaptedLvl)
    DeleMonsterByRoleId(scene, x061000_g_Boss3[1][1] + nAdaptedLvl)
    DeleMonsterByRoleId(scene, x061000_g_Boss3[1][2] + nAdaptedLvl)
    DeleMonsterByRoleId(scene, x061000_g_Boss3[1][3] + nAdaptedLvl)

    local count = #x061000_g_Delete[1]
    for i = 1, count do
        DeleMonsterByRoleId(scene, x061000_g_Delete[1][i] + nAdaptedLvl)
    end

end

function x061000_Stage_Eff(scene, EffStage)
    --print("放特效")
    --print(EffStage)
    if EffStage == 1 then
        ShowCopySceneGuideEffect(scene, 957101, 957102)
    elseif EffStage == 2 then
        ShowCopySceneGuideEffect(scene, 957201)
    elseif EffStage == 3 then
        ShowCopySceneGuideEffect(scene, 957202)
    end
end
function x061000_AddKuangbaoImpact(scene, tick, Lvl)
    if tick % x061000_g_KuangbaoCD == 0 then
        local MonObj1 = GetObjIdByRoleBase(scene, x061000_g_KuangbaoMonstorId1 + Lvl)
        local MonObj2 = GetObjIdByRoleBase(scene, x061000_g_KuangbaoMonstorId2 + Lvl)
        if (CalculatePercenHp(scene, MonObj1) > 0 or CalculatePercenHp(scene, MonObj2) > 0) then
            --print("上狂暴buff")
            SendImpactToAllByRBID(scene, x061000_g_Boss1 + Lvl, x061000_g_KuangbaoImpactId)
        end
    end
end
--死亡回调
function x061000_StageControl(scene, nstage)
    local Rand = GetCopySceneParam(scene, 10)
    SetCopySceneParam(scene, 2, 0)
    SetCopySceneParam(scene, 3, nstage + 1)
    SetCopySceneParam(scene, 7, nstage + Rand * 10)
    local nUserCount = GetSceneUserCount(scene)
    for j = 0, nUserCount - 1 do
        local nUserObjId = GetUserObjIdByIdx(scene, j)
        SynCsStageData(scene, nUserObjId)
    end
end
function x061000_OnObjEnter(scene, objId)
   
    SynCsStageData(scene, objId, true)
    AutoCombat(scene, objId, true)
end

function x061000_OnDieCountDown1(scene, stage)
    local OldValue = GetCopySceneParam(scene, x061000_g_CSParam1)
    local count = #x061000_g_TarMonstorList[stage]
    if (stage == 4 and count == OldValue + 1) then
        --	print("!!!!!")
        x061000_Success(scene)
        SetCopySceneParam(scene, x061000_g_CSParamIdxCurStage, 6)
    end
    SetCopySceneParam(scene, x061000_g_CSParam1, OldValue + 1)
    local nUserCount = GetSceneUserCount(scene)
    for i = 0, nUserCount - 1 do
        local nUserObjId = GetUserObjIdByIdx(scene, i)
        SynCsStageData(scene, nUserObjId)
    end
end
function x061000_OnObjDie(scene, objId, rolebaseId, killerId)
    local Rand = GetCopySceneParam(scene, 10)
    local nAdaptedLvl = math.floor(GetAdaptLvl(scene) / 10)
    if nAdaptedLvl > x061000_g_AdapteLvl then
        nAdaptedLvl = x061000_g_AdapteLvl
    end
    local nCurStage = GetCopySceneParam(scene, x061000_g_CSParamIdxCurStage)
    local MonCount = GetHostileMonsterCount(scene)
    local oldStage = GetCopySceneParam(scene, 3)
    local Count = #x061000_g_TarMonstorList[oldStage]
    for i = 1, Count do
        if rolebaseId == x061000_g_TarMonstorList[oldStage][i] + nAdaptedLvl then
            x061000_OnDieCountDown1(scene, oldStage)
        end
    end

    x061000_AddImpact(scene, rolebaseId)
    if MonCount == 0 then
        --if(rolebaseId == x061000_g_MonstorList[2][1]) then
        --	x061000_StageControl(scene, 2)
        --	end
        local curStage = GetCopySceneParam(scene, x061000_g_CSParamIdxCurStage)
        if curStage <= 4 then
            SetCopySceneParam(scene, x061000_g_CSParamIdxCurStage, nCurStage + 1)
            SetCopySceneParam(scene, x061000_g_CSParamIdxCurSubStage, 0)
        end
    end
end

function x061000_SkillTick(scene, Rand, tickCount, Lvl)


    local MonObj, skillId, MonRBId
    if Rand == 1 then
        skillId = x061000_g_BossZhaohuanSkillId + Lvl
        MonRBId = x061000_g_Boss1 + Lvl
        MonObj = GetObjIdByRoleBase(scene, x061000_g_Boss1 + Lvl)
    elseif Rand == 2 then
        skillId = x061000_g_BossZhaohuanSkillId2 + Lvl
        MonRBId = x061000_g_Boss2 + Lvl
        MonObj = GetObjIdByRoleBase(scene, x061000_g_Boss2 + Lvl)
    elseif Rand >= 3 then
        return
    end
    for i = 4, 6 do
        local Parm = GetCopySceneParam(scene, i)
        if (MonIsBattle(scene, MonRBId) and CalculatePercenHp(scene, MonObj) <= x061000_g_CSZhaohuanHp[1][i] and Parm == 0) then
            ProcessSkillFirstRequest(scene, MonObj, MonObj, skillId)
            SetCopySceneParam(scene, i, 1)
        end
    end
    if Rand == 1 and CalculatePercenHp(scene, MonObj) <= 50 and tickCount % x061000_g_ZhongjiSkillIdTick == 0 then
        --print("放重擊")
        --	ProcessSkillRequest(scene, MonObj, MonObj, skillId)
    end
end

function x061000_AddImpact(scene, RBId)
    -- 三兄弟互相加Buff
    --print("檢測buff")
    local nAdaptedLvl = math.floor(GetAdaptLvl(scene) / 10)
    if nAdaptedLvl > x061000_g_AdapteLvl then
        nAdaptedLvl = x061000_g_AdapteLvl
    end
    local Rand = GetCopySceneParam(scene, 10)
    if RBId == x061000_g_Boss3[1][1] + nAdaptedLvl then
        --		print("三兄弟互相加Buff")
        SendImpactToAllByRBID(scene, x061000_g_Boss3[1][1] + nAdaptedLvl, x061000_g_AddImpactId)
    end
end
function x061000_ClearParm(scene)
    for i = 4, 6 do
        SetCopySceneParam(scene, i, 0)
    end
end
--副本Tick
function x061000_OnCopySceneTick(scene, tickCount)

    local curStage = GetCopySceneParam(scene, x061000_g_CSParamIdxCurStage)
    if (curStage > 4) then
        return
    end
    local curSubStage = GetCopySceneParam(scene, x061000_g_CSParamIdxCurSubStage)
    SetCopySceneParam(scene, x061000_g_CSParamIdxCurSubStage, curSubStage + 1)

    if GetCopySceneStatus(scene) == 4 and curStage < 4 then
        --curStage = 11
        SetCopySceneParam(scene, x061000_g_CSParamIdxCurStage, 6)
        ShowModuoEnd(scene, false)
        x061000_ClearAllMonster(scene)
        DeleHostileMonster(scene)
        return
        --SetCopySceneParam(scene, x061000_g_CSParamIdxCurStage, 11)
        --ShowCardEnd(scene, false)
        --ShowSJCSEnd(scene, false)
    end
    --	local BossId = GetObjIdByRoleBase(scene,  x061000_g_MonstorList[4][1]))
    local nAdaptedLvl = math.floor(GetAdaptLvl(scene) / 10)
    if nAdaptedLvl > x061000_g_AdapteLvl then
        nAdaptedLvl = x061000_g_AdapteLvl
    end
    x061000_AddKuangbaoImpact(scene, tickCount, nAdaptedLvl)
    local Rand = GetCopySceneParam(scene, 10)
    x061000_SkillTick(scene, Rand, tickCount, nAdaptedLvl)
    if curStage < 3 then
        x061000_Stage_1(scene, curStage, curSubStage, Rand, nAdaptedLvl)
    elseif curStage == 3 then
        x061000_Stage_Boss(scene, curStage, curSubStage, Rand, nAdaptedLvl)
    elseif curStage == 4 then
        x061000_Stage_2(scene, curStage, curSubStage)

    end
end
function x061000_Stage_Boss(scene, stage, substage, rand, Lvl)
    if substage == 1 then
        OnCSMissionStage(scene, stage)
        x061000_StageControl(scene, stage)
        x061000_Stage_Eff(scene, stage)
        local MonId
        local nUserCount = GetSceneUserCount(scene)
        if nUserCount < 1 then
            nUserCount = 1
        end
        if rand == 1 then
            MonId = CreateMonster(scene, x061000_g_Boss1 + Lvl, x061000_g_BossPosList[1][1][3], x061000_g_BossPosList[1][1][1], x061000_g_BossPosList[1][1][2])
            SendImpactToUnit(scene, MonId, x061000_g_LevelAddBuff[1][nUserCount])
            SetHpFull(scene, MonId)
            UpdateObjHpMax(scene, MonId)
        elseif rand == 2 then
            MonId = CreateMonster(scene, x061000_g_Boss2 + Lvl, x061000_g_BossPosList[2][1][3], x061000_g_BossPosList[2][1][1], x061000_g_BossPosList[2][1][2])
            SendImpactToUnit(scene, MonId, x061000_g_LevelAddBuff[1][nUserCount])
            SetHpFull(scene, MonId)
            UpdateObjHpMax(scene, MonId)
        elseif rand == 3 then
            local count = #x061000_g_Boss3[1]
            for i = 1, count do
                MonId = CreateMonster(scene, x061000_g_Boss3[1][i] + Lvl, x061000_g_BossPosList[3][i][3], x061000_g_BossPosList[3][i][1], x061000_g_BossPosList[3][i][2])
                SendImpactToUnit(scene, MonId, x061000_g_LevelAddBuff[1][nUserCount])
                SetHpFull(scene, MonId)
                UpdateObjHpMax(scene, MonId)
            end
        end
    end
end
--第1阶段 刷小怪
function x061000_Stage_1(scene, stage, substage, rand, Lvl)
    if substage == 1 then
        --if(stage ~= 0) then
        --	x061000_OnDieCountDown1(scene)
        --end
        OnCSMissionStage(scene, stage)
        x061000_StageControl(scene, stage)
        x061000_Stage_Eff(scene, stage)
        local MonId
        local nUserCount = GetSceneUserCount(scene)
        if nUserCount < 1 then
            nUserCount = 1
        end
        local count = #x061000_g_MonstorList[stage + 1]
        for i = 1, count do
            MonId = CreateMonster(scene, x061000_g_MonstorList[stage + 1][i][rand] + Lvl, x061000_g_MonstorPosList[stage + 1][i][3], x061000_g_MonstorPosList[stage + 1][i][1], x061000_g_MonstorPosList[stage + 1][i][2])
            SendImpactToUnit(scene, MonId, x061000_g_LevelAddBuff[1][nUserCount])
            SetHpFull(scene, MonId)
            UpdateObjHpMax(scene, MonId)
        end
    end
end
function x061000_Success(scene)
    local curStage = GetCopySceneParam(scene, x061000_g_CSParamIdxCurStage)
    if curStage <= 4 then
        AccoutByCopyScene(scene)
        ShowModuoEnd(scene, true)

        DeleHostileMonster(scene)
        SetCopySceneSuccess(scene)
    end
end
function x061000_Stage_2(scene, stage, substage)
    if substage == 1 then
        --x061000_OnDieCountDown1(scene)
        x061000_Success(scene)
    end
end



