public static bool FindClosestTreeInPlaceArea(Vector3D fromPosition, long entityId, MyHumanoidBot bot, out ItemInfo result)
		{
            result = default(ItemInfo);
            MyPlaceArea area = MyPlaceArea.FromEntity(entityId);
            if (area == null)
                return false;

            var areaBoundingBox = area.WorldAABB;
            var entities = MyEntities.GetEntitiesInAABB(ref areaBoundingBox, true);

            double closestDistanceSq = double.MaxValue;

            foreach (MyEntity entity in entities)
            {
                MyTrees trees = entity as MyTrees;
                if (trees == null) continue;

				m_tmpEnvItemList.Clear();
                trees.GetPhysicalItemsInRadius(areaBoundingBox.Center, (float)areaBoundingBox.HalfExtents.Length(), m_tmpEnvItemList);

                foreach (var tree in m_tmpEnvItemList)
                {
                    if (!area.TestPoint(tree.Transform.Position))
                        continue;

                    if (!bot.AgentLogic.AiTarget.IsTreeReachable(entity, tree.LocalId))
                        continue;

                    double distanceSq = Vector3D.DistanceSquared(fromPosition, tree.Transform.Position);
                    if (distanceSq < closestDistanceSq)
                    {
                        result.ItemsEntityId = entity.EntityId;
                        result.ItemId = tree.LocalId;
                        result.Target = tree.Transform.Position;
                        closestDistanceSq = distanceSq;
                    }
                }
                m_tmpEnvItemList.Clear();
            }

            entities.Clear();

            return (closestDistanceSq != double.MaxValue);
		}
        public static bool FindClosestFracturedTreeInArea(Vector3D fromPosition, long areaEntityId, MyHumanoidBot bot, out EntityInfo result)
        {
            result = default(EntityInfo);

            MyPlaceArea area = MyPlaceArea.FromEntity(areaEntityId);
            if (area == null) return false;
    
            BoundingBoxD areaBB = area.WorldAABB;
            double radius = (double)areaBB.HalfExtents.Length();


			return FindClosestFracturedTreeInternal(fromPosition, areaBB.Center, radius, area, bot, out result); ;
        }
        private static bool FindClosestFracturedTreeInternal(Vector3D fromPosition, Vector3D searchCenter, double searchRadius, MyPlaceArea area, MyHumanoidBot bot, out EntityInfo result)
        {
            result = default(EntityInfo);

            double closestDistanceSq = double.MaxValue;
            MyFracturedPiece closestTarget = null;
            Vector3D closestPoint = default(Vector3D);

            BoundingSphereD searchSphere = new BoundingSphereD(searchCenter, searchRadius);
            
            m_tmpFracturePieceList.Clear();
            MyFracturedPiecesManager.Static.GetFracturesInSphere(ref searchSphere, ref m_tmpFracturePieceList);
            for (int i = 0; i < m_tmpFracturePieceList.Count; ++i)
            {
                var fracture = m_tmpFracturePieceList[i];

                // Skip non-tree fractures
                if (!MyTrees.IsEntityFracturedTree(fracture))
                {
                    continue;
                }

                if (IsFracturedTreeStump(fracture))
                    continue;

                if (!bot.AgentLogic.AiTarget.IsEntityReachable(fracture))
                    continue;

                Vector3D positionInTrunkLocal = Vector3D.Transform(fromPosition, fracture.PositionComp.WorldMatrixNormalizedInv);
                Vector3D closestPointOnTrunk;

                if (!FindClosestPointOnFracturedTree(positionInTrunkLocal, fracture, out closestPointOnTrunk))
                    continue;

                if (area == null || area.TestPoint(closestPointOnTrunk))
                {
                    double distanceSq = Vector3D.DistanceSquared(closestPointOnTrunk, fromPosition);

                    if (distanceSq < closestDistanceSq)
                    {
                        closestDistanceSq = distanceSq;
                        closestTarget = fracture;
                        closestPoint = closestPointOnTrunk;
                    }
                }
            }
            m_tmpFracturePieceList.Clear();

            if (closestTarget == null)
                return false;

            result.EntityId = closestTarget.EntityId;
            result.Target = closestPoint;
            return true;
        }
 public static bool FindClosestFracturedTreeInRadius(Vector3D fromPosition, double radius, MyHumanoidBot bot, out EntityInfo result)
 {
     return FindClosestFracturedTreeInternal(fromPosition, fromPosition, radius, null, bot, out result);
 }