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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
use crate::{
action::LocalActionEncoder,
component::Component,
entity::{Entity, EntityId, Location},
relation::{OriginComponent, Relation, TargetComponent},
type_id, NoSuchEntity,
};
use super::World;
impl World {
/// Adds relation between two entities to the [`World`].
///
/// If either entity is not alive, fails with `Err(NoSuchEntity)`.
/// When either entity is despawned, relation is removed automatically.
///
/// Relations can be queried and filtered using queries from [`edict::relation`] module.
///
/// Relation must implement [`Relation`] trait that defines its behavior.
///
/// If relation already exists, then instance is replaced.
/// If relation is symmetric then it is added in both directions.
/// If relation is exclusive, then previous relation on origin is replaced, otherwise relation is added.
/// If relation is exclusive and symmetric, then previous relation on target is replaced, otherwise relation is added.
#[inline]
pub fn insert_relation<R>(
&mut self,
origin: impl Entity,
relation: R,
target: impl Entity,
) -> Result<(), NoSuchEntity>
where
R: Relation,
{
self.maintenance();
origin.lookup(&self.entities).ok_or(NoSuchEntity)?;
target.lookup(&self.entities).ok_or(NoSuchEntity)?;
self.epoch.next_mut();
if R::SYMMETRIC {
let set_target = set_relation_component(
self,
origin.id(),
relation,
|relation| OriginComponent::new_relation(target.id(), relation),
|component, relation, encoder| {
component.insert_relation(origin.id(), target.id(), relation, encoder)
},
);
if target.id() != origin.id() && set_target {
set_relation_component(
self,
target.id(),
relation,
|relation| OriginComponent::new_relation(origin.id(), relation),
|component, relation, encoder| {
component.insert_relation(target.id(), origin.id(), relation, encoder)
},
);
}
} else {
let set_target = set_relation_component(
self,
origin.id(),
relation,
|relation| OriginComponent::new_relation(target.id(), relation),
|comp, relation, encoder| {
comp.insert_relation(origin.id(), target.id(), relation, encoder)
},
);
if set_target {
set_relation_component(
self,
target.id(),
(),
|()| TargetComponent::<R>::new(origin.id(), relation),
|comp, (), _| {
comp.add(origin.id(), relation);
false
},
);
}
}
self.execute_local_actions();
Ok(())
}
/// Removes relation between two entities in the [`World`].
///
/// If either entity is not alive, fails with `Err(NoSuchEntity)`.
/// If relation does not exist, removes `None`.
///
/// When relation is removed, [`Relation::on_drop`] behavior is not executed.
/// For symmetric relations [`Relation::on_target_drop`] is also not executed.
#[inline]
pub fn remove_relation<R>(
&mut self,
origin: impl Entity,
target: impl Entity,
) -> Result<Option<R>, NoSuchEntity>
where
R: Relation,
{
self._remove_relation(origin, target)
}
/// Drops relation between two entities in the [`World`].
///
/// If either entity is not alive, fails with `Err(NoSuchEntity)`.
/// If relation does not exist, does nothing.
///
/// When relation is dropped, [`Relation::on_drop`] behavior is executed.
#[inline]
pub fn drop_relation<R>(
&mut self,
origin: impl Entity,
target: impl Entity,
) -> Result<(), NoSuchEntity>
where
R: Relation,
{
if let Some(relation) = self._remove_relation(origin, target)? {
let encoder = LocalActionEncoder::new(self.action_buffer.get_mut(), &self.entities);
R::on_drop(relation, origin.id(), target.id(), encoder);
}
self.execute_local_actions();
Ok(())
}
#[inline]
pub(crate) fn _remove_relation<R>(
&mut self,
origin: impl Entity,
target: impl Entity,
) -> Result<Option<R>, NoSuchEntity>
where
R: Relation,
{
self.maintenance();
let origin = origin.entity_loc(&self.entities).ok_or(NoSuchEntity)?;
let target = target.entity_loc(&self.entities).ok_or(NoSuchEntity)?;
let mut removed = None;
unsafe {
if let Ok(comp) = self.get_unchecked::<&mut OriginComponent<R>>(origin) {
// Safety: Operations called on `self` won't touch it.
let mut encoder =
LocalActionEncoder::new(&mut *self.action_buffer.get(), &self.entities);
if let Some(relation) =
comp.remove_relation(origin.id(), target.id(), encoder.reborrow())
{
if R::SYMMETRIC {
if origin.id() != target.id() {
let comp = self
.get_unchecked::<&mut OriginComponent<R>>(target)
.unwrap_unchecked();
comp.remove_relation(target.id(), origin.id(), encoder);
}
} else {
let comp = self
.get_unchecked::<&mut TargetComponent<R>>(target)
.unwrap_unchecked();
comp.remove_relation(origin.id(), target.id(), encoder);
}
removed = Some(relation);
}
self.execute_local_actions();
}
}
Ok(removed)
}
}
/// Inserts component.
/// This function uses different code to assign component when it already exists on entity.
fn set_relation_component<T, C>(
world: &mut World,
id: EntityId,
value: T,
into_component: impl FnOnce(T) -> C,
set_component: impl FnOnce(&mut C, T, LocalActionEncoder) -> bool,
) -> bool
where
C: Component,
{
let src_loc = world.entities.get_location(id).unwrap();
debug_assert!(src_loc.arch < u32::MAX, "Allocated entities were spawned");
if world.archetypes[src_loc.arch as usize].has_component(type_id::<C>()) {
let component = unsafe {
world.archetypes[src_loc.arch as usize]
.get_mut::<C>(src_loc.idx, world.epoch.current_mut())
};
let encoders = LocalActionEncoder::new(world.action_buffer.get_mut(), &world.entities);
return set_component(component, value, encoders);
}
let component = into_component(value);
let dst_arch = world.edges.insert(
&mut world.registry,
&mut world.archetypes,
src_loc.arch,
type_id::<C>(),
|registry| registry.get_or_register::<C>(),
);
debug_assert_ne!(src_loc.arch, dst_arch);
let (before, after) = world
.archetypes
.split_at_mut(src_loc.arch.max(dst_arch) as usize);
let (src, dst) = match src_loc.arch < dst_arch {
true => (&mut before[src_loc.arch as usize], &mut after[0]),
false => (&mut after[0], &mut before[dst_arch as usize]),
};
let (dst_idx, opt_src_id) =
unsafe { src.insert(id, dst, src_loc.idx, component, world.epoch.current_mut()) };
world
.entities
.set_location(id, Location::new(dst_arch, dst_idx));
if let Some(src_id) = opt_src_id {
world.entities.set_location(src_id, src_loc);
}
true
}