Okay.
Let's get back into this Astra algorithm and finish it off.
So we're still in begin search and we've created our starting and ending locations.
So under here, what we're going to do is clear out our open and close lists.
So open, dot clear and closed, not clear where they're going to add the starting position to the open
list.
So open add start node.
And then finally, if you remember, we created that last pause, we're going to set that to equal the
start node at this point.
Okay.
So we're ready to start searching under here.
Let's create another method for searching.
And this is really where you'll see everything we've been talking about so far come together.
So void search and we'll pass through the path marker of this node.
Okay.
The first thing we're going to do is to test if we've hit the goal.
So this is the sort of ending condition for everything.
So if this node dot equals so there's one about equals coming into play goal node, then we're going
to set dun equals true and just return.
Okay.
And basically the goal has been found.
Right.
Next, we're going to loop through and find all of the neighbors.
Okay.
Now, this is very similar to what we were doing in all of our maze algorithms.
Okay.
How did we get the neighbor of the current position?
Well, let's just go back into Maze.
We've already got that construct defined for us right here.
Okay, so this is getting our neighbors for the map.
We can also use it for our a star because we only want the horizontal and the vertical ones.
However, if you want to be able to search on diagonals, there is no reason you can't put the diagonal
coordinates in here.
And the ASR algorithm we're about to write will work exactly the same.
Okay, so let's start by looping through those map directions.
So for each map location, dear in maze dot directions, let me scroll this up for you.
We're going to find the neighbor.
So map location neighbor is going to equal the direction.
Plus this node dot location.
Right now, before we can actually process this particular neighbor, it needs to meet a few conditions.
The first one is that it shouldn't be a wall.
If it's a wall, then we're just going to not process it and go to the next neighbor.
So as a challenge, what I'd like you to do is write this if statement to check if the neighbor is a
wall and if it is a wall, then to continue.
So pause the video.
Now, when we come back, I'll type that up for you.
Okay.
How did you go?
Well, it's pretty straightforward.
If maize dot map at the neighbor dot x and neighbor dot z equals one, it's a wall and we will continue.
So continues just going to allow us to go to the next map direction which will take us to the next neighbor.
Now the next if statement that we're going to have to consider is if the neighbor is outside the realm
of our maze.
Okay.
So another challenge for you, right?
This if statement to check if that neighbor is outside of where you want it to be, then also continue
pause video.
Now type that in when you come back, I'll shall continue.
Okay.
How did you go with that line?
Well, it's if neighbor dot X is less than one or neighbor dot X is greater than or equal to May's dot
width or neighbor dot z is less than one or neighbor dot z is greater than or equal to May's dot depth.
And if that's the case, then we're going to continue.
Now, one final condition.
If the neighbor is already in the closed list, then we're also going to continue because we don't want
to process it, but we haven't yet put anything in any closed lists, even though we do have a close
list.
So let's instead create our own method that checks if a neighbor is in that list.
So just under the search, I'll put it down here.
So let's put a ball is closed and then we'll pass through actually the map location marker and then.
For each.
Path.
Marker p in.
We're going to test if p dot location equals marker.
Then we're going to return.
True.
Otherwise, we're going to return.
False.
Okay.
Now, it seems a little bit long winded the way that we're passing through a marker and then we're actually
looping through a closed loop which has path markers in it, and then actually testing against a marker
and the location of a path, because this is the way we have to do it at this point.
Up here, neighbor is just a map location.
It's just a position on the map.
We haven't given it a path marker.
So it hasn't got like an instantiation of one of those path game objects that we've passed through above
yet.
So that's why we have these tests on a map location against whether there's a path marker with that
location.
Okay.
So down in here we can put another if statement in if is closed neighbor, then we're just going to
continue.
Next, we're going to calculate our G, our H and our F value.
So float g equals it's going to be the vector two dot distance from this node dot location, dot to
vector and neighbor, dot to vector, dot to vector.
So that's finding your Pythagoras.
Now we've found the distance between the current node K and its neighbor.
So this node here is basically the one that's going to be next to the neighbor.
But you need to find the distance that has been travelled the entire way up to that node and then beyond.
So you have to do that and then also add in this node and it's g value which is going to allow you to
move one from your current node, plus add up all the path distance that you've accumulated along the
journey.
Right?
And then we go to float.
H Now this is the distance between where that neighbor is and the final goal.
So again, we're going to use a vector to distance and we're finding the distance this time between
the neighbor dot to vector to.
And the goal node.
Goal node.
Location to vector.
Back to vector, not vector too.
And that's all you need to do for your h value and then float.
F Now you should know what this is because it's probably the easiest calculation in the whole a star
and that is g plus h.
So basically it's just a summary of our path distances or the costs involved in travelling it.
Okay, so now we've got that done.
What we can do is create a game object to put on our path for this particular spot.
So this neighbor is going to get a open block basically.
So game object, path block equals instantiate and we want that path P that we passed through before
and new vector three.
This is the position in the actual game world where we will put that so it'll be our neighbor dot x
and don't forget to multiply by our map dot scale back here which is in maze dot scale and then at zero
in the Y and then neighbor dot z multiplied by maze dot scale.
And then we want to just put Catalonian dot identity at the end of that.
Now the path block itself or I should say the path this game object, if you have a look at it, let's
go back into unity this prefab for path.
If you open that up as a prefab and have a look at it, you'll see that there's a G and H and an F text
mesh attached to it.
And that's how I'm displaying the values of G, H and F.
Of course, when you're using this in production to do a star, you don't have to worry about all little
sort of interface and fancy stuff, but I want you to be able to see it as you step through.
Okay?
So that means our code needs to set those values and we'll come back in the next video and continue
working on this code.