From 7c1f25ecc1101044e29ee2e6a0d6eaea7a6dc8ef Mon Sep 17 00:00:00 2001 From: someaddons <38401808+someaddons@users.noreply.github.com> Date: Sun, 22 Dec 2024 14:00:54 +0100 Subject: [PATCH] Reworked AI to pathfinding link: (#10543) -AI now passes along more information about safe target locations and is using better fitting pathfinding tasks when going to buildings All pathfinding calls now return true on reaching their target, and false while walking unlike being mixed before All logic for creating pathing tasks is now consolidated in a dedicated util: EntityNavigationUtils, slimming down the Navigator class a bit. --- .../api/colony/buildings/IBuilding.java | 6 - .../entity/citizen/AbstractEntityCitizen.java | 17 -- .../minecolonies/api/util/BlockPosUtil.java | 27 -- .../minecolonies/api/util/EntityUtils.java | 58 +--- .../minecolonies/api/util/ItemStackUtils.java | 77 ----- .../minecolonies/core/colony/CitizenData.java | 4 +- .../colony/buildings/AbstractBuilding.java | 55 ---- .../pirateEvent/ShipBasedRaiderUtils.java | 2 +- .../core/entity/ai/combat/AttackMoveAI.java | 7 +- .../minimal/EntityAICitizenAvoidEntity.java | 13 +- .../ai/minimal/EntityAICitizenChild.java | 24 +- .../ai/minimal/EntityAICitizenWander.java | 12 +- .../entity/ai/minimal/EntityAIEatTask.java | 14 +- .../ai/minimal/EntityAIMournCitizen.java | 38 +-- .../entity/ai/minimal/EntityAISickTask.java | 26 +- .../core/entity/ai/minimal/EntityAISleep.java | 13 +- .../entity/ai/visitor/EntityAIVisitor.java | 22 +- .../ai/workers/AbstractEntityAIBasic.java | 95 ++++-- .../ai/workers/AbstractEntityAIInteract.java | 93 +----- .../ai/workers/AbstractEntityAIStructure.java | 11 +- .../workers/AbstractEntityAIUsesFurnace.java | 8 +- .../builder/EntityAIStructureBuilder.java | 4 +- .../crafting/AbstractEntityAICrafting.java | 7 +- .../AbstractEntityAIRequestSmelter.java | 10 +- .../crafting/EntityAIConcreteMixer.java | 16 +- .../crafting/EntityAIWorkAlchemist.java | 28 +- .../workers/crafting/EntityAIWorkCrusher.java | 11 +- .../workers/crafting/EntityAIWorkSifter.java | 10 +- .../ai/workers/education/EntityAIStudy.java | 5 +- .../workers/education/EntityAIWorkPupil.java | 27 +- .../education/EntityAIWorkResearcher.java | 5 +- .../education/EntityAIWorkTeacher.java | 8 +- .../workers/guard/AbstractEntityAIFight.java | 2 +- .../workers/guard/AbstractEntityAIGuard.java | 53 ++-- .../ai/workers/guard/DruidCombatAI.java | 17 +- .../ai/workers/guard/EntityAIDruid.java | 10 +- .../ai/workers/guard/EntityAIRanger.java | 6 +- .../ai/workers/guard/KnightCombatAI.java | 6 +- .../ai/workers/guard/RangerCombatAI.java | 3 +- .../training/AbstractEntityAITraining.java | 35 +-- .../training/EntityAICombatTraining.java | 13 +- .../workers/production/EntityAIQuarrier.java | 19 +- .../production/EntityAIStructureMiner.java | 23 +- .../production/EntityAIWorkLumberjack.java | 14 +- .../production/EntityAIWorkNether.java | 14 +- .../agriculture/EntityAIWorkBeekeeper.java | 12 +- .../agriculture/EntityAIWorkComposter.java | 10 +- .../agriculture/EntityAIWorkFarmer.java | 8 +- .../agriculture/EntityAIWorkFisherman.java | 8 +- .../agriculture/EntityAIWorkFlorist.java | 6 +- .../agriculture/EntityAIWorkPlanter.java | 7 +- .../herders/AbstractEntityAIHerder.java | 12 +- .../herders/EntityAIWorkCowboy.java | 4 +- .../ai/workers/service/EntityAIWorkCook.java | 4 +- .../service/EntityAIWorkDeliveryman.java | 41 ++- .../service/EntityAIWorkEnchanter.java | 24 +- .../workers/service/EntityAIWorkHealer.java | 19 +- .../service/EntityAIWorkUndertaker.java | 17 +- .../util/BuildingStructureHandler.java | 4 +- .../core/entity/citizen/EntityCitizen.java | 64 +--- .../core/entity/mobs/EntityMercenary.java | 5 +- .../core/entity/mobs/EntityMercenaryAI.java | 11 +- .../core/entity/mobs/aitasks/CampWalkAI.java | 59 +--- .../entity/mobs/aitasks/RaiderMeleeAI.java | 10 +- .../entity/mobs/aitasks/RaiderRangedAI.java | 5 +- .../entity/mobs/aitasks/RaiderWalkAI.java | 110 ++----- .../AbstractAdvancedPathNavigate.java | 42 ++- .../navigation/EntityNavigationUtils.java | 289 ++++++++++++++++++ .../MinecoloniesAdvancedPathNavigate.java | 163 ++++------ .../navigation/PathfindingAIHelper.java | 53 ---- .../navigation/PathingStuckHandler.java | 3 +- .../pathfinding/pathjobs/AbstractPathJob.java | 16 +- .../pathjobs/PathJobMoveAwayFromLocation.java | 15 + .../pathjobs/PathJobMoveCloseToXNearY.java | 17 +- .../pathjobs/PathJobMoveToLocation.java | 30 +- .../pathjobs/PathJobRandomPos.java | 38 ++- .../proxy/EntityCitizenWalkToProxy.java | 34 ++- .../core/entity/visitor/VisitorCitizen.java | 35 +-- .../core/util/TeleportHelper.java | 19 +- .../minecolonies/core/util/WorkerUtil.java | 40 +-- 80 files changed, 990 insertions(+), 1212 deletions(-) create mode 100644 src/main/java/com/minecolonies/core/entity/pathfinding/navigation/EntityNavigationUtils.java delete mode 100644 src/main/java/com/minecolonies/core/entity/pathfinding/navigation/PathfindingAIHelper.java diff --git a/src/main/java/com/minecolonies/api/colony/buildings/IBuilding.java b/src/main/java/com/minecolonies/api/colony/buildings/IBuilding.java index 2d2f76cb36d..283581306bd 100755 --- a/src/main/java/com/minecolonies/api/colony/buildings/IBuilding.java +++ b/src/main/java/com/minecolonies/api/colony/buildings/IBuilding.java @@ -506,10 +506,4 @@ default boolean canEat(final ItemStack stack) { return true; } - - /** - * Get the standing position for a building. - * @return the standing pos. - */ - BlockPos getStandingPosition(); } diff --git a/src/main/java/com/minecolonies/api/entity/citizen/AbstractEntityCitizen.java b/src/main/java/com/minecolonies/api/entity/citizen/AbstractEntityCitizen.java index 21b8b75c468..f7fbb28df07 100755 --- a/src/main/java/com/minecolonies/api/entity/citizen/AbstractEntityCitizen.java +++ b/src/main/java/com/minecolonies/api/entity/citizen/AbstractEntityCitizen.java @@ -14,7 +14,6 @@ import com.minecolonies.api.entity.ai.statemachine.tickratestatemachine.TickRateStateMachine; import com.minecolonies.api.entity.citizen.citizenhandlers.*; import com.minecolonies.api.entity.other.MinecoloniesMinecart; -import com.minecolonies.api.entity.pathfinding.proxy.IWalkToProxy; import com.minecolonies.api.entity.pathfinding.registry.IPathNavigateRegistry; import com.minecolonies.api.inventory.InventoryCitizen; import com.minecolonies.api.sounds.EventType; @@ -554,15 +553,6 @@ public boolean checkCanDropLoot() */ public abstract ILocation getLocation(); - /** - * Checks if a worker is at his working site. If he isn't, sets it's path to the location - * - * @param site the place where he should walk to - * @param range Range to check in - * @return True if worker is at site, otherwise false. - */ - public abstract boolean isWorkerAtSiteWithMove(@NotNull BlockPos site, int range); - /** * Getter for the citizendata. Tries to get it from the colony is the data is null. * @@ -593,13 +583,6 @@ public boolean checkCanDropLoot() */ public abstract void playMoveAwaySound(); - /** - * Get the path proxy of the citizen. - * - * @return the proxy. - */ - public abstract IWalkToProxy getProxy(); - /** * Decrease the saturation of the citizen for 1 action. */ diff --git a/src/main/java/com/minecolonies/api/util/BlockPosUtil.java b/src/main/java/com/minecolonies/api/util/BlockPosUtil.java index 6ec59aa69a1..eb0a466ef91 100755 --- a/src/main/java/com/minecolonies/api/util/BlockPosUtil.java +++ b/src/main/java/com/minecolonies/api/util/BlockPosUtil.java @@ -1,7 +1,6 @@ package com.minecolonies.api.util; import com.ldtteam.structurize.util.BlockUtils; -import com.minecolonies.api.entity.citizen.AbstractEntityCitizen; import com.minecolonies.api.util.constant.ColonyConstants; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; @@ -14,7 +13,6 @@ import net.minecraft.util.Mth; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.LivingEntity; -import net.minecraft.world.entity.Mob; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.BlockGetter; import net.minecraft.world.level.Level; @@ -667,31 +665,6 @@ public static boolean setBlock(@NotNull final Level worldIn, @NotNull final Bloc return worldIn.setBlock(coords, state, flag); } - /** - * @param living A living entity. - * @param destination chunk coordinates to check moving to. - * @return True when XYZ is found, an set moving to, otherwise false. - */ - public static boolean tryMoveBaseCitizenEntityToXYZ(@NotNull final AbstractEntityCitizen living, @NotNull final BlockPos destination) - { - if (!(living instanceof LivingEntity)) - { - return false; - } - - return EntityUtils.tryMoveLivingToXYZ(living, destination.getX(), destination.getY(), destination.getZ()); - } - - /** - * @param living A living entity. - * @param destination chunk coordinates to check moving to. - * @return True when XYZ is found, an set moving to, otherwise false. - */ - public static boolean tryMoveLivingToXYZ(@NotNull final Mob living, @NotNull final BlockPos destination) - { - return EntityUtils.tryMoveLivingToXYZ(living, destination.getX(), destination.getY(), destination.getZ()); - } - /** * Create a method for using a {@link BlockPos}. * diff --git a/src/main/java/com/minecolonies/api/util/EntityUtils.java b/src/main/java/com/minecolonies/api/util/EntityUtils.java index f162fb3c1cf..1ba6d630d9e 100755 --- a/src/main/java/com/minecolonies/api/util/EntityUtils.java +++ b/src/main/java/com/minecolonies/api/util/EntityUtils.java @@ -1,11 +1,11 @@ package com.minecolonies.api.util; import com.ldtteam.structurize.util.BlockUtils; -import com.minecolonies.api.crafting.ItemStorage; -import com.minecolonies.api.entity.citizen.AbstractEntityCitizen; +import com.minecolonies.api.entity.other.AbstractFastMinecoloniesEntity; import com.minecolonies.api.items.ModTags; import com.minecolonies.core.entity.pathfinding.PathfindingUtils; import com.minecolonies.core.entity.pathfinding.SurfaceType; +import com.minecolonies.core.entity.pathfinding.navigation.EntityNavigationUtils; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.tags.BlockTags; @@ -17,7 +17,6 @@ import net.minecraft.world.level.BlockGetter; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.state.BlockState; -import net.minecraft.world.phys.AABB; import net.minecraftforge.common.util.FakePlayer; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -216,6 +215,7 @@ private static boolean checkValidSpawn(@NotNull final BlockGetter world, final B || SurfaceType.getSurfaceType(world, world.getBlockState(pos.below(2)), pos.below(2)) == SurfaceType.WALKABLE; } + // TODO: Move out movement stuff /** * Sets the movement of the entity to specific point. Returns true if direction is set, otherwise false. @@ -239,27 +239,15 @@ public static boolean tryMoveLivingToXYZ(@NotNull final Mob living, final int x, * @param y y-coordinate * @param z z-coordinate * @param speedFactor Speedfactor to modify base speed with - * @return True if the path is set to destination, otherwise false + * @return true if arrived */ public static boolean tryMoveLivingToXYZ(@NotNull final Mob living, final int x, final int y, final int z, final double speedFactor) { - return living.getNavigation().moveTo(x, y, z, speedFactor); - } - - /** - * {@link #isLivingAtSiteWithMove(LivingEntity, int, int, int)} - * - * @param entity entity to check - * @param x X-coordinate - * @param y Y-coordinate - * @param z Z-coordinate - * @return True if entity is at site, otherwise false - */ - public static boolean isLivingAtSiteWithMove(@NotNull final LivingEntity entity, final int x, final int y, final int z) - { - //Default range of 3 works better - //Range of 2 get some entitys stuck - return isLivingAtSiteWithMove(entity, x, y, z, DEFAULT_MOVE_RANGE); + if (living instanceof AbstractFastMinecoloniesEntity entity) + { + return EntityNavigationUtils.walkToPos(entity, new BlockPos(x, y, z), 4, true, speedFactor); + } + return true; } /** @@ -300,34 +288,6 @@ public static boolean isLivingAtSiteWithMove(@NotNull final LivingEntity entity, return EntityUtils.isLivingAtSite(entity, x, y, z, range); } - /** - * Checks if a certain entity is in the world at a certain position already. - * - * @param entity the entity. - * @param world the world. - * @param placer the entity to get the itemstacks from to check. - * @return true if there. - */ - public static boolean isEntityAtPosition(final Entity entity, final Level world, final Entity placer) - { - final List existingReq = ItemStackUtils.getListOfStackForEntity(entity, placer); - final BlockPos pos = BlockPos.containing(entity.getX(), entity.getY(), entity.getZ()); - return world.getEntitiesOfClass(Entity.class, new AABB(pos.offset(1, 1, 1), pos.offset(-1, -1, -1))) - .stream() - .anyMatch(ent -> ent.getX() == entity.getX() && ent.getY() == entity.getY() && ent.getZ() == entity.getZ() && ItemStackUtils.getListOfStackForEntity(entity, placer) - .equals(existingReq)); - } - - public static boolean isEntityAtPosition(final Entity entity, final Level world, final AbstractEntityCitizen entityCitizen) - { - if (entity != null) - { - return EntityUtils.isEntityAtPosition(entity, world, (Entity) entityCitizen); - } - - return false; - } - /** * Returns whether or not the entity is within a specific range of his working site. * diff --git a/src/main/java/com/minecolonies/api/util/ItemStackUtils.java b/src/main/java/com/minecolonies/api/util/ItemStackUtils.java index ee4283d2ce1..e3d25492cbe 100755 --- a/src/main/java/com/minecolonies/api/util/ItemStackUtils.java +++ b/src/main/java/com/minecolonies/api/util/ItemStackUtils.java @@ -1,6 +1,5 @@ package com.minecolonies.api.util; -import com.google.common.collect.Lists; import com.minecolonies.api.advancements.AdvancementTriggers; import com.minecolonies.api.colony.ICitizenData; import com.minecolonies.api.colony.IColony; @@ -27,14 +26,12 @@ import net.minecraft.resources.ResourceLocation; import net.minecraft.tags.ItemTags; import net.minecraft.world.entity.Entity; -import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.decoration.ArmorStand; import net.minecraft.world.entity.decoration.ItemFrame; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Player; import net.minecraft.world.food.FoodProperties; import net.minecraft.world.item.*; -import net.minecraft.world.level.Level; import net.minecraft.world.level.block.entity.BrewingStandBlockEntity; import net.minecraft.world.level.block.entity.FurnaceBlockEntity; import net.minecraft.world.phys.EntityHitResult; @@ -140,80 +137,6 @@ private ItemStackUtils() */ } - /** - * Get the entity of an entityInfo object. - * - * @param entityData the input. - * @param world the world. - * @return the output object or null. - */ - @Nullable - public static Entity getEntityFromEntityInfoOrNull(final CompoundTag entityData, final Level world) - { - try - { - final Optional> type = EntityType.by(entityData); - if (type.isPresent()) - { - final Entity entity = type.get().create(world); - if (entity != null) - { - entity.load(entityData); - return entity; - } - } - } - catch (final RuntimeException e) - { - Log.getLogger().info("Couldn't restore entitiy", e); - return null; - } - return null; - } - - /** - * Adds entities to the builder building if he needs it. - * - * @param entityData the entity info object. - * @param world the world. - * @param placer the entity placer. - * @return a list of stacks. - */ - public static List getListOfStackForEntityInfo(final CompoundTag entityData, final Level world, final Entity placer) - { - if (entityData != null) - { - final Entity entity = getEntityFromEntityInfoOrNull(entityData, world); - if (entity != null) - { - if (EntityUtils.isEntityAtPosition(entity, world, placer)) - { - return Collections.emptyList(); - } - return getListOfStackForEntity(entity, placer); - } - } - return Collections.emptyList(); - } - - /** - * Adds entities to the builder building if he needs it. - * - * @param entityData the entity info object. - * @param world the world. - * @param placer the entity placer. - * @return a list of stacks. - */ - public static List getListOfStackForEntityInfo(final CompoundTag entityData, final Level world, final AbstractEntityCitizen placer) - { - if (placer != null) - { - return getListOfStackForEntityInfo(entityData, world, (Entity) placer); - } - - return Lists.newArrayList(); - } - /** * Adds entities to the builder building if he needs it. * diff --git a/src/main/java/com/minecolonies/core/colony/CitizenData.java b/src/main/java/com/minecolonies/core/colony/CitizenData.java index 6b41abd58f5..004359e3cb6 100755 --- a/src/main/java/com/minecolonies/core/colony/CitizenData.java +++ b/src/main/java/com/minecolonies/core/colony/CitizenData.java @@ -2016,7 +2016,7 @@ public BlockPos getHomePosition() @Nullable final IBuilding homeBuilding = getHomeBuilding(); if (homeBuilding != null) { - return homeBuilding.getStandingPosition(); + return homeBuilding.getPosition(); } if (colony != null) @@ -2024,7 +2024,7 @@ public BlockPos getHomePosition() final IBuilding tavern = colony.getBuildingManager().getFirstBuildingMatching(b -> b.getBuildingType() == ModBuildings.tavern.get()); if (tavern != null) { - return tavern.getStandingPosition(); + return tavern.getPosition(); } else if (colony.getBuildingManager().getTownHall() != null) { diff --git a/src/main/java/com/minecolonies/core/colony/buildings/AbstractBuilding.java b/src/main/java/com/minecolonies/core/colony/buildings/AbstractBuilding.java index f8f0345de2d..433e96bed50 100755 --- a/src/main/java/com/minecolonies/core/colony/buildings/AbstractBuilding.java +++ b/src/main/java/com/minecolonies/core/colony/buildings/AbstractBuilding.java @@ -1046,61 +1046,6 @@ public void resetGuardBuildingNear() this.recheckGuardBuildingNear = true; } - @Override - public BlockPos getStandingPosition() - { - if (cachedStandingPosition == null) - { - if (!WorldUtil.isEntityBlockLoaded(colony.getWorld(), getPosition())) - { - return getPosition(); - } - - BlockPos bestPos = getPosition(); - int bestScore = 0; - - //Return true if the building is null to stall the worker - for (final Direction dir : Direction.Plane.HORIZONTAL) - { - final BlockPos currentPos = getPosition().relative(dir); - final BlockState hereState = colony.getWorld().getBlockState(currentPos); - // Check air here and air above. - if ((!hereState.getBlock().properties.hasCollision || hereState.is(BlockTags.WOOL_CARPETS)) - && !colony.getWorld().getBlockState(currentPos.above()).getBlock().properties.hasCollision - && BlockUtils.isAnySolid(colony.getWorld().getBlockState(currentPos.below()))) - { - int localScore = BEST_STANDING_SCORE; - if (colony.getWorld().canSeeSky(currentPos)) - { - // More critical - localScore-=2; - } - if (colony.getWorld().getBlockState(getPosition()).getValue(AbstractBlockHut.FACING) == dir) - { - // Less critical - localScore--; - } - - if (localScore == BEST_STANDING_SCORE) - { - cachedStandingPosition = currentPos; - return cachedStandingPosition; - } - - if (localScore > bestScore) - { - bestScore = localScore; - bestPos = currentPos; - } - } - } - - // prefer default rotation of the building facing this. - cachedStandingPosition = bestPos; - } - return cachedStandingPosition == null ? getPosition() : cachedStandingPosition; - } - //------------------------- Starting Required Tools/Item handling -------------------------// @Override diff --git a/src/main/java/com/minecolonies/core/colony/events/raid/pirateEvent/ShipBasedRaiderUtils.java b/src/main/java/com/minecolonies/core/colony/events/raid/pirateEvent/ShipBasedRaiderUtils.java index 40e0bdae836..fa5b70de9d7 100644 --- a/src/main/java/com/minecolonies/core/colony/events/raid/pirateEvent/ShipBasedRaiderUtils.java +++ b/src/main/java/com/minecolonies/core/colony/events/raid/pirateEvent/ShipBasedRaiderUtils.java @@ -416,7 +416,7 @@ public static List createWaypoints(final Level world, final Path path, { final BlockPos point = path.getNode(i).asBlockPos(); if (lastPoint.distManhattan(point) > spacing - && world.getBlockState(point).isAir()) + && world.getBlockState(point).isAir() && world.getBlockState(point.above()).isAir()) { wayPoints.add(point); lastPoint = point; diff --git a/src/main/java/com/minecolonies/core/entity/ai/combat/AttackMoveAI.java b/src/main/java/com/minecolonies/core/entity/ai/combat/AttackMoveAI.java index 756d377fd8b..42e334fe814 100644 --- a/src/main/java/com/minecolonies/core/entity/ai/combat/AttackMoveAI.java +++ b/src/main/java/com/minecolonies/core/entity/ai/combat/AttackMoveAI.java @@ -6,8 +6,10 @@ import com.minecolonies.api.entity.ai.statemachine.states.IState; import com.minecolonies.api.entity.ai.statemachine.tickratestatemachine.ITickRateStateMachine; import com.minecolonies.api.entity.ai.statemachine.tickratestatemachine.TickingTransition; +import com.minecolonies.api.entity.other.AbstractFastMinecoloniesEntity; import com.minecolonies.api.util.DamageSourceKeys; -import com.minecolonies.core.entity.pathfinding.navigation.AbstractAdvancedPathNavigate; +import com.minecolonies.core.entity.pathfinding.navigation.EntityNavigationUtils; +import com.minecolonies.core.entity.pathfinding.navigation.MinecoloniesAdvancedPathNavigate; import com.minecolonies.core.entity.pathfinding.pathresults.PathResult; import net.minecraft.world.InteractionHand; import net.minecraft.world.entity.LivingEntity; @@ -211,6 +213,7 @@ protected int getAttackDelay() */ protected PathResult moveInAttackPosition(final LivingEntity target) { - return ((AbstractAdvancedPathNavigate) user.getNavigation()).moveToLivingEntity(target, 1d); + EntityNavigationUtils.walkToPos((AbstractFastMinecoloniesEntity) user, target.blockPosition(), 1, false); + return ((MinecoloniesAdvancedPathNavigate) user.getNavigation()).getPathResult(); } } diff --git a/src/main/java/com/minecolonies/core/entity/ai/minimal/EntityAICitizenAvoidEntity.java b/src/main/java/com/minecolonies/core/entity/ai/minimal/EntityAICitizenAvoidEntity.java index ef363b9d2c8..f1acde761b3 100644 --- a/src/main/java/com/minecolonies/core/entity/ai/minimal/EntityAICitizenAvoidEntity.java +++ b/src/main/java/com/minecolonies/core/entity/ai/minimal/EntityAICitizenAvoidEntity.java @@ -5,9 +5,10 @@ import com.minecolonies.api.entity.ai.statemachine.states.CitizenAIState; import com.minecolonies.api.entity.ai.statemachine.states.IState; import com.minecolonies.api.entity.citizen.AbstractEntityCitizen; -import com.minecolonies.core.entity.pathfinding.pathresults.PathResult; import com.minecolonies.api.util.CompatibilityUtils; import com.minecolonies.core.entity.citizen.EntityCitizen; +import com.minecolonies.core.entity.pathfinding.navigation.EntityNavigationUtils; +import com.minecolonies.core.entity.pathfinding.pathresults.PathResult; import net.minecraft.core.BlockPos; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.player.Player; @@ -173,9 +174,11 @@ private boolean performMoveAway() { if ((moveAwayPath == null || !moveAwayPath.isInProgress()) && citizen.getNavigation().isDone()) { - moveAwayPath = - citizen.getNavigation() - .moveAwayFromXYZ(citizen.blockPosition().offset(rand.nextInt(2), 0, rand.nextInt(2)), distanceFromEntity + getMoveAwayDist(citizen), nearSpeed, true); + EntityNavigationUtils.walkAwayFrom(citizen, + citizen.blockPosition().offset(rand.nextInt(2), 0, rand.nextInt(2)), + (int) (distanceFromEntity + getMoveAwayDist(citizen)), + nearSpeed); + moveAwayPath = citizen.getNavigation().getPathResult(); return true; } return false; @@ -248,7 +251,7 @@ public void reset() safeTime = 0; if (startingPos != null) { - citizen.getNavigation().tryMoveToBlockPos(startingPos, 1); + EntityNavigationUtils.walkToPos(citizen, startingPos, 1, true); } closestLivingEntity = null; moveAwayPath = null; diff --git a/src/main/java/com/minecolonies/core/entity/ai/minimal/EntityAICitizenChild.java b/src/main/java/com/minecolonies/core/entity/ai/minimal/EntityAICitizenChild.java index 0c1d4fa9eb6..442f7936918 100755 --- a/src/main/java/com/minecolonies/core/entity/ai/minimal/EntityAICitizenChild.java +++ b/src/main/java/com/minecolonies/core/entity/ai/minimal/EntityAICitizenChild.java @@ -1,5 +1,6 @@ package com.minecolonies.core.entity.ai.minimal; +import com.minecolonies.api.colony.buildings.IBuilding; import com.minecolonies.api.entity.ai.IStateAI; import com.minecolonies.api.entity.ai.statemachine.AIEventTarget; import com.minecolonies.api.entity.ai.statemachine.AITarget; @@ -8,11 +9,12 @@ import com.minecolonies.api.entity.ai.statemachine.states.IAIState; import com.minecolonies.api.entity.ai.statemachine.states.IState; import com.minecolonies.api.entity.citizen.AbstractCivilianEntity; -import com.minecolonies.core.entity.pathfinding.pathresults.PathResult; import com.minecolonies.api.util.CompatibilityUtils; import com.minecolonies.api.util.MessageUtils; import com.minecolonies.core.colony.eventhooks.citizenEvents.CitizenGrownUpEvent; import com.minecolonies.core.entity.citizen.EntityCitizen; +import com.minecolonies.core.entity.pathfinding.navigation.EntityNavigationUtils; +import com.minecolonies.core.entity.pathfinding.pathresults.PathResult; import net.minecraft.core.BlockPos; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.player.Player; @@ -92,7 +94,7 @@ public boolean isOkayToEat() /** * The blockpos the child is visiting */ - private BlockPos visitHutPos; + private IBuilding visitingHut; /** * The path for visiting the hut @@ -195,13 +197,13 @@ private IState followingEntity() if (actionTimer <= 0 || followTarget.get() == null) { // run back to start position - child.getNavigation().moveToXYZ(followStart.getX(), followStart.getY(), followStart.getZ(), 1.0d); + EntityNavigationUtils.walkToPos(child, followStart, 2, true); setDelayForNextAction(); return CitizenAIState.IDLE; } - child.getNavigation().moveToLivingEntity(followTarget.get(), 1.0d); + EntityNavigationUtils.walkToPos(child, followTarget.get().blockPosition(), 2, false); return State.FOLLOWING; } @@ -216,7 +218,7 @@ private IState visitHuts() if (visitingPath == null && child.getCitizenColonyHandler().getColonyOrRegister() != null) { // Visiting huts for 3min. - if (actionTimer <= 0 && visitHutPos == null) + if (actionTimer <= 0 && visitingHut == null) { actionTimer = 3 * 60 * 20; } @@ -225,10 +227,11 @@ private IState visitHuts() index = rand.nextInt(index); - final List buildings = new ArrayList<>(child.getCitizenColonyHandler().getColonyOrRegister().getBuildingManager().getBuildings().keySet()); - visitHutPos = buildings.get(index); + final List buildings = new ArrayList<>(child.getCitizenColonyHandler().getColonyOrRegister().getBuildingManager().getBuildings().values()); + visitingHut = buildings.get(index); - visitingPath = child.getNavigation().moveToXYZ(visitHutPos.getX(), visitHutPos.getY(), visitHutPos.getZ(), 1.0d); + EntityNavigationUtils.walkToBuilding(child, visitingHut); + visitingPath = child.getNavigation().getPathResult(); } actionTimer -= 120; @@ -238,7 +241,8 @@ private IState visitHuts() // Path got interrupted by sth if (visitingPath != null && !visitingPath.isInProgress()) { - visitingPath = child.getNavigation().moveToXYZ(visitHutPos.getX(), visitHutPos.getY(), visitHutPos.getZ(), 1.0d); + EntityNavigationUtils.walkToBuilding(child, visitingHut); + visitingPath = child.getNavigation().getPathResult(); } return State.VISITING; @@ -246,7 +250,7 @@ private IState visitHuts() child.getNavigation().stop(); visitingPath = null; - visitHutPos = null; + visitingHut = null; setDelayForNextAction(); return CitizenAIState.IDLE; diff --git a/src/main/java/com/minecolonies/core/entity/ai/minimal/EntityAICitizenWander.java b/src/main/java/com/minecolonies/core/entity/ai/minimal/EntityAICitizenWander.java index 061fe362d9e..79db6c7b081 100755 --- a/src/main/java/com/minecolonies/core/entity/ai/minimal/EntityAICitizenWander.java +++ b/src/main/java/com/minecolonies/core/entity/ai/minimal/EntityAICitizenWander.java @@ -10,6 +10,7 @@ import com.minecolonies.core.entity.ai.workers.education.EntityAIStudy; import com.minecolonies.core.entity.citizen.EntityCitizen; import com.minecolonies.core.entity.other.SittingEntity; +import com.minecolonies.core.entity.pathfinding.navigation.EntityNavigationUtils; import com.minecolonies.core.tileentities.TileEntityColonyBuilding; import net.minecraft.core.BlockPos; import net.minecraft.world.entity.EquipmentSlot; @@ -92,7 +93,7 @@ private IState readABook() if (walkTo != null) { - if (!citizen.isWorkerAtSiteWithMove(walkTo, 3)) + if (!EntityNavigationUtils.walkToPos(citizen, walkTo, 3, true)) { return READ_A_BOOK; } @@ -129,7 +130,7 @@ private IState goToLeisureSite() return CitizenAIState.IDLE; } - if (!citizen.isWorkerAtSiteWithMove(leisureSite, 3)) + if (!EntityNavigationUtils.walkToPos(citizen, leisureSite, 3, true)) { return GO_TO_LEISURE_SITE; } @@ -146,7 +147,7 @@ private IState wanderAtLeisureSite() return CitizenAIState.IDLE; } - if (walkTo != null && !citizen.isWorkerAtSiteWithMove(walkTo, 3)) + if (walkTo != null && !EntityNavigationUtils.walkToPos(citizen, walkTo, 3, true)) { return WANDER_AT_LEISURE_SITE; } @@ -161,8 +162,7 @@ private IState wanderAtLeisureSite() { if (walkTo == null && citizen.getRandom().nextBoolean()) { - citizen.getNavigation() - .moveToRandomPos(10, DEFAULT_SPEED, ((IBlueprintDataProviderBE) blockEntity).getInWorldCorners()); + EntityNavigationUtils.walkToRandomPosWithin(citizen, 10, DEFAULT_SPEED, ((IBlueprintDataProviderBE) blockEntity).getInWorldCorners()); } if (walkTo == null && blockEntity instanceof TileEntityColonyBuilding && ((TileEntityColonyBuilding) blockEntity).getBuilding() instanceof BuildingLibrary && citizen.getRandom().nextInt(100) < 5) @@ -223,7 +223,7 @@ private IState decide() } } - citizen.getNavigation().moveToRandomPos(10, this.speed); + EntityNavigationUtils.walkToRandomPos(citizen, 10, this.speed); return CitizenAIState.IDLE; } diff --git a/src/main/java/com/minecolonies/core/entity/ai/minimal/EntityAIEatTask.java b/src/main/java/com/minecolonies/core/entity/ai/minimal/EntityAIEatTask.java index 93fdf5ed380..386509f1e22 100755 --- a/src/main/java/com/minecolonies/core/entity/ai/minimal/EntityAIEatTask.java +++ b/src/main/java/com/minecolonies/core/entity/ai/minimal/EntityAIEatTask.java @@ -19,6 +19,7 @@ import com.minecolonies.core.colony.jobs.JobCook; import com.minecolonies.core.entity.citizen.EntityCitizen; import com.minecolonies.core.entity.other.SittingEntity; +import com.minecolonies.core.entity.pathfinding.navigation.EntityNavigationUtils; import com.minecolonies.core.network.messages.client.ItemParticleEffectMessage; import net.minecraft.core.BlockPos; import net.minecraft.network.chat.Component; @@ -49,11 +50,6 @@ public class EntityAIEatTask implements IStateAI */ private static final int MINUTES_WAITING_TIME = 2; - /** - * Min distance in blocks to placeToPath block. - */ - private static final int MIN_DISTANCE_TO_RESTAURANT = 2; - /** * Time required to eat in seconds. */ @@ -253,7 +249,7 @@ private EatingState getFoodYourself() final IBuilding cookBuilding = colony.getBuildingManager().getBuilding(restaurantPos); if (cookBuilding instanceof BuildingCook) { - if (!citizen.isWorkerAtSiteWithMove(cookBuilding.getPosition(), MIN_DISTANCE_TO_RESTAURANT)) + if (!EntityNavigationUtils.walkToBuilding(citizen, cookBuilding)) { return GET_FOOD_YOURSELF; } @@ -304,7 +300,7 @@ private EatingState goToEatingPlace() } } - if (citizen.isWorkerAtSiteWithMove(eatPos, 2)) + if (EntityNavigationUtils.walkToPos(citizen, eatPos, 2, true)) { SittingEntity.sitDown(eatPos, citizen, TICKS_SECOND * 60); // Delay till they start eating @@ -392,7 +388,7 @@ private EatingState goToHut() return SEARCH_RESTAURANT; } - if (citizen.isWorkerAtSiteWithMove(buildingWorker.getPosition(), MIN_DISTANCE_TO_RESTAURANT)) + if (EntityNavigationUtils.walkToBuilding(citizen, buildingWorker)) { final int slot = FoodUtils.getBestFoodForCitizen(citizen.getInventoryCitizen(), citizen.getCitizenData(), null); if (slot != -1) @@ -427,7 +423,7 @@ private EatingState goToRestaurant() { return WAIT_FOR_FOOD; } - else if (!citizen.isWorkerAtSiteWithMove(restaurantPos, MIN_DISTANCE_TO_RESTAURANT)) + else if (!EntityNavigationUtils.walkToBuilding(citizen, building)) { return GO_TO_RESTAURANT; } diff --git a/src/main/java/com/minecolonies/core/entity/ai/minimal/EntityAIMournCitizen.java b/src/main/java/com/minecolonies/core/entity/ai/minimal/EntityAIMournCitizen.java index e6170b11543..117343db263 100755 --- a/src/main/java/com/minecolonies/core/entity/ai/minimal/EntityAIMournCitizen.java +++ b/src/main/java/com/minecolonies/core/entity/ai/minimal/EntityAIMournCitizen.java @@ -6,7 +6,6 @@ import com.minecolonies.api.entity.ai.statemachine.states.CitizenAIState; import com.minecolonies.api.entity.ai.statemachine.states.IState; import com.minecolonies.api.entity.ai.statemachine.tickratestatemachine.TickingTransition; -import com.minecolonies.core.tileentities.TileEntityNamedGrave; import com.minecolonies.api.util.BlockPosUtil; import com.minecolonies.api.util.MathUtils; import com.minecolonies.api.util.Tuple; @@ -14,6 +13,8 @@ import com.minecolonies.core.colony.buildings.modules.GraveyardManagementModule; import com.minecolonies.core.colony.buildings.workerbuildings.BuildingGraveyard; import com.minecolonies.core.entity.citizen.EntityCitizen; +import com.minecolonies.core.entity.pathfinding.navigation.EntityNavigationUtils; +import com.minecolonies.core.tileentities.TileEntityNamedGrave; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.world.entity.Entity; @@ -71,7 +72,7 @@ public enum MourningState implements IState /** * The position of the graveyard. */ - private BlockPos graveyard; + private IBuilding graveyard; /** * Position of the grave to walk to. @@ -110,13 +111,13 @@ public EntityAIMournCitizen(final EntityCitizen citizen, final double speed) */ private IState walkToTownHall() { - final BlockPos pos = getMournLocation(); - if (pos == null) + final IBuilding building = getMournLocation(); + if (building == null) { return CitizenAIState.IDLE; } - if (!citizen.isWorkerAtSiteWithMove(pos, 3)) + if (!EntityNavigationUtils.walkToBuilding(citizen, building)) { return MourningState.WALKING_TO_TOWNHALL; } @@ -136,7 +137,7 @@ private IState walkToGraveyard() return MourningState.DECIDE; } - if (!citizen.isWorkerAtSiteWithMove(graveyard, 3)) + if (!EntityNavigationUtils.walkToBuilding(citizen, graveyard)) { return MourningState.WALKING_TO_GRAVEYARD; } @@ -157,13 +158,6 @@ private IState wanderAtGraveyard() return MourningState.DECIDE; } - final IBuilding graveyardBuilding = citizen.getCitizenColonyHandler().getColonyOrRegister().getBuildingManager().getBuilding(graveyard); - if (!(graveyardBuilding instanceof BuildingGraveyard)) - { - graveyard = null; - return MourningState.DECIDE; - } - if (!citizen.getNavigation().isDone()) { return MourningState.WANDER_AT_GRAVEYARD; @@ -172,12 +166,12 @@ private IState wanderAtGraveyard() // Wander around randomly. if (MathUtils.RANDOM.nextInt(100) < 90) { - citizen.getNavigation().moveToRandomPos(10, DEFAULT_SPEED, graveyardBuilding.getCorners()); + EntityNavigationUtils.walkToRandomPosWithin(citizen, 10, DEFAULT_SPEED, graveyard.getCorners()); return MourningState.WANDER_AT_GRAVEYARD; } // Try find the grave of one of the diseased. - final Set> gravePositions = ((BuildingGraveyard) graveyardBuilding).getGravePositions(); + final Set> gravePositions = ((BuildingGraveyard) graveyard).getGravePositions(); for (final Tuple gravePos : gravePositions) { if (WorldUtil.isBlockLoaded(citizen.level, gravePos.getA())) @@ -219,7 +213,7 @@ private IState walkToGrave() return MourningState.DECIDE; } - if (!citizen.isWorkerAtSiteWithMove(gravePos, 3)) + if (!EntityNavigationUtils.walkToPosInBuilding(citizen, gravePos, graveyard, 3)) { return MourningState.WALK_TO_GRAVE; } @@ -234,7 +228,7 @@ private IState walkToGrave() */ private IState wander() { - citizen.getNavigation().moveToRandomPos(10, this.speed); + EntityNavigationUtils.walkToRandomPos(citizen, 10, this.speed); return CitizenAIState.IDLE; } @@ -300,7 +294,7 @@ private IState decide() GraveyardManagementModule.class).hasRestingCitizen(citizen.getCitizenData().getCitizenMournHandler().getDeceasedCitizens())); if (graveyardBuilding != null) { - this.graveyard = graveyardBuilding.getPosition(); + this.graveyard = graveyardBuilding; } } @@ -312,7 +306,7 @@ private IState decide() citizen.getLookControl().setLookAt(citizen.getX(), citizen.getY() - 10, citizen.getZ(), (float) citizen.getMaxHeadYRot(), (float) citizen.getMaxHeadXRot()); - if (BlockPosUtil.getDistance2D(this.citizen.blockPosition(), getMournLocation()) > MIN_DESTINATION_TO_LOCATION) + if (getMournLocation() != null && BlockPosUtil.getDistance2D(this.citizen.blockPosition(), getMournLocation().getPosition()) > MIN_DESTINATION_TO_LOCATION) { return MourningState.WALKING_TO_TOWNHALL; } @@ -332,14 +326,14 @@ public void reset() * * @return blockPos of the location to mourn at */ - protected BlockPos getMournLocation() + protected IBuilding getMournLocation() { final IColony colony = citizen.getCitizenColonyHandler().getColonyOrRegister(); if (colony != null && colony.getBuildingManager().hasTownHall()) { - return colony.getBuildingManager().getTownHall().getStandingPosition(); + return colony.getBuildingManager().getTownHall(); } - return citizen.getCitizenData().getHomePosition(); + return citizen.getCitizenData().getHomeBuilding(); } } diff --git a/src/main/java/com/minecolonies/core/entity/ai/minimal/EntityAISickTask.java b/src/main/java/com/minecolonies/core/entity/ai/minimal/EntityAISickTask.java index 842246b6eed..305db4e6bdd 100755 --- a/src/main/java/com/minecolonies/core/entity/ai/minimal/EntityAISickTask.java +++ b/src/main/java/com/minecolonies/core/entity/ai/minimal/EntityAISickTask.java @@ -16,9 +16,9 @@ import com.minecolonies.core.Network; import com.minecolonies.core.colony.buildings.workerbuildings.BuildingHospital; import com.minecolonies.core.colony.interactionhandling.StandardInteraction; -import com.minecolonies.core.datalistener.DiseasesListener; import com.minecolonies.core.datalistener.model.Disease; import com.minecolonies.core.entity.citizen.EntityCitizen; +import com.minecolonies.core.entity.pathfinding.navigation.EntityNavigationUtils; import com.minecolonies.core.network.messages.client.CircleParticleEffectMessage; import net.minecraft.core.BlockPos; import net.minecraft.core.particles.ParticleTypes; @@ -113,7 +113,7 @@ public enum DiseaseState implements IState /** * Restaurant to which the citizen should path. */ - private BlockPos placeToPath; + private BlockPos bestHospital; /** * Instantiates this task. @@ -158,7 +158,7 @@ private boolean isSick() */ public DiseaseState wander() { - citizen.getNavigation().moveToRandomPos(10, 0.6D); + EntityNavigationUtils.walkToRandomPos(citizen, 10, 0.6D); return CHECK_FOR_CURE; } @@ -214,7 +214,7 @@ private DiseaseState findEmptyBed() } } - if (citizen.isWorkerAtSiteWithMove(usedBed, 3)) + if (EntityNavigationUtils.walkToPosInBuilding(citizen, usedBed, hospital, 3)) { waitingTicks++; if (!citizen.getCitizenSleepHandler().trySleep(usedBed)) @@ -318,9 +318,9 @@ private void cure() private IState waitForCure() { final IColony colony = citizenData.getColony(); - placeToPath = colony.getBuildingManager().getBestBuilding(citizen, BuildingHospital.class); + bestHospital = colony.getBuildingManager().getBestBuilding(citizen, BuildingHospital.class); - if (placeToPath == null) + if (bestHospital == null) { return SEARCH_HOSPITAL; } @@ -355,7 +355,7 @@ else if (state == CitizenAIState.IDLE) } } - if (!citizen.getCitizenSleepHandler().isAsleep() && BlockPosUtil.getDistance2D(placeToPath, citizen.blockPosition()) > MINIMUM_DISTANCE_TO_HOSPITAL) + if (!citizen.getCitizenSleepHandler().isAsleep() && BlockPosUtil.getDistance2D(bestHospital, citizen.blockPosition()) > MINIMUM_DISTANCE_TO_HOSPITAL) { return GO_TO_HOSPITAL; } @@ -383,7 +383,7 @@ private IState goToHut() return SEARCH_HOSPITAL; } - if (citizen.getCitizenSleepHandler().isAsleep() || citizen.isWorkerAtSiteWithMove(buildingWorker.getPosition(), MIN_DIST_TO_HUT)) + if (citizen.getCitizenSleepHandler().isAsleep() || EntityNavigationUtils.walkToBuilding(citizen, buildingWorker)) { return SEARCH_HOSPITAL; } @@ -398,12 +398,12 @@ private IState goToHut() private IState goToHospital() { citizen.getCitizenData().getCitizenDiseaseHandler().setSleepsAtHospital(false); - if (placeToPath == null) + if (bestHospital == null) { return SEARCH_HOSPITAL; } - if (citizen.getCitizenSleepHandler().isAsleep() || (citizen.getNavigation().isDone() && citizen.isWorkerAtSiteWithMove(placeToPath, MIN_DIST_TO_HOSPITAL))) + if (citizen.getCitizenSleepHandler().isAsleep() || EntityNavigationUtils.walkToPos(citizen, bestHospital, MIN_DIST_TO_HOSPITAL, true)) { return WAIT_FOR_CURE; } @@ -419,9 +419,9 @@ private IState searchHospital() { final IColony colony = citizenData.getColony(); final Disease disease = citizen.getCitizenData().getCitizenDiseaseHandler().getDisease(); - placeToPath = colony.getBuildingManager().getBestBuilding(citizen, BuildingHospital.class); + bestHospital = colony.getBuildingManager().getBestBuilding(citizen, BuildingHospital.class); - if (placeToPath == null) + if (bestHospital == null) { if (disease == null) { @@ -485,7 +485,7 @@ private void reset() citizen.releaseUsingItem(); citizen.stopUsingItem(); citizen.setItemInHand(InteractionHand.MAIN_HAND, ItemStack.EMPTY); - placeToPath = null; + bestHospital = null; citizen.getCitizenData().getCitizenDiseaseHandler().setSleepsAtHospital(false); } diff --git a/src/main/java/com/minecolonies/core/entity/ai/minimal/EntityAISleep.java b/src/main/java/com/minecolonies/core/entity/ai/minimal/EntityAISleep.java index af02042cba1..a1d4c0fad8c 100755 --- a/src/main/java/com/minecolonies/core/entity/ai/minimal/EntityAISleep.java +++ b/src/main/java/com/minecolonies/core/entity/ai/minimal/EntityAISleep.java @@ -17,6 +17,7 @@ import com.minecolonies.core.colony.buildings.AbstractBuilding; import com.minecolonies.core.colony.buildings.modules.BuildingModules; import com.minecolonies.core.entity.citizen.EntityCitizen; +import com.minecolonies.core.entity.pathfinding.navigation.EntityNavigationUtils; import com.minecolonies.core.network.messages.client.SleepingParticleMessage; import net.minecraft.core.BlockPos; import net.minecraft.tags.BlockTags; @@ -200,7 +201,7 @@ private void findBedAndTryToSleep() usedBed = homePos; } - if (citizen.isWorkerAtSiteWithMove(usedBed, 3)) + if (EntityNavigationUtils.walkToPosInBuilding(citizen, usedBed, citizen.getCitizenData().getHomeBuilding(), 5)) { bedTicks++; if (!citizen.getCitizenSleepHandler().trySleep(usedBed)) @@ -237,10 +238,14 @@ private IState sleep() */ private void goHome() { - final BlockPos pos = citizen.getCitizenData().getHomePosition(); - if (pos != null && !citizen.isWorkerAtSiteWithMove(pos, 2) && citizen.getPose() == Pose.SLEEPING) + final IBuilding home = citizen.getCitizenData().getHomeBuilding(); + if (home != null) { - citizen.setPose(Pose.STANDING); + EntityNavigationUtils.walkToBuilding(citizen, home); + } + else + { + EntityNavigationUtils.walkToPos(citizen, citizen.getCitizenData().getHomePosition(), 4, true); } final int chance = citizen.getRandom().nextInt(CHANCE); diff --git a/src/main/java/com/minecolonies/core/entity/ai/visitor/EntityAIVisitor.java b/src/main/java/com/minecolonies/core/entity/ai/visitor/EntityAIVisitor.java index 16195643d4a..bfa5502b4e3 100644 --- a/src/main/java/com/minecolonies/core/entity/ai/visitor/EntityAIVisitor.java +++ b/src/main/java/com/minecolonies/core/entity/ai/visitor/EntityAIVisitor.java @@ -7,13 +7,13 @@ import com.minecolonies.api.entity.ai.statemachine.tickratestatemachine.TickingTransition; import com.minecolonies.api.entity.citizen.AbstractEntityCitizen; import com.minecolonies.api.util.DamageSourceKeys; -import com.minecolonies.api.util.Log; import com.minecolonies.api.util.WorldUtil; import com.minecolonies.core.colony.VisitorData; import com.minecolonies.core.colony.buildings.DefaultBuildingInstance; import com.minecolonies.core.colony.buildings.modules.BuildingModules; import com.minecolonies.core.colony.buildings.modules.TavernBuildingModule; import com.minecolonies.core.entity.other.SittingEntity; +import com.minecolonies.core.entity.pathfinding.navigation.EntityNavigationUtils; import com.minecolonies.core.entity.visitor.VisitorCitizen; import net.minecraft.core.BlockPos; import net.minecraft.world.InteractionHand; @@ -121,7 +121,7 @@ private boolean doFight() return true; } - if (citizen.isWorkerAtSiteWithMove(target.blockPosition(), 2) && citizen.hasLineOfSight(target)) + if (EntityNavigationUtils.walkToPos(citizen, target.blockPosition(), 2, false) && citizen.hasLineOfSight(target)) { citizen.swing(InteractionHand.MAIN_HAND); target.hurt(target.level.damageSources().source(DamageSourceKeys.VISITOR), 10.0f); @@ -142,7 +142,7 @@ private boolean wander() return true; } - citizen.getNavigation().moveToRandomPos(10, 0.6D); + EntityNavigationUtils.walkToRandomPos(citizen, 10, 0.6D); return false; } @@ -175,14 +175,14 @@ private VisitorState decide() if (pos != null) { ((VisitorData) citizen.getCitizenData()).setSittingPosition(pos); - citizen.isWorkerAtSiteWithMove(pos, 1); + EntityNavigationUtils.walkToPosInBuilding(citizen, pos, tavern, 3); actionTimeoutCounter = citizen.getRandom().nextInt(2500) + 3000; return VisitorState.SITTING; } } else if (random == 2) { - citizen.getNavigation().moveToRandomPos(10, 0.6D); + EntityNavigationUtils.walkToRandomPos(citizen, 10, 0.6D); actionTimeoutCounter = citizen.getCitizenColonyHandler().getColonyOrRegister().isDay() ? citizen.getRandom().nextInt(1000) + 1000 : 300; return VisitorState.WANDERING; } @@ -209,7 +209,7 @@ private boolean sit() } final BlockPos moveTo = ((VisitorData) citizen.getCitizenData()).getSittingPosition(); - if (citizen.isWorkerAtSiteWithMove(moveTo, 1)) + if (EntityNavigationUtils.walkToPosInBuilding(citizen, moveTo, tavern, 3)) { SittingEntity.sitDown(moveTo, citizen, actionTimeoutCounter); } @@ -257,16 +257,6 @@ private Entity getTarget() return target; } - /** - * Handles any exceptions for this AI. - * - * @param e exception to handle - */ - private void onException(final RuntimeException e) - { - Log.getLogger().warn("Visitor AI of:" + citizen.getName() + " threw an Exception:", e); - } - /** * Resets saved data of internal logic */ diff --git a/src/main/java/com/minecolonies/core/entity/ai/workers/AbstractEntityAIBasic.java b/src/main/java/com/minecolonies/core/entity/ai/workers/AbstractEntityAIBasic.java index 3339c47103a..bcb5d82e677 100755 --- a/src/main/java/com/minecolonies/core/entity/ai/workers/AbstractEntityAIBasic.java +++ b/src/main/java/com/minecolonies/core/entity/ai/workers/AbstractEntityAIBasic.java @@ -35,6 +35,7 @@ import com.minecolonies.core.colony.jobs.AbstractJob; import com.minecolonies.core.colony.jobs.JobDeliveryman; import com.minecolonies.core.colony.requestsystem.resolvers.StationRequestResolver; +import com.minecolonies.core.entity.pathfinding.navigation.EntityNavigationUtils; import com.minecolonies.core.util.citizenutils.CitizenItemUtils; import com.minecolonies.core.entity.pathfinding.proxy.EntityCitizenWalkToProxy; import com.minecolonies.core.tileentities.TileEntityRack; @@ -73,6 +74,7 @@ import static com.minecolonies.api.util.constant.TranslationConstants.COM_MINECOLONIES_COREMOD_ENTITY_WORKER_INVENTORYFULLCHEST; import static com.minecolonies.api.util.constant.TranslationConstants.WORKER_AI_EXCEPTION; import static com.minecolonies.core.entity.ai.workers.AbstractEntityAIInteract.RENDER_META_WORKING; +import static com.minecolonies.core.entity.pathfinding.navigation.EntityNavigationUtils.WOKR_IN_BUILDING_DIST; /** * This class provides basic ai functionality. @@ -278,13 +280,13 @@ public void setWalkTo(final BlockPos walkto) } /** - * Special walk state.. + * Only used for command-triggered walking * * @return IDLE once arrived. */ private IAIState walkToState() { - if (walkToBlock(walkTo, DEFAULT_RANGE_FOR_DELAY)) + if (!walkWithProxy(walkTo, DEFAULT_RANGE_FOR_DELAY)) { return getState(); } @@ -317,7 +319,7 @@ private IAIState getNeededItem() walkTo = pos; } - if (walkToBlock(walkTo) && pickUpCounter++ < PICKUP_ATTEMPTS) + if (!walkToWorkPos(walkTo) && pickUpCounter++ < PICKUP_ATTEMPTS) { return getState(); } @@ -599,7 +601,7 @@ private IAIState lookForRequests() } final ILocation pickupLocation = resolver instanceof StationRequestResolver ? resolver.getLocation() : building.getLocation(); - if (walkToBlock(pickupLocation.getInDimensionLocation()) || !WorldUtil.isBlockLoaded(world, pickupLocation.getInDimensionLocation())) + if (!walkToWorkPos(pickupLocation.getInDimensionLocation()) || !WorldUtil.isBlockLoaded(world, pickupLocation.getInDimensionLocation())) { return NEEDS_ITEM; } @@ -769,9 +771,9 @@ public int getTotalRequiredAmount(final ItemStack deliveredItemStack) } /** - * Walk the worker to it's building chest. Please return immediately if this returns true. + * Walk the worker to it's work building. * - * @return false if the worker is at his building + * @return true on arrival */ protected final boolean walkToBuilding() { @@ -780,13 +782,68 @@ protected final boolean walkToBuilding() { return true; } - final BlockPos standingPos = ownBuilding.getStandingPosition(); - int range = 2; - if (standingPos.equals(ownBuilding.getPosition())) + + return EntityNavigationUtils.walkToBuilding(worker, ownBuilding); + } + + /** + * Walk the worker to the given building. + * + * @return true while walking + */ + protected final boolean walkToWorkPos(final BlockPos pos) + { + if (pos == null) + { + return true; + } + + return EntityNavigationUtils.walkToPosInBuilding(worker, pos, building, WOKR_IN_BUILDING_DIST); + } + + /** + * Walk the worker to the given building. + * + * @return true while walking + */ + protected final boolean walkToBuilding(final IBuilding building) + { + if (building == null) { - range = 3; + return true; } - return walkToBlock(ownBuilding.getStandingPosition(), range); + + return EntityNavigationUtils.walkToBuilding(worker, building); + } + + /** + * Walk the worker to the safe position + * + * @return false while walking + */ + protected final boolean walkToSafePos(final BlockPos pos) + { + return EntityNavigationUtils.walkToPos(worker, pos, 4, true); + } + + /** + * Walk the worker to the unsafe position + * + * @return false while walking + */ + protected final boolean walkToUnSafePos(final BlockPos pos) + { + return EntityNavigationUtils.walkToPos(worker, pos, 4, false); + } + + /** + * Walk the worker to the unsafe position + * + * @return false while walking + */ + protected final boolean walkToUnSafePos(final BlockPos pos, final int distance) + { + return EntityNavigationUtils.walkToPos(worker, pos, distance, false); } /** @@ -820,9 +877,9 @@ public boolean checkAndTransferFromHut(@Nullable final ItemStack is) * @param stand where to walk to * @return true while walking to the block */ - protected final boolean walkToBlock(@NotNull final BlockPos stand) + protected final boolean walkWithProxy(@NotNull final BlockPos stand) { - return walkToBlock(stand, DEFAULT_RANGE_FOR_DELAY); + return walkWithProxy(stand, DEFAULT_RANGE_FOR_DELAY); } /** @@ -832,7 +889,7 @@ protected final boolean walkToBlock(@NotNull final BlockPos stand) * @param range how close we need to be * @return true while walking to the block */ - protected final boolean walkToBlock(@NotNull final BlockPos stand, final int range) + protected final boolean walkWithProxy(@NotNull final BlockPos stand, final int range) { if (proxy == null) { @@ -1019,7 +1076,7 @@ private boolean checkForNeededTool(@NotNull final EquipmentTypeEntry toolType, f } delay += DELAY_RECHECK; - return walkToBuilding() || !retrieveToolInHut(toolType, minimalLevel); + return !walkToBuilding() || !retrieveToolInHut(toolType, minimalLevel); } /** @@ -1092,7 +1149,7 @@ private IAIState dumpInventory() return afterDump(); } - if (!worker.isWorkerAtSiteWithMove(building.getPosition(), DEFAULT_RANGE_FOR_DELAY)) + if (!walkToBuilding()) { setDelay(WALK_DELAY); return INVENTORY_FULL; @@ -1171,7 +1228,7 @@ public IAIState afterDump() @SuppressWarnings("PMD.PrematureDeclaration") private boolean dumpOneMoreSlot() { - if (walkToBlock(getBuildingToDump().getPosition())) + if (!walkToWorkPos(getBuildingToDump().getPosition())) { return true; } @@ -1790,11 +1847,11 @@ private IAIState bePaused() final int percent = worker.getRandom().nextInt(ONE_HUNDRED_PERCENT); if (percent < VISIT_BUILDING_CHANCE) { - worker.getNavigation().tryMoveToBlockPos(building.getPosition(), worker.getRandom().nextBoolean() ? DEFAULT_SPEED * 1.5D : DEFAULT_SPEED * 2.2D); + walkToBuilding(); } else if (percent < WANDER_CHANCE) { - worker.getNavigation().moveToRandomPos(10, DEFAULT_SPEED); + EntityNavigationUtils.walkToRandomPos(worker, 10, DEFAULT_SPEED); } return null; diff --git a/src/main/java/com/minecolonies/core/entity/ai/workers/AbstractEntityAIInteract.java b/src/main/java/com/minecolonies/core/entity/ai/workers/AbstractEntityAIInteract.java index b41d475743e..7def80efbdb 100755 --- a/src/main/java/com/minecolonies/core/entity/ai/workers/AbstractEntityAIInteract.java +++ b/src/main/java/com/minecolonies/core/entity/ai/workers/AbstractEntityAIInteract.java @@ -8,8 +8,8 @@ import com.minecolonies.core.MineColonies; import com.minecolonies.core.colony.buildings.AbstractBuilding; import com.minecolonies.core.colony.jobs.AbstractJob; +import com.minecolonies.core.entity.pathfinding.navigation.EntityNavigationUtils; import com.minecolonies.core.util.citizenutils.CitizenItemUtils; -import com.minecolonies.core.entity.pathfinding.pathresults.PathResult; import net.minecraft.core.BlockPos; import net.minecraft.world.entity.item.ItemEntity; import net.minecraft.world.item.ItemStack; @@ -92,16 +92,6 @@ public abstract class AbstractEntityAIInteract, B ex @Nullable private List items; - /** - * The current path to the random position - */ - private PathResult pathResult; - - /** - * The backup factor of the path. - */ - protected int pathBackupFactor = 1; - /** * Block mining delay base */ @@ -297,7 +287,7 @@ private boolean checkMiningLocation(@NotNull final BlockPos blockToMine, @Nullab return true; } - if (safeStand != null && walkToBlock(safeStand) && MathUtils.twoDimDistance(worker.blockPosition(), safeStand) > MIN_WORKING_RANGE) + if (safeStand != null && walkWithProxy(safeStand) && MathUtils.twoDimDistance(worker.blockPosition(), safeStand) > MIN_WORKING_RANGE) { return true; } @@ -400,7 +390,7 @@ public void gatherItems() if (worker.getNavigation().isDone() || worker.getNavigation().getPath() == null) { final BlockPos pos = getAndRemoveClosestItemPosition(); - worker.isWorkerAtSiteWithMove(pos, ITEM_PICKUP_RANGE); + EntityNavigationUtils.walkToPos(worker, pos, 2, false); return; } @@ -449,83 +439,6 @@ private BlockPos getAndRemoveClosestItemPosition() return items.remove(index); } - /** - * Search for a random position to go to, anchored around the citizen. - * - * @param range the max range - * @return null until position was found. - */ - protected BlockPos findRandomPositionToWalkTo(final int range) - { - return findRandomPositionToWalkTo(range, worker.blockPosition()); - } - - /** - * Search for a random position to go to. - * - * @param range the max range - * @param pos position we want to find a random position around in the given range - * @return null until position was found. - */ - protected BlockPos findRandomPositionToWalkTo(final int range, final BlockPos pos) - { - if (pathResult == null) - { - pathBackupFactor = 1; - pathResult = getRandomNavigationPath(range, pos); - } - else if (pathResult.failedToReachDestination()) - { - pathBackupFactor++; - pathResult = getRandomNavigationPath(range * pathBackupFactor, pos); - } - - if (pathResult == null) - { - return null; - } - - if (pathResult.isPathReachingDestination()) - { - final BlockPos resultPos = pathResult.getPath().getEndNode().asBlockPos(); - pathResult = null; - return resultPos; - } - - if (pathResult.isCancelled()) - { - pathResult = null; - return null; - } - - if (pathBackupFactor > 10) - { - pathResult = null; - return null; - } - - return null; - } - - /** - * Get a navigator to find a certain position. - * - * @param range the max range. - * @param pos the position to - * @return the navigator. - */ - protected PathResult getRandomNavigationPath(final int range, final BlockPos pos) - { - if (pos == null || pos == worker.blockPosition()) - { - return worker.getNavigation().moveToRandomPos(range, 1.0D); - } - else - { - return worker.getNavigation().moveToRandomPosAroundX(range, 1.0D, pos); - } - } - /** * Reset the gathering items to null. */ diff --git a/src/main/java/com/minecolonies/core/entity/ai/workers/AbstractEntityAIStructure.java b/src/main/java/com/minecolonies/core/entity/ai/workers/AbstractEntityAIStructure.java index e7655f8f098..865d008019e 100755 --- a/src/main/java/com/minecolonies/core/entity/ai/workers/AbstractEntityAIStructure.java +++ b/src/main/java/com/minecolonies/core/entity/ai/workers/AbstractEntityAIStructure.java @@ -323,16 +323,7 @@ public IAIState afterStructureLoading() * @param currentBlock the current block it is working on. * @return true while walking to the site. */ - public boolean walkToConstructionSite(final BlockPos currentBlock) - { - if (workFrom == null) - { - workFrom = getWorkingPosition(currentBlock); - } - - //The miner shouldn't search for a save position. Just let him build from where he currently is. - return worker.isWorkerAtSiteWithMove(workFrom, STANDARD_WORKING_RANGE) || MathUtils.twoDimDistance(worker.blockPosition(), workFrom) < MIN_WORKING_RANGE; - } + public abstract boolean walkToConstructionSite(final BlockPos currentBlock); /** * Checks for blocks that need to be treated as deco diff --git a/src/main/java/com/minecolonies/core/entity/ai/workers/AbstractEntityAIUsesFurnace.java b/src/main/java/com/minecolonies/core/entity/ai/workers/AbstractEntityAIUsesFurnace.java index 26b5928643a..5cbc15ae56f 100755 --- a/src/main/java/com/minecolonies/core/entity/ai/workers/AbstractEntityAIUsesFurnace.java +++ b/src/main/java/com/minecolonies/core/entity/ai/workers/AbstractEntityAIUsesFurnace.java @@ -180,7 +180,7 @@ protected BlockPos getPositionOfOvenToRetrieveFuelFrom() */ public IAIState startWorking() { - if (walkToBuilding()) + if (!walkToBuilding()) { return getState(); } @@ -393,7 +393,7 @@ private IAIState retrieveSmeltableFromFurnace() return START_WORKING; } - if (walkToBlock(walkTo)) + if (!walkToWorkPos(walkTo)) { return getState(); } @@ -426,7 +426,7 @@ private IAIState retrieveUsedFuel() return START_WORKING; } - if (walkToBlock(walkTo)) + if (!walkToWorkPos(walkTo)) { return getState(); } @@ -468,7 +468,7 @@ private IAIState fillUpFurnace() return START_WORKING; } - if (walkToBlock(walkTo)) + if (!walkToWorkPos(walkTo)) { return getState(); } diff --git a/src/main/java/com/minecolonies/core/entity/ai/workers/builder/EntityAIStructureBuilder.java b/src/main/java/com/minecolonies/core/entity/ai/workers/builder/EntityAIStructureBuilder.java index 7df9cdf604c..b11111febda 100755 --- a/src/main/java/com/minecolonies/core/entity/ai/workers/builder/EntityAIStructureBuilder.java +++ b/src/main/java/com/minecolonies/core/entity/ai/workers/builder/EntityAIStructureBuilder.java @@ -145,7 +145,7 @@ public boolean isAfterDumpPickupAllowed() private IAIState startWorkingAtOwnBuilding() { - if (walkToBuilding()) + if (!walkToBuilding()) { return getState(); } @@ -230,7 +230,7 @@ else if (gotoPath.isDone()) return false; } - if (walkToBlock(workFrom)) + if (!walkToSafePos(workFrom)) { return false; } diff --git a/src/main/java/com/minecolonies/core/entity/ai/workers/crafting/AbstractEntityAICrafting.java b/src/main/java/com/minecolonies/core/entity/ai/workers/crafting/AbstractEntityAICrafting.java index 106f0a28b63..1bfd8233c15 100755 --- a/src/main/java/com/minecolonies/core/entity/ai/workers/crafting/AbstractEntityAICrafting.java +++ b/src/main/java/com/minecolonies/core/entity/ai/workers/crafting/AbstractEntityAICrafting.java @@ -24,6 +24,7 @@ import com.minecolonies.core.colony.jobs.AbstractJobCrafter; import com.minecolonies.core.entity.ai.workers.AbstractEntityAIInteract; import com.minecolonies.core.entity.citizen.EntityCitizen; +import com.minecolonies.core.entity.pathfinding.navigation.EntityNavigationUtils; import com.minecolonies.core.util.citizenutils.CitizenItemUtils; import com.minecolonies.core.network.messages.client.BlockParticleEffectMessage; import com.minecolonies.core.network.messages.client.LocalizedParticleEffectMessage; @@ -147,7 +148,7 @@ protected IAIState decide() if (building.isInBuilding(worker.blockPosition()) && ColonyConstants.rand.nextInt(20) != 0) { setDelay(TICKS_20 * 20); - worker.getNavigation().moveToRandomPos(10, DEFAULT_SPEED, building.getCorners()); + EntityNavigationUtils.walkToRandomPosWithin(worker, 10, DEFAULT_SPEED, building.getCorners()); } else { @@ -162,7 +163,7 @@ protected IAIState decide() return IDLE; } - if (walkToBuilding()) + if (!walkToBuilding()) { return START_WORKING; } @@ -395,7 +396,7 @@ protected IAIState craft() return GET_RECIPE; } - if (walkToBuilding()) + if (!walkToBuilding()) { return getState(); } diff --git a/src/main/java/com/minecolonies/core/entity/ai/workers/crafting/AbstractEntityAIRequestSmelter.java b/src/main/java/com/minecolonies/core/entity/ai/workers/crafting/AbstractEntityAIRequestSmelter.java index 8763a043d8c..b28c743dbad 100755 --- a/src/main/java/com/minecolonies/core/entity/ai/workers/crafting/AbstractEntityAIRequestSmelter.java +++ b/src/main/java/com/minecolonies/core/entity/ai/workers/crafting/AbstractEntityAIRequestSmelter.java @@ -378,7 +378,7 @@ private IAIState addFuelToFurnace() return START_WORKING; } - if (fuelPos == null || walkToBlock(fuelPos)) + if (fuelPos == null || !walkToWorkPos(fuelPos)) { return getState(); } @@ -533,7 +533,7 @@ private IAIState retrieveSmeltableFromFurnace() return START_WORKING; } - if (walkToBlock(walkTo)) + if (!walkToWorkPos(walkTo)) { return getState(); } @@ -585,7 +585,7 @@ private IAIState retrieveUsedFuel() return START_WORKING; } - if (walkToBlock(walkTo)) + if (!walkToWorkPos(walkTo)) { return getState(); } @@ -737,7 +737,7 @@ private IAIState fillUpFurnace() } if (toTransfer > 0) { - if (walkToBlock(walkTo)) + if (!walkToWorkPos(walkTo)) { return getState(); } @@ -813,7 +813,7 @@ protected IAIState craft() possibleFuels.removeIf(stack -> ItemStackUtils.compareItemStacksIgnoreStackSize(stack, currentRecipeStorage.getCleanedInput().get(0).getItemStack())); } - if (walkToBuilding()) + if (!walkToBuilding()) { setDelay(AbstractEntityAIBasic.STANDARD_DELAY); return getState(); diff --git a/src/main/java/com/minecolonies/core/entity/ai/workers/crafting/EntityAIConcreteMixer.java b/src/main/java/com/minecolonies/core/entity/ai/workers/crafting/EntityAIConcreteMixer.java index 0917133d94b..42f7b99c021 100644 --- a/src/main/java/com/minecolonies/core/entity/ai/workers/crafting/EntityAIConcreteMixer.java +++ b/src/main/java/com/minecolonies/core/entity/ai/workers/crafting/EntityAIConcreteMixer.java @@ -8,12 +8,12 @@ import com.minecolonies.api.util.Tuple; import com.minecolonies.core.colony.buildings.workerbuildings.BuildingConcreteMixer; import com.minecolonies.core.colony.jobs.JobConcreteMixer; -import net.minecraft.world.level.block.Block; -import net.minecraft.world.level.block.ConcretePowderBlock; +import net.minecraft.core.BlockPos; +import net.minecraft.core.Direction; import net.minecraft.world.item.BlockItem; import net.minecraft.world.item.ItemStack; -import net.minecraft.core.Direction; -import net.minecraft.core.BlockPos; +import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.ConcretePowderBlock; import net.minecraft.world.level.block.state.BlockState; import net.minecraftforge.common.capabilities.ForgeCapabilities; import org.jetbrains.annotations.NotNull; @@ -81,7 +81,7 @@ private IAIState placePowder() return START_WORKING; } - if (walkToBlock(posToPlace)) + if (!walkToWorkPos(posToPlace)) { return getState(); } @@ -110,7 +110,7 @@ private IAIState harvestConcrete() return START_WORKING; } - if (walkToBlock(posToMine)) + if (!walkToWorkPos(posToMine)) { return getState(); } @@ -171,7 +171,7 @@ protected IAIState decide() return performMixingWork(); } - if (walkTo == null && walkToBuilding()) + if (walkTo == null && !walkToBuilding()) { return START_WORKING; } @@ -209,7 +209,7 @@ protected IAIState craft() return GET_RECIPE; } - if (walkTo == null && walkToBuilding()) + if (walkTo == null && !walkToBuilding()) { return getState(); } diff --git a/src/main/java/com/minecolonies/core/entity/ai/workers/crafting/EntityAIWorkAlchemist.java b/src/main/java/com/minecolonies/core/entity/ai/workers/crafting/EntityAIWorkAlchemist.java index 918dc28a7bf..370b6e01885 100644 --- a/src/main/java/com/minecolonies/core/entity/ai/workers/crafting/EntityAIWorkAlchemist.java +++ b/src/main/java/com/minecolonies/core/entity/ai/workers/crafting/EntityAIWorkAlchemist.java @@ -15,13 +15,17 @@ import com.minecolonies.api.entity.citizen.VisibleCitizenStatus; import com.minecolonies.api.equipment.ModEquipmentTypes; import com.minecolonies.api.items.ModItems; -import com.minecolonies.api.util.*; +import com.minecolonies.api.util.InventoryUtils; +import com.minecolonies.api.util.ItemStackUtils; +import com.minecolonies.api.util.Tuple; +import com.minecolonies.api.util.WorldUtil; import com.minecolonies.core.Network; import com.minecolonies.core.colony.buildings.workerbuildings.BuildingAlchemist; import com.minecolonies.core.colony.interactionhandling.StandardInteraction; import com.minecolonies.core.colony.jobs.JobAlchemist; -import com.minecolonies.core.util.citizenutils.CitizenItemUtils; +import com.minecolonies.core.entity.pathfinding.navigation.EntityNavigationUtils; import com.minecolonies.core.network.messages.client.BlockParticleEffectMessage; +import com.minecolonies.core.util.citizenutils.CitizenItemUtils; import net.minecraft.core.BlockPos; import net.minecraft.network.chat.Component; import net.minecraft.sounds.SoundSource; @@ -150,7 +154,7 @@ else if (world.isEmptyBlock(randomSoil.above())) if (WorldUtil.isBlockLoaded(world, walkTo) && world.getBlockState(walkTo).getBlock() == Blocks.SOUL_SAND) { - if (walkToBlock(walkTo)) + if (!walkToWorkPos(walkTo)) { return HARVEST_NETHERWART; } @@ -240,7 +244,7 @@ private IAIState harvestMistleToe() if (WorldUtil.isBlockLoaded(world, walkTo) && world.getBlockState(walkTo).getBlock() instanceof LeavesBlock) { - if (walkToBlock(walkTo)) + if (!walkToWorkPos(walkTo)) { return HARVEST_MISTLETOE; } @@ -298,7 +302,7 @@ protected IAIState decide() if (building.isInBuilding(worker.blockPosition())) { setDelay(TICKS_20 * 20); - worker.getNavigation().moveToRandomPos(10, DEFAULT_SPEED, building.getCorners()); + EntityNavigationUtils.walkToRandomPosWithin(worker, 10, DEFAULT_SPEED, building.getCorners()); } else { @@ -308,7 +312,7 @@ protected IAIState decide() return IDLE; } - if (walkToBuilding()) + if (!walkToBuilding()) { return START_WORKING; } @@ -577,7 +581,7 @@ private IAIState addFuelToBrewingStand() return START_WORKING; } - if (fuelPos == null || walkToBlock(fuelPos)) + if (fuelPos == null || !walkToWorkPos(fuelPos)) { return getState(); } @@ -718,7 +722,7 @@ private IAIState retrieveBrewableFromBrewingStand() return START_WORKING; } - if (walkToBlock(walkTo)) + if (!walkToWorkPos(walkTo)) { return getState(); } @@ -780,7 +784,7 @@ private IAIState retrieveUsedFuel() return START_WORKING; } - if (walkToBlock(walkTo)) + if (!walkToWorkPos(walkTo)) { return getState(); } @@ -927,7 +931,7 @@ private IAIState fillUpBrewingStand() } if (toTransfer > 0) { - if (walkToBlock(walkTo)) + if (!walkToWorkPos(walkTo)) { return getState(); } @@ -986,7 +990,7 @@ else if (isEmpty(((BrewingStandBlockEntity) entity).getItem(INGREDIENT_SLOT))) } if (toTransfer > 0) { - if (walkToBlock(walkTo)) + if (!walkToWorkPos(walkTo)) { return getState(); } @@ -1027,7 +1031,7 @@ else if (!(world.getBlockState(walkTo).getBlock() instanceof BrewingStandBlock)) @Override protected IAIState craft() { - if (walkToBuilding()) + if (!walkToBuilding()) { setDelay(STANDARD_DELAY); return getState(); diff --git a/src/main/java/com/minecolonies/core/entity/ai/workers/crafting/EntityAIWorkCrusher.java b/src/main/java/com/minecolonies/core/entity/ai/workers/crafting/EntityAIWorkCrusher.java index 6b27a8711f7..a446ed5be02 100755 --- a/src/main/java/com/minecolonies/core/entity/ai/workers/crafting/EntityAIWorkCrusher.java +++ b/src/main/java/com/minecolonies/core/entity/ai/workers/crafting/EntityAIWorkCrusher.java @@ -6,14 +6,15 @@ import com.minecolonies.api.entity.ai.statemachine.AITarget; import com.minecolonies.api.entity.ai.statemachine.states.IAIState; import com.minecolonies.api.entity.citizen.VisibleCitizenStatus; -import com.minecolonies.core.util.citizenutils.CitizenItemUtils; import com.minecolonies.api.util.SoundUtils; import com.minecolonies.api.util.constant.Constants; import com.minecolonies.core.Network; import com.minecolonies.core.colony.buildings.workerbuildings.BuildingCrusher; import com.minecolonies.core.colony.jobs.JobCrusher; +import com.minecolonies.core.entity.pathfinding.navigation.EntityNavigationUtils; import com.minecolonies.core.network.messages.client.LocalizedParticleEffectMessage; import com.minecolonies.core.util.WorkerUtil; +import com.minecolonies.core.util.citizenutils.CitizenItemUtils; import net.minecraft.resources.ResourceLocation; import net.minecraft.sounds.SoundEvents; import net.minecraft.world.InteractionHand; @@ -78,7 +79,7 @@ protected IAIState decide() if (building.isInBuilding(worker.blockPosition())) { setDelay(TICKS_20 * 20); - worker.getNavigation().moveToRandomPos(10, DEFAULT_SPEED, building.getCorners()); + EntityNavigationUtils.walkToRandomPosWithin(worker, 10, DEFAULT_SPEED, building.getCorners()); } else { @@ -93,7 +94,7 @@ protected IAIState decide() return IDLE; } - if (walkToBuilding()) + if (!walkToBuilding()) { return START_WORKING; } @@ -114,7 +115,7 @@ protected IAIState decide() */ protected IAIState crush() { - if (walkToBuilding()) + if (!walkToBuilding()) { return getState(); } @@ -211,7 +212,7 @@ protected IAIState craft() return GET_RECIPE; } - if (walkToBuilding()) + if (!walkToBuilding()) { return getState(); } diff --git a/src/main/java/com/minecolonies/core/entity/ai/workers/crafting/EntityAIWorkSifter.java b/src/main/java/com/minecolonies/core/entity/ai/workers/crafting/EntityAIWorkSifter.java index eea5cf424b5..74de7c4e32c 100755 --- a/src/main/java/com/minecolonies/core/entity/ai/workers/crafting/EntityAIWorkSifter.java +++ b/src/main/java/com/minecolonies/core/entity/ai/workers/crafting/EntityAIWorkSifter.java @@ -13,14 +13,12 @@ import com.minecolonies.core.colony.buildings.workerbuildings.BuildingSifter; import com.minecolonies.core.colony.interactionhandling.StandardInteraction; import com.minecolonies.core.colony.jobs.JobSifter; -import com.minecolonies.core.entity.ai.workers.crafting.AbstractEntityAICrafting; import com.minecolonies.core.network.messages.client.LocalizedParticleEffectMessage; import com.minecolonies.core.util.WorkerUtil; -import net.minecraft.world.item.ItemStack; -import net.minecraft.world.InteractionHand; -import net.minecraft.sounds.SoundEvents; import net.minecraft.network.chat.Component; - +import net.minecraft.sounds.SoundEvents; +import net.minecraft.world.InteractionHand; +import net.minecraft.world.item.ItemStack; import org.jetbrains.annotations.NotNull; import static com.minecolonies.api.entity.ai.statemachine.states.AIWorkerState.*; @@ -104,7 +102,7 @@ protected IAIState sift() return IDLE; } - if (walkToBuilding()) + if (!walkToBuilding()) { return getState(); } diff --git a/src/main/java/com/minecolonies/core/entity/ai/workers/education/EntityAIStudy.java b/src/main/java/com/minecolonies/core/entity/ai/workers/education/EntityAIStudy.java index dee2f982caa..e0b1715e773 100755 --- a/src/main/java/com/minecolonies/core/entity/ai/workers/education/EntityAIStudy.java +++ b/src/main/java/com/minecolonies/core/entity/ai/workers/education/EntityAIStudy.java @@ -12,7 +12,6 @@ import com.minecolonies.core.datalistener.StudyItemListener; import com.minecolonies.core.datalistener.StudyItemListener.StudyItem; 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.InteractionHand; @@ -107,7 +106,7 @@ private IAIState study() studyPos = building.getRandomBookShelf(); } - if (PathfindingAIHelper.walkCloseToXNearY(worker, studyPos, building.getPosition(), 7)) + if (!walkToWorkPos(studyPos)) { setDelay(WALK_DELAY); return getState(); @@ -185,7 +184,7 @@ private IAIState study() */ private IAIState startWorkingAtOwnBuilding() { - if (walkToBuilding()) + if (!walkToBuilding()) { return getState(); } diff --git a/src/main/java/com/minecolonies/core/entity/ai/workers/education/EntityAIWorkPupil.java b/src/main/java/com/minecolonies/core/entity/ai/workers/education/EntityAIWorkPupil.java index e07ca0abfab..d5fe774e52e 100755 --- a/src/main/java/com/minecolonies/core/entity/ai/workers/education/EntityAIWorkPupil.java +++ b/src/main/java/com/minecolonies/core/entity/ai/workers/education/EntityAIWorkPupil.java @@ -9,9 +9,10 @@ import com.minecolonies.core.colony.buildings.workerbuildings.BuildingSchool; import com.minecolonies.core.colony.interactionhandling.StandardInteraction; import com.minecolonies.core.colony.jobs.JobPupil; -import com.minecolonies.core.entity.other.SittingEntity; import com.minecolonies.core.entity.ai.workers.AbstractEntityAIInteract; import com.minecolonies.core.entity.citizen.EntityCitizen; +import com.minecolonies.core.entity.other.SittingEntity; +import com.minecolonies.core.entity.pathfinding.navigation.EntityNavigationUtils; import com.minecolonies.core.network.messages.client.CircleParticleEffectMessage; import net.minecraft.core.BlockPos; import net.minecraft.core.particles.ParticleTypes; @@ -26,6 +27,7 @@ import static com.minecolonies.api.entity.ai.statemachine.states.AIWorkerState.*; import static com.minecolonies.api.research.util.ResearchConstants.TEACHING; +import static com.minecolonies.api.util.constant.Constants.DEFAULT_SPEED; import static com.minecolonies.api.util.constant.Constants.TICKS_SECOND; import static com.minecolonies.api.util.constant.TranslationConstants.PUPIL_NO_CARPET; @@ -56,11 +58,6 @@ public class EntityAIWorkPupil extends AbstractEntityAIInteract getExpectedBuildingClass() */ private IAIState startWorkingAtOwnBuilding() { - if (walkToBuilding()) + if (!walkToBuilding()) { return getState(); } diff --git a/src/main/java/com/minecolonies/core/entity/ai/workers/education/EntityAIWorkResearcher.java b/src/main/java/com/minecolonies/core/entity/ai/workers/education/EntityAIWorkResearcher.java index 976acb9d692..972e8aed1d1 100755 --- a/src/main/java/com/minecolonies/core/entity/ai/workers/education/EntityAIWorkResearcher.java +++ b/src/main/java/com/minecolonies/core/entity/ai/workers/education/EntityAIWorkResearcher.java @@ -7,7 +7,6 @@ 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; @@ -68,7 +67,7 @@ private IAIState study() studyPos = building.getRandomBookShelf(); } - if (PathfindingAIHelper.walkCloseToXNearY(worker, studyPos, building.getPosition(), 7)) + if (!walkToWorkPos(studyPos)) { return getState(); } @@ -104,7 +103,7 @@ private IAIState study() */ private IAIState startWorkingAtOwnBuilding() { - if (walkToBuilding()) + if (!walkToBuilding()) { return getState(); } diff --git a/src/main/java/com/minecolonies/core/entity/ai/workers/education/EntityAIWorkTeacher.java b/src/main/java/com/minecolonies/core/entity/ai/workers/education/EntityAIWorkTeacher.java index 9297ca57114..a9a204ad1a8 100755 --- a/src/main/java/com/minecolonies/core/entity/ai/workers/education/EntityAIWorkTeacher.java +++ b/src/main/java/com/minecolonies/core/entity/ai/workers/education/EntityAIWorkTeacher.java @@ -13,11 +13,11 @@ import com.minecolonies.core.colony.buildings.workerbuildings.BuildingSchool; import com.minecolonies.core.colony.jobs.JobPupil; import com.minecolonies.core.colony.jobs.JobTeacher; -import com.minecolonies.core.entity.other.SittingEntity; import com.minecolonies.core.entity.ai.workers.AbstractEntityAIInteract; +import com.minecolonies.core.entity.other.SittingEntity; +import net.minecraft.resources.ResourceLocation; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Items; -import net.minecraft.resources.ResourceLocation; import org.jetbrains.annotations.NotNull; import java.util.List; @@ -118,7 +118,7 @@ private IAIState teach() } worker.getCitizenData().setVisibleStatus(TEACHING_ICON); - if (walkToBlock(pupilToTeach.blockPosition())) + if (!walkToWorkPos(pupilToTeach.blockPosition())) { return getState(); } @@ -202,7 +202,7 @@ public Class getExpectedBuildingClass() */ private IAIState startWorkingAtOwnBuilding() { - if (walkToBuilding()) + if (!walkToBuilding()) { return getState(); } diff --git a/src/main/java/com/minecolonies/core/entity/ai/workers/guard/AbstractEntityAIFight.java b/src/main/java/com/minecolonies/core/entity/ai/workers/guard/AbstractEntityAIFight.java index b14f9d68051..8d0d52e346d 100755 --- a/src/main/java/com/minecolonies/core/entity/ai/workers/guard/AbstractEntityAIFight.java +++ b/src/main/java/com/minecolonies/core/entity/ai/workers/guard/AbstractEntityAIFight.java @@ -90,7 +90,7 @@ public AbstractEntityAIFight(@NotNull final J job) */ protected IAIState startWorkingAtOwnBuilding() { - if (walkToBuilding()) + if (!walkToBuilding()) { return getState(); } diff --git a/src/main/java/com/minecolonies/core/entity/ai/workers/guard/AbstractEntityAIGuard.java b/src/main/java/com/minecolonies/core/entity/ai/workers/guard/AbstractEntityAIGuard.java index 3f201d91e46..83fae8e1ffc 100755 --- a/src/main/java/com/minecolonies/core/entity/ai/workers/guard/AbstractEntityAIGuard.java +++ b/src/main/java/com/minecolonies/core/entity/ai/workers/guard/AbstractEntityAIGuard.java @@ -5,7 +5,6 @@ import com.minecolonies.api.colony.IColonyManager; import com.minecolonies.api.colony.buildings.IBuilding; import com.minecolonies.api.colony.buildings.IGuardBuilding; -import com.minecolonies.api.colony.buildings.modules.ISettingsModule; import com.minecolonies.api.colony.jobs.ModJobs; import com.minecolonies.api.colony.permissions.Action; import com.minecolonies.api.colony.requestsystem.location.ILocation; @@ -23,14 +22,15 @@ import com.minecolonies.api.util.LookHandler; import com.minecolonies.core.Network; import com.minecolonies.core.colony.buildings.AbstractBuildingGuards; +import com.minecolonies.core.colony.buildings.modules.BuildingModules; import com.minecolonies.core.colony.buildings.modules.EntityListModule; -import com.minecolonies.core.colony.buildings.modules.MinerLevelManagementModule; import com.minecolonies.core.colony.buildings.modules.settings.GuardTaskSetting; import com.minecolonies.core.colony.buildings.workerbuildings.BuildingMiner; import com.minecolonies.core.colony.jobs.AbstractJobGuard; import com.minecolonies.core.entity.ai.workers.util.MinerLevel; import com.minecolonies.core.entity.citizen.EntityCitizen; import com.minecolonies.core.entity.other.SittingEntity; +import com.minecolonies.core.entity.pathfinding.navigation.EntityNavigationUtils; import com.minecolonies.core.network.messages.client.SleepingParticleMessage; import com.minecolonies.core.util.TeleportHelper; import net.minecraft.core.BlockPos; @@ -49,7 +49,8 @@ import static com.minecolonies.api.entity.ai.statemachine.states.AIWorkerState.*; import static com.minecolonies.api.research.util.ResearchConstants.*; import static com.minecolonies.api.util.constant.Constants.*; -import static com.minecolonies.api.util.constant.GuardConstants.*; +import static com.minecolonies.api.util.constant.GuardConstants.GUARD_FOLLOW_LOSE_RANGE; +import static com.minecolonies.api.util.constant.GuardConstants.GUARD_FOLLOW_TIGHT_RANGE; import static com.minecolonies.core.colony.buildings.AbstractBuildingGuards.HOSTILE_LIST; /** @@ -249,7 +250,7 @@ private IAIState wakeUpGuard() // Move into range if (BlockPosUtil.getDistanceSquared(sleepingCitizen.blockPosition(), worker.blockPosition()) > 2.25) { - worker.getNavigation().moveToLivingEntity(sleepingCitizen, 1.0); + walkToUnSafePos(sleepingCitizen.blockPosition()); } else { @@ -402,7 +403,7 @@ private IAIState flee() } } - if (walkToBuilding()) + if (!walkToBuilding()) { return GUARD_FLEE; } @@ -426,7 +427,7 @@ private IAIState guard() */ public void guardMovement() { - worker.isWorkerAtSiteWithMove(buildingGuards.getGuardPos(), GUARD_POS_RANGE); + walkToSafePos(buildingGuards.getGuardPos()); } /** @@ -442,14 +443,7 @@ private IAIState follow() return null; } - if (buildingGuards.isTightGrouping()) - { - worker.isWorkerAtSiteWithMove(buildingGuards.getPositionToFollow(), GUARD_FOLLOW_TIGHT_RANGE); - } - else - { - worker.isWorkerAtSiteWithMove(buildingGuards.getPositionToFollow(), GUARD_FOLLOW_LOSE_RANGE); - } + walkToUnSafePos(buildingGuards.getPositionToFollow(), buildingGuards.isTightGrouping() ? GUARD_FOLLOW_TIGHT_RANGE : GUARD_FOLLOW_LOSE_RANGE); return null; } @@ -467,7 +461,7 @@ protected int getActionsDoneUntilDumping() private IAIState rally(final ILocation location) { final ICitizenData citizenData = worker.getCitizenData(); - if (!worker.isWorkerAtSiteWithMove(location.getInDimensionLocation() + if (!walkToUnSafePos(location.getInDimensionLocation() .offset(randomGenerator.nextInt(GUARD_FOLLOW_TIGHT_RANGE) - GUARD_FOLLOW_TIGHT_RANGE / 2, 0, randomGenerator.nextInt(GUARD_FOLLOW_TIGHT_RANGE) - GUARD_FOLLOW_TIGHT_RANGE / 2), @@ -508,20 +502,21 @@ public IAIState patrol() { if (buildingGuards.requiresManualTarget()) { - if (currentPatrolPoint == null || worker.isWorkerAtSiteWithMove(currentPatrolPoint, 3)) + if (currentPatrolPoint == null || walkToSafePos(currentPatrolPoint)) { - if (worker.getRandom().nextInt(5) <= 1) - { - currentPatrolPoint = buildingGuards.getColony().getBuildingManager().getRandomBuilding(b -> true); - } - else + currentPatrolPoint = null; + if (!EntityNavigationUtils.walkToRandomPos(worker, 20, 1.0)) { - currentPatrolPoint = findRandomPositionToWalkTo(20); + return getState(); } - if (currentPatrolPoint != null) + if (worker.getRandom().nextInt(5) <= 1) { - setNextPatrolTarget(currentPatrolPoint); + currentPatrolPoint = buildingGuards.getColony().getBuildingManager().getRandomBuilding(b -> true); + if (currentPatrolPoint != null) + { + walkToSafePos(currentPatrolPoint); + } } } } @@ -532,7 +527,7 @@ public IAIState patrol() currentPatrolPoint = buildingGuards.getNextPatrolTarget(false); } - if (currentPatrolPoint != null && (worker.isWorkerAtSiteWithMove(currentPatrolPoint, 3))) + if (currentPatrolPoint != null && (walkToSafePos(currentPatrolPoint))) { buildingGuards.arrivedAtPatrolPoint(worker); } @@ -551,7 +546,7 @@ public IAIState patrolMine() { return PREPARING; } - if (currentPatrolPoint == null || worker.isWorkerAtSiteWithMove(currentPatrolPoint, 2)) + if (currentPatrolPoint == null || walkToSafePos(currentPatrolPoint)) { final IBuilding building = buildingGuards.getColony().getBuildingManager().getBuilding(buildingGuards.getMinePos()); if (building != null) @@ -559,7 +554,7 @@ public IAIState patrolMine() if (building instanceof BuildingMiner) { final BuildingMiner buildingMiner = (BuildingMiner) building; - final MinerLevel level = buildingMiner.getFirstModuleOccurance(MinerLevelManagementModule.class).getCurrentLevel(); + final MinerLevel level = buildingMiner.getModule(BuildingModules.MINER_LEVELS).getCurrentLevel(); if (level == null) { setNextPatrolTarget(buildingMiner.getPosition()); @@ -571,12 +566,12 @@ public IAIState patrolMine() } else { - buildingGuards.getFirstModuleOccurance(ISettingsModule.class).getSetting(AbstractBuildingGuards.GUARD_TASK).set(GuardTaskSetting.PATROL); + buildingGuards.getModule(BuildingModules.GUARD_SETTINGS).getSetting(AbstractBuildingGuards.GUARD_TASK).set(GuardTaskSetting.PATROL); } } else { - buildingGuards.getFirstModuleOccurance(ISettingsModule.class).getSetting(AbstractBuildingGuards.GUARD_TASK).set(GuardTaskSetting.PATROL); + buildingGuards.getModule(BuildingModules.GUARD_SETTINGS).getSetting(AbstractBuildingGuards.GUARD_TASK).set(GuardTaskSetting.PATROL); } } return null; diff --git a/src/main/java/com/minecolonies/core/entity/ai/workers/guard/DruidCombatAI.java b/src/main/java/com/minecolonies/core/entity/ai/workers/guard/DruidCombatAI.java index 1dd075118c9..8e77268474f 100644 --- a/src/main/java/com/minecolonies/core/entity/ai/workers/guard/DruidCombatAI.java +++ b/src/main/java/com/minecolonies/core/entity/ai/workers/guard/DruidCombatAI.java @@ -2,12 +2,9 @@ import com.google.common.collect.ImmutableList; import com.minecolonies.api.colony.guardtype.registry.ModGuardTypes; +import com.minecolonies.api.entity.ai.combat.threat.IThreatTableEntity; import com.minecolonies.api.entity.ai.statemachine.tickratestatemachine.ITickRateStateMachine; import com.minecolonies.api.entity.citizen.Skill; -import com.minecolonies.api.entity.ai.combat.threat.IThreatTableEntity; -import com.minecolonies.core.entity.pathfinding.PathfindingUtils; -import com.minecolonies.core.entity.pathfinding.pathresults.PathResult; -import com.minecolonies.core.entity.pathfinding.PathingOptions; import com.minecolonies.api.items.ModItems; import com.minecolonies.api.util.BlockPosUtil; import com.minecolonies.api.util.InventoryUtils; @@ -15,11 +12,17 @@ import com.minecolonies.core.colony.buildings.modules.settings.GuardTaskSetting; import com.minecolonies.core.colony.jobs.AbstractJobGuard; import com.minecolonies.core.colony.jobs.JobDruid; -import com.minecolonies.core.entity.other.DruidPotionEntity; import com.minecolonies.core.entity.ai.combat.AttackMoveAI; import com.minecolonies.core.entity.citizen.EntityCitizen; +import com.minecolonies.core.entity.other.DruidPotionEntity; +import com.minecolonies.core.entity.pathfinding.PathfindingUtils; +import com.minecolonies.core.entity.pathfinding.PathingOptions; +import com.minecolonies.core.entity.pathfinding.navigation.EntityNavigationUtils; import com.minecolonies.core.entity.pathfinding.navigation.MinecoloniesAdvancedPathNavigate; -import com.minecolonies.core.entity.pathfinding.pathjobs.*; +import com.minecolonies.core.entity.pathfinding.pathjobs.PathJobCanSee; +import com.minecolonies.core.entity.pathfinding.pathjobs.PathJobMoveAwayFromLocation; +import com.minecolonies.core.entity.pathfinding.pathjobs.PathJobMoveToLocation; +import com.minecolonies.core.entity.pathfinding.pathresults.PathResult; import net.minecraft.world.InteractionHand; import net.minecraft.world.effect.MobEffect; import net.minecraft.world.effect.MobEffectInstance; @@ -114,7 +117,7 @@ protected void doAttack(final LivingEntity target) if (user.getRandom().nextInt(FLEE_CHANCE) == 0 && !((AbstractBuildingGuards) user.getCitizenData().getWorkBuilding()).getTask().equals(GuardTaskSetting.GUARD)) { - user.getNavigation().moveAwayFromLivingEntity(target, getAttackDistance() / 2.0, getCombatMovementSpeed()); + EntityNavigationUtils.walkAwayFrom(user, target.blockPosition(), (int) (getAttackDistance() / 2.0), getCombatMovementSpeed()); } } else diff --git a/src/main/java/com/minecolonies/core/entity/ai/workers/guard/EntityAIDruid.java b/src/main/java/com/minecolonies/core/entity/ai/workers/guard/EntityAIDruid.java index 2c84251e2ae..49f9606adbe 100644 --- a/src/main/java/com/minecolonies/core/entity/ai/workers/guard/EntityAIDruid.java +++ b/src/main/java/com/minecolonies/core/entity/ai/workers/guard/EntityAIDruid.java @@ -1,6 +1,7 @@ package com.minecolonies.core.entity.ai.workers.guard; import com.minecolonies.api.items.ModItems; +import com.minecolonies.api.util.BlockPosUtil; import com.minecolonies.api.util.InventoryUtils; import com.minecolonies.core.colony.buildings.AbstractBuildingGuards; import com.minecolonies.core.colony.jobs.JobDruid; @@ -67,16 +68,17 @@ public void guardMovement() { if (worker.getRandom().nextInt(3) < 1) { - worker.isWorkerAtSiteWithMove(buildingGuards.getGuardPos(), 3); + walkToSafePos(buildingGuards.getGuardPos()); return; } - if (worker.isWorkerAtSiteWithMove(buildingGuards.getGuardPos(), 10) || Math.abs(buildingGuards.getGuardPos().getY() - worker.blockPosition().getY()) > 3) + if ((BlockPosUtil.dist(buildingGuards.getGuardPos(), worker.blockPosition()) <= 10 || walkToSafePos(buildingGuards.getGuardPos())) + || Math.abs(buildingGuards.getGuardPos().getY() - worker.blockPosition().getY()) > 3) { // Moves the druid randomly to close edges, for better vision to mobs ((MinecoloniesAdvancedPathNavigate) worker.getNavigation()).setPathJob(new PathJobWalkRandomEdge(world, buildingGuards.getGuardPos(), 20, worker), - null, - 1.0, true); + null, + 1.0, true); } } } diff --git a/src/main/java/com/minecolonies/core/entity/ai/workers/guard/EntityAIRanger.java b/src/main/java/com/minecolonies/core/entity/ai/workers/guard/EntityAIRanger.java index 1b0039eeeb6..3011b57aff8 100755 --- a/src/main/java/com/minecolonies/core/entity/ai/workers/guard/EntityAIRanger.java +++ b/src/main/java/com/minecolonies/core/entity/ai/workers/guard/EntityAIRanger.java @@ -1,6 +1,7 @@ package com.minecolonies.core.entity.ai.workers.guard; import com.minecolonies.api.equipment.ModEquipmentTypes; +import com.minecolonies.api.util.BlockPosUtil; import com.minecolonies.api.util.InventoryUtils; import com.minecolonies.core.colony.buildings.AbstractBuildingGuards; import com.minecolonies.core.colony.jobs.JobRanger; @@ -66,11 +67,12 @@ public void guardMovement() { if (worker.getRandom().nextInt(3) < 1) { - worker.isWorkerAtSiteWithMove(buildingGuards.getGuardPos(), 3); + walkToSafePos(buildingGuards.getGuardPos()); return; } - if (worker.isWorkerAtSiteWithMove(buildingGuards.getGuardPos(), 10) || Math.abs(buildingGuards.getGuardPos().getY() - worker.blockPosition().getY()) > 3) + if ((BlockPosUtil.dist(buildingGuards.getGuardPos(), worker.blockPosition()) <= 10 || walkToSafePos(buildingGuards.getGuardPos())) + || Math.abs(buildingGuards.getGuardPos().getY() - worker.blockPosition().getY()) > 3) { // Moves the ranger randomly to close edges, for better vision to mobs ((MinecoloniesAdvancedPathNavigate) worker.getNavigation()).setPathJob(new PathJobWalkRandomEdge(world, buildingGuards.getGuardPos(), 20, worker), diff --git a/src/main/java/com/minecolonies/core/entity/ai/workers/guard/KnightCombatAI.java b/src/main/java/com/minecolonies/core/entity/ai/workers/guard/KnightCombatAI.java index d0e720a0187..8cb0739ee49 100644 --- a/src/main/java/com/minecolonies/core/entity/ai/workers/guard/KnightCombatAI.java +++ b/src/main/java/com/minecolonies/core/entity/ai/workers/guard/KnightCombatAI.java @@ -14,14 +14,15 @@ import com.minecolonies.api.util.ItemStackUtils; import com.minecolonies.api.util.SoundUtils; import com.minecolonies.api.util.constant.ColonyConstants; -import com.minecolonies.core.util.citizenutils.CitizenItemUtils; import com.minecolonies.api.util.constant.Constants; import com.minecolonies.core.MineColonies; import com.minecolonies.core.colony.jobs.AbstractJobGuard; import com.minecolonies.core.entity.ai.combat.AttackMoveAI; import com.minecolonies.core.entity.ai.combat.CombatUtils; import com.minecolonies.core.entity.citizen.EntityCitizen; +import com.minecolonies.core.entity.pathfinding.navigation.EntityNavigationUtils; import com.minecolonies.core.entity.pathfinding.pathresults.PathResult; +import com.minecolonies.core.util.citizenutils.CitizenItemUtils; import net.minecraft.core.particles.ParticleTypes; import net.minecraft.nbt.CompoundTag; import net.minecraft.network.chat.contents.TranslatableContents; @@ -302,7 +303,8 @@ protected int getAttackDelay() @Override protected PathResult moveInAttackPosition(final LivingEntity target) { - return user.getNavigation().moveToXYZ(target.getX(), target.getY(), target.getZ(), getCombatMovementSpeed()); + EntityNavigationUtils.walkToPos(user, target.blockPosition(), (int) getAttackDistance(), false, getCombatMovementSpeed()); + return user.getNavigation().getPathResult(); } /** diff --git a/src/main/java/com/minecolonies/core/entity/ai/workers/guard/RangerCombatAI.java b/src/main/java/com/minecolonies/core/entity/ai/workers/guard/RangerCombatAI.java index a409be80f4a..ccd8aeff70a 100644 --- a/src/main/java/com/minecolonies/core/entity/ai/workers/guard/RangerCombatAI.java +++ b/src/main/java/com/minecolonies/core/entity/ai/workers/guard/RangerCombatAI.java @@ -20,6 +20,7 @@ import com.minecolonies.core.entity.other.CustomArrowEntity; import com.minecolonies.core.entity.pathfinding.PathfindingUtils; import com.minecolonies.core.entity.pathfinding.PathingOptions; +import com.minecolonies.core.entity.pathfinding.navigation.EntityNavigationUtils; import com.minecolonies.core.entity.pathfinding.navigation.MinecoloniesAdvancedPathNavigate; import com.minecolonies.core.entity.pathfinding.pathjobs.PathJobCanSee; import com.minecolonies.core.entity.pathfinding.pathjobs.PathJobMoveAwayFromLocation; @@ -143,7 +144,7 @@ protected void doAttack(final LivingEntity target) if (user.getRandom().nextInt(FLEE_CHANCE) == 0 && !((AbstractBuildingGuards) user.getCitizenData().getWorkBuilding()).getTask().equals(GuardTaskSetting.GUARD)) { - user.getNavigation().moveAwayFromLivingEntity(target, getAttackDistance() / 2.0, getCombatMovementSpeed()); + EntityNavigationUtils.walkAwayFrom(user, target.blockPosition(), (int) (getAttackDistance() / 2.0), getCombatMovementSpeed()); } } else diff --git a/src/main/java/com/minecolonies/core/entity/ai/workers/guard/training/AbstractEntityAITraining.java b/src/main/java/com/minecolonies/core/entity/ai/workers/guard/training/AbstractEntityAITraining.java index a68094698f1..d21d8042f4b 100755 --- a/src/main/java/com/minecolonies/core/entity/ai/workers/guard/training/AbstractEntityAITraining.java +++ b/src/main/java/com/minecolonies/core/entity/ai/workers/guard/training/AbstractEntityAITraining.java @@ -5,6 +5,7 @@ import com.minecolonies.core.colony.buildings.AbstractBuilding; import com.minecolonies.core.colony.jobs.AbstractJob; import com.minecolonies.core.entity.ai.workers.AbstractEntityAIInteract; +import com.minecolonies.core.entity.pathfinding.navigation.EntityNavigationUtils; import net.minecraft.core.BlockPos; import org.jetbrains.annotations.NotNull; @@ -93,19 +94,12 @@ public IAIState decide() */ private IAIState wander() { - if (currentPathingTarget == null) + if (!EntityNavigationUtils.walkToRandomPosWithin(worker, 20, 0.6, building.getCorners())) { - currentPathingTarget = getWanderPosition(); return getState(); } - if (!walkToBlock(currentPathingTarget)) - { - currentPathingTarget = null; - return DECIDE; - } - - return TRAINING_WANDER; + return DECIDE; } /** @@ -115,34 +109,13 @@ private IAIState wander() */ private IAIState pathToTarget() { - if (walkToBlock(currentPathingTarget, 2)) + if (!walkToWorkPos(currentPathingTarget)) { return getState(); } return stateAfterPathing; } - /** - * Get a wander position within the archer training camp to walk to. - * - * @return the position or the location of the hut chest if not found. - */ - private BlockPos getWanderPosition() - { - final BlockPos pos = findRandomPositionToWalkTo(20); - if (pos == null) - { - return null; - } - - if (building.isInBuilding(pos)) - { - return pos; - } - - return building.getPosition(); - } - /** * Reduces the attack delay by the given Tickrate */ diff --git a/src/main/java/com/minecolonies/core/entity/ai/workers/guard/training/EntityAICombatTraining.java b/src/main/java/com/minecolonies/core/entity/ai/workers/guard/training/EntityAICombatTraining.java index 4e98efbe600..bcf5fb04d47 100755 --- a/src/main/java/com/minecolonies/core/entity/ai/workers/guard/training/EntityAICombatTraining.java +++ b/src/main/java/com/minecolonies/core/entity/ai/workers/guard/training/EntityAICombatTraining.java @@ -10,12 +10,13 @@ import com.minecolonies.api.util.SoundUtils; import com.minecolonies.core.colony.buildings.workerbuildings.BuildingCombatAcademy; import com.minecolonies.core.colony.jobs.JobCombatTraining; -import com.minecolonies.core.util.citizenutils.CitizenItemUtils; +import com.minecolonies.core.entity.pathfinding.navigation.EntityNavigationUtils; import com.minecolonies.core.util.WorkerUtil; -import net.minecraft.world.item.Items; -import net.minecraft.world.InteractionHand; -import net.minecraft.sounds.SoundEvents; +import com.minecolonies.core.util.citizenutils.CitizenItemUtils; import net.minecraft.core.BlockPos; +import net.minecraft.sounds.SoundEvents; +import net.minecraft.world.InteractionHand; +import net.minecraft.world.item.Items; import org.jetbrains.annotations.NotNull; import static com.minecolonies.api.entity.ai.statemachine.states.AIWorkerState.*; @@ -145,7 +146,7 @@ private IAIState trainWithPartner() return COMBAT_TRAINING; } - if (BlockPosUtil.getDistance2D(worker.blockPosition(), trainingPartner.blockPosition()) > MIN_DISTANCE_TO_TRAIN && walkToBlock(trainingPartner.blockPosition())) + if (BlockPosUtil.getDistance2D(worker.blockPosition(), trainingPartner.blockPosition()) > MIN_DISTANCE_TO_TRAIN && !walkToWorkPos(trainingPartner.blockPosition())) { return KNIGHT_TRAIN_WITH_PARTNER; } @@ -198,7 +199,7 @@ private IAIState attack() trainingPartner.hurt(world.damageSources().source(DamageSourceKeys.TRAINING, worker), 0.0F); CitizenItemUtils.damageItemInHand(worker, InteractionHand.MAIN_HAND, 1); } - worker.getNavigation().moveAwayFromXYZ(trainingPartner.blockPosition(), 4.0, 1.0, true); + EntityNavigationUtils.walkAwayFrom(worker, trainingPartner.blockPosition(), 4, 1.0); targetCounter++; if (targetCounter > building.getBuildingLevel() * ACTIONS_PER_BUILDING_LEVEL) diff --git a/src/main/java/com/minecolonies/core/entity/ai/workers/production/EntityAIQuarrier.java b/src/main/java/com/minecolonies/core/entity/ai/workers/production/EntityAIQuarrier.java index 9bb0748683b..0e2ab15e4cb 100644 --- a/src/main/java/com/minecolonies/core/entity/ai/workers/production/EntityAIQuarrier.java +++ b/src/main/java/com/minecolonies/core/entity/ai/workers/production/EntityAIQuarrier.java @@ -16,7 +16,6 @@ import com.minecolonies.api.entity.ai.statemachine.AITarget; import com.minecolonies.api.entity.ai.statemachine.states.IAIState; import com.minecolonies.api.entity.citizen.VisibleCitizenStatus; -import com.minecolonies.core.entity.ai.workers.util.LayerBlueprintIterator; import com.minecolonies.api.tileentities.AbstractTileEntityColonyBuilding; import com.minecolonies.api.util.*; import com.minecolonies.core.colony.buildings.AbstractBuildingStructureBuilder; @@ -27,7 +26,9 @@ import com.minecolonies.core.colony.workorders.WorkOrderMiner; import com.minecolonies.core.entity.ai.workers.AbstractEntityAIStructureWithWorkOrder; import com.minecolonies.core.entity.ai.workers.util.BuildingStructureHandler; +import com.minecolonies.core.entity.ai.workers.util.LayerBlueprintIterator; import com.minecolonies.core.entity.ai.workers.util.WorkerLoadOnlyStructureHandler; +import com.minecolonies.core.entity.pathfinding.navigation.EntityNavigationUtils; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.network.chat.Component; @@ -123,7 +124,7 @@ private IAIState startWorkingAtOwnBuilding() return IDLE; } - if (walkToBlock(quarry.getPosition())) + if (!walkToBuilding(quarry)) { return getState(); } @@ -669,35 +670,29 @@ public boolean walkToConstructionSite(final BlockPos currentBlock) { if (workFrom == null) { - workFrom = findRandomPositionToWalkTo(5, currentBlock); - if (workFrom == null && pathBackupFactor > 10) + if (EntityNavigationUtils.walkToRandomPosAround(worker, currentBlock, 5, 1.0)) { workFrom = worker.blockPosition(); } return false; } - if (BlockPosUtil.getDistance(worker.blockPosition(), currentBlock) <= 5 + 5 * pathBackupFactor) + if (BlockPosUtil.getDistance(worker.blockPosition(), currentBlock) <= 5 + 5) { return true; } - if (walkToBlock(workFrom)) + if (!walkToSafePos(workFrom)) { return false; } - if (BlockPosUtil.getDistance(worker.blockPosition(), currentBlock) > 5 + 5 * pathBackupFactor) + if (BlockPosUtil.getDistance(worker.blockPosition(), currentBlock) > 5 + 5) { workFrom = null; return false; } - if (pathBackupFactor > 1) - { - pathBackupFactor--; - } - return true; } diff --git a/src/main/java/com/minecolonies/core/entity/ai/workers/production/EntityAIStructureMiner.java b/src/main/java/com/minecolonies/core/entity/ai/workers/production/EntityAIStructureMiner.java index ee298b7b719..c197e310a9d 100755 --- a/src/main/java/com/minecolonies/core/entity/ai/workers/production/EntityAIStructureMiner.java +++ b/src/main/java/com/minecolonies/core/entity/ai/workers/production/EntityAIStructureMiner.java @@ -45,6 +45,8 @@ import static com.minecolonies.api.entity.ai.statemachine.states.AIWorkerState.*; import static com.minecolonies.api.research.util.ResearchConstants.MORE_ORES; +import static com.minecolonies.api.util.constant.CitizenConstants.MIN_WORKING_RANGE; +import static com.minecolonies.api.util.constant.CitizenConstants.STANDARD_WORKING_RANGE; import static com.minecolonies.api.util.constant.Constants.ONE_HUNDRED_PERCENT; import static com.minecolonies.api.util.constant.Constants.TICKS_SECOND; import static com.minecolonies.api.util.constant.StatisticsConstants.*; @@ -173,7 +175,7 @@ public Class getExpectedBuildingClass() private IAIState startWorkingAtOwnBuilding() { worker.getCitizenData().setVisibleStatus(VisibleCitizenStatus.WORKING); - if ((building.getLadderLocation() == null || worker.getY() >= building.getPosition().getY()) && walkToBuilding()) + if ((building.getLadderLocation() == null || worker.getY() >= building.getPosition().getY()) && !walkToBuilding()) { return START_WORKING; } @@ -308,7 +310,7 @@ private BlockPos getSurroundingOreOrDefault(final BlockPos pos) @NotNull private IAIState goToLadder() { - if (walkToLadder()) + if (!walkToLadder()) { return MINER_WALKING_TO_LADDER; } @@ -317,7 +319,18 @@ private IAIState goToLadder() private boolean walkToLadder() { - return walkToBlock(building.getLadderLocation()); + return walkToWorkPos(building.getLadderLocation()); + } + + public boolean walkToConstructionSite(final BlockPos currentBlock) + { + if (workFrom == null) + { + workFrom = getWorkingPosition(currentBlock); + } + + //The miner shouldn't search for a save position. Just let him build from where he currently is. + return walkWithProxy(workFrom, STANDARD_WORKING_RANGE) || MathUtils.twoDimDistance(worker.blockPosition(), workFrom) < MIN_WORKING_RANGE; } @NotNull @@ -627,7 +640,7 @@ private BlockPos getNextBlockInShaftToMine() @NotNull private IAIState doShaftBuilding() { - if (walkToBuilding()) + if (!walkToBuilding()) { return MINER_BUILDING_SHAFT; } @@ -730,7 +743,7 @@ else if (vectorZ == 1) return MINER_MINING_SHAFT; } - if ((workingNode.getStatus() == MineNode.NodeStatus.AVAILABLE || workingNode.getStatus() == MineNode.NodeStatus.IN_PROGRESS) && !walkToBlock(standingPosition)) + if ((workingNode.getStatus() == MineNode.NodeStatus.AVAILABLE || workingNode.getStatus() == MineNode.NodeStatus.IN_PROGRESS) && !walkWithProxy(standingPosition)) { workingNode.setRot(rotation); return executeStructurePlacement(workingNode, standingPosition, rotation); diff --git a/src/main/java/com/minecolonies/core/entity/ai/workers/production/EntityAIWorkLumberjack.java b/src/main/java/com/minecolonies/core/entity/ai/workers/production/EntityAIWorkLumberjack.java index 4ec4498d1b1..fb796961bf5 100755 --- a/src/main/java/com/minecolonies/core/entity/ai/workers/production/EntityAIWorkLumberjack.java +++ b/src/main/java/com/minecolonies/core/entity/ai/workers/production/EntityAIWorkLumberjack.java @@ -5,8 +5,8 @@ import com.minecolonies.api.entity.ai.statemachine.AITarget; import com.minecolonies.api.entity.ai.statemachine.states.IAIState; import com.minecolonies.api.entity.citizen.VisibleCitizenStatus; -import com.minecolonies.api.items.ModTags; import com.minecolonies.api.equipment.ModEquipmentTypes; +import com.minecolonies.api.items.ModTags; import com.minecolonies.api.util.*; import com.minecolonies.api.util.constant.ColonyConstants; import com.minecolonies.api.util.constant.Constants; @@ -17,13 +17,13 @@ import com.minecolonies.core.colony.jobs.JobLumberjack; import com.minecolonies.core.entity.ai.workers.crafting.AbstractEntityAICrafting; import com.minecolonies.core.entity.ai.workers.util.Tree; -import com.minecolonies.core.util.citizenutils.CitizenItemUtils; import com.minecolonies.core.entity.pathfinding.PathfindingUtils; import com.minecolonies.core.entity.pathfinding.navigation.MinecoloniesAdvancedPathNavigate; import com.minecolonies.core.entity.pathfinding.pathjobs.PathJobMoveToWithPassable; import com.minecolonies.core.entity.pathfinding.pathresults.PathResult; import com.minecolonies.core.entity.pathfinding.pathresults.TreePathResult; import com.minecolonies.core.util.WorkerUtil; +import com.minecolonies.core.util.citizenutils.CitizenItemUtils; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.resources.ResourceLocation; @@ -223,7 +223,7 @@ protected IAIState decide() return getState(); } - if (walkToBuilding()) + if (!walkToBuilding()) { return START_WORKING; } @@ -268,7 +268,7 @@ private boolean isStackLog(@Nullable final ItemStack stack) */ private IAIState startWorkingAtOwnBuilding() { - if (walkToBuilding()) + if (!walkToBuilding()) { return getState(); } @@ -393,7 +393,7 @@ private IAIState findTree() final BlockPos endPos = building.getEndRestriction(); pathResult = worker.getNavigation() - .moveToTree(startPos, + .walkToTree(startPos, endPos, 1.0D, building.getModuleMatching(ItemListModule.class, m -> m.getId().equals(SAPLINGS_LIST)).getList(), @@ -403,7 +403,7 @@ private IAIState findTree() else { pathResult = worker.getNavigation() - .moveToTree(SEARCH_RANGE + searchIncrement, + .walkToTree(SEARCH_RANGE + searchIncrement, 1.0D, building.getModuleMatching(ItemListModule.class, m -> m.getId().equals(SAPLINGS_LIST)).getList(), building.getSetting(BuildingLumberjack.DYNAMIC_TREES_SIZE).getValue(), @@ -853,7 +853,7 @@ private boolean plantSapling(@NotNull final BlockPos location) worker.swing(worker.getUsedItemHand()); } - if (timeWaited >= MAX_WAITING_TIME / 2 && !checkedInHut && !walkToBuilding()) + if (timeWaited >= MAX_WAITING_TIME / 2 && !checkedInHut && walkToBuilding()) { checkAndTransferFromHut(job.getTree().getSapling()); checkedInHut = true; diff --git a/src/main/java/com/minecolonies/core/entity/ai/workers/production/EntityAIWorkNether.java b/src/main/java/com/minecolonies/core/entity/ai/workers/production/EntityAIWorkNether.java index 66880efd086..3463229eae2 100644 --- a/src/main/java/com/minecolonies/core/entity/ai/workers/production/EntityAIWorkNether.java +++ b/src/main/java/com/minecolonies/core/entity/ai/workers/production/EntityAIWorkNether.java @@ -22,9 +22,9 @@ import com.minecolonies.core.colony.buildings.workerbuildings.BuildingNetherWorker; import com.minecolonies.core.colony.jobs.JobNetherWorker; import com.minecolonies.core.entity.ai.workers.crafting.AbstractEntityAICrafting; -import com.minecolonies.core.util.citizenutils.CitizenItemUtils; import com.minecolonies.core.items.ItemAdventureToken; import com.minecolonies.core.util.TeleportHelper; +import com.minecolonies.core.util.citizenutils.CitizenItemUtils; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.nbt.CompoundTag; @@ -308,7 +308,7 @@ protected IAIState decide() setDelay(120); return IDLE; } - if (walkTo != null || walkToBuilding()) + if (walkTo != null || !walkToBuilding()) { return getState(); } @@ -381,7 +381,7 @@ protected IAIState leaveForNether() final BlockState block = world.getBlockState(portal); if (block.is(Blocks.NETHER_PORTAL)) { - if (walkToBlock(portal, 1)) + if (!walkToWorkPos(portal)) { return getState(); } @@ -419,7 +419,7 @@ protected IAIState stayInNether() { //Ensure we stay put in the portal final BlockPos portal = building.getPortalLocation(); - if (portal != null && walkToBlock(portal, 1)) + if (portal != null && !walkToWorkPos(portal)) { return getState(); } @@ -689,7 +689,7 @@ protected IAIState returnFromNether() return NETHER_CLOSEPORTAL; } - if (walkToBuilding()) + if (!walkToBuilding()) { return getState(); } @@ -710,7 +710,7 @@ protected IAIState openPortal() final BlockPos portal = building.getPortalLocation(); if (portal != null && currentRecipeStorage != null) { - if (walkToBlock(portal, 1)) + if (!walkToWorkPos(portal)) { return getState(); } @@ -744,7 +744,7 @@ protected IAIState closePortal() if (block.is(Blocks.NETHER_PORTAL)) { - if (walkToBlock(portal, 1)) + if (!walkToWorkPos(portal)) { return getState(); } diff --git a/src/main/java/com/minecolonies/core/entity/ai/workers/production/agriculture/EntityAIWorkBeekeeper.java b/src/main/java/com/minecolonies/core/entity/ai/workers/production/agriculture/EntityAIWorkBeekeeper.java index 03ea995a813..e761f268d43 100644 --- a/src/main/java/com/minecolonies/core/entity/ai/workers/production/agriculture/EntityAIWorkBeekeeper.java +++ b/src/main/java/com/minecolonies/core/entity/ai/workers/production/agriculture/EntityAIWorkBeekeeper.java @@ -181,7 +181,7 @@ private IAIState prepareForHerding() */ private IAIState startWorkingAtOwnBuilding() { - if (walkToBuilding()) + if (!walkToBuilding()) { setDelay(2); return getState(); @@ -349,7 +349,7 @@ private IAIState harvestHoney() building.removeHive(hive); return PREPARING; } - if (walkToBlock(hive)) + if (!walkToWorkPos(hive)) { return getState(); } @@ -404,17 +404,17 @@ else if (!building.getHarvestTypes().equals(BuildingBeekeeper.HONEYCOMB) && item * Lets the herder walk to the animal. * * @param animal the animal to walk to. - * @return true if the herder is walking to the animal. + * @return false if the herder is walking to the animal. */ public boolean walkingToAnimal(final Animal animal) { if (animal != null) { - return walkToBlock(animal.blockPosition()); + return walkToWorkPos(animal.blockPosition()); } else { - return false; + return true; } } @@ -473,7 +473,7 @@ private void breedTwoAnimals(final Animal animalOne, final Animal animalTwo) for (final Animal animal : animalsToBreed) { - if (!animal.isInLove() && !walkingToAnimal(animal)) + if (!animal.isInLove() && walkingToAnimal(animal)) { animal.setInLove(null); worker.swing(InteractionHand.MAIN_HAND); diff --git a/src/main/java/com/minecolonies/core/entity/ai/workers/production/agriculture/EntityAIWorkComposter.java b/src/main/java/com/minecolonies/core/entity/ai/workers/production/agriculture/EntityAIWorkComposter.java index a682e335406..a43cb5020d6 100755 --- a/src/main/java/com/minecolonies/core/entity/ai/workers/production/agriculture/EntityAIWorkComposter.java +++ b/src/main/java/com/minecolonies/core/entity/ai/workers/production/agriculture/EntityAIWorkComposter.java @@ -17,8 +17,8 @@ import com.minecolonies.core.colony.buildings.workerbuildings.BuildingComposter; import com.minecolonies.core.colony.jobs.JobComposter; import com.minecolonies.core.entity.ai.workers.AbstractEntityAIInteract; -import com.minecolonies.core.util.citizenutils.CitizenItemUtils; import com.minecolonies.core.tileentities.TileEntityBarrel; +import com.minecolonies.core.util.citizenutils.CitizenItemUtils; import net.minecraft.core.BlockPos; import net.minecraft.resources.ResourceLocation; import net.minecraft.util.RandomSource; @@ -135,7 +135,7 @@ private IAIState accelerateBarrels() */ private IAIState getMaterials() { - if (walkToBuilding()) + if (!walkToBuilding()) { setDelay(2); return getState(); @@ -201,7 +201,7 @@ private IAIState decideWhatToDo() { worker.getCitizenData().setVisibleStatus(VisibleCitizenStatus.WORKING); - if (walkToBuilding()) + if (!walkToBuilding()) { setDelay(2); return getState(); @@ -264,7 +264,7 @@ private IAIState fillBarrels() return GET_MATERIALS; } } - if (walkToBlock(currentTarget)) + if (!walkToWorkPos(currentTarget)) { setDelay(2); return getState(); @@ -295,7 +295,7 @@ private IAIState fillBarrels() private IAIState harvestBarrels() { - if (walkToBlock(currentTarget)) + if (!walkToWorkPos(currentTarget)) { setDelay(2); return getState(); diff --git a/src/main/java/com/minecolonies/core/entity/ai/workers/production/agriculture/EntityAIWorkFarmer.java b/src/main/java/com/minecolonies/core/entity/ai/workers/production/agriculture/EntityAIWorkFarmer.java index 87629b7ed48..6ccf8f363c1 100755 --- a/src/main/java/com/minecolonies/core/entity/ai/workers/production/agriculture/EntityAIWorkFarmer.java +++ b/src/main/java/com/minecolonies/core/entity/ai/workers/production/agriculture/EntityAIWorkFarmer.java @@ -29,10 +29,10 @@ import com.minecolonies.core.colony.interactionhandling.StandardInteraction; import com.minecolonies.core.colony.jobs.JobFarmer; import com.minecolonies.core.entity.ai.workers.crafting.AbstractEntityAICrafting; -import com.minecolonies.core.util.citizenutils.CitizenItemUtils; import com.minecolonies.core.items.ItemCrop; import com.minecolonies.core.network.messages.client.CompostParticleMessage; import com.minecolonies.core.util.AdvancementUtils; +import com.minecolonies.core.util.citizenutils.CitizenItemUtils; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.core.NonNullList; @@ -66,8 +66,8 @@ import static com.minecolonies.api.util.constant.CitizenConstants.BLOCK_BREAK_SOUND_RANGE; import static com.minecolonies.api.util.constant.Constants.STACKSIZE; import static com.minecolonies.api.util.constant.Constants.TICKS_SECOND; -import static com.minecolonies.api.util.constant.StatisticsConstants.*; import static com.minecolonies.api.util.constant.EquipmentLevelConstants.TOOL_LEVEL_WOOD_OR_GOLD; +import static com.minecolonies.api.util.constant.StatisticsConstants.*; import static com.minecolonies.api.util.constant.TranslationConstants.NO_FREE_FIELDS; import static com.minecolonies.core.colony.buildings.modules.BuildingModules.STATS_MODULE; @@ -338,7 +338,7 @@ private IAIState canGoPlanting(@NotNull final FarmField farmField) return FARMER_PLANT; } - if (walkToBuilding()) + if (!walkToBuilding()) { return PREPARING; } @@ -520,7 +520,7 @@ private IAIState workAtField() final BlockPos position = farmField.getPosition().below().south(workingOffset.getZ()).east(workingOffset.getX()); // Still moving to the block - if (walkToBlock(position.above())) + if (!walkToSafePos(position.above())) { return getState(); } diff --git a/src/main/java/com/minecolonies/core/entity/ai/workers/production/agriculture/EntityAIWorkFisherman.java b/src/main/java/com/minecolonies/core/entity/ai/workers/production/agriculture/EntityAIWorkFisherman.java index a0583a3e8b0..70f079498eb 100755 --- a/src/main/java/com/minecolonies/core/entity/ai/workers/production/agriculture/EntityAIWorkFisherman.java +++ b/src/main/java/com/minecolonies/core/entity/ai/workers/production/agriculture/EntityAIWorkFisherman.java @@ -15,13 +15,13 @@ import com.minecolonies.core.colony.jobs.JobFisherman; import com.minecolonies.core.entity.ai.workers.AbstractEntityAISkill; import com.minecolonies.core.entity.citizen.EntityCitizen; -import com.minecolonies.core.util.citizenutils.CitizenItemUtils; import com.minecolonies.core.entity.other.NewBobberEntity; import com.minecolonies.core.entity.pathfinding.Pathfinding; import com.minecolonies.core.entity.pathfinding.PathfindingUtils; import com.minecolonies.core.entity.pathfinding.pathjobs.PathJobFindWater; import com.minecolonies.core.entity.pathfinding.pathresults.WaterPathResult; import com.minecolonies.core.util.WorkerUtil; +import com.minecolonies.core.util.citizenutils.CitizenItemUtils; import net.minecraft.core.BlockPos; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; @@ -182,7 +182,7 @@ public Class getExpectedBuildingClass() */ private IAIState startWorkingAtOwnBuilding() { - if (walkToBuilding()) + if (!walkToBuilding()) { return getState(); } @@ -274,7 +274,7 @@ private IAIState getToWater() return FISHERMAN_SEARCHING_WATER; } - if (walkToWater()) + if (!walkToWater()) { return getState(); } @@ -288,7 +288,7 @@ private IAIState getToWater() */ private boolean walkToWater() { - return job.getWater() != null && walkToBlock(job.getWater().getB()); + return job.getWater() != null && walkToSafePos(job.getWater().getB()); } /** diff --git a/src/main/java/com/minecolonies/core/entity/ai/workers/production/agriculture/EntityAIWorkFlorist.java b/src/main/java/com/minecolonies/core/entity/ai/workers/production/agriculture/EntityAIWorkFlorist.java index 38e1a228736..90fa4310d4f 100755 --- a/src/main/java/com/minecolonies/core/entity/ai/workers/production/agriculture/EntityAIWorkFlorist.java +++ b/src/main/java/com/minecolonies/core/entity/ai/workers/production/agriculture/EntityAIWorkFlorist.java @@ -153,7 +153,7 @@ private IAIState decide() worker.setItemInHand(InteractionHand.MAIN_HAND, ItemStack.EMPTY); final long distance = BlockPosUtil.getDistance2D(worker.blockPosition(), building.getPosition()); - if (distance > MAX_DISTANCE && walkToBuilding()) + if (distance > MAX_DISTANCE && !walkToBuilding()) { return DECIDE; } @@ -209,7 +209,7 @@ private IAIState compost() worker.getCitizenData().setVisibleStatus(GARDENING); - if (walkToBlock(compostPosition)) + if (!walkToWorkPos(compostPosition)) { return getState(); } @@ -251,7 +251,7 @@ private IAIState harvest() worker.getCitizenData().setVisibleStatus(GARDENING); - if (walkToBlock(harvestPosition)) + if (!walkToWorkPos(harvestPosition)) { return getState(); } diff --git a/src/main/java/com/minecolonies/core/entity/ai/workers/production/agriculture/EntityAIWorkPlanter.java b/src/main/java/com/minecolonies/core/entity/ai/workers/production/agriculture/EntityAIWorkPlanter.java index b82f2a25693..6acda120ba6 100755 --- a/src/main/java/com/minecolonies/core/entity/ai/workers/production/agriculture/EntityAIWorkPlanter.java +++ b/src/main/java/com/minecolonies/core/entity/ai/workers/production/agriculture/EntityAIWorkPlanter.java @@ -14,7 +14,6 @@ import com.minecolonies.api.entity.citizen.VisibleCitizenStatus; import com.minecolonies.api.util.InventoryUtils; import com.minecolonies.api.util.Tuple; -import com.minecolonies.api.util.constant.CitizenConstants; import com.minecolonies.api.util.constant.TypeConstants; import com.minecolonies.api.util.constant.translation.RequestSystemTranslationConstants; import com.minecolonies.core.colony.buildings.modules.FieldsModule; @@ -166,7 +165,7 @@ private IAIState moveToField() return PLANTATION_PICK_FIELD; } - if (walkToBlock(currentPlantationField.getPosition(), CitizenConstants.DEFAULT_RANGE_FOR_DELAY)) + if (!walkToSafePos(currentPlantationField.getPosition())) { return getState(); } @@ -208,7 +207,7 @@ private IAIState decideFieldWork() private IAIState workField() { IPlantationModule planterModule = activeModuleResult.getModule(); - if (!Objects.isNull(activeModuleResult.getActionPosition()) && walkToBlock(planterModule.getPositionToWalkTo(world, activeModuleResult.getActionPosition()))) + if (!Objects.isNull(activeModuleResult.getActionPosition()) && !walkToSafePos(planterModule.getPositionToWalkTo(world, activeModuleResult.getActionPosition()))) { return PLANTATION_WORK_FIELD; } @@ -263,7 +262,7 @@ else if (handlerResult.equals(ActionHandlerResult.NEEDS_ITEM)) */ private IAIState returnToBuilding() { - if (walkToBuilding()) + if (!walkToBuilding()) { return getState(); } diff --git a/src/main/java/com/minecolonies/core/entity/ai/workers/production/herders/AbstractEntityAIHerder.java b/src/main/java/com/minecolonies/core/entity/ai/workers/production/herders/AbstractEntityAIHerder.java index 081e0c32970..ad5785b9975 100755 --- a/src/main/java/com/minecolonies/core/entity/ai/workers/production/herders/AbstractEntityAIHerder.java +++ b/src/main/java/com/minecolonies/core/entity/ai/workers/production/herders/AbstractEntityAIHerder.java @@ -33,8 +33,8 @@ import static com.minecolonies.api.entity.ai.statemachine.states.AIWorkerState.*; import static com.minecolonies.api.util.constant.Constants.TICKS_SECOND; -import static com.minecolonies.api.util.constant.StatisticsConstants.ITEM_USED; import static com.minecolonies.api.util.constant.EquipmentLevelConstants.TOOL_LEVEL_WOOD_OR_GOLD; +import static com.minecolonies.api.util.constant.StatisticsConstants.ITEM_USED; import static com.minecolonies.core.colony.buildings.modules.BuildingModules.STATS_MODULE; /** @@ -280,7 +280,7 @@ protected boolean canBreedChildren() */ private IAIState startWorkingAtOwnBuilding() { - if (walkToBuilding()) + if (!walkToBuilding()) { return getState(); } @@ -567,7 +567,7 @@ private IAIState pickupItems() { final List items = searchForItemsInArea(); - if (!items.isEmpty() && walkToBlock(items.get(0).blockPosition(), 1)) + if (!items.isEmpty() && walkWithProxy(items.get(0).blockPosition(), 1)) { return getState(); } @@ -613,11 +613,11 @@ public boolean walkingToAnimal(final Animal animal) { if (animal != null) { - return walkToBlock(animal.blockPosition()); + return walkToWorkPos(animal.blockPosition()); } else { - return false; + return true; } } @@ -635,7 +635,7 @@ private boolean breedTwoAnimals() { it.remove(); } - else if (walkingToAnimal(animal)) + else if (!walkingToAnimal(animal)) { break; } diff --git a/src/main/java/com/minecolonies/core/entity/ai/workers/production/herders/EntityAIWorkCowboy.java b/src/main/java/com/minecolonies/core/entity/ai/workers/production/herders/EntityAIWorkCowboy.java index 72350cf0f3b..afdeed0b28b 100755 --- a/src/main/java/com/minecolonies/core/entity/ai/workers/production/herders/EntityAIWorkCowboy.java +++ b/src/main/java/com/minecolonies/core/entity/ai/workers/production/herders/EntityAIWorkCowboy.java @@ -146,7 +146,7 @@ private IAIState milkCows() if (!worker.getCitizenInventoryHandler().hasItemInInventory(building.getMilkInputItem().getItem())) { if (InventoryUtils.hasBuildingEnoughElseCount(building, new ItemStorage(building.getMilkInputItem()), 1) > 0 - && !walkToBuilding()) + && walkToBuilding()) { checkAndTransferFromHut(building.getMilkInputItem()); } @@ -196,7 +196,7 @@ private IAIState milkMooshrooms() if (!worker.getCitizenInventoryHandler().hasItemInInventory(Items.BOWL)) { if (InventoryUtils.hasBuildingEnoughElseCount(building, new ItemStorage(new ItemStack(Items.BOWL, 1)), 1) > 0 - && !walkToBuilding()) + && walkToBuilding()) { checkAndTransferFromHut(new ItemStack(Items.BOWL, 1)); } diff --git a/src/main/java/com/minecolonies/core/entity/ai/workers/service/EntityAIWorkCook.java b/src/main/java/com/minecolonies/core/entity/ai/workers/service/EntityAIWorkCook.java index cb3b58424e0..5bae5217766 100755 --- a/src/main/java/com/minecolonies/core/entity/ai/workers/service/EntityAIWorkCook.java +++ b/src/main/java/com/minecolonies/core/entity/ai/workers/service/EntityAIWorkCook.java @@ -172,7 +172,7 @@ private IAIState serveFoodToCitizen() return getState(); } - if (walkToBlock(citizenToServe.peek().blockPosition())) + if (!walkToWorkPos(citizenToServe.peek().blockPosition())) { return getState(); } @@ -257,7 +257,7 @@ private IAIState serveFoodToPlayer() return START_WORKING; } - if (walkToBlock(playerToServe.peek().blockPosition())) + if (!walkToWorkPos(playerToServe.peek().blockPosition())) { return getState(); } diff --git a/src/main/java/com/minecolonies/core/entity/ai/workers/service/EntityAIWorkDeliveryman.java b/src/main/java/com/minecolonies/core/entity/ai/workers/service/EntityAIWorkDeliveryman.java index 7305fba6c78..2c5196edb97 100755 --- a/src/main/java/com/minecolonies/core/entity/ai/workers/service/EntityAIWorkDeliveryman.java +++ b/src/main/java/com/minecolonies/core/entity/ai/workers/service/EntityAIWorkDeliveryman.java @@ -12,10 +12,6 @@ import com.minecolonies.api.entity.ai.statemachine.AITarget; import com.minecolonies.api.entity.ai.statemachine.states.IAIState; import com.minecolonies.api.entity.citizen.VisibleCitizenStatus; -import com.minecolonies.api.tileentities.AbstractTileEntityColonyBuilding; -import com.minecolonies.core.util.citizenutils.CitizenItemUtils; -import com.minecolonies.core.tileentities.TileEntityColonyBuilding; -import com.minecolonies.core.tileentities.TileEntityRack; import com.minecolonies.api.util.InventoryUtils; import com.minecolonies.api.util.ItemStackUtils; import com.minecolonies.api.util.Log; @@ -29,6 +25,9 @@ import com.minecolonies.core.colony.requestsystem.requests.StandardRequests.DeliveryRequest; import com.minecolonies.core.colony.requestsystem.requests.StandardRequests.PickupRequest; import com.minecolonies.core.entity.ai.workers.AbstractEntityAIInteract; +import com.minecolonies.core.tileentities.TileEntityColonyBuilding; +import com.minecolonies.core.tileentities.TileEntityRack; +import com.minecolonies.core.util.citizenutils.CitizenItemUtils; import net.minecraft.core.BlockPos; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; @@ -161,11 +160,6 @@ private IAIState pickup() worker.getCitizenData().setVisibleStatus(DELIVERING); final BlockPos pickupTarget = currentTask.getRequester().getLocation().getInDimensionLocation(); - if (pickupTarget != BlockPos.ZERO && !worker.isWorkerAtSiteWithMove(pickupTarget, MIN_DISTANCE_TO_WAREHOUSE)) - { - return PICKUP; - } - final IBuilding pickupBuilding = building.getColony().getBuildingManager().getBuilding(pickupTarget); if (pickupBuilding == null) { @@ -173,6 +167,11 @@ private IAIState pickup() return START_WORKING; } + if (!walkToBuilding(pickupBuilding)) + { + return PICKUP; + } + if (pickupFromBuilding(pickupBuilding)) { this.alreadyKept = new ArrayList<>(); @@ -298,7 +297,7 @@ private IAIState dump() return START_WORKING; } - if (!worker.isWorkerAtSiteWithMove(warehouse.getPosition(), MIN_DISTANCE_TO_WAREHOUSE)) + if (!walkToBuilding(warehouse)) { setDelay(WALK_DELAY); return DUMPING; @@ -346,22 +345,18 @@ private IAIState deliver() return START_WORKING; } - if (!worker.isWorkerAtSiteWithMove(targetBuildingLocation.getInDimensionLocation(), MIN_DISTANCE_TO_WAREHOUSE)) - { - setDelay(WALK_DELAY); - return DELIVERY; - } - - final BlockEntity tileEntity = world.getBlockEntity(targetBuildingLocation.getInDimensionLocation()); - - if (!(tileEntity instanceof TileEntityColonyBuilding) || ((AbstractTileEntityColonyBuilding) tileEntity).getBuilding() == null) + final IBuilding targetBuilding = worker.getCitizenColonyHandler().getColony().getBuildingManager().getBuilding(targetBuildingLocation.getInDimensionLocation()); + if (targetBuilding == null) { - // TODO: Non-Colony deliveries are unsupported yet. Fix that at some point in time. job.finishRequest(true); return START_WORKING; } - final IBuilding targetBuilding = ((AbstractTileEntityColonyBuilding) tileEntity).getBuilding(); + if (!walkToBuilding(targetBuilding)) + { + setDelay(WALK_DELAY); + return DELIVERY; + } boolean success = true; boolean extracted = false; @@ -523,7 +518,7 @@ private IAIState prepareDelivery() return START_WORKING; } - if (walkToBlock(location.getInDimensionLocation())) + if (!walkToSafePos(location.getInDimensionLocation())) { return PREPARE_DELIVERY; } @@ -595,7 +590,7 @@ private IAIState decide() if (currentTask == null) { // If there are no deliveries/pickups pending, just loiter around the warehouse. - if (!worker.isWorkerAtSiteWithMove(getAndCheckWareHouse().getPosition(), MIN_DISTANCE_TO_WAREHOUSE)) + if (!walkToBuilding(getAndCheckWareHouse())) { setDelay(WALK_DELAY); return START_WORKING; diff --git a/src/main/java/com/minecolonies/core/entity/ai/workers/service/EntityAIWorkEnchanter.java b/src/main/java/com/minecolonies/core/entity/ai/workers/service/EntityAIWorkEnchanter.java index b94e618206c..0e238308866 100755 --- a/src/main/java/com/minecolonies/core/entity/ai/workers/service/EntityAIWorkEnchanter.java +++ b/src/main/java/com/minecolonies/core/entity/ai/workers/service/EntityAIWorkEnchanter.java @@ -2,6 +2,7 @@ import com.minecolonies.api.colony.ICitizenData; import com.minecolonies.api.colony.IColonyManager; +import com.minecolonies.api.colony.buildings.IBuilding; import com.minecolonies.api.colony.interactionhandling.ChatPriority; import com.minecolonies.api.colony.requestsystem.token.IToken; import com.minecolonies.api.crafting.IRecipeStorage; @@ -12,7 +13,7 @@ import com.minecolonies.api.items.ModItems; import com.minecolonies.api.util.*; import com.minecolonies.core.Network; -import com.minecolonies.core.colony.buildings.AbstractBuilding; +import com.minecolonies.core.colony.buildings.modules.BuildingModules; import com.minecolonies.core.colony.buildings.modules.EnchanterStationsModule; import com.minecolonies.core.colony.buildings.workerbuildings.BuildingEnchanter; import com.minecolonies.core.colony.interactionhandling.StandardInteraction; @@ -21,17 +22,17 @@ import com.minecolonies.core.network.messages.client.CircleParticleEffectMessage; import com.minecolonies.core.network.messages.client.StreamParticleEffectMessage; import com.minecolonies.core.util.WorkerUtil; +import net.minecraft.core.BlockPos; +import net.minecraft.core.particles.ParticleTypes; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.network.chat.Component; import net.minecraft.sounds.SoundEvents; -import net.minecraft.world.item.enchantment.EnchantmentHelper; +import net.minecraft.world.InteractionHand; import net.minecraft.world.item.EnchantedBookItem; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Items; -import net.minecraft.nbt.CompoundTag; -import net.minecraft.core.particles.ParticleTypes; -import net.minecraft.world.InteractionHand; -import net.minecraft.core.BlockPos; +import net.minecraft.world.item.enchantment.EnchantmentHelper; import net.minecraft.world.phys.Vec3; -import net.minecraft.network.chat.Component; import org.jetbrains.annotations.NotNull; import java.util.ArrayList; @@ -117,7 +118,7 @@ public EntityAIWorkEnchanter(@NotNull final JobEnchanter job) protected IAIState decide() { worker.setItemInHand(InteractionHand.MAIN_HAND, ItemStack.EMPTY); - if (walkToBuilding()) + if (!walkToBuilding()) { return START_WORKING; } @@ -137,7 +138,7 @@ protected IAIState decide() if (getPrimarySkillLevel() < building.getBuildingLevel() * MANA_REQ_PER_LEVEL) { final BuildingEnchanter enchanterBuilding = building; - final EnchanterStationsModule module = enchanterBuilding.getFirstModuleOccurance(EnchanterStationsModule.class); + final EnchanterStationsModule module = enchanterBuilding.getModule(BuildingModules.ENCHANTER_STATIONS); if (module.getBuildingsToGatherFrom().isEmpty()) { if (worker.getCitizenData() != null) @@ -298,13 +299,12 @@ private IAIState gatherAndDrain() return IDLE; } - if (walkToBlock(job.getPosToDrainFrom())) + final IBuilding buildingWorker = building.getColony().getBuildingManager().getBuilding(job.getPosToDrainFrom()); + if (!walkToBuilding(buildingWorker)) { return getState(); } - final AbstractBuilding buildingWorker = building.getColony().getBuildingManager().getBuilding(job.getPosToDrainFrom(), AbstractBuilding.class); - if (buildingWorker == null) { resetDraining(); diff --git a/src/main/java/com/minecolonies/core/entity/ai/workers/service/EntityAIWorkHealer.java b/src/main/java/com/minecolonies/core/entity/ai/workers/service/EntityAIWorkHealer.java index 0528966f01b..62f7e75575c 100755 --- a/src/main/java/com/minecolonies/core/entity/ai/workers/service/EntityAIWorkHealer.java +++ b/src/main/java/com/minecolonies/core/entity/ai/workers/service/EntityAIWorkHealer.java @@ -10,7 +10,10 @@ import com.minecolonies.api.entity.ai.statemachine.AITarget; import com.minecolonies.api.entity.ai.statemachine.states.IAIState; import com.minecolonies.api.entity.citizen.AbstractEntityCitizen; -import com.minecolonies.api.util.*; +import com.minecolonies.api.util.BlockPosUtil; +import com.minecolonies.api.util.InventoryUtils; +import com.minecolonies.api.util.Tuple; +import com.minecolonies.api.util.WorldUtil; import com.minecolonies.core.Network; import com.minecolonies.core.colony.buildings.workerbuildings.BuildingHospital; import com.minecolonies.core.colony.interactionhandling.StandardInteraction; @@ -21,9 +24,9 @@ import com.minecolonies.core.entity.citizen.EntityCitizen; import com.minecolonies.core.network.messages.client.CircleParticleEffectMessage; import com.minecolonies.core.network.messages.client.StreamParticleEffectMessage; -import net.minecraft.world.entity.player.Player; import net.minecraft.core.particles.ParticleTypes; import net.minecraft.network.chat.Component; +import net.minecraft.world.entity.player.Player; import net.minecraftforge.common.capabilities.ForgeCapabilities; import net.minecraftforge.items.IItemHandler; import org.jetbrains.annotations.NotNull; @@ -100,7 +103,7 @@ public EntityAIWorkHealer(@NotNull final JobHealer job) */ private IAIState decide() { - if (walkToBuilding()) + if (!walkToBuilding()) { return DECIDE; } @@ -252,7 +255,7 @@ private IAIState requestCure() } final EntityCitizen citizen = (EntityCitizen) data.getEntity().get(); - if (walkToBlock(citizen.blockPosition())) + if (!walkToSafePos(citizen.blockPosition())) { return REQUEST_CURE; } @@ -322,7 +325,7 @@ private IAIState cure() } final EntityCitizen citizen = (EntityCitizen) data.getEntity().get(); - if (walkToBlock(data.getEntity().get().blockPosition())) + if (!walkToSafePos(data.getEntity().get().blockPosition())) { return CURE; } @@ -400,7 +403,7 @@ private IAIState freeCure() } final EntityCitizen citizen = (EntityCitizen) data.getEntity().get(); - if (walkToBlock(citizen.blockPosition())) + if (!walkToSafePos(citizen.blockPosition())) { progressTicks = 0; return FREE_CURE; @@ -446,7 +449,7 @@ private IAIState curePlayer() return DECIDE; } - if (walkToBlock(playerToHeal.blockPosition())) + if (!walkToUnSafePos(playerToHeal.blockPosition())) { return getState(); } @@ -476,7 +479,7 @@ private IAIState wander() } final EntityCitizen citizen = (EntityCitizen) remotePatient.getEntity().get(); - if (walkToBlock(remotePatient.getEntity().get().blockPosition())) + if (!walkToUnSafePos(remotePatient.getEntity().get().blockPosition())) { return getState(); } diff --git a/src/main/java/com/minecolonies/core/entity/ai/workers/service/EntityAIWorkUndertaker.java b/src/main/java/com/minecolonies/core/entity/ai/workers/service/EntityAIWorkUndertaker.java index 0fa07fab0fc..9a3b4325d59 100644 --- a/src/main/java/com/minecolonies/core/entity/ai/workers/service/EntityAIWorkUndertaker.java +++ b/src/main/java/com/minecolonies/core/entity/ai/workers/service/EntityAIWorkUndertaker.java @@ -9,8 +9,6 @@ import com.minecolonies.api.entity.citizen.Skill; import com.minecolonies.api.entity.citizen.VisibleCitizenStatus; import com.minecolonies.api.equipment.ModEquipmentTypes; -import com.minecolonies.core.util.citizenutils.CitizenItemUtils; -import com.minecolonies.core.tileentities.TileEntityGrave; import com.minecolonies.api.util.InventoryUtils; import com.minecolonies.api.util.MessageUtils; import com.minecolonies.api.util.Tuple; @@ -19,8 +17,11 @@ import com.minecolonies.core.colony.buildings.workerbuildings.BuildingGraveyard; import com.minecolonies.core.colony.jobs.JobUndertaker; import com.minecolonies.core.entity.ai.workers.AbstractEntityAIInteract; +import com.minecolonies.core.entity.pathfinding.navigation.EntityNavigationUtils; import com.minecolonies.core.network.messages.client.VanillaParticleMessage; +import com.minecolonies.core.tileentities.TileEntityGrave; import com.minecolonies.core.util.AdvancementUtils; +import com.minecolonies.core.util.citizenutils.CitizenItemUtils; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.core.particles.ParticleTypes; @@ -115,7 +116,7 @@ private IAIState startWorking() @Nullable final BlockPos currentGrave = building.getGraveToWorkOn(); if (currentGrave != null) { - if (walkToBuilding()) + if (!walkToBuilding()) { return getState(); } @@ -143,7 +144,7 @@ private IAIState wander() { if (building.isInBuilding(worker.blockPosition())) { - worker.getNavigation().moveToRandomPos(10, DEFAULT_SPEED, building.getCorners()); + EntityNavigationUtils.walkToRandomPosWithin(worker, 10, DEFAULT_SPEED, building.getCorners()); } else { @@ -176,7 +177,7 @@ private IAIState emptyGrave() @Nullable final BlockPos gravePos = buildingGraveyard.getGraveToWorkOn(); // Still moving to the block - if (walkToBlock(gravePos, 3)) + if (walkWithProxy(gravePos, 3)) { return getState(); } @@ -237,7 +238,7 @@ private IAIState digGrave() } // Still moving to the block - if (walkToBlock(gravePos, 3)) + if (walkWithProxy(gravePos, 3)) { return getState(); } @@ -311,7 +312,7 @@ private IAIState tryResurrect() } // Still moving to the block - if (walkToBlock(gravePos, 3)) + if (walkWithProxy(gravePos, 3)) { return getState(); } @@ -444,7 +445,7 @@ private IAIState buryCitizen() return IDLE; } - if (walkToBlock(burialPos.getA(), 4)) + if (walkWithProxy(burialPos.getA(), 4)) { return getState(); } diff --git a/src/main/java/com/minecolonies/core/entity/ai/workers/util/BuildingStructureHandler.java b/src/main/java/com/minecolonies/core/entity/ai/workers/util/BuildingStructureHandler.java index 553c73d76f2..8e30e9576ee 100755 --- a/src/main/java/com/minecolonies/core/entity/ai/workers/util/BuildingStructureHandler.java +++ b/src/main/java/com/minecolonies/core/entity/ai/workers/util/BuildingStructureHandler.java @@ -15,6 +15,7 @@ import com.minecolonies.core.colony.buildings.AbstractBuildingStructureBuilder; import com.minecolonies.core.colony.jobs.AbstractJobStructure; import com.minecolonies.core.entity.ai.workers.AbstractEntityAIStructure; +import com.minecolonies.core.entity.pathfinding.navigation.EntityNavigationUtils; import com.minecolonies.core.util.WorkerUtil; import net.minecraft.core.BlockPos; import net.minecraft.tags.ItemTags; @@ -35,7 +36,6 @@ import java.util.concurrent.Future; import java.util.function.Function; -import static com.minecolonies.api.util.constant.CitizenConstants.RUN_AWAY_SPEED; import static com.minecolonies.api.util.constant.StatisticsConstants.BLOCKS_PLACED; /** @@ -182,7 +182,7 @@ public void prePlacementLogic(final BlockPos worldPos, final BlockState blockSta && Mth.floor(structureAI.getWorker().getZ()) == worldPos.getZ() && structureAI.getWorker().getNavigation().isDone()) { - structureAI.getWorker().getNavigation().moveAwayFromXYZ(worldPos, RUN_AWAY_SPEED, 1, true); + EntityNavigationUtils.walkAwayFrom(structureAI.getWorker(), worldPos, 1, 1.0); } structureAI.getWorker().swing(InteractionHand.MAIN_HAND); diff --git a/src/main/java/com/minecolonies/core/entity/citizen/EntityCitizen.java b/src/main/java/com/minecolonies/core/entity/citizen/EntityCitizen.java index ccc43ca34b5..d5c73d58186 100755 --- a/src/main/java/com/minecolonies/core/entity/citizen/EntityCitizen.java +++ b/src/main/java/com/minecolonies/core/entity/citizen/EntityCitizen.java @@ -27,7 +27,6 @@ import com.minecolonies.api.entity.citizen.citizenhandlers.*; import com.minecolonies.api.entity.citizen.happiness.ExpirationBasedHappinessModifier; import com.minecolonies.api.entity.citizen.happiness.StaticHappinessSupplier; -import com.minecolonies.api.entity.pathfinding.proxy.IWalkToProxy; import com.minecolonies.api.eventbus.events.colony.citizens.CitizenDiedModEvent; import com.minecolonies.api.eventbus.events.colony.citizens.CitizenRemovedModEvent; import com.minecolonies.api.inventory.InventoryCitizen; @@ -57,9 +56,8 @@ import com.minecolonies.core.entity.ai.workers.CitizenAI; import com.minecolonies.core.entity.ai.workers.guard.AbstractEntityAIGuard; import com.minecolonies.core.entity.citizen.citizenhandlers.*; +import com.minecolonies.core.entity.pathfinding.navigation.EntityNavigationUtils; import com.minecolonies.core.entity.pathfinding.navigation.MovementHandler; -import com.minecolonies.core.entity.pathfinding.pathresults.PathResult; -import com.minecolonies.core.entity.pathfinding.proxy.EntityCitizenWalkToProxy; import com.minecolonies.core.event.EventHandler; import com.minecolonies.core.event.TextureReloadListener; import com.minecolonies.core.network.messages.client.ItemParticleEffectMessage; @@ -67,8 +65,8 @@ import com.minecolonies.core.network.messages.client.colony.ColonyViewCitizenViewMessage; import com.minecolonies.core.network.messages.client.colony.PlaySoundForCitizenMessage; import com.minecolonies.core.network.messages.server.colony.OpenInventoryMessage; -import com.minecolonies.core.util.citizenutils.CitizenItemUtils; import com.minecolonies.core.util.TeleportHelper; +import com.minecolonies.core.util.citizenutils.CitizenItemUtils; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.core.particles.ParticleTypes; @@ -112,9 +110,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.time.Clock; -import java.time.LocalDate; -import java.time.Month; import java.util.*; import static com.minecolonies.api.research.util.ResearchConstants.*; @@ -153,10 +148,7 @@ public class EntityCitizen extends AbstractEntityCitizen implements IThreatTable * It's citizen Id. */ private int citizenId = 0; - /** - * The Walk to proxy (Shortest path through intermediate blocks). - */ - private IWalkToProxy proxy; + /** * Reference to the data representation inside the colony. */ @@ -191,11 +183,6 @@ public class EntityCitizen extends AbstractEntityCitizen implements IThreatTable */ private final CitizenCombatTracker combatTracker; - /** - * The path-result of trying to move away - */ - private PathResult moveAwayPath; - /** * IsChild flag */ @@ -543,7 +530,7 @@ private InteractionResult directPlayerInteraction(final Player player, final Int if (!level.isClientSide()) { MessageUtils.format(MESSAGE_INTERACTION_OUCH, getCitizenData().getName()).sendTo(player); - getNavigation().moveAwayFromLivingEntity(player, 5, 1); + EntityNavigationUtils.walkAwayFrom(this, player.blockPosition(), 5, 1); setJumping(true); } @@ -962,23 +949,6 @@ public ILocation getLocation() return location; } - /** - * Checks if a worker is at his working site. If he isn't, sets it's path to the location - * - * @param site the place where he should walk to - * @param range Range to check in - * @return True if worker is at site, otherwise false. - */ - @Override - public boolean isWorkerAtSiteWithMove(@NotNull final BlockPos site, final int range) - { - if (proxy == null) - { - proxy = new EntityCitizenWalkToProxy(this); - } - return proxy.walkToBlock(site, range, true); - } - /** * Getter for the citizendata. Tries to get it from the colony is the data is null. * @@ -1085,17 +1055,6 @@ public void playMoveAwaySound() } } - /** - * Get the path proxy of the citizen. - * - * @return the proxy. - */ - @Override - public IWalkToProxy getProxy() - { - return proxy; - } - /** * Decrease the saturation of the citizen for 1 action. */ @@ -1459,10 +1418,12 @@ private void performMoveAway(@Nullable final Entity attacker) if (!(attacker instanceof LivingEntity) && (!(getCitizenJobHandler().getColonyJob() instanceof AbstractJobGuard) || getCitizenJobHandler().getColonyJob().canAIBeInterrupted())) { - if (moveAwayPath == null || !moveAwayPath.isInProgress()) - { - moveAwayPath = this.getNavigation().moveAwayFromLivingEntity(this, 5, INITIAL_RUN_SPEED_AVOID); - } + EntityNavigationUtils.walkAwayFrom(this, blockPosition(), 5, INITIAL_RUN_SPEED_AVOID); + return; + } + + if (attacker == null) + { return; } @@ -1475,10 +1436,7 @@ private void performMoveAway(@Nullable final Entity attacker) citizenAI.addTransition(new AIOneTimeEventTarget<>(CitizenAIState.FLEE)); callForHelp(attacker, MAX_GUARD_CALL_RANGE); - if (moveAwayPath == null || !moveAwayPath.isInProgress()) - { - moveAwayPath = this.getNavigation().moveAwayFromLivingEntity(attacker, 15, INITIAL_RUN_SPEED_AVOID); - } + EntityNavigationUtils.walkAwayFrom(this, attacker.blockPosition(), 15, INITIAL_RUN_SPEED_AVOID); } @Override diff --git a/src/main/java/com/minecolonies/core/entity/mobs/EntityMercenary.java b/src/main/java/com/minecolonies/core/entity/mobs/EntityMercenary.java index 686e5a1cefe..58fe5f90553 100755 --- a/src/main/java/com/minecolonies/core/entity/mobs/EntityMercenary.java +++ b/src/main/java/com/minecolonies/core/entity/mobs/EntityMercenary.java @@ -18,6 +18,7 @@ import com.minecolonies.core.entity.ai.minimal.EntityAIInteractToggleAble; import com.minecolonies.core.entity.citizen.EntityCitizen; import com.minecolonies.core.entity.pathfinding.navigation.AbstractAdvancedPathNavigate; +import com.minecolonies.core.entity.pathfinding.navigation.EntityNavigationUtils; import com.minecolonies.core.entity.pathfinding.navigation.MinecoloniesAdvancedPathNavigate; import com.minecolonies.core.entity.pathfinding.proxy.GeneralEntityWalkToProxy; import net.minecraft.core.BlockPos; @@ -261,11 +262,11 @@ private boolean spawnEvent() playSound(MercenarySounds.mercenaryCelebrate, 2.0f, 1.0f); if (blockPosition().equals(first)) { - getNavigation().tryMoveToBlockPos(last, 0.5); + EntityNavigationUtils.walkToPos(this, last, 2, true, 0.5); } else { - getNavigation().tryMoveToBlockPos(first, 0.5); + EntityNavigationUtils.walkToPos(this, first, 2, true, 0.5); } return false; diff --git a/src/main/java/com/minecolonies/core/entity/mobs/EntityMercenaryAI.java b/src/main/java/com/minecolonies/core/entity/mobs/EntityMercenaryAI.java index 80ad81e5c3d..e47015ea507 100755 --- a/src/main/java/com/minecolonies/core/entity/mobs/EntityMercenaryAI.java +++ b/src/main/java/com/minecolonies/core/entity/mobs/EntityMercenaryAI.java @@ -5,13 +5,14 @@ import com.minecolonies.api.entity.ai.statemachine.tickratestatemachine.ITickRateStateMachine; import com.minecolonies.api.entity.ai.statemachine.tickratestatemachine.TickRateStateMachine; import com.minecolonies.api.entity.ai.statemachine.tickratestatemachine.TickingTransition; -import com.minecolonies.core.entity.pathfinding.pathresults.PathResult; import com.minecolonies.api.sounds.MercenarySounds; import com.minecolonies.api.util.*; +import com.minecolonies.core.entity.pathfinding.navigation.EntityNavigationUtils; +import com.minecolonies.core.entity.pathfinding.pathresults.PathResult; +import net.minecraft.core.BlockPos; +import net.minecraft.world.InteractionHand; import net.minecraft.world.entity.ai.goal.Goal; import net.minecraft.world.item.ItemStack; -import net.minecraft.world.InteractionHand; -import net.minecraft.core.BlockPos; import net.minecraftforge.items.IItemHandler; import java.util.ArrayList; @@ -226,8 +227,8 @@ private boolean fighting() if (attackPath == null || !attackPath.isInProgress()) { - entity.getNavigation().moveToLivingEntity(entity.getTarget(), 1); - entity.getLookControl().setLookAt(entity.getTarget(), 180f, 180f); + EntityNavigationUtils.walkToPos(entity, entity.getTarget().blockPosition(), false); + entity.getLookControl().setLookAt(entity.getTarget()); } final int distance = BlockPosUtil.getMaxDistance2D(entity.blockPosition(), entity.getTarget().blockPosition()); diff --git a/src/main/java/com/minecolonies/core/entity/mobs/aitasks/CampWalkAI.java b/src/main/java/com/minecolonies/core/entity/mobs/aitasks/CampWalkAI.java index 4ae3f64a6ee..b65682b0f4b 100644 --- a/src/main/java/com/minecolonies/core/entity/mobs/aitasks/CampWalkAI.java +++ b/src/main/java/com/minecolonies/core/entity/mobs/aitasks/CampWalkAI.java @@ -7,6 +7,7 @@ import com.minecolonies.api.entity.ai.statemachine.tickratestatemachine.TickingTransition; import com.minecolonies.api.entity.mobs.AbstractEntityMinecoloniesMonster; import com.minecolonies.api.entity.pathfinding.IPathJob; +import com.minecolonies.core.entity.pathfinding.navigation.EntityNavigationUtils; import com.minecolonies.core.entity.pathfinding.pathresults.PathResult; import net.minecraft.core.BlockPos; import net.minecraft.util.Tuple; @@ -23,16 +24,6 @@ public class CampWalkAI implements IStateAI */ private final AbstractEntityMinecoloniesMonster entity; - /** - * Target block we're walking to - */ - private BlockPos targetBlock = null; - - /** - * Walk timer - */ - private long walkTimer = 0; - /** * Random path result. */ @@ -46,57 +37,21 @@ public class CampWalkAI implements IStateAI public CampWalkAI(final AbstractEntityMinecoloniesMonster raider, final ITickRateStateMachine stateMachine) { this.entity = raider; - stateMachine.addTransition(new TickingTransition<>(CombatAIStates.NO_TARGET, this::walk, () -> null, 80)); + stateMachine.addTransition(new TickingTransition<>(CombatAIStates.NO_TARGET, this::walk, () -> null, TICKS_SECOND * 30)); } /** - * Walk raider towards the colony or campfires - * + * Walk camp mob randomly */ private boolean walk() { - if (targetBlock == null || entity.level.getGameTime() > walkTimer) + if (spawnCenterBoxCache == null) { - targetBlock = findRandomPositionToWalkTo(); - walkTimer = entity.level.getGameTime() + TICKS_SECOND * 30; - } - else - { - entity.getNavigation().moveToXYZ(targetBlock.getX(), targetBlock.getY(), targetBlock.getZ(),1.1); - randomPathResult = null; + final BlockPos startPos = entity.getSpawnPos() == null ? entity.blockPosition() : entity.getSpawnPos(); + spawnCenterBoxCache = new Tuple<>(startPos.offset(-10, -5, -10), startPos.offset(10, 5, 10)); } + EntityNavigationUtils.walkToRandomPosWithin(entity, 10, 0.6, spawnCenterBoxCache); return false; } - - protected BlockPos findRandomPositionToWalkTo() - { - if (randomPathResult == null || randomPathResult.failedToReachDestination()) - { - if (spawnCenterBoxCache == null) - { - final BlockPos startPos = entity.getSpawnPos() == null ? entity.blockPosition() : entity.getSpawnPos(); - spawnCenterBoxCache = new Tuple<>(startPos.offset(-10,-5,-10), startPos.offset(10,5,10)); - } - - randomPathResult = entity.getNavigation().moveToRandomPos(10, 0.9, spawnCenterBoxCache); - if (randomPathResult != null) - { - randomPathResult.getJob().getPathingOptions().withCanEnterDoors(true).withToggleCost(0).withNonLadderClimbableCost(0); - } - } - - if (randomPathResult.isPathReachingDestination()) - { - return randomPathResult.getPath().getEndNode().asBlockPos(); - } - - if (randomPathResult.isCancelled()) - { - randomPathResult = null; - return null; - } - - return null; - } } diff --git a/src/main/java/com/minecolonies/core/entity/mobs/aitasks/RaiderMeleeAI.java b/src/main/java/com/minecolonies/core/entity/mobs/aitasks/RaiderMeleeAI.java index 461f34d2214..994895f8dc2 100644 --- a/src/main/java/com/minecolonies/core/entity/mobs/aitasks/RaiderMeleeAI.java +++ b/src/main/java/com/minecolonies/core/entity/mobs/aitasks/RaiderMeleeAI.java @@ -4,11 +4,11 @@ import com.minecolonies.api.entity.ai.statemachine.states.IState; import com.minecolonies.api.entity.ai.statemachine.tickratestatemachine.ITickRateStateMachine; import com.minecolonies.api.entity.mobs.AbstractEntityMinecoloniesMonster; -import com.minecolonies.api.entity.mobs.AbstractEntityMinecoloniesRaider; import com.minecolonies.api.util.SoundUtils; import com.minecolonies.api.util.constant.Constants; import com.minecolonies.core.entity.ai.combat.AttackMoveAI; import com.minecolonies.core.entity.citizen.EntityCitizen; +import com.minecolonies.core.entity.pathfinding.navigation.EntityNavigationUtils; import com.minecolonies.core.entity.pathfinding.pathresults.PathResult; import net.minecraft.core.registries.Registries; import net.minecraft.network.chat.contents.TranslatableContents; @@ -84,8 +84,12 @@ protected int getAttackDelay() @Override protected PathResult moveInAttackPosition(final LivingEntity target) { - return user.getNavigation() - .moveToXYZ(target.getX(), target.getY(), target.getZ(), user.getDifficulty() < ADD_SPEED_DIFFICULTY ? BASE_COMBAT_SPEED : BASE_COMBAT_SPEED * BONUS_SPEED); + EntityNavigationUtils.walkToPos(user, + target.blockPosition(), + (int) getAttackDistance(), + false, + user.getDifficulty() < ADD_SPEED_DIFFICULTY ? BASE_COMBAT_SPEED : BASE_COMBAT_SPEED * BONUS_SPEED); + return user.getNavigation().getPathResult(); } @Override diff --git a/src/main/java/com/minecolonies/core/entity/mobs/aitasks/RaiderRangedAI.java b/src/main/java/com/minecolonies/core/entity/mobs/aitasks/RaiderRangedAI.java index 39ecdff9b83..3d3e144764b 100644 --- a/src/main/java/com/minecolonies/core/entity/mobs/aitasks/RaiderRangedAI.java +++ b/src/main/java/com/minecolonies/core/entity/mobs/aitasks/RaiderRangedAI.java @@ -4,7 +4,6 @@ import com.minecolonies.api.entity.ai.statemachine.states.IState; import com.minecolonies.api.entity.ai.statemachine.tickratestatemachine.ITickRateStateMachine; import com.minecolonies.api.entity.mobs.AbstractEntityMinecoloniesMonster; -import com.minecolonies.api.entity.mobs.AbstractEntityMinecoloniesRaider; import com.minecolonies.api.entity.mobs.ICustomAttackSound; import com.minecolonies.api.entity.mobs.IRangedMobEntity; import com.minecolonies.api.util.EntityUtils; @@ -12,6 +11,7 @@ import com.minecolonies.core.entity.ai.combat.CombatUtils; import com.minecolonies.core.entity.citizen.EntityCitizen; import com.minecolonies.core.entity.other.CustomArrowEntity; +import com.minecolonies.core.entity.pathfinding.navigation.EntityNavigationUtils; import com.minecolonies.core.entity.pathfinding.pathresults.PathResult; import net.minecraft.sounds.SoundEvent; import net.minecraft.sounds.SoundEvents; @@ -185,7 +185,8 @@ protected boolean checkForTarget() @Override protected PathResult moveInAttackPosition(final LivingEntity target) { - return user.getNavigation().moveToXYZ(target.getX(), target.getY(), target.getZ(), COMBAT_MOVEMENT_SPEED); + EntityNavigationUtils.walkToPos(user, target.blockPosition(), (int) getAttackDistance(), false, COMBAT_MOVEMENT_SPEED); + return user.getNavigation().getPathResult(); } @Override diff --git a/src/main/java/com/minecolonies/core/entity/mobs/aitasks/RaiderWalkAI.java b/src/main/java/com/minecolonies/core/entity/mobs/aitasks/RaiderWalkAI.java index a1a0c52f9d7..abef1781cbc 100644 --- a/src/main/java/com/minecolonies/core/entity/mobs/aitasks/RaiderWalkAI.java +++ b/src/main/java/com/minecolonies/core/entity/mobs/aitasks/RaiderWalkAI.java @@ -1,5 +1,6 @@ package com.minecolonies.core.entity.mobs.aitasks; +import com.minecolonies.api.colony.buildings.IBuilding; import com.minecolonies.api.colony.colonyEvents.EventStatus; import com.minecolonies.api.colony.colonyEvents.IColonyEvent; import com.minecolonies.api.colony.colonyEvents.IColonyRaidEvent; @@ -9,13 +10,10 @@ import com.minecolonies.api.entity.ai.statemachine.tickratestatemachine.ITickRateStateMachine; import com.minecolonies.api.entity.ai.statemachine.tickratestatemachine.TickingTransition; import com.minecolonies.api.entity.mobs.AbstractEntityMinecoloniesRaider; -import com.minecolonies.api.entity.pathfinding.IPathJob; -import com.minecolonies.api.util.BlockPosUtil; import com.minecolonies.api.util.Log; -import com.minecolonies.core.colony.buildings.AbstractBuilding; import com.minecolonies.core.colony.events.raid.HordeRaidEvent; import com.minecolonies.core.colony.events.raid.pirateEvent.ShipBasedRaiderUtils; -import com.minecolonies.core.entity.pathfinding.pathresults.PathResult; +import com.minecolonies.core.entity.pathfinding.navigation.EntityNavigationUtils; import net.minecraft.core.BlockPos; import java.util.List; @@ -43,14 +41,9 @@ public class RaiderWalkAI implements IStateAI private long walkTimer = 0; /** - * Random path result. + * Building the raider is checking out */ - private PathResult randomPathResult; - - /** - * If we are currently trying to move to a random block. - */ - private boolean walkInBuildingState = false; + private IBuilding walkInBuilding = null; public RaiderWalkAI(final AbstractEntityMinecoloniesRaider raider, final ITickRateStateMachine stateMachine) { @@ -87,43 +80,27 @@ private boolean walk() final List wayPoints = ((IColonyRaidEvent) event).getWayPoints(); final BlockPos moveToPos = ShipBasedRaiderUtils.chooseWaypointFor(wayPoints, raider.blockPosition(), targetBlock); - raider.getNavigation().moveToXYZ(moveToPos.getX(), moveToPos.getY(), moveToPos.getZ(), !moveToPos.equals(targetBlock) && moveToPos.distManhattan(wayPoints.get(0)) > 50 ? 1.8 : 1.1); - walkInBuildingState = false; - randomPathResult = null; + EntityNavigationUtils.walkToPos(raider, moveToPos, 4, false, !moveToPos.equals(targetBlock) && moveToPos.distManhattan(wayPoints.get(0)) > 50 ? 1.8 : 1.1); + walkInBuilding = null; } - else if (walkInBuildingState) + else if (walkInBuilding != null) { - final BlockPos moveToPos = findRandomPositionToWalkTo(); - if (moveToPos != null) + if (EntityNavigationUtils.walkToRandomPosWithin(raider, 10, 0.7, walkInBuilding.getCorners()) + && raider.getRandom().nextDouble() < 0.25) { - if (moveToPos == BlockPos.ZERO) - { - walkInBuildingState = false; - targetBlock = null; - return false; - } - raider.getNavigation().moveToXYZ(moveToPos.getX(), moveToPos.getY(), moveToPos.getZ(), 0.9); - if (raider.blockPosition().distSqr(moveToPos) < 4) - { - if (raider.getRandom().nextDouble() < 0.25) - { - walkInBuildingState = false; - targetBlock = null; - } - else - { - randomPathResult = null; - walkTimer = raider.level.getGameTime() + TICKS_SECOND * 60; - findRandomPositionToWalkTo(); - } - } + walkInBuilding = null; + targetBlock = null; + } + + if (raider.getNavigation().getPathResult() != null) + { + raider.getNavigation().getPathResult().getJob().getPathingOptions().withCanEnterDoors(true).withToggleCost(0).withNonLadderClimbableCost(0); } } else if (raider.blockPosition().distSqr(targetBlock) < 25) { - findRandomPositionToWalkTo(); walkTimer = raider.level.getGameTime() + TICKS_SECOND * 30; - walkInBuildingState = true; + walkInBuilding = raider.getColony().getBuildingManager().getBuilding(targetBlock); } else if (raider.getNavigation().isDone() || raider.getNavigation().getDesiredPos() == null) { @@ -139,54 +116,13 @@ else if (raider.getNavigation().isDone() || raider.getNavigation().getDesiredPos } } - raider.getNavigation() - .moveToXYZ(moveToPos.getX(), moveToPos.getY(), moveToPos.getZ(), !moveToPos.equals(targetBlock) && moveToPos.distManhattan(wayPoints.get(0)) > 50 ? 1.8 : 1.1); + EntityNavigationUtils.walkToPos(raider, moveToPos, 7, true, !moveToPos.equals(targetBlock) && moveToPos.distManhattan(wayPoints.get(0)) > 50 ? 1.8 : 1.1); } } return false; } - protected BlockPos findRandomPositionToWalkTo() - { - if (randomPathResult == null || randomPathResult.failedToReachDestination()) - { - if (raider.getColony().getBuildingManager().getBuilding(targetBlock) instanceof AbstractBuilding building - && building.getBuildingLevel() > 0 - && !building.getCorners().getA().equals(building.getCorners().getB())) - { - randomPathResult = raider.getNavigation().moveToRandomPos(10, 0.9, building.getCorners()); - if (randomPathResult != null) - { - randomPathResult.getJob().getPathingOptions().withCanEnterDoors(true).withToggleCost(0).withNonLadderClimbableCost(0); - } - } - else - { - return BlockPos.ZERO; - } - } - - if (randomPathResult == null) - { - return null; - } - - - if (randomPathResult.isPathReachingDestination()) - { - return randomPathResult.getPath().getEndNode().asBlockPos(); - } - - if (randomPathResult.isCancelled()) - { - randomPathResult = null; - return null; - } - - return null; - } - /** * Chooses and walks to a random campfire */ @@ -205,15 +141,7 @@ private void walkToCampFire() } walkTimer = raider.level.getGameTime() + raider.level.random.nextInt(1000); - final BlockPos posAroundCampfire = BlockPosUtil.getRandomPosition(raider.level, - campFire, - BlockPos.ZERO, - 3, - 6); - if (posAroundCampfire != null && posAroundCampfire != BlockPos.ZERO) - { - raider.getNavigation().moveToXYZ(posAroundCampfire.getX(), posAroundCampfire.getY(), posAroundCampfire.getZ(), 1.0); - } + EntityNavigationUtils.walkToRandomPosAround(raider, campFire, 10, 0.7); } } diff --git a/src/main/java/com/minecolonies/core/entity/pathfinding/navigation/AbstractAdvancedPathNavigate.java b/src/main/java/com/minecolonies/core/entity/pathfinding/navigation/AbstractAdvancedPathNavigate.java index 4741a7fb26f..8b2a8688af2 100644 --- a/src/main/java/com/minecolonies/core/entity/pathfinding/navigation/AbstractAdvancedPathNavigate.java +++ b/src/main/java/com/minecolonies/core/entity/pathfinding/navigation/AbstractAdvancedPathNavigate.java @@ -6,6 +6,7 @@ import com.minecolonies.api.entity.pathfinding.IStuckHandler; import com.minecolonies.core.entity.pathfinding.PathingOptions; import com.minecolonies.core.entity.pathfinding.pathjobs.AbstractPathJob; +import com.minecolonies.core.entity.pathfinding.pathjobs.PathJobMoveCloseToXNearY; import com.minecolonies.core.entity.pathfinding.pathresults.PathResult; import com.minecolonies.core.entity.pathfinding.pathresults.TreePathResult; import net.minecraft.core.BlockPos; @@ -61,7 +62,7 @@ public BlockPos getDestination() * @param safeDestination if the destination is save and should be set. * @return the result of the pathing. */ - public abstract PathResult moveAwayFromXYZ(final BlockPos currentPosition, final double range, final double speed, final boolean safeDestination); + protected abstract PathResult walkAwayFrom(final BlockPos currentPosition, final double range, final double speed, final boolean safeDestination); /** * Try to move to a certain position. @@ -72,7 +73,7 @@ public BlockPos getDestination() * @param speed the speed to walk. * @return the PathResult. */ - public abstract PathResult moveToXYZ(final double x, final double y, final double z, final double speed); + protected abstract PathResult walkTo(final BlockPos pos, final double speed, final boolean safeDestination); /** * Used to path away from a ourEntity. @@ -82,7 +83,7 @@ public BlockPos getDestination() * @param combatMovementSpeed the speed to run at. * @return the result of the pathing. */ - public abstract PathResult moveAwayFromLivingEntity(final Entity target, final double distance, final double combatMovementSpeed); + protected abstract PathResult moveAwayFromLivingEntity(final Entity target, final double distance, final double combatMovementSpeed); /** * Attempt to move to a specific pos. @@ -91,7 +92,7 @@ public BlockPos getDestination() * @param speed the speed. * @return true if successful. */ - public abstract boolean tryMoveToBlockPos(final BlockPos position, final double speed); + protected abstract boolean walkTo(final BlockPos position, final double speed); /** * Attemps to move in the given direction, walking at least range blocks @@ -102,7 +103,7 @@ public BlockPos getDestination() * @return */ @Nullable - public abstract PathResult moveTowards(BlockPos towards, double range, double speedFactor); + protected abstract PathResult walkTowards(BlockPos towards, double range, double speedFactor); /** * Used to path towards a random pos. @@ -111,7 +112,7 @@ public BlockPos getDestination() * @param speed the speed to run at. * @return the result of the pathing. */ - public abstract PathResult moveToRandomPos(final double range, final double speed); + protected abstract PathResult walkToRandomPos(final int range, final double speed); /** * Used to path towards a random pos. @@ -121,7 +122,24 @@ public BlockPos getDestination() * @param pos the pos to circle around. * @return the result of the pathing. */ - public abstract PathResult moveToRandomPosAroundX(final int range, final double speed, final BlockPos pos); + protected abstract PathResult walkToRandomPosAround(final int range, final double speed, final BlockPos pos); + + /** + * Walks towards the desired position, while trying to not steer too far from the nearby position + * + * @param desiredPosition + * @param nearbyPosition + * @param distToDesired + * @param speedFactor + * @param safeDestination + * @return + */ + protected abstract PathResult walkCloseToXNearY( + BlockPos desiredPosition, + BlockPos nearbyPosition, + int distToDesired, + double speedFactor, + boolean safeDestination); /** * Used to path towards a random pos within some restrictions @@ -131,7 +149,7 @@ public BlockPos getDestination() * @param corners the corners they can't leave. * @return the result of the pathing. */ - public abstract PathResult moveToRandomPos( + protected abstract PathResult walkToRandomPos( final int range, final double speed, final net.minecraft.util.Tuple corners); @@ -145,7 +163,7 @@ public abstract PathResult moveToRandomPos( * @param excludedTrees the trees which should be cut. * @return the result of the search. */ - public abstract TreePathResult moveToTree( + public abstract TreePathResult walkToTree( final BlockPos startRestriction, final BlockPos endRestriction, final double speed, @@ -161,16 +179,16 @@ public abstract TreePathResult moveToTree( * @param excludedTrees the trees which should be cut. * @return the result of the search. */ - public abstract TreePathResult moveToTree(final int range, final double speed, final List excludedTrees, final int dyntreesize, final IColony colony); + public abstract TreePathResult walkToTree(final int range, final double speed, final List excludedTrees, final int dyntreesize, final IColony colony); /** - * Used to move a living ourEntity with a speed. + * Used to walk a living ourEntity with a speed. * * @param e the ourEntity. * @param speed the speed. * @return the result. */ - public abstract PathResult moveToLivingEntity(@NotNull final Entity e, final double speed); + protected abstract PathResult walkToEntity(@NotNull final Entity e, final double speed); /** * Get the pathing options diff --git a/src/main/java/com/minecolonies/core/entity/pathfinding/navigation/EntityNavigationUtils.java b/src/main/java/com/minecolonies/core/entity/pathfinding/navigation/EntityNavigationUtils.java new file mode 100644 index 00000000000..c4f3c5be86e --- /dev/null +++ b/src/main/java/com/minecolonies/core/entity/pathfinding/navigation/EntityNavigationUtils.java @@ -0,0 +1,289 @@ +package com.minecolonies.core.entity.pathfinding.navigation; + +import com.minecolonies.api.colony.buildings.IBuilding; +import com.minecolonies.api.entity.other.AbstractFastMinecoloniesEntity; +import com.minecolonies.api.util.BlockPosUtil; +import com.minecolonies.core.entity.pathfinding.pathjobs.PathJobMoveAwayFromLocation; +import com.minecolonies.core.entity.pathfinding.pathjobs.PathJobMoveCloseToXNearY; +import com.minecolonies.core.entity.pathfinding.pathjobs.PathJobMoveToLocation; +import com.minecolonies.core.entity.pathfinding.pathjobs.PathJobRandomPos; +import net.minecraft.core.BlockPos; +import net.minecraft.util.Tuple; + +public class EntityNavigationUtils +{ + /** + * Distance to consider being near a building block as reached + */ + public static int BUILDING_REACH_DIST = 4; + + /** + * Distance to consider being near a block inside a building as reached + */ + public static int WOKR_IN_BUILDING_DIST = 7; + + /** + * Distance which counts as reached + */ + public static double REACHED_DIST = 1.5; + + /** + * Tries to walk close to a given pos, staying near another position. + * + * @param entity + * @param desiredPosition + * @param nearbyPosition + * @param distToDesired + * @return True when arrived + */ + public static boolean walkCloseToXNearY( + final AbstractFastMinecoloniesEntity entity, final BlockPos desiredPosition, + final BlockPos nearbyPosition, + final int distToDesired, final boolean safeDestination) + { + return walkCloseToXNearY(entity, desiredPosition, nearbyPosition, distToDesired, safeDestination, 1.0); + } + + /** + * Tries to walk close to a given pos, staying near another position. + * + * @return True when arrived + */ + public static boolean walkCloseToXNearY( + final AbstractFastMinecoloniesEntity entity, final BlockPos desiredPosition, + final BlockPos nearbyPosition, + final int distToDesired, final boolean safeDestination, final double speedFactor) + { + final MinecoloniesAdvancedPathNavigate nav = ((MinecoloniesAdvancedPathNavigate) entity.getNavigation()); + + // Three cases + // 1. Navigation Finished + // 2. Navigation is progressing towards a previous task + // 3. Navigation did not try once + boolean isOnRightTask = (nav.getPathResult() != null + && PathJobMoveCloseToXNearY.isJobFor(nav.getPathResult().getJob(), desiredPosition, nearbyPosition, distToDesired)); + + if (nav.isDone() || !isOnRightTask) + { + if (isOnRightTask) + { + // Check distance once navigation is done, to let the entity walk + if (BlockPosUtil.dist(entity.blockPosition(), desiredPosition) <= distToDesired) + { + nav.stop(); + return true; + } + } + else if (BlockPosUtil.dist(entity.blockPosition(), desiredPosition) <= REACHED_DIST) + { + nav.stop(); + return true; + } + + nav.walkCloseToXNearY(desiredPosition, nearbyPosition, 1, speedFactor, safeDestination); + } + + return false; + } + + /** + * Walks to a position within a building + * + * @return True when arrived + */ + public static boolean walkToPosInBuilding( + final AbstractFastMinecoloniesEntity entity, final BlockPos destination, final IBuilding building, final int reachDistance) + { + if (building == null) + { + return walkToPos(entity, destination, reachDistance, true); + } + + Tuple corners = building.getCorners(); + final BlockPos center = + new BlockPos((corners.getA().getX() + corners.getB().getX()) / 2, building.getPosition().getY(), (corners.getA().getZ() + corners.getB().getZ()) / 2); + + return walkCloseToXNearY(entity, destination, center, reachDistance, true); + } + + /** + * Walks to a position within a building + * + * @return True when arrived + */ + public static boolean walkToBuilding( + final AbstractFastMinecoloniesEntity entity, final IBuilding building) + { + if (building == null) + { + return true; + } + + return walkToPosInBuilding(entity, building.getPosition(), building, BUILDING_REACH_DIST); + } + + /** + * Walks to a given position + * + * @return True when arrived + */ + public static boolean walkToPos( + final AbstractFastMinecoloniesEntity entity, final BlockPos desiredPosition, final boolean safeDestination) + { + return walkToPos(entity, desiredPosition, BUILDING_REACH_DIST, safeDestination, 1.0); + } + + /** + * Walks to a given position + * + * @return True when arrived + */ + public static boolean walkToPos( + final AbstractFastMinecoloniesEntity entity, final BlockPos desiredPosition, + final int distToDesired, final boolean safeDestination) + { + return walkToPos(entity, desiredPosition, distToDesired, safeDestination, 1.0); + } + + /** + * Walks to a given position + * + * @return True when arrived + */ + public static boolean walkToPos( + final AbstractFastMinecoloniesEntity entity, final BlockPos desiredPosition, + final int distToDesired, final boolean safeDestination, final double speedFactor) + { + final MinecoloniesAdvancedPathNavigate nav = ((MinecoloniesAdvancedPathNavigate) entity.getNavigation()); + + boolean isOnRightTask = (nav.getPathResult() != null + && PathJobMoveToLocation.isJobFor(nav.getPathResult().getJob(), desiredPosition)); + + if (nav.isDone() || !isOnRightTask) + { + if (isOnRightTask) + { + // Check distance once navigation is done, to let the entity walk + if (BlockPosUtil.dist(entity.blockPosition(), desiredPosition) <= distToDesired) + { + nav.stop(); + return true; + } + } + else if (BlockPosUtil.dist(entity.blockPosition(), desiredPosition) <= REACHED_DIST) + { + nav.stop(); + return true; + } + + nav.walkTo(desiredPosition, speedFactor, safeDestination); + } + + return false; + } + + /** + * Walks away from a given position + * + * @return True when arrived + */ + public static boolean walkAwayFrom(final AbstractFastMinecoloniesEntity entity, final BlockPos avoid, final int distance, final double speed) + { + final MinecoloniesAdvancedPathNavigate nav = ((MinecoloniesAdvancedPathNavigate) entity.getNavigation()); + boolean isOnRightTask = (nav.getPathResult() != null && PathJobMoveAwayFromLocation.isJobFor(nav.getPathResult().getJob(), distance, avoid)); + + if (nav.isDone() || !isOnRightTask) + { + if (isOnRightTask) + { + // Check distance once navigation is done, to let the entity walk + if (BlockPosUtil.dist(entity.blockPosition(), avoid) >= distance) + { + nav.stop(); + return true; + } + } + else if (BlockPosUtil.dist(entity.blockPosition(), avoid) >= REACHED_DIST) + { + nav.stop(); + return true; + } + + nav.walkAwayFrom(avoid, distance, speed, false); + } + + return false; + } + + /** + * Walks to a random position a given distance away + * + * @return True when arrived + */ + public static boolean walkToRandomPos(final AbstractFastMinecoloniesEntity entity, final int range, final double speedFactor) + { + final MinecoloniesAdvancedPathNavigate nav = ((MinecoloniesAdvancedPathNavigate) entity.getNavigation()); + boolean isOnRightTask = (nav.getPathResult() != null && nav.getPathResult().getJob() instanceof PathJobRandomPos); + + if (nav.isDone() || !isOnRightTask) + { + if (isOnRightTask) + { + nav.stop(); + return true; + } + + nav.walkToRandomPos(range, speedFactor); + } + + return false; + } + + /** + * Walks to a random position a given distance away within the provided box + * + * @return True when arrived + */ + public static boolean walkToRandomPosWithin(final AbstractFastMinecoloniesEntity entity, final int range, final double speedFactor, final Tuple corners) + { + final MinecoloniesAdvancedPathNavigate nav = ((MinecoloniesAdvancedPathNavigate) entity.getNavigation()); + boolean isOnRightTask = (nav.getPathResult() != null && nav.getPathResult().getJob() instanceof PathJobRandomPos); + + if (nav.isDone() || !isOnRightTask) + { + if (isOnRightTask) + { + nav.stop(); + return true; + } + + nav.walkToRandomPos(range, speedFactor, corners); + } + + return false; + } + + /** + * Walks to a random position a given distance away around the provided center + * + * @return True when arrived + */ + public static boolean walkToRandomPosAround(final AbstractFastMinecoloniesEntity entity, final BlockPos center, final int range, final double speedFactor) + { + final MinecoloniesAdvancedPathNavigate nav = ((MinecoloniesAdvancedPathNavigate) entity.getNavigation()); + boolean isOnRightTask = (nav.getPathResult() != null && PathJobRandomPos.isJobFor(nav.getPathResult().getJob(), center, range)); + + if (nav.isDone() || !isOnRightTask) + { + if (isOnRightTask) + { + nav.stop(); + return true; + } + + nav.walkToRandomPosAround(range, speedFactor, center); + } + + return false; + } +} diff --git a/src/main/java/com/minecolonies/core/entity/pathfinding/navigation/MinecoloniesAdvancedPathNavigate.java b/src/main/java/com/minecolonies/core/entity/pathfinding/navigation/MinecoloniesAdvancedPathNavigate.java index 6e09ed3e275..a071975cc6a 100644 --- a/src/main/java/com/minecolonies/core/entity/pathfinding/navigation/MinecoloniesAdvancedPathNavigate.java +++ b/src/main/java/com/minecolonies/core/entity/pathfinding/navigation/MinecoloniesAdvancedPathNavigate.java @@ -7,7 +7,6 @@ import com.minecolonies.api.entity.other.MinecoloniesMinecart; 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.PathFindingStatus; import com.minecolonies.core.entity.pathfinding.PathPointExtended; import com.minecolonies.core.entity.pathfinding.Pathfinding; @@ -58,12 +57,7 @@ public class MinecoloniesAdvancedPathNavigate extends AbstractAdvancedPathNaviga public static final double MIN_SPEED_ALLOWED = 0.1; @Nullable - private PathResult pathResult; - - /** - * The world time when a path was added. - */ - private long pathStartTime = 0; + private PathResult pathResult; /** * Spawn pos of minecart. @@ -144,7 +138,7 @@ public BlockPos getDestination() } @Nullable - public PathResult moveAwayFromXYZ(final BlockPos avoid, final double range, final double speedFactor, final boolean safeDestination) + protected PathResult walkAwayFrom(final BlockPos avoid, final double range, final double speedFactor, final boolean safeDestination) { @NotNull final BlockPos start = PathfindingUtils.prepareStart(ourEntity); @@ -158,7 +152,7 @@ public PathResult moveAwayFromXYZ(final BlockPos avoid, final d @Nullable @Override - public PathResult moveTowards(final BlockPos towards, final double range, final double speedFactor) + protected PathResult walkTowards(final BlockPos towards, final double range, final double speedFactor) { return setPathJob(new PathJobMoveTowards(CompatibilityUtils.getWorldFromEntity(ourEntity), PathfindingUtils.prepareStart(ourEntity), @@ -168,37 +162,29 @@ public PathResult moveTowards(final BlockPos towards, final dou } @Nullable - public PathResult moveToRandomPos(final double range, final double speedFactor) + protected PathResult walkToRandomPos(final int range, final double speedFactor) { - if (pathResult != null && pathResult.isInProgress() && pathResult.getJob() instanceof PathJobRandomPos) - { - return pathResult; - } - - desiredPos = BlockPos.ZERO; - final int theRange = (int) (mob.getRandom().nextInt((int) range) + range / 2); @NotNull final BlockPos start = PathfindingUtils.prepareStart(ourEntity); - - return setPathJob(new PathJobRandomPos(CompatibilityUtils.getWorldFromEntity(ourEntity), + final PathResult result = setPathJob(new PathJobRandomPos(CompatibilityUtils.getWorldFromEntity(ourEntity), start, - theRange, + range, (int) ourEntity.getAttribute(Attributes.FOLLOW_RANGE).getValue(), + ourEntity), null, speedFactor, true); - } - @Nullable - public PathResult moveToRandomPosAroundX(final int range, final double speedFactor, final BlockPos pos) - { - if (pathResult != null - && pathResult.isInProgress() - && pathResult.getJob() instanceof PathJobRandomPos - && ((((PathJobRandomPos) pathResult.getJob()).posAndRangeMatch(range, pos)))) + if (result == null) { - return pathResult; + return null; } - desiredPos = BlockPos.ZERO; - final PathResult result = setPathJob(new PathJobRandomPos(CompatibilityUtils.getWorldFromEntity(ourEntity), + result.getJob().getPathingOptions().withToggleCost(1).withJumpCost(1).withDropCost(1).canDrop = false; + return result; + } + + @Nullable + protected PathResult walkToRandomPosAround(final int range, final double speedFactor, final BlockPos pos) + { + final PathResult result = setPathJob(new PathJobRandomPos(CompatibilityUtils.getWorldFromEntity(ourEntity), PathfindingUtils.prepareStart(ourEntity), 3, (int) ourEntity.getAttribute(Attributes.FOLLOW_RANGE).getValue(), @@ -210,28 +196,21 @@ public PathResult moveToRandomPosAroundX(final int range, final return null; } - result.getJob().getPathingOptions().withToggleCost(1).withJumpCost(1).withDropCost(1); + result.getJob().getPathingOptions().withToggleCost(1).withJumpCost(1).withDropCost(1).canDrop = false; return result; } @Override - public PathResult moveToRandomPos( + protected PathResult walkToRandomPos( final int range, final double speedFactor, final net.minecraft.util.Tuple corners) { - if (pathResult != null && pathResult.isInProgress() && pathResult.getJob() instanceof PathJobRandomPos) - { - return pathResult; - } - - desiredPos = BlockPos.ZERO; - final int theRange = (mob.getRandom().nextInt(range) + range / 2); @NotNull final BlockPos start = PathfindingUtils.prepareStart(ourEntity); - final PathResult result = setPathJob(new PathJobRandomPos(CompatibilityUtils.getWorldFromEntity(ourEntity), + final PathResult result = setPathJob(new PathJobRandomPos(CompatibilityUtils.getWorldFromEntity(ourEntity), start, - theRange, + range, (int) ourEntity.getAttribute(Attributes.FOLLOW_RANGE).getValue(), ourEntity, corners.getA(), @@ -242,12 +221,24 @@ public PathResult moveToRandomPos( return null; } - result.getJob().getPathingOptions().withJumpCost(1).withDropCost(1); + result.getJob().getPathingOptions().withJumpCost(1).withDropCost(1).canDrop = false; return result; } + @Override + protected PathResult walkCloseToXNearY( + final BlockPos desiredPosition, + final BlockPos nearbyPosition, + final int distToDesired, + final double speedFactor, + final boolean safeDestination) + { + PathJobMoveCloseToXNearY pathJob = new PathJobMoveCloseToXNearY(ourEntity.level, desiredPosition, nearbyPosition, 1, ourEntity); + return setPathJob(pathJob, desiredPosition, speedFactor, safeDestination); + } + @Nullable - public PathResult setPathJob( + public PathResult setPathJob( @NotNull final AbstractPathJob job, final BlockPos dest, final double speedFactor, final boolean safeDestination) @@ -258,6 +249,18 @@ public PathResult setPathJob( } stop(); + if (dest != null && !dest.equals(BlockPos.ZERO)) + { + if (job.getStart().distSqr(dest) > 500 * 500) + { + Log.getLogger() + .error( + "Entity: " + ourEntity.getDisplayName().getString() + " is trying to walk too far! distance:" + Math.sqrt(job.getStart().distSqr(dest)) + " from:" + + job.getStart() + " to:" + + dest, new Exception()); + return null; + } + } this.destination = dest; this.originalDestination = dest; @@ -281,7 +284,7 @@ public PathResult setPathJob( job.setPathingOptions(getPathingOptions()); pathResult = job.getResult(); pathResult.startJob(Pathfinding.getExecutor()); - return pathResult; + return (PathResult) pathResult; } @Override @@ -438,56 +441,23 @@ public static double getSmartGroundY(final BlockGetter world, final BlockPos.Mut } @Nullable - public PathResult moveToXYZ(final double x, final double y, final double z, final double speedFactor) + protected PathResult walkTo(final BlockPos desiredPos, final double speedFactor, final boolean safeDestination) { - final int newX = Mth.floor(x); - final int newY = (int) y; - final int newZ = Mth.floor(z); - - if (pathResult != null && pathResult.getJob() instanceof PathJobMoveToLocation) - { - if (pathResult.isComputing()) - { - return pathResult; - } - - if (((destination != null && BlockPosUtil.equals(destination, newX, newY, newZ)) || (originalDestination != null && BlockPosUtil.equals(originalDestination, - newX, - newY, - newZ)))) - { - if (pathResult.getStatus() == IN_PROGRESS_FOLLOWING || ColonyConstants.rand.nextInt(20) != 0) - { - return pathResult; - } - } - } - @NotNull final BlockPos start = PathfindingUtils.prepareStart(ourEntity); - desiredPos = new BlockPos(newX, newY, newZ); - - if (start.distSqr(desiredPos) > 500 * 500) - { - Log.getLogger() - .error( - "Entity: " + ourEntity.getDisplayName().getString() + " is trying to walk too far! distance:" + Math.sqrt(start.distSqr(desiredPos)) + " from:" + start + " to:" - + desiredPos, new Exception()); - return null; - } - return setPathJob( new PathJobMoveToLocation(CompatibilityUtils.getWorldFromEntity(ourEntity), start, desiredPos, (int) ourEntity.getAttribute(Attributes.FOLLOW_RANGE).getValue(), ourEntity), - desiredPos, speedFactor, true); + desiredPos, speedFactor, safeDestination); } + @Deprecated(since = "Do not use, always returns true, vanilla override") @Override - public boolean tryMoveToBlockPos(final BlockPos pos, final double speedFactor) + public boolean walkTo(final BlockPos pos, final double speedFactor) { - moveToXYZ(pos.getX(), pos.getY(), pos.getZ(), speedFactor); + walkTo(pos, speedFactor, false); return true; } @@ -586,31 +556,25 @@ public void setSpeedModifier(final double speedFactor) walkSpeedFactor = speedFactor; } - /** - * Deprecated - try to use BlockPos instead - */ + @Deprecated(since = "Do not use, always returns true, vanilla override") @Override public boolean moveTo(final double x, final double y, final double z, final double speedFactor) { - if (x == 0 && y == 0 && z == 0) - { - return false; - } - - moveToXYZ(x, y, z, speedFactor); + walkTo(BlockPos.containing(x, y, z), speedFactor, false); return true; } @Override public boolean moveTo(final Entity entityIn, final double speedFactor) { - return tryMoveToBlockPos(entityIn.blockPosition(), speedFactor); + return walkTo(entityIn.blockPosition(), speedFactor); } // Removes stupid vanilla stuff, causing our pathpoints to occasionally be replaced by vanilla ones. @Override protected void trimPath() {} + @Deprecated(since = "Do not use, always returns true, vanilla override") @Override public boolean moveTo(@Nullable final Path path, final double speedFactor) { @@ -619,7 +583,6 @@ public boolean moveTo(@Nullable final Path path, final double speedFactor) super.stop(); return false; } - pathStartTime = level.getGameTime(); return super.moveTo(convertPath(path), speedFactor); } @@ -1115,7 +1078,7 @@ public void stop() } @Override - public TreePathResult moveToTree( + public TreePathResult walkToTree( final BlockPos startRestriction, final BlockPos endRestriction, final double speed, @@ -1141,7 +1104,7 @@ public TreePathResult moveToTree( } @Override - public TreePathResult moveToTree(final int range, final double speed, final List excludedTrees, final int dyntreesize, final IColony colony) + public TreePathResult walkToTree(final int range, final double speed, final List excludedTrees, final int dyntreesize, final IColony colony) { @NotNull BlockPos start = PathfindingUtils.prepareStart(ourEntity); final BlockPos buildingPos = ((AbstractEntityCitizen) mob).getCitizenColonyHandler().getWorkBuilding().getPosition(); @@ -1157,16 +1120,16 @@ public TreePathResult moveToTree(final int range, final double speed, final List @Nullable @Override - public PathResult moveToLivingEntity(@NotNull final Entity e, final double speed) + public PathResult walkToEntity(@NotNull final Entity e, final double speed) { - return moveToXYZ(e.getX(), e.getY(), e.getZ(), speed); + return walkTo(e.blockPosition(), speed, false); } @Nullable @Override - public PathResult moveAwayFromLivingEntity(@NotNull final Entity e, final double distance, final double speed) + public PathResult moveAwayFromLivingEntity(@NotNull final Entity e, final double distance, final double speed) { - return moveAwayFromXYZ(e.blockPosition(), distance, speed, true); + return walkAwayFrom(e.blockPosition(), distance, speed, true); } @Override diff --git a/src/main/java/com/minecolonies/core/entity/pathfinding/navigation/PathfindingAIHelper.java b/src/main/java/com/minecolonies/core/entity/pathfinding/navigation/PathfindingAIHelper.java deleted file mode 100644 index 4a596badd3d..00000000000 --- a/src/main/java/com/minecolonies/core/entity/pathfinding/navigation/PathfindingAIHelper.java +++ /dev/null @@ -1,53 +0,0 @@ -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()); - - // Three cases - // 1. Navigation Finished - // 2. Navigation is progressing towards a previous task - // 3. Navigation did not try once - boolean isOnRightTask = (nav.getPathResult() != null - && nav.getPathResult().getJob() instanceof PathJobMoveCloseToXNearY job - && job.nearbyPosition.equals(nearbyPosition) - && job.desiredPosition.equals(desiredPosition)); - - if (nav.isDone() || !isOnRightTask) - { - if (isOnRightTask) - { - // Check distance once navigation is done, to let the entity walk - if (BlockPosUtil.dist(entity.blockPosition(), desiredPosition) < distToDesired) - { - nav.stop(); - return false; - } - } - - PathJobMoveCloseToXNearY pathJob = new PathJobMoveCloseToXNearY(entity.level, desiredPosition, nearbyPosition, 1, entity); - nav.setPathJob(pathJob, desiredPosition, 1.0, false); - } - - return true; - } -} diff --git a/src/main/java/com/minecolonies/core/entity/pathfinding/navigation/PathingStuckHandler.java b/src/main/java/com/minecolonies/core/entity/pathfinding/navigation/PathingStuckHandler.java index b02e0c6f46d..b58fac9dd67 100644 --- a/src/main/java/com/minecolonies/core/entity/pathfinding/navigation/PathingStuckHandler.java +++ b/src/main/java/com/minecolonies/core/entity/pathfinding/navigation/PathingStuckHandler.java @@ -149,6 +149,7 @@ public static PathingStuckHandler createStuckHandler() @Override public void checkStuck(final AbstractAdvancedPathNavigate navigator) { + // TODO: rework to allow paths to nonsafe locations still benefit from non-teleport options(skip ahead, reset path etc) if (navigator.getDesiredPos() == null || navigator.getDesiredPos().equals(BlockPos.ZERO)) { resetGlobalStuckTimers(); @@ -338,7 +339,7 @@ private void tryUnstuck(final AbstractAdvancedPathNavigate navigator) navigator.stop(); final int range = ColonyConstants.rand.nextInt(20) + Math.min(100, Math.max(20, BlockPosUtil.distManhattan(navigator.ourEntity.blockPosition(), prevDestination))); - navigator.moveTowards(navigator.getOurEntity().blockPosition().relative(movingAwayDir, 40), range, 1.0f); + navigator.walkTowards(navigator.getOurEntity().blockPosition().relative(movingAwayDir, 40), range, 1.0f); movingAwayDir = movingAwayDir.getClockWise(); navigator.setPauseTicks(range * TICKS_PER_BLOCK); return; diff --git a/src/main/java/com/minecolonies/core/entity/pathfinding/pathjobs/AbstractPathJob.java b/src/main/java/com/minecolonies/core/entity/pathfinding/pathjobs/AbstractPathJob.java index 4b76b8066f4..84890d07d08 100644 --- a/src/main/java/com/minecolonies/core/entity/pathfinding/pathjobs/AbstractPathJob.java +++ b/src/main/java/com/minecolonies/core/entity/pathfinding/pathjobs/AbstractPathJob.java @@ -149,7 +149,7 @@ public abstract class AbstractPathJob implements Callable, IPathJob /** * Heuristic modifier */ - private double heuristicMod = 1; + private double heuristicMod = 2; /** * First node @@ -192,6 +192,10 @@ public AbstractPathJob(final Level world, @NotNull final BlockPos start, int ran result.setJob(this); this.entity = entity; + if (entity != null && entity.getNavigation() instanceof IDynamicHeuristicNavigator navigator) + { + heuristicMod = 1 + navigator.getAvgHeuristicModifier(); + } } /** @@ -218,6 +222,10 @@ protected AbstractPathJob(final Level actualWorld, final LevelReader chunkCache, result.setJob(this); this.entity = entity; + if (entity != null && entity.getNavigation() instanceof IDynamicHeuristicNavigator navigator) + { + heuristicMod = 1 + navigator.getAvgHeuristicModifier(); + } } /** @@ -256,6 +264,10 @@ public AbstractPathJob(final Level world, @NotNull final BlockPos start, @NotNul this.result = result; result.setJob(this); this.entity = entity; + if (entity != null && entity.getNavigation() instanceof IDynamicHeuristicNavigator navigator) + { + heuristicMod = 1 + navigator.getAvgHeuristicModifier(); + } } /** @@ -285,7 +297,7 @@ public final Path call() */ private MNode getAndSetupStartNode() { - final MNode startNode = new MNode(null, start.getX(), start.getY(), start.getZ(), 0, computeHeuristic(start.getX(), start.getY(), start.getZ())); + final MNode startNode = new MNode(null, start.getX(), start.getY(), start.getZ(), 0, computeHeuristic(start.getX(), start.getY(), start.getZ()) * heuristicMod); if (PathfindingUtils.isLadder(cachedBlockLookup.getBlockState(start.getX(), start.getY(), start.getZ()), pathingOptions)) { diff --git a/src/main/java/com/minecolonies/core/entity/pathfinding/pathjobs/PathJobMoveAwayFromLocation.java b/src/main/java/com/minecolonies/core/entity/pathfinding/pathjobs/PathJobMoveAwayFromLocation.java index 582ff0d55c8..0e10ffa7080 100644 --- a/src/main/java/com/minecolonies/core/entity/pathfinding/pathjobs/PathJobMoveAwayFromLocation.java +++ b/src/main/java/com/minecolonies/core/entity/pathfinding/pathjobs/PathJobMoveAwayFromLocation.java @@ -135,4 +135,19 @@ public BlockPos getDestination() { return preferredDirection; } + + /** + * Helper to compare if the given move away job matches the input parameters + * + * @return true if the given job is the same + */ + public static boolean isJobFor(final AbstractPathJob job, final int avoidDistance, final BlockPos toAvoid) + { + if (job instanceof PathJobMoveAwayFromLocation pathJob) + { + return pathJob.avoidDistance == avoidDistance && pathJob.avoid.equals(toAvoid); + } + + return false; + } } diff --git a/src/main/java/com/minecolonies/core/entity/pathfinding/pathjobs/PathJobMoveCloseToXNearY.java b/src/main/java/com/minecolonies/core/entity/pathfinding/pathjobs/PathJobMoveCloseToXNearY.java index 66db7c3cad8..ac43838b65b 100644 --- a/src/main/java/com/minecolonies/core/entity/pathfinding/pathjobs/PathJobMoveCloseToXNearY.java +++ b/src/main/java/com/minecolonies/core/entity/pathfinding/pathjobs/PathJobMoveCloseToXNearY.java @@ -60,7 +60,7 @@ protected boolean isAtDestination(@NotNull final MNode n) return false; } - return BlockPosUtil.distManhattan(desiredPosition, n.x, n.y, n.z) < distToDesired + return BlockPosUtil.distManhattan(desiredPosition, n.x, n.y, n.z) <= distToDesired && SurfaceType.getSurfaceType(world, cachedBlockLookup.getBlockState(n.x, n.y - 1, n.z), tempWorldPos.set(n.x, n.y - 1, n.z), getPathingOptions()) == SurfaceType.WALKABLE; } @@ -105,4 +105,19 @@ public BlockPos getDestination() { return desiredPosition; } + + /** + * Helper to compare if the given move close to X near Y job matches the input parameters + * + * @return true if the given job is the same + */ + public static boolean isJobFor(final AbstractPathJob job, final BlockPos desiredPosition, final BlockPos nearbyPosition, final int distance) + { + if (job instanceof PathJobMoveCloseToXNearY pathJob) + { + return pathJob.nearbyPosition.equals(desiredPosition) && pathJob.nearbyPosition.equals(nearbyPosition) && pathJob.distToDesired == distance; + } + + return false; + } } diff --git a/src/main/java/com/minecolonies/core/entity/pathfinding/pathjobs/PathJobMoveToLocation.java b/src/main/java/com/minecolonies/core/entity/pathfinding/pathjobs/PathJobMoveToLocation.java index a7097d8cc90..53421e1ff3c 100644 --- a/src/main/java/com/minecolonies/core/entity/pathfinding/pathjobs/PathJobMoveToLocation.java +++ b/src/main/java/com/minecolonies/core/entity/pathfinding/pathjobs/PathJobMoveToLocation.java @@ -6,7 +6,6 @@ import com.minecolonies.core.entity.pathfinding.MNode; import com.minecolonies.core.entity.pathfinding.PathfindingUtils; import com.minecolonies.core.entity.pathfinding.SurfaceType; -import com.minecolonies.core.entity.pathfinding.navigation.IDynamicHeuristicNavigator; import com.minecolonies.core.entity.pathfinding.pathresults.PathResult; import net.minecraft.core.BlockPos; import net.minecraft.world.entity.Mob; @@ -28,11 +27,6 @@ public class PathJobMoveToLocation extends AbstractPathJob implements IDestinati // 0 = exact match private float destinationSlack = DESTINATION_SLACK_NONE; - /** - * Modifier to the heuristics - */ - private double heuristicModifier = 1; - /** * Prepares the PathJob for the path finding system. * @@ -49,13 +43,6 @@ public PathJobMoveToLocation(final Level world, @NotNull final BlockPos start, @ maxNodes += range; this.destination = new BlockPos(end); - if (entity != null && entity.getNavigation() instanceof IDynamicHeuristicNavigator) - { - heuristicModifier = ((IDynamicHeuristicNavigator) entity.getNavigation()).getAvgHeuristicModifier(); - } - - // Overestimate for long distances, +1 per 100 blocks - heuristicModifier += BlockPosUtil.distManhattan(start, end) / 100.0; extraNodes = 4; } @@ -80,7 +67,7 @@ protected Path search() @Override protected double computeHeuristic(final int x, final int y, final int z) { - return BlockPosUtil.distManhattan(destination, x, y, z) * heuristicModifier; + return BlockPosUtil.distManhattan(destination, x, y, z); } /** @@ -174,4 +161,19 @@ public BlockPos getDestination() { return destination; } + + /** + * Helper to compare if the given move to location job matches the input parameters + * + * @return true if the given job is the same + */ + public static boolean isJobFor(final AbstractPathJob job, final BlockPos desiredPosition) + { + if (job instanceof PathJobMoveToLocation pathJob) + { + return pathJob.getDestination().equals(desiredPosition); + } + + return false; + } } diff --git a/src/main/java/com/minecolonies/core/entity/pathfinding/pathjobs/PathJobRandomPos.java b/src/main/java/com/minecolonies/core/entity/pathfinding/pathjobs/PathJobRandomPos.java index b70c2b0fb7d..c4f77836cf1 100644 --- a/src/main/java/com/minecolonies/core/entity/pathfinding/pathjobs/PathJobRandomPos.java +++ b/src/main/java/com/minecolonies/core/entity/pathfinding/pathjobs/PathJobRandomPos.java @@ -5,7 +5,6 @@ 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.navigation.IDynamicHeuristicNavigator; import com.minecolonies.core.entity.pathfinding.pathresults.PathResult; import net.minecraft.core.BlockPos; import net.minecraft.world.entity.Mob; @@ -38,11 +37,7 @@ public class PathJobRandomPos extends AbstractPathJob implements IDestinationPat * Box restriction area */ private AABB restrictionBox = null; - - /** - * Modifier to the heuristics - */ - private double heuristicModifier = 1.0; + private BlockPos restrictionBoxCenter = null; /** * Prepares the PathJob for the path finding system. @@ -65,11 +60,6 @@ public PathJobRandomPos( this.maxDistToDest = -1; this.destination = BlockPosUtil.getRandomPosAround(start, minDistFromStart); - - if (entity != null && entity.getNavigation() instanceof IDynamicHeuristicNavigator) - { - heuristicModifier = ((IDynamicHeuristicNavigator) entity.getNavigation()).getAvgHeuristicModifier(); - } } /** @@ -122,7 +112,7 @@ public PathJobRandomPos( Math.max(startRestriction.getX(), endRestriction.getX()), Math.max(startRestriction.getY(), endRestriction.getY()), Math.max(startRestriction.getZ(), endRestriction.getZ())); - + restrictionBoxCenter = BlockPos.containing(restrictionBox.getCenter()); this.minDistFromStart = minDistFromStart; this.maxDistToDest = -1; @@ -132,7 +122,12 @@ public PathJobRandomPos( @Override protected double computeHeuristic(final int x, final int y, final int z) { - return BlockPosUtil.distManhattan(destination, x, y, z) * heuristicModifier; + if (restrictionBox != null) + { + return (BlockPosUtil.distManhattan(destination, x, y, z) + BlockPosUtil.distManhattan(restrictionBoxCenter, x, y, z) / 2.0); + } + + return BlockPosUtil.distManhattan(destination, x, y, z); } @Override @@ -172,7 +167,7 @@ public void setPathingOptions(final PathingOptions pathingOptions) */ public boolean posAndRangeMatch(final int range, final BlockPos pos) { - return destination != null && pos != null && range == maxDistToDest && destination.equals(pos); + return; } @Override @@ -180,4 +175,19 @@ public BlockPos getDestination() { return destination; } + + /** + * Helper to compare if the given random pos job matches the input parameters + * + * @return true if the given job is the same + */ + public static boolean isJobFor(final AbstractPathJob job, final BlockPos center, final int range) + { + if (job instanceof PathJobRandomPos pathJob) + { + return pathJob.destination != null && pathJob.destination.equals(center) && pathJob.maxDistToDest == range; + } + + return false; + } } diff --git a/src/main/java/com/minecolonies/core/entity/pathfinding/proxy/EntityCitizenWalkToProxy.java b/src/main/java/com/minecolonies/core/entity/pathfinding/proxy/EntityCitizenWalkToProxy.java index b02676f410f..fbea2a62064 100644 --- a/src/main/java/com/minecolonies/core/entity/pathfinding/proxy/EntityCitizenWalkToProxy.java +++ b/src/main/java/com/minecolonies/core/entity/pathfinding/proxy/EntityCitizenWalkToProxy.java @@ -11,16 +11,16 @@ import com.minecolonies.core.colony.buildings.workerbuildings.BuildingMiner; import com.minecolonies.core.colony.jobs.AbstractJobGuard; import com.minecolonies.core.colony.jobs.JobMiner; -import com.minecolonies.core.entity.ai.workers.util.MinerLevel; import com.minecolonies.core.entity.ai.workers.util.MineNode; -import com.minecolonies.core.util.WorkerUtil; -import net.minecraft.world.entity.Mob; -import net.minecraft.core.Direction; +import com.minecolonies.core.entity.ai.workers.util.MinerLevel; import net.minecraft.core.BlockPos; +import net.minecraft.core.Direction; +import net.minecraft.world.entity.Mob; import org.jetbrains.annotations.NotNull; import java.util.*; +// TODO: Rework public class EntityCitizenWalkToProxy extends AbstractWalkToProxy { /** @@ -269,11 +269,35 @@ private void calculateNodes(final MinerLevel level, final int levelDepth, final @Override public boolean isLivingAtSiteWithMove(final Mob entity, final int x, final int y, final int z, final int range) { - if (!WorkerUtil.isWorkerAtSiteWithMove((AbstractEntityCitizen) entity, x, y, z, range)) + if (!isWorkerAtSiteWithMove((AbstractEntityCitizen) entity, x, y, z, range)) { EntityUtils.tryMoveLivingToXYZ(entity, x, y, z); return false; } return true; } + + /** + * Checks if a worker is at his working site. If he isn't, sets it's path to the location. + * + * @param worker Worker to check + * @param x X-coordinate + * @param y Y-coordinate + * @param z Z-coordinate + * @param range Range to check in + * @return True if worker is at site, otherwise false. + */ + public static boolean isWorkerAtSiteWithMove(@NotNull final AbstractEntityCitizen worker, final int x, final int y, final int z, final int range) + { + if (!EntityUtils.isLivingAtSiteWithMove(worker, x, y, z, range)) + { + //If not moving the try setting the point where the entity should move to + if (worker.getNavigation().isDone()) + { + EntityUtils.tryMoveLivingToXYZ(worker, x, y, z); + } + return false; + } + return true; + } } diff --git a/src/main/java/com/minecolonies/core/entity/visitor/VisitorCitizen.java b/src/main/java/com/minecolonies/core/entity/visitor/VisitorCitizen.java index 7b5745b1487..f6427bfc7f8 100644 --- a/src/main/java/com/minecolonies/core/entity/visitor/VisitorCitizen.java +++ b/src/main/java/com/minecolonies/core/entity/visitor/VisitorCitizen.java @@ -5,10 +5,8 @@ import com.minecolonies.api.colony.permissions.Action; import com.minecolonies.api.colony.requestsystem.StandardFactoryController; import com.minecolonies.api.colony.requestsystem.location.ILocation; -import com.minecolonies.api.entity.CustomGoalSelector; import com.minecolonies.api.entity.citizen.AbstractEntityCitizen; import com.minecolonies.api.entity.citizen.citizenhandlers.*; -import com.minecolonies.api.entity.pathfinding.proxy.IWalkToProxy; import com.minecolonies.api.inventory.InventoryCitizen; import com.minecolonies.api.inventory.container.ContainerCitizenInventory; import com.minecolonies.api.util.*; @@ -24,9 +22,11 @@ import com.minecolonies.core.entity.ai.minimal.LookAtEntityInteractGoal; import com.minecolonies.core.entity.ai.visitor.EntityAIVisitor; import com.minecolonies.core.entity.citizen.EntityCitizen; -import com.minecolonies.core.entity.citizen.citizenhandlers.*; +import com.minecolonies.core.entity.citizen.citizenhandlers.CitizenExperienceHandler; +import com.minecolonies.core.entity.citizen.citizenhandlers.CitizenInventoryHandler; +import com.minecolonies.core.entity.citizen.citizenhandlers.CitizenJobHandler; +import com.minecolonies.core.entity.citizen.citizenhandlers.CitizenSleepHandler; import com.minecolonies.core.entity.pathfinding.navigation.MovementHandler; -import com.minecolonies.core.entity.pathfinding.proxy.EntityCitizenWalkToProxy; import com.minecolonies.core.network.messages.client.ItemParticleEffectMessage; import com.minecolonies.core.network.messages.server.colony.OpenInventoryMessage; import com.minecolonies.core.util.citizenutils.CitizenItemUtils; @@ -78,10 +78,6 @@ public class VisitorCitizen extends AbstractEntityCitizen * It's citizen Id. */ private int citizenId = 0; - /** - * The Walk to proxy (Shortest path through intermediate blocks). - */ - private IWalkToProxy proxy; /** * Reference to the data representation inside the colony. */ @@ -200,23 +196,6 @@ public boolean hurt(@NotNull final DamageSource damageSource, final float damage return false; } - /** - * Checks if a worker is at his working site. If he isn't, sets it's path to the location - * - * @param site the place where he should walk to - * @param range Range to check in - * @return True if worker is at site, otherwise false. - */ - @Override - public boolean isWorkerAtSiteWithMove(@NotNull final BlockPos site, final int range) - { - if (proxy == null) - { - proxy = new EntityCitizenWalkToProxy(this); - } - return proxy.walkToBlock(site, range, true); - } - @Nullable @Override public ICitizenData getCitizenData() @@ -283,12 +262,6 @@ public void playMoveAwaySound() } - @Override - public IWalkToProxy getProxy() - { - return proxy; - } - @Override public void decreaseSaturationForAction() { diff --git a/src/main/java/com/minecolonies/core/util/TeleportHelper.java b/src/main/java/com/minecolonies/core/util/TeleportHelper.java index 77b1ccc2700..6e1ae9f89bb 100755 --- a/src/main/java/com/minecolonies/core/util/TeleportHelper.java +++ b/src/main/java/com/minecolonies/core/util/TeleportHelper.java @@ -6,13 +6,13 @@ import com.minecolonies.api.util.BlockPosUtil; import com.minecolonies.api.util.EntityUtils; import com.minecolonies.api.util.MessageUtils; -import net.minecraft.server.level.ServerPlayer; -import net.minecraft.resources.ResourceKey; import net.minecraft.core.BlockPos; -import net.minecraft.world.level.ChunkPos; -import net.minecraft.world.level.Level; +import net.minecraft.resources.ResourceKey; import net.minecraft.server.level.ServerLevel; +import net.minecraft.server.level.ServerPlayer; import net.minecraft.server.level.TicketType; +import net.minecraft.world.level.ChunkPos; +import net.minecraft.world.level.Level; import org.jetbrains.annotations.NotNull; import static com.minecolonies.api.util.constant.translation.CommandTranslationConstants.COMMAND_COLONY_ID_NOT_FOUND; @@ -51,6 +51,7 @@ public static boolean teleportCitizen(final AbstractEntityCitizen citizen, final citizen.getCitizenSleepHandler().onWakeUp(); } + citizen.getNavigation().stop(); citizen.stopRiding(); citizen.moveTo( spawnPoint.getX() + MIDDLE_BLOCK_OFFSET, @@ -58,16 +59,6 @@ public static boolean teleportCitizen(final AbstractEntityCitizen citizen, final spawnPoint.getZ() + MIDDLE_BLOCK_OFFSET, citizen.getRotationYaw(), citizen.getRotationPitch()); - if (citizen.getProxy() != null) - { - citizen.getProxy().reset(); - } - citizen.getNavigation().stop(); - if (citizen.getProxy() != null) - { - citizen.getProxy().reset(); - } - return true; } diff --git a/src/main/java/com/minecolonies/core/util/WorkerUtil.java b/src/main/java/com/minecolonies/core/util/WorkerUtil.java index d42a9028a8e..16a16fc770d 100755 --- a/src/main/java/com/minecolonies/core/util/WorkerUtil.java +++ b/src/main/java/com/minecolonies/core/util/WorkerUtil.java @@ -7,9 +7,9 @@ import com.minecolonies.api.crafting.ItemStorage; import com.minecolonies.api.entity.citizen.AbstractEntityCitizen; import com.minecolonies.api.equipment.ModEquipmentTypes; +import com.minecolonies.api.equipment.registry.EquipmentTypeEntry; import com.minecolonies.api.inventory.InventoryCitizen; import com.minecolonies.api.items.ModTags; -import com.minecolonies.api.equipment.registry.EquipmentTypeEntry; import com.minecolonies.api.util.EntityUtils; import com.minecolonies.api.util.ItemStackUtils; import com.minecolonies.api.util.Tuple; @@ -18,7 +18,6 @@ import com.minecolonies.core.colony.buildings.modules.SettingsModule; import com.minecolonies.core.colony.buildings.workerbuildings.BuildingFlorist; import com.minecolonies.core.entity.ai.workers.util.MinerLevel; -import com.minecolonies.core.entity.citizen.EntityCitizen; import com.minecolonies.core.tileentities.TileEntityCompostedDirt; import net.minecraft.core.BlockPos; import net.minecraft.nbt.CompoundTag; @@ -109,43 +108,6 @@ public static boolean isPathBlock(final Block block) return block.defaultBlockState().is(ModTags.pathingBlocks); } - /** - * {@link WorkerUtil#isWorkerAtSiteWithMove(AbstractEntityCitizen, int, int, int, int)}. - * - * @param worker Worker to check. - * @param site Chunk coordinates of site to check. - * @param range Range to check in. - * @return True when within range, otherwise false. - */ - public static boolean isWorkerAtSiteWithMove(@NotNull final EntityCitizen worker, @NotNull final BlockPos site, final int range) - { - return isWorkerAtSiteWithMove(worker, site.getX(), site.getY(), site.getZ(), range); - } - - /** - * Checks if a worker is at his working site. If he isn't, sets it's path to the location. - * - * @param worker Worker to check - * @param x X-coordinate - * @param y Y-coordinate - * @param z Z-coordinate - * @param range Range to check in - * @return True if worker is at site, otherwise false. - */ - public static boolean isWorkerAtSiteWithMove(@NotNull final AbstractEntityCitizen worker, final int x, final int y, final int z, final int range) - { - if (!EntityUtils.isLivingAtSiteWithMove(worker, x, y, z, range)) - { - //If not moving the try setting the point where the entity should move to - if (worker.getNavigation().isDone()) - { - EntityUtils.tryMoveLivingToXYZ(worker, x, y, z); - } - return false; - } - return true; - } - /** * Recalls the citizen, notifies player if not successful. *