Optimize ZombieVoronoi.evaluateNoise with a hook-based implementation for Build 42.21. Replace distance calculations with a single-pass method, avoiding boxed sorting. Add associated tests and integrate new patch into the performance patch set.
This commit is contained in:
1 parent
05eed7e205
commit
900f65b591
6 files changed
+576
-1
No files matched your search
+27
@@ -0,0 +1,27 @@
|
||||
package dev.overlord.zomboidforge.compat.b42.hooks;
|
||||
|
||||
import dev.overlord.zomboidforge.compat.b42.util.VoronoiNoise;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
import zombie.iso.worldgen.zombie.ClosestSelection;
|
||||
import zombie.iso.worldgen.zombie.Coord;
|
||||
|
||||
/** Hooks called by patched {@code zombie.iso.worldgen.zombie.ZombieVoronoi} code. */
|
||||
public final class ZombieVoronoiHooks
|
||||
{
|
||||
private ZombieVoronoiHooks() {}
|
||||
|
||||
/**
|
||||
* Replaces the body of {@code ZombieVoronoi.evaluateNoise(x, y, rng, points)}, which passes
|
||||
* the voronoi's fields {@code closestPoint}, {@code seed} and {@code numberPoints}. Returns
|
||||
* what the vanilla body returns ({@link VoronoiNoise}).
|
||||
*/
|
||||
public static double evaluateNoise(
|
||||
ClosestSelection selection, long seed, int numberPoints,
|
||||
double x, double y, Random rng, List<Coord> points
|
||||
) {
|
||||
return VoronoiNoise.evaluate(selection, seed, numberPoints, x, y, rng, points);
|
||||
}
|
||||
}
|
||||
+121
@@ -0,0 +1,121 @@
|
||||
package dev.overlord.zomboidforge.compat.b42.util;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Random;
|
||||
|
||||
import zombie.iso.worldgen.zombie.ClosestSelection;
|
||||
import zombie.iso.worldgen.zombie.ClosestSelectionType;
|
||||
import zombie.iso.worldgen.zombie.Coord;
|
||||
import zombie.iso.worldgen.zombie.HashUtil;
|
||||
|
||||
/**
|
||||
* The value of {@code ZombieVoronoi.evaluateNoise} without its {@link Coord} list for the
|
||||
* {@link ClosestSelectionType}s.
|
||||
*
|
||||
* <p>Vanilla clears {@code points}, adds {@code numberPoints} {@link Coord}s for each of the 9
|
||||
* sectors around {@code (x, y)}, reseeding {@code rng} per sector, and returns
|
||||
* {@code closestPoint.getClosest(x, y, points)}. The three {@link ClosestSelectionType}s map the
|
||||
* points to boxed squared distances, sort them, and return the smallest, the second smallest,
|
||||
* or the second minus the smallest.
|
||||
*
|
||||
* <p>Here the same random numbers in the same order feed the same distance expression, and one
|
||||
* pass keeps the two smallest distances in the order of {@code Double.compare}, which
|
||||
* {@code sorted()} uses. The results are identical without the {@code Coord}s, the boxing and
|
||||
* the sort. {@code points} stays untouched: the only caller, {@code evaluateCellNoise}, passes a
|
||||
* list it does not read. Any other selection gets the filled list as in vanilla.
|
||||
*/
|
||||
public final class VoronoiNoise
|
||||
{
|
||||
private VoronoiNoise() {}
|
||||
|
||||
public static double evaluate(
|
||||
ClosestSelection selection, long seed, int numberPoints,
|
||||
double x, double y, Random rng, List<Coord> points
|
||||
) {
|
||||
int rank = VoronoiNoise.rank(selection);
|
||||
if (rank < 0) return VoronoiNoise.evaluateWithPoints(selection, seed, numberPoints, x, y, rng, points);
|
||||
|
||||
int sectorX = (int) Math.floor(x);
|
||||
int sectorY = (int) Math.floor(y);
|
||||
double smallest = 0.0;
|
||||
double second = 0.0;
|
||||
int count = 0;
|
||||
|
||||
for (int offsetX = -1; offsetX <= 1; ++offsetX)
|
||||
{
|
||||
int pointSectorX = sectorX + offsetX;
|
||||
|
||||
for (int offsetY = -1; offsetY <= 1; ++offsetY)
|
||||
{
|
||||
int pointSectorY = sectorY + offsetY;
|
||||
rng.setSeed(HashUtil.hash2D(seed, pointSectorX, pointSectorY));
|
||||
|
||||
for (int i = 0; i < numberPoints; ++i)
|
||||
{
|
||||
double pointX = rng.nextDouble() + (double) pointSectorX;
|
||||
double pointY = rng.nextDouble() + (double) pointSectorY;
|
||||
double distance = (x - pointX) * (x - pointX) + (y - pointY) * (y - pointY);
|
||||
|
||||
if (count == 0) smallest = distance;
|
||||
else if (Double.compare(distance, smallest) < 0)
|
||||
{
|
||||
second = smallest;
|
||||
smallest = distance;
|
||||
}
|
||||
else if (count == 1 || Double.compare(distance, second) < 0) second = distance;
|
||||
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Vanilla reads index 0, or index 1 first, of the sorted list: same exception when it is too short.
|
||||
Objects.checkIndex(rank == 0 ? 0 : 1, count);
|
||||
|
||||
return switch (rank)
|
||||
{
|
||||
case 0 -> smallest;
|
||||
case 1 -> second;
|
||||
default -> second - smallest;
|
||||
};
|
||||
}
|
||||
|
||||
/** 0 for {@code FIRST}, 1 for {@code SECOND}, 2 for {@code SECOND_MINUS_FIRST}, -1 for any other selection. */
|
||||
private static int rank(ClosestSelection selection)
|
||||
{
|
||||
return switch (selection) {
|
||||
case ClosestSelectionType.FIRST -> 0;
|
||||
case ClosestSelectionType.SECOND -> 1;
|
||||
case ClosestSelectionType.SECOND_MINUS_FIRST -> 2;
|
||||
default -> -1;
|
||||
};
|
||||
}
|
||||
|
||||
/** The vanilla body. */
|
||||
private static double evaluateWithPoints(
|
||||
ClosestSelection selection, long seed, int numberPoints,
|
||||
double x, double y, Random rng, List<Coord> points
|
||||
) {
|
||||
points.clear();
|
||||
int sectorX = (int) Math.floor(x);
|
||||
int sectorY = (int) Math.floor(y);
|
||||
|
||||
for (int offsetX = -1; offsetX <= 1; ++offsetX)
|
||||
{
|
||||
int pointSectorX = sectorX + offsetX;
|
||||
|
||||
for (int offsetY = -1; offsetY <= 1; ++offsetY)
|
||||
{
|
||||
int pointSectorY = sectorY + offsetY;
|
||||
rng.setSeed(HashUtil.hash2D(seed, pointSectorX, pointSectorY));
|
||||
|
||||
for (int i = 0; i < numberPoints; ++i) {
|
||||
points.add(new Coord(rng.nextDouble() + (double) pointSectorX, rng.nextDouble() + (double) pointSectorY));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return selection.getClosest(x, y, points);
|
||||
}
|
||||
}
|
||||
+117
@@ -0,0 +1,117 @@
|
||||
package dev.overlord.zomboidforge.compat.b42.util;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import zombie.iso.worldgen.zombie.ClosestSelection;
|
||||
import zombie.iso.worldgen.zombie.ClosestSelectionType;
|
||||
import zombie.iso.worldgen.zombie.Coord;
|
||||
import zombie.iso.worldgen.zombie.ZombieVoronoi;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
/** Compares {@link VoronoiNoise} with the vanilla {@code ZombieVoronoi.evaluateNoise} of the game jar. */
|
||||
class VoronoiNoiseTest
|
||||
{
|
||||
private static final long[] SEEDS = { 0L, 12345L, -987654321L, Long.MAX_VALUE };
|
||||
|
||||
private static double vanilla(ZombieVoronoi voronoi, double x, double y, List<Coord> points) throws ReflectiveOperationException
|
||||
{
|
||||
Method method = ZombieVoronoi.class.getDeclaredMethod("evaluateNoise", double.class, double.class, Random.class, List.class);
|
||||
method.setAccessible(true);
|
||||
|
||||
return (double) method.invoke(voronoi, x, y, new Random(), points);
|
||||
}
|
||||
|
||||
/** Random coordinates, and coordinates on and next to sector borders, including negative ones. */
|
||||
private static List<double[]> coordinates()
|
||||
{
|
||||
List<double[]> coordinates = new ArrayList<>();
|
||||
Random random = new Random(42L);
|
||||
|
||||
for (int i = 0; i < 500; i++) coordinates.add(new double[] { random.nextDouble() * 2000.0 - 1000.0, random.nextDouble() * 2000.0 - 1000.0 });
|
||||
|
||||
for (double border : new double[] { -2.0, -1.0, 0.0, 1.0, 37.0 })
|
||||
{
|
||||
coordinates.add(new double[] { border, border });
|
||||
coordinates.add(new double[] { Math.nextDown(border), Math.nextUp(border) });
|
||||
coordinates.add(new double[] { -0.0, border });
|
||||
}
|
||||
|
||||
return coordinates;
|
||||
}
|
||||
|
||||
@Test
|
||||
void returnsTheVanillaValueForEveryClosestSelectionType() throws ReflectiveOperationException
|
||||
{
|
||||
int compared = 0;
|
||||
|
||||
for (ClosestSelectionType type : ClosestSelectionType.values())
|
||||
{
|
||||
for (int numberPoints = 1; numberPoints <= 3; numberPoints++)
|
||||
{
|
||||
for (long seed : VoronoiNoiseTest.SEEDS)
|
||||
{
|
||||
ZombieVoronoi voronoi = new ZombieVoronoi(seed, numberPoints, type, 1.0, 0.0);
|
||||
|
||||
for (double[] point : VoronoiNoiseTest.coordinates())
|
||||
{
|
||||
double expected = VoronoiNoiseTest.vanilla(voronoi, point[0], point[1], new ArrayList<>());
|
||||
double actual = VoronoiNoise.evaluate(type, seed, numberPoints, point[0], point[1], new Random(), new ArrayList<>());
|
||||
|
||||
assertEquals(
|
||||
Double.doubleToRawLongBits(expected), Double.doubleToRawLongBits(actual),
|
||||
() -> type + ", " + seed + ", " + point[0] + ", " + point[1]
|
||||
);
|
||||
compared++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
assertEquals(3 * 3 * VoronoiNoiseTest.SEEDS.length * VoronoiNoiseTest.coordinates().size(), compared);
|
||||
}
|
||||
|
||||
@Test
|
||||
void otherSelectionsGetTheVanillaPoints() throws ReflectiveOperationException
|
||||
{
|
||||
// Sums every coordinate, so any difference in the points shows in the result.
|
||||
ClosestSelection sum = (x, y, points) -> {
|
||||
double total = 0.0;
|
||||
for (Object point : points) total += ((Coord) point).x() * 3.0 + ((Coord) point).y();
|
||||
return total;
|
||||
};
|
||||
|
||||
ZombieVoronoi voronoi = new ZombieVoronoi(777L, 2, sum, 1.0, 0.0);
|
||||
List<Coord> vanillaPoints = new ArrayList<>();
|
||||
List<Coord> noisePoints = new ArrayList<>(List.of(new Coord(9.0, 9.0)));
|
||||
double expected = VoronoiNoiseTest.vanilla(voronoi, 12.5, -3.25, vanillaPoints);
|
||||
double actual = VoronoiNoise.evaluate(sum, 777L, 2, 12.5, -3.25, new Random(), noisePoints);
|
||||
|
||||
assertEquals(expected, actual);
|
||||
assertEquals(vanillaPoints, noisePoints);
|
||||
assertEquals(18, noisePoints.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void withoutPointsThrowsLikeVanilla()
|
||||
{
|
||||
for (ClosestSelectionType type : ClosestSelectionType.values())
|
||||
{
|
||||
ZombieVoronoi voronoi = new ZombieVoronoi(1L, 0, type, 1.0, 0.0);
|
||||
|
||||
InvocationTargetException vanilla = assertThrows(InvocationTargetException.class, () -> VoronoiNoiseTest.vanilla(voronoi, 1.0, 1.0, new ArrayList<>()));
|
||||
IndexOutOfBoundsException noise = assertThrows(IndexOutOfBoundsException.class, () -> VoronoiNoise.evaluate(type, 1L, 0, 1.0, 1.0, new Random(), new ArrayList<>()));
|
||||
|
||||
assertInstanceOf(IndexOutOfBoundsException.class, vanilla.getCause());
|
||||
assertEquals(vanilla.getCause().getMessage(), noise.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user