The bug
When running to get into houses during nightfall, villagers run extremely fast. However when escaping from zombies (including babies), they only walk at normal speeds, allowing zombies (especially babies) to catch up and kill them. They run from illagers faster than from zombies, but not as fast as getting into houses.
This running behavior is incorrect; if anything they should sprint away from zombies and vindicators, and walk normally to get into houses (where there's no real danger). Their sprinting to get into houses also impairs their ability to get through the doorway.
How to reproduce
Summon a NoAI
zombie and vindicator at opposite ends of a 10-15 block long tunnel, and spawn a villager. It will sprint away from the vindicator to the zombie, than walk away from the zombie back to the vindicator.
Videos demonstrating the issue
Zombie and vindicator at opposite ends: difference in speed is noticeable
Zombie and a valid house at the same end: difference in speed is even more noticeable
Vindicator and house at the same end: the villager seems to run faster towards the house than away from the vindicator, although this difference is less noticeable than the previous two
Note that the first video was taken during daytime, and the latter two were during nighttime with the Night Vision effect.
Code analysis
This was decompiled in 1.12 with MCP 9.40:
The different villager "running" actions were given different speeds (which may be intended, but is inconsistent). The file EntityVillager.java
has the following four lines in the initEntityAI()
method:
this.tasks.addTask(1, new EntityAIAvoidEntity(this, EntityZombie.class, 8.0F, 0.6D, 0.6D));
this.tasks.addTask(1, new EntityAIAvoidEntity(this, EntityEvoker.class, 12.0F, 0.8D, 0.8D));
this.tasks.addTask(1, new EntityAIAvoidEntity(this, EntityVindicator.class, 8.0F, 0.8D, 0.8D));
this.tasks.addTask(1, new EntityAIAvoidEntity(this, EntityVex.class, 8.0F, 0.6D, 0.6D));
In the file EntityAIMoveIndoors.java
, there is the following line of code:
this.entity.getNavigator().tryMoveToXYZ((double)i + 0.5D, (double)j, (double)k + 0.5D, 1.0D);
It seems like the speed modifiers of Villagers are set to 0.6 for avoiding zombies, 0.8 for illagers, and 1.0 for moving indoors. This priority is incorrect; villagers should move faster when avoiding actual zombies instead of moving indoors due to supposed threats.
Comments


Resolving as Cannot Reproduce since I can no longer reproduce this (at least in 1.15); also see Marcono's comment.
It looks like this is fixed, but it would be good if someone else can confirm this.
In 1.15 villagers do not have AI tasks, but instead activities. It appears the activities in question are returned by
VillagerGoalPackages.getRestPackage(VillagerProfession, float)
: Walks to home with a speed off
VillagerGoalPackages.getHidePackage(VillagerProfession, float)
: Walks to home with a speed off * 1.25
VillagerGoalPackages.getPanicPackage(VillagerProfession, float)
: Walks away from monsters or mobs which hurt the villager with a speed off * 1.5