Skip to content

Commit

Permalink
Added cache to player simulation (CCBlueX#3759)
Browse files Browse the repository at this point in the history
  • Loading branch information
superblaubeere27 authored Aug 22, 2024
1 parent eb0f846 commit ea5cb13
Show file tree
Hide file tree
Showing 9 changed files with 197 additions and 61 deletions.
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
16 changes: 8 additions & 8 deletions gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
Expand Down Expand Up @@ -145,15 +145,15 @@ if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
case $MAX_FD in #(
max*)
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
# shellcheck disable=SC2039,SC3045
MAX_FD=$( ulimit -H -n ) ||
warn "Could not query maximum file descriptor limit"
esac
case $MAX_FD in #(
'' | soft) :;; #(
*)
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
# shellcheck disable=SC2039,SC3045
ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD"
esac
Expand Down Expand Up @@ -202,11 +202,11 @@ fi
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'

# Collect all arguments for the java command;
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
# shell script including quotes and variable substitutions, so put them in
# double quotes to make sure that they get re-expanded; and
# * put everything else in single quotes, so that it's not re-expanded.
# Collect all arguments for the java command:
# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
# and any embedded shellness will be escaped.
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
# treated as '${Hostname}' itself on the command line.

set -- \
"-Dorg.gradle.appname=$APP_BASE_NAME" \
Expand Down
20 changes: 10 additions & 10 deletions gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if %ERRORLEVEL% equ 0 goto execute

echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
echo. 1>&2
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2

goto fail

Expand All @@ -57,11 +57,11 @@ set JAVA_EXE=%JAVA_HOME%/bin/java.exe

if exist "%JAVA_EXE%" goto execute

echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
echo. 1>&2
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2

goto fail

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ object FakeLag : Listenable {
val arrows = ModuleAutoDodge.findFlyingArrows(world)
val playerSimulation = RigidPlayerSimulation(pos)

return ModuleAutoDodge.getInflictedHits(playerSimulation, arrows, maxTicks = 40) { }
return ModuleAutoDodge.getInflictedHits(playerSimulation, arrows, maxTicks = 40)
}

