Skip to content

Commit

Permalink
feat(JOML): migrate SectorUtils and Events
Browse files Browse the repository at this point in the history
  • Loading branch information
pollend committed Jan 6, 2021
1 parent 19c233b commit b4b306e
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 34 deletions.
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 @@ -22,6 +22,7 @@
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.world.WorldComponent;
import org.terasology.world.chunks.event.BeforeChunkUnload;
Expand All @@ -39,7 +40,7 @@ public class ChunkEventErrorLogger extends BaseComponentSystem {

@ReceiveEvent(components = {WorldComponent.class})
public void onNewChunk(OnChunkLoaded chunkAvailable, EntityRef worldEntity) {
if (!loadedChunks.add(chunkAvailable.getChunkPos())) {
if (!loadedChunks.add(JomlUtil.from(chunkAvailable.getChunkPos()))) {
logger.error("Multiple loads of chunk {}", chunkAvailable.getChunkPos());
}
}
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

0 comments on commit b4b306e

Please sign in to comment.