Got Armbot working nicely

This commit is contained in:
Henry Mao 2013-01-12 23:55:11 +08:00
parent eb323c77ec
commit f02f085599
8 changed files with 114 additions and 155 deletions

View file

@ -76,122 +76,9 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult
ElectricityConnections.registerConnector(this, EnumSet.range(ForgeDirection.DOWN, ForgeDirection.EAST));
}
private void updateCommands(boolean reset, int currentTask)
{
if (this.disk != null)
{
if (reset)
{
try
{
this.commandManager.clearTasks();
List<String> commands = ItemDisk.getCommands(this.disk);
for (String commandString : commands)
{
String commandName = commandString.split(" ")[0];
Class<? extends Command> command = Command.getCommand(commandName);
if (command != null)
{
Command newCommand = command.newInstance();
newCommand.world = this.worldObj;
newCommand.tileEntity = this;
List<String> commandParameters = new ArrayList<String>();
for (String param : commandString.split(" "))
{
if (!param.equals(commandName))
{
commandParameters.add(param);
}
}
this.commandManager.addTask(this, newCommand, commandParameters.toArray(new String[0]));
}
}
this.commandManager.setCurrentTask(currentTask);
}
catch (Exception e)
{
e.printStackTrace();
}
}
else
{
if (this.commandManager.hasTasks())
{
if (this.commandManager.getCommands().get(0) instanceof CommandReturn)
{
this.commandManager.clearTasks();
}
}
if (!this.commandManager.hasTasks())
{
try
{
List<String> commands = ItemDisk.getCommands(this.disk);
for (String commandString : commands)
{
String commandName = commandString.split(" ")[0];
Class<? extends Command> command = Command.getCommand(commandName);
if (command != null)
{
Command newCommand = command.newInstance();
newCommand.world = this.worldObj;
newCommand.tileEntity = this;
List<String> commandParameters = new ArrayList<String>();
for (String param : commandString.split(" "))
{
if (!param.equals(commandName))
{
commandParameters.add(param);
}
}
this.commandManager.addTask(this, newCommand, commandParameters.toArray(new String[0]));
}
}
this.commandManager.setCurrentTask(0);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}
else
{
this.commandManager.clearTasks();
if (this.grabbedEntities.size() > 0)
{
this.commandManager.addTask(this, new CommandDrop());
}
else
{
this.commandManager.addTask(this, new CommandReturn());
}
this.commandManager.setCurrentTask(0);
}
}
@Override
public void onUpdate()
{
updateCommands(false, -1);
if (this.isRunning())
{
Vector3 handPosition = this.getHandPosition();
@ -210,6 +97,22 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult
}
}
if (this.disk == null)
{
this.commandManager.clear();
if (this.grabbedEntities.size() > 0)
{
this.commandManager.addCommand(this, CommandDrop.class);
}
else
{
this.commandManager.addCommand(this, CommandReturn.class);
}
this.commandManager.setCurrentTask(0);
}
this.commandManager.onUpdate();
// keep it within 0 - 360 degrees so ROTATE commands work properly
@ -233,7 +136,7 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult
if (FMLCommonHandler.instance().getEffectiveSide() == Side.SERVER && this.ticks % 20 == 0)
{
PacketManager.sendPacketToClients(this.getDescriptionPacket());// , this.worldObj, new Vector3(this), 20);
PacketManager.sendPacketToClients(this.getDescriptionPacket(), this.worldObj, new Vector3(this), 50);
}
}
@ -292,12 +195,9 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult
x = dis.readInt();
y = dis.readInt();
z = dis.readInt();
int curTask = dis.readInt();
this.commandManager.setCurrentTask(dis.readInt());
NBTTagCompound tag = Packet.readNBTTagCompound(dis);
readFromNBT(tag);
updateCommands(true, curTask);
}
catch (IOException e)
{
@ -381,11 +281,7 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult
public void setInventorySlotContents(int par1, ItemStack par2ItemStack)
{
this.disk = par2ItemStack;
if (par2ItemStack != null && par2ItemStack.stackSize > this.getInventoryStackLimit())
{
par2ItemStack.stackSize = this.getInventoryStackLimit();
}
this.onInventoryChanged();
}
@Override
@ -490,7 +386,6 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult
}
this.setInventorySlotContents(0, null);
onInventoryChanged();
return true;
}
else
@ -500,7 +395,6 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult
if (player.getCurrentEquippedItem().getItem() instanceof ItemDisk)
{
this.setInventorySlotContents(0, player.getCurrentEquippedItem());
onInventoryChanged();
player.inventory.setInventorySlotContents(player.inventory.currentItem, null);
return true;
}
@ -513,7 +407,34 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult
@Override
public void onInventoryChanged()
{
this.commandManager.setCurrentTask(0);
this.commandManager.clear();
if (this.disk != null)
{
List<String> commands = ItemDisk.getCommands(this.disk);
for (String commandString : commands)
{
String commandName = commandString.split(" ")[0];
Class<? extends Command> command = Command.getCommand(commandName);
if (command != null)
{
List<String> commandParameters = new ArrayList<String>();
for (String param : commandString.split(" "))
{
if (!param.equals(commandName))
{
commandParameters.add(param);
}
}
this.commandManager.addCommand(this, command, commandParameters.toArray(new String[0]));
}
}
}
}
@Override

