diff --git a/engine-tests/src/test/java/org/terasology/world/chunks/localChunkProvider/LocalChunkProviderTest.java b/engine-tests/src/test/java/org/terasology/world/chunks/localChunkProvider/LocalChunkProviderTest.java index bed4d0e816b..3a8e84a4144 100644 --- a/engine-tests/src/test/java/org/terasology/world/chunks/localChunkProvider/LocalChunkProviderTest.java +++ b/engine-tests/src/test/java/org/terasology/world/chunks/localChunkProvider/LocalChunkProviderTest.java @@ -128,8 +128,8 @@ void testGenerateSingleChunk() throws InterruptedException, ExecutionException, Event mustBeOnLoadedEvent = eventArgumentCaptor.getAllValues().get(1); Assertions.assertTrue(mustBeOnLoadedEvent instanceof OnChunkLoaded, "Second world event must be OnChunkLoaded"); - Assertions.assertEquals(((OnChunkLoaded) mustBeOnLoadedEvent).getChunkPos(), - chunkPosition, + Assertions.assertEquals(JomlUtil.from(chunkPosition), + ((OnChunkLoaded) mustBeOnLoadedEvent).getChunkPos(), "Chunk position at event not expected"); }); } @@ -157,8 +157,8 @@ void testGenerateSingleChunkWithBlockLifeCycle() throws InterruptedException, Ex Event mustBeOnLoadedEvent = worldEventCaptor.getAllValues().get(1); Assertions.assertTrue(mustBeOnLoadedEvent instanceof OnChunkLoaded, "Second world event must be OnChunkLoaded"); - Assertions.assertEquals(((OnChunkLoaded) mustBeOnLoadedEvent).getChunkPos(), - chunkPosition, + Assertions.assertEquals(JomlUtil.from(chunkPosition), + ((OnChunkLoaded) mustBeOnLoadedEvent).getChunkPos(), "Chunk position at event not expected"); }); @@ -192,8 +192,8 @@ void testLoadSingleChunk() throws InterruptedException, ExecutionException, Time Event mustBeOnLoadedEvent = eventArgumentCaptor.getAllValues().get(0); Assertions.assertTrue(mustBeOnLoadedEvent instanceof OnChunkLoaded, "Second world event must be OnChunkLoaded"); - Assertions.assertEquals(((OnChunkLoaded) mustBeOnLoadedEvent).getChunkPos(), - chunkPosition, + Assertions.assertEquals(JomlUtil.from(chunkPosition), + ((OnChunkLoaded) mustBeOnLoadedEvent).getChunkPos(), "Chunk position at event not expected"); } @@ -218,8 +218,8 @@ void testLoadSingleChunkWithBlockLifecycle() throws InterruptedException, Execut Event mustBeOnLoadedEvent = eventArgumentCaptor.getAllValues().get(0); Assertions.assertTrue(mustBeOnLoadedEvent instanceof OnChunkLoaded, "Second world event must be OnChunkLoaded"); - Assertions.assertEquals(((OnChunkLoaded) mustBeOnLoadedEvent).getChunkPos(), - chunkPosition, + Assertions.assertEquals(JomlUtil.from(chunkPosition), + ((OnChunkLoaded) mustBeOnLoadedEvent).getChunkPos(), "Chunk position at event not expected"); //TODO, it is not clear if the activate/addedBlocks event logic is correct. @@ -278,8 +278,8 @@ void testUnloadChunkAndDeactivationBlock() throws InterruptedException, TimeoutE Assertions.assertTrue(beforeChunkUnload.isPresent(), "World events must have BeforeChunkUnload event when chunk was unload"); - Assertions.assertEquals(beforeChunkUnload.get().getChunkPos(), - chunkPosition, + Assertions.assertEquals(JomlUtil.from(chunkPosition), + beforeChunkUnload.get().getChunkPos(), "Chunk position at event not expected"); //TODO, it is not clear if the activate/addedBlocks event logic is correct. diff --git a/engine/src/main/java/org/terasology/entitySystem/sectors/LoadedSectorUpdateEvent.java b/engine/src/main/java/org/terasology/entitySystem/sectors/LoadedSectorUpdateEvent.java index c2fdfc6b5c2..f133be590c4 100644 --- a/engine/src/main/java/org/terasology/entitySystem/sectors/LoadedSectorUpdateEvent.java +++ b/engine/src/main/java/org/terasology/entitySystem/sectors/LoadedSectorUpdateEvent.java @@ -15,8 +15,8 @@ */ package org.terasology.entitySystem.sectors; +import org.joml.Vector3i; import org.terasology.entitySystem.event.Event; -import org.terasology.math.geom.Vector3i; import org.terasology.module.sandbox.API; import java.util.Set; diff --git a/engine/src/main/java/org/terasology/entitySystem/sectors/SectorRegionComponent.java b/engine/src/main/java/org/terasology/entitySystem/sectors/SectorRegionComponent.java index 12a8d9b677b..83597156298 100644 --- a/engine/src/main/java/org/terasology/entitySystem/sectors/SectorRegionComponent.java +++ b/engine/src/main/java/org/terasology/entitySystem/sectors/SectorRegionComponent.java @@ -15,8 +15,8 @@ */ package org.terasology.entitySystem.sectors; +import org.joml.Vector3i; import org.terasology.entitySystem.Component; -import org.terasology.math.geom.Vector3i; import org.terasology.module.sandbox.API; import org.terasology.world.chunks.Chunk; diff --git a/engine/src/main/java/org/terasology/entitySystem/sectors/SectorUtil.java b/engine/src/main/java/org/terasology/entitySystem/sectors/SectorUtil.java index 1f6e0b11351..fcffe5e2a14 100644 --- a/engine/src/main/java/org/terasology/entitySystem/sectors/SectorUtil.java +++ b/engine/src/main/java/org/terasology/entitySystem/sectors/SectorUtil.java @@ -15,13 +15,15 @@ */ package org.terasology.entitySystem.sectors; +import org.joml.Vector3f; +import org.joml.Vector3i; +import org.joml.Vector3ic; import org.terasology.entitySystem.entity.EntityRef; import org.terasology.logic.location.LocationComponent; -import org.terasology.math.ChunkMath; -import org.terasology.math.geom.Vector3i; import org.terasology.module.sandbox.API; import org.terasology.world.chunks.Chunk; import org.terasology.world.chunks.ChunkProvider; +import org.terasology.world.chunks.Chunks; import java.util.Collection; import java.util.HashSet; @@ -57,7 +59,7 @@ public static SectorRegionComponent createSectorRegionComponent(Collection chunks) { regionComponent.chunks.addAll(chunks.stream() - .map(Chunk::getPosition) + .map(k -> k.getPosition(new Vector3i())) .collect(Collectors.toSet())); } @@ -90,12 +92,13 @@ public static void addChunksToRegionComponent(EntityRef entity, Collection getWatchedChunks(EntityRef entity) { Set chunks = new HashSet<>(); LocationComponent loc = entity.getComponent(LocationComponent.class); - if (loc != null&& !Float.isNaN(loc.getWorldPosition().x)) { - chunks.add(ChunkMath.calcChunkPos(loc.getWorldPosition())); + 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) { - chunks.addAll(regionComponent.chunks); + chunks.addAll(regionComponent.chunks); // potential leaky abstraction. component exposes its internal vectors } return chunks; } @@ -108,7 +111,7 @@ public static Set getWatchedChunks(EntityRef entity) { * @param chunkProvider the chunkProvider, so that it can check which chunks are loaded * @return whether the entity is watching no loaded chunks, or only the given chunk is loaded */ - public static boolean onlyWatchedChunk(EntityRef entity, Vector3i chunkPos, ChunkProvider chunkProvider) { + public static boolean onlyWatchedChunk(EntityRef entity, Vector3ic chunkPos, ChunkProvider chunkProvider) { Set readyChunks = getWatchedChunks(entity).stream() .filter(chunkProvider::isChunkReady) .collect(Collectors.toSet()); diff --git a/engine/src/main/java/org/terasology/logic/debug/ChunkEventErrorLogger.java b/engine/src/main/java/org/terasology/logic/debug/ChunkEventErrorLogger.java index 6cb8af4c038..15233783644 100644 --- a/engine/src/main/java/org/terasology/logic/debug/ChunkEventErrorLogger.java +++ b/engine/src/main/java/org/terasology/logic/debug/ChunkEventErrorLogger.java @@ -16,13 +16,13 @@ package org.terasology.logic.debug; import com.google.common.collect.Sets; +import org.joml.Vector3ic; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.terasology.entitySystem.entity.EntityRef; import org.terasology.entitySystem.event.ReceiveEvent; import org.terasology.entitySystem.systems.BaseComponentSystem; import org.terasology.entitySystem.systems.RegisterSystem; -import org.terasology.math.geom.Vector3i; import org.terasology.world.WorldComponent; import org.terasology.world.chunks.event.BeforeChunkUnload; import org.terasology.world.chunks.event.OnChunkLoaded; @@ -35,7 +35,7 @@ public class ChunkEventErrorLogger extends BaseComponentSystem { private static final Logger logger = LoggerFactory.getLogger(ChunkEventErrorLogger.class); - private Set loadedChunks = Sets.newHashSet(); + private Set loadedChunks = Sets.newHashSet(); @ReceiveEvent(components = {WorldComponent.class}) public void onNewChunk(OnChunkLoaded chunkAvailable, EntityRef worldEntity) { diff --git a/engine/src/main/java/org/terasology/physics/engine/VoxelLiquidWorldSystem.java b/engine/src/main/java/org/terasology/physics/engine/VoxelLiquidWorldSystem.java index 78a0b34c3b6..8a5d8f2032e 100644 --- a/engine/src/main/java/org/terasology/physics/engine/VoxelLiquidWorldSystem.java +++ b/engine/src/main/java/org/terasology/physics/engine/VoxelLiquidWorldSystem.java @@ -27,11 +27,13 @@ import gnu.trove.set.hash.TShortHashSet; import org.joml.Matrix4f; import org.joml.Vector3f; +import org.joml.Vector3ic; import org.terasology.entitySystem.entity.EntityManager; import org.terasology.entitySystem.entity.EntityRef; import org.terasology.entitySystem.event.ReceiveEvent; import org.terasology.entitySystem.systems.BaseComponentSystem; import org.terasology.entitySystem.systems.RegisterSystem; +import org.terasology.math.JomlUtil; import org.terasology.math.geom.Vector3i; import org.terasology.physics.StandardCollisionGroup; import org.terasology.physics.bullet.BulletPhysics; @@ -130,8 +132,8 @@ public void onBlockChange(OnChangedBlock event, EntityRef entity) { */ @ReceiveEvent(components = WorldComponent.class) public void onChunkUloaded(BeforeChunkUnload beforeChunkUnload, EntityRef worldEntity) { - Vector3i chunkPos = beforeChunkUnload.getChunkPos(); - wrapper.freeRegion(chunkPos.x, chunkPos.y, chunkPos.z); + Vector3ic chunkPos = beforeChunkUnload.getChunkPos(); + wrapper.freeRegion(chunkPos.x(), chunkPos.y(), chunkPos.z()); } /** @@ -141,8 +143,8 @@ public void onChunkUloaded(BeforeChunkUnload beforeChunkUnload, EntityRef worldE */ @ReceiveEvent(components = WorldComponent.class) public void onNewChunk(OnChunkLoaded chunkAvailable, EntityRef worldEntity) { - Vector3i chunkPos = chunkAvailable.getChunkPos(); - Chunk chunk = chunkProvider.getChunk(chunkPos); + Vector3ic chunkPos = chunkAvailable.getChunkPos(); + Chunk chunk = chunkProvider.getChunk(JomlUtil.from(chunkPos)); ByteBuffer buffer = ByteBuffer.allocateDirect(2 * (ChunkConstants.SIZE_X * ChunkConstants.SIZE_Y * ChunkConstants.SIZE_Z)); buffer.order(ByteOrder.nativeOrder()); @@ -156,6 +158,6 @@ public void onNewChunk(OnChunkLoaded chunkAvailable, EntityRef worldEntity) { } } buffer.rewind(); - wrapper.setRegion(chunkPos.x, chunkPos.y, chunkPos.z, buffer.asShortBuffer()); + wrapper.setRegion(chunkPos.x(), chunkPos.y(), chunkPos.z(), buffer.asShortBuffer()); } } diff --git a/engine/src/main/java/org/terasology/physics/engine/VoxelWorldSystem.java b/engine/src/main/java/org/terasology/physics/engine/VoxelWorldSystem.java index 9f722fbb184..a6c0ad156f0 100644 --- a/engine/src/main/java/org/terasology/physics/engine/VoxelWorldSystem.java +++ b/engine/src/main/java/org/terasology/physics/engine/VoxelWorldSystem.java @@ -27,11 +27,13 @@ import gnu.trove.set.hash.TShortHashSet; import org.joml.Matrix4f; import org.joml.Vector3f; +import org.joml.Vector3ic; import org.terasology.entitySystem.entity.EntityManager; import org.terasology.entitySystem.entity.EntityRef; import org.terasology.entitySystem.event.ReceiveEvent; import org.terasology.entitySystem.systems.BaseComponentSystem; import org.terasology.entitySystem.systems.RegisterSystem; +import org.terasology.math.JomlUtil; import org.terasology.math.geom.Vector3i; import org.terasology.physics.StandardCollisionGroup; import org.terasology.physics.bullet.BulletPhysics; @@ -129,8 +131,8 @@ public void onBlockChange(OnChangedBlock event, EntityRef entity) { */ @ReceiveEvent(components = WorldComponent.class) public void onChunkUloaded(BeforeChunkUnload beforeChunkUnload, EntityRef worldEntity) { - Vector3i chunkPos = beforeChunkUnload.getChunkPos(); - wrapper.freeRegion(chunkPos.x, chunkPos.y, chunkPos.z); + Vector3ic chunkPos = beforeChunkUnload.getChunkPos(); + wrapper.freeRegion(chunkPos.x(), chunkPos.y(), chunkPos.z()); } /** @@ -140,8 +142,8 @@ public void onChunkUloaded(BeforeChunkUnload beforeChunkUnload, EntityRef worldE */ @ReceiveEvent(components = WorldComponent.class) public void onNewChunk(OnChunkLoaded chunkAvailable, EntityRef worldEntity) { - Vector3i chunkPos = chunkAvailable.getChunkPos(); - Chunk chunk = chunkProvider.getChunk(chunkPos); + Vector3ic chunkPos = chunkAvailable.getChunkPos(); + Chunk chunk = chunkProvider.getChunk(JomlUtil.from(chunkPos)); ByteBuffer buffer = ByteBuffer.allocateDirect(2 * (ChunkConstants.SIZE_X * ChunkConstants.SIZE_Y * ChunkConstants.SIZE_Z)); buffer.order(ByteOrder.nativeOrder()); @@ -155,6 +157,6 @@ public void onNewChunk(OnChunkLoaded chunkAvailable, EntityRef worldEntity) { } } buffer.rewind(); - wrapper.setRegion(chunkPos.x, chunkPos.y, chunkPos.z, buffer.asShortBuffer()); + wrapper.setRegion(chunkPos.x(), chunkPos.y(), chunkPos.z(), buffer.asShortBuffer()); } } diff --git a/engine/src/main/java/org/terasology/rendering/world/WorldRendererSystem.java b/engine/src/main/java/org/terasology/rendering/world/WorldRendererSystem.java index ca1df405061..4d387c7836b 100644 --- a/engine/src/main/java/org/terasology/rendering/world/WorldRendererSystem.java +++ b/engine/src/main/java/org/terasology/rendering/world/WorldRendererSystem.java @@ -20,6 +20,7 @@ import org.terasology.entitySystem.systems.BaseComponentSystem; import org.terasology.entitySystem.systems.RegisterMode; import org.terasology.entitySystem.systems.RegisterSystem; +import org.terasology.math.JomlUtil; import org.terasology.registry.In; import org.terasology.world.WorldComponent; import org.terasology.world.chunks.event.BeforeChunkUnload; @@ -35,12 +36,12 @@ public class WorldRendererSystem extends BaseComponentSystem { @ReceiveEvent(components = WorldComponent.class) public void onChunkLoaded(OnChunkLoaded chunkLoaded, EntityRef entity) { - worldRenderer.onChunkLoaded(chunkLoaded.getChunkPos()); + worldRenderer.onChunkLoaded(JomlUtil.from(chunkLoaded.getChunkPos())); } @ReceiveEvent(components = WorldComponent.class) public void onChunkUnloaded(BeforeChunkUnload chunkUnloaded, EntityRef entity) { - worldRenderer.onChunkUnloaded(chunkUnloaded.getChunkPos()); + worldRenderer.onChunkUnloaded(JomlUtil.from(chunkUnloaded.getChunkPos())); } diff --git a/engine/src/main/java/org/terasology/world/chunks/event/BeforeChunkUnload.java b/engine/src/main/java/org/terasology/world/chunks/event/BeforeChunkUnload.java index a8af97be792..1036346d4ad 100644 --- a/engine/src/main/java/org/terasology/world/chunks/event/BeforeChunkUnload.java +++ b/engine/src/main/java/org/terasology/world/chunks/event/BeforeChunkUnload.java @@ -16,8 +16,9 @@ package org.terasology.world.chunks.event; +import org.joml.Vector3i; +import org.joml.Vector3ic; import org.terasology.entitySystem.event.Event; -import org.terasology.math.geom.Vector3i; /** */ @@ -25,11 +26,11 @@ public class BeforeChunkUnload implements Event { private Vector3i chunkPos = new Vector3i(); - public BeforeChunkUnload(Vector3i chunkPos) { + public BeforeChunkUnload(Vector3ic chunkPos) { this.chunkPos.set(chunkPos); } - public Vector3i getChunkPos() { + public Vector3ic getChunkPos() { return chunkPos; } } diff --git a/engine/src/main/java/org/terasology/world/chunks/event/OnChunkLoaded.java b/engine/src/main/java/org/terasology/world/chunks/event/OnChunkLoaded.java index dc004a98d4a..77ca9754381 100644 --- a/engine/src/main/java/org/terasology/world/chunks/event/OnChunkLoaded.java +++ b/engine/src/main/java/org/terasology/world/chunks/event/OnChunkLoaded.java @@ -16,19 +16,20 @@ package org.terasology.world.chunks.event; +import org.joml.Vector3i; +import org.joml.Vector3ic; import org.terasology.entitySystem.event.Event; -import org.terasology.math.geom.Vector3i; /** */ public class OnChunkLoaded implements Event { private Vector3i chunkPos = new Vector3i(); - public OnChunkLoaded(Vector3i chunkPos) { + public OnChunkLoaded(Vector3ic chunkPos) { this.chunkPos.set(chunkPos); } - public Vector3i getChunkPos() { + public Vector3ic getChunkPos() { return chunkPos; } } diff --git a/engine/src/main/java/org/terasology/world/chunks/localChunkProvider/LocalChunkProvider.java b/engine/src/main/java/org/terasology/world/chunks/localChunkProvider/LocalChunkProvider.java index e5dd1a8f9c6..7d137fb6d86 100644 --- a/engine/src/main/java/org/terasology/world/chunks/localChunkProvider/LocalChunkProvider.java +++ b/engine/src/main/java/org/terasology/world/chunks/localChunkProvider/LocalChunkProvider.java @@ -241,7 +241,7 @@ private void processReadyChunk(final Chunk chunk) { worldEntity.send(new OnChunkGenerated(chunk.getPosition())); } - worldEntity.send(new OnChunkLoaded(chunk.getPosition())); + worldEntity.send(new OnChunkLoaded(chunk.getPosition(new org.joml.Vector3i()))); } private void generateQueuedEntities(EntityStore store) { @@ -315,7 +315,7 @@ private boolean unloadChunkInternal(Vector3i pos) { return false; } - worldEntity.send(new BeforeChunkUnload(pos)); + worldEntity.send(new BeforeChunkUnload(JomlUtil.from(pos))); storageManager.deactivateChunk(chunk); chunk.dispose(); @@ -432,7 +432,7 @@ public void purgeWorld() { loadingPipeline.shutdown(); unloadRequestTaskMaster.shutdown(new ChunkUnloadRequest(), true); getAllChunks().stream().filter(ManagedChunk::isReady).forEach(chunk -> { - worldEntity.send(new BeforeChunkUnload(chunk.getPosition())); + worldEntity.send(new BeforeChunkUnload(chunk.getPosition(new org.joml.Vector3i()))); storageManager.deactivateChunk(chunk); chunk.dispose(); }); diff --git a/engine/src/main/java/org/terasology/world/chunks/localChunkProvider/RelevanceSystem.java b/engine/src/main/java/org/terasology/world/chunks/localChunkProvider/RelevanceSystem.java index 60a6be0a6a4..abf107e2d4f 100644 --- a/engine/src/main/java/org/terasology/world/chunks/localChunkProvider/RelevanceSystem.java +++ b/engine/src/main/java/org/terasology/world/chunks/localChunkProvider/RelevanceSystem.java @@ -81,7 +81,7 @@ public void onNewChunk(OnChunkLoaded chunkAvailable, EntityRef worldEntity) { @ReceiveEvent(components = WorldComponent.class) public void onRemoveChunk(BeforeChunkUnload chunkUnloadEvent, EntityRef worldEntity) { for (ChunkRelevanceRegion region : regions.values()) { - region.chunkUnloaded(chunkUnloadEvent.getChunkPos()); + region.chunkUnloaded(JomlUtil.from(chunkUnloadEvent.getChunkPos())); } } diff --git a/engine/src/main/java/org/terasology/world/chunks/remoteChunkProvider/RemoteChunkProvider.java b/engine/src/main/java/org/terasology/world/chunks/remoteChunkProvider/RemoteChunkProvider.java index 9f9c5aa707e..6032129dddb 100644 --- a/engine/src/main/java/org/terasology/world/chunks/remoteChunkProvider/RemoteChunkProvider.java +++ b/engine/src/main/java/org/terasology/world/chunks/remoteChunkProvider/RemoteChunkProvider.java @@ -114,7 +114,7 @@ public void update() { if (listener != null) { listener.onChunkReady(chunk.getPosition()); } - worldEntity.send(new OnChunkLoaded(chunk.getPosition())); + worldEntity.send(new OnChunkLoaded(chunk.getPosition(new org.joml.Vector3i()))); } } @@ -124,7 +124,7 @@ private void checkForUnload() { for (Vector3i pos : positions) { Chunk removed = chunkCache.remove(pos); if (removed != null && !removed.isReady()) { - worldEntity.send(new BeforeChunkUnload(pos)); + worldEntity.send(new BeforeChunkUnload(JomlUtil.from(pos))); removed.dispose(); } }