From 86fd779608eed4e5fa3c2a7b01089f3ca2b9e0b1 Mon Sep 17 00:00:00 2001 From: Brian Ricketts Date: Fri, 18 Jan 2013 21:51:38 -0600 Subject: [PATCH] Finished implementing CC API for armbots --- .../machine/armbot/TileEntityArmbot.java | 100 +++++++++++------- .../common/machine/command/Command.java | 34 ++++++ .../common/machine/command/CommandGrab.java | 10 -- .../machine/command/CommandManager.java | 19 +++- .../common/machine/command/CommandRotate.java | 4 +- 5 files changed, 113 insertions(+), 54 deletions(-) diff --git a/src/minecraft/assemblyline/common/machine/armbot/TileEntityArmbot.java b/src/minecraft/assemblyline/common/machine/armbot/TileEntityArmbot.java index b6dd55a6..ddd455d6 100644 --- a/src/minecraft/assemblyline/common/machine/armbot/TileEntityArmbot.java +++ b/src/minecraft/assemblyline/common/machine/armbot/TileEntityArmbot.java @@ -21,6 +21,7 @@ import net.minecraft.network.INetworkManager; import net.minecraft.network.packet.Packet; import net.minecraft.network.packet.Packet250CustomPayload; import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.AxisAlignedBB; import net.minecraftforge.common.ForgeDirection; import universalelectricity.core.electricity.ElectricityConnections; import universalelectricity.core.implement.IJouleStorage; @@ -60,6 +61,7 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult public double wattsReceived = 0; private int playerUsing = 0; private int computersAttached = 0; + private List connectedComputers = new ArrayList(); /** * The rotation of the arms. In Degrees. */ @@ -175,13 +177,28 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult { float speed; if (this.renderYaw > this.rotationYaw) - speed = -this.ROTATION_SPEED; + if (Math.abs(this.renderYaw - this.rotationYaw) > 180) + speed = this.ROTATION_SPEED; + else + speed = -this.ROTATION_SPEED; else - speed = this.ROTATION_SPEED; - // System.out.println("Speed: " + speed); - // System.out.println("Yaw: [" + this.rotationYaw + ", " + this.renderYaw + "]"); + if (Math.abs(this.renderYaw - this.rotationYaw) > 180) + speed = -this.ROTATION_SPEED; + else + speed = this.ROTATION_SPEED; + this.renderYaw += speed; - if (this.ticks % 5 == 0) //sound is 0.5 seconds long (20 ticks/second) + + if (this.renderYaw <= -360) + { + this.renderYaw += 360; + } + if (this.renderYaw >= 360) + { + this.renderYaw -= 360; + } + + if (this.ticks % 5 == 0) // sound is 0.5 seconds long (20 ticks/second) Minecraft.getMinecraft().sndManager.playSound("assemblyline.conveyor", this.xCoord, this.yCoord, this.zCoord, 2f, 1.7f); if (Math.abs(this.renderYaw - this.rotationYaw) < this.ROTATION_SPEED + 0.1f) { @@ -225,7 +242,7 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult { NBTTagCompound nbt = new NBTTagCompound(); writeToNBT(nbt); - return PacketManager.getPacket(AssemblyLine.CHANNEL, this, this.powerTransferRange, this.commandManager.getCurrentTask(), nbt); + return PacketManager.getPacket(AssemblyLine.CHANNEL, this, this.powerTransferRange, nbt); } /** @@ -246,7 +263,6 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult y = dis.readInt(); z = dis.readInt(); this.powerTransferRange = dis.readInt(); - this.commandManager.setCurrentTask(dis.readInt()); NBTTagCompound tag = Packet.readNBTTagCompound(dis); readFromNBT(tag); } @@ -512,6 +528,11 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult } } } + else + { + this.commandManager.addCommand(this, Command.getCommand("DROP")); + this.commandManager.addCommand(this, Command.getCommand("RETURN")); + } } @Override @@ -538,12 +559,12 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult @Override public String[] getMethodNames() { - return new String[] { "rotateBy", "rotateTo", "grab", "drop", "reset", "isWorking" }; + return new String[] { "rotate", "grab", "drop", "reset", "isWorking", "touchingEntity" }; } @Override public Object[] callMethod(IComputerAccess computer, int method, Object[] arguments) throws Exception - { + { switch (method) { case 0: // rotateBy: rotates by a certain amount @@ -555,10 +576,6 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult { double angle = (Double) arguments[0]; this.commandManager.addCommand(this, CommandRotate.class, new String[] { Double.toString(angle) }); - while (this.commandManager.hasTasks()) - { - Thread.sleep(1); - } } catch (Exception ex) { @@ -572,49 +589,42 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult } break; } - case 1: // rotateTo: rotates to an absolute angle (0 is idle position, increases clockwise) - { - if (arguments.length > 0) - { - try - { - double angle = (Double) arguments[0]; - double diff = angle - this.rotationYaw; - this.commandManager.addCommand(this, CommandRotate.class, new String[] { Double.toString(diff) }); - while (this.commandManager.hasTasks()) - ; - } - catch (Exception ex) - { - ex.printStackTrace(); - throw new IllegalArgumentException("expected number"); - } - } - else - { - throw new IllegalArgumentException("expected number"); - } - break; - } - case 2: // grab: grabs an item + case 1: // grab: grabs an item { this.commandManager.addCommand(this, CommandGrab.class); break; } - case 3: // drop: drops an item + case 2: // drop: drops an item { this.commandManager.addCommand(this, CommandDrop.class); break; } - case 4: // reset: calls the RETURN command + case 3: // reset: clears the queue and calls the RETURN command { + this.commandManager.clear(); this.commandManager.addCommand(this, CommandReturn.class); break; } - case 5: // isWorking: returns whether or not the ArmBot is executing commands + case 4: // isWorking: returns whether or not the ArmBot is executing commands { return new Object[] { this.commandManager.hasTasks() }; } + case 5: // touchingEntity: returns whether or not the ArmBot is touching an entity it is able to pick up + { + Vector3 serachPosition = this.getHandPosition(); + List found = this.worldObj.getEntitiesWithinAABB(Entity.class, AxisAlignedBB.getBoundingBox(serachPosition.x - 0.5f, serachPosition.y - 0.5f, serachPosition.z - 0.5f, serachPosition.x + 0.5f, serachPosition.y + 0.5f, serachPosition.z + 0.5f)); + + if (found != null && found.size() > 0) + { + for (int i = 0; i < found.size(); i++) + { + if (found.get(i) != null && !(found.get(i) instanceof EntityPlayer) && found.get(i).ridingEntity == null) // isn't null, isn't a player, and isn't riding anything + { return new Object[] { true }; } + } + } + + return new Object[] { false }; + } } return null; } @@ -629,12 +639,20 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult public void attach(IComputerAccess computer) { computersAttached++; + synchronized (connectedComputers) + { + connectedComputers.add(computer); + } } @Override public void detach(IComputerAccess computer) { computersAttached--; + synchronized (connectedComputers) + { + connectedComputers.remove(computer); + } } } diff --git a/src/minecraft/assemblyline/common/machine/command/Command.java b/src/minecraft/assemblyline/common/machine/command/Command.java index 854dd414..221f0f20 100644 --- a/src/minecraft/assemblyline/common/machine/command/Command.java +++ b/src/minecraft/assemblyline/common/machine/command/Command.java @@ -129,6 +129,40 @@ public abstract class Command return 0; } + + protected Double getDoubleArg(int i) + { + if (getArg(i) != null) + { + try + { + return Double.parseDouble(getArg(i)); + } + catch (Exception e) + { + + } + } + + return 0.0; + } + + protected Float getFloatArg(int i) + { + if (getArg(i) != null) + { + try + { + return Float.parseFloat(getArg(i)); + } + catch (Exception e) + { + + } + } + + return 0.0f; + } public void writeToNBT(NBTTagCompound taskCompound) { diff --git a/src/minecraft/assemblyline/common/machine/command/CommandGrab.java b/src/minecraft/assemblyline/common/machine/command/CommandGrab.java index 74259467..278e62ab 100644 --- a/src/minecraft/assemblyline/common/machine/command/CommandGrab.java +++ b/src/minecraft/assemblyline/common/machine/command/CommandGrab.java @@ -48,16 +48,6 @@ public class CommandGrab extends Command this.tileEntity.worldObj.removeEntity(found.get(i)); //items don't move right, so we render them manually Minecraft.getMinecraft().sndManager.playSound("random.pop", this.tileEntity.xCoord, this.tileEntity.yCoord, this.tileEntity.zCoord, 0.2F, ((this.tileEntity.worldObj.rand.nextFloat() - this.tileEntity.worldObj.rand.nextFloat()) * 0.7F + 1.0F) * 1.0F); found.get(i).isDead = false; - /*if (found.get(i).riddenByEntity != null) - { - found.get(i).riddenByEntity.ridingEntity = null; - found.get(i).riddenByEntity = null; - } - if (found.get(i).ridingEntity != null) - { - found.get(i).ridingEntity.riddenByEntity = null; - found.get(i).ridingEntity = null; - }*/ return false; } } diff --git a/src/minecraft/assemblyline/common/machine/command/CommandManager.java b/src/minecraft/assemblyline/common/machine/command/CommandManager.java index df9fbb68..2c687785 100644 --- a/src/minecraft/assemblyline/common/machine/command/CommandManager.java +++ b/src/minecraft/assemblyline/common/machine/command/CommandManager.java @@ -33,6 +33,7 @@ public class CommandManager if (this.currentTask < 0) { this.currentTask = 0; + this.lastTask = -1; } Command task = this.tasks.get(this.currentTask); @@ -42,7 +43,9 @@ public class CommandManager this.lastTask = this.currentTask; task.onTaskStart(); } - + + //System.out.print(Command.getCommandName(task.getClass()) + "|"); + if (!task.doTask()) { // End the task and reinitiate it into a new class to make sure it is fresh. @@ -51,6 +54,18 @@ public class CommandManager task.onTaskEnd(); this.tasks.set(tempCurrentTask, this.getNewCommand(task.tileEntity, task.getClass(), task.getArgs())); } + + /*for (Command command : this.tasks) + { + System.out.print(Command.getCommandName(command.getClass())); + System.out.print("; "); + } + + System.out.println(this.currentTask);*/ + } + else + { + this.clear(); } } } @@ -110,7 +125,7 @@ public class CommandManager */ public boolean hasTasks() { - return !tasks.isEmpty(); + return tasks.size() > 0; } public List getCommands() diff --git a/src/minecraft/assemblyline/common/machine/command/CommandRotate.java b/src/minecraft/assemblyline/common/machine/command/CommandRotate.java index 7c1e5449..994b58cd 100644 --- a/src/minecraft/assemblyline/common/machine/command/CommandRotate.java +++ b/src/minecraft/assemblyline/common/machine/command/CommandRotate.java @@ -15,6 +15,8 @@ public class CommandRotate extends Command public void onTaskStart() { super.onTaskStart(); + + this.ticks = 0; if (this.getArg(0) == null) { @@ -22,7 +24,7 @@ public class CommandRotate extends Command } else { - this.targetRotation = this.tileEntity.rotationYaw + this.getIntArg(0); + this.targetRotation = this.tileEntity.rotationYaw + this.getFloatArg(0); } while (this.targetRotation >= 360)