Beispiel #1
0
        private void SubscribeTwoNeighbors(Vector3Int neighborPos, bool subscribe)
        {
            World world = chunk.world;

            if (world == null)
            {
                return;
            }

            // No chunk lookup if the neighbor positions can't be contained in the world
            //if (!world.CheckInsideWorld(neighborPos))
            //return;

            Chunk neighbor = world.chunks.Get(ref neighborPos);

            if (neighbor == null)
            {
                return;
            }

            ChunkStateManagerClient stateManager = neighbor.stateManager;

            // Subscribe with each other. Passing Idle as event - it is ignored in this case anyway
            stateManager.Subscribe(this, ChunkState.Idle, subscribe);
            Subscribe(stateManager, ChunkState.Idle, subscribe);

            // Update required listener count of the neighbor
            UpdateListenersCount(stateManager);
        }
        private static void OnGenericWork(ref SGenericWorkItem item)
        {
            ChunkStateManagerClient chunk = item.Chunk;

            item.Action();
            OnGenericWorkDone(chunk);
        }
Beispiel #3
0
        private static void OnBuildCollider(ChunkStateManagerClient client)
        {
            Chunk chunk = client.chunk;

            chunk.ChunkColliderGeometryHandler.Build();
            OnBuildColliderDone(client);
        }
        private static void OnGenerateCollider(ref SGenerateColliderWorkItem item)
        {
            ChunkStateManagerClient stateManager = item.StateManager;

            stateManager.chunk.ChunkColliderGeometryHandler.Build(item.MinX, item.MaxX, item.MinY, item.MaxY, item.MinZ, item.MaxZ);
            OnGenerateColliderDone(stateManager);
        }
        private bool SaveData()
        {
            // We need to wait until chunk is generated and data finalized
            if (!m_completedStates.Check(ChunkState.Generate) || !m_completedStates.Check(ChunkState.LoadData))
            {
                return(true);
            }

            m_pendingStates       = m_pendingStates.Reset(CurrStateSaveData);
            m_completedStates     = m_completedStates.Reset(CurrStateSaveData);
            m_completedStatesSafe = m_completedStates;

            m_taskRunning = true;
            IOPoolManager.Add(
                new TaskPoolItem(
                    arg =>
            {
                ChunkStateManagerClient stateManager = (ChunkStateManagerClient)arg;
                OnSaveData(stateManager);
            },
                    this)
                );

            return(true);
        }
Beispiel #6
0
        private static void OnBuildVertices(ChunkStateManagerClient client)
        {
            Chunk chunk = client.chunk;

            chunk.GeometryHandler.Build();
            OnBuildVerticesDone(client);
        }
        private bool GenerateData()
        {
            m_pendingStates       = m_pendingStates.Reset(CurrStateGenerateData);
            m_completedStates     = m_completedStates.Reset(CurrStateGenerateData | CurrStateLoadData);
            m_completedStatesSafe = m_completedStates;

            m_taskRunning = true;

            if (chunk.world.networking.isServer)
            {
                // Let server generate chunk data
                WorkPoolManager.Add(
                    new ThreadPoolItem(
                        chunk.ThreadID,
                        arg =>
                {
                    ChunkStateManagerClient stateManager = (ChunkStateManagerClient)arg;
                    OnGenerateData(stateManager);
                },
                        this)
                    );
            }
            else
            {
                // Client only asks for data
                chunk.world.networking.client.RequestChunk(chunk.pos);
            }

            return(true);
        }
        private static void OnGenerateVerices(ref SGenerateVerticesWorkItem item)
        {
            ChunkStateManagerClient stateManager = item.StateManager;

            stateManager.chunk.GeometryHandler.Build(0, Env.ChunkMask, 0, Env.ChunkMask, 0, Env.ChunkMask);
            OnGenerateVerticesDone(stateManager);
        }
        private static void OnGenerateData(ChunkStateManagerClient stateManager)
        {
            Chunk chunk = stateManager.chunk;

            chunk.world.terrainGen.GenerateTerrain(chunk);

            OnGenerateDataDone(stateManager);
        }
