pub fn create_behavior_instance(&mut self, id: usize) -> usize {

        let mut to_execute : Vec<usize> = vec![];

        let mut position : Option<(usize, isize, isize)> = None;
        let mut tile     : Option<(usize, usize, usize)> = None;

        let mut scope = Scope::new();

        // Insert Dices
        /*
        for d in (2..=20).step_by(2) {
            scope.push( format!("d{}", d), 0.0 as f64);
        }
        scope.push( "d100", 0.0 as f64);*/

        // Default values
        scope.push("Value1", 0.0_f64);
        scope.push("Value2", 0.0_f64);
        scope.push("Value3", 0.0_f64);

        if let Some(behavior) = self.behaviors.get_mut(&id) {
            for (id, node) in &behavior.data.nodes {
                if node.behavior_type == BehaviorNodeType::BehaviorTree {

                    for c in &behavior.data.connections {
                        if c.0 == *id {
                            to_execute.push(c.0);
                        }
                    }
                } else
                if node.behavior_type == BehaviorNodeType::BehaviorType {
                    if let Some(value )= node.values.get(&"position".to_string()) {
                        position = Some((value.0 as usize, value.1 as isize, value.2 as isize));
                    }
                    if let Some(value )= node.values.get(&"tile".to_string()) {
                        tile = Some((value.0 as usize, value.1 as usize, value.2 as usize));
                    }
                } else
                if node.behavior_type == BehaviorNodeType::VariableNumber {
                    if let Some(value )= node.values.get(&"value".to_string()) {
                        scope.push(node.name.clone(), value.0.clone());
                    } else {
                        scope.push(node.name.clone(), 0.0_f64);
                    }
                }
            }

            let index = self.instances.len();

            let mut instance = BehaviorInstance {id: thread_rng().gen_range(1..=u32::MAX) as usize, state: BehaviorInstanceState::Normal, name: behavior.name.clone(), behavior_id: id, tree_ids: to_execute.clone(), position, tile, target_instance_index: None, locked_tree: None, party: vec![], node_values: HashMap::new(), state_values: HashMap::new(), number_values: HashMap::new(), sleep_cycles: 0, systems_id: 0, action: None, instance_type: behavior::BehaviorInstanceType::NonPlayerCharacter};

            // Make sure id is unique
            let mut has_id_already = true;
            while has_id_already {
                has_id_already = false;
                for index in 0..self.instances.len() {
                    if self.instances[index].id == instance.id {
                        has_id_already = true;
                    }
                }

                if has_id_already {
                    instance.id += 1;
                }
            }

            self.instances.push(instance);
            self.scopes.push(scope);

            return index;
        }

        0
    }
/// Create a new behavior instance for the given id and return it's instance index
Create a new behavior instance for the given id and return it's instance index
Create a new behavior instance for the given id and return it ' s instance index
pub fn create_behavior_instance(&mut self, id: usize) -> usize {
        let mut to_execute : Vec<usize> = vec![];
        let mut position : Option<(usize, isize, isize)> = None;
        let mut tile     : Option<(usize, usize, usize)> = None;
        let mut scope = Scope::new();
        // Insert Dices
        /*
        for d in (2..=20).step_by(2) {
            scope.push( format!("d{}", d), 0.0 as f64);
        }
        scope.push( "d100", 0.0 as f64);*/
        // Default values
        scope.push("Value1", 0.0_f64);
        scope.push("Value2", 0.0_f64);
        scope.push("Value3", 0.0_f64);
        if let Some(behavior) = self.behaviors.get_mut(&id) {
            for (id, node) in &behavior.data.nodes {
                if node.behavior_type == BehaviorNodeType::BehaviorTree {
                    for c in &behavior.data.connections {
                        if c.0 == *id {
                            to_execute.push(c.0);
                        }
                    }
                } else
                if node.behavior_type == BehaviorNodeType::BehaviorType {
                    if let Some(value )= node.values.get(&"position".to_string()) {
                        position = Some((value.0 as usize, value.1 as isize, value.2 as isize));
                    }
                    if let Some(value )= node.values.get(&"tile".to_string()) {
                        tile = Some((value.0 as usize, value.1 as usize, value.2 as usize));
                    }
                } else
                if node.behavior_type == BehaviorNodeType::VariableNumber {
                    if let Some(value )= node.values.get(&"value".to_string()) {
                        scope.push(node.name.clone(), value.0.clone());
                    } else {
                        scope.push(node.name.clone(), 0.0_f64);
                    }
                }
            }
            let index = self.instances.len();
            let mut instance = BehaviorInstance {id: thread_rng().gen_range(1..=u32::MAX) as usize, state: BehaviorInstanceState::Normal, name: behavior.name.clone(), behavior_id: id, tree_ids: to_execute.clone(), position, tile, target_instance_index: None, locked_tree: None, party: vec![], node_values: HashMap::new(), state_values: HashMap::new(), number_values: HashMap::new(), sleep_cycles: 0, systems_id: 0, action: None, instance_type: behavior::BehaviorInstanceType::NonPlayerCharacter};
            // Make sure id is unique
            let mut has_id_already = true;
            while has_id_already {
                has_id_already = false;
                for index in 0..self.instances.len() {
                    if self.instances[index].id == instance.id {
                        has_id_already = true;
                    }
                }
                if has_id_already {
                    instance.id += 1;
                }
            }
            self.instances.push(instance);
            self.scopes.push(scope);
            return index;
        }
        0
    }
