tcr/java/src/game/ai/EntityAIAvoidEntity.java
2025-03-12 18:13:11 +01:00

126 lines
4.1 KiB
Java
Executable file

package game.ai;
import java.util.List;
import java.util.function.Predicate;
import game.Predicates;
import game.entity.Entity;
import game.entity.types.EntityLiving;
import game.pathfinding.PathEntity;
import game.pathfinding.PathNavigate;
import game.world.Vec3;
public class EntityAIAvoidEntity<T extends Entity> extends EntityAIBase
{
private final Predicate<Entity> canBeSeenSelector;
/** The entity we are attached to */
protected EntityLiving theEntity;
private double farSpeed;
private double nearSpeed;
protected T closestLivingEntity;
private float avoidDistance;
/** The PathEntity of our entity */
private PathEntity entityPathEntity;
/** The PathNavigate of our entity */
private PathNavigate entityPathNavigate;
private Class<T> classToAvoid;
private Predicate <? super T > avoidTargetSelector;
public EntityAIAvoidEntity(EntityLiving theEntityIn, Class<T> classToAvoidIn, float avoidDistanceIn, double farSpeedIn, double nearSpeedIn)
{
this(theEntityIn, classToAvoidIn, Predicates.<T>alwaysTrue(), avoidDistanceIn, farSpeedIn, nearSpeedIn);
}
public EntityAIAvoidEntity(EntityLiving theEntityIn, Class<T> classToAvoidIn, Predicate <? super T > avoidTargetSelectorIn, float avoidDistanceIn, double farSpeedIn, double nearSpeedIn)
{
this.canBeSeenSelector = new Predicate<Entity>()
{
public boolean test(Entity p_apply_1_)
{
return p_apply_1_.isEntityAlive() && EntityAIAvoidEntity.this.theEntity.getEntitySenses().canSee(p_apply_1_);
}
};
this.theEntity = theEntityIn;
this.classToAvoid = classToAvoidIn;
this.avoidTargetSelector = avoidTargetSelectorIn;
this.avoidDistance = avoidDistanceIn;
this.farSpeed = farSpeedIn;
this.nearSpeed = nearSpeedIn;
this.entityPathNavigate = theEntityIn.getNavigator();
this.setMutexBits(1);
}
/**
* Returns whether the EntityAIBase should begin execution.
*/
public boolean shouldExecute()
{
List<T> list = this.theEntity.worldObj.<T>getEntitiesWithinAABB(this.classToAvoid, this.theEntity.getEntityBoundingBox().expand((double)this.avoidDistance, 3.0D, (double)this.avoidDistance), Predicates.and(new Predicate[] {this.canBeSeenSelector, this.avoidTargetSelector}));
if (list.isEmpty())
{
return false;
}
else
{
this.closestLivingEntity = list.get(0);
Vec3 vec3 = RandomPositionGenerator.findRandomTargetBlockAwayFrom(this.theEntity, 16, 7, new Vec3(this.closestLivingEntity.posX, this.closestLivingEntity.posY, this.closestLivingEntity.posZ));
if (vec3 == null)
{
return false;
}
else if (this.closestLivingEntity.getDistanceSq(vec3.xCoord, vec3.yCoord, vec3.zCoord) < this.closestLivingEntity.getDistanceSqToEntity(this.theEntity))
{
return false;
}
else
{
this.entityPathEntity = this.entityPathNavigate.getPathToXYZ(vec3.xCoord, vec3.yCoord, vec3.zCoord);
return this.entityPathEntity == null ? false : this.entityPathEntity.isDestinationSame(vec3);
}
}
}
/**
* Returns whether an in-progress EntityAIBase should continue executing
*/
public boolean continueExecuting()
{
return !this.entityPathNavigate.noPath();
}
/**
* Execute a one shot task or start executing a continuous task
*/
public void startExecuting()
{
this.entityPathNavigate.setPath(this.entityPathEntity, this.farSpeed);
}
/**
* Resets the task
*/
public void resetTask()
{
this.closestLivingEntity = null;
}
/**
* Updates the task
*/
public void updateTask()
{
if (this.theEntity.getDistanceSqToEntity(this.closestLivingEntity) < 49.0D)
{
this.theEntity.getNavigator().setSpeed(this.nearSpeed);
}
else
{
this.theEntity.getNavigator().setSpeed(this.farSpeed);
}
}
}