x020093_g_scriptId = 020093

-- 第一波Boss 
x020093_g_BossId1 = {}
x020093_g_BossId1[1] = {210010,	210011,	210012,	210013,	210014,	210015,	210016,	210017,	210018}

-- 第二波 Boss 对应四个职业
x020093_g_BossId2 = {}
x020093_g_BossId2[1] = { 212001,212011,	212021,	212031,	212041,	212051,	212061,	212071,	212081 }
x020093_g_BossId2[2] = { 212002,212012,	212022,	212032,	212042,	212052,	212062,	212072,	212082 }
x020093_g_BossId2[3] = { 212003,212013,	212023,	212033,	212043,	212053,	212063,	212073,	212083 }
x020093_g_BossId2[4] = { 212004,212014,	212024,	212034,	212044,	212054,	212064,	212074,	212084 }

--第一波和第二波Boss位置
x020093_g_BossPosition = {} 
x020093_g_BossPosition[1] = { 55.31, 50.25, 3.33 }
x020093_g_BossPosition[2] = { 55.31, 50.25, 3.33 }

x020093_g_AdapteLvl = 60  --   每个怪物的自适应等级套数  必须随适应等级的变化而改变（废弃）

x020093_g_MovMaxTime1 = 20 -- mov 最大播放时间


x020093_g_StoryAnimaId = { 7110,	7111,	7112,	7113,	7114,	7115,	7116,	7117,	7118 } --不同等级对应的mov

x020093_g_BossTips = {}
x020093_g_BossTips[1] = {{ 163000, 24000 }, { 163000, 24000 }, { 163000, 24000 }, { 163000, 24000 }, { 163000, 24000 }, { 163000, 24000 }, { 163000, 24000 }, { 163000, 24000 }, { 163000, 24000 }}
x020093_g_BossTips[2] = {{ 163000, 24001 }, { 163000, 24001 }, { 163000, 24001 }, { 163000, 24001 }, { 163000, 24001 }, { 163000, 24001 }, { 163000, 24001 }, { 163000, 24001 }, { 163000, 24001 }}
x020093_g_BossTips[3] = {{ 163000, 24002 }, { 163000, 24002 }, { 163000, 24002 }, { 163000, 24002 }, { 163000, 24002 }, { 163000, 24002 }, { 163000, 24002 }, { 163000, 24002 }, { 163000, 24002 }}
x020093_g_BossTips[4] = {{ 163000, 24003 }, { 163000, 24003 }, { 163000, 24003 }, { 163000, 24003 }, { 163000, 24003 }, { 163000, 24003 }, { 163000, 24003 }, { 163000, 24003 }, { 163000, 24003 }}


x020093_g_DropBoxId = { 214211, 214212, 214213, 214214, 214215, 214216, 214217, 214218, 214219}

x020093_g_CSParamIdxStage=0                        -- 场景切换波数
x020093_g_CSParamIdxCurSubStage=1                  -- 同一场景中的时间点


x020093_g_MissionId = { 1660,1661,1662,1663,1664,1665,1666,1667,1668 }--任务完成完成一个采集任务
x020093_g_BranchMissionId = {1615, 1617, 1619, 1621, 1623, 1625, 1627, 1629, 1631}

function x020093_OnObjEnter(scene, objId)
    --CopySceneNPC(scene,1001101,51001,6,201,1,"XXX")--场景、角色ID、CharModel表ID、势力、出现的场景SceneID、等级、名称
    AutoCombat(scene, objId, true)
    local nUserCount = GetSceneUserCount(scene)
    for i = 0, nUserCount - 1 do
        local nUserObjId = GetUserObjIdByIdx(scene, i)
        SynCsStageData(scene, nUserObjId)
    end
end

function x020093_OnCopySceneOpen(scene, objId)
    x020093_CreateMonsterOnStage(scene, 1)
    SetCopySceneParam(scene, x020093_g_CSParamIdxStage, 1)
    SetCopySceneParam(scene, x020093_g_CSParamIdxCurSubStage, 0)
end

function x020093_OnCopySceneTick(scene,tickCount)                   --副本阶段

    if tickCount == 0 then
        CaluCSRound(scene)
    end
    local curStage = GetCopySceneParam(scene,x020093_g_CSParamIdxStage)
    local curSubStage = GetCopySceneParam(scene,x020093_g_CSParamIdxCurSubStage) +1
    SetCopySceneParam(scene, x020093_g_CSParamIdxCurSubStage, curSubStage)
    if curStage == 2 and GetHostileMonsterCount(scene) == 0 and curSubStage == x020093_g_MovMaxTime1 then
        local nUserObjId = GetUserObjIdByIdx(scene, 0)
        local MenPai = GetMenPai(scene,nUserObjId) / 10
        local id = x020093_GetRoleId(scene, x020093_g_BossId2[MenPai])
        x020093_CreateMonsterOnStage(scene, 2)

        -- local index = x020093_GetRoleIndex(scene, x020093_g_BossId2[MenPai])
        -- BroadCastBossTips(scene, x020093_g_BossTips[MenPai][index][2], x020093_g_BossTips[MenPai][index][1], id)
    end
