fixed a NaN bug caused by a zero-width/height hitbox

Also inventory is widened to 10x7 of prev 9x7
This commit is contained in:
minjaesong
2018-11-06 04:02:33 +09:00
parent 5c8cdd3162
commit c7c68187eb
14 changed files with 78 additions and 43 deletions

Binary file not shown.

View File

@@ -29,13 +29,6 @@ class CircularArray<T>(val size: Int) {
} }
inline fun forEach(action: (T) -> Unit) { inline fun forEach(action: (T) -> Unit) {
/*if (tail >= head) { // queue not full
(head..tail - 1).map { buffer[it] }.forEach { action(it) }
}
else { // queue full
(0..size - 1).map { buffer[(it + head) % size] }.forEach { action(it) }
}*/
// has slightly better iteration performance than lambda // has slightly better iteration performance than lambda
if (tail >= head) { if (tail >= head) {
for (i in head..tail - 1) for (i in head..tail - 1)
@@ -47,6 +40,22 @@ class CircularArray<T>(val size: Int) {
} }
} }
// FIXME not working as intended
inline fun <R> fold(initial: R, operation: (R, T) -> R): R {
var accumulator = initial
//for (element in buffer) accumulator = operation(accumulator, element)
if (tail >= head) {
for (i in head..tail - 1)
operation(accumulator, buffer[i])
}
else {
for (i in 0..size - 1)
operation(accumulator, buffer[(i + head) % size])
}
return accumulator
}
inline fun forEachConcurrent(action: (T) -> Unit) { inline fun forEachConcurrent(action: (T) -> Unit) {
TODO() TODO()
} }

View File

@@ -180,7 +180,7 @@ public class AppLoader implements ApplicationListener {
TextureRegionPack.Companion.setGlobalFlipY(true); TextureRegionPack.Companion.setGlobalFlipY(true);
fontGame = new GameFontBase("assets/graphics/fonts/terrarum-sans-bitmap", false, true, Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest, false); fontGame = new GameFontBase("assets/graphics/fonts/terrarum-sans-bitmap", false, true, Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest, false, 128, false);
} }
@Override @Override

View File

