private void SendArea(Player player, Vint3 area) { if (!history.ContainsKey(area)) { return; } int time; if (!player.histroy.TryGetValue(area, out time)) { player.histroy.Add(area, 0); } var steps = history[area]; ms.Position = 0; for (int i = 0; i < steps.Count; i++) { var step = steps[i]; if (step.time > time) { foreach (var change in step.changes) { ChunkManager.Instance.SaveChunk(change, writer); //if (buffer[ms.Position - 2] == 0) //{ // Debug.LogError("Voxel error: " + change); //} //Debug.Log("Voxel written: " + change + " last 6 bytes:" + buffer[ms.Position - 6] + ", " + // buffer[ms.Position - 5] + ", " + // buffer[ms.Position - 4] + ", " + // buffer[ms.Position - 3] + ", " + // buffer[ms.Position - 2] + ", " + // buffer[ms.Position - 1]); if (ms.Position > buffer.Length - 1040) { Flush(player); } } } } player.histroy[area] = steps[steps.Count - 1].time; Flush(player); }
public byte this[Vint3 pos] { get { //return data[x, y, z]; var logic = pos/ ChunkManager.Size; if (logic == Vint3.Zero) { return data[pos.x, pos.y, pos.z]; } else { return ChunkManager.Instance.GetSingleVoxel(iPos* ChunkManager.Size + pos); } } }
// Use this for initialization public void Initialize(Vector3 position, Vint3 intPos) { iPos = intPos; data = new byte[ChunkManager.Size, ChunkManager.Size, ChunkManager.Size]; if (!WebManager.IsServer) { mesh = new Mesh(); var filter = gameObject.AddComponent<MeshFilter>(); filter.mesh = mesh; collider = gameObject.AddComponent<MeshCollider>(); collider.sharedMesh = mesh; } transform.localPosition = position; bounds = new Bounds(position + new Vector3(ChunkManager.Size, ChunkManager.Size, ChunkManager.Size)*0.5f, new Vector3(ChunkManager.Size, ChunkManager.Size, ChunkManager.Size)); }
private void PlayerMovedTo(Player player) { var pos = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); player.position = pos; var area = new Vint3(pos) / 64; if (player.area != area) { Debug.Log("Player " + player + " moved to " + area); player.area = area; for (int i = 0; i < Vint3.Offset.Length; i++) { var a = area + Vint3.Offset[i]; SendArea(player, a); } } }
public byte GetSingleVoxel(Vint3 pos) { var logic = pos/Size; VoxelChunk chunk; if (debugCounter == 0 && chunks.TryGetValue(logic, out chunk)) { debugCounter++; var found = chunk[pos - logic*Size]; debugCounter = 0; return found; } debugCounter = 0; return 0; }
public Vint3 LoadChunk(BinaryReader reader) { var v = new Vint3(reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32()); int hash = reader.ReadInt32(); if (hash != v.GetHashCode()) { Debug.LogError("Chunk at " + v + " is malformed!"); var ms = (MemoryStream)reader.BaseStream; //StringBuilder builder = new StringBuilder(); //builder.AppendLine("Data:"); //var array = WebClient.buffer; //for (int i = 0; i < array.Length; i++) //{ // builder.AppendLine(i.ToString("0000") + " " + array[i] + (i == ms.Position ? " <---" : "")); //} //Debug.Log(builder.ToString()); ms.Position = ms.Length; return v; } Debug.Log("Receiving chunk " + v); if (!chunks.ContainsKey(v)) { CreateChunk(v); } chunks[v].LoadChunk(reader); for (int i = 0; i < offset.GetLength(0); i++) { var vo = v + new Vint3(offset[i, 0], offset[i, 1], offset[i, 2]); update.Add(vo); } return v; }
public void SaveChunk(Vint3 pos, BinaryWriter writer) { var chunk = chunks[pos]; SaveChunk(chunk, writer); }
private VoxelChunk CreateChunk(Vint3 intPos) { if (intPos.x > int.MaxValue - 100 || intPos.x < int.MinValue + 100 || intPos.y > int.MaxValue - 100 || intPos.y < int.MinValue + 100 || intPos.z > int.MaxValue - 100 || intPos.z < int.MinValue + 100) { Debug.LogError("Attempted to create chunk close to border: " + intPos); return null; } var go = new GameObject("Chunk" + intPos); var chunk = go.AddComponent<VoxelChunk>(); chunk.Initialize(intPos.Vector*Size, intPos); if (!WebManager.IsServer) { var rend = go.AddComponent<MeshRenderer>(); rend.sharedMaterial = material; } go.transform.SetParent(transform, false); chunks.Add(intPos, chunk); areas.Add(intPos*Size/64); return chunk; }
public void Draw(Vector3 position, float radius, byte color) { position = transform.InverseTransformPoint(position); var pos = new Vint3(position) / Size; var bounds = new Bounds(position, new Vector3(radius*2, radius*2, radius*2)); for (int i = 0; i < Vint3.Offset.Length; i++) { var v = pos + Vint3.Offset[i]; if (!chunks.ContainsKey(v)) { CreateChunk(v); } if (chunks[v].bounds.Intersects(bounds)) { chunks[v].Draw(position, radius, color); update.Add(v); if (WebManager.IsConnected && !painted.Contains(v)) { painted.Enqueue(v); } } } }
private void SendArea(Player player, Vint3 area) { if (!history.ContainsKey(area)) { return; } int time; if (!player.histroy.TryGetValue(area, out time)) { player.histroy.Add(area, 0); } var steps = history[area]; ms.Position = 0; for (int i = 0; i < steps.Count; i++) { var step = steps[i]; if (step.time > time) { foreach (var change in step.changes) { ChunkManager.Instance.SaveChunk(change, writer); //if (buffer[ms.Position - 2] == 0) //{ // Debug.LogError("Voxel error: " + change); //} //Debug.Log("Voxel written: " + change + " last 6 bytes:" + buffer[ms.Position - 6] + ", " + // buffer[ms.Position - 5] + ", " + // buffer[ms.Position - 4] + ", " + // buffer[ms.Position - 3] + ", " + // buffer[ms.Position - 2] + ", " + // buffer[ms.Position - 1]); if (ms.Position > buffer.Length - 1040) { Flush(player); } } } } player.histroy[area] = steps[steps.Count - 1].time; Flush(player); }
private void PlayerMovedTo(Player player) { var pos = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); player.position = pos; var area = new Vint3(pos) / 64; if (player.area != area) { Debug.Log("Player " + player + " moved to " + area); player.area = area; for (int i = 0; i < Vint3.Offset.Length; i++) { var a = area + Vint3.Offset[i]; SendArea(player, a); } } }