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#[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: 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#[derive(Default, Debug)]
50pub struct IdMaps {
51 uid_mapping: HashMap<Uid, Entity>,
54
55 uid_allocator: UidAllocator,
57
58 character_to_ecs: HashMap<CharacterId, Entity>,
60 rtsim_to_ecs: HashMap<RtSimEntity, Entity>,
62}
63
64impl IdMaps {
65 pub fn new() -> Self { Default::default() }
66
67 pub fn uid_entity(&self, id: Uid) -> Option<Entity> { self.uid_mapping.get(&id).copied() }
69
70 pub fn character_entity(&self, id: CharacterId) -> Option<Entity> {
72 self.character_to_ecs.get(&id).copied()
73 }
74
75 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 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 pub fn add_entity(&mut self, uid: Uid, entity: Entity) {
146 Self::insert(&mut self.uid_mapping, uid, entity);
147 }
148
149 pub fn add_character(&mut self, cid: CharacterId, entity: Entity) {
151 Self::insert(&mut self.character_to_ecs, cid, entity);
152 }
153
154 pub fn add_rtsim(&mut self, rid: RtSimEntity, entity: Entity) {
156 Self::insert(&mut self.rtsim_to_ecs, rid, entity);
157 }
158
159 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 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}