From e69498af482047bd08a6cce36b8a15341a6f0df1 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Mon, 3 Oct 2016 18:18:53 +1100 Subject: [PATCH] Core: Optimise pixel, space, rainbow map types to be 6-7 times faster. --- MCGalaxy/Database/IDatabaseBackend.cs | 2 +- MCGalaxy/Generator/SimpleGen.cs | 112 +++++++++++++------------- 2 files changed, 59 insertions(+), 55 deletions(-) diff --git a/MCGalaxy/Database/IDatabaseBackend.cs b/MCGalaxy/Database/IDatabaseBackend.cs index 9c0205d95..64e2f4c61 100644 --- a/MCGalaxy/Database/IDatabaseBackend.cs +++ b/MCGalaxy/Database/IDatabaseBackend.cs @@ -34,7 +34,7 @@ namespace MCGalaxy.SQL { public abstract BulkTransaction CreateBulk(); /// Returns a new ParameterisedQuery instance, which executes sql statements - /// and manages binding of parameteries for sql queries. + /// and manages binding of parameters for sql queries. public abstract ParameterisedQuery CreateParameterised(); /// Returns the shared static ParamterisedQuery instance, that is only used diff --git a/MCGalaxy/Generator/SimpleGen.cs b/MCGalaxy/Generator/SimpleGen.cs index 703af8b47..2f1a6d266 100644 --- a/MCGalaxy/Generator/SimpleGen.cs +++ b/MCGalaxy/Generator/SimpleGen.cs @@ -51,77 +51,60 @@ namespace MCGalaxy.Generator { } return true; } - - unsafe static void MapSet(int width, int length, byte* ptr, int yStart, int yEnd, byte block) { - int start = yStart * length * width; - int end = (yEnd * length + (length - 1)) * width + (width - 1); - Utils.memset((IntPtr)ptr, block, start, end - start + 1); + + static bool GenEmpty(MapGenArgs args) { + int maxX = args.Level.Width - 1, maxZ = args.Level.Length - 1; + Cuboid(args, 0, 0, 0, maxX, 0, maxZ, () => Block.blackrock); + return true; } static bool GenPixel(MapGenArgs args) { - int width = args.Level.Width, height = args.Level.Height, length = args.Level.Length; - int index = 0; - byte[] blocks = args.Level.blocks; + int maxX = args.Level.Width - 1, maxY = args.Level.Height - 1, maxZ = args.Level.Length - 1; + Func block = () => Block.white; - for (int y = 0; y < height; ++y) - for (int z = 0; z < length; ++z) - for (int x = 0; x < width; ++x) - { - if (y == 0) - blocks[index] = Block.blackrock; - else if (x == 0 || x == width - 1 || z == 0 || z == length - 1) - blocks[index] = Block.white; - index++; - } - return true; - } - - static bool GenEmpty(MapGenArgs args) { - int width = args.Level.Width, length = args.Level.Length; - int index = 0; - byte[] blocks = args.Level.blocks; + // Cuboid the four walls + Cuboid(args, 0, 1, 0, maxX, maxY, 0, block); + Cuboid(args, 0, 1, maxZ, maxX, maxY, maxZ, block); + Cuboid(args, 0, 1, 0, 0, maxY, maxZ, block); + Cuboid(args, maxX, 1, 0, maxX, maxY, maxZ, block); - for (int z = 0; z < length; ++z) - for (int x = 0; x < width; ++x) - { - blocks[index++] = Block.blackrock; - } + // Cuboid base + Cuboid(args, 0, 0, 0, maxX, 0, maxZ, () => Block.blackrock); return true; } static bool GenSpace(MapGenArgs args) { + int maxX = args.Level.Width - 1, maxY = args.Level.Height - 1, maxZ = args.Level.Length - 1; Random rand = args.UseSeed ? new Random(args.Seed) : new Random(); - int width = args.Level.Width, height = args.Level.Height, length = args.Level.Length; - int index = 0; - byte[] blocks = args.Level.blocks; + Func block = () => rand.Next(100) == 0 ? Block.iron : Block.obsidian; + + // Cuboid the four walls + Cuboid(args, 0, 2, 0, maxX, maxY, 0, block); + Cuboid(args, 0, 2, maxZ, maxX, maxY, maxZ, block); + Cuboid(args, 0, 2, 0, 0, maxY, maxZ, block); + Cuboid(args, maxX, 2, 0, maxX, maxY, maxZ, block); - for (int y = 0; y < height; ++y) - for (int z = 0; z < length; ++z) - for (int x = 0; x < width; ++x) - { - if (y == 0) - blocks[index] = Block.blackrock; - else if (x == 0 || x == width - 1 || z == 0 || z == length - 1 || y == 1 || y == height - 1) - blocks[index] = rand.Next(100) == 0 ? Block.iron : Block.obsidian; - index++; - } + // Cuboid base and top + Cuboid(args, 0, 0, 0, maxX, 0, maxZ, () => Block.blackrock); + Cuboid(args, 0, 1, 0, maxX, 1, maxZ, block); + Cuboid(args, 0, maxY, 0, maxX, maxY, maxZ, block); return true; } static bool GenRainbow(MapGenArgs args) { + int maxX = args.Level.Width - 1, maxY = args.Level.Height - 1, maxZ = args.Level.Length - 1; Random rand = args.UseSeed ? new Random(args.Seed) : new Random(); - int width = args.Level.Width, height = args.Level.Height, length = args.Level.Length; - int index = 0; - byte[] blocks = args.Level.blocks; + Func block = () => (byte)rand.Next(Block.red, Block.white); + + // Cuboid the four walls + Cuboid(args, 0, 1, 0, maxX, maxY, 0, block); + Cuboid(args, 0, 1, maxZ, maxX, maxY, maxZ, block); + Cuboid(args, 0, 1, 0, 0, maxY, maxZ, block); + Cuboid(args, maxX, 1, 0, maxX, maxY, maxZ, block); - for (int y = 0; y < height; ++y) - for (int z = 0; z < length; ++z) - for (int x = 0; x < width; ++x) - { - if (y == 0 || y == height - 1 || x == 0 || x == width - 1 || z == 0 || z == length - 1) - blocks[index] = (byte)rand.Next(Block.red, Block.white); - index++; - } + // Cuboid base and top + Cuboid(args, 0, 0, 0, maxX, 0, maxZ, block); + Cuboid(args, 0, maxY, 0, maxX, maxY, maxZ, block); return true; } @@ -153,8 +136,29 @@ namespace MCGalaxy.Generator { return GenSimple(args); } + static bool GenSimple(MapGenArgs args) { return new RealisticMapGen().GenerateMap(args); } + + unsafe static void MapSet(int width, int length, byte* ptr, + int yStart, int yEnd, byte block) { + int start = (yStart * length) * width; + int end = (yEnd * length + (length - 1)) * width + (width - 1); + Utils.memset((IntPtr)ptr, block, start, end - start + 1); + } + + static void Cuboid(MapGenArgs args, int minX, int minY, int minZ, + int maxX, int maxY, int maxZ, Func block) { + int width = args.Level.Width, height = args.Level.Height, length = args.Level.Length; + byte[] blocks = args.Level.blocks; + + for (int y = minY; y <= maxY; y++) + for (int z = minZ; z <= maxZ; z++) + for (int x = minX; x <= maxX; x++) + { + blocks[x + width * (z + y * length)] = block(); + } + } } }