From d9b2679608c39d040928396d33bfb7e77b39fddf Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 3 Mar 2018 09:36:00 +1100 Subject: [PATCH] minor code cleanup --- MCGalaxy/Commands/World/CmdFixGrass.cs | 45 +++++-------------- MCGalaxy/Commands/World/CmdResizeLvl.cs | 4 +- MCGalaxy/Drawing/DrawOps/DrawOpPerformer.cs | 4 +- .../Drawing/DrawOps/RestoreSelectionDrawOp.cs | 13 ++---- MCGalaxy/Levels/IO/MapFormats.cs | 2 +- MCGalaxy/Levels/Level.Blocks.cs | 28 +++++++++--- MCGalaxy/Levels/Level.Physics.cs | 2 +- 7 files changed, 42 insertions(+), 56 deletions(-) diff --git a/MCGalaxy/Commands/World/CmdFixGrass.cs b/MCGalaxy/Commands/World/CmdFixGrass.cs index 3448ce453..d03ba21b3 100644 --- a/MCGalaxy/Commands/World/CmdFixGrass.cs +++ b/MCGalaxy/Commands/World/CmdFixGrass.cs @@ -45,7 +45,7 @@ namespace MCGalaxy.Commands.World { Help(p); return; } - Player.Message(p, "Fixed " + totalFixed + " blocks."); + Player.Message(p, "Fixed " + totalFixed + " blocks"); } static void Fix(Player p, Level lvl, ref int totalFixed, bool fixGrass, bool fixDirt) { @@ -57,29 +57,19 @@ namespace MCGalaxy.Commands.World { for (ushort z = 0; z < lvl.Length; z++) for (ushort x = 0; x < lvl.Width; x++) { - block = lvl.blocks[index]; - if (block == Block.custom_block) { - block = (ushort)(Block.Extended | lvl.GetExtTileNoCheck(x, y, z)); - } - + block = lvl.FastGetBlock(index); if (fixGrass && lvl.Props[block].GrassBlock != Block.Invalid) { - above = y == maxY ? Block.Air : lvl.blocks[index + oneY]; - if (above == Block.custom_block) { - above = (ushort)(Block.Extended | lvl.GetExtTileNoCheck(x, (ushort)(y + 1), z)); - } - + above = y == maxY ? Block.Air : lvl.FastGetBlock(index + oneY); BlockID grass = lvl.Props[block].GrassBlock; + if (lvl.LightPasses(above) && p.level.DoBlockchange(p, x, y, z, grass) == 2) { buffer.Add(index, grass); totalFixed++; } } else if (fixDirt && lvl.Props[block].DirtBlock != Block.Invalid) { - above = y == maxY ? Block.Air : lvl.blocks[index + oneY]; - if (above == Block.custom_block) { - above= (ushort)(Block.Extended | lvl.GetExtTileNoCheck(x, (ushort)(y + 1), z)); - } - + above = y == maxY ? Block.Air : lvl.FastGetBlock(index + oneY); BlockID dirt = lvl.Props[block].DirtBlock; + if (!lvl.LightPasses(above) && p.level.DoBlockchange(p, x, y, z, dirt) == 2) { buffer.Add(index, dirt); totalFixed++; @@ -91,7 +81,7 @@ namespace MCGalaxy.Commands.World { } static void FixLight(Player p, Level lvl, ref int totalFixed) { - int index = 0; + int index = 0, oneY = lvl.Width * lvl.Length; BufferedBlockSender buffer = new BufferedBlockSender(lvl); BlockID above, block; @@ -99,40 +89,29 @@ namespace MCGalaxy.Commands.World { for (ushort z = 0; z < lvl.Length; z++) for (ushort x = 0; x < lvl.Width; x++) { - block = lvl.blocks[index]; + block = lvl.FastGetBlock(index); bool inShadow = false; - if (block == Block.custom_block) { - block = (ushort)(Block.Extended | lvl.GetExtTileNoCheck(x, y, z)); - } if (lvl.Props[block].GrassBlock != Block.Invalid) { for (int i = 1; i < (lvl.Height - y); i++) { - above = lvl.blocks[index + (lvl.Width * lvl.Length) * i]; - if (above == Block.custom_block) { - above = (ushort)(Block.Extended | lvl.GetExtTileNoCheck(x, (ushort)(y + i), z)); - } - + above = lvl.FastGetBlock(index + oneY * i); if (!lvl.LightPasses(above)) { inShadow = true; break; } } BlockID grass = lvl.Props[block].GrassBlock; if (!inShadow && p.level.DoBlockchange(p, x, y, z, grass) == 2) { - buffer.Add(index, grass); + buffer.Add(lvl.PosToInt(x, y, z), grass); totalFixed++; } } else if (lvl.Props[block].DirtBlock != Block.Invalid) { for (int i = 1; i < (lvl.Height - y); i++) { - above = lvl.blocks[index + (lvl.Width * lvl.Length) * i]; - if (above == Block.custom_block) { - above = (ushort)(Block.Extended | lvl.GetExtTileNoCheck(x, (ushort)(y + i), z)); - } - + above = lvl.FastGetBlock(index + oneY * i); if (!lvl.LightPasses(above)) { inShadow = true; break; } } BlockID dirt = lvl.Props[block].DirtBlock; if (inShadow && p.level.DoBlockchange(p, x, y, z, dirt) == 2) { - buffer.Add(index, dirt); + buffer.Add(lvl.PosToInt(x, y, z), dirt); totalFixed++; } } diff --git a/MCGalaxy/Commands/World/CmdResizeLvl.cs b/MCGalaxy/Commands/World/CmdResizeLvl.cs index ada26df57..6ef69d016 100644 --- a/MCGalaxy/Commands/World/CmdResizeLvl.cs +++ b/MCGalaxy/Commands/World/CmdResizeLvl.cs @@ -71,8 +71,8 @@ namespace MCGalaxy.Commands.World { temp.blocks[x + width * (z + y * length)] = block; if (block != Block.custom_block) continue; - byte extBlock = lvl.GetExtTileNoCheck(x, y, z); - temp.SetExtTileNoCheck(x, y, z, extBlock); + byte extBlock = lvl.FastGetExtTile(x, y, z); + temp.FastSetExtTile(x, y, z, extBlock); } temp.spawnx = lvl.spawnx; temp.spawny = lvl.spawny; temp.spawnz = lvl.spawnz; diff --git a/MCGalaxy/Drawing/DrawOps/DrawOpPerformer.cs b/MCGalaxy/Drawing/DrawOps/DrawOpPerformer.cs index c01169a43..9f3b1a281 100644 --- a/MCGalaxy/Drawing/DrawOps/DrawOpPerformer.cs +++ b/MCGalaxy/Drawing/DrawOps/DrawOpPerformer.cs @@ -169,7 +169,7 @@ namespace MCGalaxy.Drawing.Ops { int index = b.X + lvl.Width * (b.Z + b.Y * lvl.Length); BlockID old = lvl.blocks[index]; - if (old == Block.custom_block) old = (BlockID)(Block.Extended | lvl.GetExtTileNoCheck(b.X, b.Y, b.Z)); + if (old == Block.custom_block) old = (BlockID)(Block.Extended | lvl.FastGetExtTile(b.X, b.Y, b.Z)); if (old == Block.Invalid) return; // Check to make sure the block is actually different and that we can change it @@ -179,7 +179,7 @@ namespace MCGalaxy.Drawing.Ops { lvl.Changed = true; if (b.Block >= Block.Extended) { lvl.blocks[index] = Block.custom_block; - lvl.SetExtTileNoCheck(b.X, b.Y, b.Z, (BlockRaw)b.Block); + lvl.FastSetExtTile(b.X, b.Y, b.Z, (BlockRaw)b.Block); } else { lvl.blocks[index] = (BlockRaw)b.Block; if (old >= Block.Extended) { diff --git a/MCGalaxy/Drawing/DrawOps/RestoreSelectionDrawOp.cs b/MCGalaxy/Drawing/DrawOps/RestoreSelectionDrawOp.cs index ed81295af..a96da1f0b 100644 --- a/MCGalaxy/Drawing/DrawOps/RestoreSelectionDrawOp.cs +++ b/MCGalaxy/Drawing/DrawOps/RestoreSelectionDrawOp.cs @@ -38,23 +38,16 @@ namespace MCGalaxy.Drawing.Ops { public Level Source; public override void Perform(Vec3S32[] marks, Brush brush, DrawOpOutput output) { - Max.X = Math.Min(Max.X, Source.Width - 1); + Max.X = Math.Min(Max.X, Source.Width - 1); Max.Y = Math.Min(Max.Y, Source.Height - 1); Max.Z = Math.Min(Max.Z, Source.Length - 1); - Vec3U16 p1 = Clamp(Min), p2 = Clamp(Max); - int width = Source.Width, length = Source.Length; - byte[] blocks = Source.blocks; - + Vec3U16 p1 = Clamp(Min), p2 = Clamp(Max); for (ushort y = p1.Y; y <= p2.Y; y++) for (ushort z = p1.Z; z <= p2.Z; z++) for (ushort x = p1.X; x <= p2.X; x++) { - BlockID block = blocks[x + width * (z + y * length)]; - if (block == Block.custom_block) { - block = (BlockID)(Block.Extended | Source.GetExtTileNoCheck(x, y, z)); - } - output(Place(x, y, z, block)); + output(Place(x, y, z, Source.FastGetBlock(x, y, z))); } Source.Dispose(); diff --git a/MCGalaxy/Levels/IO/MapFormats.cs b/MCGalaxy/Levels/IO/MapFormats.cs index 8620f7fde..7767798f7 100644 --- a/MCGalaxy/Levels/IO/MapFormats.cs +++ b/MCGalaxy/Levels/IO/MapFormats.cs @@ -62,7 +62,7 @@ namespace MCGalaxy.Levels.IO { blocks[i] = Block.custom_block; lvl.IntToPos(i, out x, out y, out z); - lvl.SetExtTileNoCheck(x, y, z, raw); + lvl.FastSetExtTile(x, y, z, raw); } } } diff --git a/MCGalaxy/Levels/Level.Blocks.cs b/MCGalaxy/Levels/Level.Blocks.cs index f2044525c..dd2f4f818 100644 --- a/MCGalaxy/Levels/Level.Blocks.cs +++ b/MCGalaxy/Levels/Level.Blocks.cs @@ -42,12 +42,26 @@ namespace MCGalaxy { } } + /// Gets the block at the given coordinates. + /// Undefined behaviour if coordinates are invalid. + public BlockID FastGetBlock(int index) { + byte raw = blocks[index]; + return raw != Block.custom_block ? raw : (BlockID)(Block.Extended | GetExtTile(index)); + } + + /// Gets the block at the given coordinates. + /// Undefined behaviour if coordinates are invalid. + public BlockID FastGetBlock(ushort x, ushort y, ushort z) { + byte raw = blocks[x + Width * (z + y * Length)]; + return raw != Block.custom_block ? raw : (BlockID)(Block.Extended | FastGetExtTile(x, y, z)); + } + /// Gets the block at the given coordinates. /// Block.Invalid if coordinates outside map. public BlockID GetBlock(ushort x, ushort y, ushort z) { if (x >= Width || y >= Height || z >= Length || blocks == null) return Block.Invalid; byte raw = blocks[x + Width * (z + y * Length)]; - return raw != Block.custom_block ? raw : (BlockID)(Block.Extended | GetExtTileNoCheck(x, y, z)); + return raw != Block.custom_block ? raw : (BlockID)(Block.Extended | FastGetExtTile(x, y, z)); } /// Gets the block at the given coordinates. @@ -56,7 +70,7 @@ namespace MCGalaxy { if (x >= Width || y >= Height || z >= Length || blocks == null) { index = -1; return Block.Invalid; } index = x + Width * (z + y * Length); byte raw = blocks[index]; - return raw != Block.custom_block ? raw : (BlockID)(Block.Extended | GetExtTileNoCheck(x, y, z)); + return raw != Block.custom_block ? raw : (BlockID)(Block.Extended | FastGetExtTile(x, y, z)); } /// Gets whether the block at the given coordinates is air. @@ -81,7 +95,7 @@ namespace MCGalaxy { return chunk == null ? Block.Air : chunk[(y & 0x0F) << 8 | (z & 0x0F) << 4 | (x & 0x0F)]; } - public byte GetExtTileNoCheck(ushort x, ushort y, ushort z) { + public byte FastGetExtTile(ushort x, ushort y, ushort z) { int cx = x >> 4, cy = y >> 4, cz = z >> 4; byte[] chunk = CustomBlocks[(cy * ChunksZ + cz) * ChunksX + cx]; return chunk == null ? Block.Air : chunk[(y & 0x0F) << 8 | (z & 0x0F) << 4 | (x & 0x0F)]; @@ -115,10 +129,10 @@ namespace MCGalaxy { public void SetExtTile(ushort x, ushort y, ushort z, byte extBlock) { int index = PosToInt(x, y, z); if (index < 0 || blocks == null) return; - SetExtTileNoCheck(x, y, z, extBlock); + FastSetExtTile(x, y, z, extBlock); } - public void SetExtTileNoCheck(ushort x, ushort y, ushort z, byte extBlock) { + public void FastSetExtTile(ushort x, ushort y, ushort z, byte extBlock) { int cx = x >> 4, cy = y >> 4, cz = z >> 4; int cIndex = (cy * ChunksZ + cz) * ChunksX + cx; byte[] chunk = CustomBlocks[cIndex]; @@ -249,7 +263,7 @@ namespace MCGalaxy { errorLocation = "Setting tile"; if (block >= Block.Extended) { SetTile(x, y, z, Block.custom_block); - SetExtTileNoCheck(x, y, z, (BlockRaw)block); + FastSetExtTile(x, y, z, (BlockRaw)block); } else { SetTile(x, y, z, (BlockRaw)block); if (old >= Block.Extended) { @@ -337,7 +351,7 @@ namespace MCGalaxy { blocks[b] = Block.custom_block; ushort x, y, z; IntToPos(b, out x, out y, out z); - SetExtTileNoCheck(x, y, z, (BlockRaw)block); + FastSetExtTile(x, y, z, (BlockRaw)block); } else { blocks[b] = (BlockRaw)block; if (old >= Block.Extended) { diff --git a/MCGalaxy/Levels/Level.Physics.cs b/MCGalaxy/Levels/Level.Physics.cs index 07a181544..968aff02d 100644 --- a/MCGalaxy/Levels/Level.Physics.cs +++ b/MCGalaxy/Levels/Level.Physics.cs @@ -153,7 +153,7 @@ namespace MCGalaxy { C.Block = blocks[chk.Index]; if (C.Block == Block.custom_block) { - C.Block = (BlockID)(Block.Extended | GetExtTileNoCheck(C.X, C.Y, C.Z)); + C.Block = (BlockID)(Block.Extended | FastGetExtTile(C.X, C.Y, C.Z)); } if ((C.Data.Raw & mask) == 0 || C.Data.Type1 == PhysicsArgs.Custom || extraHandler(this, ref C)) {