veloren/common/systems/tests/predict_controller/utils.rs
Marcel Märtens 4343dd3aea add unit tests for replication system
- make tracy experience better by adding a 0.05 to client local TIME.
- fix an error that the look_dir was wrongly predicted
- add a jump graph for testing
- update in_game code that was commented out in system
- track the simulation ahead on the debug menu
- add simulated lag with `sudo tc qdisc replace dev lo root netem delay 700ms 10ms 25%`
add basic tests for phys
2023-03-16 00:29:32 +01:00

100 lines
3.0 KiB
Rust

use common::{
comp::{
inventory::item::MaterialStatManifest, tool::AbilityMap, ControlCommand, ControlCommands,
Controller, RemoteController,
},
resources::{DeltaTime, GameMode, Time},
terrain::{MapSizeLg, TerrainChunk},
};
use common_ecs::dispatch;
use common_net::sync::WorldSyncExt;
use common_state::State;
use hashbrown::HashSet;
use specs::{Builder, Entity, WorldExt};
use std::{error::Error, sync::Arc, time::Duration};
use vek::Vec2;
use veloren_common_systems::predict_controller;
pub const DTT: f64 = 1.0 / 10.0;
pub const DT: Duration = Duration::from_millis(100);
const DEFAULT_WORLD_CHUNKS_LG: MapSizeLg =
if let Ok(map_size_lg) = MapSizeLg::new(Vec2 { x: 10, y: 10 }) {
map_size_lg
} else {
panic!("Default world chunk size does not satisfy required invariants.");
};
pub fn setup() -> State {
let pools = State::pools(GameMode::Server);
let mut state = State::new(
GameMode::Server,
pools,
DEFAULT_WORLD_CHUNKS_LG,
Arc::new(TerrainChunk::water(0)),
);
state.ecs_mut().insert(MaterialStatManifest::with_empty());
state.ecs_mut().insert(AbilityMap::load().cloned());
state.ecs_mut().read_resource::<Time>();
state.ecs_mut().read_resource::<DeltaTime>();
state
}
pub fn tick(state: &mut State, dt: Duration) {
state.tick(
dt,
|dispatch_builder| {
dispatch::<predict_controller::Sys>(dispatch_builder, &[]);
},
false,
);
}
pub fn push_remote(
state: &mut State,
entity: Entity,
command: ControlCommand,
) -> Result<u64, Box<dyn Error>> {
let mut storage = state.ecs_mut().write_storage::<RemoteController>();
let remote_controller = storage.get_mut(entity).ok_or(Box::new(std::io::Error::new(
std::io::ErrorKind::Other,
"Storage does not contain Entity RemoteController",
)))?;
remote_controller
.push(command)
.ok_or(Box::new(std::io::Error::new(
std::io::ErrorKind::Other,
"Command couldn't be pushed",
)))
}
pub fn get_controller(state: &State, entity: Entity) -> Result<Controller, Box<dyn Error>> {
let storage = state.ecs().read_storage::<Controller>();
let controller = storage.get(entity).ok_or(Box::new(std::io::Error::new(
std::io::ErrorKind::Other,
"Storage does not contain Entity Controller",
)))?;
Ok(controller.clone())
}
#[allow(dead_code)]
pub fn push_remotes(state: &mut State, entity: Entity, commands: ControlCommands) -> HashSet<u64> {
let mut storage = state.ecs_mut().write_storage::<RemoteController>();
let remote_controller = storage.get_mut(entity).unwrap();
remote_controller.append(commands)
}
pub fn create_player(state: &mut State) -> Entity {
let remote_controller = RemoteController::default();
let controller = Controller::default();
state
.ecs_mut()
.create_entity_synced()
.with(remote_controller)
.with(controller)
.build()
}