Hi, welcome to this new video. We are going to continue working on our ecosystem project. We are writing the behavior, the internal brain of a herbivore class, which is an animal that eats our food class. We're finally at the point where our animal, this herbivore has reached, can move towards the food when it's hungry, and it gets there. There's interaction here that we basically transition a behavior change. The consumption of the food triggers a change in the decision tree. What actually happens is that when we're seeking food, we reach that food and eating that food not only kills that entry of the food supply and actually takes us back into the evaluation of the decision tree. What we're going to try to implement is a sequence in which the herbivore, once it eats, gets sleepy and it goes and takes a nap. We're going to transition the rest state. The rest state will have a certain amount of frames that it's going to be resting. Of course, if you had some very expressive designs for these different states, you can actually add sprites or images, wander states maybe, like a movement, action, and then resting state is sleeping action. But that's what we're going to do, we're going to make awake false. It's going to go to rest and hungry false, so you're not hungry anymore. When you wake up, you're going to be full of energy to start over and maybe going to wander and eventually get tired, going to seeking food, and going back to awake. With that in mind, let's start writing the eat food in range function, which is what we're going to do today. The first thing I wanted to do, and I think I mentioned that at the end of last video, is looking at our move function. It's fine. I'm in the herbivore class just for reference. I'm continuing with the work we've been doing so far, the code that we've written so far in this week. If we move down, and here it is, the move function currently doesn't take any factor, any intensity. I wanted to make a difference between the movement of wander and the movement of seeking food. Let's just add here something called a factor, and we're going to say that the self.velocity. We're going to normalize, and we're going to multiply it by the factor. I know I'm overriding the diversity of movement. I'm going to just basically be dictating the velocity speed through this function now. I think that for this case, it's a little bit more cartoony, if you want. It's going to feel like wandering. It's a small, slow behavior at a certain factor, but then it creates an acceleration when seeking food. There's kind of how do you convey that expressivity of seeking food is something a bit more urgent? What we're doing is normalizing the velocity and multiplying by this factor. The challenge now is that whenever we use the function move, we will need to provide a factor. Here within one direction, we're going to make that one. We have two instances of move; one in wander which is going to be a movement of one, and one in seeking food, which I think is down here, seeking food, move 3. That's the difference we're doing at the moment. Now, let's finally write, maybe after seeking food. Here, we can say eat food. We basically return this function as well. Let's just write it together once more. Find or eat _food_ in_range. We're going to take the self, the closest _food and they eat_range. Just to give some flexibility to this function, we want to make the difference vector between the closest _food, which is going to be an object, meaning a class. We're going to ask to request the position vector of that object. Let's create a copy of that and substract the self thought position. Again, the substraction between the two points that we're evaluating, and again, distance equals diff.magnitude. If the distance is smaller than the eating range, which is going to be specified by the function the eat_ range. We could say something. This is a very small number, but it's good that we have some capacity to tune it. Think of it like if you're within five pixels. Let's just put a note here, remove the food from the list or kill the food. Then let's just say self.hunger_level equals zero. We reset the hunger. Let's make sure that we have those variables here written in the same way. Hunger_level, it's going to go down to zero. IsHungry. Hunger_level here. Hunger_level has come back to zero, isHungry is going to go back to false. Sorry. Awake is going to be false and hungry is going to be false. Awake means that it's going to go to sleep. At this point, we can do what we did in wander, which is because this is a transition, where do we have the wander_state? Here. This line is the important line, which is that it triggers the reevaluation of the decision tree, because we separated our eating function out of the others current_node. Basically repeal the decision tree means start over, start going through the top root node and evaluate with the new data that we've changed. We're not longer going to be in the seeking food state, that this is the transition action. The only thing that we're missing here is actually removing food from the list. I would like to do that by writing a function that I would call something like remove self. I think that the problem with our current food system is that they don't have any references to other food. It doesn't have a reference to self within a collection, so it's just an entity that has very little power to remove itself from the collection. Let's do an all food reference here so that all our foods are aware of who they are in a collection. Self.all _food equals all_food. Once you do that, you need to make sure that in the world, whenever you're creating food like here, all_food, we need to pass the self total food collection. New_food equals that. The constructor now, the constructor requires reference to the list. That's everything. But that means that now we could actually the food class we could write a very short function, which I'm going to call remove_self. The remove_self is going to say self.all_food.remove, self. Remove yourself from the collection. This is going to be a useful function if you want to create a state, which we've done already, something like if the H is bigger or equal than the lifespan, which I think we never completed. We wrote, so let's just use it now. If we are bigger than the lifespan, self.remove_self. Basically, the line in which you would just remove yourself from the list. Now that we have a reference to that, our grow function. This is interesting because in a way, if you have a species that dies too quickly, might not be within its prime to offer the calories or the nutrients for the herbivore to actually be consumed and potentially go out of its hunger state. The new function that we are accessing here in the food, let's just copy that. We're going to invoke it from the herbivore. Remember that here when the herbivore eats the food, instead of the herbivore removing it from the collection, let's say, it's going to call the closest_food.remove_self. That's the check that we're doing that right. Closest _food.remove_self, brilliant. Now, when we call the sick food function, we can call this eat food in range. Here, this would be self.eat_food _in_range. We have the closest_food. We have to provide that. I don't know why I spelled that like that. Food_in_range with the radius of five. I'm going to give five pixels, so a very short radius. If the closest_food is within five pixels or five units from the herbivore, and it's in the six food function, you are going to delete the food, and the herbivore will transition to be not awake. Basically, go to sleep. Let's see if we're running into any errors. We're wandering. We're seeking. No, not seeking yet. We're still wandering. We're seeking food. Basically, it went towards the food, it ate the food, and now it's resting. Our rest state doesn't have any exit. We don't have any transition, so this is where we're at. We're little by little starting to build a loop, if you want, a decision tree nested within that. There's state machine for the wandering behavior. We're finally going full circle, where we could have not only one agent, but many potentially eating the food, resting, then trying to get more food as well. We'll leave this here, and we're going to continue in the next video. I'll see you there.