From 447da324d061bcb2a3ab603d98e25e30bd48a2fd Mon Sep 17 00:00:00 2001 From: FatherToast Date: Tue, 12 Jul 2022 14:23:25 -0500 Subject: [PATCH] p a t h f i n d i n g --- .../specialmobs/common/core/SpecialMobs.java | 1 - .../entity/ai/goal/ChargeCreeperGoal.java | 55 +++++++++++-------- .../zombie/MadScientistZombieEntity.java | 9 ++- 3 files changed, 39 insertions(+), 26 deletions(-) diff --git a/src/main/java/fathertoast/specialmobs/common/core/SpecialMobs.java b/src/main/java/fathertoast/specialmobs/common/core/SpecialMobs.java index 410f595..71f76f6 100644 --- a/src/main/java/fathertoast/specialmobs/common/core/SpecialMobs.java +++ b/src/main/java/fathertoast/specialmobs/common/core/SpecialMobs.java @@ -89,7 +89,6 @@ public class SpecialMobs { * ? piglins * ? hoglins * ? zoglins - * ? endermites * ? guardians * ? vortex * ? shulkers diff --git a/src/main/java/fathertoast/specialmobs/common/entity/ai/goal/ChargeCreeperGoal.java b/src/main/java/fathertoast/specialmobs/common/entity/ai/goal/ChargeCreeperGoal.java index 6c85fb3..dd9bcaa 100644 --- a/src/main/java/fathertoast/specialmobs/common/entity/ai/goal/ChargeCreeperGoal.java +++ b/src/main/java/fathertoast/specialmobs/common/entity/ai/goal/ChargeCreeperGoal.java @@ -1,18 +1,19 @@ package fathertoast.specialmobs.common.entity.ai.goal; import fathertoast.specialmobs.common.entity.MobHelper; -import fathertoast.specialmobs.common.entity.zombie.MadScientistZombieEntity; +import net.minecraft.entity.MobEntity; import net.minecraft.entity.ai.goal.Goal; import net.minecraft.entity.monster.CreeperEntity; import net.minecraft.util.SoundCategory; import net.minecraft.util.SoundEvents; +import net.minecraft.util.math.vector.Vector3d; import net.minecraft.world.World; import java.util.EnumSet; import java.util.List; import java.util.function.BiPredicate; -public class ChargeCreeperGoal extends Goal { +public class ChargeCreeperGoal extends Goal { private final BiPredicate targetPredicate; @@ -23,9 +24,10 @@ public class ChargeCreeperGoal extends Goal /** The creeper to target for power-up injection **/ private CreeperEntity creeper; + private int pathUpdateCooldown; + private Vector3d pathTarget = Vector3d.ZERO; private boolean canUseWhileMounted = false; - public ChargeCreeperGoal( T madman, double movementSpeed, double targetRange, BiPredicate targetPredicate ) { this.madman = madman; this.movementSpeed = movementSpeed; @@ -44,8 +46,14 @@ public class ChargeCreeperGoal extends Goal @Override public boolean canUse() { if( madman.isPassenger() || !canUseWhileMounted && madman.isVehicle() ) return false; + findCreeper(); - return creeper != null; + if( creeper == null ) return false; + + madman.getNavigation().moveTo( creeper, movementSpeed ); + pathTarget = creeper.position(); + pathUpdateCooldown = 4 + madman.getRandom().nextInt( 7 ); + return madman.getNavigation().getPath() != null; } private void findCreeper() { @@ -62,33 +70,36 @@ public class ChargeCreeperGoal extends Goal } } - /** Called when this AI is activated. */ - @Override - public void start() { - madman.getNavigation().moveTo( creeper, movementSpeed ); - } - /** @return Called each update while active and returns true if this AI can remain active. */ @Override public boolean canContinueToUse() { - return !madman.isOnGround() && !madman.isPassenger() && !madman.isInWaterOrBubble() && !madman.isInLava(); + return !madman.isPassenger() && (canUseWhileMounted || madman.isVehicle()) && creeper != null && targetPredicate.test( madman, creeper ); } /** Called each tick while this AI is active. */ @Override public void tick() { - if( creeper == null || !targetPredicate.test( madman, creeper ) ) { - findCreeper(); - } - else { - madman.getNavigation().moveTo( creeper, movementSpeed ); - madman.getLookControl().setLookAt( this.creeper.getX(), this.creeper.getEyeY(), this.creeper.getZ() ); + if( creeper == null ) return; + + final double distanceSq = madman.distanceToSqr( creeper ); + + pathUpdateCooldown--; + if( pathUpdateCooldown <= 0 && (creeper.distanceToSqr( pathTarget ) >= 1.0 || madman.getRandom().nextFloat() < 0.05F) ) { + pathUpdateCooldown = 4 + madman.getRandom().nextInt( 7 ); + if( distanceSq > 1024.0 ) pathUpdateCooldown += 10; + else if( distanceSq > 256.0 ) pathUpdateCooldown += 5; - if( madman.distanceTo( creeper ) < 1.5D ) { - MobHelper.charge( creeper ); - madman.level.playSound( null, creeper.getX() + 0.5D, creeper.getY(), creeper.getZ() + 0.5D, SoundEvents.BEE_STING, SoundCategory.HOSTILE, 0.9F, 1.0F ); - creeper = null; - } + if( !madman.getNavigation().moveTo( creeper, movementSpeed ) ) pathUpdateCooldown += 15; + pathTarget = creeper.position(); + } + + madman.getLookControl().setLookAt( creeper, 30.0F, 30.0F ); + if( distanceSq < 2.5 ) { + MobHelper.charge( creeper ); + madman.level.playSound( null, creeper.getX(), creeper.getY(), creeper.getZ(), + SoundEvents.BEE_STING, SoundCategory.HOSTILE, 0.9F, 1.0F ); + + creeper = null; } } } \ No newline at end of file diff --git a/src/main/java/fathertoast/specialmobs/common/entity/zombie/MadScientistZombieEntity.java b/src/main/java/fathertoast/specialmobs/common/entity/zombie/MadScientistZombieEntity.java index 0d5a09a..3d8eaed 100644 --- a/src/main/java/fathertoast/specialmobs/common/entity/zombie/MadScientistZombieEntity.java +++ b/src/main/java/fathertoast/specialmobs/common/entity/zombie/MadScientistZombieEntity.java @@ -12,6 +12,7 @@ import fathertoast.specialmobs.datagen.loot.LootTableBuilder; import mcp.MethodsReturnNonnullByDefault; import net.minecraft.entity.*; import net.minecraft.entity.ai.attributes.Attributes; +import net.minecraft.entity.monster.CreeperEntity; import net.minecraft.inventory.EquipmentSlotType; import net.minecraft.item.ItemStack; import net.minecraft.potion.EffectInstance; @@ -22,6 +23,7 @@ import net.minecraft.world.World; import javax.annotation.Nullable; import javax.annotation.ParametersAreNonnullByDefault; +import java.util.function.BiPredicate; @ParametersAreNonnullByDefault @MethodsReturnNonnullByDefault @@ -63,14 +65,15 @@ public class MadScientistZombieEntity extends _SpecialZombieEntity { //--------------- Variant-Specific Implementations ---------------- + private final BiPredicate CHARGE_CREEPER_TARGET = ( madman, creeper ) -> + creeper.isAlive() && !creeper.isPowered() && madman.getSensing().canSee( creeper ); + public MadScientistZombieEntity( EntityType entityType, World world ) { super( entityType, world ); } /** Override to change this entity's AI goals. */ @Override protected void registerVariantGoals() { - AIHelper.insertGoal( goalSelector, 2, new ChargeCreeperGoal<>( - this, getAttributeValue( Attributes.MOVEMENT_SPEED ) * 1.25, 20.0, - ( madman, creeper ) -> creeper.isAlive() && !creeper.isPowered() && madman.getSensing().canSee( creeper ) ) ); + AIHelper.insertGoal( goalSelector, 2, new ChargeCreeperGoal<>( this, 1.25, 20.0, CHARGE_CREEPER_TARGET ) ); } /** Override to change this entity's attack goal priority. */