Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(JOML): migrate SectorUtils and Events #4378

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Comment on lines +131 to 133
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The order for assertions is expected, actual, message, so I corrected this while adding JomlUtil conversion to compare vectors of the same type.

});
}
Expand Down Expand Up @@ -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");
});

Expand Down Expand Up @@ -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");
}

Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -57,7 +59,7 @@ public static SectorRegionComponent createSectorRegionComponent(Collection<Vecto
*/
public static void addChunksToRegionComponent(SectorRegionComponent regionComponent, Collection<Chunk> chunks) {
regionComponent.chunks.addAll(chunks.stream()
.map(Chunk::getPosition)
.map(k -> k.getPosition(new Vector3i()))
.collect(Collectors.toSet()));
}

Expand Down Expand Up @@ -90,12 +92,13 @@ public static void addChunksToRegionComponent(EntityRef entity, Collection<Vecto
public static Set<Vector3i> getWatchedChunks(EntityRef entity) {
Set<Vector3i> 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;
}
Expand All @@ -108,7 +111,7 @@ public static Set<Vector3i> 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<Vector3i> readyChunks = getWatchedChunks(entity).stream()
.filter(chunkProvider::isChunkReady)
.collect(Collectors.toSet());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -35,7 +35,7 @@
public class ChunkEventErrorLogger extends BaseComponentSystem {
private static final Logger logger = LoggerFactory.getLogger(ChunkEventErrorLogger.class);

private Set<Vector3i> loadedChunks = Sets.newHashSet();
private Set<Vector3ic> loadedChunks = Sets.newHashSet();

@ReceiveEvent(components = {WorldComponent.class})
public void onNewChunk(OnChunkLoaded chunkAvailable, EntityRef worldEntity) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}

/**
Expand All @@ -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());
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}

/**
Expand All @@ -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());
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,21 @@

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 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())));
}
}

Expand All @@ -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();
}
}
Expand Down