ZeusServer 0.0.2

Moved generateChunk function to MapGen class.
Made GenerateChunk only generate points once per chunk, instead of ~16,384 times a chunk ( a lot faster )
master
aurailus 2018-09-23 01:03:10 -07:00
parent beb4638470
commit 6230b49132
2 changed files with 91 additions and 44 deletions

View File

@ -32,8 +32,8 @@ public class ClientThread extends Thread implements Runnable {
pacman.start(); pacman.start();
alive = true; alive = true;
mapGenPool = (ThreadPoolExecutor) Executors.newFixedThreadPool(16); mapGenPool = (ThreadPoolExecutor) Executors.newFixedThreadPool(32);
mapGenPool.setMaximumPoolSize(48); mapGenPool.setMaximumPoolSize(96);
mapGenPool.setKeepAliveTime(32, TimeUnit.SECONDS); mapGenPool.setKeepAliveTime(32, TimeUnit.SECONDS);
} }
@ -63,7 +63,7 @@ public class ClientThread extends Thread implements Runnable {
System.out.println("Generating chunk at position " + position); System.out.println("Generating chunk at position " + position);
var bytes = ChunkSerializer.encodeChunk(generateChunk(position), generateSides(position)); var bytes = ChunkSerializer.encodeChunk(mapGen.generateChunk(position), generateSides(position));
if (bytes == null) return; if (bytes == null) return;
s.append(new String(bytes, StandardCharsets.ISO_8859_1)); s.append(new String(bytes, StandardCharsets.ISO_8859_1));
@ -114,20 +114,6 @@ public class ClientThread extends Thread implements Runnable {
return sides; return sides;
} }
private short[] generateChunk(Vector3i pos) {
var chunk = new short[4096];
for (var i = 0; i < CHUNK_SIZE; i++) {
for (var j = 0; j < CHUNK_SIZE; j++) {
for (var k = 0; k < CHUNK_SIZE; k++) {
short fill = mapGen.getBlock(i + pos.x*CHUNK_SIZE, j + pos.y*CHUNK_SIZE, k + pos.z*CHUNK_SIZE);
chunk[i + CHUNK_SIZE * (j + CHUNK_SIZE * k)] = fill;
}
}
}
return chunk;
}
@Override @Override
public void run() { public void run() {
init(); init();

View File

@ -19,20 +19,80 @@ public class MapGen {
treeNoise = new OpenSimplexNoise(seed/2); treeNoise = new OpenSimplexNoise(seed/2);
} }
public short[] generateChunk(Vector3i pos) {
var chunk = new short[4096];
float[] points = getPoints(pos.x*CHUNK_SIZE, pos.z*CHUNK_SIZE);
for (var i = 0; i < 8; i++) {
for (var j = 0; j < 16; j++) {
for (var k = 0; k < 8; k++) {
short fill = getBlock(i + pos.x*CHUNK_SIZE, j + pos.y*CHUNK_SIZE, k + pos.z*CHUNK_SIZE, new float[] {points[0], points[1], points[2], points[3]});
chunk[i + CHUNK_SIZE * (j + CHUNK_SIZE * k)] = fill;
}
}
}
for (var i = 8; i < 16; i++) {
for (var j = 0; j < 16; j++) {
for (var k = 0; k < 8; k++) {
short fill = getBlock(i + pos.x*CHUNK_SIZE, j + pos.y*CHUNK_SIZE, k + pos.z*CHUNK_SIZE, new float[] {points[1], points[4], points[3], points[5]});
chunk[i + CHUNK_SIZE * (j + CHUNK_SIZE * k)] = fill;
}
}
}
for (var i = 0; i < 8; i++) {
for (var j = 0; j < 16; j++) {
for (var k = 8; k < 16; k++) {
short fill = getBlock(i + pos.x*CHUNK_SIZE, j + pos.y*CHUNK_SIZE, k + pos.z*CHUNK_SIZE, new float[] {points[2], points[3], points[6], points[7]});
chunk[i + CHUNK_SIZE * (j + CHUNK_SIZE * k)] = fill;
}
}
}
for (var i = 8; i < 16; i++) {
for (var j = 0; j < 16; j++) {
for (var k = 8; k < 16; k++) {
short fill = getBlock(i + pos.x*CHUNK_SIZE, j + pos.y*CHUNK_SIZE, k + pos.z*CHUNK_SIZE, new float[] {points[3], points[5], points[7], points[8]});
chunk[i + CHUNK_SIZE * (j + CHUNK_SIZE * k)] = fill;
}
}
}
return chunk;
}
private float[] getPoints(int x, int z) {
int xx = (int)Math.floor((float)x/8);
int zz = (int)Math.floor((float)z/8);
float[] points = new float[9];
points[0] = getNoisePoint(xx*8, zz*8);
points[1] = getNoisePoint((xx+1)*8, zz*8);
points[2] = getNoisePoint(xx*8, (zz+1)*8);
points[3] = getNoisePoint((xx+1)*8, (zz+1)*8);
points[4] = getNoisePoint((xx+2)*8, zz*8);
points[5] = getNoisePoint((xx+2)*8, (zz+1)*8);
points[6] = getNoisePoint(xx*8, (zz+2)*8);
points[7] = getNoisePoint((xx+1)*8, (zz+2)*8);
points[8] = getNoisePoint((xx+2)*8, (zz+2)*8);
return points;
}
public short getBlock(int x, int y, int z) { public short getBlock(int x, int y, int z) {
float[] points = getPoints(x, z);
return getBlock(x, y, z, points);
}
private short getBlock(int x, int y, int z, float[] points) {
int xx = (int)Math.floor((float)x/8); int xx = (int)Math.floor((float)x/8);
int zz = (int)Math.floor((float)z/8); int zz = (int)Math.floor((float)z/8);
float v = ((float)x/8) - xx; float v = ((float)x/8) - xx;
float u = ((float)z/8) - zz; float u = ((float)z/8) - zz;
float point00 = getNoisePoint(xx*8, zz*8); float x1 = lerp(points[0], points[2], u);
float point10 = getNoisePoint((xx+1)*8, zz*8); float x2 = lerp(points[1], points[3], u);
float point01 = getNoisePoint(xx*8, (zz+1)*8);
float point11 = getNoisePoint((xx+1)*8, (zz+1)*8);
float x1 = lerp(point00, point01, u);
float x2 = lerp(point10, point11, u);
float average = lerp(x1, x2, v); float average = lerp(x1, x2, v);
@ -40,32 +100,33 @@ public class MapGen {
if (y + 1 - average < 0) return 2; if (y + 1 - average < 0) return 2;
if (y - average < 0) return 1; if (y - average < 0) return 1;
if (y - average < 5) // if (y - average < 5)
if (Math.pow(treeNoise.eval(x, z), 2) > 0.6) return 11; // if (Math.pow(treeNoise.eval(x, z), 2) > 0.6) return 11;
//
if (y - average < 5 && y - average > 3) { // if (y - average < 5 && y - average > 3) {
for (var i = -2; i < 3; i++) { // for (var i = -2; i < 3; i++) {
for (var j = -2; j < 3; j++) { // for (var j = -2; j < 3; j++) {
if (i != 0 || j != 0) { // if (i != 0 || j != 0) {
if (Math.pow(treeNoise.eval(x + i, z + j), 2) > 0.6) return 12; // if (Math.pow(treeNoise.eval(x + i, z + j), 2) > 0.6) return 12;
} // }
} // }
} // }
} // }
//
if (y - average < 7 && y - average > 5) { // if (y - average < 7 && y - average > 5) {
for (var i = -1; i < 2; i++) { // for (var i = -1; i < 2; i++) {
for (var j = -1; j < 2; j++) { // for (var j = -1; j < 2; j++) {
if (Math.pow(treeNoise.eval(x + i, z + j), 2) > 0.6) return 12; // if (Math.pow(treeNoise.eval(x + i, z + j), 2) > 0.6) return 12;
} // }
} // }
} // }
if (y - 1 - average < 0) { if (y - 1 - average < 0) {
if (Math.random() > 0.2) { if (Math.random() > 0.2) {
return (short)(4 + Math.round(Math.random() * 4)); return (short)(4 + Math.round(Math.random() * 4));
} }
} }
return 0; return 0;
} }