| 1 | package net.minecraft.world.entity.ai.behavior;
|
| 2 |
|
| 3 | import java.util.Map;
|
| 4 | import java.util.Set;
|
| 5 | import java.util.Map.Entry;
|
| 6 | import net.minecraft.server.level.ServerLevel;
|
| 7 | import net.minecraft.world.entity.LivingEntity;
|
| 8 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 9 | import net.minecraft.world.entity.ai.memory.MemoryStatus;
|
| 10 |
|
| 11 | public abstract class Behavior<E extends LivingEntity> implements BehaviorControl<E> {
|
| 12 | public static final int DEFAULT_DURATION = 60;
|
| 13 | protected final Map<MemoryModuleType<?>, MemoryStatus> entryCondition;
|
| 14 | private Behavior.Status status = Behavior.Status.STOPPED;
|
| 15 | private long endTimestamp;
|
| 16 | private final int minDuration;
|
| 17 | private final int maxDuration;
|
| 18 |
|
| 19 | public Behavior(final Map<MemoryModuleType<?>, MemoryStatus> entryCondition) {
|
| 20 | this(entryCondition, 60);
|
| 21 | }
|
| 22 |
|
| 23 | public Behavior(final Map<MemoryModuleType<?>, MemoryStatus> entryCondition, final int timeOutDuration) {
|
| 24 | this(entryCondition, timeOutDuration, timeOutDuration);
|
| 25 | }
|
| 26 |
|
| 27 | public Behavior(final Map<MemoryModuleType<?>, MemoryStatus> entryCondition, final int minDuration, final int maxDuration) {
|
| 28 | this.minDuration = minDuration;
|
| 29 | this.maxDuration = maxDuration;
|
| 30 | this.entryCondition = entryCondition;
|
| 31 | }
|
| 32 |
|
| 33 | @Override
|
| 34 | public Behavior.Status getStatus() {
|
| 35 | return this.status;
|
| 36 | }
|
| 37 |
|
| 38 | @Override
|
| 39 | public Set<MemoryModuleType<?>> getRequiredMemories() {
|
| 40 | return this.entryCondition.keySet();
|
| 41 | }
|
| 42 |
|
| 43 | @Override
|
| 44 | public final boolean tryStart(final ServerLevel level, final E body, final long timestamp) {
|
| 45 | if (this.hasRequiredMemories(body) && this.checkExtraStartConditions(level, body)) {
|
| 46 | this.status = Behavior.Status.RUNNING;
|
| 47 | int duration = this.minDuration + level.getRandom().nextInt(this.maxDuration + 1 - this.minDuration);
|
| 48 | this.endTimestamp = timestamp + duration;
|
| 49 | this.start(level, body, timestamp);
|
| 50 | return true;
|
| 51 | } else {
|
| 52 | return false;
|
| 53 | }
|
| 54 | }
|
| 55 |
|
| 56 | protected void start(final ServerLevel level, final E body, final long timestamp) {
|
| 57 | }
|
| 58 |
|
| 59 | @Override
|
| 60 | public final void tickOrStop(final ServerLevel level, final E body, final long timestamp) {
|
| 61 | if (!this.timedOut(timestamp) && this.canStillUse(level, body, timestamp)) {
|
| 62 | this.tick(level, body, timestamp);
|
| 63 | } else {
|
| 64 | this.doStop(level, body, timestamp);
|
| 65 | }
|
| 66 | }
|
| 67 |
|
| 68 | protected void tick(final ServerLevel level, final E body, final long timestamp) {
|
| 69 | }
|
| 70 |
|
| 71 | @Override
|
| 72 | public final void doStop(final ServerLevel level, final E body, final long timestamp) {
|
| 73 | this.status = Behavior.Status.STOPPED;
|
| 74 | this.stop(level, body, timestamp);
|
| 75 | }
|
| 76 |
|
| 77 | protected void stop(final ServerLevel level, final E body, final long timestamp) {
|
| 78 | }
|
| 79 |
|
| 80 | protected boolean canStillUse(final ServerLevel level, final E body, final long timestamp) {
|
| 81 | return false;
|
| 82 | }
|
| 83 |
|
| 84 | protected boolean timedOut(final long timestamp) {
|
| 85 | return timestamp > this.endTimestamp;
|
| 86 | }
|
| 87 |
|
| 88 | protected boolean checkExtraStartConditions(final ServerLevel level, final E body) {
|
| 89 | return true;
|
| 90 | }
|
| 91 |
|
| 92 | @Override
|
| 93 | public String debugString() {
|
| 94 | return this.getClass().getSimpleName();
|
| 95 | }
|
| 96 |
|
| 97 | protected boolean hasRequiredMemories(final E body) {
|
| 98 | for (Entry<MemoryModuleType<?>, MemoryStatus> entry : this.entryCondition.entrySet()) {
|
| 99 | MemoryModuleType<?> memoryType = entry.getKey();
|
| 100 | MemoryStatus requiredStatus = entry.getValue();
|
| 101 | if (!body.getBrain().checkMemory(memoryType, requiredStatus)) {
|
| 102 | return false;
|
| 103 | }
|
| 104 | }
|
| 105 |
|
| 106 | return true;
|
| 107 | }
|
| 108 |
|
| 109 | public enum Status {
|
| 110 | STOPPED,
|
| 111 | RUNNING;
|
| 112 | }
|
| 113 | }
|
| 114 |
|