Mob AI

From Minecraft Wiki
Jump to navigation Jump to search
This article is a work in progress.
 
Please help expand and improve it. The talk page may contain suggestions.
This article is a stub.
 
You can help by expanding it. The talk page may contain suggestions.

This article describes the various components of a mob's AI that control the mob's behavior.

Brain

[edit | edit source]

Some mobs use a complex AI system called a brain. A mob's brain stores memories (data), a list of tasks that can be performed, and a list of sensors used to determine which tasks it can perform.

Sensor

[edit | edit source]

Sensors are used to detect if a mob with a brain can perform certain tasks. Examples of sensors include, a sensor to detect if an armadillo should be scared, a sensor to detect the nearest player, and a sensor to detect if a mob is in water.

List of Sensor in Java Edition
Sensor Class Uses
dummy DummySensor defaulted sensor type
nearest_items NearestItemSensor
nearest_living_entities NearestLivingEntitySensor
nearest_players PlayerSensor
nearest_bed NearestBedSensor Villager
hurt_by HurtBySensor
villager_hostiles VillagerHostilesSensor Villager
villager_babies VillagerBabiesSensor Villager
secondary_pois SecondaryPoiSensor Villager
golem_detected GolemSensor Villager
armadillo_scare_detected MobSensor Armadillo
piglin_specific_sensor PiglinSpecificSensor Piglin
piglin_brute_specific_sensor PiglinBruteSpecificSensorregister Piglin
hoglin_specific_sensor HoglinSpecificSensor Hoglin
nearest_adult AdultSensor
nearest_adult_any_type AdultSensor Happy Ghast
axolotl_attackables AxolotlAttackablesSensor Axolotl
food_temptations TemptingSensor
frog_temptations TemptingSensor
nautilus_temptations TemptingSensor
frog_attackables FrogAttackablesSensor Frog
is_in_water IsInWaterSensor Frog
warden_entity_sensor WardenEntitySensor Warden
breeze_attack_entity_sensor BreezeAttackEntitySensor Breeze


Sensor classes

[edit | edit source]
This section needs expansion.
 
You can help by expanding it.
Class Memory requires Do tick
DummySensor None Nothing
NearestItemSensor nearest_visible_wanted_item Find the nearest item that:
  • It is to a euclidean distance of up to 32 blocks from the mob.
  • On the y-axis, it is 16 blocks away from the mob's hitbox.
  • It is in mob line of sight[a].
  • The mob wants to pick up it.[b]

And save this item in the memory nearest_visible_wanted_item.

NearestLivingEntitySensor mobs and visible_mobs Lists all other living entities that are alive and within a cobuoid generated by the mob's hitbox incremented by mob's follow_range in all directions. Sort list by euclidean distance and add to memory mobs.

In visible_mobs save the entities of mobs that[c]:

  • They are not a Spectator player, or an invisible or marker armor stand,
  • They are in mob line of sight[a],
  • And they are within a range of r blocks of the mob, where r is the follow_range of the mob[d].
PlayerSensor nearest_players, nearest_visible_player, nearest_visible_targetable_player and nearest_visible_targetable_players List all non-spectator players to a euclidean distance up to mob's follow_range fro the mobs, sort by euclidean distance and save in nearest_players memory.

From this list, it filters the players who are in the mob's line of sight[a] and within follow range[d]. If it exists, save the first player from the list in memory earest_visible_player.

In memory nearest_visible_targetable_players saves this last list with the following filters:

  • If the difficulty is peaceful, remove all players.
  • Remove all players from creative mode.
  • Remove all players from the same team from the mob.
  • If the mob is the allay, remove the linked player.
  • If the mob is a illager, minecraft:player is in the tag #illager_friends, and neither the mob nor the player is on a team, remove the player.
  • If the mob is a tamed animal, remove their owner and the players on the owner's team.

If the end list is not empty, save in memory nearest_visible_targetable_player the first player on a list.

