A treatise on alien behavior
Posted: Thu May 18, 2017 2:17 pm
Because every Factorio player must create a thread where he explains to devs how to properly make alien AI.
Part I. Alien Pheromones and Hive Behavior.
The suggestions to replace alien behavior AI with a distributed pheromone system have been voiced already on this forum. This is an attempt to formalize a possible implementation of such behavior.
The basic premise is fairly simple. There are a number of alien pheromones that aliens are leaving behind them as they move around the map. There may be different pheromones, and they may have different levels in each chunk (same as pollution). Unlike pollution however, pheromones do not spread. They expire with time (unless renewed by aliens), but there is no spread of pheromones into adjacent chunks. This means that CPU requirements to maintain the pheromone map are zero: for each pheromone, a map chunk keeps track of pheromone level and the time of it's last update. If pheromone level is requested by the game, only then does the CPU access those two values, calculates actual pheromone level and updates the chunk. Otherwise, if there's no alien activity in the chunk, there is no CPU load at all.
Whenever aliens travel for whatever reason, they decide where to move by measuring pheromone levels in 3x3 chunk area, centered on their current chunk. Depending on their goals, different pheromones are checked. Eventual decision is randomized depending on pheromone levels in adjacent chunks.
Each time an alien leaves a map chunk, it drops some pheromones in the chunk it left. What type of pheromone is dropped and the amount depends on alien current activity. Aliens however never drop any pheromone at their home chunks - i.e. those chunks where alien hive is located in. Those chunks always have the maximum level of "home" pheromone (see below). The amount of pheromone each alien has is limited, and only a certain percentage is dropped every time it changes map chunks. This creates a gradient in pheromone levels.
To put it briefly, two primary characteristics of each pheromone are: how quickly it disperses once dropped (creating long-lasting or short-lasting effectors), and how quickly it's strength decreases with distance (creating long-ranged or short-ranged effectors).
There are seven different pheromone types:
1. Home pheromone is used by aliens to find the location of their hive. It's quite long-lasting and decreases slowly with distance.
2. Food pheromone is used to trace the locations of points of interests for the aliens that are not player-related (food sources probably, but there may be other targets implemented, each with it's own pheromone type). It's short-lasting (and thus needs to be refreshed frequently) and decreases slowly with distance.
3. No Food pheromone is used to indicate exhausted food locations. May not be necessary if resource exhaustion is not implemented. May be implemented as negative "Food" pheromone level (i.e. dropping "No Food" pheromone actually decreases the level of "Food" pheromone). Lasts for a very short time and decreases quickly with distance.
4. Friend pheromone is used to trace the location of other friendly hives. Moderately lasting and decreases slowly with distance.
5. Enemy pheromone is used to trace the location of other hostile hives or player's installations. Long lasting and decreases slowly with distance.
6. Danger pheromone is used to indicate dangerous areas that aliens will try to avoid if possible (i.e. this is the "we-got-our-asses-kicked-there" pheromone). Long lasting and decreases extremely quickly with distance (i.e. is quite short-ranged).
7. Empty pheromone is used to indicate free areas that may be colonized. It is moderately lasting and decreases slowly with distance.
Note that different hives will probably need to use different pheromone "frequencies" so aliens from one hive wouldn't be led home to a different hive.
Additionally, pollution gradient map can be used by aliens in addition to Enemy pheromone trails (they effectively add up).
Aliens are always moving either home (to their hive), or to some target/goal (away from hive). Aliens that move away from the hive always drop "Home" pheromone. On the way home aliens may drop different pheromone types depending on what they found in their travels.
Part II. Hive Behavior and Alien Activities.
The following "missions" can be performed by aliens away from their hive:
1. Foraging. This is the most frequent mission type for aliens. They travel along the "Food" pheromone trails until they hit a chunk with matching resource, collect the resource (or do whatever), and return by following "Home" pheromone. Worker aliens drop a small amounts of "Home" pheromone on their way to food (to keep the trail fresh), and similarly small amounts of "Food" pheromone on their way back. If they cannot find food, they will return home dropping moderate amounts of "No Food" pheromone instead. Aliens on working missions are generally less aggressive and will prefer to avoid the confrontation if hostiles are found, retreating back to their hive and dropping a trail of "Enemy" pheromone.
2. Diplomacy. Hives are exchanging genetic information by sending small groups of aliens to friendly hives. These will follow "Friend" pheromone trails until a friendly hive is found, spend some time there, and return home. A moderate trail of "Home" pheromone is left on their way to a friendly hive, and a moderate trail of "Friend" pheromone on their way back. Diplomat aliens are similarly non-aggressive - if they meet hostiles, they will prefer retreat rather than fight, leaving "Enemy" pheromone trail. If they cannot find a friendly hive, they will return home leaving "Enemy" and "Danger" pheromone trails.
3. Attack. Only happens if there are some chunks with "Enemy" pheromone on them around the hive. Attacking group strength will depend on "Enemy" and "Danger" pheromone levels. Additionally, high concentrations of "Danger" pheromone may preclude some less advanced alien types from participating in the attack. Attacking aliens will follow the "Enemy" pheromone trail, while trying to avoid "Danger" pheromone. They may even cancel an attack altogether if there's no way to "Enemy" without concentration of "Danger" pheromone is much higher than "Enemy".
4. Retreat. Occurs after an Attack mission has dealt enough damage to the enemy, destroyed him entirely, or when attacking forces sufferent too high casualties. Aliens will travel home, leaving a trail of pheromones depending on attack results: reinforce "Enemy" pheromone if attack was successful, clear "Enemy" pheromone if attack resulted in complete annihilation of enemy objects, or lay "Danger" pheromone if the attack has failed and attacking forces routed.
5. Death. Not technically a mission, but dying aliens release a large amount of "Danger" pheromone in the chunk they died.
6. Scouting. Unlike other missions, aliens on scouting missions are attracted to chunks with least total pheromone levels (i.e. the least explored chunks). Scouts leave a strong "Home" pheromone trail on their way out, and may leave different pheromones on their way back depending on what they found (friendly hives, hostile hives, destroyed hives, player's installations or player himself, food patches, empty spaces fitting for expansion etc). Distance that scouts will travel depends on overall pheromone intensity (the higher total pheromone concentrations, the farther scouts will try to travel, up to a certain limit). Scouts will generally avoid confrontation, prioritizing getting back to hive instead.
7. Expansion. When there are enough aliens and alien eggs in the hive, sufficient "Empty" pheromone levels nearby and not too high "Danger" pheromone levels, the hive may form a colonization party. It will travel along the "Empty" pheromone trail, find a place to settle (not necessarily the one that scouts found), and create a new hive which will be friendly to the one they originated in. Important note: colonizing party will leave a trail of "Friend" pheromones for their new hive, not the hive they originated from. This trail will later be used by the new hive to send "diplomats" to their mother hive.
8. Panic and Evacuation. Similar to expansion, but happens when there are extremely high levels of "Danger" pheromone around the hive. Hive will attempt to evacuate, sending colonization parties in the directions with the least amount of "Danger" pheromone, even at the expense to the hive's own defense. Newly created hives will have a much higher than normal worm-to-nests ratio.
Note that both Expansion and Evacuation colonizing missions do not leave the "Home" pheromone trail as they leave the hive - the only exception to the rule. Instead, Expansion missions leave a trail of "Friend" pheromone for their new hive, while Evacuation missions leave a mixed trail of "Friend" and "Danger" pheromones.
Part III. Alien Procreation and Frenzy Level.
To be done.
Part IV. Alien Combat Behavior and Morale.
To be done.
![Very Happy :D](./images/smilies/icon_e_biggrin.gif)
Part I. Alien Pheromones and Hive Behavior.
The suggestions to replace alien behavior AI with a distributed pheromone system have been voiced already on this forum. This is an attempt to formalize a possible implementation of such behavior.
The basic premise is fairly simple. There are a number of alien pheromones that aliens are leaving behind them as they move around the map. There may be different pheromones, and they may have different levels in each chunk (same as pollution). Unlike pollution however, pheromones do not spread. They expire with time (unless renewed by aliens), but there is no spread of pheromones into adjacent chunks. This means that CPU requirements to maintain the pheromone map are zero: for each pheromone, a map chunk keeps track of pheromone level and the time of it's last update. If pheromone level is requested by the game, only then does the CPU access those two values, calculates actual pheromone level and updates the chunk. Otherwise, if there's no alien activity in the chunk, there is no CPU load at all.
Whenever aliens travel for whatever reason, they decide where to move by measuring pheromone levels in 3x3 chunk area, centered on their current chunk. Depending on their goals, different pheromones are checked. Eventual decision is randomized depending on pheromone levels in adjacent chunks.
Each time an alien leaves a map chunk, it drops some pheromones in the chunk it left. What type of pheromone is dropped and the amount depends on alien current activity. Aliens however never drop any pheromone at their home chunks - i.e. those chunks where alien hive is located in. Those chunks always have the maximum level of "home" pheromone (see below). The amount of pheromone each alien has is limited, and only a certain percentage is dropped every time it changes map chunks. This creates a gradient in pheromone levels.
To put it briefly, two primary characteristics of each pheromone are: how quickly it disperses once dropped (creating long-lasting or short-lasting effectors), and how quickly it's strength decreases with distance (creating long-ranged or short-ranged effectors).
There are seven different pheromone types:
1. Home pheromone is used by aliens to find the location of their hive. It's quite long-lasting and decreases slowly with distance.
2. Food pheromone is used to trace the locations of points of interests for the aliens that are not player-related (food sources probably, but there may be other targets implemented, each with it's own pheromone type). It's short-lasting (and thus needs to be refreshed frequently) and decreases slowly with distance.
3. No Food pheromone is used to indicate exhausted food locations. May not be necessary if resource exhaustion is not implemented. May be implemented as negative "Food" pheromone level (i.e. dropping "No Food" pheromone actually decreases the level of "Food" pheromone). Lasts for a very short time and decreases quickly with distance.
4. Friend pheromone is used to trace the location of other friendly hives. Moderately lasting and decreases slowly with distance.
5. Enemy pheromone is used to trace the location of other hostile hives or player's installations. Long lasting and decreases slowly with distance.
6. Danger pheromone is used to indicate dangerous areas that aliens will try to avoid if possible (i.e. this is the "we-got-our-asses-kicked-there" pheromone). Long lasting and decreases extremely quickly with distance (i.e. is quite short-ranged).
7. Empty pheromone is used to indicate free areas that may be colonized. It is moderately lasting and decreases slowly with distance.
Note that different hives will probably need to use different pheromone "frequencies" so aliens from one hive wouldn't be led home to a different hive.
Additionally, pollution gradient map can be used by aliens in addition to Enemy pheromone trails (they effectively add up).
Aliens are always moving either home (to their hive), or to some target/goal (away from hive). Aliens that move away from the hive always drop "Home" pheromone. On the way home aliens may drop different pheromone types depending on what they found in their travels.
Part II. Hive Behavior and Alien Activities.
The following "missions" can be performed by aliens away from their hive:
1. Foraging. This is the most frequent mission type for aliens. They travel along the "Food" pheromone trails until they hit a chunk with matching resource, collect the resource (or do whatever), and return by following "Home" pheromone. Worker aliens drop a small amounts of "Home" pheromone on their way to food (to keep the trail fresh), and similarly small amounts of "Food" pheromone on their way back. If they cannot find food, they will return home dropping moderate amounts of "No Food" pheromone instead. Aliens on working missions are generally less aggressive and will prefer to avoid the confrontation if hostiles are found, retreating back to their hive and dropping a trail of "Enemy" pheromone.
2. Diplomacy. Hives are exchanging genetic information by sending small groups of aliens to friendly hives. These will follow "Friend" pheromone trails until a friendly hive is found, spend some time there, and return home. A moderate trail of "Home" pheromone is left on their way to a friendly hive, and a moderate trail of "Friend" pheromone on their way back. Diplomat aliens are similarly non-aggressive - if they meet hostiles, they will prefer retreat rather than fight, leaving "Enemy" pheromone trail. If they cannot find a friendly hive, they will return home leaving "Enemy" and "Danger" pheromone trails.
3. Attack. Only happens if there are some chunks with "Enemy" pheromone on them around the hive. Attacking group strength will depend on "Enemy" and "Danger" pheromone levels. Additionally, high concentrations of "Danger" pheromone may preclude some less advanced alien types from participating in the attack. Attacking aliens will follow the "Enemy" pheromone trail, while trying to avoid "Danger" pheromone. They may even cancel an attack altogether if there's no way to "Enemy" without concentration of "Danger" pheromone is much higher than "Enemy".
4. Retreat. Occurs after an Attack mission has dealt enough damage to the enemy, destroyed him entirely, or when attacking forces sufferent too high casualties. Aliens will travel home, leaving a trail of pheromones depending on attack results: reinforce "Enemy" pheromone if attack was successful, clear "Enemy" pheromone if attack resulted in complete annihilation of enemy objects, or lay "Danger" pheromone if the attack has failed and attacking forces routed.
5. Death. Not technically a mission, but dying aliens release a large amount of "Danger" pheromone in the chunk they died.
6. Scouting. Unlike other missions, aliens on scouting missions are attracted to chunks with least total pheromone levels (i.e. the least explored chunks). Scouts leave a strong "Home" pheromone trail on their way out, and may leave different pheromones on their way back depending on what they found (friendly hives, hostile hives, destroyed hives, player's installations or player himself, food patches, empty spaces fitting for expansion etc). Distance that scouts will travel depends on overall pheromone intensity (the higher total pheromone concentrations, the farther scouts will try to travel, up to a certain limit). Scouts will generally avoid confrontation, prioritizing getting back to hive instead.
7. Expansion. When there are enough aliens and alien eggs in the hive, sufficient "Empty" pheromone levels nearby and not too high "Danger" pheromone levels, the hive may form a colonization party. It will travel along the "Empty" pheromone trail, find a place to settle (not necessarily the one that scouts found), and create a new hive which will be friendly to the one they originated in. Important note: colonizing party will leave a trail of "Friend" pheromones for their new hive, not the hive they originated from. This trail will later be used by the new hive to send "diplomats" to their mother hive.
8. Panic and Evacuation. Similar to expansion, but happens when there are extremely high levels of "Danger" pheromone around the hive. Hive will attempt to evacuate, sending colonization parties in the directions with the least amount of "Danger" pheromone, even at the expense to the hive's own defense. Newly created hives will have a much higher than normal worm-to-nests ratio.
Note that both Expansion and Evacuation colonizing missions do not leave the "Home" pheromone trail as they leave the hive - the only exception to the rule. Instead, Expansion missions leave a trail of "Friend" pheromone for their new hive, while Evacuation missions leave a mixed trail of "Friend" and "Danger" pheromones.
Part III. Alien Procreation and Frenzy Level.
To be done.
Part IV. Alien Combat Behavior and Morale.
To be done.