clean up Structure API

This commit is contained in:
dfsek
2021-12-19 23:13:57 -07:00
parent 30b02a03c4
commit f088928483
10 changed files with 112 additions and 128 deletions

View File

@@ -19,13 +19,11 @@ import com.dfsek.terra.api.block.state.properties.base.Properties;
import com.dfsek.terra.api.block.state.properties.enums.Direction;
import com.dfsek.terra.api.noise.NoiseSampler;
import com.dfsek.terra.api.structure.Structure;
import com.dfsek.terra.api.structure.buffer.Buffer;
import com.dfsek.terra.api.util.Rotation;
import com.dfsek.terra.api.util.collection.MaterialSet;
import com.dfsek.terra.api.util.collection.ProbabilityCollection;
import com.dfsek.terra.api.util.vector.Vector3;
import com.dfsek.terra.api.world.WritableWorld;
import com.dfsek.terra.api.world.chunk.Chunk;
public class TerraFlora implements Structure {
@@ -80,16 +78,6 @@ public class TerraFlora implements Structure {
return id;
}
@Override
public boolean generate(Vector3 location, WritableWorld world, Chunk chunk, Random random, Rotation rotation) {
return generate(location, world, random, rotation);
}
@Override
public boolean generate(Buffer buffer, WritableWorld world, Random random, Rotation rotation, int recursions) {
return generate(buffer.getOrigin(), world, random, rotation);
}
@Override
public boolean generate(Vector3 location, WritableWorld world, Random random, Rotation rotation) {
boolean doRotation = testRotation.size() > 0;

View File

@@ -15,12 +15,10 @@ import java.util.Random;
import com.dfsek.terra.api.block.BlockType;
import com.dfsek.terra.api.block.state.BlockState;
import com.dfsek.terra.api.structure.Structure;
import com.dfsek.terra.api.structure.buffer.Buffer;
import com.dfsek.terra.api.util.Rotation;
import com.dfsek.terra.api.util.collection.MaterialSet;
import com.dfsek.terra.api.util.vector.Vector3;
import com.dfsek.terra.api.world.WritableWorld;
import com.dfsek.terra.api.world.chunk.Chunk;
public class VanillaOre implements Structure {
@@ -41,17 +39,6 @@ public class VanillaOre implements Structure {
this.materials = materials;
}
@Override
public boolean generate(Vector3 location, WritableWorld world, Chunk chunk, Random random, Rotation rotation) {
return false;
}
@Override
public boolean generate(Buffer buffer, WritableWorld world, Random random, Rotation rotation, int recursions) {
generate(buffer.getOrigin(), world, random);
return false;
}
@Override
public boolean generate(Vector3 location, WritableWorld world, Random random, Rotation rotation) {
generate(location, world, random);

View File

@@ -2,13 +2,9 @@ package com.dfsek.terra.addons.palette.shortcut.block;
import com.dfsek.terra.api.block.state.BlockState;
import com.dfsek.terra.api.structure.Structure;
import com.dfsek.terra.api.structure.buffer.Buffer;
import com.dfsek.terra.api.structure.buffer.items.BufferedBlock;
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.chunk.Chunk;
import com.dfsek.terra.api.world.chunk.generation.util.Palette;
import java.util.Random;
@@ -20,18 +16,6 @@ public class SingletonStructure implements Structure {
this.blockState = blockState;
}
@Override
public boolean generate(Vector3 location, WritableWorld world, Chunk chunk, Random random, Rotation rotation) {
world.setBlockState(location, blockState);
return true;
}
@Override
public boolean generate(Buffer buffer, WritableWorld world, Random random, Rotation rotation, int recursions) {
world.setBlockState(buffer.getOrigin(), blockState);
return true;
}
@Override
public boolean generate(Vector3 location, WritableWorld world, Random random, Rotation rotation) {
world.setBlockState(location, blockState);

View File

@@ -96,7 +96,7 @@ public class SpongeSchematicAddon implements AddonInitializer {
}
}
return new SpongeStructure(states, platform, id);
return new SpongeStructure(states, id);
} catch(IOException e) {
throw new IllegalArgumentException("Failed to parse Sponge schematic: ", e);
}

View File

@@ -7,75 +7,28 @@
package com.dfsek.terra.addons.sponge;
import net.jafama.FastMath;
import java.util.Random;
import com.dfsek.terra.api.Platform;
import com.dfsek.terra.api.block.state.BlockState;
import com.dfsek.terra.api.structure.Structure;
import com.dfsek.terra.api.structure.buffer.Buffer;
import com.dfsek.terra.api.structure.buffer.items.BufferedBlock;
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.world.WritableWorld;
import com.dfsek.terra.api.world.chunk.Chunk;
public class SpongeStructure implements Structure {
private final BlockState[][][] blocks;
private final Platform platform;
private final String id;
public SpongeStructure(BlockState[][][] blocks, Platform platform, String id) {
public SpongeStructure(BlockState[][][] blocks, String id) {
this.blocks = blocks;
this.platform = platform;
this.id = id;
}
@Override
public boolean generate(Vector3 location, WritableWorld world, Chunk chunk, Random random, Rotation rotation) {
int bX = location.getBlockX();
int bY = location.getBlockY();
int bZ = location.getBlockZ();
for(int x = 0; x < blocks.length; x++) {
for(int z = 0; z < blocks[x].length; z++) {
Vector2Int r = Vector2Int.of(x, z).rotate(rotation);
int rX = r.getX();
int rZ = r.getZ();
if(FastMath.floorDiv(bX + rX, 16) != chunk.getX() || FastMath.floorDiv(bZ + rZ, 16) != chunk.getZ()) {
continue;
}
for(int y = 0; y < blocks[z].length; y++) {
BlockState state = blocks[x][z][y];
if(state == null) continue;
world.setBlockState(bX + rX, bY + y, bZ + rZ, state);
}
}
}
return true;
}
@Override
public boolean generate(Buffer buffer, WritableWorld world, Random random, Rotation rotation, int recursions) {
for(int x = 0; x < blocks.length; x++) {
for(int z = 0; z < blocks[x].length; z++) {
Vector2Int r = Vector2Int.of(x, z).rotate(rotation);
int rX = r.getX();
int rZ = r.getZ();
for(int y = 0; y < blocks[z].length; y++) {
BlockState state = blocks[x][z][y];
if(state == null) continue;
buffer.addItem(new BufferedBlock(state, true, platform, false), new Vector3(rX, y, rZ));
}
}
}
return true;
}
@Override
public boolean generate(Vector3 location, WritableWorld world, Random random, Rotation rotation) {
int bX = location.getBlockX();

View File

@@ -46,14 +46,11 @@ import com.dfsek.terra.api.profiler.ProfileFrame;
import com.dfsek.terra.api.registry.Registry;
import com.dfsek.terra.api.structure.LootTable;
import com.dfsek.terra.api.structure.Structure;
import com.dfsek.terra.api.structure.buffer.Buffer;
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.vector.Vector3;
import com.dfsek.terra.api.world.ServerWorld;
import com.dfsek.terra.api.world.WritableWorld;
import com.dfsek.terra.api.world.chunk.Chunk;
public class StructureScript implements Structure {
@@ -132,24 +129,6 @@ public class StructureScript implements Structure {
this.cache = CacheBuilder.newBuilder().maximumSize(platform.getTerraConfig().getStructureCache()).build();
}
@Override
@SuppressWarnings("try")
public boolean generate(Vector3 location, WritableWorld world, Chunk chunk, Random random, Rotation rotation) {
try(ProfileFrame ignore = platform.getProfiler().profile("terrascript_chunk:" + id)) {
StructureBuffer buffer = computeBuffer(location, world, random, rotation);
buffer.paste(location, chunk);
return buffer.succeeded();
}
}
@Override
@SuppressWarnings("try")
public boolean generate(Buffer buffer, WritableWorld world, Random random, Rotation rotation, int recursions) {
try(ProfileFrame ignore = platform.getProfiler().profile("terrascript_recursive:" + id)) {
return applyBlock(new TerraImplementationArguments(buffer, rotation, random, world, recursions));
}
}
@Override
@SuppressWarnings("try")
public boolean generate(Vector3 location, WritableWorld world, Random random, Rotation rotation) {
@@ -159,11 +138,10 @@ public class StructureScript implements Structure {
}
}
@SuppressWarnings("try")
public boolean test(Vector3 location, ServerWorld world, Random random, Rotation rotation) {
try(ProfileFrame ignore = platform.getProfiler().profile("terrascript_test:" + id)) {
StructureBuffer buffer = computeBuffer(location, world, random, rotation);
return buffer.succeeded();
public boolean generate(Vector3 location, WritableWorld world, Random random, Rotation rotation, int recursions) {
try(ProfileFrame ignore = platform.getProfiler().profile("terrascript_direct:" + id)) {
DirectBuffer buffer = new DirectBuffer(location, world);
return applyBlock(new TerraImplementationArguments(buffer, rotation, random, world, recursions));
}
}

View File

@@ -7,6 +7,8 @@
package com.dfsek.terra.addons.terrascript.script.functions;
import com.dfsek.terra.addons.terrascript.script.StructureScript;
import net.jafama.FastMath;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -38,7 +40,7 @@ public class StructureFunction implements Function<Boolean> {
private final Position position;
private final Platform platform;
private final List<Returnable<String>> rotations;
public StructureFunction(Returnable<Number> x, Returnable<Number> y, Returnable<Number> z, Returnable<String> id,
List<Returnable<String>> rotations, Registry<Structure> registry, Position position, Platform platform) {
this.registry = registry;
@@ -76,14 +78,25 @@ public class StructureFunction implements Function<Boolean> {
rotation1 = Rotation.valueOf(rotString);
} catch(IllegalArgumentException e) {
LOGGER.warn("Invalid rotation {}", rotString);
return null;
return false;
}
Vector3 offset = new Vector3(FastMath.roundToInt(xz.getX()), y.apply(implementationArguments, variableMap).doubleValue(),
FastMath.roundToInt(xz.getZ()));
return script.generate(new IntermediateBuffer(arguments.getBuffer(), offset), arguments.getWorld(), arguments.getRandom(),
arguments.getRotation().rotate(rotation1), arguments.getRecursions() + 1);
if(script instanceof StructureScript structureScript) {
return structureScript.generate(arguments.getBuffer().getOrigin(),
arguments.getWorld()
.buffer(FastMath.roundToInt(xz.getX()),
y.apply(implementationArguments, variableMap).intValue(),
FastMath.roundToInt(xz.getZ())),
arguments.getRandom(),
arguments.getRotation().rotate(rotation1), arguments.getRecursions() + 1);
}
return script.generate(arguments.getBuffer().getOrigin(),
arguments.getWorld()
.buffer(FastMath.roundToInt(xz.getX()),
y.apply(implementationArguments, variableMap).intValue(),
FastMath.roundToInt(xz.getZ())),
arguments.getRandom(),
arguments.getRotation().rotate(rotation1));
}).orElseGet(() -> {
LOGGER.error("No such structure {}", app);
return false;

View File

@@ -18,9 +18,5 @@ import com.dfsek.terra.api.world.chunk.Chunk;
public interface Structure extends StringIdentifiable {
boolean generate(Vector3 location, WritableWorld world, Chunk chunk, Random random, Rotation rotation);
boolean generate(Buffer buffer, WritableWorld world, Random random, Rotation rotation, int recursions);
boolean generate(Vector3 location, WritableWorld world, Random random, Rotation rotation);
}

View File

@@ -0,0 +1,82 @@
package com.dfsek.terra.api.world;
import com.dfsek.terra.api.block.entity.BlockEntity;
import com.dfsek.terra.api.block.state.BlockState;
import com.dfsek.terra.api.config.ConfigPack;
import com.dfsek.terra.api.entity.Entity;
import com.dfsek.terra.api.entity.EntityType;
import com.dfsek.terra.api.world.biome.generation.BiomeProvider;
import com.dfsek.terra.api.world.chunk.generation.ChunkGenerator;
public class BufferedWorld implements WritableWorld {
private final WritableWorld delegate;
private final int offsetX, offsetY, offsetZ;
protected BufferedWorld(WritableWorld delegate, int offsetX, int offsetY, int offsetZ) {
this.delegate = delegate;
this.offsetX = offsetX;
this.offsetY = offsetY;
this.offsetZ = offsetZ;
}
@Override
public Object getHandle() {
return delegate.getHandle();
}
@Override
public BlockState getBlockState(int x, int y, int z) {
return delegate.getBlockState(x + offsetX, y + offsetY, z + offsetZ);
}
@Override
public BlockEntity getBlockEntity(int x, int y, int z) {
return delegate.getBlockEntity(x + offsetX, y + offsetY, z + offsetZ);
}
@Override
public long getSeed() {
return delegate.getSeed();
}
@Override
public int getMaxHeight() {
return delegate.getMaxHeight();
}
@Override
public int getMinHeight() {
return delegate.getMinHeight();
}
@Override
public ChunkGenerator getGenerator() {
return delegate.getGenerator();
}
@Override
public BiomeProvider getBiomeProvider() {
return delegate.getBiomeProvider();
}
@Override
public ConfigPack getPack() {
return delegate.getPack();
}
@Override
public void setBlockState(int x, int y, int z, BlockState data, boolean physics) {
delegate.setBlockState(x + offsetX, y + offsetY, z + offsetZ, data, physics);
}
@Override
public Entity spawnEntity(double x, double y, double z, EntityType entityType) {
return delegate.spawnEntity(x + offsetX, y + offsetY, z + offsetZ, entityType);
}
public WritableWorld getDelegate() {
return delegate;
}
}

View File

@@ -28,4 +28,7 @@ public interface WritableWorld extends ReadableWorld {
Entity spawnEntity(double x, double y, double z, EntityType entityType);
default BufferedWorld buffer(int offsetX, int offsetY, int offsetZ) {
return new BufferedWorld(this, offsetX, offsetY, offsetZ);
}
}