mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2025-07-03 08:25:31 +00:00
noise addon + pack.yml noise functions
This commit is contained in:
parent
b8e1e99b84
commit
39f5aef0c5
@ -1,5 +1,6 @@
|
||||
package com.dfsek.terra.addons.noise;
|
||||
|
||||
import com.dfsek.tectonic.exception.ConfigException;
|
||||
import com.dfsek.terra.addons.noise.config.NoiseSamplerBuilderLoader;
|
||||
import com.dfsek.terra.addons.noise.config.templates.DomainWarpTemplate;
|
||||
import com.dfsek.terra.addons.noise.config.templates.ImageSamplerTemplate;
|
||||
@ -31,13 +32,15 @@ import com.dfsek.terra.api.addon.annotations.Addon;
|
||||
import com.dfsek.terra.api.addon.annotations.Author;
|
||||
import com.dfsek.terra.api.addon.annotations.Version;
|
||||
import com.dfsek.terra.api.event.EventListener;
|
||||
import com.dfsek.terra.api.event.annotations.Global;
|
||||
import com.dfsek.terra.api.event.events.config.ConfigPackPreLoadEvent;
|
||||
import com.dfsek.terra.api.injection.annotations.Inject;
|
||||
import com.dfsek.terra.api.registry.CheckedRegistry;
|
||||
import com.dfsek.terra.api.util.seeded.NoiseProvider;
|
||||
import com.dfsek.terra.api.util.seeded.NoiseSeeded;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Addon("noise")
|
||||
@Author("Terra")
|
||||
@Version("1.0.0")
|
||||
@ -54,23 +57,19 @@ public class NoiseAddon extends TerraAddon implements EventListener {
|
||||
.applyLoader(NormalNormalizerTemplate.class, NormalNormalizerTemplate::new)
|
||||
.applyLoader(ImageSampler.Channel.class, (t, object, cf) -> ImageSampler.Channel.valueOf((String) object))
|
||||
.applyLoader(ClampNormalizerTemplate.class, ClampNormalizerTemplate::new)
|
||||
.applyLoader(ImageSamplerTemplate.class, ImageSamplerTemplate::new)
|
||||
.applyLoader(CellularSampler.ReturnType.class, (t, object, cf) -> CellularSampler.ReturnType.valueOf((String) object))
|
||||
.applyLoader(CellularSampler.DistanceFunction.class, (t, object, cf) -> CellularSampler.DistanceFunction.valueOf((String) object));
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public void packPreLoad(ConfigPackPreLoadEvent event) {
|
||||
|
||||
event.getPack()
|
||||
.applyLoader(NoiseSeeded.class, new NoiseSamplerBuilderLoader(event.getPack().getRegistry(NoiseProvider.class)));
|
||||
|
||||
CheckedRegistry<NoiseProvider> noiseRegistry = event.getPack().getRegistry(NoiseProvider.class);
|
||||
event.getPack()
|
||||
.applyLoader(NoiseSeeded.class, new NoiseSamplerBuilderLoader(noiseRegistry));
|
||||
|
||||
noiseRegistry.registerUnchecked("LINEAR", LinearNormalizerTemplate::new);
|
||||
noiseRegistry.registerUnchecked("NORMAL", NormalNormalizerTemplate::new);
|
||||
noiseRegistry.registerUnchecked("CLAMP", ClampNormalizerTemplate::new);
|
||||
noiseRegistry.registerUnchecked("EXPRESSION", ExpressionFunctionTemplate::new);
|
||||
|
||||
noiseRegistry.registerUnchecked("IMAGE", ImageSamplerTemplate::new);
|
||||
|
||||
@ -98,5 +97,17 @@ public class NoiseAddon extends TerraAddon implements EventListener {
|
||||
noiseRegistry.registerUnchecked("CONSTANT", ConstantNoiseTemplate::new);
|
||||
|
||||
noiseRegistry.registerUnchecked("KERNEL", KernelTemplate::new);
|
||||
|
||||
Map<String, NoiseSeeded> packFunctions = new HashMap<>();
|
||||
noiseRegistry.registerUnchecked("EXPRESSION", () -> new ExpressionFunctionTemplate(packFunctions));
|
||||
|
||||
|
||||
try {
|
||||
NoiseConfigPackTemplate template = new NoiseConfigPackTemplate();
|
||||
event.loadTemplate(template);
|
||||
packFunctions.putAll(template.getNoiseBuilderMap());
|
||||
} catch(ConfigException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,17 @@
|
||||
package com.dfsek.terra.addons.noise;
|
||||
|
||||
import com.dfsek.tectonic.annotations.Value;
|
||||
import com.dfsek.tectonic.config.ConfigTemplate;
|
||||
import com.dfsek.terra.api.config.ConfigType;
|
||||
import com.dfsek.terra.api.util.seeded.NoiseSeeded;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class NoiseConfigPackTemplate implements ConfigTemplate {
|
||||
@Value("noise")
|
||||
private Map<String, NoiseSeeded> noiseBuilderMap;
|
||||
|
||||
public Map<String, NoiseSeeded> getNoiseBuilderMap() {
|
||||
return noiseBuilderMap;
|
||||
}
|
||||
}
|
@ -26,6 +26,9 @@ public class NoiseSamplerBuilderLoader implements TypeLoader<NoiseSeeded> {
|
||||
public NoiseSeeded load(Type t, Object c, ConfigLoader loader) throws LoadException {
|
||||
Map<String, Object> map = (Map<String, Object>) c;
|
||||
try {
|
||||
if(!noiseRegistry.contains((String) map.get("type"))) {
|
||||
throw new LoadException("No such noise function: " + map.get("type"));
|
||||
}
|
||||
ObjectTemplate<NoiseSeeded> normalizerTemplate = noiseRegistry.get(((String) map.get("type")).toUpperCase(Locale.ROOT)).get();
|
||||
loader.load(normalizerTemplate, new Configuration(map));
|
||||
return normalizerTemplate.get();
|
||||
|
@ -39,6 +39,12 @@ public class ExpressionFunctionTemplate extends SamplerTemplate<ExpressionFuncti
|
||||
@Default
|
||||
private LinkedHashMap<String, FunctionTemplate> expressions = new LinkedHashMap<>();
|
||||
|
||||
private final Map<String, NoiseSeeded> otherFunctions;
|
||||
|
||||
public ExpressionFunctionTemplate(Map<String, NoiseSeeded> otherFunctions) {
|
||||
this.otherFunctions = otherFunctions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public NoiseSampler apply(Long seed) {
|
||||
try {
|
||||
@ -67,6 +73,12 @@ public class ExpressionFunctionTemplate extends SamplerTemplate<ExpressionFuncti
|
||||
noiseFunctionMap.put(entry.getKey(), UserDefinedFunction.newInstance(entry.getValue(), new Parser(), new Scope()));
|
||||
}
|
||||
|
||||
otherFunctions.forEach((id, function) -> {
|
||||
if(function.getDimensions() == 2) {
|
||||
noiseFunctionMap.put(id, new NoiseFunction2(function.apply(seed)));
|
||||
} else noiseFunctionMap.put(id, new NoiseFunction3(function.apply(seed)));
|
||||
});
|
||||
|
||||
functions.forEach((id, function) -> {
|
||||
if(function.getDimensions() == 2) {
|
||||
noiseFunctionMap.put(id, new NoiseFunction2(function.apply(seed)));
|
||||
|
@ -70,9 +70,6 @@ public class ConfigPackImpl implements ConfigPack {
|
||||
|
||||
private final RegistryFactory registryFactory = new RegistryFactoryImpl();
|
||||
|
||||
private final Map<Type, TypeLoader<?>> loaders = new HashMap<>();
|
||||
private final Map<Type, TemplateProvider<ObjectTemplate<?>>> objectLoaders = new HashMap<>();
|
||||
|
||||
private final AbstractConfigLoader abstractConfigLoader = new AbstractConfigLoader();
|
||||
private final ConfigLoader selfLoader = new ConfigLoader();
|
||||
private final Scope varScope = new Scope();
|
||||
@ -184,12 +181,6 @@ public class ConfigPackImpl implements ConfigPack {
|
||||
toWorldConfig(new TerraWorldImpl(new DummyWorld(), this, main)); // Build now to catch any errors immediately.
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void applyLoaders(TypeRegistry registry) {
|
||||
loaders.forEach(registry::registerLoader);
|
||||
objectLoaders.forEach((t, l) -> registry.registerLoader(t, (TemplateProvider<ObjectTemplate<Object>>) ((Object) l)));
|
||||
}
|
||||
|
||||
private Map<Class<?>, ImmutablePair<OpenRegistry<?>, CheckedRegistry<?>>> newRegistryMap() {
|
||||
Map<Class<?>, ImmutablePair<OpenRegistry<?>, CheckedRegistry<?>>> map = new HashMap<Class<?>, ImmutablePair<OpenRegistry<?>, CheckedRegistry<?>>>() {
|
||||
@Serial
|
||||
@ -219,14 +210,15 @@ public class ConfigPackImpl implements ConfigPack {
|
||||
}
|
||||
@Override
|
||||
public <T> ConfigPackImpl applyLoader(Type type, TypeLoader<T> loader) {
|
||||
loaders.put(type, loader);
|
||||
abstractConfigLoader.registerLoader(type, loader);
|
||||
selfLoader.registerLoader(type, loader);
|
||||
return this;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public <T> ConfigPackImpl applyLoader(Type type, TemplateProvider<ObjectTemplate<T>> loader) {
|
||||
objectLoaders.put(type, (TemplateProvider<ObjectTemplate<?>>) ((Object) loader));
|
||||
abstractConfigLoader.registerLoader(type, loader);
|
||||
selfLoader.registerLoader(type, loader);
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -236,8 +228,6 @@ public class ConfigPackImpl implements ConfigPack {
|
||||
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
private void load(long start, TerraPlugin main) throws ConfigException {
|
||||
applyLoaders(abstractConfigLoader);
|
||||
applyLoaders(selfLoader);
|
||||
configTypes.values().forEach(list -> list.forEach(pair -> configTypeRegistry.register(pair.getLeft(), pair.getRight())));
|
||||
|
||||
for(Map.Entry<String, Double> var : template.getVariables().entrySet()) {
|
||||
|
@ -16,9 +16,6 @@ public class ConfigPackTemplate implements ConfigTemplate {
|
||||
@Value("id")
|
||||
private String id;
|
||||
|
||||
@Value("noise")
|
||||
private Map<String, NoiseSeeded> noiseBuilderMap;
|
||||
|
||||
@Value("addons")
|
||||
@Default
|
||||
private Set<TerraAddon> addons = new HashSet<>();
|
||||
@ -139,10 +136,6 @@ public class ConfigPackTemplate implements ConfigTemplate {
|
||||
return vanillaStructures;
|
||||
}
|
||||
|
||||
public Map<String, NoiseSeeded> getNoiseBuilderMap() {
|
||||
return noiseBuilderMap;
|
||||
}
|
||||
|
||||
public Map<String, Double> getVariables() {
|
||||
return variables;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user