refactoring around

This commit is contained in:
minjaesong
2021-08-06 13:34:17 +09:00
parent 9578488962
commit edc3d53f4e
15 changed files with 44 additions and 244 deletions

View File

@@ -17,7 +17,7 @@ import net.torvald.terrarum.modulebasegame.gameactors.physicssolver.CollisionSol
import net.torvald.terrarum.gamecontroller.IngameController
import net.torvald.terrarum.gamecontroller.KeyToggler
import net.torvald.terrarum.gameworld.GameWorld
import net.torvald.terrarum.modulebasegame.gameworld.WorldSimulator
import net.torvald.terrarum.gameworld.WorldSimulator
import net.torvald.terrarum.weather.WeatherMixer
import net.torvald.terrarum.worlddrawer.BlocksDrawer
import net.torvald.terrarum.worlddrawer.FeaturesDrawer

View File

@@ -23,7 +23,7 @@ import net.torvald.terrarum.modulebasegame.IngameRenderer
import net.torvald.terrarum.modulebasegame.TerrarumIngame
import net.torvald.terrarum.modulebasegame.gameactors.HumanoidNPC
import net.torvald.terrarum.modulebasegame.gameworld.GameWorldExtension
import net.torvald.terrarum.modulebasegame.gameworld.WorldTime
import net.torvald.terrarum.gameworld.WorldTime
import net.torvald.terrarum.modulebasegame.ui.UIRemoCon
import net.torvald.terrarum.modulebasegame.ui.UITitleRemoConYaml
import net.torvald.terrarum.weather.WeatherMixer

View File

