Introduction
After the previous tutorial, our AI driver can automatically control the car to stay in lane, limit its speed, stop at the stop sign, and turn right at a crossing.
In this tutorial, we will extend it further so that it can follow a list of driving directions to turn left/right or go straight.

Step 1 - Remix the Starting Project
Please remix the following project:
play.creaticode.com/projects/67ad8b0919667583370d2d81
Note that the AI sprite contains the same logic from the previous tutorial, but the blocks have been divided into a few custom blocks to make them more readable:

Step 2 - The “driving directions” list
For a self-driving car in the real world, it will typically receive a destination from the passenger first, and it will calculate a driving direction based on that.
We won’t get into how to calculate the driving direction in this tutorial. Instead, we will assume the directions have already been calculated, and we will represent them using a list named “driving directions”.
Each item in this list will be a letter, which represents the direction at each crossroad:
- L: turn left at the next crossroad;
- R: turn right at the next crossroad;
- S: go straight at the next crossroad;
For example, suppose the driving directions list contains these 3 letters:

That means the car is expected to turn left at the next crossroad, go straight at the crossroad after that, turn right at the third crossroad, and then stop there.
Step 3 - Read the next direction
To keep our AI code simpler, we would only focus on the first direction from the list. After it is completed, we move on to the next direction on the list.
We can use a new variable “next direction index”, which represents the index number of the next direction we have to follow. We will need to initialize this variable to 1 at the start:

We will use another new variable “next direction” to represent the actual direction. We will initialize it at the start as well, and we will update it after this direction has been fulfilled:

Step 4 - Check for the lane on the left
In the “normal” driving mode, we need to prepare for the next turn. For example, suppose the next direction is “L” for a left turn. If there are 2 lanes and our car is on the right lane, we need to switch to the left lane first, since we can not make a left turn from the right lane.
To find out if there is another lane to our car’s left, we can use the “marker info” table, and look for the marker named “left marker”. If it exists, that means there is another lane to the left going in the same direction.
Let’s define a new custom block named “update marker”, and run it at the beginning of the normal mode handler:

In that block’s definition, whenever the next direction is “L”, we will get the index of the “left marker” from the “marker info” table:

Step 5 - Temporarily change the lane marker
Suppose we have found another lane to our left, how do we ask the car to switch to that lane? The solution is very simple: we just need to aim at the lane marker from that lane, until the car enters that lane. After that, assuming there is no other lane on the left, our car will resume the old logic of following the lane marker in this new lane it has just entered.
In code, we check if the index of the “left marker” is greater than 0, and if so, we would temporarily set the “marker angle” to be the angle relative to the marker in the left lane, not the current lane:

That is all we need to make the car switch to the left lane. You can see that the “left marker index” is 2 when there is another lane on the left, and it changes to -1 right after the car enters the new lane. After that, the car reverts back to the normal mode of aligning to the lane marker in the lane.

Step 6 - A new driving mode of “turnleft”
To make the car turn left, we need a new mode of “turnleft”, similar to the “turnright” mode.
Currently, when the AI is in “stopsign” mode, it always switch to the “turnright” mode after the car has fully stopped. We need to change that to determine which mode to use based on the next direction. The new “target dir” will also be different when the car needs to turn left: it will be the current direction of the car minus 90 degrees rather than plus 90 degrees.

Step 7 - Handle the “turnleft” mode
We will define a new custom block to handle the logic of turning left at a crossroad, similar to the “handle turn right” block.


To make the car turn left, we can reuse the same code for turning right, but change the engine force and steering angle. Can you give it a try?
[please try it yourself before looking at the next step…]
Step 8 - The logic for turning left
You should see that turning left involves a larger turn with a longer radius. There are many ways to set the engine force and the steering angle, and one example solution is the following, where the engine force is 10 and the steering angle is -9.5:

This is what we will get now:

Step 9 - Move on to the next driving direction
After the turn is completed, when we switch back to the “normal” mode, we should also update the “next direction” to the next item in the list:

Note that this needs to be done in both the “handle turn left” and “handle turn right” blocks
Additional Challenges
At this point, our AI can drive the car to switch to the left lane and also follow the driving directions to turn left or right. There are a few missing pieces, so please use them as practice:
-
Handle “go straight”: If the driving direction is “S”, the car should go straight across the crossroad. Please add logic to handle this case. For a hint, you can use the car’s distance from the starting point of the lane ahead (the “forwardxy” column of the lane marker) as a threshold: if the car is close enough to this point, you can switch back to normal mode.
-
Switch to the right lane: when the next direction is “R”, and our car is still on the left lane out of 2 lanes, it should switch to the right lane.
-
Stop at the end: when all the driving directions on the list have been fulfilled, the “next direction” becomes empty, then the car should switch to the right lane if it is not in that lane and then stop.
You can test your AI with a list of directions like this: L, S, R, R, S, R, R, S