Skip to content

Commit

Permalink
Config support for old collisions from 92faa92… …156bc30a (1.13.2-0.2…
Browse files Browse the repository at this point in the history
….9-pre11)
  • Loading branch information
Cadiboo committed Feb 9, 2024
1 parent 0936904 commit f02be98
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@
*/
public final class CollisionHandler {

static final int OLD_COLLISIONS_CORNERS = 1;
static final int OLD_COLLISIONS_EDGES = OLD_COLLISIONS_CORNERS + 1;
static final int OLD_COLLISIONS_CORNER_EDGES_INTERP = OLD_COLLISIONS_EDGES + 1;
static final int OLD_COLLISIONS_CENTER_CORNER_INTERP = OLD_COLLISIONS_CORNER_EDGES_INTERP + 1;
public static final int OLD_COLLISIONS_ENHANCEMENT_LEVEL_MAX = OLD_COLLISIONS_CENTER_CORNER_INTERP;

public static VoxelShape getCollisionShape(BlockState state, BlockGetter reader, BlockPos blockPos, CollisionContext context) {
var canCollide = ((INoCubesBlockType) state.getBlock()).noCubes$hasCollision();
try {
Expand Down Expand Up @@ -194,16 +200,32 @@ public static void generate(Area area, Mesher mesher, ShapeConsumer consumer) {
}

public static boolean generateShapes(Vec centre, Vec faceNormal, ShapeConsumer consumer, Face face) {
generateShapesOld(consumer, face, centre, faceNormal, null, 0.125F);
// if (!CollisionHandler.generateShape(centre, faceNormal, consumer, face.v0))
// return false;
// if (!CollisionHandler.generateShape(centre, faceNormal, consumer, face.v1))
// return false;
// if (!CollisionHandler.generateShape(centre, faceNormal, consumer, face.v2))
// return false;
// if (!CollisionHandler.generateShape(centre, faceNormal, consumer, face.v3))
// return false;
return true;
var mut = faceNormal;
var mutOldX = mut.x;
var mutOldY = mut.y;
var mutOldZ = mut.z;
try {
if (!generateShapesOld(consumer, face, centre, mut, Double.MAX_VALUE, 0.125F, NoCubesConfig.Server.oldStyleCollisionsEnhancementLevel))
return false;
} finally {
mut.set(
mutOldX,
mutOldY,
mutOldZ
);
}

if (!NoCubesConfig.Server.onlyOldStyleCollisions) {
if (!CollisionHandler.generateShape(centre, faceNormal, consumer, face.v0))
return false;
if (!CollisionHandler.generateShape(centre, faceNormal, consumer, face.v1))
return false;
if (!CollisionHandler.generateShape(centre, faceNormal, consumer, face.v2))
return false;
if (!CollisionHandler.generateShape(centre, faceNormal, consumer, face.v3))
return false;
}
return true;
}

private static boolean generateShape(Vec centre, Vec faceNormal, ShapeConsumer consumer, Vec v) {
Expand All @@ -219,11 +241,7 @@ private static boolean generateShape(Vec centre, Vec faceNormal, ShapeConsumer c
);
}

private static void generateShapesOld(ShapeConsumer consumer, Face face, Vec centre, Vec mut, VoxelShape originalBoxOffset, float boxRadius) {
var mutOldX = mut.x;
var mutOldY = mut.y;
var mutOldZ = mut.z;

private static boolean generateShapesOld(ShapeConsumer consumer, Face face, Vec centre, Vec mut, double maxY, float boxRadius, int level) {
var v0 = face.v0;
var v1 = face.v1;
var v2 = face.v2;
Expand All @@ -234,59 +252,63 @@ private static void generateShapesOld(ShapeConsumer consumer, Face face, Vec cen
//__c__
//_____
//1___2
if (!acceptVoxelShapeForVertex(consumer, v0, boxRadius, originalBoxOffset) ||
!acceptVoxelShapeForVertex(consumer, v1, boxRadius, originalBoxOffset) ||
!acceptVoxelShapeForVertex(consumer, v2, boxRadius, originalBoxOffset) ||
!acceptVoxelShapeForVertex(consumer, v3, boxRadius, originalBoxOffset) ||
!acceptVoxelShapeForVertex(consumer, centre, boxRadius, originalBoxOffset)
if (level < OLD_COLLISIONS_CORNERS)
return true;
if (!acceptVoxelShapeForVertex(consumer, v0, boxRadius, maxY) ||
!acceptVoxelShapeForVertex(consumer, v1, boxRadius, maxY) ||
!acceptVoxelShapeForVertex(consumer, v2, boxRadius, maxY) ||
!acceptVoxelShapeForVertex(consumer, v3, boxRadius, maxY) ||
!acceptVoxelShapeForVertex(consumer, centre, boxRadius, maxY)
)
return;

// //0_*_3
// //_____
// //*_c_*
// //_____
// //1_*_2
// if (!acceptVoxelShapeForVertex(consumer, interp(v0, v1, 0.5F, mut), boxRadius, originalBoxOffset) ||
// !acceptVoxelShapeForVertex(consumer, interp(v1, v2, 0.5F, mut), boxRadius, originalBoxOffset) ||
// !acceptVoxelShapeForVertex(consumer, interp(v2, v3, 0.5F, mut), boxRadius, originalBoxOffset) ||
// !acceptVoxelShapeForVertex(consumer, interp(v3, v0, 0.5F, mut), boxRadius, originalBoxOffset)
// )
// return;

// //0x*x3
// //x___x
// //*_c_*
// //x___x
// //1x*x2
// if (!acceptVoxelShapeForVertex(consumer, interp(v0, v1, 0.25F, mut), boxRadius, originalBoxOffset) ||
// !acceptVoxelShapeForVertex(consumer, interp(v0, v1, 0.75F, mut), boxRadius, originalBoxOffset) ||
// !acceptVoxelShapeForVertex(consumer, interp(v1, v2, 0.25F, mut), boxRadius, originalBoxOffset) ||
// !acceptVoxelShapeForVertex(consumer, interp(v1, v2, 0.75F, mut), boxRadius, originalBoxOffset) ||
// !acceptVoxelShapeForVertex(consumer, interp(v2, v3, 0.25F, mut), boxRadius, originalBoxOffset) ||
// !acceptVoxelShapeForVertex(consumer, interp(v2, v3, 0.75F, mut), boxRadius, originalBoxOffset) ||
// !acceptVoxelShapeForVertex(consumer, interp(v3, v0, 0.25F, mut), boxRadius, originalBoxOffset) ||
// !acceptVoxelShapeForVertex(consumer, interp(v3, v0, 0.75F, mut), boxRadius, originalBoxOffset)
// )
// return;

// //0x*x3
// //x†_†x
// //*_c_*
// //x†_†x
// //1x*x2
// if (!acceptVoxelShapeForVertex(consumer, interp(v0, centre, 0.5F, mut), boxRadius, originalBoxOffset) ||
// !acceptVoxelShapeForVertex(consumer, interp(v1, centre, 0.5F, mut), boxRadius, originalBoxOffset) ||
// !acceptVoxelShapeForVertex(consumer, interp(v2, centre, 0.5F, mut), boxRadius, originalBoxOffset) ||
// !acceptVoxelShapeForVertex(consumer, interp(v3, centre, 0.5F, mut), boxRadius, originalBoxOffset)
// )
// return;

mut.set(
mutOldX,
mutOldY,
mutOldZ
);
return false;

//0_*_3
//_____
//*_c_*
//_____
//1_*_2
if (level < OLD_COLLISIONS_EDGES)
return true;
if (!acceptVoxelShapeForVertex(consumer, interp(v0, v1, 0.5F, mut), boxRadius, maxY) ||
!acceptVoxelShapeForVertex(consumer, interp(v1, v2, 0.5F, mut), boxRadius, maxY) ||
!acceptVoxelShapeForVertex(consumer, interp(v2, v3, 0.5F, mut), boxRadius, maxY) ||
!acceptVoxelShapeForVertex(consumer, interp(v3, v0, 0.5F, mut), boxRadius, maxY)
)
return false;

//0x*x3
//x___x
//*_c_*
//x___x
//1x*x2
if (level < OLD_COLLISIONS_CORNER_EDGES_INTERP)
return true;
if (!acceptVoxelShapeForVertex(consumer, interp(v0, v1, 0.25F, mut), boxRadius, maxY) ||
!acceptVoxelShapeForVertex(consumer, interp(v0, v1, 0.75F, mut), boxRadius, maxY) ||
!acceptVoxelShapeForVertex(consumer, interp(v1, v2, 0.25F, mut), boxRadius, maxY) ||
!acceptVoxelShapeForVertex(consumer, interp(v1, v2, 0.75F, mut), boxRadius, maxY) ||
!acceptVoxelShapeForVertex(consumer, interp(v2, v3, 0.25F, mut), boxRadius, maxY) ||
!acceptVoxelShapeForVertex(consumer, interp(v2, v3, 0.75F, mut), boxRadius, maxY) ||
!acceptVoxelShapeForVertex(consumer, interp(v3, v0, 0.25F, mut), boxRadius, maxY) ||
!acceptVoxelShapeForVertex(consumer, interp(v3, v0, 0.75F, mut), boxRadius, maxY)
)
return false;

//0x*x3
//x†_†x
//*_c_*
//x†_†x
//1x*x2
if (level < OLD_COLLISIONS_CENTER_CORNER_INTERP)
return true;
if (!acceptVoxelShapeForVertex(consumer, interp(v0, centre, 0.5F, mut), boxRadius, maxY) ||
!acceptVoxelShapeForVertex(consumer, interp(v1, centre, 0.5F, mut), boxRadius, maxY) ||
!acceptVoxelShapeForVertex(consumer, interp(v2, centre, 0.5F, mut), boxRadius, maxY) ||
!acceptVoxelShapeForVertex(consumer, interp(v3, centre, 0.5F, mut), boxRadius, maxY)
)
return false;

return true;
}

private static Vec interp(Vec v0, Vec v1, float t, Vec toUse) {
Expand All @@ -298,22 +320,20 @@ private static Vec interp(Vec v0, Vec v1, float t, Vec toUse) {

}

private static boolean acceptVoxelShapeForVertex(ShapeConsumer consumer, Vec v, float boxRadius, VoxelShape originalBox) {
final double vy = v.y;
final double vx = v.x;
final double vz = v.z;

final boolean originalBoxMaxYGreaterThanVertex = false;//originalBox != null && originalBox.getEnd(EnumFacing.Axis.Y) >= vy;
private static boolean acceptVoxelShapeForVertex(ShapeConsumer consumer, Vec v, float boxRadius, double maxY) {
final double topX = v.x + boxRadius;
final double topY = Math.min(v.y + boxRadius, maxY);
final double topZ = v.z + boxRadius;

return consumer.accept(
//min
vx - boxRadius,
originalBoxMaxYGreaterThanVertex ? vy - boxRadius - boxRadius : vy - boxRadius,
vz - boxRadius,
topX - boxRadius * 2,
topY - boxRadius * 2,
topZ - boxRadius * 2,
//max
vx + boxRadius,
originalBoxMaxYGreaterThanVertex ? vy : vy + boxRadius,
vz + boxRadius
topX,
topY,
topZ
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import io.github.cadiboo.nocubes.NoCubes;
import io.github.cadiboo.nocubes.collision.CollisionHandler;
import io.github.cadiboo.nocubes.mesh.*;
import io.github.cadiboo.nocubes.network.NoCubesNetwork;
import io.github.cadiboo.nocubes.network.S2CUpdateServerConfig;
Expand Down Expand Up @@ -318,6 +319,8 @@ public static class Server {
public static Mesher mesher;
public static boolean collisionsEnabled;
public static boolean tempMobCollisionsDisabled;
public static int oldStyleCollisionsEnhancementLevel;
public static boolean onlyOldStyleCollisions;
public static boolean forceVisuals;
public static int extendFluidsRange;
public static boolean oldNoCubesSlopes;
Expand All @@ -340,6 +343,8 @@ public static void bake(ModConfig config) {
mesher = INSTANCE.mesher.get().instance;
collisionsEnabled = INSTANCE.collisionsEnabled.get();
tempMobCollisionsDisabled = INSTANCE.tempMobCollisionsDisabled.get();
oldStyleCollisionsEnhancementLevel = INSTANCE.oldStyleCollisionsEnhancementLevel.get();
onlyOldStyleCollisions = INSTANCE.onlyOldStyleCollisions.get();
forceVisuals = INSTANCE.forceVisuals.get();
if (forceVisuals)
// Directly setting the baked field - won't cause a NPE on the dedicated server
Expand Down Expand Up @@ -413,6 +418,8 @@ static class Impl {
final EnumValue<MesherType> mesher;
final BooleanValue collisionsEnabled;
final BooleanValue tempMobCollisionsDisabled;
final IntValue oldStyleCollisionsEnhancementLevel;
final BooleanValue onlyOldStyleCollisions;
final BooleanValue forceVisuals;
final IntValue extendFluidsRange;
final BooleanValue oldNoCubesSlopes;
Expand Down Expand Up @@ -452,6 +459,16 @@ private Impl(ForgeConfigSpec.Builder builder) {
.comment("If ONLY players should be able to walk up the smooth slopes generated by NoCubes")
.define("tempMobCollisionsDisabled", false);

oldStyleCollisionsEnhancementLevel = builder
.translation(NoCubes.MOD_ID + ".config.oldStyleCollisionsEnhancementLevel")
.comment("A higher value means more enhancement and worse performance")
.defineInRange("oldStyleCollisionsEnhancementLevel", 0, 0, CollisionHandler.OLD_COLLISIONS_ENHANCEMENT_LEVEL_MAX);

onlyOldStyleCollisions = builder
.translation(NoCubes.MOD_ID + ".config.onlyOldStyleCollisions")
.comment("If ONLY the old-style collision algorithm should be used (only use this in conjunction with 'oldStyleCollisionsEnhancementLevel')")
.define("onlyOldStyleCollisions", false);

mesher = builder
.translation(NoCubes.MOD_ID + ".config.meshGenerator")
.comment("meshGenerator")
Expand Down Expand Up @@ -480,7 +497,7 @@ private Impl(ForgeConfigSpec.Builder builder) {

oldNoCubesInFluids = builder
.translation(NoCubes.MOD_ID + ".config.oldNoCubesInFluids")
.comment("If slopes be generated inside fluids by OldNoCubes")
.comment("If slopes should be generated inside fluids by OldNoCubes")
.define("oldNoCubesInFluids", true);

oldNoCubesRoughness = builder
Expand Down

0 comments on commit f02be98

Please sign in to comment.