Add circular shape class

pull/55/head
trivernis 5 years ago
parent 53aec18a06
commit 2eeff350f8

@ -22,7 +22,7 @@ idea {
} }
group "net.trivernis" group "net.trivernis"
version "0.15-beta" version "0.16-beta"
sourceCompatibility = 1.8 sourceCompatibility = 1.8

@ -2,6 +2,8 @@ package net.trivernis.chunkmaster.lib.generation
import io.papermc.lib.PaperLib import io.papermc.lib.PaperLib
import net.trivernis.chunkmaster.Chunkmaster import net.trivernis.chunkmaster.Chunkmaster
import net.trivernis.chunkmaster.lib.shapes.Circle
import net.trivernis.chunkmaster.lib.shapes.Spiral
import org.bukkit.Chunk import org.bukkit.Chunk
import org.bukkit.Server import org.bukkit.Server
import org.bukkit.World import org.bukkit.World
@ -339,10 +341,11 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server
start: ChunkCoordinates, start: ChunkCoordinates,
stopAfter: Int stopAfter: Int
): GenerationTask { ): GenerationTask {
val shape = Spiral(Pair(center.x, center.z), Pair(center.x, center.z))
return if (PaperLib.isPaper()) { return if (PaperLib.isPaper()) {
GenerationTaskPaper(chunkmaster, world, center, start, stopAfter) GenerationTaskPaper(chunkmaster, world, center, start, stopAfter, shape)
} else { } else {
GenerationTaskSpigot(chunkmaster, world, center, start, stopAfter) GenerationTaskSpigot(chunkmaster, world, center, start, stopAfter, shape)
} }
} }
} }

