veloren_rtsim/data/
site.rs

1use crate::data::{ReportId, Reports};
2pub use common::rtsim::SiteId;
3use common::{
4    rtsim::{FactionId, NpcId},
5    store::Id,
6};
7use hashbrown::{HashMap, HashSet};
8use serde::{Deserialize, Serialize};
9use slotmap::HopSlotMap;
10use std::ops::{Deref, DerefMut};
11use vek::*;
12use world::site::Site as WorldSite;
13
14#[derive(Clone, Serialize, Deserialize)]
15pub struct Site {
16    pub seed: u32,
17    pub wpos: Vec2<i32>,
18    pub faction: Option<FactionId>,
19
20    /// The [`Report`]s that the site tracks (you can imagine them being on a
21    /// noticeboard or something).
22    pub known_reports: HashSet<ReportId>,
23
24    /// How many chunks this site is loaded in.
25    #[serde(skip)]
26    pub count_loaded_chunks: usize,
27
28    /// The site generated during initial worldgen that this site corresponds
29    /// to.
30    ///
31    /// Eventually, rtsim should replace initial worldgen's site system and this
32    /// will not be necessary.
33    ///
34    /// When setting up rtsim state, we try to 'link' these two definitions of a
35    /// site: but if initial worldgen has changed, this might not be
36    /// possible. We try to delete sites that no longer exist during setup, but
37    /// this is an inherent fallible process. If linking fails, we try to
38    /// delete the site in rtsim2 in order to avoid an 'orphaned' site.
39    /// (TODO: create new sites for new initial worldgen sites that come into
40    /// being too).
41    #[serde(skip_serializing, skip_deserializing)]
42    pub world_site: Option<Id<WorldSite>>,
43
44    // Note: there's currently no guarantee that site populations are non-intersecting
45    #[serde(skip_serializing, skip_deserializing)]
46    pub population: HashSet<NpcId>,
47
48    /// A list of the nearby sites where each elements is both further and
49    /// larger (currently based on number of plots) than the next.
50    /// Effectively, this is a list of nearby sites that might be deemed
51    /// 'important' to the current one
52    #[serde(skip_serializing, skip_deserializing)]
53    pub nearby_sites_by_size: Vec<SiteId>,
54}
55
56impl Site {
57    pub fn with_faction(mut self, faction: impl Into<Option<FactionId>>) -> Self {
58        self.faction = faction.into();
59        self
60    }
61
62    pub fn cleanup(&mut self, reports: &Reports) {
63        // Clear reports that have been forgotten
64        self.known_reports
65            .retain(|report| reports.contains_key(*report));
66        // TODO: Limit number of reports
67    }
68
69    pub fn is_loaded(&self) -> bool { self.count_loaded_chunks > 0 }
70}
71
72#[derive(Clone, Default, Serialize, Deserialize)]
73pub struct Sites {
74    pub sites: HopSlotMap<SiteId, Site>,
75
76    #[serde(skip_serializing, skip_deserializing)]
77    pub world_site_map: HashMap<Id<WorldSite>, SiteId>,
78}
79
80impl Sites {
81    pub fn create(&mut self, site: Site) -> SiteId {
82        let world_site = site.world_site;
83        let key = self.sites.insert(site);
84        if let Some(world_site) = world_site {
85            self.world_site_map.insert(world_site, key);
86        }
87        key
88    }
89}
90
91impl Deref for Sites {
92    type Target = HopSlotMap<SiteId, Site>;
93
94    fn deref(&self) -> &Self::Target { &self.sites }
95}
96
97impl DerefMut for Sites {
98    fn deref_mut(&mut self) -> &mut Self::Target { &mut self.sites }
99}