Skip to content

Commit c48a2c4

Browse files
committed
added more vector distance functions
1 parent 3cd6ebd commit c48a2c4

File tree

2 files changed

+31
-20
lines changed

2 files changed

+31
-20
lines changed

common/src/main/kotlin/com/lambda/util/combat/Explosion.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,17 @@ import com.lambda.context.SafeContext
2323
import com.lambda.core.annotations.InternalApi
2424
import com.lambda.util.BlockUtils.blockState
2525
import com.lambda.util.BlockUtils.fluidState
26+
import com.lambda.util.math.VecUtils.dist
2627
import com.lambda.util.math.VecUtils.minus
2728
import com.lambda.util.math.VecUtils.times
29+
import com.lambda.util.math.VecUtils.vec3d
2830
import com.lambda.util.world.WorldUtils.internalGetFastEntities
2931
import com.lambda.util.world.toFastVec
3032
import net.minecraft.enchantment.ProtectionEnchantment
3133
import net.minecraft.entity.LivingEntity
3234
import net.minecraft.util.math.BlockPos
3335
import net.minecraft.util.math.Vec3d
36+
import net.minecraft.util.math.Vec3i
3437
import net.minecraft.world.explosion.Explosion
3538
import kotlin.math.max
3639

@@ -44,6 +47,16 @@ object Explosion {
4447
fun SafeContext.explosionDamage(source: Explosion, entity: LivingEntity) =
4548
explosionDamage(source.position, entity, source.power.toDouble())
4649

50+
/**
51+
* Calculates the damage dealt by an explosion to a living entity.
52+
* @param position The position of the explosion.
53+
* @param entity The entity to calculate the damage for.
54+
* @param power The strength of the explosion above 0.
55+
* @return The damage dealt by the explosion.
56+
*/
57+
fun SafeContext.explosionDamage(position: Vec3i, entity: LivingEntity, power: Double): Double =
58+
explosionDamage(position.vec3d, entity, power)
59+
4760
/**
4861
* Calculates the damage dealt by an explosion to a living entity.
4962
* @param position The position of the explosion.
@@ -52,7 +65,7 @@ object Explosion {
5265
* @return The damage dealt by the explosion.
5366
*/
5467
fun SafeContext.explosionDamage(position: Vec3d, entity: LivingEntity, power: Double): Double {
55-
val distance = entity.pos.distanceTo(position)
68+
val distance = entity dist position
5669

5770
val impact = (1.0 - distance / (power * 2.0)) *
5871
Explosion.getExposure(position, entity) *

common/src/main/kotlin/com/lambda/util/math/VecUtils.kt

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import net.minecraft.util.math.Vec3d
2525
import net.minecraft.util.math.Vec3i
2626
import kotlin.math.pow
2727
import kotlin.math.roundToInt
28+
import kotlin.math.sqrt
2829

2930
object VecUtils {
3031
val Vec3d.blockPos: BlockPos
@@ -39,33 +40,30 @@ object VecUtils {
3940
get() =
4041
CENTER + vector.vec3d * 0.5
4142

42-
infix fun Vec3d.dist(other: Vec3d): Double = this.distanceTo(other)
43-
43+
fun Vec3d.approximate(other: Vec3d, precision: Double = 2.0E-4): Boolean = (subtract(other) distSq Vec3d.ZERO) > precision.pow(2)
44+
infix fun Vec3d.dist(other: Vec3d): Double = sqrt(this distSq other)
45+
infix fun Vec3d.dist(other: Vec3i): Double = sqrt(this distSq other)
4446
infix fun Vec3d.distSq(other: Vec3d): Double = this.squaredDistanceTo(other)
45-
46-
fun Vec3d.approximate(other: Vec3d, precision: Double = 2.0E-4): Boolean =
47-
(subtract(other) distSq Vec3d.ZERO) > precision.pow(2)
48-
49-
infix fun Vec3i.distSq(other: Vec3d): Double = Vec3d.of(this) distSq other
50-
51-
infix fun Vec3i.distSq(other: Vec3i): Int = (this.x - other.x).sq + (this.y - other.y).sq + (this.z - other.z).sq
52-
53-
infix fun Entity.distSq(other: Vec3d): Double = this.pos distSq other
54-
55-
infix fun Entity.distSq(other: Vec3i): Int = this.blockPos distSq other
56-
47+
infix fun Vec3d.distSq(other: Vec3i): Double = this.squaredDistanceTo(other.x.toDouble(), other.y.toDouble(), other.z.toDouble())
5748
infix operator fun Vec3d.plus(other: Vec3d): Vec3d = this.add(other)
58-
5949
infix operator fun Vec3d.minus(other: Vec3d): Vec3d = this.subtract(other)
60-
6150
infix operator fun Vec3d.times(other: Vec3d): Vec3d = this.multiply(other)
62-
6351
infix operator fun Vec3d.div(other: Vec3d): Vec3d = this.multiply(1.0 / other.x, 1.0 / other.y, 1.0 / other.z)
64-
6552
infix operator fun Vec3d.times(other: Double): Vec3d = this.multiply(other)
66-
6753
infix operator fun Vec3d.div(other: Double): Vec3d = this.multiply(1.0 / other)
6854

55+
infix fun Vec3i.dist(other: Vec3d): Double = sqrt(this distSq other)
56+
infix fun Vec3i.dist(other: Vec3i): Double = sqrt((this distSq other).toDouble())
57+
infix fun Vec3i.distSq(other: Vec3d): Double = this.getSquaredDistance(other)
58+
infix fun Vec3i.distSq(other: Vec3i): Int = (this.x - other.x).sq + (this.y - other.y).sq + (this.z - other.z).sq
59+
60+
infix fun Entity.dist(other: Vec3d): Double = pos dist other
61+
infix fun Entity.dist(other: Vec3i): Double = blockPos dist other
62+
infix fun Entity.dist(other: Entity): Double = distanceTo(other).toDouble()
63+
infix fun Entity.distSq(other: Vec3d): Double = this.pos distSq other
64+
infix fun Entity.distSq(other: Vec3i): Int = this.blockPos distSq other
65+
infix fun Entity.distSq(other: Entity): Double = squaredDistanceTo(other)
66+
6967
val UP = Vec3d(0.0, 1.0, 0.0)
7068
val DOWN = Vec3d(0.0, -1.0, 0.0)
7169
val CENTER = Vec3d(0.5, 0.5, 0.5)

0 commit comments

Comments
 (0)