Skip to content

Commit

Permalink
Make pond actually check for what is on wiki (#10175)
Browse files Browse the repository at this point in the history
add waterPathJob (and problematic pond positions) to path tracking debug
fix pond iterating so it is actually 7x7x2, keep 30% chance to not check the second level, below example of all problems within one checked position (see some blocks on second level not being checked)
will port once merged
  • Loading branch information
Nightenom authored Sep 5, 2024
1 parent 658c910 commit e4cab77
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 29 deletions.
38 changes: 16 additions & 22 deletions src/main/java/com/minecolonies/api/util/Pond.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

import com.minecolonies.api.util.constant.ColonyConstants;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.BlockPos.MutableBlockPos;
import net.minecraft.tags.FluidTags;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.FluidState;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* Utility class to search for fisher ponds.
Expand All @@ -18,44 +21,35 @@ public final class Pond
* The minimum pond requirements.
*/
public static final int WATER_POOL_WIDTH_REQUIREMENT = 5;
public static final int WATER_POOL_LENGTH_REQUIREMENT = 5;
public static final int WATER_DEPTH_REQUIREMENT = 2;

/**
* Checks if on position "water" really is water, if the water is connected to land and if the pond is big enough (bigger then 20).
*
* @param world The world the player is in.
* @param water The coordinate to check.
* @param problematicPosition Will contain position of problematic block (if not null && pond was not found).
* @return true if water.
*/
public static boolean checkWater(@NotNull final BlockGetter world, @NotNull final BlockPos water)
public static boolean checkPond(@NotNull final BlockGetter world, @NotNull final BlockPos water, @Nullable final MutableBlockPos problematicPosition)
{
final BlockPos.MutableBlockPos tempPos = water.mutable();

for (int i = 1; i < WATER_DEPTH_REQUIREMENT; i++)
{
if (!isWaterForFishing(world, world.getBlockState(tempPos.set(water.getX(), water.getY() - i, water.getZ())), tempPos))
{
return false;
}
}

for (int x = -WATER_POOL_WIDTH_REQUIREMENT / 2; x < WATER_POOL_WIDTH_REQUIREMENT / 2; x++)
for (final MutableBlockPos tempPos : BlockPos.spiralAround(water, (WATER_POOL_WIDTH_REQUIREMENT - 1) / 2, Direction.SOUTH, Direction.EAST))
{
for (int z = -WATER_POOL_LENGTH_REQUIREMENT; z < WATER_POOL_LENGTH_REQUIREMENT / 2; z++)
for (int y = 0; y < WATER_DEPTH_REQUIREMENT; y++)
{
tempPos.set(water.getX() + x, water.getY(), water.getZ() + z);
final BlockState state = world.getBlockState(tempPos);

if (!isWaterForFishing(world, state, tempPos))
if (!isWaterForFishing(world, tempPos.setY(tempPos.getY() - y)))
{
if (problematicPosition != null)
{
problematicPosition.set(tempPos);
}
return false;
}

// 70% chance to check, to on avg prefer cleared areas
if (ColonyConstants.rand.nextInt(100) >= 30 && !isWaterForFishing(world, world.getBlockState(tempPos.set(water.getX(), water.getY() - 1, water.getZ())), tempPos))
if (ColonyConstants.rand.nextInt(100) < 30)
{
return false;
break;
}
}
}
Expand All @@ -67,12 +61,12 @@ public static boolean checkWater(@NotNull final BlockGetter world, @NotNull fina
* Checks if the water is fine for fishing, see vanilla FishingHook checks
*
* @param world
* @param state
* @param pos
* @return
*/
public static boolean isWaterForFishing(final BlockGetter world, final BlockState state, final BlockPos pos)
public static boolean isWaterForFishing(final BlockGetter world, final BlockPos pos)
{
final BlockState state = world.getBlockState(pos);
if (!state.isAir() && !state.is(Blocks.LILY_PAD))
{
FluidState fluidstate = state.getFluidState();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,10 @@ private IAIState findNewWater()
pathResult = searchWater(SEARCH_RANGE * 3, 1.0D, job.getPonds());
return getState();
}
if (pathResult.isDone())
{
pathResult.getJob().syncDebug();
}
if (pathResult.failedToReachDestination())
{
return setRandomWater();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,23 @@
import com.minecolonies.api.util.Pond;
import com.minecolonies.api.util.Tuple;
import com.minecolonies.core.entity.pathfinding.MNode;
import com.minecolonies.core.entity.pathfinding.PathfindingUtils;
import com.minecolonies.core.entity.pathfinding.PathingOptions;
import com.minecolonies.core.entity.pathfinding.SurfaceType;
import com.minecolonies.core.entity.pathfinding.pathresults.PathResult;
import com.minecolonies.core.entity.pathfinding.pathresults.WaterPathResult;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.Entity;
import net.minecraft.core.BlockPos.MutableBlockPos;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.level.ClipContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.pathfinder.Path;
import net.minecraft.world.phys.HitResult;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Find and return a path to the nearest water. Created: March 25, 2016
Expand Down Expand Up @@ -81,7 +77,8 @@ protected boolean isAtDestination(@NotNull final MNode n)
return false;
}

if (n.isSwimming() && Pond.checkWater(world, tempWorldPos.set(n.x, n.y - 1, n.z)))
final MutableBlockPos problemPos = debugDrawEnabled ? BlockPosUtil.SAFE_ZERO.mutable() : null;
if (n.isSwimming() && Pond.checkPond(world, tempWorldPos.set(n.x, n.y - 1, n.z), problemPos))
{
for (Tuple<BlockPos, BlockPos> existingPond : ponds)
{
Expand All @@ -102,6 +99,12 @@ protected boolean isAtDestination(@NotNull final MNode n)
}
}

// node is not pond -> debug
if (problemPos != null && !problemPos.equals(BlockPosUtil.SAFE_ZERO))
{
debugNodesExtra.add(new MNode(n, problemPos.getX(), problemPos.getY(), problemPos.getZ(), -1, -1));
}

return false;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.minecolonies.core.entity.pathfinding.pathresults;

import com.minecolonies.core.entity.pathfinding.pathjobs.PathJobFindWater;
import net.minecraft.core.BlockPos;

/**
* Contains the result of the path job to find water.
*/
public class WaterPathResult extends PathResult
public class WaterPathResult extends PathResult<PathJobFindWater>
{
/**
* The position of the parent (stand block).
Expand Down

0 comments on commit e4cab77

Please sign in to comment.