--任务副本2
--进入副本→刷第一波怪→击杀→副本完成→传出副本

x040020_g_scriptId = 040020

x040020_g_CSParamIdxCurStage = 0              --大阶段
x040020_g_CSParamIdxCurSubStage = 1           --小阶段
x040020_g_CSParam1 = 2

--副本参数
x040020_g_MonstorList = {}
x040020_g_MonstorList[1] = { 1003114, 1003115, 1003116 } --怪物ID

x040020_g_MonsterNum = 20 -- 每波怪物的数量

x040020_g_ExpMonsterNum = 60 -- 给经验怪物数量

x040020_g_MonsterPos = {} -- 每波怪物的出生位置
x040020_g_MonsterPos[1] = { { 48.13, 36.87, 1.57 }, { 48.03, 47.16, -1.61 }, { 52.42, 42.31, -3.1 }, { 44.11, 49.44, -0.71 }, { 38.59, 42.03, 0 }, { 58.05, 41.84, -3.13 }, { 51.67, 32.7, -4.16 }, { 40.43, 38.48, 0.68 }, { 56.21, 33.26, -4.26 }, { 56.3, 49.71, -2.28 }, { 40.79, 47.74, -0.51 }, { 40.89, 34.66, 0.85 }, { 44.88, 34.56, 0.72 }, { 55.86, 45.22, -2.52 }, { 55.35, 38.21, -3.68 }, { 52.44, 36.36, -4.11 }, { 52.61, 47.17, -2.45 }, { 43.1, 37.29, 0.57 }, { 43.36, 46.47, -0.71 }, { 42.7, 41.86, -0.02 } }

x040020_g_NpcList = {} -- 站立的NPC
x040020_g_NpcList[1] = { 1003012, 1003015 }

x040020_g_MonsterReliveTime = 2 -- 怪物重生时间 单位秒

x040020_g_NpcPos = {} -- NPC位置
x040020_g_NpcPos[1] = { { 61.82, 44.48, 3.14 }, { 61.78, 39.47, 3.14 } }

x040020_g_Time = 2--多少秒发送 经验buff


function x040020_OnCopySceneTimeOut(scene)
    DeleHostileMonster(scene)
    SetCopySceneSuccess(scene)
end

function x040020_OnObjEnter(scene, objId)
    SetCopySceneParam(scene, x040020_g_CSParamIdxCurStage, 0)
    SetCopySceneParam(scene, x040020_g_CSParam1, 0)
    SetCopySceneParam(scene, 7, 0)
    x040020_CreateNpc(scene)
    x040020_StageControl(scene, 0)
end

function x040020_StageControl(scene, nstage)
    SetCopySceneParam(scene, 2, 0)
    SetCopySceneParam(scene, 3, 0)
    SetCopySceneParam(scene, 4, 0)
    SetCopySceneParam(scene, 7, nstage)
    local nUserCount = GetSceneUserCount(scene)
    for j = 0, nUserCount - 1 do
        local nUserObjId = GetUserObjIdByIdx(scene, j)
        SynCsStageData(scene, nUserObjId)
        SetExpMonsterNum(scene, nUserObjId, x040020_g_ExpMonsterNum)
        SetDramaCopyExp(scene, nUserObjId)
    end
end


--种npc
function x040020_CreateNpc(scene)
    TellPlayerAutoCombat(scene)
    for i = 1, #x040020_g_NpcList do
        for j = 1, #x040020_g_NpcList[i] do
            CreateMonster(scene, x040020_g_NpcList[i][j], x040020_g_NpcPos[i][j][3], x040020_g_NpcPos[i][j][1], x040020_g_NpcPos[i][j][2])
        end
    end
end


--死亡回调
function x040020_OnObjDie(scene, objId, rolebaseId, killerId)
    local num = GetExpMonsterNum(scene, killerId);
    if num <= 0 then
        SetCopySceneParam(scene, x040020_g_CSParamIdxCurStage, 2)
        DeleHostileMonster(scene)
        SetCopySceneSuccess(scene)
        return
    end
    local curType = 0;
    if num > 40 then
        curType = 2;
    elseif num <= 40 then
        curType = 3;
    end
    local ran = RandomMon(1, x040020_g_MonsterNum)
    if ran > 0 and ran <= x040020_g_MonsterNum then
        local tickCount = GetCopySceneParam(scene, x040020_g_CSParam1)
        SetMonsterRelive(scene, x040020_g_MonstorList[1][curType], x040020_g_MonstorList[1][curType], tickCount + x040020_g_MonsterReliveTime,
                x040020_g_MonsterPos[1][ran][3], x040020_g_MonsterPos[1][ran][1], x040020_g_MonsterPos[1][ran][2])
    end
end

--副本Tick
function x040020_OnCopySceneTick(scene, tickCount)
    local tick = GetCopySceneParam(scene, x040020_g_CSParam1)
    SetCopySceneParam(scene, x040020_g_CSParam1, tick + 1)
    local nUserCount = GetSceneUserCount(scene)
    for j = 0, nUserCount - 1 do
        local num = GetExpMonsterNum(scene, GetUserObjIdByIdx(scene, j));
        if num <= 0 then
            SetCopySceneSuccess(scene)
            return
        end
    end
    local curStage = GetCopySceneParam(scene, x040020_g_CSParamIdxCurStage)
    if curStage == 0 then
        for i = 1, x040020_g_MonsterNum do
            CreateMonster(scene, x040020_g_MonstorList[1][curStage + 1], x040020_g_MonsterPos[1][i][3], x040020_g_MonsterPos[1][i][1], x040020_g_MonsterPos[1][i][2])
        end
        SetCopySceneParam(scene, x040020_g_CSParamIdxCurStage, curStage + 1)
    end

    if tick == x040020_g_Time then
        BroadCastBossTipsExp(scene, 93000, 1)
    elseif tick == x040020_g_Time + 5 then
        BroadCastBossTipsExp(scene, 93000, 2)
    end

    if curStage < 2 then
        CheckMonsterReliveExpCopy(scene, tickCount)
    end
end