def _find_target(self):
     all_objects = self.target_type.get_objects()
     objects = []
     for o in all_objects:
         dist_sq = (o.position - self._obj.position).magnitude_squared()
         if dist_sq > self.radius:
             continue
         if o == self:
             continue
         if not o.is_sim and not o.may_reserve(self._obj):
             continue
         if self.target_selection_test:
             resolver = DoubleObjectResolver(self._obj, o)
             if not self.target_selection_test.run_tests(resolver):
                 continue
         else:
             objects.append([o, dist_sq])
     if not objects:
         return
     source_handles = [
         routing.connectivity.Handle(self._obj.position,
                                     self._obj.routing_surface)
     ]
     dest_handles = []
     for o in objects:
         obj = o[0]
         parent = obj.parent
         route_to_obj = parent if parent is not None else obj
         constraint = Anywhere()
         for tuned_constraint in self.constraints:
             constraint = constraint.intersect(
                 tuned_constraint.create_constraint(self._obj,
                                                    route_to_obj))
         dests = constraint.get_connectivity_handles(self._obj, target=obj)
         if dests:
             dest_handles.extend(dests)
     if not dest_handles:
         return
     routing_context = self._obj.get_routing_context()
     connections = routing.estimate_path_batch(
         source_handles, dest_handles, routing_context=routing_context)
     if not connections:
         return
     connections.sort(key=lambda connection: connection[2])
     best_connection = connections[0]
     best_dest_handle = best_connection[1]
     best_obj = best_dest_handle.target
     return best_obj
 def get_routes_gen(self):
     if self._target is None:
         self.on_no_target()
         return False
         yield
     routing_slot_constraint = Anywhere()
     for tuned_constraint in self.constraints:
         routing_slot_constraint = routing_slot_constraint.intersect(
             tuned_constraint.create_constraint(self._obj, self._target))
     goals = list(
         itertools.chain.from_iterable(
             h.get_goals()
             for h in routing_slot_constraint.get_connectivity_handles(
                 self._obj)))
     routing_context = self._obj.get_routing_context()
     route = routing.Route(self._obj.routing_location,
                           goals,
                           routing_context=routing_context)
     yield route