remove buffer api

This commit is contained in:
dfsek
2021-12-20 00:01:04 -07:00
parent f088928483
commit 62d0f109b4
24 changed files with 217 additions and 185 deletions
@@ -7,6 +7,8 @@
package com.dfsek.terra.addons.flora.flora.gen; package com.dfsek.terra.addons.flora.flora.gen;
import com.dfsek.terra.api.util.vector.integer.Vector3Int;
import net.jafama.FastMath; import net.jafama.FastMath;
import java.util.ArrayList; import java.util.ArrayList;
@@ -54,9 +56,9 @@ public class TerraFlora implements Structure {
}); });
} }
private void test(EnumSet<Direction> faces, Direction f, Vector3 b, WritableWorld world) { private void test(EnumSet<Direction> faces, Direction f, Vector3Int b, WritableWorld world) {
if(testRotation.contains( if(testRotation.contains(
world.getBlockState(b.getBlockX() + f.getModX(), b.getBlockY() + f.getModY(), b.getBlockZ() + f.getModZ()).getBlockType())) world.getBlockState(b.getX() + f.getModX(), b.getY() + f.getModY(), b.getZ() + f.getModZ()).getBlockType()))
faces.add(f); faces.add(f);
} }
@@ -64,7 +66,7 @@ public class TerraFlora implements Structure {
return layers.get(FastMath.max(FastMath.min(layer, layers.size() - 1), 0)); return layers.get(FastMath.max(FastMath.min(layer, layers.size() - 1), 0));
} }
private EnumSet<Direction> getFaces(Vector3 b, WritableWorld world) { private EnumSet<Direction> getFaces(Vector3Int b, WritableWorld world) {
EnumSet<Direction> faces = EnumSet.noneOf(Direction.class); EnumSet<Direction> faces = EnumSet.noneOf(Direction.class);
test(faces, Direction.NORTH, b, world); test(faces, Direction.NORTH, b, world);
test(faces, Direction.SOUTH, b, world); test(faces, Direction.SOUTH, b, world);
@@ -79,12 +81,12 @@ public class TerraFlora implements Structure {
} }
@Override @Override
public boolean generate(Vector3 location, WritableWorld world, Random random, Rotation rotation) { public boolean generate(Vector3Int location, WritableWorld world, Random random, Rotation rotation) {
boolean doRotation = testRotation.size() > 0; boolean doRotation = testRotation.size() > 0;
int size = layers.size(); int size = layers.size();
int c = ceiling ? -1 : 1; int c = ceiling ? -1 : 1;
EnumSet<Direction> faces = doRotation ? getFaces(location.clone().add(0, c, 0), world) : EnumSet.noneOf(Direction.class); EnumSet<Direction> faces = doRotation ? getFaces(location.mutable().add(0, c, 0).immutable(), world) : EnumSet.noneOf(Direction.class);
if(doRotation && faces.size() == 0) return false; // Don't plant if no faces are valid. if(doRotation && faces.size() == 0) return false; // Don't plant if no faces are valid.
for(int i = 0; FastMath.abs(i) < size; i += c) { // Down if ceiling, up if floor for(int i = 0; FastMath.abs(i) < size; i += c) { // Down if ceiling, up if floor
@@ -93,7 +95,7 @@ public class TerraFlora implements Structure {
location.getZ(), world.getSeed()).clone(); location.getZ(), world.getSeed()).clone();
if(doRotation) { if(doRotation) {
Direction oneFace = new ArrayList<>(faces).get( Direction oneFace = new ArrayList<>(faces).get(
new Random(location.getBlockX() ^ location.getBlockZ()).nextInt(faces.size())); // Get random face. new Random(location.getX() ^ location.getZ()).nextInt(faces.size())); // Get random face.
data.setIfPresent(Properties.DIRECTION, oneFace.opposite()) data.setIfPresent(Properties.DIRECTION, oneFace.opposite())
.setIfPresent(Properties.NORTH, faces.contains(Direction.NORTH)) .setIfPresent(Properties.NORTH, faces.contains(Direction.NORTH))
@@ -101,7 +103,7 @@ public class TerraFlora implements Structure {
.setIfPresent(Properties.EAST, faces.contains(Direction.EAST)) .setIfPresent(Properties.EAST, faces.contains(Direction.EAST))
.setIfPresent(Properties.WEST, faces.contains(Direction.WEST)); .setIfPresent(Properties.WEST, faces.contains(Direction.WEST));
} }
world.setBlockState(location.clone().add(0, i + c, 0), data, physics); world.setBlockState(location.mutable().add(0, i + c, 0).immutable(), data, physics);
} }
return true; return true;
} }
@@ -7,6 +7,8 @@
package com.dfsek.terra.addons.ore.ores; package com.dfsek.terra.addons.ore.ores;
import com.dfsek.terra.api.util.vector.integer.Vector3Int;
import net.jafama.FastMath; import net.jafama.FastMath;
import java.util.Map; import java.util.Map;
@@ -40,20 +42,10 @@ public class VanillaOre implements Structure {
} }
@Override @Override
public boolean generate(Vector3 location, WritableWorld world, Random random, Rotation rotation) { public boolean generate(Vector3Int location, WritableWorld world, Random random, Rotation rotation) {
generate(location, world, random); int centerX = location.getX();
return true; int centerZ = location.getZ();
} int centerY = location.getY();
@Override
public String getID() {
return null;
}
public void generate(Vector3 location, WritableWorld world, Random random) {
int centerX = location.getBlockX();
int centerZ = location.getBlockZ();
int centerY = location.getBlockY();
float f = random.nextFloat() * (float) Math.PI; float f = random.nextFloat() * (float) Math.PI;
@@ -101,6 +93,12 @@ public class VanillaOre implements Structure {
} }
} }
} }
return true;
}
@Override
public String getID() {
return null;
} }
public BlockState getMaterial(BlockType replace) { public BlockState getMaterial(BlockType replace) {
@@ -17,6 +17,7 @@ import com.dfsek.terra.api.util.PopulationUtil;
import com.dfsek.terra.api.util.Rotation; import com.dfsek.terra.api.util.Rotation;
import com.dfsek.terra.api.util.StringIdentifiable; import com.dfsek.terra.api.util.StringIdentifiable;
import com.dfsek.terra.api.util.vector.Vector3; import com.dfsek.terra.api.util.vector.Vector3;
import com.dfsek.terra.api.util.vector.integer.Vector3Int;
import com.dfsek.terra.api.world.chunk.generation.ProtoWorld; import com.dfsek.terra.api.world.chunk.generation.ProtoWorld;
import com.dfsek.terra.api.world.chunk.generation.stage.GenerationStage; import com.dfsek.terra.api.world.chunk.generation.stage.GenerationStage;
@@ -56,7 +57,7 @@ public class FeatureGenerationStage implements GenerationStage, StringIdentifiab
.getSuitableCoordinates(column) .getSuitableCoordinates(column)
.forEach(y -> .forEach(y ->
feature.getStructure(world, tx, y, tz) feature.getStructure(world, tx, y, tz)
.generate(new Vector3(tx, y, tz), .generate(Vector3Int.of(tx, y, tz),
world, world,
new Random(PopulationUtil.getCarverChunkSeed( new Random(PopulationUtil.getCarverChunkSeed(
world.centerChunkX(), world.centerChunkX(),
@@ -3,7 +3,7 @@ package com.dfsek.terra.addons.palette.shortcut.block;
import com.dfsek.terra.api.block.state.BlockState; import com.dfsek.terra.api.block.state.BlockState;
import com.dfsek.terra.api.structure.Structure; import com.dfsek.terra.api.structure.Structure;
import com.dfsek.terra.api.util.Rotation; import com.dfsek.terra.api.util.Rotation;
import com.dfsek.terra.api.util.vector.Vector3; import com.dfsek.terra.api.util.vector.integer.Vector3Int;
import com.dfsek.terra.api.world.WritableWorld; import com.dfsek.terra.api.world.WritableWorld;
import java.util.Random; import java.util.Random;
@@ -17,7 +17,7 @@ public class SingletonStructure implements Structure {
} }
@Override @Override
public boolean generate(Vector3 location, WritableWorld world, Random random, Rotation rotation) { public boolean generate(Vector3Int location, WritableWorld world, Random random, Rotation rotation) {
world.setBlockState(location, blockState); world.setBlockState(location, blockState);
return true; return true;
} }
@@ -9,12 +9,11 @@ package com.dfsek.terra.addons.sponge;
import java.util.Random; import java.util.Random;
import com.dfsek.terra.api.Platform;
import com.dfsek.terra.api.block.state.BlockState; import com.dfsek.terra.api.block.state.BlockState;
import com.dfsek.terra.api.structure.Structure; import com.dfsek.terra.api.structure.Structure;
import com.dfsek.terra.api.util.Rotation; import com.dfsek.terra.api.util.Rotation;
import com.dfsek.terra.api.util.vector.Vector3;
import com.dfsek.terra.api.util.vector.integer.Vector2Int; import com.dfsek.terra.api.util.vector.integer.Vector2Int;
import com.dfsek.terra.api.util.vector.integer.Vector3Int;
import com.dfsek.terra.api.world.WritableWorld; import com.dfsek.terra.api.world.WritableWorld;
@@ -30,10 +29,10 @@ public class SpongeStructure implements Structure {
} }
@Override @Override
public boolean generate(Vector3 location, WritableWorld world, Random random, Rotation rotation) { public boolean generate(Vector3Int location, WritableWorld world, Random random, Rotation rotation) {
int bX = location.getBlockX(); int bX = location.getX();
int bY = location.getBlockY(); int bY = location.getY();
int bZ = location.getBlockZ(); int bZ = location.getZ();
for(int x = 0; x < blocks.length; x++) { for(int x = 0; x < blocks.length; x++) {
for(int z = 0; z < blocks[x].length; z++) { for(int z = 0; z < blocks[x].length; z++) {
Vector2Int r = Vector2Int.of(x, z).rotate(rotation); Vector2Int r = Vector2Int.of(x, z).rotate(rotation);
@@ -1,58 +0,0 @@
/*
* 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.addons.terrascript.buffer.items;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Random;
import com.dfsek.terra.addons.terrascript.script.StructureScript;
import com.dfsek.terra.api.Platform;
import com.dfsek.terra.api.block.entity.BlockEntity;
import com.dfsek.terra.api.block.entity.Container;
import com.dfsek.terra.api.event.events.world.generation.LootPopulateEvent;
import com.dfsek.terra.api.structure.LootTable;
import com.dfsek.terra.api.structure.buffer.BufferedItem;
import com.dfsek.terra.api.util.vector.Vector3;
import com.dfsek.terra.api.world.WritableWorld;
public class BufferedLootApplication implements BufferedItem {
private static final Logger LOGGER = LoggerFactory.getLogger(BufferedLootApplication.class);
private final LootTable table;
private final Platform platform;
private final StructureScript structure;
public BufferedLootApplication(LootTable table, Platform platform, StructureScript structure) {
this.table = table;
this.platform = platform;
this.structure = structure;
}
@Override
public void paste(Vector3 origin, WritableWorld world) {
try {
BlockEntity data = world.getBlockEntity(origin);
if(!(data instanceof Container container)) {
LOGGER.error("Failed to place loot at {}; block {} is not a container", origin, data);
return;
}
LootPopulateEvent event = new LootPopulateEvent(container, table, world.getPack(), structure);
platform.getEventManager().callEvent(event);
if(event.isCancelled()) return;
event.getTable().fillInventory(container.getInventory(), new Random(origin.hashCode()));
data.update(false);
} catch(Exception e) {
LOGGER.error("Could not apply loot at {}", origin, e);
e.printStackTrace();
}
}
}
@@ -7,8 +7,8 @@
package com.dfsek.terra.addons.terrascript.script; package com.dfsek.terra.addons.terrascript.script;
import com.google.common.cache.Cache; import com.dfsek.terra.api.util.vector.integer.Vector3Int;
import com.google.common.cache.CacheBuilder;
import net.jafama.FastMath; import net.jafama.FastMath;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
@@ -18,7 +18,6 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.Random; import java.util.Random;
import java.util.concurrent.ExecutionException;
import com.dfsek.terra.addons.terrascript.parser.Parser; import com.dfsek.terra.addons.terrascript.parser.Parser;
import com.dfsek.terra.addons.terrascript.parser.lang.Block; import com.dfsek.terra.addons.terrascript.parser.lang.Block;
@@ -46,10 +45,7 @@ import com.dfsek.terra.api.profiler.ProfileFrame;
import com.dfsek.terra.api.registry.Registry; import com.dfsek.terra.api.registry.Registry;
import com.dfsek.terra.api.structure.LootTable; import com.dfsek.terra.api.structure.LootTable;
import com.dfsek.terra.api.structure.Structure; import com.dfsek.terra.api.structure.Structure;
import com.dfsek.terra.api.structure.buffer.buffers.DirectBuffer;
import com.dfsek.terra.api.structure.buffer.buffers.StructureBuffer;
import com.dfsek.terra.api.util.Rotation; import com.dfsek.terra.api.util.Rotation;
import com.dfsek.terra.api.util.vector.Vector3;
import com.dfsek.terra.api.world.WritableWorld; import com.dfsek.terra.api.world.WritableWorld;
@@ -57,7 +53,6 @@ public class StructureScript implements Structure {
private static final Logger LOGGER = LoggerFactory.getLogger(StructureScript.class); private static final Logger LOGGER = LoggerFactory.getLogger(StructureScript.class);
private final Block block; private final Block block;
private final String id; private final String id;
private final Cache<Vector3, StructureBuffer> cache;
private final Platform platform; private final Platform platform;
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
@@ -89,11 +84,11 @@ public class StructureScript implements Structure {
.registerFunction("getBlock", new CheckBlockFunctionBuilder()) .registerFunction("getBlock", new CheckBlockFunctionBuilder())
.registerFunction("state", new StateFunctionBuilder(platform)) .registerFunction("state", new StateFunctionBuilder(platform))
.registerFunction("setWaterlog", new UnaryBooleanFunctionBuilder((waterlog, args) -> args.setWaterlog(waterlog))) .registerFunction("setWaterlog", new UnaryBooleanFunctionBuilder((waterlog, args) -> args.setWaterlog(waterlog)))
.registerFunction("originX", new ZeroArgFunctionBuilder<Number>(arguments -> arguments.getBuffer().getOrigin().getX(), .registerFunction("originX", new ZeroArgFunctionBuilder<Number>(arguments -> arguments.getOrigin().getX(),
Returnable.ReturnType.NUMBER)) Returnable.ReturnType.NUMBER))
.registerFunction("originY", new ZeroArgFunctionBuilder<Number>(arguments -> arguments.getBuffer().getOrigin().getY(), .registerFunction("originY", new ZeroArgFunctionBuilder<Number>(arguments -> arguments.getOrigin().getY(),
Returnable.ReturnType.NUMBER)) Returnable.ReturnType.NUMBER))
.registerFunction("originZ", new ZeroArgFunctionBuilder<Number>(arguments -> arguments.getBuffer().getOrigin().getZ(), .registerFunction("originZ", new ZeroArgFunctionBuilder<Number>(arguments -> arguments.getOrigin().getZ(),
Returnable.ReturnType.NUMBER)) Returnable.ReturnType.NUMBER))
.registerFunction("rotation", new ZeroArgFunctionBuilder<>(arguments -> arguments.getRotation().toString(), .registerFunction("rotation", new ZeroArgFunctionBuilder<>(arguments -> arguments.getRotation().toString(),
Returnable.ReturnType.STRING)) Returnable.ReturnType.STRING))
@@ -126,34 +121,19 @@ public class StructureScript implements Structure {
block = parser.parse(); block = parser.parse();
this.platform = platform; this.platform = platform;
this.cache = CacheBuilder.newBuilder().maximumSize(platform.getTerraConfig().getStructureCache()).build();
} }
@Override @Override
@SuppressWarnings("try") @SuppressWarnings("try")
public boolean generate(Vector3 location, WritableWorld world, Random random, Rotation rotation) { public boolean generate(Vector3Int location, WritableWorld world, Random random, Rotation rotation) {
try(ProfileFrame ignore = platform.getProfiler().profile("terrascript_direct:" + id)) { try(ProfileFrame ignore = platform.getProfiler().profile("terrascript_direct:" + id)) {
DirectBuffer buffer = new DirectBuffer(location, world); return applyBlock(new TerraImplementationArguments(location, rotation, random, world, 0));
return applyBlock(new TerraImplementationArguments(buffer, rotation, random, world, 0));
} }
} }
public boolean generate(Vector3 location, WritableWorld world, Random random, Rotation rotation, int recursions) { public boolean generate(Vector3Int location, WritableWorld world, Random random, Rotation rotation, int recursions) {
try(ProfileFrame ignore = platform.getProfiler().profile("terrascript_direct:" + id)) { try(ProfileFrame ignore = platform.getProfiler().profile("terrascript_direct:" + id)) {
DirectBuffer buffer = new DirectBuffer(location, world); return applyBlock(new TerraImplementationArguments(location, rotation, random, world, recursions));
return applyBlock(new TerraImplementationArguments(buffer, rotation, random, world, recursions));
}
}
private StructureBuffer computeBuffer(Vector3 location, WritableWorld world, Random random, Rotation rotation) {
try {
return cache.get(location, () -> {
StructureBuffer buf = new StructureBuffer(location);
buf.setSucceeded(applyBlock(new TerraImplementationArguments(buf, rotation, random, world, 0)));
return buf;
});
} catch(ExecutionException e) {
throw new RuntimeException(e);
} }
} }
@@ -161,7 +141,7 @@ public class StructureScript implements Structure {
try { try {
return block.apply(arguments).getLevel() != Block.ReturnLevel.FAIL; return block.apply(arguments).getLevel() != Block.ReturnLevel.FAIL;
} catch(RuntimeException e) { } catch(RuntimeException e) {
LOGGER.error("Failed to generate structure at {}", arguments.getBuffer().getOrigin(), e); LOGGER.error("Failed to generate structure at {}", arguments.getOrigin(), e);
return false; return false;
} }
} }
@@ -7,32 +7,32 @@
package com.dfsek.terra.addons.terrascript.script; package com.dfsek.terra.addons.terrascript.script;
import java.util.HashMap;
import java.util.Map;
import java.util.Random; import java.util.Random;
import com.dfsek.terra.addons.terrascript.parser.lang.ImplementationArguments; import com.dfsek.terra.addons.terrascript.parser.lang.ImplementationArguments;
import com.dfsek.terra.api.structure.buffer.Buffer;
import com.dfsek.terra.api.util.Rotation; import com.dfsek.terra.api.util.Rotation;
import com.dfsek.terra.api.util.vector.Vector3;
import com.dfsek.terra.api.util.vector.integer.Vector3Int;
import com.dfsek.terra.api.world.WritableWorld; import com.dfsek.terra.api.world.WritableWorld;
public class TerraImplementationArguments implements ImplementationArguments { public class TerraImplementationArguments implements ImplementationArguments {
private final Buffer buffer;
private final Rotation rotation; private final Rotation rotation;
private final Random random; private final Random random;
private final WritableWorld world; private final WritableWorld world;
private final Map<Vector3, String> marks = new HashMap<>();
private final int recursions; private final int recursions;
private boolean waterlog = false; private boolean waterlog = false;
private final Vector3Int origin;
public TerraImplementationArguments(Buffer buffer, Rotation rotation, Random random, WritableWorld world, int recursions) { public TerraImplementationArguments(Vector3Int origin, Rotation rotation, Random random, WritableWorld world, int recursions) {
this.buffer = buffer;
this.rotation = rotation; this.rotation = rotation;
this.random = random; this.random = random;
this.world = world; this.world = world;
this.recursions = recursions; this.recursions = recursions;
} this.origin = origin;
public Buffer getBuffer() {
return buffer;
} }
public int getRecursions() { public int getRecursions() {
@@ -58,4 +58,16 @@ public class TerraImplementationArguments implements ImplementationArguments {
public WritableWorld getWorld() { public WritableWorld getWorld() {
return world; return world;
} }
public Vector3Int getOrigin() {
return origin;
}
public void setMark(Vector3 pos, String mark) {
marks.put(pos, mark);
}
public String getMark(Vector3 pos) {
return marks.get(pos);
}
} }
@@ -29,7 +29,7 @@ public class StateFunctionBuilder implements FunctionBuilder<StateFunction> {
public StateFunction build(List<Returnable<?>> argumentList, Position position) { public StateFunction build(List<Returnable<?>> argumentList, Position position) {
if(argumentList.size() < 4) throw new ParseException("Expected data", position); if(argumentList.size() < 4) throw new ParseException("Expected data", position);
return new StateFunction((Returnable<Number>) argumentList.get(0), (Returnable<Number>) argumentList.get(1), return new StateFunction((Returnable<Number>) argumentList.get(0), (Returnable<Number>) argumentList.get(1),
(Returnable<Number>) argumentList.get(2), (Returnable<String>) argumentList.get(3), platform, position); (Returnable<Number>) argumentList.get(2), (Returnable<String>) argumentList.get(3), position);
} }
@Override @Override
@@ -50,10 +50,9 @@ public class BiomeFunction implements Function<String> {
BiomeProvider grid = arguments.getWorld().getBiomeProvider(); BiomeProvider grid = arguments.getWorld().getBiomeProvider();
return grid.getBiome(arguments.getBuffer() return grid.getBiome(arguments.getOrigin()
.getOrigin() .toVector3()
.clone() .add(Vector3.of(FastMath.roundToInt(xz.getX()),
.add(new Vector3(FastMath.roundToInt(xz.getX()),
y.apply(implementationArguments, variableMap).intValue(), y.apply(implementationArguments, variableMap).intValue(),
FastMath.roundToInt(xz.getZ()))), arguments.getWorld().getSeed()).getID(); FastMath.roundToInt(xz.getZ()))), arguments.getWorld().getSeed()).getID();
} }
@@ -7,6 +7,8 @@
package com.dfsek.terra.addons.terrascript.script.functions; package com.dfsek.terra.addons.terrascript.script.functions;
import com.dfsek.terra.api.block.state.properties.base.Properties;
import net.jafama.FastMath; import net.jafama.FastMath;
import java.util.HashMap; import java.util.HashMap;
@@ -26,8 +28,12 @@ import com.dfsek.terra.api.util.RotationUtil;
import com.dfsek.terra.api.util.vector.Vector2; import com.dfsek.terra.api.util.vector.Vector2;
import com.dfsek.terra.api.util.vector.Vector3; import com.dfsek.terra.api.util.vector.Vector3;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class BlockFunction implements Function<Void> { public class BlockFunction implements Function<Void> {
private static final Logger logger = LoggerFactory.getLogger(BlockFunction.class);
protected final Returnable<Number> x, y, z; protected final Returnable<Number> x, y, z;
protected final Returnable<String> blockData; protected final Returnable<String> blockData;
protected final Platform platform; protected final Platform platform;
@@ -72,10 +78,20 @@ public class BlockFunction implements Function<Void> {
RotationUtil.rotateVector(xz, arguments.getRotation()); RotationUtil.rotateVector(xz, arguments.getRotation());
RotationUtil.rotateBlockData(rot, arguments.getRotation().inverse()); RotationUtil.rotateBlockData(rot, arguments.getRotation().inverse());
arguments.getBuffer().addItem( try {
new BufferedBlock(rot, overwrite.apply(implementationArguments, variableMap), platform, arguments.isWaterlog()), Vector3 set = Vector3.of(FastMath.roundToInt(xz.getX()),
new Vector3(FastMath.roundToInt(xz.getX()), y.apply(implementationArguments, variableMap).doubleValue(), y.apply(implementationArguments, variableMap).doubleValue(),
FastMath.roundToInt(xz.getZ()))); FastMath.roundToInt(xz.getZ())).add(arguments.getOrigin());
BlockState current = arguments.getWorld().getBlockState(set);
if(overwrite.apply(implementationArguments, variableMap) || current.isAir()) {
if(arguments.isWaterlog() && current.has(Properties.WATERLOGGED) && current.getBlockType().isWater()) {
current.set(Properties.WATERLOGGED, true);
}
arguments.getWorld().setBlockState(set, rot);
}
} catch(RuntimeException e) {
logger.error("Failed to place block at location {}", arguments.getOrigin(), e);
}
} }
protected BlockState getBlockState(ImplementationArguments arguments, Map<String, Variable<?>> variableMap) { protected BlockState getBlockState(ImplementationArguments arguments, Map<String, Variable<?>> variableMap) {
@@ -44,12 +44,11 @@ public class CheckBlockFunction implements Function<String> {
RotationUtil.rotateVector(xz, arguments.getRotation()); RotationUtil.rotateVector(xz, arguments.getRotation());
String data = arguments.getWorld() String data = arguments.getWorld()
.getBlockState(arguments.getBuffer() .getBlockState(arguments.getOrigin()
.getOrigin() .toVector3()
.clone() .add(Vector3.of(FastMath.roundToInt(xz.getX()),
.add(new Vector3(FastMath.roundToInt(xz.getX()), y.apply(implementationArguments, variableMap)
y.apply(implementationArguments, variableMap) .doubleValue(), FastMath.roundToInt(xz.getZ()))))
.doubleValue(), FastMath.roundToInt(xz.getZ()))))
.getAsString(); .getAsString();
if(data.contains("[")) return data.substring(0, data.indexOf('[')); // Strip properties if(data.contains("[")) return data.substring(0, data.indexOf('[')); // Strip properties
else return data; else return data;
@@ -19,7 +19,9 @@ import com.dfsek.terra.addons.terrascript.parser.lang.variables.Variable;
import com.dfsek.terra.addons.terrascript.script.TerraImplementationArguments; import com.dfsek.terra.addons.terrascript.script.TerraImplementationArguments;
import com.dfsek.terra.addons.terrascript.tokenizer.Position; import com.dfsek.terra.addons.terrascript.tokenizer.Position;
import com.dfsek.terra.api.Platform; import com.dfsek.terra.api.Platform;
import com.dfsek.terra.api.entity.Entity;
import com.dfsek.terra.api.entity.EntityType; import com.dfsek.terra.api.entity.EntityType;
import com.dfsek.terra.api.event.events.world.generation.EntitySpawnEvent;
import com.dfsek.terra.api.util.RotationUtil; import com.dfsek.terra.api.util.RotationUtil;
import com.dfsek.terra.api.util.vector.Vector2; import com.dfsek.terra.api.util.vector.Vector2;
import com.dfsek.terra.api.util.vector.Vector3; import com.dfsek.terra.api.util.vector.Vector3;
@@ -50,9 +52,8 @@ public class EntityFunction implements Function<Void> {
z.apply(implementationArguments, variableMap).doubleValue()); z.apply(implementationArguments, variableMap).doubleValue());
RotationUtil.rotateVector(xz, arguments.getRotation()); RotationUtil.rotateVector(xz, arguments.getRotation());
Entity entity = arguments.getWorld().spawnEntity(Vector3.of(xz.getX(), y.apply(implementationArguments, variableMap).doubleValue(), xz.getZ()).add(arguments.getOrigin()).add(0.5, 0, 0.5), data);
arguments.getBuffer().addItem(new BufferedEntity(data, platform), platform.getEventManager().callEvent(new EntitySpawnEvent(entity.world().getPack(), entity));
new Vector3(xz.getX(), y.apply(implementationArguments, variableMap).doubleValue(), xz.getZ()));
return null; return null;
} }
@@ -40,8 +40,8 @@ public class GetMarkFunction implements Function<String> {
z.apply(implementationArguments, variableMap).doubleValue()); z.apply(implementationArguments, variableMap).doubleValue());
RotationUtil.rotateVector(xz, arguments.getRotation()); RotationUtil.rotateVector(xz, arguments.getRotation());
String mark = arguments.getBuffer().getMark(new Vector3(FastMath.floorToInt(xz.getX()), FastMath.floorToInt( String mark = arguments.getMark(Vector3.of(FastMath.floorToInt(xz.getX()), FastMath.floorToInt(
y.apply(implementationArguments, variableMap).doubleValue()), FastMath.floorToInt(xz.getZ()))); y.apply(implementationArguments, variableMap).doubleValue()), FastMath.floorToInt(xz.getZ())).add(arguments.getOrigin()));
return mark == null ? "" : mark; return mark == null ? "" : mark;
} }
@@ -7,13 +7,19 @@
package com.dfsek.terra.addons.terrascript.script.functions; package com.dfsek.terra.addons.terrascript.script.functions;
import com.dfsek.terra.api.block.entity.BlockEntity;
import com.dfsek.terra.api.block.entity.Container;
import com.dfsek.terra.api.event.events.world.generation.LootPopulateEvent;
import com.dfsek.terra.api.util.vector.Vector3;
import net.jafama.FastMath; import net.jafama.FastMath;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.Map; import java.util.Map;
import java.util.Random;
import com.dfsek.terra.addons.terrascript.buffer.items.BufferedLootApplication;
import com.dfsek.terra.addons.terrascript.parser.lang.ImplementationArguments; 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.Returnable;
import com.dfsek.terra.addons.terrascript.parser.lang.functions.Function; import com.dfsek.terra.addons.terrascript.parser.lang.functions.Function;
@@ -26,7 +32,6 @@ import com.dfsek.terra.api.registry.Registry;
import com.dfsek.terra.api.structure.LootTable; import com.dfsek.terra.api.structure.LootTable;
import com.dfsek.terra.api.util.RotationUtil; import com.dfsek.terra.api.util.RotationUtil;
import com.dfsek.terra.api.util.vector.Vector2; import com.dfsek.terra.api.util.vector.Vector2;
import com.dfsek.terra.api.util.vector.Vector3;
public class LootFunction implements Function<Void> { public class LootFunction implements Function<Void> {
@@ -59,12 +64,37 @@ public class LootFunction implements Function<Void> {
RotationUtil.rotateVector(xz, arguments.getRotation()); RotationUtil.rotateVector(xz, arguments.getRotation());
String id = data.apply(implementationArguments, variableMap); String id = data.apply(implementationArguments, variableMap);
registry.get(id).ifPresentOrElse(table -> arguments.getBuffer().addItem(new BufferedLootApplication(table, platform, script),
new Vector3(FastMath.roundToInt(xz.getX()),
y.apply(implementationArguments, variableMap) registry.get(id)
.intValue(), .ifPresentOrElse(table -> {
FastMath.roundToInt(xz.getZ()))), Vector3 apply = Vector3.of(FastMath.roundToInt(xz.getX()),
() -> LOGGER.error("No such loot table {}", id)); y.apply(implementationArguments, variableMap)
.intValue(),
FastMath.roundToInt(xz.getZ())).add(arguments.getOrigin());
try {
BlockEntity data = arguments.getWorld().getBlockEntity(apply);
if(!(data instanceof Container container)) {
LOGGER.error("Failed to place loot at {}; block {} is not a container",
apply, data);
return;
}
LootPopulateEvent event = new LootPopulateEvent(container, table,
arguments.getWorld().getPack(), script);
platform.getEventManager().callEvent(event);
if(event.isCancelled()) return;
event.getTable().fillInventory(container.getInventory(),
new Random(apply.hashCode()));
data.update(false);
} catch(Exception e) {
LOGGER.error("Could not apply loot at {}", apply, e);
e.printStackTrace();
}
},
() -> LOGGER.error("No such loot table {}", id));
return null; return null;
} }
@@ -52,9 +52,16 @@ public class PullFunction implements Function<Void> {
RotationUtil.rotateVector(xz, arguments.getRotation()); RotationUtil.rotateVector(xz, arguments.getRotation());
BlockState rot = data.clone(); BlockState rot = data.clone();
RotationUtil.rotateBlockData(rot, arguments.getRotation().inverse()); RotationUtil.rotateBlockData(rot, arguments.getRotation().inverse());
arguments.getBuffer().addItem(new BufferedPulledBlock(rot),
new Vector3(FastMath.roundToInt(xz.getX()), y.apply(implementationArguments, variableMap).intValue(), Vector3 mutable = Vector3.of(FastMath.roundToInt(xz.getX()), y.apply(implementationArguments, variableMap).intValue(),
FastMath.roundToInt(xz.getZ()))); FastMath.roundToInt(xz.getZ())).add(arguments.getOrigin());
while(mutable.getY() > arguments.getWorld().getMinHeight()) {
if(!arguments.getWorld().getBlockState(mutable).isAir()) {
arguments.getWorld().setBlockState(mutable, rot);
break;
}
mutable.subtract(0, 1, 0);
}
return null; return null;
} }
@@ -43,12 +43,10 @@ public class SetMarkFunction implements Function<Void> {
RotationUtil.rotateVector(xz, arguments.getRotation()); RotationUtil.rotateVector(xz, arguments.getRotation());
arguments.getBuffer().setMark(mark.apply(implementationArguments, variableMap), new Vector3(FastMath.floorToInt(xz.getX()), arguments.setMark(Vector3.of(FastMath.floorToInt(xz.getX()),
FastMath.floorToInt( FastMath.floorToInt(
y.apply(implementationArguments, y.apply(implementationArguments, variableMap).doubleValue()),
variableMap) FastMath.floorToInt(xz.getZ())).add(arguments.getOrigin()), mark.apply(implementationArguments, variableMap));
.doubleValue()),
FastMath.floorToInt(xz.getZ())));
return null; return null;
} }
@@ -7,6 +7,8 @@
package com.dfsek.terra.addons.terrascript.script.functions; package com.dfsek.terra.addons.terrascript.script.functions;
import com.dfsek.terra.api.block.entity.BlockEntity;
import net.jafama.FastMath; import net.jafama.FastMath;
import java.util.Map; import java.util.Map;
@@ -23,17 +25,19 @@ import com.dfsek.terra.api.util.RotationUtil;
import com.dfsek.terra.api.util.vector.Vector2; import com.dfsek.terra.api.util.vector.Vector2;
import com.dfsek.terra.api.util.vector.Vector3; import com.dfsek.terra.api.util.vector.Vector3;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class StateFunction implements Function<Void> { public class StateFunction implements Function<Void> {
private static final Logger LOGGER = LoggerFactory.getLogger(StateFunction.class);
private final Returnable<String> data; private final Returnable<String> data;
private final Returnable<Number> x, y, z; private final Returnable<Number> x, y, z;
private final Position position; private final Position position;
private final Platform platform;
public StateFunction(Returnable<Number> x, Returnable<Number> y, Returnable<Number> z, Returnable<String> data, Platform platform, public StateFunction(Returnable<Number> x, Returnable<Number> y, Returnable<Number> z, Returnable<String> data,
Position position) { Position position) {
this.position = position; this.position = position;
this.platform = platform;
this.data = data; this.data = data;
this.x = x; this.x = x;
this.y = y; this.y = y;
@@ -47,9 +51,16 @@ public class StateFunction implements Function<Void> {
z.apply(implementationArguments, variableMap).doubleValue()); z.apply(implementationArguments, variableMap).doubleValue());
RotationUtil.rotateVector(xz, arguments.getRotation()); RotationUtil.rotateVector(xz, arguments.getRotation());
arguments.getBuffer().addItem(new BufferedStateManipulator(data.apply(implementationArguments, variableMap)), Vector3 origin = new Vector3(FastMath.roundToInt(xz.getX()), y.apply(implementationArguments, variableMap).intValue(),
new Vector3(FastMath.roundToInt(xz.getX()), y.apply(implementationArguments, variableMap).intValue(), FastMath.roundToInt(xz.getZ())).add(arguments.getOrigin());
FastMath.roundToInt(xz.getZ()))); try {
BlockEntity state = arguments.getWorld().getBlockEntity(origin);
state.applyState(data.apply(implementationArguments, variableMap));
state.update(false);
} catch(Exception e) {
LOGGER.warn("Could not apply BlockState at {}", origin, e);
e.printStackTrace();
}
return null; return null;
} }
@@ -82,7 +82,7 @@ public class StructureFunction implements Function<Boolean> {
} }
if(script instanceof StructureScript structureScript) { if(script instanceof StructureScript structureScript) {
return structureScript.generate(arguments.getBuffer().getOrigin(), return structureScript.generate(arguments.getOrigin(),
arguments.getWorld() arguments.getWorld()
.buffer(FastMath.roundToInt(xz.getX()), .buffer(FastMath.roundToInt(xz.getX()),
y.apply(implementationArguments, variableMap).intValue(), y.apply(implementationArguments, variableMap).intValue(),
@@ -90,7 +90,7 @@ public class StructureFunction implements Function<Boolean> {
arguments.getRandom(), arguments.getRandom(),
arguments.getRotation().rotate(rotation1), arguments.getRecursions() + 1); arguments.getRotation().rotate(rotation1), arguments.getRecursions() + 1);
} }
return script.generate(arguments.getBuffer().getOrigin(), return script.generate(arguments.getOrigin(),
arguments.getWorld() arguments.getWorld()
.buffer(FastMath.roundToInt(xz.getX()), .buffer(FastMath.roundToInt(xz.getX()),
y.apply(implementationArguments, variableMap).intValue(), y.apply(implementationArguments, variableMap).intValue(),
@@ -50,7 +50,7 @@ public class CheckFunction implements Function<String> {
RotationUtil.rotateVector(xz, arguments.getRotation()); RotationUtil.rotateVector(xz, arguments.getRotation());
Vector3 location = arguments.getBuffer().getOrigin().clone().add( Vector3 location = arguments.getOrigin().toVector3().clone().add(
new Vector3(FastMath.roundToInt(xz.getX()), y.apply(implementationArguments, variableMap).doubleValue(), new Vector3(FastMath.roundToInt(xz.getX()), y.apply(implementationArguments, variableMap).doubleValue(),
FastMath.roundToInt(xz.getZ()))); FastMath.roundToInt(xz.getZ())));
@@ -9,14 +9,12 @@ package com.dfsek.terra.api.structure;
import java.util.Random; import java.util.Random;
import com.dfsek.terra.api.structure.buffer.Buffer;
import com.dfsek.terra.api.util.Rotation; import com.dfsek.terra.api.util.Rotation;
import com.dfsek.terra.api.util.StringIdentifiable; import com.dfsek.terra.api.util.StringIdentifiable;
import com.dfsek.terra.api.util.vector.Vector3; import com.dfsek.terra.api.util.vector.integer.Vector3Int;
import com.dfsek.terra.api.world.WritableWorld; import com.dfsek.terra.api.world.WritableWorld;
import com.dfsek.terra.api.world.chunk.Chunk;
public interface Structure extends StringIdentifiable { public interface Structure extends StringIdentifiable {
boolean generate(Vector3 location, WritableWorld world, Random random, Rotation rotation); boolean generate(Vector3Int location, WritableWorld world, Random random, Rotation rotation);
} }
@@ -4,6 +4,7 @@ import com.dfsek.terra.api.block.state.BlockState;
import com.dfsek.terra.api.entity.Entity; import com.dfsek.terra.api.entity.Entity;
import com.dfsek.terra.api.entity.EntityType; import com.dfsek.terra.api.entity.EntityType;
import com.dfsek.terra.api.util.vector.Vector3; import com.dfsek.terra.api.util.vector.Vector3;
import com.dfsek.terra.api.util.vector.integer.Vector3Int;
public interface WritableWorld extends ReadableWorld { public interface WritableWorld extends ReadableWorld {
@@ -11,10 +12,18 @@ public interface WritableWorld extends ReadableWorld {
setBlockState(position.getBlockX(), position.getBlockY(), position.getBlockZ(), data, physics); setBlockState(position.getBlockX(), position.getBlockY(), position.getBlockZ(), data, physics);
} }
default void setBlockState(Vector3Int position, BlockState data, boolean physics) {
setBlockState(position.getX(), position.getY(), position.getZ(), data, physics);
}
default void setBlockState(Vector3 position, BlockState data) { default void setBlockState(Vector3 position, BlockState data) {
setBlockState(position, data, false); setBlockState(position, data, false);
} }
default void setBlockState(Vector3Int position, BlockState data) {
setBlockState(position, data, false);
}
default void setBlockState(int x, int y, int z, BlockState data) { default void setBlockState(int x, int y, int z, BlockState data) {
setBlockState(x, y, z, data, false); setBlockState(x, y, z, data, false);
} }
@@ -7,6 +7,8 @@
package com.dfsek.terra.api.util.vector; package com.dfsek.terra.api.util.vector;
import com.dfsek.terra.api.util.vector.integer.Vector3Int;
import net.jafama.FastMath; import net.jafama.FastMath;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@@ -24,6 +26,10 @@ public class Vector3 implements Cloneable {
this.z = z; this.z = z;
} }
public static Vector3 of(double x, double y, double z) {
return new Vector3(x, y, z);
}
public Vector3 multiply(double m) { public Vector3 multiply(double m) {
x *= m; x *= m;
y *= m; y *= m;
@@ -45,6 +51,13 @@ public class Vector3 implements Cloneable {
return this; return this;
} }
public Vector3 add(Vector3Int other) {
this.x += other.getX();
this.y += other.getY();
this.z += other.getZ();
return this;
}
public Vector3 add(Vector2 other) { public Vector3 add(Vector2 other) {
this.x += other.getX(); this.x += other.getX();
this.z += other.getZ(); this.z += other.getZ();
@@ -1,5 +1,8 @@
package com.dfsek.terra.api.util.vector.integer; package com.dfsek.terra.api.util.vector.integer;
import com.dfsek.terra.api.util.vector.Vector3;
public class Vector3Int { public class Vector3Int {
private static final Vector3Int ZERO = new Vector3Int(0, 0, 0); private static final Vector3Int ZERO = new Vector3Int(0, 0, 0);
private static final Vector3Int UNIT = new Vector3Int(0, 1, 0); private static final Vector3Int UNIT = new Vector3Int(0, 1, 0);
@@ -39,6 +42,9 @@ public class Vector3Int {
return new Mutable(x, y, z); return new Mutable(x, y, z);
} }
public Vector3 toVector3() {
return Vector3.of(x, y, z);
}
public static class Mutable { public static class Mutable {
private int x, y, z; private int x, y, z;
@@ -76,5 +82,16 @@ public class Vector3Int {
public Vector3Int immutable() { public Vector3Int immutable() {
return Vector3Int.of(x, y, z); return Vector3Int.of(x, y, z);
} }
public Mutable add(int x, int y, int z) {
this.x += x;
this.y += y;
this.z += z;
return this;
}
public Vector3 toVector3() {
return Vector3.of(x, y, z);
}
} }
} }