| 1 | package net.minecraft.world.entity.monster.piglin;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import com.mojang.datafixers.util.Pair;
|
| 5 | import java.util.List;
|
| 6 | import java.util.Optional;
|
| 7 | import net.minecraft.core.GlobalPos;
|
| 8 | import net.minecraft.server.level.ServerLevel;
|
| 9 | import net.minecraft.world.entity.EntityTypes;
|
| 10 | import net.minecraft.world.entity.LivingEntity;
|
| 11 | import net.minecraft.world.entity.ai.ActivityData;
|
| 12 | import net.minecraft.world.entity.ai.Brain;
|
| 13 | import net.minecraft.world.entity.ai.behavior.BehaviorUtils;
|
| 14 | import net.minecraft.world.entity.ai.behavior.DoNothing;
|
| 15 | import net.minecraft.world.entity.ai.behavior.InteractWith;
|
| 16 | import net.minecraft.world.entity.ai.behavior.InteractWithDoor;
|
| 17 | import net.minecraft.world.entity.ai.behavior.LookAtTargetSink;
|
| 18 | import net.minecraft.world.entity.ai.behavior.MeleeAttack;
|
| 19 | import net.minecraft.world.entity.ai.behavior.MoveToTargetSink;
|
| 20 | import net.minecraft.world.entity.ai.behavior.RandomStroll;
|
| 21 | import net.minecraft.world.entity.ai.behavior.RunOne;
|
| 22 | import net.minecraft.world.entity.ai.behavior.SetEntityLookTarget;
|
| 23 | import net.minecraft.world.entity.ai.behavior.SetLookAndInteract;
|
| 24 | import net.minecraft.world.entity.ai.behavior.SetWalkTargetFromAttackTargetIfTargetOutOfReach;
|
| 25 | import net.minecraft.world.entity.ai.behavior.StartAttacking;
|
| 26 | import net.minecraft.world.entity.ai.behavior.StopAttackingIfTargetInvalid;
|
| 27 | import net.minecraft.world.entity.ai.behavior.StopBeingAngryIfTargetDead;
|
| 28 | import net.minecraft.world.entity.ai.behavior.StrollAroundPoi;
|
| 29 | import net.minecraft.world.entity.ai.behavior.StrollToPoi;
|
| 30 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 31 | import net.minecraft.world.entity.ai.sensing.Sensor;
|
| 32 | import net.minecraft.world.entity.schedule.Activity;
|
| 33 |
|
| 34 | public class PiglinBruteAi {
|
| 35 | private static final int ANGER_DURATION = 600;
|
| 36 | private static final int MELEE_ATTACK_COOLDOWN = 20;
|
| 37 | private static final double ACTIVITY_SOUND_LIKELIHOOD_PER_TICK = 0.0125;
|
| 38 | private static final int MAX_LOOK_DIST = 8;
|
| 39 | private static final int INTERACTION_RANGE = 8;
|
| 40 | private static final float SPEED_MULTIPLIER_WHEN_IDLING = 0.6F;
|
| 41 | private static final int HOME_CLOSE_ENOUGH_DISTANCE = 2;
|
| 42 | private static final int HOME_TOO_FAR_DISTANCE = 100;
|
| 43 | private static final int HOME_STROLL_AROUND_DISTANCE = 5;
|
| 44 |
|
| 45 | public static List<ActivityData<PiglinBrute>> getActivities(final PiglinBrute piglin) {
|
| 46 | return List.of(initCoreActivity(), initIdleActivity(), initFightActivity(piglin));
|
| 47 | }
|
| 48 |
|
| 49 | protected static void initMemories(final PiglinBrute body) {
|
| 50 | GlobalPos currentGlobalPos = GlobalPos.of(body.level().dimension(), body.blockPosition());
|
| 51 | body.getBrain().setMemory(MemoryModuleType.HOME, currentGlobalPos);
|
| 52 | }
|
| 53 |
|
| 54 | private static ActivityData<PiglinBrute> initCoreActivity() {
|
| 55 | return ActivityData.create(
|
| 56 | Activity.CORE, 0, ImmutableList.of(new LookAtTargetSink(45, 90), new MoveToTargetSink(), InteractWithDoor.create(), StopBeingAngryIfTargetDead.create())
|
| 57 | );
|
| 58 | }
|
| 59 |
|
| 60 | private static ActivityData<PiglinBrute> initIdleActivity() {
|
| 61 | return ActivityData.create(
|
| 62 | Activity.IDLE,
|
| 63 | 10,
|
| 64 | ImmutableList.of(
|
| 65 | StartAttacking.<PiglinBrute>create(PiglinBruteAi::findNearestValidAttackTarget),
|
| 66 | createIdleLookBehaviors(),
|
| 67 | createIdleMovementBehaviors(),
|
| 68 | SetLookAndInteract.create(EntityTypes.PLAYER, 4)
|
| 69 | )
|
| 70 | );
|
| 71 | }
|
| 72 |
|
| 73 | private static ActivityData<PiglinBrute> initFightActivity(final PiglinBrute body) {
|
| 74 | return ActivityData.create(
|
| 75 | Activity.FIGHT,
|
| 76 | 10,
|
| 77 | ImmutableList.of(
|
| 78 | StopAttackingIfTargetInvalid.create((level, target) -> !isNearestValidAttackTarget(level, body, target)),
|
| 79 | SetWalkTargetFromAttackTargetIfTargetOutOfReach.create(1.0F),
|
| 80 | MeleeAttack.create(20)
|
| 81 | ),
|
| 82 | MemoryModuleType.ATTACK_TARGET
|
| 83 | );
|
| 84 | }
|
| 85 |
|
| 86 | private static RunOne<PiglinBrute> createIdleLookBehaviors() {
|
| 87 | return new RunOne<>(
|
| 88 | ImmutableList.of(
|
| 89 | Pair.of(SetEntityLookTarget.create(EntityTypes.PLAYER, 8.0F), 1),
|
| 90 | Pair.of(SetEntityLookTarget.create(EntityTypes.PIGLIN, 8.0F), 1),
|
| 91 | Pair.of(SetEntityLookTarget.create(EntityTypes.PIGLIN_BRUTE, 8.0F), 1),
|
| 92 | Pair.of(SetEntityLookTarget.create(8.0F), 1),
|
| 93 | Pair.of(new DoNothing(30, 60), 1)
|
| 94 | )
|
| 95 | );
|
| 96 | }
|
| 97 |
|
| 98 | private static RunOne<PiglinBrute> createIdleMovementBehaviors() {
|
| 99 | return new RunOne<>(
|
| 100 | ImmutableList.of(
|
| 101 | Pair.of(RandomStroll.stroll(0.6F), 2),
|
| 102 | Pair.of(InteractWith.of(EntityTypes.PIGLIN, 8, MemoryModuleType.INTERACTION_TARGET, 0.6F, 2), 2),
|
| 103 | Pair.of(InteractWith.of(EntityTypes.PIGLIN_BRUTE, 8, MemoryModuleType.INTERACTION_TARGET, 0.6F, 2), 2),
|
| 104 | Pair.of(StrollToPoi.create(MemoryModuleType.HOME, 0.6F, 2, 100), 2),
|
| 105 | Pair.of(StrollAroundPoi.create(MemoryModuleType.HOME, 0.6F, 5), 2),
|
| 106 | Pair.of(new DoNothing(30, 60), 1)
|
| 107 | )
|
| 108 | );
|
| 109 | }
|
| 110 |
|
| 111 | protected static void updateActivity(final PiglinBrute body) {
|
| 112 | Brain<PiglinBrute> brain = body.getBrain();
|
| 113 | Activity oldActivity = brain.getActiveNonCoreActivity().orElse(null);
|
| 114 | brain.setActiveActivityToFirstValid(ImmutableList.of(Activity.FIGHT, Activity.IDLE));
|
| 115 | Activity newActivity = brain.getActiveNonCoreActivity().orElse(null);
|
| 116 | if (oldActivity != newActivity) {
|
| 117 | playActivitySound(body);
|
| 118 | }
|
| 119 |
|
| 120 | body.setAggressive(brain.hasMemoryValue(MemoryModuleType.ATTACK_TARGET));
|
| 121 | }
|
| 122 |
|
| 123 | private static boolean isNearestValidAttackTarget(final ServerLevel level, final AbstractPiglin body, final LivingEntity target) {
|
| 124 | return findNearestValidAttackTarget(level, body).filter(nearestValidTarget -> nearestValidTarget == target).isPresent();
|
| 125 | }
|
| 126 |
|
| 127 | private static Optional<? extends LivingEntity> findNearestValidAttackTarget(final ServerLevel level, final AbstractPiglin body) {
|
| 128 | Optional<LivingEntity> angryAt = BehaviorUtils.getLivingEntityFromUUIDMemory(body, MemoryModuleType.ANGRY_AT);
|
| 129 | if (angryAt.isPresent() && Sensor.isEntityAttackableIgnoringLineOfSight(level, body, angryAt.get())) {
|
| 130 | return angryAt;
|
| 131 | }
|
| 132 |
|
| 133 | Optional<? extends LivingEntity> player = body.getBrain().getMemory(MemoryModuleType.NEAREST_VISIBLE_ATTACKABLE_PLAYER);
|
| 134 | return player.isPresent() ? player : body.getBrain().getMemory(MemoryModuleType.NEAREST_VISIBLE_NEMESIS);
|
| 135 | }
|
| 136 |
|
| 137 | protected static void wasHurtBy(final ServerLevel level, final PiglinBrute body, final LivingEntity attacker) {
|
| 138 | if (!(attacker instanceof AbstractPiglin)) {
|
| 139 | PiglinAi.maybeRetaliate(level, body, attacker);
|
| 140 | }
|
| 141 | }
|
| 142 |
|
| 143 | protected static void setAngerTarget(final PiglinBrute body, final LivingEntity target) {
|
| 144 | body.getBrain().eraseMemory(MemoryModuleType.CANT_REACH_WALK_TARGET_SINCE);
|
| 145 | body.getBrain().setMemoryWithExpiry(MemoryModuleType.ANGRY_AT, target.getUUID(), 600L);
|
| 146 | }
|
| 147 |
|
| 148 | protected static void maybePlayActivitySound(final PiglinBrute body) {
|
| 149 | if (body.level().getRandom().nextFloat() < 0.0125) {
|
| 150 | playActivitySound(body);
|
| 151 | }
|
| 152 | }
|
| 153 |
|
| 154 | private static void playActivitySound(final PiglinBrute body) {
|
| 155 | body.getBrain().getActiveNonCoreActivity().ifPresent(activity -> {
|
| 156 | if (activity == Activity.FIGHT) {
|
| 157 | body.playAngrySound();
|
| 158 | }
|
| 159 | });
|
| 160 | }
|
| 161 | }
|
| 162 |
|