Suggested Ideas Behind Implementing Animals with AI in Terasology Part 1

J Young Kim

New Member
In many of my posts, I've written about potential behaviors/AI's to be exhibited by various animals. For instance, I explained the AI of a friendly deer ( For those who are interested in actually creating the AI, I have a general outline of how it should be done.


We first must consider spawning and despawning mechanisms of the animal. If we don't, we will find that the game will not have enough memory to support animals. Thus, to be simple, we can state that the animals will spawn and despawn X blocks away (out of sight distance). However, it'll be unrealistic to have cows spawn in, then once the player leaves and re-enters within X blocks, pigs spawn in. With this in mind, I believe that each 'chunk' of land (X meters x Y meters x Z meters) should have an array which stores which animals were on it when the animals despawn.

This idea becomes important for animals which have 'traversing' behaviors. Essentially, traversing is when the animal embarks on large journeys. In my write-ups, some examples of animals that exhibit 'traversing' would be sharks and shy monsters in the dark. To simulate the traversing behavior without actually having the game have a sprite (which is not visible to the player) wandering mindlessly, we can have traversing animals to be transferred over chunks of land via the arrays. In other words, if a player saw a shy monster at chunk #1, and the player leaves and goes to chunk #3, the same shy monster can spawn at chunk #3 as a result of the monster 'going through' chunk #1 and #2 to end up at 3 by transferring the 'Shy monster' object from the chunk #1 array to the chunk #3 array.


There are animals which rely on predictions to execute a certain action. For instance, when a shark does an attack from below (see for more info), we have to assume that the prey is not sitting tight. The prey will be moving. For a simple prediction AI, we can use Calculus, specifically, derivatives.

For our purposes, we will define derivatives to be the speed at which the an object is going at a certain time. For instance, when a ball is kicked, the speed of the ball after 3 seconds would be the value of the derivative at time = 3 seconds. To calculate derivative, we will use the equation (distance traveled / time). To make it more clear as to what I'm talking about, let me give an example of how sharks can calculate the derivative).

When a shark is ready to attack from below, we can have the shark track the prey. The shark will have X seconds as preparation for the attack. During these X seconds, the shark will calculate the displacement of the prey (i.e. the prey traveled 5 blocks during X seconds). Then, the derivative will be (5 blocks / X seconds).

We can use derivatives because we can assume that the prey will move in a straight line most of the time, and the prey will move with relatively constant speed. Even the players would not be moving 'North, East, North, East, ...', and would more likely have moving patterns of 'North, North, North,...'. Thus, if we find the derivative of the prey, we can 'predict' where the prey will be when the sharks attack. For instance, if we found that the derivative was 3 blocks per second, and the shark attack takes 2 seconds, the shark will attack 6 blocks ahead in the direction the prey was going (because 3 blocks/second * 2 seconds = 6 blocks).

This, without any sort of AI, is a naive, simple, and practical method of movement predictions.

Needs-Based AI

In all of the write-ups, I mention the Needs-Based AI as the optimal method of coding AI into an animal. I will show one method that I believe to be a good solution for this system of AI.

For this purposes, we will take the friendly deer as the animal we are coding an AI for.

First, we must define our variables of needs, and set them initially to a maximum value when spawned

1. thirstLevel: 100
2. hungerLevel: 100
3. energyLevel: 100

We also must create boolean variables and set them to False for managing the behaviors of the deer.

4. Fear = False
5. Decreased_Sense = False
6. BadWeather = False
7. isThirsty = False
8. isHungry = False
9. isTired = False
10. isEating = False
11. isDrinking = False
12. isSleeping = False

Second, we must write a loop which will constantly update these variables:

while (deer is in game)
if(all boolean variables are False)
subtract thirstLevel
subtract hungerLevel
subtract energyLevel
exhibit IdleBehavior​
if (swimming or running) *The subtraction values under this if-statement should be large than the idle*
subtract thirstLevel
subtract hungerLevel
subtract energyLevel​
if (thirstLevel < 40)
isThirsty = True​
isThirsty = False​
if (hungerLevel < 40)
isHungry = True​
isHungry = False​
if (energyLevel < 40)
isTired = True​
isTired = False​
*Note that I've did if/else if/else because hunger has precedence over thirst and being tired
if (isHungry)
set Pathfinding to look for food​
else if (isThirsty)
set Pathfinding to look for something to drink​
else if (isTired)
set Pathfinding to look for somewhere to sleep​
*Instance of a need 'decreasing'
if (animal near food source and isHungry)
start eating animation
increase hungerLevel​
*Rest is manipulating the boolean variables to exhibit behaviors, for instance:
if (raining or snowing or storm)
badWeather = True​
badWeather = False​
if (badWeather)
set behavior to stay home

Of course, to be more effective, instead of while-loop, a set-timeout may be better for the performance of the game.
Last edited: