Skip to content

Commit 083e42b

Browse files
committed
Moved around more stuff
1 parent 5ce8b90 commit 083e42b

File tree

14 files changed

+104
-122
lines changed

14 files changed

+104
-122
lines changed

Cargo.lock

+2-26
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/dogoap/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,3 @@ homepage = "https://github.com/victorb/dogoap"
1010
bevy_reflect = "0.14.0"
1111
log = "0.4.22"
1212
pathfinding = "4.10.0"
13-
serde_json = "1.0.120"

crates/dogoap/benches/long_plan.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ mod tests {
2323

2424
let goal = Goal::new().with_req("gold", Compare::Equals(Datum::I64(7)));
2525

26-
// TOOD should keep the `10 as 64` syntax with .from somehow
2726
let sleep_action = simple_increment_action("sleep", "energy", Datum::I64(10));
2827

2928
let eat_action = simple_decrement_action("eat", "hunger", Datum::I64(10))
@@ -46,20 +45,13 @@ mod tests {
4645

4746
let plan = make_plan_with_strategy(strategy, &start, &actions[..], &goal);
4847
let effects = get_effects_from_plan(plan.clone().unwrap().0);
49-
assert_eq!(11, effects.len());
50-
51-
print_plan(plan.unwrap());
5248

49+
assert_eq!(11, effects.len());
5350
assert_eq!(expected_state, effects.last().unwrap().state);
5451
}
5552

5653
#[bench]
5754
fn bench_start_to_goal_strategy(b: &mut Bencher) {
5855
b.iter(|| long_plan(PlanningStrategy::StartToGoal));
5956
}
60-
61-
// #[bench]
62-
// fn bench_goal_to_start_strategy(b: &mut Bencher) {
63-
// b.iter(|| long_plan(PlanningStrategy::GoalToStart));
64-
// }
6557
}

crates/dogoap/src/action.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ use crate::mutator::Mutator;
1414
pub struct Action {
1515
/// String like `eat_action`
1616
pub key: String,
17-
// TODO arguments coupled with Effects
17+
// TODO arguments coupled with Effects, maybe
1818
// pub argument: Option<Datum>,
1919
/// What preconditions need to be true before we can execute this action
2020
pub preconditions: Vec<(String, Compare)>,
2121
/// What is the outcome from doing this action
22+
// TODO temporarily plural effects, as maybe we want to implement arguments with many effects...
2223
pub effects: Vec<Effect>,
2324
}
2425

crates/dogoap/src/compare.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
use crate::{action::Action, datum::Datum, state::LocalState};
1+
use crate::{action::Action, datum::Datum, localstate::LocalState};
22
use bevy_reflect::Reflect;
33
use std::hash::{Hash, Hasher};
44

5-
// TODO rename these to more rust familiar names?
6-
// https://doc.rust-lang.org/reference/expressions/operator-expr.html#comparison-operators
75
#[derive(Reflect, Clone, Debug, PartialEq)]
86
pub enum Compare {
97
Equals(Datum),
@@ -55,7 +53,6 @@ pub fn compare_values(comparison: &Compare, value: &Datum) -> bool {
5553
}
5654
}
5755