@@ -6,7 +6,7 @@ import net.torvald.gdx.graphics.Cvec
import net.torvald.random.HQRNG
import net.torvald.terrarum.Second
import net.torvald.terrarum.Terrarum
import net.torvald.terrarum.modulebasegame.gameworld.WorldTime
import net.torvald.terrarum.gameworld.WorldTime
import net.torvald.terrarum.weather.WeatherMixer
/**

View File

@@ -10,10 +10,8 @@ import net.torvald.terrarum.blockproperties.BlockCodex
import net.torvald.terrarum.blockproperties.Fluid
import net.torvald.terrarum.gameactors.WireActor
import net.torvald.terrarum.gameitem.ItemID
import net.torvald.terrarum.modulebasegame.gameworld.WorldSimulator
import net.torvald.terrarum.realestate.LandUtil
import net.torvald.terrarum.serialise.ReadLayerDataZip
import net.torvald.terrarum.worlddrawer.CreateTileAtlas
import net.torvald.util.SortedArrayList
import org.dyn4j.geometry.Vector2
import kotlin.experimental.and
@@ -102,9 +100,11 @@ open class GameWorld : Disposable {
var disposed = false
private set
/** time in (preferably) seconds */
open var TIME_T: Long = 0L
open var dayLength: Int = 86400
val worldTime: WorldTime = WorldTime( // Year EPOCH (125), Month 1, Day 1 is implied
7 * WorldTime.HOUR_SEC +
30L * WorldTime.MINUTE_SEC
)
@TEMzPayload("TMaP", TEMzPayload.EXTERNAL_JSON)
val tileNumberToNameMap: HashMap<Int, ItemID>
@@ -356,14 +356,14 @@ open class GameWorld : Disposable {
return wiringGraph[blockAddr]?.get(itemID)?.con
}
fun getWireStateOf(x: Int, y: Int, itemID: ItemID): Vector2? {
fun getWireGeneratorStateOf(x: Int, y: Int, itemID: ItemID): Vector2? {
val (x, y) = coerceXY(x, y)
val blockAddr = LandUtil.getBlockAddr(this, x, y)
return getWireStateUnsafe(blockAddr, itemID)
return getWireGeneratorStateUnsafe(blockAddr, itemID)
}
fun getWireStateUnsafe(blockAddr: BlockAddress, itemID: ItemID): Vector2? {
return wiringGraph[blockAddr]?.get(itemID)?.state
fun getWireGeneratorStateUnsafe(blockAddr: BlockAddress, itemID: ItemID): Vector2? {
return wiringGraph[blockAddr]?.get(itemID)?.generatorState
}
fun setWireGraphOf(x: Int, y: Int, itemID: ItemID, byte: Byte) {
@@ -382,19 +382,19 @@ open class GameWorld : Disposable {
}
}
fun setWireStateOf(x: Int, y: Int, itemID: ItemID, vector: Vector2) {
fun setWireGeneratorStateOf(x: Int, y: Int, itemID: ItemID, vector: Vector2) {
val (x, y) = coerceXY(x, y)
val blockAddr = LandUtil.getBlockAddr(this, x, y)
return setWireStateOfUnsafe(blockAddr, itemID, vector)
return setWireGenenatorStateOfUnsafe(blockAddr, itemID, vector)
}
fun setWireStateOfUnsafe(blockAddr: BlockAddress, itemID: ItemID, vector: Vector2) {
fun setWireGenenatorStateOfUnsafe(blockAddr: BlockAddress, itemID: ItemID, vector: Vector2) {
if (wiringGraph[blockAddr] == null) {
wiringGraph[blockAddr] = HashMap()
wiringGraph[blockAddr]!![itemID] = WiringSimCell(0, vector)
}
else {
wiringGraph[blockAddr]!![itemID]!!.state = vector
wiringGraph[blockAddr]!![itemID]!!.generatorState = vector
}
}
@@ -589,9 +589,18 @@ open class GameWorld : Disposable {
}
}
data class WireConsumerState(
var dist: Int,
var state: Vector2
)
/**
* These values must be updated by none other than [WorldSimulator]()
*/
data class WiringSimCell(
var con: Byte = 0, // connections
var state: Vector2 = Vector2(0.0, 0.0)
var generatorState: Vector2 = Vector2(0.0, 0.0), // i'm emitting this much power
var consumerStates: ArrayList<WireConsumerState> = ArrayList() // how far away are the power sources
)
fun getTemperature(worldTileX: Int, worldTileY: Int): Float? {
@@ -632,6 +641,10 @@ open class GameWorld : Disposable {
val DEFAULT_GRAVITATION = Vector2(0.0, 9.8)
}
open fun updateWorldTime(delta: Float) {
worldTime.update(delta)
}
}
infix fun Int.fmod(other: Int) = Math.floorMod(this, other)

View File

@@ -1,4 +1,4 @@
package net.torvald.terrarum.modulebasegame.gameworld
package net.torvald.terrarum.gameworld
import com.badlogic.gdx.Input
import net.torvald.terrarum.*
@@ -9,8 +9,6 @@ import net.torvald.terrarum.blockproperties.Fluid
import net.torvald.terrarum.gameactors.ActorWithBody
import net.torvald.terrarum.gamecontroller.KeyToggler
import net.torvald.terrarum.gameitem.ItemID
import net.torvald.terrarum.gameworld.FluidType
import net.torvald.terrarum.gameworld.GameWorld
import net.torvald.terrarum.modulebasegame.gameactors.ActorHumanoid
import org.khelekore.prtree.*
import kotlin.math.roundToInt
@@ -41,6 +39,7 @@ object WorldSimulator {
const val FLUID_MAX_MASS = 1f // The normal, un-pressurized mass of a full water cell
const val FLUID_MAX_COMP = 0.02f // How much excess water a cell can store, compared to the cell above it. A tile of fluid can contain more than MaxMass water.
const val FLUID_MIN_MASS = 0.0001f //Ignore cells that are almost dry
const val WIRE_MIN_FLOW = 0.0001f
const val minFlow = 0.01f
const val maxSpeed = 1f // max units of water moved out of one block to another, per timestamp

View File

@@ -1,6 +1,4 @@
package net.torvald.terrarum.modulebasegame.gameworld
import net.torvald.terrarum.gameworld.fmod
package net.torvald.terrarum.gameworld
/**

View File

@@ -14,7 +14,7 @@ import net.torvald.terrarum.gameactors.*
import net.torvald.terrarum.gameitem.ItemID
import net.torvald.terrarum.modulebasegame.gameactors.ActorHumanoid
import net.torvald.terrarum.modulebasegame.gameworld.GameWorldExtension
import net.torvald.terrarum.modulebasegame.gameworld.WorldTime
import net.torvald.terrarum.gameworld.WorldTime
import net.torvald.terrarum.modulebasegame.ui.Notification
import net.torvald.terrarum.modulebasegame.ui.UIBuildingMakerBlockChooser
import net.torvald.terrarum.modulebasegame.ui.UIBuildingMakerPenMenu

View File

@@ -28,7 +28,7 @@ import net.torvald.terrarum.gameactors.WireActor
import net.torvald.terrarum.modulebasegame.gameactors.*
import net.torvald.terrarum.modulebasegame.gameactors.physicssolver.CollisionSolver
import net.torvald.terrarum.modulebasegame.gameworld.GameWorldExtension
import net.torvald.terrarum.modulebasegame.gameworld.WorldSimulator
import net.torvald.terrarum.gameworld.WorldSimulator
import net.torvald.terrarum.modulebasegame.ui.*
import net.torvald.terrarum.weather.WeatherMixer
import net.torvald.terrarum.modulebasegame.worldgenerator.RoguelikeRandomiser

View File

@@ -1,6 +1,6 @@
package net.torvald.terrarum.modulebasegame.console
import net.torvald.terrarum.modulebasegame.gameworld.WorldTime
import net.torvald.terrarum.gameworld.WorldTime
import net.torvald.terrarum.Terrarum
import net.torvald.terrarum.console.ConsoleCommand
import net.torvald.terrarum.console.Echo

View File

@@ -1,6 +1,7 @@
package net.torvald.terrarum.modulebasegame.gameworld
import net.torvald.terrarum.gameworld.GameWorld
import net.torvald.terrarum.gameworld.WorldTime
import net.torvald.terrarum.serialise.ReadLayerDataZip
/**
@@ -12,17 +13,8 @@ class GameWorldExtension : GameWorld {
internal constructor(worldIndex: Int, layerData: ReadLayerDataZip.LayerData, creationTIME_T: Long, lastPlayTIME_T: Long, totalPlayTime: Int) : super(worldIndex, layerData, creationTIME_T, lastPlayTIME_T, totalPlayTime)
/** Extended world time */
val worldTime: WorldTime
val economy = GameEconomy()
override var TIME_T: Long
get() = worldTime.TIME_T
set(value) { worldTime.TIME_T = value }
override var dayLength: Int
get() = WorldTime.DAY_LENGTH
set(value) { throw UnsupportedOperationException() }
// delegated properties //
/*val layerWall: MapLayer; get() = baseworld.layerWall
@@ -44,14 +36,6 @@ class GameWorldExtension : GameWorld {
val damageDataArray: ByteArray; get() = baseworld.damageDataArray*/
init {
worldTime = WorldTime( // Year EPOCH (125), Month 1, Day 1 is implied
7 * WorldTime.HOUR_SEC +
30L * WorldTime.MINUTE_SEC
)
}
fun updateWorldTime(delta: Float) {
worldTime.update(delta)
}
}

View File

@@ -1,195 +0,0 @@
package net.torvald.terrarum.modulebasegame.gameworld
/**
* The World Calendar implementation of Dwarven Calendar (we're talking about DF!)
*
* Please see:
* https://en.wikipedia.org/wiki/World_Calendar
* http://dwarffortresswiki.org/index.php/DF2014:Calendar
*
* Normal format for day is
* Tysdag 12th Granite
*
* And there is no AM/PM concept, 22-hour clock is forced.
*
* Created by minjaesong on 2016-01-24.
*/
@Deprecated("Are you even reading the name?")
class YeOldeWorldTime {
internal var seconds: Int // 0 - 59
internal var minutes: Int // 0 - 59
internal var hours: Int // 0 - 21
// days on the year
internal var yearlyDays: Int //NOT a calendar day
internal var days: Int // 1 - 31
internal var months: Int // 1 - 12
internal var years: Int // 1+
internal var dayOfWeek: Int //0: Mondag-The first day of weekday (0 - 7)
internal var timeDelta = 1
@Transient private var realMillisec: Int
val DAY_NAMES = arrayOf(//daynames are taken from Nynorsk (å -> o)
"Mondag", "Tysdag", "Midvikdag" //From Islenska Miðvikudagur
, "Torsdag", "Fredag", "Laurdag", "Sundag", "Verddag" //From Norsk word 'verd'
)
val DAY_NAMES_SHORT = arrayOf("Mon", "Tys", "Mid", "Tor", "Fre", "Lau", "Sun", "Ver")
val MONTH_NAMES = arrayOf(
"Opal", "Obsidian", "Granite", "Slate", "Felsite", "Hematite",
"Malachite", "Galena", "Limestone", "Sandstone", "Timber", "Moonstone"
)
val MONTH_NAMES_SHORT = arrayOf("Opal", "Obsi", "Gran", "Slat", "Fels", "Hema",
"Mala", "Gale", "Lime", "Sand", "Timb", "Moon")
@Transient val REAL_SEC_IN_MILLI = 1000
companion object {
/** Each day is 22-hour long */
val DAY_LENGTH = 79200 //must be the multiple of 3600
val HOUR_SEC: Int = 3600
val MINUTE_SEC: Int = 60
val HOUR_MIN: Int = 60
val GAME_MIN_TO_REAL_SEC: Float = 60f
val YEAR_DAYS: Int = 365
fun parseTime(s: String): Int =
if (s.length >= 4 && s.contains('h')) {
s.toLowerCase().substringBefore('h').toInt() * WorldTime.HOUR_SEC +
s.toLowerCase().substringAfter('h').toInt() * WorldTime.MINUTE_SEC
}
else if (s.endsWith("h", true)) {
s.toLowerCase().substring(0, s.length - 1).toInt() * WorldTime.HOUR_SEC
}
else {
s.toInt()
}
}
init {
// The day when the new world ever is being made.
// If we use Multiverse system (which replaces Terraria's "hack"
// as a reward rather than a cheat), time of current world's time is
// copied to the new world's. (it's Multi-nation rather than Multiverse)
seconds = 0
minutes = 30
hours = 8
yearlyDays = 73
days = 12
months = 3
years = 125
dayOfWeek = 1 // Tysdag
realMillisec = 0
}
fun update(delta: Int) {
val oldsec = seconds
//time
realMillisec += delta * timeDelta
val newsec = Math.round(GAME_MIN_TO_REAL_SEC / REAL_SEC_IN_MILLI.toFloat() * realMillisec.toFloat())
seconds = newsec
if (realMillisec >= REAL_SEC_IN_MILLI)
realMillisec -= REAL_SEC_IN_MILLI
kickVariables()
}
/**
* How much time has passed today, in seconds.
* 0 == 6 AM
* @return
*/
val elapsedSeconds: Int
get() = (HOUR_SEC * hours + MINUTE_SEC * minutes + seconds) % DAY_LENGTH
/** Sets time of this day. */
fun setTime(t: Int) {
days += t / DAY_LENGTH
hours = t / HOUR_SEC
minutes = (t - HOUR_SEC * hours) / MINUTE_SEC
seconds = t - minutes * MINUTE_SEC
yearlyDays += t / DAY_LENGTH
}
fun addTime(t: Int) {
setTime(elapsedSeconds + t)
}
fun setTimeDelta(d: Int) {
timeDelta = if (d < 0) 0 else d
}
val dayName: String
get() = DAY_NAMES[dayOfWeek]
private fun kickVariables() {
if (seconds >= MINUTE_SEC) {
seconds = 0
minutes += 1
}
if (minutes >= HOUR_MIN) {
minutes = 0
hours += 1
}
if (hours >= DAY_LENGTH / HOUR_SEC) {
hours = 0
days += 1
yearlyDays += 1
dayOfWeek += 1
}
//calendar (the world calendar)
if (dayOfWeek == 7) {
dayOfWeek = 0
}
if (months == 12 && days == 31) {
dayOfWeek = 7
}
if (months == 12 && days == 32) {
days = 1
months = 1
years++
}
else if ((months == 1 || months == 4 || months == 7 || months == 10) && days > 31) {
days = 1
months++
}
else if (days > 30) {
days = 1
months++
}
if (months > 12) {
months = 1
years++
yearlyDays = 1
}
}
/** Format: "%A %d %B %Y %X" */
fun getFormattedTime() = "${getDayNameFull()} " +
"$days " +
"${getMonthNameFull()} " +
"$years " +
"${String.format("%02d", hours)}:" +
"${String.format("%02d", minutes)}:" +
"${String.format("%02d", seconds)}"
fun getDayNameFull() = DAY_NAMES[dayOfWeek]
fun getDayNameShort() = DAY_NAMES_SHORT[dayOfWeek]
fun getMonthNameFull() = MONTH_NAMES[months - 1]
fun getMonthNameShort() = MONTH_NAMES_SHORT[months - 1]
}

View File

@@ -7,7 +7,7 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch
import net.torvald.terrarum.*
import net.torvald.terrarum.modulebasegame.gameactors.ActorHumanoid
import net.torvald.terrarum.modulebasegame.gameworld.GameWorldExtension
import net.torvald.terrarum.modulebasegame.gameworld.WorldTime
import net.torvald.terrarum.gameworld.WorldTime
import net.torvald.terrarum.modulebasegame.imagefont.WatchFont
import net.torvald.terrarum.ui.UICanvas
import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack

View File

@@ -1,6 +1,6 @@
package net.torvald.terrarum.tests
import net.torvald.terrarum.modulebasegame.gameworld.WorldTime
import net.torvald.terrarum.gameworld.WorldTime
//import org.junit.Test
/**

View File

@@ -12,6 +12,7 @@ import net.torvald.terrarum.TerrarumAppConfiguration.TILE_SIZEF
import net.torvald.terrarum.gameactors.ActorWithBody
import net.torvald.terrarum.gamecontroller.KeyToggler
import net.torvald.terrarum.gameworld.GameWorld
import net.torvald.terrarum.gameworld.WorldTime
import net.torvald.terrarum.modulebasegame.IngameRenderer
import net.torvald.terrarum.modulebasegame.RNGConsumer
import net.torvald.terrarum.modulebasegame.TerrarumIngame
@@ -127,7 +128,7 @@ internal object WeatherMixer : RNGConsumer {
// we will not care for nextSkybox for now
val timeNow = world.TIME_T.toInt() % world.dayLength
val timeNow = world.worldTime.TIME_T.toInt() % WorldTime.DAY_LENGTH
val skyboxColourMap = currentWeather.skyboxGradColourMap
// calculate global light
@@ -228,7 +229,7 @@ internal object WeatherMixer : RNGConsumer {
getGradientColour(world, currentWeather.skyboxGradColourMap, 2, timeInSec)
fun getGradientColour(world: GameWorld, colorMap: GdxColorMap, row: Int, timeInSec: Int): Cvec {
val dataPointDistance = world.dayLength / colorMap.width
val dataPointDistance = WorldTime.DAY_LENGTH / colorMap.width
val phaseThis: Int = timeInSec / dataPointDistance // x-coord in gradmap
val phaseNext: Int = (phaseThis + 1) % colorMap.width

View File

@@ -14,8 +14,8 @@ import net.torvald.terrarum.gameitem.ItemID
import net.torvald.terrarum.gameworld.GameWorld
import net.torvald.terrarum.gameworld.fmod
import net.torvald.terrarum.modulebasegame.gameworld.GameWorldExtension
import net.torvald.terrarum.modulebasegame.gameworld.WorldSimulator
import net.torvald.terrarum.modulebasegame.gameworld.WorldTime
import net.torvald.terrarum.gameworld.WorldSimulator
import net.torvald.terrarum.gameworld.WorldTime
import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack
import kotlin.math.roundToInt