Skip to content

Only compute solid blocks once #3068

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

Merged
merged 2 commits into from
Jan 18, 2025
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.fastasyncworldedit.core.extent.processor.heightmap;

import com.fastasyncworldedit.core.registry.state.PropertyKey;
import com.sk89q.worldedit.function.mask.SolidBlockMask;
import com.sk89q.worldedit.registry.state.Property;
import com.sk89q.worldedit.world.block.BlockCategories;
import com.sk89q.worldedit.world.block.BlockState;
Expand All @@ -17,19 +18,19 @@ public enum HeightMapType {
MOTION_BLOCKING {
@Override
public boolean includes(BlockState state) {
return state.getMaterial().isMovementBlocker() || HeightMapType.hasFluid(state);
return isMovementBlocker(state) || HeightMapType.hasFluid(state);
}
},
MOTION_BLOCKING_NO_LEAVES {
@Override
public boolean includes(BlockState state) {
return (state.getMaterial().isMovementBlocker() || HeightMapType.hasFluid(state)) && !HeightMapType.isLeaf(state);
return (isMovementBlocker(state) || HeightMapType.hasFluid(state)) && !HeightMapType.isLeaf(state);
}
},
OCEAN_FLOOR {
@Override
public boolean includes(BlockState state) {
return state.getMaterial().isMovementBlocker();
return HeightMapType.isMovementBlocker(state);
}
},
WORLD_SURFACE {
Expand All @@ -39,6 +40,10 @@ public boolean includes(BlockState state) {
}
};

private static boolean isMovementBlocker(BlockState state) {
return SolidBlockMask.isSolid(state);
}

static {
BlockCategories.LEAVES.getAll(); // make sure this category is initialized, otherwise isLeaf might fail
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import com.sk89q.worldedit.function.mask.BlockMask;
import com.sk89q.worldedit.function.mask.ExistingBlockMask;
import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.function.mask.SolidBlockMask;
import com.sk89q.worldedit.function.operation.Operation;
import com.sk89q.worldedit.function.operation.Operations;
import com.sk89q.worldedit.function.pattern.BlockPattern;
Expand Down Expand Up @@ -456,32 +457,32 @@ default int getNearestSurfaceTerrainBlock(
int clearanceBelow = y - minY;
int clearance = Math.min(clearanceAbove, clearanceBelow);
BlockState block = getBlock(x, y, z);
boolean state = !block.getBlockType().getMaterial().isMovementBlocker();
boolean state = !SolidBlockMask.isSolid(block);
int offset = state ? 0 : 1;
for (int d = 0; d <= clearance; d++) {
int y1 = y + d;
block = getBlock(x, y1, z);
if (block.getMaterial().isMovementBlocker() == state && block.getBlockType() != BlockTypes.__RESERVED__) {
if (matchesSolidState(block, state)) {
return y1 - offset;
}
int y2 = y - d;
block = getBlock(x, y2, z);
if (block.getMaterial().isMovementBlocker() == state && block.getBlockType() != BlockTypes.__RESERVED__) {
if (matchesSolidState(block, state)) {
return y2 + offset;
}
}
if (clearanceAbove != clearanceBelow) {
if (clearanceAbove < clearanceBelow) {
for (int layer = y - clearance - 1; layer >= minY; layer--) {
block = getBlock(x, layer, z);
if (block.getMaterial().isMovementBlocker() == state && block.getBlockType() != BlockTypes.__RESERVED__) {
if (matchesSolidState(block, state)) {
return layer + offset;
}
}
} else {
for (int layer = y + clearance + 1; layer <= maxY; layer++) {
block = getBlock(x, layer, z);
if (block.getMaterial().isMovementBlocker() == state && block.getBlockType() != BlockTypes.__RESERVED__) {
if (matchesSolidState(block, state)) {
return layer - offset;
}
}
Expand All @@ -495,6 +496,10 @@ default int getNearestSurfaceTerrainBlock(
return result;
}

private static boolean matchesSolidState(BlockState block, boolean state) {
return SolidBlockMask.isSolid(block) == state && block.getBlockType() != BlockTypes.__RESERVED__;
}

default void addCaves(Region region) throws WorldEditException {
generate(region, new CavesGen(8));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,48 @@
package com.sk89q.worldedit.function.mask;

import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockTypesCache;

import javax.annotation.Nullable;
public class SolidBlockMask extends AbstractExtentMask {
// FAWE start - precompute solid blocks
private static final boolean[] SOLID = initialize();

public class SolidBlockMask extends BlockMask {
private static boolean[] initialize() {
final boolean[] solid = new boolean[BlockTypesCache.states.length];
for (int i = 0; i < solid.length; i++) {
solid[i] = BlockTypesCache.states[i].getBlockType().getMaterial().isMovementBlocker();
}
return solid;
}
// FAWE end

public SolidBlockMask(Extent extent) {
super(extent);
add(state -> state.getMaterial().isMovementBlocker());
}

@Nullable
// FAWE start
@Override
public boolean test(final Extent extent, final BlockVector3 position) {
final int ordinal = position.getOrdinal(extent);
return SOLID[ordinal];
}

@Override
public Mask2D toMask2D() {
return null;
public boolean test(final BlockVector3 vector) {
return test(getExtent(), vector);
}

//FAWE start
/**
* {@return whether the given block state is considered solid by this mask}
* @since TODO
*/
public static boolean isSolid(BlockState blockState) {
return SOLID[blockState.getOrdinal()];
}


@Override
public Mask copy() {
return new SolidBlockMask(getExtent());
Expand Down
Loading