From aca08ea96be50150546772d3b9c35b4894f87ffa Mon Sep 17 00:00:00 2001 From: Song Minjae Date: Mon, 9 Jan 2017 23:22:31 +0900 Subject: [PATCH] sprites! Former-commit-id: 099c7d1b27988830f6c2d876684d4675078170e9 Former-commit-id: eea5b0ff2a04142e1a19505a98c252a6ca5aa666 --- src/net/torvald/aa/KDHeapifiedTree.kt | 140 ++++++++++++++++++ .../spriteanimation/SpriteAnimation.kt | 1 - .../terrarum/gameactors/DecodeTapestry.kt | 84 +++++++++++ .../terrarum/gameactors/TapestryObject.kt | 28 ++++ .../colourmap/64_from_master_final.act | Bin 772 -> 772 bytes 5 files changed, 252 insertions(+), 1 deletion(-) create mode 100644 src/net/torvald/aa/KDHeapifiedTree.kt create mode 100644 src/net/torvald/terrarum/gameactors/DecodeTapestry.kt create mode 100644 src/net/torvald/terrarum/gameactors/TapestryObject.kt diff --git a/src/net/torvald/aa/KDHeapifiedTree.kt b/src/net/torvald/aa/KDHeapifiedTree.kt new file mode 100644 index 000000000..552f54062 --- /dev/null +++ b/src/net/torvald/aa/KDHeapifiedTree.kt @@ -0,0 +1,140 @@ +package net.torvald.aa + +import net.torvald.point.Point2d +import net.torvald.terrarum.gameactors.Actor +import net.torvald.terrarum.gameactors.ActorWithBody +import net.torvald.terrarum.gameactors.sqr +import java.util.* + +/** + * 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 SKYHi14 on 2017-01-02. + * + * + * Remarks: + * - NOT using the fullCodePage with 2x2 mode makes it slower... skewed tree generation? + */ +class KDHeapifiedTree() { + + private val dimension = 2 + private val initialSize = 128 + private val nodes = Array(initialSize, { null }) + + private val root: Int = 0 + + fun findNearest(query: Point2d) = + getNearest(root, query, 0).get()!! + + 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?) { nodes[this] = value } + 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) + + private fun create(points: List, 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(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 +} + +fun intLog2(number: Int): Int { + var number = number + if (number == 0) return 0 + var log = 0 + if (number and 0xffff0000.toInt() != 0) { + number = number ushr 16 + log = 16 + } + if (number >= 256) { + number = number ushr 8 + log += 8 + } + if (number >= 16) { + number = number ushr 4 + log += 4 + } + if (number >= 4) { + number = number ushr 2 + log += 2 + } + return log + number.ushr(1) +} + +fun nextPowerOfTwo(number: Int): Int { + var number = number - 1 + number = number or (number shr 1) + number = number or (number shr 2) + number = number or (number shr 4) + number = number or (number shr 8) + number = number or (number shr 16) + number++ + number += if (number == 0) 1 else 0 + return number +} \ No newline at end of file diff --git a/src/net/torvald/spriteanimation/SpriteAnimation.kt b/src/net/torvald/spriteanimation/SpriteAnimation.kt index 572493bae..aff318941 100644 --- a/src/net/torvald/spriteanimation/SpriteAnimation.kt +++ b/src/net/torvald/spriteanimation/SpriteAnimation.kt @@ -126,7 +126,6 @@ class SpriteAnimation(val parentActor: ActorWithBody) { } val flippedImage = currentImage!!.getFlippedCopy(flipHorizontal, flipVertical) - flippedImage.draw( Math.round(posX).toFloat(), FastMath.floor(posY).toFloat(), diff --git a/src/net/torvald/terrarum/gameactors/DecodeTapestry.kt b/src/net/torvald/terrarum/gameactors/DecodeTapestry.kt new file mode 100644 index 000000000..e0a2b6970 --- /dev/null +++ b/src/net/torvald/terrarum/gameactors/DecodeTapestry.kt @@ -0,0 +1,84 @@ +package net.torvald.terrarum.gameactors + +import org.newdawn.slick.Color +import java.io.File + +object DecodeTapestry { + + val colourIndices = arrayListOf( + 0x333.fourBitCol(), + 0x600.fourBitCol(), + 0xA36.fourBitCol(), + 0x636.fourBitCol(), + 0x73B.fourBitCol(), + 0x427.fourBitCol(), + 0x44C.fourBitCol(), + 0x038.fourBitCol(), + 0x47B.fourBitCol(), + 0x466.fourBitCol(), + 0x353.fourBitCol(), + 0x453.fourBitCol(), + 0x763.fourBitCol(), + 0xA63.fourBitCol(), + 0x742.fourBitCol(), + 0x000.fourBitCol(), + 0x666.fourBitCol(), + 0xA00.fourBitCol(), + 0xE2A.fourBitCol(), + 0xD2F.fourBitCol(), + 0x92F.fourBitCol(), + 0x548.fourBitCol(), + 0x32F.fourBitCol(), + 0x36F.fourBitCol(), + 0x588.fourBitCol(), + 0x390.fourBitCol(), + 0x5F0.fourBitCol(), + 0x684.fourBitCol(), + 0xBA2.fourBitCol(), + 0xE60.fourBitCol(), + 0x854.fourBitCol(), + 0x533.fourBitCol(), + 0x999.fourBitCol(), + 0xE00.fourBitCol(), + 0xE6A.fourBitCol(), + 0xE6F.fourBitCol(), + 0x848.fourBitCol(), + 0x62F.fourBitCol(), + 0x66F.fourBitCol(), + 0x4AF.fourBitCol(), + 0x5BA.fourBitCol(), + 0x8FE.fourBitCol(), + 0x7F8.fourBitCol(), + 0x9E0.fourBitCol(), + 0xFE0.fourBitCol(), + 0xEA0.fourBitCol(), + 0xC85.fourBitCol(), + 0xE55.fourBitCol(), + 0xCCC.fourBitCol(), + 0xFFF.fourBitCol(), + 0xFDE.fourBitCol(), + 0xEAF.fourBitCol(), + 0xA3B.fourBitCol(), + 0x96F.fourBitCol(), + 0xAAF.fourBitCol(), + 0x7AF.fourBitCol(), + 0x3DF.fourBitCol(), + 0xBFF.fourBitCol(), + 0xBFB.fourBitCol(), + 0xAF6.fourBitCol(), + 0xFEB.fourBitCol(), + 0xFD7.fourBitCol(), + 0xE96.fourBitCol(), + 0xEBA.fourBitCol() + ) + + private fun Int.fourBitCol() = Color( + this.and(0xF00).shl(12) + this.and(0xF00).shl(8) + + this.and(0x0F0).shl(8) + this.and(0x0F0).shl(4) + + this.and(0x00F).shl(4) + this.and(0x00F) + ) + + operator fun invoke(file: File) { + + } +} \ No newline at end of file diff --git a/src/net/torvald/terrarum/gameactors/TapestryObject.kt b/src/net/torvald/terrarum/gameactors/TapestryObject.kt new file mode 100644 index 000000000..8317efe14 --- /dev/null +++ b/src/net/torvald/terrarum/gameactors/TapestryObject.kt @@ -0,0 +1,28 @@ +package net.torvald.terrarum.gameactors + +import org.newdawn.slick.Color +import org.newdawn.slick.GameContainer +import org.newdawn.slick.Graphics +import java.io.File + +/** + * Created by SKYHi14 on 2017-01-07. + */ +class TapestryObject(imageDataPath: String) : FixtureBase() { + + init { + + } + + override fun update(gc: GameContainer, delta: Int) { + super.update(gc, delta) + } + + override fun drawBody(gc: GameContainer, g: Graphics) { + super.drawBody(gc, g) + } + + override fun updateBodySprite(gc: GameContainer, delta: Int) { + super.updateBodySprite(gc, delta) + } +} diff --git a/work_files/graphics/colourmap/64_from_master_final.act b/work_files/graphics/colourmap/64_from_master_final.act index 135270f57c6043d8ac2297e714588728d50f5a45..14451f449b11ab97a37381ec5700e33c73a1e62b 100644 GIT binary patch delta 71 zcmV-N0J#5z280HXB^@GnnP&fHBL7uHh%+MpL`2MHX8)?H|1)O)05gb@LQWgZh*ft) dBJP=Hh*d=HyQ=O00IC20W&i;0RaLR8Spr(|9