Skip to content

Commit

Permalink
Add feature to alert guards of nearby raiders hitting on a gate
Browse files Browse the repository at this point in the history
Improved raider waypoint pathfinding
  • Loading branch information
someaddons committed Jan 25, 2025
1 parent e5f06a0 commit 91fe198
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,16 @@ private void setNextPatrolTarget(final BlockPos target)
currentPatrolPoint = target;
}

/**
* Get the current patrol point
*
* @return
*/
public BlockPos getCurrentPatrolPoint()
{
return currentPatrolPoint;
}

/**
* Check if the worker has the required tool to fight.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
package com.minecolonies.core.entity.mobs.aitasks;

import com.minecolonies.api.blocks.decorative.AbstractBlockGate;
import com.minecolonies.api.colony.ICitizenData;
import com.minecolonies.api.colony.IColony;
import com.minecolonies.api.entity.citizen.AbstractEntityCitizen;
import com.minecolonies.api.entity.mobs.AbstractEntityMinecoloniesRaider;
import com.minecolonies.api.util.BlockPosUtil;
import com.minecolonies.core.MineColonies;
import com.minecolonies.core.colony.jobs.AbstractJobGuard;
import com.minecolonies.core.entity.ai.workers.guard.AbstractEntityAIGuard;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.entity.ai.goal.BreakDoorGoal;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.phys.Vec3;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.EnumSet;
import java.util.List;

import static com.minecolonies.api.research.util.ResearchConstants.MECHANIC_ENHANCED_GATES;

Expand Down Expand Up @@ -102,6 +111,37 @@ public void tick()
fasterBreakPerXNearby /= 2;
breakChance = (int) Math.max(1,
hardness / (1 + (mob.level.getEntitiesOfClass(AbstractEntityMinecoloniesRaider.class, mob.getBoundingBox().inflate(5)).size() / fasterBreakPerXNearby)));

// Alert nearby guards
if (this.mob.getRandom().nextInt(breakChance) == 0 && mob instanceof AbstractEntityMinecoloniesRaider raider && mob.level.getBlockState(doorPos)
.getBlock() instanceof AbstractBlockGate)
{
// Alerts guards of raiders reaching a building
final List<AbstractEntityCitizen> possibleGuards = new ArrayList<>();

for (final ICitizenData entry : raider.getColony().getCitizenManager().getCitizens())
{
if (entry.getEntity().isPresent()
&& entry.getJob() instanceof AbstractJobGuard
&& BlockPosUtil.getDistanceSquared(entry.getEntity().get().blockPosition(), doorPos) < 100 * 100 && entry.getJob().getWorkerAI() != null)
{
if (((AbstractEntityAIGuard<?, ?>) entry.getJob().getWorkerAI()).canHelp(doorPos) && !doorPos.equals(((AbstractEntityAIGuard<?, ?>) entry.getJob()
.getWorkerAI()).getCurrentPatrolPoint()))
{
possibleGuards.add(entry.getEntity().get());
}
}
}

possibleGuards.sort(Comparator.comparingInt(guard -> (int) doorPos.distSqr(guard.blockPosition())));
BlockPos gotoPos = BlockPos.containing(Vec3.atCenterOf(doorPos)
.add(Vec3.atCenterOf(raider.getColony().getCenter()).subtract(Vec3.atCenterOf(doorPos)).normalize().multiply(3, 0, 3)));

for (int i = 0; i < possibleGuards.size() && i <= 3; i++)
{
((AbstractEntityAIGuard<?, ?>) possibleGuards.get(i).getCitizenData().getJob().getWorkerAI()).setNextPatrolTargetAndMove(gotoPos);
}
}
}

if (this.breakTime == this.getDoorBreakTime() - 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public abstract class AbstractPathJob implements Callable<Path>, IPathJob
/**
* Heuristic modifier
*/
private double heuristicMod = 2;
protected double heuristicMod = 2;

/**
* First node
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ public PathJobRaiderPathing(
super(world, start, targetSpawnPoint, new PathResult<PathJobRaiderPathing>(), null);
this.buildings = buildings;
direction = targetSpawnPoint;
maxNodes = 10000;
setPathingOptions(new PathingOptions().withJumpCost(1).withStartSwimCost(1).withSwimCost(1).withCanSwim(true).withCanEnterDoors(true));
maxNodes = 20000;
heuristicMod = 5.0;
setPathingOptions(new PathingOptions().withJumpCost(1).withStartSwimCost(1).withSwimCost(1).withCanSwim(true).withCanEnterDoors(true).withDropCost(5));
}

@Override
Expand Down Expand Up @@ -160,14 +161,14 @@ protected double modifyCost(
{
double modifier = addCost;
addCost = 1.0;
if (!super.isPassable(x, y, z, false, null))
if (!super.isPassable(x, y, z, true, null))
{
modifier *= THROUGH_BLOCK_COST;
modifier = THROUGH_BLOCK_COST;
}

if (SurfaceType.getSurfaceType(cachedBlockLookup, cachedBlockLookup.getBlockState(x, y - 1, z), tempWorldPos.set(x, y - 1, z), getPathingOptions()) != SurfaceType.WALKABLE)
{
modifier *= THROUGH_BLOCK_COST;
modifier = THROUGH_BLOCK_COST;
}

return cost * modifier;
Expand Down

0 comments on commit 91fe198

Please sign in to comment.