From 6858738d4dd6fc1e233e07b1dc3379d3dd1b0b8b Mon Sep 17 00:00:00 2001 From: asiekierka Date: Fri, 12 Dec 2014 21:31:51 +0100 Subject: [PATCH] optimize zone planners, fix #2243 --- common/buildcraft/BuildCraftBuilders.java | 3 ++ .../builders/HeuristicBlockDetection.java | 2 +- .../builders/schematics/SchematicAir.java | 13 +++++ .../commander/ContainerZonePlan.java | 52 ++++++------------- common/buildcraft/commander/TileZonePlan.java | 18 ++++--- .../core/blueprints/SchematicRegistry.java | 2 +- 6 files changed, 46 insertions(+), 44 deletions(-) create mode 100644 common/buildcraft/builders/schematics/SchematicAir.java diff --git a/common/buildcraft/BuildCraftBuilders.java b/common/buildcraft/BuildCraftBuilders.java index 60fece71..9ca50eed 100644 --- a/common/buildcraft/BuildCraftBuilders.java +++ b/common/buildcraft/BuildCraftBuilders.java @@ -76,6 +76,7 @@ import buildcraft.builders.TileFiller; import buildcraft.builders.TileMarker; import buildcraft.builders.TilePathMarker; import buildcraft.builders.blueprints.BlueprintDatabase; +import buildcraft.builders.schematics.SchematicAir; import buildcraft.builders.schematics.SchematicBed; import buildcraft.builders.schematics.SchematicBlockCreative; import buildcraft.builders.schematics.SchematicCactus; @@ -280,6 +281,8 @@ public class BuildCraftBuilders extends BuildCraftMod { // Standard blocks ISchematicRegistry schemes = BuilderAPI.schematicRegistry; + schemes.registerSchematicBlock(Blocks.air, SchematicAir.class); + schemes.registerSchematicBlock(Blocks.snow, SchematicIgnore.class); schemes.registerSchematicBlock(Blocks.tallgrass, SchematicIgnore.class); schemes.registerSchematicBlock(Blocks.double_plant, SchematicIgnore.class); diff --git a/common/buildcraft/builders/HeuristicBlockDetection.java b/common/buildcraft/builders/HeuristicBlockDetection.java index 1f0e92bd..db1a8c88 100644 --- a/common/buildcraft/builders/HeuristicBlockDetection.java +++ b/common/buildcraft/builders/HeuristicBlockDetection.java @@ -63,7 +63,7 @@ public final class HeuristicBlockDetection { if (creativeOnly) { SchematicRegistry.INSTANCE.registerSchematicBlock(block, meta, SchematicBlockCreative.class); } else { - if (block instanceof IFluidBlock) { + if (block instanceof IFluidBlock) { IFluidBlock fblock = (IFluidBlock) block; if (fblock.getFluid() != null) { SchematicRegistry.INSTANCE.registerSchematicBlock(block, meta, SchematicFluid.class, new FluidStack(fblock.getFluid(), 1000)); diff --git a/common/buildcraft/builders/schematics/SchematicAir.java b/common/buildcraft/builders/schematics/SchematicAir.java new file mode 100644 index 00000000..e2449826 --- /dev/null +++ b/common/buildcraft/builders/schematics/SchematicAir.java @@ -0,0 +1,13 @@ +package buildcraft.builders.schematics; + +import java.util.LinkedList; +import net.minecraft.item.ItemStack; +import buildcraft.api.blueprints.IBuilderContext; +import buildcraft.api.blueprints.SchematicBlock; + +public class SchematicAir extends SchematicBlock { + @Override + public void getRequirementsForPlacement(IBuilderContext context, LinkedList requirements) { + + } +} diff --git a/common/buildcraft/commander/ContainerZonePlan.java b/common/buildcraft/commander/ContainerZonePlan.java index 7afc34ee..3fe56482 100755 --- a/common/buildcraft/commander/ContainerZonePlan.java +++ b/common/buildcraft/commander/ContainerZonePlan.java @@ -8,6 +8,11 @@ */ package buildcraft.commander; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.IntBuffer; +import java.util.zip.Deflater; +import java.util.zip.Inflater; import io.netty.buffer.ByteBuf; import net.minecraft.block.material.MapColor; import net.minecraft.entity.player.EntityPlayer; @@ -82,9 +87,10 @@ public class ContainerZonePlan extends BuildCraftContainer implements ICommandRe currentAreaSelection.readData(stream); gui.refreshSelectedArea(); } else if ("receiveImage".equals(command)) { - int size = stream.readUnsignedShort(); + int size = stream.readUnsignedMedium(); + for (int i = 0; i < size; ++i) { - mapTexture.colorMap[i] = stream.readInt(); + mapTexture.colorMap[i] = 0xFF000000 | MapColor.mapColorArray[stream.readUnsignedByte()].colorValue; } } } else if (side.isServer()) { @@ -109,52 +115,28 @@ public class ContainerZonePlan extends BuildCraftContainer implements ICommandRe } private void computeMap(int cx, int cz, int width, int height, int blocksPerPixel, EntityPlayer player) { - mapTexture = new BCDynamicTexture(width, height); + final byte[] textureData = new byte[width * height]; int startX = cx - width * blocksPerPixel / 2; int startZ = cz - height * blocksPerPixel / 2; for (int i = 0; i < width; ++i) { for (int j = 0; j < height; ++j) { - double r = 0; - double g = 0; - double b = 0; + int x = startX + i * blocksPerPixel; + int z = startZ + j * blocksPerPixel; + int ix = x - (map.chunkStartX << 4); + int iz = z - (map.chunkStartZ << 4); - for (int stepi = 0; stepi < blocksPerPixel; ++stepi) { - for (int stepj = 0; stepj < blocksPerPixel; ++stepj) { - int x = startX + i * blocksPerPixel + stepi; - int z = startZ + j * blocksPerPixel + stepj; - int ix = x - (map.chunkStartX << 4); - int iz = z - (map.chunkStartZ << 4); - - if (ix > 0 && ix < TileZonePlan.RESOLUTION && iz > 0 && iz < TileZonePlan.RESOLUTION) { - int color = MapColor.mapColorArray[map.colors[ix + iz * TileZonePlan.RESOLUTION]].colorValue; - - r += (color >> 16) & 255; - g += (color >> 8) & 255; - b += color & 255; - } - } + if (ix >= 0 && iz >= 0 && ix < TileZonePlan.RESOLUTION && iz < TileZonePlan.RESOLUTION) { + textureData[i + j * width] = map.colors[ix + iz * TileZonePlan.RESOLUTION]; } - - r /= blocksPerPixel * blocksPerPixel; - g /= blocksPerPixel * blocksPerPixel; - b /= blocksPerPixel * blocksPerPixel; - - r /= 255F; - g /= 255F; - b /= 255F; - - mapTexture.setColor(i, j, r, g, b, 1); } } BuildCraftCore.instance.sendToPlayer(player, new PacketCommand(this, "receiveImage", new CommandWriter() { public void write(ByteBuf data) { - data.writeShort(mapTexture.colorMap.length); - for (int i = 0; i < mapTexture.colorMap.length; i++) { - data.writeInt(mapTexture.colorMap[i]); - } + data.writeMedium(textureData.length); + data.writeBytes(textureData); } })); } diff --git a/common/buildcraft/commander/TileZonePlan.java b/common/buildcraft/commander/TileZonePlan.java index e981da9d..e6d12c08 100644 --- a/common/buildcraft/commander/TileZonePlan.java +++ b/common/buildcraft/commander/TileZonePlan.java @@ -9,12 +9,14 @@ package buildcraft.commander; import io.netty.buffer.ByteBuf; +import net.minecraft.block.material.MapColor; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.world.chunk.Chunk; +import buildcraft.api.core.SafeTimeTracker; import buildcraft.core.ItemMapLocation; import buildcraft.core.TileBuildCraft; import buildcraft.core.ZonePlan; @@ -39,6 +41,8 @@ public class TileZonePlan extends TileBuildCraft implements IInventory { private SimpleInventory inv = new SimpleInventory(2, "inv", 64); + private SafeTimeTracker zonePlannerScanning = new SafeTimeTracker(5); + @Override public void initialize() { super.initialize(); @@ -103,7 +107,7 @@ public class TileZonePlan extends TileBuildCraft implements IInventory { return; } - if (scan) { + if (scan && zonePlannerScanning.markTimeIfDelay(worldObj)) { int[] coords = getCoords(); Chunk chunk = worldObj.getChunkFromChunkCoords(coords[0], coords[1]); loadChunk(chunk); @@ -147,11 +151,11 @@ public class TileZonePlan extends TileBuildCraft implements IInventory { int x = (chunk.xPosition << 4) + cx; int z = (chunk.zPosition << 4) + cz; - int color = 0; - - for (int y = getWorldObj().getHeight() - 1; y >= 0; --y) { - if (!chunk.getBlock(cx, y, cz).isAir(worldObj, x, y, z)) { - color = chunk.getBlock(cx, y, cz).getMapColor(0).colorIndex; + int y = getWorldObj().getHeightValue(x, z); + int color; + while ((color = chunk.getBlock(cx, y, cz).getMapColor(0).colorIndex) == MapColor.airColor.colorIndex) { + y--; + if (y < 0) { break; } } @@ -193,7 +197,7 @@ public class TileZonePlan extends TileBuildCraft implements IInventory { chunkIt = nbt.getInteger("chunkIt"); colors = nbt.getByteArray("colors"); - if (colors.length != RESOLUTION * RESOLUTION || chunkIt >= colors.length) { + if (colors.length != RESOLUTION * RESOLUTION || chunkIt >= RESOLUTION_CHUNKS * RESOLUTION_CHUNKS) { colors = new byte[RESOLUTION * RESOLUTION]; scan = true; chunkIt = 0; diff --git a/common/buildcraft/core/blueprints/SchematicRegistry.java b/common/buildcraft/core/blueprints/SchematicRegistry.java index 470ac078..521b5a07 100644 --- a/common/buildcraft/core/blueprints/SchematicRegistry.java +++ b/common/buildcraft/core/blueprints/SchematicRegistry.java @@ -116,7 +116,7 @@ public final class SchematicRegistry implements ISchematicRegistry { } public SchematicBlock createSchematicBlock(Block block, int metadata) { - if (block == null || block == Blocks.air || metadata < 0 || metadata >= 16) { + if (block == null || metadata < 0 || metadata >= 16) { return null; }