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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
use std::any::TypeId;
use std::collections::HashMap;
use std::hash::{BuildHasherDefault, Hash};
use std::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};
use std::sync::atomic::{AtomicUsize, Ordering};

use fnv::FnvHasher;
use mopa::Any;

#[cfg(feature="ticket")]
use std::sync::Mutex;
#[cfg(feature="ticket")]
use ticketed_lock::{TicketedLock, ReadLockGuard, ReadTicket, WriteLockGuard, WriteTicket};
#[cfg(not(feature="ticket"))]
use std::sync::{RwLock as Lock, RwLockReadGuard as ReadLockGuard, RwLockWriteGuard as WriteLockGuard};

use bitset::{AtomicBitSet, BitSet, BitSetLike, BitSetOr};
use gate::Gate;
use join::Join;
use storage::{Storage, MaskedStorage, UnprotectedStorage};
#[cfg(feature="ticket")]
use storage::GatedStorage;
use {Index, Generation, Entity};


#[cfg(feature="ticket")]
struct Lock<T> {
    inner: Mutex<TicketedLock<T>>,
}

#[cfg(feature="ticket")]
impl<T> Gate for ReadTicket<T> {
    type Target = ReadLockGuard<T>;
    fn pass(self) -> Self::Target {
        self.wait()
    }
}

#[cfg(feature="ticket")]
impl<T> Gate for WriteTicket<T> {
    type Target = WriteLockGuard<T>;
    fn pass(self) -> Self::Target {
        self.wait()
    }
}

pub struct TimeGuard<G>(G);

#[cfg(feature="ticket")]
impl<G: Gate> TimeGuard<G> {
    fn unwrap(self) -> G::Target {
        self.0.pass()
    }
}
#[cfg(not(feature="ticket"))]
impl<G> Gate for TimeGuard<G> {
    type Target = G;
    fn pass(self) -> G {
        self.0
    }
}

#[cfg(feature="ticket")]
impl<T> Lock<T> {
    fn new(data: T) -> Self {
        Lock {
            inner: Mutex::new(TicketedLock::new(data)),
        }
    }

    fn read(&self) -> TimeGuard<ReadTicket<T>> {
        TimeGuard(self.inner.lock().unwrap().read())
    }

    fn write(&self) -> TimeGuard<WriteTicket<T>> {
        TimeGuard(self.inner.lock().unwrap().write())
    }

    fn into_inner(self) -> Option<T> {
        self.inner.into_inner().ok().map(|lock| lock.unlock())
    }
}


/// Abstract component type. Doesn't have to be Copy or even Clone.
pub trait Component: Any + Sized {
    /// Associated storage type for this component.
    type Storage: UnprotectedStorage<Self> + Any + Send + Sync;
}


/// A custom entity guard used to hide the the fact that Generations
/// is lazily created and updated. For this to be useful it _must_
/// be joined with a component. This is because the Generation table
/// includes every possible Generation of Entities even if they
/// have never existed.
pub struct Entities<'a> {
    guard: RwLockReadGuard<'a, Allocator>,
}

impl<'a> Join for &'a Entities<'a> {
    type Type = Entity;
    type Value = Self;
    type Mask = BitSetOr<&'a BitSet, &'a AtomicBitSet>;
    fn open(self) -> (Self::Mask, Self) {
        (BitSetOr(&self.guard.alive, &self.guard.raised), self)
    }
    unsafe fn get(v: &mut Self, idx: Index) -> Entity {
        let gen = v.guard.generations.get(idx as usize)
            .map(|&gen| if gen.is_alive() { gen } else { gen.raised() })
            .unwrap_or(Generation(1));
        Entity(idx, gen)
    }
}

impl<'a> Gate for Entities<'a> {
    type Target = Self;
    fn pass(self) -> Self {
        self
    }
}

/// Helper builder for entities.
pub struct EntityBuilder<'a, C = ()>(Entity, &'a World<C>)
    where C: 'a + PartialEq + Eq + Hash;

impl<'a, C> EntityBuilder<'a, C>
    where C: 'a + PartialEq + Eq + Hash
{
    /// Starts a new entity builder for a given `Entity`.
    pub fn new(e: Entity, w: &'a World<C>) -> Self {
        EntityBuilder(e, w)
    }
    /// Adds a `Component` value to the new `Entity`.
    pub fn with_w_comp_id<T: Component>(self, comp_id: C, value: T) -> EntityBuilder<'a, C> {
        self.1.write_w_comp_id::<T>(comp_id).insert(self.0, value);
        self
    }
    /// Finishes entity construction.
    pub fn build(self) -> Entity {
        self.0
    }
}

