This is the commentary post for this code sample.
As mentioned in the Postmortem, the challenge of creating enemy AI was not tackled until fairly late in the project. So when it came time to attack the problem, the task seemed rather daunting.
Simply navigating the city was a big enough challenge for the AI, but they also had to pose a threat to the player and their giant doom tank.
I sought out the advice of the FIEA faculty, and the idea for this sort of approached was raised. I would return later to help improve my skills with vector math, and the solution would become vastly more efficient.
The purpose of this example function is to update a series of priority weights corresponding to the radial directions surrounding an AI Vehicle. Provided are the direction of its' goal (usually a path node or the player vehicle) and the distance to that goal.
The radial directions around the vehicle are given a favorable weight based on how little they deviate from the direction to the goal, and then unfavorable weights are added in based on the positions of obstacles, other vehicles, and the player vehicle. The algorithm then weights these factors to decide on the 'best' path to travel down.
The AI Tanks will simply turn in place until they face their ideal direction and then press onward towards it. However, AI Cars perform a simple simulation of driving physics in which they cannot turn unless they are moving either forward or backwards. As a result, a 'mBackup' flag is set when they are in imminent danger of crashing in to an obstacle. Their tolerance for nearby threats also drops when backing up, which causes them to get a wider window of safety before they start moving forward again.
A feature I had actually forgotten about until I looked at this file was a somewhat humorous attempt to prevent vehicles from falling into a deadlocked state of constantly waiting for other cars to get out of their way. Vehicles could actually become frustrated with waiting and begin driving recklessly, without heed for safety. In particular, civilian vehicles will panic and begin to do this if the player's vehicle gets too close. This actually causes one to ram into a Squad Car in this video.
The player vehicle is an interesting case as it can also be the AI Vehicle's goal. Vehicles will drive towards the player until they get within firing range. However, since ramming in to the player's vehicle is fatal to them, they also try to avoid it once within a certain radius. It's also worth noting that the 'mSuicidal' variable does exactly what it says on the tin. It is flagged for enemies who actually benefit from kamikaze tactics.
This function and this class as a whole was a major challenge, but it was also a great deal of fun. It was very exciting when the cars finally began to avoid one another and successfully navigate the streets of our New Detroit. However, making them provide a fun challenge the player would require many more hours of work . . .