2021-08-12 09:48:00 +00:00
|
|
|
#[cfg(feature = "persistent_world")]
|
|
|
|
use crate::TerrainPersistence;
|
2023-04-02 17:37:38 +00:00
|
|
|
use crate::{client::Client, Settings};
|
2020-10-16 23:42:19 +00:00
|
|
|
use common::{
|
2021-04-14 19:51:03 +00:00
|
|
|
comp::{
|
2022-07-05 15:01:22 +00:00
|
|
|
Admin, AdminRole, CanBuild, ControlEvent, Controller, ForceUpdate, Health, Ori, Player,
|
2023-04-02 17:37:38 +00:00
|
|
|
Pos, Presence, PresenceKind, SkillSet, Vel,
|
2021-04-14 19:51:03 +00:00
|
|
|
},
|
2020-10-16 23:42:19 +00:00
|
|
|
event::{EventBus, ServerEvent},
|
2022-01-17 09:47:29 +00:00
|
|
|
link::Is,
|
2023-04-17 13:38:41 +00:00
|
|
|
mounting::{Rider, VolumeRider},
|
2023-06-22 11:25:08 +00:00
|
|
|
resources::{DeltaTime, PlayerPhysicsSetting, PlayerPhysicsSettings},
|
2022-09-16 02:38:13 +00:00
|
|
|
slowjob::SlowJobPool,
|
2021-03-11 12:07:03 +00:00
|
|
|
terrain::TerrainGrid,
|
|
|
|
vol::ReadVol,
|
2020-10-16 23:42:19 +00:00
|
|
|
};
|
2021-03-08 22:40:02 +00:00
|
|
|
use common_ecs::{Job, Origin, Phase, System};
|
2023-04-02 17:37:38 +00:00
|
|
|
use common_net::msg::{ClientGeneral, ServerGeneral};
|
2023-05-15 22:40:53 +00:00
|
|
|
use common_state::{AreasContainer, BlockChange, BuildArea};
|
2022-09-16 02:38:13 +00:00
|
|
|
use core::mem;
|
|
|
|
use rayon::prelude::*;
|
2023-09-15 20:30:54 +00:00
|
|
|
use specs::{Entities, Join, LendJoin, Read, ReadExpect, ReadStorage, Write, WriteStorage};
|
2022-09-16 02:38:13 +00:00
|
|
|
use std::{borrow::Cow, time::Instant};
|
2023-07-01 08:40:57 +00:00
|
|
|
use tracing::{debug, trace, warn};
|
2021-04-30 13:06:07 +00:00
|
|
|
use vek::*;
|
2020-10-16 23:42:19 +00:00
|
|
|
|
2021-08-12 09:48:00 +00:00
|
|
|
#[cfg(feature = "persistent_world")]
|
|
|
|
pub type TerrainPersistenceData<'a> = Option<Write<'a, TerrainPersistence>>;
|
|
|
|
#[cfg(not(feature = "persistent_world"))]
|
2022-09-16 02:38:13 +00:00
|
|
|
pub type TerrainPersistenceData<'a> = core::marker::PhantomData<&'a mut ()>;
|
|
|
|
|
|
|
|
// NOTE: These writes are considered "rare", meaning (currently) that they are
|
|
|
|
// admin-gated features that players shouldn't normally access, and which we're
|
|
|
|
// not that concerned about the performance of when two players try to use them
|
|
|
|
// at once.
|
|
|
|
//
|
|
|
|
// In such cases, we're okay putting them behind a mutex and penalizing the
|
|
|
|
// system if they're actually used concurrently by lots of users. Please do not
|
|
|
|
// put less rare writes here, unless you want to serialize the system!
|
|
|
|
struct RareWrites<'a, 'b> {
|
|
|
|
block_changes: &'b mut BlockChange,
|
|
|
|
_terrain_persistence: &'b mut TerrainPersistenceData<'a>,
|
|
|
|
}
|
2021-08-12 09:48:00 +00:00
|
|
|
|
2020-10-16 23:42:19 +00:00
|
|
|
impl Sys {
|
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
|
|
fn handle_client_in_game_msg(
|
|
|
|
server_emitter: &mut common::event::Emitter<'_, ServerEvent>,
|
|
|
|
entity: specs::Entity,
|
2020-11-02 18:30:56 +00:00
|
|
|
client: &Client,
|
2020-10-30 16:39:53 +00:00
|
|
|
maybe_presence: &mut Option<&mut Presence>,
|
2020-10-16 23:42:19 +00:00
|
|
|
terrain: &ReadExpect<'_, TerrainGrid>,
|
|
|
|
can_build: &ReadStorage<'_, CanBuild>,
|
2022-01-17 09:47:29 +00:00
|
|
|
is_rider: &ReadStorage<'_, Is<Rider>>,
|
2023-04-17 13:38:41 +00:00
|
|
|
is_volume_rider: &ReadStorage<'_, Is<VolumeRider>>,
|
2023-06-22 11:25:08 +00:00
|
|
|
force_update: Option<&&mut ForceUpdate>,
|
2022-09-16 02:38:13 +00:00
|
|
|
skill_set: &mut Option<Cow<'_, SkillSet>>,
|
2020-10-31 22:34:08 +00:00
|
|
|
healths: &ReadStorage<'_, Health>,
|
2022-09-16 02:38:13 +00:00
|
|
|
rare_writes: &parking_lot::Mutex<RareWrites<'_, '_>>,
|
|
|
|
position: Option<&mut Pos>,
|
|
|
|
controller: Option<&mut Controller>,
|
2020-10-16 23:42:19 +00:00
|
|
|
settings: &Read<'_, Settings>,
|
2023-05-15 22:40:53 +00:00
|
|
|
build_areas: &Read<'_, AreasContainer<BuildArea>>,
|
2022-09-16 02:38:13 +00:00
|
|
|
player_physics_setting: Option<&mut PlayerPhysicsSetting>,
|
2021-04-29 16:46:40 +00:00
|
|
|
maybe_admin: &Option<&Admin>,
|
2022-08-21 04:48:51 +00:00
|
|
|
time_for_vd_changes: Instant,
|
2020-10-16 23:42:19 +00:00
|
|
|
msg: ClientGeneral,
|
2023-06-22 09:43:02 +00:00
|
|
|
player_physics: &mut Option<(Pos, Vel, Ori)>,
|
2020-10-16 23:42:19 +00:00
|
|
|
) -> Result<(), crate::error::Error> {
|
2022-08-22 03:21:39 +00:00
|
|
|
let presence = match maybe_presence.as_deref_mut() {
|
2020-10-30 16:39:53 +00:00
|
|
|
Some(g) => g,
|
|
|
|
None => {
|
|
|
|
debug!(?entity, "client is not in_game, ignoring msg");
|
|
|
|
trace!(?msg, "ignored msg content");
|
|
|
|
return Ok(());
|
|
|
|
},
|
|
|
|
};
|
2020-10-16 23:42:19 +00:00
|
|
|
match msg {
|
|
|
|
// Go back to registered state (char selection screen)
|
|
|
|
ClientGeneral::ExitInGame => {
|
|
|
|
server_emitter.emit(ServerEvent::ExitIngame { entity });
|
2020-11-02 18:30:56 +00:00
|
|
|
client.send(ServerGeneral::ExitInGameSuccess)?;
|
2020-10-30 16:39:53 +00:00
|
|
|
*maybe_presence = None;
|
2020-10-16 23:42:19 +00:00
|
|
|
},
|
2022-08-22 03:21:39 +00:00
|
|
|
ClientGeneral::SetViewDistance(view_distances) => {
|
|
|
|
let clamped_vds = view_distances.clamp(settings.max_view_distance);
|
|
|
|
|
|
|
|
presence.terrain_view_distance.set_target(clamped_vds.terrain, time_for_vd_changes);
|
|
|
|
presence.entity_view_distance.set_target(clamped_vds.entity, time_for_vd_changes);
|
2020-10-16 23:42:19 +00:00
|
|
|
|
2022-08-21 04:48:51 +00:00
|
|
|
// Correct client if its requested VD is too high.
|
2022-08-22 03:21:39 +00:00
|
|
|
if view_distances.terrain != clamped_vds.terrain {
|
|
|
|
client.send(ServerGeneral::SetViewDistance(clamped_vds.terrain))?;
|
2020-10-16 23:42:19 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
ClientGeneral::ControllerInputs(inputs) => {
|
2022-02-23 03:26:15 +00:00
|
|
|
if presence.kind.controlling_char() {
|
2022-09-16 02:38:13 +00:00
|
|
|
if let Some(controller) = controller {
|
2021-02-19 23:45:48 +00:00
|
|
|
controller.inputs.update_with_new(*inputs);
|
2020-10-16 23:42:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ClientGeneral::ControlEvent(event) => {
|
2023-06-03 00:30:43 +00:00
|
|
|
if presence.kind.controlling_char() && let Some(controller) = controller {
|
2020-10-16 23:42:19 +00:00
|
|
|
// Skip respawn if client entity is alive
|
2023-06-03 00:30:43 +00:00
|
|
|
let skip_respawn = matches!(event, ControlEvent::Respawn)
|
|
|
|
&& healths.get(entity).map_or(true, |h| !h.is_dead);
|
|
|
|
|
|
|
|
if !skip_respawn {
|
2022-01-26 18:40:18 +00:00
|
|
|
controller.push_event(event);
|
2020-10-16 23:42:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ClientGeneral::ControlAction(event) => {
|
2022-02-23 03:26:15 +00:00
|
|
|
if presence.kind.controlling_char() {
|
2022-09-16 02:38:13 +00:00
|
|
|
if let Some(controller) = controller {
|
2022-01-26 18:52:19 +00:00
|
|
|
controller.push_action(event);
|
2020-10-16 23:42:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2022-07-31 21:01:10 +00:00
|
|
|
ClientGeneral::PlayerPhysics { pos, vel, ori, force_counter } => {
|
2022-02-23 03:26:15 +00:00
|
|
|
if presence.kind.controlling_char()
|
2023-06-22 11:25:08 +00:00
|
|
|
&& force_update.map_or(true, |force_update| force_update.counter() == force_counter)
|
2020-10-31 22:34:08 +00:00
|
|
|
&& healths.get(entity).map_or(true, |h| !h.is_dead)
|
2022-01-17 09:47:29 +00:00
|
|
|
&& is_rider.get(entity).is_none()
|
2023-04-17 13:38:41 +00:00
|
|
|
&& is_volume_rider.get(entity).is_none()
|
2021-04-14 21:55:19 +00:00
|
|
|
&& player_physics_setting
|
|
|
|
.as_ref()
|
|
|
|
.map_or(true, |s| s.client_authoritative())
|
2020-10-30 16:39:53 +00:00
|
|
|
{
|
2023-06-22 09:43:02 +00:00
|
|
|
*player_physics = Some((pos, vel, ori));
|
2021-04-14 19:51:03 +00:00
|
|
|
}
|
2020-10-16 23:42:19 +00:00
|
|
|
},
|
|
|
|
ClientGeneral::BreakBlock(pos) => {
|
2021-03-23 08:37:29 +00:00
|
|
|
if let Some(comp_can_build) = can_build.get(entity) {
|
2021-03-27 05:53:33 +00:00
|
|
|
if comp_can_build.enabled {
|
2021-03-23 23:59:18 +00:00
|
|
|
for area in comp_can_build.build_areas.iter() {
|
2021-07-23 12:04:16 +00:00
|
|
|
if let Some(old_block) = build_areas
|
2021-04-09 07:34:58 +00:00
|
|
|
.areas()
|
2021-03-27 05:53:33 +00:00
|
|
|
.get(*area)
|
|
|
|
// TODO: Make this an exclusive check on the upper bound of the AABB
|
|
|
|
// Vek defaults to inclusive which is not optimal
|
|
|
|
.filter(|aabb| aabb.contains_point(pos))
|
|
|
|
.and_then(|_| terrain.get(pos).ok())
|
|
|
|
{
|
2021-07-23 12:04:16 +00:00
|
|
|
let new_block = old_block.into_vacant();
|
2022-09-16 02:38:13 +00:00
|
|
|
// Take the rare writes lock as briefly as possible.
|
|
|
|
let mut guard = rare_writes.lock();
|
|
|
|
let _was_set = guard.block_changes.try_set(pos, new_block).is_some();
|
2021-08-12 09:48:00 +00:00
|
|
|
#[cfg(feature = "persistent_world")]
|
|
|
|
if _was_set {
|
2022-09-16 02:38:13 +00:00
|
|
|
if let Some(terrain_persistence) = guard._terrain_persistence.as_mut()
|
2021-07-23 12:04:16 +00:00
|
|
|
{
|
|
|
|
terrain_persistence.set_block(pos, new_block);
|
|
|
|
}
|
|
|
|
}
|
2021-03-23 23:59:18 +00:00
|
|
|
}
|
2021-03-23 08:37:29 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-16 23:42:19 +00:00
|
|
|
}
|
|
|
|
},
|
2021-07-23 12:04:16 +00:00
|
|
|
ClientGeneral::PlaceBlock(pos, new_block) => {
|
2021-03-23 08:37:29 +00:00
|
|
|
if let Some(comp_can_build) = can_build.get(entity) {
|
2021-03-27 05:53:33 +00:00
|
|
|
if comp_can_build.enabled {
|
2021-03-23 23:59:18 +00:00
|
|
|
for area in comp_can_build.build_areas.iter() {
|
2021-03-27 05:53:33 +00:00
|
|
|
if build_areas
|
2021-04-09 07:34:58 +00:00
|
|
|
.areas()
|
2021-03-27 05:53:33 +00:00
|
|
|
.get(*area)
|
|
|
|
// TODO: Make this an exclusive check on the upper bound of the AABB
|
|
|
|
// Vek defaults to inclusive which is not optimal
|
|
|
|
.filter(|aabb| aabb.contains_point(pos))
|
|
|
|
.is_some()
|
|
|
|
{
|
2022-09-16 02:38:13 +00:00
|
|
|
// Take the rare writes lock as briefly as possible.
|
|
|
|
let mut guard = rare_writes.lock();
|
|
|
|
let _was_set = guard.block_changes.try_set(pos, new_block).is_some();
|
2021-08-12 09:48:00 +00:00
|
|
|
#[cfg(feature = "persistent_world")]
|
|
|
|
if _was_set {
|
2022-09-16 02:38:13 +00:00
|
|
|
if let Some(terrain_persistence) = guard._terrain_persistence.as_mut()
|
2021-07-23 12:04:16 +00:00
|
|
|
{
|
|
|
|
terrain_persistence.set_block(pos, new_block);
|
|
|
|
}
|
|
|
|
}
|
2021-03-23 23:59:18 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-23 08:37:29 +00:00
|
|
|
}
|
2020-10-16 23:42:19 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
ClientGeneral::UnlockSkill(skill) => {
|
2022-09-16 02:38:13 +00:00
|
|
|
// FIXME: How do we want to handle the error? Probably not by swallowing it.
|
|
|
|
let _ = skill_set.as_mut().map(|skill_set| {
|
|
|
|
SkillSet::unlock_skill_cow(skill_set, skill, |skill_set| skill_set.to_mut())
|
|
|
|
}).transpose();
|
2020-10-16 23:42:19 +00:00
|
|
|
},
|
Implement /price_list (work in progress), stub for /buy and /sell
remove outdated economic simulation code
remove old values, document
add natural resources to economy
Remove NaturalResources from Place (now in Economy)
find closest site to each chunk
implement natural resources (the distance scale is wrong)
cargo fmt
working distance calculation
this collection of natural resources seem to make sense, too much Wheat though
use natural resources and controlled area to replenish goods
increase the amount of chunks controlled by one guard to 50
add new professions and goods to the list
implement multiple products per worker
remove the old code and rename the new code to the previous name
correctly determine which goods guards will give you access to
correctly estimate the amount of natural resources controlled
adapt to new server API
instrument tooltips
Now I just need to figure out how to store a (reference to) a closure
closures for tooltip content generation
pass site/cave id to the client
Add economic information to the client structure
(not yet exchanged with the server)
Send SiteId to the client, prepare messages for economy request
Make client::sites a HashMap
Specialize the Crafter into Brewer,Bladesmith and Blacksmith
working server request for economic info from within tooltip
fully operational economic tooltips
I need to fix the id clash between caves and towns though
fix overlapping ids between caves and sites
display stock amount
correctly handle invalid (cave) ids in the request
some initial balancing, turn off most info logging
less intrusive way of implementing the dynamic tool tips in map
further tooltip cleanup
further cleanup, dynamic tooltip not fully working as intended
correctly working tooltip visibility logic
cleanup, display labor value
separate economy info request in a separate translation unit
display values as well
nicer display format for economy
add last_exports and coin to the new economy
do not allocate natural resources to Dungeons (making town so much larger)
balancing attempt
print town size statistics
cargo fmt (dead code)
resource tweaks, csv debugging
output a more interesting town (and then all sites)
fix the labor value logic (now we have meaningful prices)
load professions from ron (WIP)
use assets manager in economy
loading professions works
use professions from ron file
fix Labor debug logic
count chunks per type separately
(preparing for better resource control)
better structured resource data
traders, more professions (WIP)
fix exception when starting the simulation
fix replenish function
TODO:
- use area_ratio for resource usage (chunks should be added to stock, ratio on usage?)
- fix trading
documentation clean up
fix merge artifact
Revise trader mechanic
start Coin with a reasonable default
remove the outdated economy code
preserve documentation from removed old structure
output neighboring sites (preparation)
pass list of neighbors to economy
add trade structures
trading stub
Description of purpose by zesterer on Discord
remember prices (needed for planning)
avoid growing the order vector unboundedly
into_iter doesn't clear the Vec, so clear it manually
use drain to process Vecs, avoid clone
fix the test server
implement a test stub (I need to get it faster than 30 seconds to be more useful)
enable info output in test
debug missing and extra goods
use the same logging extension as water, activate feature
update dependencies
determine good prices, good exchange goods
a working set of revisions
a cozy world which economy tests in 2s
first order planning version
fun with package version
buy according to value/priority, not missing amount
introduce min price constant, fix order priority
in depth debugging
with a correct sign the trading plans start to make sense
move the trade planning to a separate function
rename new function
reorganize code into subroutines (much cleaner)
each trading step now has its own function
cut down the number of debugging output
introduce RoadSecurity and Transportation
transport capacity bookkeeping
only plan to pay with valuable goods, you can no longer stockpile unused options
(which interestingly shows a huge impact, to be investigated)
Coin is now listed as a payment (although not used)
proper transportation estimation (although 0)
remove more left overs uncovered by viewing the code in a merge request
use the old default values, handle non-pileable stocks directly before increasing it
(as economy is based on last year's products)
don't order the missing good multiple times
also it uses coin to buy things!
fix warnings and use the transportation from stock again
cargo fmt
prepare evaluation of trade
don't count transportation multiple times
fix merge artifact
operational trade planning
trade itself is still misleading
make clippy happy
clean up
correct labor ratio of merchants (no need to multiply with amount produced)
incomplete merchant labor_value computation
correct last commit
make economy of scale more explicit
make clippy happy (and code cleaner)
more merchant tweaks (more pop=better)
beginning of real trading code
revert the update of dependencies
remove stale comments/unused code
trading implementation complete (but untested)
something is still strange ...
fix sign in trading
another sign fix
some bugfixes and plenty of debugging code
another bug fixed, more to go
fix another invariant (rounding will lead to very small negative value)
introduce Terrain and Territory
fix merge mistakes
2021-03-14 03:18:32 +00:00
|
|
|
ClientGeneral::RequestSiteInfo(id) => {
|
|
|
|
server_emitter.emit(ServerEvent::RequestSiteInfo { entity, id });
|
|
|
|
},
|
2021-04-14 19:51:03 +00:00
|
|
|
ClientGeneral::RequestPlayerPhysics {
|
|
|
|
server_authoritative,
|
|
|
|
} => {
|
|
|
|
if let Some(setting) = player_physics_setting {
|
|
|
|
setting.client_optin = server_authoritative;
|
|
|
|
}
|
|
|
|
},
|
2021-05-01 18:28:20 +00:00
|
|
|
ClientGeneral::RequestLossyTerrainCompression {
|
|
|
|
lossy_terrain_compression,
|
|
|
|
} => {
|
|
|
|
presence.lossy_terrain_compression = lossy_terrain_compression;
|
|
|
|
},
|
2022-02-20 10:10:18 +00:00
|
|
|
ClientGeneral::UpdateMapMarker(update) => {
|
|
|
|
server_emitter.emit(ServerEvent::UpdateMapMarker { entity, update });
|
|
|
|
},
|
2022-07-05 15:01:22 +00:00
|
|
|
ClientGeneral::SpectatePosition(pos) => {
|
|
|
|
if let Some(admin) = maybe_admin && admin.0 >= AdminRole::Moderator && presence.kind == PresenceKind::Spectator {
|
2022-09-16 02:38:13 +00:00
|
|
|
if let Some(position) = position {
|
2022-07-05 15:01:22 +00:00
|
|
|
position.0 = pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2021-11-11 03:24:08 +00:00
|
|
|
ClientGeneral::RequestCharacterList
|
|
|
|
| ClientGeneral::CreateCharacter { .. }
|
2021-12-13 00:13:33 +00:00
|
|
|
| ClientGeneral::EditCharacter { .. }
|
2021-11-11 03:24:08 +00:00
|
|
|
| ClientGeneral::DeleteCharacter(_)
|
2022-08-22 03:21:39 +00:00
|
|
|
| ClientGeneral::Character(_, _)
|
|
|
|
| ClientGeneral::Spectate(_)
|
2021-11-11 03:24:08 +00:00
|
|
|
| ClientGeneral::TerrainChunkRequest { .. }
|
2022-05-08 23:53:19 +00:00
|
|
|
| ClientGeneral::LodZoneRequest { .. }
|
2021-11-11 03:24:08 +00:00
|
|
|
| ClientGeneral::ChatMsg(_)
|
|
|
|
| ClientGeneral::Command(..)
|
2022-02-27 23:08:47 +00:00
|
|
|
| ClientGeneral::Terminate => {
|
|
|
|
debug!("Kicking possibly misbehaving client due to invalid client in game request");
|
|
|
|
server_emitter.emit(ServerEvent::ClientDisconnect(
|
|
|
|
entity,
|
|
|
|
common::comp::DisconnectReason::NetworkError,
|
|
|
|
));
|
|
|
|
},
|
2020-10-16 23:42:19 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This system will handle new messages from clients
|
2021-03-04 14:00:16 +00:00
|
|
|
#[derive(Default)]
|
2020-10-16 23:42:19 +00:00
|
|
|
pub struct Sys;
|
2021-03-08 11:13:59 +00:00
|
|
|
impl<'a> System<'a> for Sys {
|
2020-10-18 22:57:43 +00:00
|
|
|
#[allow(clippy::type_complexity)]
|
2020-10-16 23:42:19 +00:00
|
|
|
type SystemData = (
|
|
|
|
Entities<'a>,
|
|
|
|
Read<'a, EventBus<ServerEvent>>,
|
|
|
|
ReadExpect<'a, TerrainGrid>,
|
2022-09-16 02:38:13 +00:00
|
|
|
ReadExpect<'a, SlowJobPool>,
|
2020-10-16 23:42:19 +00:00
|
|
|
ReadStorage<'a, CanBuild>,
|
2023-06-22 11:25:08 +00:00
|
|
|
WriteStorage<'a, ForceUpdate>,
|
2022-01-17 09:47:29 +00:00
|
|
|
ReadStorage<'a, Is<Rider>>,
|
2023-04-17 13:38:41 +00:00
|
|
|
ReadStorage<'a, Is<VolumeRider>>,
|
2021-04-14 15:35:34 +00:00
|
|
|
WriteStorage<'a, SkillSet>,
|
2020-10-31 22:34:08 +00:00
|
|
|
ReadStorage<'a, Health>,
|
2020-10-16 23:42:19 +00:00
|
|
|
Write<'a, BlockChange>,
|
|
|
|
WriteStorage<'a, Pos>,
|
|
|
|
WriteStorage<'a, Vel>,
|
|
|
|
WriteStorage<'a, Ori>,
|
2020-10-30 16:39:53 +00:00
|
|
|
WriteStorage<'a, Presence>,
|
2020-10-16 23:42:19 +00:00
|
|
|
WriteStorage<'a, Client>,
|
|
|
|
WriteStorage<'a, Controller>,
|
2023-06-22 11:25:08 +00:00
|
|
|
Read<'a, DeltaTime>,
|
2020-10-16 23:42:19 +00:00
|
|
|
Read<'a, Settings>,
|
2023-05-15 22:40:53 +00:00
|
|
|
Read<'a, AreasContainer<BuildArea>>,
|
2021-04-14 19:51:03 +00:00
|
|
|
Write<'a, PlayerPhysicsSettings>,
|
2021-08-12 09:48:00 +00:00
|
|
|
TerrainPersistenceData<'a>,
|
2021-04-14 19:51:03 +00:00
|
|
|
ReadStorage<'a, Player>,
|
2021-04-29 16:46:40 +00:00
|
|
|
ReadStorage<'a, Admin>,
|
2020-10-16 23:42:19 +00:00
|
|
|
);
|
|
|
|
|
2021-03-04 14:00:16 +00:00
|
|
|
const NAME: &'static str = "msg::in_game";
|
|
|
|
const ORIGIN: Origin = Origin::Server;
|
|
|
|
const PHASE: Phase = Phase::Create;
|
|
|
|
|
2020-10-16 23:42:19 +00:00
|
|
|
fn run(
|
2021-03-08 11:13:59 +00:00
|
|
|
_job: &mut Job<Self>,
|
2020-10-16 23:42:19 +00:00
|
|
|
(
|
|
|
|
entities,
|
|
|
|
server_event_bus,
|
|
|
|
terrain,
|
2022-09-16 02:38:13 +00:00
|
|
|
slow_jobs,
|
2020-10-16 23:42:19 +00:00
|
|
|
can_build,
|
2023-06-22 11:25:08 +00:00
|
|
|
mut force_updates,
|
2022-01-17 09:47:29 +00:00
|
|
|
is_rider,
|
2023-04-17 13:38:41 +00:00
|
|
|
is_volume_rider,
|
2021-04-14 15:35:34 +00:00
|
|
|
mut skill_sets,
|
2020-10-31 22:34:08 +00:00
|
|
|
healths,
|
2020-10-16 23:42:19 +00:00
|
|
|
mut block_changes,
|
|
|
|
mut positions,
|
|
|
|
mut velocities,
|
|
|
|
mut orientations,
|
2020-10-30 16:39:53 +00:00
|
|
|
mut presences,
|
2020-10-16 23:42:19 +00:00
|
|
|
mut clients,
|
|
|
|
mut controllers,
|
2023-06-22 11:25:08 +00:00
|
|
|
dt,
|
2020-10-16 23:42:19 +00:00
|
|
|
settings,
|
2021-03-23 23:59:18 +00:00
|
|
|
build_areas,
|
2022-09-16 02:38:13 +00:00
|
|
|
mut player_physics_settings_,
|
2021-07-23 12:04:16 +00:00
|
|
|
mut terrain_persistence,
|
2021-04-14 19:51:03 +00:00
|
|
|
players,
|
2021-04-29 16:46:40 +00:00
|
|
|
admins,
|
2020-10-16 23:42:19 +00:00
|
|
|
): Self::SystemData,
|
|
|
|
) {
|
2022-08-21 04:48:51 +00:00
|
|
|
let time_for_vd_changes = Instant::now();
|
|
|
|
|
2022-09-16 02:38:13 +00:00
|
|
|
// NOTE: stdlib mutex is more than good enough on Linux and (probably) Windows,
|
|
|
|
// but not Mac.
|
|
|
|
let rare_writes = parking_lot::Mutex::new(RareWrites {
|
|
|
|
block_changes: &mut block_changes,
|
|
|
|
_terrain_persistence: &mut terrain_persistence,
|
|
|
|
});
|
|
|
|
|
|
|
|
let player_physics_settings = &*player_physics_settings_;
|
|
|
|
let mut deferred_updates = (
|
2021-04-14 19:51:03 +00:00
|
|
|
&entities,
|
|
|
|
&mut clients,
|
|
|
|
(&mut presences).maybe(),
|
|
|
|
players.maybe(),
|
2021-04-29 16:46:40 +00:00
|
|
|
admins.maybe(),
|
2022-09-16 02:38:13 +00:00
|
|
|
(&skill_sets).maybe(),
|
|
|
|
(&mut positions).maybe(),
|
|
|
|
(&mut velocities).maybe(),
|
|
|
|
(&mut orientations).maybe(),
|
|
|
|
(&mut controllers).maybe(),
|
2023-06-22 11:25:08 +00:00
|
|
|
(&mut force_updates).maybe(),
|
2021-04-14 19:51:03 +00:00
|
|
|
)
|
|
|
|
.join()
|
2022-09-16 02:38:13 +00:00
|
|
|
// NOTE: Required because Specs has very poor work splitting for sparse joins.
|
|
|
|
.par_bridge()
|
|
|
|
.map_init(
|
|
|
|
|| server_event_bus.emitter(),
|
|
|
|
|server_emitter, (
|
2020-10-17 09:36:44 +00:00
|
|
|
entity,
|
|
|
|
client,
|
2022-09-16 02:38:13 +00:00
|
|
|
mut maybe_presence,
|
|
|
|
maybe_player,
|
|
|
|
maybe_admin,
|
|
|
|
skill_set,
|
|
|
|
ref mut pos,
|
|
|
|
ref mut vel,
|
|
|
|
ref mut ori,
|
|
|
|
ref mut controller,
|
2023-06-22 11:25:08 +00:00
|
|
|
ref mut force_update,
|
2022-09-16 02:38:13 +00:00
|
|
|
)| {
|
|
|
|
let old_player_physics_setting = maybe_player.map(|p| {
|
|
|
|
player_physics_settings
|
|
|
|
.settings
|
|
|
|
.get(&p.uuid())
|
|
|
|
.copied()
|
|
|
|
.unwrap_or_default()
|
|
|
|
});
|
|
|
|
let mut new_player_physics_setting = old_player_physics_setting;
|
|
|
|
// If an `ExitInGame` message is received this is set to `None` allowing further
|
|
|
|
// ingame messages to be ignored.
|
|
|
|
let mut clearable_maybe_presence = maybe_presence.as_deref_mut();
|
|
|
|
let mut skill_set = skill_set.map(Cow::Borrowed);
|
2023-06-22 09:43:02 +00:00
|
|
|
let mut player_physics = None;
|
2022-09-16 02:38:13 +00:00
|
|
|
let _ = super::try_recv_all(client, 2, |client, msg| {
|
|
|
|
Self::handle_client_in_game_msg(
|
|
|
|
server_emitter,
|
|
|
|
entity,
|
|
|
|
client,
|
|
|
|
&mut clearable_maybe_presence,
|
|
|
|
&terrain,
|
|
|
|
&can_build,
|
|
|
|
&is_rider,
|
2023-04-17 13:38:41 +00:00
|
|
|
&is_volume_rider,
|
2023-06-22 11:25:08 +00:00
|
|
|
force_update.as_ref(),
|
2022-09-16 02:38:13 +00:00
|
|
|
&mut skill_set,
|
|
|
|
&healths,
|
|
|
|
&rare_writes,
|
|
|
|
pos.as_deref_mut(),
|
|
|
|
controller.as_deref_mut(),
|
|
|
|
&settings,
|
|
|
|
&build_areas,
|
|
|
|
new_player_physics_setting.as_mut(),
|
|
|
|
&maybe_admin,
|
|
|
|
time_for_vd_changes,
|
|
|
|
msg,
|
2023-06-22 09:43:02 +00:00
|
|
|
&mut player_physics,
|
2022-09-16 02:38:13 +00:00
|
|
|
)
|
|
|
|
});
|
2022-08-21 04:48:51 +00:00
|
|
|
|
2023-06-22 11:25:08 +00:00
|
|
|
if let Some((new_pos, new_vel, new_ori)) = player_physics
|
|
|
|
&& let Some(old_pos) = pos.as_deref_mut()
|
|
|
|
&& let Some(old_vel) = vel.as_deref_mut()
|
|
|
|
&& let Some(old_ori) = ori.as_deref_mut()
|
|
|
|
{
|
2023-06-22 09:43:02 +00:00
|
|
|
enum Rejection {
|
|
|
|
TooFar { old: Vec3<f32>, new: Vec3<f32> },
|
|
|
|
TooFast { vel: Vec3<f32> },
|
|
|
|
}
|
|
|
|
|
|
|
|
let rejection = if maybe_admin.is_some() {
|
|
|
|
None
|
2023-06-22 11:25:08 +00:00
|
|
|
} else {
|
|
|
|
// Reminder: review these frequently to ensure they're reasonable
|
|
|
|
const MAX_H_VELOCITY: f32 = 75.0;
|
2023-06-22 18:34:18 +00:00
|
|
|
const MAX_V_VELOCITY: std::ops::Range<f32> = -100.0..80.0;
|
2023-06-22 11:25:08 +00:00
|
|
|
|
|
|
|
'rejection: {
|
|
|
|
let is_velocity_ok = new_vel.0.xy().magnitude_squared() < MAX_H_VELOCITY.powi(2)
|
|
|
|
&& MAX_V_VELOCITY.contains(&new_vel.0.z);
|
|
|
|
|
|
|
|
if !is_velocity_ok {
|
|
|
|
break 'rejection Some(Rejection::TooFast { vel: new_vel.0 });
|
|
|
|
}
|
|
|
|
|
2023-06-30 18:56:24 +00:00
|
|
|
// How far the player is permitted to stray from the correct position (perhaps due to
|
|
|
|
// latency problems).
|
|
|
|
const POSITION_THRESHOLD: f32 = 16.0;
|
|
|
|
|
2023-06-22 11:25:08 +00:00
|
|
|
// The position can either be sensible with respect to either the old or the new
|
|
|
|
// velocity such that we don't punish for edge cases after a sudden change
|
|
|
|
let is_position_ok = [old_vel.0, new_vel.0]
|
|
|
|
.into_iter()
|
|
|
|
.any(|ref_vel| {
|
|
|
|
let rpos = new_pos.0 - old_pos.0;
|
|
|
|
// Determine whether the change in position is broadly consistent with both
|
|
|
|
// the magnitude and direction of the velocity, with appropriate thresholds.
|
|
|
|
LineSegment3 {
|
|
|
|
start: Vec3::zero(),
|
|
|
|
end: ref_vel * dt.0,
|
2023-06-22 09:43:02 +00:00
|
|
|
}
|
2023-06-22 11:25:08 +00:00
|
|
|
.projected_point(rpos)
|
|
|
|
// + 1.5 accounts for minor changes in position without corresponding
|
|
|
|
// velocity like block hopping/snapping
|
2023-06-30 18:56:24 +00:00
|
|
|
.distance_squared(rpos) < (rpos.magnitude() * 0.5 + 1.5 + POSITION_THRESHOLD).powi(2)
|
2023-06-22 11:25:08 +00:00
|
|
|
});
|
2023-06-22 09:43:02 +00:00
|
|
|
|
2023-06-22 11:25:08 +00:00
|
|
|
if !is_position_ok {
|
|
|
|
break 'rejection Some(Rejection::TooFar { old: old_pos.0, new: new_pos.0 });
|
|
|
|
}
|
2023-06-22 09:43:02 +00:00
|
|
|
|
2023-06-22 11:25:08 +00:00
|
|
|
None
|
|
|
|
}
|
2023-06-22 09:43:02 +00:00
|
|
|
};
|
|
|
|
|
2023-07-01 08:40:57 +00:00
|
|
|
if let Some(rejection) = rejection {
|
2023-06-30 18:56:24 +00:00
|
|
|
// TODO: Log when false positives aren't generated often
|
2023-07-01 08:40:57 +00:00
|
|
|
let alias = maybe_player.map(|p| &p.alias);
|
|
|
|
match rejection {
|
|
|
|
Rejection::TooFar { old, new } => warn!("Rejected physics for player {alias:?} (new position {new:?} is too far from old position {old:?})"),
|
|
|
|
Rejection::TooFast { vel } => warn!("Rejected physics for player {alias:?} (new velocity {vel:?} is too fast)"),
|
|
|
|
}
|
2023-06-22 11:25:08 +00:00
|
|
|
|
2023-06-22 12:14:07 +00:00
|
|
|
/*
|
|
|
|
// Perhaps this is overzealous?
|
2023-06-22 11:25:08 +00:00
|
|
|
if let Some(mut setting) = new_player_physics_setting.as_mut() {
|
2023-06-22 12:14:07 +00:00
|
|
|
setting.server_force = true;
|
|
|
|
warn!("Switching player {alias:?} to server-side physics");
|
2023-06-22 11:25:08 +00:00
|
|
|
}
|
2023-06-22 12:14:07 +00:00
|
|
|
*/
|
2023-06-22 11:25:08 +00:00
|
|
|
|
|
|
|
// Reject the change and force the server's view of the physics state
|
|
|
|
force_update.as_mut().map(|fu| fu.update());
|
|
|
|
} else {
|
|
|
|
*old_pos = new_pos;
|
|
|
|
*old_vel = new_vel;
|
|
|
|
*old_ori = new_ori;
|
2023-06-22 09:43:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-16 02:38:13 +00:00
|
|
|
// Ensure deferred view distance changes are applied (if the
|
|
|
|
// requsite time has elapsed).
|
|
|
|
if let Some(presence) = maybe_presence {
|
|
|
|
presence.terrain_view_distance.update(time_for_vd_changes);
|
|
|
|
presence.entity_view_distance.update(time_for_vd_changes);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the possibly modified skill set, and possibly modified server physics
|
|
|
|
// settings.
|
|
|
|
let skill_set_update = skill_set.and_then(|skill_set| match skill_set {
|
|
|
|
Cow::Borrowed(_) => None,
|
|
|
|
Cow::Owned(skill_set) => Some((entity, skill_set)),
|
|
|
|
});
|
|
|
|
// NOTE: Since we pass Option<&mut _> rather than &mut Option<_> to
|
|
|
|
// handle_client_in_game_msg, and the new player was initialized to the same
|
|
|
|
// value as the old setting , we know that either both the new and old setting
|
|
|
|
// are Some, or they are both None.
|
|
|
|
let physics_update = maybe_player.map(|p| p.uuid())
|
|
|
|
.zip(new_player_physics_setting
|
|
|
|
.filter(|_| old_player_physics_setting != new_player_physics_setting));
|
|
|
|
(skill_set_update, physics_update)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
// NOTE: Would be nice to combine this with the map_init somehow, but I'm not sure if
|
|
|
|
// that's possible.
|
|
|
|
.filter(|(x, y)| x.is_some() || y.is_some())
|
|
|
|
// NOTE: I feel like we shouldn't actually need to allocate here, but hopefully this
|
|
|
|
// doesn't turn out to be important as there shouldn't be that many connected clients.
|
|
|
|
// The reason we can't just use unzip is that the two sides might be different lengths.
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
let player_physics_settings = &mut *player_physics_settings_;
|
|
|
|
// Deferred updates to skillsets and player physics.
|
|
|
|
//
|
|
|
|
// NOTE: It is an invariant that there is at most one client entry per player
|
|
|
|
// uuid; since we joined on clients, it follows that there's just one update
|
|
|
|
// per uuid, so the physics update is sound and doesn't depend on evaluation
|
|
|
|
// order, even though we're not updating directly by entity or uid (note that
|
|
|
|
// for a given entity, we process messages serially).
|
|
|
|
deferred_updates
|
|
|
|
.iter_mut()
|
|
|
|
.for_each(|(skill_set_update, physics_update)| {
|
|
|
|
if let Some((entity, new_skill_set)) = skill_set_update {
|
|
|
|
// We know this exists, because we already iterated over it with the skillset
|
|
|
|
// lock taken, so we can ignore the error.
|
|
|
|
//
|
|
|
|
// Note that we replace rather than just updating. This is in order to avoid
|
|
|
|
// dropping here; we'll drop later on a background thread, in case skillsets are
|
|
|
|
// slow to drop.
|
|
|
|
skill_sets
|
|
|
|
.get_mut(*entity)
|
|
|
|
.map(|mut old_skill_set| mem::swap(&mut *old_skill_set, new_skill_set));
|
|
|
|
}
|
|
|
|
if let &mut Some((uuid, player_physics_setting)) = physics_update {
|
|
|
|
// We don't necessarily know this exists, but that's fine, because dropping
|
|
|
|
// player physics is a no op.
|
|
|
|
player_physics_settings
|
|
|
|
.settings
|
|
|
|
.insert(uuid, player_physics_setting);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// Finally, drop the deferred updates in another thread.
|
|
|
|
slow_jobs.spawn("CHUNK_DROP", move || {
|
|
|
|
drop(deferred_updates);
|
|
|
|
});
|
2020-10-16 23:42:19 +00:00
|
|
|
}
|
|
|
|
}
|