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
Of course! The problem with the current PenguinSwimGoal is that it picks a relatively distant point (up to 16 blocks away) and swims in a straight line towards it for a long time (5-10 seconds). This creates very linear, robotic movement.
To make the swimming more natural and dynamic, we need the penguin to:
Minecraft actually has a built-in goal that is perfect for this: RandomSwimmingGoal. It's designed specifically for mobs that need to wander around randomly while in water. It's a much simpler and more robust solution than writing a custom one from scratch.
The best approach is to replace your custom PenguinSwimGoal with the vanilla RandomSwimmingGoal.
PenguinSwimGoal.java file. You won't need it.PenguinEntity class, find where you register your goals (likely a method called registerGoals()).PenguinSwimGoal.RandomSwimmingGoal instead.Here's how you would add it inside your PenguinEntity class:
import net.minecraft.world.entity.ai.goal.RandomSwimmingGoal;
// ... other imports
public class PenguinEntity extends Animal {
// ... constructor and other methods
@Override
protected void registerGoals() {
super.registerGoals();
// Other goals like PanicGoal, LookAtPlayerGoal, etc.
// this.goalSelector.addGoal(0, new PenguinGoToSurfaceGoal(this, 1.0D)); // You probably have this
// ADD THIS GOAL for idle swimming
this.goalSelector.addGoal(1, new RandomSwimmingGoal(this, 1.0D, 40));
// Other goals...
}
// ... rest of the entity class
}
Why RandomSwimmingGoal is better:
new RandomSwimmingGoal(this, 1.0D, 40)
this: The penguin entity.1.0D: The speed modifier.40: The interval in ticks. It will try to find a new spot to swim to every 40 ticks (2 seconds) on average. This automatically prevents long, straight swimming paths.RandomPos generator to find valid, reachable water blocks, avoiding getting stuck on the seafloor or land.If you absolutely want to keep your custom goal to add more unique logic later, we can still fix it. Here are the key changes to make it behave more dynamically.
The main idea is to shorten the distances and the timers.
Here is the revised PenguinSwimGoal.java:
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.entity.ai.util.RandomPos;
import net.minecraft.world.phys.Vec3;
import java.util.EnumSet;
public class PenguinSwimGoal extends Goal {
private final PenguinEntity penguin;
private final double speedModifier;
private int timeToNextUpdate; // Renamed for clarity
public PenguinSwimGoal(PenguinEntity penguin, double speedModifier) {
this.penguin = penguin;
this.speedModifier = speedModifier;
this.setFlags(EnumSet.of(Goal.Flag.MOVE)); // LOOK is not strictly needed as moveTo handles it
}
@Override
public boolean canUse() {
// Only use if currently in water.
// We add a random chance to prevent this goal from always being active,
// allowing other water goals (like GoToSurface) to run more easily.
return this.penguin.isInWater() && this.penguin.getRandom().nextInt(40) == 0;
}
@Override
public boolean canContinueToUse() {
// Continue if still in water and the current path is not stuck or done.
return this.penguin.isInWater() && !this.penguin.getNavigation().isDone();
}
@Override
public void start() {
// Find a direction immediately on starting
this.setRandomLocation();
}
@Override
public void tick() {
this.timeToNextUpdate--;
// If the timer runs out, find a new direction to keep movement dynamic
if (this.timeToNextUpdate <= 0) {
this.setRandomLocation();
}
}
private void setRandomLocation() {
// Reset the timer for the next direction change. 2 to 4 seconds.
this.timeToNextUpdate = 40 + this.penguin.getRandom().nextInt(40);
// Use Minecraft's built-in utility to find a random point in the water.
// This is much more reliable than calculating offsets manually.
// It looks for a point within a 10x3x10 area around the penguin.
Vec3 target = RandomPos.generateRandomPos(this.penguin, 10, 4);
// If a valid water position is found, move to it.
if (target != null && this.penguin.level().getFluidState(BlockPos.containing(target)).is(FluidTags.WATER)) {
this.penguin.getNavigation().moveTo(target.x, target.y, target.z, this.speedModifier);
}
}
@Override
public void stop() {
this.penguin.getNavigation().stop();
super.stop();
}
}
canUse(): I added this.penguin.getRandom().nextInt(40) == 0. This makes the goal activate randomly (on average, once every 2 seconds if conditions are met). This prevents it from "hogging" the AI and gives other goals a chance to run. It also makes the penguin pause occasionally before deciding to swim again, which looks more natural.tick() and timeToNextUpdate: The goal no longer waits until the penguin reaches its destination. It now has a short timer (timeToNextUpdate). Every 2-4 seconds, it will pick a new destination, interrupting the old path. This is the core change that stops the long, straight-line swimming.setRandomLocation() and RandomPos.generateRandomPos(): Instead of manually calculating random x, y, z offsets, we now use the much more powerful and reliable RandomPos.generateRandomPos(this.penguin, 10, 4).
10 is the horizontal distance, and the 4 is the vertical distance. This keeps the targets close, forcing more turns and dynamic movement.I strongly recommend Option 1 for its simplicity and reliability, but Option 2 will also give you the desired effect and serves as a great template if you want to add more complex custom swimming behaviors later on.