x020094_g_scriptId = 020094

-- 第一波Boss 
x020094_g_BossId1 = {}
x020094_g_BossId1[1] = {210020,	210021,	210022,	210023,	210024,	210025,	210026,	210027,	210028}

-- 第二波 Boss 对应四个职业
x020094_g_BossId2 = {}
x020094_g_BossId2[1] = { 213001,213011,	213021,	213031,	213041,	213051,	213061,	213071,	213081 }
x020094_g_BossId2[2] = { 213002,213012,	213022,	213032,	213042,	213052,	213062,	213072,	213082 }
x020094_g_BossId2[3] = { 213003,213013,	213023,	213033,	213043,	213053,	213063,	213073,	213083 }
x020094_g_BossId2[4] = { 213004,213014,	213024,	213034,	213044,	213054,	213064,	213074,	213084 }

--第一波和第二波Boss位置
x020094_g_BossPosition = {} 
x020094_g_BossPosition[1] = { 95.5267, 75.02469,  -1.57 }
x020094_g_BossPosition[2] = { 95.5267, 75.02469,  -1.57  }

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

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


x020094_g_StoryAnimaId = { 7400,	7400,	7400,	7400,	7400,	7400,	7400,	7400,	7400 } --不同等级对应的mov

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


x020094_g_DropBoxId = { 214220, 214221, 214222, 214223, 214224, 214225, 214226, 214227, 214228}

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


x020094_g_MissionId = { 1670,1671,1672,1673,1674,1675,1676,1677,1678 }--任务完成完成一个采集任务
x020094_g_BranchMissionId = {1633, 1635, 1637, 1639, 1641, 1643, 1645, 1647, 1649}

function x020094_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 x020094_OnCopySceneOpen(scene, objId)
    x020094_CreateMonsterOnStage(scene, 1)
    SetCopySceneParam(scene, x020094_g_CSParamIdxStage, 1)
    SetCopySceneParam(scene, x020094_g_CSParamIdxCurSubStage, 0)
end

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

    if tickCount == 0 then
        CaluCSRound(scene)
    end
    local curStage = GetCopySceneParam(scene,x020094_g_CSParamIdxStage)
    local curSubStage = GetCopySceneParam(scene,x020094_g_CSParamIdxCurSubStage) +1
    SetCopySceneParam(scene, x020094_g_CSParamIdxCurSubStage, curSubStage)
    if curStage == 2 and GetHostileMonsterCount(scene) == 0 and curSubStage == x020094_g_MovMaxTime1 then
        local nUserObjId = GetUserObjIdByIdx(scene, 0)
        local MenPai = GetMenPai(scene,nUserObjId) / 10
        local id = x020094_GetRoleId(scene, x020094_g_BossId2[MenPai])
        x020094_CreateMonsterOnStage(scene, 2)

        -- local index = x020094_GetRoleIndex(scene, x020094_g_BossId2[MenPai])
        -- BroadCastBossTips(scene, x020094_g_BossTips[MenPai][index][2], x020094_g_BossTips[MenPai][index][1], id)
    end
end

function x020094_OnCopySceneTimeOut(scene)
    x020094_EndCopyScene(scene, false)
end


function x020094_EndCopyScene(scene, win)
    local nUserObjId = GetUserObjIdByIdx(scene, 0)
    local MenPai = GetMenPai(scene,nUserObjId)/10
    local id = x020094_GetRoleId(scene, x020094_g_BossId2[MenPai])
    local box = x020094_GetRoleId(scene, x020094_g_DropBoxId)   
    if win then
        SetArtifactCopySceneEnd(scene, 1, box, id)
        SetCopySceneSuccess(scene)   
    else
        SetArtifactCopySceneEnd(scene, 0, box, id)
        SetCopySceneFail(scene)
    end
end


function x020094_OnMissionComplete(scene, nObjId, nMissionId)
    local misId = x020094_GetRoleId(scene, x020094_g_MissionId)
    local branch_missionId = x020094_GetRoleId(scene, x020094_g_BranchMissionId)
    if nMissionId == misId then
        x020094_EndCopyScene(scene, true)
    end
end

function x020094_OnCopySceneQuit(scene)
    local nUserObjId = GetUserObjIdByIdx(scene, 0)
    local branch_missionId = x020094_GetRoleId(scene, x020094_g_BranchMissionId)
    local missions_branch_status = GetMissionState(scene, nUserObjId, branch_missionId)
    if missions_branch_status == 2 then      
        x020094_EndCopyScene(scene, true)
    end
end


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


function  x020094_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 x020094_OnObjDie(scene, objId, rolebaseId, killerId)
    if objId == GetUserObjIdByIdx(scene, 0) then
        x020094_EndCopyScene(scene, false)
    else
        local curStage = GetCopySceneParam(scene, x020094_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 == x020094_GetRoleId(scene, x020094_g_BossId1[1]) then
                local id = x020094_GetRoleId(scene, x020094_g_StoryAnimaId)
                StoryAnimation(scene, id)
            elseif rolebaseId == x020094_GetRoleId(scene, x020094_g_BossId2[MenPai]) then
                local missionId = x020094_GetRoleId(scene, x020094_g_MissionId)
                AddCSMission(scene, missionId)
                OnCSMissionStage(scene, 0)                    
            end
            SetCopySceneParam(scene, x020094_g_CSParamIdxStage, curStage + 1)
            x020094_StageControl(scene, curStage + 1)
            SetCopySceneParam(scene, x020094_g_CSParamIdxCurSubStage, 0)
        end
    end
end


function x020094_CreateMonsterOnStage(scene, stage)
    if stage == 1 then
        local id = x020094_GetRoleId(scene, x020094_g_BossId1[1])
        local pos = x020094_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 = x020094_GetRoleId(scene, x020094_g_BossId2[MenPai])
        local pos = x020094_g_BossPosition[stage]  
        CreateMonster(scene, id, pos[3], pos[1], pos[2])
     
    end
end

function x020094_OnStoryEventEnd(scene, nStoryId, nAnimationId) 
    local story_id = x020094_GetRoleId(scene, x020094_g_StoryAnimaId)
    if nAnimationId == story_id then
        local curStage = GetCopySceneParam(scene, x020094_g_CSParamIdxStage)
        x020094_CreateMonsterOnStage(scene, curStage)
        local nUserObjId = GetUserObjIdByIdx(scene, 0)
        local MenPai = GetMenPai(scene,nUserObjId) / 10
        local id = x020094_GetRoleId(scene, x020094_g_BossId2[MenPai])
        -- local index = x020094_GetRoleIndex(scene, x020094_g_BossId2[MenPai])
        -- BroadCastBossTips(scene, x020094_g_BossTips[MenPai][index][2], x020094_g_BossTips[MenPai][index][1], id)
        TellPlayerAutoCombat(scene, true)
    end
end

