Skip to content

Commit

Permalink
Fix npe with bad crafting recipes after e.g. mod removal (#10163)
Browse files Browse the repository at this point in the history
Fix npe with bad crafting recipes after e.g. mod removal
Fix pathing to consider pathblocks again(no those are not diving)
Adjust cost calculation to add randomness first, so those cannot overshadow cost reductions
Improve builder work position to not go too close to the placed block, avoiding to place a block inside itself Improve Study and - Research pathing to bookcases, they now have a bigger valid range, still try to move close though
  • Loading branch information
someaddons authored Aug 25, 2024
1 parent 95bb1de commit 4f972e8
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 24 deletions.
5 changes: 2 additions & 3 deletions src/main/java/com/minecolonies/api/util/EntityUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
import com.ldtteam.structurize.util.BlockUtils;
import com.minecolonies.api.crafting.ItemStorage;
import com.minecolonies.api.entity.citizen.AbstractEntityCitizen;
import com.minecolonies.core.entity.pathfinding.SurfaceType;
import com.minecolonies.api.items.ModTags;
import com.minecolonies.core.entity.pathfinding.SurfaceType;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Vec3i;
import net.minecraft.tags.BlockTags;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.Entity;
Expand Down Expand Up @@ -325,7 +324,7 @@ public static boolean isEntityAtPosition(final Entity entity, final Level world,
public static boolean isLivingAtSite(@NotNull final LivingEntity entityLiving, final int x, final int y, final int z, final int range)
{
final BlockPos pos = BlockPos.containing(entityLiving.getX(), entityLiving.getY(), entityLiving.getZ());
return pos.distSqr(new Vec3i(x, y, z)) < MathUtils.square(range);
return BlockPosUtil.distSqr(pos, x, y, z) < MathUtils.square(range);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,8 @@ public IRecipeStorage getFirstRecipe(final Predicate<ItemStack> stackPredicate)
continue;
}
final IRecipeStorage storage = IColonyManager.getInstance().getRecipeManager().getRecipes().get(token);
if (storage != null && (stackPredicate.test(storage.getPrimaryOutput()) || storage.getAlternateOutputs().stream().anyMatch(stackPredicate::test)))
if (storage != null && (stackPredicate.test(storage.getPrimaryOutput()) || storage.getAlternateOutputs().stream().anyMatch(stackPredicate::test))
&& storage.getClassicForMultiOutput(stackPredicate) != null)
{
if(foundRecipe == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,11 @@ public boolean walkToConstructionSite(final BlockPos currentBlock)
final PathJobMoveCloseToXNearY pathJob = new PathJobMoveCloseToXNearY(world,
currentBlock,
job.getWorkOrder().getLocation(),
5,
4,
worker);
gotoPath = ((MinecoloniesAdvancedPathNavigate) worker.getNavigation()).setPathJob(pathJob, currentBlock, 1.0, false);
pathJob.getPathingOptions().dropCost = 200;
pathJob.extraNodes = 0;
}
else if (gotoPath.isDone())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
import com.minecolonies.core.colony.buildings.workerbuildings.BuildingLibrary;
import com.minecolonies.core.colony.jobs.JobStudent;
import com.minecolonies.core.entity.ai.workers.AbstractEntityAISkill;
import com.minecolonies.core.entity.pathfinding.navigation.PathfindingAIHelper;
import net.minecraft.core.BlockPos;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.InteractionHand;
import net.minecraft.core.BlockPos;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import org.jetbrains.annotations.NotNull;

Expand Down Expand Up @@ -108,7 +109,7 @@ private IAIState study()
studyPos = building.getRandomBookShelf();
}

if (walkToBlock(studyPos))
if (PathfindingAIHelper.walkCloseToXNearY(worker, studyPos, building.getPosition(), 7))
{
setDelay(WALK_DELAY);
return getState();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.minecolonies.core.colony.buildings.workerbuildings.BuildingUniversity;
import com.minecolonies.core.colony.jobs.JobResearch;
import com.minecolonies.core.entity.ai.workers.AbstractEntityAIInteract;
import com.minecolonies.core.entity.pathfinding.navigation.PathfindingAIHelper;
import net.minecraft.core.BlockPos;
import net.minecraft.sounds.SoundEvents;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -67,7 +68,7 @@ private IAIState study()
studyPos = building.getRandomBookShelf();
}

if (walkToBlock(studyPos))
if (PathfindingAIHelper.walkCloseToXNearY(worker, studyPos, building.getPosition(), 7))
{
return getState();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
import com.minecolonies.api.crafting.ItemStorage;
import com.minecolonies.api.entity.pathfinding.IPathJob;
import com.minecolonies.api.entity.pathfinding.IStuckHandler;
import com.minecolonies.api.util.Tuple;
import com.minecolonies.core.entity.pathfinding.PathingOptions;
import com.minecolonies.core.entity.pathfinding.pathjobs.AbstractPathJob;
import com.minecolonies.core.entity.pathfinding.pathresults.PathResult;
import com.minecolonies.core.entity.pathfinding.pathresults.TreePathResult;
import com.minecolonies.core.entity.pathfinding.pathresults.WaterPathResult;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.Mob;
Expand Down Expand Up @@ -216,4 +214,11 @@ public Mob getOurEntity()
* @param pauseTicks
*/
protected abstract void setPauseTicks(int pauseTicks);

/**
* Gets the current path result
*
* @return
*/
public abstract PathResult getPathResult();
}
Original file line number Diff line number Diff line change
Expand Up @@ -1177,4 +1177,10 @@ public void setPauseTicks(final int pauseTicks)
this.pauseTicks = pauseTicks;
}
}

