More AI and Fixing of Logical Flaws

This weeks blog post will be about fixing logical flaws which existed in the code. The flaws in this case was to calculate distances and directions.   The main problem is the AI for the enemies, the AI has been a occurring problem through out most of my blog posts.

Right now the problem is priority and overall behavior of the enemies. Each enemy currently checks the distance from each tower and the player. Though if a enemy is close to multiple different towers and the player strange things start to happen. Also if the player gets the attention of a enemy and it starts to follow the player, and then if the player is 600 pixels away they get stuck in the position because they are set to follow the player, but the player position returns nothing. This will probably be rather easy to fix since only a couple of additional checks will be needed.

Since each enemy checks the position of all towers and calculates length each frame it is starting to become recourse heavy for the computer. With around 100 towers and 200 enemies on screen it is starting to slow down by a bit. This should be fixable by either reducing the amount of monsters spawned and make them stronger, or optimize the code better.

The following thing i will fix is for the enemies to attack the player correctly, and set a maximum range of which the enemies travel away from the movement nodes, So that the player cannot lure them out into a corner of the map.

It will be fun to compare the AI that i have done now to the one that we will create in the AI course, i will probably see a lot of logical flaws and choices which makes the code more resource heavy then it should be.

DistanceChecking

Each tower checks the distance between it and each enemy. I finally fixed it so that the ranged towers projectiles direction is correct to the enemies position. This was a lot easier then i imagined to fix, and only took approximately 30 minutes.

So now when a projectile is created, it gets a enemy’s position which is in range of the tower and calculates the direction by taking its destination position vector – its own position. Then the only thing that remains is to normalize the new vector to extract the direction, and use the move functionality that exists for sf::sprites with the direction times aSpeed.

About Gabriel Ajuwa

2014  Programming