Skip to content

Commit 26b9e7d

Browse files
committed
Targeting Config
1 parent 08f880a commit 26b9e7d

File tree

3 files changed

+113
-1
lines changed

3 files changed

+113
-1
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package com.lambda.config.groups
2+
3+
import com.lambda.config.Configurable
4+
import com.lambda.context.SafeContext
5+
import com.lambda.interaction.rotation.Rotation.Companion.dist
6+
import com.lambda.interaction.rotation.Rotation.Companion.rotation
7+
import com.lambda.interaction.rotation.Rotation.Companion.rotationTo
8+
import com.lambda.threading.runSafe
9+
import com.lambda.util.math.VecUtils.distSq
10+
import com.lambda.util.world.WorldUtils.getFastEntities
11+
import net.minecraft.client.network.ClientPlayerEntity
12+
import net.minecraft.entity.LivingEntity
13+
import net.minecraft.entity.mob.MobEntity
14+
import net.minecraft.entity.passive.PassiveEntity
15+
16+
abstract class Targeting(
17+
c: Configurable,
18+
vis: () -> Boolean = { true },
19+
defaultRange: Double,
20+
maxRange: Double
21+
) : TargetingConfig {
22+
override val targetingRange by c.setting("Targeting Range", defaultRange, 1.0..maxRange, 0.05) { vis() }
23+
24+
override val players by c.setting("Players", true) { vis() }
25+
private val mobs by c.setting("Mobs", true) { vis() }
26+
private val hostilesSetting by c.setting("Hostiles", true) { vis() && mobs }
27+
private val animalsSetting by c.setting("Animals", true) { vis() && mobs }
28+
override val hostiles get() = mobs && hostilesSetting
29+
override val animals get() = mobs && animalsSetting
30+
31+
override val invisible by c.setting("Invisible", true) { vis() }
32+
override val dead by c.setting("Dead", false) { vis() }
33+
34+
fun getEntities(): List<LivingEntity> =
35+
mutableListOf<LivingEntity>().apply {
36+
runSafe {
37+
getFastEntities(
38+
player.pos, targetingRange, this@apply,
39+
predicate = { entity -> validate(player, entity) }
40+
)
41+
}
42+
}
43+
44+
open fun validate(player: ClientPlayerEntity, entity: LivingEntity) = when {
45+
!players && entity.isPlayer -> false
46+
!animals && entity is PassiveEntity -> false
47+
!hostiles && entity is MobEntity -> false
48+
49+
!invisible && entity.isInvisibleTo(player) -> false
50+
!dead && entity.isDead -> false
51+
52+
else -> true
53+
}
54+
55+
class Combat(
56+
c: Configurable,
57+
vis: () -> Boolean = { true },
58+
) : Targeting(c, vis, 5.0, 16.0) {
59+
val fov by c.setting("FOV Limit", 180, 5..180, 1) { vis() }
60+
val priority by c.setting("Priority", Priority.DISTANCE) { vis() }
61+
62+
override fun validate(player: ClientPlayerEntity, entity: LivingEntity): Boolean {
63+
if (fov < 180 && player.rotation dist player.eyePos.rotationTo(entity.pos) > fov) return false
64+
return super.validate(player, entity)
65+
}
66+
67+
fun getTarget(): LivingEntity? = runSafe {
68+
var best: LivingEntity? = null
69+
var bestFactor = Double.MAX_VALUE
70+
71+
val comparator = { entity: LivingEntity, _: Int ->
72+
val factor = priority.factor(this, entity)
73+
if (factor < bestFactor) {
74+
best = entity
75+
bestFactor = factor
76+
}
77+
}
78+
79+
val predicate = { entity: LivingEntity ->
80+
validate(player, entity)
81+
}
82+
83+
getFastEntities<LivingEntity>(player.pos, targetingRange, null, comparator, predicate)
84+
85+
return@runSafe best
86+
}
87+
}
88+
89+
class ESP(
90+
c: Configurable,
91+
vis: () -> Boolean = { true },
92+
) : Targeting(c, vis, 128.0, 1024.0)
93+
94+
@Suppress("Unused")
95+
enum class Priority(val factor: SafeContext.(LivingEntity) -> Double) {
96+
DISTANCE({ player.pos distSq it.pos }),
97+
HEALTH({ it.health.toDouble() }),
98+
FOV({ player.rotation dist player.eyePos.rotationTo(it.pos) })
99+
}
100+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.lambda.config.groups
2+
3+
interface TargetingConfig {
4+
val targetingRange: Double
5+
6+
val players: Boolean
7+
val hostiles: Boolean
8+
val animals: Boolean
9+
10+
val invisible: Boolean
11+
val dead: Boolean
12+
}

common/src/main/kotlin/com/lambda/interaction/rotation/Rotation.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ data class Rotation(val yaw: Double, val pitch: Double) {
109109
return Rotation(yaw, pitch)
110110
}
111111

112-
fun Rotation.distance(b: Rotation) =
112+
infix fun Rotation.dist(b: Rotation) =
113113
hypot(
114114
wrap(yaw - b.yaw),
115115
wrap(pitch - b.pitch)

0 commit comments

Comments
 (0)