diff --git a/src/main/java/nukkitcoders/mobplugin/entities/WalkingEntity.java b/src/main/java/nukkitcoders/mobplugin/entities/WalkingEntity.java index f3ef0c7b5..56c484dbb 100644 --- a/src/main/java/nukkitcoders/mobplugin/entities/WalkingEntity.java +++ b/src/main/java/nukkitcoders/mobplugin/entities/WalkingEntity.java @@ -3,8 +3,10 @@ import cn.nukkit.block.*; import cn.nukkit.entity.Entity; import cn.nukkit.entity.EntityCreature; +import cn.nukkit.level.Location; import cn.nukkit.level.format.FullChunk; import cn.nukkit.level.particle.BubbleParticle; +import cn.nukkit.level.particle.DestroyBlockParticle; import cn.nukkit.math.NukkitMath; import cn.nukkit.math.Vector2; import cn.nukkit.math.Vector3; @@ -73,20 +75,50 @@ protected void checkTarget() { if (Utils.rand(1, 100) > 5) { return; } - x = Utils.rand(10, 30); - z = Utils.rand(10, 30); - this.target = this.add(Utils.rand() ? x : -x, Utils.rand(-20.0, 20.0) / 10, Utils.rand() ? z : -z); + x = Utils.rand(1, 3); + z = Utils.rand(1, 3); + Location tempLoc = this.add(Utils.rand() ? x : -x, Utils.rand(-20.0, 20.0) / 10, Utils.rand() ? z : -z); + if(route!=null){ + route.setDestination(tempLoc); + route.research(); + if(!route.isReachable()){ + route.resetNodes(); + target = this.getLocation(); + } + } + //System.out.println("cc"+this.getLocation()+route.nodes+route.isReachable()+route.hasNext()); + //level.addParticle(new DestroyBlockParticle(tempLoc,Block.get(Block.DIAMOND_BLOCK))); } else if (Utils.rand(1, 100) == 1) { - x = Utils.rand(10, 30); - z = Utils.rand(10, 30); + x = Utils.rand(1, 3); + z = Utils.rand(1, 3); this.stayTime = Utils.rand(100, 200); - this.target = this.add(Utils.rand() ? x : -x, Utils.rand(-20.0, 20.0) / 10, Utils.rand() ? z : -z); + Location tempLoc = this.add(Utils.rand() ? x : -x, Utils.rand(-20.0, 20.0) / 10, Utils.rand() ? z : -z); + if(route!=null){ + route.setDestination(tempLoc); + route.research(); + if(!route.isReachable()){ + route.resetNodes(); + target = this.getLocation(); + } + } + //System.out.println("dd"+this.getLocation()+route.nodes+route.isReachable()+route.hasNext()); + //level.addParticle(new DestroyBlockParticle(tempLoc,Block.get(Block.DIAMOND_BLOCK))); } else if (this.moveTime <= 0 || this.target == null) { - x = Utils.rand(20, 100); - z = Utils.rand(20, 100); + x = Utils.rand(2, 10); + z = Utils.rand(2, 10); this.stayTime = 0; - this.moveTime = Utils.rand(100, 200); - this.target = this.add(Utils.rand() ? x : -x, 0, Utils.rand() ? z : -z); + this.moveTime = Utils.rand(300, 1200); + Location tempLoc = this.add(Utils.rand() ? x : -x, 0, Utils.rand() ? z : -z); + if(route!=null){ + route.setDestination(tempLoc); + route.research(); + if(!route.isReachable()){ + route.resetNodes(); + target = this.getLocation(); + } + } + //System.out.println("ee"+this.getLocation()+route.nodes+route.isReachable()+route.hasNext()); + //level.addParticle(new DestroyBlockParticle(tempLoc,Block.get(Block.DIAMOND_BLOCK))); } } @@ -134,7 +166,9 @@ public Vector3 updateMove(int tickDiff) { } if (this.age % 10 == 0 && this.route != null && !this.route.isSearching()) { - RouteFinderThreadPool.executeRouteFinderThread(new RouteFinderSearchTask(this.route)); + if(followTarget != null) { + RouteFinderThreadPool.executeRouteFinderThread(new RouteFinderSearchTask(this.route)); + } if (this.route.hasNext()) { this.target = this.route.next(); } diff --git a/src/main/java/nukkitcoders/mobplugin/route/RouteFinder.java b/src/main/java/nukkitcoders/mobplugin/route/RouteFinder.java index d32ae13c4..27a284714 100644 --- a/src/main/java/nukkitcoders/mobplugin/route/RouteFinder.java +++ b/src/main/java/nukkitcoders/mobplugin/route/RouteFinder.java @@ -143,7 +143,7 @@ public void resetNodes() { this.nodes.clear(); this.current = 0; this.interrupt = false; - this.destination = null; + //this.destination = null; }finally { this.lock.writeLock().unlock(); } @@ -153,7 +153,7 @@ public void resetNodes() { public void research() { this.resetNodes(); - this.search(); + this.reachable = this.search(); } public boolean hasNext() { @@ -177,7 +177,11 @@ public boolean isInterrupted() { return this.interrupt; } - public boolean interrupt() { - return this.interrupt ^= true; + public void interrupt() { + this.setInterrupt(true); + } + + public void setInterrupt(boolean interrupt){ + this.interrupt = interrupt; } } diff --git a/src/main/java/nukkitcoders/mobplugin/route/WalkerRouteFinder.java b/src/main/java/nukkitcoders/mobplugin/route/WalkerRouteFinder.java index 273e2bb7c..555983f72 100644 --- a/src/main/java/nukkitcoders/mobplugin/route/WalkerRouteFinder.java +++ b/src/main/java/nukkitcoders/mobplugin/route/WalkerRouteFinder.java @@ -1,6 +1,9 @@ package nukkitcoders.mobplugin.route; import cn.nukkit.block.Block; +import cn.nukkit.event.block.BlockBreakEvent; +import cn.nukkit.item.Item; +import cn.nukkit.level.particle.ItemBreakParticle; import cn.nukkit.math.AxisAlignedBB; import cn.nukkit.math.SimpleAxisAlignedBB; import cn.nukkit.math.Vector3; @@ -9,6 +12,7 @@ import java.util.ArrayList; import java.util.Collections; +import java.util.List; import java.util.PriorityQueue; /** @@ -87,7 +91,6 @@ public boolean search() { } else { this.searching = false; this.finished = true; - this.reachable = false; this.addNode(new Node(destination)); return false; } @@ -331,7 +334,7 @@ private boolean hasBarrier(Vector3 pos1, Vector3 pos2) { if (traverseDirection) { double loopStart = Math.min(pos1.getX(), pos2.getX()); double loopEnd = Math.max(pos1.getX(), pos2.getX()); - ArrayList list = new ArrayList<>(); + List list = new ArrayList<>(); for (double i = Math.ceil(loopStart); i <= Math.floor(loopEnd); i += 1.0) { double result; if ((result = Utils.calLinearFunction(pos1, pos2, i, Utils.ACCORDING_X_OBTAIN_Y)) != Double.MAX_VALUE) @@ -341,7 +344,7 @@ private boolean hasBarrier(Vector3 pos1, Vector3 pos2) { } else { double loopStart = Math.min(pos1.getZ(), pos2.getZ()); double loopEnd = Math.max(pos1.getZ(), pos2.getZ()); - ArrayList list = new ArrayList<>(); + List list = new ArrayList<>(); for (double i = Math.ceil(loopStart); i <= Math.floor(loopEnd); i += 1.0) { double result; if ((result = Utils.calLinearFunction(pos1, pos2, i, Utils.ACCORDING_Y_OBTAIN_X)) != Double.MAX_VALUE) @@ -354,7 +357,7 @@ private boolean hasBarrier(Vector3 pos1, Vector3 pos2) { } - private boolean hasBlocksAround(ArrayList list) { + private boolean hasBlocksAround(List list) { double radius = (this.entity.getWidth() * this.entity.getScale()) / 2 + 0.1; double height = this.entity.getHeight() * this.entity.getScale(); for (Vector3 vector3 : list) {