Cache pathfinder block data

This commit is contained in:
themode 2020-08-17 21:07:37 +02:00
parent ebaa2ad1a5
commit 84b74eb45f
6 changed files with 1372 additions and 1323 deletions

View File

@ -1,10 +1,35 @@
package net.minestom.server.entity.pathfinding;
import com.extollit.gaming.ai.path.model.IBlockDescription;
import it.unimi.dsi.fastutil.shorts.Short2ObjectMap;
import it.unimi.dsi.fastutil.shorts.Short2ObjectOpenHashMap;
import net.minestom.server.instance.block.Block;
public class PFBlockDescription implements IBlockDescription {
private static Short2ObjectMap<PFBlockDescription> blockDescriptionMap = new Short2ObjectOpenHashMap<>();
/**
* Get the {@link PFBlockDescription} linked to the block state id
* <p>
* Cache the result if it is not already
*
* @param blockStateId the block state id
* @return the {@link PFBlockDescription} linked to {@code blockStateId}
*/
public static PFBlockDescription getBlockDescription(short blockStateId) {
if (!blockDescriptionMap.containsKey(blockStateId)) {
synchronized (blockDescriptionMap) {
final Block block = Block.fromStateId(blockStateId);
final PFBlockDescription blockDescription = new PFBlockDescription(block);
blockDescriptionMap.put(blockStateId, blockDescription);
return blockDescription;
}
}
return blockDescriptionMap.get(blockStateId);
}
private Block block;
public PFBlockDescription(Block block) {

View File

@ -2,10 +2,35 @@ package net.minestom.server.entity.pathfinding;
import com.extollit.gaming.ai.path.model.IBlockObject;
import com.extollit.linalg.immutable.AxisAlignedBBox;
import it.unimi.dsi.fastutil.shorts.Short2ObjectMap;
import it.unimi.dsi.fastutil.shorts.Short2ObjectOpenHashMap;
import net.minestom.server.instance.block.Block;
public class PFBlockObject implements IBlockObject {
private static Short2ObjectMap<PFBlockObject> blockObjectsMap = new Short2ObjectOpenHashMap<>();
/**
* Get the {@link PFBlockObject} linked to the block state id
* <p>
* Cache the result if it is not already
*
* @param blockStateId the block state id
* @return the {@link PFBlockObject} linked to {@code blockStateId}
*/
public static PFBlockObject getBlockObject(short blockStateId) {
if (!blockObjectsMap.containsKey(blockStateId)) {
synchronized (blockObjectsMap) {
final Block block = Block.fromStateId(blockStateId);
final PFBlockObject blockObject = new PFBlockObject(block);
blockObjectsMap.put(blockStateId, blockObject);
return blockObject;
}
}
return blockObjectsMap.get(blockStateId);
}
private Block block;
public PFBlockObject(Block block) {

View File

@ -5,7 +5,6 @@ import com.extollit.gaming.ai.path.model.IBlockDescription;
import com.extollit.gaming.ai.path.model.IColumnarSpace;
import com.extollit.gaming.ai.path.model.IInstanceSpace;
import net.minestom.server.instance.Chunk;
import net.minestom.server.instance.block.Block;
public class PFColumnarSpace implements IColumnarSpace {
@ -22,8 +21,7 @@ public class PFColumnarSpace implements IColumnarSpace {
@Override
public IBlockDescription blockAt(int x, int y, int z) {
final short blockStateId = chunk.getBlockStateId(x, y, z);
final Block block = Block.fromStateId(blockStateId);
return new PFBlockDescription(block);
return PFBlockDescription.getBlockDescription(blockStateId);
}
@Override

View File

@ -5,7 +5,6 @@ import com.extollit.gaming.ai.path.model.IColumnarSpace;
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 java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@ -22,8 +21,7 @@ public class PFInstanceSpace implements IInstanceSpace {
@Override
public IBlockObject blockObjectAt(int x, int y, int z) {
final short blockStateId = instance.getBlockStateId(x, y, z);
final Block block = Block.fromStateId(blockStateId);
return new PFBlockObject(block);
return PFBlockObject.getBlockObject(blockStateId);
}
@Override

View File

@ -5,7 +5,6 @@ import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import net.minestom.server.data.Data;
import net.minestom.server.data.SerializableData;
import net.minestom.server.entity.pathfinding.PFBlockDescription;
import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.CustomBlock;
import net.minestom.server.instance.block.UpdateConsumer;
import net.minestom.server.network.packet.server.play.ChunkDataPacket;
@ -45,7 +44,7 @@ public class DynamicChunk extends Chunk {
// Update pathfinder
if (columnarSpace != null) {
final ColumnarOcclusionFieldList columnarOcclusionFieldList = columnarSpace.occlusionFields();
final PFBlockDescription blockDescription = new PFBlockDescription(Block.fromStateId(blockStateId));
final PFBlockDescription blockDescription = PFBlockDescription.getBlockDescription(blockStateId);
columnarOcclusionFieldList.onBlockChanged(x, y, z, blockDescription, 0);
}
}