Beispiel #10
0
        private static void UpdateListenersCount(ChunkStateManagerClient stateManager)
        {
            Chunk chunk = stateManager.chunk;
            World world = chunk.world;

            if (world == null)
            {
                return;
            }

            // Calculate how many listeners a chunk can have
            int maxListeners = 0;

            if (world.CheckInsideWorld(chunk.pos.Add(Env.ChunkSize, 0, 0)) && (chunk.pos.x != world.Bounds.maxX))
            {
                ++maxListeners;
            }
            if (world.CheckInsideWorld(chunk.pos.Add(-Env.ChunkSize, 0, 0)) && (chunk.pos.x != world.Bounds.minX))
            {
                ++maxListeners;
            }
            if (world.CheckInsideWorld(chunk.pos.Add(0, Env.ChunkSize, 0)) && (chunk.pos.y != world.Bounds.maxY))
            {
                ++maxListeners;
            }
            if (world.CheckInsideWorld(chunk.pos.Add(0, -Env.ChunkSize, 0)) && (chunk.pos.y != world.Bounds.minY))
            {
                ++maxListeners;
            }
            if (world.CheckInsideWorld(chunk.pos.Add(0, 0, Env.ChunkSize)) && (chunk.pos.z != world.Bounds.maxZ))
            {
                ++maxListeners;
            }
            if (world.CheckInsideWorld(chunk.pos.Add(0, 0, -Env.ChunkSize)) && (chunk.pos.z != world.Bounds.minZ))
            {
                ++maxListeners;
            }

            //int prevListeners = stateManager.ListenerCountMax;

            // Update max listeners and request geometry update
            stateManager.ListenerCountMax = maxListeners;

            // Request synchronization of edges and build geometry
            //if(prevListeners<maxListeners)
            stateManager.m_syncEdgeBlocks = true;

            // Geometry needs to be rebuild
            stateManager.RequestState(ChunkState.BuildVertices);

            // Collider might beed to be rebuild
            if (chunk.NeedsCollider)
            {
                chunk.blocks.RequestCollider();
            }
        }
 public SGenerateColliderWorkItem(ChunkStateManagerClient stateManager, int minX, int maxX, int minY, int maxY, int minZ, int maxZ)
 {
     StateManager = stateManager;
     MinX         = minX;
     MaxX         = maxX;
     MinY         = minY;
     MaxY         = maxY;
     MinZ         = minZ;
     MaxZ         = maxZ;
 }
        private void SubscribeTwoNeighbors(Vector3Int neighborPos, bool subscribe)
        {
            Chunk neighbor = chunk.world.chunks.Get(neighborPos);

            if (neighbor != null)
            {
                ChunkStateManagerClient stateManager = (ChunkStateManagerClient)neighbor.stateManager;
                // Subscribe with each other. Passing Idle as event - it is ignored in this case anyway
                stateManager.Subscribe(this, ChunkState.Idle, subscribe);
                Subscribe(stateManager, ChunkState.Idle, subscribe);
            }
        }
Beispiel #13
0
        private static void OnLoadDataDone(ChunkStateManagerClient stateManager, bool success)
        {
            if (success)
            {
                stateManager.m_completedStates = stateManager.m_completedStates.Set(CurrStateLoadData);
                stateManager.m_nextState       = NextStateLoadData;
            }
            else
            {
                stateManager.m_completedStates = stateManager.m_completedStates.Set(CurrStateLoadData | ChunkState.PrepareGenerate);
                stateManager.m_nextState       = ChunkState.Generate;
            }

            stateManager.m_taskRunning = false;
        }
Beispiel #14
0
        private static void OnGenerateData(ChunkStateManagerClient stateManager)
        {
            Chunk chunk = stateManager.chunk;

            chunk.world.terrainGen.GenerateTerrain(chunk);

            // Commit serialization changes if any
            if (Features.UseSerialization)
            {
                stateManager.m_save.CommitChanges();
            }

            // Calculate the amount of non-empty blocks
            chunk.blocks.CalculateEmptyBlocks();

            //chunk.blocks.Compress();
            //chunk.blocks.Decompress();

            OnGenerateDataDone(stateManager);
        }
Beispiel #15
0
        private static void OnSaveDataDone(ChunkStateManagerClient stateManager, bool success)
        {
            if (Features.UseSerialization)
            {
                if (success)
                {
                    // Notify listeners in case of success
                    stateManager.m_stateExternal = ChunkStateExternal.Saved;
                }
                else
                {
                    // Free temporary memory in case of failure
                    stateManager.m_save.MarkAsProcessed();
                    stateManager.m_completedStates = stateManager.m_completedStates.Set(ChunkState.SaveData);
                }
            }

            stateManager.m_completedStates = stateManager.m_completedStates.Set(CurrStateSaveData);
            stateManager.m_isSaveNeeded    = false;
            stateManager.m_taskRunning     = false;
        }
Beispiel #16
0
        private static void OnPrepareSaveDataDone(ChunkStateManagerClient stateManager, bool success)
        {
            if (Features.UseSerialization)
            {
                if (!success)
                {
                    // Free temporary memory in case of failure
                    stateManager.m_save.MarkAsProcessed();

                    // Consider SaveData completed as well
                    stateManager.m_completedStates = stateManager.m_completedStates.Set(NextStatePrepareSaveData);
                    stateManager.m_isSaveNeeded    = false;
                }
                else
                {
                    stateManager.m_nextState = NextStatePrepareSaveData;
                }
            }

            stateManager.m_completedStates = stateManager.m_completedStates.Set(CurrStatePrepareSaveData);
            stateManager.m_taskRunning     = false;
        }
