fn execute_node(&mut self, instance_index: usize, node_id: usize) -> Option<BehaviorNodeConnector> {

        let mut connectors : Vec<BehaviorNodeConnector> = vec![];
        let mut connected_node_ids : Vec<usize> = vec![];
        let mut possibly_executed_connections : Vec<(BehaviorType, usize, BehaviorNodeConnector)> = vec![];

        let mut is_sequence = false;
        let mut rc : Option<BehaviorNodeConnector> = None;

        // Call the node and get the resulting BehaviorNodeConnector
        if let Some(behavior) = self.behaviors.get_mut(&self.instances[instance_index].behavior_id) {
            if let Some(node) = behavior.data.nodes.get_mut(&node_id) {

                // Handle special nodes
                if node.behavior_type == BehaviorNodeType::BehaviorTree || node.behavior_type == BehaviorNodeType::Linear {
                    connectors.push(BehaviorNodeConnector::Bottom1);
                    connectors.push(BehaviorNodeConnector::Bottom2);
                    connectors.push(BehaviorNodeConnector::Bottom);
                    connectors.push(BehaviorNodeConnector::Bottom3);
                    connectors.push(BehaviorNodeConnector::Bottom4);
                } else
                if node.behavior_type == BehaviorNodeType::Sequence {
                    connectors.push(BehaviorNodeConnector::Bottom1);
                    connectors.push(BehaviorNodeConnector::Bottom2);
                    connectors.push(BehaviorNodeConnector::Bottom);
                    connectors.push(BehaviorNodeConnector::Bottom3);
                    connectors.push(BehaviorNodeConnector::Bottom4);
                    is_sequence = true;
                } else {
                    if let Some(node_call) = self.nodes.get_mut(&node.behavior_type) {
                        let behavior_id = self.instances[instance_index].behavior_id.clone();
                        let connector = node_call(instance_index, (behavior_id, node_id), self, BehaviorType::Behaviors);
                        rc = Some(connector);
                        connectors.push(connector);
                    } else {
                        connectors.push(BehaviorNodeConnector::Bottom);
                    }
                }
            }
        }

        // Search the connections to check if we can find an ongoing node connection
        for connector in connectors {
            if let Some(behavior) = self.behaviors.get_mut(&self.instances[instance_index].behavior_id) {

                for c in &behavior.data.connections {
                    if c.0 == node_id && c.1 == connector {
                        connected_node_ids.push(c.2);
                        if is_sequence == false {
                            self.executed_connections.push((BehaviorType::Behaviors, c.0, c.1));
                        } else {
                            possibly_executed_connections.push((BehaviorType::Behaviors, c.0, c.1));
                        }
                    }
                }
            }
        }

        // And if yes execute it
        for (index, connected_node_id) in connected_node_ids.iter().enumerate() {

            // If this is a sequence, mark this connection as executed
            if is_sequence {
                self.executed_connections.push(possibly_executed_connections[index]);
            }

            if let Some(connector) = self.execute_node(instance_index, *connected_node_id) {
                if is_sequence {
                    // Inside a sequence break out if the connector is not Success
                    if connector == BehaviorNodeConnector::Fail || connector == BehaviorNodeConnector::Right {
                        break;
                    }
                }
            }
        }
        rc
    }
/// Executes the given node and follows the connection chain
Executes the given node and follows the connection chain
Executes the given node and follows the connection chain
fn execute_node(&mut self, instance_index: usize, node_id: usize) -> Option<BehaviorNodeConnector> {
        let mut connectors : Vec<BehaviorNodeConnector> = vec![];
        let mut connected_node_ids : Vec<usize> = vec![];
        let mut possibly_executed_connections : Vec<(BehaviorType, usize, BehaviorNodeConnector)> = vec![];
        let mut is_sequence = false;
        let mut rc : Option<BehaviorNodeConnector> = None;
        // Call the node and get the resulting BehaviorNodeConnector
        if let Some(behavior) = self.behaviors.get_mut(&self.instances[instance_index].behavior_id) {
            if let Some(node) = behavior.data.nodes.get_mut(&node_id) {
                // Handle special nodes
                if node.behavior_type == BehaviorNodeType::BehaviorTree || node.behavior_type == BehaviorNodeType::Linear {
                    connectors.push(BehaviorNodeConnector::Bottom1);
                    connectors.push(BehaviorNodeConnector::Bottom2);
                    connectors.push(BehaviorNodeConnector::Bottom);
                    connectors.push(BehaviorNodeConnector::Bottom3);
                    connectors.push(BehaviorNodeConnector::Bottom4);
                } else
                if node.behavior_type == BehaviorNodeType::Sequence {
                    connectors.push(BehaviorNodeConnector::Bottom1);
                    connectors.push(BehaviorNodeConnector::Bottom2);
                    connectors.push(BehaviorNodeConnector::Bottom);
                    connectors.push(BehaviorNodeConnector::Bottom3);
                    connectors.push(BehaviorNodeConnector::Bottom4);
                    is_sequence = true;
                } else {
                    if let Some(node_call) = self.nodes.get_mut(&node.behavior_type) {
                        let behavior_id = self.instances[instance_index].behavior_id.clone();
                        let connector = node_call(instance_index, (behavior_id, node_id), self, BehaviorType::Behaviors);
                        rc = Some(connector);
                        connectors.push(connector);
                    } else {
                        connectors.push(BehaviorNodeConnector::Bottom);
                    }
                }
            }
        }
        // Search the connections to check if we can find an ongoing node connection
        for connector in connectors {
            if let Some(behavior) = self.behaviors.get_mut(&self.instances[instance_index].behavior_id) {
                for c in &behavior.data.connections {
                    if c.0 == node_id && c.1 == connector {
                        connected_node_ids.push(c.2);
                        if is_sequence == false {
                            self.executed_connections.push((BehaviorType::Behaviors, c.0, c.1));
                        } else {
                            possibly_executed_connections.push((BehaviorType::Behaviors, c.0, c.1));
                        }
                    }
                }
            }
        }
        // And if yes execute it
        for (index, connected_node_id) in connected_node_ids.iter().enumerate() {
            // If this is a sequence, mark this connection as executed
            if is_sequence {
                self.executed_connections.push(possibly_executed_connections[index]);
            }
            if let Some(connector) = self.execute_node(instance_index, *connected_node_id) {
                if is_sequence {
                    // Inside a sequence break out if the connector is not Success
                    if connector == BehaviorNodeConnector::Fail || connector == BehaviorNodeConnector::Right {
                        break;
                    }
                }
            }
        }
        rc
    }
