示例#1
0
        // this can be slow...
        public Spot FindClosestSpot(Location l, float max_d, Set <Spot> Not)
        {
            Spot  closest   = null;
            float closest_d = 1E30f;
            int   d         = 0;

            while ((float)d <= max_d + 0.1f)
            {
                for (int i = -d; i <= d; i++)
                {
                    float x_up = l.X + (float)d;
                    float x_dn = l.X - (float)d;
                    float y_up = l.Y + (float)d;
                    float y_dn = l.Y - (float)d;

                    Spot s0 = GetSpot2D(x_up, l.Y + i);
                    Spot s2 = GetSpot2D(x_dn, l.Y + i);

                    Spot   s1 = GetSpot2D(l.X + i, y_dn);
                    Spot   s3 = GetSpot2D(l.X + i, y_up);
                    Spot[] sv = { s0, s1, s2, s3 };
                    foreach (Spot s in sv)
                    {
                        Spot ss = s;
                        while (ss != null)
                        {
                            float di = ss.GetDistanceTo(l);
                            if (di < max_d && !ss.IsBlocked() &&
                                (di < closest_d))
                            {
                                closest   = ss;
                                closest_d = di;
                            }
                            ss = ss.next;
                        }
                    }
                }

                if (closest_d < d) // can't get better
                {
                    //Log("Closest2 spot to " + l + " is " + closest);
                    return(closest);
                }
                d++;
            }
            //Log("Closest1 spot to " + l + " is " + closest);
            return(closest);
        }