use bevy::platform::prelude::{String, ToOwned};
use bevy::prelude::*;
use bevy::platform::sync::Arc;
use crate::types::{self, ActionContextRef, AiEntity, CraniumKvMap, CraniumRwLock, PawnEntityRef};
use crate::identifiers::{ConsiderationIdentifier, CurveIdentifier};
#[cfg(any(feature = "actionset_loader"))]
use serde::{Serialize, Deserialize};
#[derive(Clone, Debug, Reflect)]
#[cfg_attr(feature = "actionset_loader", derive(Serialize, Deserialize))]
pub struct ConsiderationData {
#[cfg_attr(any(feature = "actionset_loader"), serde(rename="consideration"))]
pub consideration_name: ConsiderationIdentifier,
#[cfg_attr(any(feature = "actionset_loader"), serde(rename="curve"))]
pub curve_name: CurveIdentifier,
pub min: types::ActionScore,
pub max: types::ActionScore,
}
impl ConsiderationData {
pub fn new<CNN: Into<ConsiderationIdentifier>, CRN: Into<CurveIdentifier>>(
consideration_name: CNN,
curve_name: CRN,
min: types::ActionScore,
max: types::ActionScore,
) -> Self {
Self {
consideration_name: consideration_name.into(),
curve_name: curve_name.into(),
min: min,
max: max,
}
}
}
pub type ConsiderationInputs = bevy::prelude::In<(
AiEntity,
PawnEntityRef,
ActionContextRef,
)>;
pub type ConsiderationOutputs = Option<f32>;
pub trait ConsiderationSystem: bevy::ecs::system::ReadOnlySystem<
In = ConsiderationInputs,
Out = ConsiderationOutputs
> {}
impl<
ROS: bevy::ecs::system::ReadOnlySystem<
In = ConsiderationInputs,
Out = ConsiderationOutputs
>
> ConsiderationSystem for ROS {}
pub trait IntoConsiderationSystem<Marker>: IntoSystem<
ConsiderationInputs,
ConsiderationOutputs,
Marker,
> {}
impl<
Marker,
CS: ConsiderationSystem,
IS: IntoSystem<
ConsiderationInputs,
ConsiderationOutputs,
Marker,
System = CS
>
> IntoConsiderationSystem<Marker> for IS {}
#[derive(Clone)]
pub struct ConsiderationMappedToSystem {
pub func_name: ConsiderationIdentifier,
pub consideration_system: Result<Arc<CraniumRwLock<dyn ConsiderationSystem>>, ()>,
pub curve_name: CurveIdentifier,
pub min: types::ActionScore,
pub max: types::ActionScore,
}
#[derive(Resource, Default)]
pub struct ConsiderationKeyToSystemMap {
pub mapping: CraniumKvMap<
ConsiderationIdentifier,
Arc<CraniumRwLock<dyn ConsiderationSystem>>
>
}
pub trait AcceptsConsiderationRegistrations {
fn register_consideration<
CS: ConsiderationSystem,
Marker,
F: IntoConsiderationSystem<Marker, System = CS> + 'static,
IS: Into<String>,
>(
&mut self,
consideration: F,
key: IS,
) -> &mut Self;
}
impl AcceptsConsiderationRegistrations for App {
fn register_consideration<
CS: ConsiderationSystem,
Marker,
F: IntoConsiderationSystem<Marker, System = CS> + 'static,
IS: Into<String>,
>(
&mut self,
consideration: F,
key: IS
) -> &mut Self {
self.world_mut().register_consideration(consideration, key);
self
}
}
impl AcceptsConsiderationRegistrations for &mut App {
fn register_consideration<
CS: ConsiderationSystem,
Marker,
F: IntoConsiderationSystem<Marker, System = CS> + 'static,
IS: Into<String>,
>(
&mut self,
consideration: F,
key: IS
) -> &mut Self {
self.world_mut().register_consideration(consideration, key);
self
}
}
impl AcceptsConsiderationRegistrations for World {
fn register_consideration<
CS: ConsiderationSystem,
Marker,
F: IntoConsiderationSystem<Marker, System = CS> + 'static,
IS: Into<String>,
>(
&mut self,
consideration: F,
key: IS
) -> &mut Self {
let system = F::into_system(consideration);
let system_key = ConsiderationIdentifier::from(key);
let mut system_registry = self.get_resource_or_init::<ConsiderationKeyToSystemMap>();
let old = system_registry.mapping.insert(
system_key.to_owned(),
Arc::new(CraniumRwLock::new(
system
)));
match old {
None => {},
Some(_) => {
#[cfg(feature = "logging")]
bevy::log::warn!(
"Detected a key collision for key {:?}. Ejecting previous registration...",
system_key
);
}
}
self
}
}
#[derive(Resource, Debug)]
pub struct ShouldReinitConsiderationQueries(bool);
impl ShouldReinitConsiderationQueries {
pub fn get(&self) -> bool {
self.0
}
pub fn set(&mut self, val: bool) {
self.0 = val;
}
}
impl Default for ShouldReinitConsiderationQueries {
fn default() -> Self {
Self(true)
}
}
pub fn reinit_consideration_queries(world: &mut World) {
let world_cell = world.as_unsafe_world_cell();
let should_reinit_res = unsafe {
world_cell.get_resource::<ShouldReinitConsiderationQueries>()
};
let should_reinit = match should_reinit_res {
None => true,
Some(reinit_mark) => reinit_mark.get()
};
if !should_reinit {
return
};
let registry = unsafe {
world_cell.get_resource_mut::<ConsiderationKeyToSystemMap>()
};
let mut registry = match registry {
None => return,
Some(r) => r,
};
registry.mapping.iter_mut().for_each(|(_key, system_lock)| {
let write_state = system_lock.write();
{
match write_state {
Ok(mut system) => {
#[cfg(feature = "logging")]
bevy::log::debug!("reinit_consideration_queries: Reinitializing System {:?}", _key);
system.initialize(unsafe { world_cell.world_mut() });
},
Err(e) => panic!("{:?}", e)
}
}
});
}
pub struct ConsiderationPlugin;
impl Plugin for ConsiderationPlugin {
fn build(&self, app: &mut App) {
app
.init_resource::<ShouldReinitConsiderationQueries>()
.init_resource::<ConsiderationKeyToSystemMap>()
.add_systems(Startup, reinit_consideration_queries)
.add_systems(FixedFirst, reinit_consideration_queries)
.add_observer(crate::decision_loop::disable_consideration_reinit)
;
}
}