use of org.joml.Vector3i in project Terasology by MovingBlocks.
the class SectorUtil method getWatchedChunks.
/**
* Watched chunks are defined as the union of:
* <ul>
* <li>The chunk in which the {@link LocationComponent#getWorldPosition(Vector3f)} resides, if any</li>
* <li>The set of chunks in {@link SectorRegionComponent#chunks}, if any</li>
* </ul>
*
* @param entity the entity to query the watched chunks of
* @return the set of positions of this entity's watched chunks
*/
public static Set<Vector3i> getWatchedChunks(EntityRef entity) {
Set<Vector3i> chunks = new HashSet<>();
LocationComponent loc = entity.getComponent(LocationComponent.class);
Vector3f position = loc.getWorldPosition(new Vector3f());
if (position.isFinite()) {
chunks.add(Chunks.toChunkPos(position, new Vector3i()));
}
SectorRegionComponent regionComponent = entity.getComponent(SectorRegionComponent.class);
if (regionComponent != null) {
// potential leaky abstraction. component exposes its internal vectors
chunks.addAll(regionComponent.chunks);
}
return chunks;
}
use of org.joml.Vector3i in project Terasology by MovingBlocks.
the class AbstractStorageManager method loadChunkZip.
protected byte[] loadChunkZip(Vector3ic chunkPos) {
byte[] chunkData = null;
Vector3i chunkZipPos = storagePathProvider.getChunkZipPosition(chunkPos);
Path chunkPath = storagePathProvider.getChunkZipPath(chunkZipPos);
if (Files.isRegularFile(chunkPath)) {
try (FileSystem chunkZip = FileSystems.newFileSystem(chunkPath, null)) {
Path targetChunk = chunkZip.getPath(storagePathProvider.getChunkFilename(chunkPos));
if (Files.isRegularFile(targetChunk)) {
chunkData = Files.readAllBytes(targetChunk);
}
} catch (IOException e) {
logger.error("Failed to load chunk zip {}", chunkPath, e);
}
}
return chunkData;
}
use of org.joml.Vector3i in project Terasology by MovingBlocks.
the class MapWorldProvider method getBlock.
@Override
public Block getBlock(int x, int y, int z) {
Vector3i pos = new Vector3i(x, y, z);
Block block = blocks.get(pos);
if (block != null) {
return block;
}
// TODO block manager
Vector3i chunkPos = Chunks.toChunkPos(pos, new Vector3i());
Chunk chunk = chunks.get(chunkPos);
if (chunk == null && worldGenerator != null) {
chunk = new ChunkImpl(chunkPos, blockManager, extraDataManager);
worldGenerator.createChunk(chunk, entityBuffer);
chunks.put(chunkPos, chunk);
}
if (chunk != null) {
return chunk.getBlock(Chunks.toRelative(pos, pos));
}
return null;
}
use of org.joml.Vector3i in project Terasology by MovingBlocks.
the class HeadlessWorldRenderer method updateChunksInProximity.
/**
* Updates the list of chunks around the player.
*
* @param force Forces the update
* @return True if the list was changed
*/
public boolean updateChunksInProximity(boolean force) {
Vector3i newChunkPos = calcCamChunkOffset();
// TODO: This should actually be done based on events from the ChunkProvider on new chunk availability/old chunk removal
boolean chunksCurrentlyPending = false;
if (!newChunkPos.equals(chunkPos) || force || pendingChunks) {
Vector3ic viewingDistance = config.getRendering().getViewDistance().getChunkDistance();
BlockRegion viewRegion = new BlockRegion(newChunkPos).expand(new Vector3i(viewingDistance.x() / 2, viewingDistance.y() / 2, viewingDistance.z() / 2));
if (chunksInProximity.size() == 0 || force || pendingChunks) {
// just add all visible chunks
chunksInProximity.clear();
for (Vector3ic chunkPosition : viewRegion) {
Chunk c = chunkProvider.getChunk(chunkPosition);
if (c != null && worldProvider.getLocalView(c.getPosition(new Vector3i())) != null) {
chunksInProximity.add(c);
} else {
chunksCurrentlyPending = true;
}
}
} else {
BlockRegion oldRegion = new BlockRegion(chunkPos).expand(new Vector3i(viewingDistance.x() / 2, viewingDistance.y() / 2, viewingDistance.z() / 2));
// remove
for (Vector3ic candidateForRemove : viewRegion) {
if (!oldRegion.contains(candidateForRemove)) {
Chunk c = chunkProvider.getChunk(candidateForRemove);
if (c != null) {
chunksInProximity.remove(c);
c.disposeMesh();
}
}
}
// add
for (Vector3ic chunkPosition : viewRegion) {
Chunk c = chunkProvider.getChunk(chunkPosition);
if (c != null && worldProvider.getLocalView(c.getPosition(new Vector3i())) != null) {
chunksInProximity.add(c);
} else {
chunksCurrentlyPending = true;
}
}
}
chunkPos.set(newChunkPos);
pendingChunks = chunksCurrentlyPending;
Collections.sort(chunksInProximity, new ChunkFrontToBackComparator());
return true;
}
return false;
}
use of org.joml.Vector3i in project Terasology by MovingBlocks.
the class NetClient method sendChunkInvalidations.
private void sendChunkInvalidations(NetData.NetMessage.Builder message) {
Iterator<Vector3i> i = invalidatedChunks.iterator();
while (i.hasNext()) {
Vector3i pos = i.next();
i.remove();
relevantChunks.remove(pos);
message.addInvalidateChunk(NetData.InvalidateChunkMessage.newBuilder().setPos(NetMessageUtil.convert(pos)));
}
invalidatedChunks.clear();
}
Aggregations