use std::any::type_name; use std::marker::PhantomData; use seq_macro::seq; use crate::component::{Component, FromLockedOptional, Ref as ComponentRef}; use crate::entity::Handle as EntityHandle; use crate::query::flexible::{Iter as FlexibleQueryIter, Query as FlexibleQuery}; use crate::system::{Param as SystemParam, System}; use crate::uid::Uid; use crate::util::array_vec::ArrayVec; use crate::util::Array; use crate::World; pub mod flexible; pub mod term; #[derive(Debug)] pub struct Query<'world, FieldTerms, FieldlessTerms = ()> where FieldTerms: TermWithFieldTuple, FieldlessTerms: TermWithoutFieldTuple, { world: &'world World, // A term tuple type can have a maximum of 17 elements inner: FlexibleQuery<'world, 17>, _pd: PhantomData<(FieldTerms, FieldlessTerms)>, } impl<'world, FieldTerms, FieldlessTerms> Query<'world, FieldTerms, FieldlessTerms> where FieldTerms: TermWithFieldTuple, FieldlessTerms: TermWithoutFieldTuple, { /// Iterates over the entities matching this query, the iterator item being the entity /// components. #[must_use] pub fn iter<'query>( &'query self, ) -> Iter<'query, 'world, FieldTerms, FlexibleQueryIter<'query>> { tracing::trace!("Searching for {}", std::any::type_name::()); Iter { world: self.world, iter: self.inner.iter(), comps_pd: PhantomData, } } /// Iterates over the entities matching this query, the iterator item being the entity /// [`Uid`] and the matching entity components. #[must_use] pub fn iter_with_euids<'query>( &'query self, ) -> ComponentAndEuidIter<'query, 'world, FieldTerms, FlexibleQueryIter<'query>> { tracing::trace!("Searching for {}", std::any::type_name::()); ComponentAndEuidIter { world: self.world, iter: self.inner.iter(), comps_pd: PhantomData, } } /// Iterates over the entities matching this query using the iterator returned by /// `func`. /// /// This function exists so that a custom [`EntityHandle`] iterator can be given to /// [`Iter`] without giving the user access to a reference to the [`World`]. #[must_use] pub fn iter_with<'query, OutIter>( &'query self, func: impl FnOnce(FlexibleQueryIter<'query>) -> OutIter, ) -> Iter<'query, 'world, FieldTerms, OutIter> where OutIter: Iterator>, { tracing::trace!("Searching for {}", std::any::type_name::()); Iter { world: self.world, iter: func(self.inner.iter()), comps_pd: PhantomData, } } /// Returns the UID of the entity at the given query iteration index. #[must_use] pub fn get_entity_uid(&self, entity_index: usize) -> Option { Some(self.inner.iter().nth(entity_index)?.uid()) } pub(crate) fn new(world: &'world World) -> Self { let mut terms_builder = Terms::builder(); FieldTerms::apply_terms_to_builder(&mut terms_builder); FieldlessTerms::apply_terms_to_builder(&mut terms_builder); Self { world, inner: world.flexible_query(terms_builder.build()), _pd: PhantomData, } } } impl<'query, 'world, FieldTerms, FieldlessTerms> IntoIterator for &'query Query<'world, FieldTerms, FieldlessTerms> where FieldTerms: TermWithFieldTuple + 'world, FieldlessTerms: TermWithoutFieldTuple, { type IntoIter = Iter<'query, 'world, FieldTerms, FlexibleQueryIter<'query>>; type Item = FieldTerms::Fields<'query>; fn into_iter(self) -> Self::IntoIter { self.iter() } } impl<'world, FieldTerms, FieldlessTerms> SystemParam<'world> for Query<'world, FieldTerms, FieldlessTerms> where FieldTerms: TermWithFieldTuple, FieldlessTerms: TermWithoutFieldTuple, { type Input = (); fn initialize( _system: &mut impl System<'world, SystemImpl>, _input: Self::Input, ) { } fn new( _system: &'world impl System<'world, SystemImpl>, world: &'world World, ) -> Self { Self::new(world) } } #[derive(Debug)] pub struct Terms { required_components: ArrayVec, excluded_components: ArrayVec, } impl Terms { pub fn builder() -> TermsBuilder { TermsBuilder::default() } } #[derive(Debug, Default)] pub struct TermsBuilder { required_components: ArrayVec, excluded_components: ArrayVec, } pub trait TermsBuilderInterface { fn with(self) -> Self; fn without(self) -> Self; fn with_required(self, ids: impl Array) -> Self; } macro_rules! impl_terms_builder { ($($impl_content: tt)*) => { impl TermsBuilderInterface for TermsBuilder { $($impl_content)* } impl TermsBuilderInterface for &mut TermsBuilder { $($impl_content)* } }; } impl_terms_builder! { #[allow(unused_mut)] fn with(mut self) -> Self { if ComponentT::is_optional() { return self; } let insert_index = self.required_components .partition_point(|id| *id <= ComponentT::id()); self.required_components .insert(insert_index, ComponentT::id()); self } #[allow(unused_mut)] fn without(mut self) -> Self { if ComponentT::is_optional() { panic!( "{}::without cannot take optional component", type_name::() ); } let insert_index = self.excluded_components .partition_point(|id| *id <= ComponentT::id()); self.excluded_components .insert(insert_index, ComponentT::id()); self } #[allow(unused_mut)] fn with_required(mut self, mut ids: impl Array) -> Self { if !ids.as_ref().is_sorted() { ids.as_mut().sort(); } if self.required_components.len() == 0 { self.required_components.extend(ids); return self; } let mut id_iter = ids.into_iter(); while let Some(id) = id_iter.next() { let insert_index = self.required_components .partition_point(|other_id| *other_id <= id); if insert_index == self.required_components.len() { self.required_components.extend([id].into_iter().chain(id_iter)); return self; } self.required_components .insert(insert_index, id); } self } } impl TermsBuilder { pub fn build(self) -> Terms { assert!(self.required_components.is_sorted()); assert!(self.excluded_components.is_sorted()); Terms { required_components: self.required_components, excluded_components: self.excluded_components, } } } pub trait TermWithoutField { fn apply_to_terms_builder( terms_builder: &mut TermsBuilder, ); } pub trait TermWithField { type Field<'a>; fn apply_to_terms_builder( terms_builder: &mut TermsBuilder, ); fn get_field<'world>( entity_handle: &EntityHandle<'world>, world: &'world World, ) -> Self::Field<'world>; } impl TermWithField for ComponentRefT { type Field<'a> = ComponentRefT::Handle<'a>; fn apply_to_terms_builder( terms_builder: &mut TermsBuilder, ) { terms_builder.with::(); } fn get_field<'world>( entity_handle: &EntityHandle<'world>, world: &'world World, ) -> Self::Field<'world> { Self::Field::from_locked_optional_component( entity_handle .get_component(ComponentRefT::Component::id()) .map(|component| component.component()), world, ) .unwrap_or_else(|err| { panic!( "Taking component {} lock failed: {err}", type_name::() ); }) } } pub trait TermWithoutFieldTuple { fn apply_terms_to_builder( terms_builder: &mut TermsBuilder, ); } pub trait TermWithFieldTuple { type Fields<'component>; fn apply_terms_to_builder( terms_builder: &mut TermsBuilder, ); fn get_fields<'component>( entity_handle: &EntityHandle<'component>, world: &'component World, ) -> Self::Fields<'component>; } pub struct Iter<'query, 'world, FieldTerms, EntityHandleIter> where FieldTerms: TermWithFieldTuple + 'world, EntityHandleIter: Iterator>, { world: &'world World, iter: EntityHandleIter, comps_pd: PhantomData, } impl<'query, 'world, FieldTerms, EntityHandleIter> Iter<'query, 'world, FieldTerms, EntityHandleIter> where FieldTerms: TermWithFieldTuple + 'world, EntityHandleIter: Iterator>, 'world: 'query, { /// Creates a new iterator from the given entity handle iterator. /// /// # Important /// All of the yielded entities of the entity handle iterator should match the /// terms `Terms`. The [`Self::next`] function will panic if it encounters a /// entity that does not match the terms `Terms`. pub fn new(world: &'world World, iter: EntityHandleIter) -> Self { Self { world, iter, comps_pd: PhantomData } } } impl<'query, 'world, FieldTerms, EntityHandleIter> Iterator for Iter<'query, 'world, FieldTerms, EntityHandleIter> where FieldTerms: TermWithFieldTuple + 'world, EntityHandleIter: Iterator>, 'world: 'query, { type Item = FieldTerms::Fields<'query>; fn next(&mut self) -> Option { let entity_handle = self.iter.next()?; Some(FieldTerms::get_fields(&entity_handle, self.world)) } } pub struct ComponentAndEuidIter<'query, 'world, FieldTerms, EntityHandleIter> where FieldTerms: TermWithFieldTuple + 'world, EntityHandleIter: Iterator>, { world: &'world World, iter: EntityHandleIter, comps_pd: PhantomData, } impl<'query, 'world, FieldTerms, EntityHandleIter> Iterator for ComponentAndEuidIter<'query, 'world, FieldTerms, EntityHandleIter> where FieldTerms: TermWithFieldTuple + 'world, EntityHandleIter: Iterator>, 'world: 'query, { type Item = (Uid, FieldTerms::Fields<'query>); fn next(&mut self) -> Option { let entity_handle = self.iter.next()?; Some(( entity_handle.uid(), FieldTerms::get_fields(&entity_handle, self.world), )) } } macro_rules! impl_term_sequence { ($c: tt) => { seq!(I in 0..=$c { impl<#(Term~I: TermWithoutField,)*> TermWithoutFieldTuple for (#(Term~I,)*) { fn apply_terms_to_builder( terms_builder: &mut TermsBuilder ) { #( Term~I::apply_to_terms_builder(terms_builder); )* } } impl<#(Term~I: TermWithField,)*> TermWithFieldTuple for (#(Term~I,)*) { type Fields<'component> = (#(Term~I::Field<'component>,)*); fn apply_terms_to_builder( terms_builder: &mut TermsBuilder ) { #( Term~I::apply_to_terms_builder(terms_builder); )* } fn get_fields<'component>( entity_handle: &EntityHandle<'component>, world: &'component World, ) -> Self::Fields<'component> { (#(Term~I::get_field(entity_handle, world),)*) } } }); }; } seq!(C in 0..=16 { impl_term_sequence!(C); }); impl TermWithoutFieldTuple for () { fn apply_terms_to_builder( _terms_builder: &mut TermsBuilder, ) { } } impl TermWithFieldTuple for () { type Fields<'component> = (); fn apply_terms_to_builder( _terms_builder: &mut TermsBuilder, ) { } fn get_fields<'component>( _entity_handle: &EntityHandle<'_>, _world: &'component World, ) -> Self::Fields<'component> { } }