private PathfindingChunk CreateChunk(GridId gridId, MapIndices indices)
        {
            var newChunk = new PathfindingChunk(gridId, indices);

            if (!_graph.ContainsKey(gridId))
            {
                _graph.Add(gridId, new Dictionary <MapIndices, PathfindingChunk>());
            }

            _graph[gridId].Add(indices, newChunk);
            newChunk.Initialize(_mapManager.GetGrid(gridId));

            return(newChunk);
        }
Exemple #2
0
        private PathfindingChunk CreateChunk(GridId gridId, MapIndices indices)
        {
            var newChunk = new PathfindingChunk(gridId, indices);

            newChunk.Initialize();
            if (_graph.TryGetValue(gridId, out var chunks))
            {
                for (var x = -1; x < 2; x++)
                {
                    for (var y = -1; y < 2; y++)
                    {
                        if (x == 0 && y == 0)
                        {
                            continue;
                        }

                        var neighborIndices = new MapIndices(
                            indices.X + x * PathfindingChunk.ChunkSize,
                            indices.Y + y * PathfindingChunk.ChunkSize);

                        if (chunks.TryGetValue(neighborIndices, out var neighborChunk))
                        {
                            neighborChunk.AddNeighbor(newChunk);
                        }
                    }
                }
            }
            else
            {
                _graph.Add(gridId, new Dictionary <MapIndices, PathfindingChunk>());
            }

            _graph[gridId].Add(indices, newChunk);

            return(newChunk);
        }
 public PathfindingNode(PathfindingChunk parent, TileRef tileRef)
 {
     _parentChunk = parent;
     TileRef      = tileRef;
     GenerateMask();
 }
 public PathfindingChunkUpdateMessage(PathfindingChunk chunk)
 {
     Chunk = chunk;
 }
        /// <summary>
        /// This will work both ways
        /// </summary>
        /// <param name="chunk"></param>
        /// <exception cref="InvalidOperationException"></exception>
        public void AddNeighbor(PathfindingChunk chunk)
        {
            if (chunk == this)
            {
                return;
            }
            if (Neighbors.ContainsValue(chunk))
            {
                return;
            }

            Direction direction;

            if (chunk.Indices.X < _indices.X)
            {
                if (chunk.Indices.Y > _indices.Y)
                {
                    direction = Direction.NorthWest;
                }
                else if (chunk.Indices.Y < _indices.Y)
                {
                    direction = Direction.SouthWest;
                }
                else
                {
                    direction = Direction.West;
                }
            }
            else if (chunk.Indices.X > _indices.X)
            {
                if (chunk.Indices.Y > _indices.Y)
                {
                    direction = Direction.NorthEast;
                }
                else if (chunk.Indices.Y < _indices.Y)
                {
                    direction = Direction.SouthEast;
                }
                else
                {
                    direction = Direction.East;
                }
            }
            else
            {
                if (chunk.Indices.Y > _indices.Y)
                {
                    direction = Direction.North;
                }
                else if (chunk.Indices.Y < _indices.Y)
                {
                    direction = Direction.South;
                }
                else
                {
                    throw new InvalidOperationException();
                }
            }

            Neighbors.TryAdd(direction, chunk);

            foreach (var node in GetBorderNodes(direction))
            {
                foreach (var counter in chunk.GetCounterpartNodes(direction))
                {
                    var xDiff = node.TileRef.X - counter.TileRef.X;
                    var yDiff = node.TileRef.Y - counter.TileRef.Y;

                    if (Math.Abs(xDiff) <= 1 && Math.Abs(yDiff) <= 1)
                    {
                        node.AddNeighbor(counter);
                        counter.AddNeighbor(node);
                    }
                }
            }

            chunk.Neighbors.TryAdd(OppositeDirection(direction), this);

            if (Neighbors.Count > 8)
            {
                throw new InvalidOperationException();
            }
        }