public void Generate(int _seed, bool _enableCaves, float _amp, float _caveDensity, float _grassOffset) { try { /*seed = _seed; * enableCaves = _enableCaves; * amp = _amp; * caveDensity = _caveDensity; * grassOffset = _grassOffset; * * Vector2Int bottomLeft = new Vector2(location.x * ChunkSizeX, location.z * ChunkSizeZ); * Vector2Int topRight = new Vector2(location.x * ChunkSizeX + ChunkSizeX, location.z * ChunkSizeZ + ChunkSizeZ); * * MazeGen mountainTerrain = new MazeGen(ChunkSizeX / 2, 4, ChunkMeterSizeY / 2, seed, 2, 1); * * NoiseModule = mountainTerrain;*/ //NoisePlane = new LibNoise.Models.Plane(NoiseModule); //SetSurfaceData(bottomLeft, topRight); } catch (Exception e) { SafeDebug.LogError(e.Message + "\nFunction: Generate, Chunk: " + location.ToString(), e); } }
public void GenerateChunks(string threadName, Vector2Int[] columns) { Loom.QueueAsyncTask(threadName, () => { try { Stopwatch watch = new Stopwatch(); watch.Start(); for (int i = 0; i < columns.Length; i++) { _generating = true; Columns[columns[i]].Generate(height); chunksGenerated += height; } watch.Stop(); Debug.Log("Finished generating chunks in: " + watch.Elapsed); if (!renderCompleteCalled) { renderCompleteCalled = true; //if (OnChunksGenerated != null) // Loom.QueueOnMainThread(() => { OnChunksGenerated(columns); }); } _generating = false; } catch (Exception e) { SafeDebug.LogError(string.Format("{0}: {1}\n {2}", e.GetType().ToString(), e.Message, e.StackTrace)); } }); }
private void DoSend(byte[] data, Protocal type = Protocal.Tcp) { //SafeDebug.Log("Sending some shit 1"); if (type == Protocal.Tcp || !udpStarted) { //SafeDebug.Log("Sending some shit 2"); if (tcpClient != null && tcpSocket != null) { //if ((ServerCMD)data[0] != ServerCMD.Ping) // SafeDebug.Log("SENDING: " + (ServerCMD)data[0]); data = BufferUtils.AddLength(data); //SafeDebug.Log("Sending some shit 3"); tcpSocket.Send(data); } else { SafeDebug.LogError("Client TCP socket null!"); } } else { if (udpClient != null) { byte[] udpIdBuff = BitConverter.GetBytes((UInt16)UdpID); byte[] buffer = BufferUtils.Add(udpIdBuff, data); udpClient.Send(buffer, buffer.Length); } else { DoSend(data); } } }
public float[,] Generate(IModule module, int _seed, bool _enableCaves, float _amp, float _caveDensity, float _groundOffset, float _grassOffset) { try { NoiseModule = module; seed = _seed; enableCaves = _enableCaves; amp = _amp; caveDensity = _caveDensity; groundOffset = _groundOffset; grassOffset = _grassOffset; RidgedMultifractal _caves = new RidgedMultifractal(); _caves.Seed = _seed; _caves.Frequency = 0.3; caveModule = _caves; Vector2Int bottomLeft = new Vector2(Location.x * ChunkSizeX, Location.z * ChunkSizeZ); Vector2Int topRight = new Vector2(Location.x * ChunkSizeX + ChunkSizeX, Location.z * ChunkSizeZ + ChunkSizeZ); SetSurfaceData(bottomLeft, topRight); } catch (Exception e) { SafeDebug.LogError(string.Format("{0}\nFunction: Generate\n Chunk: {1}", e.Message, Location.ToString()), e); } return(SurfaceData); }
public float[,] Generate(int _seed, bool _enableCaves, float _amp, float _caveDensity, float _groundOffset, float _grassOffset) { try { seed = _seed; enableCaves = _enableCaves; amp = _amp; caveDensity = _caveDensity; groundOffset = _groundOffset; grassOffset = _grassOffset; Vector2Int bottomLeft = new Vector2(Location.x * ChunkSizeX, Location.z * ChunkSizeZ); Vector2Int topRight = new Vector2(Location.x * ChunkSizeX + ChunkSizeX, Location.z * ChunkSizeZ + ChunkSizeZ); MazeGen mountainTerrain = new MazeGen(ChunkSizeX / 2, 4, ChunkMeterSizeY / 2, seed, 2, 1); NoiseModule = mountainTerrain; //NoisePlane = new LibNoise.Models.Plane(NoiseModule); //SetSurfaceData(bottomLeft, topRight); } catch (Exception e) { SafeDebug.LogError(e.Message + "\nFunction: Generate, Chunk: " + Location.ToString(), e); } return(SurfaceData); }
public void Generate(IModule module, int _seed, bool _enableCaves, float _amp, float _caveDensity, float _grassOffset) { try { Sampler = new TerrainSampler(module, _seed, _enableCaves, _amp, _caveDensity, _grassOffset);; Sampler.SetChunkSettings(VoxelsPerMeter, new Vector3Int(ChunkSizeX, ChunkSizeY, ChunkSizeZ), new Vector3Int(ChunkMeterSizeX, ChunkMeterSizeY, ChunkMeterSizeZ), skipDist, half, new Vector3(xSideLength, ySideLength, zSideLength)); Vector2Int bottomLeft = new Vector2(location.x * ChunkSizeX, location.z * ChunkSizeZ); Vector2Int topRight = new Vector2(location.x * ChunkSizeX + ChunkSizeX, location.z * ChunkSizeZ + ChunkSizeZ); watch.Start(); Sampler.SetSurfaceData(bottomLeft, topRight); watch.Stop(); noiseGenTime = watch.Elapsed.ToString(); int bottom = VoxelConversions.ChunkToVoxel(location).y; empty = bottom > Sampler.GetMax(); } catch (Exception e) { SafeDebug.LogError(string.Format("{0}\nFunction: Generate\n Chunk: {1}", e.Message, location.ToString()), e); } }
public void GenerateChunks(string threadName, int index, IModule module, Vector3Int[] chunks) { Loom.QueueAsyncTask(threadName, () => { try { for (int i = 0; i < chunks.Length; i++) { _generating = true; //SmoothChunk.CreateChunk(chunks[i], module, this); // TODO: use sampler chunksGenerated++; } if (!renderCompleteCalled) { for (int i = 0; i < chunks.Length; i++) { if (Vector3.Distance(new Vector3Int(), chunks[i]) > SmoothVoxelSettings.radius / 2) { renderCompleteCalled = true; Loom.QueueOnMainThread(OnRenderComplete); } } } _generating = false; } catch (Exception e) { SafeDebug.LogError(string.Format("{0}: {1}\n {2}", e.GetType().ToString(), e.Message, e.StackTrace)); } }); }
public static void LogError(object message, params object[] args) { SafeDebug.LogError("[Server]: " + string.Format(message.ToString(), args)); /*string messageStr = string.Format(message.ToString(), args); * AddEntry(LogLevel.Error, string.Format("[{0}]: {1}", GetTime(), messageStr)); * Action a = () => LogToFile(string.Format("[{0} E]: {1}", GetTime(), messageStr)); * QueueLog(a);*/ }
private void SystemCmds(Data data) { byte cmd = data.Buffer[0]; data.Buffer = BufferUtils.RemoveFront(BufferUtils.Remove.CMD, data.Buffer); switch (cmd) { case 0x01: // connected. if (data.Buffer[0] == 0x01) { UdpID = BitConverter.ToUInt16(data.Buffer, 1); StartTcpPing(); if (OnConnectSuccess != null) { OnConnectSuccess(); } } else { if (OnConnectFailed != null) { OnConnectFailed(); } Close(); } break; case 0x02: // udp enabled udpConnected = true; if (OnUdpEnabled != null) { OnUdpEnabled(); } break; case 0x03: // ping. bool pingBack = BitConverter.ToBoolean(data.Buffer, 0); if (pingBack) { Send(0xff, BufferUtils.AddFirst(0x03, BitConverter.GetBytes(false)), data.Type); } break; case 0x04: // server closed. if (OnServerStoped != null) { OnServerStoped(); } Close(); break; default: SafeDebug.LogError("Received invalid system command: " + cmd); break; } }
public void Run() { while (run) { if (actions.Count > 0) { resetEvent.WaitOne(1); } else { resetEvent.WaitOne(10); } try { lock (actions) { _currentActions.Clear(); _currentActions.AddRange(actions); actions.Clear(); } if (_currentActions.Count > 0) { for (int i = 0; i < _currentActions.Count; i++) { try { if (run) { functionRunning = true; FuctionWorkTime.Reset(); FuctionWorkTime.Start(); _currentActions[i](); _currentActions[i] = null; FuctionWorkTime.Stop(); functionRunning = false; } //ConsoleWpr.LogDebug(threadName + ": function Called."); } catch (Exception e) { SafeDebug.LogError("message: " + e.Message + ", thread: " + threadName, e); _currentActions[i] = null; } } } } catch (Exception e) { SafeDebug.LogError("\nMessage: " + e.Message + "\nFunction: Run\nThread: " + threadName); } resetEvent.Reset(); } }
public void EditNextFrame(BlockChange change) { Vector3Int position = change.position; byte type = change.type; if (position.x >= 0 && position.x < VoxelSettings.ChunkSizeX && position.y >= 0 && position.y < VoxelSettings.ChunkSizeY && position.z >= 0 && position.z < VoxelSettings.ChunkSizeZ) { editQueue.Add(new BlockChange(position, type)); } else { SafeDebug.LogError(string.Format("Out of Bounds: chunk: {0}, localVoxel: {1}, Function: EditNextFrame", chunkPosition, position)); } }
public static void QueueMessage(messageType type, string message) { lock (Current._messages) { try { Current._messages.Add(new Message(type, message)); } catch (Exception ex) { SafeDebug.LogError("\nMessage: " + ex.Message + "\nFunction: QueueMessage"); } } }
public byte GetBlockType(int LocalPositionX, int LocalPositionY, int LocalPositionZ, int NoiseLocationX, int NoiseLocationY, int NoiseLocationZ, int globalY) { byte result = 0; try { float _stoneHeight = GetSurfaceHeight(LocalPositionX, LocalPositionZ); float _dirtHeight = _stoneHeight + grassOffset - 1; float _grassHeight = _dirtHeight + 1; // Set surface layers. byte groundType = 0; if (_stoneHeight > NoiseLocationY) { groundType = 2; } else if (_dirtHeight > NoiseLocationY) { groundType = 3; } else if (_grassHeight > NoiseLocationY) { groundType = 1; } // set caves. if (groundType != 0) { if (enableCaves && Noise(caveModule, NoiseLocationX, NoiseLocationY, NoiseLocationZ, 16.0 * VoxelsPerMeter, 17.0, 1.0) > caveDensity) { groundType = 0; } } result = groundType; //if (NoiseLocationY == 0) // result = 1; //else result = 0; } catch (Exception e) { SafeDebug.LogError(string.Format("Message: {0}\nnoiseX={1}, noiseZ={2}\nlocalX={3}/{4}, localZ={5}/{6}", e.Message, NoiseLocationX, NoiseLocationZ, LocalPositionX, SurfaceData.GetLength(0), LocalPositionZ, SurfaceData.GetLength(1)), e); } return(result); }
public double GetIsoValue(Vector3Int LocalPosition, Vector3Int globalLocation, out uint type) { double result = -1; type = 1; try { float distance = Vector3.Distance(LocalPosition, Center); float iso = Radius - distance; /*float iso = -1; * if (LocalPosition.x > 1 && LocalPosition.x < ChunkSizeZ - 1 && * LocalPosition.y > 1 && LocalPosition.y < ChunkSizeZ - 1 && * LocalPosition.z > 1 && LocalPosition.z < ChunkSizeZ - 1) * { * iso = 1; * }*/ //type = (uint)Mathf.RoundToInt(Random.Range(0.6f, 4.4f)); //if (iso > 0) //{ if (LocalPosition.y - 10 > 3) { type = 1; } else if (LocalPosition.y - 10 >= 0) { type = 2; } else { type = 3; } //} //else // type = 0; result = iso; } catch (System.Exception e) { SafeDebug.LogError(string.Format("Message: {0}\nglobalX={1}, globalZ={2}\nlocalX={3}/{4}, localZ={5}/{6}", e.Message, globalLocation.x, globalLocation.z, LocalPosition.x, SurfaceData.GetLength(0), LocalPosition.z, SurfaceData.GetLength(1)), e); type = 0; } return(result); }
public void DestroyChunk(Vector3Int chunk) { try { if (BuilderExists(chunk.x, chunk.y, chunk.z)) { SmoothChunk chunkInst = Chunks[chunk]; chunkInst.Close(); Chunks.Remove(chunk); Loom.QueueOnMainThread(() => Destroy(chunkInst.gameObject)); } } catch (Exception e) { SafeDebug.LogError(string.Format("{0}\n", e.Message), e); } }
private void SetSurfaceData(Vector2Int bottomLeft, Vector2Int topRight) { try { for (int noiseX = bottomLeft.x - 1, x = 0; noiseX < topRight.x + 1; noiseX++, x++) { for (int noiseZ = bottomLeft.y - 1, z = 0; noiseZ < topRight.y + 1; noiseZ++, z++) { SurfaceData[x, z] = GetHeight(noiseX, noiseZ); } } } catch (Exception e) { SafeDebug.LogError(e.Message + "\nFunction: SetSurfaceData", e); } }
public void GenerateChunk(Vector3Int location3D, IModule module) { try { if (BuilderExists(location3D.x, location3D.y, location3D.z) && !Chunks[new Vector3Int(location3D.x, location3D.y, location3D.z)].Generated) { _generating = true; Chunks[new Vector3Int(location3D.x, location3D.y, location3D.z)].GenerateChunk(module); Chunks[new Vector3Int(location3D.x, location3D.y, location3D.z)].Render(false); } } catch (Exception e) { SafeDebug.LogError(e.Message + "\nFunction: GenerateChunks" + "\n" + location3D.x + "," + location3D.y + "," + location3D.y, e); } }
public void GenerateChunk(SmoothChunk chunk) { try { if (chunk != null && !chunk.Generated) { _generating = true; //chunk.Generate(); } } catch (Exception e) { SafeDebug.LogError(e.Message + "\nFunction: GenerateChunks: " + chunk.chunkPosition.x + "," + chunk.chunkPosition.y + "," + chunk.chunkPosition.z); SafeDebug.LogError(e.StackTrace); } }
public void ChangeBlock(Vector3Int chunk, Chunk.BlockChange change) { Vector3Int localVoxel = change.position; if (BuilderExists(chunk.x, chunk.y, chunk.z)) { if (localVoxel.x >= 0 && localVoxel.x < VoxelSettings.ChunkSizeX && localVoxel.y >= 0 && localVoxel.y < VoxelSettings.ChunkSizeY && localVoxel.z >= 0 && localVoxel.z < VoxelSettings.ChunkSizeZ) { Chunks[chunk].EditNextFrame(change); } else { SafeDebug.LogError(string.Format("Out of Bounds: chunk: {0}, localVoxel: {1}, Function: GenerateExplosion", chunk, localVoxel)); } } }
public static void QueueAsyncTask(string thread, Action e) { lock (Current._AsynAction) { try { if (Current._AsynAction.ContainsKey(thread)) { Current._AsynAction[thread].AddAsyncTask(e); } else { SafeDebug.LogError("failed to locate thread " + thread); } } catch (Exception ex) { SafeDebug.LogError("\nMessage: " + ex.Message + "\nFunction: QueueAsyncTask\nThread: " + thread); } } }
public static void AddAsyncThread(string thread) { if (Current._AsynAction != null) { lock (Current._AsynAction) { try { if (!Current._AsynAction.ContainsKey(thread)) { AsyncRunner _runner = new AsyncRunner(thread); Current._AsynAction.Add(thread, _runner); } } catch (Exception e) { SafeDebug.LogError("\nMessage: " + e.Message + "\nFunction: AddAsyncThread\nThread: " + thread, e); } } } }
public void ChangeBlock(Chunk.BlockChange change) { Vector3Int chunk = VoxelConversions.VoxelToChunk(change.position); Vector3Int localVoxel = VoxelConversions.GlobalVoxToLocalChunkVoxCoord(chunk, change.position); //Debug.LogFormat("voxel: {0}, localVoxel: {1}, chunk: {2}", voxel, localVoxel, chunk); if (BuilderExists(chunk.x, chunk.y, chunk.z)) { if (localVoxel.x >= 0 && localVoxel.x < VoxelSettings.ChunkSizeX && localVoxel.y >= 0 && localVoxel.y < VoxelSettings.ChunkSizeY && localVoxel.z >= 0 && localVoxel.z < VoxelSettings.ChunkSizeZ) { Chunks[chunk].EditNextFrame(new Chunk.BlockChange(localVoxel, change.type)); } else { SafeDebug.LogError(string.Format("Out of Bounds: chunk: {0}, globalVoxel:{1}, localVoxel: {2}, Function: GenerateExplosion", chunk, change.position, localVoxel)); } } }
private void Send(byte[] data, Protocal type = Protocal.Tcp) { if ((data.Length + 2) >= 65536) { SafeDebug.LogError(string.Format("Send data length exceeds 65,536: {0} - {1}", data.Length + 2, BitConverter.ToString(data, 0, 4))); return; } if (Thread.CurrentThread == mainThread) { Loom.QueueAsyncTask("Net", () => { DoSend(data, type); }); } else { DoSend(data, type); } }
private void Setpage(int index, Vector3Int pageLoc) { if (controller.BuilderExists(pageLoc.x, pageLoc.y, pageLoc.z)) { IVoxelBuilder _builder = controller.GetBuilder(pageLoc.x, pageLoc.y, pageLoc.z); if (_builder != null) { neighbors[index] = new Neighbor(true, controller.BuilderGenerated(pageLoc.x, pageLoc.y, pageLoc.z), pageLoc, _builder); } else { SafeDebug.LogError("Controller says chunk builder exists, but chunk builder is null!."); neighbors[index] = new Neighbor(false, false, pageLoc, null); } } else { neighbors[index] = new Neighbor(false, false, pageLoc, null); } }
public void ChangeBlock(Vector3Int[] voxels, byte type) { foreach (Vector3Int block in voxels) { Vector3Int chunk = VoxelConversions.VoxelToChunk(block); Vector3Int localVoxel = VoxelConversions.GlobalToLocalChunkCoord(chunk, block); //Debug.LogFormat("voxel: {0}, localVoxel: {1}, chunk: {2}", voxel, localVoxel, chunk); if (BuilderExists(chunk.x, chunk.y, chunk.z)) { if (localVoxel.x >= 0 && localVoxel.x < SmoothVoxelSettings.ChunkSizeX && localVoxel.y >= 0 && localVoxel.y < SmoothVoxelSettings.ChunkSizeY && localVoxel.z >= 0 && localVoxel.z < SmoothVoxelSettings.ChunkSizeZ) { Chunks[chunk].EditNextFrame(new SmoothChunk.BlockChange(localVoxel, type)); } else { SafeDebug.LogError(string.Format("Out of Bounds: chunk: {0}, globalVoxel:{1}, localVoxel: {2}, Function: GenerateExplosion", chunk, block, localVoxel)); } } } }
public void AddCommands(object target) { MethodInfo[] methods = target.GetType().GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static); for (int i = 0; i < methods.Length; i++) { try { ClientCommand cmdAttribute = (ClientCommand)Attribute.GetCustomAttribute(methods[i], typeof(ClientCommand)); if (cmdAttribute != null) { CMD function = null; if (methods[i].IsStatic) { function = (CMD)Delegate.CreateDelegate(typeof(CMD), methods[i], true); } else { function = (CMD)Delegate.CreateDelegate(typeof(CMD), target, methods[i], true); } if (function != null) { commands[cmdAttribute.byteCommand] = function; } else { SafeDebug.LogError("Failed to add main server network function: " + methods[i].DeclaringType.Name + "." + methods[i].Name); } } } catch (Exception e) { if (methods[i] != null) { SafeDebug.LogError("Error adding main server network function: " + methods[i].DeclaringType.Name + "." + methods[i].Name); SafeDebug.LogError(string.Format("{0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace)); } } } }
public void GenerateChunks(Vector2Int[] columns) { try { Stopwatch watch = new Stopwatch(); watch.Start(); for (int i = 0; i < columns.Length; i++) { _generating = true; if (!Columns.ContainsKey(columns[i])) { GameObject obj = Instantiate(ColumnObj, VoxelConversions.ChunkCoordToWorld(new Vector3Int(columns[i].x, 0, columns[i].y)), Quaternion.identity, transform); obj.name = "Column " + columns[i].ToString(); Columns[columns[i]] = obj.GetComponent <ChunkColumn>(); Columns[columns[i]].Init(columns[i], this); gameClient.RequestColumn(columns[i], LOD_Mode.ReducedDepth, false); } Columns[columns[i]].Generate(height); chunksGenerated += height; } watch.Stop(); Debug.Log("Finished generating chunks in: " + watch.Elapsed); if (!renderCompleteCalled) { renderCompleteCalled = true; //if (OnChunksGenerated != null) // Loom.QueueOnMainThread(() => { OnChunksGenerated(columns); }); } _generating = false; } catch (Exception e) { SafeDebug.LogError(string.Format("{0}: {1}\n {2}", e.GetType().ToString(), e.Message, e.StackTrace)); } }
private Vector3Int[] Trace(Dictionary <Vector3Int, Vector3Int> cameFrom, Vector3Int goal, out bool pathBrocken) { Vector3Int current = goal; pathBrocken = false; List <Vector3Int> path = new List <Vector3Int>(); try { for (int i = 0; i < 10000; i++) { if (cameFrom.ContainsKey(current)) { current = cameFrom[current]; if (!path.Contains(current)) { path.Add(current); } if (current == start) { break; } } else { SafeDebug.Log("Path brocken"); pathBrocken = true; break; } } path.Reverse(); } catch (Exception e) { SafeDebug.LogException(e); SafeDebug.LogError("length: " + path.Count); throw new Exception(":("); } return(path.ToArray()); }
public double GetValue(double x, double y, double z) { if (Voxels != null) { try { if (IsInBounds((int)x, (int)y, (int)z)) { return(Voxels[(int)x, (int)z, (int)z] ? 1 : 0); } else { return(0); } } catch (Exception e) { SafeDebug.LogError(string.Format("Message: {0}, \nfunction: GetValue, \nStacktrace: {1}, \nValues: x={2}/{3}, z={4}/{5}.", e.Message, e.StackTrace, x, Voxels.GetLength(0), z, Voxels.GetLength(1)), e); } } return(0); }
public byte GetBlockType(Vector3Int LocalPosition, Vector3Int NoiseLocation, Vector3Int globalLocation, bool generate) { try { if (generate && !IsBlockSet(LocalPosition.x, LocalPosition.y, LocalPosition.z)) { byte generatedBlock = GetBlockType(LocalPosition.x, LocalPosition.y, LocalPosition.z, NoiseLocation.x, NoiseLocation.y, NoiseLocation.z, globalLocation.y); SetBlock(LocalPosition.x, LocalPosition.y, LocalPosition.z, new Block(generatedBlock)); MarkAsSet(LocalPosition.x, LocalPosition.y, LocalPosition.z); } return(GetBlock(LocalPosition.x, LocalPosition.y, LocalPosition.z).type); } catch (Exception e) { if (SurfaceData != null) { SafeDebug.LogError(string.Format("Message: {0}\nnoiseX={1}, noiseZ={2}\nGenerate: {3}", e.Message, NoiseLocation.x, NoiseLocation.z, generate.ToString()), e); } else { SafeDebug.LogError(string.Format("Message: {0}\nnoiseX={1}, noiseZ={2}\nGenerate: {3}", e.Message, NoiseLocation.x, NoiseLocation.z, generate.ToString()), e); } return(0); } }