/// <summary> /// Calculates the proper hashcode for a chunk with x and z coordinates in a certain space. /// </summary> /// <param name="x">The X coordinate</param> /// <param name="z">The Z coordinate</param> /// <param name="space">Which space the coordinates are in</param> /// <returns>A long value to serve as the hashcode for the specific coordinate pair </returns> public static ulong GetChunkHashCode(int x, int z, CoordinateSpace space) { if (space == CoordinateSpace.Chunk) { x = ChunkCoordinates2D.ConvertSpace(x, CoordinateSpace.Chunk); z = ChunkCoordinates2D.ConvertSpace(z, CoordinateSpace.Chunk); } return((ulong)x + ((ulong)z << 32)); }
public Chunk(int x, int z, CoordinateSpace space) { if (space == CoordinateSpace.World) { x >>= 4; z >>= 4; } chunkSegments = new List <ChunkSegment>(height); coordinates = new ChunkCoordinates2D(x, z, CoordinateSpace.Chunk); chunks.Add(GetChunkHashCode(), this); for (int y = 0; y < height; y++) { ChunkCoordinates3D coordinates = new ChunkCoordinates3D(x, y, z, CoordinateSpace.Chunk); ChunkSegment segment = new ChunkSegment(this, coordinates); chunkSegments.Add(segment); } }
/// <summary> /// If the chunk exists, returns a reference to the chunk at the provided cooridnates and null otherwise. /// </summary> /// <param name="x">The X coordinate for the chunk.</param> /// <param name="z">The Z coordinate for the chunk.</param> /// <param name="space">The space in which the X and Z coordinates are.</param> /// <returns>The existing chunk at the specific coordinates.</returns> public static Chunk GetChunk(int x, int z, CoordinateSpace space) { if (space == CoordinateSpace.Chunk) { x = ChunkCoordinates2D.ConvertSpace(x, CoordinateSpace.Chunk); z = ChunkCoordinates2D.ConvertSpace(z, CoordinateSpace.Chunk); } Chunk chunk; chunks.TryGetValue((ulong)x + ((ulong)z << 32), out chunk); if (chunk == null) { chunk = new Chunk(x, z, CoordinateSpace.World); } return(chunk); }