domingo, 17 de marzo de 2013

Villagers Finite State Machine

As mentioned in one of the first publications, we have two kinds of agents. Here is a quote for those who don't remember it:

"The technique to implement is a Multi-agent system with a Goal-directed behaviour. With this we will create the "brain" (a non-physical entity) able to handle all the game information and act logically.

We will also have physical agents inside the world like soldiers and villagers/workers. For this agents I will be using the well known technique Finite State Machines, but I will talk about them in other publication."

In this publication I'm going to talk about the Finite State Machine I have designed for the Villagers, the agents responsible for gathering resources to rise the empire.

Here is the Diagram of the FMS:


Villagers are in the IDLE state by default until someone orders them something to do. Right now there are four functions to task the Villagers, and the only thing you need is to have the correct building in a good position (for example, if you want to gather wood you will need a Lumber camp in a good spot with lots of trees):

AssignLumberCamp(GameObject lumbercamp_building);
AssignStoneCamp(GameObject stonecamp_building);
AssignGoldCamp(GameObject goldcamp_building);
AssignBarn(GameObject barn_building);

So, by calling the next line of code, Villager.AssignLumberCamp(GameObject lumbercamp), it will automatically start working and it won't stop until it can't find more trees (assuming that there is a Lumber camp placed and it's in a good position near trees) or someone tells him to stop.

Here is a brief description of the states:

Idle: It does nothing.

Search resource: Try to find the nearest resource to the given building within a certain limited radius and selects it.

Go to resource: Moves to the selected resource (uses A*).

Gather resource: Gathers resources from the selected resource (It takes 12 seconds to gather 10 resource units more or less, although it depends on the resource). It stays in this state until the max resource capacity has been reached (10 units) or until the selected resource is finished.

Deliver resource: Moves to the assigned building to deliver the resources (uses A*).

I have used the green colour in the FSM diagram to mark the "natural flow" of the villager if everything goes fine, which is:

Search resource -> Go to resource - > Gather resource-> Deliver resource -> Go to resource - > Gather resource -> Deliver resource etc.

There are 3 "special cases" that interrupts the natural flow of the FSM. They are the white arrows with text in them:

Search resource -> Idle: The Villager was unable to find any resources so it returns to the Idle state. This is because the building has been placed incorrectly or the resources around have been already gathered.

Gather resource -> Search resource: The villager finished the selected resource but hasn't reached its max. resource capacity. It searches for another resource near him before going to deliver it. So if it has 7/10 of wood, and the selected tree has been completely chopped, it will try to find another tree to reach 10/10 before travelling to deliver it.

Deliver resource -> Search resource: It can be the case that, during the travel to deliver the resources, your selected resource (a tree for example) was chopped by other villager. In that case, the villager will try to find another resource to keep working. Otherwise, it would travel to an empty resource, being a waste of time.

After hundreds and hundreds of lines of code and lots of hours invested, I am happy to show you the first video of my demo:



In this video we can see a bunch of Villagers in the map (they are white capsules for the moment) and how I assign them to gather different resources. The buildings for the resources gathering have been placed by hand.

As I have mentioned, the Villagers depend on the correct placement of the building to gather resources efficiently.  Who is the responsible of placing the buildings? The Building Agent, which is the first non-physical agent that I am going to implement in the next days.

The Building agent will requite a lot of work, because it needs to analyse the terrain and find the best spots for each resource, and each building has its very own requirements.

No hay comentarios:

Publicar un comentario