Skip to content

Commit

Permalink
Clippy's Bloodlust Satiated
Browse files Browse the repository at this point in the history
  • Loading branch information
QueenOfSquiggles committed Aug 9, 2024
1 parent 85ff7a7 commit 2f92cbb
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 56 deletions.
13 changes: 7 additions & 6 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,12 @@ impl WorldState {
return false;
}
}
return true;
true
}

pub fn get(&self, s: impl Into<UniqueName>) -> Option<Variant> {
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) {
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -285,6 +283,7 @@ impl From<UniqueName> for Variant {
}
}

#[allow(clippy::from_over_into)]
impl Into<Variant> for &'static str {
fn into(self) -> Variant {
Variant::String(self.into())
Expand All @@ -297,6 +296,7 @@ impl From<f32> for Variant {
}
}

#[allow(clippy::from_over_into)]
impl Into<Requirements> 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!
Expand All @@ -309,6 +309,7 @@ impl Into<Requirements> for WorldState {
}
}

#[allow(clippy::from_over_into)]
impl Into<UniqueName> for &'static str {
fn into(self) -> UniqueName {
UniqueName::new(self)
Expand Down
23 changes: 11 additions & 12 deletions src/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub enum HtnAgentState {
Failure,
}

#[allow(clippy::type_complexity)]
pub fn system_extract_plans_for_unplanned_agents(
query: Query<
(
Expand Down Expand Up @@ -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<TaskRegistry>,
) {
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<TaskRegistry>,
previous: &HtnAgentCurrentTask,
) {
let Some(task) = task_registry.get_named(&previous.0) else {
return;
};
task.remove(&mut entity);
task.remove(entity);
}
6 changes: 5 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
38 changes: 8 additions & 30 deletions src/planning/plan_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::{
fmt::Debug,
sync::Arc,
time::{Duration, Instant},
u32,
};

use bevy::{
Expand Down Expand Up @@ -45,7 +44,7 @@ impl Debug for Plan {
}
}

#[derive(Component)]
#[derive(Component, Default)]
pub struct TimeSlicedTreeGen {
pub active_nodes: VecDeque<Arc<Node<PlanNode>>>,
pub valid_nodes: Vec<Arc<Node<PlanNode>>>,
Expand Down Expand Up @@ -148,7 +147,7 @@ impl TimeSlicedTreeGen {
value: PlanNode {
task: Some(s),
world: current_world.clone().concat(data.postconditions()),
cost: data.cost(&current_world),
cost: data.cost(current_world),
depth: 0,
},
parent: None,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -284,9 +263,8 @@ impl TimeSlicedTreeGen {
task: &Task,
registry: &TaskRegistry,
) -> Option<Node<PlanNode>> {
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::<PlanNode> {
value: PlanNode {
Expand Down
10 changes: 3 additions & 7 deletions src/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -187,7 +183,7 @@ pub enum Task {
impl Task {
pub fn decompose_iter(iter: impl Iterator<Item = Task>) -> Vec<String> {
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()
}

Expand Down

0 comments on commit 2f92cbb

Please sign in to comment.