Started work on worldgen

Former-commit-id: 277a4b2a0a5a393c590a710aebaf62e5cce2ad1d
This commit is contained in:
Joshua Barretto 2019-04-23 15:49:14 +01:00
parent 837ba99d2a
commit f9f434a1c2
4 changed files with 29 additions and 19 deletions

View File

@ -147,7 +147,7 @@ impl Client {
frontend_events.append(&mut self.handle_new_messages()?);
self.state.terrain().iter().for_each(|(k, _)| {
println!("Chunk at {:?}", k);
//println!("Chunk at {:?}", k);
});
self.state.write_component(
@ -194,9 +194,9 @@ impl Client {
{
let chunk_pos = self.state.terrain().pos_key(pos.0.map(|e| e as i32));
for i in chunk_pos.x - 1..chunk_pos.x + 1 {
for j in chunk_pos.y - 1..chunk_pos.y + 1 {
for k in -1..3 {
for i in chunk_pos.x - 2..chunk_pos.x + 2 {
for j in chunk_pos.y - 2..chunk_pos.y + 2 {
for k in 0..1 {
let key = chunk_pos + Vec3::new(i, j, k);
if self.state.terrain().get_key(key).is_none()
&& !self.pending_chunks.contains(&key)

View File

@ -73,6 +73,7 @@ impl<V: Vox + Clone, S: VolSize, M> SampleVol for VolMap<V, S, M> {
/// Note that the resultant volume does not carry forward metadata from the original chunks.
fn sample(&self, range: Aabb<i32>) -> Result<Self::Sample, VolMapErr> {
// Return early if we don't have all the needed chunks that we need!
/*
let min_chunk = Self::chunk_key(range.min);
let max_chunk = Self::chunk_key(range.max - Vec3::one());
for x in min_chunk.x..=max_chunk.x {
@ -84,6 +85,7 @@ impl<V: Vox + Clone, S: VolSize, M> SampleVol for VolMap<V, S, M> {
}
}
}
*/
let mut sample = Dyna::filled(
range.size().map(|e| e as u32).into(),
@ -92,7 +94,7 @@ impl<V: Vox + Clone, S: VolSize, M> SampleVol for VolMap<V, S, M> {
);
for pos in sample.iter_positions() {
sample.set(pos, self.get(range.min + pos)?.clone())
sample.set(pos, self.get(range.min + pos).map(|v| v.clone()).unwrap_or(V::empty()))
.map_err(|err| VolMapErr::DynaErr(err))?;
}

View File

@ -182,6 +182,7 @@ impl Server {
// Fetch any generated `TerrainChunk`s and insert them into the terrain
// Also, send the chunk data to anybody that is close by
for (key, chunk) in self.chunk_rx.try_iter() {
println!("Generation finished {:?}", key);
// Send the chunk to all nearby players
for (entity, player, pos) in (
&self.state.ecs().entities(),
@ -193,12 +194,11 @@ impl Server {
// TODO: Distance check
// if self.state.terrain().key_pos(key)
/*
println!("Send to player {:?}", key);
self.clients.notify(entity, ServerMsg::TerrainChunkUpdate {
key,
chunk: Box::new(chunk.clone()),
});
*/
}
self.state.insert_chunk(key, chunk);
@ -322,10 +322,10 @@ impl Server {
}
ClientState::Spectator | ClientState::Character => {
match state.terrain().get_key(key) {
Some(chunk) => {} /*client.postbox.send_message(ServerMsg::TerrainChunkUpdate {
key,
chunk: Box::new(chunk.clone()),
}),*/
Some(chunk) => client.postbox.send_message(ServerMsg::TerrainChunkUpdate {
key,
chunk: Box::new(chunk.clone()),
}),
None => requested_chunks.push(key),
}
}

View File

@ -99,14 +99,22 @@ impl Terrain {
// What happens if the block on the edge of a chunk gets modified? We need to spawn
// a mesh worker to remesh its neighbour(s) too since their ambient occlusion and face
// elision information changes too!
match self.mesh_todo.iter_mut().find(|todo| todo.pos == *pos) {
Some(todo) => todo.started_tick = current_tick,
// The chunk it's queued yet, add it to the queue
None => self.mesh_todo.push_back(ChunkMeshState {
pos: *pos,
started_tick: current_tick,
active_worker: false,
}),
for i in -1..2 {
for j in -1..2 {
for k in -1..2 {
let pos = pos + Vec3::new(i, j, k);
match self.mesh_todo.iter_mut().find(|todo| todo.pos == pos) {
Some(todo) => todo.started_tick = current_tick,
// The chunk it's queued yet, add it to the queue
None => self.mesh_todo.push_back(ChunkMeshState {
pos,
started_tick: current_tick,
active_worker: false,
}),
}
}
}
}
}