diff options
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/utils')
3 files changed, 91 insertions, 49 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt index 129cec02e..d0b970187 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt @@ -384,4 +384,9 @@ object LorenzUtils { get(index) } else null } + + fun <T : Any> T?.toSingletonListOrEmpty(): List<T> { + if (this == null) return emptyList() + return listOf(this) + } }
\ No newline at end of file diff --git a/src/main/java/at/hannibal2/skyhanni/utils/LorenzVec.kt b/src/main/java/at/hannibal2/skyhanni/utils/LorenzVec.kt index 1d1909875..7380c4c37 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/LorenzVec.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/LorenzVec.kt @@ -7,11 +7,7 @@ import net.minecraft.util.AxisAlignedBB import net.minecraft.util.BlockPos import net.minecraft.util.Rotations import net.minecraft.util.Vec3 -import kotlin.math.cos -import kotlin.math.pow -import kotlin.math.round -import kotlin.math.sin -import kotlin.math.sqrt +import kotlin.math.* data class LorenzVec( val x: Double, @@ -107,7 +103,7 @@ data class LorenzVec( return LorenzVec(x, y, z) } - fun boundingToOffset(offX: Int, offY: Int, offZ: Int) = AxisAlignedBB(x, y, z, x + offX, y + offY, z + offZ) + fun boundingToOffset(offX: Double, offY: Double, offZ: Double) = AxisAlignedBB(x, y, z, x + offX, y + offY, z + offZ) fun scale(scalar: Double): LorenzVec { return LorenzVec(scalar * x, scalar * y, scalar * z) diff --git a/src/main/java/at/hannibal2/skyhanni/utils/ParkourHelper.kt b/src/main/java/at/hannibal2/skyhanni/utils/ParkourHelper.kt index b33f6bc99..081641c9a 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/ParkourHelper.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/ParkourHelper.kt @@ -1,9 +1,12 @@ package at.hannibal2.skyhanni.utils +import at.hannibal2.skyhanni.test.command.CopyErrorCommand import at.hannibal2.skyhanni.utils.LocationUtils.distanceToPlayer +import at.hannibal2.skyhanni.utils.LorenzUtils.toSingletonListOrEmpty import at.hannibal2.skyhanni.utils.RenderUtils.draw3DLine import at.hannibal2.skyhanni.utils.RenderUtils.drawDynamicText import at.hannibal2.skyhanni.utils.RenderUtils.drawFilledBoundingBox +import at.hannibal2.skyhanni.utils.RenderUtils.drawString import at.hannibal2.skyhanni.utils.RenderUtils.expandBlock import at.hannibal2.skyhanni.utils.jsonobjects.ParkourJson import net.minecraft.client.Minecraft @@ -11,13 +14,19 @@ import net.minecraftforge.client.event.RenderWorldLastEvent import java.awt.Color import kotlin.time.Duration.Companion.seconds -class ParkourHelper(val locations: List<LorenzVec>, private val shortCuts: List<ParkourJson.ShortCut>) { +class ParkourHelper( + val locations: List<LorenzVec>, + private val shortCuts: List<ParkourJson.ShortCut>, + val platformSize: Double = 1.0, + val detectionRange: Double = 1.0 +) { private var current = -1 private var visible = false - var rainbowColor = true + var rainbowColor = false var monochromeColor: Color = Color.WHITE var lookAhead = 2 + var showEverything = false fun inParkour() = current != -1 @@ -27,54 +36,91 @@ class ParkourHelper(val locations: List<LorenzVec>, private val shortCuts: List< } fun render(event: RenderWorldLastEvent) { - if (current == locations.size - 1) visible = false - - val distanceToPlayer = locations.first().distanceToPlayer() - if (distanceToPlayer < 2) { - visible = true - } else if (distanceToPlayer > 15) { - if (current < 1) { - visible = false - } + if (locations.isEmpty()) { + CopyErrorCommand.logError( + IllegalArgumentException("locations is empty"), + "Trying to render an empty parkour" + ) + return } - if (!visible) return + try { + if (!showEverything) { + if (current == locations.size - 1) visible = false + + val distanceToPlayer = locations.first().offsetCenter().distanceToPlayer() + if (distanceToPlayer < detectionRange) { + visible = true + } else if (distanceToPlayer > 15) { + if (current < 1) { + visible = false + } + } + + if (!visible) return - for ((index, location) in locations.withIndex()) { - if (location.distanceToPlayer() < 2) { - if (Minecraft.getMinecraft().thePlayer.onGround) { - current = index + for ((index, location) in locations.withIndex()) { + if (location.offsetCenter().distanceToPlayer() < detectionRange) { + if (Minecraft.getMinecraft().thePlayer.onGround) { + current = index + } + } } - } - } - if (current < 0) return - val inProgressVec = getInProgressPair().toSingletonListOrEmpty() - for ((prev, next) in locations.asSequence().withIndex().zipWithNext().drop(current) - .take(lookAhead - 1) + inProgressVec) { - event.draw3DLine(prev.value, next.value, colorForIndex(prev.index), 5, false, colorForIndex(next.index)) - } - val nextShortcuts = current until current + lookAhead - for (shortCut in shortCuts) { - if (shortCut.from in nextShortcuts && shortCut.to in locations.indices) { - event.draw3DLine(locations[shortCut.from], locations[shortCut.to], Color.RED, 3, false) - event.drawFilledBoundingBox(axisAlignedBB(locations[shortCut.to]), Color.RED, 1f) - event.drawDynamicText(locations[shortCut.to].add(-0.5, 1.0, -0.5), "§cShortcut", 2.5) + if (current < 0) return + } else { + current = 0 + lookAhead = locations.size } - } + val inProgressVec = getInProgressPair().toSingletonListOrEmpty() + for ((prev, next) in locations.asSequence().withIndex().zipWithNext().drop(current) + .take(lookAhead - 1) + inProgressVec) { + event.draw3DLine( + prev.value.offsetCenter(), + next.value.offsetCenter(), + colorForIndex(prev.index), + 5, + false, + colorForIndex(next.index) + ) + } + val nextShortcuts = current until current + lookAhead + for (shortCut in shortCuts) { + if (shortCut.from in nextShortcuts && shortCut.to in locations.indices) { + event.draw3DLine( + locations[shortCut.from].offsetCenter(), + locations[shortCut.to].offsetCenter(), + Color.RED, + 3, + false + ) + event.drawFilledBoundingBox(axisAlignedBB(locations[shortCut.to]), Color.RED, 1f) + event.drawDynamicText(locations[shortCut.to].add(-0.5, 1.0, -0.5), "§cShortcut", 2.5) + } - for ((index, location) in locations.asSequence().withIndex().drop(current) - .take(lookAhead) + inProgressVec.map { it.second }) { - var aabb = axisAlignedBB(location) - if (location !in locations) { - aabb = aabb.expandBlock() } - event.drawFilledBoundingBox(aabb, colorForIndex(index), 1f) + for ((index, location) in locations.asSequence().withIndex().drop(current) + .take(lookAhead) + inProgressVec.map { it.second }) { + var aabb = axisAlignedBB(location) + val isMovingPlattform = location !in locations + if (isMovingPlattform) { + aabb = aabb.expandBlock() + } + + event.drawFilledBoundingBox(aabb, colorForIndex(index), 1f) + if (!isMovingPlattform) { + event.drawString(location.offsetCenter().add(0, 1, 0), "§a§l$index", seeThroughBlocks = true) + } + } + } catch (e: Throwable) { + CopyErrorCommand.logError(e, "Error while rendering a parkour") } } + private fun LorenzVec.offsetCenter() = add(platformSize / 2, 1.0, platformSize / 2) + private fun getInProgressPair(): Pair<IndexedValue<LorenzVec>, IndexedValue<LorenzVec>>? { if (current < 0 || current + lookAhead >= locations.size) return null val currentPosition = locations[current] @@ -92,14 +138,9 @@ class ParkourHelper(val locations: List<LorenzVec>, private val shortCuts: List< ) } - private fun axisAlignedBB(loc: LorenzVec) = loc.add(-1.0, 0.0, -1.0).boundingToOffset(2, -1, 2).expandBlock() + private fun axisAlignedBB(loc: LorenzVec) = loc.boundingToOffset(platformSize, 1.0, platformSize).expandBlock() private fun colorForIndex(index: Int) = if (rainbowColor) { RenderUtils.chromaColor(4.seconds, offset = -index / 12f, brightness = 0.7f) } else monochromeColor -} - -private fun <T : Any> T?.toSingletonListOrEmpty(): List<T> { - if (this == null) return emptyList() - return listOf(this) }
\ No newline at end of file |
