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; package buildcraft.core.blueprints;
import java.util.HashSet; import java.util.BitSet;
import org.apache.logging.log4j.Level; import org.apache.logging.log4j.Level;
import net.minecraft.block.Block; import net.minecraft.block.Block;
@ -40,6 +40,7 @@ import buildcraft.core.builders.BuildingSlot;
import buildcraft.core.builders.BuildingSlotBlock; import buildcraft.core.builders.BuildingSlotBlock;
import buildcraft.core.builders.IBuildingItemsProvider; import buildcraft.core.builders.IBuildingItemsProvider;
import buildcraft.core.builders.TileAbstractBuilder; import buildcraft.core.builders.TileAbstractBuilder;
import buildcraft.core.lib.utils.BitSetUtils;
import buildcraft.core.lib.utils.BlockUtils; import buildcraft.core.lib.utils.BlockUtils;
import buildcraft.core.proxy.CoreProxy; import buildcraft.core.proxy.CoreProxy;
@ -47,9 +48,8 @@ public abstract class BptBuilderBase implements IAreaProvider {
public BlueprintBase blueprint; public BlueprintBase blueprint;
public BptContext context; public BptContext context;
protected BitSet usedLocations;
protected boolean done; protected boolean done;
protected HashSet<BlockIndex> clearedLocations = new HashSet<BlockIndex>();
protected HashSet<BlockIndex> builtLocations = new HashSet<BlockIndex>();
protected int x, y, z; protected int x, y, z;
protected boolean initialized = false; protected boolean initialized = false;
@ -60,6 +60,7 @@ public abstract class BptBuilderBase implements IAreaProvider {
this.x = x; this.x = x;
this.y = y; this.y = y;
this.z = z; this.z = z;
this.usedLocations = new BitSet(bluePrint.sizeX * bluePrint.sizeY * bluePrint.sizeZ);
done = false; done = false;
Box box = new Box(); Box box = new Box();
@ -68,6 +69,20 @@ public abstract class BptBuilderBase implements IAreaProvider {
context = bluePrint.getContext(world, box); 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() { public void initialize() {
if (!initialized) { if (!initialized) {
internalInit(); internalInit();
@ -202,23 +217,7 @@ public abstract class BptBuilderBase implements IAreaProvider {
public void saveBuildStateToNBT(NBTTagCompound nbt, IBuildingItemsProvider builder) { public void saveBuildStateToNBT(NBTTagCompound nbt, IBuildingItemsProvider builder) {
NBTTagList clearList = new NBTTagList(); NBTTagList clearList = new NBTTagList();
for (BlockIndex loc : clearedLocations) { nbt.setByteArray("usedLocationList", BitSetUtils.toByteArray(usedLocations));
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);
NBTTagList buildingList = new NBTTagList(); NBTTagList buildingList = new NBTTagList();
@ -232,20 +231,8 @@ public abstract class BptBuilderBase implements IAreaProvider {
} }
public void loadBuildStateToNBT(NBTTagCompound nbt, IBuildingItemsProvider builder) { public void loadBuildStateToNBT(NBTTagCompound nbt, IBuildingItemsProvider builder) {
NBTTagList clearList = nbt.getTagList("clearList", Constants.NBT.TAG_COMPOUND); if (nbt.hasKey("usedLocationList")) {
usedLocations = BitSetUtils.fromByteArray(nbt.getByteArray("usedLocationList"));
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));
} }
NBTTagList buildingList = nbt NBTTagList buildingList = nbt
@ -263,6 +250,28 @@ public abstract class BptBuilderBase implements IAreaProvider {
BCLog.logger.log(Level.WARN, "can't load building item", e); 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) { protected boolean isBlockBreakCanceled(World world, int x, int y, int z) {

View file

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

View file

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