impl <'a> EntityBuilder<'a, ()> {
    /// Adds a `Component` value to the new `Entity`.
    pub fn with<T: Component>(self, value: T) -> EntityBuilder<'a> {
        self.1.write::<T>().pass().insert(self.0, value);
        self
    }
}


/// Internally used structure for `Entity` allocation.
pub struct Allocator {
    #[doc(hidden)]
    pub generations: Vec<Generation>,
    alive: BitSet,
    raised: AtomicBitSet,
    killed: AtomicBitSet,
    start_from: AtomicUsize
}

impl Allocator {
    #[doc(hidden)]
    pub fn new() -> Allocator {
        Allocator {
            generations: vec![],
            alive: BitSet::new(),
            raised: AtomicBitSet::new(),
            killed: AtomicBitSet::new(),
            start_from: AtomicUsize::new(0)
        }
    }

    fn kill(&self, e: Entity) {
        self.killed.add_atomic(e.get_id());
    }

    /// Return `true` if the entity is alive.
    pub fn is_alive(&self, e: Entity) -> bool {
        e.get_gen() ==  match self.generations.get(e.get_id() as usize) {
            Some(g) if !g.is_alive() && self.raised.contains(e.get_id()) => g.raised(),
            Some(g) => *g,
            None => Generation(1),
        }
    }

    /// Attempt to move the `start_from` value
    fn update_start_from(&self, start_from: usize) {
        loop {
            let current = self.start_from.load(Ordering::Relaxed);

            // if the current value is bigger then ours, we bail
            if current >= start_from {
                return;
            }

            if start_from == self.start_from.compare_and_swap(current, start_from, Ordering::Relaxed) {
                return;
            }
        }
    }

    /// Allocate a new entity
    fn allocate_atomic(&self) -> Entity {
        let idx = self.start_from.load(Ordering::Relaxed);
        for i in idx.. {
            if !self.alive.contains(i as Index) && !self.raised.add_atomic(i as Index) {
                self.update_start_from(i+1);

                let gen = self.generations.get(i as usize)
                    .map(|&gen| if gen.is_alive() { gen } else { gen.raised() })
                    .unwrap_or(Generation(1));

                return Entity(i as Index, gen);
            }
        }
        panic!("No entities left to allocate")
    }

    /// Allocate a new entity
    fn allocate(&mut self) -> Entity {
        let idx = self.start_from.load(Ordering::Relaxed);
        for i in idx.. {
            if !self.raised.contains(i as Index) && !self.alive.add(i as Index) {
                // this is safe since we have mutable access to everything!
                self.start_from.store(i+1, Ordering::Relaxed);

                while self.generations.len() <= i as usize {
                    self.generations.push(Generation(0));
                }
                self.generations[i as usize] = self.generations[i as usize].raised();

                return Entity(i as Index, self.generations[i as usize]);
            }
        }
        panic!("No entities left to allocate")
    }

    fn merge(&mut self) -> Vec<Entity> {
        let mut deleted = vec![];

        for i in (&self.raised).iter() {
            while self.generations.len() <= i as usize {
                self.generations.push(Generation(0));
            }
            self.generations[i as usize] = self.generations[i as usize].raised();
            self.alive.add(i);
        }
        self.raised.clear();

        if let Some(lowest) = (&self.killed).iter().next() {
            if lowest < self.start_from.load(Ordering::Relaxed) as Index {
                self.start_from.store(lowest as usize, Ordering::Relaxed);
            }
        }

        for i in (&self.killed).iter() {
            self.alive.remove(i);
            self.generations[i as usize].die();
            deleted.push(Entity(i, self.generations[i as usize]))
        }
        self.killed.clear();

        deleted
    }
}

/// Entity creation iterator. Will yield new empty entities infinitely.
/// Useful for bulk entity construction, since the locks are only happening once.
pub struct CreateEntities<'a> {
    allocate: RwLockWriteGuard<'a, Allocator>,
}

impl<'a> Iterator for CreateEntities<'a> {
    type Item = Entity;
    fn next(&mut self) -> Option<Entity> {
        Some(self.allocate.allocate())
    }
}


trait StorageLock: Any + Send + Sync {
    fn del_slice(&self, &[Entity]);
}

mopafy!(StorageLock);

impl<T: Component> StorageLock for Lock<MaskedStorage<T>> {
    fn del_slice(&self, entities: &[Entity]) {
        let mut guard = self.write().unwrap();
        for &e in entities.iter() {
            guard.remove(e.get_id());
        }
    }
}

trait ResourceLock: Any + Send + Sync {}

mopafy!(ResourceLock);

impl<T:Any+Send+Sync> ResourceLock for Lock<T> {}

