Made temperature/humidity generate on world gen

This commit is contained in:
josua 2020-08-27 09:12:57 +10:00
parent 877c5371a0
commit a6b8edcd67
11 changed files with 184 additions and 48 deletions

View File

@ -228,6 +228,7 @@ public class DisplayLighting
public static void updateLighting() public static void updateLighting()
{ {
Lighting lighting = getLighting(); Lighting lighting = getLighting();
Layer layer = Main.world.getLayer();
// Copy the pixels // Copy the pixels
float[] pixels = new float[lighting.p.length]; float[] pixels = new float[lighting.p.length];
@ -235,7 +236,17 @@ public class DisplayLighting
pixels[i] = lighting.p[i]; pixels[i] = lighting.p[i];
} }
Layer layer = Main.world.getLayer(); for(int x2=0;x2<lighting.w;x2++) {
for(int y2=0;y2<lighting.h;y2++)
{
int i = (x2 + y2 * lighting.w) * 3;
// Send temperature and humidity data to the image
pixels[i+1] = (float)layer.getTemperature(new Vec2i(x2 + lighting.x * 16, y2 + lighting.y * 16));
pixels[i+2] = (float)layer.getHumidity(new Vec2i(x2 + lighting.x * 16, y2 + lighting.y * 16));
}
}
Vec2d ppos = Main.player.getPos().xz(); Vec2d ppos = Main.player.getPos().xz();
calculateEntityLighting(layer, lighting, Main.player, pixels); calculateEntityLighting(layer, lighting, Main.player, pixels);
@ -334,21 +345,6 @@ public class DisplayLighting
lighting.x = x; lighting.x = x;
lighting.y = y; lighting.y = y;
Layer layer = Main.world.getLayer();
for(int x2=0;x2<lighting.w;x2++) {
for(int y2=0;y2<lighting.h;y2++)
{
int i = (x2 + y2 * lighting.w) * 3;
// Send temperature and humidity data to the image
pixels[i+1] = (float)layer.layergen.getTemperatureStatic(
layer, new Vec2d(x2 + lighting.x * 16, y2 + lighting.y * 16));
pixels[i+2] = (float)layer.layergen.getHumidity(
layer, new Vec2d(x2 + lighting.x * 16, y2 + lighting.y * 16));
}
}
setLighting(lighting); setLighting(lighting);
} }
} }

View File

