Mekanism-tilera-Edition/src/main/java/mekanism/common/RobitAIPickup.java

153 lines
3.8 KiB
Java
Raw Normal View History

2013-11-22 00:51:22 +01:00
package mekanism.common;
import java.util.Iterator;
import java.util.List;
import mekanism.common.entity.EntityRobit;
2013-11-22 00:51:22 +01:00
import net.minecraft.entity.ai.EntityAIBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.pathfinding.PathNavigate;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;
/*
* Written by pixlepix (I'm in mekanism! Yay!)
* Boilerplate copied from RobitAIFollow
*/
2013-11-22 00:51:22 +01:00
public class RobitAIPickup extends EntityAIBase
{
/** The robit entity. */
private EntityRobit theRobit;
/** The world the robit is located in. */
private World theWorld;
/** How fast the robit can travel. */
private float moveSpeed;
/** The robit's pathfinder. */
private PathNavigate thePathfinder;
/** The ticker for updates. */
private int ticker;
/** Whether or not this robit avoids water. */
private boolean avoidWater;
private EntityItem closest;
public RobitAIPickup(EntityRobit entityRobit, float speed)
{
theRobit = entityRobit;
2014-05-29 20:01:43 +02:00
theWorld = entityRobit.worldObj;
2013-11-22 00:51:22 +01:00
moveSpeed = speed;
thePathfinder = entityRobit.getNavigator();
}
@Override
public boolean shouldExecute()
{
if(!theRobit.getDropPickup())
{
2013-11-22 00:51:22 +01:00
return false;
}
2016-07-06 00:23:22 +02:00
2013-11-22 02:31:28 +01:00
if(closest != null && closest.getDistanceSqToEntity(closest) > 100 && thePathfinder.getPathToXYZ(closest.posX, closest.posY, closest.posZ) != null)
{
2013-11-22 00:51:22 +01:00
return true;
}
2014-05-29 20:01:43 +02:00
List items = theRobit.worldObj.getEntitiesWithinAABB(EntityItem.class, AxisAlignedBB.getBoundingBox(theRobit.posX-10, theRobit.posY-10, theRobit.posZ-10, theRobit.posX+10, theRobit.posY+10, theRobit.posZ+10));
2013-11-22 02:31:28 +01:00
Iterator iter = items.iterator();
2013-11-22 00:51:22 +01:00
//Cached for slight performance
2014-05-29 20:01:43 +02:00
double closestDistance = -1;
while(iter.hasNext())
{
2013-11-22 02:31:28 +01:00
EntityItem entity = (EntityItem)iter.next();
2013-11-22 02:31:28 +01:00
double distance = theRobit.getDistanceToEntity(entity);
if(distance <= 10)
{
2013-11-22 02:31:28 +01:00
if(closestDistance == -1 || distance < closestDistance)
{
2013-11-22 02:31:28 +01:00
if(thePathfinder.getPathToXYZ(entity.posX, entity.posY, entity.posZ) != null)
{
2013-11-22 02:31:28 +01:00
closest = entity;
closestDistance = distance;
2013-11-22 00:51:22 +01:00
}
}
}
}
2016-07-06 00:23:22 +02:00
if(closest == null || closest.isDead)
{
//No valid items
return false;
}
return true;
2013-11-22 00:51:22 +01:00
}
@Override
public boolean continueExecuting()
{
2016-07-06 00:23:22 +02:00
return !closest.isDead && !thePathfinder.noPath() && theRobit.getDistanceSqToEntity(closest) > 100 && theRobit.getDropPickup() && theRobit.getEnergy() > 0 && closest.worldObj.provider.dimensionId == theRobit.worldObj.provider.dimensionId;
2013-11-22 00:51:22 +01:00
}
@Override
public void startExecuting()
{
ticker = 0;
avoidWater = theRobit.getNavigator().getAvoidsWater();
theRobit.getNavigator().setAvoidsWater(false);
}
@Override
public void resetTask()
{
thePathfinder.clearPathEntity();
theRobit.getNavigator().setAvoidsWater(avoidWater);
}
@Override
public void updateTask()
{
if(!theRobit.getDropPickup())
{
2013-11-22 00:51:22 +01:00
return;
}
2016-07-06 00:23:22 +02:00
theRobit.getLookHelper().setLookPositionWithEntity(closest, 6.0F, theRobit.getVerticalFaceSpeed()/10);
2013-11-22 00:51:22 +01:00
if(--ticker <= 0)
{
ticker = 10;
if(!thePathfinder.tryMoveToEntityLiving(closest, moveSpeed))
{
if(theRobit.getDistanceSqToEntity(closest) >= 144.0D)
{
int x = MathHelper.floor_double(closest.posX) - 2;
int y = MathHelper.floor_double(closest.posZ) - 2;
int z = MathHelper.floor_double(closest.boundingBox.minY);
for(int l = 0; l <= 4; ++l)
{
for(int i1 = 0; i1 <= 4; ++i1)
{
2014-05-29 20:01:43 +02:00
if((l < 1 || i1 < 1 || l > 3 || i1 > 3) && theWorld.doesBlockHaveSolidTopSurface(theWorld, x + l, z - 1, y + i1) && !theWorld.getBlock(x + l, z, y + i1).isNormalCube() && !theWorld.getBlock(x + l, z + 1, y + i1).isNormalCube())
2013-11-22 00:51:22 +01:00
{
theRobit.setLocationAndAngles((x + l) + 0.5F, z, (y + i1) + 0.5F, theRobit.rotationYaw, theRobit.rotationPitch);
thePathfinder.clearPathEntity();
return;
}
}
}
}
}
}
}
}