Allow to customize the search range of an entity

This commit is contained in:
Felix Cravic 2020-07-31 21:58:49 +02:00
parent aada943f91
commit f6fac5773d
5 changed files with 35 additions and 12 deletions

View File

@ -19,7 +19,6 @@ import net.minestom.server.event.item.ItemDropEvent;
import net.minestom.server.event.item.ItemUpdateStateEvent;
import net.minestom.server.event.item.PickupItemEvent;
import net.minestom.server.event.player.*;
import net.minestom.server.instance.Chunk;
import net.minestom.server.instance.Instance;
import net.minestom.server.instance.InstanceContainer;
import net.minestom.server.instance.WorldBorder;
@ -52,7 +51,7 @@ public class PlayerInit {
//instanceContainer = MinecraftServer.getInstanceManager().createInstanceContainer(storageFolder);
instanceContainer = MinecraftServer.getInstanceManager().createInstanceContainer(DimensionType.OVERWORLD);
instanceContainer.enableAutoChunkLoad(true);
instanceContainer.setChunkGenerator(noiseTestGenerator);
instanceContainer.setChunkGenerator(chunkGeneratorDemo);
netherTest = MinecraftServer.getInstanceManager().createInstanceContainer(DimensionType.NETHER);
netherTest.enableAutoChunkLoad(true);
@ -63,12 +62,12 @@ public class PlayerInit {
end.setChunkGenerator(noiseTestGenerator);
// Load some chunks beforehand
int loopStart = -2;
int loopEnd = 10;
final int loopStart = -10;
final int loopEnd = 10;
for (int x = loopStart; x < loopEnd; x++)
for (int z = loopStart; z < loopEnd; z++) {
instanceContainer.loadChunk(x, z);
//netherTest.loadChunk(x, z);
netherTest.loadChunk(x, z);
end.loadChunk(x, z);
}
@ -350,7 +349,7 @@ public class PlayerInit {
System.out.println("PLAYER EAT EVENT");
});
player.addEventCallback(PlayerChunkUnloadEvent.class, event -> {
/*player.addEventCallback(PlayerChunkUnloadEvent.class, event -> {
Instance instance = player.getInstance();
Chunk chunk = instance.getChunk(event.getChunkX(), event.getChunkZ());
@ -362,7 +361,7 @@ public class PlayerInit {
if (chunk.getViewers().isEmpty()) {
player.getInstance().unloadChunk(chunk);
}
});
});*/
});
}

View File

@ -56,7 +56,7 @@ public class SimpleCommand implements CommandProcessor {
ChickenCreature chickenCreature = new ChickenCreature(new Position(-10, 43, -10));
chickenCreature.setInstance(instance);
chickenCreature.setPathTo(player.getPosition());
chickenCreature.setPathTo(player.getPosition().clone().add(30, 0, 30));
return true;
}

View File

@ -311,7 +311,7 @@ public abstract class EntityCreature extends LivingEntity {
// the purpose of these few lines is to slow down entities when they reach their destination
float distSquared = dx * dx + dz * dz;
if(speed > distSquared) {
if (speed > distSquared) {
speed = distSquared;
}
@ -327,6 +327,17 @@ public abstract class EntityCreature extends LivingEntity {
velocity.setZ(speedZ * tps);
}
/**
* Get the pathing entity
* <p>
* Used by the pathfinder
*
* @return the pathing entity
*/
public PFPathingEntity getPathingEntity() {
return pathingEntity;
}
private void lookAlong(float dx, float dy, float dz) {
final float horizontalAngle = (float) Math.atan2(dz, dx);
final float yaw = (float) (horizontalAngle * (180.0 / Math.PI)) - 90;

View File

@ -6,6 +6,7 @@ import com.extollit.gaming.ai.path.model.IInstanceSpace;
import net.minestom.server.instance.Chunk;
import net.minestom.server.instance.Instance;
import net.minestom.server.instance.block.Block;
import net.minestom.server.utils.validate.Check;
import java.util.HashMap;
import java.util.Map;
@ -29,6 +30,9 @@ public class PFInstanceSpace implements IInstanceSpace {
@Override
public IColumnarSpace columnarSpaceAt(int cx, int cz) {
final Chunk chunk = instance.getChunk(cx, cz);
Check.notNull(chunk,
"The pathfinder tried to get the unloaded chunk [" + cx + ":" + cz + "]," +
" try to lower the search range or to load the chunk manually");
final PFColumnarSpace columnarSpace =
chunkSpaceMap.computeIfAbsent(chunk, c -> {
final PFColumnarSpace cs = new PFColumnarSpace(this, c);

View File

@ -8,14 +8,14 @@ import net.minestom.server.utils.Position;
public class PFPathingEntity implements IPathingEntity {
private static final float SEARCH_RANGE = 32;
private EntityCreature entity;
private float searchRange;
private Position targetPosition;
public PFPathingEntity(EntityCreature entity) {
this.entity = entity;
this.searchRange = entity.getAttributeValue(Attribute.FOLLOW_RANGE);
}
public Position getTargetPosition() {
@ -29,7 +29,16 @@ public class PFPathingEntity implements IPathingEntity {
@Override
public float searchRange() {
return SEARCH_RANGE;
return searchRange;
}
/**
* Change the search range of the entity
*
* @param searchRange the new entity's search range
*/
public void setSearchRange(float searchRange) {
this.searchRange = searchRange;
}
@Override