@ -1,8 +1,9 @@
package net.trivernis.chunkmaster.lib.generation package net.trivernis.chunkmaster.lib.generation
import net.trivernis.chunkmaster.Chunkmaster import net.trivernis.chunkmaster.Chunkmaster
import net.trivernis.chunkmaster.lib.Spiral import net.trivernis.chunkmaster.lib.shapes.Spiral
import net.trivernis.chunkmaster.lib.dynmap.* import net.trivernis.chunkmaster.lib.dynmap.*
import net.trivernis.chunkmaster.lib.shapes.Shape
import org.bukkit.Chunk import org.bukkit.Chunk
import org.bukkit.World import org.bukkit.World
import kotlin.math.* import kotlin.math.*
@ -10,7 +11,7 @@ import kotlin.math.*
/** /**
* Interface for generation tasks. * Interface for generation tasks.
*/ */
abstract class GenerationTask(plugin: Chunkmaster, private val centerChunk: ChunkCoordinates, startChunk: ChunkCoordinates) : abstract class GenerationTask(plugin: Chunkmaster, private val centerChunk: ChunkCoordinates, startChunk: ChunkCoordinates, protected val shape: Shape) :
Runnable { Runnable {
abstract val stopAfter: Int abstract val stopAfter: Int
@ -18,8 +19,6 @@ abstract class GenerationTask(plugin: Chunkmaster, private val centerChunk: Chun
abstract val count: Int abstract val count: Int
abstract var endReached: Boolean abstract var endReached: Boolean
protected val spiral: Spiral =
Spiral(Pair(centerChunk.x, centerChunk.z), Pair(startChunk.x, startChunk.z))
protected val loadedChunks: HashSet<Chunk> = HashSet() protected val loadedChunks: HashSet<Chunk> = HashSet()
var lastChunkCoords = ChunkCoordinates(startChunk.x, startChunk.z) var lastChunkCoords = ChunkCoordinates(startChunk.x, startChunk.z)
protected set protected set
@ -40,7 +39,7 @@ abstract class GenerationTask(plugin: Chunkmaster, private val centerChunk: Chun
private val markerAreaStyle = MarkerStyle(null, LineStyle(2, 1.0, 0x0022FF), FillStyle(.0, 0)) private val markerAreaStyle = MarkerStyle(null, LineStyle(2, 1.0, 0x0022FF), FillStyle(.0, 0))
private val markerAreaId = "chunkmaster_genarea" private val markerAreaId = "chunkmaster_genarea"
private val markerAreaName = "Chunkmaster Generation Area" private val markerAreaName = "Chunkmaster Generation Area"
private val markerLastStyle = MarkerStyle(null, LineStyle(2, 1.0, 0x0077FF), FillStyle(.0, 0)) private val markerLastStyle = MarkerStyle(null, LineStyle(2, 1.0, 0x0077FF), FillStyle(.5, 0x0077FF))
private val markerLastId = "chunkmaster_lastchunk" private val markerLastId = "chunkmaster_lastchunk"
private val markerLastName = "Chunkmaster Last Chunk" private val markerLastName = "Chunkmaster Last Chunk"
private val ignoreWorldborder = plugin.config.getBoolean("generation.ignore-worldborder") private val ignoreWorldborder = plugin.config.getBoolean("generation.ignore-worldborder")
@ -50,7 +49,7 @@ abstract class GenerationTask(plugin: Chunkmaster, private val centerChunk: Chun
val nextChunkCoordinates: ChunkCoordinates val nextChunkCoordinates: ChunkCoordinates
get() { get() {
val nextChunkCoords = spiral.next() val nextChunkCoords = shape.next()
return ChunkCoordinates(nextChunkCoords.first, nextChunkCoords.second) return ChunkCoordinates(nextChunkCoords.first, nextChunkCoords.second)
} }

@ -1,6 +1,7 @@
package net.trivernis.chunkmaster.lib.generation package net.trivernis.chunkmaster.lib.generation
import net.trivernis.chunkmaster.Chunkmaster import net.trivernis.chunkmaster.Chunkmaster
import net.trivernis.chunkmaster.lib.shapes.Shape
import org.bukkit.Chunk import org.bukkit.Chunk
import org.bukkit.World import org.bukkit.World
import java.util.concurrent.CompletableFuture import java.util.concurrent.CompletableFuture
@ -8,8 +9,9 @@ import java.util.concurrent.CompletableFuture
class GenerationTaskPaper( class GenerationTaskPaper(
private val plugin: Chunkmaster, override val world: World, private val plugin: Chunkmaster, override val world: World,
centerChunk: ChunkCoordinates, private val startChunk: ChunkCoordinates, centerChunk: ChunkCoordinates, private val startChunk: ChunkCoordinates,
override val stopAfter: Int = -1 override val stopAfter: Int = -1,
) : GenerationTask(plugin, centerChunk, startChunk) { shape: Shape
) : GenerationTask(plugin, centerChunk, startChunk, shape) {
private val maxPendingChunks = plugin.config.getInt("generation.max-pending-chunks") private val maxPendingChunks = plugin.config.getInt("generation.max-pending-chunks")
@ -59,7 +61,7 @@ class GenerationTaskPaper(
} }
} }
lastChunkCoords = chunk lastChunkCoords = chunk
count = spiral.count // set the count to the more accurate spiral count count = shape.count // set the count to the more accurate spiral count
} }
} }
checkChunksLoaded() checkChunksLoaded()

@ -1,14 +1,16 @@
package net.trivernis.chunkmaster.lib.generation package net.trivernis.chunkmaster.lib.generation
import net.trivernis.chunkmaster.Chunkmaster import net.trivernis.chunkmaster.Chunkmaster
import net.trivernis.chunkmaster.lib.shapes.Shape
import org.bukkit.World import org.bukkit.World
import java.lang.Exception import java.lang.Exception
class GenerationTaskSpigot( class GenerationTaskSpigot(
private val plugin: Chunkmaster, override val world: World, private val plugin: Chunkmaster, override val world: World,
centerChunk: ChunkCoordinates, private val startChunk: ChunkCoordinates, centerChunk: ChunkCoordinates, private val startChunk: ChunkCoordinates,
override val stopAfter: Int = -1 override val stopAfter: Int = -1,
) : GenerationTask(plugin, centerChunk, startChunk) { shape: Shape
) : GenerationTask(plugin, centerChunk, startChunk, shape) {
override var count = 0 override var count = 0
@ -48,7 +50,7 @@ class GenerationTaskSpigot(
loadedChunks.add(chunkInstance) loadedChunks.add(chunkInstance)
} }
lastChunkCoords = chunk lastChunkCoords = chunk
count = spiral.count // set the count to the more accurate spiral count count = shape.count // set the count to the more accurate spiral count
} }
} }
} }

