mirror of
https://github.com/PixelGuys/Cubyz.git
synced 2025-08-04 03:37:59 -04:00
Make Chunk generation (0.5-1ms)/(5-10%) faster
This commit is contained in:
parent
9e89e51181
commit
524e704e34
@ -31,14 +31,40 @@ public class Chunk {
|
|||||||
private static Block bedrock = br.getByID("cubyz:bedrock");
|
private static Block bedrock = br.getByID("cubyz:bedrock");
|
||||||
|
|
||||||
// Ores:
|
// Ores:
|
||||||
private static ArrayList<Ore> ores = new ArrayList<>();
|
private static Ore [] ores;
|
||||||
|
private static float [] oreChances;
|
||||||
|
private static int [] oreHeights;
|
||||||
static {
|
static {
|
||||||
ores.add((Ore) br.getByID("cubyz:coal_ore"));
|
ArrayList<Ore> oress = new ArrayList<>();
|
||||||
ores.add((Ore) br.getByID("cubyz:iron_ore"));
|
oress.add((Ore) br.getByID("cubyz:coal_ore"));
|
||||||
ores.add((Ore) br.getByID("cubyz:ruby_ore"));
|
oress.add((Ore) br.getByID("cubyz:iron_ore"));
|
||||||
ores.add((Ore) br.getByID("cubyz:gold_ore"));
|
oress.add((Ore) br.getByID("cubyz:ruby_ore"));
|
||||||
ores.add((Ore) br.getByID("cubyz:diamond_ore"));
|
oress.add((Ore) br.getByID("cubyz:gold_ore"));
|
||||||
ores.add((Ore) br.getByID("cubyz:emerald_ore"));
|
oress.add((Ore) br.getByID("cubyz:diamond_ore"));
|
||||||
|
oress.add((Ore) br.getByID("cubyz:emerald_ore"));
|
||||||
|
ores = oress.toArray(new Ore[0]);
|
||||||
|
oreChances = new float[ores.length+1];
|
||||||
|
oreHeights = new int[ores.length];
|
||||||
|
for(int i = 0; i < ores.length; i++) {
|
||||||
|
oreHeights[i] = ores[i].getHeight();
|
||||||
|
}
|
||||||
|
// (Selection-)Sort the ores by height to accelerate selectOre
|
||||||
|
for(int i = 0; i < oreHeights.length; i++) {
|
||||||
|
int lowest = i;
|
||||||
|
for(int j = i+1; j < oreHeights.length; j++) {
|
||||||
|
if(oreHeights[j] < oreHeights[lowest])
|
||||||
|
lowest = j;
|
||||||
|
}
|
||||||
|
Ore ore = ores[lowest];
|
||||||
|
int height = oreHeights[lowest];
|
||||||
|
ores[lowest] = ores[i];
|
||||||
|
oreHeights[lowest] = oreHeights[i];
|
||||||
|
ores[i] = ore;
|
||||||
|
oreHeights[i] = height;
|
||||||
|
}
|
||||||
|
for(int i = 0; i < ores.length; i++) {
|
||||||
|
oreChances[i+1] = oreChances[i] + ores[i].getChance();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Liquids:
|
// Liquids:
|
||||||
@ -287,13 +313,13 @@ public class Chunk {
|
|||||||
|
|
||||||
// This function only allows a less than 50% of the underground to be ores.
|
// This function only allows a less than 50% of the underground to be ores.
|
||||||
public BlockInstance selectOre(float rand, int height) {
|
public BlockInstance selectOre(float rand, int height) {
|
||||||
float chance1 = 0.0F;
|
if(rand >= oreChances[oreHeights.length])
|
||||||
float chance2 = 0.0F;
|
return new BlockInstance(stone);
|
||||||
for (Ore ore : ores) {
|
for (int i = oreChances.length - 2; i >= 0; i--) {
|
||||||
chance2 += ore.getChance();
|
if(height > oreHeights[i])
|
||||||
if(height < ore.getHeight() && rand > chance1 && rand < chance2)
|
break;
|
||||||
return new BlockInstance(ore);
|
if(rand >= oreChances[i])
|
||||||
chance1 += ore.getChance();
|
return new BlockInstance(ores[i]);
|
||||||
}
|
}
|
||||||
return new BlockInstance(stone);
|
return new BlockInstance(stone);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user