improved schematic architecture with regards to translations

This commit is contained in:
SpaceToad 2014-04-06 13:52:43 +02:00
parent 7bde6b7903
commit c55f124408
12 changed files with 198 additions and 87 deletions

View file

@ -124,6 +124,26 @@ public class Schematic {
}
/**
* Performs a transformations from world to blueprints. In particular, it should:
* - use the registry to map ids from world to blueprints
* - apply translations to all positions in the schematic to center in the
* blueprint referencial
*/
public void transformToBlueprint(MappingRegistry registry, Translation transform) {
}
/**
* Performs a transformations from blueprints to worlds. In particular, it should:
* - use the registry to map ids from blueprints to world
* - apply translations to all positions in the schematic to center in the
* builder referencial
*/
public void transformToWorld(MappingRegistry registry, Translation transform) {
}
/**
* Places the block in the world, at the location specified in the slot,
* using the stack in parameters

View file

@ -23,7 +23,7 @@ import buildcraft.core.utils.Utils;
public class SchematicEntity extends Schematic {
public Class <? extends Entity> entity;
public Class<? extends Entity> entity;
public NBTTagCompound cpt = new NBTTagCompound();
@ -32,28 +32,38 @@ public class SchematicEntity extends Schematic {
* blueprint. Modders can either rely on this list or compute their own int
* Schematic.
*/
public ItemStack [] storedRequirements = new ItemStack [0];
public void writeToWorld(IBuilderContext context, CoordTransformation transform) {
NBTTagList nbttaglist = cpt.getTagList("Pos", 6);
Position pos = new Position(nbttaglist.func_150309_d(0),
nbttaglist.func_150309_d(1), nbttaglist.func_150309_d(2));
pos = transform.translate(pos);
cpt.setTag("Pos", this.newDoubleNBTList(new double[] {pos.x, pos.y, pos.z}));
public ItemStack[] storedRequirements = new ItemStack[0];
public void writeToWorld(IBuilderContext context) {
Entity e = EntityList.createEntityFromNBT(cpt, context.world());
context.world().spawnEntityInWorld(e);
}
public void readFromWorld(IBuilderContext context, Entity entity, CoordTransformation transform) {
public void readFromWorld(IBuilderContext context, Entity entity) {
entity.writeToNBTOptional(cpt);
}
@Override
public void transformToBlueprint(MappingRegistry registry,
Translation transform) {
NBTTagList nbttaglist = cpt.getTagList("Pos", 6);
Position pos = new Position(nbttaglist.func_150309_d(0),
nbttaglist.func_150309_d(1), nbttaglist.func_150309_d(2));
pos = transform.translate(pos);
cpt.setTag("Pos", this.newDoubleNBTList(new double[] {pos.x, pos.y, pos.z}));
cpt.setTag("Pos",
this.newDoubleNBTList(new double[] { pos.x, pos.y, pos.z }));
}
@Override
public void transformToWorld(MappingRegistry registry, Translation transform) {
NBTTagList nbttaglist = cpt.getTagList("Pos", 6);
Position pos = new Position(nbttaglist.func_150309_d(0),
nbttaglist.func_150309_d(1), nbttaglist.func_150309_d(2));
pos = transform.translate(pos);
cpt.setTag("Pos",
this.newDoubleNBTList(new double[] { pos.x, pos.y, pos.z }));
}
@Override
@ -62,17 +72,23 @@ public class SchematicEntity extends Schematic {
Position pos = new Position(nbttaglist.func_150309_d(0),
nbttaglist.func_150309_d(1), nbttaglist.func_150309_d(2));
pos = context.rotatePositionLeft(pos);
cpt.setTag("Pos", this.newDoubleNBTList(new double[] {pos.x, pos.y, pos.z}));
cpt.setTag("Pos",
this.newDoubleNBTList(new double[] { pos.x, pos.y, pos.z }));
nbttaglist = cpt.getTagList("Rotation", 5);
float yaw = nbttaglist.func_150308_e (0);
float yaw = nbttaglist.func_150308_e(0);
yaw += 90;
cpt.setTag("Rotation", this.newFloatNBTList(new float[] {yaw, nbttaglist.func_150308_e (1)}));
cpt.setTag(
"Rotation",
this.newFloatNBTList(new float[] { yaw,
nbttaglist.func_150308_e(1) }));
}
@Override
public void writeToNBT(NBTTagCompound nbt, MappingRegistry registry) {
nbt.setInteger ("entityId", registry.getIdForEntity(entity));
NBTTagList nbttaglist = cpt.getTagList("Pos", 6);
nbt.setInteger("entityId", registry.getIdForEntity(entity));
nbt.setTag("entity", cpt);
NBTTagList rq = new NBTTagList();
@ -91,7 +107,8 @@ public class SchematicEntity extends Schematic {
public void readFromNBT(NBTTagCompound nbt, MappingRegistry registry) {
cpt = nbt.getCompoundTag("entity");
NBTTagList rq = nbt.getTagList("rq", Utils.NBTTag_Types.NBTTagCompound.ordinal());
NBTTagList rq = nbt.getTagList("rq",
Utils.NBTTag_Types.NBTTagCompound.ordinal());
ArrayList<ItemStack> rqs = new ArrayList<ItemStack>();
@ -117,7 +134,7 @@ public class SchematicEntity extends Schematic {
}
}
storedRequirements = rqs.toArray(new ItemStack [rqs.size()]);
storedRequirements = rqs.toArray(new ItemStack[rqs.size()]);
}
protected NBTTagList newDoubleNBTList(double... par1ArrayOfDouble) {
@ -146,23 +163,22 @@ public class SchematicEntity extends Schematic {
return nbttaglist;
}
public boolean isAlreadyBuilt(IBuilderContext context, CoordTransformation transform) {
public boolean isAlreadyBuilt(IBuilderContext context) {
NBTTagList nbttaglist = cpt.getTagList("Pos", 6);
Position newPosition = new Position(nbttaglist.func_150309_d(0),
nbttaglist.func_150309_d(1), nbttaglist.func_150309_d(2));
newPosition = transform.translate(newPosition);
for (Object o : context.world().loadedEntityList) {
Entity e = (Entity) o;
Position existingPositon = new Position(e.posX, e.posY, e.posZ);
if (existingPositon.isClose (newPosition, 0.1F)) {
if (existingPositon.isClose(newPosition, 0.1F)) {
return true;
}
}
return false;
}
}

View file

@ -10,7 +10,7 @@ package buildcraft.api.blueprints;
import buildcraft.api.core.Position;
public class CoordTransformation {
public class Translation {
public double x = 0;
public double y = 0;
@ -26,4 +26,9 @@ public class CoordTransformation {
return p2;
}
@Override
public String toString () {
return "{" + x + ", " + y + ", " + z + "}";
}
}

View file

@ -16,6 +16,7 @@ import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.AxisAlignedBB;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.BuildCraftBuilders;
import buildcraft.api.blueprints.Translation;
import buildcraft.api.core.IAreaProvider;
import buildcraft.core.BlockIndex;
import buildcraft.core.BlockScanner;
@ -74,8 +75,17 @@ public class TileArchitect extends TileBuildCraft implements IInventory, IBoxPro
/ (float) blockScanner.totalBlocks()) * 100);
if (blockScanner.blocksLeft() == 0) {
System.out.println ("Z_A " + writingContext.box.sizeZ());
writingBlueprint.readEntitiesFromWorld (writingContext, this);
Translation transform = new Translation();
transform.x = -writingContext.surroundingBox().pMin().x;
transform.y = -writingContext.surroundingBox().pMin().y;
transform.z = -writingContext.surroundingBox().pMin().z;
writingBlueprint.transformToBlueprint(transform);
ForgeDirection o = ForgeDirection.values()[worldObj.getBlockMetadata(
xCoord, yCoord, zCoord)].getOpposite();

View file

@ -21,6 +21,7 @@ import net.minecraft.util.AxisAlignedBB;
import net.minecraft.world.WorldSettings.GameType;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.BuildCraftBuilders;
import buildcraft.api.blueprints.Translation;
import buildcraft.api.core.Position;
import buildcraft.api.gates.IAction;
import buildcraft.core.BlockIndex;
@ -274,6 +275,14 @@ public class TileBuilder extends TileAbstractBuilder implements IMachine {
}
}
Translation transform = new Translation();
transform.x = x - bpt.anchorX;
transform.y = y - bpt.anchorY;
transform.z = z - bpt.anchorZ;
bpt.transformToWorld(transform);
BptBuilderBase result = null;
if (items[0].getItem() instanceof ItemBlueprintStandard) {

View file

@ -14,9 +14,10 @@ import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import buildcraft.api.blueprints.CoordTransformation;
import buildcraft.api.blueprints.IBuilderContext;
import buildcraft.api.blueprints.MappingRegistry;
import buildcraft.api.blueprints.SchematicEntity;
import buildcraft.api.blueprints.Translation;
import buildcraft.api.core.Position;
public class SchematicHanging extends SchematicEntity {
@ -27,6 +28,28 @@ public class SchematicHanging extends SchematicEntity {
this.baseItem = baseItem;
}
@Override
public void transformToBlueprint(MappingRegistry registry, Translation transform) {
super.transformToBlueprint(registry, transform);
Position pos = new Position (cpt.getInteger("TileX"), cpt.getInteger("TileY"), cpt.getInteger("TileZ"));
pos = transform.translate(pos);
cpt.setInteger("TileX", (int) pos.x);
cpt.setInteger("TileY", (int) pos.y);
cpt.setInteger("TileZ", (int) pos.z);
}
@Override
public void transformToWorld(MappingRegistry registry, Translation transform) {
super.transformToWorld(registry, transform);
Position pos = new Position (cpt.getInteger("TileX"), cpt.getInteger("TileY"), cpt.getInteger("TileZ"));
pos = transform.translate(pos);
cpt.setInteger("TileX", (int) pos.x);
cpt.setInteger("TileY", (int) pos.y);
cpt.setInteger("TileZ", (int) pos.z);
}
@Override
public void rotateLeft(IBuilderContext context) {
super.rotateLeft(context);
@ -43,13 +66,7 @@ public class SchematicHanging extends SchematicEntity {
}
@Override
public void writeToWorld(IBuilderContext context, CoordTransformation transform) {
Position pos = new Position (cpt.getInteger("TileX"), cpt.getInteger("TileY"), cpt.getInteger("TileZ"));
pos = transform.translate(pos);
cpt.setInteger("TileX", (int) pos.x);
cpt.setInteger("TileY", (int) pos.y);
cpt.setInteger("TileZ", (int) pos.z);
public void writeToWorld(IBuilderContext context) {
if (baseItem == Items.item_frame) {
NBTTagCompound tag = cpt.getCompoundTag("Item");
tag.setInteger("id", Item.itemRegistry.getIDForObject(context
@ -57,30 +74,28 @@ public class SchematicHanging extends SchematicEntity {
cpt.setTag("Item", tag);
}
super.writeToWorld(context, transform);
super.writeToWorld(context);
}
@Override
public void readFromWorld(IBuilderContext context, Entity entity, CoordTransformation transform) {
super.readFromWorld(context, entity, transform);
Position pos = new Position (cpt.getInteger("TileX"), cpt.getInteger("TileY"), cpt.getInteger("TileZ"));
pos = transform.translate(pos);
cpt.setInteger("TileX", (int) pos.x);
cpt.setInteger("TileY", (int) pos.y);
cpt.setInteger("TileZ", (int) pos.z);
public void readFromWorld(IBuilderContext context, Entity entity) {
super.readFromWorld(context, entity);
if (baseItem == Items.item_frame) {
NBTTagCompound tag = cpt.getCompoundTag("Item");
ItemStack stack = ItemStack.loadItemStackFromNBT(tag);
storedRequirements = new ItemStack [2];
storedRequirements [0] = new ItemStack(baseItem);
storedRequirements [1] = stack;
if (stack != null) {
storedRequirements = new ItemStack [2];
storedRequirements [0] = new ItemStack(baseItem);
storedRequirements [1] = stack;
tag.setInteger("id", context.getMappingRegistry().getIdForItem(stack.getItem()));
cpt.setTag("Item", tag);
tag.setInteger("id", context.getMappingRegistry().getIdForItem(stack.getItem()));
cpt.setTag("Item", tag);
} else {
storedRequirements = new ItemStack [1];
storedRequirements [0] = new ItemStack(baseItem);
}
} else {
storedRequirements = new ItemStack [1];
storedRequirements [0] = new ItemStack(baseItem);
@ -88,9 +103,8 @@ public class SchematicHanging extends SchematicEntity {
}
@Override
public boolean isAlreadyBuilt(IBuilderContext context, CoordTransformation transform) {
public boolean isAlreadyBuilt(IBuilderContext context) {
Position newPosition = new Position (cpt.getInteger("TileX"), cpt.getInteger("TileY"), cpt.getInteger("TileZ"));
newPosition = transform.translate(newPosition);
int dir = cpt.getInteger("Direction");
@ -101,9 +115,6 @@ public class SchematicHanging extends SchematicEntity {
EntityHanging h = (EntityHanging) e;
Position existingPositon = new Position(h.field_146063_b, h.field_146064_c, h.field_146062_d);
System.out.println ("EXPECTED POS" + newPosition + ", found " + existingPositon);
System.out.println ("EXPECTED DIR" + dir + ", found " + ((EntityHanging) e).hangingDirection);
if (existingPositon.isClose(newPosition, 0.1F) && dir == ((EntityHanging) e).hangingDirection) {
return true;
}

View file

@ -13,9 +13,10 @@ import net.minecraft.entity.item.EntityMinecart;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagList;
import buildcraft.api.blueprints.CoordTransformation;
import buildcraft.api.blueprints.IBuilderContext;
import buildcraft.api.blueprints.MappingRegistry;
import buildcraft.api.blueprints.SchematicEntity;
import buildcraft.api.blueprints.Translation;
import buildcraft.api.core.Position;
public class SchematicMinecart extends SchematicEntity {
@ -27,20 +28,8 @@ public class SchematicMinecart extends SchematicEntity {
}
@Override
public void writeToWorld(IBuilderContext context, CoordTransformation transform) {
NBTTagList nbttaglist = cpt.getTagList("Pos", 6);
Position pos = new Position(nbttaglist.func_150309_d(0),
nbttaglist.func_150309_d(1), nbttaglist.func_150309_d(2));
pos.x += 0.5;
pos.z += 0.5;
cpt.setTag("Pos", this.newDoubleNBTList(new double[] {pos.x, pos.y, pos.z}));
super.writeToWorld(context, transform);
}
@Override
public void readFromWorld(IBuilderContext context, Entity entity, CoordTransformation transform) {
super.readFromWorld(context, entity, transform);
public void transformToBlueprint(MappingRegistry registry, Translation transform) {
super.transformToBlueprint(registry, transform);
NBTTagList nbttaglist = cpt.getTagList("Pos", 6);
Position pos = new Position(nbttaglist.func_150309_d(0),
@ -48,29 +37,41 @@ public class SchematicMinecart extends SchematicEntity {
pos.x -= 0.5;
pos.z -= 0.5;
cpt.setTag("Pos", this.newDoubleNBTList(new double[] {pos.x, pos.y, pos.z}));
}
@Override
public void transformToWorld(MappingRegistry registry, Translation transform) {
super.transformToWorld(registry, transform);
NBTTagList nbttaglist = cpt.getTagList("Pos", 6);
Position pos = new Position(nbttaglist.func_150309_d(0),
nbttaglist.func_150309_d(1), nbttaglist.func_150309_d(2));
pos.x += 0.5;
pos.z += 0.5;
cpt.setTag("Pos", this.newDoubleNBTList(new double[] {pos.x, pos.y, pos.z}));
}
@Override
public void readFromWorld(IBuilderContext context, Entity entity) {
super.readFromWorld(context, entity);
storedRequirements = new ItemStack [1];
storedRequirements [0] = new ItemStack(baseItem);
}
@Override
public boolean isAlreadyBuilt(IBuilderContext context, CoordTransformation transform) {
public boolean isAlreadyBuilt(IBuilderContext context) {
NBTTagList nbttaglist = cpt.getTagList("Pos", 6);
Position newPosition = new Position(nbttaglist.func_150309_d(0),
nbttaglist.func_150309_d(1), nbttaglist.func_150309_d(2));
newPosition.x += 0.5;
newPosition.z += 0.5;
newPosition = transform.translate(newPosition);
for (Object o : context.world().loadedEntityList) {
Entity e = (Entity) o;
Position existingPositon = new Position(e.posX, e.posY, e.posZ);
if (e instanceof EntityMinecart) {
System.out.println ("found " + existingPositon + ", should be " + newPosition);
if (existingPositon.isClose(newPosition, 0.1F)) {
return true;
}

View file

@ -17,11 +17,11 @@ import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import buildcraft.BuildCraftBuilders;
import buildcraft.api.blueprints.CoordTransformation;
import buildcraft.api.blueprints.IBuilderContext;
import buildcraft.api.blueprints.SchematicBlock;
import buildcraft.api.blueprints.SchematicEntity;
import buildcraft.api.blueprints.SchematicRegistry;
import buildcraft.api.blueprints.Translation;
import buildcraft.core.utils.BCLog;
import buildcraft.core.utils.NBTUtils;
import buildcraft.core.utils.Utils;
@ -46,6 +46,24 @@ public class Blueprint extends BlueprintBase {
super.rotateLeft(context);
}
@Override
public void transformToBlueprint(Translation transform) {
super.transformToBlueprint(transform);
for (SchematicEntity e : entities) {
e.transformToBlueprint(mapping, transform);
}
}
@Override
public void transformToWorld(Translation transform) {
super.transformToWorld(transform);
for (SchematicEntity e : entities) {
e.transformToWorld(mapping, transform);
}
}
@Override
public void readFromWorld(IBuilderContext context, TileEntity anchorTile, int x, int y, int z) {
BptContext bptContext = (BptContext) context;
@ -80,7 +98,7 @@ public class Blueprint extends BlueprintBase {
@Override
public void readEntitiesFromWorld(IBuilderContext context, TileEntity anchorTile) {
CoordTransformation transform = new CoordTransformation();
Translation transform = new Translation();
transform.x = -context.surroundingBox().pMin().x;
transform.y = -context.surroundingBox().pMin().y;
@ -93,7 +111,7 @@ public class Blueprint extends BlueprintBase {
SchematicEntity s = SchematicRegistry.newSchematicEntity(e.getClass());
if (s != null) {
s.readFromWorld(context, e, transform);
s.readFromWorld(context, e);
entities.add(s);
}
}

View file

@ -18,6 +18,7 @@ import net.minecraft.world.World;
import buildcraft.api.blueprints.IBuilderContext;
import buildcraft.api.blueprints.MappingRegistry;
import buildcraft.api.blueprints.SchematicBlockBase;
import buildcraft.api.blueprints.Translation;
import buildcraft.builders.blueprints.BlueprintId;
import buildcraft.core.Box;
import buildcraft.core.Version;
@ -51,6 +52,30 @@ public abstract class BlueprintBase {
anchorZ = 0;
}
public void transformToBlueprint(Translation transform) {
for (int x = 0; x < sizeX; ++x) {
for (int y = 0; y < sizeY; ++y) {
for (int z = 0; z < sizeZ; ++z) {
if (contents [x][y][z] != null) {
contents [x][y][z].transformToBlueprint(mapping, transform);
}
}
}
}
}
public void transformToWorld(Translation transform) {
for (int x = 0; x < sizeX; ++x) {
for (int y = 0; y < sizeY; ++y) {
for (int z = 0; z < sizeZ; ++z) {
if (contents [x][y][z] != null) {
contents [x][y][z].transformToWorld(mapping, transform);
}
}
}
}
}
public void rotateLeft(BptContext context) {
SchematicBlockBase newContents[][][] = new SchematicBlockBase[sizeZ][sizeY][sizeX];

View file

@ -21,10 +21,10 @@ import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import net.minecraft.world.WorldSettings.GameType;
import buildcraft.api.blueprints.CoordTransformation;
import buildcraft.api.blueprints.Schematic;
import buildcraft.api.blueprints.SchematicBlock;
import buildcraft.api.blueprints.SchematicEntity;
import buildcraft.api.blueprints.Translation;
import buildcraft.api.core.StackKey;
import buildcraft.builders.TileAbstractBuilder;
import buildcraft.core.blueprints.BuildingSlotBlock.Mode;
@ -101,7 +101,7 @@ public class BptBuilderBlueprint extends BptBuilderBase {
iterator = new BuildingSlotIterator(buildList);
CoordTransformation transform = new CoordTransformation();
Translation transform = new Translation();
transform.x = x - blueprint.anchorX;
transform.y = y - blueprint.anchorY;
@ -110,8 +110,6 @@ public class BptBuilderBlueprint extends BptBuilderBase {
for (SchematicEntity e : bluePrint.entities) {
BuildingSlotEntity b = new BuildingSlotEntity();
b.schematic = e;
b.transform = transform;
entityList.add(b);
}
@ -136,6 +134,8 @@ public class BptBuilderBlueprint extends BptBuilderBase {
if (slot != null) {
return slot;
} else {
return null;
}
}
@ -145,6 +145,8 @@ public class BptBuilderBlueprint extends BptBuilderBase {
if (slot != null) {
return slot;
} else {
return null;
}
}

View file

@ -12,19 +12,17 @@ import java.util.LinkedList;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagList;
import buildcraft.api.blueprints.CoordTransformation;
import buildcraft.api.blueprints.IBuilderContext;
import buildcraft.api.blueprints.SchematicEntity;
import buildcraft.api.core.Position;
public class BuildingSlotEntity extends BuildingSlot {
public CoordTransformation transform;
public SchematicEntity schematic;
@Override
public void writeToWorld(IBuilderContext context) {
schematic.writeToWorld(context, transform);
schematic.writeToWorld(context);
}
@Override
@ -33,8 +31,6 @@ public class BuildingSlotEntity extends BuildingSlot {
Position pos = new Position(nbttaglist.func_150309_d(0),
nbttaglist.func_150309_d(1), nbttaglist.func_150309_d(2));
pos = transform.translate(pos);
return pos;
}
@ -56,6 +52,6 @@ public class BuildingSlotEntity extends BuildingSlot {
@Override
public boolean isAlreadyBuilt(IBuilderContext context) {
return schematic.isAlreadyBuilt(context, transform);
return schematic.isAlreadyBuilt(context);
}
}

View file

@ -105,7 +105,6 @@ public class EntityRobot extends EntityLivingBase implements
public void showLaser () {
if (!laser.isVisible) {
System.out.println ("SHOW LASER");
laser.isVisible = true;
needsUpdate = true;
}
@ -136,7 +135,6 @@ public class EntityRobot extends EntityLivingBase implements
if (!worldObj.isRemote) {
if (currentTask == null) {
if (scanForTasks.markTimeIfDelay(worldObj)) {
System.out.println ("SCAN");
RobotTaskProviderRegistry.scanForTask(this);
}
} else {