p a t h f i n d i n g

This commit is contained in:
FatherToast 2022-07-12 14:23:25 -05:00
parent 69f183b707
commit 447da324d0
3 changed files with 39 additions and 26 deletions

View file

@ -89,7 +89,6 @@ public class SpecialMobs {
* ? piglins * ? piglins
* ? hoglins * ? hoglins
* ? zoglins * ? zoglins
* ? endermites
* ? guardians * ? guardians
* ? vortex * ? vortex
* ? shulkers * ? shulkers

View file

@ -1,18 +1,19 @@
package fathertoast.specialmobs.common.entity.ai.goal; package fathertoast.specialmobs.common.entity.ai.goal;
import fathertoast.specialmobs.common.entity.MobHelper; 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.ai.goal.Goal;
import net.minecraft.entity.monster.CreeperEntity; import net.minecraft.entity.monster.CreeperEntity;
import net.minecraft.util.SoundCategory; import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvents; import net.minecraft.util.SoundEvents;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraft.world.World; import net.minecraft.world.World;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.List; import java.util.List;
import java.util.function.BiPredicate; import java.util.function.BiPredicate;
public class ChargeCreeperGoal<T extends MadScientistZombieEntity> extends Goal { public class ChargeCreeperGoal<T extends MobEntity> extends Goal {
private final BiPredicate<T, ? super CreeperEntity> targetPredicate; private final BiPredicate<T, ? super CreeperEntity> targetPredicate;
@ -23,9 +24,10 @@ public class ChargeCreeperGoal<T extends MadScientistZombieEntity> extends Goal
/** The creeper to target for power-up injection **/ /** The creeper to target for power-up injection **/
private CreeperEntity creeper; private CreeperEntity creeper;
private int pathUpdateCooldown;
private Vector3d pathTarget = Vector3d.ZERO;
private boolean canUseWhileMounted = false; private boolean canUseWhileMounted = false;
public ChargeCreeperGoal( T madman, double movementSpeed, double targetRange, BiPredicate<T, ? super CreeperEntity> targetPredicate ) { public ChargeCreeperGoal( T madman, double movementSpeed, double targetRange, BiPredicate<T, ? super CreeperEntity> targetPredicate ) {
this.madman = madman; this.madman = madman;
this.movementSpeed = movementSpeed; this.movementSpeed = movementSpeed;
@ -44,8 +46,14 @@ public class ChargeCreeperGoal<T extends MadScientistZombieEntity> extends Goal
@Override @Override
public boolean canUse() { public boolean canUse() {
if( madman.isPassenger() || !canUseWhileMounted && madman.isVehicle() ) return false; if( madman.isPassenger() || !canUseWhileMounted && madman.isVehicle() ) return false;
findCreeper(); 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() { private void findCreeper() {
@ -62,33 +70,36 @@ public class ChargeCreeperGoal<T extends MadScientistZombieEntity> 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. */ /** @return Called each update while active and returns true if this AI can remain active. */
@Override @Override
public boolean canContinueToUse() { 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. */ /** Called each tick while this AI is active. */
@Override @Override
public void tick() { public void tick() {
if( creeper == null || !targetPredicate.test( madman, creeper ) ) { if( creeper == null ) return;
findCreeper();
} final double distanceSq = madman.distanceToSqr( creeper );
else {
madman.getNavigation().moveTo( creeper, movementSpeed ); pathUpdateCooldown--;
madman.getLookControl().setLookAt( this.creeper.getX(), this.creeper.getEyeY(), this.creeper.getZ() ); 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 ) { if( !madman.getNavigation().moveTo( creeper, movementSpeed ) ) pathUpdateCooldown += 15;
MobHelper.charge( creeper ); pathTarget = creeper.position();
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;
} 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;
} }
} }
} }

View file

@ -12,6 +12,7 @@ import fathertoast.specialmobs.datagen.loot.LootTableBuilder;
import mcp.MethodsReturnNonnullByDefault; import mcp.MethodsReturnNonnullByDefault;
import net.minecraft.entity.*; import net.minecraft.entity.*;
import net.minecraft.entity.ai.attributes.Attributes; import net.minecraft.entity.ai.attributes.Attributes;
import net.minecraft.entity.monster.CreeperEntity;
import net.minecraft.inventory.EquipmentSlotType; import net.minecraft.inventory.EquipmentSlotType;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.potion.EffectInstance; import net.minecraft.potion.EffectInstance;
@ -22,6 +23,7 @@ import net.minecraft.world.World;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault; import javax.annotation.ParametersAreNonnullByDefault;
import java.util.function.BiPredicate;
@ParametersAreNonnullByDefault @ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault @MethodsReturnNonnullByDefault
@ -63,14 +65,15 @@ public class MadScientistZombieEntity extends _SpecialZombieEntity {
//--------------- Variant-Specific Implementations ---------------- //--------------- Variant-Specific Implementations ----------------
private final BiPredicate<MadScientistZombieEntity, ? super CreeperEntity> CHARGE_CREEPER_TARGET = ( madman, creeper ) ->
creeper.isAlive() && !creeper.isPowered() && madman.getSensing().canSee( creeper );
public MadScientistZombieEntity( EntityType<? extends _SpecialZombieEntity> entityType, World world ) { super( entityType, world ); } public MadScientistZombieEntity( EntityType<? extends _SpecialZombieEntity> entityType, World world ) { super( entityType, world ); }
/** Override to change this entity's AI goals. */ /** Override to change this entity's AI goals. */
@Override @Override
protected void registerVariantGoals() { protected void registerVariantGoals() {
AIHelper.insertGoal( goalSelector, 2, new ChargeCreeperGoal<>( AIHelper.insertGoal( goalSelector, 2, new ChargeCreeperGoal<>( this, 1.25, 20.0, CHARGE_CREEPER_TARGET ) );
this, getAttributeValue( Attributes.MOVEMENT_SPEED ) * 1.25, 20.0,
( madman, creeper ) -> creeper.isAlive() && !creeper.isPowered() && madman.getSensing().canSee( creeper ) ) );
} }
/** Override to change this entity's attack goal priority. */ /** Override to change this entity's attack goal priority. */