Simon's GM website > Games
> Bacteria
> Technical version
Technical story
Bacteria 2
Within this article I will try to explain how the AI of Bacteria
works. This article will not describe how to write the GML for such
an AI but rather the technique used to make such an idea. I am writing
this article in the hope to help other people in learning ways to
create better AI for there creations. Within this article I will
assume that you understand how Bacteria works, how terrains are
built up and how the game goes. This article will explain the normal
AI of Bacteria 2. This is also the AI of Bacteria 1. All the other
AI variants in the game are based upon this system.
First some basic info on how the game is built up. There are three
different object: point_green, point_red and point_neutral. There
is one parent object for all these objects called point. Each object
has a variable called image_index which holds a variable which direction
it is facing. 0 is right, 1 is up, 2 is left and 3 is down. These
properties define how the map is built up.
Now the goal of the AI is to calculate which move is best for red.
Therefore it checks for all point_red to see what the result is
of a move by that stone. This is done by counting up all different
points that will be taken when the stone is being rotated. So we
need a procedure that will check to each point one by one. This
procedure is in the form of a script which I will call move_check.
Within this script there is a code to see if there are any stones
surrounding the current stone that are part of this area. So the
stone within the direction this stone is facing is looked at as
well as the surrounding stones which are pointing towards this stone.
As one stone points to the other but the other stone also points
back you will get an infinitive loop going trough all stones. Therefore
there is a variable called done which will be set to true once a
stone is counted. This prevents a stone from being counted double.
This gives a loop which will return an amount of stones that is
being taken when rotating this stone. This system is also built
up that a gray stone is far less important to a green stone. (1
to 5 points) So a move taking in green stones (the enemy of red)
will be far more likely then a stone taking in green stones. Taking
in red stones will obviously have a rating of 0 as such moves have
as only purpose to make the area larger which will make red more
vulnerable to attacks.
Now there is a problem. What if there isn’t a single move
possible which will take in some stones. There is for instance a
25% that this will happen at the start of the game. In that case
this AI will go through all the steps above again but this time
use as reference to check when a stone is rotated 2 times. If that
fails he will check again for 3 times of course.
And with such a relatively simple system it will find out what move
is best to make.
The hard AI
The hard AI has been started from the normal AI. I have taken that
AI and added 2 additional features. The first feature is that it
will calculate the counter ratio for this move. This means that
it will calculate what the effect is when you take back the area.
If the AI then looses half of his terrain then he is a lot less
willing to make the move then when he looses just one stone. The
second feature is that he will always check for the result of rotating
a stone multiple times. However a move that will require a stone
to be rotated 3 times will of course happen far less likely then
a direct attack. However when the AI is stuck it does not keep taking
in very small gray blocks but it will actually attack the player.
The countercheck happens quite similar to the move check. It has
a script and passes through each stone that is pointing to it and
goes on to count all stones. The higher the number of stones you
will loose the smaller the change that the AI is going to try it.
The multiple rotation system works just like the rotation system
within the original system. The only difference is that this time
automatically all options are counted. This means the procedure
is run 3 times and the results are decreased by a certain factor
depending on the number of rotations it takes.
I hope that this article has given you some idea on how to make
an effective AI within Game Maker or at least gave you some ideas
on how to defeat the AI.
Note that each game requires a very specific AI and that you can
not simply port this AI over to your game and hope it works. Also
note that the programming of this AI within GML will give some small
issues which I have not discussed which will make this AI quite
a bit more complicated. This story is mainly intended to give you
an idea on how the basics of the AI works.
If you have any questions after reading this article feel free
to contact me about it. Note that I
will not make an AI for your game and that I will
not give you the source of the AI so you do not have to ask that.
But any sensible comments after reading this article are always
welcome.
Simon Donkers.
If you wish to post this story at any location, please contact
me. I will probably say yes but I would like to know in advance.
You are ofcource free to link to this page.
|