aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/utils/ParkourHelper.kt
blob: 081641c9a5bf85db1f3b205c080c29cad6cc4027 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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
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>,
    val platformSize: Double = 1.0,
    val detectionRange: Double = 1.0
) {
    private var current = -1
    private var visible = false

    var rainbowColor = false
    var monochromeColor: Color = Color.WHITE
    var lookAhead = 2
    var showEverything = false

    fun inParkour() = current != -1

    fun reset() {
        current = -1
        visible = false
    }

    fun render(event: RenderWorldLastEvent) {
        if (locations.isEmpty()) {
            CopyErrorCommand.logError(
                IllegalArgumentException("locations is empty"),
                "Trying to render an empty parkour"
            )
            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.offsetCenter().distanceToPlayer() < detectionRange) {
                        if (Minecraft.getMinecraft().thePlayer.onGround) {
                            current = index
                        }
                    }
                }

                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)
                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]
        val nextPosition = locations[current + 1]
        val lookAheadStart = locations[current + lookAhead - 1]
        val lookAheadEnd = locations[current + lookAhead]

        if (LocationUtils.playerLocation().distance(nextPosition) > currentPosition.distance(nextPosition)) return null

        val factor = LocationUtils.playerLocation().distance(currentPosition) / currentPosition.distance(nextPosition)
        val solpeLocation = lookAheadStart.add(lookAheadEnd.subtract(lookAheadStart).scale(factor))
        return Pair(
            IndexedValue(current + lookAhead - 1, lookAheadStart),
            IndexedValue(current + lookAhead, solpeLocation)
        )
    }

    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
}