Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #87 #122

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 45 additions & 11 deletions src/main/java/nukkitcoders/mobplugin/entities/WalkingEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)));
}
}

Expand Down Expand Up @@ -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();
}
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/nukkitcoders/mobplugin/route/RouteFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -153,7 +153,7 @@ public void resetNodes() {

public void research() {
this.resetNodes();
this.search();
this.reachable = this.search();
}

public boolean hasNext() {
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -9,6 +12,7 @@

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.PriorityQueue;

/**
Expand Down Expand Up @@ -87,7 +91,6 @@ public boolean search() {
} else {
this.searching = false;
this.finished = true;
this.reachable = false;
this.addNode(new Node(destination));
return false;
}
Expand Down Expand Up @@ -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<Vector3> list = new ArrayList<>();
List<Vector3> 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)
Expand All @@ -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<Vector3> list = new ArrayList<>();
List<Vector3> 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)
Expand All @@ -354,7 +357,7 @@ private boolean hasBarrier(Vector3 pos1, Vector3 pos2) {
}


private boolean hasBlocksAround(ArrayList<Vector3> list) {
private boolean hasBlocksAround(List<Vector3> list) {
double radius = (this.entity.getWidth() * this.entity.getScale()) / 2 + 0.1;
double height = this.entity.getHeight() * this.entity.getScale();
for (Vector3 vector3 : list) {
Expand Down