@@ -13,6 +13,7 @@ import com.google.gson.JsonArray
import com.google.gson.JsonPrimitive import com.google.gson.JsonPrimitive
import com.jme3.math.FastMath import com.jme3.math.FastMath
import net.torvald.dataclass.ArrayListMap import net.torvald.dataclass.ArrayListMap
import net.torvald.dataclass.CircularArray
import net.torvald.random.HQRNG import net.torvald.random.HQRNG
import net.torvald.terrarum.AppLoader.printdbg import net.torvald.terrarum.AppLoader.printdbg
import net.torvald.terrarum.AppLoader.printdbgerr import net.torvald.terrarum.AppLoader.printdbgerr
@@ -114,12 +115,29 @@ object Terrarum : Screen {
lateinit var defaultSaveDir: String lateinit var defaultSaveDir: String
private set private set
val memInUse: Long
get() = (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory() + Gdx.app.nativeHeap) shr 20
val memTotal: Long private val javaHeapCircularArray = CircularArray<Int>(128)
get() = Runtime.getRuntime().totalMemory() shr 20 private val nativeHeapCircularArray = CircularArray<Int>(128)
val memXmx: Long
get() = Runtime.getRuntime().maxMemory() shr 20 val memJavaHeap: Int
get() {
javaHeapCircularArray.add((Gdx.app.javaHeap shr 20).toInt())
var acc = 0
javaHeapCircularArray.forEach { acc = maxOf(acc, it) }
return acc
}
val memNativeHeap: Int
get() {
nativeHeapCircularArray.add((Gdx.app.javaHeap shr 20).toInt())
var acc = 0
nativeHeapCircularArray.forEach { acc = maxOf(acc, it) }
return acc
}
val memXmx: Int
get() = (Runtime.getRuntime().maxMemory() shr 20).toInt()
var environment: RunningEnvironment var environment: RunningEnvironment
private set private set

View File

@@ -217,10 +217,7 @@ class TitleScreen(val batch: SpriteBatch) : Screen {
} }
fun updateScreen(delta: Float) { fun updateScreen(delta: Float) {
Gdx.graphics.setTitle(AppLoader.GAME_NAME + Gdx.graphics.setTitle(Ingame.getCanonicalTitle())
" — F: ${Gdx.graphics.framesPerSecond} (${Terrarum.TARGET_INTERNAL_FPS})" +
" — M: ${Terrarum.memInUse}M / ${Terrarum.memTotal}M / ${Terrarum.memXmx}M"
)
demoWorld.globalLight = WeatherMixer.globalLightNow demoWorld.globalLight = WeatherMixer.globalLightNow
demoWorld.updateWorldTime(delta) demoWorld.updateWorldTime(delta)

View File

@@ -62,8 +62,8 @@ class UIItemInventoryCatBar(
val iconIndex = arrayOf(12, 16, 17, 13) val iconIndex = arrayOf(12, 16, 17, 13)
println(relativeStartX) println("[UIItemInventoryCatBar] relativeStartX: $relativeStartX")
println(posX) println("[UIItemInventoryCatBar] posX: $posX")
sideButtons = Array(iconIndex.size, { index -> sideButtons = Array(iconIndex.size, { index ->
val iconPosX = if (index < 2) val iconPosX = if (index < 2)

View File

@@ -288,6 +288,7 @@ open class ActorWBMovable(renderOrder: RenderOrder, val immobileBody: Boolean =
baseHitboxW = w baseHitboxW = w
hitboxTranslateX = tx hitboxTranslateX = tx
hitboxTranslateY = ty hitboxTranslateY = ty
hitbox.setDimension(w.toDouble(), h.toDouble())
} }
fun setPosition(pos: Point2d) = setPosition(pos.x, pos.y) fun setPosition(pos: Point2d) = setPosition(pos.x, pos.y)
@@ -1079,8 +1080,10 @@ open class ActorWBMovable(renderOrder: RenderOrder, val immobileBody: Boolean =
}*/ }*/
private inline val submergedRatio: Double private inline val submergedRatio: Double
get() = submergedHeight / hitbox.height get() {
if (hitbox.height == 0.0) throw RuntimeException("Hitbox.height is zero")
return submergedHeight / hitbox.height
}
private inline val submergedVolume: Double private inline val submergedVolume: Double
get() = submergedHeight * hitbox.width * hitbox.width get() = submergedHeight * hitbox.width * hitbox.width
@@ -1538,10 +1541,12 @@ open class ActorWBMovable(renderOrder: RenderOrder, val immobileBody: Boolean =
actorValue[AVKey.BASEMASS] = value actorValue[AVKey.BASEMASS] = value
} }
internal val avAcceleration: Double internal val avAcceleration: Double
get() = actorValue.getAsDouble(AVKey.ACCEL)!! * get() { if (accelMultMovement.isNaN()) println("accelMultMovement: $accelMultMovement")
return actorValue.getAsDouble(AVKey.ACCEL)!! *
(actorValue.getAsDouble(AVKey.ACCELBUFF) ?: 1.0) * (actorValue.getAsDouble(AVKey.ACCELBUFF) ?: 1.0) *
accelMultMovement * accelMultMovement *
scale.sqrt() scale.sqrt()
}
internal val avSpeedCap: Double internal val avSpeedCap: Double
get() = actorValue.getAsDouble(AVKey.SPEED)!! * get() = actorValue.getAsDouble(AVKey.SPEED)!! *
(actorValue.getAsDouble(AVKey.SPEEDBUFF) ?: 1.0) * (actorValue.getAsDouble(AVKey.SPEEDBUFF) ?: 1.0) *

View File

@@ -71,6 +71,12 @@ class Hitbox(x1: Double, y1: Double, width: Double, height: Double) {
fun setPosition(x1: Double, y1: Double): Hitbox { fun setPosition(x1: Double, y1: Double): Hitbox {
hitboxStart = Point2d(x1, y1) hitboxStart = Point2d(x1, y1)
if (width == 0.0 || height == 0.0) {
println("[Hitbox] width or height is zero, perhaps you want to check it out?")
Thread.currentThread().stackTrace.forEach { println(it) }
}
return this return this
} }
fun setPosition(vector: Vector2) = setPosition(vector.x, vector.y) fun setPosition(vector: Vector2) = setPosition(vector.x, vector.y)

View File

@@ -110,10 +110,8 @@ class BuildingMaker(batch: SpriteBatch) : IngameInstance(batch) {
} }
override fun render(delta: Float) { override fun render(delta: Float) {
Gdx.graphics.setTitle("${AppLoader.GAME_NAME} Building Maker" + Gdx.graphics.setTitle(Ingame.getCanonicalTitle())
" — F: ${Gdx.graphics.framesPerSecond} (${Terrarum.TARGET_INTERNAL_FPS})" +
" — M: ${Terrarum.memInUse}M / ${Terrarum.memTotal}M / ${Terrarum.memXmx}M"
)
// ASYNCHRONOUS UPDATE AND RENDER // // ASYNCHRONOUS UPDATE AND RENDER //

View File

@@ -79,6 +79,10 @@ open class Ingame(batch: SpriteBatch) : IngameInstance(batch) {
camera.update() camera.update()
batch.projectionMatrix = camera.combined batch.projectionMatrix = camera.combined
} }
fun getCanonicalTitle() = AppLoader.GAME_NAME +
" — F: ${Gdx.graphics.framesPerSecond} (${Terrarum.TARGET_INTERNAL_FPS})" +
" — M: J${Terrarum.memJavaHeap}M / N${Terrarum.memNativeHeap}M / X${Terrarum.memXmx}M"
} }
@@ -461,10 +465,7 @@ open class Ingame(batch: SpriteBatch) : IngameInstance(batch) {
Gdx.graphics.setTitle(AppLoader.GAME_NAME + Gdx.graphics.setTitle(getCanonicalTitle())
" — F: ${Gdx.graphics.framesPerSecond} (${Terrarum.TARGET_INTERNAL_FPS})" +
" — M: ${Terrarum.memInUse}M / ${Terrarum.memTotal}M / ${Terrarum.memXmx}M"
)
// ASYNCHRONOUS UPDATE AND RENDER // // ASYNCHRONOUS UPDATE AND RENDER //
@@ -913,7 +914,8 @@ open class Ingame(batch: SpriteBatch) : IngameInstance(batch) {
*/ */
override fun resize(width: Int, height: Int) { override fun resize(width: Int, height: Int) {
MegaRainGovernor.resize() // FIXME debugger is pointing at this thing, not sure it actually caused memleak
//MegaRainGovernor.resize()
IngameRenderer.resize(Terrarum.WIDTH, Terrarum.HEIGHT) IngameRenderer.resize(Terrarum.WIDTH, Terrarum.HEIGHT)

View File

@@ -31,7 +31,7 @@ class UIInventoryFull(
override var width: Int = Terrarum.WIDTH override var width: Int = Terrarum.WIDTH
override var height: Int = Terrarum.HEIGHT override var height: Int = Terrarum.HEIGHT
val internalWidth: Int = 630 val internalWidth: Int = 686
val internalHeight: Int = 558 // grad_begin..grad_end..contents..grad_begin..grad_end val internalHeight: Int = 558 // grad_begin..grad_end..contents..grad_begin..grad_end

View File

@@ -23,8 +23,8 @@ import java.util.ArrayList
* *
* Note: everything is pretty much fixed size. * Note: everything is pretty much fixed size.
* *
* Dimension of the whole area: 496x384 * Dimension of the whole area: 552x384
* Number of grids: 9x7 * Number of grids: 10x7
* Number of lists: 2x7 * Number of lists: 2x7
* *
* Created by minjaesong on 2017-10-21. * Created by minjaesong on 2017-10-21.
@@ -79,9 +79,9 @@ class UIItemInventoryDynamicList(
7 * 2, { 7 * 2, {
UIItemInventoryElem( UIItemInventoryElem(
parentUI = inventoryUI, parentUI = inventoryUI,
posX = this.posX + (244 + listGap) * (it % 2), posX = this.posX + (272 + listGap) * (it % 2),
posY = this.posY + (UIItemInventoryElem.height + listGap) * (it / 2), posY = this.posY + (UIItemInventoryElem.height + listGap) * (it / 2),
width = 244, width = 272,
item = null, item = null,
amount = UIItemInventoryElem.UNIQUE_ITEM_HAS_NO_AMOUNT, amount = UIItemInventoryElem.UNIQUE_ITEM_HAS_NO_AMOUNT,
itemImage = null, itemImage = null,
@@ -93,11 +93,11 @@ class UIItemInventoryDynamicList(
inactiveTextCol = defaultTextColour inactiveTextCol = defaultTextColour
) }) ) })
private val itemGrid = Array<UIItemInventoryCellBase>( private val itemGrid = Array<UIItemInventoryCellBase>(
7 * 9, { 7 * 10, {
UIItemInventoryElemSimple( UIItemInventoryElemSimple(
parentUI = inventoryUI, parentUI = inventoryUI,
posX = this.posX + (UIItemInventoryElemSimple.height + listGap) * (it % 9), posX = this.posX + (UIItemInventoryElemSimple.height + listGap) * (it % 10),
posY = this.posY + (UIItemInventoryElemSimple.height + listGap) * (it / 9), posY = this.posY + (UIItemInventoryElemSimple.height + listGap) * (it / 10),
item = null, item = null,
amount = UIItemInventoryElem.UNIQUE_ITEM_HAS_NO_AMOUNT, amount = UIItemInventoryElem.UNIQUE_ITEM_HAS_NO_AMOUNT,
itemImage = null, itemImage = null,

View File

@@ -320,7 +320,7 @@ open class UIRemoCon(treeRepresentation: QNDTreeNode<String>) : UICanvas() {
} }
companion object { companion object {
val remoConWidth = 280 val remoConWidth = 304
fun getRemoConHeight(menu: ArrayList<String>) = 36 * menu.size.plus(1) fun getRemoConHeight(menu: ArrayList<String>) = 36 * menu.size.plus(1)
fun getRemoConHeight(menu: Array<String>) = 36 * menu.size.plus(1) fun getRemoConHeight(menu: Array<String>) = 36 * menu.size.plus(1)
val menubarOffY: Int; get() = Terrarum.HEIGHT / 2 - (Terrarum.fontGame.lineHeight * 1.5).toInt() val menubarOffY: Int; get() = Terrarum.HEIGHT / 2 - (Terrarum.fontGame.lineHeight * 1.5).toInt()

View File

@@ -181,9 +181,9 @@ class BasicDebugInfoWindow : UICanvas() {
(Terrarum.WIDTH - 2 - 6 * 8).toFloat(), 10f) (Terrarum.WIDTH - 2 - 6 * 8).toFloat(), 10f)
//g.color = GameFontBase.codeToCol["g"] //g.color = GameFontBase.codeToCol["g"]
Terrarum.fontSmallNumbers.draw(batch, "${Terrarum.memInUse}M", Terrarum.fontSmallNumbers.draw(batch, "${Terrarum.memJavaHeap}M",
(Terrarum.WIDTH - 17 * 8 - 2).toFloat(), 2f) (Terrarum.WIDTH - 17 * 8 - 2).toFloat(), 2f)
Terrarum.fontSmallNumbers.draw(batch, "/${Terrarum.memTotal}M/", Terrarum.fontSmallNumbers.draw(batch, "/${Terrarum.memNativeHeap}M/",
(Terrarum.WIDTH - 12 * 8 - 2).toFloat(), 2f) (Terrarum.WIDTH - 12 * 8 - 2).toFloat(), 2f)
//Terrarum.fontSmallNumbers.color = GameFontBase.codeToCol["m"] //Terrarum.fontSmallNumbers.color = GameFontBase.codeToCol["m"]
Terrarum.fontSmallNumbers.draw(batch, "${Terrarum.memXmx}M", Terrarum.fontSmallNumbers.draw(batch, "${Terrarum.memXmx}M",