Pathing improvements.

EntityAIReturn:
-Fixed NPC still creating a new path when close to the NPC's home.

EntityNPCInterface:
-Fixed flying NPCs having their home Y position dip into the ground.
-NPCs update their move helpers and navigators when they can't breathe.

FlyingMoveHelper:
-Reduced calls to Math.atan2 (very expensive operation to be unnecessarily calling every tick)

FlyPathFinder:
-Water opens an OPEN path point iff the NPC can swim, not iff the NPC's drowning type != 1.

NPCPath:
-Made PathPoint occurrences use NPCPathPoint

PathNavigateFlying:
-A slightly larger area of blocks with respect to width is checked om rayTraceBlocks(), and the function now also correctly checks for liquids.
This commit is contained in:
Luis C.M committed 2022-11-13 23:49:49 -06:00
1 parent e1c9b69335
commit 51b412a0bf
6 files changed
+47 -32

No files matched your search

@@ -137,7 +137,7 @@ public class EntityAIReturn extends EntityAIBase
distance /= 2;
if(distance > 2){
Vec3 start = Vec3.createVectorHelper(posX, posY, posZ);
Vec3 pos = RandomPositionGeneratorAlt.findRandomTargetBlockTowards(npc, distance,distance /2 > 7? 7:distance/2, start);
Vec3 pos = RandomPositionGeneratorAlt.findRandomTargetBlockTowards(npc, distance, Math.min(distance / 2, 7), start);
if(pos != null){
posX = pos.xCoord;
posY = pos.yCoord;
@@ -146,7 +146,7 @@ public class EntityAIReturn extends EntityAIBase
}
}
if (range > 1f) {
if (range > 1.1f) {
npc.getNavigator().clearPathEntity();
npc.getNavigator().tryMoveToXYZ(posX, posY, posZ, 1);
}
@@ -737,7 +737,7 @@ public class FlyPathFinder extends PathFinder
{
if (material == Material.water)
{
return drowningType == 1 ? PathNodeType.WATER : PathNodeType.OPEN;
return ((EntityNPCInterface)this.theEntity).ai.canSwim ? PathNodeType.OPEN : PathNodeType.WATER;
}
else if (material == Material.lava)
{
@@ -51,9 +51,8 @@ public class FlyingMoveHelper extends EntityMoveHelper{
}
this.entity.velocityChanged = true;
this.entity.rotationYaw = this.limitAngle(this.entity.rotationYaw,(float) ((Math.atan2(-d0, -d2) + Math.PI) * -(180F / Math.PI)),20.0F);
}
this.entity.rotationYaw = this.limitAngle(this.entity.rotationYaw,(float) ((Math.atan2(-d0, -d2) + Math.PI) * -(180F / Math.PI)),20.0F);
}
}
@@ -6,13 +6,13 @@ import net.minecraft.pathfinding.PathPoint;
import net.minecraft.util.Vec3;
public class NPCPath extends PathEntity{
public final PathPoint[] points;
public final NPCPathPoint[] points;
/** PathEntity Array Index the Entity is currently targeting */
public int currentPathIndex;
/** The total length of the path */
public int pathLength;
public NPCPath(PathPoint[] pointsIn)
public NPCPath(NPCPathPoint[] pointsIn)
{
super(pointsIn);
this.points = pointsIn;
@@ -38,7 +38,7 @@ public class NPCPath extends PathEntity{
/**
* returns the last PathPoint of the Array
*/
public PathPoint getFinalPathPoint()
public NPCPathPoint getFinalPathPoint()
{
return this.pathLength > 0 ? this.points[this.pathLength - 1] : null;
}
@@ -46,7 +46,7 @@ public class NPCPath extends PathEntity{
/**
* return the PathPoint located at the specified PathIndex, usually the current one
*/
public PathPoint getPathPointFromIndex(int p_75877_1_)
public NPCPathPoint getPathPointFromIndex(int p_75877_1_)
{
return this.points[p_75877_1_];
}
@@ -77,7 +77,7 @@ public class NPCPath extends PathEntity{
public Vec3 getVectorFromIndex(Entity entity, int index)
{
double d0 = (double)this.points[index].xCoord + (double)((int)(entity.width + 1.0F)) * 0.5D;
double d1 = (double)this.points[index].yCoord;
double d1 = (double)this.points[index].yCoord + 0.05D;
double d2 = (double)this.points[index].zCoord + (double)((int)(entity.width + 1.0F)) * 0.5D;
return Vec3.createVectorHelper(d0, d1, d2);
}
@@ -92,7 +92,7 @@ public class NPCPath extends PathEntity{
public Vec3 getCurrentPos()
{
PathPoint pathpoint = this.points[this.currentPathIndex];
NPCPathPoint pathpoint = this.points[this.currentPathIndex];
return Vec3.createVectorHelper((double)pathpoint.xCoord, (double)pathpoint.yCoord, (double)pathpoint.zCoord);
}
@@ -129,7 +129,7 @@ public class NPCPath extends PathEntity{
*/
public boolean isDestinationSame(Vec3 vec3)
{
PathPoint pathpoint = this.getFinalPathPoint();
NPCPathPoint pathpoint = this.getFinalPathPoint();
return pathpoint == null ? false : pathpoint.xCoord == (int)vec3.xCoord && pathpoint.zCoord == (int)vec3.zCoord;
}
}
@@ -1,6 +1,7 @@
package noppes.npcs.ai.pathfinder;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.SharedMonsterAttributes;
@@ -434,7 +435,7 @@ public class PathNavigateFlying extends PathNavigate {
{
Vec3 pos17 = Vec3.createVectorHelper(startPos.xCoord,startPos.yCoord, startPos.zCoord);
Vec3 pos18 = Vec3.createVectorHelper(endPos.xCoord,endPos.yCoord, endPos.zCoord);
if (this.rayTraceBlocks(pos17, pos18, false, false, false, width, height))
if (this.rayTraceBlocks(pos17, pos18, true, false, false, width, height))
return false;
return true;
@@ -626,7 +627,7 @@ public class PathNavigateFlying extends PathNavigate {
}
boolean value = false;
for (int x = 1 - width; x < width; x++) {
for (int x = -width; x <= width; x++) {
if (value) {
break;
}
@@ -634,7 +635,11 @@ public class PathNavigateFlying extends PathNavigate {
if (value) {
break;
}
for (int z = 1 - width; z < width; z++) {
for (int z = -width; z <= width; z++) {
if (Math.abs(x) == Math.abs(z)) {
continue;
}
l += x;
i1 += y;
j1 += z;
@@ -642,8 +647,11 @@ public class PathNavigateFlying extends PathNavigate {
Block block2 = this.worldObj.getBlock(l, i1, j1);
int meta2 = this.worldObj.getBlockMetadata(l, i1, j1);
if (block2 != Blocks.air && (!p_147447_4_ || block2.getCollisionBoundingBoxFromPool(this.worldObj, l, i1, j1) != null)
&& block2.canCollideCheck(meta2, p_147447_3_)) {
boolean collideCheck = (!p_147447_4_ || block2.getCollisionBoundingBoxFromPool(this.worldObj, l, i1, j1) != null) && block2.canCollideCheck(meta2, p_147447_3_);
boolean noCollisionBlock = block2 == Blocks.air || block2.getMaterial() == Material.lava && this.theEntity.isImmuneToFire() ||
block2.getMaterial() == Material.water && this.theEntity.stats.drowningType == 2;
if (collideCheck && !noCollisionBlock) {
if (!block2.getBlocksMovement(this.worldObj, l, i1, j1)) {
value = true;
l -= x;
@@ -652,7 +660,6 @@ public class PathNavigateFlying extends PathNavigate {
break;
}
MovingObjectPosition movingobjectposition = block2.collisionRayTrace(this.worldObj, l, i1, j1, p_147447_1_, p_147447_2_);
if (movingobjectposition != null && movingobjectposition.typeOfHit != MovingObjectPosition.MovingObjectType.MISS) {
value = true;
l -= x;
@@ -21,6 +21,7 @@ import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.entity.projectile.EntityArrow;
import net.minecraft.entity.projectile.EntityThrowable;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBow;
import net.minecraft.item.ItemStack;
@@ -423,13 +424,13 @@ public abstract class EntityNPCInterface extends EntityCreature implements IEnti
@Override
protected void updateAITasks()
{
if (this.canBreathe()) {
this.getNavigator().onUpdateNavigation();
this.getMoveHelper().onUpdateMoveHelper();
try {
super.updateAITasks();
} catch (ConcurrentModificationException ignored){}
} else {
this.getNavigator().onUpdateNavigation();
this.getMoveHelper().onUpdateMoveHelper();
try {
super.updateAITasks();
} catch (ConcurrentModificationException ignored){}
if (!this.canBreathe()) {
this.setAir(this.decreaseAirSupply(this.getAir()));
if (this.getAir() <= -20) {
@@ -1340,10 +1341,21 @@ public abstract class EntityNPCInterface extends EntityCreature implements IEnti
int j = getStartPos()[1];
int k = getStartPos()[2];
double yy = 0;
if (!this.canFly()) {
for (int ii = j; ii >= 0; ii--) {
for (int ii = j; ii >= 0; ii--) {
if (this.canFly()) {
if (ii < j - 1) {
yy = j;
break;
}
Block block = worldObj.getBlock(i, ii, k);
if (block == null)
AxisAlignedBB bb = block.getCollisionBoundingBoxFromPool(worldObj, i, ii, k);
if (bb != null) {
yy = bb.maxY;
break;
}
} else {
Block block = worldObj.getBlock(i, ii, k);
if (block == null || block == Blocks.air)
continue;
AxisAlignedBB bb = block.getCollisionBoundingBoxFromPool(worldObj, i, ii, k);
if (bb == null)
@@ -1351,12 +1363,9 @@ public abstract class EntityNPCInterface extends EntityCreature implements IEnti
yy = bb.maxY;
break;
}
} else {
yy = j;
}
if (yy <= 0)
setDead();
//yy += 0.5;
return yy;
}
@@ -1638,7 +1647,7 @@ public abstract class EntityNPCInterface extends EntityCreature implements IEnti
public boolean handleWaterMovement()
{
if (stats.drowningType != 1) {
if (this.worldObj.handleMaterialAcceleration(this.boundingBox.expand(0.0D, -0.4000000059604645D, 0.0D).contract(0.001D, 0.001D, 0.001D), Material.water, this)) {
if (this.worldObj.handleMaterialAcceleration(this.boundingBox.expand(0.0D, -this.height/2.0D, 0.0D).contract(0.001D, 0.001D, 0.001D), Material.water, this)) {
this.fallDistance = 0.0F;
this.inWater = true;
this.setFire(0);