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

New VS Core #414

Merged
merged 6 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 2 additions & 2 deletions common/src/main/kotlin/org/valkyrienskies/eureka/EurekaMod.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.valkyrienskies.eureka

import org.valkyrienskies.core.impl.config.VSConfigClass
import org.valkyrienskies.mod.common.ValkyrienSkiesMod

object EurekaMod {
const val MOD_ID = "vs_eureka"
Expand All @@ -13,7 +13,7 @@ object EurekaMod {
EurekaScreens.register()
EurekaEntities.register()
EurekaWeights.register()
VSConfigClass.registerConfig("vs_eureka", EurekaConfig::class.java)
ValkyrienSkiesMod.vsCore.registerConfigLegacy("vs_eureka", EurekaConfig::class.java)
}

@JvmStatic
Expand Down
11 changes: 0 additions & 11 deletions common/src/main/kotlin/org/valkyrienskies/eureka/EurekaWeights.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,10 @@ import net.minecraft.world.level.block.state.properties.BlockStateProperties
import org.valkyrienskies.core.apigame.world.chunks.BlockType
import org.valkyrienskies.mod.common.BlockStateInfo
import org.valkyrienskies.mod.common.BlockStateInfoProvider
import org.valkyrienskies.physics_api.Lod1BlockStateId
import org.valkyrienskies.physics_api.Lod1LiquidBlockStateId
import org.valkyrienskies.physics_api.Lod1SolidBlockStateId
import org.valkyrienskies.physics_api.voxel.Lod1LiquidBlockState
import org.valkyrienskies.physics_api.voxel.Lod1SolidBlockState

object EurekaWeights : BlockStateInfoProvider {
override val blockStateData: List<Triple<Lod1SolidBlockStateId, Lod1LiquidBlockStateId, Lod1BlockStateId>>
get() = emptyList()
override val liquidBlockStates: List<Lod1LiquidBlockState>
get() = emptyList()
override val priority: Int
get() = 200
override val solidBlockStates: List<Lod1SolidBlockState>
get() = emptyList()

override fun getBlockStateMass(blockState: BlockState): Double? {
if (blockState.block == EurekaBlocks.BALLAST.get()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import net.minecraft.world.damagesource.DamageSource
import net.minecraft.world.entity.Entity
import net.minecraft.world.entity.projectile.Projectile
import net.minecraft.world.level.Level
import net.minecraft.world.level.LevelAccessor
import net.minecraft.world.level.block.Block
import net.minecraft.world.level.block.state.BlockState
import net.minecraft.world.phys.BlockHitResult
Expand Down Expand Up @@ -48,7 +47,7 @@ class BalloonBlock(properties: Properties) : Block(properties) {
if (level.isClientSide) return

level.destroyBlock(hit.blockPos, false)
Direction.values().forEach {
Direction.entries.forEach {
val neighbor = hit.blockPos.relative(it)
if (level.getBlockState(neighbor).block == this &&
level.random.nextFloat() < EurekaConfig.SERVER.popSideBalloonChance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import org.joml.Vector3d
import org.joml.Vector3dc
import org.valkyrienskies.core.api.ships.ServerShip
import org.valkyrienskies.core.api.ships.getAttachment
import org.valkyrienskies.core.impl.util.logger
import org.valkyrienskies.eureka.EurekaBlockEntities
import org.valkyrienskies.eureka.EurekaConfig
import org.valkyrienskies.eureka.EurekaMod
Expand All @@ -39,6 +38,7 @@ import org.valkyrienskies.mod.common.entity.ShipMountingEntity
import org.valkyrienskies.mod.common.getShipObjectManagingPos
import org.valkyrienskies.mod.common.util.toDoubles
import org.valkyrienskies.mod.common.util.toJOMLD
import org.valkyrienskies.mod.util.logger

var ASSEMBLE_BLACKLIST: TagKey<Block> =
TagKey.create(Registry.BLOCK_REGISTRY, ResourceLocation(EurekaMod.MOD_ID, "assemble_blacklist"))
Expand Down Expand Up @@ -129,17 +129,26 @@ class ShipHelmBlockEntity(pos: BlockPos, state: BlockState) :
val blockState = level.getBlockState(blockPos)
if (blockState.block !is ShipHelmBlock) return

var helmCount = 0
val builtShip = ShipAssembler.collectBlocks(
level,
blockPos
) { !it.isAir && !it.`is`(ASSEMBLE_BLACKLIST) &&
) {
val allowed = !it.isAir && !it.`is`(ASSEMBLE_BLACKLIST) &&
// TODO: Remove blockBlacklist
!(EurekaConfig.SERVER.blockBlacklist.isNotEmpty() && EurekaConfig.SERVER.blockBlacklist.contains(Registry.BLOCK.getKey(it.block).toString()))
// This isn't the best way to count helms, but it'll work I promise!
if (allowed && it.block is ShipHelmBlock) {
helmCount++
}
return@collectBlocks allowed
}

if (builtShip == null) {
player.sendMessage(TextComponent("Ship is too big! Max size is ${EurekaConfig.SERVER.maxShipBlocks} blocks (changeable in the config)"), Util.NIL_UUID)
logger.warn("Failed to assemble ship for ${player.name.string}")
} else {
EurekaShipControl.getOrCreate(builtShip).helms = helmCount
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import org.valkyrienskies.core.api.ships.ServerTickListener
import org.valkyrienskies.core.api.ships.ShipForcesInducer
import org.valkyrienskies.core.api.ships.getAttachment
import org.valkyrienskies.core.api.ships.saveAttachment
import org.valkyrienskies.core.impl.game.ships.PhysShipImpl
import org.valkyrienskies.eureka.EurekaConfig
import org.valkyrienskies.mod.api.SeatedControllingPlayer
import org.valkyrienskies.mod.common.util.toJOMLD
Expand Down Expand Up @@ -92,13 +91,11 @@ class EurekaShipControl : ShipForcesInducer, ServerTickListener {
// Disable fluid drag when helms are present, because it makes ships hard to drive
physShip.doFluidDrag = EurekaConfig.SERVER.doFluidDrag

physShip as PhysShipImpl

val ship = ship ?: return
val mass = physShip.inertia.shipMass
val moiTensor = physShip.inertia.momentOfInertiaTensor
val omega: Vector3dc = physShip.poseVel.omega
val vel: Vector3dc = physShip.poseVel.vel
val mass = physShip.mass
val moiTensor = physShip.momentOfInertia
val omega: Vector3dc = physShip.omega
val vel: Vector3dc = physShip.velocity
val balloonForceProvided = balloons * forcePerBalloon

val buoyantFactorPerFloater = min(
Expand All @@ -123,7 +120,7 @@ class EurekaShipControl : ShipForcesInducer, ServerTickListener {

// region Aligning

val invRotation = physShip.poseVel.rot.invert(Quaterniond())
val invRotation = physShip.transform.shipToWorldRotation.invert(Quaterniond())
val invRotationAxisAngle = AxisAngle4d(invRotation)
// Floor makes a number 0 to 3, which corresponds to direction
val alignTarget = floor((invRotationAxisAngle.angle / (PI * 0.5)) + 4.5).toInt() % 4
Expand Down Expand Up @@ -233,16 +230,16 @@ class EurekaShipControl : ShipForcesInducer, ServerTickListener {
return currentControlData
}

private fun applyPlayerControl(control: ControlData, physShip: PhysShipImpl) {
private fun applyPlayerControl(control: ControlData, physShip: PhysShip) {

val ship = ship ?: return
val transform = physShip.transform
val aabb = ship.worldAABB
val center = transform.positionInWorld

// region Player controlled rotation
val moiTensor = physShip.inertia.momentOfInertiaTensor
val omega: Vector3dc = physShip.poseVel.omega
val moiTensor = physShip.momentOfInertia
val omega: Vector3dc = physShip.omega

val largestDistance = run {
var dist = center.distance(aabb.minX(), center.y(), aabb.minZ())
Expand Down Expand Up @@ -277,42 +274,42 @@ class EurekaShipControl : ShipForcesInducer, ServerTickListener {
physShip.applyInvariantForce(getPlayerForwardVel(control, physShip))
}

private fun getPlayerControlledBanking(control: ControlData, physShip: PhysShipImpl, moiTensor: Matrix3dc, strength: Double): Vector3d {
private fun getPlayerControlledBanking(control: ControlData, physShip: PhysShip, moiTensor: Matrix3dc, strength: Double): Vector3d {
val rotationVector = control.seatInDirection.normal.toJOMLD()
physShip.poseVel.transformDirection(rotationVector)
physShip.transform.shipToWorldRotation.transform(rotationVector)
rotationVector.y = 0.0
rotationVector.mul(strength * 1.5)

physShip.poseVel.rot.transform(
physShip.transform.shipToWorldRotation.transform(
moiTensor.transform(
physShip.poseVel.rot.transformInverse(rotationVector)
physShip.transform.shipToWorldRotation.transformInverse(rotationVector)
)
)

return rotationVector
}

// Player controlled forward and backward thrust
private fun getPlayerForwardVel(control: ControlData, physShip: PhysShipImpl): Vector3d {
private fun getPlayerForwardVel(control: ControlData, physShip: PhysShip): Vector3d {

val scaledMass = physShip.inertia.shipMass * EurekaConfig.SERVER.speedMassScale
val vel: Vector3dc = physShip.poseVel.vel
val scaledMass = physShip.mass * EurekaConfig.SERVER.speedMassScale
val vel: Vector3dc = physShip.velocity

// region Player controlled forward and backward thrust
val forwardVector = control.seatInDirection.normal.toJOMLD()
physShip.poseVel.rot.transform(forwardVector)
physShip.transform.shipToWorldRotation.transform(forwardVector)
forwardVector.normalize()

val s = 1 / smoothingATanMax(
EurekaConfig.SERVER.linearMaxMass,
physShip.inertia.shipMass * EurekaConfig.SERVER.linearMassScaling + EurekaConfig.SERVER.linearBaseMass
physShip.mass * EurekaConfig.SERVER.linearMassScaling + EurekaConfig.SERVER.linearBaseMass
)

val maxSpeed = EurekaConfig.SERVER.linearMaxSpeed / 15
oldSpeed = max(min(oldSpeed * (1 - s) + control.forwardImpulse.toDouble() * s, maxSpeed), -maxSpeed)
forwardVector.mul(oldSpeed)

val playerUpDirection = physShip.poseVel.transformDirection(Vector3d(0.0, 1.0, 0.0))
val playerUpDirection = physShip.transform.shipToWorldRotation.transform(Vector3d(0.0, 1.0, 0.0))
val velOrthogonalToPlayerUp = vel.sub(playerUpDirection.mul(playerUpDirection.dot(vel)), Vector3d())

// This is the speed that the ship is always allowed to go out, without engines
Expand All @@ -321,8 +318,8 @@ class EurekaShipControl : ShipForcesInducer, ServerTickListener {

if (extraForceLinear != 0.0) {
// engine boost
val boost = max((extraForceLinear - EurekaConfig.SERVER.enginePowerLinear * EurekaConfig.SERVER.engineBoostOffset) * EurekaConfig.SERVER.engineBoost, 0.0);
extraForceLinear += boost + boost * boost * EurekaConfig.SERVER.engineBoostExponentialPower;
val boost = max((extraForceLinear - EurekaConfig.SERVER.enginePowerLinear * EurekaConfig.SERVER.engineBoostOffset) * EurekaConfig.SERVER.engineBoost, 0.0)
extraForceLinear += boost + boost * boost * EurekaConfig.SERVER.engineBoostExponentialPower

// This is the maximum speed we want to go in any scenario (when not sprinting)
val idealForwardVel = Vector3d(forwardVector).mul(EurekaConfig.SERVER.maxCasualSpeed)
Expand Down
19 changes: 8 additions & 11 deletions common/src/main/kotlin/org/valkyrienskies/eureka/ship/Stabilize.kt
Copy link
Collaborator

Choose a reason for hiding this comment

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

this breaks the futures added by #371

Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ package org.valkyrienskies.eureka.ship
import org.joml.Vector3d
import org.joml.Vector3dc
import org.valkyrienskies.core.api.ships.PhysShip
import org.valkyrienskies.core.impl.game.ships.PhysShipImpl
import org.valkyrienskies.eureka.EurekaConfig
import kotlin.math.atan
import kotlin.math.max

fun stabilize(
ship: PhysShipImpl,
ship: PhysShip,
omega: Vector3dc,
vel: Vector3dc,
forces: PhysShip,
Expand All @@ -18,7 +17,7 @@ fun stabilize(
) {
val shipUp = Vector3d(0.0, 1.0, 0.0)
val worldUp = Vector3d(0.0, 1.0, 0.0)
ship.poseVel.rot.transform(shipUp)
ship.transform.shipToWorldRotation.transform(shipUp)

val angleBetween = shipUp.angle(worldUp)
val idealAngularAcceleration = Vector3d()
Expand All @@ -41,28 +40,26 @@ fun stabilize(
omega.z()
)

val stabilizationTorque = ship.poseVel.rot.transform(
ship.inertia.momentOfInertiaTensor.transform(
ship.poseVel.rot.transformInverse(idealAngularAcceleration)
val stabilizationTorque = ship.transform.shipToWorldRotation.transform(
ship.momentOfInertia.transform(
ship.transform.shipToWorldRotation.transformInverse(idealAngularAcceleration)
)
)

val speed = ship.poseVel.vel.length()

stabilizationTorque.mul(EurekaConfig.SERVER.stabilizationTorqueConstant / max(1.0, speed * speed * EurekaConfig.SERVER.scaledInstability / ship.inertia.shipMass + speed * EurekaConfig.SERVER.unscaledInstability))
stabilizationTorque.mul(EurekaConfig.SERVER.stabilizationTorqueConstant)
forces.applyInvariantTorque(stabilizationTorque)

if (linear) {
val idealVelocity = Vector3d(vel).negate()
idealVelocity.y = 0.0

// ideally this should work the same way as input is scaled
val s = EurekaConfig.SERVER.linearStabilizeMaxAntiVelocity * (1 - 1 / smoothingATanMax(EurekaConfig.SERVER.linearMaxMass, ship.inertia.shipMass * EurekaConfig.SERVER.linearMassScaling + 1.0)) / 10.0
val s = EurekaConfig.SERVER.linearStabilizeMaxAntiVelocity * (1 - 1 / smoothingATanMax(EurekaConfig.SERVER.linearMaxMass, ship.mass * EurekaConfig.SERVER.linearMassScaling + 1.0)) / 10.0

if (idealVelocity.lengthSquared() > s * s)
idealVelocity.normalize(s)

idealVelocity.mul(ship.inertia.shipMass * (10 - EurekaConfig.SERVER.antiVelocityMassRelevance))
idealVelocity.mul(ship.mass * (10 - EurekaConfig.SERVER.antiVelocityMassRelevance))
forces.applyInvariantForce(idealVelocity)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import org.joml.AxisAngle4d
import org.joml.Matrix4d
import org.joml.Vector3d
import org.valkyrienskies.core.api.ships.ServerShip
import org.valkyrienskies.core.impl.networking.simple.sendToClient
import org.valkyrienskies.core.util.datastructures.DenseBlockPosSet
import org.valkyrienskies.eureka.EurekaConfig
import org.valkyrienskies.mod.common.assembly.createNewShipWithBlocks
Expand All @@ -22,6 +21,7 @@ import org.valkyrienskies.mod.common.networking.PacketRestartChunkUpdates
import org.valkyrienskies.mod.common.networking.PacketStopChunkUpdates
import org.valkyrienskies.mod.common.playerWrapper
import org.valkyrienskies.mod.common.util.toJOML
import org.valkyrienskies.mod.common.vsCore
import org.valkyrienskies.mod.util.logger
import org.valkyrienskies.mod.util.relocateBlock
import org.valkyrienskies.mod.util.updateBlock
Expand Down Expand Up @@ -122,7 +122,9 @@ object ShipAssembler {
// Send a list of all the chunks that we plan on updating to players, so that they
// defer all updates until assembly is finished
level.players().forEach { player ->
PacketStopChunkUpdates(chunkPosesJOML).sendToClient(player.playerWrapper)
with (vsCore.simplePacketNetworking) {
PacketStopChunkUpdates(chunkPosesJOML).sendToClient(player.playerWrapper)
}
}

val toUpdate = Sets.newHashSet<Triple<BlockPos, BlockPos, BlockState>>()
Expand Down Expand Up @@ -165,7 +167,9 @@ object ShipAssembler {
) {
// Once all the chunk updates are sent to players, we can tell them to restart chunk updates
level.players().forEach { player ->
PacketRestartChunkUpdates(chunkPosesJOML).sendToClient(player.playerWrapper)
with (vsCore.simplePacketNetworking) {
PacketRestartChunkUpdates(chunkPosesJOML).sendToClient(player.playerWrapper)
}
}
}
}
Expand Down Expand Up @@ -206,7 +210,7 @@ object ShipAssembler {
}

private fun directions(center: BlockPos, lambda: (BlockPos) -> Unit) {
if (!EurekaConfig.SERVER.diagonals) Direction.values().forEach { lambda(center.relative(it)) }
if (!EurekaConfig.SERVER.diagonals) Direction.entries.forEach { lambda(center.relative(it)) }
for (x in -1..1) {
for (y in -1..1) {
for (z in -1..1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import net.fabricmc.loader.api.ModContainer;
import net.minecraft.client.Minecraft;
import net.minecraft.resources.ResourceLocation;
import org.valkyrienskies.core.impl.config.VSConfigClass;
import org.valkyrienskies.eureka.EurekaBlockEntities;
import org.valkyrienskies.eureka.EurekaConfig;
import org.valkyrienskies.eureka.EurekaMod;
Expand Down Expand Up @@ -55,7 +54,7 @@ public void onInitializeClient() {
);

ModelLoadingRegistry.INSTANCE.registerModelProvider((manager, out) -> {
for (final WoodType woodType : WoodType.values()) {
for (final WoodType woodType : WoodType.getEntries()) {
out.accept(new ResourceLocation(
EurekaMod.MOD_ID,
"block/" + woodType.getResourceName() + "_ship_helm_wheel"
Expand All @@ -77,7 +76,7 @@ public static class ModMenu implements ModMenuApi {
public ConfigScreenFactory<?> getModConfigScreenFactory() {
return (parent) -> VSClothConfig.createConfigScreenFor(
parent,
VSConfigClass.Companion.getRegisteredConfig(EurekaConfig.class)
EurekaConfig.class
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import net.minecraftforge.client.model.ForgeModelBakery
import net.minecraftforge.eventbus.api.IEventBus
import net.minecraftforge.fml.common.Mod
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent
import org.valkyrienskies.core.impl.config.VSConfigClass.Companion.getRegisteredConfig
import org.valkyrienskies.eureka.EurekaBlockEntities.SHIP_HELM
import org.valkyrienskies.eureka.EurekaConfig
import org.valkyrienskies.eureka.EurekaMod
Expand Down Expand Up @@ -52,7 +51,7 @@ class EurekaModForge {
ConfigGuiFactory { _: Minecraft?, parent: Screen? ->
createConfigScreenFor(
parent!!,
getRegisteredConfig(EurekaConfig::class.java)
EurekaConfig::class.java,
)
}
}
Expand Down Expand Up @@ -88,7 +87,7 @@ class EurekaModForge {
}

private fun onModelRegistry(event: ModelRegistryEvent?) {
for (woodType in WoodType.values()) {
for (woodType in WoodType.entries) {
ForgeModelBakery.addSpecialModel(
ResourceLocation(
EurekaMod.MOD_ID, "block/" + woodType.resourceName + "_ship_helm_wheel"
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ enabled_platforms=fabric,forge
archives_base_name=eureka
maven_group=org.valkyrienskies
# Dependencies
vs2_version=2.1.1-beta.5+72fac3232e
vs_core_version=1.1.0+b19b27c4a4
vs2_version=2.1.1-beta.6+cf815ca370
vs_core_version=1.1.0+bf19ff814e
minecraft_version=1.18.2
architectury_version=4.10.86
fabric_loader_version=0.14.11
Expand Down
Loading