aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/features/event/diana/SoopyGuessBurrow.kt
blob: 177f16db26bbb85d81c833f8a9d469a1a8859eb8 (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package at.hannibal2.skyhanni.features.event.diana

import at.hannibal2.skyhanni.events.PlaySoundEvent
import at.hannibal2.skyhanni.events.ReceiveParticleEvent
import at.hannibal2.skyhanni.events.SoopyGuessBurrowEvent
import at.hannibal2.skyhanni.utils.LorenzVec
import at.hannibal2.skyhanni.utils.toLorenzVec
import net.minecraft.util.EnumParticleTypes
import net.minecraftforge.event.world.WorldEvent
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import kotlin.math.*

/**
 * Taken and ported from Soopyboo32's javascript module SoopyV2
 */
class SoopyGuessBurrow {

    var dingIndex = 0
    var lastDing = 0L
    var lastDingPitch = 0f
    var firstPitch = 0f
    var lastParticlePoint: LorenzVec? = null
    var lastParticlePoint2: LorenzVec? = null
    var firstParticlePoint: LorenzVec? = null
    var particlePoint: LorenzVec? = null
    var guessPoint: LorenzVec? = null

    var lastSoundPoint: LorenzVec? = null
    var locs = mutableListOf<LorenzVec>()

    var dingSlope = mutableListOf<Float>()

    var distance: Double? = null
    var distance2: Double? = null

    @SubscribeEvent
    fun onWorldChange(event: WorldEvent.Load) {
        lastDing = 0L
        lastDingPitch = 0f
        firstPitch = 0f
        lastParticlePoint = null
        lastParticlePoint2 = null
        lastSoundPoint = null
        firstParticlePoint = null
        particlePoint = null
        guessPoint = null
        distance = null
        dingIndex = 0
        dingSlope.clear()
    }

    @SubscribeEvent
    fun onPlaySound(event: PlaySoundEvent) {
        if (event.soundName != "note.harp") return

        val pitch = event.pitch
        if (lastDing == 0L) {
            firstPitch = pitch
        }

        lastDing = System.currentTimeMillis()

        if (pitch < lastDingPitch) {
            firstPitch = pitch
            dingIndex = 0
            dingSlope.clear()
            lastDingPitch = pitch
            lastParticlePoint = null
            lastParticlePoint2 = null
            lastSoundPoint = null
            firstParticlePoint = null
            distance = null
            locs.clear()
        }

        if (lastDingPitch == 0f) {
            lastDingPitch = pitch
            distance = null
            lastParticlePoint = null
            lastParticlePoint2 = null
            lastSoundPoint = null
            firstParticlePoint = null
            locs.clear()
            return
        }

        dingIndex++

        if (dingIndex > 1) dingSlope.add(pitch - lastDingPitch)
        if (dingSlope.size > 20) dingSlope.removeFirst()
        val slope = if (dingSlope.isNotEmpty()) dingSlope.reduce { a, b -> a + b }.toDouble() / dingSlope.size else 0.0
        val pos = event.location
        lastSoundPoint = pos
        lastDingPitch = pitch

        if (lastParticlePoint2 == null || particlePoint == null || firstParticlePoint == null) {
            return
        }

        distance2 = (Math.E / slope) - firstParticlePoint?.distance(pos)!!

        if (distance2!! > 1000) {
            distance2 = null
            guessPoint = null
        }

        val lineDist = lastParticlePoint2?.distance(particlePoint!!)!!

        distance = distance2!!
        val changesHelp = particlePoint?.subtract(lastParticlePoint2!!)!!
        var changes = listOf(changesHelp.x, changesHelp.y, changesHelp.z)
        changes = changes.map { o -> o / lineDist }

        lastSoundPoint?.let {
            guessPoint =
                LorenzVec(
                    it.x + changes[0] * distance!!,
                    it.y + changes[1] * distance!!,
                    it.z + changes[2] * distance!!
                )
        }
    }

    private fun solveEquationThing(x: LorenzVec, y: LorenzVec): LorenzVec {
        val a =
            (-y.x * x.y * x.x - y.y * x.y * x.z + y.y * x.y * x.x + x.y * x.z * y.z + x.x * x.z * y.x - x.x * x.z * y.z) / (x.y * y.x - x.y * y.z + x.x * y.z - y.x * x.z + y.y * x.z - y.y * x.x)
        val b = (y.x - y.y) * (x.x + a) * (x.y + a) / (x.y - x.x)
        val c = y.x - b / (x.x + a)
        return LorenzVec(a, b, c)
    }

    @SubscribeEvent
    fun onReceiveParticle(event: ReceiveParticleEvent) {
        val type = event.type
        if (type != EnumParticleTypes.DRIP_LAVA) return
        val currLoc = event.location

        var run = false
        lastSoundPoint?.let {
            if (abs(currLoc.x - it.x) < 2 && abs(currLoc.y - it.y) < 0.5 && abs(currLoc.z - it.z) < 2) {
                run = true
            }
        }
        if (run) {
            if (locs.size < 100 && locs.isEmpty() || locs.last().distance(currLoc) != 0.0) {
                var distMultiplier = 1.0
                if (locs.size > 2) {
                    val predictedDist = 0.06507 * locs.size + 0.259
                    val lastPos = locs.last()
                    val actualDist = currLoc.distance(lastPos)
                    distMultiplier = actualDist / predictedDist
                }
                locs.add(currLoc)

                if (locs.size > 5 && guessPoint != null) {

                    val slopeThing = locs.zipWithNext { a, b ->
                        atan((a.x - b.x) / (a.z - b.z))
                    }

                    val (a, b, c) = solveEquationThing(
                        LorenzVec(slopeThing.size - 5, slopeThing.size - 3, slopeThing.size - 1), LorenzVec(
                            slopeThing[slopeThing.size - 5],
                            slopeThing[slopeThing.size - 3],
                            slopeThing[slopeThing.size - 1]
                        )
                    )

                    val pr1 = mutableListOf<LorenzVec>()
                    val pr2 = mutableListOf<LorenzVec>()

                    val start = slopeThing.size - 1
                    val lastPos = locs[start].multiply(1).toDoubleArray()
                    val lastPos2 = locs[start].multiply(1).toDoubleArray()

                    var distCovered = 0.0

                    val ySpeed = locs[locs.size - 1].x - locs[locs.size - 2].x / hypot(
                        locs[locs.size - 1].x - locs[locs.size - 2].x,
                        locs[locs.size - 1].z - locs[locs.size - 2