pub fn create_behavior_instance ( & mut self , id : usize ) -> usize { let mut to_execute : Vec < usize > = vec ! [ ] ; let mut position : Option < ( usize , isize , isize ) > = None ; let mut tile : Option < ( usize , usize , usize ) > = None ; let mut scope = Scope :: new ( ) ; // Insert Dices /*
        for d in (2..=20).step_by(2) {
            scope.push( format!("d{}", d), 0.0 as f64);
        }
        scope.push( "d100", 0.0 as f64);*/ // Default values scope . push ( " " , 0.0_f64 ) ; scope . push ( " " , 0.0_f64 ) ; scope . push ( " " , 0.0_f64 ) ; if let Some ( behavior ) = self . behaviors . get_mut ( & id ) { for ( id , node ) in & behavior . data . nodes { if node . behavior_type == BehaviorNodeType :: BehaviorTree { for c in & behavior . data . connections { if c . 0 == * id { to_execute . push ( c . 0 ) ; } } } else if node . behavior_type == BehaviorNodeType :: BehaviorType { if let Some ( value ) = node . values . get ( & " " . to_string ( ) ) { position = Some ( ( value . 0 as usize , value . 1 as isize , value . 2 as isize ) ) ; } if let Some ( value ) = node . values . get ( & " " . to_string ( ) ) { tile = Some ( ( value . 0 as usize , value . 1 as usize , value . 2 as usize ) ) ; } } else if node . behavior_type == BehaviorNodeType :: VariableNumber { if let Some ( value ) = node . values . get ( & " " . to_string ( ) ) { scope . push ( node . name . clone ( ) , value . 0 . clone ( ) ) ; } else { scope . push ( node . name . clone ( ) , 0.0_f64 ) ; } } } let index = self . instances . len ( ) ; let mut instance = BehaviorInstance { id : thread_rng ( ) . gen_range ( 1 ..= u32 :: MAX ) as usize , state : BehaviorInstanceState :: Normal , name : behavior . name . clone ( ) , behavior_id : id , tree_ids : to_execute . clone ( ) , position , tile , target_instance_index : None , locked_tree : None , party : vec ! [ ] , node_values : HashMap :: new ( ) , state_values : HashMap :: new ( ) , number_values : HashMap :: new ( ) , sleep_cycles : 0 , systems_id : 0 , action : None , instance_type : behavior :: BehaviorInstanceType :: NonPlayerCharacter } ; // Make sure id is unique let mut has_id_already = true ; while has_id_already { has_id_already = false ; for index in 0 .. self . instances . len ( ) { if self . instances [ index ] . id == instance . id { has_id_already = true ; } } if has_id_already { instance . id += 1 ; } } self . instances . push ( instance ) ; self . scopes . push ( scope ) ; return index ; } 0 }
Create a new behavior instance for the given id and return it's instance index
Create a new behavior instance for the given id and return it ' s instance index
&mut self
id
usize
&mut self
id
usize