NearestBedSensor nearest_bed If the mob is baby:
  • Set the auxiliary variables triedCount = 0 and lastUpdate = GameTime + RandomInt(20)[e], where GameTime is the total number of game ticks that have elapsed in the world.
  • In the batchCache cache, it attempts to save POIs of type home that are up to 48 blocks from the mob at a euclid distance. For each POI found whose position Pos is not in the cache, it increases triedCount by 1, if triedCount < 5, it saves (Pos, lastUpdate + 40) in the cache. In this way, it saves up to 5 new beds in the batchCache. The POIs added to the cache create a new set pois, with the position and type of each PO.
  • Try creating a path to one of the POI in pois. If it find a path, that path can reach and the target is a POI, save the target's position in mob's memory nearest_bed.
  • If the path cannot be found, or the path cannot be reach, and triedCount < 5, remove from the batchcache the values where the second coordinate is less than lastUpdate.
HurtBySensor huty_by and hurt_by_entity
VillagerHostilesSensor
VillagerBabiesSensor
SecondaryPoiSensor
GolemSensor
MobSensor
PiglinSpecificSensor
PiglinBruteSpecificSensorregister
HoglinSpecificSensor
AdultSensor
AxolotlAttackablesSensor
TemptingSensor
FrogAttackablesSensor
IsInWaterSensor is_in_water If the mob is touching the water, add the memory is_in_water, otherwise remove the memory.
WardenEntitySensor
BreezeAttackEntitySensor
  1. a b c It is possible to draw a straight line from the mob's eye to the item without intersecting a block collision box.
  2. It depends on the mob, defined independently of the sensor.
  3. Technically, in memory mob_visibel a list equal to that of mob is saved, along with a binary list of the same size that indicates whether the entity passes the test or not.
  4. a b For entities that are not in mob's memory attack_target, this range is multiplied by a modifier that indicates the visibility percentage of the entity. The modifier starting with 1 and cumulatively:
    • Multiplying by 0.8 if it is a sneaking player,
    • Multiplying by 0.07 if it is invisible and without equipament in humanoid armor slot,
    • Multiplying by 0.175p if it is invisible and has p≥1 equipaments in humanoid armor slot,
    • Multiplying by 0.5 if it is using the corresponding mob head in the head slot (for skeleton, zombie, piglin, piglin brute, and creeper)
    The final range must be at least 2.
  5. RandomInt(x) is a random integer n such that 0 ≤ n < x.

Tasks are activities that mobs with brain AI systems can perform. Sensors are used to determine if a task can be performed, and tasks may use information stored in memories. Examples of tasks include a villager walking to its jobsite, a piglin admiring an item, or a warden emerging. Tasks have priorities, so tasks with a higher priority will over run tasks with lower priority, for example, if a zombie is wandering and it sees a player, it will switch tasks to attacking.

Memory

[edit | edit source]

Memories are used to store data, and are used when mobs perform tasks. Examples of memories include a list of all visible mobs in range, the location of a villager's jobsite block, and any cooldowns the mob may have.

Control

[edit | edit source]

Walking

[edit | edit source]
This section of the article is empty.
 
You can help by expanding it.

Swimming

[edit | edit source]
This section of the article is empty.
 
You can help by expanding it.

Flying

[edit | edit source]
This section of the article is empty.
 
You can help by expanding it.

Goals

[edit | edit source]

Goals are a simple AI system used primarily by mobs already existed before the brain AI. However, there are recent mobs that use both AIs simultaneously, for example, the happy ghast.

Goals can range from wandering around, opening doors, attacking another mob, and more. Each type of mob that uses goals has its own defined list of goals, and each goal has a priority. Mobs attempt to perform the lowest priority goal they can, and may switch goals if there is an opportunity to pursue a lower priority goal. For example, if a zombie is targeting and chasing a villager (priority of 3) and a player comes within the detection range of the zombie, the zombie may target and chase the player (priority of 2) instead.

