1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use crate::{character::CharacterId, rtsim::RtSimEntity};
use core::hash::Hash;
use hashbrown::HashMap;
use serde::{Deserialize, Serialize};
use specs::{Component, Entity, FlaggedStorage, VecStorage};
use std::{fmt, u64};
use tracing::error;

// TODO: could we switch this to `NonZeroU64`?
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct Uid(pub u64);

impl From<Uid> for u64 {
    fn from(uid: Uid) -> u64 { uid.0 }
}

impl From<u64> for Uid {
    fn from(uid: u64) -> Self { Self(uid) }
}

impl fmt::Display for Uid {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.0) }
}

impl Component for Uid {
    type Storage = FlaggedStorage<Self, VecStorage<Self>>;
}

#[derive(Debug)]
struct UidAllocator {
    /// Next Uid.
    next_uid: u64,
}

impl UidAllocator {
    fn new() -> Self { Self { next_uid: 0 } }

    fn allocate(&mut self) -> Uid {
        let id = self.next_uid;
        self.next_uid += 1;
        Uid(id)
    }
}

/// Mappings from various Id types to `Entity`s.
#[derive(Default, Debug)]
pub struct IdMaps {
    /// "Universal" IDs (used to communicate entity identity over the
    /// network).
    uid_mapping: HashMap<Uid, Entity>,

    // -- Fields below are only used on the server --
    uid_allocator: UidAllocator,

    /// Character IDs.
    character_to_ecs: HashMap<CharacterId, Entity>,
    /// Rtsim Entities.
    rtsim_to_ecs: HashMap<RtSimEntity, Entity>,
}

impl IdMaps {
    pub fn new() -> Self { Default::default() }

    /// Given a `Uid` retrieve the corresponding `Entity`.
    pub fn uid_entity(&self, id: Uid) -> Option<Entity> { self.uid_mapping.get(&id).copied() }

    /// Given a `CharacterId` retrieve the corresponding `Entity`.
    pub fn character_entity(&self, id: CharacterId) -> Option<Entity> {
        self.character_to_ecs.get(&id).copied()
    }

    /// Given a `RtSimEntity` retrieve the corresponding `Entity`.
    pub fn rtsim_entity(&self, id: RtSimEntity) -> Option<Entity> {
        self.rtsim_to_ecs.get(&id).copied()
    }

    /// Removes mappings for the provided Id(s).
    ///
    /// Returns the `Entity` that the provided `Uid` was mapped to.
    ///
    /// Used on both the client and the server when deleting entities,
    /// although the client only ever provides a Some value for the
    /// `Uid` parameter since the other mappings are not used on the
    /// client.
    pub fn remove_entity(
        &mut self,
        expected_entity: Option<Entity>,
        uid: Option<Uid>,
        cid: Option<CharacterId>,
        rid: Option<RtSimEntity>,
    ) -> Option<Entity> {
        #[cold]
        #[inline(never)]
        fn unexpected_entity<ID>() {
            let kind = core::any::type_name::<ID>();
            error!("Provided {kind} was mapped to an unexpected entity!");
        }
        #[cold]
        #[inline(never)]
        fn not_present<ID>() {
            let kind = core::any::type_name::<ID>();
            error!("Provided {kind} was not mapped to any entity!");
        }

        fn remove<ID: Hash + Eq>(
            mapping: &mut HashMap<ID, Entity>,
            id: Option<ID>,
            expected: Option<Entity>,
        ) -> Option<Entity> {
            if let Some(id) = id {
                if let Some(e) = mapping.remove(&id) {
                    if expected.map_or(false, |expected| e != expected) {
                        unexpected_entity::<ID>();
                    }
                    Some(e)
                } else {
                    not_present::<ID>();
                    None
                }
            } else {
                None
            }
        }

        let maybe_entity = remove(&mut self.uid_mapping, uid, expected_entity);
        let expected_entity = expected_entity.or(maybe_entity);
        remove(&mut self.character_to_ecs, cid, expected_entity);
        remove(&mut self.rtsim_to_ecs, rid, expected_entity);
        maybe_entity
    }

    /// Only used on the client (server solely uses `Self::allocate` to
    /// allocate and add Uid mappings and `Self::remap` to move the `Uid` to
    /// a different entity).
    pub fn add_entity(&mut self, uid: Uid, entity: Entity) {
        Self::insert(&mut self.uid_mapping, uid, entity);
    }

    /// Only used on the server.
    pub fn add_character(&mut self, cid: CharacterId, entity: Entity) {
        Self::insert(&mut self.character_to_ecs, cid, entity);
    }

    /// Only used on the server.
    pub fn add_rtsim(&mut self, rid: RtSimEntity, entity: Entity) {
        Self::insert(&mut self.rtsim_to_ecs, rid, entity);
    }

    /// Allocates a new `Uid` and links it to the provided entity.
    ///
    /// Only used on the server.
    pub fn allocate(&mut self, entity: Entity) -> Uid {
        let uid = self.uid_allocator.allocate();
        self.uid_mapping.insert(uid, entity);
        uid
    }

    /// Links an existing `Uid` to a new entity.
    ///
    /// Only used on the server.
    ///
    /// Used for `handle_exit_ingame` which moves the same `Uid` to a new
    /// entity.
    pub fn remap_entity(&mut self, uid: Uid, new_entity: Entity) {
        if self.uid_mapping.insert(uid, new_entity).is_none() {
            error!("Uid {uid:?} remaped but there was no existing entry for it!");
        }
    }

    #[cold]
    #[inline(never)]
    fn already_present<ID>() {
        let kind = core::any::type_name::<ID>();
        error!("Provided {kind} was already mapped to an entity!!!");
    }

    fn insert<ID: Hash + Eq>(mapping: &mut HashMap<ID, Entity>, new_id: ID, entity: Entity) {
        if let Some(_previous_entity) = mapping.insert(new_id, entity) {
            Self::already_present::<ID>();
        }
    }
}

impl Default for UidAllocator {
    fn default() -> Self { Self::new() }
}