From 67a59a6af7235044d57c1887a4dc813bac43f00b Mon Sep 17 00:00:00 2001 From: Henry Mao Date: Fri, 11 Jan 2013 23:56:36 +0800 Subject: [PATCH] More work on Armbot --- buildnumber.txt | 2 +- info.txt | 1 + .../machine/armbot/TileEntityArmbot.java | 82 ++++++++++++------- .../common/machine/command/Command.java | 2 +- .../common/machine/command/CommandDrop.java | 3 +- .../common/machine/command/CommandGrab.java | 7 +- .../machine/command/CommandManager.java | 47 +++++------ .../common/machine/command/CommandRepeat.java | 12 ++- .../common/machine/command/CommandRotate.java | 22 +++-- 9 files changed, 99 insertions(+), 79 deletions(-) diff --git a/buildnumber.txt b/buildnumber.txt index e244606c4..5d0fcf63c 100644 --- a/buildnumber.txt +++ b/buildnumber.txt @@ -1 +1 @@ -50 +51 diff --git a/info.txt b/info.txt index abdc6ee57..5f454570f 100644 --- a/info.txt +++ b/info.txt @@ -44,3 +44,4 @@ Minecraft 1.4.5 @. AssemblyLine_v0.2.3.48.jar AssemblyLine_v0.2.3.48_api.zip @ AssemblyLine_v0.2.3.49.jar AssemblyLine_v0.2.3.49_api.zip @ AssemblyLine_v0.2.3.50.jar AssemblyLine_v0.2.3.50_api.zip +@ AssemblyLine_v0.2.3.51.jar AssemblyLine_v0.2.3.51_api.zip diff --git a/src/minecraft/assemblyline/common/machine/armbot/TileEntityArmbot.java b/src/minecraft/assemblyline/common/machine/armbot/TileEntityArmbot.java index a87332e80..cbe2f7d94 100644 --- a/src/minecraft/assemblyline/common/machine/armbot/TileEntityArmbot.java +++ b/src/minecraft/assemblyline/common/machine/armbot/TileEntityArmbot.java @@ -1,5 +1,8 @@ package assemblyline.common.machine.armbot; +import java.io.ByteArrayInputStream; +import java.io.DataInputStream; +import java.io.IOException; import java.util.ArrayList; import java.util.EnumSet; import java.util.List; @@ -12,7 +15,6 @@ import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.network.INetworkManager; import net.minecraft.network.packet.Packet; -import net.minecraft.network.packet.Packet132TileEntityData; import net.minecraft.network.packet.Packet250CustomPayload; import net.minecraft.tileentity.TileEntity; import net.minecraftforge.common.ForgeDirection; @@ -22,6 +24,7 @@ import universalelectricity.core.vector.Vector3; import universalelectricity.prefab.TranslationHelper; import universalelectricity.prefab.multiblock.IMultiBlock; import universalelectricity.prefab.network.IPacketReceiver; +import universalelectricity.prefab.network.PacketManager; import assemblyline.common.AssemblyLine; import assemblyline.common.machine.TileEntityAssemblyNetwork; import assemblyline.common.machine.command.Command; @@ -33,6 +36,7 @@ import assemblyline.common.machine.encoder.ItemDisk; import com.google.common.io.ByteArrayDataInput; import cpw.mods.fml.common.FMLCommonHandler; +import cpw.mods.fml.common.FMLLog; import cpw.mods.fml.relauncher.Side; public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMultiBlock, IInventory, IPacketReceiver, IJouleStorage @@ -82,6 +86,7 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult this.commandManager.clearTasks(); } } + if (!this.commandManager.hasTasks()) { List commands = ItemDisk.getCommands(this.disk); @@ -130,6 +135,8 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult { this.commandManager.addTask(this, new CommandReturn()); } + + this.commandManager.setCurrentTask(0); } if (this.isRunning()) @@ -142,14 +149,16 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult entity.motionX = 0; entity.motionY = 0; entity.motionZ = 0; + + if (entity instanceof EntityItem) + { + ((EntityItem) entity).delayBeforeCanPickup = 20; + } } - if (FMLCommonHandler.instance().getEffectiveSide() == Side.SERVER) - { - this.commandManager.onUpdate(); - } + this.commandManager.onUpdate(); - // keep it within [0, 360) so ROTATE commands work properly + // keep it within 0 - 360 degrees so ROTATE commands work properly if (this.rotationPitch <= -360) this.rotationPitch += 360; if (this.rotationPitch >= 360) @@ -161,10 +170,9 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult } - // Simulates smoothness on client side - if (FMLCommonHandler.instance().getEffectiveSide() == Side.CLIENT) + if (FMLCommonHandler.instance().getEffectiveSide() == Side.SERVER && this.ticks % 5 == 0) { - this.commandManager.onUpdate(); + PacketManager.sendPacketToClients(this.getDescriptionPacket(), this.worldObj, new Vector3(this), 20); } } @@ -186,8 +194,9 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult delta.x = Math.sin(Math.toRadians(-this.rotationYaw)) * dH; delta.z = Math.cos(Math.toRadians(-this.rotationYaw)) * dH; position.add(delta); - - this.worldObj.spawnParticle("smoke", position.x, position.y, position.z, 0, 0, 0); + // TODO: Use Smoke Spawning to Determine Hand Calculation Position. Delete when done + // developing this part. + // this.worldObj.spawnParticle("smoke", position.x, position.y, position.z, 0, 0, 0); return position; } @@ -202,20 +211,7 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult { NBTTagCompound nbt = new NBTTagCompound(); writeToNBT(nbt); - Packet132TileEntityData data = new Packet132TileEntityData(this.xCoord, this.yCoord, this.zCoord, 0, nbt); - return data; - } - - @Override - public void onDataPacket(INetworkManager netManager, Packet132TileEntityData packet) - { - if (FMLCommonHandler.instance().getEffectiveSide() == Side.CLIENT) - { - this.xCoord = packet.xPosition; - this.yCoord = packet.yPosition; - this.zCoord = packet.zPosition; - readFromNBT(packet.customParam1); - } + return PacketManager.getPacket(AssemblyLine.CHANNEL, this, this.commandManager.getCurrentTask(), nbt); } /** @@ -224,12 +220,29 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult @Override public void handlePacketData(INetworkManager network, int packetType, Packet250CustomPayload packet, EntityPlayer player, ByteArrayDataInput dataStream) { - /* - * if (packetType == PACKET_COMMANDS) { String commandString = dataStream.readUTF(); - * String[] commands = commandString.split("|"); - * - * } - */ + if (this.worldObj.isRemote) + { + try + { + ByteArrayInputStream bis = new ByteArrayInputStream(packet.data); + DataInputStream dis = new DataInputStream(bis); + final int id, x, y, z; + + id = dis.readInt(); + x = dis.readInt(); + y = dis.readInt(); + z = dis.readInt(); + this.commandManager.setCurrentTask(dis.readInt()); + + NBTTagCompound tag = Packet.readNBTTagCompound(dis); + readFromNBT(tag); + } + catch (IOException e) + { + FMLLog.severe("Failed to receive packet for Armbot"); + e.printStackTrace(); + } + } } /** @@ -351,9 +364,14 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult { this.disk = ItemStack.loadItemStackFromNBT(diskNBT); } + else + { + this.disk = null; + } this.rotationYaw = nbt.getFloat("yaw"); this.rotationPitch = nbt.getFloat("pitch"); + this.commandManager.setCurrentTask(nbt.getInteger("currentTask")); } /** @@ -374,6 +392,7 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult nbt.setTag("disk", diskNBT); nbt.setFloat("yaw", this.rotationYaw); nbt.setFloat("pitch", this.rotationPitch); + nbt.setInteger("currentTask", this.commandManager.getCurrentTask()); } @Override @@ -432,6 +451,7 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult @Override public void onInventoryChanged() { + this.commandManager.setCurrentTask(0); } @Override diff --git a/src/minecraft/assemblyline/common/machine/command/Command.java b/src/minecraft/assemblyline/common/machine/command/Command.java index 399df22e3..1ebd6e7dc 100644 --- a/src/minecraft/assemblyline/common/machine/command/Command.java +++ b/src/minecraft/assemblyline/common/machine/command/Command.java @@ -59,7 +59,7 @@ public abstract class Command * Called by the TaskManager to propagate tick updates * * @param ticks The amount of ticks this task has been running - * @return false if the task is finished and can be removed, true otherwise + * @return false if the task is finished and can be continued, true otherwise */ protected boolean doTask() { diff --git a/src/minecraft/assemblyline/common/machine/command/CommandDrop.java b/src/minecraft/assemblyline/common/machine/command/CommandDrop.java index d6c7e5812..ac9e3eb34 100644 --- a/src/minecraft/assemblyline/common/machine/command/CommandDrop.java +++ b/src/minecraft/assemblyline/common/machine/command/CommandDrop.java @@ -14,7 +14,6 @@ public class CommandDrop extends Command // TODO: Animate Armbot to move down and drop all items. this.tileEntity.grabbedEntities.clear(); - - return true; + return false; } } diff --git a/src/minecraft/assemblyline/common/machine/command/CommandGrab.java b/src/minecraft/assemblyline/common/machine/command/CommandGrab.java index 7eb403367..b880c228c 100644 --- a/src/minecraft/assemblyline/common/machine/command/CommandGrab.java +++ b/src/minecraft/assemblyline/common/machine/command/CommandGrab.java @@ -4,7 +4,7 @@ import java.util.List; import net.minecraft.entity.Entity; import net.minecraft.util.AxisAlignedBB; -import assemblyline.common.machine.armbot.TileEntityArmbot; +import universalelectricity.core.vector.Vector3; /** * Used by arms to search for entities in a region @@ -14,7 +14,7 @@ import assemblyline.common.machine.armbot.TileEntityArmbot; public class CommandGrab extends Command { - public static final int radius = 1; + public static final float radius = 0.5f; /** * The item to be collected. @@ -32,7 +32,8 @@ public class CommandGrab extends Command { super.doTask(); - List found = this.tileEntity.worldObj.getEntitiesWithinAABB(entityToInclude, AxisAlignedBB.getBoundingBox(tileEntity.xCoord - radius, tileEntity.yCoord - radius, tileEntity.zCoord - radius, tileEntity.xCoord + radius, tileEntity.yCoord + radius, tileEntity.zCoord + radius)); + Vector3 serachPosition = this.tileEntity.getHandPosition(); + List found = this.world.getEntitiesWithinAABB(this.entityToInclude, AxisAlignedBB.getBoundingBox(serachPosition.x - radius, serachPosition.y - radius, serachPosition.z - radius, serachPosition.x + radius, serachPosition.y + radius, serachPosition.z + radius)); if (found != null && found.size() > 0) { diff --git a/src/minecraft/assemblyline/common/machine/command/CommandManager.java b/src/minecraft/assemblyline/common/machine/command/CommandManager.java index 06b3180dd..04c77e61c 100644 --- a/src/minecraft/assemblyline/common/machine/command/CommandManager.java +++ b/src/minecraft/assemblyline/common/machine/command/CommandManager.java @@ -27,35 +27,28 @@ public class CommandManager */ try { - /* - * Iterator iter = tasks.iterator(); - * - * while (iter.hasNext()) { task = iter.next(); - * - * if (task.getTickInterval() > 0) { if (this.ticks % task.getTickInterval() == 0) { if - * (!task.doTask()) { task.onTaskEnd(); iter.remove(); } - * - * break; } } } - */ - if (this.tasks != null && this.tasks.size() > 0) + if (this.tasks.size() > 0) { - if (this.currentTask >= this.tasks.size()) - this.currentTask = 0; - if (this.currentTask < 0) - this.currentTask = 0; - - Command task = this.tasks.get(this.currentTask); - - if (this.currentTask != this.lastTask) + if (this.currentTask < this.tasks.size()) { - this.lastTask = this.currentTask; - task.onTaskStart(); - } + if (this.currentTask < 0) + { + this.currentTask = 0; + } - if (!task.doTask()) - { - task.onTaskEnd(); - this.currentTask++; + Command task = this.tasks.get(this.currentTask); + + if (this.currentTask != this.lastTask) + { + this.lastTask = this.currentTask; + task.onTaskStart(); + } + + if (!task.doTask()) + { + task.onTaskEnd(); + this.currentTask++; + } } } } @@ -81,7 +74,7 @@ public class CommandManager task.tileEntity = tileEntity; task.commandManager = this; task.setParameters(parameters); - tasks.add(task); + this.tasks.add(task); task.onTaskStart(); } diff --git a/src/minecraft/assemblyline/common/machine/command/CommandRepeat.java b/src/minecraft/assemblyline/common/machine/command/CommandRepeat.java index 313b90101..7c1b4388f 100644 --- a/src/minecraft/assemblyline/common/machine/command/CommandRepeat.java +++ b/src/minecraft/assemblyline/common/machine/command/CommandRepeat.java @@ -15,13 +15,21 @@ public class CommandRepeat extends Command public void onTaskStart() { - this.tasksToRepeat = this.getIntArg(0); + this.tasksToRepeat = Math.max(this.getIntArg(0), 0); } @Override protected boolean doTask() { - this.commandManager.setCurrentTask(this.commandManager.getCurrentTask() - this.tasksToRepeat); + if (this.tasksToRepeat > 0) + { + this.commandManager.setCurrentTask(this.commandManager.getCurrentTask() - this.tasksToRepeat); + } + else + { + this.commandManager.setCurrentTask(0); + } + return false; } } diff --git a/src/minecraft/assemblyline/common/machine/command/CommandRotate.java b/src/minecraft/assemblyline/common/machine/command/CommandRotate.java index 7eb322f62..77fbea78f 100644 --- a/src/minecraft/assemblyline/common/machine/command/CommandRotate.java +++ b/src/minecraft/assemblyline/common/machine/command/CommandRotate.java @@ -10,12 +10,12 @@ import cpw.mods.fml.relauncher.Side; */ public class CommandRotate extends Command { - public static final float ROTATION_SPEED = 1f; + public static final float ROTATION_SPEED = 1.3f; float targetRotation = 0; @Override public void onTaskStart() - { + { if (this.getArg(0) == null) { this.targetRotation = this.tileEntity.rotationYaw + 90; @@ -39,8 +39,13 @@ public class CommandRotate extends Command protected boolean doTask() { super.doTask(); + float rotationalDifference = Math.abs(this.tileEntity.rotationYaw - this.targetRotation); - if (Math.abs(this.targetRotation - this.tileEntity.rotationYaw) > 0.125) + if (rotationalDifference < 0.8) + { + this.tileEntity.rotationYaw = this.targetRotation; + } + else { if (this.tileEntity.rotationYaw > this.targetRotation) { @@ -51,17 +56,10 @@ public class CommandRotate extends Command this.tileEntity.rotationYaw += ROTATION_SPEED; } - // System.out.println("[" + ((FMLCommonHandler.instance().getEffectiveSide() == - // Side.SERVER) ? "S" : "C") + "]" + "Target: " + this.targetRotation + "; current: " + - // this.tileEntity.rotationYaw); - - if (Math.abs(this.tileEntity.rotationYaw - this.targetRotation) < 0.5) - this.tileEntity.rotationYaw = this.targetRotation; - return true; } - if (ticks < 80) // wait for a few ticks after rotating - return true; + + if (this.ticks < 80) { return true; } return false; }