| 1 | package net.minecraft.world.entity.animal.sniffer;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import com.mojang.datafixers.util.Pair;
|
| 5 | import com.mojang.logging.LogUtils;
|
| 6 | import java.util.List;
|
| 7 | import java.util.Map;
|
| 8 | import java.util.Optional;
|
| 9 | import java.util.Set;
|
| 10 | import java.util.function.Function;
|
| 11 | import net.minecraft.core.BlockPos;
|
| 12 | import net.minecraft.server.level.ServerLevel;
|
| 13 | import net.minecraft.util.Unit;
|
| 14 | import net.minecraft.world.entity.EntityType;
|
| 15 | import net.minecraft.world.entity.EntityTypes;
|
| 16 | import net.minecraft.world.entity.LivingEntity;
|
| 17 | import net.minecraft.world.entity.PathfinderMob;
|
| 18 | import net.minecraft.world.entity.ai.ActivityData;
|
| 19 | import net.minecraft.world.entity.ai.behavior.AnimalMakeLove;
|
| 20 | import net.minecraft.world.entity.ai.behavior.AnimalPanic;
|
| 21 | import net.minecraft.world.entity.ai.behavior.Behavior;
|
| 22 | import net.minecraft.world.entity.ai.behavior.CountDownCooldownTicks;
|
| 23 | import net.minecraft.world.entity.ai.behavior.DoNothing;
|
| 24 | import net.minecraft.world.entity.ai.behavior.FollowTemptation;
|
| 25 | import net.minecraft.world.entity.ai.behavior.LookAtTargetSink;
|
| 26 | import net.minecraft.world.entity.ai.behavior.MoveToTargetSink;
|
| 27 | import net.minecraft.world.entity.ai.behavior.PositionTracker;
|
| 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.SetEntityLookTarget;
|
| 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.memory.MemoryModuleType;
|
| 34 | import net.minecraft.world.entity.ai.memory.MemoryStatus;
|
| 35 | import net.minecraft.world.entity.ai.memory.WalkTarget;
|
| 36 | import net.minecraft.world.entity.animal.Animal;
|
| 37 | import net.minecraft.world.entity.schedule.Activity;
|
| 38 | import org.slf4j.Logger;
|
| 39 |
|
| 40 | public class SnifferAi {
|
| 41 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 42 | private static final int MAX_LOOK_DISTANCE = 6;
|
| 43 | private static final int SNIFFING_COOLDOWN_TICKS = 9600;
|
| 44 | private static final float SPEED_MULTIPLIER_WHEN_IDLING = 1.0F;
|
| 45 | private static final float SPEED_MULTIPLIER_WHEN_PANICKING = 2.0F;
|
| 46 | private static final float SPEED_MULTIPLIER_WHEN_SNIFFING = 1.25F;
|
| 47 | private static final float SPEED_MULTIPLIER_WHEN_TEMPTED = 1.25F;
|
| 48 |
|
| 49 | public static List<ActivityData<Sniffer>> getActivities() {
|
| 50 | return List.of(initCoreActivity(), initIdleActivity(), initSniffingActivity(), initDigActivity());
|
| 51 | }
|
| 52 |
|
| 53 | private static Sniffer resetSniffing(final Sniffer body) {
|
| 54 | body.getBrain().eraseMemory(MemoryModuleType.SNIFFER_DIGGING);
|
| 55 | body.getBrain().eraseMemory(MemoryModuleType.SNIFFER_SNIFFING_TARGET);
|
| 56 | return body.transitionTo(Sniffer.State.IDLING);
|
| 57 | }
|
| 58 |
|
| 59 | private static ActivityData<Sniffer> initCoreActivity() {
|
| 60 | return ActivityData.create(Activity.CORE, 0, ImmutableList.of(new Swim<>(0.8F), new AnimalPanic<Sniffer>(2.0F) {
|
| 61 | protected void start(final ServerLevel level, final Sniffer body, final long timestamp) {
|
| 62 | SnifferAi.resetSniffing(body);
|
| 63 | super.start(level, body, timestamp);
|
| 64 | }
|
| 65 | }, new MoveToTargetSink(500, 700), new CountDownCooldownTicks(MemoryModuleType.TEMPTATION_COOLDOWN_TICKS)));
|
| 66 | }
|
| 67 |
|
| 68 | private static ActivityData<Sniffer> initSniffingActivity() {
|
| 69 | return ActivityData.create(
|
| 70 | Activity.SNIFF,
|
| 71 | ImmutableList.of(Pair.of(0, new SnifferAi.Searching())),
|
| 72 | Set.of(
|
| 73 | Pair.of(MemoryModuleType.IS_PANICKING, MemoryStatus.VALUE_ABSENT),
|
| 74 | Pair.of(MemoryModuleType.SNIFFER_SNIFFING_TARGET, MemoryStatus.VALUE_PRESENT),
|
| 75 | Pair.of(MemoryModuleType.WALK_TARGET, MemoryStatus.VALUE_PRESENT)
|
| 76 | )
|
| 77 | );
|
| 78 | }
|
| 79 |
|
| 80 | private static ActivityData<Sniffer> initDigActivity() {
|
| 81 | return ActivityData.create(
|
| 82 | Activity.DIG,
|
| 83 | ImmutableList.of(Pair.of(0, new SnifferAi.Digging(160, 180)), Pair.of(0, new SnifferAi.FinishedDigging(40))),
|
| 84 | Set.of(
|
| 85 | Pair.of(MemoryModuleType.IS_PANICKING, MemoryStatus.VALUE_ABSENT),
|
| 86 | Pair.of(MemoryModuleType.WALK_TARGET, MemoryStatus.VALUE_ABSENT),
|
| 87 | Pair.of(MemoryModuleType.SNIFFER_DIGGING, MemoryStatus.VALUE_PRESENT)
|
| 88 | )
|
| 89 | );
|
| 90 | }
|
| 91 |
|
| 92 | private static ActivityData<Sniffer> initIdleActivity() {
|
| 93 | return ActivityData.create(
|
| 94 | Activity.IDLE,
|
| 95 | ImmutableList.of(
|
| 96 | Pair.of(0, new AnimalMakeLove(EntityTypes.SNIFFER) {
|
| 97 | @Override
|
| 98 | protected void start(final ServerLevel level, final Animal body, final long timestamp) {
|
| 99 | SnifferAi.resetSniffing((Sniffer)body);
|
| 100 | super.start(level, body, timestamp);
|
| 101 | }
|
| 102 | }),
|
| 103 | Pair.of(1, new FollowTemptation(sniffer -> 1.25F, sniffer -> sniffer.isBaby() ? 2.5 : 3.5) {
|
| 104 | protected void start(final ServerLevel level, final PathfinderMob body, final long timestamp) {
|
| 105 | SnifferAi.resetSniffing((Sniffer)body);
|
| 106 | super.start(level, body, timestamp);
|
| 107 | }
|
| 108 | }),
|
| 109 | Pair.of(2, new LookAtTargetSink(45, 90)),
|
| 110 | Pair.of(3, new SnifferAi.FeelingHappy(40, 100)),
|
| 111 | Pair.of(
|
| 112 | 4,
|
| 113 | new RunOne<>(
|
| 114 | ImmutableList.of(
|
| 115 | Pair.of(SetWalkTargetFromLookTarget.create(1.0F, 3), 2),
|
| 116 | Pair.of(new SnifferAi.Scenting(40, 80), 1),
|
| 117 | Pair.of(new SnifferAi.Sniffing(40, 80), 1),
|
| 118 | Pair.of(SetEntityLookTarget.create(EntityTypes.PLAYER, 6.0F), 1),
|
| 119 | Pair.of(RandomStroll.stroll(1.0F), 1),
|
| 120 | Pair.of(new DoNothing(5, 20), 2)
|
| 121 | )
|
| 122 | )
|
| 123 | )
|
| 124 | ),
|
| 125 | Set.of(Pair.of(MemoryModuleType.SNIFFER_DIGGING, MemoryStatus.VALUE_ABSENT))
|
| 126 | );
|
| 127 | }
|
| 128 |
|
| 129 | public static void updateActivity(final Sniffer body) {
|
| 130 | body.getBrain().setActiveActivityToFirstValid(ImmutableList.of(Activity.DIG, Activity.SNIFF, Activity.IDLE));
|
| 131 | }
|
| 132 |
|
| 133 | private static class Digging extends Behavior<Sniffer> {
|
| 134 | private Digging(final int min, final int max) {
|
| 135 | super(
|
| 136 | Map.of(
|
| 137 | MemoryModuleType.IS_PANICKING,
|
| 138 | MemoryStatus.VALUE_ABSENT,
|
| 139 | MemoryModuleType.WALK_TARGET,
|
| 140 | MemoryStatus.VALUE_ABSENT,
|
| 141 | MemoryModuleType.SNIFFER_DIGGING,
|
| 142 | MemoryStatus.VALUE_PRESENT,
|
| 143 | MemoryModuleType.SNIFF_COOLDOWN,
|
| 144 | MemoryStatus.VALUE_ABSENT
|
| 145 | ),
|
| 146 | min,
|
| 147 | max
|
| 148 | );
|
| 149 | }
|
| 150 |
|
| 151 | protected boolean checkExtraStartConditions(final ServerLevel level, final Sniffer sniffer) {
|
| 152 | return sniffer.canSniff();
|
| 153 | }
|
| 154 |
|
| 155 | protected boolean canStillUse(final ServerLevel level, final Sniffer sniffer, final long timestamp) {
|
| 156 | return sniffer.getBrain().getMemory(MemoryModuleType.SNIFFER_DIGGING).isPresent() && sniffer.canDig() && !sniffer.isInLove();
|
| 157 | }
|
| 158 |
|
| 159 | protected void start(final ServerLevel level, final Sniffer sniffer, final long timestamp) {
|
| 160 | sniffer.transitionTo(Sniffer.State.DIGGING);
|
| 161 | }
|
| 162 |
|
| 163 | protected void stop(final ServerLevel level, final Sniffer sniffer, final long timestamp) {
|
| 164 | boolean finished = this.timedOut(timestamp);
|
| 165 | if (finished) {
|
| 166 | sniffer.getBrain().setMemoryWithExpiry(MemoryModuleType.SNIFF_COOLDOWN, Unit.INSTANCE, 9600L);
|
| 167 | } else {
|
| 168 | SnifferAi.resetSniffing(sniffer);
|
| 169 | }
|
| 170 | }
|
| 171 | }
|
| 172 |
|
| 173 | private static class FeelingHappy extends Behavior<Sniffer> {
|
| 174 | private FeelingHappy(final int min, final int max) {
|
| 175 | super(Map.of(MemoryModuleType.SNIFFER_HAPPY, MemoryStatus.VALUE_PRESENT), min, max);
|
| 176 | }
|
| 177 |
|
| 178 | protected boolean canStillUse(final ServerLevel level, final Sniffer sniffer, final long timestamp) {
|
| 179 | return true;
|
| 180 | }
|
| 181 |
|
| 182 | protected void start(final ServerLevel level, final Sniffer sniffer, final long timestamp) {
|
| 183 | sniffer.transitionTo(Sniffer.State.FEELING_HAPPY);
|
| 184 | }
|
| 185 |
|
| 186 | protected void stop(final ServerLevel level, final Sniffer sniffer, final long timestamp) {
|
| 187 | sniffer.transitionTo(Sniffer.State.IDLING);
|
| 188 | sniffer.getBrain().eraseMemory(MemoryModuleType.SNIFFER_HAPPY);
|
| 189 | }
|
| 190 | }
|
| 191 |
|
| 192 | private static class FinishedDigging extends Behavior<Sniffer> {
|
| 193 | private FinishedDigging(final int duration) {
|
| 194 | super(
|
| 195 | Map.of(
|
| 196 | MemoryModuleType.IS_PANICKING,
|
| 197 | MemoryStatus.VALUE_ABSENT,
|
| 198 | MemoryModuleType.WALK_TARGET,
|
| 199 | MemoryStatus.VALUE_ABSENT,
|
| 200 | MemoryModuleType.SNIFFER_DIGGING,
|
| 201 | MemoryStatus.VALUE_PRESENT,
|
| 202 | MemoryModuleType.SNIFF_COOLDOWN,
|
| 203 | MemoryStatus.VALUE_PRESENT
|
| 204 | ),
|
| 205 | duration,
|
| 206 | duration
|
| 207 | );
|
| 208 | }
|
| 209 |
|
| 210 | protected boolean checkExtraStartConditions(final ServerLevel level, final Sniffer sniffer) {
|
| 211 | return true;
|
| 212 | }
|
| 213 |
|
| 214 | protected boolean canStillUse(final ServerLevel level, final Sniffer sniffer, final long timestamp) {
|
| 215 | return sniffer.getBrain().getMemory(MemoryModuleType.SNIFFER_DIGGING).isPresent();
|
| 216 | }
|
| 217 |
|
| 218 | protected void start(final ServerLevel level, final Sniffer sniffer, final long timestamp) {
|
| 219 | sniffer.transitionTo(Sniffer.State.RISING);
|
| 220 | }
|
| 221 |
|
| 222 | protected void stop(final ServerLevel level, final Sniffer sniffer, final long timestamp) {
|
| 223 | boolean finished = this.timedOut(timestamp);
|
| 224 | sniffer.transitionTo(Sniffer.State.IDLING).onDiggingComplete(finished);
|
| 225 | sniffer.getBrain().eraseMemory(MemoryModuleType.SNIFFER_DIGGING);
|
| 226 | sniffer.getBrain().setMemory(MemoryModuleType.SNIFFER_HAPPY, true);
|
| 227 | }
|
| 228 | }
|
| 229 |
|
| 230 | private static class Scenting extends Behavior<Sniffer> {
|
| 231 | private Scenting(final int min, final int max) {
|
| 232 | super(
|
| 233 | Map.of(
|
| 234 | MemoryModuleType.IS_PANICKING,
|
| 235 | MemoryStatus.VALUE_ABSENT,
|
| 236 | MemoryModuleType.SNIFFER_DIGGING,
|
| 237 | MemoryStatus.VALUE_ABSENT,
|
| 238 | MemoryModuleType.SNIFFER_SNIFFING_TARGET,
|
| 239 | MemoryStatus.VALUE_ABSENT,
|
| 240 | MemoryModuleType.SNIFFER_HAPPY,
|
| 241 | MemoryStatus.VALUE_ABSENT,
|
| 242 | MemoryModuleType.BREED_TARGET,
|
| 243 | MemoryStatus.VALUE_ABSENT
|
| 244 | ),
|
| 245 | min,
|
| 246 | max
|
| 247 | );
|
| 248 | }
|
| 249 |
|
| 250 | protected boolean checkExtraStartConditions(final ServerLevel level, final Sniffer sniffer) {
|
| 251 | return !sniffer.isTempted();
|
| 252 | }
|
| 253 |
|
| 254 | protected boolean canStillUse(final ServerLevel level, final Sniffer sniffer, final long timestamp) {
|
| 255 | return true;
|
| 256 | }
|
| 257 |
|
| 258 | protected void start(final ServerLevel level, final Sniffer sniffer, final long timestamp) {
|
| 259 | sniffer.transitionTo(Sniffer.State.SCENTING);
|
| 260 | }
|
| 261 |
|
| 262 | protected void stop(final ServerLevel level, final Sniffer sniffer, final long timestamp) {
|
| 263 | sniffer.transitionTo(Sniffer.State.IDLING);
|
| 264 | }
|
| 265 | }
|
| 266 |
|
| 267 | private static class Searching extends Behavior<Sniffer> {
|
| 268 | private Searching() {
|
| 269 | super(
|
| 270 | Map.of(
|
| 271 | MemoryModuleType.WALK_TARGET,
|
| 272 | MemoryStatus.VALUE_PRESENT,
|
| 273 | MemoryModuleType.IS_PANICKING,
|
| 274 | MemoryStatus.VALUE_ABSENT,
|
| 275 | MemoryModuleType.SNIFFER_SNIFFING_TARGET,
|
| 276 | MemoryStatus.VALUE_PRESENT
|
| 277 | ),
|
| 278 | 600
|
| 279 | );
|
| 280 | }
|
| 281 |
|
| 282 | protected boolean checkExtraStartConditions(final ServerLevel level, final Sniffer sniffer) {
|
| 283 | return sniffer.canSniff();
|
| 284 | }
|
| 285 |
|
| 286 | protected boolean canStillUse(final ServerLevel level, final Sniffer sniffer, final long timestamp) {
|
| 287 | if (!sniffer.canSniff()) {
|
| 288 | sniffer.transitionTo(Sniffer.State.IDLING);
|
| 289 | return false;
|
| 290 | } else {
|
| 291 | Optional<BlockPos> walkTarget = sniffer.getBrain()
|
| 292 | .getMemory(MemoryModuleType.WALK_TARGET)
|
| 293 | .map(WalkTarget::getTarget)
|
| 294 | .map(PositionTracker::currentBlockPosition);
|
| 295 | Optional<BlockPos> sniffingTarget = sniffer.getBrain().getMemory(MemoryModuleType.SNIFFER_SNIFFING_TARGET);
|
| 296 | return !walkTarget.isEmpty() && !sniffingTarget.isEmpty() ? sniffingTarget.get().equals(walkTarget.get()) : false;
|
| 297 | }
|
| 298 | }
|
| 299 |
|
| 300 | protected void start(final ServerLevel level, final Sniffer sniffer, final long timestamp) {
|
| 301 | sniffer.transitionTo(Sniffer.State.SEARCHING);
|
| 302 | }
|
| 303 |
|
| 304 | protected void stop(final ServerLevel level, final Sniffer sniffer, final long timestamp) {
|
| 305 | if (sniffer.canDig() && sniffer.canSniff()) {
|
| 306 | sniffer.getBrain().setMemory(MemoryModuleType.SNIFFER_DIGGING, true);
|
| 307 | }
|
| 308 |
|
| 309 | sniffer.getBrain().eraseMemory(MemoryModuleType.WALK_TARGET);
|
| 310 | sniffer.getBrain().eraseMemory(MemoryModuleType.SNIFFER_SNIFFING_TARGET);
|
| 311 | }
|
| 312 | }
|
| 313 |
|
| 314 | private static class Sniffing extends Behavior<Sniffer> {
|
| 315 | private Sniffing(final int min, final int max) {
|
| 316 | super(
|
| 317 | Map.of(
|
| 318 | MemoryModuleType.WALK_TARGET,
|
| 319 | MemoryStatus.VALUE_ABSENT,
|
| 320 | MemoryModuleType.SNIFFER_SNIFFING_TARGET,
|
| 321 | MemoryStatus.VALUE_ABSENT,
|
| 322 | MemoryModuleType.SNIFF_COOLDOWN,
|
| 323 | MemoryStatus.VALUE_ABSENT
|
| 324 | ),
|
| 325 | min,
|
| 326 | max
|
| 327 | );
|
| 328 | }
|
| 329 |
|
| 330 | protected boolean checkExtraStartConditions(final ServerLevel level, final Sniffer body) {
|
| 331 | return !body.isBaby() && body.canSniff();
|
| 332 | }
|
| 333 |
|
| 334 | protected boolean canStillUse(final ServerLevel level, final Sniffer body, final long timestamp) {
|
| 335 | return body.canSniff();
|
| 336 | }
|
| 337 |
|
| 338 | protected void start(final ServerLevel level, final Sniffer sniffer, final long timestamp) {
|
| 339 | sniffer.transitionTo(Sniffer.State.SNIFFING);
|
| 340 | }
|
| 341 |
|
| 342 | protected void stop(final ServerLevel level, final Sniffer sniffer, final long timestamp) {
|
| 343 | boolean finished = this.timedOut(timestamp);
|
| 344 | sniffer.transitionTo(Sniffer.State.IDLING);
|
| 345 | if (finished) {
|
| 346 | sniffer.calculateDigPosition().ifPresent(position -> {
|
| 347 | sniffer.getBrain().setMemory(MemoryModuleType.SNIFFER_SNIFFING_TARGET, position);
|
| 348 | sniffer.getBrain().setMemory(MemoryModuleType.WALK_TARGET, new WalkTarget(position, 1.25F, 0));
|
| 349 | });
|
| 350 | }
|
| 351 | }
|
| 352 | }
|
| 353 | }
|
| 354 |
|