| 1 | package net.minecraft.world.entity.monster.breeze;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import com.google.common.collect.Lists;
|
| 5 | import it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
| 6 | import java.util.Map;
|
| 7 | import java.util.Optional;
|
| 8 | import net.minecraft.commands.arguments.EntityAnchorArgument;
|
| 9 | import net.minecraft.core.BlockPos;
|
| 10 | import net.minecraft.core.Direction;
|
| 11 | import net.minecraft.server.level.ServerLevel;
|
| 12 | import net.minecraft.sounds.SoundEvents;
|
| 13 | import net.minecraft.sounds.SoundSource;
|
| 14 | import net.minecraft.tags.FluidTags;
|
| 15 | import net.minecraft.util.RandomSource;
|
| 16 | import net.minecraft.util.Unit;
|
| 17 | import net.minecraft.util.Util;
|
| 18 | import net.minecraft.world.effect.MobEffects;
|
| 19 | import net.minecraft.world.entity.LivingEntity;
|
| 20 | import net.minecraft.world.entity.Pose;
|
| 21 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 22 | import net.minecraft.world.entity.ai.behavior.Behavior;
|
| 23 | import net.minecraft.world.entity.ai.behavior.LongJumpUtil;
|
| 24 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 25 | import net.minecraft.world.entity.ai.memory.MemoryStatus;
|
| 26 | import net.minecraft.world.level.ClipContext;
|
| 27 | import net.minecraft.world.level.block.Blocks;
|
| 28 | import net.minecraft.world.level.block.state.BlockState;
|
| 29 | import net.minecraft.world.phys.HitResult;
|
| 30 | import net.minecraft.world.phys.Vec3;
|
| 31 | import org.jspecify.annotations.Nullable;
|
| 32 |
|
| 33 | public class LongJump extends Behavior<Breeze> {
|
| 34 | private static final int REQUIRED_AIR_BLOCKS_ABOVE = 4;
|
| 35 | private static final int JUMP_COOLDOWN_TICKS = 10;
|
| 36 | private static final int JUMP_COOLDOWN_WHEN_HURT_TICKS = 2;
|
| 37 | private static final int INHALING_DURATION_TICKS = Math.round(10.0F);
|
| 38 | private static final float DEFAULT_FOLLOW_RANGE = 24.0F;
|
| 39 | private static final float DEFAULT_MAX_JUMP_VELOCITY = 1.4F;
|
| 40 | private static final float MAX_JUMP_VELOCITY_MULTIPLIER = 0.058333334F;
|
| 41 | private static final ObjectArrayList<Integer> ALLOWED_ANGLES = new ObjectArrayList<>(Lists.newArrayList(40, 55, 60, 75, 80));
|
| 42 |
|
| 43 | @VisibleForTesting
|
| 44 | public LongJump() {
|
| 45 | super(
|
| 46 | Map.of(
|
| 47 | MemoryModuleType.ATTACK_TARGET,
|
| 48 | MemoryStatus.VALUE_PRESENT,
|
| 49 | MemoryModuleType.BREEZE_JUMP_COOLDOWN,
|
| 50 | MemoryStatus.VALUE_ABSENT,
|
| 51 | MemoryModuleType.BREEZE_JUMP_INHALING,
|
| 52 | MemoryStatus.REGISTERED,
|
| 53 | MemoryModuleType.BREEZE_JUMP_TARGET,
|
| 54 | MemoryStatus.REGISTERED,
|
| 55 | MemoryModuleType.BREEZE_SHOOT,
|
| 56 | MemoryStatus.VALUE_ABSENT,
|
| 57 | MemoryModuleType.WALK_TARGET,
|
| 58 | MemoryStatus.VALUE_ABSENT,
|
| 59 | MemoryModuleType.BREEZE_LEAVING_WATER,
|
| 60 | MemoryStatus.REGISTERED
|
| 61 | ),
|
| 62 | 200
|
| 63 | );
|
| 64 | }
|
| 65 |
|
| 66 | public static boolean canRun(final ServerLevel level, final Breeze breeze) {
|
| 67 | if (!breeze.onGround() && !breeze.isInWater()) {
|
| 68 | return false;
|
| 69 | }
|
| 70 |
|
| 71 | if (!breeze.isInFluidDeeperThan(breeze.getFluidJumpThreshold(), FluidTags.ENTITY_FLOATABLE) && !breeze.isInLava()) {
|
| 72 | if (breeze.getBrain().checkMemory(MemoryModuleType.BREEZE_JUMP_TARGET, MemoryStatus.VALUE_PRESENT)) {
|
| 73 | return true;
|
| 74 | }
|
| 75 |
|
| 76 | LivingEntity attackTarget = breeze.getBrain().getMemory(MemoryModuleType.ATTACK_TARGET).orElse(null);
|
| 77 | if (attackTarget == null) {
|
| 78 | return false;
|
| 79 | }
|
| 80 |
|
| 81 | if (outOfAggroRange(breeze, attackTarget)) {
|
| 82 | breeze.getBrain().eraseMemory(MemoryModuleType.ATTACK_TARGET);
|
| 83 | return false;
|
| 84 | }
|
| 85 |
|
| 86 | if (tooCloseForJump(breeze, attackTarget)) {
|
| 87 | return false;
|
| 88 | }
|
| 89 |
|
| 90 | if (!canJumpFromCurrentPosition(level, breeze)) {
|
| 91 | return false;
|
| 92 | }
|
| 93 |
|
| 94 | BlockPos targetPos = snapToSurface(breeze, BreezeUtil.randomPointBehindTarget(attackTarget, breeze.getRandom()));
|
| 95 | if (targetPos == null) {
|
| 96 | return false;
|
| 97 | }
|
| 98 |
|
| 99 | BlockState bs = level.getBlockState(targetPos.below());
|
| 100 | if (breeze.getType().isBlockDangerous(bs)) {
|
| 101 | return false;
|
| 102 | }
|
| 103 |
|
| 104 | if (!BreezeUtil.hasLineOfSight(breeze, Vec3.atCenterOf(targetPos)) && !BreezeUtil.hasLineOfSight(breeze, Vec3.atCenterOf(targetPos.above(4)))) {
|
| 105 | return false;
|
| 106 | }
|
| 107 |
|
| 108 | breeze.getBrain().setMemory(MemoryModuleType.BREEZE_JUMP_TARGET, targetPos);
|
| 109 | return true;
|
| 110 | } else {
|
| 111 | return false;
|
| 112 | }
|
| 113 | }
|
| 114 |
|
| 115 | protected boolean checkExtraStartConditions(final ServerLevel level, final Breeze breeze) {
|
| 116 | return canRun(level, breeze);
|
| 117 | }
|
| 118 |
|
| 119 | protected boolean canStillUse(final ServerLevel level, final Breeze breeze, final long timestamp) {
|
| 120 | return breeze.getPose() != Pose.STANDING && !breeze.getBrain().hasMemoryValue(MemoryModuleType.BREEZE_JUMP_COOLDOWN);
|
| 121 | }
|
| 122 |
|
| 123 | protected void start(final ServerLevel level, final Breeze breeze, final long timestamp) {
|
| 124 | if (breeze.getBrain().checkMemory(MemoryModuleType.BREEZE_JUMP_INHALING, MemoryStatus.VALUE_ABSENT)) {
|
| 125 | breeze.getBrain().setMemoryWithExpiry(MemoryModuleType.BREEZE_JUMP_INHALING, Unit.INSTANCE, INHALING_DURATION_TICKS);
|
| 126 | }
|
| 127 |
|
| 128 | breeze.setPose(Pose.INHALING);
|
| 129 | level.playSound(null, breeze, SoundEvents.BREEZE_CHARGE, SoundSource.HOSTILE, 1.0F, 1.0F);
|
| 130 | breeze.getBrain()
|
| 131 | .getMemory(MemoryModuleType.BREEZE_JUMP_TARGET)
|
| 132 | .ifPresent(targetPos -> breeze.lookAt(EntityAnchorArgument.Anchor.EYES, Vec3.atCenterOf(targetPos)));
|
| 133 | }
|
| 134 |
|
| 135 | protected void tick(final ServerLevel level, final Breeze breeze, final long timestamp) {
|
| 136 | boolean inWater = breeze.isInWater();
|
| 137 | if (!inWater && breeze.getBrain().checkMemory(MemoryModuleType.BREEZE_LEAVING_WATER, MemoryStatus.VALUE_PRESENT)) {
|
| 138 | breeze.getBrain().eraseMemory(MemoryModuleType.BREEZE_LEAVING_WATER);
|
| 139 | }
|
| 140 |
|
| 141 | if (isFinishedInhaling(breeze)) {
|
| 142 | Vec3 velocityVector = breeze.getBrain()
|
| 143 | .getMemory(MemoryModuleType.BREEZE_JUMP_TARGET)
|
| 144 | .flatMap(targetPos -> calculateOptimalJumpVector(breeze, breeze.getRandom(), Vec3.atBottomCenterOf(targetPos)))
|
| 145 | .orElse(null);
|
| 146 | if (velocityVector == null) {
|
| 147 | breeze.setPose(Pose.STANDING);
|
| 148 | return;
|
| 149 | }
|
| 150 |
|
| 151 | if (inWater) {
|
| 152 | breeze.getBrain().setMemory(MemoryModuleType.BREEZE_LEAVING_WATER, Unit.INSTANCE);
|
| 153 | }
|
| 154 |
|
| 155 | breeze.playSound(SoundEvents.BREEZE_JUMP, 1.0F, 1.0F);
|
| 156 | breeze.setPose(Pose.LONG_JUMPING);
|
| 157 | breeze.setYRot(breeze.yBodyRot);
|
| 158 | breeze.setDiscardFriction(true);
|
| 159 | breeze.setDeltaMovement(velocityVector);
|
| 160 | } else if (isFinishedJumping(breeze)) {
|
| 161 | breeze.playSound(SoundEvents.BREEZE_LAND, 1.0F, 1.0F);
|
| 162 | breeze.setPose(Pose.STANDING);
|
| 163 | breeze.setDiscardFriction(false);
|
| 164 | boolean wasHurt = breeze.getBrain().hasMemoryValue(MemoryModuleType.HURT_BY);
|
| 165 | breeze.getBrain().setMemoryWithExpiry(MemoryModuleType.BREEZE_JUMP_COOLDOWN, Unit.INSTANCE, wasHurt ? 2L : 10L);
|
| 166 | breeze.getBrain().setMemoryWithExpiry(MemoryModuleType.BREEZE_SHOOT, Unit.INSTANCE, 100L);
|
| 167 | }
|
| 168 | }
|
| 169 |
|
| 170 | protected void stop(final ServerLevel level, final Breeze breeze, final long timestamp) {
|
| 171 | if (breeze.getPose() == Pose.LONG_JUMPING || breeze.getPose() == Pose.INHALING) {
|
| 172 | breeze.setPose(Pose.STANDING);
|
| 173 | }
|
| 174 |
|
| 175 | breeze.getBrain().eraseMemory(MemoryModuleType.BREEZE_JUMP_TARGET);
|
| 176 | breeze.getBrain().eraseMemory(MemoryModuleType.BREEZE_JUMP_INHALING);
|
| 177 | breeze.getBrain().eraseMemory(MemoryModuleType.BREEZE_LEAVING_WATER);
|
| 178 | }
|
| 179 |
|
| 180 | private static boolean isFinishedInhaling(final Breeze breeze) {
|
| 181 | return breeze.getBrain().getMemory(MemoryModuleType.BREEZE_JUMP_INHALING).isEmpty() && breeze.getPose() == Pose.INHALING;
|
| 182 | }
|
| 183 |
|
| 184 | private static boolean isFinishedJumping(final Breeze breeze) {
|
| 185 | boolean isJumping = breeze.getPose() == Pose.LONG_JUMPING;
|
| 186 | boolean landedOnGround = breeze.onGround();
|
| 187 | boolean landedInWater = breeze.isInWater() && breeze.getBrain().checkMemory(MemoryModuleType.BREEZE_LEAVING_WATER, MemoryStatus.VALUE_ABSENT);
|
| 188 | return isJumping && (landedOnGround || landedInWater);
|
| 189 | }
|
| 190 |
|
| 191 | private static @Nullable BlockPos snapToSurface(final LivingEntity entity, final Vec3 target) {
|
| 192 | ClipContext collisionBelow = new ClipContext(target, target.relative(Direction.DOWN, 10.0), ClipContext.Block.COLLIDER, ClipContext.Fluid.NONE, entity);
|
| 193 | HitResult surfaceBelow = entity.level().clip(collisionBelow);
|
| 194 | if (surfaceBelow.getType() == HitResult.Type.BLOCK) {
|
| 195 | return BlockPos.containing(surfaceBelow.getLocation()).above();
|
| 196 | }
|
| 197 |
|
| 198 | ClipContext collisionAbove = new ClipContext(target, target.relative(Direction.UP, 10.0), ClipContext.Block.COLLIDER, ClipContext.Fluid.NONE, entity);
|
| 199 | HitResult surfaceAbove = entity.level().clip(collisionAbove);
|
| 200 | return surfaceAbove.getType() == HitResult.Type.BLOCK ? BlockPos.containing(surfaceAbove.getLocation()).above() : null;
|
| 201 | }
|
| 202 |
|
| 203 | private static boolean outOfAggroRange(final Breeze breeze, final LivingEntity attackTarget) {
|
| 204 | return !attackTarget.closerThan(breeze, breeze.getAttributeValue(Attributes.FOLLOW_RANGE));
|
| 205 | }
|
| 206 |
|
| 207 | private static boolean tooCloseForJump(final Breeze breeze, final LivingEntity attackTarget) {
|
| 208 | return attackTarget.distanceTo(breeze) - 4.0F <= 0.0F;
|
| 209 | }
|
| 210 |
|
| 211 | private static boolean canJumpFromCurrentPosition(final ServerLevel level, final Breeze breeze) {
|
| 212 | BlockPos currentPos = breeze.blockPosition();
|
| 213 | if (level.getBlockState(currentPos).is(Blocks.HONEY_BLOCK)) {
|
| 214 | return false;
|
| 215 | }
|
| 216 |
|
| 217 | for (int i = 1; i <= 4; i++) {
|
| 218 | BlockPos offsetPos = currentPos.relative(Direction.UP, i);
|
| 219 | if (!level.getBlockState(offsetPos).isAir() && !level.getFluidState(offsetPos).is(FluidTags.WATER)) {
|
| 220 | return false;
|
| 221 | }
|
| 222 | }
|
| 223 |
|
| 224 | return true;
|
| 225 | }
|
| 226 |
|
| 227 | private static Optional<Vec3> calculateOptimalJumpVector(final Breeze body, final RandomSource random, final Vec3 targetPos) {
|
| 228 | for (int angle : Util.shuffledCopy(ALLOWED_ANGLES, random)) {
|
| 229 | float maxJumpVelocity = 0.058333334F * (float)body.getAttributeValue(Attributes.FOLLOW_RANGE);
|
| 230 | Optional<Vec3> velocityVector = LongJumpUtil.calculateJumpVectorForAngle(body, targetPos, maxJumpVelocity, angle, false);
|
| 231 | if (velocityVector.isPresent()) {
|
| 232 | if (body.hasEffect(MobEffects.JUMP_BOOST)) {
|
| 233 | double jumpEffectAmplifier = velocityVector.get().normalize().y * body.getJumpBoostPower();
|
| 234 | return velocityVector.map(v -> v.add(0.0, jumpEffectAmplifier, 0.0));
|
| 235 | }
|
| 236 |
|
| 237 | return velocityVector;
|
| 238 | }
|
| 239 | }
|
| 240 |
|
| 241 | return Optional.empty();
|
| 242 | }
|
| 243 | }
|
| 244 |
|