Skip to content

Commit

Permalink
chore(JOML): migrate SectorUtils and Events (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
skaldarnar authored Jan 9, 2021
1 parent 438d722 commit c9677ab
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/main/java/org/terasology/randomUpdate/RandomUpdateSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import org.joml.Vector3ic;
import org.terasology.engine.Time;
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.entitySystem.systems.UpdateSubscriberSystem;
import org.terasology.math.JomlUtil;
import org.terasology.math.geom.Vector3i;
import org.terasology.monitoring.PerformanceMonitor;
import org.terasology.registry.In;
Expand All @@ -32,6 +34,7 @@
import org.terasology.world.block.Block;
import org.terasology.world.block.BlockManager;
import org.terasology.world.chunks.ChunkConstants;
import org.terasology.world.chunks.Chunks;
import org.terasology.world.chunks.event.BeforeChunkUnload;
import org.terasology.world.chunks.event.OnChunkLoaded;

Expand All @@ -43,7 +46,7 @@
*/
@RegisterSystem
public class RandomUpdateSystem extends BaseComponentSystem implements UpdateSubscriberSystem {
private Set<Vector3i> loadedChunks = new HashSet<>();
private Set<Vector3ic> loadedChunks = new HashSet<>();

private long lastUpdate;
private int updateInterval = 20;
Expand All @@ -62,7 +65,7 @@ public class RandomUpdateSystem extends BaseComponentSystem implements UpdateSub

@ReceiveEvent
public void addChunk(OnChunkLoaded chunkLoaded, EntityRef worldEntity) {
loadedChunks.add(chunkLoaded.getChunkPos());
loadedChunks.add(new org.joml.Vector3i(chunkLoaded.getChunkPos()));
}

@ReceiveEvent
Expand All @@ -81,19 +84,24 @@ public void update(float delta) {
FastRandom rand = new FastRandom();
Multimap<Block, Vector3i> nonEntityUpdates = HashMultimap.create();

for (Vector3i loadedChunk : loadedChunks) {
for (Vector3ic loadedChunk : loadedChunks) {
for (int i = 0; i < updateCountPerChunk; i++) {
final Vector3i blockPosition = new Vector3i(
loadedChunk.x * ChunkConstants.SIZE_X + rand.nextInt(ChunkConstants.SIZE_X),
loadedChunk.y * ChunkConstants.SIZE_Y + rand.nextInt(ChunkConstants.SIZE_Y),
loadedChunk.z * ChunkConstants.SIZE_Z + rand.nextInt(ChunkConstants.SIZE_Z));
//TODO: reduce the number of vector allocations here
// - only compute the chunk location once: loadedChunk * Chunks.SIZE for each loaded chunk
// - reuse the allocated vector for different chunks
// - compute the random position in another temporary vector
// - only create new instance when storing the block in `nonEntityUpdates`
final org.joml.Vector3i blockPosition = new org.joml.Vector3i(
loadedChunk.x() * Chunks.SIZE_X + rand.nextInt(Chunks.SIZE_X),
loadedChunk.y() * Chunks.SIZE_Y + rand.nextInt(Chunks.SIZE_Y),
loadedChunk.z() * Chunks.SIZE_Z + rand.nextInt(Chunks.SIZE_Z));
EntityRef entity = blockEntityRegistry.getExistingBlockEntityAt(blockPosition);
if (entity.exists()) {
entity.send(RandomUpdateEvent.singleton());
} else {
final Block block = worldProvider.getBlock(blockPosition);
if (block.isPenetrable()) {
nonEntityUpdates.put(block, blockPosition);
nonEntityUpdates.put(block, JomlUtil.from(blockPosition));
}
}
}
Expand Down

0 comments on commit c9677ab

Please sign in to comment.