From 8f407465f50c5a79bce0bb9632b827527e3bad40 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Thu, 18 Apr 2024 16:54:05 +0700 Subject: [PATCH] All composing terrain selectors now can reference other terrain selectors by name --- ADDITIONS.md | 3 +++ .../world/terrain/ComparingTerrainSelector.kt | 5 +++-- .../world/terrain/DisplacementTerrainSelector.kt | 3 ++- .../kstarbound/world/terrain/MixTerrainSelector.kt | 7 ++++--- .../world/terrain/TerrainSelectorType.kt | 14 ++++++++++++-- 5 files changed, 24 insertions(+), 8 deletions(-) diff --git a/ADDITIONS.md b/ADDITIONS.md index fa1304fc..913e4b23 100644 --- a/ADDITIONS.md +++ b/ADDITIONS.md @@ -13,6 +13,9 @@ This document briefly documents what have been added (or removed) regarding modd * Perlin noise now can be of arbitrary scale (defaults to `512`, specified with `scale` key, integer type, 2048>=x>=16) #### Terrain + * All composing terrain selectors (such as `min`, `displacement`, `rotate`, etc) now can reference other terrain selectors by name (the `.terrain` files) instead of embedding entire config inside them + * They can be referenced by either specifying corresponding field as string, or as object like so: `{"name": "namedselector"}` + * `min`, `max` and `minmax` terrain selectors now also accept next format: `{"name": "namedselector", "seedBias": 4}` * `mix` terrain selector got `mixSeedBias`, `aSeedBias` and `bSeedBias` fields, whose deviate respective selectors seeds (default to `0`) * `displacement` terrain selector has `seedBias` added, which deviate seed of `source` selector (default to `0`) * `displacement` terrain selector has `xClamp` added, works like `yClamp` diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/world/terrain/ComparingTerrainSelector.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/world/terrain/ComparingTerrainSelector.kt index bef944e2..40e69e34 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/world/terrain/ComparingTerrainSelector.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/world/terrain/ComparingTerrainSelector.kt @@ -1,6 +1,7 @@ package ru.dbotthepony.kstarbound.world.terrain import com.google.common.collect.ImmutableList +import com.google.gson.JsonElement import com.google.gson.JsonObject import ru.dbotthepony.kommons.gson.get import ru.dbotthepony.kstarbound.defs.world.TerrainSelectorParameters @@ -8,7 +9,7 @@ import ru.dbotthepony.kstarbound.json.builder.JsonFactory sealed class ComparingTerrainSelector(data: Data, parameters: TerrainSelectorParameters) : AbstractTerrainSelector(data, parameters) { @JsonFactory - data class Data(val sources: ImmutableList) + data class Data(val sources: ImmutableList) protected val sources = ArrayList>() @@ -16,7 +17,7 @@ sealed class ComparingTerrainSelector(data: Data, parameters: TerrainSelectorPar require(data.sources.isNotEmpty()) { "'sources' array is empty" } for (source in data.sources) { - sources.add(TerrainSelectorType.create(source, parameters.withSeed(parameters.seed + source.get("seedBias", 0L)))) + sources.add(TerrainSelectorType.create(source, parameters.withSeed(parameters.seed + if (source is JsonObject) source.get("seedBias", 0L) else 0L))) } } diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/world/terrain/DisplacementTerrainSelector.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/world/terrain/DisplacementTerrainSelector.kt index 1fc11b9b..ba6958f3 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/world/terrain/DisplacementTerrainSelector.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/world/terrain/DisplacementTerrainSelector.kt @@ -1,5 +1,6 @@ package ru.dbotthepony.kstarbound.world.terrain +import com.google.gson.JsonElement import com.google.gson.JsonObject import ru.dbotthepony.kommons.vector.Vector2d import ru.dbotthepony.kstarbound.defs.PerlinNoiseParameters @@ -41,7 +42,7 @@ class DisplacementTerrainSelector(data: Data, parameters: TerrainSelectorParamet val xClampSmoothing: Double = 0.0, val seedBias: Long = 0L, - val source: JsonObject, + val source: JsonElement, ) val xFn: AbstractPerlinNoise diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/world/terrain/MixTerrainSelector.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/world/terrain/MixTerrainSelector.kt index 818ca9eb..3771385e 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/world/terrain/MixTerrainSelector.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/world/terrain/MixTerrainSelector.kt @@ -1,5 +1,6 @@ package ru.dbotthepony.kstarbound.world.terrain +import com.google.gson.JsonElement import com.google.gson.JsonObject import ru.dbotthepony.kommons.math.linearInterpolation import ru.dbotthepony.kstarbound.defs.world.TerrainSelectorParameters @@ -8,11 +9,11 @@ import ru.dbotthepony.kstarbound.json.builder.JsonFactory class MixTerrainSelector(data: Data, parameters: TerrainSelectorParameters) : AbstractTerrainSelector(data, parameters) { @JsonFactory data class Data( - val mixSource: JsonObject, + val mixSource: JsonElement, val mixSeedBias: Long = 0L, - val aSource: JsonObject, + val aSource: JsonElement, val aSeedBias: Long = 0L, - val bSource: JsonObject, + val bSource: JsonElement, val bSeedBias: Long = 0L, ) diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/world/terrain/TerrainSelectorType.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/world/terrain/TerrainSelectorType.kt index d5f076f8..bcc018f1 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/world/terrain/TerrainSelectorType.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/world/terrain/TerrainSelectorType.kt @@ -1,6 +1,7 @@ package ru.dbotthepony.kstarbound.world.terrain import com.google.gson.Gson +import com.google.gson.JsonElement import com.google.gson.JsonObject import com.google.gson.JsonSyntaxException import com.google.gson.TypeAdapter @@ -9,6 +10,7 @@ import com.google.gson.reflect.TypeToken import com.google.gson.stream.JsonReader import com.google.gson.stream.JsonWriter import ru.dbotthepony.kommons.gson.consumeNull +import ru.dbotthepony.kommons.gson.contains import ru.dbotthepony.kommons.gson.value import ru.dbotthepony.kstarbound.Registries import ru.dbotthepony.kstarbound.Starbound @@ -95,8 +97,16 @@ enum class TerrainSelectorType(val jsonName: String, private val data: Data<*, * throw IllegalArgumentException("Unknown terrain selector type $type") } - fun create(json: JsonObject, parameters: TerrainSelectorParameters): AbstractTerrainSelector<*> { - return factory(json, false).create(parameters) + fun create(json: JsonElement, parameters: TerrainSelectorParameters): AbstractTerrainSelector<*> { + if (json is JsonObject) { + if ("name" in json) { + return named(json["name"].asString, parameters) + } else { + return factory(json, false).create(parameters) + } + } else { + return named(json.asString, parameters) + } } fun load(json: JsonObject): AbstractTerrainSelector<*> {