Add compute_density_function to replace removed `sample_noise (#1591)
This commit is contained in:
1 parent
d26a56ea98
commit
f2c658bf03
10 files changed
+141
-318
No files matched your search
@@ -3,6 +3,7 @@ package carpet.script.api;
|
||||
import carpet.CarpetSettings;
|
||||
import carpet.fakes.ChunkGeneratorInterface;
|
||||
import carpet.fakes.ChunkTicketManagerInterface;
|
||||
import carpet.fakes.RandomStateVisitorAccessor;
|
||||
import carpet.fakes.ServerChunkManagerInterface;
|
||||
import carpet.fakes.ServerWorldInterface;
|
||||
import carpet.fakes.SpawnHelperInnerInterface;
|
||||
@@ -41,15 +42,22 @@ import it.unimi.dsi.fastutil.longs.LongSet;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.core.Direction;
|
||||
import net.minecraft.core.Holder;
|
||||
import net.minecraft.core.HolderLookup;
|
||||
import net.minecraft.core.HolderSet;
|
||||
import net.minecraft.core.QuartPos;
|
||||
import net.minecraft.core.Registry;
|
||||
import net.minecraft.core.registries.Registries;
|
||||
import net.minecraft.resources.ResourceKey;
|
||||
import net.minecraft.tags.TagKey;
|
||||
import net.minecraft.world.level.chunk.ChunkGenerator;
|
||||
import net.minecraft.world.level.chunk.PalettedContainer;
|
||||
import net.minecraft.world.level.levelgen.DensityFunction;
|
||||
import net.minecraft.world.level.levelgen.NoiseBasedChunkGenerator;
|
||||
import net.minecraft.world.level.levelgen.NoiseRouter;
|
||||
import net.minecraft.world.level.levelgen.RandomState;
|
||||
import net.minecraft.world.level.levelgen.structure.Structure;
|
||||
import net.minecraft.world.level.levelgen.structure.StructureType;
|
||||
import net.minecraft.world.level.levelgen.synth.NormalNoise;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -1626,23 +1634,68 @@ public class WorldAccess {
|
||||
}
|
||||
|
||||
@ScarpetFunction(maxParams = -1)
|
||||
public Value sample_noise(Context c, @Locator.Block BlockPos pos, String... noiseQueries) {
|
||||
return Value.NULL;
|
||||
/*
|
||||
int mappedX = QuartPos.fromBlock(pos.getX());
|
||||
int mappedY = QuartPos.fromBlock(pos.getY());
|
||||
int mappedZ = QuartPos.fromBlock(pos.getZ());
|
||||
Climate.Sampler mns = ((CarpetContext) c).s.getLevel().getChunkSource().getGenerator().climateSampler();
|
||||
Map<Value, Value> ret = new HashMap<>();
|
||||
public Value compute_density_function(Context c, @Locator.Block BlockPos pos, String... densityFunctionQueries) {
|
||||
|
||||
if (noiseQueries.length == 0) {
|
||||
noiseQueries = new String[]{"continentalness", "erosion", "weirdness", "temperature", "humidity", "depth"};
|
||||
Map<Value, Value> result = new HashMap<>();
|
||||
|
||||
ServerLevel level = ((CarpetContext) c).level();
|
||||
Registry<DensityFunction> densityFunctionRegistry = level.registryAccess().registryOrThrow(Registries.DENSITY_FUNCTION);
|
||||
|
||||
if (densityFunctionQueries.length == 0) {
|
||||
return ListValue.wrap(densityFunctionRegistry.keySet().stream().map(ValueConversions::of));
|
||||
}
|
||||
|
||||
for (String noise : noiseQueries) {
|
||||
double noiseValue = ((NoiseColumnSamplerInterface) mns).getNoiseSample(noise, mappedX, mappedY, mappedZ);
|
||||
ret.put(new StringValue(noise), new NumericValue(noiseValue));
|
||||
ChunkGenerator chunkGenerator = level.getChunkSource().getGenerator();
|
||||
if (!(chunkGenerator instanceof NoiseBasedChunkGenerator generator)) {
|
||||
return MapValue.wrap(result);
|
||||
}
|
||||
return MapValue.wrap(ret);*/
|
||||
|
||||
RandomState randomState = RandomState.create(
|
||||
generator.generatorSettings().value(),
|
||||
level.registryAccess().lookupOrThrow(Registries.NOISE), level.getSeed()
|
||||
);
|
||||
|
||||
DensityFunction.Visitor visitor = ((RandomStateVisitorAccessor) (Object) randomState).getVisitor();
|
||||
DensityFunction.SinglePointContext singlePointContext = new DensityFunction.SinglePointContext(pos.getX(), pos.getY(), pos.getZ());
|
||||
NoiseRouter router = generator.generatorSettings().value().noiseRouter();
|
||||
|
||||
for (String densityFunctionQuery : densityFunctionQueries) {
|
||||
DensityFunction densityFunction = switch (densityFunctionQuery) {
|
||||
case "barrier_noise" -> router.barrierNoise();
|
||||
case "fluid_level_floodedness_noise" -> router.fluidLevelFloodednessNoise();
|
||||
case "fluid_level_spread_noise" -> router.fluidLevelSpreadNoise();
|
||||
case "lava_noise" -> router.lavaNoise();
|
||||
case "temperature" -> router.temperature();
|
||||
case "vegetation" -> router.vegetation();
|
||||
case "continents" -> router.continents();
|
||||
case "erosion" -> router.erosion();
|
||||
case "depth" -> router.depth();
|
||||
case "ridges" -> router.ridges();
|
||||
case "initial_density_without_jaggedness" -> router.initialDensityWithoutJaggedness();
|
||||
case "final_density" -> router.finalDensity();
|
||||
case "vein_toggle" -> router.veinToggle();
|
||||
case "vein_ridged" -> router.veinRidged();
|
||||
case "vein_gap" -> router.veinGap();
|
||||
default -> {
|
||||
ResourceLocation densityFunctionKey = InputValidator.identifierOf(densityFunctionQuery);
|
||||
|
||||
if (densityFunctionRegistry.containsKey(densityFunctionKey)) {
|
||||
yield densityFunctionRegistry.get(densityFunctionKey);
|
||||
}
|
||||
|
||||
throw new InternalExpressionException(
|
||||
"Density function '" + densityFunctionQuery + "' doesn't exist. " +
|
||||
"Please check the spelling or use with full 'namespace:value' if its custom defined."
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
if (densityFunction == null) // IDE thinks may be null as Registry.get is Nullable, but we checked it
|
||||
throw new IllegalStateException("density function can't be null here");
|
||||
|
||||
double value = densityFunction.mapAll(visitor).compute(singlePointContext);
|
||||
result.put(StringValue.of(densityFunctionQuery), new NumericValue(value));
|
||||
}
|
||||
return MapValue.wrap(result);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user