thread pooling terraingen, WIP

Former-commit-id: 1d0687d8b34d5e8192b652904a437cdb29f27b10
Former-commit-id: 1c06ce97a59eb13455cc180a4d5f13ffbead1f84
This commit is contained in:
Song Minjae
2016-06-13 00:48:37 +09:00
parent f98cd773b1
commit 9f42ae9639
8 changed files with 180 additions and 46 deletions

View File

@@ -373,7 +373,7 @@ constructor() : BasicGameState() {
}
fun updateActors(gc: GameContainer, delta: Int) {
if (CORES >= 2 && Terrarum.getConfigBoolean("multithread")) {
if (false) { // don't multithread this for now, it's SLOWER //if (Terrarum.MULTITHREAD) {
val actors = actorContainer.size.toFloat()
// set up indices
for (i in 0..ThreadPool.POOL_SIZE - 1) {

View File

@@ -114,6 +114,15 @@ constructor(gamename: String) : StateBasedGame(gamename) {
/** Available CPU cores */
val CORES = Runtime.getRuntime().availableProcessors();
/**
* If the game is multithreading.
* True if:
*
* CORES >= 2 and config "multithread" is true
*/
val MULTITHREAD: Boolean
get() = CORES >= 2 && getConfigBoolean("multithread")
private lateinit var configDir: String
/**

View File

@@ -1,6 +1,6 @@
package net.torvald.terrarum
package net.torvald.terrarum.gameactors
import net.torvald.terrarum.gameactors.Player
import net.torvald.terrarum.Terrarum
import org.newdawn.slick.GameContainer
/**

View File

@@ -6,12 +6,15 @@ import net.torvald.terrarum.tileproperties.TileNameCode
import com.jme3.math.FastMath
import com.sudoplay.joise.Joise
import com.sudoplay.joise.module.*
import net.torvald.terrarum.Terrarum
import net.torvald.terrarum.concurrent.ThreadPool
import net.torvald.terrarum.gameactors.ThreadActorUpdate
import java.util.*
object MapGenerator {
private lateinit var map: GameMap
private lateinit var random: Random
internal lateinit var map: GameMap
internal lateinit var random: Random
//private static float[] noiseArray;
var SEED: Long = 0
var WIDTH: Int = 0
@@ -56,7 +59,7 @@ object MapGenerator {
private val GRASSCUR_DOWN = 2
private val GRASSCUR_LEFT = 3
private val TILE_MACRO_ALL = -1
internal val TILE_MACRO_ALL = -1
fun attachMap(map: GameMap) {
this.map = map
@@ -665,33 +668,50 @@ object MapGenerator {
}
private fun processNoiseLayers(noiseRecords: Array<TaggedJoise>) {
for (record in noiseRecords) {
println("[mapgenerator] ${record.message}...")
for (y in 0..HEIGHT - 1) {
for (x in 0..WIDTH - 1) {
val noise: Float = record.noiseModule.get(
x.toDouble() / 48.0, // 48: Fixed value
y.toDouble() / 48.0
).toFloat()
val fromTerr = record.replaceFromTerrain
val fromWall = record.replaceFromWall
val to: Int = when(record.replaceTo) {
is Int -> record.replaceTo as Int
is IntArray -> (record.replaceTo as IntArray)[random.nextInt((record.replaceTo as IntArray).size)]
else -> throw IllegalArgumentException("[mapgenerator] Unknown replaceTo tile type '${record.replaceTo.javaClass.canonicalName}': Only 'kotlin.Int' and 'kotlin.IntArray' is valid.")
}
if (to == TILE_MACRO_ALL) throw IllegalArgumentException("[mapgenerator] Invalid replaceTo: TILE_MACRO_ALL")
val threshold = record.filter.getGrad(y, record.filterArg1, record.filterArg2)
if (noise > threshold * record.scarcity) {
if ((map.getTileFromTerrain(x, y) == fromTerr || fromTerr == TILE_MACRO_ALL)
&& (map.getTileFromWall(x, y) == fromWall || fromWall == TILE_MACRO_ALL)) {
map.setTileTerrain(x, y, to)
}
}
}
if (Terrarum.MULTITHREAD) {
// set up indices
for (i in 0..ThreadPool.POOL_SIZE - 1) {
ThreadPool.map(
i,
ThreadProcessNoiseLayers(
((HEIGHT / Terrarum.CORES) * i).toInt(),
((HEIGHT / Terrarum.CORES) * i.plus(1)).toInt() - 1,
noiseRecords
),
"SampleJoiseMap"
)
}
ThreadPool.startAll()
// FIXME game starts prematurely
/* Console:
[mapgenerator] Seed: 85336530
[mapgenerator] Raising and eroding terrain...
[mapgenerator] Shaping world...
[mapgenerator] Carving caves...
[mapgenerator] Carving caves...
[mapgenerator] Carving caves...
[mapgenerator] Flooding bottom lava...
[mapgenerator] Carving caves...
[mapgenerator] Planting grass...
[mapgenerator] Placing floating islands...
[UIHandler] Creating UI 'ConsoleWindow'
Mon Jun 13 00:43:57 KST 2016 INFO:Offscreen Buffers FBO=true PBUFFER=true PBUFFERRT=false
Mon Jun 13 00:43:57 KST 2016 DEBUG:Creating FBO 2048x256
[UIHandler] Creating UI 'BasicDebugInfoWindow'
Mon Jun 13 00:43:57 KST 2016 INFO:Offscreen Buffers FBO=true PBUFFER=true PBUFFERRT=false
Mon Jun 13 00:43:57 KST 2016 DEBUG:Creating FBO 2048x1024
[UIHandler] Creating UI 'Notification'
Mon Jun 13 00:43:57 KST 2016 INFO:Offscreen Buffers FBO=true PBUFFER=true PBUFFERRT=false
Mon Jun 13 00:43:57 KST 2016 DEBUG:Creating FBO 512x64
[mapgenerator] Collapsing caves...
[mapgenerator] Collapsing caves...
[mapgenerator] Collapsing caves...
[mapgenerator] Collapsing caves...
*/
}
else {
ThreadProcessNoiseLayers(0, HEIGHT - 1, noiseRecords).run()
}
}

View File

@@ -0,0 +1,46 @@
package net.torvald.terrarum.mapgenerator
/**
* Created by minjaesong on 16-06-13.
*/
class ThreadProcessNoiseLayers(val startIndex: Int, val endIndex: Int,
val noiseRecords: Array<MapGenerator.TaggedJoise>) : Runnable {
override fun run() {
for (record in noiseRecords) {
println("[mapgenerator] ${record.message}...")
for (y in startIndex..endIndex) {
for (x in 0..MapGenerator.WIDTH - 1) {
val noise: Float = record.noiseModule.get(
x.toDouble() / 48.0, // 48: Fixed value
y.toDouble() / 48.0
).toFloat()
val fromTerr = record.replaceFromTerrain
val fromWall = record.replaceFromWall
val to: Int = when (record.replaceTo) {
// replace to designated tile
is Int -> record.replaceTo as Int
// replace to randomly selected tile from given array of tile IDs
is IntArray -> (record.replaceTo as IntArray)[MapGenerator.random.nextInt((record.replaceTo as IntArray).size)]
else -> throw IllegalArgumentException("[mapgenerator] Unknown replaceTo tile type '${record.replaceTo.javaClass.canonicalName}': Only 'kotlin.Int' and 'kotlin.IntArray' is valid.")
}
// replace to ALL? this is bullshit
if (to == MapGenerator.TILE_MACRO_ALL) throw IllegalArgumentException("[mapgenerator] Invalid replaceTo: TILE_MACRO_ALL")
// filtered threshold
val threshold = record.filter.getGrad(y, record.filterArg1, record.filterArg2)
if (noise > threshold * record.scarcity) {
if ((MapGenerator.map.getTileFromTerrain(x, y) == fromTerr || fromTerr == MapGenerator.TILE_MACRO_ALL)
&& (MapGenerator.map.getTileFromWall(x, y) == fromWall || fromWall == MapGenerator.TILE_MACRO_ALL)) {
MapGenerator.map.setTileTerrain(x, y, to)
}
}
}
}
}
}
}

View File

@@ -45,7 +45,7 @@
"11"; "1";"TILE_TORCH_FROST" ; "8396808"; "0"; "N/A"; "0"; "0"; "0"; "81916159"; "11"; "1"; "0";"16"
"12"; "0";"TILE_TORCH" ; "8396808"; "0"; "N/A"; "0"; "0"; "0"; "0"; "11"; "0"; "0";"16"
"12"; "1";"TILE_TORCH_FROST" ; "8396808"; "0"; "N/A"; "0"; "0"; "0"; "0"; "11"; "1"; "0";"16"
"13"; "0";"TILE_ILLUMINATOR_WHITE" ; "8396808"; "0"; "N/A"; "0"; "1"; "1"; "248768744"; "13"; "0"; "0";"16"
"13"; "0";"TILE_ILLUMINATOR_WHITE" ; "8396808"; "0"; "N/A"; "0"; "1"; "1"; "239319274"; "13"; "0"; "0";"16"
"13"; "1";"TILE_ILLUMINATOR_YELLOW" ; "8396808"; "0"; "N/A"; "0"; "1"; "1"; "246656000"; "13"; "1"; "0";"16"
"13"; "2";"TILE_ILLUMINATOR_ORANGE" ; "8396808"; "0"; "N/A"; "0"; "1"; "1"; "246602752"; "13"; "2"; "0";"16"
"13"; "3";"TILE_ILLUMINATOR_RED" ; "8396808"; "0"; "N/A"; "0"; "1"; "1"; "246415360"; "13"; "3"; "0";"16"
@@ -128,12 +128,12 @@
# dsty: density. As we are putting water an 1000, it is identical to specific gravity. [g/l]
## Illuminants ##
## Illuminators ##
# Illuminant white: RGB(237,250,232), simulation of mercury-vapour lamp (If you want high CRI lamp, collect a daylight!)
# Illuminator white: RGB(228,238,234), simulation of a halophosphate FL lamp (If you want high CRI lamp, collect a daylight!)
# Defalut torch : L 70 a 51 b 59; real candlelight colour taken from properly configured camera.
# Sunstone: Artificial sunlight, change colour over time in sync with sunlight. Set by game's code.
# Sunlight capacitor: daylight at 11h of 22h day. Set by game's code.
# Sunstone: Artificial sunlight, change colour over time in sync with sunlight. The light is set by game's code.
# Sunlight capacitor: daylight at noon. Set by game's code.
## Tiles ##
Can't render this file because it contains an unexpected character in line 1 and column 18.