mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-13 12:04:06 +09:00
BlockStats is upgraded to TileSurvey
This commit is contained in:
@@ -1,59 +0,0 @@
|
||||
package net.torvald.terrarum.blockstats
|
||||
|
||||
import com.jme3.math.FastMath
|
||||
import net.torvald.terrarum.App
|
||||
import net.torvald.terrarum.INGAME
|
||||
import net.torvald.terrarum.Terrarum
|
||||
import net.torvald.terrarum.TerrarumAppConfiguration.TILE_SIZE
|
||||
import net.torvald.terrarum.TerrarumAppConfiguration.TILE_SIZEF
|
||||
import net.torvald.terrarum.gameitems.ItemID
|
||||
import net.torvald.terrarum.modulebasegame.TerrarumIngame
|
||||
import net.torvald.terrarum.worlddrawer.BlocksDrawer
|
||||
|
||||
/**
|
||||
* Created by minjaesong on 2016-02-01.
|
||||
*/
|
||||
object BlockStats {
|
||||
|
||||
private val tilestat = HashMap<ItemID, Int>()
|
||||
|
||||
/**
|
||||
* Update tile stats from tiles on screen
|
||||
*/
|
||||
fun update() {
|
||||
tilestat.clear()
|
||||
|
||||
// Get stats on no-zoomed screen area. In other words, will behave as if screen zoom were 1.0
|
||||
// no matter how the screen is zoomed.
|
||||
val map = (INGAME.world)
|
||||
val player = (Terrarum.ingame!! as TerrarumIngame).actorNowPlaying
|
||||
if (player == null) return
|
||||
|
||||
val renderWidth = FastMath.ceil(App.scr.wf)
|
||||
val renderHeight = FastMath.ceil(App.scr.hf)
|
||||
|
||||
val noZoomCameraX = Math.round(FastMath.clamp(
|
||||
player.hitbox.centeredX.toFloat() - renderWidth / 2, TILE_SIZEF, map.width * TILE_SIZE - renderWidth - TILE_SIZEF))
|
||||
val noZoomCameraY = Math.round(FastMath.clamp(
|
||||
player.hitbox.centeredY.toFloat() - renderHeight / 2, TILE_SIZEF, map.width * TILE_SIZE - renderHeight - TILE_SIZEF))
|
||||
|
||||
val for_x_start = noZoomCameraX / TILE_SIZE
|
||||
val for_y_start = noZoomCameraY / TILE_SIZE
|
||||
val for_y_end = BlocksDrawer.clampHTile(for_y_start + (renderHeight / TILE_SIZE) + 2)
|
||||
val for_x_end = BlocksDrawer.clampWTile(for_x_start + (renderWidth / TILE_SIZE) + 2)
|
||||
|
||||
for (y in for_y_start..for_y_end - 1) {
|
||||
for (x in for_x_start..for_x_end - 1) {
|
||||
val tileWall = map.getTileFromWall(x, y)
|
||||
val tileTerrain = map.getTileFromTerrain(x, y)
|
||||
tilestat[tileWall] = 1 + (tilestat[tileWall] ?: 0)
|
||||
tilestat[tileTerrain] = 1 + (tilestat[tileTerrain] ?: 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun getCount(vararg tiles: ItemID): Int {
|
||||
return tiles.fold(0) { acc, key -> acc + (tilestat[key] ?: 0) }
|
||||
}
|
||||
|
||||
}
|
||||
78
src/net/torvald/terrarum/blockstats/TileSurvey.kt
Normal file
78
src/net/torvald/terrarum/blockstats/TileSurvey.kt
Normal file
@@ -0,0 +1,78 @@
|
||||
package net.torvald.terrarum.blockstats
|
||||
|
||||
import com.jme3.math.FastMath
|
||||
import net.torvald.terrarum.App
|
||||
import net.torvald.terrarum.INGAME
|
||||
import net.torvald.terrarum.Terrarum
|
||||
import net.torvald.terrarum.gameworld.GameWorld
|
||||
import net.torvald.terrarum.modulebasegame.TerrarumIngame
|
||||
import kotlin.math.ceil
|
||||
import kotlin.math.floor
|
||||
|
||||
/**
|
||||
* Created by minjaesong on 2016-02-01.
|
||||
*/
|
||||
object TileSurvey {
|
||||
|
||||
data class SurveyProposal(
|
||||
val surveyIdentifier: String,
|
||||
val width: Int,
|
||||
val height: Int,
|
||||
val spatialGranularity: Int, // 1: survey every (w*h) tile, 2: survey every other (w*h/4) tile ...
|
||||
val temporalGranularity: Int, // 1: survey every frame, 2: every other frame ...
|
||||
val predicate: (GameWorld, Int, Int) -> Boolean
|
||||
)
|
||||
|
||||
private val proposals = HashMap<String, SurveyProposal>()
|
||||
private val results = HashMap<String, Pair<Double, Int>>() // (matching tiles/actually surveyed tiles)
|
||||
|
||||
fun submitProposal(proposal: SurveyProposal) {
|
||||
proposals[proposal.surveyIdentifier] = proposal
|
||||
}
|
||||
|
||||
fun withdrawProposal(surveyIdentifier: String) {
|
||||
proposals.remove(surveyIdentifier)
|
||||
results.remove(surveyIdentifier)
|
||||
}
|
||||
|
||||
/**
|
||||
* Update tile stats from tiles on screen
|
||||
*/
|
||||
fun update() {
|
||||
|
||||
// Get stats on no-zoomed screen area. In other words, will behave as if screen zoom were 1.0
|
||||
// no matter how the screen is zoomed.
|
||||
val world = INGAME.world
|
||||
val player = (Terrarum.ingame!! as TerrarumIngame).actorNowPlaying
|
||||
if (player == null) return
|
||||
|
||||
proposals.forEach { (_, proposal) ->
|
||||
|
||||
if (INGAME.WORLD_UPDATE_TIMER % proposal.temporalGranularity == 0) {
|
||||
val for_x_start = floor(player.intTilewiseHitbox.centeredX - proposal.width / 2.0).toInt()
|
||||
val for_y_start = floor(player.intTilewiseHitbox.centeredY - proposal.height / 2.0).toInt()
|
||||
val for_x_end = ceil(player.intTilewiseHitbox.centeredX + proposal.width / 2.0).toInt()
|
||||
val for_y_end = ceil(player.intTilewiseHitbox.centeredY + proposal.height / 2.0).toInt()
|
||||
|
||||
var akku = 0
|
||||
|
||||
for (y in for_y_start until for_y_end step proposal.spatialGranularity) {
|
||||
for (x in for_x_start until for_x_end step proposal.spatialGranularity) {
|
||||
if (proposal.predicate(world, x, y)) akku += 1
|
||||
}
|
||||
}
|
||||
|
||||
val surveyedTileCount =
|
||||
(proposal.width * proposal.height) / (proposal.spatialGranularity * proposal.spatialGranularity)
|
||||
val ratio = akku.toDouble() / surveyedTileCount
|
||||
|
||||
results[proposal.surveyIdentifier] = ratio to akku
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun getRawCount(surveyIdentifier: String) = results[surveyIdentifier]?.second
|
||||
fun getRatio(surveyIdentifier: String) = results[surveyIdentifier]?.first
|
||||
fun getRatioAndRawCount(surveyIdentifier: String) = results[surveyIdentifier]
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user