View file

@ -42,7 +42,7 @@ public abstract class Command
{
return COMMANDS.get(command.toLowerCase());
}
public static String getCommandName(Class<? extends Command> command)
{
return REVERSE_LOOKUP.get(command);
@ -72,7 +72,7 @@ public abstract class Command
protected boolean doTask()
{
this.ticks++;
return true;
return false;
}
public void onTaskStart()
@ -96,6 +96,11 @@ public abstract class Command
this.parameters = strings;
}
public String[] getArgs()
{
return this.parameters;
}
/**
* Some functions to help get parameter arguments.
*/
@ -125,6 +130,6 @@ public abstract class Command
public void writeToNBT(NBTTagCompound taskCompound)
{
}
}

View file

@ -31,7 +31,6 @@ public class CommandGrab extends Command
protected boolean doTask()
{
super.doTask();
Vector3 serachPosition = this.tileEntity.getHandPosition();
List<Entity> 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));

View file

@ -11,10 +11,14 @@ public class CommandIdle extends Command
public void onTaskStart()
{
super.onTaskStart();
if (this.getIntArg(0) > 0)
{
this.idleTime = this.getIntArg(0);
}
System.out.println("INITIATE");
}
protected boolean doTask()

View file

@ -28,6 +28,8 @@ public class CommandManager
{
if (this.tasks.size() > 0)
{
System.out.println(this.lastTask + " vs " + currentTask + ": " + this.tasks.size());
if (this.currentTask < this.tasks.size())
{
if (this.currentTask < 0)
@ -45,8 +47,11 @@ public class CommandManager
if (!task.doTask())
{
task.onTaskEnd();
// End the task and reinitiate it into a new class to make sure it is fresh.
int tempCurrentTask = this.currentTask;
this.currentTask++;
task.onTaskEnd();
this.tasks.set(tempCurrentTask, this.getNewCommand(task.tileEntity, task.getClass(), task.getArgs()));
}
}
}
@ -60,25 +65,46 @@ public class CommandManager
this.ticks++;
}
/**
* Used to register Tasks for a TileEntity, executes onTaskStart for the Task after registering it
*
* @param tileEntity TE instance to register the task for
* @param task Task instance to register
*/
public void addTask(TileEntityArmbot tileEntity, Command task, String[] parameters)
public Command getNewCommand(TileEntityArmbot tileEntity, Class<? extends Command> commandClass, String[] parameters)
{
task.world = tileEntity.worldObj;
task.tileEntity = tileEntity;
task.commandManager = this;
task.setParameters(parameters);
this.tasks.add(task);
task.onTaskStart();
try
{
Command newCommand = commandClass.newInstance();
newCommand.world = tileEntity.worldObj;
newCommand.tileEntity = tileEntity;
newCommand.commandManager = this;
newCommand.setParameters(parameters);
return newCommand;
}
catch (Exception e)
{
FMLLog.severe("Failed to add command");
e.printStackTrace();
}
return null;
}
public void addTask(TileEntityArmbot tileEntity, Command task)
/**
* Used to register Tasks for a TileEntity, executes onTaskStart for the Task after registering
* it
*
* @param tileEntity TE instance to register the task for
* @param newCommand Task instance to register
*/
public void addCommand(TileEntityArmbot tileEntity, Class<? extends Command> commandClass, String[] parameters)
{
this.addTask(tileEntity, task, new String[0]);
Command newCommand = this.getNewCommand(tileEntity, commandClass, parameters);
if (newCommand != null)
{
this.tasks.add(newCommand);
}
}
public void addCommand(TileEntityArmbot tileEntity, Class<? extends Command> task)
{
this.addCommand(tileEntity, task, new String[0]);
}
/**
@ -94,14 +120,20 @@ public class CommandManager
return tasks;
}
public void clearTasks()
/**
* Resets the command manager.
*/
public void clear()
{
this.tasks.clear();
this.currentTask = 0;
this.lastTask = -1;
this.ticks = 0;
}
public void setCurrentTask(int i)
{
this.currentTask = Math.max(Math.min(i, this.tasks.size() - 1), 0);
this.currentTask = Math.max(Math.min(i, this.tasks.size()), 0);
}
public int getCurrentTask()

