From 4bad48352c4f946b6fb78822099a810873dabd34 Mon Sep 17 00:00:00 2001 From: "Luis C.M" <42545778+c0508383@users.noreply.github.com> Date: Fri, 4 Nov 2022 23:56:09 -0500 Subject: [PATCH] NPCs no longer constantly pathfind when set to return to their home, and already there. Flying NPCs: -Pathfinding has a cooldown to decrease the amount of times they create a new path. -Fly height limit implemented using getTopSolidOrLiquidBlock instead of iterating through blocks below the NPC. --- .../java/noppes/npcs/ai/EntityAIReturn.java | 7 +++++-- .../npcs/ai/pathfinder/PathNavigateFlying.java | 17 +++++++++++++++++ .../noppes/npcs/entity/EntityNPCFlying.java | 11 +---------- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/main/java/noppes/npcs/ai/EntityAIReturn.java b/src/main/java/noppes/npcs/ai/EntityAIReturn.java index 771f42797..94014013d 100644 --- a/src/main/java/noppes/npcs/ai/EntityAIReturn.java +++ b/src/main/java/noppes/npcs/ai/EntityAIReturn.java @@ -145,8 +145,11 @@ public class EntityAIReturn extends EntityAIBase } } } - npc.getNavigator().clearPathEntity(); - npc.getNavigator().tryMoveToXYZ(posX, posY, posZ, 1); + + if (range > 1f) { + npc.getNavigator().clearPathEntity(); + npc.getNavigator().tryMoveToXYZ(posX, posY, posZ, 1); + } } public void resetTask(){ diff --git a/src/main/java/noppes/npcs/ai/pathfinder/PathNavigateFlying.java b/src/main/java/noppes/npcs/ai/pathfinder/PathNavigateFlying.java index 944d87d55..fc52af434 100644 --- a/src/main/java/noppes/npcs/ai/pathfinder/PathNavigateFlying.java +++ b/src/main/java/noppes/npcs/ai/pathfinder/PathNavigateFlying.java @@ -46,6 +46,9 @@ public class PathNavigateFlying extends PathNavigate { private boolean canSwim; private static final String __OBFID = "CL_00001627"; + private int pathCooldown = 0; + private int maxCooldown = 2; + public PathNavigateFlying(EntityLiving p_i45873_1_, World worldIn) { super(p_i45873_1_, worldIn); this.theEntity = (EntityNPCInterface)p_i45873_1_; @@ -129,6 +132,13 @@ public class PathNavigateFlying extends PathNavigate { */ public boolean tryMoveToXYZ(double p_75492_1_, double p_75492_3_, double p_75492_5_, double p_75492_7_) { + if (this.pathCooldown == 0) { + this.pathCooldown = this.maxCooldown; + } else { + this.pathCooldown--; + return false; + } + FlyPathEntity pathentity = this.getPathToXYZ((double)MathHelper.floor_double(p_75492_1_), (double)((int)p_75492_3_), (double)MathHelper.floor_double(p_75492_5_)); return this.setPath(pathentity, p_75492_7_); } @@ -146,6 +156,13 @@ public class PathNavigateFlying extends PathNavigate { */ public boolean tryMoveToEntityLiving(Entity p_75497_1_, double p_75497_2_) { + if (this.pathCooldown == 0) { + this.pathCooldown = this.maxCooldown; + } else { + this.pathCooldown--; + return false; + } + FlyPathEntity pathentity = this.getPathToEntityLiving(p_75497_1_); return pathentity != null && this.setPath(pathentity, p_75497_2_); } diff --git a/src/main/java/noppes/npcs/entity/EntityNPCFlying.java b/src/main/java/noppes/npcs/entity/EntityNPCFlying.java index de8b8d1f9..f391cb855 100644 --- a/src/main/java/noppes/npcs/entity/EntityNPCFlying.java +++ b/src/main/java/noppes/npcs/entity/EntityNPCFlying.java @@ -38,16 +38,7 @@ public abstract class EntityNPCFlying extends EntityNPCInterface { return; } - double heightOffGround = 0; - if(this.ai.hasFlyLimit) { - for (int blockY = (int) this.posY; blockY > 0; blockY--) { - heightOffGround = this.posY - blockY; - if (this.worldObj.getBlock((int) this.posX, blockY, (int) this.posZ) != Blocks.air || heightOffGround > this.ai.flyHeightLimit){ - break; - } - } - } - + double heightOffGround = this.posY - this.worldObj.getTopSolidOrLiquidBlock((int) this.posX, (int) this.posZ); if (heightOffGround > this.ai.flyHeightLimit && this.ai.hasFlyLimit) { super.moveEntityWithHeading(p_70612_1_,p_70612_2_); return;