local TeamAiTargetSystem = inherited("TeamAiTargetSystem", SystemMap)

function TeamAiTargetSystem:new()
	local ms = instance(self)
	ms.team_masks = {}
	return ms
end

TeamAiTargetSystem.key = "team_ai_target"


function TeamAiTargetSystem:wants(ent)
	return ent.aspects.team_target and ent.aspects.team_owned and ent.aspects.ai_target
end

function TeamAiTargetSystem:dynamic_wants(ent)
	return ent.aspects.team_target and ent.aspects.team_owned and ent.aspects.ai_target and not ent.dead
end

function TeamAiTargetSystem:ent_added(ent)
	local team = ent.team
	if team then
		local mask = self.team_masks[team]
		if not mask then
			mask = {}
			self.team_masks[team] = mask
		end
		mask[ent] = true
	end
end

function TeamAiTargetSystem:ent_removed(ent)
	local team = ent.team
	if team then
		local mask = self.team_masks[team]
		if mask then
			mask[ent] = nil
		end
	end
end


function TeamAiTargetSystem:ent_leaving_team(ent, team)
	self:ent_removed(ent)
end

function TeamAiTargetSystem:ent_joined_team(ent, team)
	self:ent_added(ent)
end


function TeamAiTargetSystem:get_target_map(team)
	return self.team_masks[team]
end

return TeamAiTargetSystem