Added try to prevent crashes during world generation

This commit is contained in:
Francesco Macagno 2015-08-20 22:41:37 -07:00
parent 3e8d774941
commit 19372a61a6

View file

@ -21,19 +21,19 @@ public class SpaceWorldGenerator implements IWorldGenerator {
// Radius of simple moon
public final int MOON_RADIUS = 32;
public final int MOON_CORE_RADIUS = 10;
// Star radius
public final int RED_DWARF_RADIUS = 42;
public final int YELLOW_GIANT_RADIUS = 64;
public final int YELLOW_SUPERGIANT_RADIUS = 80;
// Upper than 200 nothing should generate naturally (safe place)
public static int Y_LIMIT_HARD_MAX = 200;
// Upper than 128 almost nothing will be generated
public static int Y_LIMIT_SOFT_MAX = 128;
// Lower limit
public static int Y_LIMIT_SOFT_MIN = 55;
/**
* Generator for chunk
*
@ -46,55 +46,59 @@ public class SpaceWorldGenerator implements IWorldGenerator {
*/
@Override
public void generate(Random random, int chunkX, int chunkZ, World world, IChunkProvider chunkGenerator, IChunkProvider chunkProvider) {
if (world.provider.dimensionId != WarpDriveConfig.G_SPACE_DIMENSION_ID) {
return;
}
int x = (chunkX * 16) + (5 - random.nextInt(10));
int z = (chunkZ * 16) + (5 - random.nextInt(10));
if (WarpDriveConfig.G_SPACE_WORLDBORDER_BLOCKS > 0 && (Math.abs(x) > WarpDriveConfig.G_SPACE_WORLDBORDER_BLOCKS || Math.abs(z) > WarpDriveConfig.G_SPACE_WORLDBORDER_BLOCKS)) {
return;
}
int y = Y_LIMIT_SOFT_MIN + random.nextInt(Y_LIMIT_SOFT_MAX - Y_LIMIT_SOFT_MIN);
// Moon setup
if (random.nextInt(700) == 1)
generateMoon(world, x, y, z, null);
// Simple asteroids
else if (random.nextInt(150) == 1) {
generateAsteroidOfBlock(world, x, y, z, 6, 11, null, 0);
// Random asteroid of block
} else if (random.nextInt(400) == 1) {
generateRandomAsteroid(world, x, y, z, 6, 11);
if (random.nextBoolean()) {
generateGasCloudOfColor(world, x, y, z, 6, 11, null);
try {
if (world.provider.dimensionId != WarpDriveConfig.G_SPACE_DIMENSION_ID) {
return;
}
} else if (random.nextInt(200) == 1) {// Ice asteroid
generateAsteroidOfBlock(world, x, y, z, 6, 11, Blocks.ice, 0);
} else if (random.nextInt(500) == 1) {// Asteroid field
generateAsteroidField(world, x, y, z);
} else if (random.nextInt(1400) == 1) {// Diamond asteroid
generateAsteroidOfBlock(world, x, y, z, 3, 2, Blocks.diamond_ore, 0);
// Diamond block core
world.setBlock(x, y, z, Blocks.diamond_block, 0, 2);
if (random.nextBoolean()) {
generateGasCloudOfColor(world, x, y, z, 6, 11, null);
int x = (chunkX * 16) + (5 - random.nextInt(10));
int z = (chunkZ * 16) + (5 - random.nextInt(10));
if (WarpDriveConfig.G_SPACE_WORLDBORDER_BLOCKS > 0 && (Math.abs(x) > WarpDriveConfig.G_SPACE_WORLDBORDER_BLOCKS || Math.abs(z) > WarpDriveConfig.G_SPACE_WORLDBORDER_BLOCKS)) {
return;
}
int y = Y_LIMIT_SOFT_MIN + random.nextInt(Y_LIMIT_SOFT_MAX - Y_LIMIT_SOFT_MIN);
// Moon setup
if (random.nextInt(700) == 1)
generateMoon(world, x, y, z, null);
// Simple asteroids
else if (random.nextInt(150) == 1) {
generateAsteroidOfBlock(world, x, y, z, 6, 11, null, 0);
// Random asteroid of block
} else if (random.nextInt(400) == 1) {
generateRandomAsteroid(world, x, y, z, 6, 11);
if (random.nextBoolean()) {
generateGasCloudOfColor(world, x, y, z, 6, 11, null);
}
} else if (random.nextInt(200) == 1) {// Ice asteroid
generateAsteroidOfBlock(world, x, y, z, 6, 11, Blocks.ice, 0);
} else if (random.nextInt(500) == 1) {// Asteroid field
generateAsteroidField(world, x, y, z);
} else if (random.nextInt(1400) == 1) {// Diamond asteroid
generateAsteroidOfBlock(world, x, y, z, 3, 2, Blocks.diamond_ore, 0);
// Diamond block core
world.setBlock(x, y, z, Blocks.diamond_block, 0, 2);
if (random.nextBoolean()) {
generateGasCloudOfColor(world, x, y, z, 6, 11, null);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void generateMoon(World world, int x, int y, int z, final String moonName) {
DeployableStructure moon = StructureManager.getMoon(world.rand, moonName);
WarpDrive.logger.info("Generating moon (class " + moon + ") at " + x + " " + y + " " + z);
moon.generate(world, world.rand, x, y, z);
}
public void generateStar(World world, int x, int y, int z, final String starName) {
DeployableStructure star = StructureManager.getStar(world.rand, starName);
WarpDrive.logger.info("Generating star (class " + star + ") at " + x + " " + y + " " + z);
star.generate(world, world.rand, x, y, z);
}
private static void generateSmallShip(World world, int x, int y, int z, int jitter) {
int x2 = x + (((world.rand.nextBoolean()) ? -1 : 1) * world.rand.nextInt(jitter));
int y2 = y + (((world.rand.nextBoolean()) ? -1 : 1) * world.rand.nextInt(jitter));
@ -102,7 +106,7 @@ public class SpaceWorldGenerator implements IWorldGenerator {
WarpDrive.logger.info("Generating small ship at " + x2 + "," + y2 + "," + z2);
new WorldGenSmallShip(world.rand.nextBoolean()).generate(world, world.rand, x2, y2, z2);
}
private static void generateStation(World world, int x, int y, int z, int jitter) {
int x2 = x + (((world.rand.nextBoolean()) ? -1 : 1) * world.rand.nextInt(jitter));
int y2 = y + (((world.rand.nextBoolean()) ? -1 : 1) * world.rand.nextInt(jitter));
@ -110,36 +114,36 @@ public class SpaceWorldGenerator implements IWorldGenerator {
WarpDrive.logger.info("Generating small ship at " + x2 + "," + y2 + "," + z2);
new WorldGenStation(world.rand.nextBoolean()).generate(world, world.rand, x2, y2, z2);
}
public static void generateRandomAsteroid(World world, int x, int y, int z, int asteroidSizeMax, int centerRadiusMax) {
generateAsteroidOfBlock(world, x, y, z, asteroidSizeMax, centerRadiusMax, null, 0);
}
private static float binomialRandom(World world) {
float linear = world.rand.nextFloat();
// ideal sphere repartition = x ^ 0.5 (sqrt)
// Dilution but slow to compute = 0.5 * ( x ^ 0.3 + 1 + (x - 1) ^ 3 )
// Optimized 'pushed out' form = 1.25 - 0.625 / (0.5 + 2 * x)
// Natural sphere with ring = (1 - x ^ 2.5) * x ^ 0.5 + x ^ 4
// rectangular approach: return 0.5F * linear + 0.5F * linear * linear;
return 1.25F - 0.625F / (0.5F + 2.0F * linear);
}
public static void generateAsteroidField(World world, int x, int y1, int z) {
LocalProfiler.start("SpaceWorldGenerator.generateAsteroidField");
// 6.0.1 au = 120 radius with 60 to 140 big + 60 to 140 small + 5 to 13 gaz
// 45238 blocks surface with 120 to 280 asteroids => 161 to 376 blocks per asteroid (big & small)
// 6.0.2 av big = 80 to 180 radius with 40 to 90 big + 80 to 200 small + 5 to 13 gaz
// 20106 to 101787 surface with 120 to 290 asteroids => 69 to 848 blocks per asteroid
// 6.0.2 av small = 30 to 80 radius with 2 to 22 big + 15 to 75 small + 0 to 3 gaz
// 2827 to 20106 surface with 17 to 97 asteroids => 29 to 1182 blocks per asteroid
// random distanced one = 89727 surface 256 asteroids => 350 blocks per asteroid
/*
boolean isBig = world.rand.nextInt(3) == 1;
int numOfBigAsteroids, numOfSmallAsteroids, numOfClouds, maxDistance, maxHeight;
@ -157,7 +161,7 @@ public class SpaceWorldGenerator implements IWorldGenerator {
maxHeight = 30 + world.rand.nextInt(30);
}
/**/
float surfacePerAsteroid = 80.0F + world.rand.nextFloat() * 300;
int maxDistance = 30 + world.rand.nextInt(170);
int maxDistanceBig = Math.round(maxDistance * (0.6F + 0.2F * world.rand.nextFloat()));
@ -172,7 +176,7 @@ public class SpaceWorldGenerator implements IWorldGenerator {
int y2 = Math.min(Y_LIMIT_HARD_MAX - maxHeight, Math.max(y1, maxHeight));
WarpDrive.logger.info("Generating asteroid field at " + x + "," + y2 + "," + z + " qty " + numOfBigAsteroids + ", " + numOfSmallAsteroids + ", "
+ numOfClouds + " over " + maxDistance + ", " + maxHeight + " surfacePerAsteroid " + String.format("%.1f", surfacePerAsteroid));
// Setting up of big asteroids
for (int i = 1; i <= numOfBigAsteroids; i++) {
float binomial = binomialRandom(world);
@ -180,20 +184,20 @@ public class SpaceWorldGenerator implements IWorldGenerator {
double yawn = world.rand.nextFloat() * Math.PI;
float horizontalRange = Math.max(6.0F, binomial * maxDistanceBig);
float verticalRange = Math.max(3.0F, binomial * maxHeight);
int aX = (int) (x + Math.round(horizontalRange * Math.cos(bearing)));
int aY = (int) (y2 + Math.round(verticalRange * Math.cos(yawn)));
int aZ = (int) (z + Math.round(horizontalRange * Math.sin(bearing)));
if (WarpDriveConfig.LOGGING_WORLDGEN) {
System.out.println(String.format("Big asteroid: %.3f %.3f r %.3f r makes %3d, %3d, %3d",
new Object[] { Double.valueOf(binomial), Double.valueOf(bearing), Double.valueOf(yawn), Integer.valueOf(aX), Integer.valueOf(aY), Integer.valueOf(aZ) }));
}
// Place an asteroid
generateRandomAsteroid(world, aX, aY, aZ, 4, 6);
}
// Setting up small asteroids
for (int i = 1; i <= numOfSmallAsteroids; i++) {
float binomial = binomialRandom(world);
@ -201,11 +205,11 @@ public class SpaceWorldGenerator implements IWorldGenerator {
double yawn = world.rand.nextFloat() * Math.PI;
float horizontalRange = Math.max(6.0F, binomial * maxDistanceSmall);
float verticalRange = Math.max(3.0F, binomial * maxHeight);
int aX = (int) (x + Math.round(horizontalRange * Math.cos(bearing)));
int aY = (int) (y2 + Math.round(verticalRange * Math.cos(yawn)));
int aZ = (int) (z + Math.round(horizontalRange * Math.sin(bearing)));
// Placing
if (world.rand.nextInt(400) != 1) {
generateRandomAsteroid(world, aX, aY, aZ, 3, 3);
@ -217,7 +221,7 @@ public class SpaceWorldGenerator implements IWorldGenerator {
}
}
}
// Setting up gas clouds
for (int i = 1; i <= numOfClouds; i++) {
float binomial = binomialRandom(world);
@ -225,20 +229,20 @@ public class SpaceWorldGenerator implements IWorldGenerator {
double yawn = world.rand.nextFloat() * Math.PI;
float horizontalRange = Math.max(6.0F, binomial * maxDistanceBig);
float verticalRange = Math.max(3.0F, binomial * maxHeight);
int aX = (int) (x + Math.round(horizontalRange * Math.cos(bearing)));
int aY = (int) (y2 + Math.round(verticalRange * Math.cos(yawn)));
int aZ = (int) (z + Math.round(horizontalRange * Math.sin(bearing)));
// Placing
if (world.rand.nextBoolean()) {
generateGasCloudOfColor(world, aX, aY, aZ, 12, 15, null);
}
}
LocalProfiler.stop();
}
/**
* Gas cloud generator
*
@ -261,13 +265,13 @@ public class SpaceWorldGenerator implements IWorldGenerator {
if (centerRadiusMax != 0)
centerRadius = Math.min(centerRadiusMax, centerRadius);
final int CENTER_SHIFT = 2; // Offset from center of central ball
DeployableStructure cloud = StructureManager.getGasCloud(world.rand, type);
if (cloud == null) {
WarpDrive.logger.error("No gaz cloud defined, cancelling world generation");
return;
}
for (int i = 1; i <= cloudSize; i++) {
int radius = 2 + world.rand.nextInt(centerRadius);
int newX = x + (((world.rand.nextBoolean()) ? -1 : 1) * world.rand.nextInt(CENTER_SHIFT + centerRadius / 2));
@ -276,7 +280,7 @@ public class SpaceWorldGenerator implements IWorldGenerator {
((Orb)cloud).generate(world, world.rand, newX, newY, newZ, radius);
}
}
/**
* Asteroid of block generator
*
@ -316,14 +320,14 @@ public class SpaceWorldGenerator implements IWorldGenerator {
}
*/
}
public static void generateSphereDirect(
World world, int xCoord, int yCoord, int zCoord, Orb orb, Random rand) {
World world, int xCoord, int yCoord, int zCoord, Orb orb, Random rand) {
double radiusC = orb.getHeight() / 2 + 0.5D; // Radius from center of block
double radiusSq = radiusC * radiusC; // Optimization to avoid sqrts...
// sphere
int ceilRadius = (int) Math.ceil(radiusC);
// Pass the cube and check points for sphere equation x^2 + y^2 + z^2 = r^2
for (int x = 0; x <= ceilRadius; x++) {
double x2 = (x + 0.5D) * (x + 0.5D);
@ -333,15 +337,15 @@ World world, int xCoord, int yCoord, int zCoord, Orb orb, Random rand) {
double z2 = (z + 0.5D) * (z + 0.5D);
int dSq = (int) Math.sqrt(x2 + y2 + z2); // Distance from current position
//TODO: Find quicker form of sqrt
// Skip too far blocks
if (dSq > radiusSq) {
continue;
}
// Place blocks
// cheat by using axial symmetry so we don't create random numbers too frequently
world.setBlock(xCoord + x, yCoord + y, zCoord + z, orb.getBlockForRadius(rand, dSq), 0, 2);
world.setBlock(xCoord - x, yCoord + y, zCoord + z, orb.getBlockForRadius(rand, dSq), 0, 2);
world.setBlock(xCoord + x, yCoord - y, zCoord + z, orb.getBlockForRadius(rand, dSq), 0, 2);
@ -350,12 +354,12 @@ World world, int xCoord, int yCoord, int zCoord, Orb orb, Random rand) {
world.setBlock(xCoord + x, yCoord - y, zCoord - z, orb.getBlockForRadius(rand, dSq), 0, 2);
world.setBlock(xCoord - x, yCoord + y, zCoord - z, orb.getBlockForRadius(rand, dSq), 0, 2);
world.setBlock(xCoord - x, yCoord - y, zCoord - z, orb.getBlockForRadius(rand, dSq), 0, 2);
}
}
}
}
private static double lengthSq(double x, double y, double z) {
return (x * x) + (y * y) + (z * z);
}