Ejemplo n.º 1
0
        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);
                }
            }
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
 public void UnloadChunk(Vec2i chunk)
 {
     if (LoadedChunks.ContainsKey(chunk))
     {
         LoadedChunk loaded = LoadedChunks[chunk];
         LoadedChunks.Remove(chunk);
         GameManager.EntityManager.UnloadChunk(chunk);
         Destroy(loaded.gameObject);
     }
 }
Ejemplo n.º 4
0
        public void UnloadAll(List <Chunk> items)
        {
            var list = new List <Chunk>(items);

            foreach (var item in list)
            {
                LoadedChunks.Remove(item);
                Unload(item);
            }
        }
Ejemplo n.º 5
0
        public void ChunkUnloaded(Int3 batchId, int level)
        {
            Chunk chunk = new Chunk(batchId, level);

            if (loadedChunks.Contains(chunk))
            {
                loadedChunks.Remove(chunk);
                removed.Add(chunk);
                markChunksReadyForSync(0);
            }
        }
Ejemplo n.º 6
0
        public List <Chunk> RemoveChunks(IEnumerable <Vector2Int> chunkPositions)
        {
            List <Chunk> removedChunks = new List <Chunk>();

            foreach (var chunkPosition in chunkPositions)
            {
                removedChunks.Add(LoadedChunks[chunkPosition]);
                LoadedChunks.Remove(chunkPosition);
            }
            return(removedChunks);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Unloads the given chunk on the client.
        /// </summary>
        public virtual void UnloadChunk(Coordinates2D position)
        {
            var dataPacket = new ChunkDataPacket();

            dataPacket.AddBitMap          = 0;
            dataPacket.GroundUpContinuous = true;
            dataPacket.PrimaryBitMap      = 0;
            dataPacket.X    = (int)position.X;
            dataPacket.Z    = (int)position.Z;
            dataPacket.Data = ChunkHelper.ChunkRemovalSequence;
            SendPacket(dataPacket);
            LoadedChunks.Remove(position);
        }
Ejemplo n.º 8
0
    /// <summary>
    /// Loads all chunks within a square of size 2*radius centred on the player
    /// </summary>
    /// <param name="middle"></param>
    /// <param name="radius"></param>
    public void LoadChunks(Vec2i middle, int radius, bool forceLoad)
    {
        LoadedChunksCentre = middle;
        //Create lists to hold chunks to load and unload
        List <Vec2i> toLoad = new List <Vec2i>((LoadChunkRadius * 2) * (LoadChunkRadius * 2));

        toLoad.AddRange(ToLoad);
        List <Vec2i> toUnload = new List <Vec2i>(100);

        //Iterate all loaded chunks and add to to unload
        foreach (KeyValuePair <Vec2i, LoadedChunk> kvp in LoadedChunks)
        {
            toUnload.Add(kvp.Key);
        }
        for (int x = -radius; x <= radius; x++)
        {
            for (int z = -radius; z <= radius; z++)
            {
                if (x + middle.x < 0 || x + middle.x >= World.WorldSize - 1 || z + middle.z < 0 || z + middle.z >= World.WorldSize - 1)
                {
                    continue;
                }
                Vec2i pos = new Vec2i(x + middle.x, z + middle.z);
                //If it is already loaed, remove it from the to remove
                if (toUnload.Contains(pos))
                {
                    toUnload.Remove(pos);
                }
                else if (!toLoad.Contains(pos))
                {
                    toLoad.Add(pos);
                }
            }
        }
        foreach (Vec2i v in toLoad)
        {
            if (LoadChunk(v, forceLoad))
            {
                ToLoad.Remove(v);
            }
        }
        List <LoadedChunk> toUnloadChunks = new List <LoadedChunk>(20);

        foreach (Vec2i v in toUnload)
        {
            LoadedChunk lc = LoadedChunks[v];
            LoadedChunks.Remove(v);
            Destroy(lc.gameObject);
            GameManager.EntityManager.UnloadChunk(v);
        }
    }
Ejemplo n.º 9
0
    public void UnloadChunk(Vec2i chunk)
    {
        if (LoadedChunks.ContainsKey(chunk))
        {
            LoadedChunk2 loaded = LoadedChunks[chunk];
            lock (LoadedChunksLock)
            {
                LoadedChunks.Remove(chunk);
            }


            //We set inactive, and add it the chunk Loaders ChunkBuffer
            loaded.gameObject.SetActive(false);
            ChunkLoader.AddToChunkBuffer(loaded);
            //GameManager.EntityManager.UnloadChunk(chunk);
            //Destroy(loaded.gameObject);
        }
    }
Ejemplo n.º 10
0
        /// <summary>
        /// Handles all actions that need to be executed only once per second, such as chunk saving to file or cloud movement ticking.
        /// </summary>
        void OncePerSecondActions()
        {
            TickClouds();
            List <Vector3i> DelMe = new List <Vector3i>();

            foreach (Chunk chk in LoadedChunks.Values)
            {
                if (chk.LastEdited >= 0)
                {
                    chk.SaveToFile(null);
                }
                bool seen = false;
                foreach (PlayerEntity player in Players)
                {
                    if (player.ShouldLoadChunk(chk.WorldPosition))
                    {
                        seen            = true;
                        chk.UnloadTimer = 0;
                        break;
                    }
                }
                if (!seen)
                {
                    chk.UnloadTimer += Delta;
                    if (chk.UnloadTimer > UnloadLimit)
                    {
                        chk.UnloadSafely();
                        DelMe.Add(chk.WorldPosition);
                    }
                }
            }
            foreach (Vector3i loc in DelMe)
            {
                LoadedChunks.Remove(loc);
            }
            foreach (KeyValuePair <Vector2i, BlockUpperArea> bua in UpperAreas)
            {
                if (bua.Value.Edited)
                {
                    PushTopsEdited(bua.Key, bua.Value);
                }
            }
        }
Ejemplo n.º 11
0
        private static void CheckForUnload()
        {
            int counter = 0;

            foreach (var item in LoadedChunks.Values.ToList())
            {
                if (Vector2.Distance(new Vector2(CamX, CamZ), item.ChunkPosition) > MAX_RENDER_DISTANCE)
                {
                    //Console.WriteLine("ChunkDistance: " + Vector2.Distance(new Vector2(CamX, CamZ), item.ChunkPosition));

                    if (counter > MAX_CHUNKS_PER_FRAME)
                    {
                        return;
                    }

                    Chunk chunk = item;
                    chunk.Unload();

                    LoadedChunks.Remove(chunk.ChunkPosition);

                    counter++;
                }
            }
        }
Ejemplo n.º 12
0
 internal void UnloadChunk(Coordinates2D position)
 {
     QueuePacket(new ChunkPreamblePacket(position.X, position.Z, false));
     LoadedChunks.Remove(position);
 }
Ejemplo n.º 13
0
        public override void Update()
        {
            if (_unloaded)
            {
                return;
            }

            //Update entities
            foreach (var playerEntity in PlayerEntities)
            {
                playerEntity.Update();
            }
            foreach (var entity in Entities)
            {
                entity.Update();
            }

            lock (_chunksReadyToRemove)
            {
                while (_chunksReadyToRemove.Count > 0)
                {
                    var chunkPos = _chunksReadyToRemove.First();

                    if (LoadedChunks.TryGetValue(chunkPos, out var chunk))
                    {
                        lock (LoadedChunks)
                        {
                            LoadedChunks.Remove(chunkPos);
                        }
                        _populatedChunks.Remove(chunkPos);
                        chunk.Dispose();
                    }

                    _chunksReadyToRemove.Remove(chunkPos);
                }
            }

            lock (_chunksReadyToAdd)
            {
                while (_chunksReadyToAdd.Count > 0)
                {
                    var entry = _chunksReadyToAdd.First();
                    lock (LoadedChunks)
                    {
                        if (!LoadedChunks.ContainsKey(entry.Key))
                        {
                            LoadedChunks.Add(entry.Key, new Chunk(entry.Value));
                        }
                        else
                        {
                            Logger.Error("Chunk has already been loaded! " + entry.Key);
                        }
                    }
                    _populatedChunks.Add(entry.Key);
                    _chunksReadyToAdd.Remove(entry.Key);
                }
            }

            while (_chunksReadyToUploadHp.Count > 0)
            {
                lock (_chunksReadyToUploadHp)
                {
                    _chunksReadyToUploadHp.Dequeue().Upload();
                }
            }

            while (_chunksReadyToUploadLp.Count > 0)
            {
                lock (_chunksReadyToUploadLp)
                {
                    _chunksReadyToUploadLp.Dequeue().Upload();
                }
            }
        }
Ejemplo n.º 14
0
 public void UnloadChunk(CubeChunk chunk)
 {
     TrySetChunk(chunk);
     LoadedChunks.Remove(chunk);
 }