mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-06 08:38:30 +09:00
removing unused classes
This commit is contained in:
@@ -1,94 +0,0 @@
|
|||||||
package net.torvald.aa
|
|
||||||
|
|
||||||
import net.torvald.terrarum.gameworld.toUint
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param terminal: for sending redraw only
|
|
||||||
*
|
|
||||||
* Created by minjaesong on 2016-08-10.
|
|
||||||
*/
|
|
||||||
/*class AAFrame(var width: Int, var height: Int, var terminal: SimpleTextTerminal) {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 0000_0000_00000000
|
|
||||||
|
|
||||||
* Upper bits: Background colour
|
|
||||||
*
|
|
||||||
* Middle bits: Foreground colour
|
|
||||||
*
|
|
||||||
* Lower 8 bits: CP437
|
|
||||||
*/
|
|
||||||
internal val frameBuffer: CharArray
|
|
||||||
|
|
||||||
val sizeof = 2 * width * height // magic number 2: indicator that we're using char
|
|
||||||
|
|
||||||
init {
|
|
||||||
frameBuffer = CharArray(width * height)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun drawBuffer(x: Int, y: Int, c: Char, colourKey: Int) {
|
|
||||||
if (y * width + x >= frameBuffer.size || y * width + x < 0)
|
|
||||||
throw ArrayIndexOutOfBoundsException("x: $x, y; $y")
|
|
||||||
frameBuffer[y * width + x] = ((c.toInt().and(0xFF)) + colourKey.shl(8)).toChar()
|
|
||||||
|
|
||||||
//terminal.redraw()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun drawBuffer(x: Int, y: Int, raw: Char): Boolean =
|
|
||||||
if (checkOOB(x, y))
|
|
||||||
false
|
|
||||||
else {
|
|
||||||
frameBuffer[y * width + x] = raw
|
|
||||||
//terminal.redraw()
|
|
||||||
true
|
|
||||||
}
|
|
||||||
|
|
||||||
fun drawFromBytes(other: ByteArray) {
|
|
||||||
for (i in 0..other.size - 1 step 2) {
|
|
||||||
val char = (other[i].toUint().shl(8) + other[i + 1].toUint()).toChar()
|
|
||||||
frameBuffer[i.ushr(1)] = char
|
|
||||||
}
|
|
||||||
//terminal.redraw()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getBackgroundColour(x: Int, y: Int): Int {
|
|
||||||
return frameBuffer[y * width + x].toInt().ushr(12) and 0xF
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getForegroundColour(x: Int, y: Int): Int {
|
|
||||||
return frameBuffer[y * width + x].toInt().ushr(8) and 0xF
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getChar(x: Int, y: Int): Char {
|
|
||||||
return (frameBuffer[y * width + x].toInt() and 0xFF).toChar()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getRaw(x: Int, y: Int): Char? =
|
|
||||||
if (checkOOB(x, y))
|
|
||||||
null
|
|
||||||
else
|
|
||||||
frameBuffer[y * width + x]
|
|
||||||
|
|
||||||
fun clear(background: Int = 0) {
|
|
||||||
for (y in 0..height - 1) {
|
|
||||||
for (x in 0..width - 1) {
|
|
||||||
drawBuffer(x, y, 0.toChar(), background.shl(4))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//terminal.redraw()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun drawFromOther(other: AAFrame) {
|
|
||||||
//this.framebuffer = other.getFrameBuffer();
|
|
||||||
for (y in 0..height - 1) {
|
|
||||||
for (x in 0..width - 1) {
|
|
||||||
frameBuffer[y * width + x] = other.getRaw(x, y)!!
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//terminal.redraw()
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun checkOOB(x: Int, y: Int) = (x < 0 || y < 0 || x >= width || y >= height)
|
|
||||||
|
|
||||||
fun getColourKey(x: Int, y: Int): Int = frameBuffer[y * width + x].toInt().ushr(8).and(0xFF)
|
|
||||||
}*/
|
|
||||||
@@ -1,117 +0,0 @@
|
|||||||
package net.torvald.aa
|
|
||||||
|
|
||||||
import net.torvald.terrarum.Point2d
|
|
||||||
import net.torvald.terrarum.gameactors.ActorWithBody
|
|
||||||
import net.torvald.terrarum.sqr
|
|
||||||
|
|
||||||
/**
|
|
||||||
* k-d Tree that uses binary heap instead of binary tree to improve data locality
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* -- I couldn't observe any significant boost in performance but this one seems
|
|
||||||
* to give 3-4 more frames per second.
|
|
||||||
*
|
|
||||||
* Created by minjaesong on 2017-01-02.
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* Remarks:
|
|
||||||
* - NOT using the fullCodePage with 2x2 mode makes it slower... skewed tree generation?
|
|
||||||
*/
|
|
||||||
class KDHeapifiedTree(actors: List<ActorWithBody>) {
|
|
||||||
|
|
||||||
private val dimension = 2
|
|
||||||
private val initialSize = 128
|
|
||||||
private val nodes = Array<ActorWithBody?>(initialSize, { null })
|
|
||||||
|
|
||||||
private val root: Int = 0
|
|
||||||
|
|
||||||
fun findNearestActor(query: Point2d): ActorWithBody =
|
|
||||||
getNearest(root, query, 0).getActor()!!
|
|
||||||
|
|
||||||
private fun Int.get() = nodes[this]?.feetPosPoint
|
|
||||||
private fun Int.getActor() = nodes[this]
|
|
||||||
private fun Int.getLeft() = this * 2 + 1
|
|
||||||
private fun Int.getRight() = this * 2 + 2
|
|
||||||
private fun Int.set(value: ActorWithBody?) {
|
|
||||||
try {
|
|
||||||
nodes[this] = value
|
|
||||||
}
|
|
||||||
catch (_: ArrayIndexOutOfBoundsException) {
|
|
||||||
// modification of the private fun expandArray()
|
|
||||||
val prevNodes = nodes.copyOf() + value
|
|
||||||
Array<ActorWithBody?>(prevNodes.size * 2, { null })
|
|
||||||
create(prevNodes.toList(), 0, 0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
private fun Int.setLeftChild(value: ActorWithBody?) { nodes[this.getLeft()] = value }
|
|
||||||
private fun Int.setRightChild(value: ActorWithBody?) { nodes[this.getRight()] = value }
|
|
||||||
|
|
||||||
private val zeroPoint = Point2d(0.0, 0.0)
|
|
||||||
|
|
||||||
init {
|
|
||||||
create(actors, 0, 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun create(points: List<ActorWithBody?>, depth: Int, index: Int): ActorWithBody? {
|
|
||||||
if (points.isEmpty()) {
|
|
||||||
index.set(null)
|
|
||||||
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
val items = points.sortedBy {
|
|
||||||
if (it != null) it.feetPosPoint[depth % dimension]
|
|
||||||
else Double.POSITIVE_INFINITY
|
|
||||||
}
|
|
||||||
val halfItems = items.size shr 1
|
|
||||||
|
|
||||||
index.setLeftChild(create(items.subList(0, halfItems), depth + 1, index.getLeft()))
|
|
||||||
index.setRightChild(create(items.subList(halfItems + 1, items.size), depth + 1, index.getRight()))
|
|
||||||
index.set(items[halfItems])
|
|
||||||
|
|
||||||
return index.getActor()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun getNearest(currentNode: Int, query: Point2d, depth: Int): Int {
|
|
||||||
//println("depth, $depth")
|
|
||||||
|
|
||||||
val direction = currentNode.compare(query, depth % dimension)
|
|
||||||
|
|
||||||
val next = if (direction < 0) currentNode.getLeft() else currentNode.getRight()
|
|
||||||
val other = if (direction < 0) currentNode.getRight() else currentNode.getLeft()
|
|
||||||
var best = if (next.get() == null)
|
|
||||||
currentNode
|
|
||||||
else
|
|
||||||
getNearest(next, query, depth + 1) // traverse to leaf
|
|
||||||
|
|
||||||
if (currentNode.get()!!.distSqr(query) < best.get()!!.distSqr(query)) {
|
|
||||||
best = currentNode
|
|
||||||
}
|
|
||||||
|
|
||||||
if (other.get() != null) {
|
|
||||||
if (currentNode.get()!!.dimDistSqr(query, depth % dimension) < best.get()!!.distSqr(query)) {
|
|
||||||
val bestCandidate = getNearest(other, query, depth + 1)
|
|
||||||
if (bestCandidate.get()!!.distSqr(query) < best.get()!!.distSqr(query)) {
|
|
||||||
best = bestCandidate
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return best // work back up
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun expandArray() {
|
|
||||||
val prevNodes = nodes.copyOf()
|
|
||||||
Array<ActorWithBody?>(prevNodes.size * 2, { null })
|
|
||||||
create(prevNodes.toList(), 0, 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun Int.compare(other: Point2d, dimension: Int) =
|
|
||||||
other[dimension] - this.get()!![dimension]
|
|
||||||
|
|
||||||
private fun Point2d.dimDistSqr(other: Point2d, dimension: Int) =
|
|
||||||
other[dimension].minus(this[dimension]).sqr()
|
|
||||||
|
|
||||||
private operator fun Point2d.get(index: Int) = if (index == 0) this.x else this.y
|
|
||||||
}
|
|
||||||
@@ -1,82 +0,0 @@
|
|||||||
package net.torvald.aa
|
|
||||||
|
|
||||||
import net.torvald.terrarum.gameactors.ActorWithBody
|
|
||||||
import net.torvald.terrarum.gameactors.Hitbox
|
|
||||||
import net.torvald.terrarum.sqr
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Created by minjaesong on 2019-04-18.
|
|
||||||
*/
|
|
||||||
class KDTree(points: List<ActorWithBody>) {
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
const val DIMENSION = 2
|
|
||||||
}
|
|
||||||
|
|
||||||
private val root: KDNode?
|
|
||||||
|
|
||||||
init {
|
|
||||||
root = create(points, 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun findNearest(query: ActorWithBody) = getNearest(root!!, query.hitbox, 0)
|
|
||||||
|
|
||||||
private fun create(points: List<ActorWithBody>, depth: Int): KDNode? {
|
|
||||||
if (points.isEmpty()) {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
val items = points.sortedBy { it.getDimensionalPoint(depth) }
|
|
||||||
val currentIndex = items.size shr 1
|
|
||||||
|
|
||||||
return KDNode(
|
|
||||||
create(items.subList(0, currentIndex), depth + 1),
|
|
||||||
create(items.subList(currentIndex + 1, items.size), depth + 1),
|
|
||||||
items[currentIndex],
|
|
||||||
items[currentIndex].hitbox.clone()
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun getNearest(currentNode: KDNode, actorHitbox: Hitbox, depth: Int = 0): KDNode {
|
|
||||||
val direction = currentNode.compare(actorHitbox, depth % DIMENSION)
|
|
||||||
|
|
||||||
val next = if (direction < 0) currentNode.left else currentNode.right
|
|
||||||
val other = if (direction < 0) currentNode.right else currentNode.left
|
|
||||||
var best = if (next == null) currentNode else getNearest(next, actorHitbox, depth + 1) // traverse to leaf
|
|
||||||
|
|
||||||
if (currentNode.position.distSqr(actorHitbox) < best.position.distSqr(actorHitbox)) {
|
|
||||||
best = currentNode
|
|
||||||
}
|
|
||||||
|
|
||||||
if (other != null) {
|
|
||||||
if (currentNode.position.dimDistSqr(actorHitbox, depth % DIMENSION) < best.position.distSqr(actorHitbox)) {
|
|
||||||
val bestCandidate = getNearest(other, actorHitbox, depth + 1)
|
|
||||||
if (bestCandidate.position.distSqr(actorHitbox) < best.position.distSqr(actorHitbox)) {
|
|
||||||
best = bestCandidate
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return best // work back up
|
|
||||||
}
|
|
||||||
|
|
||||||
data class KDNode(val left: KDNode?, val right: KDNode?, val actor: ActorWithBody, val position: Hitbox) {
|
|
||||||
//fun compare(other: ActorWithBody, dimension: Int) = other.getDimensionalPoint(dimension) - this.position.getDimensionalPoint(dimension)
|
|
||||||
fun compare(other: Hitbox, dimension: Int) = other.getDimensionalPoint(dimension) - this.position.getDimensionalPoint(dimension)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun Hitbox.distSqr(other: Hitbox): Double {
|
|
||||||
var dist = 0.0
|
|
||||||
for (i in 0 until DIMENSION)
|
|
||||||
dist += (this.getDimensionalPoint(i) - other.getDimensionalPoint(i)).sqr()
|
|
||||||
return dist
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun Hitbox.dimDistSqr(other: Hitbox, dimension: Int) = other.getDimensionalPoint(dimension).minus(this.getDimensionalPoint(dimension)).sqr()
|
|
||||||
}
|
|
||||||
|
|
||||||
internal fun ActorWithBody.getDimensionalPoint(depth: Int) = this.hitbox.getDimensionalPoint(depth)
|
|
||||||
// TODO take ROUNDWORLD into account
|
|
||||||
internal fun Hitbox.getDimensionalPoint(depth: Int) =
|
|
||||||
if (depth % KDTree.DIMENSION == 0) this.canonicalX else this.canonicalY
|
|
||||||
Reference in New Issue
Block a user