Skip to main content

veloren_common_state/
state.rs

1#[cfg(feature = "plugins")]
2use crate::plugin::PluginMgr;
3#[cfg(feature = "plugins")]
4use crate::plugin::memory_manager::EcsWorld;
5use crate::{BuildArea, NoDurabilityArea};
6#[cfg(feature = "plugins")]
7use common::uid::IdMaps;
8use common::{
9    calendar::Calendar,
10    comp::{self, gizmos::RtsimGizmos},
11    event::{BonkEvent, EventBus, LocalEvent},
12    interaction,
13    link::Is,
14    mounting::{Mount, Rider, VolumeRider, VolumeRiders},
15    outcome::Outcome,
16    resources::{
17        DeltaTime, EntitiesDiedLastTick, GameMode, PlayerEntity, PlayerPhysicsSettings,
18        ProgramTime, Time, TimeOfDay, TimeScale,
19    },
20    shared_server_config::ServerConstants,
21    slowjob::SlowJobPool,
22    terrain::{Block, MapSizeLg, TerrainChunk, TerrainGrid, sprite::SpriteAdjecencyRequirement},
23    tether,
24    time::DayPeriod,
25    trade::Trades,
26    util::Dir2,
27    vol::{ReadVol, WriteVol},
28    weather::{Weather, WeatherGrid},
29};
30use common_base::{prof_span, span};
31use common_ecs::{PhysicsMetrics, SysMetrics};
32use common_net::sync::{WorldSyncExt, interpolation as sync_interp};
33use core::{convert::identity, time::Duration};
34use hashbrown::{HashMap, HashSet};
35use rayon::{ThreadPool, ThreadPoolBuilder};
36use specs::{
37    Component, DispatcherBuilder, Entity as EcsEntity, WorldExt,
38    prelude::Resource,
39    shred::{Fetch, FetchMut, SendDispatcher},
40    storage::{MaskedStorage as EcsMaskedStorage, Storage as EcsStorage},
41};
42use std::{
43    sync::{
44        Arc,
45        atomic::{AtomicBool, Ordering},
46    },
47    time::Instant,
48};
49use timer_queue::TimerQueue;
50use vek::*;
51
52/// At what point should we stop speeding up physics to compensate for lag? If
53/// we speed physics up too fast, we'd skip important physics events like
54/// collisions. This constant determines the upper limit. If delta time exceeds
55/// this value, the game's physics will begin to produce time lag. Ideally, we'd
56/// avoid such a situation.
57const MAX_DELTA_TIME: f32 = 1.0;
58/// convert seconds to milliseconds to use in TimerQueue
59const SECONDS_TO_MILLISECONDS: f64 = 1000.0;
60
61#[derive(Default)]
62pub struct BlockChange {
63    blocks: HashMap<Vec3<i32>, Block>,
64}
65
66impl BlockChange {
67    pub fn set(&mut self, pos: Vec3<i32>, block: Block) { self.blocks.insert(pos, block); }
68
69    pub fn try_set(&mut self, pos: Vec3<i32>, block: Block) -> Option<()> {
70        if !self.blocks.contains_key(&pos) {
71            self.blocks.insert(pos, block);
72            Some(())
73        } else {
74            None
75        }
76    }
77
78    /// Check if the block at given position `pos` has already been modified
79    /// this tick.
80    pub fn can_set_block(&self, pos: Vec3<i32>) -> bool { !self.blocks.contains_key(&pos) }
81
82    pub fn clear(&mut self) { self.blocks.clear(); }
83}
84
85#[derive(Default)]
86pub struct ScheduledBlockChange {
87    changes: TimerQueue<HashMap<Vec3<i32>, Block>>,
88    outcomes: TimerQueue<HashMap<Vec3<i32>, Block>>,
89    last_poll_time: u64,
90}
91impl ScheduledBlockChange {
92    pub fn set(&mut self, pos: Vec3<i32>, block: Block, replace_time: f64) {
93        let timer = self.changes.insert(
94            (replace_time * SECONDS_TO_MILLISECONDS) as u64,
95            HashMap::new(),
96        );
97        self.changes.get_mut(timer).insert(pos, block);
98    }
99
100    pub fn outcome_set(&mut self, pos: Vec3<i32>, block: Block, replace_time: f64) {
101        let outcome_timer = self.outcomes.insert(
102            (replace_time * SECONDS_TO_MILLISECONDS) as u64,
103            HashMap::new(),
104        );
105        self.outcomes.get_mut(outcome_timer).insert(pos, block);
106    }
107}
108
109#[derive(Default)]
110pub struct TerrainChanges {
111    pub new_chunks: HashSet<Vec2<i32>>,
112    pub modified_chunks: HashSet<Vec2<i32>>,
113    pub removed_chunks: HashSet<Vec2<i32>>,
114    pub modified_blocks: HashMap<Vec3<i32>, Block>,
115}
116
117impl TerrainChanges {
118    pub fn clear(&mut self) {
119        self.new_chunks.clear();
120        self.modified_chunks.clear();
121        self.removed_chunks.clear();
122    }
123}
124
125#[derive(Clone)]
126pub struct BlockDiff {
127    pub wpos: Vec3<i32>,
128    pub old: Block,
129    pub new: Block,
130}
131
132/// A type used to represent game state stored on both the client and the
133/// server. This includes things like entity components, terrain data, and
134/// global states like weather, time of day, etc.
135pub struct State {
136    ecs: specs::World,
137    // Avoid lifetime annotation by storing a thread pool instead of the whole dispatcher
138    thread_pool: Arc<ThreadPool>,
139    dispatcher: SendDispatcher<'static>,
140}
141
142pub type Pools = Arc<ThreadPool>;
143
144impl State {
145    pub fn pools(game_mode: GameMode) -> Pools {
146        let (thread_name_infix, is_main_task) = match game_mode {
147            GameMode::Server => ("s", true),
148            GameMode::Client => ("c", true),
149            // Note: We don't currently use `Singleplayer`. When we do, server-side tasks should be
150            // deprioritised in favour of things that sit on the main thread!
151            GameMode::Singleplayer => ("sp", false),
152        };
153
154        let is_first_error = Arc::new(AtomicBool::new(true));
155        let set_priority = move || {
156            use thread_priority::*;
157            let priority = if is_main_task {
158                // These threads are critical for the main tick loop, so need a higher priority
159                ThreadPriority::Crossplatform(TryFrom::try_from(50).unwrap())
160            } else {
161                ThreadPriority::Min
162            };
163            let res = cfg_select! {
164                target_os = "linux" => std::thread::current().set_priority_and_policy(
165                    ThreadSchedulePolicy::Realtime(RealtimeThreadSchedulePolicy::RoundRobin),
166                    priority,
167                ),
168                _ => std::thread::current().set_priority(priority),
169            };
170            if let Err(err) = res
171                && is_first_error.swap(false, Ordering::Relaxed)
172            {
173                tracing::warn!(
174                    "Unable to set priority/schedule policy for dispatcher pool thread: {err}"
175                );
176            }
177        };
178
179        Arc::new(
180            ThreadPoolBuilder::new()
181                .num_threads(num_cpus::get().max(common::consts::MIN_RECOMMENDED_RAYON_THREADS))
182                .thread_name(move |i| format!("rayon-{}-{}", thread_name_infix, i))
183                .spawn_handler(|thread| {
184                    let mut b = std::thread::Builder::new();
185                    if let Some(name) = thread.name() {
186                        b = b.name(name.to_owned());
187                    }
188                    if let Some(stack_size) = thread.stack_size() {
189                        b = b.stack_size(stack_size);
190                    }
191                    let set_priority = set_priority.clone();
192                    b.spawn(move || {
193                        set_priority();
194                        thread.run()
195                    })?;
196                    Ok(())
197                })
198                .build()
199                .unwrap(),
200        )
201    }
202
203    /// Create a new `State` in client mode.
204    pub fn client(
205        pools: Pools,
206        map_size_lg: MapSizeLg,
207        default_chunk: Arc<TerrainChunk>,
208        add_systems: impl Fn(&mut DispatcherBuilder),
209        #[cfg(feature = "plugins")] plugin_mgr: PluginMgr,
210    ) -> Self {
211        Self::new(
212            GameMode::Client,
213            pools,
214            map_size_lg,
215            default_chunk,
216            add_systems,
217            #[cfg(feature = "plugins")]
218            plugin_mgr,
219        )
220    }
221
222    /// Create a new `State` in server mode.
223    pub fn server(
224        pools: Pools,
225        map_size_lg: MapSizeLg,
226        default_chunk: Arc<TerrainChunk>,
227        add_systems: impl Fn(&mut DispatcherBuilder),
228        #[cfg(feature = "plugins")] plugin_mgr: PluginMgr,
229    ) -> Self {
230        Self::new(
231            GameMode::Server,
232            pools,
233            map_size_lg,
234            default_chunk,
235            add_systems,
236            #[cfg(feature = "plugins")]
237            plugin_mgr,
238        )
239    }
240
241    pub fn new(
242        game_mode: GameMode,
243        pools: Pools,
244        map_size_lg: MapSizeLg,
245        default_chunk: Arc<TerrainChunk>,
246        add_systems: impl Fn(&mut DispatcherBuilder),
247        #[cfg(feature = "plugins")] plugin_mgr: PluginMgr,
248    ) -> Self {
249        prof_span!(guard, "create dispatcher");
250        let mut dispatch_builder =
251            DispatcherBuilder::<'static, 'static>::new().with_pool(Arc::clone(&pools));
252        // TODO: Consider alternative ways to do this
253        add_systems(&mut dispatch_builder);
254        let dispatcher = dispatch_builder
255            .build()
256            .try_into_sendable()
257            .unwrap_or_else(|_| panic!("Thread local systems not allowed"));
258        drop(guard);
259
260        Self {
261            ecs: Self::setup_ecs_world(
262                game_mode,
263                Arc::clone(&pools),
264                map_size_lg,
265                default_chunk,
266                #[cfg(feature = "plugins")]
267                plugin_mgr,
268            ),
269            thread_pool: pools,
270            dispatcher,
271        }
272    }
273
274    /// Creates ecs world and registers all the common components and resources
275    // TODO: Split up registering into server and client (e.g. move
276    // EventBus<ServerEvent> to the server)
277    fn setup_ecs_world(
278        game_mode: GameMode,
279        thread_pool: Arc<ThreadPool>,
280        map_size_lg: MapSizeLg,
281        default_chunk: Arc<TerrainChunk>,
282        #[cfg(feature = "plugins")] mut plugin_mgr: PluginMgr,
283    ) -> specs::World {
284        prof_span!("State::setup_ecs_world");
285        let mut ecs = specs::World::new();
286        // Uids for sync
287        ecs.register_sync_marker();
288        // Register server -> all clients synced components.
289        ecs.register::<comp::Body>();
290        ecs.register::<comp::Hardcore>();
291        ecs.register::<comp::body::parts::Heads>();
292        ecs.register::<comp::Player>();
293        ecs.register::<comp::Stats>();
294        ecs.register::<comp::SkillSet>();
295        ecs.register::<comp::ActiveAbilities>();
296        ecs.register::<comp::Buffs>();
297        ecs.register::<comp::Auras>();
298        ecs.register::<comp::EnteredAuras>();
299        ecs.register::<comp::Energy>();
300        ecs.register::<comp::Combo>();
301        ecs.register::<comp::Health>();
302        ecs.register::<comp::Poise>();
303        ecs.register::<comp::CanBuild>();
304        ecs.register::<comp::LightEmitter>();
305        ecs.register::<comp::PickupItem>();
306        ecs.register::<comp::ThrownItem>();
307        ecs.register::<comp::Scale>();
308        ecs.register::<Is<Mount>>();
309        ecs.register::<Is<Rider>>();
310        ecs.register::<Is<VolumeRider>>();
311        ecs.register::<Is<tether::Leader>>();
312        ecs.register::<Is<tether::Follower>>();
313        ecs.register::<Is<interaction::Interactor>>();
314        ecs.register::<interaction::Interactors>();
315        ecs.register::<comp::Mass>();
316        ecs.register::<comp::Density>();
317        ecs.register::<comp::Collider>();
318        ecs.register::<comp::Sticky>();
319        ecs.register::<comp::Immovable>();
320        ecs.register::<comp::CharacterState>();
321        ecs.register::<comp::CharacterActivity>();
322        ecs.register::<comp::Object>();
323        ecs.register::<comp::Group>();
324        ecs.register::<comp::Shockwave>();
325        ecs.register::<comp::ShockwaveHitEntities>();
326        ecs.register::<comp::projectile::ProjectileHitEntities>();
327        ecs.register::<comp::Beam>();
328        ecs.register::<comp::Arcing>();
329        ecs.register::<comp::Pool>();
330        ecs.register::<comp::Alignment>();
331        ecs.register::<comp::LootOwner>();
332        ecs.register::<comp::Admin>();
333        ecs.register::<comp::Stance>();
334        ecs.register::<comp::Teleporting>();
335        ecs.register::<comp::GizmoSubscriber>();
336        ecs.register::<comp::FrontendMarker>();
337
338        // Register components send from clients -> server
339        ecs.register::<comp::Controller>();
340
341        // Register components send directly from server -> all but one client
342        ecs.register::<comp::PhysicsState>();
343
344        // Register components synced from client -> server -> all other clients
345        ecs.register::<comp::Pos>();
346        ecs.register::<comp::Vel>();
347        ecs.register::<comp::Ori>();
348        ecs.register::<comp::Inventory>();
349
350        // Register common unsynced components
351        ecs.register::<comp::PreviousPhysCache>();
352        ecs.register::<comp::PosVelOriDefer>();
353
354        // Register client-local components
355        // TODO: only register on the client
356        ecs.register::<comp::LightAnimation>();
357        ecs.register::<sync_interp::InterpBuffer<comp::Pos>>();
358        ecs.register::<sync_interp::InterpBuffer<comp::Vel>>();
359        ecs.register::<sync_interp::InterpBuffer<comp::Ori>>();
360
361        // Register server-local components
362        // TODO: only register on the server
363        ecs.register::<comp::Last<comp::Pos>>();
364        ecs.register::<comp::Last<comp::Vel>>();
365        ecs.register::<comp::Last<comp::Ori>>();
366        ecs.register::<comp::Agent>();
367        ecs.register::<comp::WaypointArea>();
368        ecs.register::<comp::ForceUpdate>();
369        ecs.register::<comp::InventoryUpdateBuffer>();
370        ecs.register::<comp::Waypoint>();
371        ecs.register::<comp::MapMarker>();
372        ecs.register::<comp::Projectile>();
373        ecs.register::<comp::Melee>();
374        ecs.register::<comp::ItemDrops>();
375        ecs.register::<comp::ChatMode>();
376        ecs.register::<comp::Faction>();
377        ecs.register::<comp::invite::Invite>();
378        ecs.register::<comp::invite::PendingInvites>();
379        ecs.register::<VolumeRiders>();
380        ecs.register::<common::combat::DeathEffects>();
381        ecs.register::<common::combat::RiderEffects>();
382        ecs.register::<comp::SpectatingEntity>();
383
384        // Register synced resources used by the ECS.
385        ecs.insert(TimeOfDay(0.0));
386        ecs.insert(Calendar::default());
387        ecs.insert(WeatherGrid::new(Vec2::zero()));
388        ecs.insert(Time(0.0));
389        ecs.insert(ProgramTime(0.0));
390        ecs.insert(TimeScale(1.0));
391
392        // Register unsynced resources used by the ECS.
393        ecs.insert(DeltaTime(0.0));
394        ecs.insert(PlayerEntity(None));
395        ecs.insert(TerrainGrid::new(map_size_lg, default_chunk).unwrap());
396        ecs.insert(BlockChange::default());
397        ecs.insert(ScheduledBlockChange::default());
398        ecs.insert(crate::special_areas::AreasContainer::<BuildArea>::default());
399        ecs.insert(crate::special_areas::AreasContainer::<NoDurabilityArea>::default());
400        ecs.insert(TerrainChanges::default());
401        ecs.insert(EventBus::<LocalEvent>::default());
402        ecs.insert(game_mode);
403        ecs.insert(EventBus::<Outcome>::default());
404        ecs.insert(common::CachedSpatialGrid::default());
405        ecs.insert(EntitiesDiedLastTick::default());
406        ecs.insert(RtsimGizmos::default());
407
408        let num_cpu = num_cpus::get() as u64;
409        let slow_limit = (num_cpu / 2 + num_cpu / 4).max(1);
410        tracing::trace!(?slow_limit, "Slow Thread limit");
411        ecs.insert(SlowJobPool::new(slow_limit, 10_000, thread_pool));
412
413        // TODO: only register on the server
414        ecs.insert(comp::group::GroupManager::default());
415        ecs.insert(SysMetrics::default());
416        ecs.insert(PhysicsMetrics::default());
417        ecs.insert(Trades::default());
418        ecs.insert(PlayerPhysicsSettings::default());
419        ecs.insert(VolumeRiders::default());
420
421        // Load plugins from asset directory
422        #[cfg(feature = "plugins")]
423        ecs.insert({
424            let ecs_world = EcsWorld {
425                entities: &ecs.entities(),
426                health: ecs.read_component().into(),
427                uid: ecs.read_component().into(),
428                id_maps: &ecs.read_resource::<IdMaps>().into(),
429                player: ecs.read_component().into(),
430            };
431            if let Err(e) = plugin_mgr.load_event(&ecs_world, game_mode) {
432                tracing::debug!(?e, "Failed to run plugin init");
433                tracing::info!("Plugins disabled, enable debug logging for more information.");
434                PluginMgr::default()
435            } else {
436                plugin_mgr
437            }
438        });
439
440        ecs
441    }
442
443    /// Register a component with the state's ECS.
444    #[must_use]
445    pub fn with_component<T: Component>(mut self) -> Self
446    where
447        <T as Component>::Storage: Default,
448    {
449        self.ecs.register::<T>();
450        self
451    }
452
453    /// Write a component attributed to a particular entity, ignoring errors.
454    ///
455    /// This should be used *only* when we can guarantee that the rest of the
456    /// code does not rely on the insert having succeeded (meaning the
457    /// entity is no longer alive!).
458    ///
459    /// Returns None if the entity was dead or there was no previous entry for
460    /// this component; otherwise, returns Some(old_component).
461    pub fn write_component_ignore_entity_dead<C: Component>(
462        &mut self,
463        entity: EcsEntity,
464        comp: C,
465    ) -> Option<C> {
466        self.ecs
467            .write_storage()
468            .insert(entity, comp)
469            .ok()
470            .and_then(identity)
471    }
472
473    /// Delete a component attributed to a particular entity.
474    pub fn delete_component<C: Component>(&mut self, entity: EcsEntity) -> Option<C> {
475        self.ecs.write_storage().remove(entity)
476    }
477
478    /// Read a component attributed to a particular entity.
479    pub fn read_component_cloned<C: Component + Clone>(&self, entity: EcsEntity) -> Option<C> {
480        self.ecs.read_storage().get(entity).cloned()
481    }
482
483    /// Read a component attributed to a particular entity.
484    pub fn read_component_copied<C: Component + Copy>(&self, entity: EcsEntity) -> Option<C> {
485        self.ecs.read_storage().get(entity).copied()
486    }
487
488    /// # Panics
489    /// Panics if `EventBus<E>` is borrowed
490    pub fn emit_event_now<E>(&self, event: E)
491    where
492        EventBus<E>: Resource,
493    {
494        self.ecs.write_resource::<EventBus<E>>().emit_now(event)
495    }
496
497    /// Given mutable access to the resource R, assuming the resource
498    /// component exists (this is already the behavior of functions like `fetch`
499    /// and `write_component_ignore_entity_dead`).  Since all of our resources
500    /// are generated up front, any failure here is definitely a code bug.
501    pub fn mut_resource<R: Resource>(&mut self) -> &mut R {
502        self.ecs.get_mut::<R>().expect(
503            "Tried to fetch an invalid resource even though all our resources should be known at \
504             compile time.",
505        )
506    }
507
508    /// Get a read-only reference to the storage of a particular component type.
509    pub fn read_storage<C: Component>(&self) -> EcsStorage<'_, C, Fetch<'_, EcsMaskedStorage<C>>> {
510        self.ecs.read_storage::<C>()
511    }
512
513    /// Get a reference to the internal ECS world.
514    pub fn ecs(&self) -> &specs::World { &self.ecs }
515
516    /// Get a mutable reference to the internal ECS world.
517    pub fn ecs_mut(&mut self) -> &mut specs::World { &mut self.ecs }
518
519    pub fn thread_pool(&self) -> &Arc<ThreadPool> { &self.thread_pool }
520
521    /// Get a reference to the `TerrainChanges` structure of the state. This
522    /// contains information about terrain state that has changed since the
523    /// last game tick.
524    pub fn terrain_changes(&self) -> Fetch<'_, TerrainChanges> { self.ecs.read_resource() }
525
526    /// Get a reference the current in-game weather grid.
527    pub fn weather_grid(&self) -> Fetch<'_, WeatherGrid> { self.ecs.read_resource() }
528
529    /// Get a mutable reference the current in-game weather grid.
530    pub fn weather_grid_mut(&mut self) -> FetchMut<'_, WeatherGrid> { self.ecs.write_resource() }
531
532    /// Get the current weather at a position in worldspace.
533    pub fn weather_at(&self, pos: Vec2<f32>) -> Weather {
534        self.weather_grid().get_interpolated(pos)
535    }
536
537    /// Get the max weather near a position in worldspace.
538    pub fn max_weather_near(&self, pos: Vec2<f32>) -> Weather {
539        self.weather_grid().get_max_near(pos)
540    }
541
542    /// Get the current in-game time of day.
543    ///
544    /// Note that this should not be used for physics, animations or other such
545    /// localised timings.
546    pub fn get_time_of_day(&self) -> f64 { self.ecs.read_resource::<TimeOfDay>().0 }
547
548    /// Get the current in-game day period (period of the day/night cycle)
549    pub fn get_day_period(&self) -> DayPeriod { self.get_time_of_day().into() }
550
551    /// Get the current in-game time.
552    ///
553    /// Note that this does not correspond to the time of day.
554    pub fn get_time(&self) -> f64 { self.ecs.read_resource::<Time>().0 }
555
556    /// Get the current true in-game time, unaffected by time_scale.
557    ///
558    /// Note that this does not correspond to the time of day.
559    pub fn get_program_time(&self) -> f64 { self.ecs.read_resource::<ProgramTime>().0 }
560
561    /// Get the current delta time.
562    pub fn get_delta_time(&self) -> f32 { self.ecs.read_resource::<DeltaTime>().0 }
563
564    /// Get a reference to this state's terrain.
565    pub fn terrain(&self) -> Fetch<'_, TerrainGrid> { self.ecs.read_resource() }
566
567    /// Get a reference to this state's terrain.
568    pub fn slow_job_pool(&self) -> Fetch<'_, SlowJobPool> { self.ecs.read_resource() }
569
570    /// Get a writable reference to this state's terrain.
571    pub fn terrain_mut(&self) -> FetchMut<'_, TerrainGrid> { self.ecs.write_resource() }
572
573    /// Get a block in this state's terrain.
574    pub fn get_block(&self, pos: Vec3<i32>) -> Option<Block> {
575        self.terrain().get(pos).ok().copied()
576    }
577
578    /// Set a block in this state's terrain.
579    pub fn set_block(&self, pos: Vec3<i32>, block: Block) {
580        self.ecs.write_resource::<BlockChange>().set(pos, block);
581    }
582
583    /// Set a block in this state's terrain (used to delete temporary summoned
584    /// sprites after a timeout).
585    pub fn schedule_set_block(
586        &self,
587        pos: Vec3<i32>,
588        block: Block,
589        sprite_block: Block,
590        replace_time: f64,
591    ) {
592        self.ecs
593            .write_resource::<ScheduledBlockChange>()
594            .set(pos, block, replace_time);
595        self.ecs
596            .write_resource::<ScheduledBlockChange>()
597            .outcome_set(pos, sprite_block, replace_time);
598    }
599
600    /// Check if the block at given position `pos` has already been modified
601    /// this tick.
602    pub fn can_set_block(&self, pos: Vec3<i32>) -> bool {
603        self.ecs.read_resource::<BlockChange>().can_set_block(pos)
604    }
605
606    /// Removes every chunk of the terrain.
607    pub fn clear_terrain(&mut self) -> usize {
608        let removed_chunks = &mut self.ecs.write_resource::<TerrainChanges>().removed_chunks;
609
610        self.terrain_mut()
611            .drain()
612            .map(|(key, _)| {
613                removed_chunks.insert(key);
614            })
615            .count()
616    }
617
618    /// Insert the provided chunk into this state's terrain.
619    pub fn insert_chunk(&mut self, key: Vec2<i32>, chunk: Arc<TerrainChunk>) {
620        if self
621            .ecs
622            .write_resource::<TerrainGrid>()
623            .insert(key, chunk)
624            .is_some()
625        {
626            self.ecs
627                .write_resource::<TerrainChanges>()
628                .modified_chunks
629                .insert(key);
630        } else {
631            self.ecs
632                .write_resource::<TerrainChanges>()
633                .new_chunks
634                .insert(key);
635        }
636    }
637
638    /// Remove the chunk with the given key from this state's terrain, if it
639    /// exists.
640    pub fn remove_chunk(&mut self, key: Vec2<i32>) -> bool {
641        if self
642            .ecs
643            .write_resource::<TerrainGrid>()
644            .remove(key)
645            .is_some()
646        {
647            self.ecs
648                .write_resource::<TerrainChanges>()
649                .removed_chunks
650                .insert(key);
651
652            true
653        } else {
654            false
655        }
656    }
657
658    // Apply terrain changes
659    pub fn apply_terrain_changes(&self, block_update: impl FnMut(&specs::World, Vec<BlockDiff>)) {
660        self.apply_terrain_changes_internal(false, block_update);
661    }
662
663    /// `during_tick` is true if and only if this is called from within
664    /// [State::tick].
665    ///
666    /// This only happens if [State::tick] is asked to update terrain itself
667    /// (using `update_terrain: true`).  [State::tick] is called from within
668    /// both the client and the server ticks, right after handling terrain
669    /// messages; currently, client sets it to true and server to false.
670    fn apply_terrain_changes_internal(
671        &self,
672        during_tick: bool,
673        mut block_update: impl FnMut(&specs::World, Vec<BlockDiff>),
674    ) {
675        span!(
676            _guard,
677            "apply_terrain_changes",
678            "State::apply_terrain_changes"
679        );
680        let mut terrain = self.ecs.write_resource::<TerrainGrid>();
681        let mut modified_blocks =
682            std::mem::take(&mut self.ecs.write_resource::<BlockChange>().blocks);
683
684        let mut scheduled_changes = self.ecs.write_resource::<ScheduledBlockChange>();
685        let current_time: f64 = self.ecs.read_resource::<Time>().0 * SECONDS_TO_MILLISECONDS;
686        let current_time = current_time as u64;
687        // This is important as the poll function has a debug assert that the new poll
688        // is at a more recent time than the old poll. As Time is synced between server
689        // and client, there is a chance that client dt can get slightly ahead of a
690        // server update, so we do not want to panic in that scenario.
691        if scheduled_changes.last_poll_time < current_time {
692            scheduled_changes.last_poll_time = current_time;
693            while let Some(changes) = scheduled_changes.changes.poll(current_time) {
694                modified_blocks.extend(changes.iter());
695            }
696            let outcome = self.ecs.read_resource::<EventBus<Outcome>>();
697            while let Some(outcomes) = scheduled_changes.outcomes.poll(current_time) {
698                for (pos, block) in outcomes.into_iter() {
699                    if let Some(sprite) = block.get_sprite() {
700                        outcome.emit_now(Outcome::SpriteDelete { pos, sprite });
701                    }
702                }
703            }
704        }
705        // Apply block modifications
706        // Only include in `TerrainChanges` if successful
707        let mut updated_blocks = Vec::with_capacity(modified_blocks.len());
708
709        // All positions that should recieve a block update.
710        let mut block_updates = HashSet::<Vec3<i32>>::default();
711
712        modified_blocks.retain(|wpos, new| {
713            let res = terrain.map(*wpos, |old| {
714                updated_blocks.push(BlockDiff {
715                    wpos: *wpos,
716                    old,
717                    new: *new,
718                });
719                *new
720            });
721
722            if let (&Ok(old), true) = (&res, during_tick) {
723                // NOTE: If the changes are applied during the tick, we push the *old* value as
724                // the modified block (since it otherwise can't be recovered after the tick).
725                // Otherwise, the changes will be applied after the tick, so we push the *new*
726                // value.
727                *new = old;
728            }
729
730            if let (&Ok(old), false) = (&res, during_tick) {
731                let h = old
732                    .get_sprite()
733                    .and_then(|s| s.solid_height())
734                    .unwrap_or(1.0)
735                    .max(
736                        new.get_sprite()
737                            .and_then(|s| s.solid_height())
738                            .unwrap_or(1.0),
739                    )
740                    .ceil() as i32;
741
742                block_updates.extend((-1..=h + 1).map(|z| wpos + Vec3::unit_z() * z).chain(
743                    (0..=h).flat_map(|z| {
744                        Dir2::ALL
745                            .iter()
746                            .map(move |d| wpos + Vec3::unit_z() * z + d.to_vec2())
747                    }),
748                ));
749            };
750
751            res.is_ok()
752        });
753
754        if !updated_blocks.is_empty() {
755            block_update(&self.ecs, updated_blocks);
756        }
757
758        // Only do block updates not during the tick since that's when actual
759        // terrain changes are applied.
760        //
761        // Clients will get these changes since they're just normal block updates
762        // next tick.
763        if !during_tick {
764            prof_span!(_guard, "Indirectly modified sprites");
765
766            // Collects all blocks that are neighbors with a modified block,
767            // where the `adjecency_requirement` is no longer upheld.
768            let indirectly_modified = block_updates
769                .into_iter()
770                // Filter for blocks that have an adjecency requirement.
771                .filter_map(|wpos| {
772                    let block = terrain.get(wpos).ok()?;
773                    Some((wpos, block.get_sprite()?.adjecency_requirement()?, block))
774                })
775                // Check if said adjecency requirement is upheld.
776                .filter(|(wpos, adjecency_requirement, block)| {
777                    let rot_mat = block.rotation_mat();
778                    // Tries to find a solid block for the given adjecent block.
779                    let find_solid = |adj: Vec3<i32>| {
780                        let wpos = wpos + adj;
781
782                        let res = terrain.get(wpos).copied().unwrap_or(Block::empty());
783
784                        // Don't check for sprites if we're checking for a block
785                        // directly above.
786                        let not_above = adj.z <= 0 || adj.x != 0 || adj.y != 0;
787
788                        if not_above && !res.is_solid() {
789                            // Sprites can be taller than 1 block.
790                            for z in 1..=Block::MAX_HEIGHT.ceil() as i32 {
791                                if let Ok(block) = terrain.get(wpos - Vec3::unit_z() * z)
792                                    && let Some(sprite) = block.get_sprite()
793                                    && let Some(h) = sprite.solid_height()
794                                    && h.ceil() as i32 > z
795                                {
796                                    return *block;
797                                }
798                            }
799                        }
800
801                        res
802                    };
803
804                    // Same as `find_solid` but first rotates with the sprites rotation
805                    // and mirroring.
806                    let rel_solid = |adj: Vec3<i32>| find_solid(rot_mat * adj);
807
808                    let valid = match adjecency_requirement {
809                        SpriteAdjecencyRequirement::AllSolid(v) => {
810                            v.iter().all(|v| rel_solid(*v).is_solid())
811                        },
812                        SpriteAdjecencyRequirement::AnySolid(v) => {
813                            v.iter().any(|v| rel_solid(*v).is_solid())
814                        },
815                    };
816
817                    !valid
818                })
819                .map(|(wpos, _, block)| (wpos, block))
820                .collect::<Vec<_>>();
821
822            // If the sprite is bonkable, bonk it.
823            let bonk_event_bus = self.ecs.write_resource::<EventBus<BonkEvent>>();
824            let mut bonk_emitter = bonk_event_bus.emitter();
825
826            let mut block_change = self.ecs.write_resource::<BlockChange>();
827
828            for (wpos, block) in indirectly_modified {
829                if block.is_bonkable() {
830                    bonk_emitter.emit(BonkEvent {
831                        pos: wpos.as_::<f32>() + 0.5,
832                        // TODO: Pass who destroyed the block?
833                        owner: None,
834                        target: None,
835                    });
836                } else {
837                    block_change.blocks.insert(wpos, block.into_vacant());
838                }
839            }
840        }
841
842        self.ecs.write_resource::<TerrainChanges>().modified_blocks = modified_blocks;
843    }
844
845    /// Execute a single tick, simulating the game state by the given duration.
846    pub fn tick(
847        &mut self,
848        dt: Duration,
849        update_terrain: bool,
850        mut metrics: Option<&mut StateTickMetrics>,
851        server_constants: &ServerConstants,
852        block_update: impl FnMut(&specs::World, Vec<BlockDiff>),
853    ) {
854        span!(_guard, "tick", "State::tick");
855
856        // Timing code for server metrics
857        macro_rules! section_span {
858            ($guard:ident, $label:literal) => {
859                span!(span_guard, $label);
860                let metrics_guard = metrics.as_mut().map(|m| MetricsGuard::new($label, m));
861                let $guard = (span_guard, metrics_guard);
862            };
863        }
864
865        // Change the time accordingly.
866        let time_scale = self.ecs.read_resource::<TimeScale>().0;
867        self.ecs.write_resource::<TimeOfDay>().0 +=
868            dt.as_secs_f64() * server_constants.day_cycle_coefficient * time_scale;
869        self.ecs.write_resource::<Time>().0 += dt.as_secs_f64() * time_scale;
870        self.ecs.write_resource::<ProgramTime>().0 += dt.as_secs_f64();
871
872        // Update delta time.
873        // Beyond a delta time of MAX_DELTA_TIME, start lagging to avoid skipping
874        // important physics events.
875        self.ecs.write_resource::<DeltaTime>().0 =
876            (dt.as_secs_f32() * time_scale as f32).min(MAX_DELTA_TIME);
877
878        section_span!(guard, "run systems");
879        // This dispatches all the systems in parallel.
880        self.dispatcher.dispatch(&self.ecs);
881        drop(guard);
882
883        self.maintain_ecs();
884
885        if update_terrain {
886            self.apply_terrain_changes_internal(true, block_update);
887        }
888
889        // Process local events
890        section_span!(guard, "process local events");
891
892        let outcomes = self.ecs.read_resource::<EventBus<Outcome>>();
893        let mut outcomes_emitter = outcomes.emitter();
894
895        let events = self.ecs.read_resource::<EventBus<LocalEvent>>().recv_all();
896        for event in events {
897            let mut velocities = self.ecs.write_storage::<comp::Vel>();
898            let physics = self.ecs.read_storage::<comp::PhysicsState>();
899            match event {
900                LocalEvent::Jump(entity, impulse) => {
901                    if let Some(vel) = velocities.get_mut(entity) {
902                        vel.0.z = impulse + physics.get(entity).map_or(0.0, |ps| ps.ground_vel.z);
903                    }
904                },
905                LocalEvent::ApplyImpulse { entity, impulse } => {
906                    if let Some(vel) = velocities.get_mut(entity) {
907                        vel.0 = impulse;
908                    }
909                },
910                LocalEvent::Boost {
911                    entity,
912                    vel: extra_vel,
913                } => {
914                    if let Some(vel) = velocities.get_mut(entity) {
915                        vel.0 += extra_vel;
916                    }
917                },
918                LocalEvent::CreateOutcome(outcome) => {
919                    outcomes_emitter.emit(outcome);
920                },
921            }
922        }
923        drop(guard);
924    }
925
926    pub fn maintain_ecs(&mut self) {
927        span!(_guard, "maintain ecs");
928        self.ecs.maintain();
929    }
930
931    /// Clean up the state after a tick.
932    pub fn cleanup(&mut self) {
933        span!(_guard, "cleanup", "State::cleanup");
934        // Clean up data structures from the last tick.
935        self.ecs.write_resource::<TerrainChanges>().clear();
936    }
937}
938
939// Timing code for server metrics
940#[derive(Default)]
941pub struct StateTickMetrics {
942    pub timings: Vec<(&'static str, Duration)>,
943}
944
945impl StateTickMetrics {
946    fn add(&mut self, label: &'static str, dur: Duration) {
947        // Check for duplicates!
948        debug_assert!(
949            self.timings.iter().all(|(l, _)| *l != label),
950            "Duplicate label in state tick metrics {label}"
951        );
952        self.timings.push((label, dur));
953    }
954}
955
956struct MetricsGuard<'a> {
957    start: Instant,
958    label: &'static str,
959    metrics: &'a mut StateTickMetrics,
960}
961
962impl<'a> MetricsGuard<'a> {
963    fn new(label: &'static str, metrics: &'a mut StateTickMetrics) -> Self {
964        Self {
965            start: Instant::now(),
966            label,
967            metrics,
968        }
969    }
970}
971
972impl Drop for MetricsGuard<'_> {
973    fn drop(&mut self) { self.metrics.add(self.label, self.start.elapsed()); }
974}