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-03-20 14:45:36 +00:00
|
|
|
comp::{self, CharacterAbility, Item, ItemConfig, Player, Pos},
|
2019-10-20 05:19:50 +00:00
|
|
|
event::{EventBus, ServerEvent},
|
2020-01-25 02:15:15 +00:00
|
|
|
generation::EntityKind,
|
2019-10-20 05:19:50 +00:00
|
|
|
msg::ServerMsg,
|
2020-01-29 03:38:45 +00:00
|
|
|
npc::{self, NPC_NAMES},
|
2019-10-20 05:19:50 +00:00
|
|
|
state::TerrainChanges,
|
|
|
|
terrain::TerrainGrid,
|
|
|
|
};
|
2020-01-26 00:22:48 +00:00
|
|
|
use rand::{seq::SliceRandom, Rng};
|
2019-10-20 05:19:50 +00:00
|
|
|
use specs::{Join, Read, ReadStorage, System, Write, WriteExpect, WriteStorage};
|
2020-03-16 13:27:52 +00:00
|
|
|
use std::{sync::Arc, time::Duration};
|
2019-10-20 05:19:50 +00:00
|
|
|
use vek::*;
|
|
|
|
|
2020-01-27 16:48:42 +00:00
|
|
|
/// This system will handle loading generated chunks and unloading
|
|
|
|
/// uneeded chunks.
|
2019-10-20 05:19:50 +00:00
|
|
|
/// 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,
|
|
|
|
(
|
2020-03-22 04:49:32 +00:00
|
|
|
server_event_bus,
|
2019-10-20 05:19:50 +00:00
|
|
|
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();
|
|
|
|
|
2020-03-22 04:49:32 +00:00
|
|
|
let mut server_emitter = server_event_bus.emitter();
|
|
|
|
|
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;
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2019-10-20 05:19:50 +00:00
|
|
|
};
|
|
|
|
// 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
|
2020-01-25 02:15:15 +00:00
|
|
|
for entity in supplement.entities {
|
|
|
|
if let EntityKind::Waypoint = entity.kind {
|
|
|
|
server_emitter.emit(ServerEvent::CreateWaypoint(entity.pos));
|
2019-10-20 05:19:50 +00:00
|
|
|
} else {
|
2020-01-29 03:38:45 +00:00
|
|
|
fn get_npc_name<
|
2020-01-29 16:56:28 +00:00
|
|
|
'a,
|
2020-01-29 03:38:45 +00:00
|
|
|
Species,
|
2020-01-29 16:56:28 +00:00
|
|
|
SpeciesData: for<'b> core::ops::Index<&'b Species, Output = npc::SpeciesNames>,
|
2020-01-29 03:38:45 +00:00
|
|
|
>(
|
2020-01-29 16:56:28 +00:00
|
|
|
body_data: &'a comp::BodyData<npc::BodyNames, SpeciesData>,
|
2020-01-29 03:38:45 +00:00
|
|
|
species: Species,
|
2020-01-29 16:56:28 +00:00
|
|
|
) -> &'a str {
|
|
|
|
&body_data.species[&species].generic
|
2020-01-29 03:38:45 +00:00
|
|
|
}
|
2020-03-19 10:00:28 +00:00
|
|
|
|
2020-01-26 01:11:32 +00:00
|
|
|
const SPAWN_NPCS: &'static [fn() -> (
|
|
|
|
String,
|
|
|
|
comp::Body,
|
|
|
|
Option<comp::Item>,
|
|
|
|
comp::Alignment,
|
|
|
|
)] = &[
|
2020-01-25 02:15:15 +00:00
|
|
|
(|| {
|
2020-01-29 03:38:45 +00:00
|
|
|
let body = comp::humanoid::Body::random();
|
2020-01-25 02:15:15 +00:00
|
|
|
(
|
2020-01-29 03:38:45 +00:00
|
|
|
format!(
|
|
|
|
"{} Traveler",
|
|
|
|
get_npc_name(&NPC_NAMES.humanoid, body.race)
|
|
|
|
),
|
|
|
|
comp::Body::Humanoid(body),
|
2020-03-18 22:09:58 +00:00
|
|
|
Some(assets::load_expect_cloned(
|
|
|
|
"common.items.weapons.starter_axe",
|
|
|
|
)),
|
2020-01-27 10:02:36 +00:00
|
|
|
comp::Alignment::Npc,
|
|
|
|
)
|
|
|
|
}) as _,
|
|
|
|
(|| {
|
2020-01-29 03:38:45 +00:00
|
|
|
let body = comp::humanoid::Body::random();
|
2020-01-27 10:02:36 +00:00
|
|
|
(
|
2020-01-29 03:38:45 +00:00
|
|
|
format!("{} Bandit", get_npc_name(&NPC_NAMES.humanoid, body.race)),
|
|
|
|
comp::Body::Humanoid(body),
|
2020-03-18 22:09:58 +00:00
|
|
|
Some(assets::load_expect_cloned(
|
|
|
|
"common.items.weapons.short_sword_0",
|
|
|
|
)),
|
2020-01-26 01:11:32 +00:00
|
|
|
comp::Alignment::Enemy,
|
2020-01-25 02:15:15 +00:00
|
|
|
)
|
|
|
|
}) as _,
|
|
|
|
(|| {
|
2020-01-29 03:38:45 +00:00
|
|
|
let body = comp::quadruped_medium::Body::random();
|
2020-01-25 02:15:15 +00:00
|
|
|
(
|
2020-01-29 03:38:45 +00:00
|
|
|
get_npc_name(&NPC_NAMES.quadruped_medium, body.species).into(),
|
|
|
|
comp::Body::QuadrupedMedium(body),
|
2020-01-25 02:15:15 +00:00
|
|
|
None,
|
2020-01-26 01:11:32 +00:00
|
|
|
comp::Alignment::Enemy,
|
2020-01-25 02:15:15 +00:00
|
|
|
)
|
|
|
|
}) as _,
|
|
|
|
(|| {
|
2020-01-29 03:38:45 +00:00
|
|
|
let body = comp::bird_medium::Body::random();
|
2020-01-25 02:15:15 +00:00
|
|
|
(
|
2020-01-29 03:38:45 +00:00
|
|
|
get_npc_name(&NPC_NAMES.bird_medium, body.species).into(),
|
|
|
|
comp::Body::BirdMedium(body),
|
2020-01-25 02:15:15 +00:00
|
|
|
None,
|
2020-01-26 01:11:32 +00:00
|
|
|
comp::Alignment::Wild,
|
2020-01-25 02:15:15 +00:00
|
|
|
)
|
|
|
|
}) as _,
|
|
|
|
(|| {
|
2020-01-29 03:38:45 +00:00
|
|
|
let body = comp::critter::Body::random();
|
2020-01-25 02:15:15 +00:00
|
|
|
(
|
2020-01-29 03:38:45 +00:00
|
|
|
get_npc_name(&NPC_NAMES.critter, body.species).into(),
|
|
|
|
comp::Body::Critter(body),
|
2020-01-25 02:15:15 +00:00
|
|
|
None,
|
2020-01-26 01:11:32 +00:00
|
|
|
comp::Alignment::Wild,
|
2020-01-25 02:15:15 +00:00
|
|
|
)
|
|
|
|
}) as _,
|
|
|
|
(|| {
|
2020-01-29 03:38:45 +00:00
|
|
|
let body = comp::quadruped_small::Body::random();
|
2020-01-25 02:15:15 +00:00
|
|
|
(
|
2020-01-29 03:38:45 +00:00
|
|
|
get_npc_name(&NPC_NAMES.quadruped_small, body.species).into(),
|
|
|
|
comp::Body::QuadrupedSmall(body),
|
2020-01-25 02:15:15 +00:00
|
|
|
None,
|
2020-01-26 01:11:32 +00:00
|
|
|
comp::Alignment::Wild,
|
2020-01-25 02:15:15 +00:00
|
|
|
)
|
|
|
|
}),
|
|
|
|
];
|
2020-01-27 10:02:36 +00:00
|
|
|
let (name, mut body, main, mut alignment) = SPAWN_NPCS
|
2020-01-25 02:15:15 +00:00
|
|
|
.choose(&mut rand::thread_rng())
|
|
|
|
.expect("SPAWN_NPCS is nonempty")(
|
|
|
|
);
|
2020-03-15 14:27:06 +00:00
|
|
|
let mut stats = comp::Stats::new(name, body);
|
2020-03-16 13:27:52 +00:00
|
|
|
|
2020-03-19 10:00:28 +00:00
|
|
|
let active_item =
|
2020-03-16 13:27:52 +00:00
|
|
|
if let Some(comp::ItemKind::Tool(tool)) = main.as_ref().map(|i| &i.kind) {
|
|
|
|
let mut abilities = tool.get_abilities();
|
|
|
|
let mut ability_drain = abilities.drain(..);
|
|
|
|
|
2020-03-19 10:00:28 +00:00
|
|
|
main.map(|item| comp::ItemConfig {
|
|
|
|
item,
|
|
|
|
primary_ability: ability_drain.next(),
|
|
|
|
secondary_ability: ability_drain.next(),
|
2020-03-22 15:25:47 +00:00
|
|
|
block_ability: None,
|
2020-03-19 10:00:28 +00:00
|
|
|
dodge_ability: Some(comp::CharacterAbility::Roll),
|
|
|
|
})
|
2020-03-16 13:27:52 +00:00
|
|
|
} else {
|
2020-03-19 10:00:28 +00:00
|
|
|
Some(ItemConfig {
|
2020-03-22 15:25:47 +00:00
|
|
|
// We need the empty item so npcs can attack
|
2020-03-19 10:00:28 +00:00
|
|
|
item: Item::empty(),
|
|
|
|
primary_ability: Some(CharacterAbility::BasicMelee {
|
2020-03-22 15:25:47 +00:00
|
|
|
buildup_duration: Duration::from_millis(0),
|
|
|
|
recover_duration: Duration::from_millis(300),
|
|
|
|
base_damage: 2,
|
|
|
|
range: 3.5,
|
|
|
|
max_angle: 60.0,
|
2020-03-16 13:27:52 +00:00
|
|
|
}),
|
2020-03-19 10:00:28 +00:00
|
|
|
secondary_ability: None,
|
|
|
|
block_ability: None,
|
|
|
|
dodge_ability: None,
|
|
|
|
})
|
2020-03-16 13:27:52 +00:00
|
|
|
};
|
2020-01-26 00:22:48 +00:00
|
|
|
|
2020-03-19 10:00:28 +00:00
|
|
|
let mut loadout = match alignment {
|
|
|
|
comp::Alignment::Npc => comp::Loadout {
|
|
|
|
active_item,
|
|
|
|
second_item: None,
|
|
|
|
shoulder: Some(assets::load_expect_cloned(
|
2020-03-19 16:47:37 +00:00
|
|
|
"common.items.armor.shoulder.leather_0",
|
2020-03-19 10:00:28 +00:00
|
|
|
)),
|
|
|
|
chest: Some(assets::load_expect_cloned(
|
2020-03-19 16:47:37 +00:00
|
|
|
"common.items.armor.chest.leather_0",
|
2020-03-19 10:00:28 +00:00
|
|
|
)),
|
|
|
|
belt: Some(assets::load_expect_cloned(
|
2020-03-19 16:47:37 +00:00
|
|
|
"common.items.armor.belt.plate_0",
|
2020-03-19 10:00:28 +00:00
|
|
|
)),
|
|
|
|
hand: Some(assets::load_expect_cloned(
|
2020-03-19 16:47:37 +00:00
|
|
|
"common.items.armor.hand.plate_0",
|
2020-03-19 10:00:28 +00:00
|
|
|
)),
|
|
|
|
pants: Some(assets::load_expect_cloned(
|
2020-03-19 16:47:37 +00:00
|
|
|
"common.items.armor.pants.plate_green_0",
|
2020-03-19 10:00:28 +00:00
|
|
|
)),
|
|
|
|
foot: Some(assets::load_expect_cloned(
|
2020-03-19 16:47:37 +00:00
|
|
|
"common.items.armor.foot.leather_0",
|
2020-03-19 10:00:28 +00:00
|
|
|
)),
|
|
|
|
},
|
|
|
|
comp::Alignment::Enemy => comp::Loadout {
|
|
|
|
active_item,
|
|
|
|
second_item: None,
|
|
|
|
shoulder: Some(assets::load_expect_cloned(
|
2020-03-19 16:47:37 +00:00
|
|
|
"common.items.armor.shoulder.leather_0",
|
2020-03-19 10:00:28 +00:00
|
|
|
)),
|
|
|
|
chest: Some(assets::load_expect_cloned(
|
2020-03-19 16:47:37 +00:00
|
|
|
"common.items.armor.chest.plate_green_0",
|
2020-03-19 10:00:28 +00:00
|
|
|
)),
|
|
|
|
belt: Some(assets::load_expect_cloned(
|
2020-03-19 16:47:37 +00:00
|
|
|
"common.items.armor.belt.plate_0",
|
2020-03-19 10:00:28 +00:00
|
|
|
)),
|
|
|
|
hand: Some(assets::load_expect_cloned(
|
2020-03-19 16:47:37 +00:00
|
|
|
"common.items.armor.hand.plate_0",
|
2020-03-19 10:00:28 +00:00
|
|
|
)),
|
|
|
|
pants: Some(assets::load_expect_cloned(
|
2020-03-19 16:47:37 +00:00
|
|
|
"common.items.armor.pants.plate_green_0",
|
2020-03-19 10:00:28 +00:00
|
|
|
)),
|
|
|
|
foot: Some(assets::load_expect_cloned(
|
2020-03-19 16:47:37 +00:00
|
|
|
"common.items.armor.foot.plate_0",
|
2020-03-19 10:00:28 +00:00
|
|
|
)),
|
|
|
|
},
|
|
|
|
_ => comp::Loadout {
|
|
|
|
active_item,
|
|
|
|
second_item: None,
|
|
|
|
shoulder: None,
|
|
|
|
chest: None,
|
|
|
|
belt: None,
|
|
|
|
hand: None,
|
|
|
|
pants: None,
|
|
|
|
foot: None,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-01-25 02:15:15 +00:00
|
|
|
let mut scale = 1.0;
|
2019-10-20 05:19:50 +00:00
|
|
|
|
2020-02-01 20:39:39 +00:00
|
|
|
// TODO: Remove this and implement scaling or level depending on stuff like
|
|
|
|
// species instead
|
2020-01-25 02:15:15 +00:00
|
|
|
stats.level.set_level(rand::thread_rng().gen_range(1, 4));
|
2019-10-20 05:19:50 +00:00
|
|
|
|
2020-03-19 10:00:28 +00:00
|
|
|
// Replace stuff if it's a boss
|
2020-01-25 02:15:15 +00:00
|
|
|
if let EntityKind::Boss = entity.kind {
|
2020-01-27 10:02:36 +00:00
|
|
|
if rand::random::<f32>() < 0.65 {
|
2020-01-29 03:38:45 +00:00
|
|
|
let body_new = comp::humanoid::Body::random();
|
|
|
|
body = comp::Body::Humanoid(body_new);
|
2020-01-27 10:02:36 +00:00
|
|
|
alignment = comp::Alignment::Npc;
|
2020-01-25 02:15:15 +00:00
|
|
|
stats = comp::Stats::new(
|
2020-01-29 03:38:45 +00:00
|
|
|
format!(
|
|
|
|
"Fearless Giant {}",
|
|
|
|
get_npc_name(&NPC_NAMES.humanoid, body_new.race)
|
|
|
|
),
|
2020-01-25 02:15:15 +00:00
|
|
|
body,
|
|
|
|
);
|
|
|
|
}
|
2020-03-14 21:33:20 +00:00
|
|
|
loadout = comp::Loadout {
|
|
|
|
active_item: Some(comp::ItemConfig {
|
2020-03-18 22:09:58 +00:00
|
|
|
item: assets::load_expect_cloned(
|
|
|
|
"common.items.weapons.zweihander_sword_0",
|
|
|
|
),
|
2020-03-16 13:27:52 +00:00
|
|
|
primary_ability: Some(CharacterAbility::BasicMelee {
|
|
|
|
buildup_duration: Duration::from_millis(800),
|
|
|
|
recover_duration: Duration::from_millis(200),
|
2020-03-16 21:04:49 +00:00
|
|
|
base_damage: 13,
|
2020-03-22 15:25:47 +00:00
|
|
|
range: 3.5,
|
|
|
|
max_angle: 60.0,
|
2020-03-16 13:27:52 +00:00
|
|
|
}),
|
2020-03-14 21:33:20 +00:00
|
|
|
secondary_ability: None,
|
|
|
|
block_ability: None,
|
|
|
|
dodge_ability: None,
|
|
|
|
}),
|
|
|
|
second_item: None,
|
2020-03-18 22:09:58 +00:00
|
|
|
shoulder: Some(assets::load_expect_cloned(
|
2020-03-19 16:47:37 +00:00
|
|
|
"common.items.armor.shoulder.plate_0",
|
2020-03-18 22:09:58 +00:00
|
|
|
)),
|
|
|
|
chest: Some(assets::load_expect_cloned(
|
2020-03-19 16:47:37 +00:00
|
|
|
"common.items.armor.chest.plate_green_0",
|
2020-03-18 22:09:58 +00:00
|
|
|
)),
|
|
|
|
belt: Some(assets::load_expect_cloned(
|
2020-03-19 16:47:37 +00:00
|
|
|
"common.items.armor.belt.plate_0",
|
2020-03-18 22:09:58 +00:00
|
|
|
)),
|
|
|
|
hand: Some(assets::load_expect_cloned(
|
2020-03-19 16:47:37 +00:00
|
|
|
"common.items.armor.hand.plate_0",
|
2020-03-18 22:09:58 +00:00
|
|
|
)),
|
|
|
|
pants: Some(assets::load_expect_cloned(
|
2020-03-19 16:47:37 +00:00
|
|
|
"common.items.armor.pants.plate_green_0",
|
2020-03-18 22:09:58 +00:00
|
|
|
)),
|
|
|
|
foot: Some(assets::load_expect_cloned(
|
2020-03-19 16:47:37 +00:00
|
|
|
"common.items.armor.foot.plate_0",
|
2020-03-18 22:09:58 +00:00
|
|
|
)),
|
2020-03-14 21:33:20 +00:00
|
|
|
};
|
|
|
|
|
2020-01-25 02:15:15 +00:00
|
|
|
stats.level.set_level(rand::thread_rng().gen_range(8, 15));
|
|
|
|
scale = 2.0 + rand::random::<f32>();
|
2019-10-20 05:19:50 +00:00
|
|
|
}
|
|
|
|
|
2020-01-25 02:15:15 +00:00
|
|
|
stats.update_max_hp();
|
2020-03-15 13:34:17 +00:00
|
|
|
|
2020-01-25 02:15:15 +00:00
|
|
|
stats
|
|
|
|
.health
|
|
|
|
.set_to(stats.health.maximum(), comp::HealthSource::Revive);
|
2020-03-15 13:34:17 +00:00
|
|
|
|
|
|
|
// TODO: This code sets an appropriate base_damage for the enemy. This doesn't
|
|
|
|
// work because the damage is now saved in an ability
|
|
|
|
/*
|
2020-03-14 21:33:20 +00:00
|
|
|
if let Some(item::ItemKind::Tool(item::ToolData { base_damage, .. })) =
|
|
|
|
&mut loadout.active_item.map(|i| i.item.kind)
|
2020-01-25 02:15:15 +00:00
|
|
|
{
|
2020-03-14 21:33:20 +00:00
|
|
|
*base_damage = stats.level.level() as u32 * 3;
|
2020-01-25 02:15:15 +00:00
|
|
|
}
|
2020-03-15 13:34:17 +00:00
|
|
|
*/
|
2020-01-25 02:15:15 +00:00
|
|
|
server_emitter.emit(ServerEvent::CreateNpc {
|
|
|
|
pos: Pos(entity.pos),
|
|
|
|
stats,
|
2020-02-26 17:04:43 +00:00
|
|
|
loadout,
|
2020-01-25 02:15:15 +00:00
|
|
|
body,
|
2020-01-26 01:11:32 +00:00
|
|
|
alignment,
|
2020-01-25 02:15:15 +00:00
|
|
|
agent: comp::Agent::default().with_patrol_origin(entity.pos),
|
|
|
|
scale: comp::Scale(scale),
|
|
|
|
})
|
2020-01-20 16:42:35 +00:00
|
|
|
}
|
2019-10-20 05:19:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|