diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/molang/MoLangFunctions.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/molang/MoLangFunctions.kt new file mode 100644 index 0000000000..10f27f8129 --- /dev/null +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/molang/MoLangFunctions.kt @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2023 Cobblemon Contributors + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +package com.cobblemon.mod.common.api.molang + +import com.bedrockk.molang.runtime.MoLangEnvironment +import com.bedrockk.molang.runtime.MoParams +import com.bedrockk.molang.runtime.struct.QueryStruct +import com.bedrockk.molang.runtime.value.DoubleValue +import net.minecraft.block.Block +import net.minecraft.registry.Registry +import net.minecraft.registry.RegistryKey +import net.minecraft.registry.RegistryKeys +import net.minecraft.registry.entry.RegistryEntry +import net.minecraft.registry.tag.TagKey +import net.minecraft.util.Identifier +import net.minecraft.world.World +import net.minecraft.world.biome.Biome +import net.minecraft.world.dimension.DimensionType + +/** + * Holds a bunch of useful MoLang trickery that can be used or extended in API + * + * @author Hiroku + * @since October 2nd, 2023 + */ +object MoLangFunctions { + val generalFunctions = hashMapOf>() + val biomeFunctions = hashMapOf>() + val worldFunctions = hashMapOf>() + val dimensionTypeFunctions = hashMapOf>() + val blockFunctions = hashMapOf>() + + fun RegistryEntry.asBiomeMoLangValue() = asMoLangValue(RegistryKeys.BIOME).addFunctions(biomeFunctions) + fun RegistryEntry.asWorldMoLangValue() = asMoLangValue(RegistryKeys.WORLD).addFunctions(worldFunctions) + fun RegistryEntry.asBlockMoLangValue() = asMoLangValue(RegistryKeys.BLOCK).addFunctions(blockFunctions) + fun RegistryEntry.asDimensionTypeMoLangValue() = asMoLangValue(RegistryKeys.DIMENSION_TYPE).addFunctions(dimensionTypeFunctions) + + fun RegistryEntry.asMoLangValue(key: RegistryKey>): ObjectValue> { + val value = ObjectValue( + obj = this, + stringify = { it.key.get().value.toString() } + ) + value.functions.put("is_in") { + val tag = TagKey.of(key, Identifier(it.getString(0).replace("#", ""))) + return@put DoubleValue(if (value.obj.isIn(tag)) 1.0 else 0.0) + } + value.functions.put("is_of") { + val identifier = Identifier(it.getString(0)) + return@put DoubleValue(if (value.obj.matchesId(identifier)) 1.0 else 0.0) + } + return value + } + + fun QueryStruct.addStandardFunctions(): QueryStruct { + functions.putAll(generalFunctions) + return this + } + + fun T.addFunctions(functions: Map>): T { + this.functions.putAll(functions) + return this + } + + fun MoLangEnvironment.getQueryStruct(name: String = "query") = structs.getOrPut(name) { QueryStruct(hashMapOf()).addStandardFunctions() } as QueryStruct +} \ No newline at end of file diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/molang/ObjectValue.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/molang/ObjectValue.kt new file mode 100644 index 0000000000..ca92a34a73 --- /dev/null +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/molang/ObjectValue.kt @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2023 Cobblemon Contributors + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +package com.cobblemon.mod.common.api.molang + +import com.bedrockk.molang.runtime.struct.QueryStruct +import com.bedrockk.molang.runtime.value.MoValue + +/** + * A [MoValue] that just wraps around some other object value. It's for good and valid reasons. + * + * @author Hiroku + * @since October 2nd, 2023 + */ +class ObjectValue( + var obj: T, + val stringify: (T) -> String = { it.toString() }, + val doublify: (T) -> Double = { throw NotImplementedError("Doublify not implemented for this object value, are you doing weird molang") } +) : QueryStruct(hashMapOf()) { + override fun value() = this + override fun asDouble() = doublify(obj) + override fun asString() = stringify(obj) +} \ No newline at end of file diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/pokemon/PokemonProperties.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/pokemon/PokemonProperties.kt index 28eda53cda..981b711740 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/api/pokemon/PokemonProperties.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/pokemon/PokemonProperties.kt @@ -8,6 +8,8 @@ package com.cobblemon.mod.common.api.pokemon +import com.bedrockk.molang.runtime.struct.VariableStruct +import com.bedrockk.molang.runtime.value.StringValue import com.cobblemon.mod.common.Cobblemon import com.cobblemon.mod.common.api.abilities.Abilities import com.cobblemon.mod.common.api.pokeball.PokeBalls @@ -631,6 +633,13 @@ open class PokemonProperties { return pieces.joinToString(separator) } + fun asStruct(): VariableStruct { + val struct = VariableStruct() + species?.let { struct.setDirectly("species", StringValue(it)) } + // todo a bunch more + return struct + } + fun updateAspects() { val aspects = mutableSetOf() AspectProvider.providers.forEach { aspects.addAll(it.provide(this)) } diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/CobblemonSpawnRules.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/CobblemonSpawnRules.kt new file mode 100644 index 0000000000..202780d3e7 --- /dev/null +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/CobblemonSpawnRules.kt @@ -0,0 +1,84 @@ +/* + * Copyright (C) 2023 Cobblemon Contributors + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +package com.cobblemon.mod.common.api.spawning + +import com.bedrockk.molang.Expression +import com.cobblemon.mod.common.api.data.JsonDataRegistry +import com.cobblemon.mod.common.api.reactive.SimpleObservable +import com.cobblemon.mod.common.api.spawning.condition.SpawningCondition +import com.cobblemon.mod.common.api.spawning.influence.SpawningInfluence +import com.cobblemon.mod.common.api.spawning.rules.SpawnRule +import com.cobblemon.mod.common.api.spawning.rules.component.FilterRuleComponent +import com.cobblemon.mod.common.api.spawning.rules.component.LocationRuleCalculator +import com.cobblemon.mod.common.api.spawning.rules.component.SpawnRuleComponent +import com.cobblemon.mod.common.api.spawning.rules.component.WeightTweakRuleComponent +import com.cobblemon.mod.common.api.spawning.rules.selector.ConditionalSpawningContextSelector +import com.cobblemon.mod.common.api.spawning.rules.selector.ExpressionSpawnDetailSelector +import com.cobblemon.mod.common.api.spawning.rules.selector.ExpressionSpawningContextSelector +import com.cobblemon.mod.common.api.spawning.rules.selector.SpawnDetailSelector +import com.cobblemon.mod.common.api.spawning.rules.selector.SpawningContextSelector +import com.cobblemon.mod.common.util.adapters.ExpressionAdapter +import com.cobblemon.mod.common.util.adapters.SpawnDetailSelectorAdapter +import com.cobblemon.mod.common.util.adapters.SpawnRuleComponentAdapter +import com.cobblemon.mod.common.util.adapters.SpawningConditionAdapter +import com.cobblemon.mod.common.util.adapters.SpawningContextSelectorAdapter +import com.cobblemon.mod.common.util.adapters.TextAdapter +import com.cobblemon.mod.common.util.cobblemonResource +import com.google.gson.GsonBuilder +import com.google.gson.reflect.TypeToken +import net.minecraft.resource.ResourceType +import net.minecraft.server.network.ServerPlayerEntity +import net.minecraft.text.Text +import net.minecraft.util.Identifier + +/** + * All of the [SpawnRule]s that will be applied as global [SpawningInfluence]s. + * + * @author Hiroku + * @since September 30th, 2023 + */ +object CobblemonSpawnRules : JsonDataRegistry { + override val gson = GsonBuilder() + .registerTypeAdapter(SpawnRuleComponent::class.java, SpawnRuleComponentAdapter) + .registerTypeAdapter(SpawnDetailSelector::class.java, SpawnDetailSelectorAdapter) + .registerTypeAdapter(SpawningContextSelector::class.java, SpawningContextSelectorAdapter) + .registerTypeAdapter(SpawningCondition::class.java, SpawningConditionAdapter) + .registerTypeAdapter(Expression::class.java, ExpressionAdapter) + .registerTypeAdapter(Text::class.java, TextAdapter) + .create() + + override val typeToken = TypeToken.get(SpawnRule::class.java) + override val resourcePath = "spawn_rules" + + init { + SpawnRuleComponent.register("weight") + SpawnRuleComponent.register("filter") + SpawnRuleComponent.register("location") + + SpawnDetailSelector.register("expression") + + SpawningContextSelector.register("expression") + SpawningContextSelector.register("conditional") + } + + val rules = mutableMapOf() + + override fun reload(data: Map) { + rules.clear() + rules.putAll(data) + data.forEach { (id, value) -> value.id = id } + observable.emit(this) + } + + override val id: Identifier = cobblemonResource("spawn_rules") + override val type: ResourceType = ResourceType.SERVER_DATA + override val observable = SimpleObservable() + + override fun sync(player: ServerPlayerEntity) {} +} \ No newline at end of file diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/condition/AppendageCondition.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/condition/AppendageCondition.kt index fc0ac2cc3d..b2ea96d541 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/condition/AppendageCondition.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/condition/AppendageCondition.kt @@ -55,5 +55,5 @@ interface AppendageCondition { fun getAppendages(spawningCondition: SpawningCondition<*>) = appendages.filter { it.spawningConditionFits(spawningCondition) }.map { it.clazz } } - fun fits(ctx: SpawningContext, detail: SpawnDetail): Boolean + fun fits(ctx: SpawningContext): Boolean } \ No newline at end of file diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/condition/AreaSpawningCondition.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/condition/AreaSpawningCondition.kt index 4fa0101ab3..73395c2d48 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/condition/AreaSpawningCondition.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/condition/AreaSpawningCondition.kt @@ -26,8 +26,8 @@ abstract class AreaTypeSpawningCondition : SpawningCond var maxHeight: Int? = null var neededNearbyBlocks: MutableList>? = null - override fun fits(ctx: T, detail: SpawnDetail): Boolean { - if (!super.fits(ctx, detail)) { + override fun fits(ctx: T): Boolean { + if (!super.fits(ctx)) { return false } else if (minHeight != null && ctx.height < minHeight!!) { return false diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/condition/CompositeSpawningCondition.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/condition/CompositeSpawningCondition.kt index 9bc9e82dc9..b794702d9e 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/condition/CompositeSpawningCondition.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/condition/CompositeSpawningCondition.kt @@ -30,11 +30,11 @@ class CompositeSpawningCondition { var conditions = mutableListOf>() var anticonditions = mutableListOf>() - fun satisfiedBy(ctx: SpawningContext, detail: SpawnDetail): Boolean { - return if (conditions.isNotEmpty() && conditions.none { it.isSatisfiedBy(ctx, detail) }) { + fun satisfiedBy(ctx: SpawningContext): Boolean { + return if (conditions.isNotEmpty() && conditions.none { it.isSatisfiedBy(ctx) }) { false } else { - !(anticonditions.isNotEmpty() && anticonditions.any { it.isSatisfiedBy(ctx, detail) }) + !(anticonditions.isNotEmpty() && anticonditions.any { it.isSatisfiedBy(ctx) }) } } } \ No newline at end of file diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/condition/GroundedSpawningCondition.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/condition/GroundedSpawningCondition.kt index a0ed45739d..1a8f3923fc 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/condition/GroundedSpawningCondition.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/condition/GroundedSpawningCondition.kt @@ -24,8 +24,8 @@ import net.minecraft.block.Block abstract class GroundedTypeSpawningCondition : AreaTypeSpawningCondition() { var neededBaseBlocks: MutableList>? = null - override fun fits(ctx: T, detail: SpawnDetail): Boolean { - return if (!super.fits(ctx, detail)) { + override fun fits(ctx: T): Boolean { + return if (!super.fits(ctx)) { false } else if (minHeight != null && ctx.height < minHeight!!) { return false diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/condition/SpawningCondition.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/condition/SpawningCondition.kt index bb7c117bc9..2c6d2b3111 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/condition/SpawningCondition.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/condition/SpawningCondition.kt @@ -11,8 +11,6 @@ package com.cobblemon.mod.common.api.spawning.condition import com.cobblemon.mod.common.api.conditional.RegistryLikeCondition import com.cobblemon.mod.common.api.spawning.MoonPhaseRange import com.cobblemon.mod.common.api.spawning.TimeRange -import com.cobblemon.mod.common.api.spawning.condition.ListCheckMode.ALL -import com.cobblemon.mod.common.api.spawning.condition.ListCheckMode.ANY import com.cobblemon.mod.common.api.spawning.context.SpawningContext import com.cobblemon.mod.common.api.spawning.detail.SpawnDetail import com.cobblemon.mod.common.util.Merger @@ -55,8 +53,6 @@ abstract class SpawningCondition { var isRaining: Boolean? = null var isThundering: Boolean? = null var timeRange: TimeRange? = null - var labels: MutableList? = null - var labelMode = ANY var structures: MutableList>>? = null @Transient @@ -65,15 +61,15 @@ abstract class SpawningCondition { abstract fun contextClass(): Class fun contextMatches(ctx: SpawningContext) = contextClass().isAssignableFrom(ctx::class.java) - fun isSatisfiedBy(ctx: SpawningContext, detail: SpawnDetail): Boolean { + fun isSatisfiedBy(ctx: SpawningContext): Boolean { return if (contextMatches(ctx)) { - fits(ctx as T, detail) + fits(ctx as T) } else { false } } - protected open fun fits(ctx: T, detail: SpawnDetail): Boolean { + protected open fun fits(ctx: T): Boolean { if (ctx.position.x < minX.orMin() || ctx.position.x > maxX.orMax()) { return false } else if (ctx.position.y < minY.orMin() || ctx.position.y > maxY.orMax()) { @@ -96,14 +92,7 @@ abstract class SpawningCondition { return false } else if (isThundering != null && ctx.world.isThundering != isThundering!!) { return false - } else if (labels != null && labels!!.isNotEmpty() && - ( - (labelMode == ANY && labels!!.none { it in detail.labels }) || - (labelMode == ALL && labels!!.any { it !in detail.labels }) - ) - ) { - return false - } else if (appendages.any { !it.fits(ctx, detail) }) { + } else if (appendages.any { !it.fits(ctx) }) { return false } else if (structures != null && structures!!.isNotEmpty() && structures!!.let { structures -> @@ -123,7 +112,6 @@ abstract class SpawningCondition { open fun copyFrom(other: SpawningCondition<*>, merger: Merger) { dimensions = merger.merge(dimensions, other.dimensions)?.toMutableList() biomes = merger.merge(biomes, other.biomes)?.toMutableSet() - labels = merger.merge(labels, other.labels)?.toMutableList() moonPhase = merger.mergeSingle(moonPhase, other.moonPhase) canSeeSky = merger.mergeSingle(canSeeSky, other.canSeeSky) minX = merger.mergeSingle(minX, other.minX) diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/condition/SubmergedSpawningCondition.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/condition/SubmergedSpawningCondition.kt index 122b9ae38a..f0c97b5a08 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/condition/SubmergedSpawningCondition.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/condition/SubmergedSpawningCondition.kt @@ -27,8 +27,8 @@ abstract class SubmergedTypeSpawningCondition : Ar var fluidIsSource: Boolean? = null var fluid: RegistryLikeCondition? = null - override fun fits(ctx: T, detail: SpawnDetail): Boolean { - return if (!super.fits(ctx, detail)) { + override fun fits(ctx: T): Boolean { + return if (!super.fits(ctx)) { false } else if (minHeight != null && ctx.height < minHeight!!) { return false diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/condition/SurfaceSpawningCondition.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/condition/SurfaceSpawningCondition.kt index f4c47d4d27..1774906290 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/condition/SurfaceSpawningCondition.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/condition/SurfaceSpawningCondition.kt @@ -26,8 +26,8 @@ abstract class SurfaceTypeSpawningCondition : AreaTy var maxDepth: Int? = null var fluid: RegistryLikeCondition? = null - override fun fits(ctx: T, detail: SpawnDetail): Boolean { - return if (!super.fits(ctx, detail)) { + override fun fits(ctx: T): Boolean { + return if (!super.fits(ctx)) { false } else if (minDepth != null && ctx.depth < minDepth!!) { false diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/context/AreaContextResolver.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/context/AreaContextResolver.kt index f1ee7634b2..93337f7458 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/context/AreaContextResolver.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/context/AreaContextResolver.kt @@ -49,7 +49,8 @@ interface AreaContextResolver { pos.set(x, y, z) val vec = pos.toVec3d() if (slice.nearbyEntityPositions.none { it.isInRange(vec, config.minimumDistanceBetweenEntities) && it != slice.cause.entity }) { - val fittedContextCalculator = contextCalculators.firstOrNull { it.fits(input) } + val fittedContextCalculator = contextCalculators + .firstOrNull { calc -> calc.fits(input) && input.spawner.influences.none { !it.isAllowedPosition(input.world, input.position, calc) } } if (fittedContextCalculator != null) { val context = fittedContextCalculator.calculate(input) if (context != null) { diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/context/SpawningContext.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/context/SpawningContext.kt index 505ca61194..f0ffeaf002 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/context/SpawningContext.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/context/SpawningContext.kt @@ -8,6 +8,9 @@ package com.cobblemon.mod.common.api.spawning.context +import com.bedrockk.molang.runtime.struct.VariableStruct +import com.bedrockk.molang.runtime.value.DoubleValue +import com.cobblemon.mod.common.api.molang.ObjectValue import com.cobblemon.mod.common.api.spawning.SpawnCause import com.cobblemon.mod.common.api.spawning.condition.BasicSpawningCondition import com.cobblemon.mod.common.api.spawning.detail.SpawnDetail @@ -80,6 +83,9 @@ abstract class SpawningContext { val biomeName: Identifier get() = this.biomeRegistry.getId(biome)!! + private val struct = VariableStruct() + private var structCompiled = false + class StructureChunkCache { val missingTags = mutableSetOf>() val foundTags = mutableSetOf>() @@ -159,4 +165,21 @@ abstract class SpawningContext { } return weight } + + fun getOrSetupStruct(): VariableStruct { + if (structCompiled) { + return struct + } + + struct.setDirectly("light", DoubleValue(light.toDouble())) + struct.setDirectly("x", DoubleValue(position.x.toDouble())) + struct.setDirectly("y", DoubleValue(position.y.toDouble())) + struct.setDirectly("z", DoubleValue(position.z.toDouble())) + struct.setDirectly("moon_phase", DoubleValue(moonPhase.toDouble())) + struct.setDirectly("world", ObjectValue(world.registryManager.get(RegistryKeys.WORLD).getEntry(world))) + struct.setDirectly("biome", ObjectValue(biomeRegistry.getEntry(biome))) + + structCompiled = true + return struct + } } \ No newline at end of file diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/context/calculators/FlooredContextCalculators.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/context/calculators/FlooredContextCalculators.kt index 7b130caa3d..b5a776dd06 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/context/calculators/FlooredContextCalculators.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/context/calculators/FlooredContextCalculators.kt @@ -50,6 +50,7 @@ interface FlooredSpawningContextCalculator : AreaSpa */ object GroundedSpawningContextCalculator : FlooredSpawningContextCalculator { // TODO expand base condition + override val name = "grounded" override val baseCondition: (BlockState) -> Boolean = isSolidCondition override val surroundingCondition: (BlockState) -> Boolean = isAirCondition @@ -76,6 +77,7 @@ object GroundedSpawningContextCalculator : FlooredSpawningContextCalculator { + override val name = "seafloor" override val baseCondition: (BlockState) -> Boolean = isSolidCondition override val surroundingCondition: (BlockState) -> Boolean = isWaterCondition @@ -102,6 +104,7 @@ object SeafloorSpawningContextCalculator : FlooredSpawningContextCalculator { + override val name = "lavafloor" override val baseCondition: (BlockState) -> Boolean = isSolidCondition override val surroundingCondition: (BlockState) -> Boolean = isLavaCondition @@ -129,6 +132,7 @@ object LavafloorSpawningContextCalculator : FlooredSpawningContextCalculator { + override val name = "surface" override val baseCondition: (BlockState) -> Boolean = { !it.fluidState.isEmpty } override val surroundingCondition: (BlockState) -> Boolean = isAirCondition diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/context/calculators/SpawningContextCalculator.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/context/calculators/SpawningContextCalculator.kt index 47f12e0785..ecd2240985 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/context/calculators/SpawningContextCalculator.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/context/calculators/SpawningContextCalculator.kt @@ -48,6 +48,8 @@ interface SpawningContextCalculator { + override val name = "submerged" val fluidConditions = mutableListOf( isWaterCondition, isLavaCondition diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/detail/PokemonSpawnAction.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/detail/PokemonSpawnAction.kt index 457fabc94b..f5479424e1 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/detail/PokemonSpawnAction.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/detail/PokemonSpawnAction.kt @@ -8,8 +8,8 @@ package com.cobblemon.mod.common.api.spawning.detail -import com.cobblemon.mod.common.Cobblemon.LOGGER import com.cobblemon.mod.common.Cobblemon +import com.cobblemon.mod.common.Cobblemon.LOGGER import com.cobblemon.mod.common.api.pokemon.PokemonProperties import com.cobblemon.mod.common.api.spawning.context.SpawningContext import com.cobblemon.mod.common.entity.pokemon.PokemonEntity @@ -50,6 +50,9 @@ class PokemonSpawnAction( entity.pokemon.swapHeldItem(heldItem) } entity.drops = detail.drops + // Useful debug code in situations where you want to find spawns +// val fireworkRocketEntity = FireworkRocketEntity(ctx.world, ctx.position.x.toDouble(), ctx.position.y.toDouble() + 2, ctx.position.z.toDouble(), ItemStack(Items.FIREWORK_ROCKET)) +// ctx.world.spawnEntity(fireworkRocketEntity) return entity } } \ No newline at end of file diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/detail/PokemonSpawnDetail.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/detail/PokemonSpawnDetail.kt index c41f2f1526..f4ecdb124d 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/detail/PokemonSpawnDetail.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/detail/PokemonSpawnDetail.kt @@ -69,7 +69,7 @@ class PokemonSpawnDetail : SpawnDetail() { } override fun autoLabel() { - super.autoLabel() + val pokemonStruct = pokemon.asStruct() if (pokemon.species != null) { val species = PokemonSpecies.getByIdentifier(pokemon.species!!.asIdentifierDefaultingNamespace()) if (species != null) { @@ -87,6 +87,9 @@ class PokemonSpawnDetail : SpawnDetail() { } } } + + struct.setDirectly("pokemon", pokemonStruct) + super.autoLabel() } fun getDerivedLevelRange() = levelRange.let { levelRange -> diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/detail/SpawnDetail.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/detail/SpawnDetail.kt index c17fa0867c..f67eab54ec 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/detail/SpawnDetail.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/detail/SpawnDetail.kt @@ -8,6 +8,10 @@ package com.cobblemon.mod.common.api.spawning.detail +import com.bedrockk.molang.runtime.struct.ArrayStruct +import com.bedrockk.molang.runtime.struct.VariableStruct +import com.bedrockk.molang.runtime.value.DoubleValue +import com.bedrockk.molang.runtime.value.StringValue import com.cobblemon.mod.common.api.ModDependant import com.cobblemon.mod.common.api.spawning.SpawnBucket import com.cobblemon.mod.common.api.spawning.condition.CompositeSpawningCondition @@ -50,20 +54,32 @@ abstract class SpawnDetail : ModDependant { var labels = mutableListOf() + val struct: VariableStruct = VariableStruct() + override var neededInstalledMods = listOf() override var neededUninstalledMods = listOf() - open fun autoLabel() {} + open fun autoLabel() { + struct.setDirectly("weight", DoubleValue(weight.toDouble())) + struct.setDirectly("percentage", DoubleValue(percentage.toDouble())) + struct.setDirectly("id", StringValue(id)) + struct.setDirectly("bucket", StringValue(bucket.name)) + struct.setDirectly("width", DoubleValue(width.toDouble())) + struct.setDirectly("height", DoubleValue(height.toDouble())) + struct.setDirectly("context", StringValue(context.name)) + struct.setDirectly("labels", ArrayStruct(labels.mapIndexed { index, s -> "$index" to StringValue(s) }.toMap())) + } + open fun getName() = displayName?.asTranslated() ?: id.text() open fun isSatisfiedBy(ctx: SpawningContext): Boolean { if (!ctx.preFilter(this)) { return false - } else if (conditions.isNotEmpty() && conditions.none { it.isSatisfiedBy(ctx, this) }) { + } else if (conditions.isNotEmpty() && conditions.none { it.isSatisfiedBy(ctx) }) { return false - } else if (anticonditions.isNotEmpty() && anticonditions.any { it.isSatisfiedBy(ctx, this) }) { + } else if (anticonditions.isNotEmpty() && anticonditions.any { it.isSatisfiedBy(ctx) }) { return false - } else if (compositeCondition?.satisfiedBy(ctx, this) == false) { + } else if (compositeCondition?.satisfiedBy(ctx) == false) { return false } else if (!ctx.postFilter(this)) { return false diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/influence/SpawningInfluence.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/influence/SpawningInfluence.kt index 3e469ac111..66f09e4019 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/influence/SpawningInfluence.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/influence/SpawningInfluence.kt @@ -11,9 +11,12 @@ package com.cobblemon.mod.common.api.spawning.influence import com.cobblemon.mod.common.api.spawning.BestSpawner import com.cobblemon.mod.common.api.spawning.SpawnBucket import com.cobblemon.mod.common.api.spawning.context.SpawningContext +import com.cobblemon.mod.common.api.spawning.context.calculators.SpawningContextCalculator import com.cobblemon.mod.common.api.spawning.detail.SpawnAction import com.cobblemon.mod.common.api.spawning.detail.SpawnDetail import net.minecraft.entity.Entity +import net.minecraft.server.world.ServerWorld +import net.minecraft.util.math.BlockPos /** * An influence over spawning that can affect various parts of the spawning process. These can be attached to @@ -35,4 +38,6 @@ interface SpawningInfluence { fun affectSpawn(entity: Entity) {} /** Applies some influence over the weight of spawn buckets. */ fun affectBucketWeight(bucket: SpawnBucket, weight: Float): Float = weight + /** Filters positions that could be usable for a spawning context calculator */ + fun isAllowedPosition(world: ServerWorld, pos: BlockPos, contextCalculator: SpawningContextCalculator<*, *>): Boolean = true } \ No newline at end of file diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/multiplier/WeightMultiplier.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/multiplier/WeightMultiplier.kt index ce935f219f..b4d0b3f82f 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/multiplier/WeightMultiplier.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/multiplier/WeightMultiplier.kt @@ -31,8 +31,8 @@ class WeightMultiplier : SpawningInfluence { var multiplier = 1F override fun affectWeight(detail: SpawnDetail, ctx: SpawningContext, weight: Float): Float { - val meetsConditions = (conditions.isEmpty() || conditions.any { it.isSatisfiedBy(ctx, detail) }) - && (anticonditions.isEmpty() || anticonditions.none { it.isSatisfiedBy(ctx, detail) }) + val meetsConditions = (conditions.isEmpty() || conditions.any { it.isSatisfiedBy(ctx) }) + && (anticonditions.isEmpty() || anticonditions.none { it.isSatisfiedBy(ctx) }) return if (meetsConditions) multiplier * weight else weight } } \ No newline at end of file diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/rules/SpawnRule.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/rules/SpawnRule.kt new file mode 100644 index 0000000000..0f0c5977fd --- /dev/null +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/rules/SpawnRule.kt @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2023 Cobblemon Contributors + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +package com.cobblemon.mod.common.api.spawning.rules + +import com.cobblemon.mod.common.api.spawning.rules.component.SpawnRuleComponent +import com.cobblemon.mod.common.api.text.text +import net.minecraft.text.Text +import net.minecraft.util.Identifier + +/** + * A bundling of [SpawnRuleComponent]s. + * + * @author Hiroku + * @since September 30th, 2023 + */ +class SpawnRule { + + lateinit var id: Identifier + val displayName: Text = "Spawn Rule".text() +// val pool: String? = null Kinda difficult to see how this would get used in practice. +// var enabled = true One would expect there to be some kind of persistence if this could be enabled and disabled willy nilly. + val components: MutableList = mutableListOf() +} \ No newline at end of file diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/rules/component/FilterRuleComponent.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/rules/component/FilterRuleComponent.kt new file mode 100644 index 0000000000..6d554d405b --- /dev/null +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/rules/component/FilterRuleComponent.kt @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2023 Cobblemon Contributors + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +package com.cobblemon.mod.common.api.spawning.rules.component + +import com.bedrockk.molang.Expression +import com.bedrockk.molang.ast.BooleanExpression +import com.bedrockk.molang.runtime.MoLangRuntime +import com.cobblemon.mod.common.api.spawning.context.SpawningContext +import com.cobblemon.mod.common.api.spawning.detail.SpawnDetail +import com.cobblemon.mod.common.api.spawning.rules.selector.AllSpawnDetailSelector +import com.cobblemon.mod.common.api.spawning.rules.selector.AllSpawningContextSelector +import com.cobblemon.mod.common.api.spawning.rules.selector.SpawnDetailSelector +import com.cobblemon.mod.common.api.spawning.rules.selector.SpawningContextSelector +import com.cobblemon.mod.common.util.resolveBoolean + +/** + * A rule component that targets specific spawns and contexts and judges whether it + * is allowed to spawn. This runs prior to the regular SpawnDetail conditions. + * + * @author Hiroku + * @since October 1st, 2023 + */ +class FilterRuleComponent : SpawnRuleComponent { + @Transient + val runtime = MoLangRuntime() + + val spawnSelector: SpawnDetailSelector = AllSpawnDetailSelector + val contextSelector: SpawningContextSelector = AllSpawningContextSelector + val allow: Expression = BooleanExpression(true) + + override fun affectSpawnable(detail: SpawnDetail, ctx: SpawningContext): Boolean { + return if (spawnSelector.selects(detail) && contextSelector.selects(ctx)) { + runtime.environment.setSimpleVariable("spawn", detail.struct) + runtime.environment.setSimpleVariable("context", ctx.getOrSetupStruct()) + runtime.resolveBoolean(allow) + } else { + true + } + } +} \ No newline at end of file diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/rules/component/LocationRuleCalculator.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/rules/component/LocationRuleCalculator.kt new file mode 100644 index 0000000000..bab60c7ee3 --- /dev/null +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/rules/component/LocationRuleCalculator.kt @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2023 Cobblemon Contributors + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +package com.cobblemon.mod.common.api.spawning.rules.component + +import com.bedrockk.molang.runtime.MoLangRuntime +import com.bedrockk.molang.runtime.value.DoubleValue +import com.bedrockk.molang.runtime.value.StringValue +import com.cobblemon.mod.common.api.molang.MoLangFunctions.asDimensionTypeMoLangValue +import com.cobblemon.mod.common.api.molang.MoLangFunctions.asWorldMoLangValue +import com.cobblemon.mod.common.api.molang.MoLangFunctions.getQueryStruct +import com.cobblemon.mod.common.api.molang.ObjectValue +import com.cobblemon.mod.common.api.spawning.context.SpawningContext +import com.cobblemon.mod.common.api.spawning.context.calculators.SpawningContextCalculator +import com.cobblemon.mod.common.util.asExpression +import com.cobblemon.mod.common.util.resolveBoolean +import net.minecraft.registry.RegistryKeys +import net.minecraft.registry.entry.RegistryEntry +import net.minecraft.server.world.ServerWorld +import net.minecraft.util.math.BlockPos +import net.minecraft.world.World +import net.minecraft.world.dimension.DimensionType + +/** + * An early rule that filters possible locations that would go into a [SpawningContext]. + * + * @author Hiroku + * @since October 2nd, 2023 + */ +class LocationRuleCalculator : SpawnRuleComponent { + @Transient + val runtime = MoLangRuntime().also { it.environment.getQueryStruct() } + @Transient + private val reusableX = DoubleValue(0.0) + @Transient + private val reusableY = DoubleValue(0.0) + @Transient + private val reusableZ = DoubleValue(0.0) + @Transient + private val reusableContext = StringValue("") + @Transient + private lateinit var reusableWorldValue: ObjectValue> + @Transient + private lateinit var reusableDimensionTypeValue: ObjectValue> + + val allow = "true".asExpression() + + override fun isAllowedPosition( + world: ServerWorld, + pos: BlockPos, + contextCalculator: SpawningContextCalculator<*, *> + ): Boolean { + reusableX.value = pos.x.toDouble() + reusableY.value = pos.y.toDouble() + reusableZ.value = pos.z.toDouble() + reusableContext.value = contextCalculator.name + + if (!this::reusableWorldValue.isInitialized) { + reusableWorldValue = world.registryManager.get(RegistryKeys.WORLD).getEntry(world).asWorldMoLangValue() + } else { + reusableWorldValue.obj = world.registryManager.get(RegistryKeys.WORLD).getEntry(world) + } + + if (!this::reusableDimensionTypeValue.isInitialized) { + reusableDimensionTypeValue = world.dimensionEntry.asDimensionTypeMoLangValue() + } else { + reusableDimensionTypeValue.obj = world.dimensionEntry + } + + runtime.environment.setSimpleVariable("x", reusableX) + runtime.environment.setSimpleVariable("y", reusableY) + runtime.environment.setSimpleVariable("z", reusableZ) + runtime.environment.setSimpleVariable("context", reusableContext) + runtime.environment.setSimpleVariable("world", reusableWorldValue) + runtime.environment.setSimpleVariable("dimension_type", reusableDimensionTypeValue) + return runtime.resolveBoolean(allow) + } +} \ No newline at end of file diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/rules/component/SpawnRuleComponent.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/rules/component/SpawnRuleComponent.kt new file mode 100644 index 0000000000..9032cb2414 --- /dev/null +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/rules/component/SpawnRuleComponent.kt @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2023 Cobblemon Contributors + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +package com.cobblemon.mod.common.api.spawning.rules.component + +import com.cobblemon.mod.common.api.spawning.influence.SpawningInfluence + +/** + * Responsible for changing a thing about spawns. A spawn rule may have many components like this. + * + * @author Hiroku + * @since September 30th, 2023 + */ +interface SpawnRuleComponent : SpawningInfluence { + companion object { + val types = mutableMapOf>() + + inline fun register(type: String) { + types[type] = T::class.java + } + } +} \ No newline at end of file diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/rules/component/WeightTweakRuleComponent.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/rules/component/WeightTweakRuleComponent.kt new file mode 100644 index 0000000000..aaa1f0c53f --- /dev/null +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/rules/component/WeightTweakRuleComponent.kt @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2023 Cobblemon Contributors + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +package com.cobblemon.mod.common.api.spawning.rules.component + +import com.bedrockk.molang.Expression +import com.bedrockk.molang.runtime.MoLangRuntime +import com.bedrockk.molang.runtime.value.DoubleValue +import com.cobblemon.mod.common.api.spawning.context.SpawningContext +import com.cobblemon.mod.common.api.spawning.detail.SpawnDetail +import com.cobblemon.mod.common.api.spawning.rules.selector.AllSpawnDetailSelector +import com.cobblemon.mod.common.api.spawning.rules.selector.AllSpawningContextSelector +import com.cobblemon.mod.common.api.spawning.rules.selector.SpawnDetailSelector +import com.cobblemon.mod.common.api.spawning.rules.selector.SpawningContextSelector +import com.cobblemon.mod.common.util.asExpression +import com.cobblemon.mod.common.util.resolveFloat + +/** + * A rule component that alters the weight of spawns at specific contexts. + * + * @author Hiroku + * @since October 1st, 2023 + */ +class WeightTweakRuleComponent : SpawnRuleComponent { + val spawnSelector: SpawnDetailSelector = AllSpawnDetailSelector + val contextSelector: SpawningContextSelector = AllSpawningContextSelector + val weight: Expression = "v.weight".asExpression() + + @Transient + val runtime = MoLangRuntime() + + override fun affectWeight(detail: SpawnDetail, ctx: SpawningContext, weight: Float): Float { + return if (spawnSelector.selects(detail) && contextSelector.selects(ctx)) { + runtime.environment.setSimpleVariable("spawn", detail.struct) + runtime.environment.setSimpleVariable("weight", DoubleValue(weight.toDouble())) + runtime.resolveFloat(this.weight) + } else { + weight + } + } +} \ No newline at end of file diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/rules/selector/AllSpawnDetailSelector.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/rules/selector/AllSpawnDetailSelector.kt new file mode 100644 index 0000000000..cd3b9cb730 --- /dev/null +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/rules/selector/AllSpawnDetailSelector.kt @@ -0,0 +1,15 @@ +/* + * Copyright (C) 2023 Cobblemon Contributors + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +package com.cobblemon.mod.common.api.spawning.rules.selector + +import com.cobblemon.mod.common.api.spawning.detail.SpawnDetail + +object AllSpawnDetailSelector : SpawnDetailSelector { + override fun selects(spawnDetail: SpawnDetail) = true +} \ No newline at end of file diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/rules/selector/AllSpawningContextSelector.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/rules/selector/AllSpawningContextSelector.kt new file mode 100644 index 0000000000..4e3870b09d --- /dev/null +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/rules/selector/AllSpawningContextSelector.kt @@ -0,0 +1,15 @@ +/* + * Copyright (C) 2023 Cobblemon Contributors + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +package com.cobblemon.mod.common.api.spawning.rules.selector + +import com.cobblemon.mod.common.api.spawning.context.SpawningContext + +object AllSpawningContextSelector : SpawningContextSelector { + override fun selects(ctx: SpawningContext) = true +} \ No newline at end of file diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/rules/selector/ConditionalSpawningContextSelector.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/rules/selector/ConditionalSpawningContextSelector.kt new file mode 100644 index 0000000000..954ec46513 --- /dev/null +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/rules/selector/ConditionalSpawningContextSelector.kt @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2023 Cobblemon Contributors + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +package com.cobblemon.mod.common.api.spawning.rules.selector + +import com.cobblemon.mod.common.api.spawning.condition.SpawningCondition +import com.cobblemon.mod.common.api.spawning.context.SpawningContext + +class ConditionalSpawningContextSelector : SpawningContextSelector { + var conditions = mutableListOf>() + var anticonditions = mutableListOf>() + + override fun selects(ctx: SpawningContext): Boolean { + return if (conditions.isNotEmpty() && conditions.none { it.isSatisfiedBy(ctx) }) { + false + } else { + !(anticonditions.isNotEmpty() && anticonditions.any { it.isSatisfiedBy(ctx) }) + } + } +} \ No newline at end of file diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/rules/selector/ExpressionSpawnDetailSelector.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/rules/selector/ExpressionSpawnDetailSelector.kt new file mode 100644 index 0000000000..2ca84113ab --- /dev/null +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/rules/selector/ExpressionSpawnDetailSelector.kt @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2023 Cobblemon Contributors + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +package com.cobblemon.mod.common.api.spawning.rules.selector + +import com.bedrockk.molang.runtime.MoLangRuntime +import com.cobblemon.mod.common.api.spawning.detail.SpawnDetail +import com.cobblemon.mod.common.util.asExpression +import com.cobblemon.mod.common.util.resolveBoolean + +class ExpressionSpawnDetailSelector : SpawnDetailSelector { + @Transient + val runtime = MoLangRuntime() + var expression = "true".asExpression() + + override fun selects(spawnDetail: SpawnDetail): Boolean { + runtime.environment.setSimpleVariable("spawn", spawnDetail.struct) + return runtime.resolveBoolean(expression) + } +} \ No newline at end of file diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/rules/selector/ExpressionSpawningContextSelector.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/rules/selector/ExpressionSpawningContextSelector.kt new file mode 100644 index 0000000000..871bea81bd --- /dev/null +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/rules/selector/ExpressionSpawningContextSelector.kt @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2023 Cobblemon Contributors + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +package com.cobblemon.mod.common.api.spawning.rules.selector + +import com.bedrockk.molang.runtime.MoLangRuntime +import com.cobblemon.mod.common.api.spawning.context.SpawningContext +import com.cobblemon.mod.common.util.asExpression +import com.cobblemon.mod.common.util.resolveBoolean + +class ExpressionSpawningContextSelector : SpawningContextSelector { + @Transient + val runtime = MoLangRuntime() + var expression = "true".asExpression() + + override fun selects(ctx: SpawningContext): Boolean { + runtime.environment.setSimpleVariable("context", ctx.getOrSetupStruct()) + return runtime.resolveBoolean(expression) + } +} \ No newline at end of file diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/rules/selector/SpawnDetailSelector.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/rules/selector/SpawnDetailSelector.kt new file mode 100644 index 0000000000..75382858a7 --- /dev/null +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/rules/selector/SpawnDetailSelector.kt @@ -0,0 +1,22 @@ +/* + * Copyright (C) 2023 Cobblemon Contributors + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +package com.cobblemon.mod.common.api.spawning.rules.selector + +import com.cobblemon.mod.common.api.spawning.detail.SpawnDetail + +interface SpawnDetailSelector { + companion object { + val types = mutableMapOf>() + + inline fun register(type: String) { + types[type] = T::class.java + } + } + fun selects(spawnDetail: SpawnDetail): Boolean +} \ No newline at end of file diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/rules/selector/SpawningContextSelector.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/rules/selector/SpawningContextSelector.kt new file mode 100644 index 0000000000..ac38cffea4 --- /dev/null +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/rules/selector/SpawningContextSelector.kt @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2023 Cobblemon Contributors + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +package com.cobblemon.mod.common.api.spawning.rules.selector + +import com.cobblemon.mod.common.api.spawning.context.SpawningContext + +interface SpawningContextSelector { + companion object { + val types = mutableMapOf>() + + inline fun register(type: String) { + types[type] = T::class.java + } + } + + fun selects(ctx: SpawningContext): Boolean +} \ No newline at end of file diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/selection/FlatSelector.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/selection/FlatSelector.kt index 003f864fd0..a6e0a8b7d3 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/selection/FlatSelector.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/selection/FlatSelector.kt @@ -26,8 +26,13 @@ import kotlin.random.Random * [FlatContextWeightedSelector] because here a spawning context type only appearing once * will not make it any less likely to be selected. * + * A spawn detail's weight when selecting between all possible spawn details will be the + * highest of its contextual weights. In other words, if a spawn is possible at both A and + * B contexts but is weighed more highly at A due to a weight multiplier, it will use the + * weight of the spawn at A when choosing which spawn will run. + * * When choosing which context a specific spawn detail will spawn at once that spawn detail - * has been chosen, it does a flat random across the possible contexts. + * has been chosen, it does a context-weighted random selection across those contexts. * * At a glance: * - Spawn detail selection is flat across context quantity @@ -37,10 +42,22 @@ import kotlin.random.Random * @since July 10th, 2022 */ open class FlatSelector : SpawningSelector { - open fun getWeight(contextType: RegisteredSpawningContext<*>) = contextType.getWeight() + protected class SelectingSpawnInformation { + val spawningContexts = mutableMapOf() + var highestWeight = 0F + + fun add(spawningContext: SpawningContext, contextWeight: Float) { + spawningContexts[spawningContext] = contextWeight + if (contextWeight > highestWeight) { + highestWeight = contextWeight + } + } + + fun chooseContext() = spawningContexts.entries.weightedSelection { it.value }!!.key + } protected class ContextSelectionData( - val spawnsToContexts: MutableMap>, + val spawnsToContexts: MutableMap, var percentSum: Float ) { val size: Int @@ -51,7 +68,7 @@ open class FlatSelector : SpawningSelector { spawner: Spawner, contexts: List ): ContextSelectionData { - val spawnsToContexts: MutableMap> = mutableMapOf() + val spawnsToContexts: MutableMap = mutableMapOf() var percentSum = 0F contexts.forEach { ctx -> @@ -65,9 +82,9 @@ open class FlatSelector : SpawningSelector { val selectingSpawnInformation = spawnsToContexts.getOrPut( it, - ::mutableListOf + ::SelectingSpawnInformation ) - selectingSpawnInformation.add(ctx) + selectingSpawnInformation.add(ctx, ctx.getWeight(it)) } } @@ -87,7 +104,6 @@ open class FlatSelector : SpawningSelector { val spawnsToContexts = selectionData.spawnsToContexts var percentSum = selectionData.percentSum - // First pass is doing percentage checks. if (percentSum > 0) { @@ -111,15 +127,14 @@ open class FlatSelector : SpawningSelector { if (spawnDetail.percentage > 0) { percentSum += spawnDetail.percentage if (percentSum >= selectedPercentage) { - return info.random() to spawnDetail + return info.chooseContext() to spawnDetail } } } } - val selectedSpawn = spawnsToContexts.entries.toList().weightedSelection { it.key.weight }!! - - return selectedSpawn.value.random() to selectedSpawn.key + val selectedSpawn = spawnsToContexts.entries.toList().weightedSelection { it.value.highestWeight }!! + return selectedSpawn.value.chooseContext() to selectedSpawn.key } override fun getTotalWeights( @@ -129,13 +144,13 @@ open class FlatSelector : SpawningSelector { val selectionData = getSelectionData(spawner, contexts) if (selectionData.size == 0) { - return mapOf() + return emptyMap() } val totalWeights = mutableMapOf() - for (spawnDetail in selectionData.spawnsToContexts.keys) { - totalWeights[spawnDetail] = spawnDetail.weight + for ((spawnDetail, info) in selectionData.spawnsToContexts.entries) { + totalWeights[spawnDetail] = info.highestWeight } return totalWeights diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/spawner/FixedAreaSpawner.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/spawner/FixedAreaSpawner.kt index 61640bcfa6..30bfb86c0b 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/spawner/FixedAreaSpawner.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/spawner/FixedAreaSpawner.kt @@ -13,7 +13,6 @@ import com.cobblemon.mod.common.api.spawning.SpawnerManager import com.cobblemon.mod.common.api.spawning.detail.SpawnPool import net.minecraft.server.world.ServerWorld import net.minecraft.util.math.BlockPos -import net.minecraft.world.World /** * A spawner that works within a fixed area, and ticks on its own. The location diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/spawner/PlayerSpawnerFactory.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/spawner/PlayerSpawnerFactory.kt index 3cf71d440d..c1e90e74fe 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/spawner/PlayerSpawnerFactory.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/spawning/spawner/PlayerSpawnerFactory.kt @@ -9,10 +9,12 @@ package com.cobblemon.mod.common.api.spawning.spawner import com.cobblemon.mod.common.api.spawning.CobblemonSpawnPools +import com.cobblemon.mod.common.api.spawning.CobblemonSpawnRules import com.cobblemon.mod.common.api.spawning.SpawnerManager import com.cobblemon.mod.common.api.spawning.detail.SpawnPool import com.cobblemon.mod.common.api.spawning.influence.PlayerLevelRangeInfluence import com.cobblemon.mod.common.api.spawning.influence.SpawningInfluence +import com.cobblemon.mod.common.api.spawning.rules.SpawnRule import net.minecraft.server.network.ServerPlayerEntity /** @@ -31,6 +33,7 @@ object PlayerSpawnerFactory { val influences = influenceBuilders.mapNotNull { it(player) } return PlayerSpawner(player, spawns, spawnerManager).also { it.influences.addAll(influences) + it.influences.addAll(CobblemonSpawnRules.rules.values.flatMap(SpawnRule::components)) } } } diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/client/render/models/blockbench/bedrock/animation/BedrockAnimation.kt b/common/src/main/kotlin/com/cobblemon/mod/common/client/render/models/blockbench/bedrock/animation/BedrockAnimation.kt index 51f6d6b7ba..b9f5aa2571 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/client/render/models/blockbench/bedrock/animation/BedrockAnimation.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/client/render/models/blockbench/bedrock/animation/BedrockAnimation.kt @@ -132,7 +132,7 @@ data class BedrockAnimation( val boneTimelines: Map ) { companion object { - val functionMappings = mutableMapOf>() + val functionMappings = hashMapOf>() val sharedRuntime = MoLangRuntime().also { it.environment.structs["query"] = it.environment.structs["variable"] it.environment.structs["script"] = QueryStruct(functionMappings) diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/data/CobblemonDataProvider.kt b/common/src/main/kotlin/com/cobblemon/mod/common/data/CobblemonDataProvider.kt index 91f00be678..8b60e577e4 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/data/CobblemonDataProvider.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/data/CobblemonDataProvider.kt @@ -24,12 +24,12 @@ import com.cobblemon.mod.common.api.pokemon.feature.GlobalSpeciesFeatures import com.cobblemon.mod.common.api.pokemon.feature.SpeciesFeatureAssignments import com.cobblemon.mod.common.api.pokemon.feature.SpeciesFeatures import com.cobblemon.mod.common.api.spawning.CobblemonSpawnPools +import com.cobblemon.mod.common.api.spawning.CobblemonSpawnRules import com.cobblemon.mod.common.api.spawning.SpawnDetailPresets import com.cobblemon.mod.common.battles.BagItems import com.cobblemon.mod.common.net.messages.client.data.UnlockReloadPacket import com.cobblemon.mod.common.platform.events.PlatformEvents import com.cobblemon.mod.common.pokemon.SpeciesAdditions -import com.cobblemon.mod.common.client.render.models.blockbench.repository.BerryModelRepository import com.cobblemon.mod.common.pokemon.properties.PropertiesCompletionProvider import com.cobblemon.mod.common.util.cobblemonResource import com.cobblemon.mod.common.util.ifClient @@ -62,6 +62,7 @@ object CobblemonDataProvider : DataProvider { this.register(PokeBalls) this.register(PropertiesCompletionProvider) this.register(SpawnDetailPresets) + this.register(CobblemonSpawnRules) this.register(CobblemonMechanics) this.register(BagItems) this.register(NPCClasses) diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/util/adapters/SpawnDetailSelectorAdapter.kt b/common/src/main/kotlin/com/cobblemon/mod/common/util/adapters/SpawnDetailSelectorAdapter.kt new file mode 100644 index 0000000000..ec25081676 --- /dev/null +++ b/common/src/main/kotlin/com/cobblemon/mod/common/util/adapters/SpawnDetailSelectorAdapter.kt @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2023 Cobblemon Contributors + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +package com.cobblemon.mod.common.util.adapters + +import com.cobblemon.mod.common.api.spawning.rules.selector.ExpressionSpawnDetailSelector +import com.cobblemon.mod.common.api.spawning.rules.selector.SpawnDetailSelector +import com.cobblemon.mod.common.util.asExpression +import com.google.gson.JsonDeserializationContext +import com.google.gson.JsonDeserializer +import com.google.gson.JsonElement +import com.google.gson.JsonObject +import java.lang.reflect.Type + +object SpawnDetailSelectorAdapter : JsonDeserializer { + override fun deserialize(json: JsonElement, type: Type, ctx: JsonDeserializationContext): SpawnDetailSelector { + return if (json.isJsonPrimitive) { + val expression = json.asString.asExpression() + ExpressionSpawnDetailSelector().also { it.expression = expression } + } else { + json as JsonObject + val type = json.get("type").asString + val clazz = SpawnDetailSelector.types[type] ?: throw IllegalArgumentException("Unknown spawn detail selector type: $type") + ctx.deserialize(json, clazz) + } + } +} \ No newline at end of file diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/util/adapters/SpawnRuleComponentAdapter.kt b/common/src/main/kotlin/com/cobblemon/mod/common/util/adapters/SpawnRuleComponentAdapter.kt new file mode 100644 index 0000000000..e2aa40d4d9 --- /dev/null +++ b/common/src/main/kotlin/com/cobblemon/mod/common/util/adapters/SpawnRuleComponentAdapter.kt @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2023 Cobblemon Contributors + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +package com.cobblemon.mod.common.util.adapters + +import com.cobblemon.mod.common.api.spawning.rules.component.SpawnRuleComponent +import com.google.gson.JsonDeserializationContext +import com.google.gson.JsonDeserializer +import com.google.gson.JsonElement +import com.google.gson.JsonObject +import java.lang.reflect.Type + +object SpawnRuleComponentAdapter : JsonDeserializer { + override fun deserialize(json: JsonElement, type: Type, ctx: JsonDeserializationContext): SpawnRuleComponent { + json as JsonObject + val type = json.get("type").asString + val clazz = SpawnRuleComponent.types[type] ?: throw IllegalArgumentException("Unknown spawn rule component type: $type") + return ctx.deserialize(json, clazz) + } +} \ No newline at end of file diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/util/adapters/SpawningContextSelectorAdapter.kt b/common/src/main/kotlin/com/cobblemon/mod/common/util/adapters/SpawningContextSelectorAdapter.kt new file mode 100644 index 0000000000..beb4850779 --- /dev/null +++ b/common/src/main/kotlin/com/cobblemon/mod/common/util/adapters/SpawningContextSelectorAdapter.kt @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2023 Cobblemon Contributors + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +package com.cobblemon.mod.common.util.adapters + +import com.cobblemon.mod.common.api.spawning.rules.selector.ConditionalSpawningContextSelector +import com.cobblemon.mod.common.api.spawning.rules.selector.ExpressionSpawningContextSelector +import com.cobblemon.mod.common.api.spawning.rules.selector.SpawningContextSelector +import com.cobblemon.mod.common.util.asExpression +import com.google.gson.JsonDeserializationContext +import com.google.gson.JsonDeserializer +import com.google.gson.JsonElement +import com.google.gson.JsonObject +import java.lang.reflect.Type + +object SpawningContextSelectorAdapter : JsonDeserializer { + override fun deserialize(json: JsonElement, type: Type, ctx: JsonDeserializationContext): SpawningContextSelector { + return if (json.isJsonPrimitive) { + val expression = json.asString.asExpression() + ExpressionSpawningContextSelector().also { it.expression = expression } + } else { + json as JsonObject + val type = json.get("type")?.asString ?: return ctx.deserialize(json, ConditionalSpawningContextSelector::class.java) + val clazz = SpawningContextSelector.types[type] ?: throw IllegalArgumentException("Unknown spawn detail selector type: $type") + ctx.deserialize(json, clazz) + } + } +} \ No newline at end of file diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/util/adapters/TextAdapter.kt b/common/src/main/kotlin/com/cobblemon/mod/common/util/adapters/TextAdapter.kt new file mode 100644 index 0000000000..e889033b5b --- /dev/null +++ b/common/src/main/kotlin/com/cobblemon/mod/common/util/adapters/TextAdapter.kt @@ -0,0 +1,20 @@ +/* + * Copyright (C) 2023 Cobblemon Contributors + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +package com.cobblemon.mod.common.util.adapters + +import com.cobblemon.mod.common.api.text.text +import com.google.gson.JsonDeserializationContext +import com.google.gson.JsonDeserializer +import com.google.gson.JsonElement +import java.lang.reflect.Type +import net.minecraft.text.Text + +object TextAdapter : JsonDeserializer { + override fun deserialize(json: JsonElement, type: Type, ctx: JsonDeserializationContext) = json.asString.text() +} \ No newline at end of file diff --git a/common/src/main/resources/data/cobblemon/spawn_rules/pikachu_bright.json b/common/src/main/resources/data/cobblemon/spawn_rules/pikachu_bright.json new file mode 100644 index 0000000000..db03d033ee --- /dev/null +++ b/common/src/main/resources/data/cobblemon/spawn_rules/pikachu_bright.json @@ -0,0 +1,7 @@ +{ + "displayName": "Pikachu Daylight Multiplier", + "components": [ + ] +} + + diff --git a/deps/local/com/bedrockk/molang/1.1.8/molang-1.1.8.jar b/deps/local/com/bedrockk/molang/1.1.8/molang-1.1.8.jar new file mode 100644 index 0000000000..6730000151 Binary files /dev/null and b/deps/local/com/bedrockk/molang/1.1.8/molang-1.1.8.jar differ diff --git a/deps/local/com/bedrockk/molang/1.1.8/molang-1.1.8.pom b/deps/local/com/bedrockk/molang/1.1.8/molang-1.1.8.pom new file mode 100644 index 0000000000..dc73e5ccb0 --- /dev/null +++ b/deps/local/com/bedrockk/molang/1.1.8/molang-1.1.8.pom @@ -0,0 +1,8 @@ + + + 4.0.0 + local.com.bedrockk + molang + 1.1.8 + diff --git a/deps/local/com/bedrockk/molang/maven-metadata-local.xml b/deps/local/com/bedrockk/molang/maven-metadata-local.xml index 33b1d68b07..4401fa6242 100644 --- a/deps/local/com/bedrockk/molang/maven-metadata-local.xml +++ b/deps/local/com/bedrockk/molang/maven-metadata-local.xml @@ -3,10 +3,10 @@ local.com.bedrockk molang - 1.1.5 + 1.1.8 - 1.1.5 + 1.1.8 - 20221939127999 + 20221949138999 diff --git a/gradle/libs.versions.yml b/gradle/libs.versions.yml index 8f66e20637..71f700fe63 100644 --- a/gradle/libs.versions.yml +++ b/gradle/libs.versions.yml @@ -25,7 +25,7 @@ versions: # common graal: 22.3.0 - molang: 1.1.5 + molang: 1.1.8 # test junit: 5.9.0