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 methods of WorldProvider #3899

Merged
merged 6 commits into from
Apr 30, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 10 additions & 2 deletions engine-tests/src/main/java/org/terasology/MapWorldProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
package org.terasology;

import com.google.common.collect.Maps;
import org.joml.Vector3ic;
import org.terasology.entitySystem.entity.EntityRef;
import org.terasology.math.ChunkMath;
import org.terasology.math.JomlUtil;
import org.terasology.math.Region3i;
import org.terasology.math.geom.Vector3i;
import org.terasology.world.WorldChangeListener;
Expand Down Expand Up @@ -89,6 +91,12 @@ public Block setBlock(Vector3i pos, Block type) {
return blocks.put(pos, type);
}

@Override
public Block setBlock(Vector3ic pos, Block type) {
return blocks.put(JomlUtil.from(pos), type);
}


@Override
public Block getBlock(int x, int y, int z) {
Vector3i pos = new Vector3i(x, y, z);
Expand Down Expand Up @@ -151,12 +159,12 @@ public byte getSunlight(int x, int y, int z) {
public byte getTotalLight(int x, int y, int z) {
return 0;
}

@Override
public int setExtraData(int index, Vector3i pos, int value) {
return 0;
}

@Override
public int getExtraData(int index, int x, int y, int z) {
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
package org.terasology.testUtil;

import com.google.common.collect.Maps;
import org.joml.Vector3ic;
import org.terasology.entitySystem.entity.EntityRef;
import org.terasology.math.JomlUtil;
import org.terasology.math.Region3i;
import org.terasology.math.geom.Vector3i;
import org.terasology.world.WorldChangeListener;
Expand Down Expand Up @@ -102,7 +104,12 @@ public boolean isRegionRelevant(Region3i region) {

@Override
public Block setBlock(Vector3i pos, Block type) {
Block old = blocks.put(pos, type);
return this.setBlock(JomlUtil.from(pos), type);
}

@Override
public Block setBlock(Vector3ic pos, Block type) {
Block old = blocks.put(JomlUtil.from(pos), type);
if (old == null) {
return air;
}
Expand Down Expand Up @@ -142,18 +149,18 @@ public byte getSunlight(int x, int y, int z) {
public byte getTotalLight(int x, int y, int z) {
return 0; //To change body of implemented methods use File | Settings | File Templates.
}

@Override
public int setExtraData(int index, Vector3i pos, int value) {
Integer prevValue = getExtraDataLayer(index).put(pos, value);
return prevValue == null ? 0 : prevValue;
}

@Override
public int getExtraData(int index, int x, int y, int z) {
return getExtraDataLayer(index).getOrDefault(new Vector3i(x, y, z), 0);
}

private Map<Vector3i, Integer> getExtraDataLayer(int index) {
while (extraData.size() <= index) {
extraData.add(Maps.newHashMap());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,16 @@ public void testEntityBecomesTemporaryWhenChangedFromAKeepActiveBlock() {
assertFalse(blockEntity.isActive());
}

@Test
public void testEntityBecomesTemporaryWhenChangedFromAKeepActiveBlockJoml() {
Copy link
Member

Choose a reason for hiding this comment

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

I love this test name 👍

worldProvider.setBlock(new org.joml.Vector3i(), keepActiveBlock);
EntityRef blockEntity = worldProvider.getBlockEntityAt(new Vector3i(0, 0, 0));
worldProvider.setBlock(new org.joml.Vector3i(), airBlock);
worldProvider.update(1.0f);
assertFalse(blockEntity.isActive());
}


@Test
public void testEntityBecomesTemporaryIfForceBlockActiveComponentRemoved() {
EntityRef blockEntity = worldProvider.getBlockEntityAt(new Vector3i(0, 0, 0));
Expand Down
65 changes: 58 additions & 7 deletions engine/src/main/java/org/terasology/world/WorldProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.terasology.world;

import org.joml.Vector3fc;
import org.joml.Vector3ic;
import org.terasology.math.geom.Vector3f;
import org.terasology.math.geom.Vector3i;
import org.terasology.world.block.Block;
Expand All @@ -29,19 +31,49 @@ public interface WorldProvider extends WorldProviderCore {
/**
* An active block is in a chunk that is available and fully generated.
*
* @param pos
* @param pos The position
* @return Whether the given block is active
* @deprecated This is scheduled for removal in an upcoming version
* method will be replaced with JOML implementation {@link #isBlockRelevant(Vector3ic)}.
*/
@Deprecated
boolean isBlockRelevant(Vector3i pos);

/**
* An active block is in a chunk that is available and fully generated.
* @param pos The position
* @return Whether the given block is active
*/
boolean isBlockRelevant(Vector3ic pos);
Copy link
Member

Choose a reason for hiding this comment

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

Should we consider (in general) adding default interface implementations when adding new methods like this? That could avoid having to update some things in modulespace. In this case I'm not sure it makes sense as I only see one non-engine usage over in FlexiblePathfinding.

Copy link
Member Author

@pollend pollend Apr 22, 2020

Choose a reason for hiding this comment

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

It can't hurt at the moment but a lot of this is temporary and all the old methods are going to go away anyways. It can't hurt to implement it even if the use case is limited.


/**
* An active block is in a chunk that is available and fully generated.
*
* @param pos The position
* @return Whether the given block is active
* @deprecated This is scheduled for removal in an upcoming version
* method will be replaced with JOML implementation {@link #isBlockRelevant(Vector3fc)}.
*/
@Deprecated
boolean isBlockRelevant(Vector3f pos);

/**
* An active block is in a chunk that is available and fully generated.
*
* @param pos The position
* @return Whether the given block is active
*/
boolean isBlockRelevant(Vector3fc pos);

/**
* Returns the block value at the given position.
*
* @param pos The position
* @return The block value at the given position
* @deprecated This is scheduled for removal in an upcoming version
* method will be replaced with JOML implementation {@link #getBlock(Vector3fc)}.
*/
@Deprecated
Block getBlock(Vector3f pos);

/**
Expand All @@ -50,8 +82,27 @@ public interface WorldProvider extends WorldProviderCore {
* @param pos The position
* @return The block value at the given position
*/
Block getBlock(Vector3fc pos);

/**
* Returns the block value at the given position.
*
* @param pos The position
* @return The block value at the given position
* @deprecated This is scheduled for removal in an upcoming version
* method will be replaced with JOML implementation {@link #getBlock(Vector3ic)}.
*/
@Deprecated
Block getBlock(Vector3i pos);

/**
* Returns the block value at the given position.
*
* @param pos The position
* @return The block value at the given position
*/
Block getBlock(Vector3ic pos);

/**
* Returns the light value at the given position.
*
Expand Down Expand Up @@ -87,7 +138,7 @@ public interface WorldProvider extends WorldProviderCore {
byte getSunlight(Vector3i pos);

byte getTotalLight(Vector3i pos);

/**
* Gets one of the per-block custom data values at the given position. Returns 0 outside the view.
*
Expand All @@ -96,7 +147,7 @@ public interface WorldProvider extends WorldProviderCore {
* @return The (index)th extra-data value at the given position
*/
int getExtraData(int index, Vector3i pos);

/**
* Sets one of the per-block custom data values at the given position, if it is within the view.
*
Expand All @@ -108,7 +159,7 @@ public interface WorldProvider extends WorldProviderCore {
* @return The replaced value
*/
int setExtraData(int index, int x, int y, int z, int value);

/**
* Gets one of the per-block custom data values at the given position. Returns 0 outside the view.
*
Expand All @@ -119,7 +170,7 @@ public interface WorldProvider extends WorldProviderCore {
* @return The named extra-data value at the given position
*/
int getExtraData(String fieldName, int x, int y, int z);

/**
* Gets one of the per-block custom data values at the given position. Returns 0 outside the view.
*
Expand All @@ -128,7 +179,7 @@ public interface WorldProvider extends WorldProviderCore {
* @return The named extra-data value at the given position
*/
int getExtraData(String fieldName, Vector3i pos);

/**
* Sets one of the per-block custom data values at the given position, if it is within the view.
*
Expand All @@ -140,7 +191,7 @@ public interface WorldProvider extends WorldProviderCore {
* @return The replaced value
*/
int setExtraData(String fieldName, int x, int y, int z, int value);

/**
* Sets one of the per-block custom data values at the given position, if it is within the view.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.terasology.world.internal;

import org.joml.Vector3ic;
import org.terasology.entitySystem.entity.EntityRef;
import org.terasology.math.Region3i;
import org.terasology.math.geom.Vector3i;
Expand Down Expand Up @@ -96,6 +97,11 @@ public Block setBlock(Vector3i pos, Block type) {
return base.setBlock(pos, type);
}

@Override
public Block setBlock(Vector3ic pos, Block type) {
return base.setBlock(pos, type);
}

@Override
public Map<Vector3i, Block> setBlocks(Map<Vector3i, Block> blocks) {
return base.setBlocks(blocks);
Expand All @@ -120,12 +126,12 @@ public byte getSunlight(int x, int y, int z) {
public byte getTotalLight(int x, int y, int z) {
return base.getTotalLight(x, y, z);
}

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

@Override
public int setExtraData(int index, Vector3i pos, int value) {
return base.setExtraData(index, pos, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.joml.Vector3ic;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.context.Context;
Expand All @@ -42,6 +43,7 @@
import org.terasology.entitySystem.prefab.Prefab;
import org.terasology.entitySystem.systems.UpdateSubscriberSystem;
import org.terasology.logic.location.LocationComponent;
import org.terasology.math.JomlUtil;
import org.terasology.math.Region3i;
import org.terasology.math.geom.Vector3f;
import org.terasology.math.geom.Vector3i;
Expand Down Expand Up @@ -113,11 +115,16 @@ public void shutdown() {

@Override
public Block setBlock(Vector3i pos, Block type) {
return this.setBlock(JomlUtil.from(pos),type);
}

@Override
public Block setBlock(Vector3ic pos, Block type) {
if (GameThread.isCurrentThread()) {
EntityRef blockEntity = getBlockEntityAt(pos);
Block oldType = super.setBlock(pos, type);
EntityRef blockEntity = getBlockEntityAt(JomlUtil.from(pos));
Block oldType = super.setBlock(JomlUtil.from(pos), type);
if (oldType != null) {
updateBlockEntity(blockEntity, pos, oldType, type, false, Collections.<Class<? extends Component>>emptySet());
updateBlockEntity(blockEntity, JomlUtil.from(pos), oldType, type, false, Collections.<Class<? extends Component>>emptySet());
}
return oldType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.terasology.world.internal;

import com.google.common.collect.Maps;
import org.joml.Vector3ic;
import org.terasology.entitySystem.entity.EntityRef;
import org.terasology.math.Region3i;
import org.terasology.math.geom.Vector3i;
Expand Down Expand Up @@ -100,9 +101,22 @@ public interface WorldProviderCore {
* @param pos The world position to change
* @param type The type of the block to set
* @return The previous block type. Null if the change failed (because the necessary chunk was not loaded)
* @deprecated This is scheduled for removal in an upcoming version
* method will be replaced with JOML implementation {@link #setBlock(Vector3ic, Block)}.
*/
@Deprecated
Block setBlock(Vector3i pos, Block type);


/**
* Places a block of a specific type at a given position
*
* @param pos The world position to change
* @param type The type of the block to set
* @return The previous block type. Null if the change failed (because the necessary chunk was not loaded)
*/
Block setBlock(Vector3ic pos, Block type);

/**
* Places all given blocks of specific types at their corresponding positions
* </p>
Expand Down Expand Up @@ -152,7 +166,7 @@ default Map<Vector3i, Block> setBlocks(Map<Vector3i, Block> blocks) {
byte getSunlight(int x, int y, int z);

byte getTotalLight(int x, int y, int z);

/**
* Gets one of the per-block custom data values at the given position. Returns 0 outside the view.
*
Expand All @@ -163,7 +177,7 @@ default Map<Vector3i, Block> setBlocks(Map<Vector3i, Block> blocks) {
* @return The (index)th extra-data value at the given position
*/
int getExtraData(int index, int x, int y, int z);

/**
* Sets one of the per-block custom data values at the given position, if it is within the view.
*
Expand All @@ -185,4 +199,5 @@ default Map<Vector3i, Block> setBlocks(Map<Vector3i, Block> blocks) {
* @return an unmodifiable view on the generated relevant regions
*/
Collection<Region3i> getRelevantRegions();

}
Loading