| 1 | package net.minecraft.world.entity.ai.behavior;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import com.google.common.collect.ImmutableMap;
|
| 5 | import com.google.common.collect.ImmutableSet;
|
| 6 | import com.mojang.datafixers.util.Pair;
|
| 7 | import java.util.Optional;
|
| 8 | import net.minecraft.core.BlockPos;
|
| 9 | import net.minecraft.core.Holder;
|
| 10 | import net.minecraft.server.level.ServerLevel;
|
| 11 | import net.minecraft.tags.BlockTags;
|
| 12 | import net.minecraft.world.entity.AgeableMob;
|
| 13 | import net.minecraft.world.entity.EntityTypes;
|
| 14 | import net.minecraft.world.entity.LivingEntity;
|
| 15 | import net.minecraft.world.entity.MobCategory;
|
| 16 | import net.minecraft.world.entity.ai.behavior.declarative.BehaviorBuilder;
|
| 17 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 18 | import net.minecraft.world.entity.ai.memory.MemoryStatus;
|
| 19 | import net.minecraft.world.entity.ai.village.poi.PoiTypes;
|
| 20 | import net.minecraft.world.entity.npc.villager.Villager;
|
| 21 | import net.minecraft.world.entity.npc.villager.VillagerProfession;
|
| 22 | import net.minecraft.world.entity.raid.Raid;
|
| 23 | import net.minecraft.world.level.block.BedBlock;
|
| 24 | import net.minecraft.world.level.block.state.BlockState;
|
| 25 |
|
| 26 | public class VillagerGoalPackages {
|
| 27 | private static final float STROLL_SPEED_MODIFIER = 0.4F;
|
| 28 | public static final int INTERACT_DIST_SQR = 5;
|
| 29 | public static final int INTERACT_WALKUP_DIST = 2;
|
| 30 | public static final float INTERACT_SPEED_MODIFIER = 0.5F;
|
| 31 |
|
| 32 | public static ImmutableList<Pair<Integer, ? extends BehaviorControl<? super Villager>>> getCorePackage(
|
| 33 | final Holder<VillagerProfession> profession, final float speedModifier
|
| 34 | ) {
|
| 35 | return ImmutableList.of(
|
| 36 | Pair.of(0, new Swim<>(0.8F)),
|
| 37 | Pair.of(0, InteractWithDoor.create()),
|
| 38 | Pair.of(0, new LookAtTargetSink(45, 90)),
|
| 39 | Pair.of(0, new VillagerPanicTrigger()),
|
| 40 | Pair.of(0, WakeUp.create()),
|
| 41 | Pair.of(0, ReactToBell.create()),
|
| 42 | Pair.of(0, SetRaidStatus.create()),
|
| 43 | Pair.of(0, ValidateNearbyPoi.create(profession.value().heldJobSite(), MemoryModuleType.JOB_SITE)),
|
| 44 | Pair.of(0, ValidateNearbyPoi.create(profession.value().acquirableJobSite(), MemoryModuleType.POTENTIAL_JOB_SITE)),
|
| 45 | Pair.of(1, new MoveToTargetSink()),
|
| 46 | Pair.of(2, PoiCompetitorScan.create()),
|
| 47 | Pair.of(3, new LookAndFollowTradingPlayerSink(speedModifier)),
|
| 48 | Pair.of(5, GoToWantedItem.create(speedModifier, false, 4)),
|
| 49 | Pair.of(
|
| 50 | 6,
|
| 51 | AcquirePoi.create(
|
| 52 | profession.value().acquirableJobSite(), MemoryModuleType.JOB_SITE, MemoryModuleType.POTENTIAL_JOB_SITE, true, Optional.empty(), (l, p) -> true
|
| 53 | )
|
| 54 | ),
|
| 55 | Pair.of(7, new GoToPotentialJobSite(speedModifier)),
|
| 56 | Pair.of(8, YieldJobSite.create(speedModifier)),
|
| 57 | Pair.of(10, AcquirePoi.create(p -> p.is(PoiTypes.HOME), MemoryModuleType.HOME, false, Optional.of((byte)14), VillagerGoalPackages::validateBedPoi)),
|
| 58 | Pair.of(10, AcquirePoi.create(p -> p.is(PoiTypes.MEETING), MemoryModuleType.MEETING_POINT, true, Optional.of((byte)14))),
|
| 59 | Pair.of(10, AssignProfessionFromJobSite.create()),
|
| 60 | Pair.of(10, ResetProfession.create())
|
| 61 | );
|
| 62 | }
|
| 63 |
|
| 64 | private static boolean validateBedPoi(final ServerLevel level, final BlockPos blockPos) {
|
| 65 | BlockState blockState = level.getBlockState(blockPos);
|
| 66 | return blockState.is(BlockTags.VILLAGERS_CAN_SLEEP_ON_BED) && !blockState.getValue(BedBlock.OCCUPIED);
|
| 67 | }
|
| 68 |
|
| 69 | public static ImmutableList<Pair<Integer, ? extends BehaviorControl<? super Villager>>> getWorkPackage(
|
| 70 | final Holder<VillagerProfession> profession, final float speedModifier
|
| 71 | ) {
|
| 72 | WorkAtPoi workAtPoi;
|
| 73 | if (profession.is(VillagerProfession.FARMER)) {
|
| 74 | workAtPoi = new WorkAtComposter();
|
| 75 | } else {
|
| 76 | workAtPoi = new WorkAtPoi();
|
| 77 | }
|
| 78 |
|
| 79 | return ImmutableList.of(
|
| 80 | getMinimalLookBehavior(),
|
| 81 | Pair.of(
|
| 82 | 5,
|
| 83 | new RunOne<>(
|
| 84 | ImmutableList.of(
|
| 85 | Pair.of(workAtPoi, 7),
|
| 86 | Pair.of(StrollAroundPoi.create(MemoryModuleType.JOB_SITE, 0.4F, 4), 2),
|
| 87 | Pair.of(StrollToPoi.create(MemoryModuleType.JOB_SITE, 0.4F, 1, 10), 5),
|
| 88 | Pair.of(StrollToPoiList.create(MemoryModuleType.SECONDARY_JOB_SITE, speedModifier, 1, 6, MemoryModuleType.JOB_SITE), 5),
|
| 89 | Pair.of(new HarvestFarmland(), profession.is(VillagerProfession.FARMER) ? 2 : 5),
|
| 90 | Pair.of(new UseBonemeal(), profession.is(VillagerProfession.FARMER) ? 4 : 7)
|
| 91 | )
|
| 92 | )
|
| 93 | ),
|
| 94 | Pair.of(10, new ShowTradesToPlayer(400, 1600)),
|
| 95 | Pair.of(10, SetLookAndInteract.create(EntityTypes.PLAYER, 4)),
|
| 96 | Pair.of(2, SetWalkTargetFromBlockMemory.create(MemoryModuleType.JOB_SITE, speedModifier, 9, 100, 1200)),
|
| 97 | Pair.of(3, new GiveGiftToHero(100)),
|
| 98 | Pair.of(99, UpdateActivityFromSchedule.create())
|
| 99 | );
|
| 100 | }
|
| 101 |
|
| 102 | public static ImmutableList<Pair<Integer, ? extends BehaviorControl<? super Villager>>> getPlayPackage(final float speedModifier) {
|
| 103 | return ImmutableList.of(
|
| 104 | Pair.of(0, new MoveToTargetSink(80, 120)),
|
| 105 | getFullLookBehavior(),
|
| 106 | Pair.of(5, PlayTagWithOtherKids.create()),
|
| 107 | Pair.of(
|
| 108 | 5,
|
| 109 | new RunOne<>(
|
| 110 | ImmutableMap.of(MemoryModuleType.VISIBLE_VILLAGER_BABIES, MemoryStatus.VALUE_ABSENT),
|
| 111 | ImmutableList.of(
|
| 112 | Pair.of(InteractWith.of(EntityTypes.VILLAGER, 8, MemoryModuleType.INTERACTION_TARGET, speedModifier, 2), 2),
|
| 113 | Pair.of(InteractWith.of(EntityTypes.CAT, 8, MemoryModuleType.INTERACTION_TARGET, speedModifier, 2), 1),
|
| 114 | Pair.of(VillageBoundRandomStroll.create(speedModifier), 1),
|
| 115 | Pair.of(SetWalkTargetFromLookTarget.create(speedModifier, 2), 1),
|
| 116 | Pair.of(new JumpOnBed(speedModifier), 2),
|
| 117 | Pair.of(new DoNothing(20, 40), 2)
|
| 118 | )
|
| 119 | )
|
| 120 | ),
|
| 121 | Pair.of(99, UpdateActivityFromSchedule.create())
|
| 122 | );
|
| 123 | }
|
| 124 |
|
| 125 | public static ImmutableList<Pair<Integer, ? extends BehaviorControl<? super Villager>>> getRestPackage(final float speedModifier) {
|
| 126 | return ImmutableList.of(
|
| 127 | Pair.of(2, SetWalkTargetFromBlockMemory.create(MemoryModuleType.HOME, speedModifier, 1, 150, 1200)),
|
| 128 | Pair.of(3, ValidateNearbyPoi.create(p -> p.is(PoiTypes.HOME), MemoryModuleType.HOME)),
|
| 129 | Pair.of(3, new SleepInBed()),
|
| 130 | Pair.of(
|
| 131 | 5,
|
| 132 | new RunOne<>(
|
| 133 | ImmutableMap.of(MemoryModuleType.HOME, MemoryStatus.VALUE_ABSENT),
|
| 134 | ImmutableList.of(
|
| 135 | Pair.of(SetClosestHomeAsWalkTarget.create(speedModifier), 1),
|
| 136 | Pair.of(InsideBrownianWalk.create(speedModifier), 4),
|
| 137 | Pair.of(GoToClosestVillage.create(speedModifier, 4), 2),
|
| 138 | Pair.of(new DoNothing(20, 40), 2)
|
| 139 | )
|
| 140 | )
|
| 141 | ),
|
| 142 | getMinimalLookBehavior(),
|
| 143 | Pair.of(99, UpdateActivityFromSchedule.create())
|
| 144 | );
|
| 145 | }
|
| 146 |
|
| 147 | public static ImmutableList<Pair<Integer, ? extends BehaviorControl<? super Villager>>> getMeetPackage(final float speedModifier) {
|
| 148 | return ImmutableList.of(
|
| 149 | Pair.of(
|
| 150 | 2,
|
| 151 | TriggerGate.triggerOneShuffled(
|
| 152 | ImmutableList.of(Pair.of(StrollAroundPoi.create(MemoryModuleType.MEETING_POINT, 0.4F, 40), 2), Pair.of(SocializeAtBell.create(), 2))
|
| 153 | )
|
| 154 | ),
|
| 155 | Pair.of(10, new ShowTradesToPlayer(400, 1600)),
|
| 156 | Pair.of(10, SetLookAndInteract.create(EntityTypes.PLAYER, 4)),
|
| 157 | Pair.of(2, SetWalkTargetFromBlockMemory.create(MemoryModuleType.MEETING_POINT, speedModifier, 6, 100, 200)),
|
| 158 | Pair.of(3, new GiveGiftToHero(100)),
|
| 159 | Pair.of(3, ValidateNearbyPoi.create(p -> p.is(PoiTypes.MEETING), MemoryModuleType.MEETING_POINT)),
|
| 160 | Pair.of(
|
| 161 | 3,
|
| 162 | new GateBehavior<>(
|
| 163 | ImmutableMap.of(),
|
| 164 | ImmutableSet.of(MemoryModuleType.INTERACTION_TARGET),
|
| 165 | GateBehavior.OrderPolicy.ORDERED,
|
| 166 | GateBehavior.RunningPolicy.RUN_ONE,
|
| 167 | ImmutableList.of(Pair.of(new TradeWithVillager(), 1))
|
| 168 | )
|
| 169 | ),
|
| 170 | getFullLookBehavior(),
|
| 171 | Pair.of(99, UpdateActivityFromSchedule.create())
|
| 172 | );
|
| 173 | }
|
| 174 |
|
| 175 | public static ImmutableList<Pair<Integer, ? extends BehaviorControl<? super Villager>>> getIdlePackage(final float speedModifier) {
|
| 176 | return ImmutableList.of(
|
| 177 | Pair.of(
|
| 178 | 2,
|
| 179 | new RunOne<>(
|
| 180 | ImmutableList.of(
|
| 181 | Pair.of(InteractWith.of(EntityTypes.VILLAGER, 8, MemoryModuleType.INTERACTION_TARGET, speedModifier, 2), 2),
|
| 182 | Pair.of(InteractWith.of(EntityTypes.VILLAGER, 8, AgeableMob::canBreed, AgeableMob::canBreed, MemoryModuleType.BREED_TARGET, speedModifier, 2), 1),
|
| 183 | Pair.of(InteractWith.of(EntityTypes.CAT, 8, MemoryModuleType.INTERACTION_TARGET, speedModifier, 2), 1),
|
| 184 | Pair.of(VillageBoundRandomStroll.create(speedModifier), 1),
|
| 185 | Pair.of(SetWalkTargetFromLookTarget.create(speedModifier, 2), 1),
|
| 186 | Pair.of(new JumpOnBed(speedModifier), 1),
|
| 187 | Pair.of(new DoNothing(30, 60), 1)
|
| 188 | )
|
| 189 | )
|
| 190 | ),
|
| 191 | Pair.of(3, new GiveGiftToHero(100)),
|
| 192 | Pair.of(3, SetLookAndInteract.create(EntityTypes.PLAYER, 4)),
|
| 193 | Pair.of(3, new ShowTradesToPlayer(400, 1600)),
|
| 194 | Pair.of(
|
| 195 | 3,
|
| 196 | new GateBehavior<>(
|
| 197 | ImmutableMap.of(),
|
| 198 | ImmutableSet.of(MemoryModuleType.INTERACTION_TARGET),
|
| 199 | GateBehavior.OrderPolicy.ORDERED,
|
| 200 | GateBehavior.RunningPolicy.RUN_ONE,
|
| 201 | ImmutableList.of(Pair.of(new TradeWithVillager(), 1))
|
| 202 | )
|
| 203 | ),
|
| 204 | Pair.of(
|
| 205 | 3,
|
| 206 | new GateBehavior<>(
|
| 207 | ImmutableMap.of(),
|
| 208 | ImmutableSet.of(MemoryModuleType.BREED_TARGET),
|
| 209 | GateBehavior.OrderPolicy.ORDERED,
|
| 210 | GateBehavior.RunningPolicy.RUN_ONE,
|
| 211 | ImmutableList.of(Pair.of(new VillagerMakeLove(), 1))
|
| 212 | )
|
| 213 | ),
|
| 214 | getFullLookBehavior(),
|
| 215 | Pair.of(99, UpdateActivityFromSchedule.create())
|
| 216 | );
|
| 217 | }
|
| 218 |
|
| 219 | public static ImmutableList<Pair<Integer, ? extends BehaviorControl<? super Villager>>> getPanicPackage(final float speedModifier) {
|
| 220 | float runawaySpeed = speedModifier * 1.5F;
|
| 221 | return ImmutableList.of(
|
| 222 | Pair.of(0, VillagerCalmDown.create()),
|
| 223 | Pair.of(1, SetWalkTargetAwayFrom.entity(MemoryModuleType.NEAREST_HOSTILE, runawaySpeed, 6, false)),
|
| 224 | Pair.of(1, SetWalkTargetAwayFrom.entity(MemoryModuleType.HURT_BY_ENTITY, runawaySpeed, 6, false)),
|
| 225 | Pair.of(3, VillageBoundRandomStroll.create(runawaySpeed, 2, 2)),
|
| 226 | getMinimalLookBehavior()
|
| 227 | );
|
| 228 | }
|
| 229 |
|
| 230 | public static ImmutableList<Pair<Integer, ? extends BehaviorControl<? super Villager>>> getPreRaidPackage(final float speedModifier) {
|
| 231 | return ImmutableList.of(
|
| 232 | Pair.of(0, RingBell.create()),
|
| 233 | Pair.of(
|
| 234 | 0,
|
| 235 | TriggerGate.triggerOneShuffled(
|
| 236 | ImmutableList.of(
|
| 237 | Pair.of(SetWalkTargetFromBlockMemory.create(MemoryModuleType.MEETING_POINT, speedModifier * 1.5F, 2, 150, 200), 6),
|
| 238 | Pair.of(VillageBoundRandomStroll.create(speedModifier * 1.5F), 2)
|
| 239 | )
|
| 240 | )
|
| 241 | ),
|
| 242 | getMinimalLookBehavior(),
|
| 243 | Pair.of(99, ResetRaidStatus.create())
|
| 244 | );
|
| 245 | }
|
| 246 |
|
| 247 | public static ImmutableList<Pair<Integer, ? extends BehaviorControl<? super Villager>>> getRaidPackage(final float speedModifier) {
|
| 248 | return ImmutableList.of(
|
| 249 | Pair.of(
|
| 250 | 0,
|
| 251 | BehaviorBuilder.sequence(
|
| 252 | BehaviorBuilder.triggerIf(VillagerGoalPackages::raidExistsAndNotVictory),
|
| 253 | TriggerGate.triggerOneShuffled(
|
| 254 | ImmutableList.of(Pair.of(MoveToSkySeeingSpot.create(speedModifier), 5), Pair.of(VillageBoundRandomStroll.create(speedModifier * 1.1F), 2))
|
| 255 | )
|
| 256 | )
|
| 257 | ),
|
| 258 | Pair.of(0, new CelebrateVillagersSurvivedRaid(600, 600)),
|
| 259 | Pair.of(
|
| 260 | 2, BehaviorBuilder.sequence(BehaviorBuilder.triggerIf(VillagerGoalPackages::raidExistsAndActive), LocateHidingPlace.create(24, speedModifier * 1.4F, 1))
|
| 261 | ),
|
| 262 | getMinimalLookBehavior(),
|
| 263 | Pair.of(99, ResetRaidStatus.create())
|
| 264 | );
|
| 265 | }
|
| 266 |
|
| 267 | public static ImmutableList<Pair<Integer, ? extends BehaviorControl<? super Villager>>> getHidePackage(final float speedModifier) {
|
| 268 | int closeEnoughDist = 2;
|
| 269 | return ImmutableList.of(
|
| 270 | Pair.of(0, SetHiddenState.create(15, 3)), Pair.of(1, LocateHidingPlace.create(32, speedModifier * 1.25F, 2)), getMinimalLookBehavior()
|
| 271 | );
|
| 272 | }
|
| 273 |
|
| 274 | private static Pair<Integer, BehaviorControl<LivingEntity>> getFullLookBehavior() {
|
| 275 | return Pair.of(
|
| 276 | 5,
|
| 277 | new RunOne<>(
|
| 278 | ImmutableList.of(
|
| 279 | Pair.of(SetEntityLookTarget.create(EntityTypes.CAT, 8.0F), 8),
|
| 280 | Pair.of(SetEntityLookTarget.create(EntityTypes.VILLAGER, 8.0F), 2),
|
| 281 | Pair.of(SetEntityLookTarget.create(EntityTypes.PLAYER, 8.0F), 2),
|
| 282 | Pair.of(SetEntityLookTarget.create(MobCategory.CREATURE, 8.0F), 1),
|
| 283 | Pair.of(SetEntityLookTarget.create(MobCategory.WATER_CREATURE, 8.0F), 1),
|
| 284 | Pair.of(SetEntityLookTarget.create(MobCategory.AXOLOTLS, 8.0F), 1),
|
| 285 | Pair.of(SetEntityLookTarget.create(MobCategory.UNDERGROUND_WATER_CREATURE, 8.0F), 1),
|
| 286 | Pair.of(SetEntityLookTarget.create(MobCategory.WATER_AMBIENT, 8.0F), 1),
|
| 287 | Pair.of(SetEntityLookTarget.create(MobCategory.MONSTER, 8.0F), 1),
|
| 288 | Pair.of(new DoNothing(30, 60), 2)
|
| 289 | )
|
| 290 | )
|
| 291 | );
|
| 292 | }
|
| 293 |
|
| 294 | private static Pair<Integer, BehaviorControl<LivingEntity>> getMinimalLookBehavior() {
|
| 295 | return Pair.of(
|
| 296 | 5,
|
| 297 | new RunOne<>(
|
| 298 | ImmutableList.of(
|
| 299 | Pair.of(SetEntityLookTarget.create(EntityTypes.VILLAGER, 8.0F), 2),
|
| 300 | Pair.of(SetEntityLookTarget.create(EntityTypes.PLAYER, 8.0F), 2),
|
| 301 | Pair.of(new DoNothing(30, 60), 8)
|
| 302 | )
|
| 303 | )
|
| 304 | );
|
| 305 | }
|
| 306 |
|
| 307 | private static boolean raidExistsAndActive(final ServerLevel level, final LivingEntity body) {
|
| 308 | Raid currentRaid = level.getRaidAt(body.blockPosition());
|
| 309 | return currentRaid != null && currentRaid.isActive() && !currentRaid.isVictory() && !currentRaid.isLoss();
|
| 310 | }
|
| 311 |
|
| 312 | private static boolean raidExistsAndNotVictory(final ServerLevel level, final LivingEntity body) {
|
| 313 | Raid currentRaid = level.getRaidAt(body.blockPosition());
|
| 314 | return currentRaid != null && currentRaid.isVictory();
|
| 315 | }
|
| 316 | }
|
| 317 |
|