Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Several navigation fix #10406

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class PathingOptions
/**
* Maximum cost used
*/
public static final int MAX_COST = 25;
public static final int MAX_COST = 100;

/**
* Additional cost of jumping
Expand All @@ -22,6 +22,11 @@ public class PathingOptions
*/
public double dropCost = 1D;

/**
* Additional cost of dropping damage
*/
public double dropDamage = 25D;

/**
* Cost improvement of paths - base 1.
*/
Expand All @@ -32,6 +37,11 @@ public class PathingOptions
*/
public double onRailCost = 1 / 10D;

/**
* The cost to open a door.
*/
public double openDoorCost = 4;

/**
* The rails exit cost.
*/
Expand All @@ -40,7 +50,7 @@ public class PathingOptions
/**
* Additional cost of swimming - base 1.
*/
public double swimCost = 2D;
public double swimCost = 4D;

/**
* Additional cost of cave air.
Expand All @@ -50,7 +60,7 @@ public class PathingOptions
/**
* Additional cost enter entering water
*/
public double swimCostEnter = 24D;
public double swimCostEnter = 4D;

/**
* Cost to traverse trap doors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.Half;
import net.minecraft.world.level.block.state.properties.StairsShape;
import net.minecraft.world.level.pathfinder.Node;
import net.minecraft.world.level.pathfinder.Path;
import net.minecraft.world.level.pathfinder.PathType;
Expand Down Expand Up @@ -733,6 +734,7 @@ else if (!node.isCornerNode() && newY - node.y < 0 && (dX != 0 || dZ != 0) &&
final BlockState aboveState = cachedBlockLookup.getBlockState(nextX, nextY + 1, nextZ);
final BlockState state = cachedBlockLookup.getBlockState(nextX, nextY, nextZ);
final BlockState belowState = cachedBlockLookup.getBlockState(nextX, nextY - 1, nextZ);
final BlockState lastState = cachedBlockLookup.getBlockState(node.x, node.y, node.z);

final boolean isSwimming = calculateSwimming(belowState, state, aboveState, nextNode);
if (isSwimming && !pathingOptions.canSwim())
Expand Down Expand Up @@ -760,7 +762,7 @@ else if (!node.isCornerNode() && newY - node.y < 0 && (dX != 0 || dZ != 0) &&
costFrom = node.parent;
}

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

if (nextCost > maxCost)
Expand Down Expand Up @@ -888,7 +890,7 @@ protected double computeCost(
final boolean railsExit,
final boolean swimStart,
final boolean ladder,
final BlockState state, final BlockState below,
final BlockState state, final BlockState below, final BlockState last,
final int x, final int y, final int z)
{
double cost = 1;
Expand All @@ -897,7 +899,8 @@ protected double computeCost(
{
cost += ColonyConstants.rand.nextDouble() * pathingOptions.randomnessFactor;
}

// todo The speed of the citizen should be divided here, and distinguish costs that are related
// with speed and not related with speed (e.g. on rail is not related with speed)
if (!isSwimming)
{
if (onPath)
Expand All @@ -917,15 +920,32 @@ protected double computeCost(

if (!isDiving)
{
if (dY != 0 && !(ladder && parent.isLadder()) && !(Math.abs(dY) == 1 && below.is(BlockTags.STAIRS)))
boolean correctlyOnStairs = false;
if(Math.abs(dY) == 1 && below.getBlock() instanceof StairBlock && below.getValue(StairBlock.HALF) == Half.BOTTOM)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please reformat your code to ensure it's up to the defined code style (in this case, space between if and (

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please reformat your code to ensure it's up to the defined code style (in this case, space between if and (

Done.

{
Direction facing = below.getValue(StairBlock.FACING);
StairsShape shape = below.getValue(StairBlock.SHAPE);
if(dX == dY) correctlyOnStairs = facing == Direction.EAST || (facing == Direction.NORTH && shape == StairsShape.OUTER_RIGHT) || (facing == Direction.SOUTH && shape == StairsShape.OUTER_LEFT);
else if(dX != 0) correctlyOnStairs = facing == Direction.WEST || (facing == Direction.SOUTH && shape == StairsShape.OUTER_RIGHT) || (facing == Direction.NORTH && shape == StairsShape.OUTER_LEFT);
else if(dZ == dY) correctlyOnStairs = facing == Direction.SOUTH || (facing == Direction.EAST && shape == StairsShape.OUTER_RIGHT) || (facing == Direction.WEST && shape == StairsShape.OUTER_LEFT);
else if(dZ != 0) correctlyOnStairs = facing == Direction.NORTH || (facing == Direction.WEST && shape == StairsShape.OUTER_RIGHT) || (facing == Direction.EAST && shape == StairsShape.OUTER_LEFT);
}
if (dY != 0 && !(ladder && parent.isLadder()) && !correctlyOnStairs)
{
if (dY > 0)
{
cost += pathingOptions.jumpCost;
}
else if (pathingOptions.dropCost != 0)
//else if (pathingOptions.dropCost != 0)
//{
// cost += pathingOptions.dropCost * Math.abs(dY * dY * dY);
//}
if(dY < -3.375 && pathingOptions.dropDamage != 0)
{
cost += pathingOptions.dropCost * Math.abs(dY * dY * dY);
//Avoid falling damage.
if(!PathfindingUtils.isWater(world, null, below, null)){
cost += pathingOptions.dropDamage * (-dY - 3.375);
}
}
}
}
Expand Down Expand Up @@ -969,7 +989,23 @@ else if (!ShapeUtil.isEmpty(state.getCollisionShape(cachedBlockLookup, tempWorld
cost += pathingOptions.divingCost;
}
}

if (state.getBlock() instanceof DoorBlock)
{
boolean open = state.getValue(DoorBlock.OPEN);
Direction facing = state.getValue(DoorBlock.FACING);
boolean targetState = (facing == Direction.EAST || facing == Direction.WEST);
boolean controlled = last.getBlock() instanceof BasePressurePlateBlock;
if(dZ != 0) targetState = !targetState;
if(controlled)
{
if(targetState) cost += pathingOptions.openDoorCost;
else cost += 100; //This is a trap!!!
}
else if(open != targetState)
{
cost += pathingOptions.openDoorCost;
}
}
return cost;
}

Expand Down