use BitSet for templates and blueprints rather than BlockIndex Sets

This commit is contained in:
asiekierka 2015-04-02 21:13:10 +02:00
parent d6f5063c42
commit 9b06dff367
3 changed files with 57 additions and 73 deletions

View file

@ -8,7 +8,7 @@
*/
package buildcraft.core.blueprints;
import java.util.HashSet;
import java.util.BitSet;
import org.apache.logging.log4j.Level;
import net.minecraft.block.Block;
@ -40,6 +40,7 @@ import buildcraft.core.builders.BuildingSlot;
import buildcraft.core.builders.BuildingSlotBlock;
import buildcraft.core.builders.IBuildingItemsProvider;
import buildcraft.core.builders.TileAbstractBuilder;
import buildcraft.core.lib.utils.BitSetUtils;
import buildcraft.core.lib.utils.BlockUtils;
import buildcraft.core.proxy.CoreProxy;
@ -47,9 +48,8 @@ public abstract class BptBuilderBase implements IAreaProvider {
public BlueprintBase blueprint;
public BptContext context;
protected BitSet usedLocations;
protected boolean done;
protected HashSet<BlockIndex> clearedLocations = new HashSet<BlockIndex>();
protected HashSet<BlockIndex> builtLocations = new HashSet<BlockIndex>();
protected int x, y, z;
protected boolean initialized = false;
@ -60,6 +60,7 @@ public abstract class BptBuilderBase implements IAreaProvider {
this.x = x;
this.y = y;
this.z = z;
this.usedLocations = new BitSet(bluePrint.sizeX * bluePrint.sizeY * bluePrint.sizeZ);
done = false;
Box box = new Box();
@ -68,6 +69,20 @@ public abstract class BptBuilderBase implements IAreaProvider {
context = bluePrint.getContext(world, box);
}
protected boolean isLocationUsed(int i, int j, int k) {
int xCoord = i - x + blueprint.anchorX;
int yCoord = j - y + blueprint.anchorY;
int zCoord = k - z + blueprint.anchorZ;
return usedLocations.get((zCoord * blueprint.sizeY + yCoord) * blueprint.sizeX + xCoord);
}
protected void markLocationUsed(int i, int j, int k) {
int xCoord = i - x + blueprint.anchorX;
int yCoord = j - y + blueprint.anchorY;
int zCoord = k - z + blueprint.anchorZ;
usedLocations.set((zCoord * blueprint.sizeY + yCoord) * blueprint.sizeX + xCoord, true);
}
public void initialize() {
if (!initialized) {
internalInit();
@ -202,23 +217,7 @@ public abstract class BptBuilderBase implements IAreaProvider {
public void saveBuildStateToNBT(NBTTagCompound nbt, IBuildingItemsProvider builder) {
NBTTagList clearList = new NBTTagList();
for (BlockIndex loc : clearedLocations) {
NBTTagCompound cpt = new NBTTagCompound();
loc.writeTo(cpt);
clearList.appendTag(cpt);
}
nbt.setTag("clearList", clearList);
NBTTagList builtList = new NBTTagList();
for (BlockIndex loc : builtLocations) {
NBTTagCompound cpt = new NBTTagCompound();
loc.writeTo(cpt);
builtList.appendTag(cpt);
}
nbt.setTag("builtList", builtList);
nbt.setByteArray("usedLocationList", BitSetUtils.toByteArray(usedLocations));
NBTTagList buildingList = new NBTTagList();
@ -232,20 +231,8 @@ public abstract class BptBuilderBase implements IAreaProvider {
}
public void loadBuildStateToNBT(NBTTagCompound nbt, IBuildingItemsProvider builder) {
NBTTagList clearList = nbt.getTagList("clearList", Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < clearList.tagCount(); ++i) {
NBTTagCompound cpt = clearList.getCompoundTagAt(i);
clearedLocations.add (new BlockIndex(cpt));
}
NBTTagList builtList = nbt.getTagList("builtList", Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < builtList.tagCount(); ++i) {
NBTTagCompound cpt = builtList.getCompoundTagAt(i);
builtLocations.add (new BlockIndex(cpt));
if (nbt.hasKey("usedLocationList")) {
usedLocations = BitSetUtils.fromByteArray(nbt.getByteArray("usedLocationList"));
}
NBTTagList buildingList = nbt
@ -263,6 +250,28 @@ public abstract class BptBuilderBase implements IAreaProvider {
BCLog.logger.log(Level.WARN, "can't load building item", e);
}
}
// 6.4.6 and below migration
if (nbt.hasKey("clearList")) {
NBTTagList clearList = nbt.getTagList("clearList", Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < clearList.tagCount(); ++i) {
NBTTagCompound cpt = clearList.getCompoundTagAt(i);
BlockIndex o = new BlockIndex(cpt);
markLocationUsed(o.x, o.y, o.z);
}
}
if (nbt.hasKey("builtList")) {
NBTTagList builtList = nbt.getTagList("builtList", Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < builtList.tagCount(); ++i) {
NBTTagCompound cpt = builtList.getCompoundTagAt(i);
BlockIndex o = new BlockIndex(cpt);
markLocationUsed(o.x, o.y, o.z);
}
}
}
protected boolean isBlockBreakCanceled(World world, int x, int y, int z) {

View file

@ -79,8 +79,7 @@ public class BptBuilderBlueprint extends BptBuilderBase {
continue;
}
if (!clearedLocations.contains(new BlockIndex(
xCoord, yCoord, zCoord))) {
if (!isLocationUsed(xCoord, yCoord, zCoord)) {
SchematicBlock slot = (SchematicBlock) blueprint.contents[i][j][k];
if (slot == null && !blueprint.excavate) {
@ -140,8 +139,7 @@ public class BptBuilderBlueprint extends BptBuilderBase {
b.z = zCoord;
b.mode = Mode.Build;
if (!builtLocations.contains(new BlockIndex(xCoord, yCoord,
zCoord))) {
if (!isLocationUsed(xCoord, yCoord, zCoord)) {
switch (slot.getBuildStage()) {
case STANDALONE:
tmpStandalone.add(b);
@ -315,15 +313,7 @@ public class BptBuilderBlueprint extends BptBuilderBase {
if (slot.built) {
iterator.remove();
if (slot.mode == Mode.ClearIfInvalid) {
clearedLocations.add(new BlockIndex(slot.x,
slot.y, slot.z));
} else {
builtLocations.add(new BlockIndex(slot.x,
slot.y, slot.z));
}
markLocationUsed(slot.x, slot.y, slot.z);
postProcessing.add(slot);
continue;
@ -351,22 +341,14 @@ public class BptBuilderBlueprint extends BptBuilderBase {
if (BlockUtils.isUnbreakableBlock(world, slot.x, slot.y, slot.z)) {
// if the block can't be broken, just forget this iterator
iterator.remove();
if (slot.mode == Mode.ClearIfInvalid) {
clearedLocations.add(new BlockIndex(slot.x,
slot.y, slot.z));
} else {
builtLocations.add(new BlockIndex(slot.x,
slot.y, slot.z));
}
markLocationUsed(slot.x, slot.y, slot.z);
} else {
if (slot.mode == Mode.ClearIfInvalid) {
if (BuildCraftAPI.isSoftBlock(world, slot.x, slot.y,
slot.z)
|| isBlockBreakCanceled(world, slot.x, slot.y, slot.z)) {
iterator.remove();
clearedLocations.add(new BlockIndex(slot.x,
slot.y, slot.z));
markLocationUsed(slot.x, slot.y, slot.z);
} else {
if (builder == null) {
createDestroyItems(slot);
@ -376,8 +358,7 @@ public class BptBuilderBlueprint extends BptBuilderBase {
createDestroyItems(slot);
iterator.remove();
clearedLocations.add(new BlockIndex(slot.x,
slot.y, slot.z));
markLocationUsed(slot.x, slot.y, slot.z);
return slot;
}
}
@ -389,8 +370,7 @@ public class BptBuilderBlueprint extends BptBuilderBase {
// Forge does not allow us to place a block in
// this position.
iterator.remove();
builtLocations.add(new BlockIndex(slot.x,
slot.y, slot.z));
markLocationUsed(slot.x, slot.y, slot.z);
continue;
}
@ -403,9 +383,8 @@ public class BptBuilderBlueprint extends BptBuilderBase {
useRequirements(builder, slot);
iterator.remove();
markLocationUsed(slot.x, slot.y, slot.z);
postProcessing.add(slot);
builtLocations.add(new BlockIndex(slot.x,
slot.y, slot.z));
return slot;
}
} else {

View file

@ -51,9 +51,7 @@ public class BptBuilderTemplate extends BptBuilderBase {
SchematicBlockBase slot = blueprint.contents[i][j][k];
if (slot == null
&& !clearedLocations.contains(new BlockIndex(
xCoord, yCoord, zCoord))) {
if (slot == null && !isLocationUsed(xCoord, yCoord, zCoord)) {
BuildingSlotBlock b = new BuildingSlotBlock();
b.schematic = null;
@ -83,7 +81,7 @@ public class BptBuilderTemplate extends BptBuilderBase {
SchematicBlockBase slot = blueprint.contents[i][j][k];
if (slot != null && !builtLocations.contains(new BlockIndex(xCoord, yCoord, zCoord))) {
if (slot != null && !isLocationUsed(xCoord, yCoord, zCoord)) {
BuildingSlotBlock b = new BuildingSlotBlock();
b.schematic = slot;
@ -166,15 +164,14 @@ public class BptBuilderTemplate extends BptBuilderBase {
|| isBlockBreakCanceled(world, slot.x, slot.y, slot.z)
|| BuildCraftAPI.isSoftBlock(world, slot.x, slot.y, slot.z)) {
iteratorClear.remove();
clearedLocations.add(new BlockIndex(slot.x, slot.y, slot.z));
markLocationUsed(slot.x, slot.y, slot.z);
} else {
consumeEnergyToDestroy(builder, slot);
createDestroyItems(slot);
result = slot;
iteratorClear.remove();
clearedLocations.add(new BlockIndex(slot.x, slot.y, slot.z));
markLocationUsed(slot.x, slot.y, slot.z);
break;
}
}
@ -203,13 +200,12 @@ public class BptBuilderTemplate extends BptBuilderBase {
|| isBlockPlaceCanceled(world, slot.x, slot.y, slot.z, slot.schematic)
|| !BuildCraftAPI.isSoftBlock(world, slot.x, slot.y, slot.z)) {
iteratorBuild.remove();
builtLocations.add(new BlockIndex(slot.x, slot.y, slot.z));
markLocationUsed(slot.x, slot.y, slot.z);
} else if (builder.consumeEnergy(BuilderAPI.BUILD_ENERGY)) {
slot.addStackConsumed(firstSlotToConsume.decreaseStackInSlot(1));
result = slot;
iteratorBuild.remove();
builtLocations.add(new BlockIndex(slot.x, slot.y, slot.z));
markLocationUsed(slot.x, slot.y, slot.z);
break;
}
}