Skip to content

Commit

Permalink
Caching biomeId to improve performance + version bump
Browse files Browse the repository at this point in the history
  • Loading branch information
Dioswilson committed Jan 30, 2024
1 parent d3c8a45 commit 5a70d35
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 23 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

group 'com.dioswilson'
version '1.2.3'
version '1.2.4'

repositories {
mavenCentral()
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/dioswilson/SeedFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class SeedFinder extends Thread {

private List<Chunk> witchChunks;
private HashSet<Chunk> neighbourChunks = new HashSet<>();
private int[][] cachedBiomes = new int[256][256];

private Integer witchX;
private Integer witchZ;
Expand Down Expand Up @@ -191,7 +192,7 @@ public void setRandomSeed(int x, int z) {
if (!stop) {
long i = x * 341873128712L + z * 132897987541L + this.seed + this.KONST;
OverworldBiomeSource biomeSource = new OverworldBiomeSource(MCVersion.v1_12_2, this.seed);
WitchSimulator witchSimulator = new WitchSimulator(x, z, i, biomeSource, this.semaphore, this.witchChunks, this.neighbourChunks, this.chunksForSpawning, this.maxAdvancers, this.maxPlayers);
WitchSimulator witchSimulator = new WitchSimulator(x, z, i, biomeSource, this.semaphore, this.witchChunks, this.neighbourChunks, this.chunksForSpawning, this.maxAdvancers, this.maxPlayers, this.cachedBiomes);
witchSimulator.initialize();
}

Expand All @@ -201,7 +202,7 @@ public void setRandomSeed(int x, int z) {
public void setRandomSeedWithAdvancer(int x, int z, int advancers) {//Bad name
long i = x * 341873128712L + z * 132897987541L + this.seed + this.KONST;
OverworldBiomeSource biomeSource = new OverworldBiomeSource(MCVersion.v1_12_2, this.seed);
WitchSimulator witchSimulator = new WitchSimulator(i, biomeSource, this.witchChunks, this.neighbourChunks, this.chunksForSpawning, this.maxPlayers);
WitchSimulator witchSimulator = new WitchSimulator(i, biomeSource, this.witchChunks, this.neighbourChunks, this.chunksForSpawning, this.maxPlayers, this.cachedBiomes);
witchSimulator.getBlocksPositions(4 + advancers, playerX, playerZ);
}

Expand Down
55 changes: 35 additions & 20 deletions src/main/java/com/dioswilson/WitchSimulator.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.dioswilson.minecraft.BlockPos;
import com.dioswilson.minecraft.Chunk;
import com.dioswilson.utils.CopyableRandom;
import com.dioswilson.utils.SpawnPackNode;
import com.seedfinding.mcbiome.source.OverworldBiomeSource;

import javax.swing.*;
Expand Down Expand Up @@ -45,19 +44,24 @@ public class WitchSimulator {
private Set<Chunk> eligibleChunksForSpawning;

private double succesfulSpawns;
SpawnPackNode[] roots;
// private SpawnPackNode[] roots;
private final int[][] cachedBiomes;

public WitchSimulator(int areaMansionX, int areaMansionZ, long seed, OverworldBiomeSource biomeSource, Semaphore semaphore, List<Chunk> witchChunks, Set<Chunk> neighbourChunks, Set<Chunk> chunksForSpawning, int maxAdvancers, int maxPlayers) {
private int minX = Integer.MAX_VALUE;
private int minZ = Integer.MAX_VALUE;

this(seed, biomeSource, witchChunks, neighbourChunks, chunksForSpawning, maxPlayers);
//It's super mega long, should split this into multiple methods, not just constructor, but I am lazy
public WitchSimulator(int areaMansionX, int areaMansionZ, long seed, OverworldBiomeSource biomeSource, Semaphore semaphore, List<Chunk> witchChunks, Set<Chunk> neighbourChunks, Set<Chunk> chunksForSpawning, int maxAdvancers, int maxPlayers, int[][] cachedBiomes) {

this(seed, biomeSource, witchChunks, neighbourChunks, chunksForSpawning, maxPlayers, cachedBiomes);
this.semaphore = semaphore;
this.maxAdvancers = maxAdvancers;
this.areaMansionX = areaMansionX;
this.areaMansionZ = areaMansionZ;

}

public WitchSimulator(long seed, OverworldBiomeSource biomeSource, List<Chunk> witchChunks, Set<Chunk> neighbourChunks, Set<Chunk> chunksForSpawning,/*, int maxAdvancers*/int maxPlayers) {
public WitchSimulator(long seed, OverworldBiomeSource biomeSource, List<Chunk> witchChunks, Set<Chunk> neighbourChunks, Set<Chunk> chunksForSpawning, int maxPlayers, int[][] cachedBiomes) {

int totalChunks = 255 * maxPlayers;

Expand All @@ -77,20 +81,17 @@ public WitchSimulator(long seed, OverworldBiomeSource biomeSource, List<Chunk> w

finalHeightMap = new int[this.witchChunks.size()];

//Maybe improve this initialization
roots = new SpawnPackNode[]{new SpawnPackNode(0, 1, this.biomeSource), new SpawnPackNode(0, 2, this.biomeSource), new SpawnPackNode(0, 3, this.biomeSource), new SpawnPackNode(0, 4, this.biomeSource),};
this.cachedBiomes = cachedBiomes;

for (int i = 0; i < 4; i++) {
roots[i].addChild(new SpawnPackNode(1, 1, this.biomeSource));
roots[i].addChild(new SpawnPackNode(1, 2, this.biomeSource));
roots[i].addChild(new SpawnPackNode(1, 3, this.biomeSource));
roots[i].addChild(new SpawnPackNode(1, 4, this.biomeSource));
for (Chunk chunk : witchChunks) {
int x = (chunk.getX() << 4) - 5;
int z = (chunk.getZ() << 4) - 5;

for (int j = 0; j < 4; j++) {
roots[i].getChildren().get(j).addChild(new SpawnPackNode(2, 1, this.biomeSource));
roots[i].getChildren().get(j).addChild(new SpawnPackNode(2, 2, this.biomeSource));
roots[i].getChildren().get(j).addChild(new SpawnPackNode(2, 3, this.biomeSource));
roots[i].getChildren().get(j).addChild(new SpawnPackNode(2, 4, this.biomeSource));
if (x < minX) {
minX = x;
}
if (z < minZ) {
minZ = z;
}
}

Expand Down Expand Up @@ -254,7 +255,8 @@ private void calculateRandomChunkPositionsFull(int calls, CopyableRandom ogRand)

}

public int simulatePackSpawns(int staticX, int staticY, int staticZ, Chunk chunk, CopyableRandom rand, HashMap<Long, Integer> seeds, Set<BlockPos> positionsTemp, int repetition) {
public int simulatePackSpawns(int staticX, int staticY, int staticZ, Chunk chunk, CopyableRandom
rand, HashMap<Long, Integer> seeds, Set<BlockPos> positionsTemp, int repetition) {
int spawns = 0;
long currSeed;

Expand Down Expand Up @@ -331,7 +333,8 @@ public int simulatePackSpawns(int staticX, int staticY, int staticZ, Chunk chunk
return spawns;
}

public HashMap<String, Long> performSpawning(int packSize, CopyableRandom rand, int staticX, int staticY, int staticZ, Chunk chunk, int spawnedMobs, Set<BlockPos> positionsTemp) {
public HashMap<String, Long> performSpawning(int packSize, CopyableRandom rand, int staticX, int staticY,
int staticZ, Chunk chunk, int spawnedMobs, Set<BlockPos> positionsTemp) {

int x = staticX;
int z = staticZ;
Expand All @@ -355,7 +358,19 @@ public HashMap<String, Long> performSpawning(int packSize, CopyableRandom rand,
list = true;
if ((x > (getX + offSetX)) || (z > (getZ + offSetZ)) || (x < (getX)) || (z < (getZ))) {//Can ignore height

int biomeId = biomeSource.getBiome(x, staticY, z).getId();
int biomeId;
int cacheX = x - minX;
int cacheZ = z - minZ;

if (cachedBiomes[cacheX][cacheZ] != 0) {
biomeId = cachedBiomes[cacheX][cacheZ];

}
else {
biomeId = biomeSource.getBiome(x, staticY, z).getId();
cachedBiomes[cacheX][cacheZ] = biomeId;
}

int weight;
if (biomeId == 2 || biomeId == 17 || biomeId == 130) {//2,17,130=desert;21,22,23=jungle;6,134= swamp, ignoring snow,end,hell,void and mooshroom island
weight = rand.nextInt(515);
Expand Down

0 comments on commit 5a70d35

Please sign in to comment.