/// The `World` struct contains all the data, which is entities and their components.
/// All methods are supposed to be valid for any context they are available in.
/// The type parameter C is for component identification in addition of their types.
pub struct World<C = ()>
    where C: PartialEq + Eq + Hash
{
    allocator: RwLock<Allocator>,
    components: HashMap<(C, TypeId), Box<StorageLock>, BuildHasherDefault<FnvHasher>>,
    resources: HashMap<TypeId, Box<ResourceLock>, BuildHasherDefault<FnvHasher>>,
}

impl<C> World<C>
    where C: PartialEq + Eq + Hash
{
    /// Creates a new empty `World` with the associated component id.
    pub fn new_w_comp_id() -> World<C> {
        World {
            components: Default::default(),
            allocator: RwLock::new(Allocator::new()),
            resources: Default::default()
        }
    }
    /// Registers a new component type and id pair.
    ///
    /// Does nothing if the type and id pair was already registered.
    pub fn register_w_comp_id<T: Component>(&mut self, comp_id: C) {
        self.components.entry((comp_id, TypeId::of::<T>()))
            .or_insert_with(|| {
                let any = Lock::new(MaskedStorage::<T>::new());
                Box::new(any)
            });
    }
    /// Unregisters a component type and id pair.
    pub fn unregister_w_comp_id<T: Component>(&mut self, comp_id: C) -> Option<MaskedStorage<T>> {
        self.components.remove(&(comp_id, TypeId::of::<T>())).map(|boxed|
            match boxed.downcast::<Lock<MaskedStorage<T>>>() {
                Ok(b) => (*b).into_inner().unwrap(),
                Err(_) => panic!("Unable to downcast the storage type"),
            }
        )
    }
    fn lock_w_comp_id<T: Component>(&self, comp_id: C) -> &Lock<MaskedStorage<T>> {
        let boxed = self.components.get(&(comp_id, TypeId::of::<T>()))
            .expect("Tried to perform an operation on component type that was not registered");
        boxed.downcast_ref().unwrap()
    }
    /// Locks a component's storage for reading.
    pub fn read_w_comp_id<T: Component>(&self, comp_id: C) ->
                          Storage<T, RwLockReadGuard<Allocator>, ReadLockGuard<MaskedStorage<T>>> {
        let data = self.lock_w_comp_id::<T>(comp_id).read().unwrap();
        Storage::new(self.allocator.read().unwrap(), data)
    }
    /// Locks a component's storage for writing.
    pub fn write_w_comp_id<T: Component>(&self, comp_id: C) ->
                           Storage<T, RwLockReadGuard<Allocator>, WriteLockGuard<MaskedStorage<T>>>
    {
        let data = self.lock_w_comp_id::<T>(comp_id).write().unwrap();
        Storage::new(self.allocator.read().unwrap(), data)
    }
    /// Returns the entity iterator.
    pub fn entities(&self) -> Entities {
        Entities {
            guard: self.allocator.read().unwrap(),
        }
    }
    /// Returns the entity creation iterator. Can be used to create many
    /// empty entities at once without paying the locking overhead.
    pub fn create_iter(&mut self) -> CreateEntities {
        CreateEntities {
            allocate: self.allocator.write().unwrap(),
        }
    }
    /// Creates a new entity instantly, locking the generations data.
    pub fn create_now(&mut self) -> EntityBuilder<C> {
        let id = self.allocator.write().unwrap().allocate();
        EntityBuilder::new(id, self)
    }
    /// Deletes a new entity instantly, locking the generations data.
    pub fn delete_now(&mut self, entity: Entity) {
        for comp in self.components.values() {
            comp.del_slice(&[entity]);
        }
        let mut gens = self.allocator.write().unwrap();
        gens.alive.remove(entity.get_id());
        gens.raised.remove(entity.get_id());
        let id = entity.get_id() as usize;
        gens.generations[id].die();
        if id < gens.start_from.load(Ordering::Relaxed) {
            gens.start_from.store(id, Ordering::Relaxed);
        }
    }
    /// Creates a new entity dynamically.
    pub fn create_pure(&self) -> Entity {
        let allocator = self.allocator.read().unwrap();
        allocator.allocate_atomic()
    }
    /// Creates a new entity dynamically, and starts building it.
    pub fn create(&self) -> EntityBuilder<C> {
        EntityBuilder::new(self.create_pure(), self)
    }
    /// Deletes an entity dynamically.
    pub fn delete_later(&self, entity: Entity) {
        let allocator = self.allocator.read().unwrap();
        allocator.kill(entity);
    }
    /// Returns `true` if the given `Entity` is alive.
    pub fn is_alive(&self, entity: Entity) -> bool {
        debug_assert!(entity.get_gen().is_alive());
        let gens = self.allocator.read().unwrap();
        gens.generations.get(entity.get_id() as usize).map(|&x| x == entity.get_gen()).unwrap_or(false)
    }
    /// Merges in the appendix, recording all the dynamically created
    /// and deleted entities into the persistent generations vector.
    /// Also removes all the abandoned components.
    pub fn maintain(&mut self) {
        let mut allocator = self.allocator.write().unwrap();

        let temp_list = allocator.merge();
        for comp in self.components.values() {
            comp.del_slice(&temp_list);
        }
    }
    /// Add a new resource to the world.
    pub fn add_resource<T: Any+Send+Sync>(&mut self, resource: T) {
        let resource = Box::new(Lock::new(resource));
        self.resources.insert(TypeId::of::<T>(), resource);
    }
    /// Check to see if a resource is present.
    pub fn has_resource<T: Any+Send+Sync>(&self) -> bool {
        self.resources.get(&TypeId::of::<T>()).is_some()
    }
    fn get_resource<T: Any+Send+Sync>(&self) -> &Lock<T> {
        self.resources.get(&TypeId::of::<T>())
            .expect("Resource was not registered")
            .downcast_ref::<Lock<T>>()
            .unwrap()
    }
    /// Get read-only access to a resource.
    pub fn read_resource_now<T: Any+Send+Sync>(&self) -> ReadLockGuard<T> {
        self.get_resource::<T>().read().unwrap()
    }
    /// Get read-write access to a resource.
    pub fn write_resource_now<T: Any+Send+Sync>(&self) -> WriteLockGuard<T> {
        self.get_resource::<T>().write().unwrap()
    }
}