Beispiel #17
0
        private static void OnPrepareGenerateDone(ChunkStateManagerClient stateManager, bool success)
        {
            // Consume info about invalidated chunk
            stateManager.m_completedStates = stateManager.m_completedStates.Set(CurrStatePrepareGenerate);

            if (success)
            {
                if (stateManager.m_save.IsDifferential)
                {
                    stateManager.m_nextState = NextStatePrepareGenerate;
                }
                else
                {
                    stateManager.m_completedStates = stateManager.m_completedStates.Set(ChunkState.Generate);
                    stateManager.m_nextState       = ChunkState.BuildVertices;
                }
            }
            else
            {
                stateManager.m_nextState = NextStatePrepareGenerate;
            }

            stateManager.m_taskRunning = false;
        }
        private bool LoadData()
        {
            if (!m_completedStates.Check(ChunkState.Generate))
            {
                return(true);
            }

            m_pendingStates       = m_pendingStates.Reset(CurrStateLoadData);
            m_completedStates     = m_completedStates.Reset(CurrStateLoadData);
            m_completedStatesSafe = m_completedStates;

            m_taskRunning = true;
            IOPoolManager.Add(
                new TaskPoolItem(
                    arg =>
            {
                ChunkStateManagerClient stateManager = (ChunkStateManagerClient)arg;
                OnLoadData(stateManager);
            },
                    this)
                );

            return(true);
        }
Beispiel #19
0
 private static void OnBuildVerticesDone(ChunkStateManagerClient stateManager)
 {
     stateManager.m_completedStates = stateManager.m_completedStates.Set(ChunkStates.CurrStateBuildVertices);
     stateManager.m_taskRunning     = false;
 }
        private static void OnSaveData(ChunkStateManagerClient stateManager)
        {
            Serialization.Serialization.SaveChunk(stateManager.chunk);

            OnSaveDataDone(stateManager);
        }
 private static void OnSaveDataDone(ChunkStateManagerClient stateManager)
 {
     stateManager.m_stateExternal   = ChunkStateExternal.Saved;
     stateManager.m_completedStates = stateManager.m_completedStates.Set(CurrStateSaveData);
     stateManager.m_taskRunning     = false;
 }
Beispiel #22
0
        private static void OnSaveData(ChunkStateManagerClient stateManager)
        {
            bool success = Serialization.Serialization.Write(stateManager.m_save);

            OnSaveDataDone(stateManager, success);
        }
 private static void OnGenerateColliderDone(ChunkStateManagerClient stateManager)
 {
     stateManager.m_completedStates = stateManager.m_completedStates.Set(CurrStateGenerateCollider);
     stateManager.m_taskRunning     = false;
 }
Beispiel #24
0
 public static void OnGenerateDataOverNetworkDone(ChunkStateManagerClient stateManager)
 {
     OnGenerateDataDone(stateManager);
     OnLoadDataDone(stateManager, false); //TODO: change to true once the network layers is implemented properly
 }
Beispiel #25
0
 private static void OnGenerateDataDone(ChunkStateManagerClient stateManager)
 {
     stateManager.m_completedStates = stateManager.m_completedStates.Set(CurrStateGenerateData);
     stateManager.m_nextState       = NextStateGenerateData;
     stateManager.m_taskRunning     = false;
 }
 public static void OnGenerateDataOverNetworkDone(ChunkStateManagerClient stateManager)
 {
     OnGenerateDataDone(stateManager);
     OnLoadDataDone(stateManager);
 }
 private static void OnGenericWorkDone(ChunkStateManagerClient chunk)
 {
     chunk.m_completedStates = chunk.m_completedStates.Set(CurrStateGenericWork);
     chunk.m_nextState       = NextStateGenericWork;
     chunk.m_taskRunning     = false;
 }
Beispiel #28
0
        private static void OnPrepareSaveData(ChunkStateManagerClient stateManager)
        {
            bool success = stateManager.m_save.DoCompression();

            OnPrepareSaveDataDone(stateManager, success);
        }
Beispiel #29
0
        private static void OnPrepareGenerate(ChunkStateManagerClient stateManager)
        {
            bool success = stateManager.m_save.DoDecompression();

            OnPrepareGenerateDone(stateManager, success);
        }
 public SGenerateVerticesWorkItem(ChunkStateManagerClient stateManager)
 {
     StateManager = stateManager;
 }