Skip to content

Commit

Permalink
Fix player movement speed and add iterations per tick setting for new…
Browse files Browse the repository at this point in the history
… pathfinder
  • Loading branch information
fullwall committed May 6, 2020
1 parent e1f7492 commit c720160
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 51 deletions.
2 changes: 1 addition & 1 deletion main/pom.xml
Expand Up @@ -16,7 +16,7 @@
<craftbukkit.version>1.15.2-R0.1-SNAPSHOT</craftbukkit.version>
<citizensapi.version>${project.version}</citizensapi.version>
<powermock.version>1.4.12</powermock.version>
<bstats.version>1.5</bstats.version>
<bstats.version>1.7</bstats.version>
</properties>

<repositories>
Expand Down
15 changes: 10 additions & 5 deletions main/src/main/java/net/citizensnpcs/Citizens.java
Expand Up @@ -454,7 +454,7 @@ private void setupTranslator() {

private void startMetrics() {
try {
Metrics metrics = new Metrics(this);
Metrics metrics = new Metrics(this, 2463);

metrics.addCustomChart(new Metrics.SingleLineChart("total_npcs", new Callable<Integer>() {
@Override
Expand All @@ -464,10 +464,15 @@ public Integer call() {
return Iterables.size(npcRegistry);
}
}));

// todo: Read the comment in CitizensTraitFactory.
// traitFactory.addPlotters(metrics.createGraph("traits"));

/*
TODO: not implemented yet
metrics.addCustomChart(new Metrics.MultiLineChart("traits", new Callable<Map<String, Integer>>() {
@Override
public Map<String, Integer> call() throws Exception {
return traitFactory.getTraitPlot();
}
}));
*/
} catch (Exception e) {
Messaging.logTr(Messages.METRICS_ERROR_NOTIFICATION, e.getMessage());
}
Expand Down
1 change: 1 addition & 0 deletions main/src/main/java/net/citizensnpcs/Settings.java
Expand Up @@ -57,6 +57,7 @@ private void updateMessagingSettings() {
}

public enum Setting {
ASTAR_ITERATIONS_PER_TICK("npc.pathfinding.new-finder.iterations-per-tick", 25000),
AUTH_SERVER_URL("general.authlib.profile-url", "https://sessionserver.mojang.com/session/minecraft/profile/"),
CHAT_BYSTANDERS_HEAR_TARGETED_CHAT("npc.chat.options.bystanders-hear-targeted-chat", true),
CHAT_FORMAT("npc.chat.format.no-targets", "[<npc>]: <text>"),
Expand Down
37 changes: 13 additions & 24 deletions main/src/main/java/net/citizensnpcs/npc/CitizensTraitFactory.java
Expand Up @@ -10,6 +10,7 @@
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitFactory;
Expand Down Expand Up @@ -103,30 +104,6 @@ public void addDefaultTraits(NPC npc) {
}
}

/*
TODO: We are waiting for multiline charts in bStats to implement this
*
public void addPlotters(Graph graph) {
for (Map.Entry<String, TraitInfo> entry : registered.entrySet()) {
if (INTERNAL_TRAITS.contains(entry.getKey()) || entry.getKey() == null)
continue;
final Class<? extends Trait> traitClass = entry.getValue().getTraitClass();
graph.addPlotter(new Metrics.Plotter(entry.getKey()) {
@Override
public int getValue() {
int numberUsingTrait = 0;
for (NPC npc : CitizensAPI.getNPCRegistry()) {
if (npc.hasTrait(traitClass)) {
++numberUsingTrait;
}
}
return numberUsingTrait;
}
});
}
}
*/

private <T extends Trait> T create(TraitInfo info) {
return info.tryCreateInstance();
}
Expand Down Expand Up @@ -167,6 +144,18 @@ public Class<? extends Trait> getTraitClass(String name) {
return info == null ? null : info.getTraitClass();
}

public Map<String, Integer> getTraitPlot() {
Map<String, Integer> counts = Maps.newHashMap();
for (NPC npc : CitizensAPI.getNPCRegistry()) {
for (Trait trait : npc.getTraits()) {
if (INTERNAL_TRAITS.contains(trait.getName()))
continue;
counts.put(trait.getName(), counts.getOrDefault(trait.getName(), 0) + 1);
}
}
return counts;
}

@Override
public boolean isInternalTrait(Trait trait) {
return INTERNAL_TRAITS.contains(trait.getName());
Expand Down
Expand Up @@ -28,10 +28,12 @@

public class AStarNavigationStrategy extends AbstractPathStrategy {
private final Location destination;
private int iterations;
private final NPC npc;
private final NavigatorParameters params;
private Path plan;
private boolean planned = false;
private AStarMachine<VectorNode, Path>.AStarState state;
private Vector vector;

public AStarNavigationStrategy(NPC npc, Iterable<Vector> path, NavigatorParameters params) {
Expand Down Expand Up @@ -60,6 +62,27 @@ public Location getTargetAsLocation() {
return destination;
}

public void initialisePathfinder() {
params.examiner(new BlockExaminer() {
@Override
public float getCost(BlockSource source, PathPoint point) {
Vector pos = point.getVector();
Material in = source.getMaterialAt(pos);
Material above = source.getMaterialAt(pos.setY(pos.getY() + 1));
return params.avoidWater() && MinecraftBlockExaminer.isLiquid(in, above) ? 1F : 0F;
}

@Override
public PassableState isPassable(BlockSource source, PathPoint point) {
return PassableState.IGNORE;
}
});
Location location = npc.getEntity().getLocation();
VectorGoal goal = new VectorGoal(destination, (float) params.pathDistanceMargin());
state = ASTAR.getStateFor(goal,
new VectorNode(goal, location, new ChunkBlockSource(location, params.range()), params.examiners()));
}

public void setPlan(Path path) {
this.plan = path;
this.planned = true;
Expand All @@ -78,38 +101,41 @@ public void stop() {
if (plan != null && params.debug()) {
plan.debugEnd();
}
state = null;
plan = null;
}

@Override
public boolean update() {
if (!planned) {
params.examiner(new BlockExaminer() {
@Override
public float getCost(BlockSource source, PathPoint point) {
Vector pos = point.getVector();
Material in = source.getMaterialAt(pos);
Material above = source.getMaterialAt(pos.setY(pos.getY() + 1));
return params.avoidWater() && MinecraftBlockExaminer.isLiquid(in, above) ? 1F : 0F;
if (state == null) {
initialisePathfinder();
}
int maxIterations = Setting.MAXIMUM_ASTAR_ITERATIONS.asInt();
int iterationsPerTick = Setting.ASTAR_ITERATIONS_PER_TICK.asInt();
Path plan = ASTAR.run(state, iterationsPerTick);
if (plan == null) {
if (state.isEmpty()) {
setCancelReason(CancelReason.STUCK);
}

@Override
public PassableState isPassable(BlockSource source, PathPoint point) {
return PassableState.IGNORE;
if (iterationsPerTick > 0 && maxIterations > 0) {
iterations += iterationsPerTick;
if (iterations > maxIterations) {
setCancelReason(CancelReason.STUCK);
}
}
});
Location location = npc.getEntity().getLocation();
VectorGoal goal = new VectorGoal(destination, (float) params.pathDistanceMargin());
setPlan(ASTAR.runFully(goal,
new VectorNode(goal, location, new ChunkBlockSource(location, params.range()), params.examiners()),
Setting.MAXIMUM_ASTAR_ITERATIONS.asInt()));
} else {
setPlan(plan);
}
}
if (getCancelReason() != null || plan == null || plan.isComplete()) {
return true;
}
Location currLoc = npc.getEntity().getLocation(NPC_LOCATION);
Vector destVector = new Vector(vector.getX() + 0.5, vector.getY(), vector.getZ() + 0.5);
/*Block block = currLoc.getWorld().getBlockAt(vector.getBlockX(), vector.getBlockY(), vector.getBlockZ());
/* Proper door movement - gets stuck on corners at times
Block block = currLoc.getWorld().getBlockAt(vector.getBlockX(), vector.getBlockY(), vector.getBlockZ());
if (MinecraftBlockExaminer.isDoor(block.getType())) {
Door door = (Door) block.getState().getData();
if (door.isOpen()) {
Expand Down
Expand Up @@ -49,9 +49,7 @@ public void a() {
this.a.yaw = a(this.a.yaw, f, 90.0F);
NMS.setHeadYaw(a.getBukkitEntity(), this.a.yaw);
AttributeInstance speed = this.a.getAttributeInstance(GenericAttributes.MOVEMENT_SPEED);
if (this.a instanceof EntitySlime) {
speed.setValue(0.3D * this.e);
}
speed.setValue(0.3D * this.e);
float movement = (float) (this.e * speed.getValue());
this.a.o(movement);
this.a.bb = movement;
Expand Down

0 comments on commit c720160

Please sign in to comment.