View file

@ -19,7 +19,7 @@ public class CommandRepeat extends Command
}
@Override
protected boolean doTask()
public void onTaskEnd()
{
if (this.tasksToRepeat > 0)
{
@ -29,7 +29,5 @@ public class CommandRepeat extends Command
{
this.commandManager.setCurrentTask(0);
}
return false;
}
}

View file

@ -13,7 +13,7 @@ public class CommandReturn extends Command
/**
* Move the arm rotation to idle position if the machine is not idling
*/
if (Math.abs(this.tileEntity.rotationPitch - IDLE_ROTATION_PITCH) > 0.001 || Math.abs(this.tileEntity.rotationYaw - IDLE_ROTATION_YAW) > 0.001)
if (Math.abs(this.tileEntity.rotationPitch - IDLE_ROTATION_PITCH) > 0.01 || Math.abs(this.tileEntity.rotationYaw - IDLE_ROTATION_YAW) > 0.01)
{
if (Math.abs(IDLE_ROTATION_PITCH - this.tileEntity.rotationPitch) > 0.125)
this.tileEntity.rotationPitch += (IDLE_ROTATION_PITCH - this.tileEntity.rotationPitch) * 0.05;
@ -28,9 +28,9 @@ public class CommandReturn extends Command
this.tileEntity.rotationYaw += Math.signum(IDLE_ROTATION_YAW - this.tileEntity.rotationYaw) * (0.125 * 0.05);
if (Math.abs(this.tileEntity.rotationYaw - IDLE_ROTATION_YAW) < 0.0125)
this.tileEntity.rotationYaw = IDLE_ROTATION_YAW;
return true;
}
return false;
}

View file

@ -18,7 +18,7 @@ public class CommandRotate extends Command
public void onTaskStart()
{
super.onTaskStart();
this.ticks = 0;
if (this.getArg(0) == null)
{
this.targetRotation = this.tileEntity.rotationYaw + 90;
@ -44,7 +44,7 @@ public class CommandRotate extends Command
super.doTask();
float rotationalDifference = Math.abs(this.tileEntity.rotationYaw - this.targetRotation);
if (rotationalDifference < 0.8)
if (rotationalDifference < 0.1)
{
this.tileEntity.rotationYaw = this.targetRotation;
}