Exemplo n.º 1
0
        // ReSharper disable once UnusedMember.Local
        public IEnumerator CreateMap(BiomeConfiguration biomeConfig, GameLoader loader)
        {
            if (GenerateMap)
            {
                loader.SetStatus("Calculating Heightmap", 0.03f);
                var hmg = new HeightmapGenerator();
                yield return(hmg.CreateHeightMap(129, 129, 42));

                loader.SetStatus("Building Map", 0.1f);
                MapData      = new MapData(hmg.Values.GetLength(0) / Chunk.ChunkSize, 100 / Chunk.ChunkSize, 2f);
                AStarNetwork = new VoxelGraph();
                yield return(MapData.LoadHeightmap(hmg.Values, hmg.BottomValues, hmg.CutPattern, 100));

                yield return(null);
                //RemoveTerrainNotOfType(new[] { MaterialRegistry.Instance.GetMaterialFromName("Iron"), MaterialRegistry.Instance.GetMaterialFromName("Gold"), MaterialRegistry.Instance.GetMaterialFromName("Copper"), MaterialRegistry.Instance.GetMaterialFromName("Coal") });
                //TestAStar();
            }
            else
            {
                MapData      = new MapData(129 / Chunk.ChunkSize, 100 / Chunk.ChunkSize, 2f);
                AStarNetwork = new VoxelGraph();
            }
            IsDoneGenerating          = true;
            VoxelContainer.EnableDraw = true;
        }
Exemplo n.º 2
0
        private static Path CalculateLowlevelPath(VoxelGraph graph, Vector3I from, List <Vector3I> to)
        {
            var start = graph.GetNode(from) ?? graph.GetClosestNode(from, 5);

            if (start == null)
            {
                return(null);
            }
            var targets = to.Select(graph.GetNode).Where(t => t != null).ToList();

            if (targets.Count == 0)
            {
                return(null);
            }
            var path = new Path(start, targets, graph.GetPathRegistry());

            path.Thread = new Thread(() =>
            {
                path          = AStar.GetPath(start, targets, path);
                path.Finished = true;
                path.State    = PathState.Ready;
            });
            path.Thread.Start();
            return(path);
        }
Exemplo n.º 3
0
 public static Path Calculate(VoxelGraph graph, Vector3I from, List <Vector3I> to, bool forceOptimal = false)
 {
     if (to.Any(t => (from - (Vector3)t).magnitude < 200 || forceOptimal))
     {
         return(CalculateLowlevelPath(graph, from, to));
     }
     return(CalculateHighlevelPath(graph, from, to));
 }
Exemplo n.º 4
0
        public Path FollowPath(Path path, VoxelGraph graph)
        {
            if (path.IsT0 || path.Nodes == null)
            {
                return(path);
            }
            var   lastSuper = (SuperNode)path.Nodes.Last();
            var   pos       = 0;
            var   curNode   = path.Start;
            var   visited   = new List <Node>();
            float length    = 0;

            while (!curNode.SuperNodes.ContainsKey(lastSuper))
            {
                if (visited.Contains(curNode))
                {
                    throw new Exception("Loop detected");
                }
                visited.Add(curNode);
                for (var i = 1; i >= 0; i--)
                {
                    if (curNode.SuperNodes.ContainsKey((SuperNode)path.Nodes[pos + i]))
                    {
                        var nextStep = curNode.SuperNodes[(SuperNode)path.Nodes[pos + i]];
                        curNode = nextStep.To;
                        length += nextStep.Length - (nextStep.To != null ? (nextStep.To.SuperNodes[(SuperNode)path.Nodes[pos + i]] != null ? nextStep.To.SuperNodes[(SuperNode)path.Nodes[pos + i]].Length : 0) : 0);
                        pos    += i;
                        break;
                    }
                }
            }
            var finalPath = Path.Calculate(graph, curNode.Position, path.Targets.Select(t => t.Position).ToList());

            finalPath.Thread.Join();
            visited.AddRange(finalPath.Nodes);
            var walkedPath = new Path(path.Start, path.Targets, graph.GetPathRegistry())
            {
                Nodes  = visited,
                Length = length + finalPath.Length
            };

            finalPath.Dispose();
            return(walkedPath);
        }
Exemplo n.º 5
0
 public void Recalculate(Node removedNode, VoxelGraph graph)
 {
     if (State.Equals(PathState.Recalculation))
     {
         Thread.Abort();
     }
     if (Nodes[Nodes.Count - 1].Equals(removedNode))
     {
         State = PathState.Invalid;
     }
     else
     {
         State = PathState.Recalculation;
         var currentNode = GetNode(_currentNode);
         if (currentNode == null)
         {
             State = PathState.Invalid;
         }
         else
         {
             var p2 = Calculate(graph, currentNode.Position, Targets.Select(t => t.Position).ToList());
             if (p2 == null)
             {
                 State = PathState.Invalid;
             }
             else
             {
                 Thread      = p2.Thread;
                 p2.OnFinish = () =>
                 {
                     Nodes  = p2.Nodes;
                     Start  = p2.Start;
                     Length = p2.Length;
                     IsT0   = p2.IsT0;
                     State  = p2.State;
                     p2.Dispose();
                 };
             }
         }
     }
 }
Exemplo n.º 6
0
        private static Path CalculateHighlevelPath(VoxelGraph graph, Vector3I from, List <Vector3I> to)
        {
            var start = graph.GetNode(from) ?? graph.GetClosestNode(from, 5);

            if (start == null)
            {
                return(null);
            }
            var targets = to.Select(graph.GetNode).Where(t => t != null).ToList();

            if (targets.Count == 0)
            {
                return(null);
            }
            var path = new HighLevelPath(start, targets, graph);

            path.Thread = new Thread(() =>
            {
                path          = (HighLevelPath)AStar.GetPath(start.SuperNodes.ToDictionary(n => n.Key as Node, n => n.Value.Length), targets.Select(t => t.GetClosestSuperNode()).ToList(), path);
                path.Finished = true;
            });
            path.Thread.Start();
            return(path);
        }
Exemplo n.º 7
0
 public static Path Calculate(VoxelGraph graph, Vector3I from, Vector3I to, bool forceOptimal = false)
 {
     return(Calculate(graph, from, new List <Vector3I> {
         to
     }, forceOptimal));
 }
Exemplo n.º 8
0
 public HighLevelPath(Node start, List <Node> target, VoxelGraph graph) : base(start, target, graph.GetPathRegistry())
 {
     _graph = graph;
 }