impl World<()> {
    /// Creates a new empty `World`.
    pub fn new() -> World<()> {
        World {
            components: Default::default(),
            allocator: RwLock::new(Allocator::new()),
            resources: Default::default()
        }
    }

    /// Registers a new component type.
    ///
    /// Does nothing if the component type was already registered.
    pub fn register<T: Component>(&mut self) {
        self.register_w_comp_id::<T>(())
    }
    /// Unregisters a component type.
    pub fn unregister<T: Component>(&mut self) -> Option<MaskedStorage<T>> {
        self.unregister_w_comp_id::<T>(())
    }
    /*fn lock<T: Component>(&self) -> &Lock<MaskedStorage<T>> {
        self.lock_w_comp_id::<T>(())
    }*/
}

#[cfg(not(feature="ticket"))]
impl World<()> {
    /// Locks a component's storage for reading.
    pub fn read<T: Component>(&self) -> Storage<T, RwLockReadGuard<Allocator>, ReadLockGuard<MaskedStorage<T>>> {
        self.read_w_comp_id::<T>(())
    }
    /// Locks a component's storage for writing.
    pub fn write<T: Component>(&self) -> Storage<T, RwLockReadGuard<Allocator>, WriteLockGuard<MaskedStorage<T>>> {
        self.write_w_comp_id::<T>(())
    }
    /// Get read-only access to a resource.
    pub fn read_resource<T: Any+Send+Sync>(&self) -> TimeGuard<ReadLockGuard<T>> {
        TimeGuard(self.get_resource::<T>().read().unwrap())
    }
    /// Get read-write access to a resource.
    pub fn write_resource<T: Any+Send+Sync>(&self) -> TimeGuard<WriteLockGuard<T>> {
        TimeGuard(self.get_resource::<T>().write().unwrap())
    }
}

#[cfg(feature="ticket")]
impl World<()> {
    /// Request a read ticket for a particular component storage.
    pub fn read<T: Component>(&self) -> GatedStorage<T, RwLockReadGuard<Allocator>, ReadTicket<MaskedStorage<T>>> {
        let ticket = self.lock_w_comp_id::<T>(()).read();
        GatedStorage::new(self.allocator.read().unwrap(), ticket.0)
    }
    /// Request a write ticket for a particular component storage.
    pub fn write<T: Component>(&self) -> GatedStorage<T, RwLockReadGuard<Allocator>, WriteTicket<MaskedStorage<T>>> {
        let ticket = self.lock_w_comp_id::<T>(()).write();
        GatedStorage::new(self.allocator.read().unwrap(), ticket.0)
    }
    /// Get read-only access to a resource.
    pub fn read_resource<T: Any+Send+Sync>(&self) -> ReadTicket<T> {
        self.get_resource::<T>().read().0
    }
    /// Get read-write access to a resource.
    pub fn write_resource<T: Any+Send+Sync>(&self) -> WriteTicket<T> {
        self.get_resource::<T>().write().0
    }
}