diff --git a/common/net/minecraft/src/BuildCraftCore.java b/common/net/minecraft/src/BuildCraftCore.java index 28e388b2..0335fb4b 100644 --- a/common/net/minecraft/src/BuildCraftCore.java +++ b/common/net/minecraft/src/BuildCraftCore.java @@ -28,8 +28,6 @@ import net.minecraft.src.buildcraft.core.CoreProxy; import net.minecraft.src.buildcraft.core.DefaultActionProvider; import net.minecraft.src.buildcraft.core.DefaultProps; import net.minecraft.src.buildcraft.core.DefaultTriggerProvider; -import net.minecraft.src.buildcraft.core.EntityEnergyLaser; -import net.minecraft.src.buildcraft.core.EntityRobot; import net.minecraft.src.buildcraft.core.ItemBuildCraft; import net.minecraft.src.buildcraft.core.ItemWrench; import net.minecraft.src.buildcraft.core.RedstonePowerFramework; @@ -37,7 +35,6 @@ import net.minecraft.src.buildcraft.core.TriggerInventory; import net.minecraft.src.buildcraft.core.TriggerLiquidContainer; import net.minecraft.src.buildcraft.core.TriggerMachine; import net.minecraft.src.buildcraft.core.network.ConnectionHandler; -import net.minecraft.src.buildcraft.core.network.EntityIds; import net.minecraft.src.buildcraft.core.network.PacketUpdate; import net.minecraft.src.buildcraft.transport.TriggerRedstoneInput; import net.minecraft.src.forge.Configuration; @@ -127,11 +124,6 @@ public class BuildCraftCore { public static void load() { MinecraftForge.registerConnectionHandler(new ConnectionHandler()); - - //MinecraftForge.registerEntity(EntityBlock.class, mod_BuildCraftCore.instance, EntityIds.BLOCK, 64, 10, true); - MinecraftForge.registerEntity(EntityRobot.class, mod_BuildCraftCore.instance, EntityIds.ROBOT, 64, 1, true); - //MinecraftForge.registerEntity(EntityLaser.class, mod_BuildCraftCore.instance, EntityIds.LASER, 64, 1, false); - MinecraftForge.registerEntity(EntityEnergyLaser.class, mod_BuildCraftCore.instance, EntityIds.LASER, 64, 1, false); } public static void initialize() { diff --git a/common/net/minecraft/src/buildcraft/api/PowerProvider.java b/common/net/minecraft/src/buildcraft/api/PowerProvider.java index 0a693d3a..fa1686ef 100644 --- a/common/net/minecraft/src/buildcraft/api/PowerProvider.java +++ b/common/net/minecraft/src/buildcraft/api/PowerProvider.java @@ -18,9 +18,8 @@ public abstract class PowerProvider { public int minEnergyReceived; public int maxEnergyReceived; public int maxEnergyStored; - public int minActivationEnergy; - public @TileNetworkData - float energyStored = 0; + public @TileNetworkData int minActivationEnergy; + public @TileNetworkData float energyStored = 0; protected int powerLoss = 1; protected int powerLossRegularity = 100; diff --git a/common/net/minecraft/src/buildcraft/core/EntityLaser.java b/common/net/minecraft/src/buildcraft/core/EntityLaser.java index b991668b..ed95e05b 100644 --- a/common/net/minecraft/src/buildcraft/core/EntityLaser.java +++ b/common/net/minecraft/src/buildcraft/core/EntityLaser.java @@ -133,8 +133,6 @@ public class EntityLaser extends Entity implements ISpawnHandler { @Override public void setPosition(double x, double y, double z) { - //System.out.println(new Position(x, y, z)); - posX = x; posY = y; posZ = z; diff --git a/common/net/minecraft/src/buildcraft/core/EntityRobot.java b/common/net/minecraft/src/buildcraft/core/EntityRobot.java index 4031fe5d..824c9aaf 100644 --- a/common/net/minecraft/src/buildcraft/core/EntityRobot.java +++ b/common/net/minecraft/src/buildcraft/core/EntityRobot.java @@ -1,307 +1,296 @@ -/** - * Copyright (c) SpaceToad, 2011-2012 - * 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 net.minecraft.src.buildcraft.core; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; -import java.math.BigDecimal; -import java.util.ArrayList; -import java.util.LinkedList; -import java.util.List; - -import net.minecraft.src.Entity; -import net.minecraft.src.ModLoader; -import net.minecraft.src.NBTTagCompound; -import net.minecraft.src.World; -import net.minecraft.src.buildcraft.api.APIProxy; -import net.minecraft.src.buildcraft.api.BptSlotInfo; -import net.minecraft.src.buildcraft.api.BuildCraftAPI; -import net.minecraft.src.buildcraft.api.Position; -import net.minecraft.src.buildcraft.core.BptSlot.Mode; -import net.minecraft.src.forge.ISpawnHandler; - -public class EntityRobot extends Entity implements ISpawnHandler { - - private Box box; - private int destX, destY, destZ; - - EntityEnergyLaser laser; - - public LinkedList targets = new LinkedList(); - public static int MAX_TARGETS = 20; - public int wait = 0; - - private class Action { - - public Action(BptSlot slot, BptContext context) { - this.slot = slot; - this.context = context; - } - - public Action(BptBuilderBase builder) { - this.builder = builder; - } - - BptSlot slot; - BptBuilderBase builder; - BptContext context; - } - - public EntityRobot(World world) { - super(world); - } - - public EntityRobot(World world, Box box) { - - super(world); - - this.box = box; - init(); - } - - protected void init() { - - destX = (int) box.centerX(); - destY = (int) box.centerY(); - destZ = (int) box.centerZ(); - - motionX = 0; - motionY = 0; - motionZ = 0; - - setPosition(destX, destY, destZ); - - if (!APIProxy.isClient(worldObj)) { - - laser = new EntityEnergyLaser(worldObj, new Position(posX, posY, posZ), new Position(posX, posY, posZ)); - worldObj.spawnEntityInWorld(laser); - } - } - - @Override - public void writeSpawnData(DataOutputStream data) throws IOException { - - data.writeInt(box.xMin); - data.writeInt(box.yMin); - data.writeInt(box.zMin); - data.writeInt(box.xMax); - data.writeInt(box.yMax); - data.writeInt(box.zMax); - } - - @Override - public void readSpawnData(DataInputStream data) throws IOException { - - box = new Box(); - box.xMin = data.readInt(); - box.yMin = data.readInt(); - box.zMin = data.readInt(); - box.xMax = data.readInt(); - box.yMax = data.readInt(); - box.zMax = data.readInt(); - - init(); - } - - @Override - protected void entityInit() {} - - @Override - protected void readEntityFromNBT(NBTTagCompound nbttagcompound) {} - - @Override - protected void writeEntityToNBT(NBTTagCompound nbttagcompound) {} - - @Override - public void onUpdate() { - - move(); - build(); - updateLaser(); - } - - protected void move() { - - //System.out.println("move: " + new Position(motionX, motionY, motionZ)); - setPosition(posX + motionX, posY + motionY, posZ + motionZ); - - if (APIProxy.isClient(worldObj)) - return; - - if (reachedDesination()) { - - BlockIndex newDesination = getNewDesination(); - if (newDesination != null) { - setDestination(newDesination.i, newDesination.j, newDesination.k); - } - - } - - } - - protected BlockIndex getNewDesination() { - - Box movementBoundary = new Box(); - movementBoundary.initialize(box); - movementBoundary.expand(1); - - Box moveArea = new Box(); - moveArea.initialize(destX, destY, destZ, 1); - - List potentialDestinations = new ArrayList(); - for (BlockIndex blockIndex : moveArea.getBlocksInArea()) { - - if (BuildCraftAPI.softBlock(blockIndex.getBlockId(worldObj)) && movementBoundary.contains(blockIndex)) { - potentialDestinations.add(blockIndex); - } - } - - if (!potentialDestinations.isEmpty()) { - - int i = worldObj.rand.nextInt(potentialDestinations.size()); - return potentialDestinations.get(i); - } - - return null; - } - - protected void setDestination(int x, int y, int z) { - - destX = x; - destY = y; - destZ = z; - - // TODO: apply power modifier - motionX = new BigDecimal((float) (destX - posX) / 75 * 1).setScale(4, BigDecimal.ROUND_HALF_EVEN).doubleValue(); - motionY = new BigDecimal((float) (destY - posY) / 75 * 1).setScale(4, BigDecimal.ROUND_HALF_EVEN).doubleValue(); - motionZ = new BigDecimal((float) (destZ - posZ) / 75 * 1).setScale(4, BigDecimal.ROUND_HALF_EVEN).doubleValue(); - - //System.out.println("setDest: " + new Position(motionX, motionY, motionZ)); - } - - protected boolean reachedDesination() { - - if (getDistance(destX, destY, destZ) <= .2) - return true; - - return false; - } - - protected void build() { - - updateWait(); - - // TODO: possible rewrite - if (targets.size() > 0) { - - Action a = targets.getFirst(); - if (a.slot != null) { - - BptSlot target = a.slot; - if (wait <= 0) { - - if (target.mode == Mode.ClearIfInvalid) { - - if (!target.isValid(a.context)) - worldObj.setBlockAndMetadataWithNotify(target.x, target.y, target.z, 0, 0); - - } else if (target.stackToUse != null) { - - worldObj.setBlockWithNotify(target.x, target.y, target.z, 0); - target.stackToUse.getItem().onItemUse(target.stackToUse, - BuildCraftAPI.getBuildCraftPlayer(worldObj), worldObj, target.x, target.y - 1, - target.z, 1); - } else { - - try { - target.buildBlock(a.context); - } catch (Throwable t) { - // Defensive code against errors in implementers - t.printStackTrace(); - ModLoader.getLogger().throwing("EntityRobot", "update", t); - } - } - - targets.pop(); - } - - } else if (a.builder != null) { - a.builder.postProcessing(worldObj); - targets.pop(); - } - } - } - - public void updateWait() { - - if (targets.size() > 0) - if (wait == 0) - wait = MAX_TARGETS - targets.size() + 2; - else - wait--; - } - - private void updateLaser() { - - if (APIProxy.isClient(worldObj)) - return; - - if (targets.size() > 0) { - - Action a = targets.getFirst(); - BptSlotInfo target = a.slot; - - laser.setPositions(new Position(posX, posY, posZ), new Position(target.x + 0.5, target.y + 0.5, target.z + 0.5)); - laser.show(); - } - else { - laser.hide(); - } - - laser.pushPower(((float) targets.size()) / ((float) MAX_TARGETS) * 4F); - } - - public void scheduleContruction(BptSlot slot, BptContext context) { - - if (slot != null) { - targets.add(new Action(slot, context)); - - } - } - - public void markEndOfBlueprint(BptBuilderBase builder) { - targets.add(new Action(builder)); - } - - public boolean readyToBuild() { - return targets.size() < MAX_TARGETS; - } - - public boolean done() { - return targets.isEmpty(); - } - - public void setBox(Box box) { - - this.box = box; - setDestination((int) box.centerX(), (int) box.centerY(), (int) box.centerZ()); - } - - @Override - public void setDead() { - - if (laser != null) - laser.setDead(); - - super.setDead(); - } - -} +/** + * Copyright (c) SpaceToad, 2011-2012 + * 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 net.minecraft.src.buildcraft.core; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; + +import net.minecraft.src.Entity; +import net.minecraft.src.ModLoader; +import net.minecraft.src.NBTTagCompound; +import net.minecraft.src.World; +import net.minecraft.src.buildcraft.api.APIProxy; +import net.minecraft.src.buildcraft.api.BptSlotInfo; +import net.minecraft.src.buildcraft.api.BuildCraftAPI; +import net.minecraft.src.buildcraft.api.Position; +import net.minecraft.src.buildcraft.core.BptSlot.Mode; +import net.minecraft.src.forge.ISpawnHandler; + +public class EntityRobot extends Entity implements ISpawnHandler { + + private Box box; + private int destX, destY, destZ; + + EntityEnergyLaser laser; + + public LinkedList targets = new LinkedList(); + public static int MAX_TARGETS = 20; + public int wait = 0; + + private class Action { + + public Action(BptSlot slot, BptContext context) { + this.slot = slot; + this.context = context; + } + + public Action(BptBuilderBase builder) { + this.builder = builder; + } + + BptSlot slot; + BptBuilderBase builder; + BptContext context; + } + + public EntityRobot(World world) { + super(world); + } + + public EntityRobot(World world, Box box) { + + super(world); + + this.box = box; + init(); + } + + protected void init() { + + destX = (int) box.centerX(); + destY = (int) box.centerY(); + destZ = (int) box.centerZ(); + + motionX = 0; + motionY = 0; + motionZ = 0; + + setPosition(destX, destY, destZ); + + laser = new EntityEnergyLaser(worldObj, new Position(posX, posY, posZ), new Position(posX, posY, posZ)); + worldObj.spawnEntityInWorld(laser); + } + + @Override + public void writeSpawnData(DataOutputStream data) throws IOException { + + data.writeInt(box.xMin); + data.writeInt(box.yMin); + data.writeInt(box.zMin); + data.writeInt(box.xMax); + data.writeInt(box.yMax); + data.writeInt(box.zMax); + } + + @Override + public void readSpawnData(DataInputStream data) throws IOException { + + box = new Box(); + box.xMin = data.readInt(); + box.yMin = data.readInt(); + box.zMin = data.readInt(); + box.xMax = data.readInt(); + box.yMax = data.readInt(); + box.zMax = data.readInt(); + + init(); + } + + @Override + protected void entityInit() {} + + @Override + protected void readEntityFromNBT(NBTTagCompound nbttagcompound) {} + + @Override + protected void writeEntityToNBT(NBTTagCompound nbttagcompound) {} + + @Override + public void onUpdate() { + + move(); + build(); + updateLaser(); + } + + protected void move() { + + //System.out.println("move: " + new Position(motionX, motionY, motionZ)); + setPosition(posX + motionX, posY + motionY, posZ + motionZ); + + if (reachedDesination()) { + + BlockIndex newDesination = getNewDesination(); + if (newDesination != null) { + setDestination(newDesination.i, newDesination.j, newDesination.k); + } + + } + + } + + protected BlockIndex getNewDesination() { + + Box movementBoundary = new Box(); + movementBoundary.initialize(box); + movementBoundary.expand(1); + + Box moveArea = new Box(); + moveArea.initialize(destX, destY, destZ, 1); + + List potentialDestinations = new ArrayList(); + for (BlockIndex blockIndex : moveArea.getBlocksInArea()) { + + if (BuildCraftAPI.softBlock(blockIndex.getBlockId(worldObj)) && movementBoundary.contains(blockIndex)) { + potentialDestinations.add(blockIndex); + } + } + + if (!potentialDestinations.isEmpty()) { + + int i = worldObj.rand.nextInt(potentialDestinations.size()); + return potentialDestinations.get(i); + } + + return null; + } + + protected void setDestination(int x, int y, int z) { + + destX = x; + destY = y; + destZ = z; + + motionX = (destX - posX) / 75 * laser.getPowerAverage() / 2; + motionY = (destY - posY) / 75 * laser.getPowerAverage() / 2; + motionZ = (destZ - posZ) / 75 * laser.getPowerAverage() / 2; + } + + protected boolean reachedDesination() { + + if (getDistance(destX, destY, destZ) <= .2) + return true; + + return false; + } + + protected void build() { + + updateWait(); + + if (targets.size() > 0) { + + Action a = targets.getFirst(); + if (a.slot != null) { + + BptSlot target = a.slot; + if (wait <= 0) { + + if (!APIProxy.isClient(worldObj)) { + + if (target.mode == Mode.ClearIfInvalid) { + + if (!target.isValid(a.context)) + worldObj.setBlockAndMetadataWithNotify(target.x, target.y, target.z, 0, 0); + + } else if (target.stackToUse != null) { + + worldObj.setBlockWithNotify(target.x, target.y, target.z, 0); + target.stackToUse.getItem().onItemUse(target.stackToUse, + BuildCraftAPI.getBuildCraftPlayer(worldObj), worldObj, target.x, target.y - 1, + target.z, 1); + } else { + + try { + target.buildBlock(a.context); + } catch (Throwable t) { + // Defensive code against errors in implementers + t.printStackTrace(); + ModLoader.getLogger().throwing("EntityRobot", "update", t); + } + } + } + + targets.pop(); + } + + } else if (a.builder != null) { + a.builder.postProcessing(worldObj); + targets.pop(); + } + } + } + + public void updateWait() { + + if (targets.size() > 0) + if (wait == 0) + wait = MAX_TARGETS - targets.size() + 2; + else + wait--; + } + + private void updateLaser() { + + if (targets.size() > 0) { + + Action a = targets.getFirst(); + BptSlotInfo target = a.slot; + + laser.setPositions(new Position(posX, posY, posZ), new Position(target.x + 0.5, target.y + 0.5, target.z + 0.5)); + laser.show(); + } + else { + laser.hide(); + } + + laser.pushPower(((float) targets.size()) / ((float) MAX_TARGETS) * 4F); + } + + public void scheduleContruction(BptSlot slot, BptContext context) { + + if (slot != null) { + targets.add(new Action(slot, context)); + + } + } + + public void markEndOfBlueprint(BptBuilderBase builder) { + targets.add(new Action(builder)); + } + + public boolean readyToBuild() { + return targets.size() < MAX_TARGETS; + } + + public boolean done() { + return targets.isEmpty(); + } + + public void setBox(Box box) { + + this.box = box; + setDestination((int) box.centerX(), (int) box.centerY(), (int) box.centerZ()); + } + + @Override + public void setDead() { + + if (laser != null) + laser.setDead(); + + super.setDead(); + } + +} diff --git a/common/net/minecraft/src/buildcraft/factory/EntityMechanicalArm.java b/common/net/minecraft/src/buildcraft/factory/EntityMechanicalArm.java index 1647bf67..3daa47c3 100644 --- a/common/net/minecraft/src/buildcraft/factory/EntityMechanicalArm.java +++ b/common/net/minecraft/src/buildcraft/factory/EntityMechanicalArm.java @@ -1,287 +1,288 @@ -/** - * 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 net.minecraft.src.buildcraft.factory; - -import net.minecraft.src.BuildCraftFactory; -import net.minecraft.src.Entity; -import net.minecraft.src.NBTTagCompound; -import net.minecraft.src.TileEntity; -import net.minecraft.src.World; -import net.minecraft.src.buildcraft.core.EntityBlock; - -public class EntityMechanicalArm extends Entity { - - double sizeX, sizeZ; - EntityBlock xArm, yArm, zArm, head; - - double angle; - - public double targetX, targetY, targetZ; - public double headPosX, headPosY, headPosZ; - public double speed = 0.03; - - double baseY; - - public IArmListener listener; - boolean inProgressionXZ = false; - boolean inProgressionY = false; - - protected TileEntity parent; - - public EntityMechanicalArm(World world) { - super(world); - } - - public EntityMechanicalArm(World world, double i, double j, double k, double width, double height, TileEntity parent) { - super(world); - - setPosition(i, j, k); - - motionX = 0.0D; - motionY = 0.0D; - motionZ = 0.0D; - prevPosX = i; - prevPosY = j; - prevPosZ = k; - sizeX = height; - sizeZ = width; - noClip = true; - baseY = j; - - headPosX = i; - headPosY = j - 2; - headPosZ = k; - - setTarget(headPosX, headPosY, headPosZ); - inProgressionXZ = false; - inProgressionY = false; - - xArm = new EntityBlock(world, i, j, k, width, 0.5, 0.5); - xArm.texture = BuildCraftFactory.drillTexture; - world.spawnEntityInWorld(xArm); - - yArm = new EntityBlock(world, i, j, k, 0.5, 1, 0.5); - yArm.texture = BuildCraftFactory.drillTexture; - world.spawnEntityInWorld(yArm); - - zArm = new EntityBlock(world, i, j, k, 0.5, 0.5, height); - zArm.texture = BuildCraftFactory.drillTexture; - world.spawnEntityInWorld(zArm); - - head = new EntityBlock(world, i, j, k, 0.2, 1, 0.2); - head.texture = 2 * 16 + 10; - world.spawnEntityInWorld(head); - head.shadowSize = 1.0F; - - updatePosition(); - - this.parent = parent; - } - - @Override - protected void entityInit() {} - - @Override - protected void readEntityFromNBT(NBTTagCompound nbttagcompound) { - sizeX = nbttagcompound.getDouble("sizeX"); - sizeZ = nbttagcompound.getDouble("sizeZ"); - - targetX = nbttagcompound.getDouble("targetX"); - targetY = nbttagcompound.getDouble("targetY"); - targetZ = nbttagcompound.getDouble("targetZ"); - angle = nbttagcompound.getDouble("angle"); - - headPosX = nbttagcompound.getDouble("headPosX"); - headPosY = nbttagcompound.getDouble("headPosY"); - headPosZ = nbttagcompound.getDouble("headPosZ"); - - baseY = nbttagcompound.getDouble("baseY"); - speed = nbttagcompound.getDouble("speed"); - - inProgressionXZ = nbttagcompound.getBoolean("progressionXY"); - inProgressionY = nbttagcompound.getBoolean("progressionY"); - - NBTTagCompound xArmStore, yArmStore, zArmStore, headStore; - - xArmStore = nbttagcompound.getCompoundTag("xArm"); - yArmStore = nbttagcompound.getCompoundTag("yArm"); - zArmStore = nbttagcompound.getCompoundTag("zArm"); - headStore = nbttagcompound.getCompoundTag("head"); - - xArm = new EntityBlock(worldObj); - yArm = new EntityBlock(worldObj); - zArm = new EntityBlock(worldObj); - head = new EntityBlock(worldObj); - - xArm.texture = BuildCraftFactory.drillTexture; - yArm.texture = BuildCraftFactory.drillTexture; - zArm.texture = BuildCraftFactory.drillTexture; - head.texture = 2 * 16 + 10; - - xArm.readFromNBT(xArmStore); - yArm.readFromNBT(yArmStore); - zArm.readFromNBT(zArmStore); - head.readFromNBT(headStore); - } - - @Override - protected void writeEntityToNBT(NBTTagCompound nbttagcompound) { - nbttagcompound.setDouble("sizeX", sizeX); - nbttagcompound.setDouble("sizeZ", sizeZ); - - nbttagcompound.setDouble("targetX", targetX); - nbttagcompound.setDouble("targetY", targetY); - nbttagcompound.setDouble("targetZ", targetZ); - nbttagcompound.setDouble("angle", angle); - - nbttagcompound.setDouble("headPosX", headPosX); - nbttagcompound.setDouble("headPosY", headPosY); - nbttagcompound.setDouble("headPosZ", headPosZ); - - nbttagcompound.setDouble("baseY", baseY); - nbttagcompound.setDouble("speed", speed); - - nbttagcompound.setBoolean("progressionXY", inProgressionXZ); - nbttagcompound.setBoolean("progressionY", inProgressionY); - - NBTTagCompound xArmStore, yArmStore, zArmStore, headStore; - - xArmStore = new NBTTagCompound(); - yArmStore = new NBTTagCompound(); - zArmStore = new NBTTagCompound(); - headStore = new NBTTagCompound(); - - nbttagcompound.setTag("xArm", xArmStore); - nbttagcompound.setTag("yArm", yArmStore); - nbttagcompound.setTag("zArm", zArmStore); - nbttagcompound.setTag("head", headStore); - - xArm.writeToNBT(xArmStore); - yArm.writeToNBT(yArmStore); - zArm.writeToNBT(zArmStore); - head.writeToNBT(headStore); - } - - public void setTarget(double x, double y, double z) { - targetX = x; - targetY = y; - targetZ = z; - - double dX = targetX - headPosX; - double dZ = targetZ - headPosZ; - - angle = Math.atan2(dZ, dX); - - inProgressionXZ = true; - inProgressionY = true; - } - - public double[] getTarget() { - return new double[] { targetX, targetY, targetZ }; - } - - @Override - public void onUpdate() { - - if (parent != null && worldObj.getBlockTileEntity(parent.xCoord, parent.yCoord, parent.zCoord) != parent) { - setDead(); - return; - } - - if (speed > 0) { - doMove(speed); - } - } - - public void doMove(double instantSpeed) { - super.onUpdate(); - - if (inProgressionXZ) { - if (Math.abs(targetX - headPosX) < instantSpeed * 2 && Math.abs(targetZ - headPosZ) < instantSpeed * 2) { - headPosX = targetX; - headPosZ = targetZ; - - inProgressionXZ = false; - - if (listener != null && !inProgressionY) { - listener.positionReached(this); - } - } else { - headPosX += Math.cos(angle) * instantSpeed; - headPosZ += Math.sin(angle) * instantSpeed; - } - } - - if (inProgressionY) { - if (Math.abs(targetY - headPosY) < instantSpeed * 2) { - headPosY = targetY; - - inProgressionY = false; - - if (listener != null && !inProgressionXZ) { - listener.positionReached(this); - } - } else { - if (targetY > headPosY) { - headPosY += instantSpeed / 2; - } else { - headPosY -= instantSpeed / 2; - } - } - - } - - updatePosition(); - } - - public void updatePosition() { - xArm.setPosition(xArm.posX, xArm.posY, headPosZ + 0.25); - yArm.jSize = baseY - headPosY - 1; - yArm.setPosition(headPosX + 0.25, headPosY + 1, headPosZ + 0.25); - zArm.setPosition(headPosX + 0.25, zArm.posY, zArm.posZ); - - head.setPosition(headPosX + 0.4, headPosY, headPosZ + 0.4); - } - - public void joinToWorld(World w) { - super.worldObj = w; - - xArm.worldObj = w; - yArm.worldObj = w; - zArm.worldObj = w; - head.worldObj = w; - - w.spawnEntityInWorld(this); - w.spawnEntityInWorld(xArm); - w.spawnEntityInWorld(yArm); - w.spawnEntityInWorld(zArm); - w.spawnEntityInWorld(head); - } - - @Override - public void setDead() { - xArm.setDead(); - yArm.setDead(); - zArm.setDead(); - head.setDead(); - super.setDead(); - - } - - public double[] getHeadPosition() { - return new double[] { headPosX, headPosY, headPosZ }; - } - - public void setHeadPosition(double x, double y, double z) { - headPosX = x; - headPosY = y; - headPosZ = z; - } -} +/** + * 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 net.minecraft.src.buildcraft.factory; + +import net.minecraft.src.BuildCraftFactory; +import net.minecraft.src.Entity; +import net.minecraft.src.NBTTagCompound; +import net.minecraft.src.TileEntity; +import net.minecraft.src.World; +import net.minecraft.src.buildcraft.core.EntityBlock; + +public class EntityMechanicalArm extends Entity { + + double sizeX, sizeZ; + EntityBlock xArm, yArm, zArm, head; + + double angle; + + public double targetX, targetY, targetZ; + public double headPosX, headPosY, headPosZ; + public double speed = 0.03; + + double baseY; + + public IArmListener listener; + boolean inProgressionXZ = false; + boolean inProgressionY = false; + + protected TileEntity parent; + + public EntityMechanicalArm(World world) { + super(world); + } + + public EntityMechanicalArm(World world, double i, double j, double k, double width, double height, TileEntity parent) { + super(world); + + setPosition(i, j, k); + + motionX = 0.0D; + motionY = 0.0D; + motionZ = 0.0D; + prevPosX = i; + prevPosY = j; + prevPosZ = k; + sizeX = height; + sizeZ = width; + noClip = true; + baseY = j; + + headPosX = i; + headPosY = j - 2; + headPosZ = k; + + setTarget(headPosX, headPosY, headPosZ); + inProgressionXZ = false; + inProgressionY = false; + + xArm = new EntityBlock(world, i, j, k, width, 0.5, 0.5); + xArm.texture = BuildCraftFactory.drillTexture; + world.spawnEntityInWorld(xArm); + + yArm = new EntityBlock(world, i, j, k, 0.5, 1, 0.5); + yArm.texture = BuildCraftFactory.drillTexture; + world.spawnEntityInWorld(yArm); + + zArm = new EntityBlock(world, i, j, k, 0.5, 0.5, height); + zArm.texture = BuildCraftFactory.drillTexture; + world.spawnEntityInWorld(zArm); + + head = new EntityBlock(world, i, j, k, 0.2, 1, 0.2); + head.texture = 2 * 16 + 10; + world.spawnEntityInWorld(head); + head.shadowSize = 1.0F; + + updatePosition(); + + this.parent = parent; + } + + @Override + protected void entityInit() {} + + @Override + protected void readEntityFromNBT(NBTTagCompound nbttagcompound) { + sizeX = nbttagcompound.getDouble("sizeX"); + sizeZ = nbttagcompound.getDouble("sizeZ"); + + targetX = nbttagcompound.getDouble("targetX"); + targetY = nbttagcompound.getDouble("targetY"); + targetZ = nbttagcompound.getDouble("targetZ"); + angle = nbttagcompound.getDouble("angle"); + + headPosX = nbttagcompound.getDouble("headPosX"); + headPosY = nbttagcompound.getDouble("headPosY"); + headPosZ = nbttagcompound.getDouble("headPosZ"); + + baseY = nbttagcompound.getDouble("baseY"); + speed = nbttagcompound.getDouble("speed"); + + inProgressionXZ = nbttagcompound.getBoolean("progressionXY"); + inProgressionY = nbttagcompound.getBoolean("progressionY"); + + NBTTagCompound xArmStore, yArmStore, zArmStore, headStore; + + xArmStore = nbttagcompound.getCompoundTag("xArm"); + yArmStore = nbttagcompound.getCompoundTag("yArm"); + zArmStore = nbttagcompound.getCompoundTag("zArm"); + headStore = nbttagcompound.getCompoundTag("head"); + + xArm = new EntityBlock(worldObj); + yArm = new EntityBlock(worldObj); + zArm = new EntityBlock(worldObj); + head = new EntityBlock(worldObj); + + xArm.texture = BuildCraftFactory.drillTexture; + yArm.texture = BuildCraftFactory.drillTexture; + zArm.texture = BuildCraftFactory.drillTexture; + head.texture = 2 * 16 + 10; + + xArm.readFromNBT(xArmStore); + yArm.readFromNBT(yArmStore); + zArm.readFromNBT(zArmStore); + head.readFromNBT(headStore); + } + + @Override + protected void writeEntityToNBT(NBTTagCompound nbttagcompound) { + nbttagcompound.setDouble("sizeX", sizeX); + nbttagcompound.setDouble("sizeZ", sizeZ); + + nbttagcompound.setDouble("targetX", targetX); + nbttagcompound.setDouble("targetY", targetY); + nbttagcompound.setDouble("targetZ", targetZ); + nbttagcompound.setDouble("angle", angle); + + nbttagcompound.setDouble("headPosX", headPosX); + nbttagcompound.setDouble("headPosY", headPosY); + nbttagcompound.setDouble("headPosZ", headPosZ); + + nbttagcompound.setDouble("baseY", baseY); + nbttagcompound.setDouble("speed", speed); + + nbttagcompound.setBoolean("progressionXY", inProgressionXZ); + nbttagcompound.setBoolean("progressionY", inProgressionY); + + NBTTagCompound xArmStore, yArmStore, zArmStore, headStore; + + xArmStore = new NBTTagCompound(); + yArmStore = new NBTTagCompound(); + zArmStore = new NBTTagCompound(); + headStore = new NBTTagCompound(); + + nbttagcompound.setTag("xArm", xArmStore); + nbttagcompound.setTag("yArm", yArmStore); + nbttagcompound.setTag("zArm", zArmStore); + nbttagcompound.setTag("head", headStore); + + xArm.writeToNBT(xArmStore); + yArm.writeToNBT(yArmStore); + zArm.writeToNBT(zArmStore); + head.writeToNBT(headStore); + } + + public void setTarget(double x, double y, double z) { + + targetX = x; + targetY = y; + targetZ = z; + + double dX = targetX - headPosX; + double dZ = targetZ - headPosZ; + + angle = Math.atan2(dZ, dX); + + inProgressionXZ = true; + inProgressionY = true; + } + + public double[] getTarget() { + return new double[] { targetX, targetY, targetZ }; + } + + @Override + public void onUpdate() { + + if (parent != null && worldObj.getBlockTileEntity(parent.xCoord, parent.yCoord, parent.zCoord) != parent) { + setDead(); + return; + } + + if (speed > 0) { + doMove(speed); + } + } + + public void doMove(double instantSpeed) { + super.onUpdate(); + + if (inProgressionXZ) { + if (Math.abs(targetX - headPosX) < instantSpeed * 2 && Math.abs(targetZ - headPosZ) < instantSpeed * 2) { + headPosX = targetX; + headPosZ = targetZ; + + inProgressionXZ = false; + + if (listener != null && !inProgressionY) { + listener.positionReached(this); + } + } else { + headPosX += Math.cos(angle) * instantSpeed; + headPosZ += Math.sin(angle) * instantSpeed; + } + } + + if (inProgressionY) { + if (Math.abs(targetY - headPosY) < instantSpeed * 2) { + headPosY = targetY; + + inProgressionY = false; + + if (listener != null && !inProgressionXZ) { + listener.positionReached(this); + } + } else { + if (targetY > headPosY) { + headPosY += instantSpeed / 2; + } else { + headPosY -= instantSpeed / 2; + } + } + + } + + updatePosition(); + } + + public void updatePosition() { + xArm.setPosition(xArm.posX, xArm.posY, headPosZ + 0.25); + yArm.jSize = baseY - headPosY - 1; + yArm.setPosition(headPosX + 0.25, headPosY + 1, headPosZ + 0.25); + zArm.setPosition(headPosX + 0.25, zArm.posY, zArm.posZ); + + head.setPosition(headPosX + 0.4, headPosY, headPosZ + 0.4); + } + + public void joinToWorld(World w) { + super.worldObj = w; + + xArm.worldObj = w; + yArm.worldObj = w; + zArm.worldObj = w; + head.worldObj = w; + + w.spawnEntityInWorld(this); + w.spawnEntityInWorld(xArm); + w.spawnEntityInWorld(yArm); + w.spawnEntityInWorld(zArm); + w.spawnEntityInWorld(head); + } + + @Override + public void setDead() { + xArm.setDead(); + yArm.setDead(); + zArm.setDead(); + head.setDead(); + super.setDead(); + + } + + public double[] getHeadPosition() { + return new double[] { headPosX, headPosY, headPosZ }; + } + + public void setHeadPosition(double x, double y, double z) { + headPosX = x; + headPosY = y; + headPosZ = z; + } +} diff --git a/common/net/minecraft/src/buildcraft/factory/TileQuarry.java b/common/net/minecraft/src/buildcraft/factory/TileQuarry.java index 89617024..b216137e 100644 --- a/common/net/minecraft/src/buildcraft/factory/TileQuarry.java +++ b/common/net/minecraft/src/buildcraft/factory/TileQuarry.java @@ -1,669 +1,690 @@ -/** - * 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 net.minecraft.src.buildcraft.factory; - -import java.util.ArrayList; -import java.util.List; - -import net.minecraft.src.AxisAlignedBB; -import net.minecraft.src.Block; -import net.minecraft.src.BuildCraftBlockUtil; -import net.minecraft.src.BuildCraftFactory; -import net.minecraft.src.EntityItem; -import net.minecraft.src.EntityPlayer; -import net.minecraft.src.ItemStack; -import net.minecraft.src.NBTTagCompound; -import net.minecraft.src.buildcraft.api.APIProxy; -import net.minecraft.src.buildcraft.api.BuildCraftAPI; -import net.minecraft.src.buildcraft.api.IAreaProvider; -import net.minecraft.src.buildcraft.api.IPipeConnection; -import net.minecraft.src.buildcraft.api.IPowerReceptor; -import net.minecraft.src.buildcraft.api.LaserKind; -import net.minecraft.src.buildcraft.api.Orientations; -import net.minecraft.src.buildcraft.api.PowerFramework; -import net.minecraft.src.buildcraft.api.PowerProvider; -import net.minecraft.src.buildcraft.api.TileNetworkData; -import net.minecraft.src.buildcraft.core.Box; -import net.minecraft.src.buildcraft.core.BptBlueprint; -import net.minecraft.src.buildcraft.core.BptBuilderBase; -import net.minecraft.src.buildcraft.core.BptBuilderBlueprint; -import net.minecraft.src.buildcraft.core.DefaultAreaProvider; -import net.minecraft.src.buildcraft.core.EntityRobot; -import net.minecraft.src.buildcraft.core.IBuilderInventory; -import net.minecraft.src.buildcraft.core.IMachine; -import net.minecraft.src.buildcraft.core.StackUtil; -import net.minecraft.src.buildcraft.core.Utils; -import net.minecraft.src.buildcraft.core.network.PacketUpdate; - -public class TileQuarry extends TileMachine implements IArmListener, IMachine, IPowerReceptor, IPipeConnection, IBuilderInventory { - - boolean isDigging = false; - - public @TileNetworkData - Box box = new Box(); - public @TileNetworkData - boolean inProcess = false; - - public EntityMechanicalArm arm; - public @TileNetworkData - int targetX, targetY, targetZ; - public @TileNetworkData - double headPosX, headPosY, headPosZ; - public @TileNetworkData - double speed = 0.03; - - public EntityRobot builder; - public @TileNetworkData - boolean builderDone = false; - - boolean loadArm = false; - - BptBuilderBase bluePrintBuilder; - - public @TileNetworkData - PowerProvider powerProvider; - - public static int MAX_ENERGY = 7000; - - public TileQuarry() { - powerProvider = PowerFramework.currentFramework.createPowerProvider(); - powerProvider.configure(20, 25, 25, 25, MAX_ENERGY); - } - - public void createUtilsIfNeeded() { - - if (!box.isInitialized() && APIProxy.isClient(worldObj)) { - return; - } - - if (bluePrintBuilder == null) { - if (!box.isInitialized()) { - setBoundaries(loadDefaultBoundaries); - } - - initializeBluePrintBuilder(); - } - - if (builderDone) { - box.deleteLasers(); - - if (arm == null) { - createArm(); - } - - if (loadArm) { - arm.joinToWorld(worldObj); - loadArm = false; - - if (findTarget(false)) { - isDigging = true; - } - } - } else { - box.createLasers(worldObj, LaserKind.Stripes); - isDigging = true; - } - } - - private boolean loadDefaultBoundaries = false; - - private void createArm() { - arm = new EntityMechanicalArm(worldObj, box.xMin + Utils.pipeMaxPos, - yCoord + bluePrintBuilder.bluePrint.sizeY - 1 - + Utils.pipeMinPos, box.zMin + Utils.pipeMaxPos, - bluePrintBuilder.bluePrint.sizeX - 2 + Utils.pipeMinPos * 2, - bluePrintBuilder.bluePrint.sizeZ - 2 + Utils.pipeMinPos * 2, this); - - arm.listener = this; - loadArm = true; - } - - @Override - public void updateEntity() { - - super.updateEntity(); - - if (inProcess && arm != null) { - - arm.speed = 0; - float energyToUse = 2 + powerProvider.energyStored / 1000; - - float energy = powerProvider.useEnergy(energyToUse, energyToUse, true); - - if (energy > 0) { - arm.doMove(0.015 + energy / 200F); - } - } - - if (arm != null) { - - headPosX = arm.headPosX; - headPosY = arm.headPosY; - headPosZ = arm.headPosZ; - - speed = arm.speed; - } - } - - @Override - public void doWork() { - - builderDone = bluePrintBuilder.done; - - if (APIProxy.isClient(worldObj)) { - return; - } - - if (inProcess || !isDigging) { - return; - } - - createUtilsIfNeeded(); - - if (bluePrintBuilder != null) { - - if (!builderDone) { - // configuration for building phase - powerProvider.configure(20, 25, 25, 25, MAX_ENERGY); - - if (powerProvider.useEnergy(25, 25, true) != 25) { - return; - } - - powerProvider.timeTracker.markTime(worldObj); - - if (builder == null) { - builder = new EntityRobot(worldObj, box); - worldObj.spawnEntityInWorld(builder); - } - - if (builder.readyToBuild()) { - builder.scheduleContruction(bluePrintBuilder.getNextBlock(worldObj, this), bluePrintBuilder.getContext()); - } - - return; - - } else { - - if (builder != null && builder.done()) { - box.deleteLasers(); - builder.setDead(); - builder = null; - } - } - } - - if (builder == null) { - // configuration for digging phase - powerProvider.configure(20, 30, 200, 50, MAX_ENERGY); - - if (!findTarget(true)) { - arm.setTarget(box.xMin + arm.sizeX / 2, yCoord + 2, box.zMin + arm.sizeX / 2); - - isDigging = false; - } - - inProcess = true; - } - - if (APIProxy.isServerSide()) { - sendNetworkUpdate(); - } - } - - public boolean findTarget(boolean doSet) { - boolean[][] blockedColumns = new boolean[bluePrintBuilder.bluePrint.sizeX - 2][bluePrintBuilder.bluePrint.sizeZ - 2]; - - for (int searchX = 0; searchX < bluePrintBuilder.bluePrint.sizeX - 2; ++searchX) { - for (int searchZ = 0; searchZ < bluePrintBuilder.bluePrint.sizeZ - 2; ++searchZ) { - blockedColumns[searchX][searchZ] = false; - } - } - - for (int searchY = yCoord + 3; searchY >= 0; --searchY) { - int startX, endX, incX; - - if (searchY % 2 == 0) { - startX = 0; - endX = bluePrintBuilder.bluePrint.sizeX - 2; - incX = 1; - } else { - startX = bluePrintBuilder.bluePrint.sizeX - 3; - endX = -1; - incX = -1; - } - - for (int searchX = startX; searchX != endX; searchX += incX) { - int startZ, endZ, incZ; - - if (searchX % 2 == searchY % 2) { - startZ = 0; - endZ = bluePrintBuilder.bluePrint.sizeZ - 2; - incZ = 1; - } else { - startZ = bluePrintBuilder.bluePrint.sizeZ - 3; - endZ = -1; - incZ = -1; - } - - for (int searchZ = startZ; searchZ != endZ; searchZ += incZ) { - if (!blockedColumns[searchX][searchZ]) { - int bx = box.xMin + searchX + 1, by = searchY, bz = box.zMin + searchZ + 1; - - int blockId = worldObj.getBlockId(bx, by, bz); - - if (blockDig(blockId)) { - blockedColumns[searchX][searchZ] = true; - } else if (canDig(blockId)) { - if (doSet) { - arm.setTarget(bx, by + 1, bz); - - targetX = (int) arm.targetX; - targetY = (int) arm.targetY; - targetZ = (int) arm.targetZ; - } - - return true; - } - } - } - } - } - - return false; - } - - @Override - public void readFromNBT(NBTTagCompound nbttagcompound) { - super.readFromNBT(nbttagcompound); - - PowerFramework.currentFramework.loadPowerProvider(this, nbttagcompound); - - if (nbttagcompound.hasKey("box")) { - box.initialize(nbttagcompound.getCompoundTag("box")); - - loadDefaultBoundaries = false; - } else if (nbttagcompound.hasKey("xSize")) { - // This is a legacy save, get old data - - int xMin = nbttagcompound.getInteger("xMin"); - int zMin = nbttagcompound.getInteger("zMin"); - - int xSize = nbttagcompound.getInteger("xSize"); - int ySize = nbttagcompound.getInteger("ySize"); - int zSize = nbttagcompound.getInteger("zSize"); - - box.initialize(xMin, yCoord, zMin, xMin + xSize - 1, yCoord + ySize - 1, zMin + zSize - 1); - - loadDefaultBoundaries = false; - } else { - // This is a legacy save, compute boundaries - - loadDefaultBoundaries = true; - } - - targetX = nbttagcompound.getInteger("targetX"); - targetY = nbttagcompound.getInteger("targetY"); - targetZ = nbttagcompound.getInteger("targetZ"); - - if (nbttagcompound.getBoolean("hasArm")) { - NBTTagCompound armStore = nbttagcompound.getCompoundTag("arm"); - arm = new EntityMechanicalArm(worldObj); - arm.readFromNBT(armStore); - arm.listener = this; - - loadArm = true; - } - - } - - @Override - public void writeToNBT(NBTTagCompound nbttagcompound) { - super.writeToNBT(nbttagcompound); - - PowerFramework.currentFramework.savePowerProvider(this, nbttagcompound); - - nbttagcompound.setInteger("targetX", targetX); - nbttagcompound.setInteger("targetY", targetY); - nbttagcompound.setInteger("targetZ", targetZ); - nbttagcompound.setBoolean("hasArm", arm != null); - - if (arm != null) { - NBTTagCompound armStore = new NBTTagCompound(); - nbttagcompound.setTag("arm", armStore); - arm.writeToNBT(armStore); - } - - NBTTagCompound boxTag = new NBTTagCompound(); - box.writeToNBT(boxTag); - nbttagcompound.setTag("box", boxTag); - } - - @Override - public void positionReached(EntityMechanicalArm arm) { - inProcess = false; - - if (APIProxy.isClient(worldObj)) { - return; - } - - int i = targetX; - int j = targetY - 1; - int k = targetZ; - - int blockId = worldObj.getBlockId(i, j, k); - - if (canDig(blockId)) { - powerProvider.timeTracker.markTime(worldObj); - - // Share this with mining well! - - ArrayList stacks = BuildCraftBlockUtil.getItemStackFromBlock(worldObj, i, j, k); - - if (stacks != null) { - for (ItemStack s : stacks) { - if (s != null) { - mineStack(s); - } - } - } - - worldObj.setBlockWithNotify(i, j, k, 0); - } - - // Collect any lost items laying around - AxisAlignedBB axis = AxisAlignedBB.getBoundingBoxFromPool(arm.headPosX - 1.5, arm.headPosY, arm.headPosZ - 1.5, - arm.headPosX + 2.5, arm.headPosY + 2.5, arm.headPosZ + 2.5); - List result = worldObj.getEntitiesWithinAABB(EntityItem.class, axis); - for (int ii = 0; ii < result.size(); ii++) { - if (result.get(ii) instanceof EntityItem) { - EntityItem entity = (EntityItem) result.get(ii); - if (entity.isDead) - continue; - if (entity.item.stackSize <= 0) - continue; - APIProxy.removeEntity(entity); - mineStack(entity.item); - } - } - } - - private void mineStack(ItemStack s) { - boolean added = false; - - // First, try to add to a nearby chest - - StackUtil stackUtils = new StackUtil(s); - - added = stackUtils.addToRandomInventory(this, Orientations.Unknown); - - if (!added || stackUtils.items.stackSize > 0) { - added = Utils.addToRandomPipeEntry(this, Orientations.Unknown, stackUtils.items); - } - - // Last, throw the object away - - if (!added) { - float f = worldObj.rand.nextFloat() * 0.8F + 0.1F; - float f1 = worldObj.rand.nextFloat() * 0.8F + 0.1F; - float f2 = worldObj.rand.nextFloat() * 0.8F + 0.1F; - - EntityItem entityitem = new EntityItem(worldObj, xCoord + f, yCoord + f1 + 0.5F, zCoord + f2, stackUtils.items); - - float f3 = 0.05F; - entityitem.motionX = (float) worldObj.rand.nextGaussian() * f3; - entityitem.motionY = (float) worldObj.rand.nextGaussian() * f3 + 1.0F; - entityitem.motionZ = (float) worldObj.rand.nextGaussian() * f3; - worldObj.spawnEntityInWorld(entityitem); - } - } - - private boolean blockDig(int blockID) { - - if (Block.blocksList[blockID] != null && Block.blocksList[blockID].getHardness() == -1.0f) - return true; - - return blockID == Block.lavaStill.blockID || blockID == Block.lavaMoving.blockID; - } - - private boolean canDig(int blockID) { - return !blockDig(blockID) && !BuildCraftAPI.softBlock(blockID); - } - - @Override - public void invalidate () { - destroy (); - } - - @Override - public void destroy() { - if (arm != null) { - arm.setDead(); - } - - if (builder != null){ - builder.setDead(); - } - - box.deleteLasers(); - arm = null; - } - - @Override - public boolean isActive() { - return isDigging; - } - - private void setBoundaries(boolean useDefault) { - IAreaProvider a = null; - - if (!useDefault) { - a = Utils.getNearbyAreaProvider(worldObj, xCoord, yCoord, zCoord); - } - - if (a == null) { - a = new DefaultAreaProvider(xCoord, yCoord, zCoord, xCoord + 10, yCoord + 4, zCoord + 10); - - useDefault = true; - } - - int xSize = a.xMax() - a.xMin() + 1; - int ySize = a.yMax() - a.yMin() + 1; - int zSize = a.zMax() - a.zMin() + 1; - - if (xSize < 3 || zSize < 3) { - a = new DefaultAreaProvider(xCoord, yCoord, zCoord, xCoord + 10, yCoord + 4, zCoord + 10); - - useDefault = true; - } - - xSize = a.xMax() - a.xMin() + 1; - ySize = a.yMax() - a.yMin() + 1; - zSize = a.zMax() - a.zMin() + 1; - - box.initialize(a); - - if (ySize < 5) { - ySize = 5; - box.yMax = box.yMin + ySize - 1; - } - - if (useDefault) { - int xMin = 0, zMin = 0; - - Orientations o = Orientations.values()[worldObj.getBlockMetadata(xCoord, yCoord, zCoord)].reverse(); - - switch (o) { - case XPos: - xMin = xCoord + 1; - zMin = zCoord - 4 - 1; - break; - case XNeg: - xMin = xCoord - 9 - 2; - zMin = zCoord - 4 - 1; - break; - case ZPos: - xMin = xCoord - 4 - 1; - zMin = zCoord + 1; - break; - case ZNeg: - xMin = xCoord - 4 - 1; - zMin = zCoord - 9 - 2; - break; - } - - box.initialize(xMin, yCoord, zMin, xMin + xSize - 1, yCoord + ySize - 1, zMin + zSize - 1); - } - - a.removeFromWorld(); - } - - private void initializeBluePrintBuilder() { - - BptBlueprint bluePrint = new BptBlueprint(box.sizeX(), box.sizeY(), box.sizeZ()); - - for (int i = 0; i < bluePrint.sizeX; ++i) { - for (int j = 0; j < bluePrint.sizeY; ++j) { - for (int k = 0; k < bluePrint.sizeZ; ++k) { - bluePrint.setBlockId(i, j, k, 0); - } - } - } - - for (int it = 0; it < 2; it++) { - for (int i = 0; i < bluePrint.sizeX; ++i) { - bluePrint.setBlockId(i, it * (box.sizeY() - 1), 0, BuildCraftFactory.frameBlock.blockID); - bluePrint.setBlockId(i, it * (box.sizeY() - 1), bluePrint.sizeZ - 1, BuildCraftFactory.frameBlock.blockID); - } - - for (int k = 0; k < bluePrint.sizeZ; ++k) { - bluePrint.setBlockId(0, it * (box.sizeY() - 1), k, BuildCraftFactory.frameBlock.blockID); - bluePrint.setBlockId(bluePrint.sizeX - 1, it * (box.sizeY() - 1), k, BuildCraftFactory.frameBlock.blockID); - - } - } - - for (int h = 1; h < box.sizeY(); ++h) { - bluePrint.setBlockId(0, h, 0, BuildCraftFactory.frameBlock.blockID); - bluePrint.setBlockId(0, h, bluePrint.sizeZ - 1, BuildCraftFactory.frameBlock.blockID); - bluePrint.setBlockId(bluePrint.sizeX - 1, h, 0, BuildCraftFactory.frameBlock.blockID); - bluePrint.setBlockId(bluePrint.sizeX - 1, h, bluePrint.sizeZ - 1, BuildCraftFactory.frameBlock.blockID); - } - - bluePrintBuilder = new BptBuilderBlueprint(bluePrint, worldObj, box.xMin, yCoord, box.zMin); - } - - @Override - public void postPacketHandling(PacketUpdate packet) { - super.postPacketHandling(packet); - - createUtilsIfNeeded(); - - if (arm != null) { - arm.setHeadPosition(headPosX, headPosY, headPosZ); - arm.setTarget(targetX, targetY, targetZ); - arm.speed = speed; - } - } - - @Override - public void initialize() { - super.initialize(); - - if (!APIProxy.isClient(worldObj)) { - createUtilsIfNeeded(); - } - - sendNetworkUpdate(); - } - - @Override - public void setPowerProvider(PowerProvider provider) { - provider = powerProvider; - - } - - @Override - public PowerProvider getPowerProvider() { - return powerProvider; - } - - @Override - public boolean manageLiquids() { - return false; - } - - @Override - public boolean manageSolids() { - return true; - } - - @Override - public boolean isPipeConnected(Orientations with) { - return true; - } - - @Override - public int getSizeInventory() { - return 0; - } - - @Override - public ItemStack getStackInSlot(int i) { - return null; - } - - @Override - public ItemStack decrStackSize(int i, int j) { - return null; - } - - @Override - public void setInventorySlotContents(int i, ItemStack itemstack) { - - } - - @Override - public ItemStack getStackInSlotOnClosing(int slot) { - return null; - } - - @Override - public String getInvName() { - return ""; - } - - @Override - public int getInventoryStackLimit() { - return 0; - } - - @Override - public boolean isUseableByPlayer(EntityPlayer entityplayer) { - return false; - } - - @Override - public void openChest() {} - - @Override - public void closeChest() {} - - @Override - public boolean isBuildingMaterial(int i) { - return true; - } - - @Override - public boolean allowActions() { - return false; - } - -} +/** + * 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 net.minecraft.src.buildcraft.factory; + +import java.util.ArrayList; +import java.util.List; + +import net.minecraft.src.AxisAlignedBB; +import net.minecraft.src.Block; +import net.minecraft.src.BuildCraftBlockUtil; +import net.minecraft.src.BuildCraftFactory; +import net.minecraft.src.EntityItem; +import net.minecraft.src.EntityPlayer; +import net.minecraft.src.ItemStack; +import net.minecraft.src.NBTTagCompound; +import net.minecraft.src.buildcraft.api.APIProxy; +import net.minecraft.src.buildcraft.api.BuildCraftAPI; +import net.minecraft.src.buildcraft.api.IAreaProvider; +import net.minecraft.src.buildcraft.api.IPipeConnection; +import net.minecraft.src.buildcraft.api.IPowerReceptor; +import net.minecraft.src.buildcraft.api.LaserKind; +import net.minecraft.src.buildcraft.api.Orientations; +import net.minecraft.src.buildcraft.api.PowerFramework; +import net.minecraft.src.buildcraft.api.PowerProvider; +import net.minecraft.src.buildcraft.api.TileNetworkData; +import net.minecraft.src.buildcraft.core.Box; +import net.minecraft.src.buildcraft.core.BptBlueprint; +import net.minecraft.src.buildcraft.core.BptBuilderBase; +import net.minecraft.src.buildcraft.core.BptBuilderBlueprint; +import net.minecraft.src.buildcraft.core.DefaultAreaProvider; +import net.minecraft.src.buildcraft.core.EntityRobot; +import net.minecraft.src.buildcraft.core.IBuilderInventory; +import net.minecraft.src.buildcraft.core.IMachine; +import net.minecraft.src.buildcraft.core.StackUtil; +import net.minecraft.src.buildcraft.core.Utils; +import net.minecraft.src.buildcraft.core.network.PacketUpdate; + +public class TileQuarry extends TileMachine implements IArmListener, IMachine, IPowerReceptor, IPipeConnection, IBuilderInventory { + + boolean isDigging = false; + + public @TileNetworkData + Box box = new Box(); + public @TileNetworkData + boolean inProcess = false; + + public EntityMechanicalArm arm; + public @TileNetworkData + int targetX, targetY, targetZ; + public @TileNetworkData + double headPosX, headPosY, headPosZ; + public @TileNetworkData + double speed = 0.03; + + public EntityRobot builder; + public @TileNetworkData + boolean builderDone = false; + + boolean loadArm = false; + + BptBuilderBase bluePrintBuilder; + + public @TileNetworkData PowerProvider powerProvider; + + public static int MAX_ENERGY = 7000; + + public TileQuarry() { + + powerProvider = PowerFramework.currentFramework.createPowerProvider(); + powerProvider.configure(20, 25, 25, 25, MAX_ENERGY); + } + + public void createUtilsIfNeeded() { + + if (!box.isInitialized() && APIProxy.isClient(worldObj)) { + return; + } + + if (bluePrintBuilder == null) { + + if (!box.isInitialized()) { + setBoundaries(loadDefaultBoundaries); + } + + initializeBluePrintBuilder(); + } + + if (builderDone) { + + box.deleteLasers(); + + if (arm == null) { + createArm(); + } + + if (loadArm) { + arm.joinToWorld(worldObj); + loadArm = false; + + if (findTarget(false)) { + isDigging = true; + } + } + + } else { + + box.createLasers(worldObj, LaserKind.Stripes); + isDigging = true; + } + } + + private boolean loadDefaultBoundaries = false; + + private void createArm() { + + arm = new EntityMechanicalArm(worldObj, + box.xMin + Utils.pipeMaxPos, + yCoord + bluePrintBuilder.bluePrint.sizeY - 1 + Utils.pipeMinPos, box.zMin + Utils.pipeMaxPos, + bluePrintBuilder.bluePrint.sizeX - 2 + Utils.pipeMinPos * 2, + bluePrintBuilder.bluePrint.sizeZ - 2 + Utils.pipeMinPos * 2, this); + + arm.listener = this; + loadArm = true; + } + + @Override + public void updateEntity() { + + super.updateEntity(); + + if (inProcess && arm != null) { + + arm.speed = 0; + float energyToUse = 2 + powerProvider.energyStored / 1000; + + float energy = powerProvider.useEnergy(energyToUse, energyToUse, true); + + if (energy > 0) { + arm.doMove(0.015 + energy / 200F); + } + } + + if (arm != null) { + + headPosX = arm.headPosX; + headPosY = arm.headPosY; + headPosZ = arm.headPosZ; + + speed = arm.speed; + } + + if (APIProxy.isServerSide()) { + sendNetworkUpdate(); + } + + if (inProcess || !isDigging) { + return; + } + + createUtilsIfNeeded(); + + if (bluePrintBuilder != null) { + + builderDone = bluePrintBuilder.done; + if (!builderDone) { + + buildFrame(); + return; + + } else { + + if (builder != null && builder.done()) { + + box.deleteLasers(); + builder.setDead(); + builder = null; + } + } + } + + if (builder == null) { + + dig(); + } + + + } + + @Override + public void doWork() {} + + protected void buildFrame() { + + System.out.println(powerProvider.energyStored); + powerProvider.configure(20, 25, 25, 25, MAX_ENERGY); + if (powerProvider.useEnergy(25, 25, true) != 25) { + return; + } + + powerProvider.timeTracker.markTime(worldObj); + + if (builder == null) { + builder = new EntityRobot(worldObj, box); + worldObj.spawnEntityInWorld(builder); + } + + if (builder.readyToBuild()) { + builder.scheduleContruction(bluePrintBuilder.getNextBlock(worldObj, this), bluePrintBuilder.getContext()); + } + } + + protected void dig() { + + powerProvider.configure(20, 30, 200, 50, MAX_ENERGY); + if (powerProvider.useEnergy(30, 30, true) != 30) { + return; + } + + if (!findTarget(true)) { + + arm.setTarget(box.xMin + arm.sizeX / 2, yCoord + 2, box.zMin + arm.sizeX / 2); + + isDigging = false; + } + + inProcess = true; + } + + public boolean findTarget(boolean doSet) { + + if (APIProxy.isClient(worldObj)) + return false; + + boolean[][] blockedColumns = new boolean[bluePrintBuilder.bluePrint.sizeX - 2][bluePrintBuilder.bluePrint.sizeZ - 2]; + + for (int searchX = 0; searchX < bluePrintBuilder.bluePrint.sizeX - 2; ++searchX) { + for (int searchZ = 0; searchZ < bluePrintBuilder.bluePrint.sizeZ - 2; ++searchZ) { + blockedColumns[searchX][searchZ] = false; + } + } + + for (int searchY = yCoord + 3; searchY >= 0; --searchY) { + int startX, endX, incX; + + if (searchY % 2 == 0) { + startX = 0; + endX = bluePrintBuilder.bluePrint.sizeX - 2; + incX = 1; + } else { + startX = bluePrintBuilder.bluePrint.sizeX - 3; + endX = -1; + incX = -1; + } + + for (int searchX = startX; searchX != endX; searchX += incX) { + int startZ, endZ, incZ; + + if (searchX % 2 == searchY % 2) { + startZ = 0; + endZ = bluePrintBuilder.bluePrint.sizeZ - 2; + incZ = 1; + } else { + startZ = bluePrintBuilder.bluePrint.sizeZ - 3; + endZ = -1; + incZ = -1; + } + + for (int searchZ = startZ; searchZ != endZ; searchZ += incZ) { + if (!blockedColumns[searchX][searchZ]) { + int bx = box.xMin + searchX + 1, by = searchY, bz = box.zMin + searchZ + 1; + + int blockId = worldObj.getBlockId(bx, by, bz); + + if (blockDig(blockId)) { + blockedColumns[searchX][searchZ] = true; + } else if (canDig(blockId)) { + if (doSet) { + arm.setTarget(bx, by + 1, bz); + + targetX = (int) arm.targetX; + targetY = (int) arm.targetY; + targetZ = (int) arm.targetZ; + } + + return true; + } + } + } + } + } + + return false; + } + + @Override + public void readFromNBT(NBTTagCompound nbttagcompound) { + super.readFromNBT(nbttagcompound); + + PowerFramework.currentFramework.loadPowerProvider(this, nbttagcompound); + + if (nbttagcompound.hasKey("box")) { + box.initialize(nbttagcompound.getCompoundTag("box")); + + loadDefaultBoundaries = false; + } else if (nbttagcompound.hasKey("xSize")) { + // This is a legacy save, get old data + + int xMin = nbttagcompound.getInteger("xMin"); + int zMin = nbttagcompound.getInteger("zMin"); + + int xSize = nbttagcompound.getInteger("xSize"); + int ySize = nbttagcompound.getInteger("ySize"); + int zSize = nbttagcompound.getInteger("zSize"); + + box.initialize(xMin, yCoord, zMin, xMin + xSize - 1, yCoord + ySize - 1, zMin + zSize - 1); + + loadDefaultBoundaries = false; + } else { + // This is a legacy save, compute boundaries + + loadDefaultBoundaries = true; + } + + targetX = nbttagcompound.getInteger("targetX"); + targetY = nbttagcompound.getInteger("targetY"); + targetZ = nbttagcompound.getInteger("targetZ"); + + if (nbttagcompound.getBoolean("hasArm")) { + NBTTagCompound armStore = nbttagcompound.getCompoundTag("arm"); + arm = new EntityMechanicalArm(worldObj); + arm.readFromNBT(armStore); + arm.listener = this; + + loadArm = true; + } + + } + + @Override + public void writeToNBT(NBTTagCompound nbttagcompound) { + super.writeToNBT(nbttagcompound); + + PowerFramework.currentFramework.savePowerProvider(this, nbttagcompound); + + nbttagcompound.setInteger("targetX", targetX); + nbttagcompound.setInteger("targetY", targetY); + nbttagcompound.setInteger("targetZ", targetZ); + nbttagcompound.setBoolean("hasArm", arm != null); + + if (arm != null) { + NBTTagCompound armStore = new NBTTagCompound(); + nbttagcompound.setTag("arm", armStore); + arm.writeToNBT(armStore); + } + + NBTTagCompound boxTag = new NBTTagCompound(); + box.writeToNBT(boxTag); + nbttagcompound.setTag("box", boxTag); + } + + @Override + public void positionReached(EntityMechanicalArm arm) { + inProcess = false; + + if (APIProxy.isClient(worldObj)) { + return; + } + + int i = targetX; + int j = targetY - 1; + int k = targetZ; + + int blockId = worldObj.getBlockId(i, j, k); + + if (canDig(blockId)) { + powerProvider.timeTracker.markTime(worldObj); + + // Share this with mining well! + + ArrayList stacks = BuildCraftBlockUtil.getItemStackFromBlock(worldObj, i, j, k); + + if (stacks != null) { + for (ItemStack s : stacks) { + if (s != null) { + mineStack(s); + } + } + } + + worldObj.setBlockWithNotify(i, j, k, 0); + } + + // Collect any lost items laying around + AxisAlignedBB axis = AxisAlignedBB.getBoundingBoxFromPool(arm.headPosX - 1.5, arm.headPosY, arm.headPosZ - 1.5, + arm.headPosX + 2.5, arm.headPosY + 2.5, arm.headPosZ + 2.5); + List result = worldObj.getEntitiesWithinAABB(EntityItem.class, axis); + for (int ii = 0; ii < result.size(); ii++) { + if (result.get(ii) instanceof EntityItem) { + EntityItem entity = (EntityItem) result.get(ii); + if (entity.isDead) + continue; + if (entity.item.stackSize <= 0) + continue; + APIProxy.removeEntity(entity); + mineStack(entity.item); + } + } + } + + private void mineStack(ItemStack s) { + boolean added = false; + + // First, try to add to a nearby chest + + StackUtil stackUtils = new StackUtil(s); + + added = stackUtils.addToRandomInventory(this, Orientations.Unknown); + + if (!added || stackUtils.items.stackSize > 0) { + added = Utils.addToRandomPipeEntry(this, Orientations.Unknown, stackUtils.items); + } + + // Last, throw the object away + + if (!added) { + float f = worldObj.rand.nextFloat() * 0.8F + 0.1F; + float f1 = worldObj.rand.nextFloat() * 0.8F + 0.1F; + float f2 = worldObj.rand.nextFloat() * 0.8F + 0.1F; + + EntityItem entityitem = new EntityItem(worldObj, xCoord + f, yCoord + f1 + 0.5F, zCoord + f2, stackUtils.items); + + float f3 = 0.05F; + entityitem.motionX = (float) worldObj.rand.nextGaussian() * f3; + entityitem.motionY = (float) worldObj.rand.nextGaussian() * f3 + 1.0F; + entityitem.motionZ = (float) worldObj.rand.nextGaussian() * f3; + worldObj.spawnEntityInWorld(entityitem); + } + } + + private boolean blockDig(int blockID) { + + if (Block.blocksList[blockID] != null && Block.blocksList[blockID].getHardness() == -1.0f) + return true; + + return blockID == Block.lavaStill.blockID || blockID == Block.lavaMoving.blockID; + } + + private boolean canDig(int blockID) { + return !blockDig(blockID) && !BuildCraftAPI.softBlock(blockID); + } + + @Override + public void invalidate () { + destroy (); + } + + @Override + public void destroy() { + if (arm != null) { + arm.setDead(); + } + + if (builder != null){ + builder.setDead(); + } + + box.deleteLasers(); + arm = null; + } + + @Override + public boolean isActive() { + return isDigging; + } + + private void setBoundaries(boolean useDefault) { + + IAreaProvider a = null; + + if (!useDefault) { + a = Utils.getNearbyAreaProvider(worldObj, xCoord, yCoord, zCoord); + } + + if (a == null) { + a = new DefaultAreaProvider(xCoord, yCoord, zCoord, xCoord + 10, yCoord + 4, zCoord + 10); + + useDefault = true; + } + + int xSize = a.xMax() - a.xMin() + 1; + int ySize = a.yMax() - a.yMin() + 1; + int zSize = a.zMax() - a.zMin() + 1; + + if (xSize < 3 || zSize < 3) { + a = new DefaultAreaProvider(xCoord, yCoord, zCoord, xCoord + 10, yCoord + 4, zCoord + 10); + + useDefault = true; + } + + xSize = a.xMax() - a.xMin() + 1; + ySize = a.yMax() - a.yMin() + 1; + zSize = a.zMax() - a.zMin() + 1; + + box.initialize(a); + + if (ySize < 5) { + ySize = 5; + box.yMax = box.yMin + ySize - 1; + } + + if (useDefault) { + int xMin = 0, zMin = 0; + + Orientations o = Orientations.values()[worldObj.getBlockMetadata(xCoord, yCoord, zCoord)].reverse(); + + switch (o) { + case XPos: + xMin = xCoord + 1; + zMin = zCoord - 4 - 1; + break; + case XNeg: + xMin = xCoord - 9 - 2; + zMin = zCoord - 4 - 1; + break; + case ZPos: + xMin = xCoord - 4 - 1; + zMin = zCoord + 1; + break; + case ZNeg: + xMin = xCoord - 4 - 1; + zMin = zCoord - 9 - 2; + break; + } + + box.initialize(xMin, yCoord, zMin, xMin + xSize - 1, yCoord + ySize - 1, zMin + zSize - 1); + } + + a.removeFromWorld(); + } + + private void initializeBluePrintBuilder() { + + BptBlueprint bluePrint = new BptBlueprint(box.sizeX(), box.sizeY(), box.sizeZ()); + + for (int i = 0; i < bluePrint.sizeX; ++i) { + for (int j = 0; j < bluePrint.sizeY; ++j) { + for (int k = 0; k < bluePrint.sizeZ; ++k) { + bluePrint.setBlockId(i, j, k, 0); + } + } + } + + for (int it = 0; it < 2; it++) { + for (int i = 0; i < bluePrint.sizeX; ++i) { + bluePrint.setBlockId(i, it * (box.sizeY() - 1), 0, BuildCraftFactory.frameBlock.blockID); + bluePrint.setBlockId(i, it * (box.sizeY() - 1), bluePrint.sizeZ - 1, BuildCraftFactory.frameBlock.blockID); + } + + for (int k = 0; k < bluePrint.sizeZ; ++k) { + bluePrint.setBlockId(0, it * (box.sizeY() - 1), k, BuildCraftFactory.frameBlock.blockID); + bluePrint.setBlockId(bluePrint.sizeX - 1, it * (box.sizeY() - 1), k, BuildCraftFactory.frameBlock.blockID); + + } + } + + for (int h = 1; h < box.sizeY(); ++h) { + bluePrint.setBlockId(0, h, 0, BuildCraftFactory.frameBlock.blockID); + bluePrint.setBlockId(0, h, bluePrint.sizeZ - 1, BuildCraftFactory.frameBlock.blockID); + bluePrint.setBlockId(bluePrint.sizeX - 1, h, 0, BuildCraftFactory.frameBlock.blockID); + bluePrint.setBlockId(bluePrint.sizeX - 1, h, bluePrint.sizeZ - 1, BuildCraftFactory.frameBlock.blockID); + } + + bluePrintBuilder = new BptBuilderBlueprint(bluePrint, worldObj, box.xMin, yCoord, box.zMin); + } + + @Override + public void postPacketHandling(PacketUpdate packet) { + + super.postPacketHandling(packet); + + createUtilsIfNeeded(); + + if (arm != null) { + arm.setHeadPosition(headPosX, headPosY, headPosZ); + arm.setTarget(targetX, targetY, targetZ); + arm.speed = speed; + } + } + + @Override + public void initialize() { + super.initialize(); + + if (!APIProxy.isClient(worldObj)) { + createUtilsIfNeeded(); + } + + sendNetworkUpdate(); + } + + @Override + public void setPowerProvider(PowerProvider provider) { + provider = powerProvider; + + } + + @Override + public PowerProvider getPowerProvider() { + return powerProvider; + } + + @Override + public boolean manageLiquids() { + return false; + } + + @Override + public boolean manageSolids() { + return true; + } + + @Override + public boolean isPipeConnected(Orientations with) { + return true; + } + + @Override + public int getSizeInventory() { + return 0; + } + + @Override + public ItemStack getStackInSlot(int i) { + return null; + } + + @Override + public ItemStack decrStackSize(int i, int j) { + return null; + } + + @Override + public void setInventorySlotContents(int i, ItemStack itemstack) { + + } + + @Override + public ItemStack getStackInSlotOnClosing(int slot) { + return null; + } + + @Override + public String getInvName() { + return ""; + } + + @Override + public int getInventoryStackLimit() { + return 0; + } + + @Override + public boolean isUseableByPlayer(EntityPlayer entityplayer) { + return false; + } + + @Override + public void openChest() {} + + @Override + public void closeChest() {} + + @Override + public boolean isBuildingMaterial(int i) { + return true; + } + + @Override + public boolean allowActions() { + return false; + } + +}