viernes, 15 de marzo de 2013

Building Agent - Resource Buildings

As mentioned in the last post, resource buildings require not only a free spot to place the structure but also a good place so the villagers can gather resources efficiently.

Resource buildings for food, gold and stone were fairly simple. Barns could be considered almost like a generic building, as it doesn't need a specific location to perform properly, as long as there's enough room in its surroundings to place farms. Because of that, the Barn is the only Resource Building that uses the Building Agent's BuildNode system.

Lumberjack hut and minings camps are placed using a more accurate system that ensures the building is close enough to the resource.

Gold/stone mining camps were also quite easy. All the stone and gold nodes contain the same amount of resources, so in this case we only need to find the nearest node, and place our structure there.

As for the Lumberjack hut, things were a little bit more complicated than expected, and ended up in a quite complex algorithm. My first approach was obviously find for the nearest trees to the bastion, and see how many trees were around those trees, and then, repeat that search 4 or 5 times (for the 4-5 nearest trees). The tree with the higher number of surroundings trees would be the best spot to place the hut.

I quickly realized that it wasn't going to work, and I didn't even try to implement it. Lets see the next example:


Yellow spots are the ones that I consider the best to place the Lumberjack hut and red ones are the ones that the Building Agent will consider using the mentioned technique. As you can see, looking for the nearest trees to the bastion X times is not the solution, as there are a hundred ways this method could fail depending on the map and the trees distribution. Using a brute-force technique and doing 100 iterations ( X= 100) is not a solution for me, as it could work, but it would be awful in terms of optimization.

To solve this problem, I realised that a whole analysis of your surroundings should be done. I'm not going to explain the whole process here, but after several hours, I had it working.

In this image we can see the the same situation but with my new system. For debugging purposes, I have done that all the analysed trees grow x2 in size, and the best tree grows x3 in size (anyway I've coloured them so they are more visible in the image).


By the distribution of the analysed trees (red ones) you can see that it did pretty well searching equally on the Bastion surroundings for not missing any potential tree spot. The proof is that it successfully found the best spot (with 108 surrounding trees) with only 14 iterations, and placed the hut there.

So we have a system that detects the best spots for our resource buildings.

Once we have a tree or a gold/stone node selected as the best one, we need to place the building there. The thing is that it's not a trivial task, because you need to place the building  as close as possible but avoid all kind of obstacles.

What happens if the best selected tree is a little bit deep in the forest? You can't place the Lumberjack hut in the middle of the trees ignoring all the collisions. Same can happens with gold/stone nodes, they can have trees/mushrooms or other blocking elements in its surroundings. With our human eyes, we can easily solve that problem, but take in mind that our Building Agent doesn't have such an extraordinary system, so we could consider it's blind.

To solve this problem, I have designed an accurate Obstacle Avoidance system that it's used for this purpose. In this video we can see some kind of "fight" between me and the Building Agent. He is trying to place a Gold mining camp and I'm trying to block every free place he finds:

                                                             



So it seems that we have everything we need to place resource buildings around the map. Lets see what happens:


                                                               

Remember that video where I showed the Villagers gathering resources? in that video I had to place the buildings by hand. Now there's no need for that any more !

We have smart villagers that gathers resources as long as they have the appropriate building in the appropriate spot, and we have a building agent able to satisfy that need. So plenty of resources for our empire are 100% guaranteed !!

We are slowly removing the "human" interaction. In this video, I was the one giving the order to build things. Next step is that those orders are all given by the AI itself.

Now I need to do a Protocol/Communication system between the agents so they can communicate properly in that client/server relation.

Final goal is to remove all human interaction, so you can just execute the demo and enjoy the show while you realx.

No hay comentarios:

Publicar un comentario