List of goals

[edit | edit source]

List of goals that are used by many different mobs.

Lists of mob goals

[edit | edit source]

Lists of goals and associated priorities for individual mobs.

Zombie
Goal Priority Notes
Revenge 1 Attack entity that damaged the zombie
Active Target - Player 2 Target the player
Zombie Attack 2 Attack current target
Active Target - Iron Golem 3 Target Iron Golem
Active Target - Merchant 3 Target Villager or Wandering Trader
Destroy Egg 4 Break turtle eggs
Active Target - Turtle 5 Target baby turtle
Wander Around Far 7
Look Around 8 Looks away from Entities
Look at Entity 8 Looks at nearest Entities

Pathfinding

[edit | edit source]

Pathfinding penalties

[edit | edit source]
Zombified piglin starts pathfinding to a turtle egg
Due to pathfinding penalties, the zombified piglin takes an indirect path to the turtle egg, pathing through wither roses and taking damage, instead of pathing through the lava or magma blocks, which do not damage the zombified piglin

When pathfinding to a target, mobs will sometimes avoid certain blocks (usually blocks that cause damage or slow the mob down). These blocks have a penalty associated with them. Generally, mobs try to path through blocks with the smallest penalty. There are some blocks that most mobs cannot path through; these all have a penalty of -1.

The following table is a list of all pathfinding penalties and their default values. Each type of pathfinding (land, water, air) uses its own subset of these penalties. Some mobs override these default values.

Penalty type Penalty Description
blocked -1 can't pathfind through block (most full solid blocks)
powder snow -1 powder snow
fence -1 fences
walls
fence gates (closed)
lava -1 lava
unpassable rail -1 rail
damage - other -1 cactus
sweet berry bush
closed wood door -1 wood door
closed iron door -1 iron door
open 0 air
cave air
void air
walkable 0 TNT with pressure plate on top (under the mob)
walkable door 0 a door the mob can open
trapdoor 0 trapdoors
lily pad
big dripleaf
danger - powder snow 0 neighboring block is powder snow
open door 0 open wood or iron door
cocoa 0 cocoa
damage - cautious 0 wither rose
pointed dripstone
danger - trapdoor 0 neighboring block is trapdoor
breach water 4 there is an air block above a water block
water 8 water
water border 8 neighboring block is water
danger - fire 8 neighboring block is:
fire
lava
magma block
lit campfire
lava cauldron
danger - other 8 neighboring block is:
cactus
sweet berry bush
honey 8 honey block
damage - fire 16 fire
lava
magma block
lit campfire
lava cauldron
Mob pathfinding penalty overrides

The following mobs override the default pathfinding penalty for some blocks:

Pathfinding on land

[edit | edit source]
  • All entities Move based off of random targets that are generated every tick with a priority toward their entity specific goals.
  • These targets can be located anywhere in 3D space within the entity range and are only every generated when wandering or when they have no goals.
  • For every target a path is generated; with the entity preferring the path with the lowest score regarding the Pathfinding penalties.
  • If an entity cannot take any paths with a low enough score, or any paths without a score of -1 the entity will not move until one is found.
  • If an entity's target is located inside a block (including water), the target is moved to the nearest air block above it.

This means, every entitiy when wandering will always prefer to pathfind to a block with the most blocks below it within range.

Due to the higher chance that a target will be generated within the blocks below it and moved up to the nearest air block above before generating a path.

Pathfinding in water

[edit | edit source]
  • Pathfinding in water is roughly the same as land, however, most entities, excluding fish and drowned, will bounce on the surface preferring land due to the Pathfinding penalties of water.

Pathfinding in air

[edit | edit source]
  • Pathfinding in air is roughly the same as land, however, there are few entities that can pathfind to air.

See also

[edit | edit source]
[edit | edit source]