enum class LagResult {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import net.ccbluex.liquidbounce.event.handler
import net.ccbluex.liquidbounce.event.repeatable
import net.ccbluex.liquidbounce.features.module.Category
import net.ccbluex.liquidbounce.features.module.Module
import net.ccbluex.liquidbounce.features.module.modules.render.murdermystery.ModuleMurderMystery
import net.ccbluex.liquidbounce.render.renderEnvironmentForGUI
import net.ccbluex.liquidbounce.utils.aiming.Rotation
import net.ccbluex.liquidbounce.utils.aiming.RotationManager
Expand Down Expand Up @@ -180,12 +179,12 @@ object ModuleAutoBow : Module("AutoBow", Category.COMBAT, aliases = arrayOf("Bow
arrow.tick()

players.forEach { (entity, player) ->
player.tick()
val playerSnapshot = player.getSnapshotAt(i)

val playerHitBox =
Box(-0.3, 0.0, -0.3, 0.3, 1.8, 0.3)
.expand(0.3)
.offset(player.pos)
.offset(playerSnapshot.pos)

val raycastResult = playerHitBox.raycast(lastPos, arrow.pos)

Expand All @@ -198,12 +197,12 @@ object ModuleAutoBow : Module("AutoBow", Category.COMBAT, aliases = arrayOf("Bow
return null
}

fun findAndBuildSimulatedPlayers(): List<Pair<AbstractClientPlayerEntity, SimulatedPlayer>> {
fun findAndBuildSimulatedPlayers(): List<Pair<AbstractClientPlayerEntity, SimulatedPlayerCache>> {
return world.players.filter {
it != player &&
Line(player.pos, player.rotationVector).squaredDistanceTo(it.pos) < 10.0 * 10.0
}.map {
Pair(it, SimulatedPlayer.fromOtherPlayer(it, SimulatedPlayer.SimulatedPlayerInput.guessInput(it)))
Pair(it, PlayerSimulationCache.getSimulationForOtherPlayers(it))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import net.ccbluex.liquidbounce.render.engine.Color4b
import net.ccbluex.liquidbounce.render.renderEnvironmentForWorld
import net.ccbluex.liquidbounce.render.withColor
import net.ccbluex.liquidbounce.utils.combat.findEnemy
import net.ccbluex.liquidbounce.utils.entity.SimulatedPlayer
import net.ccbluex.liquidbounce.utils.entity.PlayerSimulationCache
import net.ccbluex.liquidbounce.utils.math.toVec3
import net.minecraft.network.packet.s2c.play.PlayerPositionLookS2CPacket
import net.minecraft.util.math.Vec3d
Expand Down Expand Up @@ -149,8 +149,7 @@ internal object ModuleTickBase : Module("TickBase", Category.COMBAT) {

tickBuffer.clear()

val input = SimulatedPlayer.SimulatedPlayerInput.fromClientPlayer(event.directionalInput)
val simulatedPlayer = SimulatedPlayer.fromClientPlayer(input)
val simulatedPlayer = PlayerSimulationCache.getSimulationForLocalPlayer()

if (tickBalance <= 0) {
reachedTheLimit = true
Expand All @@ -166,13 +165,15 @@ internal object ModuleTickBase : Module("TickBase", Category.COMBAT) {
return@handler
}

repeat(min(tickBalance.toInt(), maxTicksAtATime)) {
simulatedPlayer.tick()
val tickRange = 0 until min(tickBalance.toInt(), maxTicksAtATime)
val snapshots = simulatedPlayer.getSnapshotsBetween(tickRange)

snapshots.forEach {
tickBuffer += TickData(
simulatedPlayer.pos,
simulatedPlayer.fallDistance,
simulatedPlayer.velocity,
simulatedPlayer.onGround
it.pos,
it.fallDistance,
it.velocity,
it.onGround
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ import net.ccbluex.liquidbounce.features.module.modules.render.murdermystery.Mod
import net.ccbluex.liquidbounce.features.module.modules.world.scaffold.ModuleScaffold
import net.ccbluex.liquidbounce.utils.client.EventScheduler
import net.ccbluex.liquidbounce.utils.client.Timer
import net.ccbluex.liquidbounce.utils.entity.CachedPlayerSimulation
import net.ccbluex.liquidbounce.utils.entity.PlayerSimulation
import net.ccbluex.liquidbounce.utils.entity.PlayerSimulationCache
import net.ccbluex.liquidbounce.utils.entity.SimulatedArrow
import net.ccbluex.liquidbounce.utils.entity.SimulatedPlayer
import net.ccbluex.liquidbounce.utils.kotlin.Priority
import net.minecraft.client.world.ClientWorld
import net.minecraft.entity.projectile.ArrowEntity
Expand Down Expand Up @@ -65,12 +66,10 @@ object ModuleAutoDodge : Module("AutoDodge", Category.COMBAT) {

val arrows = findFlyingArrows(world)

val input = SimulatedPlayer.SimulatedPlayerInput.fromClientPlayer(event.directionalInput)

val simulatedPlayer = SimulatedPlayer.fromClientPlayer(input)
val simulatedPlayer = CachedPlayerSimulation(PlayerSimulationCache.getSimulationForLocalPlayer())

val inflictedHit =
getInflictedHits(simulatedPlayer, arrows, hitboxExpansion = DodgePlanner.SAFE_DISTANCE_WITH_PADDING) {}
getInflictedHits(simulatedPlayer, arrows, hitboxExpansion = DodgePlanner.SAFE_DISTANCE_WITH_PADDING)
?: return@handler

val dodgePlan =
Expand Down Expand Up @@ -112,13 +111,10 @@ object ModuleAutoDodge : Module("AutoDodge", Category.COMBAT) {
arrows: List<ArrowEntity>,
maxTicks: Int = 80,
hitboxExpansion: Double = 0.7,
behaviour: (T) -> Unit,
): HitInfo? {
val simulatedArrows = arrows.map { SimulatedArrow(world, it.pos, it.velocity, false) }

for (i in 0 until maxTicks) {
behaviour(simulatedPlayer)

simulatedPlayer.tick()

simulatedArrows.forEachIndexed { arrowIndex, arrow ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import net.ccbluex.liquidbounce.features.module.modules.player.ModuleBlink
import net.ccbluex.liquidbounce.features.module.modules.world.scaffold.ModuleScaffold
import net.ccbluex.liquidbounce.render.*
import net.ccbluex.liquidbounce.render.engine.Color4b
import net.ccbluex.liquidbounce.utils.entity.SimulatedPlayer
import net.ccbluex.liquidbounce.utils.entity.PlayerSimulationCache
import net.ccbluex.liquidbounce.utils.entity.eyes
import net.ccbluex.liquidbounce.utils.math.geometry.Face
import net.ccbluex.liquidbounce.utils.math.geometry.Line
Expand All @@ -59,33 +59,26 @@ object ModuleDebug : Module("Debug", Category.RENDER) {
object RenderSimulatedPlayer : ToggleableConfigurable(this, "SimulatedPlayer", false) {

private val ticksToPredict by int("TicksToPredict", 20, 5..100)
private val simLines = mutableListOf<Vec3d>()

@Suppress("unused")
val tickRep = handler<MovementInputEvent> { event ->
val tickRep = handler<MovementInputEvent> { _ ->
// We aren't actually where we are because of blink.
// So this module shall not cause any disturbance in that case.
if (ModuleBlink.enabled) {
return@handler
}

simLines.clear()

val input =
SimulatedPlayer.SimulatedPlayerInput.fromClientPlayer(event.directionalInput)

val simulatedPlayer = SimulatedPlayer.fromClientPlayer(input)

repeat(ticksToPredict) {
simulatedPlayer.tick()
simLines.add(simulatedPlayer.pos)
}
PlayerSimulationCache.getSimulationForLocalPlayer().simulateUntil(this.ticksToPredict)
}

val renderHandler = handler<WorldRenderEvent> { event ->
val cachedPositions = PlayerSimulationCache
.getSimulationForLocalPlayer()
.getSnapshotsBetween(0 until this.ticksToPredict)

renderEnvironmentForWorld(event.matrixStack) {
withColor(Color4b.BLUE) {
drawLineStrip(positions = simLines.map { relativeToCamera(it).toVec3() }.toTypedArray())
drawLineStrip(positions = cachedPositions.map { relativeToCamera(it.pos).toVec3() })
}
}
}
Expand Down Expand Up @@ -136,10 +129,6 @@ object ModuleDebug : Module("Debug", Category.RENDER) {

val pointTo = face.nearestPointTo(line)

if (pointTo == null) {
return@repeatable
}

ModuleDebug.debugGeometry(
ModuleScaffold,
"targetPoint",
Expand Down Expand Up @@ -212,7 +201,7 @@ object ModuleDebug : Module("Debug", Category.RENDER) {
textList.forEachIndexed { index, text ->
context.drawCenteredTextWithShadow(
mc.textRenderer, text, width / 2, 40 +
(mc.textRenderer.fontHeight * index), Color4b.WHITE.toRGBA()
(mc.textRenderer.fontHeight * index), Color4b.WHITE.toRGBA()
)
}
}
Expand Down
Loading

0 comments on commit ea5cb13

Please sign in to comment.