diff --git a/src/data.rs b/src/data.rs index 408393c..af18b61 100644 --- a/src/data.rs +++ b/src/data.rs @@ -106,14 +106,12 @@ impl WorldState { return false; } } - return true; + true } pub fn get(&self, s: impl Into) -> Option { - let Some(value) = self.entries.get(&s.into()) else { - return None; - }; - return Some(value.clone()); + let value = self.entries.get(&s.into())?; + Some(value.clone()) } pub fn append(&mut self, other: &WorldState) { @@ -237,7 +235,7 @@ where for (name, truth) in value { let un: UniqueName = name.into(); - if let Some(_) = map.insert(un.clone(), truth) { + if map.insert(un.clone(), truth).is_some() { warn!("Duplicate entries for key: {:?}", un); } } @@ -285,6 +283,7 @@ impl From for Variant { } } +#[allow(clippy::from_over_into)] impl Into for &'static str { fn into(self) -> Variant { Variant::String(self.into()) @@ -297,6 +296,7 @@ impl From for Variant { } } +#[allow(clippy::from_over_into)] impl Into for WorldState { /// Converts the world state to a Requirements struct with all predicates being `Equals`. /// Not terribly customizeable, but that's what you get for taking the easy way you rapscallion! @@ -309,6 +309,7 @@ impl Into for WorldState { } } +#[allow(clippy::from_over_into)] impl Into for &'static str { fn into(self) -> UniqueName { UniqueName::new(self) diff --git a/src/execution.rs b/src/execution.rs index 9220721..c0d9b03 100644 --- a/src/execution.rs +++ b/src/execution.rs @@ -26,6 +26,7 @@ pub enum HtnAgentState { Failure, } +#[allow(clippy::type_complexity)] pub fn system_extract_plans_for_unplanned_agents( query: Query< ( @@ -115,38 +116,36 @@ pub fn system_handle_agent_state_changes( .remove::<(HtnAgentCurrentTask, HtnAgentState, HtnAgentPlan)>(); } } + } else if let Some(next_task) = plan.plan_stack.pop() { + push_task_to_agent(next_task, &mut command.entity(entity), &task_registry); } else { - if let Some(next_task) = plan.plan_stack.pop() { - push_task_to_agent(next_task, &mut command.entity(entity), &task_registry); - } else { - command - .entity(entity) - .remove::<(HtnAgentCurrentTask, HtnAgentState, HtnAgentPlan)>(); - warn!("Failed to initialize a plan for entity {}", entity); - } + command + .entity(entity) + .remove::<(HtnAgentCurrentTask, HtnAgentState, HtnAgentPlan)>(); + warn!("Failed to initialize a plan for entity {}", entity); } } } fn push_task_to_agent( task: String, - mut entity: &mut EntityCommands, + entity: &mut EntityCommands, task_registry: &Res, ) { let Some(task_data) = task_registry.get_named(&task) else { return; }; - task_data.add(&mut entity); + task_data.add(entity); entity.insert((HtnAgentCurrentTask(task), HtnAgentState::Running)); } fn try_remove_previous_task( - mut entity: &mut EntityCommands, + entity: &mut EntityCommands, task_registry: &Res, previous: &HtnAgentCurrentTask, ) { let Some(task) = task_registry.get_named(&previous.0) else { return; }; - task.remove(&mut entity); + task.remove(entity); } diff --git a/src/lib.rs b/src/lib.rs index 6881f78..14dc39d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -31,7 +31,11 @@ pub mod prelude { crate::orchestration::orchestrate_systems(app, &self.orchestrate); } } - + impl Default for HtnPlanningPlugin { + fn default() -> Self { + Self::new() + } + } impl HtnPlanningPlugin { pub fn new() -> Self { Self { diff --git a/src/planning/plan_data.rs b/src/planning/plan_data.rs index 5039e7f..57d0a08 100644 --- a/src/planning/plan_data.rs +++ b/src/planning/plan_data.rs @@ -3,7 +3,6 @@ use std::{ fmt::Debug, sync::Arc, time::{Duration, Instant}, - u32, }; use bevy::{ @@ -45,7 +44,7 @@ impl Debug for Plan { } } -#[derive(Component)] +#[derive(Component, Default)] pub struct TimeSlicedTreeGen { pub active_nodes: VecDeque>>, pub valid_nodes: Vec>>, @@ -148,7 +147,7 @@ impl TimeSlicedTreeGen { value: PlanNode { task: Some(s), world: current_world.clone().concat(data.postconditions()), - cost: data.cost(¤t_world), + cost: data.cost(current_world), depth: 0, }, parent: None, @@ -232,30 +231,10 @@ impl TimeSlicedTreeGen { let Some(ref parent3) = parent2.parent else { return false; }; - let t0 = node - .value - .task - .as_ref() - .and_then(|task| Some(task.name())) - .unwrap_or("0".into()); - let t1 = parent - .value - .task - .as_ref() - .and_then(|task| Some(task.name())) - .unwrap_or("0".into()); - let t2 = parent2 - .value - .task - .as_ref() - .and_then(|task| Some(task.name())) - .unwrap_or("0".into()); - let t4 = parent3 - .value - .task - .as_ref() - .and_then(|task| Some(task.name())) - .unwrap_or("0".into()); + let t0 = node.value.task.as_ref().map(|task| task.name()); + let t1 = parent.value.task.as_ref().map(|task| task.name()); + let t2 = parent2.value.task.as_ref().map(|task| task.name()); + let t4 = parent3.value.task.as_ref().map(|task| task.name()); // this only catches A-B-A-B patterns, not A-B-C-A-B-C patterns // goddamn I need a better solution @@ -284,9 +263,8 @@ impl TimeSlicedTreeGen { task: &Task, registry: &TaskRegistry, ) -> Option> { - let Some(data) = registry.get_task(task) else { - return None; - }; + let data = registry.get_task(task)?; + let virtual_world = parent.value.world.concat(data.postconditions()); Some(Node:: { value: PlanNode { diff --git a/src/tasks.rs b/src/tasks.rs index c63c38a..8d07ae2 100644 --- a/src/tasks.rs +++ b/src/tasks.rs @@ -77,9 +77,7 @@ impl TaskRegistry { }) .unwrap_or_default() { - let Some(data) = self.get_named(&t) else { - return None; - }; + let data = self.get_named(&t)?; req = req.unmet_requirements(data.postconditions()); req.append(data.preconditions()); } @@ -109,9 +107,7 @@ impl TaskRegistry { }) .unwrap_or_default() { - let Some(data) = self.get_named(&t) else { - return None; - }; + let data = self.get_named(&t)?; context = data.preconditions().consume(&context); context.append(data.postconditions()); } @@ -187,7 +183,7 @@ pub enum Task { impl Task { pub fn decompose_iter(iter: impl Iterator) -> Vec { iter.map(|t| t.decompose()) - .reduce(|agg, item| agg.into_iter().chain(item.into_iter()).collect()) + .reduce(|agg, item| agg.into_iter().chain(item).collect()) .unwrap_or_default() }