Welcome back.
Let's get stuck right in and continue coding with our build graph method, which is a little bit more
complex than this because we have to build that multi branched structure of all of the nodes.
It's not just one great big long line and for that we're going to use.
So I hope you're all up to date on your use of recursion because it's a complicated thing to get straight
in your mind, but it's a very, very powerful programming concept.
And basically it's when a method calls itself.
And so you end up with all these versions of the method running on top of each other in memory.
And at some point you have to sort of dig your way back out of them.
But I'm going to show you how to do this, so don't panic.
So we're going to create a private bool build graph and it's going to take our parent node.
So node parent list of.
And leaves.
A list of g actions which are usable actions.
And our dictionary with our goal in a dictionary which is a remember it's a string int and that will
have goal.
Okay.
So this is Bill Groff.
So the first thing we're going to do is bool found path.
Equals false because at this point we haven't found a path and we need to keep track of whether we have
or not.
Then we're going to do a four each through each of our actions.
So for each g action, action in usable actions we're passing through.
And I think I've spelt that wrong, but I'm going to keep the wrong spelling in there.
And now if action is achievable, given the parent state.
Then this action is definitely one that we're looking for.
So at this point, we'll go dictionary.
String int of the current state equals a new dictionary of string int, but it is being copied from
the parent state.
So the first time we come in here, the list of states, remember they come from the very first node
and that first node is the starting node and it has a list of all of the world states in it.
So the first time we come through into this current state is going to be filled up with the states of
the world.
Now the idea with current state is as we go through all of our actions and build up our branch, is
that we keep track of everything that's being satisfied and all the conditions that are changing as
we're going through.
So it's kind of like doing a sort of pre test run through the branch and because this current state
has all of the states that are going to be achieved before you get to that action, it helps to match
them as we're going along.
Okay.
So we've got this is achievable parent state, we've got our current and now we're going to create a
for each.
Key value pair, which will.
Match up string and int.
So the key values of the effects in action effect.
And then if not current state contains key in the effects dot key.
Current state.
Don't add.
A fixed key and a fix dot value.
So now current state is going to have not only the state of the world, but we're also adding to it
the effects of the current action.
And then as we move on, the effects from the next action will be added and the next.
So it is keeping that really long list.
All right.
So we're going to finish our for each loop there.
Then we're going to go node and node equals new node.
So we now create the next node along and its parent and the parent cost plus the action cost.
So the cost value here, you can see that we're getting each cost of the individual actions.
But as we move through all of our nodes, it accumulates and that's how we can do that.
Get cheapest node up the top in the code here and know that we've found the plan by just looking at
the very last node on the end.
That is the cheapest.
Okay.
And we're also going to set the current state here.
So we're giving it to our new node or passing along this accumulation of states and then also the action.
And then if.
Goal achieved, which we're yet to write, where we're going to match up the goal that we're looking
for with the current state.
So somewhere in the current state, if we have achieved our goal, then we are in fact finished.
So leaves add our node.
And then found path equals true.
Now else we haven't found a path.
So what we do is we go to the next node along.
So we've created our next node along already here.
But now what we're going to do is to create a subset of the usable actions to take out the current action
that we've already got in our node.
So first of all, list G action subset.
A class action subset, another method that we're yet to write usable actions.
Action.
And this is going to take out of our usable actions the action that we've already created a node in
our graph for, so that as we go along the branches, this list of usable actions becomes smaller and
smaller, which means that you can't create a circular path anywhere.
So you're not going to end up with some kind of endless circle that you're searching through for creating
a plan.
Okay, so ball found and that's going to equal build graph.
And here you can see our recursive call where we now send through the next node.
We still give it our leaves, but instead of usable actions, all of them we're just sending through
our subset and again, we're sending through the goal.
So as bill graph keeps getting called over and over again, we're sending it the next node and then
the next node, the next node that have been attached.
And this usable action set that's getting smaller and smaller and smaller into subset.
And at some point if found.
Happens to be true from this then found path equals true.
And that then finishes out else.
And we can finish our if our four h at the top.
And before we finish our method we can return found.
Path back.
To our planner.
Okay, so that's our recursive function.
That's doing all of the work for building up our graph.
Right.
The last thing we need to do in this code file and I promise, well, it's not the last thing.
It's the last thing you get to do for now, before we can kind of get started on creating something
is to declare these two methods for you want goal achieved and action subset, right?
So we're going to put that underneath here.
So make sure that you're still inside your class but outside of the previous method.
So this is going to be our private bool goal achieved and it's going to use the dictionary again.
Surprise, surprise int.
Go and then dictionary.
INT State.
And in here for each key value pair which will be again our string and int.
A G in goal.
So looping through all of our goals and making sure that they exist in the effects.
So if.
Not State contains key.
G dot key.
Then we will return false.
Otherwise, if we make it all the way through looping and checking, then we're returning true that
we're able to satisfy all of the goals.
So that's a goal achieved straight forward as far as checking.
And then we need our action subset up here, which gets rid of the current action that we've just assigned
to a node out of the usable actions.
So this again will be a private.
And list of.
G.
Action Action Subset List.
G Action.
Actions and action.
Remove me.
So that'll be the action that we're passing through.
We're looking for it as we loop through, and then we're just going to get rid of it out of this particular
list case.
So it's going to be list.
G action.
Subset.
Equals new list.
G action.
And then for each g action a in actions we've passed through.
If we find the one that we're going to remove, so if or if we don't find it should say if not action.
Dot equals remove me.
So the one that we've got to just leave out, then we kind of are just ignoring it.
Otherwise subset add a and we'll put the action in to there.
Then after we've done our loop, we will just return our subset and that basically is removing that
action out of the usable actions.
Okay, so you'll be relieved to know we've finished with our G planner for now.
Right next we can actually start making some actions and start pressing that play button in unity to
see things working and how they work and then start building on it from there.