/
Maths.kt
252 lines (215 loc) · 8.25 KB
/
Maths.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
/*
* This file is part of the Krypton project, licensed under the Apache License v2.0
*
* Copyright (C) 2021-2023 KryptonMC and the contributors of the Krypton project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kryptonmc.krypton.util.math
import com.google.common.math.IntMath
import org.kryptonmc.api.util.Vec3i
import org.kryptonmc.krypton.util.random.RandomSource
import org.kryptonmc.krypton.coordinate.ChunkPos
import java.util.UUID
import java.util.function.IntPredicate
import kotlin.math.sin
import kotlin.math.sqrt
object Maths {
private val MULTIPLY_DE_BRUIJN_BIT_POSITION = intArrayOf(
0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
)
private val SIN = FloatArray(65536) { sin(it.toDouble() * Math.PI * 2.0 / 65536.0).toFloat() }
const val EPSILON: Float = 1.0E-5F
private const val TO_RADIANS_FACTOR = Math.PI.toFloat() / 180F
/**
* Calculates a chunk position from a given [id] in a spiral pattern.
*
* **Algorithm:**
*
* Given n, an index in the squared spiral
* p, the sum of a point in the inner square
* and a, the position on the current square
*
* n = p + a
*
* Credit for this algorithm goes to
* [davidonet](https://stackoverflow.com/users/1068670/davidonet) (for the original algorithm),
* and [Esophose](https://github.com/Esophose) (for the Kotlin conversion and modifications)
*
* See [here](https://stackoverflow.com/questions/398299/looping-in-a-spiral) for original
*
* @param id the id in the spiral
* @param xOffset an optional X offset
* @param zOffset an optional Z offset
* @return a [ChunkPos] containing the calculated position in the spiral.
*/
@JvmStatic
fun chunkInSpiral(id: Int, xOffset: Int = 0, zOffset: Int = 0): ChunkPos {
// if the id is 0 then we know we're in the centre
if (id == 0) return ChunkPos(0 + xOffset, 0 + zOffset)
val index = id - 1
// compute radius (inverse arithmetic sum of 8 + 16 + 24 + ...)
val radius = floor((sqrt(index + 1.0) - 1) / 2) + 1
// compute total point on radius -1 (arithmetic sum of 8 + 16 + 24 + ...)
val p = 8 * radius * (radius - 1) / 2
// points by face
val en = radius * 2
// compute de position and shift it so the first is (-r, -r) but (-r + 1, -r)
// so the square can connect
val a = (1 + index - p) % (radius * 8)
return when (a / (radius * 2)) {
// find the face (0 = top, 1 = right, 2 = bottom, 3 = left)
0 -> ChunkPos(a - radius + xOffset, -radius + zOffset)
1 -> ChunkPos(radius + xOffset, a % en - radius + zOffset)
2 -> ChunkPos(radius - a % en + xOffset, radius + zOffset)
3 -> ChunkPos(-radius + xOffset, radius - a % en + zOffset)
else -> ChunkPos.ZERO
}
}
@JvmStatic
fun lcm(a: Int, b: Int): Long = a.toLong() * (b / IntMath.gcd(a, b))
@JvmStatic
fun fastBinarySearch(minimum: Int, maximum: Int, predicate: IntPredicate): Int {
var range = maximum - minimum
var tempMinimum = minimum
while (range > 0) {
val halfRange = range / 2
val halfOffset = tempMinimum + halfRange
if (predicate.test(halfOffset)) {
range = halfRange
} else {
tempMinimum = halfOffset + 1
range -= halfRange + 1
}
}
return tempMinimum
}
// The magic values in here are from the mixer parts of MurmurHash3
@JvmStatic
@Suppress("MagicNumber")
fun murmurHash3Mixer(value: Int): Int {
var temp = value
temp = temp xor (temp ushr 16)
temp *= -2048144789
temp = temp xor (temp ushr 13)
temp *= -1028477387
return temp xor (temp ushr 16)
}
@JvmStatic
fun getSeed(position: Vec3i): Long = getSeed(position.x, position.y, position.z)
@JvmStatic
@Suppress("MagicNumber")
fun getSeed(x: Int, y: Int, z: Int): Long {
var seed = (x * 3129871).toLong() xor y.toLong() * 116129781L xor z.toLong()
seed = seed * seed * 42317861L + seed * 11L
return seed shr 16
}
@JvmStatic
fun floor(value: Float): Int {
val result = value.toInt()
return if (value < result) result - 1 else result
}
@JvmStatic
fun floor(value: Double): Int {
val result = value.toInt()
return if (value < result) result - 1 else result
}
@JvmStatic
fun lfloor(value: Double): Long {
val result = value.toLong()
return if (value < result) result - 1 else result
}
@JvmStatic
fun ceil(value: Double): Int {
val result = value.toInt()
return if (value > result) result + 1 else result
}
@JvmStatic
fun positiveCeilDivide(x: Int, y: Int): Int = -Math.floorDiv(-x, y)
@JvmStatic
@Suppress("MagicNumber")
fun ceillog2(value: Int): Int {
val temp = if (isPowerOfTwo(value)) value else roundUpPow2(value)
return MULTIPLY_DE_BRUIJN_BIT_POSITION[(temp.toLong() * 125613361L shr 27 and 31).toInt()]
}
@JvmStatic
fun log2(value: Int): Int = ceillog2(value) - if (isPowerOfTwo(value)) 0 else 1
@JvmStatic
fun isPowerOfTwo(value: Int): Boolean = value != 0 && value and value - 1 == 0
@JvmStatic
fun roundUpPow2(value: Int): Int {
var temp = value - 1
temp = temp or (temp shr 1)
temp = temp or (temp shr 2)
temp = temp or (temp shr 4)
temp = temp or (temp shr 8)
temp = temp or (temp shr 16)
return temp + 1
}
/**
* Returns the fractional component of the value. This is equivalent to the value minus the floored value (the whole part).
*/
@JvmStatic
fun fraction(value: Double): Double = value - lfloor(value)
@JvmStatic
fun sin(value: Float): Float = SIN[(value * 10430.378F).toInt() and 65535]
@JvmStatic
fun cos(value: Float): Float = SIN[(value * 10430.378F + 16384F).toInt() and 65535]
/**
* A fast approximation of the degrees value in radians, using floats instead of doubles.
*/
@JvmStatic
fun toRadians(degrees: Float): Float = degrees * TO_RADIANS_FACTOR
@JvmStatic
fun clamp(value: Int, low: Int, high: Int): Int {
if (value < low) return low
if (value > high) return high
return value
}
@JvmStatic
fun clamp(value: Float, low: Float, high: Float): Float {
if (value < low) return low
if (value > high) return high
return value
}
@JvmStatic
fun clamp(value: Double, low: Double, high: Double): Double {
if (value < low) return low
if (value > high) return high
return value
}
@JvmStatic
fun lerp(delta: Float, start: Float, end: Float): Float = start + delta * (end - start)
@JvmStatic
fun lerp(delta: Double, start: Double, end: Double): Double = start + delta * (end - start)
@JvmStatic
fun nextInt(random: RandomSource, low: Int, high: Int): Int {
if (low >= high) return low
return random.nextInt(high - low + 1) + low
}
@JvmStatic
fun nextFloat(random: RandomSource, low: Float, high: Float): Float {
if (low >= high) return low
return random.nextFloat() * (high - low) + low
}
@JvmStatic
fun randomBetween(random: RandomSource, min: Int, max: Int): Int = random.nextInt(max - min + 1) + min
@JvmStatic
@Suppress("MagicNumber")
fun createInsecureUUID(random: RandomSource): UUID {
val most = random.nextLong() and -61441L or 16384L
val least = random.nextLong() and 4611686018427387903L or Long.MIN_VALUE
return UUID(most, least)
}
}