x020017_g_scriptId=020017

x020017_g_BluePosX=91.78
x020017_g_BluePosZ=64.75

x020017_g_RedPosX=31.5
x020017_g_RedPosZ=64.75

x020017_g_TeamBlueTeamInfo={{0,0},{0,0},{0,0},{0,0},{0,0}}	--蓝队玩家信息(objId,state)
x020017_g_TeamRedTeamInfo={{0,0},{0,0},{0,0},{0,0},{0,0}}	--红队玩家信息(objId,state)

function x020017_OnObjEnter(scene,objId)

	if(x020017_g_Start == 1) then
		return
	end

	--初始化队长ID
	if(x020017_g_TeamBlueLeaderId == nil) then
		x020017_g_TeamBlueLeaderId = GetTeamLeaderId(scene,6)
	end	

	--初始化红蓝队伍最大人数
	if(x020017_g_TeamBlueCountMax == nil) then
		x020017_g_TeamBlueCountMax = GetTeamPvpCount(scene,6)
	end
	if(x020017_g_TeamRedCountMax == nil) then
		x020017_g_TeamRedCountMax = GetTeamPvpCount(scene,7)
	end
	
	--初始化红蓝队伍当前人数
	if(x020017_g_TeamBlueCount == nil) then
		x020017_g_TeamBlueCount = 0
	end
	if(x020017_g_TeamRedCount == nil) then
		x020017_g_TeamRedCount = 0
	end
		
	--玩家初始化
	if(x020017_g_TeamBlueCount < x020017_g_TeamBlueCountMax) then
		x020017_g_TeamBlueCount = x020017_g_TeamBlueCount + 1
		SetPos(scene,objId,x020017_g_BluePosX+math.random(-1,1),x020017_g_BluePosZ+math.random(-1,1),-3.14)
		TestFullHp(scene,objId)
		RemoveCDTime(scene,objId)
		SetPvpCamp(scene,objId,6)
		AutoCombat(scene,objId,true)
		x020017_g_TeamBlueTeamInfo[x020017_g_TeamBlueCount][1] = objId
		x020017_g_TeamBlueTeamInfo[x020017_g_TeamBlueCount][2] = 1
	end
		
	--机器人初始化(等玩家全部进入场景)
	if(x020017_g_TeamBlueCount == x020017_g_TeamBlueCountMax) then
		local robotRoleBaseId = -1
		for i = 1, x020017_g_TeamRedCountMax do
			robotRoleBaseId = GetRobotRoleBaseIdByIndex(scene,i - 1)
			if (robotRoleBaseId > 0) then
				x020017_g_TeamRedTeamInfo[i][1] = CreateMonster(scene,robotRoleBaseId,0,x020017_g_RedPosX+math.random(-1,1),x020017_g_RedPosZ+math.random(-1,1))
				x020017_g_TeamRedTeamInfo[i][2] = 1
				x020017_g_TeamRedCount = x020017_g_TeamRedCount + 1
			end
		end
		if (x020017_g_Start == nil) then
			x020017_g_Start = 1
		end
	end
end

function x020017_OnObjLeave(scene,objId,rolebaseId)
	x020017_Modify(scene,objId,rolebaseId)
end

function x020017_OnObjDie(scene,objId,rolebaseId,killerId)
	x020017_Modify(scene,objId,rolebaseId)
end

function x020017_Modify(scene,objId,rolebaseId) --离开场景或者在场景内死亡的处理
	if(x020017_g_TeamBlueCount == 0) or (x020017_g_TeamRedCount == 0) or (GetCopySceneStatus(scene) > 3) then
		return
	end
			
	local curTeamId = -1
	local curIndex = 0
	
	for i = 1, x020017_g_TeamBlueCountMax do
		if (x020017_g_TeamBlueTeamInfo[i][1] == objId) and (x020017_g_TeamBlueTeamInfo[i][2] == 1) then
			curTeamId = 0
			curIndex = i
			break
		end
	end
	
	for i = 1, x020017_g_TeamRedCountMax do
		if (x020017_g_TeamRedTeamInfo[i][1] == objId) and (x020017_g_TeamRedTeamInfo[i][2] == 1) then
			curTeamId = 1
			curIndex = i
			break
		end
	end
		
	if(curTeamId == 0) then
		x020017_g_TeamBlueCount = x020017_g_TeamBlueCount - 1
		x020017_g_TeamBlueTeamInfo[curIndex][2] = 0;
	elseif(curTeamId == 1) then
		x020017_g_TeamRedCount = x020017_g_TeamRedCount - 1
		x020017_g_TeamRedTeamInfo[curIndex][2] = 0;
	else
		return
	end
	
	if(x020017_g_TeamBlueCount == 0) then 
		RecordPvpRankData(scene,x020017_g_TeamBlueLeaderId,x020017_g_TeamRedLeaderId,3,false,true,GetRobotNameByIndex(scene,0))
		x020017_Succeed(scene)
		SetCopySceneSuccess(scene)
		return
	end

	if(x020017_g_TeamRedCount == 0) then
		RecordPvpRankData(scene,x020017_g_TeamBlueLeaderId,x020017_g_TeamRedLeaderId,3,true,true,GetRobotNameByIndex(scene,0))
		x020017_Succeed(scene)
		SetCopySceneSuccess(scene)
		return
	end
end

function x020017_Succeed(scene)

	local nUserCount = GetSceneUserCount(scene)
	for i=0,nUserCount-1 do		
		local nUserObjId = GetUserObjIdByIdx(scene,i)
		AddActivtyNum(scene, nUserObjId , 13)
		SetHpFull(scene,nUserObjId)
		RemoveCDTime(scene,nUserObjId)	
		SetPvpCamp(scene,nUserObjId,1)		
	end
end











