Building a better RTS AI. Making AI player tanks chose and deploy on high ground

posted in DreamLand editor
Published October 10, 2024
Advertisement

Making AI player tanks chose high ground when defending base.

Find a path with the start point the enemy base and the end your base. This will be the attack path of the enemy. For every node in the attack path of the enemy run the pathfinding algorithm to nearby tiles. Lets call the resulting paths Test paths. Nearby tiles means all the tiles that fall in the range of a tank in deploy mode. For each test path compute the direct distance between path start tile and path end tile. Then make a comparison between the length of the test path and the length of the direct distance. If the test path is longer then direct distance you found a tile that is difficult to reach. You need to go around to get there hence it's a good location to place tanks. What you found could be high ground or some kind of other difficult spot to reach (i.e. there could be a compact cluster of buildings in between, a row of crystal tiles etc. )

The problem is you might find areas difficult to reach far away from your base. If you place tanks there the enemy will just avoid the tanks and find another path to your base. To solve this problem there is a solution. In a game like Starcraft often there is only one entrance in the base (a ramp or just a map bottleneck). You can find out if there is only one entrance by trancing two or three paths to the base(lets call them scout paths) from different locations on the map. One location could be the enemy base the other locations could be places on the opposite side of the map. If all scout paths begin to overlap somewhere you found a unique entrance in your base. You can deploy tanks around in that location.

Making AI player tanks chose high ground when attacking the enemy base/expansions

It's pretty much the same strategy. If you know the place where the enemy is you can trace paths all around that place and find a location difficult to reach.

0 likes 3 comments

Comments

JoeJ

In a game like Starcraft often there is only one entrance in the base (a ramp or just a map bottleneck). You can find out if there is only one entrance by trancing two or three paths to the base(lets call them scout paths) from different locations on the map. One location could be the enemy base the other locations could be places on the opposite side of the map.

No bad idea, but ther are the usual problems of such stochastic approach utilizing randomness. E.g., if all your random locations on the map are on the left of the base, than you might miss the entrance on the right.
Randomness includes bad luck, and to avoid it, you need more ‘samples’, or locations in your case. It also matters that the samples are well distributed. E.g. if you generate them using a 4x4 grid over the whole map, chances you would miss the right entrance are smaller than using 16 of random points over the map.

However, i think there is a better way: Instead tracing paths from random points to the base, you can use Dijkstra to grow paths outwards from the base. No goal is needed, just grow the paths until they have a distance large enough to ensure we're outside. Would look like this:

All the possible paths from the base form a tree. The green start point in the base is the root.

We get many red end points, and we know the shrotest path to each of them.

So we also know the shortest path from each red point to the base, obviously. Which is what you're interested in.

Now we can walk the path from each red do to the base, and we could increase a counter for each path segment we visit. Would look like this maybe:


The less entrances you have, the higher the numbers near them.
So maybe you could use that number to identify entries, or more generaral: Locations worth to defend, and paths more likely to be visited than others.

Comparing this with your idea to use random points of the whole map, using Dijsktra your entire map would be filled with paths, so you do much more work. And maybe even better: You do not need to come up with good random points at all. And maybe you do not need to classify an ‘entrance’ either. Maybe the number is all you need to get expected behavior.

(Maybe the number can be computed already while doing the path finding, and there is no need to traverse the paths.)

October 11, 2024 10:58 AM
Calin

You could use Dijkstra too, I haven't thought of that.

After looking at your pictures I've realized two or three paths in my approach might not be enough. What you can do is run the operation once, block the bottleneck area where paths begin to overlap then run the pathfinder a second time. If no path is found in the second attempt the blocked bottleneck is the only entrance in the base.

October 13, 2024 10:28 AM
JoeJ

If no path is found in the second attempt the blocked bottleneck is the only entrance in the base.

Non quite, it's only that the probability of an existing second entry has been reduced from doing one more attempt.

But for a watertight solution, we could do this:
After one entry is found, block it temporarily, then do a new search (i would do so still from the base outwards).
We repeat this until no more entry can be found, then we can be sure to have them all.

But ofc. we can only guarantee correctness if we can robustly classify what's inside the base and what's outside, which is just another variant of that same hard problem. (There is a similar problem in topology, which is finding smallest loops identifying holes or handles. That's a NP hard or complete problem. Greedy algorithms exist but they are very slow.)

However, for AI it's probably good enough to have a faster solution with a good probability to be right. It does not need to be perfect, i guess.

October 13, 2024 11:22 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement