Example #1
0
 public NearRegionResult(NearRegionPoint root, List <NearRegionPoint> ends)
 {
     Ends = ends;
     Root = root;
 }
        private NearRegionPoint SearchRoot(Point from, int around = 4, int current = 0, NearRegionPoint parentResult = null)
        {
            Dumper?.Record("Check Movable");
            if (parentResult == null)
            {
                parentResult = new NearRegionPoint()
                {
                    To             = from,
                    MovedDirection = Direction.None,
                    Went           = new List <Point>(new Point[] { from }),
                    Max            = from,
                    Min            = from
                };
            }

            if (current++ < around)
            {
                var reversed = parentResult.MovedDirection.Reverse();

                var movable = SearchableDirections.Where(d =>
                {
                    if (d != reversed)
                    {
                        var to = parentResult.To.Move(d);
                        return(Game.Field.IsInField(parentResult.To.Move(d)) && !parentResult.Went.Contains(to));
                    }
                    return(false);
                }).Select((d, i) => (d, i)).ToArray();

                var children = new NearRegionPoint[movable.Length];

                Parallel.ForEach(movable, d =>
                {
                    var to     = parentResult.To.Move(d.d);
                    var result = new NearRegionPoint()
                    {
                        From           = parentResult.To,
                        To             = to,
                        MovedDirection = d.d,
                        Went           = parentResult.Went.ToList(),
                        Parent         = parentResult,
                        Max            = parentResult.Max,
                        Min            = parentResult.Min
                    };

                    //Maxを更新
                    if (to.X > parentResult.Max.X)
                    {
                        result.Max = new Point(to.X, result.Max.Y);
                    }

                    if (to.Y > parentResult.Max.Y)
                    {
                        result.Max = new Point(result.Max.X, to.Y);
                    }

                    //Minを更新
                    if (to.X < parentResult.Min.X)
                    {
                        result.Min = new Point(to.X, result.Min.Y);
                    }

                    if (to.Y < parentResult.Min.Y)
                    {
                        result.Min = new Point(result.Min.X, to.Y);
                    }

                    result.Went.Add(to);
                    children[d.i] = result;

                    SearchRoot(from, around, current, result);
                });

                foreach (var c in children)
                {
                    parentResult.Children.Add(c);
                }
            }

            return(parentResult);
        }