58-
// TODO should be in Action or LocalState instead perhaps?
5956
/// Checks all the preconditions from the `Action` against passed in `LocalState`
6057
/// Returns `true` if all the preconditions pass (or if there is none), otherwise `false`
6158
pub fn check_preconditions(state: &LocalState, action: &Action) -> bool {

crates/dogoap/src/datum.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
use bevy_reflect::*;
21
use std::fmt::Display;
32
use std::hash::Hash;
43
use std::ops::{Add, AddAssign, Sub, SubAssign};
54

5+
use bevy_reflect::Reflect;
6+
67
#[derive(Reflect, Clone, Debug, PartialOrd, Copy)]
78
pub enum Datum {
89
Bool(bool),

crates/dogoap/src/effect.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{mutator::Mutator, state::LocalState};
1+
use crate::{localstate::LocalState, mutator::Mutator};
22
use bevy_reflect::Reflect;
33
use std::hash::{Hash, Hasher};
44

crates/dogoap/src/goal.rs

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::hash::{Hash, Hasher};
33

44
use bevy_reflect::*;
55

6-
// use crate::requirement::DynRequirement;
76
use crate::compare::Compare;
87

98
#[derive(Reflect, Clone, Debug, PartialEq)]

crates/dogoap/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
#![feature(trivial_bounds)]
12
#![doc = include_str!("../README.md")]
23
mod action;
34
mod compare;
45
mod datum;
56
mod effect;
67
mod goal;
8+
mod localstate;
79
mod mutator;
8-
mod state;
910

1011
pub mod planner;
1112
pub mod prelude;

crates/dogoap/src/state.rs renamed to crates/dogoap/src/localstate.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,34 @@
1+
use std::hash::{Hash, Hasher};
2+
13
use std::collections::BTreeMap;
4+
// List of other hashmap-likes we've tried, but none faster than BTreeMap
25
// use scc::HashMap as BTreeMap;
36
// use rustc_hash::FxHashMap as BTreeMap;
47
// use ahash::HashMapExt;
58
// use indexset::BTreeMap;
69
// use cow_hashmap::CowHashMap as BTreeMap;
7-
use std::hash::{Hash, Hasher};
8-
910
// use ahash::{AHasher, RandomState};
1011
// use std::collections::HashMap as BTreeMap;
11-
1212
// use ahash::AHashMap as BTreeMap;
13+
// use indexmap::IndexMap; // 37,873.88 ns/iter
14+
// use micromap::Map; // 30,480.55 ns/iter
15+
16+
use bevy_reflect::Reflect;
1317

1418
use crate::datum::Datum;
1519
use crate::goal::Goal;
1620

17-
use bevy_reflect::*;
18-
// use indexmap::IndexMap;
21+
pub type InternalData = BTreeMap<String, Datum>;
1922

2023
#[derive(Reflect, Debug, Clone, Eq, PartialEq, Default)]
2124
pub struct LocalState {
22-
pub data: BTreeMap<String, Datum>,
25+
pub data: InternalData,
2326
}
2427

2528
impl LocalState {
2629
pub fn new() -> Self {
2730
Self {
28-
data: BTreeMap::new(),
31+
data: InternalData::new(),
2932
}
3033
}
3134

crates/dogoap/src/mutator.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use crate::datum::Datum;
1+
use std::collections::BTreeMap;
2+
3+
use crate::{datum::Datum, localstate::InternalData};
24

35
use bevy_reflect::*;
46

@@ -9,6 +11,24 @@ pub enum Mutator {
911
Decrement(String, Datum), // :key, :decrement-by
1012
}
1113

14+
pub fn apply_mutator(data: &mut InternalData, mutator: &Mutator) {
15+
match mutator {
16+
Mutator::Set(key, value) => {
17+
data.insert(key.to_string(), *value);
18+
}
19+
Mutator::Increment(key, value) => {
20+
if let Some(current_value) = data.get_mut(key) {
21+
*current_value += *value;
22+
}
23+
}
24+
Mutator::Decrement(key, value) => {
25+
if let Some(current_value) = data.get_mut(key) {
26+
*current_value -= *value;
27+
}
28+
}
29+
}
30+
}
31+
1232
pub fn print_mutators(mutators: Vec<Mutator>) {
1333
for mutator in mutators {
1434
match mutator {

crates/dogoap/src/planner.rs

+30-40
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
use std::collections::BTreeMap;
2+
13
use crate::{
24
action::Action,
35
compare::{check_preconditions, compare_values},
6+
datum::Datum,
47
effect::Effect,
58
goal::Goal,
6-
mutator::{print_mutators, Mutator},
7-
state::LocalState,
9+
localstate::LocalState,
10+
mutator::{apply_mutator, print_mutators, Mutator},
811
};
912

1013
use bevy_reflect::Reflect;
@@ -44,35 +47,22 @@ fn successors<'a>(
4447
) -> impl Iterator<Item = (Node, usize)> + 'a {
4548
let state = node.state();
4649
actions.iter().filter_map(move |action| {
47-
if check_preconditions(state, action) {
48-
let mut new_state = state.clone();
49-
if action.effects.len() == 0 {
50-
return None;
51-
}
52-
for mutator in &action.effects[0].mutators {
53-
match mutator {
54-
Mutator::Set(key, value) => {
55-
new_state.data.insert(key.to_string(), *value);
56-
}
57-
Mutator::Increment(key, value) => {
58-
if let Some(current_value) = new_state.data.get_mut(key) {
59-
*current_value += *value;
60-
}
61-
}
62-
Mutator::Decrement(key, value) => {
63-
if let Some(current_value) = new_state.data.get_mut(key) {
64-
*current_value -= *value;
65-
}
66-
}
67-
}
50+
if check_preconditions(state, action) && !action.effects.is_empty() {
51+
let new_state = state.clone();
52+
let first_effect = &action.effects[0];
53+
54+
let mut new_data = new_state.data.clone();
55+
for mutator in &first_effect.mutators {
56+
apply_mutator(&mut new_data, mutator);
6857
}
58+
6959
let new_effect = Effect {
70-
action: action.effects[0].action.clone(),
71-
mutators: action.effects[0].mutators.clone(),
72-
cost: action.effects[0].cost,
73-
state: new_state,
60+
action: first_effect.action.clone(),
61+
mutators: first_effect.mutators.clone(),
62+
cost: first_effect.cost,
63+
state: LocalState { data: new_data },
7464
};
75-
Some((Node::Effect(new_effect), action.effects[0].cost))
65+
Some((Node::Effect(new_effect), first_effect.cost))
7666
} else {
7767
None
7868
}
@@ -89,18 +79,6 @@ fn is_goal(node: &Node, goal: &Goal) -> bool {
8979
})
9080
}
9181

92-
/// We implement two different strategies for finding a solution
93-
#[derive(Default)]
94-
pub enum PlanningStrategy {
95-
#[default]
96-
/// StartToGoal begins with our current state, and finds the most optimal path to the goal, based on the costs
97-
/// Might take longer time than GoalToStart, but finds the path with the lowest cost
98-
StartToGoal,
99-
/// GoalToStart begins with the goal state, and works backwards from there, in order to find a path as quick as possible
100-
/// Might lead to less-than-optimial paths, but should find a valid path quicker
101-
GoalToStart,
102-
}
103-
10482
pub fn make_plan_with_strategy(
10583
strategy: PlanningStrategy,
10684
start: &LocalState,
@@ -123,6 +101,18 @@ pub fn make_plan_with_strategy(
123101
}
124102
}
125103

104+
/// We implement two different strategies for finding a solution
105+
#[derive(Default)]
106+
pub enum PlanningStrategy {
107+
#[default]
108+
/// StartToGoal begins with our current state, and finds the most optimal path to the goal, based on the costs
109+
/// Might take longer time than GoalToStart, but finds the path with the lowest cost
110+
StartToGoal,
111+
/// GoalToStart begins with the goal state, and works backwards from there, in order to find a path as quick as possible
112+
/// Might lead to less-than-optimial paths, but should find a valid path quicker
113+
GoalToStart,
114+
}
115+
126116
pub fn make_plan(
127117
start: &LocalState,
128118
actions: &[Action],

crates/dogoap/src/prelude.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ pub use crate::compare::Compare;
44
pub use crate::datum::Datum;
55
pub use crate::effect::Effect;
66
pub use crate::goal::Goal;
7+
pub use crate::localstate::LocalState;
78
pub use crate::mutator::Mutator;
89
pub use crate::planner::{
910
get_effects_from_plan, make_plan, make_plan_with_strategy, print_plan, Node, PlanningStrategy,
1011
};
11-
pub use crate::state::LocalState;

0 commit comments

Comments
 (0)