-
-
Notifications
You must be signed in to change notification settings - Fork 208
/
SoopyGuessBurrow.kt
285 lines (229 loc) · 9.69 KB
/
SoopyGuessBurrow.kt
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package at.hannibal2.skyhanni.features.event.diana
import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.events.BurrowGuessEvent
import at.hannibal2.skyhanni.events.LorenzWorldChangeEvent
import at.hannibal2.skyhanni.events.PlaySoundEvent
import at.hannibal2.skyhanni.events.ReceiveParticleEvent
import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
import at.hannibal2.skyhanni.utils.ChatUtils
import at.hannibal2.skyhanni.utils.LorenzVec
import at.hannibal2.skyhanni.utils.toLorenzVec
import net.minecraft.util.EnumParticleTypes
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import kotlin.math.abs
import kotlin.math.atan
import kotlin.math.cos
import kotlin.math.floor
import kotlin.math.hypot
import kotlin.math.pow
import kotlin.math.sin
/**
* Taken and ported from Soopyboo32's javascript module SoopyV2
*/
@SkyHanniModule
object SoopyGuessBurrow {
private var dingIndex = 0
private var hasDinged = false
private var lastDingPitch = 0f
private var firstPitch = 0f
private var lastParticlePoint: LorenzVec? = null
private var lastParticlePoint2: LorenzVec? = null
private var firstParticlePoint: LorenzVec? = null
private var particlePoint: LorenzVec? = null
private var guessPoint: LorenzVec? = null
private var lastSoundPoint: LorenzVec? = null
private var locs = mutableListOf<LorenzVec>()
private var dingSlope = mutableListOf<Float>()
var distance: Double? = null
private var distance2: Double? = null
@SubscribeEvent
fun onWorldChange(event: LorenzWorldChangeEvent) {
hasDinged = false
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 (!isEnabled()) return
if (event.soundName != "note.harp") return
val pitch = event.pitch
if (!hasDinged) {
firstPitch = pitch
}
hasDinged = true
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) {
ChatUtils.debug("Soopy distance2 is $distance2")
distance2 = null
guessPoint = null
// workaround: returning if the distance is too big
return
}
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) {
if (!isEnabled()) return
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].toDoubleArray()
val lastPos2 = locs[start].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].x
)
var i = start + 1
while (distCovered < distance2!! && i < 10000) {
val y = b / (i + a) + c
val dist = distMultiplier * (0.06507 * i + 0.259)
val xOff = dist * sin(y)
val zOff = dist * cos(y)
val density = 5
for (o in 0..density) {
lastPos[0] += xOff / density
lastPos[2] += zOff / density
lastPos[1] += ySpeed * dist / density
lastPos2[1] += ySpeed * dist / density
lastPos2[0] -= xOff / density
lastPos2[2] -= zOff / density
pr1.add(lastPos.toLorenzVec())
pr2.add(lastPos2.toLorenzVec())
lastSoundPoint?.let {
distCovered = hypot(lastPos[0] - it.x, lastPos[2] - it.z)
}
if (distCovered > distance2!!) break
}
i++
}
// Why does this happen?
if (pr1.isEmpty()) return
val p1 = pr1.last()
val p2 = pr2.last()
guessPoint?.let {
val d1 = ((p1.x - it.x).times(2 + (p1.z - it.z))).pow(2)
val d2 = ((p2.x - it.x).times(2 + (p2.z - it.z))).pow(2)
val finalLocation = if (d1 < d2) {
LorenzVec(floor(p1.x), 255.0, floor(p1.z))
} else {
LorenzVec(floor(p2.x), 255.0, floor(p2.z))
}
BurrowGuessEvent(finalLocation).postAndCatch()
}
}
}
if (lastParticlePoint == null) {
firstParticlePoint = currLoc.clone()
}
lastParticlePoint2 = lastParticlePoint
lastParticlePoint = particlePoint
particlePoint = currLoc.clone()
if (lastParticlePoint2 == null || firstParticlePoint == null || distance2 == null || lastSoundPoint == null) return
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 }
lastParticlePoint?.let {
guessPoint =
LorenzVec(
it.x + changes[0] * distance!!,
it.y + changes[1] * distance!!,
it.z + changes[2] * distance!!
)
}
}
}
private fun isEnabled() = DianaAPI.isDoingDiana() && SkyHanniMod.feature.event.diana.burrowsSoopyGuess
}