private void Handle(object i) { ManualResetEvent doneEvent = null; try { doneEvent = _threadManager.GetEvents()[(int)i]; while (_status == Status.process) { var chunk = _queueReader.Dequeue(); if (chunk.Equals(default(KeyValuePair <int, byte[]>))) { doneEvent.Set(); return; } _gZipStrategy.Handle(chunk, _queueWriter); } doneEvent.Set(); } catch (ObjectDisposedException) { HandleException($"Thread number: {i}. Message: Работа потока прервана из-за непредвиденной ошибки"); } catch (Exception ex) { HandleException($"Thread number: {i}. Message: {ex.Message}"); } }
private void Write() { try { using (var outStream = new FileStream(_outputFile, FileMode.Append)) { while (_status == Status.process) { var chunk = _queueWriter.Dequeue(); if (chunk.Equals(default(KeyValuePair <int, byte[]>))) { return; } _gZipStrategy.Write(chunk, outStream); } } } catch (Exception ex) { HandleException(ex.Message); } }
void ProcessQueue(ChunkQueue queue) { Vector3I p = chunkPos; while (queue.Size > 0) { ChunkInfo chunk = queue.Dequeue(); int x1 = chunk.CentreX - 8, x2 = chunk.CentreX + 8; int y1 = chunk.CentreY - 8, y2 = chunk.CentreY + 8; int z1 = chunk.CentreZ - 8, z2 = chunk.CentreZ + 8; int xOffset, yOffset, zOffset; int dx = Math.Max(x1 - p.X, Math.Max(0, p.X - x2)); int dy = Math.Max(y1 - p.Y, Math.Max(0, p.Y - y2)); int dz = Math.Max(z1 - p.Z, Math.Max(0, p.Z - z2)); int distX, distY, distZ; // X axis collisions int dxLeft = Math.Abs(x1 - p.X), dxRight = Math.Abs(x2 - p.X); if (dxLeft < dxRight) { distX = dxLeft * dxLeft + dy * dy + dz * dz; xOffset = -1; } else { distX = dxRight * dxRight + dy * dy + dz * dz; xOffset = 1; } // Z axis collisions int dxFront = Math.Abs(z1 - p.Z), dxBack = Math.Abs(z2 - p.Z); if (dxFront < dxBack) { distZ = dx * dx + dy * dy + dxFront * dxFront; zOffset = -1; } else { distZ = dx * dx + dy * dy + dxBack * dxBack; zOffset = 1; } // Y axis collisions int dxBottom = Math.Abs(y1 - p.Y), dxTop = Math.Abs(y2 - p.Y); if (dxBottom < dxTop) { distY = dx * dx + dxBottom * dxBottom + dz * dz; yOffset = -1; } else { distY = dx * dx + dxTop * dxTop + dz * dz; yOffset = 1; } chunk.Occluded = true; int distMin = Math.Min(distX, Math.Min(distY, distZ)); int cx = chunk.CentreX >> 4, cy = chunk.CentreY >> 4, cz = chunk.CentreZ >> 4; if (!chunk.Empty) { Console.WriteLine(chunk.OccludedFlags + " , " + xOffset + " : " + yOffset + " : " + zOffset); } if (distMin == distX) { OccludeX(cx, cy, cz, xOffset, chunk); } if (distMin == distY) { OccludeY(cx, cy, cz, yOffset, chunk); } if (distMin == distZ) { OccludeZ(cx, cy, cz, zOffset, chunk); } //Console.WriteLine(distMin + " , " + distX + " , " + distY + " , " + distZ); //Console.WriteLine(chunk.DistanceFlags + " : " + chunk.OccludedFlags + " : " + chunk.OcclusionFlags); QueueChunk(cx - 1, cy, cz, queue); QueueChunk(cx + 1, cy, cz, queue); QueueChunk(cx, cy, cz - 1, queue); QueueChunk(cx, cy, cz + 1, queue); QueueChunk(cx, cy - 1, cz, queue); QueueChunk(cx, cy + 1, cz, queue); if (!chunk.Empty) { Console.WriteLine("{0} D {1}: V {2}, O {3}, {4}", cx + "," + cy + "," + cz, chunk.DistanceFlags, chunk.OccludedFlags, chunk.OcclusionFlags, chunk.Occluded); Console.WriteLine(" M {0} : X {1}, Y {2}, Z {3} ({4}, {5})", distMin, distX, distY, distZ, dxFront, dxBack); } } Console.WriteLine("======================"); }
void ProcessQueue( ChunkQueue queue ) { Vector3I p = chunkPos; while( queue.Size > 0 ) { ChunkInfo chunk = queue.Dequeue(); int x1 = chunk.CentreX - 8, x2 = chunk.CentreX + 8; int y1 = chunk.CentreY - 8, y2 = chunk.CentreY + 8; int z1 = chunk.CentreZ - 8, z2 = chunk.CentreZ + 8; int xOffset, yOffset, zOffset; int dx = Math.Max( x1 - p.X, Math.Max( 0, p.X - x2 ) ); int dy = Math.Max( y1 - p.Y, Math.Max( 0, p.Y - y2 ) ); int dz = Math.Max( z1 - p.Z, Math.Max( 0, p.Z - z2 ) ); int distX, distY, distZ; // X axis collisions int dxLeft = Math.Abs( x1 - p.X ), dxRight = Math.Abs( x2 - p.X ); if( dxLeft < dxRight ) { distX = dxLeft * dxLeft + dy * dy + dz * dz; xOffset = -1; } else { distX = dxRight * dxRight + dy * dy + dz * dz; xOffset = 1; } // Z axis collisions int dxFront = Math.Abs( z1 - p.Z ), dxBack = Math.Abs( z2 - p.Z ); if( dxFront < dxBack ) { distZ = dx * dx + dy * dy + dxFront * dxFront; zOffset = -1; } else { distZ = dx * dx + dy * dy + dxBack * dxBack; zOffset = 1; } // Y axis collisions int dxBottom = Math.Abs( y1 - p.Y ), dxTop = Math.Abs( y2 - p.Y ); if( dxBottom < dxTop ) { distY = dx * dx + dxBottom * dxBottom + dz * dz; yOffset = -1; } else { distY = dx * dx + dxTop * dxTop + dz * dz; yOffset = 1; } chunk.Occluded = true; int distMin = Math.Min( distX, Math.Min( distY, distZ ) ); int cx = chunk.CentreX >> 4, cy = chunk.CentreY >> 4, cz = chunk.CentreZ >> 4; if( !chunk.Empty ) { Console.WriteLine( chunk.OccludedFlags + " , " + xOffset + " : " + yOffset + " : " + zOffset ); } if( distMin == distX ) OccludeX( cx, cy, cz, xOffset, chunk ); if( distMin == distY ) OccludeY( cx, cy, cz, yOffset, chunk ); if( distMin == distZ ) OccludeZ( cx, cy, cz, zOffset, chunk ); //Console.WriteLine( distMin + " , " + distX + " , " + distY + " , " + distZ ); //Console.WriteLine( chunk.DistanceFlags + " : " + chunk.OccludedFlags + " : " + chunk.OcclusionFlags ); QueueChunk( cx - 1, cy, cz, queue ); QueueChunk( cx + 1, cy, cz, queue ); QueueChunk( cx, cy, cz - 1, queue ); QueueChunk( cx, cy, cz + 1, queue ); QueueChunk( cx, cy - 1, cz, queue ); QueueChunk( cx, cy + 1, cz, queue ); if( !chunk.Empty ) { Console.WriteLine( "{0} D {1}: V {2}, O {3}, {4}", cx + "," + cy + "," + cz, chunk.DistanceFlags, chunk.OccludedFlags, chunk.OcclusionFlags, chunk.Occluded ); Console.WriteLine( " M {0} : X {1}, Y {2}, Z {3} ({4}, {5})", distMin, distX, distY, distZ, dxFront, dxBack ); } } Console.WriteLine( "======================" ); }
public void Run() { Chunk currentChunk; while (true) { if (!Variables.Game.IsInitialized) { continue; } if (UnloadQueue.Count > 0) { currentChunk = UnloadQueue.Dequeue(); if (currentChunk == null) { continue; } // Unload Chunk if (Constants.World.SaveDynamicWorld) { ChunkManager.StoreChunkImmediate(currentChunk); } else { currentChunk = null; } Console.Write("UnloadQueue: " + UnloadQueue.Count); // End Unload Chunk LoadQueue.Remove(currentChunk); GenerationQueue.Remove(currentChunk); SetupQueue.Remove(currentChunk); } else if (LoadQueue.Count > 0) { currentChunk = LoadQueue.Dequeue(); if (currentChunk == null) { continue; } // Load Chunk ChunkManager.LoadChunkImmediate(currentChunk); currentChunk.BuildOctree(); currentChunk.HasData = true; Console.Write("LoadQueue: " + LoadQueue.Count); // End Load Chunk if (SetupQueue.Contains(currentChunk)) { SetupQueue.Remove(currentChunk); SetupQueue.Enqueue(currentChunk); } else { SetupQueue.Enqueue(currentChunk); } } else if (GenerationQueue.Count > 0) { currentChunk = GenerationQueue.Dequeue(); if (currentChunk == null) { continue; } // Generate Chunk TerrainGenerator.SetChunkTerrain(currentChunk); Console.Write("GenerationQueue: " + GenerationQueue.Count); // End Generate Chunk if (VegetationQueue.Contains(currentChunk)) { VegetationQueue.Remove(currentChunk); VegetationQueue.Enqueue(currentChunk); } else { AddToVegetation(currentChunk); } } else if (VegetationQueue.Count > 0) { currentChunk = VegetationQueue.Dequeue(); if (currentChunk == null) { continue; } // Vegetate Chunk Vegetation.Vegetate(currentChunk); Console.Write("VegetationQueue: " + VegetationQueue.Count); // End Vegetate Chunk if (BuildQueue.Contains(currentChunk)) { BuildQueue.Remove(currentChunk); BuildQueue.Enqueue(currentChunk); } else { AddToBuild(currentChunk); } if (VegetationQueue.Count == 0 && !Constants.Engines.Physics.Player.IsReleased) { Constants.Engines.Physics.Player.Release(); } } else if (BuildQueue.Count > 0) { currentChunk = BuildQueue.Dequeue(); if (currentChunk == null) { continue; } // Build Chunk currentChunk.BuildOctree(); currentChunk.HasData = true; Console.Write("BuildQueue: " + BuildQueue.Count); // End Build Chunk if (SetupQueue.Contains(currentChunk)) { SetupQueue.Remove(currentChunk); SetupQueue.Enqueue(currentChunk); } else { AddToSetup(currentChunk); } } else if (SetupQueue.Count > 0) { currentChunk = SetupQueue.Dequeue(); if (currentChunk == null) { continue; } currentChunk.SetupState = 4; // Setup Chunk currentChunk.BuildGeometry(false); currentChunk.SetupState = 1; Console.Write("SetupQueue: " + SetupQueue.Count); // End Setup Chunk } } }
void ProcessQueue( ChunkInfo src, ChunkQueue queue ) { Vector3I p = new Vector3I( src.CentreX, src.CentreY, src.CentreZ ); while( queue.Size > 0 ) { ChunkInfo chunk = queue.Dequeue(); chunk.VisibilityFlags = chunk.OcclusionFlags; int x1 = chunk.CentreX - 8, x2 = chunk.CentreX + 8; int y1 = chunk.CentreY - 8, y2 = chunk.CentreY + 8; int z1 = chunk.CentreZ - 8, z2 = chunk.CentreZ + 8; int cx = chunk.CentreX >> 4; int cy = chunk.CentreY >> 4; int cz = chunk.CentreZ >> 4; int xOffset, yOffset, zOffset; int dx = Math.Max( x1 - p.X, Math.Max( 0, p.X - x2 ) ); int dy = Math.Max( y1 - p.Y, Math.Max( 0, p.Y - y2 ) ); int dz = Math.Max( z1 - p.Z, Math.Max( 0, p.Z - z2 ) ); int distX, distY, distZ; // X axis collisions int dxLeft = Math.Abs( x1 - p.X ), dxRight = Math.Abs( x2 - p.X ); if( dxLeft < dxRight ) { distX = dxLeft * dxLeft + dy * dy + dz * dz; xOffset = -1; } else { distX = dxRight * dxRight + dy * dy + dz * dz; xOffset = 1; } // Z axis collisions int dxFront = Math.Abs( z1 - p.Z ), dxBack = Math.Abs( z2 - p.Z ); if( dxFront < dxBack ) { distZ = dx * dx + dy * dy + dxFront * dxFront; zOffset = -1; } else { distZ = dx * dx + dy * dy + dxBack * dxBack; zOffset = 1; } // Y axis collisions int dxBottom = Math.Abs( y1 - p.Y ), dxTop = Math.Abs( y2 - p.Y ); if( dxBottom < dxTop ) { distY = dx * dx + dxBottom * dxBottom + dz * dz; yOffset = -1; } else { distY = dx * dx + dxTop * dxTop + dz * dz; yOffset = 1; } int distMin = Math.Min( distX, Math.Min( distY, distZ ) ); bool occlude = true; byte flags = 0; if( distMin == distX ) OccludeX( cx, cy, cz, xOffset, ref occlude, ref flags ); if( distMin == distZ ) OccludeZ( cx, cy, cz, zOffset, ref occlude, ref flags ); if( distMin == distY ) OccludeY( cx, cy, cz, yOffset, ref occlude, ref flags ); if( occlude ) chunk.Occluded = true; chunk.VisibilityFlags = (byte)( flags | chunk.OcclusionFlags ); QueueChunk( cx - 1, cy, cz, queue ); QueueChunk( cx + 1, cy, cz, queue ); QueueChunk( cx, cy, cz - 1, queue ); QueueChunk( cx, cy, cz + 1, queue ); QueueChunk( cx, cy - 1, cz, queue ); QueueChunk( cx, cy + 1, cz, queue ); } Console.WriteLine( "======================" ); }
public void Run() { Chunk currentChunk; while (true) { if (!Variables.Game.IsInitialized) { continue; } if (UnloadQueue.Count > 0) { currentChunk = UnloadQueue.Dequeue(); if (currentChunk == null) { continue; } // Unload Chunk if (Constants.World.SaveDynamicWorld) { ChunkManager.SaveChunk(currentChunk); } else { currentChunk = null; } Console.Write("UnloadQueue: " + UnloadQueue.Count); // End Unload Chunk LoadQueue.Remove(currentChunk); GenerationQueue.Remove(currentChunk); } else if (LoadQueue.Count > 0) { currentChunk = LoadQueue.Dequeue(); if (currentChunk == null) { continue; } // Load Chunk ChunkManager.LoadChunkImmediate(currentChunk); Console.Write("LoadQueue: " + LoadQueue.Count); // End Load Chunk } else if (GenerationQueue.Count > 0) { currentChunk = GenerationQueue.Dequeue(); if (currentChunk == null) { continue; } // Generate Chunk TerrainGenerator.SetChunkTerrain(currentChunk); Console.Write("GenerationQueue: " + GenerationQueue.Count); // End Generate Chunk if (VegetationQueue.Contains(currentChunk)) { VegetationQueue.Remove(currentChunk); VegetationQueue.Enqueue(currentChunk); } else { AddToVegetation(currentChunk); } } else if (VegetationQueue.Count > 0) { currentChunk = VegetationQueue.Dequeue(); if (currentChunk == null) { continue; } // Vegetate Chunk Vegetation.Vegetate(currentChunk); Console.Write("VegetationQueue: " + VegetationQueue.Count); // End Vegetate Chunk } } }