import net.sf.freecol.common.model.CombatModel; //导入方法依赖的package包/类
/**
* Searches for a unit that is a credible threatening unit to this
* unit within a range.
*
* @param range The number of turns to search for a threat in.
* @param threat The maximum tolerable probability of a potentially
* threatening unit defeating this unit in combat.
* @return A path to the threat, or null if not found.
*/
public PathNode searchForDanger(final int range, final float threat) {
final CombatModel cm = getGame().getCombatModel();
final Tile start = getTile();
final GoalDecider threatDecider = new GoalDecider() {
private PathNode found = null;
@Override
public PathNode getGoal() { return found; }
@Override
public boolean hasSubGoals() { return false; }
@Override
public boolean check(Unit unit, PathNode path) {
Tile tile = path.getTile();
if (tile == null) return false;
Unit first = tile.getFirstUnit();
if (first == null
|| !getOwner().atWarWith(first.getOwner())) {
return false;
}
final Predicate attackerPred = u -> {
PathNode p;
return (u.canAttack(unit)
&& cm.calculateCombatOdds(u, unit).win >= threat
&& (p = u.findPath(start)) != null
&& p.getTotalTurns() < range);
};
if (any(transform(tile.getUnits(), attackerPred))) {
found = path;
return true;
}
return false;
}
};
// The range to search will depend on the speed of the other
// unit. We can not know what it will be in advance, and it
// might be significantly faster than this unit. We do not
// want to just use an unbounded search range because this
// routine must be quick (especially when the supplied range
// is low). So use the heuristic of increasing the range by
// the ratio of the fastest appropriate (land/naval) unit type
// speed over the unit speed.
int reverseRange = range * (((isNaval())
? getSpecification().getFastestNavalUnitType()
: getSpecification().getFastestLandUnitType())
.getMovement()) / this.getType().getMovement();
return (start == null) ? null
: search(start, threatDecider, CostDeciders.avoidIllegal(),
reverseRange, getCarrier());
}