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    /// The site generated during initial worldgen that this site corresponds
25    /// to.
26    ///
27    /// Eventually, rtsim should replace initial worldgen's site system and this
28    /// will not be necessary.
29    ///
30    /// When setting up rtsim state, we try to 'link' these two definitions of a
31    /// site: but if initial worldgen has changed, this might not be
32    /// possible. We try to delete sites that no longer exist during setup, but
33    /// this is an inherent fallible process. If linking fails, we try to
34    /// delete the site in rtsim2 in order to avoid an 'orphaned' site.
35    /// (TODO: create new sites for new initial worldgen sites that come into
36    /// being too).
37    #[serde(skip_serializing, skip_deserializing)]
38    pub world_site: Option<Id<WorldSite>>,
39
40    // Note: there's currently no guarantee that site populations are non-intersecting
41    #[serde(skip_serializing, skip_deserializing)]
42    pub population: HashSet<NpcId>,
43
44    /// A list of the nearby sites where each elements is both further and
45    /// larger (currently based on number of plots) than the next.
46    /// Effectively, this is a list of nearby sites that might be deemed
47    /// 'important' to the current one
48    #[serde(skip_serializing, skip_deserializing)]
49    pub nearby_sites_by_size: Vec<SiteId>,
50}
51
52impl Site {
53    pub fn with_faction(mut self, faction: impl Into<Option<FactionId>>) -> Self {
54        self.faction = faction.into();
55        self
56    }
57
58    pub fn cleanup(&mut self, reports: &Reports) {
59        // Clear reports that have been forgotten
60        self.known_reports
61            .retain(|report| reports.contains_key(*report));
62        // TODO: Limit number of reports
63    }
64}
65
66#[derive(Clone, Default, Serialize, Deserialize)]
67pub struct Sites {
68    pub sites: HopSlotMap<SiteId, Site>,
69
70    #[serde(skip_serializing, skip_deserializing)]
71    pub world_site_map: HashMap<Id<WorldSite>, SiteId>,
72}
73
74impl Sites {
75    pub fn create(&mut self, site: Site) -> SiteId {
76        let world_site = site.world_site;
77        let key = self.sites.insert(site);
78        if let Some(world_site) = world_site {
79            self.world_site_map.insert(world_site, key);
80        }
81        key
82    }
83}
84
85impl Deref for Sites {
86    type Target = HopSlotMap<SiteId, Site>;
87
88    fn deref(&self) -> &Self::Target { &self.sites }
89}
90
91impl DerefMut for Sites {
92    fn deref_mut(&mut self) -> &mut Self::Target { &mut self.sites }
93}