Skip to content

Commit

Permalink
refactor(Chunk): provide default implementations of methods where pos…
Browse files Browse the repository at this point in the history
…sible (#4989)
  • Loading branch information
keturn authored Jan 22, 2022
1 parent ba06085 commit edecd07
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 114 deletions.
63 changes: 47 additions & 16 deletions engine/src/main/java/org/terasology/engine/world/chunks/Chunk.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The Terasology Foundation
// Copyright 2022 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.engine.world.chunks;

Expand Down Expand Up @@ -35,15 +35,19 @@ public interface Chunk extends RenderableChunk {
* @deprecated use {@link #getPosition()}
*/
@Deprecated
Vector3i getPosition(Vector3i dest);
default Vector3i getPosition(Vector3i dest) {
return dest.set(getPosition());
}

/**
* Returns block at given position relative to the chunk.
*
* @param pos Position of the block relative to corner of the chunk
* @return Block at given position
*/
Block getBlock(Vector3ic pos);
default Block getBlock(Vector3ic pos) {
return getBlock(pos.x(), pos.y(), pos.z());
}

/**
* Returns block at given position relative to the chunk.
Expand Down Expand Up @@ -73,7 +77,9 @@ public interface Chunk extends RenderableChunk {
* @param block Block to set block at given position to
* @return Old Block at given position
*/
Block setBlock(Vector3ic pos, Block block);
default Block setBlock(Vector3ic pos, Block block) {
return setBlock(pos.x(), pos.y(), pos.z(), block);
}

/**
* Sets one of the per-block custom data values at a given position relative to the chunk.
Expand All @@ -97,7 +103,9 @@ public interface Chunk extends RenderableChunk {
* @param pos Position of the block relative to the corner of the chunk
* @param value New value to set the block to
*/
void setExtraData(int index, Vector3ic pos, int value);
default void setExtraData(int index, Vector3ic pos, int value) {
setExtraData(index, pos.x(), pos.y(), pos.z(), value);
}

/**
* Returns one of the per-block custom data values at a given position relative to the chunk.
Expand All @@ -117,7 +125,9 @@ public interface Chunk extends RenderableChunk {
* @param pos Position of the block relative to the corner of the chunk
* @return Selected extra data value at the given location
*/
int getExtraData(int index, Vector3ic pos);
default int getExtraData(int index, Vector3ic pos) {
return getExtraData(index, pos.x(), pos.y(), pos.z());
}


/**
Expand Down Expand Up @@ -160,7 +170,9 @@ default Vector3f getRenderPosition() {
* @param dest Position in this chunk you want to transform
* @return Transformed position
*/
Vector3i chunkToWorldPosition(Vector3ic blockPos, Vector3i dest);
default Vector3i chunkToWorldPosition(Vector3ic blockPos, Vector3i dest) {
return chunkToWorldPosition(blockPos.x(), blockPos.y(), blockPos.z(), dest);
}


/**
Expand Down Expand Up @@ -202,29 +214,38 @@ default Vector3f getRenderPosition() {
/**
* @return Size of the chunk along the X axis.
*/
int getChunkSizeX();
default int getChunkSizeX() {
return Chunks.SIZE_X;
}

/**
* @return Size of the chunk along the Y axis.
*/
int getChunkSizeY();
default int getChunkSizeY() {
return Chunks.SIZE_Y;
}

/**
* @return Size of the chunk along the Z axis.
*/
int getChunkSizeZ();
default int getChunkSizeZ() {
return Chunks.SIZE_Z;
}

/**
* @return Chunk's Region
*/
BlockRegionc getRegion();

/**
* Returns the current amount of sunlight at given position relative to the chunk.
*
* @param pos Position of the block relative to corner of the chunk
* @return Current sunlight
*/
byte getSunlight(Vector3ic pos);
default byte getSunlight(Vector3ic pos) {
return getSunlight(pos.x(), pos.y(), pos.z());
}

/**
* Returns the current amount of sunlight at given position relative to the chunk.
Expand All @@ -243,7 +264,9 @@ default Vector3f getRenderPosition() {
* @param amount Amount of sunlight to set this block to
* @return False if the amount is same as the old value, true otherwise
*/
boolean setSunlight(Vector3ic pos, byte amount);
default boolean setSunlight(Vector3ic pos, byte amount) {
return setSunlight(pos.x(), pos.y(), pos.z(), amount);
}

/**
* Sets the amount of sunlight at given position relative to the chunk.
Expand All @@ -261,7 +284,9 @@ default Vector3f getRenderPosition() {
* @param pos Position of the block relative to corner of the chunk
* @return Current sunlight regeneration
*/
byte getSunlightRegen(Vector3ic pos);
default byte getSunlightRegen(Vector3ic pos) {
return getSunlightRegen(pos.x(), pos.y(), pos.z());
}

/**
* Returns current value of sunlight regeneration for given block relative to the chunk.
Expand All @@ -280,7 +305,9 @@ default Vector3f getRenderPosition() {
* @param amount Sunlight regeneration amount
* @return False if the amount is same as the old value, true otherwise
*/
boolean setSunlightRegen(Vector3ic pos, byte amount);
default boolean setSunlightRegen(Vector3ic pos, byte amount) {
return setSunlightRegen(pos.x(), pos.y(), pos.z(), amount);
}

/**
* Sets sunlight regeneration for given block relative to the chunk.
Expand All @@ -299,7 +326,9 @@ default Vector3f getRenderPosition() {
* @param pos Position of the block relative to corner of the chunk
* @return Current lightness
*/
byte getLight(Vector3ic pos);
default byte getLight(Vector3ic pos) {
return getLight(pos.x(), pos.y(), pos.z());
}

/**
* Returns current amount of light for given block relative to the chunk.
Expand All @@ -318,7 +347,9 @@ default Vector3f getRenderPosition() {
* @param amount Lightness value
* @return False if the amount is same as the old value, true otherwise
*/
boolean setLight(Vector3ic pos, byte amount);
default boolean setLight(Vector3ic pos, byte amount) {
return setLight(pos.x(), pos.y(), pos.z(), amount);
}

/**
* Sets lightness for given block relative to the chunk.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The Terasology Foundation
// Copyright 2022 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.engine.world.chunks.internal;

Expand All @@ -8,24 +8,24 @@
import org.joml.Vector3ic;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.engine.world.chunks.blockdata.ExtraBlockDataManager;
import org.terasology.engine.world.chunks.blockdata.TeraArray;
import org.terasology.engine.world.chunks.blockdata.TeraDenseArray16Bit;
import org.terasology.engine.world.chunks.blockdata.TeraDenseArray8Bit;
import org.terasology.engine.world.chunks.blockdata.TeraSparseArray8Bit;
import org.terasology.joml.geom.AABBf;
import org.terasology.joml.geom.AABBfc;
import org.terasology.engine.monitoring.chunk.ChunkMonitor;
import org.terasology.protobuf.EntityData;
import org.terasology.engine.rendering.primitives.ChunkMesh;
import org.terasology.engine.world.block.Block;
import org.terasology.engine.world.block.BlockManager;
import org.terasology.engine.world.block.BlockRegion;
import org.terasology.engine.world.chunks.Chunk;
import org.terasology.engine.world.chunks.ChunkBlockIterator;
import org.terasology.engine.world.chunks.Chunks;
import org.terasology.engine.world.chunks.blockdata.ExtraBlockDataManager;
import org.terasology.engine.world.chunks.blockdata.TeraArray;
import org.terasology.engine.world.chunks.blockdata.TeraDenseArray16Bit;
import org.terasology.engine.world.chunks.blockdata.TeraDenseArray8Bit;
import org.terasology.engine.world.chunks.blockdata.TeraSparseArray8Bit;
import org.terasology.engine.world.chunks.deflate.TeraDeflator;
import org.terasology.engine.world.chunks.deflate.TeraStandardDeflator;
import org.terasology.joml.geom.AABBf;
import org.terasology.joml.geom.AABBfc;
import org.terasology.protobuf.EntityData;

import java.text.DecimalFormat;

Expand Down Expand Up @@ -89,10 +89,10 @@ public ChunkImpl(Vector3ic chunkPos, TeraArray blocks, TeraArray[] extra, BlockM
dirty = true;
this.blockManager = blockManager;
region = new BlockRegion(
chunkPos.x() * Chunks.SIZE_X,
chunkPos.y() * Chunks.SIZE_Y,
chunkPos.z() * Chunks.SIZE_Z)
.setSize(Chunks.SIZE_X, Chunks.SIZE_Y, Chunks.SIZE_Z);
chunkPos.x() * getChunkSizeX(),
chunkPos.y() * getChunkSizeY(),
chunkPos.z() * getChunkSizeZ())
.setSize(getChunkSizeX(), getChunkSizeY(), getChunkSizeZ());
ChunkMonitor.fireChunkCreated(this);
}

Expand All @@ -101,11 +101,6 @@ public Vector3ic getPosition() {
return chunkPos;
}

@Override
public Vector3i getPosition(Vector3i dest) {
return dest.set(chunkPos.x(), chunkPos.y(), chunkPos.z());
}

@Override
public boolean isDirty() {
return dirty;
Expand All @@ -130,12 +125,6 @@ public int getEstimatedMemoryConsumptionInBytes() {
}


@Override
public Block getBlock(Vector3ic pos) {
short id = (short) blockData.get(pos.x(), pos.y(), pos.z());
return blockManager.getBlock(id);
}

@Override
public final Block getBlock(int x, int y, int z) {
short id = (short) blockData.get(x, y, z);
Expand All @@ -154,68 +143,33 @@ public Block setBlock(int x, int y, int z, Block block) {
return blockManager.getBlock((short) oldValue);
}

@Override
public Block setBlock(Vector3ic pos, Block block) {
return setBlock(pos.x(), pos.y(), pos.z(), block);
}

@Override
public byte getSunlight(Vector3ic pos) {
return getSunlight(pos.x(), pos.y(), pos.z());
}

@Override
public byte getSunlight(int x, int y, int z) {
return (byte) sunlightData.get(x, y, z);
}

@Override
public boolean setSunlight(Vector3ic pos, byte amount) {
return setSunlight(pos.x(), pos.y(), pos.z(), amount);
}

@Override
public boolean setSunlight(int x, int y, int z, byte amount) {
Preconditions.checkArgument(amount >= 0 && amount <= Chunks.MAX_SUNLIGHT);
return sunlightData.set(x, y, z, amount) != amount;
}

@Override
public byte getSunlightRegen(Vector3ic pos) {
return getSunlightRegen(pos.x(), pos.y(), pos.z());
}

@Override
public byte getSunlightRegen(int x, int y, int z) {
return (byte) sunlightRegenData.get(x, y, z);
}

@Override
public boolean setSunlightRegen(Vector3ic pos, byte amount) {
return setSunlightRegen(pos.x(), pos.y(), pos.z(), amount);
}

@Override
public boolean setSunlightRegen(int x, int y, int z, byte amount) {
Preconditions.checkArgument(amount >= 0 && amount <= Chunks.MAX_SUNLIGHT_REGEN);
return sunlightRegenData.set(x, y, z, amount) != amount;
}

@Override
public byte getLight(Vector3ic pos) {
return getLight(pos.x(), pos.y(), pos.z());
}

@Override
public byte getLight(int x, int y, int z) {
return (byte) lightData.get(x, y, z);
}

@Override
public boolean setLight(Vector3ic pos, byte amount) {
return setLight(pos.x(), pos.y(), pos.z(), amount);
}

@Override
public boolean setLight(int x, int y, int z, byte amount) {
Preconditions.checkArgument(amount >= 0 && amount <= Chunks.MAX_LIGHT);
Expand All @@ -227,11 +181,6 @@ public int getExtraData(int index, int x, int y, int z) {
return extraData[index].get(x, y, z);
}

@Override
public int getExtraData(int index, Vector3ic pos) {
return getExtraData(index, pos.x(), pos.y(), pos.z());
}

@Override
public void setExtraData(int index, int x, int y, int z, int value) {
if (extraDataSnapshots != null && extraData[index] == extraDataSnapshots[index]) {
Expand All @@ -240,11 +189,6 @@ public void setExtraData(int index, int x, int y, int z, int value) {
extraData[index].set(x, y, z, value);
}

@Override
public void setExtraData(int index, Vector3ic pos, int value) {
setExtraData(index, pos.x(), pos.y(), pos.z(), value);
}

@Override
public Vector3i getChunkWorldOffset(Vector3i dest) {
return dest.set(getChunkWorldOffsetX(), getChunkWorldOffsetY(), getChunkWorldOffsetZ());
Expand All @@ -265,11 +209,6 @@ public int getChunkWorldOffsetZ() {
return chunkPos.z() * getChunkSizeZ();
}

@Override
public Vector3i chunkToWorldPosition(Vector3ic blockPos, Vector3i dest) {
return chunkToWorldPosition(blockPos.x(), blockPos.y(), blockPos.z(), dest);
}

@Override
public Vector3i chunkToWorldPosition(int x, int y, int z, Vector3i dest) {
return dest.set(chunkToWorldPositionX(x), chunkToWorldPositionY(y), chunkToWorldPositionZ(z));
Expand Down Expand Up @@ -466,10 +405,10 @@ public void markReady() {
public void prepareForReactivation() {
if (disposed) {
disposed = false;
sunlightData = new TeraDenseArray8Bit(Chunks.SIZE_X, Chunks.SIZE_Y, Chunks.SIZE_Z);
sunlightRegenData = new TeraDenseArray8Bit(Chunks.SIZE_X, Chunks.SIZE_Y,
Chunks.SIZE_Z);
lightData = new TeraDenseArray8Bit(Chunks.SIZE_X, Chunks.SIZE_Y, Chunks.SIZE_Z);
sunlightData = new TeraDenseArray8Bit(getChunkSizeX(), getChunkSizeY(), getChunkSizeZ());
sunlightRegenData = new TeraDenseArray8Bit(getChunkSizeX(), getChunkSizeY(),
getChunkSizeZ());
lightData = new TeraDenseArray8Bit(getChunkSizeX(), getChunkSizeY(), getChunkSizeZ());
}
}

Expand Down Expand Up @@ -507,21 +446,6 @@ public BlockRegion getRegion() {
return region;
}

@Override
public int getChunkSizeX() {
return Chunks.SIZE_X;
}

@Override
public int getChunkSizeY() {
return Chunks.SIZE_Y;
}

@Override
public int getChunkSizeZ() {
return Chunks.SIZE_Z;
}

@Override
public ChunkBlockIterator getBlockIterator() {
return new ChunkBlockIteratorImpl(blockManager, getChunkWorldOffset(new Vector3i()), blockData);
Expand Down
Loading

0 comments on commit edecd07

Please sign in to comment.