x020091_g_scriptId = 020091

-- 第一波Boss 
x020091_g_BossId1 = {}
x020091_g_BossId1[1] = {210000,	210001,	210002,	210003,	210004,	210005,	210006,	210007}

-- 第二波 Boss 对应四个职业
x020091_g_BossId2 = {}
x020091_g_BossId2[1] = { 211001,211011,	211021,	211031,	211041,	211051,	211061,	211071 }
x020091_g_BossId2[2] = { 211002,211012,	211022,	211032,	211042,	211052,	211062,	211072 }
x020091_g_BossId2[3] = { 211003,211013,	211023,	211033,	211043,	211053,	211063,	211073 }
x020091_g_BossId2[4] = { 211004,211014,	211024,	211034,	211044,	211054,	211064,	211074 }

--第一波和第二波Boss位置
x020091_g_BossPosition = {} 
x020091_g_BossPosition[1] = { 51.23, 61.67, -1.57 }
x020091_g_BossPosition[2] = { 51.23, 61.67, -1.57 }

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

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


x020091_g_StoryAnimaId = { 7100, 7100, 7100, 7100, 7100, 7100, 7100, 7100 } --不同等级对应的mov

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


x020091_g_DropBoxId = { 214201, 214202, 214203, 214204, 214205, 214206, 214207, 214208, 214209, 214210, 214211, 214212, 214213 }

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


x020091_g_MissionId = { 1650, 1651, 1652, 1653, 1654, 1655, 1656, 1657 }--任务完成完成一个采集任务
x020091_g_BranchMissionId = {154, 1601, 1603, 1605, 1607, 1609, 1611, 1613}

function x020091_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 x020091_OnCopySceneOpen(scene, objId)
    x020091_CreateMonsterOnStage(scene, 1)
    SetCopySceneParam(scene, x020091_g_CSParamIdxStage, 1)
    SetCopySceneParam(scene, x020091_g_CSParamIdxCurSubStage, 0)
end

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

    if tickCount == 0 then
        CaluCSRound(scene)
    end
    local curStage = GetCopySceneParam(scene,x020091_g_CSParamIdxStage)
    local curSubStage = GetCopySceneParam(scene,x020091_g_CSParamIdxCurSubStage) +1
    SetCopySceneParam(scene, x020091_g_CSParamIdxCurSubStage, curSubStage)
    if curStage == 2 and GetHostileMonsterCount(scene) == 0 and curSubStage == x020091_g_MovMaxTime1 then
        local nUserObjId = GetUserObjIdByIdx(scene, 0)
        local MenPai = GetMenPai(scene,nUserObjId) / 10
        local id = x020091_GetRoleId(scene, x020091_g_BossId2[MenPai])
        x020091_CreateMonsterOnStage(scene, 2)

        -- local index = x020091_GetRoleIndex(scene, x020091_g_BossId2[MenPai])
        -- BroadCastBossTips(scene, x020091_g_BossTips[MenPai][index][2], x020091_g_BossTips[MenPai][index][1], id)
    end
end

function x020091_OnCopySceneTimeOut(scene)
    x020091_EndCopyScene(scene, false)
end


function x020091_EndCopyScene(scene, win)
    local nUserObjId = GetUserObjIdByIdx(scene, 0)
    local MenPai = GetMenPai(scene,nUserObjId)/10
    local id = x020091_GetRoleId(scene, x020091_g_BossId2[MenPai])
    local box = x020091_GetRoleId(scene, x020091_g_DropBoxId)   
    if win then
        SetArtifactCopySceneEnd(scene, 1, box, id)
        SetCopySceneSuccess(scene)   
    else
        SetArtifactCopySceneEnd(scene, 0, box, id)
        SetCopySceneFail(scene)
    end
end


function x020091_OnMissionComplete(scene, nObjId, nMissionId)
    local misId = x020091_GetRoleId(scene, x020091_g_MissionId)
    local branch_missionId = x020091_GetRoleId(scene, x020091_g_BranchMissionId)
    if nMissionId == misId then
        x020091_EndCopyScene(scene, true)
    end
end

function x020091_OnCopySceneQuit(scene)
    local nUserObjId = GetUserObjIdByIdx(scene, 0)
    local branch_missionId = x020091_GetRoleId(scene, x020091_g_BranchMissionId)
    local missions_branch_status = GetMissionState(scene, nUserObjId, branch_missionId)
    if missions_branch_status == 2 then      
        x020091_EndCopyScene(scene, true)
    end
end


function x020091_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 x020091_GetRoleIndex(scene, list)
    local index = GetDestinyIndex(scene)
    if index < 1 then
        index = 1
    elseif index > #list then
        index = #list
    end
    return index
end


function  x020091_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 x020091_OnObjDie(scene, objId, rolebaseId, killerId)
    if objId == GetUserObjIdByIdx(scene, 0) then
        x020091_EndCopyScene(scene, false)
    else
        local curStage = GetCopySceneParam(scene, x020091_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 == x020091_GetRoleId(scene, x020091_g_BossId1[1]) then
                local id = x020091_GetRoleId(scene, x020091_g_StoryAnimaId)
                StoryAnimation(scene, id)
            elseif rolebaseId == x020091_GetRoleId(scene, x020091_g_BossId2[MenPai]) then
                local missionId = x020091_GetRoleId(scene, x020091_g_MissionId)
                AddCSMission(scene, missionId)
                OnCSMissionStage(scene, 0)                    
            end
            SetCopySceneParam(scene, x020091_g_CSParamIdxStage, curStage + 1)
            x020091_StageControl(scene, curStage + 1)
            SetCopySceneParam(scene, x020091_g_CSParamIdxCurSubStage, 0)
        end
    end
end


function x020091_CreateMonsterOnStage(scene, stage)
    if stage == 1 then
        local id = x020091_GetRoleId(scene, x020091_g_BossId1[1])
        local pos = x020091_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 = x020091_GetRoleId(scene, x020091_g_BossId2[MenPai])
        local pos = x020091_g_BossPosition[stage]  
        CreateMonster(scene, id, pos[3], pos[1], pos[2])
     
    end
end

function x020091_OnStoryEventEnd(scene, nStoryId, nAnimationId) 
    local story_id = x020091_GetRoleId(scene, x020091_g_StoryAnimaId)
    if nAnimationId == story_id then
        local curStage = GetCopySceneParam(scene, x020091_g_CSParamIdxStage)
        x020091_CreateMonsterOnStage(scene, curStage)
        local nUserObjId = GetUserObjIdByIdx(scene, 0)
        local MenPai = GetMenPai(scene,nUserObjId) / 10
        local id = x020091_GetRoleId(scene, x020091_g_BossId2[MenPai])
        -- local index = x020091_GetRoleIndex(scene, x020091_g_BossId2[MenPai])
        -- BroadCastBossTips(scene, x020091_g_BossTips[MenPai][index][2], x020091_g_BossTips[MenPai][index][1], id)
        TellPlayerAutoCombat(scene, true)
    end
end