fn execute_node ( & mut self , instance_index : usize , node_id : usize ) -> Option < BehaviorNodeConnector > { let mut connectors : Vec < BehaviorNodeConnector > = vec ! [ ] ; let mut connected_node_ids : Vec < usize > = vec ! [ ] ; let mut possibly_executed_connections : Vec < ( BehaviorType , usize , BehaviorNodeConnector ) > = vec ! [ ] ; let mut is_sequence = false ; let mut rc : Option < BehaviorNodeConnector > = None ; // Call the node and get the resulting BehaviorNodeConnector if let Some ( behavior ) = self . behaviors . get_mut ( & self . instances [ instance_index ] . behavior_id ) { if let Some ( node ) = behavior . data . nodes . get_mut ( & node_id ) { // Handle special nodes if node . behavior_type == BehaviorNodeType :: BehaviorTree || node . behavior_type == BehaviorNodeType :: Linear { connectors . push ( BehaviorNodeConnector :: Bottom1 ) ; connectors . push ( BehaviorNodeConnector :: Bottom2 ) ; connectors . push ( BehaviorNodeConnector :: Bottom ) ; connectors . push ( BehaviorNodeConnector :: Bottom3 ) ; connectors . push ( BehaviorNodeConnector :: Bottom4 ) ; } else if node . behavior_type == BehaviorNodeType :: Sequence { connectors . push ( BehaviorNodeConnector :: Bottom1 ) ; connectors . push ( BehaviorNodeConnector :: Bottom2 ) ; connectors . push ( BehaviorNodeConnector :: Bottom ) ; connectors . push ( BehaviorNodeConnector :: Bottom3 ) ; connectors . push ( BehaviorNodeConnector :: Bottom4 ) ; is_sequence = true ; } else { if let Some ( node_call ) = self . nodes . get_mut ( & node . behavior_type ) { let behavior_id = self . instances [ instance_index ] . behavior_id . clone ( ) ; let connector = node_call ( instance_index , ( behavior_id , node_id ) , self , BehaviorType :: Behaviors ) ; rc = Some ( connector ) ; connectors . push ( connector ) ; } else { connectors . push ( BehaviorNodeConnector :: Bottom ) ; } } } } // Search the connections to check if we can find an ongoing node connection for connector in connectors { if let Some ( behavior ) = self . behaviors . get_mut ( & self . instances [ instance_index ] . behavior_id ) { for c in & behavior . data . connections { if c . 0 == node_id && c . 1 == connector { connected_node_ids . push ( c . 2 ) ; if is_sequence == false { self . executed_connections . push ( ( BehaviorType :: Behaviors , c . 0 , c . 1 ) ) ; } else { possibly_executed_connections . push ( ( BehaviorType :: Behaviors , c . 0 , c . 1 ) ) ; } } } } } // And if yes execute it for ( index , connected_node_id ) in connected_node_ids . iter ( ) . enumerate ( ) { // If this is a sequence, mark this connection as executed if is_sequence { self . executed_connections . push ( possibly_executed_connections [ index ] ) ; } if let Some ( connector ) = self . execute_node ( instance_index , * connected_node_id ) { if is_sequence { // Inside a sequence break out if the connector is not Success if connector == BehaviorNodeConnector :: Fail || connector == BehaviorNodeConnector :: Right { break ; } } } } rc }
Executes the given node and follows the connection chain
Executes the given node and follows the connection chain
&mut self
instance_index
usize
node_id
usize
&mut self
instance_index
usize
node_id
usize