Change the quarry so all processing is in the TileEntity. The entity is

now completely dumb and does nothing on either side.
Also, the quarry now plays the block break effect when it breaks a block
for sheer awesome :)
This commit is contained in:
Christian 2012-09-09 02:45:27 -04:00
parent 37994cdbd5
commit 8335cbd15e
3 changed files with 117 additions and 202 deletions

View file

@ -20,10 +20,9 @@ import net.minecraft.src.NBTTagCompound;
import net.minecraft.src.TileEntity; import net.minecraft.src.TileEntity;
import net.minecraft.src.World; import net.minecraft.src.World;
public class EntityMechanicalArm extends Entity implements IEntityAdditionalSpawnData { public class EntityMechanicalArm extends Entity {
EntityBlock xArm, yArm, zArm, head; EntityBlock xArm, yArm, zArm, head;
public IArmListener listener;
boolean inProgressionXZ = false; boolean inProgressionXZ = false;
boolean inProgressionY = false; boolean inProgressionY = false;
@ -31,30 +30,16 @@ public class EntityMechanicalArm extends Entity implements IEntityAdditionalSpaw
private double armSizeX; private double armSizeX;
private double armSizeZ; private double armSizeZ;
private double xRoot; private double xRoot;
private double yRoot; private double yRoot;
private double zRoot; private double zRoot;
private double angle; private double angle;
private int headX, headY, headZ;
public EntityMechanicalArm(World world) { public EntityMechanicalArm(World world) {
super(world); super(world);
makeParts(world); makeParts(world);
// Head X, Y, Z
dataWatcher.addObject(2, 1);
dataWatcher.addObject(3, 1);
dataWatcher.addObject(4, 1);
// Target X, Y, Z
dataWatcher.addObject(5, 1);
dataWatcher.addObject(6, 1);
dataWatcher.addObject(7, 1);
// Speed
dataWatcher.addObject(8, (int)(0.03 * 8000D));
} }
public EntityMechanicalArm(World world, double i, double j, double k, double width, double height, TileQuarry parent) { public EntityMechanicalArm(World world, double i, double j, double k, double width, double height, TileQuarry parent) {
@ -68,38 +53,19 @@ public class EntityMechanicalArm extends Entity implements IEntityAdditionalSpaw
this.motionZ = 0.0; this.motionZ = 0.0;
setArmSize(width, height); setArmSize(width, height);
setHead(i, j - 2, k); setHead(i, j - 2, k);
setTarget(i, j - 2, k);
noClip = true; noClip = true;
inProgressionXZ = false;
inProgressionY = false;
this.parent = parent; this.parent = parent;
parent.setArm(this); parent.setArm(this);
updatePosition(); updatePosition();
} }
public void setArmSpeed(double speed)
{
dataWatcher.updateObject(8, (int)(speed * 8000D));
}
public double getArmSpeed()
{
return dataWatcher.getWatchableObjectInt(8) / 8000D;
}
void setHead(double x, double y, double z) void setHead(double x, double y, double z)
{ {
dataWatcher.updateObject(2, ((int)(x * 32D))); this.headX = (int) (x * 32D);
dataWatcher.updateObject(3, ((int)(y * 32D))); this.headY = (int) (y * 32D);
dataWatcher.updateObject(4, ((int)(z * 32D))); this.headZ = (int) (z * 32D);
}
void setTarget(double x, double y, double z) {
dataWatcher.updateObject(5, ((int)(x * 32D)));
dataWatcher.updateObject(6, ((int)(y * 32D)));
dataWatcher.updateObject(7, ((int)(z * 32D)));
} }
private void setArmSize(double x, double z) private void setArmSize(double x, double z)
@ -151,7 +117,6 @@ public class EntityMechanicalArm extends Entity implements IEntityAdditionalSpaw
{ {
parent = (TileQuarry) te; parent = (TileQuarry) te;
parent.setArm(this); parent.setArm(this);
} }
else else
{ {
@ -170,13 +135,8 @@ public class EntityMechanicalArm extends Entity implements IEntityAdditionalSpaw
@Override @Override
public void onUpdate() { public void onUpdate() {
if (worldObj.isRemote)
{
super.onUpdate();
updatePosition();
return;
}
super.onUpdate(); super.onUpdate();
updatePosition();
if (parent == null) if (parent == null)
{ {
findAndJoinQuarry(); findAndJoinQuarry();
@ -187,69 +147,6 @@ public class EntityMechanicalArm extends Entity implements IEntityAdditionalSpaw
setDead(); setDead();
return; return;
} }
double[] target = getTarget();
double[] head = getHead();
double dX = target[0] - head[0];
double dY = target[1] - head[1];
double dZ = target[2] - head[2];
if (dX != 0 || dY != 0 || dZ != 0)
{
angle = Math.atan2(target[2]-head[2], target[0]-head[0]);
inProgressionXZ = true;
inProgressionY = true;
}
if (getArmSpeed() > 0) {
doMove(getArmSpeed());
}
}
public void doMove(double instantSpeed) {
double[] target = getTarget();
double[] head = getHead();
if (inProgressionXZ) {
if (Math.abs(target[0] - head[0]) < instantSpeed * 2 && Math.abs(target[2] - head[2]) < instantSpeed * 2) {
head[0] = target[0];
head[2] = target[2];
inProgressionXZ = false;
if (listener != null && !inProgressionY) {
listener.positionReached(this);
head[1] = target[1];
}
} else {
head[0] += Math.cos(angle) * instantSpeed;
head[2] += Math.sin(angle) * instantSpeed;
}
setHead(head[0], head[1], head[2]);
}
if (inProgressionY) {
if (Math.abs(target[1] - head[1]) < instantSpeed * 2) {
head[1] = target[1];
inProgressionY = false;
if (listener != null && !inProgressionXZ) {
listener.positionReached(this);
head[0] = target[0];
head[2] = target[2];
}
} else {
if (target[1] > head[1]) {
head[1] += instantSpeed / 2;
} else {
head[1] -= instantSpeed / 2;
}
}
setHead(head[0],head[1],head[2]);
}
updatePosition();
} }
public void updatePosition() { public void updatePosition() {
@ -262,13 +159,6 @@ public class EntityMechanicalArm extends Entity implements IEntityAdditionalSpaw
} }
public void joinToWorld(World w) {
if (!w.isRemote)
{
w.spawnEntityInWorld(this);
}
}
@Override @Override
public void setDead() { public void setDead() {
if (worldObj!=null && worldObj.isRemote) if (worldObj!=null && worldObj.isRemote)
@ -281,27 +171,8 @@ public class EntityMechanicalArm extends Entity implements IEntityAdditionalSpaw
super.setDead(); super.setDead();
} }
@Override private double[] getHead()
public void writeSpawnData(ByteArrayDataOutput data) {
data.writeDouble(armSizeX);
data.writeDouble(armSizeZ);
}
@Override
public void readSpawnData(ByteArrayDataInput data) {
armSizeX = data.readDouble();
armSizeZ = data.readDouble();
setArmSize(armSizeX, armSizeZ);
updatePosition();
}
public double[] getTarget()
{ {
return new double[] { this.dataWatcher.getWatchableObjectInt(5) / 32D, this.dataWatcher.getWatchableObjectInt(6) / 32D, this.dataWatcher.getWatchableObjectInt(7) / 32D }; return new double[] { this.headX / 32D, this.headY / 32D, this.headZ / 32D };
}
public double[] getHead()
{
return new double[] { this.dataWatcher.getWatchableObjectInt(2) / 32D, this.dataWatcher.getWatchableObjectInt(3) / 32D, this.dataWatcher.getWatchableObjectInt(4) / 32D };
} }
} }

View file

@ -1,16 +0,0 @@
/**
* Copyright (c) SpaceToad, 2011
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.factory;
public interface IArmListener {
public void positionReached(EntityMechanicalArm arm);
}

View file

@ -40,37 +40,36 @@ import net.minecraft.src.Block;
import net.minecraft.src.EntityItem; import net.minecraft.src.EntityItem;
import net.minecraft.src.EntityPlayer; import net.minecraft.src.EntityPlayer;
import net.minecraft.src.ItemStack; import net.minecraft.src.ItemStack;
import net.minecraft.src.MathHelper;
import net.minecraft.src.NBTTagCompound; import net.minecraft.src.NBTTagCompound;
public class TileQuarry extends TileMachine implements IArmListener, IMachine, IPowerReceptor, IPipeConnection, IBuilderInventory { public class TileQuarry extends TileMachine implements IMachine, IPowerReceptor, IPipeConnection, IBuilderInventory {
boolean isDigging = false;
public @TileNetworkData public @TileNetworkData
Box box = new Box(); Box box = new Box();
public @TileNetworkData public @TileNetworkData
boolean inProcess = false; boolean inProcess = false;
public EntityMechanicalArm arm;
public @TileNetworkData public @TileNetworkData
int targetX, targetY, targetZ; int targetX, targetY, targetZ;
public @TileNetworkData public @TileNetworkData
double headPosX, headPosY, headPosZ; double headPosX, headPosY, headPosZ;
public @TileNetworkData public @TileNetworkData
double speed = 0.03; double speed = 0.03;
public EntityRobot builder;
public @TileNetworkData public @TileNetworkData
boolean builderDone = false; boolean builderDone = false;
public EntityRobot builder;
BptBuilderBase bluePrintBuilder; BptBuilderBase bluePrintBuilder;
public EntityMechanicalArm arm;
public IPowerProvider powerProvider; public IPowerProvider powerProvider;
boolean isDigging = false;
public static int MAX_ENERGY = 7000; public static int MAX_ENERGY = 7000;
public TileQuarry() { public TileQuarry() {
powerProvider = PowerFramework.currentFramework.createPowerProvider(); powerProvider = PowerFramework.currentFramework.createPowerProvider();
powerProvider.configure(20, 25, 25, 25, MAX_ENERGY); powerProvider.configure(20, 25, 25, 25, MAX_ENERGY);
} }
@ -95,6 +94,10 @@ public class TileQuarry extends TileMachine implements IArmListener, IMachine, I
if (findTarget(false)) { if (findTarget(false)) {
isDigging = true; isDigging = true;
if (box != null && (( headPosX < box.xMin || headPosX > box.xMax) || (headPosZ < box.zMin || headPosZ > box.zMax)))
{
setHead(box.xMin + 1, yCoord + 2, box.zMin + 1);
}
} }
} else { } else {
@ -105,10 +108,12 @@ public class TileQuarry extends TileMachine implements IArmListener, IMachine, I
} }
private boolean loadDefaultBoundaries = false; private boolean loadDefaultBoundaries = false;
private boolean movingHorizontally;
private boolean movingVertically;
private double headTrajectory;
private void createArm() { private void createArm() {
// worldObj.getEntitiesWithinAABB(EntityMechanicalArm.class, AxisAlignedBB.getBoundingBox(xCoord, yCoord, zCoord, xCoord, yCoord, zCoord));
worldObj.spawnEntityInWorld(new EntityMechanicalArm(worldObj, worldObj.spawnEntityInWorld(new EntityMechanicalArm(worldObj,
box.xMin + Utils.pipeMaxPos, box.xMin + Utils.pipeMaxPos,
yCoord + bluePrintBuilder.bluePrint.sizeY - 1 + Utils.pipeMinPos, box.zMin + Utils.pipeMaxPos, yCoord + bluePrintBuilder.bluePrint.sizeY - 1 + Utils.pipeMinPos, box.zMin + Utils.pipeMaxPos,
@ -120,38 +125,21 @@ public class TileQuarry extends TileMachine implements IArmListener, IMachine, I
public void setArm(EntityMechanicalArm arm) public void setArm(EntityMechanicalArm arm)
{ {
this.arm = arm; this.arm = arm;
arm.listener = this;
isDigging = true;
} }
@Override @Override
public void updateEntity() { public void updateEntity() {
super.updateEntity(); super.updateEntity();
if (inProcess) {
if (inProcess && arm != null) {
arm.setArmSpeed(0);
float energyToUse = 2 + powerProvider.getEnergyStored() / 1000; float energyToUse = 2 + powerProvider.getEnergyStored() / 1000;
boolean enoughStep=(0.015 + energyToUse / 200F)>(1F/32F); // (otherwise the movement is rounded to 0 and the energy absorbed with no movement) float energy = powerProvider.useEnergy(energyToUse, energyToUse, true);
if(enoughStep){
float energy = powerProvider.useEnergy(energyToUse, energyToUse, true);
if (energy > 0) { if (energy > 0) {
arm.doMove(0.015 + energy / 200F); moveHead(0.05 + energy / 200F);
}
} }
} }
if (arm != null) {
double[] head = arm.getHead();
headPosX = head[0];
headPosY = head[1];
headPosZ = head[2];
speed = arm.getArmSpeed();
}
if (CoreProxy.proxy.isSimulating(worldObj)) { if (CoreProxy.proxy.isSimulating(worldObj)) {
sendNetworkUpdate(); sendNetworkUpdate();
} }
@ -210,7 +198,6 @@ public class TileQuarry extends TileMachine implements IArmListener, IMachine, I
} }
protected void dig() { protected void dig() {
powerProvider.configure(20, 30, 200, 50, MAX_ENERGY); powerProvider.configure(20, 30, 200, 50, MAX_ENERGY);
if (powerProvider.useEnergy(30, 30, true) != 30) { if (powerProvider.useEnergy(30, 30, true) != 30) {
return; return;
@ -220,14 +207,20 @@ public class TileQuarry extends TileMachine implements IArmListener, IMachine, I
//I believe the issue is box going null becuase of bad chunkloader positioning //I believe the issue is box going null becuase of bad chunkloader positioning
if (arm != null && box != null) if (arm != null && box != null)
arm.setTarget((double)box.xMin + (box.xMax - box.xMin) / 2D, yCoord + 2D, (double)box.zMin + ( box.zMax - box.zMin) / 2D); setTarget(box.xMin + 1, yCoord + 2, box.zMin + 1);
isDigging = false; isDigging = false;
} }
inProcess = true; inProcess = true;
movingHorizontally = true;
movingVertically = true;
double[] head = getHead();
int[] target = getTarget();
headTrajectory = Math.atan2(target[2] -head[2], target[0] -head[0]);
} }
public boolean findTarget(boolean doSet) { public boolean findTarget(boolean doSet) {
if (worldObj.isRemote) if (worldObj.isRemote)
@ -276,8 +269,8 @@ public class TileQuarry extends TileMachine implements IArmListener, IMachine, I
if (isUnquarriableBlock(blockId, bx, by, bz)) { if (isUnquarriableBlock(blockId, bx, by, bz)) {
blockedColumns[searchX][searchZ] = true; blockedColumns[searchX][searchZ] = true;
} else if (isQuarriableBlock(blockId, bx, by + 1, bz)) { } else if (isQuarriableBlock(blockId, bx, by + 1, bz)) {
if (doSet && arm != null) { if (doSet) {
arm.setTarget(bx, by + 1, bz); setTarget(bx, by + 1, bz);
} }
return true; return true;
@ -345,19 +338,14 @@ public class TileQuarry extends TileMachine implements IArmListener, IMachine, I
nbttagcompound.setTag("box", boxTag); nbttagcompound.setTag("box", boxTag);
} }
@Override public void positionReached() {
public void positionReached(EntityMechanicalArm arm) {
inProcess = false; inProcess = false;
System.out.println("PositionReached!" + Thread.currentThread());
if (worldObj.isRemote) { if (worldObj.isRemote) {
return; return;
} }
double[] targ = arm.getTarget();
targetX = (int)targ[0];
targetY = (int)targ[1];
targetZ = (int)targ[2];
int i = targetX; int i = targetX;
int j = targetY - 1; int j = targetY - 1;
int k = targetZ; int k = targetZ;
@ -379,12 +367,13 @@ public class TileQuarry extends TileMachine implements IArmListener, IMachine, I
} }
} }
worldObj.playAuxSFXAtEntity(null, 2001, i, j, k, blockId + (worldObj.getBlockMetadata(i, j, k) << 12));
worldObj.setBlockWithNotify(i, j, k, 0); worldObj.setBlockWithNotify(i, j, k, 0);
} }
// Collect any lost items laying around // Collect any lost items laying around
double[] armHead = arm.getHead(); double[] head = getHead();
AxisAlignedBB axis = AxisAlignedBB.getBoundingBox(armHead[0] - 1.5, armHead[1], armHead[2] - 1.5, armHead[0] + 2.5, armHead[1] + 2.5, armHead[2] + 2.5); AxisAlignedBB axis = AxisAlignedBB.getBoundingBox(head[0] - 1.5, head[1], head[2] - 1.5, head[0] + 2.5, head[1] + 2.5, head[2] + 2.5);
List result = worldObj.getEntitiesWithinAABB(EntityItem.class, axis); List result = worldObj.getEntitiesWithinAABB(EntityItem.class, axis);
for (int ii = 0; ii < result.size(); ii++) { for (int ii = 0; ii < result.size(); ii++) {
if (result.get(ii) instanceof EntityItem) { if (result.get(ii) instanceof EntityItem) {
@ -400,7 +389,7 @@ public class TileQuarry extends TileMachine implements IArmListener, IMachine, I
} }
private void mineStack(ItemStack stack) { private void mineStack(ItemStack stack) {
// First, try to add to a nearby chest // First, try to add to a nearby chest
ItemStack added = Utils.addToRandomInventory(stack, worldObj, xCoord, yCoord, zCoord, Orientations.Unknown); ItemStack added = Utils.addToRandomInventory(stack, worldObj, xCoord, yCoord, zCoord, Orientations.Unknown);
stack.stackSize -= added.stackSize; stack.stackSize -= added.stackSize;
@ -576,11 +565,10 @@ public class TileQuarry extends TileMachine implements IArmListener, IMachine, I
super.postPacketHandling(packet); super.postPacketHandling(packet);
createUtilsIfNeeded(); createUtilsIfNeeded();
//
if (arm != null) { if (arm != null) {
arm.setHead(headPosX, headPosY, headPosZ); arm.setHead(headPosX, headPosY, headPosZ);
// arm.setTarget(targetX, targetY, targetZ); arm.updatePosition();
// arm.setArmSpeed(speed);
} }
} }
@ -681,4 +669,76 @@ public class TileQuarry extends TileMachine implements IArmListener, IMachine, I
return false; return false;
} }
public void moveHead(double instantSpeed) {
int[] target = getTarget();
double[] head = getHead();
if (movingHorizontally) {
if (Math.abs(target[0] - head[0]) < instantSpeed * 2 && Math.abs(target[2] - head[2]) < instantSpeed * 2) {
head[0] = target[0];
head[2] = target[2];
movingHorizontally = false;
if (!movingVertically) {
positionReached();
head[1] = target[1];
}
} else {
head[0] += Math.cos(headTrajectory) * instantSpeed;
head[2] += Math.sin(headTrajectory) * instantSpeed;
}
setHead(head[0], head[1], head[2]);
}
if (movingVertically) {
if (Math.abs(target[1] - head[1]) < instantSpeed * 2) {
head[1] = target[1];
movingVertically = false;
if (!movingHorizontally) {
positionReached();
head[0] = target[0];
head[2] = target[2];
}
} else {
if (target[1] > head[1]) {
head[1] += instantSpeed;
} else {
head[1] -= instantSpeed;
}
}
setHead(head[0],head[1],head[2]);
}
updatePosition();
}
private void updatePosition() {
if (arm!=null && worldObj.isRemote)
{
arm.setHead(headPosX, headPosY, headPosZ);
arm.updatePosition();
}
}
private void setHead(double x, double y, double z) {
this.headPosX = x;
this.headPosY = y;
this.headPosZ = z;
}
private double[] getHead() {
return new double[] { headPosX, headPosY, headPosZ };
}
private int[] getTarget() {
return new int[] { targetX, targetY, targetZ };
}
private void setTarget(int x, int y, int z) {
this.targetX = x;
this.targetY = y;
this.targetZ = z;
}
} }