mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-02-16 10:30:42 +00:00
add SamplerFunction
This commit is contained in:
@@ -1,26 +1,65 @@
|
||||
package com.dfsek.terra.addons.terrascript.sampler;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.ImplementationArguments;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.functions.Function;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.variables.Variable;
|
||||
import com.dfsek.terra.addons.terrascript.script.TerraImplementationArguments;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
import com.dfsek.terra.api.noise.NoiseSampler;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
|
||||
public class SamplerFunction implements Function<Void> {
|
||||
public class SamplerFunction implements Function<Number> {
|
||||
private final Returnable<Number> x, y, z;
|
||||
private final Returnable<String> function;
|
||||
|
||||
private final java.util.function.Function<Supplier<String>, NoiseSampler> samplerFunction;
|
||||
|
||||
private final boolean twoD;
|
||||
private final Position position;
|
||||
|
||||
public SamplerFunction(Returnable<String> function,
|
||||
Returnable<Number> x,
|
||||
Returnable<Number> y,
|
||||
Returnable<Number> z,
|
||||
java.util.function.Function<Supplier<String>, NoiseSampler> samplerFunction,
|
||||
boolean twoD,
|
||||
Position position) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
this.function = function;
|
||||
this.samplerFunction = samplerFunction;
|
||||
this.twoD = twoD;
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
|
||||
return null;
|
||||
public Number apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
|
||||
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
|
||||
double x = this.x.apply(implementationArguments, variableMap).doubleValue();
|
||||
|
||||
double z = this.z.apply(implementationArguments, variableMap).doubleValue();
|
||||
|
||||
NoiseSampler sampler = samplerFunction.apply(() -> function.apply(implementationArguments, variableMap));
|
||||
if(twoD) {
|
||||
return sampler.noise(arguments.getWorld().getSeed(), x, z);
|
||||
} else {
|
||||
double y = this.y.apply(implementationArguments, variableMap).doubleValue();
|
||||
return sampler.noise(arguments.getWorld().getSeed(), x, y, z);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position getPosition() {
|
||||
return null;
|
||||
return position;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReturnType returnType() {
|
||||
return null;
|
||||
return ReturnType.NUMBER;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,74 @@
|
||||
package com.dfsek.terra.addons.terrascript.sampler;
|
||||
|
||||
import com.dfsek.terra.addons.noise.config.DimensionApplicableNoiseSampler;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable.ReturnType;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.constants.NumericConstant;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.constants.StringConstant;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.functions.FunctionBuilder;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
import com.dfsek.terra.api.noise.NoiseSampler;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
|
||||
public class SamplerFunctionBuilder implements FunctionBuilder<SamplerFunction> {
|
||||
private final Map<String, DimensionApplicableNoiseSampler> samplers2d;
|
||||
private final Map<String, DimensionApplicableNoiseSampler> samplers3d;
|
||||
|
||||
public SamplerFunctionBuilder(Map<String, DimensionApplicableNoiseSampler> samplers) {
|
||||
this.samplers2d = new HashMap<>();
|
||||
this.samplers3d = new HashMap<>();
|
||||
|
||||
samplers.forEach((id, sampler) -> {
|
||||
if(sampler.getDimensions() == 2) {
|
||||
samplers2d.put(id, sampler);
|
||||
} else if(sampler.getDimensions() == 3) {
|
||||
samplers3d.put(id, sampler);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public SamplerFunction build(List<Returnable<?>> argumentList, Position position) {
|
||||
return null;
|
||||
Function<Supplier<String>, NoiseSampler> function;
|
||||
|
||||
Returnable<String> arg = (Returnable<String>) argumentList.get(0);
|
||||
|
||||
if(argumentList.size() == 3) { // 2D
|
||||
if(arg instanceof StringConstant constant) {
|
||||
NoiseSampler sampler = samplers2d.get(constant.getConstant()).getSampler();
|
||||
function = s -> sampler;
|
||||
} else {
|
||||
function = s -> samplers2d.get(s.get()).getSampler();
|
||||
}
|
||||
return new SamplerFunction((Returnable<String>) argumentList.get(0),
|
||||
(Returnable<Number>) argumentList.get(1),
|
||||
new NumericConstant(0, position),
|
||||
(Returnable<Number>) argumentList.get(3),
|
||||
function,
|
||||
true,
|
||||
position);
|
||||
} else { // 3D
|
||||
if(arg instanceof StringConstant constant) {
|
||||
NoiseSampler sampler = samplers3d.get(constant.getConstant()).getSampler();
|
||||
function = s -> sampler;
|
||||
} else {
|
||||
function = s -> samplers3d.get(s.get()).getSampler();
|
||||
}
|
||||
return new SamplerFunction((Returnable<String>) argumentList.get(0),
|
||||
(Returnable<Number>) argumentList.get(1),
|
||||
(Returnable<Number>) argumentList.get(2),
|
||||
(Returnable<Number>) argumentList.get(3),
|
||||
function,
|
||||
false,
|
||||
position);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.dfsek.terra.addons.terrascript.sampler;
|
||||
|
||||
import com.dfsek.terra.addons.manifest.api.AddonInitializer;
|
||||
import com.dfsek.terra.addons.noise.NoiseConfigPackTemplate;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.functions.FunctionBuilder;
|
||||
import com.dfsek.terra.api.Platform;
|
||||
import com.dfsek.terra.api.addon.BaseAddon;
|
||||
@@ -26,7 +27,8 @@ public class TerraScriptSamplerFunctionAddon implements AddonInitializer {
|
||||
.then(event -> event
|
||||
.getPack()
|
||||
.getOrCreateRegistry(FunctionBuilder.class)
|
||||
.register(addon.key("sampler"), new SamplerFunctionBuilder(event.getPack().getContext().get())))
|
||||
.register(addon.key("sampler"), new SamplerFunctionBuilder(event.getPack().getContext().get(
|
||||
NoiseConfigPackTemplate.class).getSamplers())))
|
||||
.failThrough();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user