implement TerraScriptCheckFunctionAddon

This commit is contained in:
dfsek
2021-12-02 18:04:16 -07:00
parent b222129478
commit ad66fa8022
7 changed files with 49 additions and 22 deletions

View File

@@ -0,0 +1,91 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra Core Addons are licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in this module's root directory.
*/
package com.dfsek.terra.addon.terrascript.check;
import com.dfsek.terra.api.world.WritableWorld;
import net.jafama.FastMath;
import java.util.Map;
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.Platform;
import com.dfsek.terra.api.util.RotationUtil;
import com.dfsek.terra.api.util.vector.Vector2;
import com.dfsek.terra.api.util.vector.Vector3;
import com.dfsek.terra.api.world.chunk.generation.util.math.SamplerProvider;
public class CheckFunction implements Function<String> {
private final Returnable<Number> x, y, z;
private final Position position;
public CheckFunction(Returnable<Number> x, Returnable<Number> y, Returnable<Number> z, Position position) {
this.x = x;
this.y = y;
this.z = z;
this.position = position;
}
@Override
public String apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
Vector2 xz = new Vector2(x.apply(implementationArguments, variableMap).doubleValue(),
z.apply(implementationArguments, variableMap).doubleValue());
RotationUtil.rotateVector(xz, arguments.getRotation());
Vector3 location = arguments.getBuffer().getOrigin().clone().add(
new Vector3(FastMath.roundToInt(xz.getX()), y.apply(implementationArguments, variableMap).doubleValue(),
FastMath.roundToInt(xz.getZ())));
return apply(location, arguments.getWorld());
}
@Override
public Position getPosition() {
return position;
}
@Override
public ReturnType returnType() {
return ReturnType.STRING;
}
private String apply(Vector3 vector, WritableWorld world) {
int y = vector.getBlockY();
if(y >= world.getMaxHeight() || y < 0) return "AIR";
SamplerProvider cache = world.getConfig().getSamplerCache();
double comp = sample(vector.getBlockX(), vector.getBlockY(), vector.getBlockZ(), cache);
if(comp > 0) return "LAND"; // If noise val is greater than zero, location will always be land.
//BiomeProvider provider = tw.getBiomeProvider();
//TerraBiome b = provider.getBiome(vector.getBlockX(), vector.getBlockZ());
/*if(vector.getY() > c.getSeaLevel())*/
return "AIR"; // Above sea level
//return "OCEAN"; // Below sea level
}
private double sample(int x, int y, int z, SamplerProvider cache) {
int cx = FastMath.floorDiv(x, 16);
int cz = FastMath.floorDiv(z, 16);
return cache.get(x, z).sample(x - (cx << 4), y, z - (cz << 4));
}
}

View File

@@ -0,0 +1,44 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra Core Addons are licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in this module's root directory.
*/
package com.dfsek.terra.addon.terrascript.check;
import java.util.List;
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
import com.dfsek.terra.addons.terrascript.parser.lang.functions.FunctionBuilder;
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
import com.dfsek.terra.api.Platform;
public class CheckFunctionBuilder implements FunctionBuilder<CheckFunction> {
private final Platform platform;
public CheckFunctionBuilder(Platform platform) {
this.platform = platform;
}
@SuppressWarnings("unchecked")
@Override
public CheckFunction build(List<Returnable<?>> argumentList, Position position) {
return new CheckFunction((Returnable<Number>) argumentList.get(0), (Returnable<Number>) argumentList.get(1),
(Returnable<Number>) argumentList.get(2), position);
}
@Override
public int argNumber() {
return 3;
}
@Override
public Returnable.ReturnType getArgument(int position) {
return switch(position) {
case 0, 1, 2 -> Returnable.ReturnType.NUMBER;
default -> null;
};
}
}

View File

@@ -0,0 +1,30 @@
package com.dfsek.terra.addon.terrascript.check;
import com.dfsek.terra.addons.manifest.api.AddonInitializer;
import com.dfsek.terra.addons.terrascript.parser.lang.functions.FunctionBuilder;
import com.dfsek.terra.api.Platform;
import com.dfsek.terra.api.addon.BaseAddon;
import com.dfsek.terra.api.event.events.config.pack.ConfigPackPreLoadEvent;
import com.dfsek.terra.api.event.functional.FunctionalEventHandler;
import com.dfsek.terra.api.inject.annotations.Inject;
public class TerraScriptCheckFunctionAddon implements AddonInitializer {
@Inject
private Platform platform;
@Inject
private BaseAddon addon;
@Override
public void initialize() {
platform.getEventManager()
.getHandler(FunctionalEventHandler.class)
.register(addon, ConfigPackPreLoadEvent.class)
.priority(1)
.then(event -> {
event.getPack().getOrCreateRegistry(FunctionBuilder.class).register("check", new CheckFunctionBuilder(platform));
})
.failThrough();
}
}

View File

@@ -4,7 +4,7 @@ contributors:
id: structure-terrascript-loader
version: @VERSION@
entrypoints:
- "com.dfsek.terra.addons.terrascript.TerraScriptAddon"
- "com.dfsek.terra.addon.terrascript.check.TerraScriptCheckFunctionAddon"
website:
issues: https://github.com/PolyhedralDev/Terra-structure-terrascript-loader/issues
source: https://github.com/PolyhedralDev/Terra-structure-terrascript-loader