Skip to content

Commit

Permalink
Smallfixes (#10120)
Browse files Browse the repository at this point in the history
Improve entity setshift toggle performance
Add limits to pathing pause time, limit unstuck handler max range
  • Loading branch information
someaddons authored and Raycoms committed Aug 18, 2024
1 parent c17a828 commit 70b8938
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,29 @@ public void updateSwimAmount()
{

}

/**
* Static Byte values to avoid frequent autoboxing
*/
final Byte ENABLE = 2;
final Byte DISABLE = 0;

@Override
public void setShiftKeyDown(boolean enable)
{
if (enable)
{
this.entityData.set(DATA_SHARED_FLAGS_ID, ENABLE);
}
else
{
this.entityData.set(DATA_SHARED_FLAGS_ID, DISABLE);
}
}

@Override
public boolean isShiftKeyDown()
{
return (this.entityData.get(DATA_SHARED_FLAGS_ID)).byteValue() == ENABLE.byteValue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
import com.minecolonies.api.entity.ModEntities;
import com.minecolonies.api.entity.citizen.AbstractEntityCitizen;
import com.minecolonies.api.entity.other.MinecoloniesMinecart;
import com.minecolonies.api.entity.pathfinding.*;
import com.minecolonies.api.entity.pathfinding.IStuckHandler;
import com.minecolonies.api.util.*;
import com.minecolonies.api.util.constant.ColonyConstants;
import com.minecolonies.core.entity.pathfinding.*;
import com.minecolonies.core.entity.pathfinding.PathFindingStatus;
import com.minecolonies.core.entity.pathfinding.PathPointExtended;
import com.minecolonies.core.entity.pathfinding.Pathfinding;
import com.minecolonies.core.entity.pathfinding.PathfindingUtils;
import com.minecolonies.core.entity.pathfinding.pathjobs.*;
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 com.minecolonies.core.util.WorkerUtil;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
Expand All @@ -22,6 +24,7 @@
import net.minecraft.world.entity.Mob;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.BaseRailBlock;
import net.minecraft.world.level.block.LadderBlock;
Expand All @@ -40,6 +43,7 @@
import java.util.HashSet;
import java.util.List;

import static com.minecolonies.api.util.constant.Constants.TICKS_SECOND;
import static com.minecolonies.core.entity.pathfinding.PathFindingStatus.IN_PROGRESS_FOLLOWING;
import static com.minecolonies.core.entity.pathfinding.pathjobs.AbstractPathJob.MAX_NODES;

Expand Down Expand Up @@ -359,7 +363,7 @@ else if (this.path != null && !this.path.isDone())
Vec3 vector3d2 = path.getNextEntityPos(mob);
BlockPos blockpos = BlockPos.containing(vector3d2);

if (WorldUtil.isEntityBlockLoaded(level, blockpos))
if (ChunkPos.asLong(blockpos) == mob.chunkPosition().toLong() || WorldUtil.isEntityBlockLoaded(level, blockpos))
{
mob.getMoveControl()
.setWantedPosition(vector3d2.x,
Expand Down Expand Up @@ -391,11 +395,12 @@ public static double getSmartGroundY(final BlockGetter world, final BlockPos pos
{
final BlockPos blockpos = pos.below();
final VoxelShape voxelshape = world.getBlockState(blockpos).getCollisionShape(world, blockpos);
if (voxelshape.isEmpty() || voxelshape.max(Direction.Axis.Y) < 1.0)
final double maxY = ShapeUtil.max(voxelshape, Direction.Axis.Y);
if (maxY < 1.0)
{
return pos.getY();
}
return blockpos.getY() + voxelshape.max(Direction.Axis.Y);
return blockpos.getY() + maxY;
}

@Nullable
Expand Down Expand Up @@ -955,7 +960,7 @@ protected void followThePath()
boolean wentAhead = false;
boolean isTracking = PathfindingUtils.trackingMap.containsValue(ourEntity.getUUID());

final HashSet<BlockPos> reached = new HashSet<>();
HashSet<BlockPos> reached = null;
// Look at multiple points, incase we're too fast
for (int i = this.path.getNextNodeIndex(); i < Math.min(this.path.getNodeCount(), this.path.getNextNodeIndex() + 4); i++)
{
Expand All @@ -969,6 +974,10 @@ protected void followThePath()

if (isTracking)
{
if (reached == null)
{
reached = new HashSet<>();
}
final Node point = path.getNode(i);
reached.add(new BlockPos(point.x, point.y, point.z));
}
Expand Down Expand Up @@ -1152,6 +1161,14 @@ public double getAvgHeuristicModifier()
@Override
public void setPauseTicks(final int pauseTicks)
{
this.pauseTicks = pauseTicks;
if (pauseTicks > TICKS_SECOND * 120)
{
Log.getLogger().warn("Tried to pause entity pathfinding for " + mob + " too long for " + pauseTicks + " ticks.", new Exception());
this.pauseTicks = 50;
}
else
{
this.pauseTicks = pauseTicks;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ private void tryUnstuck(final AbstractAdvancedPathNavigate navigator)
}

navigator.stop();
final int range = ColonyConstants.rand.nextInt(20) + BlockPosUtil.distManhattan(navigator.ourEntity.blockPosition(), prevDestination);
final int range = ColonyConstants.rand.nextInt(20) + Math.min(100, BlockPosUtil.distManhattan(navigator.ourEntity.blockPosition(), prevDestination));
navigator.moveTowards(navigator.getOurEntity().blockPosition().relative(movingAwayDir, 40), range, 1.0f);
movingAwayDir = movingAwayDir.getClockWise();
navigator.setPauseTicks(range * TICKS_PER_BLOCK);
Expand Down

0 comments on commit 70b8938

Please sign in to comment.