2019-10-20 07:20:21 +00:00
|
|
|
use super::SysTimer;
|
2019-10-20 05:19:50 +00:00
|
|
|
use crate::{chunk_generator::ChunkGenerator, client::Client, Tick};
|
|
|
|
use common::{
|
2019-10-22 18:18:40 +00:00
|
|
|
assets,
|
2020-01-20 16:42:35 +00:00
|
|
|
comp::{self, item, Player, Pos},
|
2019-10-20 05:19:50 +00:00
|
|
|
event::{EventBus, ServerEvent},
|
|
|
|
msg::ServerMsg,
|
|
|
|
state::TerrainChanges,
|
|
|
|
terrain::TerrainGrid,
|
|
|
|
};
|
|
|
|
use rand::Rng;
|
|
|
|
use specs::{Join, Read, ReadStorage, System, Write, WriteExpect, WriteStorage};
|
|
|
|
use std::sync::Arc;
|
|
|
|
use vek::*;
|
|
|
|
|
|
|
|
/// This system will handle loading generated chunks and unloading uneeded chunks.
|
|
|
|
/// 1. Inserts newly generated chunks into the TerrainGrid
|
|
|
|
/// 2. Sends new chunks to neaby clients
|
|
|
|
/// 3. Handles the chunk's supplement (e.g. npcs)
|
|
|
|
/// 4. Removes chunks outside the range of players
|
|
|
|
pub struct Sys;
|
|
|
|
impl<'a> System<'a> for Sys {
|
|
|
|
type SystemData = (
|
|
|
|
Read<'a, EventBus<ServerEvent>>,
|
|
|
|
Read<'a, Tick>,
|
2019-10-20 07:20:21 +00:00
|
|
|
Write<'a, SysTimer<Self>>,
|
2019-10-20 05:19:50 +00:00
|
|
|
WriteExpect<'a, ChunkGenerator>,
|
|
|
|
WriteExpect<'a, TerrainGrid>,
|
|
|
|
Write<'a, TerrainChanges>,
|
|
|
|
ReadStorage<'a, Pos>,
|
|
|
|
ReadStorage<'a, Player>,
|
|
|
|
WriteStorage<'a, Client>,
|
|
|
|
);
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&mut self,
|
|
|
|
(
|
|
|
|
server_emitter,
|
|
|
|
tick,
|
2019-10-20 07:20:21 +00:00
|
|
|
mut timer,
|
2019-10-20 05:19:50 +00:00
|
|
|
mut chunk_generator,
|
|
|
|
mut terrain,
|
|
|
|
mut terrain_changes,
|
|
|
|
positions,
|
|
|
|
players,
|
|
|
|
mut clients,
|
|
|
|
): Self::SystemData,
|
|
|
|
) {
|
2019-10-20 07:20:21 +00:00
|
|
|
timer.start();
|
|
|
|
|
2019-10-20 05:19:50 +00:00
|
|
|
// Fetch any generated `TerrainChunk`s and insert them into the terrain.
|
|
|
|
// Also, send the chunk data to anybody that is close by.
|
|
|
|
'insert_terrain_chunks: while let Some((key, res)) = chunk_generator.recv_new_chunk() {
|
|
|
|
let (chunk, supplement) = match res {
|
|
|
|
Ok((chunk, supplement)) => (chunk, supplement),
|
|
|
|
Err(entity) => {
|
|
|
|
if let Some(client) = clients.get_mut(entity) {
|
|
|
|
client.notify(ServerMsg::TerrainChunkUpdate {
|
|
|
|
key,
|
|
|
|
chunk: Err(()),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
continue 'insert_terrain_chunks;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// Send the chunk to all nearby players.
|
|
|
|
for (view_distance, pos, client) in (&players, &positions, &mut clients)
|
|
|
|
.join()
|
|
|
|
.filter_map(|(player, pos, client)| {
|
|
|
|
player.view_distance.map(|vd| (vd, pos, client))
|
|
|
|
})
|
|
|
|
{
|
|
|
|
let chunk_pos = terrain.pos_key(pos.0.map(|e| e as i32));
|
2019-12-23 06:02:00 +00:00
|
|
|
// Subtract 2 from the offset before computing squared magnitude
|
|
|
|
// 1 since chunks need neighbors to be meshed
|
|
|
|
// 1 to act as a buffer if the player moves in that direction
|
2019-10-20 05:19:50 +00:00
|
|
|
let adjusted_dist_sqr = (Vec2::from(chunk_pos) - Vec2::from(key))
|
|
|
|
.map(|e: i32| (e.abs() as u32).checked_sub(2).unwrap_or(0))
|
|
|
|
.magnitude_squared();
|
|
|
|
|
|
|
|
if adjusted_dist_sqr <= view_distance.pow(2) {
|
|
|
|
client.notify(ServerMsg::TerrainChunkUpdate {
|
|
|
|
key,
|
|
|
|
chunk: Ok(Box::new(chunk.clone())),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: code duplication for chunk insertion between here and state.rs
|
|
|
|
// Insert the chunk into terrain changes
|
|
|
|
if terrain.insert(key, Arc::new(chunk)).is_some() {
|
|
|
|
terrain_changes.modified_chunks.insert(key);
|
|
|
|
} else {
|
|
|
|
terrain_changes.new_chunks.insert(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle chunk supplement
|
|
|
|
for npc in supplement.npcs {
|
|
|
|
let (mut stats, mut body) = if rand::random() {
|
2020-01-13 22:49:46 +00:00
|
|
|
let body = comp::Body::Humanoid(comp::humanoid::Body::random());
|
2019-10-20 05:19:50 +00:00
|
|
|
let stats = comp::Stats::new(
|
2019-12-30 12:16:35 +00:00
|
|
|
"Traveler".to_string(),
|
2020-01-13 22:49:46 +00:00
|
|
|
body,
|
2019-12-30 12:16:35 +00:00
|
|
|
Some(assets::load_expect_cloned("common.items.weapons.staff_1")),
|
2019-10-20 05:19:50 +00:00
|
|
|
);
|
|
|
|
(stats, body)
|
|
|
|
} else {
|
|
|
|
let body = comp::Body::QuadrupedMedium(comp::quadruped_medium::Body::random());
|
2020-01-13 22:49:46 +00:00
|
|
|
let stats = comp::Stats::new("Wolf".to_string(), body, None);
|
2019-10-20 05:19:50 +00:00
|
|
|
(stats, body)
|
|
|
|
};
|
|
|
|
let mut scale = 1.0;
|
|
|
|
|
|
|
|
// TODO: Remove this and implement scaling or level depending on stuff like species instead
|
2020-01-20 16:42:35 +00:00
|
|
|
stats.level.set_level(rand::thread_rng().gen_range(1, 4));
|
2019-10-20 05:19:50 +00:00
|
|
|
|
|
|
|
if npc.boss {
|
|
|
|
if rand::random::<f32>() < 0.8 {
|
2020-01-13 22:49:46 +00:00
|
|
|
let hbody = comp::humanoid::Body::random();
|
|
|
|
body = comp::Body::Humanoid(hbody);
|
2019-10-20 05:19:50 +00:00
|
|
|
stats = comp::Stats::new(
|
2019-12-30 12:16:35 +00:00
|
|
|
"Fearless Wanderer".to_string(),
|
2020-01-13 22:49:46 +00:00
|
|
|
body,
|
2019-12-30 12:16:35 +00:00
|
|
|
Some(assets::load_expect_cloned("common.items.weapons.hammer_1")),
|
2019-10-20 05:19:50 +00:00
|
|
|
);
|
|
|
|
}
|
2020-01-20 16:42:35 +00:00
|
|
|
stats.level.set_level(rand::thread_rng().gen_range(8, 15));
|
2019-12-30 12:16:35 +00:00
|
|
|
scale = 2.0 + rand::random::<f32>();
|
2019-10-20 05:19:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stats.update_max_hp();
|
|
|
|
stats
|
|
|
|
.health
|
|
|
|
.set_to(stats.health.maximum(), comp::HealthSource::Revive);
|
2020-01-20 16:42:35 +00:00
|
|
|
if let Some(item::Item {
|
|
|
|
kind: item::ItemKind::Tool { power, .. },
|
|
|
|
..
|
|
|
|
}) = &mut stats.equipment.main
|
|
|
|
{
|
|
|
|
*power = stats.level.level() * 3;
|
|
|
|
}
|
2019-10-20 05:19:50 +00:00
|
|
|
server_emitter.emit(ServerEvent::CreateNpc {
|
|
|
|
pos: Pos(npc.pos),
|
|
|
|
stats,
|
|
|
|
body,
|
|
|
|
agent: comp::Agent::enemy(),
|
|
|
|
scale: comp::Scale(scale),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove chunks that are too far from players.
|
|
|
|
let mut chunks_to_remove = Vec::new();
|
|
|
|
terrain
|
|
|
|
.iter()
|
|
|
|
.map(|(k, _)| k)
|
2019-10-26 02:00:30 +00:00
|
|
|
// Don't check every chunk every tick (spread over 16 ticks)
|
|
|
|
.filter(|k| k.x.abs() as u64 % 4 + (k.y.abs() as u64 % 4) * 4 == tick.0 % 16)
|
2019-10-20 05:19:50 +00:00
|
|
|
// There shouldn't be to many pending chunks so we will just check them all
|
|
|
|
.chain(chunk_generator.pending_chunks())
|
|
|
|
.for_each(|chunk_key| {
|
|
|
|
let mut should_drop = true;
|
|
|
|
|
|
|
|
// For each player with a position, calculate the distance.
|
|
|
|
for (player, pos) in (&players, &positions).join() {
|
|
|
|
if player
|
|
|
|
.view_distance
|
|
|
|
.map(|vd| chunk_in_vd(pos.0, chunk_key, &terrain, vd))
|
|
|
|
.unwrap_or(false)
|
|
|
|
{
|
|
|
|
should_drop = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if should_drop {
|
|
|
|
chunks_to_remove.push(chunk_key);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
for key in chunks_to_remove {
|
|
|
|
// TODO: code duplication for chunk insertion between here and state.rs
|
|
|
|
if terrain.remove(key).is_some() {
|
|
|
|
terrain_changes.removed_chunks.insert(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
chunk_generator.cancel_if_pending(key);
|
|
|
|
}
|
2019-10-20 07:20:21 +00:00
|
|
|
|
|
|
|
timer.end()
|
2019-10-20 05:19:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn chunk_in_vd(
|
|
|
|
player_pos: Vec3<f32>,
|
|
|
|
chunk_pos: Vec2<i32>,
|
|
|
|
terrain: &TerrainGrid,
|
|
|
|
vd: u32,
|
|
|
|
) -> bool {
|
|
|
|
let player_chunk_pos = terrain.pos_key(player_pos.map(|e| e as i32));
|
|
|
|
|
|
|
|
let adjusted_dist_sqr = Vec2::from(player_chunk_pos - chunk_pos)
|
|
|
|
.map(|e: i32| (e.abs() as u32).checked_sub(2).unwrap_or(0))
|
|
|
|
.magnitude_squared();
|
|
|
|
|
|
|
|
adjusted_dist_sqr <= vd.pow(2)
|
|
|
|
}
|