| 1 | package net.minecraft.world.entity.animal.armadillo;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import com.google.common.collect.ImmutableMap;
|
| 5 | import com.mojang.datafixers.util.Pair;
|
| 6 | import java.util.List;
|
| 7 | import java.util.Map;
|
| 8 | import java.util.Set;
|
| 9 | import net.minecraft.server.level.ServerLevel;
|
| 10 | import net.minecraft.sounds.SoundEvents;
|
| 11 | import net.minecraft.tags.DamageTypeTags;
|
| 12 | import net.minecraft.util.TimeUtil;
|
| 13 | import net.minecraft.util.valueproviders.UniformInt;
|
| 14 | import net.minecraft.world.entity.EntityTypes;
|
| 15 | import net.minecraft.world.entity.Mob;
|
| 16 | import net.minecraft.world.entity.ai.ActivityData;
|
| 17 | import net.minecraft.world.entity.ai.behavior.AnimalMakeLove;
|
| 18 | import net.minecraft.world.entity.ai.behavior.AnimalPanic;
|
| 19 | import net.minecraft.world.entity.ai.behavior.BabyFollowAdult;
|
| 20 | import net.minecraft.world.entity.ai.behavior.Behavior;
|
| 21 | import net.minecraft.world.entity.ai.behavior.CountDownCooldownTicks;
|
| 22 | import net.minecraft.world.entity.ai.behavior.DoNothing;
|
| 23 | import net.minecraft.world.entity.ai.behavior.FollowTemptation;
|
| 24 | import net.minecraft.world.entity.ai.behavior.LookAtTargetSink;
|
| 25 | import net.minecraft.world.entity.ai.behavior.MoveToTargetSink;
|
| 26 | import net.minecraft.world.entity.ai.behavior.OneShot;
|
| 27 | import net.minecraft.world.entity.ai.behavior.RandomLookAround;
|
| 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.SetWalkTargetFromLookTarget;
|
| 32 | import net.minecraft.world.entity.ai.behavior.Swim;
|
| 33 | import net.minecraft.world.entity.ai.behavior.declarative.BehaviorBuilder;
|
| 34 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 35 | import net.minecraft.world.entity.ai.memory.MemoryStatus;
|
| 36 | import net.minecraft.world.entity.schedule.Activity;
|
| 37 |
|
| 38 | public class ArmadilloAi {
|
| 39 | private static final float SPEED_MULTIPLIER_WHEN_PANICKING = 2.0F;
|
| 40 | private static final float SPEED_MULTIPLIER_WHEN_IDLING = 1.0F;
|
| 41 | private static final float SPEED_MULTIPLIER_WHEN_TEMPTED = 1.25F;
|
| 42 | private static final float SPEED_MULTIPLIER_WHEN_FOLLOWING_ADULT = 1.25F;
|
| 43 | private static final float SPEED_MULTIPLIER_WHEN_MAKING_LOVE = 1.0F;
|
| 44 | private static final double DEFAULT_CLOSE_ENOUGH_DIST = 2.0;
|
| 45 | private static final double BABY_CLOSE_ENOUGH_DIST = 1.0;
|
| 46 | private static final UniformInt ADULT_FOLLOW_RANGE = UniformInt.of(5, 16);
|
| 47 | private static final OneShot<Armadillo> ARMADILLO_ROLLING_OUT = BehaviorBuilder.create(
|
| 48 | i -> i.group(i.absent(MemoryModuleType.DANGER_DETECTED_RECENTLY)).apply(i, location -> (level, body, timestamp) -> {
|
| 49 | if (body.isScared()) {
|
| 50 | body.rollOut();
|
| 51 | return true;
|
| 52 | } else {
|
| 53 | return false;
|
| 54 | }
|
| 55 | })
|
| 56 | );
|
| 57 |
|
| 58 | protected static List<ActivityData<Armadillo>> getActivities() {
|
| 59 | return List.of(initCoreActivity(), initIdleActivity(), initScaredActivity());
|
| 60 | }
|
| 61 |
|
| 62 | private static ActivityData<Armadillo> initCoreActivity() {
|
| 63 | return ActivityData.create(
|
| 64 | Activity.CORE,
|
| 65 | 0,
|
| 66 | ImmutableList.of(
|
| 67 | new Swim<>(0.8F),
|
| 68 | new ArmadilloAi.ArmadilloPanic(2.0F),
|
| 69 | new LookAtTargetSink(45, 90),
|
| 70 | new MoveToTargetSink() {
|
| 71 | @Override
|
| 72 | protected boolean checkExtraStartConditions(final ServerLevel level, final Mob body) {
|
| 73 | return body instanceof Armadillo armadillo && armadillo.isScared() ? false : super.checkExtraStartConditions(level, body);
|
| 74 | }
|
| 75 | },
|
| 76 | new CountDownCooldownTicks(MemoryModuleType.TEMPTATION_COOLDOWN_TICKS),
|
| 77 | new CountDownCooldownTicks(MemoryModuleType.GAZE_COOLDOWN_TICKS),
|
| 78 | ARMADILLO_ROLLING_OUT
|
| 79 | )
|
| 80 | );
|
| 81 | }
|
| 82 |
|
| 83 | private static ActivityData<Armadillo> initIdleActivity() {
|
| 84 | return ActivityData.create(
|
| 85 | Activity.IDLE,
|
| 86 | ImmutableList.of(
|
| 87 | Pair.of(0, SetEntityLookTargetSometimes.create(EntityTypes.PLAYER, 6.0F, UniformInt.of(30, 60))),
|
| 88 | Pair.of(1, new AnimalMakeLove(EntityTypes.ARMADILLO, 1.0F, 1)),
|
| 89 | Pair.of(
|
| 90 | 2,
|
| 91 | new RunOne<>(
|
| 92 | ImmutableList.of(
|
| 93 | Pair.of(new FollowTemptation(armadillo -> 1.25F, armadillo -> armadillo.isBaby() ? 1.0 : 2.0), 1),
|
| 94 | Pair.of(BabyFollowAdult.create(ADULT_FOLLOW_RANGE, 1.25F), 1)
|
| 95 | )
|
| 96 | )
|
| 97 | ),
|
| 98 | Pair.of(3, new RandomLookAround(UniformInt.of(150, 250), 30.0F, 0.0F, 0.0F)),
|
| 99 | Pair.of(
|
| 100 | 4,
|
| 101 | new RunOne<>(
|
| 102 | ImmutableMap.of(MemoryModuleType.WALK_TARGET, MemoryStatus.VALUE_ABSENT),
|
| 103 | ImmutableList.of(Pair.of(RandomStroll.stroll(1.0F), 1), Pair.of(SetWalkTargetFromLookTarget.create(1.0F, 3), 1), Pair.of(new DoNothing(30, 60), 1))
|
| 104 | )
|
| 105 | )
|
| 106 | )
|
| 107 | );
|
| 108 | }
|
| 109 |
|
| 110 | private static ActivityData<Armadillo> initScaredActivity() {
|
| 111 | return ActivityData.create(
|
| 112 | Activity.PANIC,
|
| 113 | ImmutableList.of(Pair.of(0, new ArmadilloAi.ArmadilloBallUp())),
|
| 114 | Set.of(Pair.of(MemoryModuleType.DANGER_DETECTED_RECENTLY, MemoryStatus.VALUE_PRESENT), Pair.of(MemoryModuleType.IS_PANICKING, MemoryStatus.VALUE_ABSENT))
|
| 115 | );
|
| 116 | }
|
| 117 |
|
| 118 | public static void updateActivity(final Armadillo body) {
|
| 119 | body.getBrain().setActiveActivityToFirstValid(ImmutableList.of(Activity.PANIC, Activity.IDLE));
|
| 120 | }
|
| 121 |
|
| 122 | public static class ArmadilloBallUp extends Behavior<Armadillo> {
|
| 123 | private static final int BALL_UP_STAY_IN_STATE = 5 * TimeUtil.SECONDS_PER_MINUTE * 20;
|
| 124 | private static final int TICKS_DELAY_TO_DETERMINE_IF_DANGER_IS_STILL_AROUND = 5;
|
| 125 | private static final int DANGER_DETECTED_RECENTLY_DANGER_THRESHOLD = 75;
|
| 126 | private int nextPeekTimer = 0;
|
| 127 | private boolean dangerWasAround;
|
| 128 |
|
| 129 | public ArmadilloBallUp() {
|
| 130 | super(Map.of(), BALL_UP_STAY_IN_STATE);
|
| 131 | }
|
| 132 |
|
| 133 | protected void tick(final ServerLevel level, final Armadillo body, final long timestamp) {
|
| 134 | super.tick(level, body, timestamp);
|
| 135 | if (this.nextPeekTimer > 0) {
|
| 136 | this.nextPeekTimer--;
|
| 137 | }
|
| 138 |
|
| 139 | if (body.shouldSwitchToScaredState()) {
|
| 140 | body.switchToState(Armadillo.ArmadilloState.SCARED);
|
| 141 | if (body.onGround()) {
|
| 142 | body.playSound(SoundEvents.ARMADILLO_LAND);
|
| 143 | }
|
| 144 | } else {
|
| 145 | Armadillo.ArmadilloState state = body.getState();
|
| 146 | long dangerTickCounter = body.getBrain().getTimeUntilExpiry(MemoryModuleType.DANGER_DETECTED_RECENTLY);
|
| 147 | boolean dangerIsAround = dangerTickCounter > 75L;
|
| 148 | if (dangerIsAround != this.dangerWasAround) {
|
| 149 | this.nextPeekTimer = this.pickNextPeekTimer(body);
|
| 150 | }
|
| 151 |
|
| 152 | this.dangerWasAround = dangerIsAround;
|
| 153 | if (state == Armadillo.ArmadilloState.SCARED) {
|
| 154 | if (this.nextPeekTimer == 0 && body.onGround() && dangerIsAround) {
|
| 155 | level.broadcastEntityEvent(body, (byte)64);
|
| 156 | this.nextPeekTimer = this.pickNextPeekTimer(body);
|
| 157 | }
|
| 158 |
|
| 159 | if (dangerTickCounter < Armadillo.ArmadilloState.UNROLLING.animationDuration()) {
|
| 160 | body.playSound(SoundEvents.ARMADILLO_UNROLL_START);
|
| 161 | body.switchToState(Armadillo.ArmadilloState.UNROLLING);
|
| 162 | }
|
| 163 | } else if (state == Armadillo.ArmadilloState.UNROLLING && dangerTickCounter > Armadillo.ArmadilloState.UNROLLING.animationDuration()) {
|
| 164 | body.switchToState(Armadillo.ArmadilloState.SCARED);
|
| 165 | }
|
| 166 | }
|
| 167 | }
|
| 168 |
|
| 169 | private int pickNextPeekTimer(final Armadillo body) {
|
| 170 | return Armadillo.ArmadilloState.SCARED.animationDuration() + body.getRandom().nextIntBetweenInclusive(100, 400);
|
| 171 | }
|
| 172 |
|
| 173 | protected boolean checkExtraStartConditions(final ServerLevel level, final Armadillo body) {
|
| 174 | return body.onGround() && !body.isInLiquid();
|
| 175 | }
|
| 176 |
|
| 177 | protected boolean canStillUse(final ServerLevel level, final Armadillo body, final long timestamp) {
|
| 178 | return body.getState().isThreatened();
|
| 179 | }
|
| 180 |
|
| 181 | protected void start(final ServerLevel level, final Armadillo body, final long timestamp) {
|
| 182 | body.rollUp();
|
| 183 | }
|
| 184 |
|
| 185 | protected void stop(final ServerLevel level, final Armadillo body, final long timestamp) {
|
| 186 | if (!body.canStayRolledUp()) {
|
| 187 | body.rollOut();
|
| 188 | }
|
| 189 | }
|
| 190 | }
|
| 191 |
|
| 192 | public static class ArmadilloPanic extends AnimalPanic<Armadillo> {
|
| 193 | public ArmadilloPanic(final float speedMultiplier) {
|
| 194 | super(speedMultiplier, mob -> DamageTypeTags.PANIC_ENVIRONMENTAL_CAUSES);
|
| 195 | }
|
| 196 |
|
| 197 | protected void start(final ServerLevel level, final Armadillo armadillo, final long timestamp) {
|
| 198 | armadillo.rollOut();
|
| 199 | super.start(level, armadillo, timestamp);
|
| 200 | }
|
| 201 | }
|
| 202 | }
|
| 203 |
|