PrepareRamNearestTarget .java

net.minecraft.world.entity.ai.behavior.PrepareRamNearestTarget (class): members: reachedRamPositionTimestamp, getPrepareRamSound, getCooldownOnFail, stop.

net.minecraft.world.entity.ai.behaviorclass189 linescommonextends PathfinderMob>7.5 KB

API Surface (7)

void start(final ServerLevel level, final PathfinderMob body, final long timestamp)
void stop(final ServerLevel level, final E body, final long timestamp)
boolean canStillUse(final ServerLevel level, final PathfinderMob body, final long timestamp)
void tick(final ServerLevel level, final E body, final long timestamp)
BlockPos getStartPosition()
BlockPos getTargetPosition()
LivingEntity getTarget()

Fields & Constants (10)

TIME_OUT_DURATION
getCooldownOnFail
minRamDistance
maxRamDistance
walkSpeed
ramTargeting
ramPrepareTime
getPrepareRamSound
reachedRamPositionTimestamp
ramCandidate

Full Source

PrepareRamNearestTarget.java189 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.ai.behavior;
2
3
import com.google.common.collect.ImmutableMap;
4
import com.google.common.collect.Lists;
5
import java.util.Comparator;
6
import java.util.List;
7
import java.util.Optional;
8
import java.util.function.Function;
9
import java.util.function.ToIntFunction;
10
import net.minecraft.core.BlockPos;
11
import net.minecraft.core.Direction;
12
import net.minecraft.server.level.ServerLevel;
13
import net.minecraft.sounds.SoundEvent;
14
import net.minecraft.sounds.SoundSource;
15
import net.minecraft.util.Mth;
16
import net.minecraft.world.entity.LivingEntity;
17
import net.minecraft.world.entity.PathfinderMob;
18
import net.minecraft.world.entity.ai.Brain;
19
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
20
import net.minecraft.world.entity.ai.memory.MemoryStatus;
21
import net.minecraft.world.entity.ai.memory.WalkTarget;
22
import net.minecraft.world.entity.ai.navigation.PathNavigation;
23
import net.minecraft.world.entity.ai.targeting.TargetingConditions;
24
import net.minecraft.world.level.pathfinder.Path;
25
import net.minecraft.world.level.pathfinder.WalkNodeEvaluator;
26
import net.minecraft.world.phys.Vec3;
27
28
public class PrepareRamNearestTarget<E extends PathfinderMob> extends Behavior<E> {
29
	public static final int TIME_OUT_DURATION = 160;
30
	private final ToIntFunction<E> getCooldownOnFail;
31
	private final int minRamDistance;
32
	private final int maxRamDistance;
33
	private final float walkSpeed;
34
	private final TargetingConditions ramTargeting;
35
	private final int ramPrepareTime;
36
	private final Function<E, SoundEvent> getPrepareRamSound;
37
	private Optional<Long> reachedRamPositionTimestamp = Optional.empty();
38
	private Optional<PrepareRamNearestTarget.RamCandidate> ramCandidate = Optional.empty();
39
40
	public PrepareRamNearestTarget(
41
		final ToIntFunction<E> getCooldownOnFail,
42
		final int minRamDistance,
43
		final int maxRamDistance,
44
		final float walkSpeed,
45
		final TargetingConditions ramTargeting,
46
		final int ramPrepareTime,
47
		final Function<E, SoundEvent> getPrepareRamSound
48
	) {
49
		super(
50
			ImmutableMap.of(
51
				MemoryModuleType.LOOK_TARGET,
52
				MemoryStatus.REGISTERED,
53
				MemoryModuleType.RAM_COOLDOWN_TICKS,
54
				MemoryStatus.VALUE_ABSENT,
55
				MemoryModuleType.NEAREST_VISIBLE_LIVING_ENTITIES,
56
				MemoryStatus.VALUE_PRESENT,
57
				MemoryModuleType.RAM_TARGET,
58
				MemoryStatus.VALUE_ABSENT
59
			),
60
			160
61
		);
62
		this.getCooldownOnFail = getCooldownOnFail;
63
		this.minRamDistance = minRamDistance;
64
		this.maxRamDistance = maxRamDistance;
65
		this.walkSpeed = walkSpeed;
66
		this.ramTargeting = ramTargeting;
67
		this.ramPrepareTime = ramPrepareTime;
68
		this.getPrepareRamSound = getPrepareRamSound;
69
	}
70
71
	protected void start(final ServerLevel level, final PathfinderMob body, final long timestamp) {
72
		Brain<?> brain = body.getBrain();
73
		brain.getMemory(MemoryModuleType.NEAREST_VISIBLE_LIVING_ENTITIES)
74
			.flatMap(livingEntities -> livingEntities.findClosest(entity -> this.ramTargeting.test(level, body, entity)))
75
			.ifPresent(livingEntity -> this.chooseRamPosition(body, livingEntity));
76
	}
77
78
	protected void stop(final ServerLevel level, final E body, final long timestamp) {
79
		Brain<?> brain = body.getBrain();
80
		if (!brain.hasMemoryValue(MemoryModuleType.RAM_TARGET)) {
81
			level.broadcastEntityEvent(body, (byte)59);
82
			brain.setMemory(MemoryModuleType.RAM_COOLDOWN_TICKS, this.getCooldownOnFail.applyAsInt(body));
83
		}
84
	}
85
86
	protected boolean canStillUse(final ServerLevel level, final PathfinderMob body, final long timestamp) {
87
		return this.ramCandidate.isPresent() && this.ramCandidate.get().getTarget().isAlive();
88
	}
89
90
	protected void tick(final ServerLevel level, final E body, final long timestamp) {
91
		if (!this.ramCandidate.isEmpty()) {
92
			body.getBrain().setMemory(MemoryModuleType.WALK_TARGET, new WalkTarget(this.ramCandidate.get().getStartPosition(), this.walkSpeed, 0));
93
			body.getBrain().setMemory(MemoryModuleType.LOOK_TARGET, new EntityTracker(this.ramCandidate.get().getTarget(), true));
94
			boolean didTargetMove = !this.ramCandidate.get().getTarget().blockPosition().equals(this.ramCandidate.get().getTargetPosition());
95
			if (didTargetMove) {
96
				level.broadcastEntityEvent(body, (byte)59);
97
				body.getNavigation().stop();
98
				this.chooseRamPosition(body, this.ramCandidate.get().target);
99
			} else {
100
				BlockPos startRamPos = body.blockPosition();
101
				if (startRamPos.equals(this.ramCandidate.get().getStartPosition())) {
102
					level.broadcastEntityEvent(body, (byte)58);
103
					if (this.reachedRamPositionTimestamp.isEmpty()) {
104
						this.reachedRamPositionTimestamp = Optional.of(timestamp);
105
					}
106
107
					if (timestamp - this.reachedRamPositionTimestamp.get() >= this.ramPrepareTime) {
108
						body.getBrain().setMemory(MemoryModuleType.RAM_TARGET, this.getEdgeOfBlock(startRamPos, this.ramCandidate.get().getTargetPosition()));
109
						level.playSound(null, body, this.getPrepareRamSound.apply(body), SoundSource.NEUTRAL, 1.0F, body.getVoicePitch());
110
						this.ramCandidate = Optional.empty();
111
					}
112
				}
113
			}
114
		}
115
	}
116
117
	private Vec3 getEdgeOfBlock(final BlockPos startRamPos, final BlockPos targetPos) {
118
		double offsetDistance = 0.5;
119
		double xOffset = 0.5 * Mth.sign(targetPos.getX() - startRamPos.getX());
120
		double zOffset = 0.5 * Mth.sign(targetPos.getZ() - startRamPos.getZ());
121
		return Vec3.atBottomCenterOf(targetPos).add(xOffset, 0.0, zOffset);
122
	}
123
124
	private Optional<BlockPos> calculateRammingStartPosition(final PathfinderMob body, final LivingEntity ramableTarget) {
125
		BlockPos targetPos = ramableTarget.blockPosition();
126
		if (!this.isWalkableBlock(body, targetPos)) {
127
			return Optional.empty();
128
		}
129
130
		List<BlockPos> possibleRamPositions = Lists.newArrayList();
131
		BlockPos.MutableBlockPos walkablePosFurthestAwayFromTarget = targetPos.mutable();
132
133
		for (Direction direction : Direction.Plane.HORIZONTAL) {
134
			walkablePosFurthestAwayFromTarget.set(targetPos);
135
136
			for (int distance = 0; distance < this.maxRamDistance; distance++) {
137
				if (!this.isWalkableBlock(body, walkablePosFurthestAwayFromTarget.move(direction))) {
138
					walkablePosFurthestAwayFromTarget.move(direction.getOpposite());
139
					break;
140
				}
141
			}
142
143
			if (walkablePosFurthestAwayFromTarget.distManhattan(targetPos) >= this.minRamDistance) {
144
				possibleRamPositions.add(walkablePosFurthestAwayFromTarget.immutable());
145
			}
146
		}
147
148
		PathNavigation navigation = body.getNavigation();
149
		return possibleRamPositions.stream().sorted(Comparator.comparingDouble(body.blockPosition()::distSqr)).filter(pos -> {
150
			Path path = navigation.createPath(pos, 0);
151
			return path != null && path.canReach();
152
		}).findFirst();
153
	}
154
155
	private boolean isWalkableBlock(final PathfinderMob body, final BlockPos targetPos) {
156
		return body.getNavigation().isStableDestination(targetPos) && body.getPathfindingMalus(WalkNodeEvaluator.getPathTypeStatic(body, targetPos)) == 0.0F;
157
	}
158
159
	private void chooseRamPosition(final PathfinderMob body, final LivingEntity ramableTarget) {
160
		this.reachedRamPositionTimestamp = Optional.empty();
161
		this.ramCandidate = this.calculateRammingStartPosition(body, ramableTarget)
162
			.map(pos -> new PrepareRamNearestTarget.RamCandidate(pos, ramableTarget.blockPosition(), ramableTarget));
163
	}
164
165
	public static class RamCandidate {
166
		private final BlockPos startPosition;
167
		private final BlockPos targetPosition;
168
		private final LivingEntity target;
169
170
		public RamCandidate(final BlockPos startPosition, final BlockPos targetPosition, final LivingEntity target) {
171
			this.startPosition = startPosition;
172
			this.targetPosition = targetPosition;
173
			this.target = target;
174
		}
175
176
		public BlockPos getStartPosition() {
177
			return this.startPosition;
178
		}
179
180
		public BlockPos getTargetPosition() {
181
			return this.targetPosition;
182
		}
183
184
		public LivingEntity getTarget() {
185
			return this.target;
186
		}
187
	}
188
}
189

Decompiled Minecraft 26.3 source of PrepareRamNearestTarget (net/minecraft/world/entity/ai/behavior/PrepareRamNearestTarget.java) with Mojang official mappings, stored verbatim on the site.