Compare commits

..

7 Commits

Author SHA1 Message Date
Zoë Gidiere 5829112a74 Merge branch 'ver/6.4.0' into dev/image-caching 2023-10-26 12:56:17 -06:00
Astrash 0149a29b04 Load correct path 2023-10-24 14:08:56 +11:00
Astrash cd8605850f Replace unload-on-timeout with timeout > 0 2023-10-24 13:47:25 +11:00
Astrash 148b8dfe35 Bump image lib patch version 2023-10-24 13:34:10 +11:00
Astrash 3f485b1825 Add to unload on timeout description 2023-10-24 13:30:09 +11:00
Astrash cb6ecff113 Remove sout 2023-10-24 13:29:53 +11:00
Astrash e30bcbf1ba Improve image caching options 2023-10-24 13:26:35 +11:00
12 changed files with 162 additions and 235 deletions
@@ -19,6 +19,8 @@ import org.slf4j.LoggerFactory;
public class OreAddon implements AddonInitializer {
private static final Logger logger = LoggerFactory.getLogger(OreAddon.class);
@Inject
private Platform platform;
@@ -31,7 +33,9 @@ public class OreAddon implements AddonInitializer {
.getHandler(FunctionalEventHandler.class)
.register(addon, ConfigPackPreLoadEvent.class)
.then(event -> event.getPack().registerConfigType(new OreConfigType(), addon.key("ORE"), 1))
.then(event -> event.getPack().registerConfigType(new ScatteredOreConfigType(), addon.key("SCATTERED_ORE"), 1))
.failThrough();
if(platform.getTerraConfig().isDebugLog())
logger.warn("The ore-config addon is deprecated and scheduled for removal in Terra 7.0. It is recommended to use the ore-config-v2 addon for future pack development instead.");
}
}
@@ -1,37 +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.ore;
import com.dfsek.terra.api.Platform;
import com.dfsek.terra.api.config.ConfigFactory;
import com.dfsek.terra.api.config.ConfigPack;
import com.dfsek.terra.api.config.ConfigType;
import com.dfsek.terra.api.structure.Structure;
import com.dfsek.terra.api.util.reflection.TypeKey;
public class ScatteredOreConfigType implements ConfigType<ScatteredOreTemplate, Structure> {
public static final TypeKey<Structure> ORE_TYPE_TOKEN = new TypeKey<>() {
};
private final ScatteredOreFactory factory = new ScatteredOreFactory();
@Override
public ScatteredOreTemplate getTemplate(ConfigPack pack, Platform platform) {
return new ScatteredOreTemplate();
}
@Override
public ConfigFactory<ScatteredOreTemplate, Structure> getFactory() {
return factory;
}
@Override
public TypeKey<Structure> getTypeKey() {
return ORE_TYPE_TOKEN;
}
}
@@ -1,27 +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.ore;
import com.dfsek.tectonic.api.exception.LoadException;
import com.dfsek.terra.addons.ore.ores.VanillaOre;
import com.dfsek.terra.addons.ore.ores.VanillaScatteredOre;
import com.dfsek.terra.api.Platform;
import com.dfsek.terra.api.block.state.BlockState;
import com.dfsek.terra.api.config.ConfigFactory;
import com.dfsek.terra.api.structure.Structure;
public class ScatteredOreFactory implements ConfigFactory<ScatteredOreTemplate, Structure> {
@Override
public Structure build(ScatteredOreTemplate config, Platform platform) throws LoadException {
BlockState m = config.getMaterial();
return new VanillaScatteredOre(m, config.getSize(), config.getReplaceable(), config.doPhysics(), config.isExposed(),
config.getMaterialOverrides(), config.getSpread());
}
}
@@ -1,34 +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.ore;
import com.dfsek.tectonic.api.config.template.annotations.Default;
import com.dfsek.tectonic.api.config.template.annotations.Description;
import com.dfsek.tectonic.api.config.template.annotations.Final;
import com.dfsek.tectonic.api.config.template.annotations.Value;
import com.dfsek.terra.api.block.BlockType;
import com.dfsek.terra.api.block.state.BlockState;
import com.dfsek.terra.api.config.AbstractableTemplate;
import com.dfsek.terra.api.config.meta.Meta;
import com.dfsek.terra.api.util.collection.MaterialSet;
import java.util.HashMap;
import java.util.Map;
@SuppressWarnings({ "unused", "FieldMayBeFinal" })
public class ScatteredOreTemplate extends OreTemplate {
@Value("size")
private @Meta int spread = 7;
public int getSpread() {
return spread;
}
}
@@ -20,18 +20,16 @@ import com.dfsek.terra.api.util.collection.MaterialSet;
import com.dfsek.terra.api.util.vector.Vector3Int;
import com.dfsek.terra.api.world.WritableWorld;
import static com.dfsek.terra.addons.ore.utils.VanillaOreUtils.shouldPlace;
public class VanillaOre implements Structure {
protected final BlockState material;
private final BlockState material;
protected final double size;
protected final MaterialSet replaceable;
protected final boolean applyGravity;
protected final double exposed;
protected final Map<BlockType, BlockState> materials;
private final double size;
private final MaterialSet replaceable;
private final boolean applyGravity;
private final double exposed;
private final Map<BlockType, BlockState> materials;
public VanillaOre(BlockState material, double size, MaterialSet replaceable, boolean applyGravity,
double exposed, Map<BlockType, BlockState> materials) {
@@ -43,6 +41,16 @@ public class VanillaOre implements Structure {
this.materials = materials;
}
protected static boolean shouldNotDiscard(Random random, double chance) {
if(chance <= 0.0F) {
return true;
} else if(chance >= 1.0F) {
return false;
} else {
return random.nextFloat() >= chance;
}
}
public static double lerp(double t, double v0, double v1) {
return v0 + t * (v1 - v0);
}
@@ -50,14 +58,14 @@ public class VanillaOre implements Structure {
@Override
public boolean generate(Vector3Int location, WritableWorld world, Random random, Rotation rotation) {
float randomRadian = random.nextFloat() * (float) Math.PI;
double eighthSize = size / 8.0F;
double eigthSize = size / 8.0F;
// Place points to form a line segment
double startX = (double) location.getX() + MathUtil.sin(randomRadian) * eighthSize;
double endX = (double) location.getX() - MathUtil.sin(randomRadian) * eighthSize;
double startX = (double) location.getX() + MathUtil.sin(randomRadian) * eigthSize;
double endX = (double) location.getX() - MathUtil.sin(randomRadian) * eigthSize;
double startZ = (double) location.getZ() + MathUtil.cos(randomRadian) * eighthSize;
double endZ = (double) location.getZ() - MathUtil.cos(randomRadian) * eighthSize;
double startZ = (double) location.getZ() + MathUtil.cos(randomRadian) * eigthSize;
double endZ = (double) location.getZ() - MathUtil.cos(randomRadian) * eigthSize;
double startY = location.getY() + random.nextInt(3) - 2;
double endY = location.getY() + random.nextInt(3) - 2;
@@ -107,14 +115,14 @@ public class VanillaOre implements Structure {
}
int outset = (int) Math.ceil((size / 16.0F * 2.0F + 1.0F) / 2.0F);
int x = (int) (location.getX() - Math.ceil(eighthSize) - outset);
int x = (int) (location.getX() - Math.ceil(eigthSize) - outset);
int y = location.getY() - 2 - outset;
int z = (int) (location.getZ() - Math.ceil(eighthSize) - outset);
int z = (int) (location.getZ() - Math.ceil(eigthSize) - outset);
int horizontalSize = (int) (2 * (Math.ceil(eighthSize) + outset));
int horizontalSize = (int) (2 * (Math.ceil(eigthSize) + outset));
int verticalSize = 2 * (2 + outset);
int blockCount = 0;
int sphereCount = 0;
BitSet visited = new BitSet(horizontalSize * verticalSize * horizontalSize);
// Generate a sphere at each point
@@ -151,9 +159,10 @@ public class VanillaOre implements Structure {
visited.set(index);
BlockType block = world.getBlockState(xi, yi, zi).getBlockType();
if(shouldPlace(getReplaceable(), block, exposed, random, world, xi, yi, zi)) {
if(shouldPlace(block, random, world, xi, yi, zi)) {
world.setBlockState(xi, yi, zi, getMaterial(block), isApplyGravity());
++blockCount;
++sphereCount;
break;
}
}
}
@@ -165,8 +174,24 @@ public class VanillaOre implements Structure {
}
}
return blockCount > 0;
return sphereCount > 0;
}
public boolean shouldPlace(BlockType type, Random random, WritableWorld world, int x, int y, int z) {
if(!getReplaceable().contains(type)) {
return false;
} else if(shouldNotDiscard(random, exposed)) {
return true;
} else {
return !(world.getBlockState(x, y, z - 1).isAir() ||
world.getBlockState(x, y, z + 1).isAir() ||
world.getBlockState(x, y - 1, z).isAir() ||
world.getBlockState(x, y + 1, z).isAir() ||
world.getBlockState(x - 1, y, z).isAir() ||
world.getBlockState(x + 1, y, z).isAir());
}
}
public BlockState getMaterial(BlockType replace) {
return materials.getOrDefault(replace, material);
}
@@ -1,56 +0,0 @@
package com.dfsek.terra.addons.ore.ores;
import com.dfsek.terra.addons.ore.utils.VanillaOreUtils;
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.util.Rotation;
import com.dfsek.terra.api.util.collection.MaterialSet;
import com.dfsek.terra.api.util.vector.Vector3;
import com.dfsek.terra.api.util.vector.Vector3Int;
import com.dfsek.terra.api.world.WritableWorld;
import java.util.Map;
import java.util.Random;
import static com.dfsek.terra.addons.ore.utils.VanillaOreUtils.shouldPlace;
public class VanillaScatteredOre extends VanillaOre {
protected final int spread;
public VanillaScatteredOre(BlockState material, double size, MaterialSet replaceable, boolean applyGravity, double exposed,
Map<BlockType, BlockState> materials, int spread) {
super(material, size, replaceable, applyGravity, exposed, materials);
this.spread = spread;
}
@Override
public boolean generate(Vector3Int location, WritableWorld world, Random random, Rotation rotation) {
int i = random.nextInt((int) (size + 1));
Vector3Int.Mutable mutable = Vector3Int.zero().mutable();
for(int j = 0; j < i; ++j) {
this.setPos(mutable, random, location, Math.min(j, spread));
BlockType block = world.getBlockState(mutable).getBlockType();
if (shouldPlace(getReplaceable(), block, exposed, random, world, mutable.getX(), mutable.getY(), mutable.getZ())) {
world.setBlockState(mutable, getMaterial(block), isApplyGravity());
}
}
return true;
}
private void setPos(Vector3Int.Mutable mutable, Random random, Vector3Int location, int spread) {
int x = this.getSpread(random, spread);
int y = this.getSpread(random, spread);
int z = this.getSpread(random, spread);
mutable.setX(location.getX() + x);
mutable.setY(location.getY() + y);
mutable.setZ(location.getZ() + z);
}
private int getSpread(Random random, int spread) {
return Math.round((random.nextFloat() - random.nextFloat()) * (float)spread);
}
}
@@ -1,37 +0,0 @@
package com.dfsek.terra.addons.ore.utils;
import com.dfsek.terra.api.block.BlockType;
import com.dfsek.terra.api.util.collection.MaterialSet;
import com.dfsek.terra.api.world.WritableWorld;
import java.util.Random;
public class VanillaOreUtils {
protected static boolean shouldNotDiscard(Random random, double chance) {
if(chance <= 0.0F) {
return true;
} else if(chance >= 1.0F) {
return false;
} else {
return random.nextFloat() >= chance;
}
}
public static boolean shouldPlace(MaterialSet replaceable, BlockType type, Double exposed, Random random, WritableWorld world, int x, int y, int z) {
if(!replaceable.contains(type)) {
return false;
} else if(shouldNotDiscard(random, exposed)) {
return true;
} else {
return !(world.getBlockState(x, y, z - 1).isAir() ||
world.getBlockState(x, y, z + 1).isAir() ||
world.getBlockState(x, y - 1, z).isAir() ||
world.getBlockState(x, y + 1, z).isAir() ||
world.getBlockState(x - 1, y, z).isAir() ||
world.getBlockState(x + 1, y, z).isAir());
}
}
}
+1 -1
View File
@@ -1,4 +1,4 @@
version = version("1.0.0")
version = version("1.0.1")
dependencies {
compileOnlyApi(project(":common:addons:manifest-addon-loader"))
@@ -7,6 +7,11 @@ import java.util.function.Supplier;
import com.dfsek.terra.addons.image.colorsampler.ColorSampler;
import com.dfsek.terra.addons.image.config.ColorLoader;
import com.dfsek.terra.addons.image.config.ColorLoader.ColorString;
import com.dfsek.terra.addons.image.config.ImageLibraryPackConfigTemplate;
import com.dfsek.terra.addons.image.config.noisesampler.ChannelNoiseSamplerTemplate;
import com.dfsek.terra.addons.image.config.noisesampler.DistanceTransformNoiseSamplerTemplate;
import com.dfsek.terra.addons.image.config.image.ImageTemplate;
import com.dfsek.terra.addons.image.config.image.StitchedImageTemplate;
import com.dfsek.terra.addons.image.config.colorsampler.ConstantColorSamplerTemplate;
import com.dfsek.terra.addons.image.config.colorsampler.image.SingleImageColorSamplerTemplate;
import com.dfsek.terra.addons.image.config.colorsampler.image.TileImageColorSamplerTemplate;
@@ -52,6 +57,10 @@ public class ImageLibraryAddon implements AddonInitializer {
.getHandler(FunctionalEventHandler.class)
.register(addon, ConfigPackPreLoadEvent.class)
.priority(10)
.then(event -> {
ImageLibraryPackConfigTemplate config = event.loadTemplate(new ImageLibraryPackConfigTemplate());
event.getPack().getContext().put(config);
})
.then(event -> {
ConfigPack pack = event.getPack();
CheckedRegistry<Supplier<ObjectTemplate<Image>>> imageRegistry = pack.getOrCreateRegistry(IMAGE_REGISTRY_KEY);
@@ -0,0 +1,37 @@
package com.dfsek.terra.addons.image.config;
import com.dfsek.tectonic.api.config.template.ConfigTemplate;
import com.dfsek.tectonic.api.config.template.annotations.Default;
import com.dfsek.tectonic.api.config.template.annotations.Description;
import com.dfsek.tectonic.api.config.template.annotations.Value;
import com.dfsek.terra.api.properties.Properties;
public class ImageLibraryPackConfigTemplate implements ConfigTemplate, Properties {
// TODO - These would be better as plugin wide config parameters in config.yml
@Value("images.cache.load-on-use")
@Description("If set to true, images will load into memory upon use rather than on pack load.")
@Default
private boolean loadOnUse = false;
@Value("images.cache.timeout")
@Description("How many seconds to keep images loaded in the image cache for. " +
"If set to a number greater than 0, images will be removed from memory if not used after the timeout, otherwise images will stay loaded in memory. " +
"Setting the timeout to greater than 0 will trade decreased memory consumption when not performing any image reads for a period of time for extra processing time required to perform cache lookups.")
@Default
private int cacheTimeout = 0;
public boolean loadOnUse() {
return loadOnUse;
}
public boolean unloadOnTimeout() {
return cacheTimeout > 0;
}
public int getCacheTimeout() {
return cacheTimeout;
}
}
@@ -3,44 +3,60 @@ package com.dfsek.terra.addons.image.config.image;
import javax.imageio.ImageIO;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import com.dfsek.terra.addons.image.config.ImageLibraryPackConfigTemplate;
import com.dfsek.terra.addons.image.image.BufferedImageWrapper;
import com.dfsek.terra.addons.image.image.Image;
import com.dfsek.terra.addons.image.image.SuppliedImage;
import com.dfsek.terra.api.config.ConfigPack;
import com.dfsek.terra.api.config.Loader;
import com.dfsek.terra.api.properties.Properties;
import com.dfsek.terra.api.util.generic.Lazy;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
/*
* Cache prevents configs from loading the same image multiple times into memory
*/
record ImageCache(ConcurrentHashMap<String, Image> map) implements Properties {
record ImageCache(LoadingCache<String, Image> cache) implements Properties {
public static Image load(String path, ConfigPack pack, Loader files) throws IOException {
ImageCache cache;
ImageLibraryPackConfigTemplate config = pack.getContext().get(ImageLibraryPackConfigTemplate.class);
ImageCache images;
if(!pack.getContext().has(ImageCache.class)) {
cache = new ImageCache(new ConcurrentHashMap<>());
pack.getContext().put(cache);
} else {
cache = pack.getContext().get(ImageCache.class);
var cacheBuilder = Caffeine.newBuilder();
if (config.unloadOnTimeout()) cacheBuilder.expireAfterAccess(config.getCacheTimeout(), TimeUnit.SECONDS);
images = new ImageCache(cacheBuilder.build(s -> loadImage(s, files)));
pack.getContext().put(images);
} else images = pack.getContext().get(ImageCache.class);
if (config.loadOnUse()) {
if(config.unloadOnTimeout()) { // Grab directly from cache if images are to unload on timeout
return new SuppliedImage(() -> images.cache.get(path));
} else {
// If images do not time out, image can be lazily loaded once instead of performing cache lookups for each image operation
Lazy<Image> lazyImage = Lazy.lazy(() -> images.cache.get(path));
return new SuppliedImage(lazyImage::value);
}
}
if(cache.map.containsKey(path)) {
return cache.map.get(path);
} else {
try {
BufferedImageWrapper image = new BufferedImageWrapper(ImageIO.read(files.get(path)));
cache.map.put(path, image);
return image;
} catch(IllegalArgumentException e) {
throw new IllegalArgumentException("Unable to load image (image might be too large?)", e);
} catch(IOException e) {
if(e instanceof FileNotFoundException) {
// Rethrow using nicer message
throw new IOException("Unable to load image: No such file or directory: " + path, e);
}
throw new IOException("Unable to load image", e);
return images.cache.get(path);
}
private static Image loadImage(String path, Loader files) throws IOException {
try {
return new BufferedImageWrapper(ImageIO.read(files.get(path)));
} catch(IllegalArgumentException e) {
throw new IllegalArgumentException("Unable to load image (image might be too large?)", e);
} catch(IOException e) {
if(e instanceof FileNotFoundException) {
// Rethrow using nicer message
throw new IOException("Unable to load image: No such file or directory: " + path, e);
}
throw new IOException("Unable to load image", e);
}
}
}
@@ -0,0 +1,27 @@
package com.dfsek.terra.addons.image.image;
import java.util.function.Supplier;
public class SuppliedImage implements Image {
private final Supplier<Image> imageSupplier;
public SuppliedImage(Supplier<Image> imageSupplier) {
this.imageSupplier = imageSupplier;
}
@Override
public int getRGB(int x, int y) {
return imageSupplier.get().getRGB(x, y);
}
@Override
public int getWidth() {
return imageSupplier.get().getWidth();
}
@Override
public int getHeight() {
return imageSupplier.get().getHeight();
}
}