Skip to content

Commit

Permalink
Merge pull request #4 from Braffolk/v0.5
Browse files Browse the repository at this point in the history
v 0.5 #3 is fixed. Also performance improvements
  • Loading branch information
Braffolk authored Oct 26, 2020
2 parents eb29a60 + 844a89a commit bb286aa
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
9 changes: 9 additions & 0 deletions src/main/kotlin/utility.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ee.braffolk.factionsx

import kotlin.math.pow
import kotlin.math.sqrt

fun List<Double>.standardDeviation(avg: Double): Double {
val sum = this.map { (it - avg).pow(2) }.sum()
return sqrt(sum / (this.size - 1))
}
38 changes: 23 additions & 15 deletions src/main/kotlin/visualisers/LineVisualiser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import ee.braffolk.factionsx.VisualisationHandler
import ee.braffolk.factionsx.VisualisationPerformance
import ee.braffolk.factionsx.cache.BlockHeightCache
import ee.braffolk.factionsx.cache.ShapeCache
import ee.braffolk.factionsx.standardDeviation
import kotlinx.coroutines.*
import net.prosavage.factionsx.manager.FactionManager
import org.bukkit.Bukkit
Expand All @@ -22,8 +23,8 @@ class LineVisualiser(override val shapeCache: ShapeCache) : IVisualiserHandler {

override fun visualise(player: Player, visualisationPerformance: VisualisationPerformance) {
val eyeY = player.eyeLocation.y.toLong()
val playerDir = player.getLocation().direction.normalize()
val playerVec = player.getLocation().toVector()
val playerDir = player.location.direction.normalize()
val playerPos = player.location.toVector()

val playerPerformanceF = when (visualisationPerformance) {
VisualisationPerformance.Fast -> 4.0
Expand Down Expand Up @@ -52,23 +53,28 @@ class LineVisualiser(override val shapeCache: ShapeCache) : IVisualiserHandler {
{ v: Vector -> v.z.toInt().rem(iPlayerPerformanceF) == 0 },
{ v: Vector -> v.x.toInt().rem(iPlayerPerformanceF) == 0 }
)
listOf(xLines, zLines).flatMapIndexed { index, lines ->
listOf(xLines, zLines).flatMapIndexed { index, l ->
val lines = l.filter { it.heights.isNotEmpty() }
lines.zip(lines.map {
val loc = Location(player.world, (it.location.x * 16.0 + 8.0), eyeY.toDouble(), (it.location.z * 16.0 + 8.0))
player.location.distanceSquared(loc)
})
.filter { (_, distance) -> distance < maxRenderDistance }
.filter { (chunk, distance) -> distance < maxRenderDistance }
.filter { (chunk, _) ->
val linePos = Vector(
chunk.location.x * 16.0 + 8.0,
chunk.heights.fold(0.0, { acc, v -> acc + v.y }) / chunk.heights.size,
chunk.location.z * 16.0 + 8.0
)
val lineDir = linePos.subtract(playerVec).normalize()
val lineDirFlat = linePos.setY(eyeY.toFloat())
.subtract(playerVec.setY(eyeY.toFloat())).normalize()

playerDir.angle(lineDir) < Math.PI * 0.5 || playerDir.angle(lineDirFlat) < Math.PI * 0.5
val listY = chunk.heights.map { it.y }
val avgY = listY.average()
val standardDeviation = listY.standardDeviation(avgY)

val linePos = Vector(chunk.location.x * 16.0 + 8.0, avgY, chunk.location.z * 16.0 + 8.0)

if(standardDeviation > 30) {
val lineDirFlat = linePos.clone().setY(eyeY.toFloat())
.subtract(playerPos.setY(eyeY.toFloat())).normalize()
playerDir.angle(lineDirFlat) < Math.PI * 0.5
} else {
val lineDir = linePos.clone().subtract(playerPos).normalize()
playerDir.angle(lineDir) < Math.PI * 0.5
}
}
.flatMap { (chunk, distance) ->
val size = (distance / maxRenderDistance * maxDustSize)
Expand Down Expand Up @@ -102,4 +108,6 @@ class LineVisualiser(override val shapeCache: ShapeCache) : IVisualiserHandler {
sendAndWait(operations, time)
}
}
}
}


0 comments on commit bb286aa

Please sign in to comment.