seperate sys/state

This commit is contained in:
Marcel Märtens 2021-04-06 17:47:03 +02:00
parent c5455fa1a1
commit 5862920f32
40 changed files with 127 additions and 69 deletions

28
Cargo.lock generated
View File

@ -5442,6 +5442,7 @@ dependencies = [
"veloren-common-ecs",
"veloren-common-frontend",
"veloren-common-net",
"veloren-common-state",
"veloren-common-sys",
"veloren-network",
]
@ -5535,18 +5536,14 @@ dependencies = [
]
[[package]]
name = "veloren-common-sys"
name = "veloren-common-state"
version = "0.9.0"
dependencies = [
"bincode",
"hashbrown",
"indexmap",
"ordered-float 2.1.1",
"rand 0.8.3",
"rayon",
"scopeguard",
"serde",
"slab",
"specs",
"tar",
"toml",
@ -5560,6 +5557,25 @@ dependencies = [
"wasmer",
]
[[package]]
name = "veloren-common-sys"
version = "0.9.0"
dependencies = [
"hashbrown",
"indexmap",
"ordered-float 2.1.1",
"rand 0.8.3",
"rayon",
"slab",
"specs",
"tracing",
"vek",
"veloren-common",
"veloren-common-base",
"veloren-common-ecs",
"veloren-common-net",
]
[[package]]
name = "veloren-network"
version = "0.3.0"
@ -5666,6 +5682,7 @@ dependencies = [
"veloren-common-base",
"veloren-common-ecs",
"veloren-common-net",
"veloren-common-state",
"veloren-common-sys",
"veloren-network",
"veloren-plugin-api",
@ -5753,6 +5770,7 @@ dependencies = [
"veloren-common-ecs",
"veloren-common-frontend",
"veloren-common-net",
"veloren-common-state",
"veloren-common-sys",
"veloren-server",
"veloren-voxygen-anim",

View File

@ -6,6 +6,7 @@ members = [
"common/base",
"common/ecs",
"common/net",
"common/state",
"common/sys",
"common/frontend",
"client",

View File

@ -7,7 +7,7 @@ edition = "2018"
[features]
tracy = ["common/tracy", "common-base/tracy", "common-sys/tracy", "common-net/tracy", "common-frontend/tracy"]
simd = ["vek/platform_intrinsics"]
plugins = ["common-sys/plugins"]
plugins = ["common-state/plugins"]
bin_bot = ["common-ecs", "serde", "ron", "clap", "rustyline", "common-frontend", "async-channel"]
default = ["simd"]
@ -15,6 +15,7 @@ default = ["simd"]
[dependencies]
common = { package = "veloren-common", path = "../common", features = ["no-assets"] }
common-base = { package = "veloren-common-base", path = "../common/base" }
common-state = { package = "veloren-common-state", path = "../common/state", default-features = false }
common-sys = { package = "veloren-common-sys", path = "../common/sys", default-features = false }
common-net = { package = "veloren-common-net", path = "../common/net" }
network = { package = "veloren-network", path = "../network", features = ["compression"], default-features = false }

View File

@ -54,7 +54,8 @@ use common_net::{
},
sync::WorldSyncExt,
};
use common_sys::state::State;
use common_state::State;
use common_sys::add_local_systems;
use comp::BuffKind;
use futures_util::FutureExt;
use hashbrown::{HashMap, HashSet};
@ -1418,7 +1419,8 @@ impl Client {
}
// 4) Tick the client's LocalState
self.state.tick(dt, add_foreign_systems, true);
self.state
.tick(dt, add_local_systems, add_foreign_systems, true);
// TODO: avoid emitting these in the first place
self.state
.ecs()

40
common/state/Cargo.toml Normal file
View File

@ -0,0 +1,40 @@
[package]
authors = ["Marcel Märtens <marcel.cochem@googlemail.com>"]
edition = "2018"
name = "veloren-common-state"
version = "0.9.0"
[features]
tracy = ["common/tracy"]
simd = ["vek/platform_intrinsics"]
plugins = ["toml", "tar", "wasmer", "bincode", "plugin-api", "serde"]
default = ["simd"]
[dependencies]
common = { package = "veloren-common", path = ".." }
common-net = { package = "veloren-common-net", path = "../net" }
common-ecs = { package = "veloren-common-ecs", path = "../ecs" }
common-base = { package = "veloren-common-base", path = "../base" }
rayon = "1.5"
tracing = { version = "0.1", default-features = false }
vek = { version = "=0.14.1", features = ["serde"] }
# Data structures
hashbrown = { version = "0.9", features = ["rayon", "serde", "nightly"] }
# ECS
specs = { git = "https://github.com/amethyst/specs.git", features = ["serde", "storage-event-control", "derive"], rev = "5a9b71035007be0e3574f35184acac1cd4530496" }
# Plugins
scopeguard = "1.1.0"
serde = { version = "1.0.110", features = ["derive"], optional = true }
toml = { version = "0.5.7", optional = true }
tar = { version = "0.4.30", optional = true }
wasmer = { version = "1.0.0", optional = true, default-features = false, features = ["wat", "default-cranelift", "default-jit"] }
bincode = { version = "1.3.1", optional = true }
plugin-api = { package = "veloren-plugin-api", path = "../../plugin/api", optional = true }
# Tweak running code
#inline_tweak = { version = "1.0.8", features = ["release_tweak"] }

View File

@ -1,3 +1,5 @@
#[cfg(feature = "plugins")] pub mod plugin;
#[cfg(feature = "plugins")]
use crate::plugin::memory_manager::EcsWorld;
#[cfg(feature = "plugins")]
@ -18,7 +20,7 @@ use common::{
vol::{ReadVol, WriteVol},
};
use common_base::span;
use common_ecs::{run_now, PhysicsMetrics, SysMetrics};
use common_ecs::{PhysicsMetrics, SysMetrics};
use common_net::sync::{interpolation as sync_interp, WorldSyncExt};
use core::{convert::identity, time::Duration};
use hashbrown::{hash_map, HashMap, HashSet};
@ -506,6 +508,7 @@ impl State {
pub fn tick(
&mut self,
dt: Duration,
add_local_systems: impl Fn(&mut DispatcherBuilder),
add_foreign_systems: impl Fn(&mut DispatcherBuilder),
update_terrain_and_regions: bool,
) {
@ -528,14 +531,13 @@ impl State {
// Create and run a dispatcher for ecs systems.
let mut dispatch_builder =
DispatcherBuilder::new().with_pool(Arc::clone(&self.thread_pool));
crate::add_local_systems(&mut dispatch_builder);
// TODO: Consider alternative ways to do this
add_local_systems(&mut dispatch_builder);
add_foreign_systems(&mut dispatch_builder);
// This dispatches all the systems in parallel.
let mut dispatcher = dispatch_builder.build();
drop(guard);
span!(guard, "run systems");
run_now::<crate::interpolation::InterpolationSystem>(&self.ecs);
dispatcher.dispatch(&self.ecs);
drop(guard);

View File

@ -7,7 +7,6 @@ version = "0.9.0"
[features]
tracy = ["common/tracy"]
simd = ["vek/platform_intrinsics"]
plugins = ["toml", "tar", "wasmer", "bincode", "plugin-api"]
default = ["simd"]
@ -31,16 +30,5 @@ slab = "0.4.2"
# ECS
specs = { git = "https://github.com/amethyst/specs.git", features = ["serde", "storage-event-control", "derive"], rev = "5a9b71035007be0e3574f35184acac1cd4530496" }
# Serde
serde = { version = "1.0.110", features = ["derive"] }
# Plugins
scopeguard = "1.1.0"
toml = { version = "0.5.7", optional = true }
tar = { version = "0.4.30", optional = true }
wasmer = { version = "1.0.0", optional = true, default-features = false, features = ["wat", "default-cranelift", "default-jit"] }
bincode = { version = "1.3.1", optional = true }
plugin-api = { package = "veloren-plugin-api", path = "../../plugin/api", optional = true }
# Tweak running code
#inline_tweak = { version = "1.0.8", features = ["release_tweak"] }

View File

@ -11,35 +11,37 @@ use specs::{
};
#[derive(SystemData)]
pub struct InterpolationSystemData<'a> {
pub struct ReadData<'a> {
time: Read<'a, Time>,
player: Read<'a, PlayerEntity>,
entities: Entities<'a>,
pos: WriteStorage<'a, Pos>,
pos_interpdata: ReadStorage<'a, <Pos as InterpolatableComponent>::InterpData>,
vel: WriteStorage<'a, Vel>,
vel_interpdata: ReadStorage<'a, <Vel as InterpolatableComponent>::InterpData>,
ori: WriteStorage<'a, Ori>,
ori_interpdata: ReadStorage<'a, <Ori as InterpolatableComponent>::InterpData>,
}
#[derive(Default)]
pub struct InterpolationSystem;
pub struct Sys;
impl<'a> System<'a> for InterpolationSystem {
type SystemData = InterpolationSystemData<'a>;
impl<'a> System<'a> for Sys {
type SystemData = (
ReadData<'a>,
WriteStorage<'a, Pos>,
WriteStorage<'a, Vel>,
WriteStorage<'a, Ori>,
);
const NAME: &'static str = "interpolation";
const ORIGIN: Origin = Origin::Common;
const PHASE: Phase = Phase::Apply;
fn run(_job: &mut Job<Self>, mut data: InterpolationSystemData<'a>) {
fn run(_job: &mut Job<Self>, (data, mut pos, mut vel, mut ori): Self::SystemData) {
let time = data.time.0;
let player = data.player.0;
(
&data.entities,
&mut data.pos,
&mut pos,
&data.pos_interpdata,
&data.vel_interpdata,
)
@ -54,7 +56,7 @@ impl<'a> System<'a> for InterpolationSystem {
*pos = pos.interpolate(interp, time, vel);
},
);
(&data.entities, &mut data.vel, &data.vel_interpdata)
(&data.entities, &mut vel, &data.vel_interpdata)
.par_join()
.filter(|(e, _, _)| Some(e) != player.as_ref())
.for_each_init(
@ -66,7 +68,7 @@ impl<'a> System<'a> for InterpolationSystem {
*vel = vel.interpolate(interp, time, &());
},
);
(&data.entities, &mut data.ori, &data.ori_interpdata)
(&data.entities, &mut ori, &data.ori_interpdata)
.par_join()
.filter(|(e, _, _)| Some(e) != player.as_ref())
.for_each_init(

View File

@ -1,4 +1,4 @@
#![feature(label_break_value, bool_to_option, option_unwrap_none, array_map)]
#![feature(bool_to_option, option_unwrap_none, array_map)]
#![allow(clippy::option_map_unit_fn)]
mod aura;
@ -6,14 +6,12 @@ mod beam;
mod buff;
pub mod character_behavior;
pub mod controller;
mod interpolation;
pub mod interpolation;
pub mod melee;
mod mount;
pub mod phys;
#[cfg(feature = "plugins")] pub mod plugin;
pub mod projectile;
mod shockwave;
pub mod state;
mod stats;
// External
@ -21,12 +19,14 @@ use common_ecs::{dispatch, System};
use specs::DispatcherBuilder;
pub fn add_local_systems(dispatch_builder: &mut DispatcherBuilder) {
dispatch::<interpolation::Sys>(dispatch_builder, &[]);
dispatch::<mount::Sys>(dispatch_builder, &[]);
dispatch::<controller::Sys>(dispatch_builder, &[&mount::Sys::sys_name()]);
dispatch::<character_behavior::Sys>(dispatch_builder, &[&controller::Sys::sys_name()]);
dispatch::<stats::Sys>(dispatch_builder, &[]);
dispatch::<buff::Sys>(dispatch_builder, &[]);
dispatch::<phys::Sys>(dispatch_builder, &[
&interpolation::Sys::sys_name(),
&controller::Sys::sys_name(),
&mount::Sys::sys_name(),
&stats::Sys::sys_name(),

View File

@ -8,7 +8,7 @@ edition = "2018"
worldgen = []
tracy = ["common/tracy", "common-base/tracy", "common-ecs/tracy", "common-sys/tracy", "common-net/tracy", "world/tracy"]
simd = ["vek/platform_intrinsics"]
plugins = ["common-sys/plugins"]
plugins = ["common-state/plugins"]
default = ["worldgen", "plugins", "simd"]
@ -16,6 +16,7 @@ default = ["worldgen", "plugins", "simd"]
common = { package = "veloren-common", path = "../common" }
common-base = { package = "veloren-common-base", path = "../common/base" }
common-ecs = { package = "veloren-common-ecs", path = "../common/ecs" }
common-state = { package = "veloren-common-state", path = "../common/state" }
common-sys = { package = "veloren-common-sys", path = "../common/sys" }
common-net = { package = "veloren-common-net", path = "../common/net" }
world = { package = "veloren-world", path = "../world" }

View File

@ -34,7 +34,7 @@ use common_net::{
msg::{DisconnectReason, Notification, PlayerListUpdate, ServerGeneral},
sync::WorldSyncExt,
};
use common_sys::state::{BuildAreaError, BuildAreas};
use common_state::{BuildAreaError, BuildAreas};
use core::{convert::TryFrom, ops::Not, time::Duration};
use hashbrown::HashSet;
use rand::Rng;

View File

@ -29,7 +29,7 @@ use common::{
Damage, DamageSource, Explosion, GroupTarget, RadiusEffect,
};
use common_net::{msg::ServerGeneral, sync::WorldSyncExt};
use common_sys::state::BlockChange;
use common_state::BlockChange;
use comp::chat::GenericChatMsg;
use hashbrown::HashSet;
use specs::{join::Join, saveload::MarkerAllocator, Entity as EcsEntity, WorldExt};

View File

@ -9,7 +9,7 @@ use common::{
uid::Uid,
};
use common_net::{msg::ServerGeneral, sync::WorldSyncExt};
use common_sys::state::State;
use common_state::State;
use specs::{
world::{Entity, WorldExt},
ReadStorage, WriteStorage,

View File

@ -17,7 +17,7 @@ use common::{
vol::ReadVol,
};
use common_net::{msg::ServerGeneral, sync::WorldSyncExt};
use common_sys::state::State;
use common_state::State;
use comp::LightEmitter;
use crate::{client::Client, Server, StateExt};

View File

@ -10,7 +10,7 @@ use common::{
};
use common_base::span;
use common_net::msg::{PlayerListUpdate, PresenceKind, ServerGeneral};
use common_sys::state::State;
use common_state::State;
use specs::{saveload::MarkerAllocator, Builder, Entity as EcsEntity, WorldExt};
use tracing::{debug, error, trace, warn, Instrument};

View File

@ -77,10 +77,11 @@ use common_net::{
sync::WorldSyncExt,
};
#[cfg(feature = "plugins")]
use common_sys::plugin::memory_manager::EcsWorld;
use common_state::plugin::memory_manager::EcsWorld;
#[cfg(feature = "plugins")]
use common_sys::plugin::PluginMgr;
use common_sys::state::{BuildAreas, State};
use common_state::plugin::PluginMgr;
use common_state::{BuildAreas, State};
use common_sys::add_local_systems;
use metrics::{EcsSystemMetrics, PhysicsMetrics, TickMetrics};
use network::{Network, Pid, ProtocolAddr};
use persistence::{
@ -516,6 +517,7 @@ impl Server {
// in sys/terrain.rs
self.state.tick(
dt,
add_local_systems,
|dispatcher_builder| {
sys::msg::add_server_systems(dispatcher_builder);
sys::add_server_systems(dispatcher_builder);

View File

@ -2,9 +2,9 @@ use crate::settings::BanRecord;
use authc::{AuthClient, AuthClientError, AuthToken, Uuid};
use common_net::msg::RegisterError;
#[cfg(feature = "plugins")]
use common_sys::plugin::memory_manager::EcsWorld;
use common_state::plugin::memory_manager::EcsWorld;
#[cfg(feature = "plugins")]
use common_sys::plugin::PluginMgr;
use common_state::plugin::PluginMgr;
use hashbrown::{HashMap, HashSet};
use plugin_api::event::{PlayerJoinEvent, PlayerJoinResult};
use specs::Component;

View File

@ -14,7 +14,7 @@ use common::{
vol::RectRasterableVol,
};
use common_ecs::{dispatch, System};
use common_sys::state::State;
use common_state::State;
use rand::prelude::*;
use slab::Slab;
use specs::{DispatcherBuilder, WorldExt};

View File

@ -18,7 +18,7 @@ use common_net::{
msg::{CharacterInfo, PlayerListUpdate, PresenceKind, ServerGeneral},
sync::WorldSyncExt,
};
use common_sys::state::State;
use common_state::State;
use rand::prelude::*;
use specs::{
saveload::MarkerAllocator, Builder, Entity as EcsEntity, EntityBuilder as EcsEntityBuilder,

View File

@ -12,7 +12,7 @@ pub mod terrain_sync;
pub mod waypoint;
use common_ecs::{dispatch, run_now, System};
use common_sys::{melee, projectile};
use common_sys::{interpolation, melee, projectile};
use specs::DispatcherBuilder;
use std::{
marker::PhantomData,
@ -23,7 +23,7 @@ pub type PersistenceScheduler = SysScheduler<persistence::Sys>;
pub fn add_server_systems(dispatch_builder: &mut DispatcherBuilder) {
dispatch::<melee::Sys>(dispatch_builder, &[&projectile::Sys::sys_name()]);
dispatch::<agent::Sys>(dispatch_builder, &[]);
dispatch::<agent::Sys>(dispatch_builder, &[&interpolation::Sys::sys_name()]);
dispatch::<terrain::Sys>(dispatch_builder, &[]);
dispatch::<waypoint::Sys>(dispatch_builder, &[]);
dispatch::<invite_timeout::Sys>(dispatch_builder, &[]);

View File

@ -10,7 +10,7 @@ use common::{
};
use common_ecs::{Job, Origin, Phase, System};
use common_net::msg::{ClientGeneral, PresenceKind, ServerGeneral};
use common_sys::state::{BlockChange, BuildAreas};
use common_state::{BlockChange, BuildAreas};
use specs::{Entities, Join, Read, ReadExpect, ReadStorage, Write, WriteStorage};
use tracing::{debug, trace, warn};

View File

@ -20,10 +20,10 @@ use specs::{Entities, Join, Read, ReadExpect, ReadStorage, WriteExpect, WriteSto
use tracing::trace;
#[cfg(feature = "plugins")]
use common_sys::plugin::memory_manager::EcsWorld;
use common_state::plugin::memory_manager::EcsWorld;
#[cfg(feature = "plugins")]
use common_sys::plugin::PluginMgr;
use common_state::plugin::PluginMgr;
#[cfg(feature = "plugins")]
type ReadPlugin<'a> = Read<'a, PluginMgr>;

View File

@ -15,7 +15,7 @@ use common::{
};
use common_ecs::{Job, Origin, Phase, System};
use common_net::msg::ServerGeneral;
use common_sys::state::TerrainChanges;
use common_state::TerrainChanges;
use comp::Behavior;
use specs::{Join, Read, ReadStorage, Write, WriteExpect};
use std::sync::Arc;

View File

@ -2,7 +2,7 @@ use crate::{client::Client, presence::Presence};
use common::{comp::Pos, terrain::TerrainGrid};
use common_ecs::{Job, Origin, Phase, System};
use common_net::msg::ServerGeneral;
use common_sys::state::TerrainChanges;
use common_state::TerrainChanges;
use specs::{Join, Read, ReadExpect, ReadStorage};
/// This systems sends new chunks to clients as well as changes to existing

View File

@ -24,7 +24,7 @@ gl = ["gfx_device_gl", "gfx_gl"]
hot-anim = ["anim/use-dyn-lib"]
singleplayer = ["server"]
simd = ["vek/platform_intrinsics"]
tracy = ["common/tracy", "common-ecs/tracy", "common-frontend/tracy", "common-net/tracy", "common-sys/tracy", "client/tracy"]
tracy = ["common/tracy", "common-ecs/tracy", "common-frontend/tracy", "common-net/tracy", "common-state/tracy", "client/tracy"]
plugins = ["client/plugins"]
default = ["gl", "singleplayer", "native-dialog", "plugins", "simd"]
@ -36,6 +36,7 @@ common-base = {package = "veloren-common-base", path = "../common/base"}
common-ecs = {package = "veloren-common-ecs", path = "../common/ecs"}
common-frontend = {package = "veloren-common-frontend", path = "../common/frontend"}
common-net = {package = "veloren-common-net", path = "../common/net"}
common-state = {package = "veloren-common-state", path = "../common/state"}
common-sys = {package = "veloren-common-sys", path = "../common/sys"}
anim = {package = "veloren-voxygen-anim", path = "anim"}

View File

@ -8,7 +8,7 @@ use common::{
assets::{self, AssetExt, AssetHandle},
vol::ReadVol,
};
use common_sys::state::State;
use common_state::State;
use serde::Deserialize;
use std::time::Instant;
use tracing::warn;

View File

@ -49,7 +49,7 @@ use common::{
assets::{self, AssetExt, AssetHandle},
terrain::{BiomeKind, SitesKind},
};
use common_sys::state::State;
use common_state::State;
use hashbrown::HashMap;
use rand::{prelude::SliceRandom, thread_rng, Rng};
use serde::Deserialize;

View File

@ -14,7 +14,7 @@ use common::{
terrain::TerrainChunk,
vol::{ReadVol, RectRasterableVol},
};
use common_sys::state::State;
use common_state::State;
use hashbrown::HashMap;
use rand::{thread_rng, Rng};
use std::time::{Duration, Instant};

View File

@ -13,7 +13,7 @@ use common::{
terrain::TerrainChunk,
vol::ReadVol,
};
use common_sys::state::State;
use common_state::State;
use hashbrown::HashMap;
use specs::{Entity as EcsEntity, Join, WorldExt};
use std::time::{Duration, Instant};

View File

@ -17,7 +17,7 @@ use common::{
terrain::TerrainChunk,
vol::ReadVol,
};
use common_sys::state::State;
use common_state::State;
use hashbrown::HashMap;
use specs::{Entity as EcsEntity, Join, WorldExt};
use std::time::{Duration, Instant};

View File

@ -5,7 +5,7 @@ mod movement;
use client::Client;
use common::terrain::TerrainChunk;
use common_sys::state::State;
use common_state::State;
use block::BlockEventMapper;
use campfire::CampfireEventMapper;

View File

@ -14,7 +14,7 @@ use common::{
terrain::{BlockKind, TerrainChunk},
vol::ReadVol,
};
use common_sys::state::State;
use common_state::State;
use hashbrown::HashMap;
use specs::{Entity as EcsEntity, Join, WorldExt};
use std::time::{Duration, Instant};

View File

@ -98,7 +98,7 @@ use common::{
outcome::Outcome,
terrain::{BlockKind, TerrainChunk},
};
use common_sys::state::State;
use common_state::State;
use event_mapper::SfxEventMapper;
use hashbrown::HashMap;
use rand::prelude::*;

View File

@ -39,7 +39,7 @@ use common::{
vol::RectRasterableVol,
};
use common_base::span;
use common_sys::state::State;
use common_state::State;
use core::{
borrow::Borrow,
convert::TryFrom,

View File

@ -32,7 +32,7 @@ use common::{
vol::ReadVol,
};
use common_base::span;
use common_sys::state::State;
use common_state::State;
use comp::item::Reagent;
use num::traits::{Float, FloatConst};
use specs::{Entity as EcsEntity, Join, WorldExt};