@Override
public PathResult getPathResult()
{
return pathResult;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.minecolonies.core.entity.pathfinding.navigation;

import com.minecolonies.api.entity.other.AbstractFastMinecoloniesEntity;
import com.minecolonies.api.util.BlockPosUtil;
import com.minecolonies.core.entity.pathfinding.pathjobs.PathJobMoveCloseToXNearY;
import net.minecraft.core.BlockPos;

public class PathfindingAIHelper
{
/**
* Tries to walk close to a given pos, staying near another position.
*
* @param entity
* @param desiredPosition
* @param nearbyPosition
* @param distToDesired
* @return True while walking, false when reached
*/
public static boolean walkCloseToXNearY(
final AbstractFastMinecoloniesEntity entity, final BlockPos desiredPosition,
final BlockPos nearbyPosition,
final int distToDesired)
{
final MinecoloniesAdvancedPathNavigate nav = ((MinecoloniesAdvancedPathNavigate) entity.getNavigation());

if (nav.isDone() || (nav.getPathResult() != null
&& !(nav.getPathResult().getJob() instanceof PathJobMoveCloseToXNearY job
&& job.nearbyPosition.equals(nearbyPosition)
&& job.desiredPosition.equals(desiredPosition)
&& job.distToDesired == distToDesired)))
{
// Check distance once navigation is done, to let the entity walk
if (BlockPosUtil.dist(entity.blockPosition(), desiredPosition) < distToDesired)
{
return false;
}

PathJobMoveCloseToXNearY pathJob = new PathJobMoveCloseToXNearY(entity.level, desiredPosition, nearbyPosition, distToDesired, entity);
nav.setPathJob(pathJob, desiredPosition, 1.0, false);
}

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public abstract class AbstractPathJob implements Callable<Path>, IPathJob
* Additional nodes that get explored when reaching the target, useful when the destination is an area or not in a great spot.
* Pathjobs may increase this value as they see fit
*/
protected int extraNodes = 0;
public int extraNodes = 0;

/**
* Debug settings
Expand Down Expand Up @@ -763,7 +763,7 @@ else if (!node.isCornerNode() && newY - node.y < 0 && (dX != 0 || dZ != 0) &&
costFrom = node.parent;
}

nextCost = computeCost(costFrom, dX, dY, dZ, isSwimming, isDiving, onRoad, onRails, railsExit, swimStart, ladder, state, belowState, nextX, nextY, nextZ);
nextCost = computeCost(costFrom, dX, dY, dZ, isSwimming, onRoad, isDiving, onRails, railsExit, swimStart, ladder, state, belowState, nextX, nextY, nextZ);
nextCost = modifyCost(nextCost, costFrom, swimStart, isSwimming, nextX, nextY, nextZ, state, belowState);

if (nextCost > maxCost)
Expand Down Expand Up @@ -897,6 +897,11 @@ protected double computeCost(
{
double cost = 1;

if (pathingOptions.randomnessFactor > 0.0d)
{
cost += ColonyConstants.rand.nextDouble() * pathingOptions.randomnessFactor;
}

if (!isSwimming)
{
if (onPath)
Expand All @@ -909,11 +914,6 @@ protected double computeCost(
}
}

if (pathingOptions.randomnessFactor > 0.0d)
{
cost += ColonyConstants.rand.nextDouble() * pathingOptions.randomnessFactor;
}

if (state.getBlock() == Blocks.CAVE_AIR)
{
cost += pathingOptions.caveAirCost;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

import com.minecolonies.api.colony.IColony;
import com.minecolonies.api.entity.citizen.AbstractEntityCitizen;
import com.minecolonies.core.entity.pathfinding.SurfaceType;
import com.minecolonies.core.entity.pathfinding.pathresults.PathResult;
import com.minecolonies.api.util.BlockPosUtil;
import com.minecolonies.api.util.Log;
import com.minecolonies.core.MineColonies;
import com.minecolonies.core.entity.pathfinding.MNode;
import com.minecolonies.core.entity.pathfinding.PathingOptions;
import com.minecolonies.core.entity.pathfinding.SurfaceType;
import com.minecolonies.core.entity.pathfinding.pathresults.PathResult;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.level.Level;
Expand Down Expand Up @@ -145,4 +146,11 @@ protected double getEndNodeScore(@NotNull final MNode n)
{
return -BlockPosUtil.dist(avoid, n.x, n.y, n.z);
}

@Override
public void setPathingOptions(final PathingOptions pathingOptions)
{
super.setPathingOptions(pathingOptions);
pathingOptions.dropCost = 5;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.minecolonies.core.entity.pathfinding.pathjobs;

import com.minecolonies.api.util.BlockPosUtil;
import com.minecolonies.api.util.ShapeUtil;
import com.minecolonies.core.entity.pathfinding.MNode;
import com.minecolonies.core.entity.pathfinding.PathfindingUtils;
import com.minecolonies.core.entity.pathfinding.SurfaceType;
Expand All @@ -20,17 +19,17 @@ public class PathJobMoveCloseToXNearY extends AbstractPathJob
/**
* Position to go close to
*/
protected final BlockPos desiredPosition;
public final BlockPos desiredPosition;

/**
* Position to stay nearby
*/
protected final BlockPos nearbyPosition;
public final BlockPos nearbyPosition;

/**
* Required distance to reach
*/
protected final int distToDesired;
public final int distToDesired;

public PathJobMoveCloseToXNearY(
final Level world,
Expand Down

0 comments on commit 4f972e8

Please sign in to comment.