private TileSide GetClosestSide(VineTile tile, Vector2 targetPos)
        {
            var(distX, distY) = tile.Position + Behavior.GetWorldPosition() - targetPos;
            int absDistX = (int)Math.Abs(distX), absDistY = (int)Math.Abs(distY);

            return(absDistX > absDistY ? distX > 0 ? TileSide.Left : TileSide.Right : distY > 0 ? TileSide.Bottom : TileSide.Top);
        }
        private Item?ScanForTargets(VineTile branch)
        {
            Hull    parent   = Behavior.Parent;
            Vector2 worldPos = Behavior.GetWorldPosition() + branch.Position;
            Vector2 pos      = parent.Position + Behavior.Offset + branch.Position;

            Vector2 diameter    = ConvertUnits.ToSimUnits(new Vector2(branch.Rect.Width / 2f, branch.Rect.Height / 2f));
            Vector2 topLeft     = ConvertUnits.ToSimUnits(pos) - diameter;
            Vector2 bottomRight = ConvertUnits.ToSimUnits(pos) + diameter;

            int  highestPriority = 0;
            Item?currentItem     = null;

            foreach (Item item in Item.ItemList.Where(it => !Behavior.ClaimedTargets.Contains(it)))
            {
                if (Behavior.IgnoredTargets.ContainsKey(item))
                {
                    continue;
                }

                int priority = 0;
                foreach (BallastFloraBehavior.AITarget target in Behavior.Targets)
                {
                    if (!target.Matches(item) || target.Priority <= highestPriority)
                    {
                        continue;
                    }
                    priority = target.Priority;
                    break;
                }

                if (priority == 0)
                {
                    continue;
                }

                if (item.Submarine != parent.Submarine || Vector2.Distance(worldPos, item.WorldPosition) > Behavior.Sight)
                {
                    continue;
                }

                Vector2 itemSimPos = ConvertUnits.ToSimUnits(item.Position);

#if DEBUG || UNSTABLE
                Tuple <Vector2, Vector2> debugLine1 = Tuple.Create(parent.Position - ConvertUnits.ToDisplayUnits(topLeft), parent.Position - ConvertUnits.ToDisplayUnits(itemSimPos - diameter));
                Tuple <Vector2, Vector2> debugLine2 = Tuple.Create(parent.Position - ConvertUnits.ToDisplayUnits(bottomRight), parent.Position - ConvertUnits.ToDisplayUnits(itemSimPos + diameter));
                Behavior.debugSearchLines.Add(debugLine2);
                Behavior.debugSearchLines.Add(debugLine1);
#endif

                Body?body1 = Submarine.CheckVisibility(itemSimPos - diameter, topLeft);
                if (Blocks(body1, item))
                {
                    continue;
                }

                Body?body2 = Submarine.CheckVisibility(itemSimPos + diameter, bottomRight);
                if (Blocks(body2, item))
                {
                    continue;
                }

                highestPriority = priority;
                currentItem     = item;
            }

            if (currentItem != null)
            {
                foreach (BallastFloraBranch existingBranch in Behavior.Branches)
                {
                    if (Behavior.BranchContainsTarget(existingBranch, currentItem))
                    {
                        Behavior.ClaimTarget(currentItem, existingBranch);
                        return(null);
                    }
                }

                return(currentItem);
            }

            return(null);