@ -204,10 +204,10 @@ public class DisplayRenderUI
if(ChunkEventHandler.loaded) if(ChunkEventHandler.loaded)
{ {
Text.render("temperature: " + dec.format( Text.render("temperature: " + dec.format(
layer.layergen.getTemperatureStatic(layer, Main.player.getPos().xz())), matrix); layer.getTemperature(Main.player.getPos().xz().toInt())), matrix);
matrix = Matrix4.multiply(matrix, Matrix4.translate(0, -0.35, 0)); matrix = Matrix4.multiply(matrix, Matrix4.translate(0, -0.35, 0));
Text.render("humidity: " + dec.format( Text.render("humidity: " + dec.format(
layer.layergen.getHumidity(layer, Main.player.getPos().xz())), matrix); layer.getHumidity(Main.player.getPos().xz().toInt())), matrix);
matrix = Matrix4.multiply(matrix, Matrix4.translate(0, -0.7, 0)); matrix = Matrix4.multiply(matrix, Matrix4.translate(0, -0.7, 0));
Text.render("x: "+dec.format(pos.x), matrix); Text.render("x: "+dec.format(pos.x), matrix);

View File

@ -204,7 +204,7 @@ public class EntityPlayer extends Entity implements
super.tick(chunk, layer); super.tick(chunk, layer);
double temp_diff = MathHelpers.biggest( double temp_diff = MathHelpers.biggest(
layer.layergen.getTemperatureDynamic(layer, getPos().xz()), layer.layergen.getTemperature(layer, getPos().xz()),
chunk.getLightLevel(getPos().xz().toInt()) * 0.6) - temperature; chunk.getLightLevel(getPos().xz().toInt()) * 0.6) - temperature;
temperature += temp_diff / 1000; temperature += temp_diff / 1000;

View File

@ -53,8 +53,14 @@ public class Map2D<T> implements Iterable<Map2DElement<T>>
} }
// Loop over the elements // Loop over the elements
for(Map2DElement<T> e : this.elements) for(int i=0;i<this.elements.size();i++)
{ {
Map2DElement<T> e = this.elements.get(i);
if(e == null) {
continue;
}
// Send back the object if these positions are the same // Send back the object if these positions are the same
if(e.pos.equal(pos)) { if(e.pos.equal(pos)) {
element_last = e; element_last = e;

View File

@ -99,6 +99,8 @@ public class Chunk implements IBdfClassManager
short[] tf = nl.get("tilesFront").getShortArray(); short[] tf = nl.get("tilesFront").getShortArray();
byte[] mb = nl.get("metaBack").getByteArray(); byte[] mb = nl.get("metaBack").getByteArray();
byte[] mf = nl.get("metaFront").getByteArray(); byte[] mf = nl.get("metaFront").getByteArray();
byte[] a_t = nl.get("temperature").getByteArray();
byte[] a_h = nl.get("humidity").getByteArray();
for(int i=0;i<CHUNK_INDEX;i++) for(int i=0;i<CHUNK_INDEX;i++)
{ {
@ -106,6 +108,8 @@ public class Chunk implements IBdfClassManager
tiles_front[i] = Tiles.tiles.get(tf[i]); tiles_front[i] = Tiles.tiles.get(tf[i]);
tiles_back_meta[i] = mb[i]; tiles_back_meta[i] = mb[i];
tiles_front_meta[i] = mf[i]; tiles_front_meta[i] = mf[i];
tiles_temperature[i] = a_t[i];
tiles_humidity[i] = a_h[i];
tiles_lighting[i] = 0; tiles_lighting[i] = 0;
} }
@ -140,6 +144,8 @@ public class Chunk implements IBdfClassManager
nl.set("tilesFront", bdf.newObject().setShortArray(tf)); nl.set("tilesFront", bdf.newObject().setShortArray(tf));
nl.set("metaBack", bdf.newObject().setByteArray(tiles_back_meta)); nl.set("metaBack", bdf.newObject().setByteArray(tiles_back_meta));
nl.set("metaFront", bdf.newObject().setByteArray(tiles_front_meta)); nl.set("metaFront", bdf.newObject().setByteArray(tiles_front_meta));
nl.set("temperature", bdf.newObject().setByteArray(tiles_temperature));
nl.set("humidity", bdf.newObject().setByteArray(tiles_humidity));
// Save all the saveable entity data // Save all the saveable entity data
BdfArray bdf_entities = bdf.newArray(); BdfArray bdf_entities = bdf.newArray();
@ -505,7 +511,7 @@ public class Chunk implements IBdfClassManager
return null; return null;
} }
public void setTemperature(double v, Vec2i pos) public void setTemperature(Vec2i pos, double v)
{ {
// Get the id // Get the id
Vec2i cpos = new Vec2i(0, 0); Vec2i cpos = new Vec2i(0, 0);
@ -513,14 +519,14 @@ public class Chunk implements IBdfClassManager
cpos.y = MathHelpers.mod(pos.y, CHUNK_SIZE.my); cpos.y = MathHelpers.mod(pos.y, CHUNK_SIZE.my);
int id = cpos.getId(CHUNK_SIZE); int id = cpos.getId(CHUNK_SIZE);
setTemperature(v, id); setTemperature(id, v);
} }
public void setTemperature(double v, int id) { public void setTemperature(int id, double v) {
tiles_temperature[id] = (byte)(v * 127); tiles_temperature[id] = (byte)(v * 255);
} }
public void setHumidity(double v, Vec2i pos) public double getTemperature(Vec2i pos)
{ {
// Get the id // Get the id
Vec2i cpos = new Vec2i(0, 0); Vec2i cpos = new Vec2i(0, 0);
@ -528,11 +534,41 @@ public class Chunk implements IBdfClassManager
cpos.y = MathHelpers.mod(pos.y, CHUNK_SIZE.my); cpos.y = MathHelpers.mod(pos.y, CHUNK_SIZE.my);
int id = cpos.getId(CHUNK_SIZE); int id = cpos.getId(CHUNK_SIZE);
setHumidity(v, id); return getTemperature(id);
} }
public void setHumidity(double v, int id) { public double getTemperature(int id) {
tiles_humidity[id] = (byte)(v * 127); return (0xff & tiles_temperature[id]) / 255.0;
}
public void setHumidity(Vec2i pos, double v)
{
// Get the id
Vec2i cpos = new Vec2i(0, 0);
cpos.x = MathHelpers.mod(pos.x, CHUNK_SIZE.mx);
cpos.y = MathHelpers.mod(pos.y, CHUNK_SIZE.my);
int id = cpos.getId(CHUNK_SIZE);
setHumidity(id, v);
}
public void setHumidity(int id, double v) {
tiles_humidity[id] = (byte)(v * 255);
}
public double getHumidity(Vec2i pos)
{
// Get the id
Vec2i cpos = new Vec2i(0, 0);
cpos.x = MathHelpers.mod(pos.x, CHUNK_SIZE.mx);
cpos.y = MathHelpers.mod(pos.y, CHUNK_SIZE.my);
int id = cpos.getId(CHUNK_SIZE);
return getHumidity(id);
}
public double getHumidity(int id) {
return (0xff & tiles_humidity[id]) / 255.0;
} }
public void setBackTile(TileState tile, Vec2i pos) public void setBackTile(TileState tile, Vec2i pos)

View File

@ -9,6 +9,7 @@ import gl_engine.vec.Vec2d;
import gl_engine.vec.Vec2i; import gl_engine.vec.Vec2i;
import projectzombie.display.Camera; import projectzombie.display.Camera;
import projectzombie.entity.Entity; import projectzombie.entity.Entity;
import projectzombie.entity.tileentity.TileEntity;
import projectzombie.util.math.TileState; import projectzombie.util.math.TileState;
public class ChunkEmpty extends Chunk public class ChunkEmpty extends Chunk
@ -130,6 +131,56 @@ public class ChunkEmpty extends Chunk
@Override @Override
public void tickRandomly() { public void tickRandomly() {
} }
@Override
public double getHumidity(int id) {
return 0;
}
@Override
public double getHumidity(Vec2i pos) {
return 0;
}
@Override
public double getTemperature(int id) {
return 0;
}
@Override
public void createTileEntity(Vec2i pos, TileEntity te) {
}
@Override
public void destroyTileEntity(Vec2i pos) {
}
@Override
public double getTemperature(Vec2i pos) {
return 0;
}
@Override
public void setHumidity(int id, double v) {
}
@Override
public void setHumidity(Vec2i pos, double v) {
}
@Override
public void setTemperature(int id, double v) {
}
@Override
public TileEntity getTileEntity(Vec2i pos) {
return null;
}
@Override
public void setTemperature(Vec2i pos, double v) {
}
@Override @Override
public void clearDirty() { public void clearDirty() {
} }

View File

@ -149,6 +149,42 @@ public class Layer implements IBdfClassManager
chunks.get(c_pos).destroyTileEntity(pos); chunks.get(c_pos).destroyTileEntity(pos);
} }
public double getTemperature(Vec2i pos)
{
// Get the chunk pos
Vec2i c_pos = getChunkPosFromPos(pos);
// Get the temperature from the chunk
return chunks.get(c_pos).getTemperature(pos);
}
public double getHumidity(Vec2i pos)
{
// Get the chunk pos
Vec2i c_pos = getChunkPosFromPos(pos);
// Get the humidity from the chunk
return chunks.get(c_pos).getHumidity(pos);
}
public void setTemperature(Vec2i pos, double v)
{
// Get the chunk pos
Vec2i c_pos = getChunkPosFromPos(pos);
// Get the temperature from the chunk
chunks.get(c_pos).setTemperature(pos, v);
}
public void setHumidity(Vec2i pos, double v)
{
// Get the chunk pos
Vec2i c_pos = getChunkPosFromPos(pos);
// Get the humidity from the chunk
chunks.get(c_pos).setHumidity(pos, v);
}
public void setBackTile(TileState tile, Vec2i pos) public void setBackTile(TileState tile, Vec2i pos)
{ {
// Get the chunk pos // Get the chunk pos

View File

@ -49,6 +49,9 @@ public class LayerGenBossArena extends LayerGen implements LayerGenRememberPlaye
// Get the tile position // Get the tile position
Vec2i tpos = new Vec2i(pos.x * 16 + x, pos.y * 16 + y); Vec2i tpos = new Vec2i(pos.x * 16 + x, pos.y * 16 + y);
chunk.setTemperature(tpos, 0.8);
chunk.setHumidity(tpos, 0);
// Arena walls // Arena walls
if(layer.noise_gens[0].eval(tpos.x / 8.0, tpos.y / 8.0) > tpos.distance(new Vec2i(0, 0)) / 10 - 1.25) { if(layer.noise_gens[0].eval(tpos.x / 8.0, tpos.y / 8.0) > tpos.distance(new Vec2i(0, 0)) / 10 - 1.25) {
chunk.setBackTile(Tiles.STONE.getDefaultState(), tpos); chunk.setBackTile(Tiles.STONE.getDefaultState(), tpos);

View File

@ -87,6 +87,9 @@ public class LayerGenCaves extends LayerGen
double temperature = getTemperatureStatic(layer, new Vec2d(cx, cy)); double temperature = getTemperatureStatic(layer, new Vec2d(cx, cy));
double humidity = getHumidity(layer, new Vec2d(cx, cy)); double humidity = getHumidity(layer, new Vec2d(cx, cy));
chunk.setTemperature(pos, temperature);
chunk.setHumidity(pos, humidity);
// Get the noise value and the position vector // Get the noise value and the position vector
double noise_n = 100 - MathHelpers.positive( layer.noise_gens[2].eval(cx/16.0, cy/16.0) * 100 ); double noise_n = 100 - MathHelpers.positive( layer.noise_gens[2].eval(cx/16.0, cy/16.0) * 100 );
double noise_c = layer.noise_gens[3].eval(cx/32.0, cy/32.0); double noise_c = layer.noise_gens[3].eval(cx/32.0, cy/32.0);

View File

@ -65,8 +65,8 @@ public class LayerGenEarth extends LayerGen
layer.noise_gens = new NoiseGenerator[] layer.noise_gens = new NoiseGenerator[]
{ {
new NoiseGeneratorSimplex(rand, 4), // Temperature new NoiseGeneratorSimplex(rand, 64), // Temperature
new NoiseGeneratorSimplex(rand, 4), // Humidity new NoiseGeneratorSimplex(rand, 64), // Humidity
new NoiseGeneratorSimplex(lrand), // Wind new NoiseGeneratorSimplex(lrand), // Wind
new NoiseGeneratorSimplex(lrand), // Wind new NoiseGeneratorSimplex(lrand), // Wind
@ -91,12 +91,16 @@ public class LayerGenEarth extends LayerGen
for(int y=0;y<Chunk.CHUNK_SIZE.my;y++) for(int y=0;y<Chunk.CHUNK_SIZE.my;y++)
{ {
Vec2d pos = new Vec2d(c_pos.x * 16 + x, c_pos.y * 16 + y); Vec2d pos = new Vec2d(c_pos.x * 16 + x, c_pos.y * 16 + y);
Vec2i tpos = pos.toInt();
double temperature = getTemperatureStatic(layer, pos); double temperature = getTemperatureStatic(layer, pos);
double humidity = getHumidity(layer, pos); double humidity = getHumidity(layer, pos);
chunk.setTemperature(tpos, temperature);
chunk.setHumidity(tpos, humidity);
if(layer.noise_gens[4].eval(pos.x / 64.0, pos.y / 64.0) > (1 - humidity) * 1.5) { if(layer.noise_gens[4].eval(pos.x / 64.0, pos.y / 64.0) > (1 - humidity) * 1.5) {
chunk.setFrontTile(Tiles.WATER.getDefaultState(), pos.toInt()); chunk.setFrontTile(Tiles.WATER.getDefaultState(), tpos);
} }
else else
@ -105,41 +109,41 @@ public class LayerGenEarth extends LayerGen
if(temperature < 0.35) if(temperature < 0.35)
{ {
chunk.setBackTile(Tiles.SNOW.getDefaultState(), pos.toInt()); chunk.setBackTile(Tiles.SNOW.getDefaultState(), tpos);
rock_type = (byte)1; rock_type = (byte)1;
if(rand.nextDouble() > MathHelpers.map(humidity, 0, 1, 0.99, 0.8)) { if(rand.nextDouble() > MathHelpers.map(humidity, 0, 1, 0.99, 0.8)) {
chunk.setFrontTile(new TileState(Tiles.TREE, (byte)1), pos.toInt()); chunk.setFrontTile(new TileState(Tiles.TREE, (byte)1), tpos);
} }
} }
else if(temperature > 0.65 && humidity < 0.5) else if(temperature > 0.65 && humidity < 0.5)
{ {
chunk.setBackTile(Tiles.SAND.getDefaultState(), pos.toInt()); chunk.setBackTile(Tiles.SAND.getDefaultState(), tpos);
rock_type = (byte)2; rock_type = (byte)2;
if(rand.nextDouble() > MathHelpers.map(humidity, 0, 0.5, 0.98, 0.95)) { if(rand.nextDouble() > MathHelpers.map(humidity, 0, 0.5, 0.98, 0.95)) {
chunk.setFrontTile(Tiles.CACTUS.getDefaultState(), pos.toInt()); chunk.setFrontTile(Tiles.CACTUS.getDefaultState(), tpos);
} }
} }
else else
{ {
chunk.setBackTile(Tiles.GRASS.getDefaultState(), pos.toInt()); chunk.setBackTile(Tiles.GRASS.getDefaultState(), tpos);
if(rand.nextDouble() > MathHelpers.map(humidity, 0, 1, 0.99, 0.9)) { if(rand.nextDouble() > MathHelpers.map(humidity, 0, 1, 0.99, 0.9)) {
chunk.setFrontTile(Tiles.TREE.getDefaultState(), pos.toInt()); chunk.setFrontTile(Tiles.TREE.getDefaultState(), tpos);
} }
if(rand.nextDouble() > 0.9) { if(rand.nextDouble() > 0.9) {
chunk.setFrontTile(Tiles.TALL_GRASS.getDefaultState(), pos.toInt()); chunk.setFrontTile(Tiles.TALL_GRASS.getDefaultState(), tpos);
} }
if(temperature > 0.7 && humidity > 0.7 && Math.random() > 0.9998) { if(temperature > 0.7 && humidity > 0.7 && Math.random() > 0.9998) {
chunk.setFrontTile(Tiles.HEMP.getDefaultState(), pos.toInt()); chunk.setFrontTile(Tiles.HEMP.getDefaultState(), tpos);
while(Math.random() < 0.8) { while(Math.random() < 0.8) {
chunk.setFrontTile(Tiles.HEMP.getDefaultState(), chunk.setFrontTile(Tiles.HEMP.getDefaultState(),
pos.toInt().add(new Vec2d( tpos.add(new Vec2d(
MathHelpers.map(Math.random(), 0, 1, -2, 3), MathHelpers.map(Math.random(), 0, 1, -2, 3),
MathHelpers.map(Math.random(), 0, 1, -2, 3)).toInt())); MathHelpers.map(Math.random(), 0, 1, -2, 3)).toInt()));
} }
@ -147,7 +151,7 @@ public class LayerGenEarth extends LayerGen
} }
if(rand.nextDouble() > 0.98) { if(rand.nextDouble() > 0.98) {
chunk.setFrontTile(new TileState(Tiles.ROCK, rock_type), pos.toInt()); chunk.setFrontTile(new TileState(Tiles.ROCK, rock_type), tpos);
} }
} }
} }

View File

@ -57,8 +57,6 @@ public class LayerGenLavaCaves extends LayerGen
RandomHelpers.randrange(rand, 16)); RandomHelpers.randrange(rand, 16));
} }
// Loop over the chunk // Loop over the chunk
for(int x=0;x<Chunk.CHUNK_SIZE.mx;x++) { for(int x=0;x<Chunk.CHUNK_SIZE.mx;x++) {
for(int y=0;y<Chunk.CHUNK_SIZE.my;y++) for(int y=0;y<Chunk.CHUNK_SIZE.my;y++)
@ -72,6 +70,9 @@ public class LayerGenLavaCaves extends LayerGen
double noise_c = (layer.noise_gens[1].eval(cx/16.0, cy/16.0) + 1) * 50; double noise_c = (layer.noise_gens[1].eval(cx/16.0, cy/16.0) + 1) * 50;
Vec2i tpos = new Vec2i(x, y); Vec2i tpos = new Vec2i(x, y);
chunk.setTemperature(tpos, 0.8);
chunk.setHumidity(tpos, 0);
if(noise_n > 60 || noise_n < 20) { if(noise_n > 60 || noise_n < 20) {
chunk.setBackTile(Tiles.STONE.getDefaultState(), tpos); chunk.setBackTile(Tiles.STONE.getDefaultState(), tpos);
} }