| 1 | package net.minecraft.world.entity.monster.hoglin;
|
| 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.BlockPos;
|
| 8 | import net.minecraft.server.level.ServerLevel;
|
| 9 | import net.minecraft.sounds.SoundEvent;
|
| 10 | import net.minecraft.sounds.SoundEvents;
|
| 11 | import net.minecraft.util.TimeUtil;
|
| 12 | import net.minecraft.util.valueproviders.UniformInt;
|
| 13 | import net.minecraft.world.entity.AgeableMob;
|
| 14 | import net.minecraft.world.entity.EntityTypes;
|
| 15 | import net.minecraft.world.entity.LivingEntity;
|
| 16 | import net.minecraft.world.entity.PathfinderMob;
|
| 17 | import net.minecraft.world.entity.ai.ActivityData;
|
| 18 | import net.minecraft.world.entity.ai.Brain;
|
| 19 | import net.minecraft.world.entity.ai.behavior.AnimalMakeLove;
|
| 20 | import net.minecraft.world.entity.ai.behavior.BabyFollowAdult;
|
| 21 | import net.minecraft.world.entity.ai.behavior.BecomePassiveIfMemoryPresent;
|
| 22 | import net.minecraft.world.entity.ai.behavior.BehaviorUtils;
|
| 23 | import net.minecraft.world.entity.ai.behavior.DoNothing;
|
| 24 | import net.minecraft.world.entity.ai.behavior.EraseMemoryIf;
|
| 25 | import net.minecraft.world.entity.ai.behavior.LookAtTargetSink;
|
| 26 | import net.minecraft.world.entity.ai.behavior.MeleeAttack;
|
| 27 | import net.minecraft.world.entity.ai.behavior.MoveToTargetSink;
|
| 28 | import net.minecraft.world.entity.ai.behavior.RandomStroll;
|
| 29 | import net.minecraft.world.entity.ai.behavior.RunOne;
|
| 30 | import net.minecraft.world.entity.ai.behavior.SetEntityLookTargetSometimes;
|
| 31 | import net.minecraft.world.entity.ai.behavior.SetWalkTargetAwayFrom;
|
| 32 | import net.minecraft.world.entity.ai.behavior.SetWalkTargetFromAttackTargetIfTargetOutOfReach;
|
| 33 | import net.minecraft.world.entity.ai.behavior.SetWalkTargetFromLookTarget;
|
| 34 | import net.minecraft.world.entity.ai.behavior.StartAttacking;
|
| 35 | import net.minecraft.world.entity.ai.behavior.StopAttackingIfTargetInvalid;
|
| 36 | import net.minecraft.world.entity.ai.behavior.declarative.BehaviorBuilder;
|
| 37 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 38 | import net.minecraft.world.entity.ai.sensing.Sensor;
|
| 39 | import net.minecraft.world.entity.schedule.Activity;
|
| 40 |
|
| 41 | public class HoglinAi {
|
| 42 | public static final int REPELLENT_DETECTION_RANGE_HORIZONTAL = 8;
|
| 43 | public static final int REPELLENT_DETECTION_RANGE_VERTICAL = 4;
|
| 44 | private static final UniformInt RETREAT_DURATION = TimeUtil.rangeOfSeconds(5, 20);
|
| 45 | private static final int ATTACK_DURATION = 200;
|
| 46 | private static final int DESIRED_DISTANCE_FROM_PIGLIN_WHEN_IDLING = 8;
|
| 47 | private static final int DESIRED_DISTANCE_FROM_PIGLIN_WHEN_RETREATING = 15;
|
| 48 | private static final int ATTACK_INTERVAL = 40;
|
| 49 | private static final int BABY_ATTACK_INTERVAL = 15;
|
| 50 | private static final int REPELLENT_PACIFY_TIME = 200;
|
| 51 | private static final UniformInt ADULT_FOLLOW_RANGE = UniformInt.of(5, 16);
|
| 52 | private static final float SPEED_MULTIPLIER_WHEN_AVOIDING_REPELLENT = 1.0F;
|
| 53 | private static final float SPEED_MULTIPLIER_WHEN_RETREATING = 1.3F;
|
| 54 | private static final float SPEED_MULTIPLIER_WHEN_MAKING_LOVE = 0.6F;
|
| 55 | private static final float SPEED_MULTIPLIER_WHEN_IDLING = 0.4F;
|
| 56 | private static final float SPEED_MULTIPLIER_WHEN_FOLLOWING_ADULT = 0.6F;
|
| 57 |
|
| 58 | protected static List<ActivityData<Hoglin>> getActivities() {
|
| 59 | return List.of(initCoreActivity(), initIdleActivity(), initFightActivity(), initRetreatActivity());
|
| 60 | }
|
| 61 |
|
| 62 | private static ActivityData<Hoglin> initCoreActivity() {
|
| 63 | return ActivityData.create(Activity.CORE, 0, ImmutableList.of(new LookAtTargetSink(45, 90), new MoveToTargetSink()));
|
| 64 | }
|
| 65 |
|
| 66 | private static ActivityData<Hoglin> initIdleActivity() {
|
| 67 | return ActivityData.create(
|
| 68 | Activity.IDLE,
|
| 69 | 10,
|
| 70 | ImmutableList.of(
|
| 71 | BecomePassiveIfMemoryPresent.create(MemoryModuleType.NEAREST_REPELLENT, 200),
|
| 72 | new AnimalMakeLove(EntityTypes.HOGLIN, 0.6F, 2),
|
| 73 | SetWalkTargetAwayFrom.pos(MemoryModuleType.NEAREST_REPELLENT, 1.0F, 8, true),
|
| 74 | StartAttacking.create(HoglinAi::findNearestValidAttackTarget),
|
| 75 | BehaviorBuilder.triggerIf(Hoglin::isAdult, SetWalkTargetAwayFrom.entity(MemoryModuleType.NEAREST_VISIBLE_ADULT_PIGLIN, 0.4F, 8, false)),
|
| 76 | SetEntityLookTargetSometimes.create(8.0F, UniformInt.of(30, 60)),
|
| 77 | BabyFollowAdult.create(ADULT_FOLLOW_RANGE, 0.6F),
|
| 78 | createIdleMovementBehaviors()
|
| 79 | )
|
| 80 | );
|
| 81 | }
|
| 82 |
|
| 83 | private static ActivityData<Hoglin> initFightActivity() {
|
| 84 | return ActivityData.create(
|
| 85 | Activity.FIGHT,
|
| 86 | 10,
|
| 87 | ImmutableList.of(
|
| 88 | BecomePassiveIfMemoryPresent.create(MemoryModuleType.NEAREST_REPELLENT, 200),
|
| 89 | new AnimalMakeLove(EntityTypes.HOGLIN, 0.6F, 2),
|
| 90 | SetWalkTargetFromAttackTargetIfTargetOutOfReach.create(1.0F),
|
| 91 | BehaviorBuilder.triggerIf(Hoglin::isAdult, MeleeAttack.create(40)),
|
| 92 | BehaviorBuilder.triggerIf(AgeableMob::isBaby, MeleeAttack.create(15)),
|
| 93 | StopAttackingIfTargetInvalid.create(),
|
| 94 | EraseMemoryIf.create(HoglinAi::isBreeding, MemoryModuleType.ATTACK_TARGET)
|
| 95 | ),
|
| 96 | MemoryModuleType.ATTACK_TARGET
|
| 97 | );
|
| 98 | }
|
| 99 |
|
| 100 | private static ActivityData<Hoglin> initRetreatActivity() {
|
| 101 | return ActivityData.create(
|
| 102 | Activity.AVOID,
|
| 103 | 10,
|
| 104 | ImmutableList.of(
|
| 105 | SetWalkTargetAwayFrom.entity(MemoryModuleType.AVOID_TARGET, 1.3F, 15, false),
|
| 106 | createIdleMovementBehaviors(),
|
| 107 | SetEntityLookTargetSometimes.create(8.0F, UniformInt.of(30, 60)),
|
| 108 | EraseMemoryIf.<PathfinderMob>create(HoglinAi::wantsToStopFleeing, MemoryModuleType.AVOID_TARGET)
|
| 109 | ),
|
| 110 | MemoryModuleType.AVOID_TARGET
|
| 111 | );
|
| 112 | }
|
| 113 |
|
| 114 | private static RunOne<Hoglin> createIdleMovementBehaviors() {
|
| 115 | return new RunOne<>(
|
| 116 | ImmutableList.of(Pair.of(RandomStroll.stroll(0.4F), 2), Pair.of(SetWalkTargetFromLookTarget.create(0.4F, 3), 2), Pair.of(new DoNothing(30, 60), 1))
|
| 117 | );
|
| 118 | }
|
| 119 |
|
| 120 | protected static void updateActivity(final Hoglin body) {
|
| 121 | Brain<Hoglin> brain = body.getBrain();
|
| 122 | Activity oldActivity = brain.getActiveNonCoreActivity().orElse(null);
|
| 123 | brain.setActiveActivityToFirstValid(ImmutableList.of(Activity.FIGHT, Activity.AVOID, Activity.IDLE));
|
| 124 | Activity newActivity = brain.getActiveNonCoreActivity().orElse(null);
|
| 125 | if (oldActivity != newActivity) {
|
| 126 | getSoundForCurrentActivity(body).ifPresent(body::makeSound);
|
| 127 | }
|
| 128 |
|
| 129 | body.setAggressive(brain.hasMemoryValue(MemoryModuleType.ATTACK_TARGET));
|
| 130 | }
|
| 131 |
|
| 132 | protected static void onHitTarget(final Hoglin attackerBody, final LivingEntity target) {
|
| 133 | if (!attackerBody.isBaby()) {
|
| 134 | if (target.is(EntityTypes.PIGLIN) && piglinsOutnumberHoglins(attackerBody)) {
|
| 135 | setAvoidTarget(attackerBody, target);
|
| 136 | broadcastRetreat(attackerBody, target);
|
| 137 | } else {
|
| 138 | broadcastAttackTarget(attackerBody, target);
|
| 139 | }
|
| 140 | }
|
| 141 | }
|
| 142 |
|
| 143 | private static void broadcastRetreat(final Hoglin body, final LivingEntity target) {
|
| 144 | getVisibleAdultHoglins(body).forEach(hoglin -> retreatFromNearestTarget(hoglin, target));
|
| 145 | }
|
| 146 |
|
| 147 | private static void retreatFromNearestTarget(final Hoglin body, final LivingEntity newAvoidTarget) {
|
| 148 | LivingEntity nearest = newAvoidTarget;
|
| 149 | Brain<Hoglin> brain = body.getBrain();
|
| 150 | nearest = BehaviorUtils.getNearestTarget(body, brain.getMemory(MemoryModuleType.AVOID_TARGET), nearest);
|
| 151 | nearest = BehaviorUtils.getNearestTarget(body, brain.getMemory(MemoryModuleType.ATTACK_TARGET), nearest);
|
| 152 | setAvoidTarget(body, nearest);
|
| 153 | }
|
| 154 |
|
| 155 | private static void setAvoidTarget(final Hoglin body, final LivingEntity avoidTarget) {
|
| 156 | body.getBrain().eraseMemory(MemoryModuleType.ATTACK_TARGET);
|
| 157 | body.getBrain().eraseMemory(MemoryModuleType.WALK_TARGET);
|
| 158 | body.getBrain().setMemoryWithExpiry(MemoryModuleType.AVOID_TARGET, avoidTarget, RETREAT_DURATION.sample(body.level().getRandom()));
|
| 159 | }
|
| 160 |
|
| 161 | private static Optional<? extends LivingEntity> findNearestValidAttackTarget(final ServerLevel level, final Hoglin body) {
|
| 162 | return !isPacified(body) && !isBreeding(body) ? body.getBrain().getMemory(MemoryModuleType.NEAREST_VISIBLE_ATTACKABLE_PLAYER) : Optional.empty();
|
| 163 | }
|
| 164 |
|
| 165 | static boolean isPosNearNearestRepellent(final Hoglin body, final BlockPos pos) {
|
| 166 | Optional<BlockPos> repellentPos = body.getBrain().getMemory(MemoryModuleType.NEAREST_REPELLENT);
|
| 167 | return repellentPos.isPresent() && repellentPos.get().closerThan(pos, 8.0);
|
| 168 | }
|
| 169 |
|
| 170 | private static boolean wantsToStopFleeing(final Hoglin body) {
|
| 171 | return body.isAdult() && !piglinsOutnumberHoglins(body);
|
| 172 | }
|
| 173 |
|
| 174 | private static boolean piglinsOutnumberHoglins(final Hoglin body) {
|
| 175 | if (body.isBaby()) {
|
| 176 | return false;
|
| 177 | }
|
| 178 |
|
| 179 | int piglinCount = body.getBrain().getMemory(MemoryModuleType.VISIBLE_ADULT_PIGLIN_COUNT).orElse(0);
|
| 180 | int hoglinCount = body.getBrain().getMemory(MemoryModuleType.VISIBLE_ADULT_HOGLIN_COUNT).orElse(0) + 1;
|
| 181 | return piglinCount > hoglinCount;
|
| 182 | }
|
| 183 |
|
| 184 | protected static void wasHurtBy(final ServerLevel level, final Hoglin body, final LivingEntity attacker) {
|
| 185 | Brain<Hoglin> brain = body.getBrain();
|
| 186 | brain.eraseMemory(MemoryModuleType.PACIFIED);
|
| 187 | brain.eraseMemory(MemoryModuleType.BREED_TARGET);
|
| 188 | if (body.isBaby()) {
|
| 189 | retreatFromNearestTarget(body, attacker);
|
| 190 | } else {
|
| 191 | maybeRetaliate(level, body, attacker);
|
| 192 | }
|
| 193 | }
|
| 194 |
|
| 195 | private static void maybeRetaliate(final ServerLevel level, final Hoglin body, final LivingEntity attacker) {
|
| 196 | if (!body.getBrain().isActive(Activity.AVOID) || !attacker.is(EntityTypes.PIGLIN)) {
|
| 197 | if (!attacker.is(EntityTypes.HOGLIN)) {
|
| 198 | if (!BehaviorUtils.isOtherTargetMuchFurtherAwayThanCurrentAttackTarget(body, attacker, 4.0)) {
|
| 199 | if (Sensor.isEntityAttackable(level, body, attacker)) {
|
| 200 | setAttackTarget(body, attacker);
|
| 201 | broadcastAttackTarget(body, attacker);
|
| 202 | }
|
| 203 | }
|
| 204 | }
|
| 205 | }
|
| 206 | }
|
| 207 |
|
| 208 | private static void setAttackTarget(final Hoglin body, final LivingEntity target) {
|
| 209 | Brain<Hoglin> brain = body.getBrain();
|
| 210 | brain.eraseMemory(MemoryModuleType.CANT_REACH_WALK_TARGET_SINCE);
|
| 211 | brain.eraseMemory(MemoryModuleType.BREED_TARGET);
|
| 212 | brain.setMemoryWithExpiry(MemoryModuleType.ATTACK_TARGET, target, 200L);
|
| 213 | }
|
| 214 |
|
| 215 | private static void broadcastAttackTarget(final Hoglin body, final LivingEntity target) {
|
| 216 | getVisibleAdultHoglins(body).forEach(hoglin -> setAttackTargetIfCloserThanCurrent(hoglin, target));
|
| 217 | }
|
| 218 |
|
| 219 | private static void setAttackTargetIfCloserThanCurrent(final Hoglin body, final LivingEntity newTarget) {
|
| 220 | if (!isPacified(body)) {
|
| 221 | Optional<LivingEntity> currentTarget = body.getBrain().getMemory(MemoryModuleType.ATTACK_TARGET);
|
| 222 | LivingEntity nearest = BehaviorUtils.getNearestTarget(body, currentTarget, newTarget);
|
| 223 | setAttackTarget(body, nearest);
|
| 224 | }
|
| 225 | }
|
| 226 |
|
| 227 | public static Optional<SoundEvent> getSoundForCurrentActivity(final Hoglin body) {
|
| 228 | return body.getBrain().getActiveNonCoreActivity().map(activity -> getSoundForActivity(body, activity));
|
| 229 | }
|
| 230 |
|
| 231 | private static SoundEvent getSoundForActivity(final Hoglin body, final Activity activity) {
|
| 232 | if (activity == Activity.AVOID || body.isConverting()) {
|
| 233 | return SoundEvents.HOGLIN_RETREAT;
|
| 234 | } else if (activity == Activity.FIGHT) {
|
| 235 | return SoundEvents.HOGLIN_ANGRY;
|
| 236 | } else {
|
| 237 | return isNearRepellent(body) ? SoundEvents.HOGLIN_RETREAT : SoundEvents.HOGLIN_AMBIENT;
|
| 238 | }
|
| 239 | }
|
| 240 |
|
| 241 | private static List<Hoglin> getVisibleAdultHoglins(final Hoglin body) {
|
| 242 | return body.getBrain().getMemory(MemoryModuleType.NEAREST_VISIBLE_ADULT_HOGLINS).orElse(ImmutableList.of());
|
| 243 | }
|
| 244 |
|
| 245 | private static boolean isNearRepellent(final Hoglin body) {
|
| 246 | return body.getBrain().hasMemoryValue(MemoryModuleType.NEAREST_REPELLENT);
|
| 247 | }
|
| 248 |
|
| 249 | private static boolean isBreeding(final Hoglin body) {
|
| 250 | return body.getBrain().hasMemoryValue(MemoryModuleType.BREED_TARGET);
|
| 251 | }
|
| 252 |
|
| 253 | protected static boolean isPacified(final Hoglin body) {
|
| 254 | return body.getBrain().hasMemoryValue(MemoryModuleType.PACIFIED);
|
| 255 | }
|
| 256 | }
|
| 257 |
|