Skip to content

Commit

Permalink
Joints!
Browse files Browse the repository at this point in the history
  • Loading branch information
ThePlasticPotato committed Dec 27, 2024
1 parent 93cc755 commit 58c6bfb
Show file tree
Hide file tree
Showing 14 changed files with 157 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.minecraft.core.SectionPos;
import net.minecraft.network.protocol.game.ClientboundSectionBlocksUpdatePacket;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(ClientboundSectionBlocksUpdatePacket.class)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.valkyrienskies.mod.mixin.feature.debug_sparse_voxel_rendering;

import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.debug.DebugRenderer;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.valkyrienskies.mod.client.SparseVoxelRenderer;

@Mixin(DebugRenderer.class)
public class MixinDebugRenderer {
@Unique
@Final
SparseVoxelRenderer sparseVoxelRenderer = new SparseVoxelRenderer();

@Inject(method = "render", at = @At("HEAD"))
void render(PoseStack poseStack, MultiBufferSource.BufferSource bufferSource, double d, double e, double f, final CallbackInfo ci) {
//sparseVoxelRenderer.render(poseStack, bufferSource, d, e, f);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.valkyrienskies.mod.mixin.feature.sealed_air_sync;

public class MixinBlockUpdatePacket {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.valkyrienskies.mod.mixin.feature.sealed_air_sync;

import net.minecraft.network.protocol.game.ClientboundSectionBlocksUpdatePacket;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;

@Mixin(ClientboundSectionBlocksUpdatePacket.class)
public class MixinSectionBlocksUpdatePacket {
@Unique
private boolean[] sealed;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.valkyrienskies.mod.mixinducks.world;

import net.minecraft.core.BlockPos;

public interface WithSealedPositions {
public void setSealed(BlockPos pos, boolean sealed);

public boolean[] getSealed();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.valkyrienskies.mod.client

import com.mojang.blaze3d.vertex.PoseStack
import com.mojang.blaze3d.vertex.Tesselator
import net.minecraft.client.Minecraft
import net.minecraft.client.renderer.MultiBufferSource.BufferSource
import net.minecraft.client.renderer.RenderType
import net.minecraft.client.renderer.debug.DebugRenderer
import net.minecraft.world.phys.AABB
import org.valkyrienskies.core.util.datastructures.SparseVoxelPosition

class SparseVoxelRenderer() {
val voxels = HashSet<SparseVoxelPosition>()

init {
voxels.add(SparseVoxelPosition(0, 128, 0, 2))
}

fun render(ms: PoseStack, buffer: BufferSource, camX: Double, camY: Double, camZ: Double) {
for (voxel in voxels) {
drawVoxel(voxel, ms, buffer, camX, camY, camZ)
}
}

fun drawVoxel(voxel: SparseVoxelPosition, poseStack: PoseStack, buffer: BufferSource, camX: Double, camY: Double, camZ: Double) {
poseStack.pushPose()

// Draw the voxel
val random = Minecraft.getInstance().level?.random ?: return
DebugRenderer.renderFilledBox(voxel.toAABB(-camX, -camY, -camZ), 1.0f, 1.0f, 0.5f, 0.5f)
//Tesselator.getInstance().end()

poseStack.popPose()
}

private fun SparseVoxelPosition.toAABB(offsetX: Double = 0.0, offsetY: Double = 0.0, offsetZ: Double = 0.0): AABB {
return AABB(x.toDouble() + offsetX, y.toDouble() + offsetY, z.toDouble() + offsetZ,
x.toDouble() + extent.toDouble() + offsetX, y.toDouble() + extent.toDouble() + offsetY, z.toDouble() + extent.toDouble() + offsetZ)

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,16 @@ fun createNewShipWithStructure(
blocks.placeInWorld(level, BlockPos(lowerCornerInShip.toMinecraft()), BlockPos(lowerCornerInShip.toMinecraft()), StructurePlaceSettings(), level.random, Block.UPDATE_ALL)

val diff = higherCorner.subtract(lowerCorner)
val centerPos = lowerCorner.offset(diff.x / 2, diff.y / 2, diff.z / 2)
val centerPos = lowerCorner.toJOMLD().add(diff.x / 2.0, diff.y / 2.0, diff.z / 2.0)

// The ship's position has shifted from the center block since we assembled the ship, compensate for that
val centerBlockPosInWorld = ship.inertiaData.centerOfMassInShip.sub(lowerCornerInShip, Vector3d())
val centerBlockPosInWorld = ship.inertiaData.centerOfMassInShip.sub(centerPos, Vector3d())
.add(ship.transform.positionInWorld)
// Put the ship into the compensated position, so that all the assembled blocks stay in the same place
// TODO: AAAAAAAAA THIS IS HORRIBLE how can the API support this?
//(ship as ShipData).transform = (ship.transform as ShipTransformImpl).copy(positionInWorld = centerBlockPosInWorld)
level.shipObjectWorld
.teleportShip(ship, ShipTeleportDataImpl(newPos = centerBlockPosInWorld.add(0.0, (128.0 - lowerCorner.y.toDouble() + 0.5), 0.0, Vector3d()), newPosInShip = ship.inertiaData.centerOfMassInShip))
.teleportShip(ship, ShipTeleportDataImpl(newPos = centerBlockPosInWorld.add(0.0, 0.0, 0.0, Vector3d()), newPosInShip = ship.inertiaData.centerOfMassInShip))


for (x in lowerCorner.x..higherCorner.x) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ import org.joml.Quaterniond
import org.joml.Quaterniondc
import org.joml.Vector3d
import org.joml.Vector3dc
import org.valkyrienskies.core.apigame.constraints.VSAttachmentConstraint
import org.valkyrienskies.core.apigame.constraints.VSHingeOrientationConstraint
import org.valkyrienskies.core.apigame.joints.VSJointMaxForceTorque
import org.valkyrienskies.core.apigame.joints.VSJointPose
import org.valkyrienskies.core.apigame.joints.VSRevoluteJoint
import org.valkyrienskies.core.impl.game.ships.ShipDataCommon
import org.valkyrienskies.core.impl.game.ships.ShipTransformImpl
import org.valkyrienskies.mod.common.ValkyrienSkiesMod
Expand Down Expand Up @@ -200,18 +201,18 @@ object TestHingeBlock :
val shipId1 = ship.id

// Attachment constraint
run {
// I don't recommend setting compliance lower than 1e-10 because it tends to cause instability
// TODO: Investigate why small compliance cause instability
val attachmentCompliance = 1e-10
val attachmentMaxForce = 1e10
val attachmentFixedDistance = 0.0
val attachmentConstraint = VSAttachmentConstraint(
shipId0, shipId1, attachmentCompliance, attachmentLocalPos0, attachmentLocalPos1,
attachmentMaxForce, attachmentFixedDistance
)
blockEntity.get().constraintId = level.shipObjectWorld.createNewConstraint(attachmentConstraint)
}
// run {
// // I don't recommend setting compliance lower than 1e-10 because it tends to cause instability
// // TODO: Investigate why small compliance cause instability
// val attachmentCompliance = 1e-10
// val attachmentMaxForce = 1e10
// val attachmentFixedDistance = 0.0
// val attachmentConstraint = VSRevoluteJoint(
// shipId0, shipId1, attachmentCompliance, attachmentLocalPos0, attachmentLocalPos1,
// attachmentMaxForce, attachmentFixedDistance
// )
// blockEntity.get().constraintId = level.shipObjectWorld.createNewConstraint(attachmentConstraint)
// }

// Hinge constraints will attempt to align the X-axes of both bodies, so to align the Y axis we
// apply this rotation to the X-axis
Expand All @@ -221,9 +222,11 @@ object TestHingeBlock :
run {
// I don't recommend setting compliance lower than 1e-10 because it tends to cause instability
val hingeOrientationCompliance = 1e-10
val attachmentMaxForce = 1e10
val hingeMaxTorque = 1e10
val hingeConstraint = VSHingeOrientationConstraint(
shipId0, shipId1, hingeOrientationCompliance, hingeOrientation, hingeOrientation, hingeMaxTorque
val hingeConstraint = VSRevoluteJoint(
shipId0, VSJointPose(attachmentLocalPos0, hingeOrientation), shipId1, VSJointPose(attachmentLocalPos1, hingeOrientation),
VSJointMaxForceTorque(attachmentMaxForce.toFloat(), hingeMaxTorque.toFloat())
)
blockEntity.get().constraintId = level.shipObjectWorld.createNewConstraint(hingeConstraint)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package org.valkyrienskies.mod.common.blockentity
import net.minecraft.core.BlockPos
import net.minecraft.world.level.block.entity.BlockEntity
import net.minecraft.world.level.block.state.BlockState
import org.valkyrienskies.core.apigame.constraints.VSConstraintId
import org.valkyrienskies.core.apigame.joints.VSJointId
import org.valkyrienskies.mod.common.ValkyrienSkiesMod

class TestHingeBlockEntity(blockPos: BlockPos, blockState: BlockState) : BlockEntity(
ValkyrienSkiesMod.TEST_HINGE_BLOCK_ENTITY_TYPE, blockPos, blockState
) {
var otherHingePos: BlockPos? = null
var constraintId: VSConstraintId? = null
var constraintId: VSJointId? = null

fun tick() {
// println("Amogus")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import net.minecraft.world.item.Item
import net.minecraft.world.item.ItemStack
import net.minecraft.world.item.context.UseOnContext
import net.minecraft.world.level.block.state.BlockState
import org.valkyrienskies.core.impl.config_impl.VSCoreConfig
import org.valkyrienskies.mod.common.ValkyrienSkiesMod
import org.valkyrienskies.mod.common.dimensionId
import org.valkyrienskies.mod.common.getShipManagingPos
import org.valkyrienskies.mod.common.shipObjectWorld
Expand Down Expand Up @@ -36,9 +38,11 @@ class ConnectionCheckerItem(
if (!blockState.isAir) {
// Make a ship
val dimensionId = level.dimensionId

if (parentShip != null) {
println("tested")
if (parentShip != null || ValkyrienSkiesMod.vsCore.hooks.enableWorldConnectivity) {
println("can we get much")
if (item.tag != null && item.tag!!.contains("firstPosX")) {
println("so high")
val firstPosX = item.tag!!.getInt("firstPosX")
val firstPosY = item.tag!!.getInt("firstPosY")
val firstPosZ = item.tag!!.getInt("firstPosZ")
Expand All @@ -48,6 +52,7 @@ class ConnectionCheckerItem(
item.tag!!.remove("firstPosY")
item.tag!!.remove("firstPosZ")
} else {
println("higher")
item.tag = item.orCreateTag.apply {
putInt("firstPosX", blockPos.x)
putInt("firstPosY", blockPos.y)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ import net.minecraft.world.item.context.UseOnContext
import org.joml.Quaterniond
import org.joml.Vector3d
import org.joml.Vector3dc
import org.valkyrienskies.core.apigame.constraints.VSAttachmentConstraint
import org.valkyrienskies.core.apigame.constraints.VSPosDampingConstraint
import org.valkyrienskies.core.apigame.constraints.VSRotDampingAxes
import org.valkyrienskies.core.apigame.constraints.VSRotDampingConstraint
import org.valkyrienskies.core.impl.game.ships.ShipTransformImpl.Companion
import org.valkyrienskies.mod.common.ValkyrienSkiesMod
import org.valkyrienskies.mod.common.dimensionId
Expand Down Expand Up @@ -61,23 +57,23 @@ class PhysicsEntityCreatorItem(
val attachCompliance = 1e-8
val attachMaxForce = 1e10
// Attach the click position of the ship to the surface of the physics entity
val attachConstraint = VSAttachmentConstraint(
shipOn.id, physicsEntityData.shipId, attachCompliance, ctx.clickLocation.toJOML(), offsetInGlobal.mul(-1.0, Vector3d()),
attachMaxForce, 0.0
)
val posDampingConstraint = VSPosDampingConstraint(
shipOn.id, physicsEntityData.shipId, attachCompliance, ctx.clickLocation.toJOML(), offsetInGlobal.mul(-1.0, Vector3d()),
attachMaxForce, 0.1
)
val rotDampingConstraint = VSRotDampingConstraint(
shipOn.id, physicsEntityData.shipId, attachCompliance, shipOn.transform.shipToWorldRotation.invert(
Quaterniond()
),
Quaterniond(), 1e10, 0.1, VSRotDampingAxes.ALL_AXES
)
level.shipObjectWorld.createNewConstraint(attachConstraint)
level.shipObjectWorld.createNewConstraint(posDampingConstraint)
level.shipObjectWorld.createNewConstraint(rotDampingConstraint)
// val attachConstraint = VSAttachmentConstraint(
// shipOn.id, physicsEntityData.shipId, attachCompliance, ctx.clickLocation.toJOML(), offsetInGlobal.mul(-1.0, Vector3d()),
// attachMaxForce, 0.0
// )
// val posDampingConstraint = VSPosDampingConstraint(
// shipOn.id, physicsEntityData.shipId, attachCompliance, ctx.clickLocation.toJOML(), offsetInGlobal.mul(-1.0, Vector3d()),
// attachMaxForce, 0.1
// )
// val rotDampingConstraint = VSRotDampingConstraint(
// shipOn.id, physicsEntityData.shipId, attachCompliance, shipOn.transform.shipToWorldRotation.invert(
// Quaterniond()
// ),
// Quaterniond(), 1e10, 0.1, VSRotDampingAxes.ALL_AXES
// )
// level.shipObjectWorld.createNewConstraint(attachConstraint)
// level.shipObjectWorld.createNewConstraint(posDampingConstraint)
// level.shipObjectWorld.createNewConstraint(rotDampingConstraint)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import org.valkyrienskies.core.api.world.connectivity.ConnectionStatus.CONNECTED
import org.valkyrienskies.core.api.world.connectivity.ConnectionStatus.DISCONNECTED
import org.valkyrienskies.core.util.datastructures.DenseBlockPosSet
import org.valkyrienskies.core.util.expand
import org.valkyrienskies.mod.common.ValkyrienSkiesMod
import org.valkyrienskies.mod.common.assembly.createNewShipWithBlocks
import org.valkyrienskies.mod.common.dimensionId
import org.valkyrienskies.mod.common.getShipObjectManagingPos
Expand All @@ -22,16 +23,16 @@ class SplitHandler(private val doEdges: Boolean, private val doCorners: Boolean)
fun split(level: Level, x: Int, y: Int, z: Int, prevBlockState: BlockState, newBlockState: BlockState) {
if (level is ServerLevel) {
val loadedShip = level.getShipObjectManagingPos(x shr 4, z shr 4)
if (loadedShip != null && loadedShip.getAttachment<SplittingDisablerAttachment>()?.canSplit() != false) {
if ((loadedShip != null && loadedShip.getAttachment<SplittingDisablerAttachment>()?.canSplit() != false)) {
if (!prevBlockState.isAir && newBlockState.isAir) {
val blockNeighbors: HashSet<BlockPos> = HashSet()

val shipBox = loadedShip.shipAABB?.expand(1, AABBi()) ?: return
//val shipBox = loadedShip.shipAABB?.expand(1, AABBi()) ?: return

for (neighborOffset in getOffsets(doEdges, doCorners)) {
val neighborPos = BlockPos(x + neighborOffset.x, y + neighborOffset.y, z + neighborOffset.z)
val neighborState = level.getBlockState(neighborPos)
if (!neighborState.isAir && neighborPos != BlockPos(x, y, z) && shipBox.containsPoint(neighborPos.toJOML())) {
if (!neighborState.isAir && neighborPos != BlockPos(x, y, z)) {
blockNeighbors.add(neighborPos)
}
}
Expand Down Expand Up @@ -85,6 +86,11 @@ class SplitHandler(private val doEdges: Boolean, private val doCorners: Boolean)
toIgnore.add(component)
}
}
if (level.shipObjectWorld.isIsolatedSolid(otherComponent.x, otherComponent.y, otherComponent.z, level.dimensionId) == CONNECTED) {
if (!toIgnore.contains(otherComponent) && !toIgnore.contains(component)) {
toIgnore.add(component)
}
}
}
}

Expand All @@ -93,7 +99,7 @@ class SplitHandler(private val doEdges: Boolean, private val doCorners: Boolean)
if (disconnected.isEmpty()) {
return
} else {
loadedShip.getAttachment(SplittingDisablerAttachment::class.java)?.disableSplitting()
loadedShip?.getAttachment(SplittingDisablerAttachment::class.java)?.disableSplitting()
}

//begin the DFSing
Expand Down Expand Up @@ -126,7 +132,7 @@ class SplitHandler(private val doEdges: Boolean, private val doCorners: Boolean)
}

if (toAssemble.isEmpty()) {
loadedShip.getAttachment(SplittingDisablerAttachment::class.java)?.enableSplitting()
loadedShip?.getAttachment(SplittingDisablerAttachment::class.java)?.enableSplitting()
return
}

Expand All @@ -135,7 +141,7 @@ class SplitHandler(private val doEdges: Boolean, private val doCorners: Boolean)
createNewShipWithBlocks(component.first().toBlockPos(), component, level)
}

loadedShip.getAttachment(SplittingDisablerAttachment::class.java)?.enableSplitting()
loadedShip?.getAttachment(SplittingDisablerAttachment::class.java)?.enableSplitting()
}
}
}
Expand Down
Loading

0 comments on commit 58c6bfb

Please sign in to comment.