public bool GetNeighbouringChunkTile(Vector2Int chunk, Vector2Int direction, Vector2Int tilePosition, out Tile tile) { if (LoadedChunks.ContainsKey(chunk + direction)) { // 15 -> 0 (+1) // 0 -> 15 (-1) int xPosition = (int)tilePosition.x; int yPosition = (int)tilePosition.y; if (xPosition == -1) { xPosition = Chunk.SIZE - 1; } else if (xPosition == Chunk.SIZE) { xPosition = 0; } if (yPosition == -1) { yPosition = Chunk.SIZE - 1; } else if (yPosition == Chunk.SIZE) { yPosition = 0; } tile = LoadedChunks[chunk + direction].Tiles[xPosition, yPosition]; return(true); } tile = null; return(false); }
private static void UpdateToMesh() { if (CreationQueue.Count < 1) { return; } // Max chunk per frame counter int counter = 0; foreach (Vector2 chunkPos in CreationQueue.ToList()) { if (counter > MAX_CHUNKS_PER_FRAME) { return; } if (IsChunkLoaded(chunkPos)) { continue; } Chunk newChunk = new Chunk((int)chunkPos.X, (int)chunkPos.Y); LoadedChunks.Add(chunkPos, newChunk); CreationQueue.Remove(chunkPos); counter++; } }
public List <Chunk> RemoveChunks(int playerId, IEnumerable <Vector2Int> chunksToUnloadForPlayer) { List <Chunk> chunks = new List <Chunk>(); List <Vector2Int> chunksLoadedByPlayer; if (PlayerLoadedChunks.TryGetValue(playerId, out chunksLoadedByPlayer)) { foreach (var chunkToUnload in chunksToUnloadForPlayer) { chunksLoadedByPlayer.Remove(chunkToUnload); var playersDependantOnChunk = ChunkLoadedByPlayers[chunkToUnload]; playersDependantOnChunk.Remove(playerId); if (playersDependantOnChunk.Count == 0) { chunks.Add(LoadedChunks[chunkToUnload]); ChunkLoadedByPlayers.Remove(chunkToUnload); LoadedChunks.Remove(chunkToUnload); } } } return(chunks); }
public void QueueChunkUpdate(Vector3i chunkPos, bool lowPrioriity) { if (LoadedChunks.TryGetValue(chunkPos, out var chunk)) { QueueChunkUpdate(chunk, lowPrioriity); } }
internal void UpdateChunks() { var newChunks = new List <Coordinates2D>(); for (int x = -ChunkRadius; x < ChunkRadius; x++) { for (int z = -ChunkRadius; z < ChunkRadius; z++) { newChunks.Add(new Coordinates2D( ((int)Entity.Position.X >> 4) + x, ((int)Entity.Position.Z >> 4) + z)); } } // Unload extraneous columns lock (LoadedChunks) { var currentChunks = new List <Coordinates2D>(LoadedChunks); foreach (Coordinates2D chunk in currentChunks) { if (!newChunks.Contains(chunk)) { UnloadChunk(chunk); } } // Load new columns foreach (Coordinates2D chunk in newChunks) { if (!LoadedChunks.Contains(chunk)) { LoadChunk(chunk); } } } ((EntityManager)Server.GetEntityManagerForWorld(World)).UpdateClientEntities(this); }
public override BlockData GetBlockData(int x, int y, int z) { var chunkInWorld = ChunkInWorld(x, y, z); var blockInChunk = BlockInChunk(x, y, z); return(LoadedChunks.TryGetValue(chunkInWorld, out Chunk chunk) ? chunk.GetBlockData(blockInChunk) : null); }
internal void LoadChunk(IChunk chunk) { QueuePacket(new ChunkPreamblePacket(chunk.Coordinates.X, chunk.Coordinates.Z)); QueuePacket(CreatePacket(chunk)); Server.Scheduler.ScheduleEvent("client.finalize-chunks", this, TimeSpan.Zero, server => { return; LoadedChunks.Add(chunk.Coordinates); foreach (var kvp in chunk.TileEntities) { var coords = kvp.Key; var descriptor = new BlockDescriptor { Coordinates = coords + new Coordinates3D(chunk.X, 0, chunk.Z), Metadata = chunk.GetMetadata(coords), ID = chunk.GetBlockID(coords), BlockLight = chunk.GetBlockLight(coords), SkyLight = chunk.GetSkyLight(coords) }; var provider = Server.BlockRepository.GetBlockProvider(descriptor.ID); provider.TileEntityLoadedForClient(descriptor, World, kvp.Value, this); } }); }
public Chunk GetChunk(Vector2Int chunkPosition) { Chunk result; LoadedChunks.TryGetValue(chunkPosition, out result); return(result); }
/// <summary> /// Get the relative Chunk from a world position. /// </summary> /// <param name="chunkPosition"></param> /// <param name="worldPosition"></param> /// <returns></returns> /// <exception cref="ArgumentOutOfRangeException"></exception> public Chunk GetRelativeChunk(Direction chunkPosition, Vector3Int worldPosition) { switch (chunkPosition) { case Direction.Left: return(LoadedChunks.TryGetValue(worldPosition + new Vector3Int(-8, 0, 0), out var leftChunk) ? leftChunk : null); case Direction.Right: return(LoadedChunks.TryGetValue(worldPosition + new Vector3Int(8, 0, 0), out var rightChunk) ? rightChunk : null); case Direction.Bottom: return(LoadedChunks.TryGetValue(worldPosition + new Vector3Int(0, -4, 0), out var bottomChunk) ? bottomChunk : null); case Direction.Top: return(LoadedChunks.TryGetValue(worldPosition + new Vector3Int(0, 4, 0), out var topChunk) ? topChunk : null); case Direction.Back: return(LoadedChunks.TryGetValue(worldPosition + new Vector3Int(0, 0, -8), out var backChunk) ? backChunk : null); case Direction.Front: return(LoadedChunks.TryGetValue(worldPosition + new Vector3Int(0, 0, 8), out var frontChunk) ? frontChunk : null); default: throw new ArgumentOutOfRangeException(nameof(chunkPosition), chunkPosition, null); } }
public TerrainChunk GetGeneratedChunk(Vector2i chunkPosition) { if (LoadedChunks.ContainsKey(chunkPosition)) return LoadedChunks[chunkPosition]; return null; }
private void CreateTerrainForReadyChunks() { var anyTerrainCreated = false; var chunks = ChunksBeingGenerated.ToList(); foreach (var chunk in chunks) { if (chunk.Value.IsHeightmapReady()) { ChunksBeingGenerated.Remove(chunk.Key); LoadedChunks.Add(chunk.Key, chunk.Value); chunk.Value.CreateTerrain(); anyTerrainCreated = true; if (OnChunkGenerated != null) { OnChunkGenerated.Invoke(ChunksBeingGenerated.Count); } SetChunkNeighborhood(chunk.Value); } } if (anyTerrainCreated) { UpdateAllChunkNeighbors(); } }
private void TryToDeleteQueuedChunks() { var chunksToRemove = ChunksToRemove.ToList(); foreach (var chunkPosition in chunksToRemove) { if (RequestedChunks.ContainsKey(chunkPosition)) { RequestedChunks.Remove(chunkPosition); ChunksToRemove.Remove(chunkPosition); } else if (LoadedChunks.ContainsKey(chunkPosition)) { var chunk = LoadedChunks[chunkPosition]; chunk.Remove(); LoadedChunks.Remove(chunkPosition); ChunksToRemove.Remove(chunkPosition); } else if (!ChunksBeingGenerated.ContainsKey(chunkPosition)) { ChunksToRemove.Remove(chunkPosition); } } }
internal void UnloadAllChunks() { while (LoadedChunks.Any()) { UnloadChunk(LoadedChunks.First()); } }
private void LoadInSingleChunk(ChunkData data) { Debug.BeginDeepProfile("chunk_load"); //Initiate chunk Vec2i chunk = new Vec2i(data.X, data.Z); if (LoadedChunks.ContainsKey(chunk)) { return; } GameObject chunkObject = Instantiate(ResourceManager.ChunkPrefab); chunkObject.transform.parent = transform; chunkObject.name = "Chunk " + chunk; //LoadedChunk loadedChunk = chunkObject.AddComponent<LoadedChunk>(); LoadedChunk loadedChunk = chunkObject.GetComponent <LoadedChunk>(); ChunkData[] neigh = { GetChunk(chunk.x, chunk.z + 1), GetChunk(chunk.x + 1, chunk.z + 1), GetChunk(chunk.x + 1, chunk.z) }; //ChunkData[] neigh = { null, null, null }; loadedChunk.SetChunkData(data, neigh); LoadedChunks.Add(chunk, loadedChunk); GameManager.EntityManager.LoadChunk(World.ChunkBases[chunk.x, chunk.z], chunk); Debug.EndDeepProfile("chunk_load"); }
public bool LoadChunk(Vec2i chunk, bool forceLoad = false) { //Check if chunk is loaded if (LoadedChunks.ContainsKey(chunk)) { Debug.Log("Chunk " + chunk + " is already loaded!", Debug.CHUNK_LOADING); return(true); } //Retrieve data ChunkData data = GetChunk(chunk.x, chunk.z); if (data == null) { Debug.Log("Chunk " + chunk + " could not be found!", Debug.CHUNK_LOADING); return(false); } if (forceLoad) { LoadInSingleChunk(data); } else if (!ToLoadIn.Contains(data)) { Debug.Log("Added " + chunk + " to load in", Debug.CHUNK_LOADING); ToLoadIn.Add(data); } return(true); }
private void SetChunkNeighborhood(TerrainChunk chunk) { TerrainChunk xUp; TerrainChunk xDown; TerrainChunk zUp; TerrainChunk zDown; LoadedChunks.TryGetValue(new Vector2i(chunk.Position.X + 1, chunk.Position.Z), out xUp); LoadedChunks.TryGetValue(new Vector2i(chunk.Position.X - 1, chunk.Position.Z), out xDown); LoadedChunks.TryGetValue(new Vector2i(chunk.Position.X, chunk.Position.Z + 1), out zUp); LoadedChunks.TryGetValue(new Vector2i(chunk.Position.X, chunk.Position.Z - 1), out zDown); if (xUp != null) { chunk.SetNeighbors(xUp, TerrainNeighbor.XUp); xUp.SetNeighbors(chunk, TerrainNeighbor.XDown); } if (xDown != null) { chunk.SetNeighbors(xDown, TerrainNeighbor.XDown); xDown.SetNeighbors(chunk, TerrainNeighbor.XUp); } if (zUp != null) { chunk.SetNeighbors(zUp, TerrainNeighbor.ZUp); zUp.SetNeighbors(chunk, TerrainNeighbor.ZDown); } if (zDown != null) { chunk.SetNeighbors(zDown, TerrainNeighbor.ZDown); zDown.SetNeighbors(chunk, TerrainNeighbor.ZUp); } }
private void UnloadThread() { while (!_unloaded) { List <Chunk> chunksToUnload; lock (LoadedChunks) { chunksToUnload = LoadedChunks.Where( pair => DateTime.Now - pair.Value.Time > ChunkLifetime && !_chunksReadyToRemove.Contains(pair.Key)).Select(pair => pair.Value).ToList(); } foreach (var chunk in chunksToUnload) { WorldSerializer.SaveChunk(chunk); lock (_chunksReadyToRemove) { _chunksReadyToRemove.Add(chunk.Position); } } Thread.Sleep(1000); } }
/// <summary> /// Checks if the specified chunk coordinate is currently loaded, /// checking both the world, and checking if a subworld is loaded /// </summary> /// <param name="chunkPos"></param> /// <returns></returns> public bool IsCurrentChunkPositionLoaded(Vec2i chunkPos) { if (InSubworld) { return(SubworldChunks.ContainsKey(chunkPos)); } return(LoadedChunks.ContainsKey(chunkPos)); }
public MultiplayerClient(String playerId) { loadedChunks = new LoadedChunks(); chunkAwarePacketReceiver = new ChunkAwarePacketReceiver(loadedChunks); client = new TcpClient(chunkAwarePacketReceiver); PacketSender = new PacketSender(client); PacketSender.PlayerId = playerId; }
public bool ChunkCanBeRemoved(int x, int z) { var key = new Vector2i(x, z); return RequestedChunks.ContainsKey(key) || ChunksBeingGenerated.ContainsKey(key) || LoadedChunks.ContainsKey(key); }
/// <summary> /// Chunks the can be added. /// </summary> /// <returns><c>true</c>, if can be added was chunked, <c>false</c> otherwise.</returns> /// <param name="x">The x coordinate.</param> /// <param name="z">The z coordinate.</param> public bool ChunkCanBeAdded(int x, int z) { var key = new XVec2I(x, z); return (!(RequestedChunks.ContainsKey(key) || ChunksBeingGenerated.ContainsKey(key) || LoadedChunks.ContainsKey(key))); }
public void Awake() { DevConsole.RegisterConsoleCommand(this, "mplayer", false); loadedChunks = new LoadedChunks(); chunkAwarePacketReceiver = new ChunkAwarePacketReceiver(loadedChunks); client = new TcpClient(chunkAwarePacketReceiver); PacketSender = new PacketSender(client); }
public override LightLevel GetBlockLightLevel(int x, int y, int z) { var chunkInWorld = ChunkInWorld(x, y, z); var blockInChunk = BlockInChunk(x, y, z); return(LoadedChunks.TryGetValue(chunkInWorld, out Chunk chunk) ? chunk.GetLightLevel(blockInChunk) : LightLevel.Zero); }
public TerrainChunk GetGeneratedChunk(Vector2i chunkPosition) { if (LoadedChunks.ContainsKey(chunkPosition)) { return(LoadedChunks[chunkPosition]); } return(null); }
public override Block GetBlock(int x, int y, int z) { var chunkInWorld = ChunkInWorld(x, y, z); var blockInChunk = BlockInChunk(x, y, z); return(LoadedChunks.TryGetValue(chunkInWorld, out Chunk chunk) ? GameRegistry.GetBlock(chunk.GetBlock(blockInChunk)) : BlockRegistry.BlockAir); }
public void UnloadAll(List <Chunk> items) { var list = new List <Chunk>(items); foreach (var item in list) { LoadedChunks.Remove(item); Unload(item); } }
public void UnloadChunk(Vec2i chunk) { if (LoadedChunks.ContainsKey(chunk)) { LoadedChunk loaded = LoadedChunks[chunk]; LoadedChunks.Remove(chunk); GameManager.EntityManager.UnloadChunk(chunk); Destroy(loaded.gameObject); } }
public virtual void UnloadAllChunks() { lock (LoadedChunks) { while (LoadedChunks.Any()) { UnloadChunk(LoadedChunks[0]); } } }
public MultiplayerClient(String playerId) { Log.SetLevel(Log.LogLevel.ConsoleInfo | Log.LogLevel.ConsoleDebug); loadedChunks = new LoadedChunks(); chunkAwarePacketReceiver = new ChunkAwarePacketReceiver(loadedChunks); client = new TcpClient(chunkAwarePacketReceiver); PacketSender = new PacketSender(client); PacketSender.PlayerId = playerId; Logic = new Logic(PacketSender, loadedChunks, chunkAwarePacketReceiver); }
internal void UnloadAllChunks() { lock (LoadedChunks) { while (LoadedChunks.Any()) { UnloadChunk(LoadedChunks[0]); } } }