function TargetingModule.FindTargetInScope(x,r,m)if not(r and m)then return nil end if x.TargetingMode=="\068\105\114\101\099\116"and r:IsDescendantOf(m)then return r end local L={r}while r and r.Parent~=m do table.insert(L,1,r.Parent)r=r.Parent end for r,m in ipairs(L)do if IsTargetable(m)then return m end end end function TargetingModule.UpdateTarget(x,r,m)local L=r or x.Scope local s=Mouse.Target local Q=x:FindTargetInScope(s,L)if x.LastTarget==s and not m then return s,Q else x.LastTarget=s x.TargetChanged:Fire(s)end local q=GetCore()if not q.IsSelectable({s})then x.HighlightTarget(nil)x.LastTarget=nil x.LastScopeTarget=nil x.TargetChanged:Fire(nil)x.ScopeTargetChanged:Fire(nil)return end if x.LastScopeTarget==Q and not m then return s,Q else x.LastScopeTarget=Q x.ScopeTargetChanged:Fire(Q)end if not q.Selection.IsSelected(Q)then x.HighlightTarget(Q)end return s,Q end local function r(m,x)local L=x or{}for m,x in pairs(m:GetChildren())do if IsVisible(x)then L[#L+1]=x else r(x,L)end end return L end

function TargetingModule:FindTargetInScope(Target, Scope)

	-- Return `nil` if no target, or if scope is unset
	if not (Target and Scope) then
		return nil
	end

	-- If in direct targeting mode, return target
	if self.TargetingMode == 'Direct' and (Target:IsDescendantOf(Scope)) then
		return Target
	end

	-- Search for ancestor of target directly within scope
	local TargetChain = { Target }
	while Target and (Target.Parent ~= Scope) do
		table.insert(TargetChain, 1, Target.Parent)
		Target = Target.Parent
	end

	-- Return targetable ancestor closest to scope
	for Index, Target in ipairs(TargetChain) do
		if IsTargetable(Target) then
			return Target
		end
	end

end

function TargetingModule:UpdateTarget(Scope, Force)
	local Scope = Scope or self.Scope

	-- Get target
	local NewTarget = Mouse.Target
	local NewScopeTarget = self:FindTargetInScope(NewTarget, Scope)

	-- Register whether target has changed
	if (self.LastTarget == NewTarget) and (not Force) then
		return NewTarget, NewScopeTarget
	else
		self.LastTarget = NewTarget
		self.TargetChanged:Fire(NewTarget)
	end

	-- Make sure target is selectable
	local Core = GetCore()
	if not Core.IsSelectable({ NewTarget }) then
		self.HighlightTarget(nil)
		self.LastTarget = nil
		self.LastScopeTarget = nil
		self.TargetChanged:Fire(nil)
		self.ScopeTargetChanged:Fire(nil)
		return
	end

	-- Register whether scope target has changed
	if (self.LastScopeTarget == NewScopeTarget) and (not Force) then
		return NewTarget, NewScopeTarget
	else
		self.LastScopeTarget = NewScopeTarget
		self.ScopeTargetChanged:Fire(NewScopeTarget)
	end

	-- Update scope target highlight
	if not Core.Selection.IsSelected(NewScopeTarget) then
		self.HighlightTarget(NewScopeTarget)
	end

	-- Return new targets
	return NewTarget, NewScopeTarget
end

local function GetVisibleChildren(Item, Table)
	local Table = Table or {}

	-- Search for visible items recursively
	for _, Item in pairs(Item:GetChildren()) do
		if IsVisible(Item) then
			Table[#Table + 1] = Item
		else
			GetVisibleChildren(Item, Table)
		end
	end

	-- Return visible items
	return Table
end
