codes to produce fluid atlas

This commit is contained in:
minjaesong
2019-03-03 00:25:42 +09:00
parent 44745bfad1
commit 58bbb73cb1
7 changed files with 292 additions and 191 deletions

View File

@@ -16,15 +16,15 @@ import java.io.IOException
*/
object BlockCodex {
private var blockProps: Array<BlockProp>
private var blockProps = HashMap<Int, BlockProp>()
/** 4096 */
const val MAX_TERRAIN_TILES = MapLayer.RANGE * PairedMapLayer.RANGE
private val nullProp = BlockProp()
init {
blockProps = Array<BlockProp>(MAX_TERRAIN_TILES * 2, { BlockProp() })
}
var highestNumber = -1
private set
/**
* Later entry (possible from other modules) will replace older ones
@@ -36,12 +36,18 @@ object BlockCodex {
AppLoader.printdbg(this, "Building block properties table")
records.forEach {
if (intVal(it, "id") == -1) {
/*if (intVal(it, "id") == -1) {
setProp(nullProp, it)
}
else {
setProp(blockProps[intVal(it, "id")], it)
}
}*/
val id = intVal(it, "id")
setProp(id, it)
if (id > highestNumber)
highestNumber = id
}
}
catch (e: IOException) {
@@ -68,7 +74,7 @@ object BlockCodex {
}
try {
return blockProps[rawIndex]
return blockProps[rawIndex]!!
}
catch (e: NullPointerException) {
throw NullPointerException("Blockprop with raw id $rawIndex does not exist.")
@@ -77,11 +83,11 @@ object BlockCodex {
operator fun get(fluidType: FluidType?): BlockProp {
if (fluidType == null || fluidType.value == 0) {
return blockProps[Block.AIR]
return blockProps[Block.AIR]!!
}
try {
return blockProps[fluidType.abs() + GameWorld.TILES_SUPPORTED - 1]
return blockProps[fluidType.abs() + GameWorld.TILES_SUPPORTED - 1]!!
}
catch (e: NullPointerException) {
throw NullPointerException("Blockprop with raw id $fluidType does not exist.")
@@ -89,22 +95,14 @@ object BlockCodex {
}
fun getOrNull(rawIndex: Int?): BlockProp? {
if (rawIndex == null || rawIndex == Block.NULL) {
return null
}
try {
return blockProps[rawIndex]
}
catch (e: NullPointerException) {
throw NullPointerException("Blockprop with raw id $rawIndex does not exist.")
}
return blockProps[rawIndex]
}
private fun setProp(prop: BlockProp, record: CSVRecord) {
private fun setProp(key: Int, record: CSVRecord) {
val prop = BlockProp()
prop.nameKey = record.get("name")
prop.id = intVal(record, "id")
prop.id = if (key == -1) 0 else intVal(record, "id")
prop.drop = intVal(record, "drop")
prop.shadeColR = floatVal(record, "shdr") / LightmapRenderer.MUL_FLOAT
@@ -124,6 +122,7 @@ object BlockCodex {
prop.friction = intVal(record, "fr")
prop.viscosity = intVal(record, "vscs")
prop.colour = str16ToInt(record, "colour")
//prop.isFluid = boolVal(record, "fluid")
prop.isSolid = boolVal(record, "solid")
@@ -135,10 +134,25 @@ object BlockCodex {
prop.dynamicLuminosityFunction = intVal(record, "dlfn")
blockProps[key] = prop
print("${intVal(record, "id")}")
println("\t" + prop.nameKey)
}
private fun str16ToInt(rec: CSVRecord, s: String): Int {
var ret = 0
try {
ret = rec.get(s).toLong(16).toInt()
}
catch (e: NumberFormatException) {
}
catch (e1: IllegalStateException) {
}
return ret
}
private fun intVal(rec: CSVRecord, s: String): Int {
var ret = -1
try {

View File

@@ -22,6 +22,7 @@ class BlockProp {
var strength: Int = 0
var density: Int = 0
var viscosity: Int = 0
var colour: Int = 0
var isSolid: Boolean = false
//var isClear: Boolean = false

View File

@@ -28,12 +28,12 @@ import java.util.Properties;
public class CSVEditor extends JFrame {
/** Default columns. When you open existing csv, it should overwrite this. */
private String[] columns = new String[]{"id", "drop", "name", "shdr", "shdg", "shdb", "shduv", "str", "dsty", "mate", "solid", "plat", "wall", "fall", "dlfn", "fv", "fr", "lumr", "lumg", "lumb", "lumuv", "colour"};
private String[] columns = new String[]{"id", "drop", "name", "shdr", "shdg", "shdb", "shduv", "str", "dsty", "mate", "solid", "plat", "wall", "fall", "dlfn", "fv", "fr", "lumr", "lumg", "lumb", "lumuv", "colour", "vscs"};
private final int FOUR_DIGIT = 42;
private final int SIX_DIGIT = 50;
private final int TWO_DIGIT = 30;
private final int ARBITRARY = 240;
private int[] colWidth = new int[]{FOUR_DIGIT, FOUR_DIGIT, ARBITRARY, SIX_DIGIT, SIX_DIGIT, SIX_DIGIT, SIX_DIGIT, TWO_DIGIT, FOUR_DIGIT, FOUR_DIGIT, TWO_DIGIT, TWO_DIGIT, TWO_DIGIT, TWO_DIGIT, TWO_DIGIT, TWO_DIGIT, TWO_DIGIT, SIX_DIGIT, SIX_DIGIT, SIX_DIGIT, SIX_DIGIT, FOUR_DIGIT * 2};
private int[] colWidth = new int[]{FOUR_DIGIT, FOUR_DIGIT, ARBITRARY, SIX_DIGIT, SIX_DIGIT, SIX_DIGIT, SIX_DIGIT, TWO_DIGIT, FOUR_DIGIT, FOUR_DIGIT, TWO_DIGIT, TWO_DIGIT, TWO_DIGIT, TWO_DIGIT, TWO_DIGIT, TWO_DIGIT, TWO_DIGIT, SIX_DIGIT, SIX_DIGIT, SIX_DIGIT, SIX_DIGIT, FOUR_DIGIT * 2, TWO_DIGIT};
private final int UNDO_BUFFER_SIZE = 10;
@@ -500,7 +500,8 @@ public class CSVEditor extends JFrame {
"dlfn=Dynamic Light Function. 0=Static. Please see <strong>notes</strong>\n" +
"fv=Vertical friction when player slide on the cliff. 0 means not slide-able\n" +
"fr=Horizontal friction. &lt;16:slippery 16:regular &gt;16:sticky\n" +
"colour=Colour of the block in hexadecimal RGBA. Only makes sense for fluids (id >= 4096)\n";
"colour=[Fluids] Colour of the block in hexadecimal RGBA.\n" +
"vscs=[Fluids] Viscocity of the block. 16 for water.\n";
/**
* ¤ is used as a \n marker

View File

@@ -36,66 +36,70 @@ class EntryPoint : ModuleEntryPoint() {
// blocks.csvs are loaded by ModMgr beforehand
// block items (blocks and walls are the same thing basically)
for (i in ItemCodex.ITEM_TILES + ItemCodex.ITEM_WALLS) {
ItemCodex.itemCodex[i] = object : GameItem() {
override val originalID = i
override var dynamicID = i
override val isUnique: Boolean = false
override var baseMass: Double = BlockCodex[i].density / 1000.0
override var baseToolSize: Double? = null
override val originalName = BlockCodex[i % ItemCodex.ITEM_WALLS.first].nameKey
override var stackable = true
override var inventoryCategory = if (i in ItemCodex.ITEM_TILES) Category.BLOCK else Category.WALL
override var isDynamic = false
override val material = Material(0,0,0,0,0,0,0,0,0,0.0)
val blockProp = BlockCodex.getOrNull(i % ItemCodex.ITEM_WALLS.first)
init {
equipPosition = EquipPosition.HAND_GRIP
if (blockProp != null) {
ItemCodex.itemCodex[i] = object : GameItem() {
override val originalID = i
override var dynamicID = i
override val isUnique: Boolean = false
override var baseMass: Double = blockProp.density / 1000.0
override var baseToolSize: Double? = null
override val originalName = blockProp.nameKey
override var stackable = true
override var inventoryCategory = if (i in ItemCodex.ITEM_TILES) Category.BLOCK else Category.WALL
override var isDynamic = false
override val material = Material(0, 0, 0, 0, 0, 0, 0, 0, 0, 0.0)
if (IS_DEVELOPMENT_BUILD)
print("$originalID ")
}
init {
equipPosition = EquipPosition.HAND_GRIP
override fun startPrimaryUse(delta: Float): Boolean {
val ingame = Terrarum.ingame!! as Ingame
if (IS_DEVELOPMENT_BUILD)
print("$originalID ")
}
val mousePoint = Point2d(Terrarum.mouseTileX.toDouble(), Terrarum.mouseTileY.toDouble())
override fun startPrimaryUse(delta: Float): Boolean {
val ingame = Terrarum.ingame!! as Ingame
// check for collision with actors (BLOCK only)
if (this.inventoryCategory == Category.BLOCK) {
ingame.actorContainerActive.forEach {
if (it is ActorWBMovable && it.hIntTilewiseHitbox.intersects(mousePoint))
return false
val mousePoint = Point2d(Terrarum.mouseTileX.toDouble(), Terrarum.mouseTileY.toDouble())
// check for collision with actors (BLOCK only)
if (this.inventoryCategory == Category.BLOCK) {
ingame.actorContainerActive.forEach {
if (it is ActorWBMovable && it.hIntTilewiseHitbox.intersects(mousePoint))
return false
}
}
}
// return false if the tile is already there
if (this.inventoryCategory == Category.BLOCK &&
this.dynamicID == ingame.world.getTileFromTerrain(Terrarum.mouseTileX, Terrarum.mouseTileY) ||
this.inventoryCategory == Category.WALL &&
this.dynamicID - ItemCodex.ITEM_WALLS.start == ingame.world.getTileFromWall(Terrarum.mouseTileX, Terrarum.mouseTileY) ||
this.inventoryCategory == Category.WIRE &&
this.dynamicID - ItemCodex.ITEM_WIRES.start == ingame.world.getTileFromWire(Terrarum.mouseTileX, Terrarum.mouseTileY)
)
return false
// filter passed, do the job
// FIXME this is only useful for Player
if (i in ItemCodex.ITEM_TILES) {
ingame.world.setTileTerrain(
Terrarum.mouseTileX,
Terrarum.mouseTileY,
i
// return false if the tile is already there
if (this.inventoryCategory == Category.BLOCK &&
this.dynamicID == ingame.world.getTileFromTerrain(Terrarum.mouseTileX, Terrarum.mouseTileY) ||
this.inventoryCategory == Category.WALL &&
this.dynamicID - ItemCodex.ITEM_WALLS.start == ingame.world.getTileFromWall(Terrarum.mouseTileX, Terrarum.mouseTileY) ||
this.inventoryCategory == Category.WIRE &&
this.dynamicID - ItemCodex.ITEM_WIRES.start == ingame.world.getTileFromWire(Terrarum.mouseTileX, Terrarum.mouseTileY)
)
}
else {
ingame.world.setTileWall(
Terrarum.mouseTileX,
Terrarum.mouseTileY,
i
)
}
return false
return true
// filter passed, do the job
// FIXME this is only useful for Player
if (i in ItemCodex.ITEM_TILES) {
ingame.world.setTileTerrain(
Terrarum.mouseTileX,
Terrarum.mouseTileY,
i
)
}
else {
ingame.world.setTileWall(
Terrarum.mouseTileX,
Terrarum.mouseTileY,
i
)
}
return true
}
}
}
}

View File

@@ -107,6 +107,8 @@ internal object BlocksDrawer {
//PixmapIO2.writeTGA(Gdx.files.absolute("${AppLoader.defaultDir}/atlasWinter.tga"), CreateTileAtlas.atlasWinter, false)
//printdbg(this, "Writing pixmap as tga: atlasSpring.tga")
//PixmapIO2.writeTGA(Gdx.files.absolute("${AppLoader.defaultDir}/atlasSpring.tga"), CreateTileAtlas.atlasSpring, false)
//printdbg(this, "Writing pixmap as tga: atlasFluid.tga")
//PixmapIO2.writeTGA(Gdx.files.absolute("${AppLoader.defaultDir}/atlasFluid.tga"), CreateTileAtlas.atlasFluid, false)
@@ -139,7 +141,7 @@ internal object BlocksDrawer {
val itemWallPixmap = Pixmap(16 * TILE_SIZE, TILES_IN_X * TILE_SIZE, Pixmap.Format.RGBA8888)
CreateTileAtlas.tags.toMap().forEach { t, u ->
val tilePosFromAtlas = u.atlasStartingPosition + maskTypetoTileIDForItemImage(u.maskType)
val tilePosFromAtlas = u.tileNumber + maskTypetoTileIDForItemImage(u.maskType)
val srcX = (tilePosFromAtlas % TILES_IN_X) * TILE_SIZE
val srcY = (tilePosFromAtlas / TILES_IN_X) * TILE_SIZE
val destX = (t % 16) * TILE_SIZE
@@ -347,7 +349,7 @@ internal object BlocksDrawer {
}
val renderTag = CreateTileAtlas.getRenderTag(thisTile)
val tileNumberBase = renderTag.atlasStartingPosition
val tileNumberBase = renderTag.tileNumber
val tileNumber = tileNumberBase + when (renderTag.maskType) {
CreateTileAtlas.RenderTag.MASK_NA -> 0
CreateTileAtlas.RenderTag.MASK_16 -> connectLut16[nearbyTilesInfo]

View File

@@ -2,12 +2,16 @@ package net.torvald.terrarum.worlddrawer
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.files.FileHandle
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.Pixmap
import com.badlogic.gdx.utils.GdxRuntimeException
import net.torvald.terrarum.AppLoader.printdbg
import net.torvald.terrarum.ModMgr
import net.torvald.terrarum.blockproperties.BlockCodex
import net.torvald.terrarum.blockproperties.Fluid
import net.torvald.terrarum.gameworld.GameWorld
import net.torvald.terrarum.toInt
import net.torvald.terrarum.worlddrawer.FeaturesDrawer.TILE_SIZE
import kotlin.math.roundToInt
/**
* This class implements work_files/dynamic_shape_2_0.psd
@@ -22,6 +26,7 @@ object CreateTileAtlas {
lateinit var atlasAutumn: Pixmap
lateinit var atlasWinter: Pixmap
lateinit var atlasSpring: Pixmap
lateinit var atlasFluid: Pixmap
internal lateinit var tags: HashMap<Int, RenderTag>
private set
private val defaultRenderTag = RenderTag(3, RenderTag.CONNECT_SELF, RenderTag.MASK_NA) // 'update' block
@@ -31,6 +36,7 @@ object CreateTileAtlas {
/** 0000.tga, 1.tga.gz, 3242423.tga, 000033.tga.gz */
// for right now, TGA file only, no gzip
private val validTerrainTilesFilename = Regex("""[0-9]+\.tga""")//Regex("""[0-9]+\.tga(.gz)?""")
private val validFluidTilesFilename = Regex("""fluid_[0-9]+\.tga""")
// 16 tiles are reserved for internal use: solid black, solid white, breakage stages.
// 0th tile is complete transparent tile and is also a BlockID of zero: air.
@@ -38,7 +44,7 @@ object CreateTileAtlas {
private val atlasInit = "./assets/graphics/blocks/init.tga"
/**
* Must be called AFTER mods' loading
* Must be called AFTER mods' loading so that all the block props are loaded
*/
operator fun invoke(updateExisting: Boolean = false) { if (updateExisting || !initialised) {
@@ -49,6 +55,7 @@ object CreateTileAtlas {
atlasAutumn = Pixmap(TILES_IN_X * TILE_SIZE, TILES_IN_X * TILE_SIZE, Pixmap.Format.RGBA8888)
atlasWinter = Pixmap(TILES_IN_X * TILE_SIZE, TILES_IN_X * TILE_SIZE, Pixmap.Format.RGBA8888)
atlasSpring = Pixmap(TILES_IN_X * TILE_SIZE, TILES_IN_X * TILE_SIZE, Pixmap.Format.RGBA8888)
atlasFluid = Pixmap(TILES_IN_X * TILE_SIZE, TILES_IN_X * TILE_SIZE, Pixmap.Format.RGBA8888)
val initMap = Pixmap(Gdx.files.internal(atlasInit))
@@ -57,21 +64,20 @@ object CreateTileAtlas {
// get all the files applicable
// first, get all the '/blocks' directory
// first, get all the '/blocks' directory, and add all the files, regardless of their extension, to the list
val tgaList = ArrayList<FileHandle>()
ModMgr.getGdxFilesFromEveryMod("blocks").forEach {
if (!it.isDirectory) {
throw Error("Path '${it.path()}' is not a directory")
}
// then, filter the file such that its name matches the regex
it.list { _, name -> name.matches(validTerrainTilesFilename) }.forEach { tgaFile ->
tgaList.add(tgaFile)
it.list().forEach { tgaFile ->
if (!tgaFile.isDirectory) tgaList.add(tgaFile)
}
}
// Sift through the file list, but TGA format first
tgaList.filter { it.extension().toUpperCase() == "TGA" }.forEach {
// Sift through the file list for blocks, but TGA format first
tgaList.filter { it.name().matches(validTerrainTilesFilename) && it.extension().toUpperCase() == "TGA" }.forEach {
try {
fileToAtlantes(it)
}
@@ -91,6 +97,72 @@ object CreateTileAtlas {
}*/
// Sift through the file list for fluids, but TGA format first
val fluidMasterPixmap = Pixmap(TILE_SIZE * 47, TILE_SIZE * 8, Pixmap.Format.RGBA8888)
tgaList.filter { it.name().matches(validFluidTilesFilename) && it.extension().toUpperCase() == "TGA" }.forEachIndexed { fluidLevel, it ->
val pixmap = Pixmap(it)
// dirty manual copy
repeat(5) {
fluidMasterPixmap.drawPixmap(pixmap,
it * TILE_SIZE * 7, fluidLevel * TILE_SIZE,
0, TILE_SIZE * it,
TILE_SIZE * 7, TILE_SIZE
)
}
repeat(2) {
fluidMasterPixmap.drawPixmap(pixmap,
(35 + it * 6) * TILE_SIZE, fluidLevel * TILE_SIZE,
0, TILE_SIZE * (5 + it),
TILE_SIZE * 6, TILE_SIZE
)
}
pixmap.dispose()
}
// test print
//PixmapIO2.writeTGA(Gdx.files.absolute("${AppLoader.defaultDir}/fluidpixmapmaster.tga"), fluidMasterPixmap, false)
// occupy the fluid pixmap with software rendering
for (i in BlockCodex.MAX_TERRAIN_TILES..BlockCodex.highestNumber) {
val fluid = Color(BlockCodex[i].colour)
// pixmap <- (color SCREEN fluidMasterPixmap)
// then occupy the atlasFluid
val pixmap = Pixmap(fluidMasterPixmap.width, fluidMasterPixmap.height, Pixmap.Format.RGBA8888)
for (y in 0 until pixmap.height) {
for (x in 0 until pixmap.width) {
val inColour = Color(fluidMasterPixmap.getPixel(x, y))
// SCREEN for RGB, MUL for A.
inColour.r = 1f - (1f - fluid.r) * (1f - inColour.r)
inColour.g = 1f - (1f - fluid.g) * (1f - inColour.g)
inColour.b = 1f - (1f - fluid.b) * (1f - inColour.b)
inColour.a = fluid.a * inColour.a
pixmap.drawPixel(x, y, inColour.toRGBA())
}
}
// test print
//PixmapIO2.writeTGA(Gdx.files.absolute("${AppLoader.defaultDir}/$i.tga"), pixmap, false)
// using the test print, I figured out that the output is alpha premultiplied.
// to the atlas
val atlasTargetPos = 1 + 47 * 8 * (i - BlockCodex.MAX_TERRAIN_TILES)
for (k in 0 until 47 * 8) {
val srcX = (k % 47) * TILE_SIZE
val srcY = (k / 47) * TILE_SIZE
val destX = ((atlasTargetPos + k) % TILES_IN_X) * TILE_SIZE
val destY = ((atlasTargetPos + k) / TILES_IN_X) * TILE_SIZE
atlasFluid.drawPixmap(pixmap, srcX, srcY, TILE_SIZE, TILE_SIZE, destX, destY, TILE_SIZE, TILE_SIZE)
}
pixmap.dispose()
}
fluidMasterPixmap.dispose()
initialised = true
} }
@@ -98,6 +170,12 @@ object CreateTileAtlas {
return tags.getOrDefault(blockID, defaultRenderTag)
}
fun fluidToTileNumber(fluid: GameWorld.FluidInfo): Int {
val fluidLevel = fluid.amount.coerceIn(0f, 1f).times(9).roundToInt()
return if (fluid.type == Fluid.NULL || fluidLevel == 0) 0 else
47 * 8 * (fluid.type.abs() - 1) + 47 * (fluidLevel - 1)
}
private fun fileToAtlantes(it: FileHandle) {
val tilesPixmap = Pixmap(it)
val blockID = it.nameWithoutExtension().toInt()
@@ -160,7 +238,7 @@ object CreateTileAtlas {
val txOfPixmap = pixmap.width / TILE_SIZE
val tyOfPixmap = pixmap.height / TILE_SIZE
for (i in 0 until tilesCount) {
printdbg(this, "Rendering to atlas, tile# $atlasCursor")
//printdbg(this, "Rendering to atlas, tile# $atlasCursor")
// different texture for different seasons (224x224)
if (seasonal) {
@@ -200,11 +278,12 @@ object CreateTileAtlas {
2 -> atlasAutumn.drawPixmap(pixmap, sourceX, sourceY, TILE_SIZE, TILE_SIZE, atlasX, atlasY, TILE_SIZE, TILE_SIZE)
3 -> atlasWinter.drawPixmap(pixmap, sourceX, sourceY, TILE_SIZE, TILE_SIZE, atlasX, atlasY, TILE_SIZE, TILE_SIZE)
4 -> atlasSpring.drawPixmap(pixmap, sourceX, sourceY, TILE_SIZE, TILE_SIZE, atlasX, atlasY, TILE_SIZE, TILE_SIZE)
5 -> atlasFluid.drawPixmap(pixmap, sourceX, sourceY, TILE_SIZE, TILE_SIZE, atlasX, atlasY, TILE_SIZE, TILE_SIZE)
}
}
}
data class RenderTag(val atlasStartingPosition: Int, val connectionType: Int, val maskType: Int) {
data class RenderTag(val tileNumber: Int, val connectionType: Int, val maskType: Int) {
companion object {
const val CONNECT_MUTUAL = 0
const val CONNECT_SELF = 1
@@ -233,6 +312,6 @@ object CreateTileAtlas {
atlasAutumn.dispose()
atlasWinter.dispose()
atlasSpring.dispose()
atlasFluid.dispose()
}
}