// Block Breaking mechanic public void BreakBlock() { if (!current.active) { return; } ChunkPos toUpdate = new ChunkPos(current.chunkX, current.chunkZ); ushort blockCode = loader.chunks[toUpdate].data.GetCell(current.blockX, current.blockY, current.blockZ); ushort state = loader.chunks[toUpdate].metadata.GetState(current.blockX, current.blockY, current.blockZ); ushort hp = loader.chunks[toUpdate].metadata.GetHP(current.blockX, current.blockY, current.blockZ); NetMessage message = new NetMessage(NetCode.BLOCKDAMAGE); message.BlockDamage(current.GetChunkPos(), current.blockX, current.blockY, current.blockZ, this.blockDamage, false); this.loader.client.Send(message.GetMessage(), message.size); }
// Receives a block damage from client and processes block HP private void BlockDamage(byte[] data, ulong id) { // Loaded from message ChunkPos pos; int x, y, z; ushort blockDamage; Chunk c; // Auxiliary int currentHP, actualDamage; ushort block; int decalBefore, decalAfter; bool isBlock; NetMessage message; pos = NetDecoder.ReadChunkPos(data, 1); x = NetDecoder.ReadInt(data, 9); y = NetDecoder.ReadInt(data, 13); z = NetDecoder.ReadInt(data, 17); blockDamage = NetDecoder.ReadUshort(data, 21); if (this.cl.chunks.ContainsKey(pos)) { c = this.cl.chunks[pos]; block = c.data.GetCell(x, y, z); currentHP = c.metadata.GetHP(x, y, z); if (block <= ushort.MaxValue / 2) { isBlock = true; } else { isBlock = false; } if (currentHP == 0 || currentHP == ushort.MaxValue) { if (isBlock) { currentHP = BlockEncyclopediaECS.blockHP[block]; } else { currentHP = BlockEncyclopediaECS.objectHP[ushort.MaxValue - block]; } } actualDamage = this.cl.blockBook.GetDamageReceived(block, blockDamage); if (actualDamage == 0) { return; } decalBefore = GetDecalCode(block, currentHP); currentHP -= actualDamage; decalAfter = GetDecalCode(block, currentHP); // If block has broken if (currentHP <= 0) { this.cacheBreakData[0] = 0; NetDecoder.WriteChunkPos(pos, this.cacheBreakData, 1); NetDecoder.WriteInt(x, this.cacheBreakData, 9); NetDecoder.WriteInt(y, this.cacheBreakData, 13); NetDecoder.WriteInt(z, this.cacheBreakData, 17); NetDecoder.WriteInt(0, this.cacheBreakData, 21); NetDecoder.WriteUshort(block, this.cacheBreakData, 25); NetDecoder.WriteInt((int)BUDCode.BREAK, this.cacheBreakData, 27); this.DirectBlockUpdate(this.cacheBreakData, id, comesFromMessage: false); } // If damage wasn't significant enough for a decal change else { c.metadata.SetHP(x, y, z, (ushort)currentHP); message = new NetMessage(NetCode.BLOCKDAMAGE); message.BlockDamage(pos, x, y, z, (ushort)currentHP, decalBefore != decalAfter); SendToClients(pos, message); this.cl.regionHandler.SaveChunk(c); } } }