end

function x020093_OnCopySceneTimeOut(scene)
    x020093_EndCopyScene(scene, false)
end


function x020093_EndCopyScene(scene, win)
    local nUserObjId = GetUserObjIdByIdx(scene, 0)
    local MenPai = GetMenPai(scene,nUserObjId)/10
    local id = x020093_GetRoleId(scene, x020093_g_BossId2[MenPai])
    local box = x020093_GetRoleId(scene, x020093_g_DropBoxId)   
    if win then
        SetArtifactCopySceneEnd(scene, 1, box, id)
        SetCopySceneSuccess(scene)   
    else
        SetArtifactCopySceneEnd(scene, 0, box, id)
        SetCopySceneFail(scene)
    end
end


function x020093_OnMissionComplete(scene, nObjId, nMissionId)
    local misId = x020093_GetRoleId(scene, x020093_g_MissionId)
    local branch_missionId = x020093_GetRoleId(scene, x020093_g_BranchMissionId)
    if nMissionId == misId then
        x020093_EndCopyScene(scene, true)
    end
end

function x020093_OnCopySceneQuit(scene)
    local nUserObjId = GetUserObjIdByIdx(scene, 0)
    local branch_missionId = x020093_GetRoleId(scene, x020093_g_BranchMissionId)
    local missions_branch_status = GetMissionState(scene, nUserObjId, branch_missionId)
    if missions_branch_status == 2 then      
        x020093_EndCopyScene(scene, true)
    end
end


function x020093_GetRoleId(scene, list)
    local index = GetDestinyIndex(scene)
    if index < 1 then
        index = 1
    elseif index > #list then
        index = #list
    end
    return list[index]
end

function x020093_GetRoleIndex(scene, list)
    local index = GetDestinyIndex(scene)
    if index < 1 then
        index = 1
    elseif index > #list then
        index = #list
    end
    return index
end


function  x020093_StageControl(scene,nstage)

    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 x020093_OnObjDie(scene, objId, rolebaseId, killerId)
    if objId == GetUserObjIdByIdx(scene, 0) then
        x020093_EndCopyScene(scene, false)
    else
        local curStage = GetCopySceneParam(scene, x020093_g_CSParamIdxStage)
        local count = GetHostileMonsterCount(scene)
        local nUserObjId = GetUserObjIdByIdx(scene, 0)
        local MenPai = GetMenPai(scene,nUserObjId)/10
        local nUserCount = GetSceneUserCount(scene)
        SetCopySceneParam(scene, 7, curStage)
        for i = 0, nUserCount - 1 do
            local nUserObjId = GetUserObjIdByIdx(scene, i)
        SynCsStageData(scene, nUserObjId)
        end
        if count == 0 then
            if rolebaseId == x020093_GetRoleId(scene, x020093_g_BossId1[1]) then
                local id = x020093_GetRoleId(scene, x020093_g_StoryAnimaId)
                StoryAnimation(scene, id)
            elseif rolebaseId == x020093_GetRoleId(scene, x020093_g_BossId2[MenPai]) then
                local missionId = x020093_GetRoleId(scene, x020093_g_MissionId)
                AddCSMission(scene, missionId)
                OnCSMissionStage(scene, 0)                    
            end
            SetCopySceneParam(scene, x020093_g_CSParamIdxStage, curStage + 1)
            x020093_StageControl(scene, curStage + 1)
            SetCopySceneParam(scene, x020093_g_CSParamIdxCurSubStage, 0)
        end
    end
end


function x020093_CreateMonsterOnStage(scene, stage)
    if stage == 1 then
        local id = x020093_GetRoleId(scene, x020093_g_BossId1[1])
        local pos = x020093_g_BossPosition[stage]          
        CreateMonster(scene, id, pos[3], pos[1], pos[2])
    elseif stage == 2 then    
        local nUserObjId = GetUserObjIdByIdx(scene, 0)
        local MenPai = GetMenPai(scene,nUserObjId) / 10
        local id = x020093_GetRoleId(scene, x020093_g_BossId2[MenPai])
        local pos = x020093_g_BossPosition[stage]  
        CreateMonster(scene, id, pos[3], pos[1], pos[2])
     
    end
end

function x020093_OnStoryEventEnd(scene, nStoryId, nAnimationId) 
    local story_id = x020093_GetRoleId(scene, x020093_g_StoryAnimaId)
    if nAnimationId == story_id then
        local curStage = GetCopySceneParam(scene, x020093_g_CSParamIdxStage)
        x020093_CreateMonsterOnStage(scene, curStage)
        local nUserObjId = GetUserObjIdByIdx(scene, 0)
        local MenPai = GetMenPai(scene,nUserObjId) / 10
        local id = x020093_GetRoleId(scene, x020093_g_BossId2[MenPai])
        -- local index = x020093_GetRoleIndex(scene, x020093_g_BossId2[MenPai])
        -- BroadCastBossTips(scene, x020093_g_BossTips[MenPai][index][2], x020093_g_BossTips[MenPai][index][1], id)
        TellPlayerAutoCombat(scene, true)
    end
end

