Fixed rotation logic for armbot

This commit is contained in:
Robert 2013-12-26 16:57:20 -05:00
parent ae85dcb492
commit 5dba6d8cf5
3 changed files with 56 additions and 63 deletions

View file

@ -6,7 +6,6 @@ import java.util.Set;
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer; import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.MovingObjectPosition; import net.minecraft.util.MovingObjectPosition;
@ -49,7 +48,7 @@ public class BlockArmbot extends BlockAssembly
{ {
TileEntity tileEntity = world.getBlockTileEntity(x, y, z); TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
if (tileEntity != null && tileEntity instanceof IMultiBlock) if (tileEntity instanceof IMultiBlock)
{ {
return ((IMultiBlock) tileEntity).onActivated(player); return ((IMultiBlock) tileEntity).onActivated(player);
} }
@ -62,9 +61,10 @@ public class BlockArmbot extends BlockAssembly
{ {
TileEntity tileEntity = world.getBlockTileEntity(x, y, z); TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
if (tileEntity != null && tileEntity instanceof IMultiBlock) if (tileEntity instanceof TileEntityArmbot)
{ {
((IMultiBlock) tileEntity).onDestroy(tileEntity); ((TileEntityArmbot) tileEntity).onDestroy(tileEntity);
((TileEntityArmbot) tileEntity).drop("all");
} }
this.dropBlockAsItem_do(world, x, y, z, new ItemStack(this)); this.dropBlockAsItem_do(world, x, y, z, new ItemStack(this));

View file

@ -1,8 +1,5 @@
package com.builtbroken.assemblyline.armbot; package com.builtbroken.assemblyline.armbot;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityList; import net.minecraft.entity.EntityList;
import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.item.EntityItem;
@ -11,7 +8,6 @@ import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.packet.Packet; import net.minecraft.network.packet.Packet;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraftforge.common.ForgeDirection; import net.minecraftforge.common.ForgeDirection;
import universalelectricity.api.vector.Vector2; import universalelectricity.api.vector.Vector2;
@ -25,7 +21,6 @@ import com.builtbroken.assemblyline.armbot.command.TaskDrop;
import com.builtbroken.assemblyline.armbot.command.TaskGOTO; import com.builtbroken.assemblyline.armbot.command.TaskGOTO;
import com.builtbroken.assemblyline.armbot.command.TaskGrabItem; import com.builtbroken.assemblyline.armbot.command.TaskGrabItem;
import com.builtbroken.assemblyline.armbot.command.TaskReturn; import com.builtbroken.assemblyline.armbot.command.TaskReturn;
import com.builtbroken.assemblyline.armbot.command.TaskRotateBy;
import com.builtbroken.assemblyline.armbot.command.TaskRotateTo; import com.builtbroken.assemblyline.armbot.command.TaskRotateTo;
import com.builtbroken.assemblyline.machine.TileEntityAssembly; import com.builtbroken.assemblyline.machine.TileEntityAssembly;
import com.builtbroken.assemblyline.machine.encoder.ItemDisk; import com.builtbroken.assemblyline.machine.encoder.ItemDisk;
@ -41,16 +36,13 @@ import com.google.common.io.ByteArrayDataInput;
import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.network.Player; import cpw.mods.fml.common.network.Player;
import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.Side;
import dan200.computer.api.IComputerAccess;
import dan200.computer.api.ILuaContext;
import dan200.computer.api.IPeripheral;
public class TileEntityArmbot extends TileEntityAssembly implements IMultiBlock, IArmbot public class TileEntityArmbot extends TileEntityAssembly implements IMultiBlock, IArmbot
{ {
protected int ROTATION_SPEED = 2; protected int ROTATION_SPEED = 2;
/** The rotation of the arms. In Degrees. */ /** The rotation of the arms. In Degrees. */
protected int rotationPitch = 0, rotationYaw = 0; protected int targetPitch = 0, targetYaw = 0;
protected int actualPitch = 0, actualYaw = 0; protected int actualPitch = 0, actualYaw = 0;
protected boolean spawnEntity = false; protected boolean spawnEntity = false;
@ -71,11 +63,9 @@ public class TileEntityArmbot extends TileEntityAssembly implements IMultiBlock,
super(20); super(20);
programHelper = new ProgramHelper(this).setMemoryLimit(20); programHelper = new ProgramHelper(this).setMemoryLimit(20);
Program program = new Program(); Program program = new Program();
program.setTaskAt(0, 0, new TaskDrop()); program.setTaskAt(0, 0, new TaskRotateTo(180, 0));
program.setTaskAt(0, 1, new TaskRotateTo(180, 0)); program.setTaskAt(0, 1, new TaskReturn());
program.setTaskAt(0, 2, new TaskGrabItem()); program.setTaskAt(0, 2, new TaskGOTO(0, 0));
program.setTaskAt(0, 3, new TaskReturn());
program.setTaskAt(0, 4, new TaskGOTO(0, 0));
programHelper.setProgram(program); programHelper.setProgram(program);
} }
@ -111,11 +101,13 @@ public class TileEntityArmbot extends TileEntityAssembly implements IMultiBlock,
if (this.isFunctioning()) if (this.isFunctioning())
{ {
float preYaw = this.rotationYaw, prePitch = this.rotationPitch; float preYaw = this.targetYaw, prePitch = this.targetPitch;
if (!this.worldObj.isRemote && this.ticks % 3 == 0) if (!this.worldObj.isRemote && this.ticks % 10 == 0)
{ {
System.out.println("ArmbotTargetRotation Yaw:" + this.targetYaw + " Pitch:" + this.targetPitch);
System.out.println("ArmbotActualRotation Yaw:" + this.actualYaw + " Pitch:" + this.actualPitch);
this.programHelper.onUpdate(this.worldObj, new Vector3(this)); this.programHelper.onUpdate(this.worldObj, new Vector3(this));
if (this.rotationYaw != preYaw || this.rotationPitch != prePitch) if (this.targetYaw != preYaw || this.targetPitch != prePitch)
{ {
this.sendRotationPacket(); this.sendRotationPacket();
} }
@ -126,12 +118,19 @@ public class TileEntityArmbot extends TileEntityAssembly implements IMultiBlock,
public void updateRotation() public void updateRotation()
{ {
if (Math.abs(this.actualYaw - this.rotationYaw) > 1) //Clamp target angles
this.targetYaw = (int) MathHelper.clampAngleTo360(this.targetYaw);
if (this.targetPitch < 0)
this.targetPitch = 0;
if (this.targetPitch > 60)
this.targetPitch = 60;
//Handle change in yaw rotation
if (Math.abs(this.actualYaw - this.targetYaw) > 1)
{ {
float speedYaw; float speedYaw;
if (this.actualYaw > this.rotationYaw) if (this.actualYaw > this.targetYaw)
{ {
if (Math.abs(this.actualYaw - this.rotationYaw) >= 180) if (Math.abs(this.actualYaw - this.targetYaw) >= 180)
{ {
speedYaw = this.ROTATION_SPEED; speedYaw = this.ROTATION_SPEED;
} }
@ -142,7 +141,7 @@ public class TileEntityArmbot extends TileEntityAssembly implements IMultiBlock,
} }
else else
{ {
if (Math.abs(this.actualYaw - this.rotationYaw) >= 180) if (Math.abs(this.actualYaw - this.targetYaw) >= 180)
{ {
speedYaw = -this.ROTATION_SPEED; speedYaw = -this.ROTATION_SPEED;
} }
@ -154,40 +153,36 @@ public class TileEntityArmbot extends TileEntityAssembly implements IMultiBlock,
this.actualYaw += speedYaw; this.actualYaw += speedYaw;
this.rotationYaw = (int) MathHelper.clampAngleTo360(this.rotationYaw); if (Math.abs(this.actualYaw - this.targetYaw) < this.ROTATION_SPEED)
if (Math.abs(this.actualYaw - this.rotationYaw) < this.ROTATION_SPEED)
{ {
this.actualYaw = this.rotationYaw; this.actualYaw = this.targetYaw;
} }
this.playRotationSound(); this.playRotationSound();
} }
//Handle change in pitch rotation
if (Math.abs(this.actualPitch - this.rotationPitch) > 1) if (Math.abs(this.actualPitch - this.targetPitch) > 1)
{ {
float speedPitch; if (this.actualPitch > this.targetPitch)
if (this.actualPitch > this.rotationPitch)
{ {
speedPitch = -this.ROTATION_SPEED; this.actualPitch -= this.ROTATION_SPEED;
} }
else else
{ {
speedPitch = this.ROTATION_SPEED; this.actualPitch += this.ROTATION_SPEED;
} }
this.actualPitch += speedPitch; if (Math.abs(this.actualPitch - this.targetPitch) < this.ROTATION_SPEED)
this.rotationPitch = (int) MathHelper.clampAngle(this.rotationPitch, 0, 60);
if (Math.abs(this.actualPitch - this.rotationPitch) < this.ROTATION_SPEED)
{ {
this.actualPitch = this.rotationPitch; this.actualPitch = this.targetPitch;
} }
this.playRotationSound(); this.playRotationSound();
} }
//Clamp actual angles angles
this.rotationYaw = (int) MathHelper.clampAngleTo360(this.rotationYaw); this.actualYaw = (int) MathHelper.clampAngleTo360(this.actualYaw);
this.rotationPitch = (int) MathHelper.clampAngle(this.rotationPitch, 0, 60); if (this.actualPitch < 0)
this.actualPitch = 0;
if (this.actualPitch > 60)
this.actualPitch = 60;
} }
public void playRotationSound() public void playRotationSound()
@ -250,8 +245,8 @@ public class TileEntityArmbot extends TileEntityAssembly implements IMultiBlock,
{ {
super.readFromNBT(nbt); super.readFromNBT(nbt);
this.rotationYaw = nbt.getInteger("armYaw"); this.targetYaw = nbt.getInteger("armYaw");
this.rotationPitch = nbt.getInteger("armPitch"); this.targetPitch = nbt.getInteger("armPitch");
this.actualYaw = nbt.getInteger("armYawActual"); this.actualYaw = nbt.getInteger("armYawActual");
this.actualPitch = nbt.getInteger("armPitchActual"); this.actualPitch = nbt.getInteger("armPitchActual");
@ -280,8 +275,8 @@ public class TileEntityArmbot extends TileEntityAssembly implements IMultiBlock,
{ {
super.writeToNBT(nbt); super.writeToNBT(nbt);
nbt.setInteger("armYaw", this.rotationYaw); nbt.setInteger("armYaw", this.targetYaw);
nbt.setInteger("armPitch", this.rotationPitch); nbt.setInteger("armPitch", this.targetPitch);
nbt.setInteger("armYawActual", this.actualYaw); nbt.setInteger("armYawActual", this.actualYaw);
nbt.setInteger("armPitchActual", this.actualPitch); nbt.setInteger("armPitchActual", this.actualPitch);
@ -306,12 +301,12 @@ public class TileEntityArmbot extends TileEntityAssembly implements IMultiBlock,
@Override @Override
public Packet getDescriptionPacket() public Packet getDescriptionPacket()
{ {
return PacketHandler.instance().getTilePacket(this.getChannel(), "armbot", this, this.functioning, this.rotationYaw, this.rotationPitch); return PacketHandler.instance().getTilePacket(this.getChannel(), "armbot", this, this.functioning, this.targetYaw, this.targetPitch);
} }
public void sendRotationPacket() public void sendRotationPacket()
{ {
PacketHandler.instance().sendPacketToClients(PacketHandler.instance().getPacket(this.getChannel(), "arbotRotation", this.rotationYaw, this.rotationPitch, this.actualYaw, this.actualPitch), worldObj, new Vector3(this).translate(new Vector3(.5f, 1f, .5f)), 40); PacketHandler.instance().sendPacketToClients(PacketHandler.instance().getPacket(this.getChannel(), "arbotRotation", this.targetYaw, this.targetPitch, this.actualYaw, this.actualPitch), worldObj, new Vector3(this).translate(new Vector3(.5f, 1f, .5f)), 40);
} }
@Override @Override
@ -324,14 +319,14 @@ public class TileEntityArmbot extends TileEntityAssembly implements IMultiBlock,
if (id.equalsIgnoreCase("armbot")) if (id.equalsIgnoreCase("armbot"))
{ {
this.functioning = dis.readBoolean(); this.functioning = dis.readBoolean();
this.rotationYaw = dis.readInt(); this.targetYaw = dis.readInt();
this.rotationPitch = dis.readInt(); this.targetPitch = dis.readInt();
return true; return true;
} }
else if (id.equalsIgnoreCase("arbotRotation")) else if (id.equalsIgnoreCase("arbotRotation"))
{ {
this.rotationYaw = dis.readInt(); this.targetYaw = dis.readInt();
this.rotationPitch = dis.readInt(); this.targetPitch = dis.readInt();
this.actualYaw = dis.readInt(); this.actualYaw = dis.readInt();
this.actualPitch = dis.readInt(); this.actualPitch = dis.readInt();
return true; return true;
@ -463,8 +458,8 @@ public class TileEntityArmbot extends TileEntityAssembly implements IMultiBlock,
{ {
if (!this.worldObj.isRemote) if (!this.worldObj.isRemote)
{ {
this.rotationYaw = (int) yaw; this.targetYaw = (int) yaw;
this.rotationPitch = (int) pitch; this.targetPitch = (int) pitch;
return true; return true;
} }
return false; return false;
@ -475,23 +470,23 @@ public class TileEntityArmbot extends TileEntityAssembly implements IMultiBlock,
{ {
if (direction == ForgeDirection.SOUTH) if (direction == ForgeDirection.SOUTH)
{ {
this.rotationYaw = 0; this.targetYaw = 0;
return true; return true;
} }
else if (direction == ForgeDirection.EAST) else if (direction == ForgeDirection.EAST)
{ {
this.rotationYaw = 90; this.targetYaw = 90;
return true; return true;
} }
else if (direction == ForgeDirection.NORTH) else if (direction == ForgeDirection.NORTH)
{ {
this.rotationYaw = 180; this.targetYaw = 180;
return true; return true;
} }
else if (direction == ForgeDirection.WEST) else if (direction == ForgeDirection.WEST)
{ {
this.rotationYaw = 270; this.targetYaw = 270;
return true; return true;
} }
return false; return false;

View file

@ -55,9 +55,7 @@ public class TaskRotateTo extends TaskBaseArmbot
if (super.onUpdate() == ProcessReturn.CONTINUE) if (super.onUpdate() == ProcessReturn.CONTINUE)
{ {
((IArmbot) this.program.getMachine()).moveArmTo(this.targetRotationYaw, this.targetRotationPitch); ((IArmbot) this.program.getMachine()).moveArmTo(this.targetRotationYaw, this.targetRotationPitch);
int deltaYaw = (int) MathHelper.shortestAngleTo360(((IArmbot) this.program.getMachine()).getRotation().intX(), this.targetRotationYaw); return ((IArmbot) this.program.getMachine()).getRotation().intX() != this.targetRotationYaw || ((IArmbot) this.program.getMachine()).getRotation().intY() != this.targetRotationPitch ? ProcessReturn.CONTINUE : ProcessReturn.DONE;
int deltaPitch = (int) MathHelper.shortestAngleTo360(((IArmbot) this.program.getMachine()).getRotation().intY(), this.targetRotationPitch);
return deltaYaw == 0 && deltaPitch == 0 ? ProcessReturn.CONTINUE : ProcessReturn.DONE;
} }
return ProcessReturn.GENERAL_ERROR; return ProcessReturn.GENERAL_ERROR;
} }