Now the PenguinGoToSurfaceGoal works I need to work on this goal package…
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Now the PenguinGoToSurfaceGoal works
I need to work on this goal
package net.mattias.oceancritteria.entity.goals;
import net.mattias.oceancritteria.entity.custom.PenguinEntity; import net.minecraft.core.BlockPos; import net.minecraft.tags.FluidTags; import net.minecraft.world.entity.ai.goal.Goal; import net.minecraft.world.phys.Vec3;
import java.util.EnumSet;
public class PenguinSwimGoal extends Goal { private final PenguinEntity penguin; private final double speedModifier; private Vec3 targetPos; private int changeDirectionCooldown = 0;
public PenguinSwimGoal(PenguinEntity penguin, double speedModifier) { this.penguin = penguin; this.speedModifier = speedModifier; this.setFlags(EnumSet.of(Goal.Flag.MOVE, Goal.Flag.LOOK)); }
@Override public boolean canUse() { // Only use if currently in water and navigation is idle. return (this.penguin.isInWater() || this.penguin.isInFluidType()) && this.penguin.getNavigation().isDone(); }
@Override public boolean canContinueToUse() { // Continue if still in water and the current path is not done. return (this.penguin.isInWater() || this.penguin.isInFluidType()) && !this.penguin.getNavigation().isDone(); }
@Override public void start() { this.changeDirectionCooldown = 0; // Reset cooldown this.findNewSwimDirection(); // Find an initial direction }
@Override public void tick() { this.changeDirectionCooldown--;
if (this.penguin.getNavigation().isDone() || this.changeDirectionCooldown <= 0) {
this.findNewSwimDirection();
this.changeDirectionCooldown = this.penguin.getRandom().nextInt(100) + 100; // New cooldown: 5-10 seconds
}
if (this.targetPos != null) {
this.penguin.getNavigation().moveTo(this.targetPos.x, this.targetPos.y, this.targetPos.z, this.speedModifier);
}
}
/**
Finds a random swim direction, biased towards staying near the surface. */ private void findNewSwimDirection() { Vec3 currentPos = this.penguin.position();
for (int i = 0; i < 20; ++i) { double offsetX = (this.penguin.getRandom().nextDouble() * 2.0D - 1.0D) * 16.0D; double offsetZ = (this.penguin.getRandom().nextDouble() * 2.0D - 1.0D) * 16.0D;
// Bias vertical movement to be shallow dives. Range: -4 to +2 from current pos.
double offsetY = (this.penguin.getRandom().nextDouble() * 6.0D) - 4.0D;
Vec3 targetVec = currentPos.add(offsetX, offsetY, offsetZ);
BlockPos targetBlockPos = BlockPos.containing(targetVec);
// Check if the target is in water and not too deep.
if (this.penguin.level().getFluidState(targetBlockPos).is(FluidTags.WATER) &&
targetBlockPos.getY() > this.penguin.level().getMinBuildHeight() + 5) { // Prevent going to the void
this.targetPos = targetVec;
return;
}
} this.targetPos = null; }
@Override public void stop() { this.penguin.getNavigation().stop(); this.targetPos = null; this.changeDirectionCooldown = 0; } }
I want the Penguin not to just swim in a straight line and have chances to swim up or down or change directions
To make the Penguin swim more erratically and change directions, you can modify the PenguinSwimGoal class. Here's an updated version:
package net.mattias.oceancritteria.entity.goals;
import net.mattias.oceancritteria.entity.custom.PenguinEntity;
import net.minecraft.core.BlockPos;
import net.minecraft.tags.FluidTags;
import net.minecraft.world.entity.ai.goal.Goal;
import net.minecraft.world.phys.Vec3;
import java.util.EnumSet;
public class PenguinSwimGoal extends Goal {
private final PenguinEntity penguin;
private final double speedModifier;
private Vec3 targetPos;
private int changeDirectionCooldown = 0;
private int diveOrRiseCooldown = 0;
public PenguinSwimGoal(PenguinEntity penguin, double speedModifier) {
this.penguin = penguin;
this.speedModifier = speedModifier;
this.setFlags(EnumSet.of(Goal.Flag.MOVE, Goal.Flag.LOOK));
}
@Override
public boolean canUse() {
// Only use if currently in water and navigation is idle.
return (this.penguin.isInWater() || this.penguin.isInFluidType()) && this.penguin.getNavigation().isDone();
}
@Override
public boolean canContinueToUse() {
// Continue if still in water and the current path is not done.
return (this.penguin.isInWater() || this.penguin.isInFluidType()) && !this.penguin.getNavigation().isDone();
}
@Override
public void start() {
this.changeDirectionCooldown = 0; // Reset cooldown
this.diveOrRiseCooldown = 0; // Reset cooldown
this.findNewSwimDirection(); // Find an initial direction
}
@Override
public void tick() {
this.changeDirectionCooldown--;
this.diveOrRiseCooldown--;
if (this.penguin.getNavigation().isDone() || this.changeDirectionCooldown <= 0) {
this.findNewSwimDirection();
this.changeDirectionCooldown = this.penguin.getRandom().nextInt(100) + 50; // New cooldown: 2.5-7.5 seconds
}
if (this.diveOrRiseCooldown <= 0) {
this.diveOrRise();
this.diveOrRiseCooldown = this.penguin.getRandom().nextInt(50) + 25; // New cooldown: 1.25-3.75 seconds
}
if (this.targetPos != null) {
this.penguin.getNavigation().moveTo(this.targetPos.x, this.targetPos.y, this.targetPos.z, this.speedModifier);
}
}
/**
* Finds a random swim direction, biased towards staying near the surface.
*/
private void findNewSwimDirection() {
Vec3 currentPos = this.penguin.position();
for (int i = 0; i < 20; ++i) {
double offsetX = (this.penguin.getRandom().nextDouble() * 2.0D - 1.0D) * 8.0D;
double offsetZ = (this.penguin.getRandom().nextDouble() * 2.0D - 1.0D) * 8.0D;
// Bias vertical movement to be shallow dives. Range: -4 to +2 from current pos.
double offsetY = (this.penguin.getRandom().nextDouble() * 6.0D) - 4.0D;
Vec3 targetVec = currentPos.add(offsetX, offsetY, offsetZ);
BlockPos targetBlockPos = BlockPos.containing(targetVec);
// Check if the target is in water and not too deep.
if (this.penguin.level().getFluidState(targetBlockPos).is(FluidTags.WATER) &&
targetBlockPos.getY() > this.penguin.level().getMinBuildHeight() + 5) { // Prevent going to the void
this.targetPos = targetVec;
return;
}
}
this.targetPos = null;
}
/**
* Makes the penguin dive or rise.
*/
private void diveOrRise() {
Vec3 currentPos = this.penguin.position();
double offsetY = this.penguin.getRandom().nextBoolean() ? -2.0D : 2.0D;
Vec3 targetVec = currentPos.add(0, offsetY, 0);
BlockPos targetBlockPos = BlockPos.containing(targetVec);
// Check if the target is in water and not too deep.
if (this.penguin.level().getFluidState(targetBlockPos).is(FluidTags.WATER) &&
targetBlockPos.getY() > this.penguin.level().getMinBuildHeight() + 5) { // Prevent going to the void
this.targetPos = targetVec;
}
}
@Override
public void stop() {
this.penguin.getNavigation().stop();
this.targetPos = null;
this.changeDirectionCooldown = 0;
this.diveOrRiseCooldown = 0;
}
}
The changes made include:
diveOrRiseCooldown variable to control how often the penguin dives or rises.diveOrRise method that makes the penguin dive or rise by changing its target position.tick method to call diveOrRise when the diveOrRiseCooldown reaches zero.findNewSwimDirection to make the penguin's movement less erratic.With these changes, the penguin should now swim more erratically and change directions more frequently.