From 5a8ce26c443cf7f95e620f1667dc33f50464019f Mon Sep 17 00:00:00 2001 From: gnembon Date: Sat, 28 Nov 2020 19:02:58 -0400 Subject: [PATCH 1/2] 'biome' query api --- docs/scarpet/api/BlocksAndWorldAccess.md | 27 +++++++++- .../java/carpet/script/api/WorldAccess.java | 16 ++++-- .../java/carpet/script/utils/BiomeInfo.java | 51 +++++++++++++------ .../java/carpet/script/value/ListValue.java | 6 ++- 4 files changed, 76 insertions(+), 24 deletions(-) diff --git a/docs/scarpet/api/BlocksAndWorldAccess.md b/docs/scarpet/api/BlocksAndWorldAccess.md index 8b64135c..7f233fa6 100644 --- a/docs/scarpet/api/BlocksAndWorldAccess.md +++ b/docs/scarpet/api/BlocksAndWorldAccess.md @@ -306,9 +306,32 @@ poi(x,y,z,5) => [] // nothing around poi(x,y,z,5) => [['nether_portal',0,[7,8,9]],['nether_portal',0,[7,9,9]]] // two portal blocks in the range -### `biome(pos)` +### `biome()` `biome(name)` `biome(block)` `biome(block/name, feature)` -Returns the biome at that block position. +Without arguments, returns the list of biomes in the world. + +With block, or name, returns the name of the biome in that position, or null, if provided biome is not valid. + +With an optional feature, it returns value for the specified attribute for that biome. Available and querable features include: +* `'top_material'`: unlocalized block representing the top surface material +* `'under_material'`: unlocalized block representing what sits below topsoil +* `'category'`: the parent biome this biome is derived from. Possible values include: +`'none'`, `'taiga'`, `'extreme_hills'`, `'jungle'`, `'mesa'`, `'plains'`, `'savanna'`, +`'icy'`, `'the_end'`, `'beach'`, `'forest'`, `'ocean'`, `'desert'`, `'river'`, +`'swamp'`, `'mushroom'` and `'nether'`. +* `'temperature'`: temperature from 0 to 1 +* `'fog_color'`: RGBA color value of fog +* `'foliage_color'`: RGBA color value of foliage +* `'sky_color'`: RGBA color value of sky +* `'water_color'`: RGBA color value of water +* `'water_fog_color'`: RGBA color value of water fog +* `'humidity'`: value from 0 to 1 indicating how wet is the biome +* `'precipitation'`: value from 0 to 1 indicating if the biome has precipitation +* `'depth'`: value from -2 to 2 indicating how high or low the terrain should generate. Values > 0 indicate generation above sea level +and values < 0, below sea level. +* `'scale'`: value from -2 to 2 indicating how flat is the terrain. +* `'features'`: list of features that generate in the biome, grouped by generation steps +* `'structures'`: list of structures that generate in the biome. ### `solid(pos)` diff --git a/src/main/java/carpet/script/api/WorldAccess.java b/src/main/java/carpet/script/api/WorldAccess.java index 154b09f5..5beb2a25 100644 --- a/src/main/java/carpet/script/api/WorldAccess.java +++ b/src/main/java/carpet/script/api/WorldAccess.java @@ -1061,16 +1061,22 @@ public class WorldAccess { expression.addLazyFunction("biome", -1, (c, t, lv) -> { CarpetContext cc = (CarpetContext)c; + ServerWorld world = cc.s.getWorld(); + if (lv.size() == 0) + { + Value ret = ListValue.wrap(world.getRegistryManager().get(Registry.BIOME_KEY).getIds().stream().map(ValueConversions::of)); + return (_c, _t) -> ret; + } BlockArgument locator = BlockArgument.findIn(cc, lv, 0, false, false, true); + Biome biome; if (locator.replacement != null) { - biome = cc.s.getMinecraftServer().getRegistryManager().get(Registry.BIOME_KEY).get(new Identifier(locator.replacement)); - if (biome == null) throw new InternalExpressionException("Unknown biome: "+locator.replacement); + biome = world.getRegistryManager().get(Registry.BIOME_KEY).get(new Identifier(locator.replacement)); + if (biome == null) return LazyValue.NULL; } else { - ServerWorld world = cc.s.getWorld(); BlockPos pos = locator.block.getPos(); biome = world.getBiome(pos); } @@ -1082,9 +1088,9 @@ public class WorldAccess { return (_c, _t) -> res; } String biomeFeature = lv.get(locator.offset).evalValue(c).getString(); - Function featureProvider = BiomeInfo.biomeFeatures.get(biomeFeature); + BiFunction featureProvider = BiomeInfo.biomeFeatures.get(biomeFeature); if (featureProvider == null) throw new InternalExpressionException("Unknown biome feature: "+biomeFeature); - Value ret = featureProvider.apply(biome); + Value ret = featureProvider.apply(world, biome); return (_c, _t) -> ret; }); diff --git a/src/main/java/carpet/script/utils/BiomeInfo.java b/src/main/java/carpet/script/utils/BiomeInfo.java index d62dd88e..9ff63005 100644 --- a/src/main/java/carpet/script/utils/BiomeInfo.java +++ b/src/main/java/carpet/script/utils/BiomeInfo.java @@ -1,32 +1,51 @@ package carpet.script.utils; import carpet.script.value.BlockValue; +import carpet.script.value.ListValue; import carpet.script.value.NumericValue; import carpet.script.value.StringValue; import carpet.script.value.Value; import carpet.script.value.ValueConversions; +import net.minecraft.server.world.ServerWorld; +import net.minecraft.util.registry.Registry; import net.minecraft.world.biome.Biome; +import net.minecraft.world.gen.feature.ConfiguredFeature; +import net.minecraft.world.gen.feature.ConfiguredStructureFeature; import java.util.HashMap; import java.util.Map; -import java.util.function.Function; +import java.util.function.BiFunction; public class BiomeInfo { - public final static Map> biomeFeatures = new HashMap>(){{ - put("top_material", b -> new BlockValue( b.getGenerationSettings().getSurfaceConfig().getTopMaterial(), null, null)); - put("under_material", b -> new BlockValue( b.getGenerationSettings().getSurfaceConfig().getUnderMaterial(), null, null)); - put("category", b -> StringValue.of(b.getCategory().getName())); - put("temperature", b -> NumericValue.of(b.getTemperature())); - put("fog_color", b -> ValueConversions.ofRGB(b.getFogColor())); - put("foliage_color", b -> ValueConversions.ofRGB(b.getFoliageColor())); - put("sky_color", b -> ValueConversions.ofRGB(b.getSkyColor())); - put("water_color", b -> ValueConversions.ofRGB(b.getWaterColor())); - put("water_fog_color", b -> ValueConversions.ofRGB(b.getWaterFogColor())); - put("humidity", b -> NumericValue.of(b.getDownfall())); - put("precipitation", b -> StringValue.of(b.getPrecipitation().getName())); - put("depth", b -> NumericValue.of(b.getDepth())); - put("scale", b -> NumericValue.of(b.getScale())); - //put("features", b -> b.getGenerationSettings().getFeatures()) + public final static Map> biomeFeatures = new HashMap>(){{ + put("top_material", (w, b) -> new BlockValue( b.getGenerationSettings().getSurfaceConfig().getTopMaterial(), null, null)); + put("under_material", (w, b) -> new BlockValue( b.getGenerationSettings().getSurfaceConfig().getUnderMaterial(), null, null)); + put("category", (w, b) -> StringValue.of(b.getCategory().getName())); + put("temperature", (w, b) -> NumericValue.of(b.getTemperature())); + put("fog_color", (w, b) -> ValueConversions.ofRGB(b.getFogColor())); + put("foliage_color", (w, b) -> ValueConversions.ofRGB(b.getFoliageColor())); + put("sky_color", (w, b) -> ValueConversions.ofRGB(b.getSkyColor())); + put("water_color", (w, b) -> ValueConversions.ofRGB(b.getWaterColor())); + put("water_fog_color", (w, b) -> ValueConversions.ofRGB(b.getWaterFogColor())); + put("humidity", (w, b) -> NumericValue.of(b.getDownfall())); + put("precipitation", (w, b) -> StringValue.of(b.getPrecipitation().getName())); + put("depth", (w, b) -> NumericValue.of(b.getDepth())); + put("scale", (w, b) -> NumericValue.of(b.getScale())); + put("features", (w, b) -> { + + Registry> registry = w.getRegistryManager().get(Registry.CONFIGURED_FEATURE_WORLDGEN); + return ListValue.wrap( + b.getGenerationSettings().getFeatures().stream().map(step -> + ListValue.wrap(step.stream().map(cfp -> + ValueConversions.of(registry.getId(cfp.get())) + )) + ) + ); + }); + put("structures", (w, b) -> { + Registry> registry = w.getRegistryManager().get(Registry.CONFIGURED_STRUCTURE_FEATURE_WORLDGEN); + return ListValue.wrap(b.getGenerationSettings().getStructureFeatures().stream().map(str -> ValueConversions.of(registry.getId(str.get())))); + }); }}; } diff --git a/src/main/java/carpet/script/value/ListValue.java b/src/main/java/carpet/script/value/ListValue.java index 3637837a..c7a7a409 100644 --- a/src/main/java/carpet/script/value/ListValue.java +++ b/src/main/java/carpet/script/value/ListValue.java @@ -22,6 +22,7 @@ import java.util.Iterator; import java.util.List; import java.util.Set; import java.util.stream.Collectors; +import java.util.stream.Stream; import java.util.stream.StreamSupport; import static java.lang.Math.abs; @@ -91,7 +92,10 @@ public class ListValue extends AbstractListValue implements ContainerValueInterf } - + public static ListValue wrap(Stream stream) + { + return wrap(stream.collect(Collectors.toList())); + } public static ListValue wrap(List list) { From e78541e5b93fcc08a8742be3b7aec2847c452fca Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Sat, 28 Nov 2020 23:03:23 +0000 Subject: [PATCH 2/2] Merge docs for ''biome' query api' --- docs/scarpet/Full.md | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/docs/scarpet/Full.md b/docs/scarpet/Full.md index d861d1f1..de176790 100644 --- a/docs/scarpet/Full.md +++ b/docs/scarpet/Full.md @@ -2262,9 +2262,32 @@ poi(x,y,z,5) => [] // nothing around poi(x,y,z,5) => [['nether_portal',0,[7,8,9]],['nether_portal',0,[7,9,9]]] // two portal blocks in the range -### `biome(pos)` +### `biome()` `biome(name)` `biome(block)` `biome(block/name, feature)` -Returns the biome at that block position. +Without arguments, returns the list of biomes in the world. + +With block, or name, returns the name of the biome in that position, or null, if provided biome is not valid. + +With an optional feature, it returns value for the specified attribute for that biome. Available and querable features include: +* `'top_material'`: unlocalized block representing the top surface material +* `'under_material'`: unlocalized block representing what sits below topsoil +* `'category'`: the parent biome this biome is derived from. Possible values include: +`'none'`, `'taiga'`, `'extreme_hills'`, `'jungle'`, `'mesa'`, `'plains'`, `'savanna'`, +`'icy'`, `'the_end'`, `'beach'`, `'forest'`, `'ocean'`, `'desert'`, `'river'`, +`'swamp'`, `'mushroom'` and `'nether'`. +* `'temperature'`: temperature from 0 to 1 +* `'fog_color'`: RGBA color value of fog +* `'foliage_color'`: RGBA color value of foliage +* `'sky_color'`: RGBA color value of sky +* `'water_color'`: RGBA color value of water +* `'water_fog_color'`: RGBA color value of water fog +* `'humidity'`: value from 0 to 1 indicating how wet is the biome +* `'precipitation'`: value from 0 to 1 indicating if the biome has precipitation +* `'depth'`: value from -2 to 2 indicating how high or low the terrain should generate. Values > 0 indicate generation above sea level +and values < 0, below sea level. +* `'scale'`: value from -2 to 2 indicating how flat is the terrain. +* `'features'`: list of features that generate in the biome, grouped by generation steps +* `'structures'`: list of structures that generate in the biome. ### `solid(pos)`