mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-06-14 12:51:20 +00:00
implement SamplerCache for drastically increased structure perf
This commit is contained in:
@@ -21,7 +21,7 @@ import com.dfsek.terra.api.structures.script.builders.UnaryNumberFunctionBuilder
|
||||
import com.dfsek.terra.api.structures.structure.Rotation;
|
||||
import com.dfsek.terra.api.structures.structure.buffer.Buffer;
|
||||
import com.dfsek.terra.api.structures.structure.buffer.StructureBuffer;
|
||||
import com.dfsek.terra.api.structures.world.CheckCache;
|
||||
import com.dfsek.terra.generation.math.SamplerCache;
|
||||
import com.dfsek.terra.registry.LootRegistry;
|
||||
import com.dfsek.terra.registry.ScriptRegistry;
|
||||
import net.jafama.FastMath;
|
||||
@@ -38,7 +38,7 @@ public class StructureScript {
|
||||
private final String id;
|
||||
private final LinkedHashMap<Location, StructureBuffer> cache;
|
||||
|
||||
public StructureScript(InputStream inputStream, TerraPlugin main, ScriptRegistry registry, LootRegistry lootRegistry, CheckCache cache) throws ParseException {
|
||||
public StructureScript(InputStream inputStream, TerraPlugin main, ScriptRegistry registry, LootRegistry lootRegistry, SamplerCache cache) throws ParseException {
|
||||
Parser parser;
|
||||
try {
|
||||
parser = new Parser(IOUtils.toString(inputStream));
|
||||
|
||||
+3
-3
@@ -6,15 +6,15 @@ import com.dfsek.terra.api.structures.parser.lang.Returnable;
|
||||
import com.dfsek.terra.api.structures.parser.lang.functions.FunctionBuilder;
|
||||
import com.dfsek.terra.api.structures.script.functions.CheckFunction;
|
||||
import com.dfsek.terra.api.structures.tokenizer.Position;
|
||||
import com.dfsek.terra.api.structures.world.CheckCache;
|
||||
import com.dfsek.terra.generation.math.SamplerCache;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class CheckFunctionBuilder implements FunctionBuilder<CheckFunction> {
|
||||
private final TerraPlugin main;
|
||||
private final CheckCache cache;
|
||||
private final SamplerCache cache;
|
||||
|
||||
public CheckFunctionBuilder(TerraPlugin main, CheckCache cache) {
|
||||
public CheckFunctionBuilder(TerraPlugin main, SamplerCache cache) {
|
||||
this.main = main;
|
||||
this.cache = cache;
|
||||
}
|
||||
|
||||
+3
-3
@@ -12,20 +12,20 @@ import com.dfsek.terra.api.structures.parser.lang.functions.Function;
|
||||
import com.dfsek.terra.api.structures.script.TerraImplementationArguments;
|
||||
import com.dfsek.terra.api.structures.structure.RotationUtil;
|
||||
import com.dfsek.terra.api.structures.tokenizer.Position;
|
||||
import com.dfsek.terra.api.structures.world.CheckCache;
|
||||
import com.dfsek.terra.api.world.generation.GenerationPhase;
|
||||
import com.dfsek.terra.biome.UserDefinedBiome;
|
||||
import com.dfsek.terra.biome.grid.master.TerraBiomeGrid;
|
||||
import com.dfsek.terra.config.templates.BiomeTemplate;
|
||||
import com.dfsek.terra.generation.math.SamplerCache;
|
||||
import net.jafama.FastMath;
|
||||
|
||||
public class CheckFunction implements Function<String> {
|
||||
private final TerraPlugin main;
|
||||
private final Returnable<Number> x, y, z;
|
||||
private final Position position;
|
||||
private final CheckCache cache;
|
||||
private final SamplerCache cache;
|
||||
|
||||
public CheckFunction(TerraPlugin main, Returnable<Number> x, Returnable<Number> y, Returnable<Number> z, CheckCache cache, Position position) {
|
||||
public CheckFunction(TerraPlugin main, Returnable<Number> x, Returnable<Number> y, Returnable<Number> z, SamplerCache cache, Position position) {
|
||||
this.main = main;
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
|
||||
@@ -1,49 +0,0 @@
|
||||
package com.dfsek.terra.api.structures.world;
|
||||
|
||||
import com.dfsek.terra.TerraWorld;
|
||||
import com.dfsek.terra.api.platform.TerraPlugin;
|
||||
import com.dfsek.terra.api.platform.world.World;
|
||||
import com.dfsek.terra.generation.math.Sampler;
|
||||
import net.jafama.FastMath;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class CheckCache {
|
||||
private final Map<Long, Container> cache;
|
||||
private final TerraPlugin main;
|
||||
|
||||
public CheckCache(TerraPlugin main) {
|
||||
cache = new HashMap<>();
|
||||
this.main = main;
|
||||
}
|
||||
|
||||
public Sampler get(World world, int x, int z) {
|
||||
return cache.computeIfAbsent(world.getSeed(), seed -> new Container(world, new LinkedHashMap<Long, Sampler>() {
|
||||
@Override
|
||||
protected boolean removeEldestEntry(Map.Entry<Long, Sampler> eldest) {
|
||||
return size() > main.getTerraConfig().getCheckCache();
|
||||
}
|
||||
})).get(x, z);
|
||||
}
|
||||
|
||||
|
||||
private class Container {
|
||||
private final World world;
|
||||
private final Map<Long, Sampler> cache;
|
||||
|
||||
private Container(World world, Map<Long, Sampler> cache) {
|
||||
this.world = world;
|
||||
this.cache = cache;
|
||||
}
|
||||
|
||||
public Sampler get(int x, int z) {
|
||||
int cx = FastMath.floorDiv(x, 16);
|
||||
int cz = FastMath.floorDiv(z, 16);
|
||||
long key = (((long) cx) << 32) | (cz & 0xffffffffL);
|
||||
TerraWorld tw = main.getWorld(world);
|
||||
return cache.computeIfAbsent(key, k -> new Sampler(cx, cz, tw.getGrid(), world, 4, 8));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user