diff --git a/common/src/main/kotlin/com/lambda/config/groups/Targeting.kt b/common/src/main/kotlin/com/lambda/config/groups/Targeting.kt index f9efb26db..dfe49c340 100644 --- a/common/src/main/kotlin/com/lambda/config/groups/Targeting.kt +++ b/common/src/main/kotlin/com/lambda/config/groups/Targeting.kt @@ -24,7 +24,7 @@ import com.lambda.interaction.rotation.Rotation.Companion.dist import com.lambda.interaction.rotation.Rotation.Companion.rotation import com.lambda.interaction.rotation.Rotation.Companion.rotationTo import com.lambda.threading.runSafe -import com.lambda.util.math.VecUtils.distSq +import com.lambda.util.math.distSq import com.lambda.util.world.fastEntitySearch import net.minecraft.client.network.ClientPlayerEntity import net.minecraft.client.network.OtherClientPlayerEntity @@ -40,16 +40,16 @@ import net.minecraft.entity.passive.PassiveEntity * based on player settings and entity characteristics. It allows for specifying which types of entities * are targetable, the range of targeting, and various other conditions for targeting. * - * @property owner The [Configurable] instance used to get and set configuration options for targeting. - * @property predicate The predicate used to determine whether the targeting settings are visible and active. - * @property defaultRange The default range within which entities can be targeted. - * @property maxRange The maximum range within which entities can be targeted. + * @param owner The [Configurable] instance used to get and set configuration options for targeting. + * @param predicate The predicate used to determine whether the targeting settings are visible and active. + * @param defaultRange The default range within which entities can be targeted. + * @param maxRange The maximum range within which entities can be targeted. */ abstract class Targeting( - owner: Configurable, - predicate: () -> Boolean = { true }, - defaultRange: Double, - maxRange: Double, + private val owner: Configurable, + private val predicate: () -> Boolean = { true }, + private val defaultRange: Double, + private val maxRange: Double, ) : TargetingConfig { /** diff --git a/common/src/main/kotlin/com/lambda/graphics/renderer/esp/DynamicAABB.kt b/common/src/main/kotlin/com/lambda/graphics/renderer/esp/DynamicAABB.kt index 95b22ecae..540420854 100644 --- a/common/src/main/kotlin/com/lambda/graphics/renderer/esp/DynamicAABB.kt +++ b/common/src/main/kotlin/com/lambda/graphics/renderer/esp/DynamicAABB.kt @@ -17,11 +17,8 @@ package com.lambda.graphics.renderer.esp -import com.lambda.util.math.VecUtils.minus -import com.lambda.util.math.VecUtils.plus -import com.lambda.util.extension.max -import com.lambda.util.extension.min import com.lambda.util.extension.prevPos +import com.lambda.util.math.minus import net.minecraft.entity.Entity import net.minecraft.util.math.Box diff --git a/common/src/main/kotlin/com/lambda/gui/api/component/core/DockingRect.kt b/common/src/main/kotlin/com/lambda/gui/api/component/core/DockingRect.kt index aa797d94d..3fbb65a28 100644 --- a/common/src/main/kotlin/com/lambda/gui/api/component/core/DockingRect.kt +++ b/common/src/main/kotlin/com/lambda/gui/api/component/core/DockingRect.kt @@ -57,12 +57,15 @@ abstract class DockingRect { var screenSize: Vec2d = Vec2d.ZERO var position: Vec2d - get() = relativeToAbs(relativePos).coerceIn(0.0, screenSize.x - size.x, 0.0, screenSize.y - size.y) + get() = relativeToAbs(relativePos) + .coerceIn(0.0, screenSize.x - size.x, 0.0, screenSize.y - size.y) set(value) { - relativePos = absToRelative(value.roundToStep(ClickGui.dockingGridSize)); if (autoDocking) autoDocking() + relativePos = absToRelative(value.roundToStep(ClickGui.dockingGridSize)) + if (autoDocking) autoDocking() } - private val dockingOffset get() = (screenSize - size) * Vec2d(dockingH.multiplier, dockingV.multiplier) + private val dockingOffset get() = + (screenSize - size) * Vec2d(dockingH.multiplier, dockingV.multiplier) private fun relativeToAbs(posIn: Vec2d) = posIn + dockingOffset private fun absToRelative(posIn: Vec2d) = posIn - dockingOffset diff --git a/common/src/main/kotlin/com/lambda/interaction/PlayerPacketManager.kt b/common/src/main/kotlin/com/lambda/interaction/PlayerPacketManager.kt index 785a000f7..6dc00a86f 100644 --- a/common/src/main/kotlin/com/lambda/interaction/PlayerPacketManager.kt +++ b/common/src/main/kotlin/com/lambda/interaction/PlayerPacketManager.kt @@ -24,10 +24,10 @@ import com.lambda.event.events.PlayerPacketEvent import com.lambda.interaction.rotation.Rotation import com.lambda.threading.runSafe import com.lambda.util.collections.LimitedOrderedSet -import com.lambda.util.extension.component1 -import com.lambda.util.extension.component2 -import com.lambda.util.extension.component3 -import com.lambda.util.math.VecUtils.approximate +import com.lambda.util.math.approximate +import com.lambda.util.math.component1 +import com.lambda.util.math.component2 +import com.lambda.util.math.component3 import com.lambda.util.player.MovementUtils.motionX import com.lambda.util.player.MovementUtils.motionZ import net.minecraft.network.packet.c2s.play.ClientCommandC2SPacket diff --git a/common/src/main/kotlin/com/lambda/interaction/blockplace/PlaceFinder.kt b/common/src/main/kotlin/com/lambda/interaction/blockplace/PlaceFinder.kt index b3bcf6689..fbcfd5297 100644 --- a/common/src/main/kotlin/com/lambda/interaction/blockplace/PlaceFinder.kt +++ b/common/src/main/kotlin/com/lambda/interaction/blockplace/PlaceFinder.kt @@ -22,8 +22,8 @@ import com.lambda.interaction.blockplace.PlaceInteraction.canPlaceAt import com.lambda.interaction.blockplace.PlaceInteraction.isClickable import com.lambda.interaction.visibilty.VisibilityChecker.getVisibleSurfaces import com.lambda.util.BlockUtils.blockState -import com.lambda.util.math.VecUtils.distSq -import com.lambda.util.math.VecUtils.getHitVec +import com.lambda.util.math.distSq +import com.lambda.util.math.getHitVec import net.minecraft.util.math.BlockPos import net.minecraft.util.math.Box import net.minecraft.util.math.Direction diff --git a/common/src/main/kotlin/com/lambda/interaction/construction/blueprint/Blueprint.kt b/common/src/main/kotlin/com/lambda/interaction/construction/blueprint/Blueprint.kt index 0e5f74e46..cf459f0e9 100644 --- a/common/src/main/kotlin/com/lambda/interaction/construction/blueprint/Blueprint.kt +++ b/common/src/main/kotlin/com/lambda/interaction/construction/blueprint/Blueprint.kt @@ -21,7 +21,7 @@ import com.lambda.interaction.construction.verify.TargetState import com.lambda.util.BlockUtils.blockPos import com.lambda.util.collections.ResettableLazy import com.lambda.util.extension.Structure -import com.lambda.util.math.VecUtils.blockPos +import com.lambda.util.math.blockPos import net.minecraft.structure.StructureTemplate import net.minecraft.util.math.* diff --git a/common/src/main/kotlin/com/lambda/interaction/construction/simulation/BuildSimulator.kt b/common/src/main/kotlin/com/lambda/interaction/construction/simulation/BuildSimulator.kt index 7a4ef8655..270b395d1 100644 --- a/common/src/main/kotlin/com/lambda/interaction/construction/simulation/BuildSimulator.kt +++ b/common/src/main/kotlin/com/lambda/interaction/construction/simulation/BuildSimulator.kt @@ -43,7 +43,7 @@ import com.lambda.util.BlockUtils.instantBreakable import com.lambda.util.BlockUtils.vecOf import com.lambda.util.Communication.warn import com.lambda.util.item.ItemStackUtils.equal -import com.lambda.util.math.VecUtils.distSq +import com.lambda.util.math.distSq import com.lambda.util.player.copyPlayer import com.lambda.util.world.raycast.RayCastUtils.blockResult import net.minecraft.block.OperatorBlock diff --git a/common/src/main/kotlin/com/lambda/interaction/material/container/containers/StashContainer.kt b/common/src/main/kotlin/com/lambda/interaction/material/container/containers/StashContainer.kt index 647f2e7a5..c75c6649b 100644 --- a/common/src/main/kotlin/com/lambda/interaction/material/container/containers/StashContainer.kt +++ b/common/src/main/kotlin/com/lambda/interaction/material/container/containers/StashContainer.kt @@ -19,7 +19,7 @@ package com.lambda.interaction.material.container.containers import com.lambda.interaction.material.StackSelection import com.lambda.interaction.material.container.MaterialContainer -import com.lambda.util.math.VecUtils.blockPos +import com.lambda.util.math.blockPos import com.lambda.util.text.buildText import com.lambda.util.text.highlighted import com.lambda.util.text.literal diff --git a/common/src/main/kotlin/com/lambda/interaction/rotation/Rotation.kt b/common/src/main/kotlin/com/lambda/interaction/rotation/Rotation.kt index 3bf05d1f5..3a078551b 100644 --- a/common/src/main/kotlin/com/lambda/interaction/rotation/Rotation.kt +++ b/common/src/main/kotlin/com/lambda/interaction/rotation/Rotation.kt @@ -22,8 +22,8 @@ import com.lambda.threading.runSafe import com.lambda.util.math.MathUtils.toDegree import com.lambda.util.math.MathUtils.toRadian import com.lambda.util.math.Vec2d -import com.lambda.util.math.VecUtils.plus -import com.lambda.util.math.VecUtils.times +import com.lambda.util.math.plus +import com.lambda.util.math.times import com.lambda.util.world.raycast.RayCastMask import com.lambda.util.world.raycast.RayCastUtils.rayCast import net.minecraft.entity.Entity diff --git a/common/src/main/kotlin/com/lambda/interaction/visibilty/VisibilityChecker.kt b/common/src/main/kotlin/com/lambda/interaction/visibilty/VisibilityChecker.kt index 6489517b5..9604f3b48 100644 --- a/common/src/main/kotlin/com/lambda/interaction/visibilty/VisibilityChecker.kt +++ b/common/src/main/kotlin/com/lambda/interaction/visibilty/VisibilityChecker.kt @@ -28,7 +28,7 @@ import com.lambda.interaction.rotation.RotationRequest import com.lambda.module.modules.client.TaskFlowModule import com.lambda.util.BlockUtils.blockState import com.lambda.util.extension.component6 -import com.lambda.util.math.VecUtils.distSq +import com.lambda.util.math.distSq import com.lambda.util.world.raycast.RayCastUtils.blockResult import com.lambda.util.world.raycast.RayCastUtils.entityResult import net.minecraft.entity.Entity diff --git a/common/src/main/kotlin/com/lambda/module/modules/client/DiscordRPC.kt b/common/src/main/kotlin/com/lambda/module/modules/client/DiscordRPC.kt index 88afb6c8f..dba2c907b 100644 --- a/common/src/main/kotlin/com/lambda/module/modules/client/DiscordRPC.kt +++ b/common/src/main/kotlin/com/lambda/module/modules/client/DiscordRPC.kt @@ -93,7 +93,7 @@ object DiscordRPC : Module( get() = uuid == currentParty?.leader?.uuid private val PlayerEntity.isInParty - get() = currentParty?.players?.any { it.uuid == this@isInParty.uuid } + get() = currentParty?.players?.any { it.uuid == this.uuid } init { listenUnsafe { @@ -160,7 +160,7 @@ object DiscordRPC : Module( runConcurrent { while (rpc.connected) { updateActivity() - delay(delay.toLong()) + delay(delay) } } } diff --git a/common/src/main/kotlin/com/lambda/module/modules/combat/Criticals.kt b/common/src/main/kotlin/com/lambda/module/modules/combat/Criticals.kt index fcaf6308c..a46b24721 100644 --- a/common/src/main/kotlin/com/lambda/module/modules/combat/Criticals.kt +++ b/common/src/main/kotlin/com/lambda/module/modules/combat/Criticals.kt @@ -24,10 +24,10 @@ import com.lambda.interaction.rotation.Rotation import com.lambda.interaction.rotation.Rotation.Companion.rotationTo import com.lambda.module.Module import com.lambda.module.tag.ModuleTag -import com.lambda.util.extension.component1 -import com.lambda.util.extension.component2 -import com.lambda.util.extension.component3 import com.lambda.util.extension.rotation +import com.lambda.util.math.component1 +import com.lambda.util.math.component2 +import com.lambda.util.math.component3 import net.minecraft.network.packet.c2s.play.PlayerActionC2SPacket import net.minecraft.network.packet.c2s.play.PlayerInteractItemC2SPacket import net.minecraft.network.packet.c2s.play.PlayerMoveC2SPacket diff --git a/common/src/main/kotlin/com/lambda/module/modules/combat/KillAura.kt b/common/src/main/kotlin/com/lambda/module/modules/combat/KillAura.kt index 9e5f24556..8c9850a59 100644 --- a/common/src/main/kotlin/com/lambda/module/modules/combat/KillAura.kt +++ b/common/src/main/kotlin/com/lambda/module/modules/combat/KillAura.kt @@ -38,9 +38,9 @@ import com.lambda.module.tag.ModuleTag import com.lambda.threading.runConcurrent import com.lambda.threading.runSafe import com.lambda.util.math.MathUtils.random -import com.lambda.util.math.VecUtils.distSq -import com.lambda.util.math.VecUtils.plus -import com.lambda.util.math.VecUtils.times +import com.lambda.util.math.distSq +import com.lambda.util.math.plus +import com.lambda.util.math.times import com.lambda.util.math.lerp import com.lambda.util.player.MovementUtils.moveDiff import com.lambda.util.player.prediction.buildPlayerPrediction diff --git a/common/src/main/kotlin/com/lambda/module/modules/movement/BackTrack.kt b/common/src/main/kotlin/com/lambda/module/modules/movement/BackTrack.kt index c28202998..0a0ab8957 100644 --- a/common/src/main/kotlin/com/lambda/module/modules/movement/BackTrack.kt +++ b/common/src/main/kotlin/com/lambda/module/modules/movement/BackTrack.kt @@ -33,9 +33,9 @@ import com.lambda.util.ClientPacket import com.lambda.util.PacketUtils.handlePacketSilently import com.lambda.util.PacketUtils.sendPacketSilently import com.lambda.util.ServerPacket -import com.lambda.util.math.VecUtils.dist -import com.lambda.util.math.VecUtils.minus -import com.lambda.util.math.VecUtils.plus +import com.lambda.util.math.dist +import com.lambda.util.math.minus +import com.lambda.util.math.plus import com.lambda.util.math.lerp import com.lambda.util.math.multAlpha import net.minecraft.entity.LivingEntity diff --git a/common/src/main/kotlin/com/lambda/module/modules/movement/Blink.kt b/common/src/main/kotlin/com/lambda/module/modules/movement/Blink.kt index 1b0eed92b..67dd99510 100644 --- a/common/src/main/kotlin/com/lambda/module/modules/movement/Blink.kt +++ b/common/src/main/kotlin/com/lambda/module/modules/movement/Blink.kt @@ -30,7 +30,7 @@ import com.lambda.module.tag.ModuleTag import com.lambda.util.ClientPacket import com.lambda.util.PacketUtils.handlePacketSilently import com.lambda.util.PacketUtils.sendPacketSilently -import com.lambda.util.math.VecUtils.minus +import com.lambda.util.math.minus import com.lambda.util.math.setAlpha import net.minecraft.network.packet.c2s.play.PlayerMoveC2SPacket import net.minecraft.network.packet.s2c.play.EntityVelocityUpdateS2CPacket diff --git a/common/src/main/kotlin/com/lambda/module/modules/movement/Jesus.kt b/common/src/main/kotlin/com/lambda/module/modules/movement/Jesus.kt index 1d36a2b89..4eefe996d 100644 --- a/common/src/main/kotlin/com/lambda/module/modules/movement/Jesus.kt +++ b/common/src/main/kotlin/com/lambda/module/modules/movement/Jesus.kt @@ -27,7 +27,7 @@ import com.lambda.module.Module import com.lambda.module.tag.ModuleTag import com.lambda.util.Nameable import com.lambda.util.math.MathUtils.toInt -import com.lambda.util.math.VecUtils.minus +import com.lambda.util.math.minus import com.lambda.util.player.MovementUtils.isInputting import com.lambda.util.player.MovementUtils.motionY import com.lambda.util.player.MovementUtils.setSpeed diff --git a/common/src/main/kotlin/com/lambda/module/modules/movement/NoFall.kt b/common/src/main/kotlin/com/lambda/module/modules/movement/NoFall.kt index 0b342a16b..193f431c1 100644 --- a/common/src/main/kotlin/com/lambda/module/modules/movement/NoFall.kt +++ b/common/src/main/kotlin/com/lambda/module/modules/movement/NoFall.kt @@ -21,9 +21,9 @@ import com.lambda.event.events.MovementEvent import com.lambda.event.listener.SafeListener.Companion.listen import com.lambda.module.Module import com.lambda.module.tag.ModuleTag -import com.lambda.util.extension.component1 -import com.lambda.util.extension.component2 -import com.lambda.util.extension.component3 +import com.lambda.util.math.component1 +import com.lambda.util.math.component2 +import com.lambda.util.math.component3 import com.lambda.util.player.MovementUtils.motion import com.lambda.util.player.MovementUtils.motionY import net.minecraft.network.packet.c2s.play.PlayerActionC2SPacket diff --git a/common/src/main/kotlin/com/lambda/module/modules/movement/TargetStrafe.kt b/common/src/main/kotlin/com/lambda/module/modules/movement/TargetStrafe.kt index 0ac488f05..b80138325 100644 --- a/common/src/main/kotlin/com/lambda/module/modules/movement/TargetStrafe.kt +++ b/common/src/main/kotlin/com/lambda/module/modules/movement/TargetStrafe.kt @@ -24,7 +24,7 @@ import com.lambda.interaction.rotation.Rotation.Companion.rotationTo import com.lambda.module.Module import com.lambda.module.modules.combat.KillAura import com.lambda.module.tag.ModuleTag -import com.lambda.util.math.VecUtils.distSq +import com.lambda.util.math.distSq import com.lambda.util.player.MovementUtils.buildMovementInput import com.lambda.util.player.MovementUtils.mergeFrom import kotlin.math.pow diff --git a/common/src/main/kotlin/com/lambda/module/modules/network/Rubberband.kt b/common/src/main/kotlin/com/lambda/module/modules/network/Rubberband.kt index 5b722aae9..78ebb96bb 100644 --- a/common/src/main/kotlin/com/lambda/module/modules/network/Rubberband.kt +++ b/common/src/main/kotlin/com/lambda/module/modules/network/Rubberband.kt @@ -23,8 +23,8 @@ import com.lambda.interaction.PlayerPacketManager import com.lambda.module.Module import com.lambda.module.tag.ModuleTag import com.lambda.util.Communication.warn -import com.lambda.util.math.VecUtils.dist -import com.lambda.util.math.VecUtils.distSq +import com.lambda.util.math.dist +import com.lambda.util.math.distSq import com.lambda.util.text.buildText import com.lambda.util.text.color import com.lambda.util.text.literal diff --git a/common/src/main/kotlin/com/lambda/module/modules/player/Freecam.kt b/common/src/main/kotlin/com/lambda/module/modules/player/Freecam.kt index 47cd7c3cb..2698d4fda 100644 --- a/common/src/main/kotlin/com/lambda/module/modules/player/Freecam.kt +++ b/common/src/main/kotlin/com/lambda/module/modules/player/Freecam.kt @@ -27,11 +27,11 @@ import com.lambda.interaction.rotation.RotationRequest import com.lambda.interaction.rotation.RotationMode import com.lambda.module.Module import com.lambda.module.tag.ModuleTag -import com.lambda.util.extension.interpolate import com.lambda.util.extension.partialTicks import com.lambda.util.extension.rotation -import com.lambda.util.math.VecUtils.plus -import com.lambda.util.math.VecUtils.times +import com.lambda.util.math.interpolate +import com.lambda.util.math.plus +import com.lambda.util.math.times import com.lambda.util.player.MovementUtils.calcMoveRad import com.lambda.util.player.MovementUtils.cancel import com.lambda.util.player.MovementUtils.handledByBaritone @@ -64,7 +64,7 @@ object Freecam : Module( private var prevPosition: Vec3d = Vec3d.ZERO private var position: Vec3d = Vec3d.ZERO private val lerpPos: Vec3d - get() = prevPosition.interpolate(position, mc.partialTicks) + get() = prevPosition.interpolate(mc.partialTicks, position) private var rotation: Rotation = Rotation.ZERO private var velocity: Vec3d = Vec3d.ZERO diff --git a/common/src/main/kotlin/com/lambda/module/modules/player/HighwayTools.kt b/common/src/main/kotlin/com/lambda/module/modules/player/HighwayTools.kt index 89ec24286..7a69b28eb 100644 --- a/common/src/main/kotlin/com/lambda/module/modules/player/HighwayTools.kt +++ b/common/src/main/kotlin/com/lambda/module/modules/player/HighwayTools.kt @@ -29,7 +29,7 @@ import com.lambda.util.Communication.info import com.lambda.util.extension.Structure import com.lambda.util.extension.moveY import com.lambda.util.math.MathUtils.floorToInt -import com.lambda.util.math.VecUtils.rotateClockwise +import com.lambda.util.math.rotateClockwise import com.lambda.util.player.MovementUtils.octant import com.lambda.util.world.StructureUtils.generateDirectionalTube import net.minecraft.block.Blocks diff --git a/common/src/main/kotlin/com/lambda/module/modules/player/Scaffold.kt b/common/src/main/kotlin/com/lambda/module/modules/player/Scaffold.kt index 2ff777e43..13e5acbe2 100644 --- a/common/src/main/kotlin/com/lambda/module/modules/player/Scaffold.kt +++ b/common/src/main/kotlin/com/lambda/module/modules/player/Scaffold.kt @@ -45,8 +45,8 @@ import com.lambda.module.modules.client.GuiSettings import com.lambda.module.modules.client.TaskFlowModule import com.lambda.module.tag.ModuleTag import com.lambda.util.math.MathUtils.floorToInt -import com.lambda.util.math.VecUtils.dist -import com.lambda.util.math.VecUtils.distSq +import com.lambda.util.math.dist +import com.lambda.util.math.distSq import com.lambda.util.math.multAlpha import com.lambda.util.math.step import com.lambda.util.math.transform diff --git a/common/src/main/kotlin/com/lambda/module/modules/render/Particles.kt b/common/src/main/kotlin/com/lambda/module/modules/render/Particles.kt index 6b66ba47b..b27fb7989 100644 --- a/common/src/main/kotlin/com/lambda/module/modules/render/Particles.kt +++ b/common/src/main/kotlin/com/lambda/module/modules/render/Particles.kt @@ -40,9 +40,10 @@ import com.lambda.module.modules.client.GuiSettings.colorSpeed import com.lambda.module.tag.ModuleTag import com.lambda.util.extension.partialTicks import com.lambda.util.math.MathUtils.random -import com.lambda.util.math.VecUtils -import com.lambda.util.math.VecUtils.plus -import com.lambda.util.math.VecUtils.times +import com.lambda.util.math.UP +import com.lambda.util.math.DOWN +import com.lambda.util.math.plus +import com.lambda.util.math.times import com.lambda.util.math.lerp import com.lambda.util.math.multAlpha import com.lambda.util.math.transform @@ -134,8 +135,8 @@ object Particles : Module( repeat(environmentSpawnAmount) { var particlePos = player.pos + Rotation(random(-180.0, 180.0), 0.0).vector * random(0.0, environmentRange) - Rotation.DOWN.rayCast(6.0, particlePos + VecUtils.UP * 2.0, true, RayCastMask.BLOCK)?.pos?.let { - particlePos = it + VecUtils.UP * 0.03 + Rotation.DOWN.rayCast(6.0, particlePos + UP * 2.0, true, RayCastMask.BLOCK)?.pos?.let { + particlePos = it + UP * 0.03 } ?: return@repeat val particleMotion = Rotation( @@ -169,7 +170,7 @@ object Particles : Module( prevPos = position - if (!lay) motion += VecUtils.DOWN * gravity * 0.01 + if (!lay) motion += DOWN * gravity * 0.01 motion *= 0.9 + inertia * 0.1 position += motion diff --git a/common/src/main/kotlin/com/lambda/util/combat/CombatUtils.kt b/common/src/main/kotlin/com/lambda/util/combat/CombatUtils.kt index 0d0784e85..56d86a32c 100644 --- a/common/src/main/kotlin/com/lambda/util/combat/CombatUtils.kt +++ b/common/src/main/kotlin/com/lambda/util/combat/CombatUtils.kt @@ -19,10 +19,9 @@ package com.lambda.util.combat import com.lambda.context.SafeContext import com.lambda.core.annotations.InternalApi -import com.lambda.util.math.VecUtils.dist -import com.lambda.util.math.VecUtils.minus -import com.lambda.util.math.VecUtils.times -import com.lambda.util.math.VecUtils.vec3d +import com.lambda.util.math.dist +import com.lambda.util.math.minus +import com.lambda.util.math.times import com.lambda.util.world.WorldUtils.internalGetFastEntities import com.lambda.util.world.fastEntitySearch import com.lambda.util.world.toFastVec @@ -34,7 +33,6 @@ import net.minecraft.entity.decoration.EndCrystalEntity import net.minecraft.entity.effect.StatusEffects import net.minecraft.registry.tag.DamageTypeTags import net.minecraft.util.math.Vec3d -import net.minecraft.util.math.Vec3i import net.minecraft.world.explosion.Explosion import kotlin.math.max import kotlin.math.min @@ -81,16 +79,6 @@ object CombatUtils { fun SafeContext.explosionDamage(source: Explosion, entity: LivingEntity) = explosionDamage(source.position, entity, source.power.toDouble()) - /** - * Calculates the damage dealt by an explosion to a living entity - * - * @param position The position of the explosion - * @param entity The entity to calculate the damage for - * @param power The [power of the explosion](https://minecraft.wiki/w/Explosion#Damage) - */ - fun SafeContext.explosionDamage(position: Vec3i, entity: LivingEntity, power: Double): Double = - explosionDamage(position.vec3d, entity, power) - /** * Calculates the damage dealt by an explosion to a living entity. * @@ -135,7 +123,7 @@ object CombatUtils { * @param position The position of the explosion * @param power The strength of the explosion */ - fun SafeContext.explosionVelocity(entity: LivingEntity, position: Vec3d, power: Double): Vec3d { + fun explosionVelocity(entity: LivingEntity, position: Vec3d, power: Double): Vec3d { val distance = entity.pos.distanceTo(position) val size = power * 2.0 diff --git a/common/src/main/kotlin/com/lambda/util/extension/Box.kt b/common/src/main/kotlin/com/lambda/util/extension/Box.kt index f094d9beb..61879d9e5 100644 --- a/common/src/main/kotlin/com/lambda/util/extension/Box.kt +++ b/common/src/main/kotlin/com/lambda/util/extension/Box.kt @@ -21,7 +21,7 @@ import net.minecraft.util.math.Box import net.minecraft.util.math.Vec3d val Box.min get() = Vec3d(minX, minY, minZ) - val Box.max get() = Vec3d(maxX, maxY, maxZ) operator fun Box.contains(boundingBox: Box) = this.intersects(boundingBox) +operator fun DoubleArray.component6() = this[5] diff --git a/common/src/main/kotlin/com/lambda/util/extension/Collections.kt b/common/src/main/kotlin/com/lambda/util/extension/Collections.kt index 507a6c185..b414a0ca8 100644 --- a/common/src/main/kotlin/com/lambda/util/extension/Collections.kt +++ b/common/src/main/kotlin/com/lambda/util/extension/Collections.kt @@ -20,42 +20,12 @@ package com.lambda.util.extension import kotlin.reflect.KClass /** - * Filters elements of the iterable by their runtime type and a predicate, and adds the matching elements to the specified mutable collection. + * Filters elements by their type and adds the result in the destination * - * This function allows filtering elements of an iterable based on their runtime type and a provided predicate function. - * The elements that match both the type constraint and the predicate are added to the destination mutable collection. - * Because we do not want additional overhead, this function acts as pointer receiver to a collection. - * The predicate function determines whether an element should be included based on its type and any additional criteria. - * - * @param R The target type to filter elements to. - * @param C The type of the destination mutable collection. - * @param destination The mutable collection to which the filtered elements will be added. - * @param predicate The predicate function that determines whether an element should be included based on its type and other criteria. - */ -inline fun > Iterable<*>.filterPointer( - destination: C?, - predicate: (R) -> Boolean, -) = - forEach { element -> - val fulfilled = predicate(element as R) - - if (fulfilled && destination != null) - destination.add(element) - - } - -/** - * Filters elements of the iterable by their runtime type and a predicate, and adds the matching elements to the specified mutable collection. - * - * This function allows filtering elements of an iterable based on their runtime type and a provided predicate function. - * The elements that match both the type constraint and the predicate are added to the destination mutable collection. - * Because we do not want additional overhead, this function acts as pointer receiver to a collection. - * The predicate function determines whether an element should be included based on its type and any additional criteria. - * - * @param R The target type to filter elements to. - * @param C The type of the destination mutable collection. - * @param destination The mutable collection to which the filtered elements will be added. - * @param predicate The predicate function that determines whether an element should be included based on its type and other criteria. + * @param R The target type to filter elements to + * @param C The type of the destination mutable collection + * @param destination The mutable collection to which the filtered elements will be added + * @param predicate The predicate function that determines whether an element should be included based on its type and other criteria */ inline fun > Iterable<*>.filterPointer( kClass: KClass, diff --git a/common/src/main/kotlin/com/lambda/util/extension/Components.kt b/common/src/main/kotlin/com/lambda/util/extension/Components.kt deleted file mode 100644 index 95c8659dd..000000000 --- a/common/src/main/kotlin/com/lambda/util/extension/Components.kt +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright 2024 Lambda - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package com.lambda.util.extension - -import net.minecraft.util.math.Vec2f -import net.minecraft.util.math.Vec3d - -operator fun Vec2f.component1() = this.x -operator fun Vec2f.component2() = this.y -operator fun Vec3d.component1() = this.x -operator fun Vec3d.component2() = this.y -operator fun Vec3d.component3() = this.z -operator fun DoubleArray.component6() = this[5] diff --git a/common/src/main/kotlin/com/lambda/util/extension/Entity.kt b/common/src/main/kotlin/com/lambda/util/extension/Entity.kt index d94a33c0f..a58b76cb2 100644 --- a/common/src/main/kotlin/com/lambda/util/extension/Entity.kt +++ b/common/src/main/kotlin/com/lambda/util/extension/Entity.kt @@ -28,11 +28,3 @@ val Entity.prevPos val Entity.rotation get() = Rotation(yaw, pitch) - -var LivingEntity.isElytraFlying - get() = isFallFlying - set(value) { - setFlag(7, value) - } - -fun Vec3d.interpolate(other: Vec3d, t: Double) = lerp(t, this, other) diff --git a/common/src/main/kotlin/com/lambda/util/extension/Other.kt b/common/src/main/kotlin/com/lambda/util/extension/Other.kt index 9dff4ea5c..778e45c58 100644 --- a/common/src/main/kotlin/com/lambda/util/extension/Other.kt +++ b/common/src/main/kotlin/com/lambda/util/extension/Other.kt @@ -17,24 +17,13 @@ package com.lambda.util.extension +import com.mojang.authlib.GameProfile import kotlin.contracts.ExperimentalContracts import kotlin.contracts.InvocationKind import kotlin.contracts.contract -/** - * Executes the given block only if the object receiver is null - * Opposite of `Any?.let {}` - */ -@OptIn(ExperimentalContracts::class) -inline fun T?.ifNull(block: () -> Unit): T? { - contract { - callsInPlace(block, InvocationKind.AT_MOST_ONCE) - } - - if (this == null) block() - - return this -} +val GameProfile.isOffline + get() = properties.isEmpty val Class<*>.isObject: Boolean get() = declaredFields.any { it.name == "INSTANCE" } diff --git a/common/src/main/kotlin/com/lambda/util/extension/Player.kt b/common/src/main/kotlin/com/lambda/util/extension/Player.kt deleted file mode 100644 index 0e1e6bf6a..000000000 --- a/common/src/main/kotlin/com/lambda/util/extension/Player.kt +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright 2024 Lambda - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package com.lambda.util.extension - -import com.mojang.authlib.GameProfile -import net.minecraft.entity.player.PlayerEntity - -val PlayerEntity.isOffline - get() = gameProfile.isOffline - -val GameProfile.isOffline - get() = properties.isEmpty diff --git a/common/src/main/kotlin/com/lambda/util/extension/Structures.kt b/common/src/main/kotlin/com/lambda/util/extension/Structures.kt new file mode 100644 index 000000000..12d4d80ed --- /dev/null +++ b/common/src/main/kotlin/com/lambda/util/extension/Structures.kt @@ -0,0 +1,160 @@ +/* + * Copyright 2025 Lambda + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.lambda.util.extension + +import com.lambda.Lambda.mc +import com.lambda.util.VarIntIterator +import com.lambda.util.world.FastVector +import com.lambda.util.world.fastVectorOf +import com.lambda.util.world.x +import com.lambda.util.world.y +import com.lambda.util.world.z +import net.minecraft.block.Block +import net.minecraft.datafixer.DataFixTypes +import net.minecraft.nbt.NbtCompound +import net.minecraft.nbt.NbtList +import net.minecraft.registry.RegistryEntryLookup +import net.minecraft.structure.StructureTemplate +import kotlin.experimental.and + +private fun positionFromIndex(width: Int, length: Int, index: Int): FastVector { + val y = index / (width * length) + val remainder = index - (y * width * length) + val z = remainder / width + val x = remainder - z * width + + return fastVectorOf(x, y, z) +} + +fun StructureTemplate.readNbtOrException( + lookup: RegistryEntryLookup, + nbt: NbtCompound, +): Throwable? = runCatching { readNbt(lookup, nbt) }.exceptionOrNull() + +fun StructureTemplate.readSpongeOrException( + lookup: RegistryEntryLookup, + nbt: NbtCompound, +): Throwable? = when (nbt.getInt("Version")) { + 1, 2, 3 -> readSpongeV1OrException(lookup, nbt) + else -> IllegalStateException("Invalid sponge schematic version") +} + +fun StructureTemplate.readSchematicOrException( + lookup: RegistryEntryLookup, + nbt: NbtCompound, +): Throwable = when (nbt.getString("Materials")) { + "Alpha" -> IllegalStateException("Not implemented, you can help us by contributing to the project") + "Classic" -> IllegalStateException("Method not implemented, you can help us by contributing to the Minecraft Wiki (https://minecraft.wiki/w/Data_values_(Classic))") + "Pocket" -> IllegalStateException("Pocket Edition schematics are not supported") + else -> IllegalStateException("Invalid MCEdit schematic version") +} + +private fun StructureTemplate.readSpongeV1OrException( + lookup: RegistryEntryLookup, + nbt: NbtCompound, +): Throwable? { + val version = nbt.getInt("DataVersion") + val width = (nbt.getShort("Width") and 0xFFFF.toShort()).toInt() + val height = (nbt.getShort("Height") and 0xFFFF.toShort()).toInt() + val length = (nbt.getShort("Length") and 0xFFFF.toShort()).toInt() + + val metadata = nbt.getCompound("Metadata") + + // If the offset is too far, we simply ignore it + // I think at some point schematica calculated + // the offset based on the distance between the player position + // and the schematic lower corner, so it would fuck up everything + // when you tried to import and build it using Baritone + // val minimumPosition = nbt.getIntArray("Offset") + // .takeIf { it.isNotEmpty() } + // ?.let { fastVectorOf(it[0], it[1], it[2]) } + // ?.takeIf { 274945015809L times 16 < it } ?: 0L + + val palette = nbt.getCompound("Palette") + + val paletteMax = nbt.getInt("PaletteMax") + val newPalette = NbtList() + + if (palette.size != paletteMax) return IllegalStateException("Block palette size does not match the provided size (corrupted?)") + + palette.keys + .sortedBy { palette.getInt(it) } + .forEach { key -> + val resource = key.substringBefore('[') + val blockState = NbtCompound() + + // Why ? + // I know it's supposed to be SNBT, but it cannot be parsed back + key.substringAfter('[') + .substringBefore(']') + .takeIf { it != resource } + ?.split(',') + ?.associate { it.substringBefore('=') to it.substringAfter('=') } + ?.forEach { (key, value) -> blockState.putString(key, value) } + + // Populate the list using the correct indices + newPalette.add(NbtCompound().apply { + putString("Name", resource) + put("Properties", blockState) + }) + } + + val newBlocks = NbtList() + var blockIndex = 0 + VarIntIterator(nbt.getByteArray("BlockData")) + .forEach { blockId -> + val blockpos = positionFromIndex(width, length, blockIndex++) + + newBlocks.add(NbtCompound().apply { + putIntList("pos", blockpos.x, blockpos.y, blockpos.z) + putInt("state", blockId) + }) + } + + // Construct a structure compatible nbt compound + nbt.putIntList("size", width, height, length) + nbt.put("palette", newPalette) + nbt.put("blocks", newBlocks) + nbt.putString("author", metadata.getString("Author")) + + // Fix the data for future versions + DataFixTypes.STRUCTURE.update(mc.dataFixer, nbt, version) + + // Use the StructureTemplate NBT read utils in order to construct the template + return readNbtOrException(lookup, nbt) +} + +private fun StructureTemplate.readSpongeV3OrException( + lookup: RegistryEntryLookup, + nbt: NbtCompound, +): Throwable? { + // Third revision + // - 3D Biome support + // - Rename Palette to BlockPalette + // - Wordsmithing varint and palette usages + nbt.put("Palette", nbt.getCompound("BlockPalette")) + + return readSpongeV1OrException(lookup, nbt) +} + +fun StructureTemplate.readLitematicaOrException( + lookup: RegistryEntryLookup, + nbt: NbtCompound, +): Throwable { + return IllegalStateException("Litematica parsing is not implemented") +} diff --git a/common/src/main/kotlin/com/lambda/util/extension/World.kt b/common/src/main/kotlin/com/lambda/util/extension/World.kt index de812386a..03e6dc789 100644 --- a/common/src/main/kotlin/com/lambda/util/extension/World.kt +++ b/common/src/main/kotlin/com/lambda/util/extension/World.kt @@ -17,24 +17,15 @@ package com.lambda.util.extension -import com.lambda.Lambda.mc import com.lambda.context.SafeContext -import com.lambda.util.VarIntIterator import com.lambda.util.world.* -import net.minecraft.block.Block import net.minecraft.block.BlockState import net.minecraft.block.Blocks -import net.minecraft.datafixer.DataFixTypes import net.minecraft.fluid.FluidState import net.minecraft.fluid.Fluids -import net.minecraft.nbt.NbtCompound -import net.minecraft.nbt.NbtList -import net.minecraft.registry.RegistryEntryLookup -import net.minecraft.structure.StructureTemplate import net.minecraft.util.math.BlockPos import net.minecraft.world.World import java.awt.Color -import kotlin.experimental.and fun SafeContext.collisionShape(state: BlockState, pos: BlockPos) = state.getCollisionShape(world, pos).offset(pos.x.toDouble(), pos.y.toDouble(), pos.z.toDouble()) @@ -70,130 +61,3 @@ fun World.getFluidState(x: Int, y: Int, z: Int): FluidState { fun World.getBlockState(vec: FastVector): BlockState = getBlockState(vec.x, vec.y, vec.z) fun World.getBlockEntity(vec: FastVector) = getBlockEntity(vec.toBlockPos()) fun World.getFluidState(vec: FastVector): FluidState = getFluidState(vec.x, vec.y, vec.z) - -private fun positionFromIndex(width: Int, length: Int, index: Int): FastVector { - val y = index / (width * length) - val remainder = index - (y * width * length) - val z = remainder / width - val x = remainder - z * width - - return fastVectorOf(x, y, z) -} - -fun StructureTemplate.readNbtOrException( - lookup: RegistryEntryLookup, - nbt: NbtCompound, -): Throwable? = runCatching { readNbt(lookup, nbt) }.exceptionOrNull() - -fun StructureTemplate.readSpongeOrException( - lookup: RegistryEntryLookup, - nbt: NbtCompound, -): Throwable? = when (nbt.getInt("Version")) { - 1, 2, 3 -> readSpongeV1OrException(lookup, nbt) - else -> IllegalStateException("Invalid sponge schematic version") -} - -fun StructureTemplate.readSchematicOrException( - lookup: RegistryEntryLookup, - nbt: NbtCompound, -): Throwable = when (nbt.getString("Materials")) { - "Alpha" -> IllegalStateException("Not implemented, you can help us by contributing to the project") - "Classic" -> IllegalStateException("Method not implemented, you can help us by contributing to the Minecraft Wiki (https://minecraft.wiki/w/Data_values_(Classic))") - "Pocket" -> IllegalStateException("Pocket Edition schematics are not supported") - else -> IllegalStateException("Invalid MCEdit schematic version") -} - -private fun StructureTemplate.readSpongeV1OrException( - lookup: RegistryEntryLookup, - nbt: NbtCompound, -): Throwable? { - val version = nbt.getInt("DataVersion") - val width = (nbt.getShort("Width") and 0xFFFF.toShort()).toInt() - val height = (nbt.getShort("Height") and 0xFFFF.toShort()).toInt() - val length = (nbt.getShort("Length") and 0xFFFF.toShort()).toInt() - - val metadata = nbt.getCompound("Metadata") - - // If the offset is too far, we simply ignore it - // I think at some point schematica calculated - // the offset based on the distance between the player position - // and the schematic lower corner, so it would fuck up everything - // when you tried to import and build it using Baritone - // val minimumPosition = nbt.getIntArray("Offset") - // .takeIf { it.isNotEmpty() } - // ?.let { fastVectorOf(it[0], it[1], it[2]) } - // ?.takeIf { 274945015809L times 16 < it } ?: 0L - - val palette = nbt.getCompound("Palette") - - val paletteMax = nbt.getInt("PaletteMax") - val newPalette = NbtList() - - if (palette.size != paletteMax) return IllegalStateException("Block palette size does not match the provided size (corrupted?)") - - palette.keys - .sortedBy { palette.getInt(it) } - .forEach { key -> - val resource = key.substringBefore('[') - val blockState = NbtCompound() - - // Why ? - // I know it's supposed to be SNBT, but it cannot be parsed back - key.substringAfter('[') - .substringBefore(']') - .takeIf { it != resource } - ?.split(',') - ?.associate { it.substringBefore('=') to it.substringAfter('=') } - ?.forEach { (key, value) -> blockState.putString(key, value) } - - // Populate the list using the correct indices - newPalette.add(NbtCompound().apply { - putString("Name", resource) - put("Properties", blockState) - }) - } - - val newBlocks = NbtList() - var blockIndex = 0 - VarIntIterator(nbt.getByteArray("BlockData")) - .forEach { blockId -> - val blockpos = positionFromIndex(width, length, blockIndex++) - - newBlocks.add(NbtCompound().apply { - putIntList("pos", blockpos.x, blockpos.y, blockpos.z) - putInt("state", blockId) - }) - } - - // Construct a structure compatible nbt compound - nbt.putIntList("size", width, height, length) - nbt.put("palette", newPalette) - nbt.put("blocks", newBlocks) - nbt.putString("author", metadata.getString("Author")) - - // Fix the data for future versions - DataFixTypes.STRUCTURE.update(mc.dataFixer, nbt, version) - - // Use the StructureTemplate NBT read utils in order to construct the template - return readNbtOrException(lookup, nbt) -} - -private fun StructureTemplate.readSpongeV3OrException( - lookup: RegistryEntryLookup, - nbt: NbtCompound, -): Throwable? { - // Third revision - // - 3D Biome support - // - Rename Palette to BlockPalette - // - Wordsmithing varint and palette usages - nbt.put("Palette", nbt.getCompound("BlockPalette")) - - return readSpongeV1OrException(lookup, nbt) -} - -fun StructureTemplate.readLitematicaOrException( - lookup: RegistryEntryLookup, - nbt: NbtCompound, -): Throwable { - return IllegalStateException("Litematica parsing is not implemented") -} diff --git a/common/src/main/kotlin/com/lambda/util/math/Color.kt b/common/src/main/kotlin/com/lambda/util/math/Color.kt index 98f92803e..92b210ec3 100644 --- a/common/src/main/kotlin/com/lambda/util/math/Color.kt +++ b/common/src/main/kotlin/com/lambda/util/math/Color.kt @@ -20,16 +20,6 @@ package com.lambda.util.math import net.minecraft.util.math.Vec3d import java.awt.Color -val Color.hsb - get() = Color.RGBtoHSB(red, green, blue, null) - .map(Float::toDouble) - -fun DoubleArray.readHSB(): Color = Color.getHSBColor(this[0].toFloat(), this[1].toFloat(), this[2].toFloat()) - -val Color.hue get() = hsb[0] -val Color.saturation get() = hsb[1] -val Color.brightness get() = hsb[2] - fun Color.setAlpha(value: Double) = Color(red, green, blue, (value * 255.0).coerceIn(0.0, 255.0).toInt()) diff --git a/common/src/main/kotlin/com/lambda/util/math/MathUtils.kt b/common/src/main/kotlin/com/lambda/util/math/MathUtils.kt index b397cf190..f6a889191 100644 --- a/common/src/main/kotlin/com/lambda/util/math/MathUtils.kt +++ b/common/src/main/kotlin/com/lambda/util/math/MathUtils.kt @@ -25,20 +25,18 @@ import kotlin.random.Random.Default.nextDouble object MathUtils { private const val PI_FLOAT = 3.141593f - fun Float.toRadian() = this / 180.0f * PI_FLOAT + inline val Int.sq: Int get() = this * this + fun Float.toRadian() = this / 180.0f * PI_FLOAT fun Double.toRadian() = this / 180.0 * PI fun Float.toDegree() = this * 180.0f / PI_FLOAT - fun Double.toDegree() = this * 180.0 / PI fun Boolean.toInt() = if (this) 1 else 0 - fun Boolean.toIntSign() = if (this) 1 else -1 fun Double.floorToInt() = floor(this).toInt() - fun Double.ceilToInt() = ceil(this).toInt() fun T.roundToStep(step: T): T { @@ -46,18 +44,18 @@ object MathUtils { if (stepD == 0.0) return this var value = round(toDouble() / stepD) * stepD - value = value.roundToPlaces(decimalPlaces(stepD)) + value = value.roundToPlaces(stepD.decimals) if (abs(value) == 0.0) value = 0.0 return typeConvert(value) } - fun Vec2d.roundToStep(step: Double): Vec2d = - Vec2d(x.roundToStep(step), y.roundToStep(step)) - - fun Double.roundToPlaces(places: Int) = + private fun Double.roundToPlaces(places: Int) = BigDecimal(this).setScale(places, RoundingMode.HALF_EVEN).toDouble() + private val Double.decimals: Int + get() = BigDecimal.valueOf(this).scale() + fun T.typeConvert(valueIn: Double): T { @Suppress("UNCHECKED_CAST") return when (this) { @@ -71,7 +69,8 @@ object MathUtils { } as T } - private fun decimalPlaces(value: Double) = BigDecimal.valueOf(value).scale() + fun Vec2d.roundToStep(step: Double) = + Vec2d(x.roundToStep(step), y.roundToStep(step)) fun random(v1: Double, v2: Double): Double { if (v1 == v2) return v1 @@ -79,8 +78,4 @@ object MathUtils { val max = max(v1, v2) return nextDouble(min, max) } - - fun Int.nextPowerOf2() = 2f.pow(ceil(log2(toFloat()))).toInt() - - inline val Int.sq: Int get() = this * this } diff --git a/common/src/main/kotlin/com/lambda/util/math/Rect.kt b/common/src/main/kotlin/com/lambda/util/math/Rect.kt index c053d3d41..bc94d0559 100644 --- a/common/src/main/kotlin/com/lambda/util/math/Rect.kt +++ b/common/src/main/kotlin/com/lambda/util/math/Rect.kt @@ -34,8 +34,8 @@ data class Rect(private val pos1: Vec2d, private val pos2: Vec2d) { val size get() = Vec2d(right - left, bottom - top) val center get() = lerp(0.5, pos1, pos2) - operator fun plus(vec2d: Vec2d) = Rect(pos1 + vec2d, pos2 + vec2d) - operator fun minus(vec2d: Vec2d) = Rect(pos1 - vec2d, pos2 - vec2d) + infix operator fun plus(vec2d: Vec2d) = Rect(pos1 + vec2d, pos2 + vec2d) + infix operator fun minus(vec2d: Vec2d) = Rect(pos1 - vec2d, pos2 - vec2d) fun moveFirst(vec2d: Vec2d) = Rect(pos1 + vec2d, pos2) fun moveSecond(vec2d: Vec2d) = Rect(pos1, pos2 + vec2d) diff --git a/common/src/main/kotlin/com/lambda/util/math/Vec2d.kt b/common/src/main/kotlin/com/lambda/util/math/Vec2d.kt index 9a6038f87..89614d23e 100644 --- a/common/src/main/kotlin/com/lambda/util/math/Vec2d.kt +++ b/common/src/main/kotlin/com/lambda/util/math/Vec2d.kt @@ -20,28 +20,36 @@ package com.lambda.util.math import kotlin.math.roundToInt data class Vec2d(val x: Double, val y: Double) { - constructor(x: Int, y: Int) : this(x.toDouble(), y.toDouble()) constructor(x: Float, y: Float) : this(x.toDouble(), y.toDouble()) + constructor(x: Int, y: Int) : this(x.toDouble(), y.toDouble()) + + operator fun unaryMinus() = Vec2d(-x, -y) - operator fun unaryPlus() = this - operator fun plus(vec2d: Vec2d) = plus(vec2d.x, vec2d.y) - operator fun plus(add: Double) = plus(add, add) fun plus(x: Double, y: Double) = Vec2d(this.x + x, this.y + y) + infix operator fun plus(other: Vec2d) = plus(other.x, other.y) + infix operator fun plus(other: Double) = plus(other, other) + infix operator fun plus(other: Float): Vec2d = plus(other.toDouble(), other.toDouble()) + infix operator fun plus(other: Int): Vec2d = plus(other.toDouble(), other.toDouble()) - operator fun unaryMinus() = Vec2d(-x, -y) - operator fun minus(vec2d: Vec2d) = minus(vec2d.x, vec2d.y) - operator fun minus(sub: Double) = minus(sub, sub) - fun minus(x: Double, y: Double) = plus(-x, -y) + fun minus(x: Double, y: Double) = Vec2d(this.x - x, this.y - y) + infix operator fun minus(other: Vec2d): Vec2d = minus(other.x, other.y) + infix operator fun minus(other: Double): Vec2d = minus(other, other) + infix operator fun minus(other: Float): Vec2d = minus(other.toDouble(), other.toDouble()) + infix operator fun minus(other: Int): Vec2d = minus(other.toDouble(), other.toDouble()) - operator fun times(vec2d: Vec2d) = times(vec2d.x, vec2d.y) - operator fun times(multiplier: Double) = times(multiplier, multiplier) fun times(x: Double, y: Double) = Vec2d(this.x * x, this.y * y) + infix operator fun times(other: Double) = times(other, other) + infix operator fun times(other: Vec2d) = times(other.x, other.y) + infix operator fun times(other: Float): Vec2d = times(other.toDouble(), other.toDouble()) + infix operator fun times(other: Int): Vec2d = times(other.toDouble(), other.toDouble()) - operator fun div(vec2d: Vec2d) = div(vec2d.x, vec2d.y) - operator fun div(divider: Double) = div(divider, divider) fun div(x: Double, y: Double) = Vec2d(this.x / x, this.y / y) + infix operator fun div(other: Vec2d) = div(other.x, other.y) + infix operator fun div(other: Double) = div(other, other) + infix operator fun div(other: Float): Vec2d = div(other.toDouble(), other.toDouble()) + infix operator fun div(other: Int): Vec2d = div(other.toDouble(), other.toDouble()) - fun roundToInt(): Vec2d = Vec2d(this.x.roundToInt(), this.y.roundToInt()) + fun roundToInt() = Vec2d(x.roundToInt(), y.roundToInt()) companion object { val ZERO = Vec2d(0.0, 0.0) diff --git a/common/src/main/kotlin/com/lambda/util/math/VecUtils.kt b/common/src/main/kotlin/com/lambda/util/math/VecUtils.kt deleted file mode 100644 index 7ef9db79d..000000000 --- a/common/src/main/kotlin/com/lambda/util/math/VecUtils.kt +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright 2024 Lambda - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package com.lambda.util.math - -import com.lambda.util.math.MathUtils.sq -import net.minecraft.entity.Entity -import net.minecraft.util.math.* -import kotlin.math.pow -import kotlin.math.roundToInt -import kotlin.math.sqrt - -object VecUtils { - val Vec3d.blockPos: BlockPos - get() = BlockPos(x.roundToInt(), y.roundToInt(), z.roundToInt()) - - val Vec3i.vec3d get() = Vec3d.of(this) - - fun BlockPos.getHitVec(side: Direction) = - vec3d + side.hitVecOffset - - val Direction.hitVecOffset - get() = - CENTER + vector.vec3d * 0.5 - - fun EightWayDirection.rotateClockwise(steps: Int) = - EightWayDirection.entries[(ordinal + steps) % 8] - - fun Vec3d.approximate(other: Vec3d, precision: Double = 2.0E-4): Boolean = - (subtract(other) distSq Vec3d.ZERO) > precision.pow(2) - - infix fun Vec3d.dist(other: Vec3d): Double = sqrt(this distSq other) - infix fun Vec3d.dist(other: Vec3i): Double = sqrt(this distSq other) - infix fun Vec3d.distSq(other: Vec3d): Double = this.squaredDistanceTo(other) - infix fun Vec3d.distSq(other: Vec3i): Double = - this.squaredDistanceTo(other.x.toDouble(), other.y.toDouble(), other.z.toDouble()) - - infix operator fun Vec3d.plus(other: Vec3d): Vec3d = this.add(other) - infix operator fun Vec3d.minus(other: Vec3d): Vec3d = this.subtract(other) - infix operator fun Vec3d.times(other: Vec3d): Vec3d = this.multiply(other) - infix operator fun Vec3d.div(other: Vec3d): Vec3d = this.multiply(1.0 / other.x, 1.0 / other.y, 1.0 / other.z) - infix operator fun Vec3d.times(other: Double): Vec3d = this.multiply(other) - infix operator fun Vec3d.div(other: Double): Vec3d = this.multiply(1.0 / other) - - infix fun Vec3i.dist(other: Vec3d): Double = sqrt(this distSq other) - infix fun Vec3i.dist(other: Vec3i): Double = sqrt((this distSq other).toDouble()) - infix fun Vec3i.distSq(other: Vec3d): Double = this.getSquaredDistance(other) - infix fun Vec3i.distSq(other: Vec3i): Int = (this.x - other.x).sq + (this.y - other.y).sq + (this.z - other.z).sq - - infix fun Entity.dist(other: Vec3d): Double = pos dist other - infix fun Entity.dist(other: Vec3i): Double = blockPos dist other - infix fun Entity.dist(other: Entity): Double = distanceTo(other).toDouble() - infix fun Entity.distSq(other: Vec3d): Double = this.pos distSq other - infix fun Entity.distSq(other: Vec3i): Int = this.blockPos distSq other - infix fun Entity.distSq(other: Entity): Double = squaredDistanceTo(other) - - val UP = Vec3d(0.0, 1.0, 0.0) - val DOWN = Vec3d(0.0, -1.0, 0.0) - val CENTER = Vec3d(0.5, 0.5, 0.5) -} diff --git a/common/src/main/kotlin/com/lambda/util/math/Vectors.kt b/common/src/main/kotlin/com/lambda/util/math/Vectors.kt new file mode 100644 index 000000000..823b54328 --- /dev/null +++ b/common/src/main/kotlin/com/lambda/util/math/Vectors.kt @@ -0,0 +1,117 @@ +/* + * Copyright 2024 Lambda + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.lambda.util.math + +import com.lambda.util.math.MathUtils.sq +import net.minecraft.entity.Entity +import net.minecraft.util.math.* +import kotlin.math.pow +import kotlin.math.roundToInt +import kotlin.math.sqrt + +/* Direction */ +val Direction.hitVecOffset: Vec3d get() = CENTER + vector * 0.5 + +fun EightWayDirection.rotateClockwise(steps: Int) = + EightWayDirection.entries[(ordinal + steps) % 8] + +/* Vec2f */ +operator fun Vec2f.component1() = x +operator fun Vec2f.component2() = y + +/* Vec3d */ +fun Vec3d.approximate(other: Vec3d, precision: Double = 2.0E-4): Boolean = + (subtract(other) distSq Vec3d.ZERO) > precision.pow(2) + +val Vec3d.blockPos: BlockPos + get() = BlockPos(x.roundToInt(), y.roundToInt(), z.roundToInt()) + +fun Vec3d.interpolate(value: Double, max: Vec3d) = lerp(value, this, max) + +operator fun Vec3d.component1() = x +operator fun Vec3d.component2() = y +operator fun Vec3d.component3() = z + +infix fun Vec3d.dist(other: Vec3d): Double = sqrt(this distSq other) +infix fun Vec3d.dist(other: Vec3i): Double = sqrt(this distSq other) +infix fun Vec3d.distSq(other: Vec3d): Double = squaredDistanceTo(other) +infix fun Vec3d.distSq(other: Vec3i): Double = + squaredDistanceTo(other.x.toDouble(), other.y.toDouble(), other.z.toDouble()) + +infix operator fun Vec3d.plus(other: Vec3d): Vec3d = add(other) +infix operator fun Vec3d.plus(other: Vec3i): Vec3d = Vec3d(x + other.x, y + other.y, z + other.z) +infix operator fun Vec3d.plus(other: Double): Vec3d = add(other, other, other) +infix operator fun Vec3d.plus(other: Float): Vec3d = add(other.toDouble(), other.toDouble(), other.toDouble()) +infix operator fun Vec3d.plus(other: Int): Vec3d = add(other.toDouble(), other.toDouble(), other.toDouble()) +infix operator fun Vec3d.minus(other: Vec3d): Vec3d = subtract(other) +infix operator fun Vec3d.minus(other: Vec3i): Vec3d = Vec3d(x - other.x, y - other.y, z - other.z) +infix operator fun Vec3d.minus(other: Double): Vec3d = subtract(other, other, other) +infix operator fun Vec3d.minus(other: Float): Vec3d = subtract(other.toDouble(), other.toDouble(), other.toDouble()) +infix operator fun Vec3d.minus(other: Int): Vec3d = subtract(other.toDouble(), other.toDouble(), other.toDouble()) +infix operator fun Vec3d.times(other: Vec3d): Vec3d = multiply(other) +infix operator fun Vec3d.times(other: Vec3i): Vec3d = Vec3d(x * other.x, y * other.y, z * other.z) +infix operator fun Vec3d.times(other: Double): Vec3d = multiply(other) +infix operator fun Vec3d.times(other: Float): Vec3d = multiply(other.toDouble()) +infix operator fun Vec3d.times(other: Int): Vec3d = multiply(other.toDouble()) +infix operator fun Vec3d.div(other: Vec3d): Vec3d = multiply(1.0 / other.x, 1.0 / other.y, 1.0 / other.z) +infix operator fun Vec3d.div(other: Vec3i): Vec3d = Vec3d(x / other.x, y / other.y, z / other.z) +infix operator fun Vec3d.div(other: Double): Vec3d = times(1 / other) +infix operator fun Vec3d.div(other: Float): Vec3d = times(1 / other) +infix operator fun Vec3d.div(other: Int): Vec3d = times(1 / other) + +/* Vec3i */ +fun BlockPos.getHitVec(side: Direction): Vec3d = + side.hitVecOffset + this + +infix fun Vec3i.dist(other: Vec3d): Double = sqrt(this distSq other) +infix fun Vec3i.dist(other: Vec3i): Double = sqrt((this distSq other).toDouble()) +infix fun Vec3i.distSq(other: Vec3d): Double = getSquaredDistance(other) +infix fun Vec3i.distSq(other: Vec3i): Int = (x - other.x).sq + (y - other.y).sq + (z - other.z).sq + +infix operator fun Vec3i.plus(other: Vec3d): Vec3i = Vec3i(x + other.x.toInt(), y + other.y.toInt(), z + other.z.toInt()) +infix operator fun Vec3i.plus(other: Vec3i): Vec3i = add(other) +infix operator fun Vec3i.plus(other: Double): Vec3i = add(other.toInt(), other.toInt(), other.toInt()) +infix operator fun Vec3i.plus(other: Float): Vec3i = add(other.toInt(), other.toInt(), other.toInt()) +infix operator fun Vec3i.plus(other: Int): Vec3i = add(other, other, other) +infix operator fun Vec3i.minus(other: Vec3d): Vec3i = Vec3i(x - other.x.toInt(), y - other.y.toInt(), z - other.z.toInt()) +infix operator fun Vec3i.minus(other: Vec3i): Vec3i = subtract(other) +infix operator fun Vec3i.minus(other: Double): Vec3i = add(-other.toInt(), -other.toInt(), -other.toInt()) +infix operator fun Vec3i.minus(other: Float): Vec3i = add(-other.toInt(), -other.toInt(), -other.toInt()) +infix operator fun Vec3i.minus(other: Int): Vec3i = add(-other, -other, -other) +infix operator fun Vec3i.times(other: Vec3d): Vec3i = Vec3i(x * other.x.toInt(), y * other.y.toInt(), z * other.z.toInt()) +infix operator fun Vec3i.times(other: Vec3i): Vec3i = Vec3i(x * other.x, y * other.y, z * other.z) +infix operator fun Vec3i.times(other: Double): Vec3i = multiply(other.toInt()) +infix operator fun Vec3i.times(other: Float): Vec3i = multiply(other.toInt()) +infix operator fun Vec3i.times(other: Int): Vec3i = multiply(other) +infix operator fun Vec3i.div(other: Vec3d): Vec3i = Vec3i((x / other.x).toInt(), (y / other.y).toInt(), (z / other.z).toInt()) +infix operator fun Vec3i.div(other: Vec3i): Vec3i = Vec3i(x / other.x, y / other.y, z / other.z) +infix operator fun Vec3i.div(other: Double): Vec3i = times(1 / other) +infix operator fun Vec3i.div(other: Float): Vec3i = times(1 / other) +infix operator fun Vec3i.div(other: Int): Vec3i = times(1 / other) + +/* Entity */ +infix fun Entity.dist(other: Vec3d): Double = pos dist other +infix fun Entity.dist(other: Vec3i): Double = blockPos dist other +infix fun Entity.dist(other: Entity): Double = distanceTo(other).toDouble() +infix fun Entity.distSq(other: Vec3d): Double = pos distSq other +infix fun Entity.distSq(other: Vec3i): Int = blockPos distSq other +infix fun Entity.distSq(other: Entity): Double = squaredDistanceTo(other) + +val UP = Vec3d(0.0, 1.0, 0.0) +val DOWN = Vec3d(0.0, -1.0, 0.0) +val CENTER = Vec3d(0.5, 0.5, 0.5) diff --git a/common/src/main/kotlin/com/lambda/util/player/MovementUtils.kt b/common/src/main/kotlin/com/lambda/util/player/MovementUtils.kt index 28a6eb73e..7231c501d 100644 --- a/common/src/main/kotlin/com/lambda/util/player/MovementUtils.kt +++ b/common/src/main/kotlin/com/lambda/util/player/MovementUtils.kt @@ -22,8 +22,8 @@ import com.lambda.interaction.RotationManager import com.lambda.util.math.MathUtils.toDegree import com.lambda.util.math.MathUtils.toInt import com.lambda.util.math.MathUtils.toRadian -import com.lambda.util.math.VecUtils.plus -import com.lambda.util.math.VecUtils.times +import com.lambda.util.math.plus +import com.lambda.util.math.times import net.minecraft.client.input.Input import net.minecraft.client.input.KeyboardInput import net.minecraft.client.network.ClientPlayerEntity diff --git a/common/src/main/kotlin/com/lambda/util/player/prediction/PredictionEntity.kt b/common/src/main/kotlin/com/lambda/util/player/prediction/PredictionEntity.kt index 79cdc273b..7ce175c09 100644 --- a/common/src/main/kotlin/com/lambda/util/player/prediction/PredictionEntity.kt +++ b/common/src/main/kotlin/com/lambda/util/player/prediction/PredictionEntity.kt @@ -23,11 +23,11 @@ import com.lambda.interaction.rotation.Rotation import com.lambda.threading.runSafe import com.lambda.util.BlockUtils.blockState import com.lambda.util.BlockUtils.flooredPos +import com.lambda.util.math.DOWN import com.lambda.util.math.MathUtils.toIntSign import com.lambda.util.math.MathUtils.toRadian -import com.lambda.util.math.VecUtils -import com.lambda.util.math.VecUtils.plus -import com.lambda.util.math.VecUtils.times +import com.lambda.util.math.plus +import com.lambda.util.math.times import com.lambda.util.player.MovementUtils.motion import com.lambda.util.player.MovementUtils.moveYaw import com.lambda.util.player.MovementUtils.movementVector @@ -83,7 +83,7 @@ class PredictionEntity(val player: ClientPlayerEntity) { // Other shit private var jumpingCooldown = player.jumpingCooldown - private var velocityAffectingPos = player.supportingBlockPos.orElse((position + VecUtils.DOWN * 0.001).flooredPos) + private var velocityAffectingPos = player.supportingBlockPos.orElse((position + DOWN * 0.001).flooredPos) private var horizontalCollision = player.horizontalCollision private var verticalCollision = player.verticalCollision @@ -153,7 +153,7 @@ class PredictionEntity(val player: ClientPlayerEntity) { applyMovementInput(travelVec, slipperiness) move() - motion += VecUtils.DOWN * gravity + motion += DOWN * gravity motion *= Vec3d(friction, 0.98, friction) } @@ -214,7 +214,7 @@ class PredictionEntity(val player: ClientPlayerEntity) { boundingBox = normalized.offset(position) } - velocityAffectingPos = (position + VecUtils.DOWN * 0.001).flooredPos + velocityAffectingPos = (position + DOWN * 0.001).flooredPos } /** @see net.minecraft.entity.LivingEntity.jump */ diff --git a/common/src/main/kotlin/com/lambda/util/world/WorldDsl.kt b/common/src/main/kotlin/com/lambda/util/world/WorldDsl.kt index b37145069..ded36d784 100644 --- a/common/src/main/kotlin/com/lambda/util/world/WorldDsl.kt +++ b/common/src/main/kotlin/com/lambda/util/world/WorldDsl.kt @@ -19,7 +19,7 @@ package com.lambda.util.world import com.lambda.context.SafeContext import com.lambda.core.annotations.InternalApi -import com.lambda.util.math.VecUtils.distSq +import com.lambda.util.math.distSq import com.lambda.util.world.WorldUtils.internalGetBlockEntities import com.lambda.util.world.WorldUtils.internalGetEntities import com.lambda.util.world.WorldUtils.internalGetFastEntities diff --git a/common/src/main/kotlin/com/lambda/util/world/raycast/RayCastUtils.kt b/common/src/main/kotlin/com/lambda/util/world/raycast/RayCastUtils.kt index 0196f6271..b523f3361 100644 --- a/common/src/main/kotlin/com/lambda/util/world/raycast/RayCastUtils.kt +++ b/common/src/main/kotlin/com/lambda/util/world/raycast/RayCastUtils.kt @@ -21,7 +21,7 @@ import com.lambda.Lambda.mc import com.lambda.context.SafeContext import com.lambda.interaction.rotation.Rotation import com.lambda.threading.runSafe -import com.lambda.util.math.VecUtils.distSq +import com.lambda.util.math.distSq import net.minecraft.client.network.ClientPlayerEntity import net.minecraft.entity.Entity import net.minecraft.entity.projectile.ProjectileUtil