@ -0,0 +1,86 @@
package net.trivernis.chunkmaster.lib.shapes
import java.util.*
import kotlin.collections.ArrayList
import kotlin.collections.HashSet
import kotlin.system.exitProcess
class Circle(center: Pair<Int, Int>, start: Pair<Int, Int>): Shape(center, start) {
var r = 0
private set
private var coords = Stack<Pair<Int, Int>>()
override fun next(): Pair<Int, Int> {
if (count == 0 && currentPos != center) {
val tmpCircle = Circle(center, center)
while (tmpCircle.next() != currentPos);
this.count = tmpCircle.count
this.r = tmpCircle.r
}
if (count == 0) {
count++
return center
}
if (coords.isEmpty()) {
r++
val tmpCoords = HashSet<Pair<Int, Int>>()
tmpCoords.addAll(getCircleCoordinates((r*2)-1).map { Pair(it.first / 2, it.second / 2) })
tmpCoords.addAll(getCircleCoordinates(r))
coords.addAll(tmpCoords)
}
count++
val coord = coords.pop()
currentPos = Pair(coord.first + center.first, coord.second + center.second)
return currentPos
}
/**
* Returns the int coordinates for a circle
* @param r - the radius
*/
private fun getCircleCoordinates(r: Int): HashSet<Pair<Int, Int>> {
val coords = ArrayList<Pair<Int, Int>>()
val segCoords = getSegment(r)
coords.addAll(segCoords)
for (step in 0..7) {
for (pos in segCoords) {
coords.add(when (step) {
0 -> pos
1 -> Pair(pos.second, pos.first)
2 -> Pair(pos.first, -pos.second)
3 -> Pair(-pos.second, pos.first)
4 -> Pair(-pos.first, -pos.second)
5 -> Pair(-pos.second, -pos.first)
6 -> Pair(-pos.first, pos.second)
7 -> Pair(pos.second, -pos.first)
else -> pos
})
}
}
val set = HashSet<Pair<Int, Int>>()
set.addAll(coords)
return set
}
/**
* Returns the int coordinates for a circles segment
* @param r - the radius
*/
private fun getSegment(r: Int): ArrayList<Pair<Int, Int>> {
var d = -r
var x = r
var y = 0
val coords = ArrayList<Pair<Int, Int>>()
while (y <= x) {
coords.add(Pair(x, y))
d += 2 * y + 1
y += 1
if (d > 0) {
x -= 1
d -= 2 * x
}
}
return coords
}
}

@ -0,0 +1,11 @@
package net.trivernis.chunkmaster.lib.shapes
abstract class Shape(protected val center: Pair<Int, Int>, start: Pair<Int, Int>) {
protected var currentPos = start
var count = 0
/**
* Returns the next value
*/
abstract fun next(): Pair<Int, Int>
}

@ -1,16 +1,14 @@
package net.trivernis.chunkmaster.lib package net.trivernis.chunkmaster.lib.shapes
import kotlin.math.abs import kotlin.math.abs
class Spiral(private val center: Pair<Int, Int>, start: Pair<Int, Int>) { class Spiral(center: Pair<Int, Int>, start: Pair<Int, Int>): Shape(center, start) {
private var currentPos = start
private var direction = 0 private var direction = 0
var count = 0
/** /**
* Returns the next value in the spiral * Returns the next value in the spiral
*/ */
fun next(): Pair<Int, Int> { override fun next(): Pair<Int, Int> {
if (count == 0 && currentPos != center) { if (count == 0 && currentPos != center) {
// simulate the spiral to get the correct direction // simulate the spiral to get the correct direction
// TODO: Improve performance of this workaround (replace it with acutal stuff) // TODO: Improve performance of this workaround (replace it with acutal stuff)

@ -1,6 +1,6 @@
main: net.trivernis.chunkmaster.Chunkmaster main: net.trivernis.chunkmaster.Chunkmaster
name: Chunkmaster name: Chunkmaster
version: '0.15-beta' version: '0.16-beta'
description: Chunk commands plugin. description: Chunk commands plugin.
author: Trivernis author: Trivernis
website: trivernis.net website: trivernis.net

Loading…
Cancel
Save