| 1 | package net.minecraft.world.entity.ai.behavior;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableMap;
|
| 4 | import java.util.ArrayList;
|
| 5 | import java.util.List;
|
| 6 | import java.util.Optional;
|
| 7 | import net.minecraft.server.level.ServerLevel;
|
| 8 | import net.minecraft.sounds.SoundEvent;
|
| 9 | import net.minecraft.util.Mth;
|
| 10 | import net.minecraft.world.damagesource.DamageSource;
|
| 11 | import net.minecraft.world.effect.MobEffects;
|
| 12 | import net.minecraft.world.entity.LivingEntity;
|
| 13 | import net.minecraft.world.entity.TamableAnimal;
|
| 14 | import net.minecraft.world.entity.ai.Brain;
|
| 15 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 16 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 17 | import net.minecraft.world.entity.ai.memory.MemoryStatus;
|
| 18 | import net.minecraft.world.entity.ai.targeting.TargetingConditions;
|
| 19 | import net.minecraft.world.entity.animal.Animal;
|
| 20 | import net.minecraft.world.item.enchantment.EnchantmentHelper;
|
| 21 | import net.minecraft.world.level.entity.EntityTypeTest;
|
| 22 | import net.minecraft.world.phys.Vec3;
|
| 23 |
|
| 24 | public class ChargeAttack extends Behavior<Animal> {
|
| 25 | private final int timeBetweenAttacks;
|
| 26 | private final TargetingConditions chargeTargeting;
|
| 27 | private final float speed;
|
| 28 | private final float knockbackForce;
|
| 29 | private final double maxTargetDetectionDistance;
|
| 30 | private final double maxChargeDistance;
|
| 31 | private final SoundEvent chargeSound;
|
| 32 | private Vec3 chargeVelocityVector;
|
| 33 | private Vec3 startPosition;
|
| 34 |
|
| 35 | public ChargeAttack(
|
| 36 | final int timeBetweenAttacks,
|
| 37 | final TargetingConditions chargeTargeting,
|
| 38 | final float speed,
|
| 39 | final float knockbackForce,
|
| 40 | final double maxChargeDistance,
|
| 41 | final double maxTargetDetectionDistance,
|
| 42 | final SoundEvent chargeSound
|
| 43 | ) {
|
| 44 | super(ImmutableMap.of(MemoryModuleType.CHARGE_COOLDOWN_TICKS, MemoryStatus.VALUE_ABSENT, MemoryModuleType.ATTACK_TARGET, MemoryStatus.VALUE_PRESENT));
|
| 45 | this.timeBetweenAttacks = timeBetweenAttacks;
|
| 46 | this.chargeTargeting = chargeTargeting;
|
| 47 | this.speed = speed;
|
| 48 | this.knockbackForce = knockbackForce;
|
| 49 | this.maxChargeDistance = maxChargeDistance;
|
| 50 | this.maxTargetDetectionDistance = maxTargetDetectionDistance;
|
| 51 | this.chargeSound = chargeSound;
|
| 52 | this.chargeVelocityVector = Vec3.ZERO;
|
| 53 | this.startPosition = Vec3.ZERO;
|
| 54 | }
|
| 55 |
|
| 56 | protected boolean checkExtraStartConditions(final ServerLevel level, final Animal body) {
|
| 57 | return body.getBrain().hasMemoryValue(MemoryModuleType.ATTACK_TARGET);
|
| 58 | }
|
| 59 |
|
| 60 | protected boolean canStillUse(final ServerLevel level, final Animal body, final long timestamp) {
|
| 61 | Brain<?> brain = body.getBrain();
|
| 62 | Optional<LivingEntity> attackCandidate = brain.getMemory(MemoryModuleType.ATTACK_TARGET);
|
| 63 | if (attackCandidate.isEmpty()) {
|
| 64 | return false;
|
| 65 | } else {
|
| 66 | LivingEntity attackTarget = attackCandidate.get();
|
| 67 | if (body instanceof TamableAnimal tamedAnimal && tamedAnimal.isTame()) {
|
| 68 | return false;
|
| 69 | } else if (body.position().subtract(this.startPosition).lengthSqr() >= this.maxChargeDistance * this.maxChargeDistance) {
|
| 70 | return false;
|
| 71 | } else if (attackTarget.position().subtract(body.position()).lengthSqr() >= this.maxTargetDetectionDistance * this.maxTargetDetectionDistance) {
|
| 72 | return false;
|
| 73 | } else {
|
| 74 | return !body.hasLineOfSight(attackTarget) ? false : !brain.hasMemoryValue(MemoryModuleType.CHARGE_COOLDOWN_TICKS);
|
| 75 | }
|
| 76 | }
|
| 77 | }
|
| 78 |
|
| 79 | protected void start(final ServerLevel level, final Animal body, final long timestamp) {
|
| 80 | Brain<?> brain = body.getBrain();
|
| 81 | this.startPosition = body.position();
|
| 82 | LivingEntity attackCandidate = brain.getMemory(MemoryModuleType.ATTACK_TARGET).get();
|
| 83 | Vec3 direction = attackCandidate.position().subtract(body.position()).normalize();
|
| 84 | this.chargeVelocityVector = direction.scale(this.speed);
|
| 85 | if (this.canStillUse(level, body, timestamp)) {
|
| 86 | body.playSound(this.chargeSound);
|
| 87 | }
|
| 88 | }
|
| 89 |
|
| 90 | protected void tick(final ServerLevel level, final Animal body, final long timestamp) {
|
| 91 | Brain<?> brain = body.getBrain();
|
| 92 | LivingEntity attackTarget = brain.getMemory(MemoryModuleType.ATTACK_TARGET).orElseThrow();
|
| 93 | body.lookAt(attackTarget, 360.0F, 360.0F);
|
| 94 | body.setDeltaMovement(this.chargeVelocityVector);
|
| 95 | List<LivingEntity> collidingEntities = new ArrayList<>(1);
|
| 96 | level.getEntities(EntityTypeTest.forClass(LivingEntity.class), body.getBoundingBox(), e -> this.chargeTargeting.test(level, body, e), collidingEntities, 1);
|
| 97 | if (!collidingEntities.isEmpty()) {
|
| 98 | LivingEntity closestAttackTarget = collidingEntities.get(0);
|
| 99 | if (body.hasPassenger(closestAttackTarget)) {
|
| 100 | return;
|
| 101 | }
|
| 102 |
|
| 103 | this.dealDamageToTarget(level, body, closestAttackTarget);
|
| 104 | this.dealKnockBack(level, body, closestAttackTarget);
|
| 105 | this.stop(level, body, timestamp);
|
| 106 | }
|
| 107 | }
|
| 108 |
|
| 109 | private void dealDamageToTarget(final ServerLevel level, final Animal body, final LivingEntity target) {
|
| 110 | DamageSource damageSource = level.damageSources().mobAttack(body);
|
| 111 | float damage = (float)body.getAttributeValue(Attributes.ATTACK_DAMAGE);
|
| 112 | if (target.hurtServer(level, damageSource, damage)) {
|
| 113 | EnchantmentHelper.doPostAttackEffects(level, target, damageSource);
|
| 114 | }
|
| 115 | }
|
| 116 |
|
| 117 | private void dealKnockBack(final ServerLevel level, final Animal body, final LivingEntity target) {
|
| 118 | int movementSpeedLevel = body.hasEffect(MobEffects.SPEED) ? body.getEffect(MobEffects.SPEED).getAmplifier() + 1 : 0;
|
| 119 | int movementSlowdownLevel = body.hasEffect(MobEffects.SLOWNESS) ? body.getEffect(MobEffects.SLOWNESS).getAmplifier() + 1 : 0;
|
| 120 | float speedBoostPower = 0.25F * (movementSpeedLevel - movementSlowdownLevel);
|
| 121 | float speedFactor = Mth.clamp(this.speed * (float)body.getAttributeValue(Attributes.MOVEMENT_SPEED), 0.2F, 2.0F) + speedBoostPower;
|
| 122 | DamageSource damageSource = level.damageSources().mobAttack(body);
|
| 123 | float damage = (float)body.getAttributeValue(Attributes.ATTACK_DAMAGE);
|
| 124 | body.causeExtraKnockback(target, speedFactor * this.knockbackForce, body.getDeltaMovement(), damageSource, damage, false);
|
| 125 | }
|
| 126 |
|
| 127 | protected void stop(final ServerLevel level, final Animal body, final long timestamp) {
|
| 128 | body.getBrain().setMemory(MemoryModuleType.CHARGE_COOLDOWN_TICKS, this.timeBetweenAttacks);
|
| 129 | body.getBrain().eraseMemory(MemoryModuleType.ATTACK_TARGET);
|
| 130 | }
|
| 131 | }
|
| 132 |
|