1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
use bevy::{
ecs::system::SystemState,
prelude::{AncestorIter, Entity, Parent, Query, World},
};
use serde::{Deserialize, Serialize};
use strum::{AsRefStr, EnumString};
use uuid::Uuid;
use crate::{components::DirworldEntity, resources::DirworldCurrentDir};
// I Store Conditions as Enum Data
#[derive(Serialize, Deserialize, AsRefStr, Debug, Default, Clone, PartialEq, Eq)]
pub enum Condition {
#[default]
#[strum(serialize = "Always True")]
True,
#[strum(serialize = "Child Of")]
ChildOf {
child: Uuid,
parent: Uuid,
},
#[strum(serialize = "Parent Of")]
ParentOf {
parent: Uuid,
child: Uuid,
},
#[strum(serialize = "Descendant Of")]
DescendantOf {
descendant: Uuid,
ancestor: Uuid,
},
#[strum(serialize = "Ancestor Of")]
AncestorOf {
ancestor: Uuid,
descendant: Uuid,
},
#[strum(serialize = "In Room")]
InRoom(Uuid),
#[strum(serialize = "Object In Room")]
ObjectInRoom(Uuid),
}
impl Condition {
pub fn evaluate(&self, world: &mut World) -> bool {
match self {
Condition::True => true,
Condition::ChildOf { child, parent } => parent_of(world, *parent, *child),
Condition::ParentOf { parent, child } => parent_of(world, *parent, *child),
Condition::DescendantOf {
descendant,
ancestor,
} => ancestor_of(world, *ancestor, *descendant),
Condition::AncestorOf {
ancestor,
descendant,
} => ancestor_of(world, *ancestor, *descendant),
Condition::InRoom(uuid) => in_room(world, *uuid),
Condition::ObjectInRoom(uuid) => object_in_room(world, *uuid),
}
}
pub fn get_api_function_name(&self) -> &'static str {
match self {
Condition::True => "conditional_true",
Condition::ChildOf { .. } => "conditional_child_of",
Condition::ParentOf { .. } => "conditional_parent_of",
Condition::DescendantOf { .. } => "conditional_descendant_of",
Condition::AncestorOf { .. } =>"conditional_ancestor_of",
Condition::InRoom(_) => "conditional_in_room",
Condition::ObjectInRoom(_) => "conditional_object_in_room",
}
}
pub fn from_api_function_name_and_args(name: &str, args: &[&str]) -> Option<Self> {
match name {
"conditional_true" => Some(Condition::True),
"conditional_child_of" => {
let Some(child) = args.get(0).and_then(|arg| Uuid::parse_str(arg).ok()) else {
return None;
};
let Some(parent) = args.get(1).and_then(|arg| Uuid::parse_str(arg).ok()) else {
return None;
};
Some(Condition::ChildOf { child, parent })
}
"conditional_parent_of" => {
let Some(child) = args.get(1).and_then(|arg| Uuid::parse_str(arg).ok()) else {
return None;
};
let Some(parent) = args.get(0).and_then(|arg| Uuid::parse_str(arg).ok()) else {
return None;
};
Some(Condition::ParentOf { child, parent })
}
"conditional_descendant_of" => {
let Some(descendant) = args.get(0).and_then(|arg| Uuid::parse_str(arg).ok()) else {
return None;
};
let Some(ancestor) = args.get(1).and_then(|arg| Uuid::parse_str(arg).ok()) else {
return None;
};
Some(Condition::DescendantOf { descendant, ancestor })
}
"conditional_ancestor_of" => {
let Some(descendant) = args.get(1).and_then(|arg| Uuid::parse_str(arg).ok()) else {
return None;
};
let Some(ancestor) = args.get(0).and_then(|arg| Uuid::parse_str(arg).ok()) else {
return None;
};
Some(Condition::AncestorOf { descendant, ancestor })
}
"condtitional_in_room" => {
let Some(room_id) = args.get(0).and_then(|arg| Uuid::parse_str(arg).ok()) else {
return None;
};
Some(Condition::InRoom(room_id))
}
"condtitional_object_in_room" => {
let Some(object_id) = args.get(0).and_then(|arg| Uuid::parse_str(arg).ok()) else {
return None;
};
Some(Condition::ObjectInRoom(object_id))
}
_ => None,
}
}
}
// Condition Checkers beyond this point
fn ancestor_of(world: &mut World, ancestor: Uuid, descendant: Uuid) -> bool {
let mut system_state =
SystemState::<(Query<(Entity, &DirworldEntity)>, Query<&Parent>)>::new(world);
let (dirworld_entities, parents) = system_state.get(world);
let Some((ancestor_entity, _)) = dirworld_entities.iter().find(|(_, entity)| {
entity
.payload
.as_ref()
.is_some_and(|payload| payload.id == ancestor)
}) else {
return false;
};
let Some((descendant_entity, _)) = dirworld_entities.iter().find(|(_, entity)| {
entity
.payload
.as_ref()
.is_some_and(|payload| payload.id == descendant)
}) else {
return false;
};
AncestorIter::new(&parents, descendant_entity)
.find(|descendant| *descendant == ancestor_entity)
.is_some()
}
fn parent_of(world: &mut World, parent: Uuid, child: Uuid) -> bool {
let mut system_state =
SystemState::<(Query<(Entity, &DirworldEntity)>, Query<&Parent>)>::new(world);
let (dirworld_entities, parents) = system_state.get(world);
let Some((parent_entity, _)) = dirworld_entities.iter().find(|(_, entity)| {
entity
.payload
.as_ref()
.is_some_and(|payload| payload.id == parent)
}) else {
return false;
};
let Some((child_entity, _)) = dirworld_entities.iter().find(|(_, entity)| {
entity
.payload
.as_ref()
.is_some_and(|payload| payload.id == child)
}) else {
return false;
};
parents
.get(child_entity)
.is_ok_and(|parent| parent.get() == parent_entity)
}
fn in_room(world: &mut World, room: Uuid) -> bool {
let current_dir = world.resource::<DirworldCurrentDir>();
current_dir.payload.as_ref().is_some_and(|payload| payload.id == room)
}
fn object_in_room(world: &mut World, object: Uuid) -> bool {
let mut dirworld_entities = world.query::<&DirworldEntity>();
dirworld_entities
.iter(world)
.find(|entity| {
entity
.payload
.as_ref()
.is_some_and(|payload| payload.id == object)
})
.is_some()
}
|