veloren_common/
uid.rs

1use crate::{
2    character::CharacterId,
3    rtsim::{Actor, RtSimEntity},
4};
5use core::hash::Hash;
6use hashbrown::HashMap;
7use serde::{Deserialize, Serialize};
8use specs::{Component, Entity, FlaggedStorage, VecStorage};
9use std::fmt;
10use tracing::error;
11
12// TODO: could we switch this to `NonZeroU64`?
13#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
14pub struct Uid(pub u64);
15
16impl From<Uid> for u64 {
17    fn from(uid: Uid) -> u64 { uid.0 }
18}
19
20impl From<u64> for Uid {
21    fn from(uid: u64) -> Self { Self(uid) }
22}
23
24impl fmt::Display for Uid {
25    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.0) }
26}
27
28impl Component for Uid {
29    type Storage = FlaggedStorage<Self, VecStorage<Self>>;
30}
31
32#[derive(Debug)]
33struct UidAllocator {
34    /// Next Uid.
35    next_uid: u64,
36}
37
38impl UidAllocator {
39    fn new() -> Self { Self { next_uid: 0 } }
40
41    fn allocate(&mut self) -> Uid {
42        let id = self.next_uid;
43        self.next_uid += 1;
44        Uid(id)
45    }
46}
47
48/// Mappings from various Id types to `Entity`s.
49#[derive(Default, Debug)]
50pub struct IdMaps {
51    /// "Universal" IDs (used to communicate entity identity over the
52    /// network).
53    uid_mapping: HashMap<Uid, Entity>,
54
55    // -- Fields below are only used on the server --
56    uid_allocator: UidAllocator,
57
58    /// Character IDs.
59    character_to_ecs: HashMap<CharacterId, Entity>,
60    /// Rtsim Entities.
61    rtsim_to_ecs: HashMap<RtSimEntity, Entity>,
62}
63
64impl IdMaps {
65    pub fn new() -> Self { Default::default() }
66
67    /// Given a `Uid` retrieve the corresponding `Entity`.
68    pub fn uid_entity(&self, id: Uid) -> Option<Entity> { self.uid_mapping.get(&id).copied() }
69
70    /// Given a `CharacterId` retrieve the corresponding `Entity`.
71    pub fn character_entity(&self, id: CharacterId) -> Option<Entity> {
72        self.character_to_ecs.get(&id).copied()
73    }
74
75    /// Given a `RtSimEntity` retrieve the corresponding `Entity`.
76    pub fn rtsim_entity(&self, id: RtSimEntity) -> Option<Entity> {
77        self.rtsim_to_ecs.get(&id).copied()
78    }
79
80    pub fn actor_entity(&self, actor: Actor) -> Option<Entity> {
81        match actor {
82            Actor::Character(character_id) => self.character_entity(character_id),
83            Actor::Npc(npc_id) => self.rtsim_entity(RtSimEntity(npc_id)),
84        }
85    }
86
87    /// Removes mappings for the provided Id(s).
88    ///
89    /// Returns the `Entity` that the provided `Uid` was mapped to.
90    ///
91    /// Used on both the client and the server when deleting entities,
92    /// although the client only ever provides a Some value for the
93    /// `Uid` parameter since the other mappings are not used on the
94    /// client.
95    pub fn remove_entity(
96        &mut self,
97        expected_entity: Option<Entity>,
98        uid: Option<Uid>,
99        cid: Option<CharacterId>,
100        rid: Option<RtSimEntity>,
101    ) -> Option<Entity> {
102        #[cold]
103        #[inline(never)]
104        fn unexpected_entity<ID>() {
105            let kind = core::any::type_name::<ID>();
106            error!("Provided {kind} was mapped to an unexpected entity!");
107        }
108        #[cold]
109        #[inline(never)]
110        fn not_present<ID>() {
111            let kind = core::any::type_name::<ID>();
112            error!("Provided {kind} was not mapped to any entity!");
113        }
114
115        fn remove<ID: Hash + Eq>(
116            mapping: &mut HashMap<ID, Entity>,
117            id: Option<ID>,
118            expected: Option<Entity>,
119        ) -> Option<Entity> {
120            if let Some(id) = id {
121                if let Some(e) = mapping.remove(&id) {
122                    if expected.is_some_and(|expected| e != expected) {
123                        unexpected_entity::<ID>();
124                    }
125                    Some(e)
126                } else {
127                    not_present::<ID>();
128                    None
129                }
130            } else {
131                None
132            }
133        }
134
135        let maybe_entity = remove(&mut self.uid_mapping, uid, expected_entity);
136        let expected_entity = expected_entity.or(maybe_entity);
137        remove(&mut self.character_to_ecs, cid, expected_entity);
138        remove(&mut self.rtsim_to_ecs, rid, expected_entity);
139        maybe_entity
140    }
141
142    /// Only used on the client (server solely uses `Self::allocate` to
143    /// allocate and add Uid mappings and `Self::remap` to move the `Uid` to
144    /// a different entity).
145    pub fn add_entity(&mut self, uid: Uid, entity: Entity) {
146        Self::insert(&mut self.uid_mapping, uid, entity);
147    }
148
149    /// Only used on the server.
150    pub fn add_character(&mut self, cid: CharacterId, entity: Entity) {
151        Self::insert(&mut self.character_to_ecs, cid, entity);
152    }
153
154    /// Only used on the server.
155    pub fn add_rtsim(&mut self, rid: RtSimEntity, entity: Entity) {
156        Self::insert(&mut self.rtsim_to_ecs, rid, entity);
157    }
158
159    /// Allocates a new `Uid` and links it to the provided entity.
160    ///
161    /// Only used on the server.
162    pub fn allocate(&mut self, entity: Entity) -> Uid {
163        let uid = self.uid_allocator.allocate();
164        self.uid_mapping.insert(uid, entity);
165        uid
166    }
167
168    /// Links an existing `Uid` to a new entity.
169    ///
170    /// Only used on the server.
171    ///
172    /// Used for `handle_exit_ingame` which moves the same `Uid` to a new
173    /// entity.
174    pub fn remap_entity(&mut self, uid: Uid, new_entity: Entity) {
175        if self.uid_mapping.insert(uid, new_entity).is_none() {
176            error!("Uid {uid:?} remaped but there was no existing entry for it!");
177        }
178    }
179
180    #[cold]
181    #[inline(never)]
182    fn already_present<ID>() {
183        let kind = core::any::type_name::<ID>();
184        error!("Provided {kind} was already mapped to an entity!!!");
185    }
186
187    fn insert<ID: Hash + Eq>(mapping: &mut HashMap<ID, Entity>, new_id: ID, entity: Entity) {
188        if let Some(_previous_entity) = mapping.insert(new_id, entity) {
189            Self::already_present::<ID>();
190        }
191    }
192}
193
194impl Default for UidAllocator {
195    fn default() -> Self { Self::new() }
196}