Beispiel #1
0
        public void TracerTest2()
        {
            var binary = MapTests.GenerateCleanMap2D(5, 5);

            binary[2, 1] = false;
            binary[2, 2] = false;
            binary[2, 3] = false;

            var start  = new Vector3(0, 2, 0);
            var finish = new Vector3(4, 2, 0);

            var map     = new GridMap(binary);
            var queue   = new PriorityQueue <Wave>(new PriorityByEstimatedPathLength <Wave>(finish));
            var builder = new Base <Wave> .Builder();

            var initial     = new InitializeX <Wave>(map, builder, start);
            var propagation = new PropagateX <Wave>(map, builder);

            builder.PropagationStrategy = propagation;
            builder.AcceptanceCondition = new AreaCondition(finish);

            var tracer = new Tracer <Wave> {
                DefaultMap = map
            };
            var wave = tracer.Search(initial, map, queue).FirstOrDefault();

            Assert.NotNull(wave);

            var path = wave.FullPath;

            Assert.Equal(path.Length, 5);
            Assert.Equal(path[0], start);
            Assert.Equal(path[4], finish);
            Assert.True(path.All(p => binary[(int)p.X, (int)p.Y] == true));
        }
Beispiel #2
0
        /// <summary>
        /// Searches for the shortest path between two points.
        /// </summary>
        /// <typeparam name="TWave">Type of waves to use (e.g. <see cref="Wave"/>).</typeparam>
        /// <param name="tracer">Tracer to search with.</param>
        /// <param name="from">Location to start from.</param>
        /// <param name="to">Location to reach.</param>
        /// <param name="map">Map to navigate (e.g. <see cref="GridMap"/>); if left null, <see cref="Tracer{TWave}.DefaultMap"/> will be used.</param>
        /// <param name="custom">Wave builder to create waves with (e.g. <see cref="Waves.Base{TWave}.Builder"/>).</param>
        /// <returns>List of waypoints.</returns>
        /// <exception cref="ArgumentNullException">Thrown if both <paramref name="map"/> parameter and <see cref="Tracer{TWave}.DefaultMap"/> field are null.</exception>
        public static IReadOnlyList <Vector3> FindShortestPath <TWave>(
            this Tracer <TWave> tracer, Vector3 from, Vector3 to, IMap <TWave> map = null, IWaveBuilder <TWave> custom = null)
            where TWave : Base <TWave>, new()
        {
            if (map == null && tracer.DefaultMap == null)
            {
                throw new ArgumentNullException(nameof(map), "ECHO: Either [map] parameter or [Tracer.DefaultMap] field must not be null.");
            }

            map = map ?? tracer.DefaultMap;

            var guide   = map as IDirectionsProvider;
            var builder = new Base <TWave> .Builder {
                NestedBuilder = custom
            };

            IInitializationStrategy <TWave> initial;
            IPropagationStrategy <TWave>    propagation;

            if (guide == null)
            {
                initial     = new Initialize26x3D <TWave>(builder, from);
                propagation = new Propagate16x3D <TWave>(builder);
            }
            else
            {
                initial     = new InitializeX <TWave>(guide, builder, from);
                propagation = new PropagateX <TWave>(guide, builder);
            }

            builder.PropagationStrategy = propagation;
            builder.AcceptanceCondition = new AreaCondition(to);
            builder.FadeCondition       = new GlobalIntersectionsCondition();
            return(tracer.FindShortestPath(initial, to, map));
        }