MCPcopy Create free account
hub / github.com/OpenXcom/OpenXcom / selectNearestTarget

Method selectNearestTarget

src/Battlescape/AlienBAIState.cpp:1012–1056  ·  view source on GitHub ↗

* Selects the nearest known living target we can see/reach and returns the number of visible enemies. * This function includes civilians as viable targets. * @return viable targets. */

Source from the content-addressed store, hash-verified

1010 * @return viable targets.
1011 */
1012int AlienBAIState::selectNearestTarget()
1013{
1014 int tally = 0;
1015 _closestDist= 100;
1016 _aggroTarget = 0;
1017 Position origin = _save->getTileEngine()->getSightOriginVoxel(_unit);
1018 origin.z -= 2;
1019 Position target;
1020 for (std::vector<BattleUnit*>::const_iterator i = _save->getUnits()->begin(); i != _save->getUnits()->end(); ++i)
1021 {
1022 if (validTarget(*i, true, true) &&
1023 _save->getTileEngine()->visible(_unit, (*i)->getTile()))
1024 {
1025 tally++;
1026 int dist = _save->getTileEngine()->distance(_unit->getPosition(), (*i)->getPosition());
1027 if (dist < _closestDist)
1028 {
1029 bool valid = false;
1030 if (_rifle || !_melee)
1031 {
1032 valid = _save->getTileEngine()->canTargetUnit(&origin, (*i)->getTile(), &target, _unit);
1033 }
1034 else
1035 {
1036 if (selectPointNearTarget(*i, _unit->getTimeUnits()))
1037 {
1038 int dir = _save->getTileEngine()->getDirectionTo(_attackAction->target, (*i)->getPosition());
1039 valid = _save->getTileEngine()->validMeleeRange(_attackAction->target, dir, _unit, *i, 0);
1040 }
1041 }
1042 if (valid)
1043 {
1044 _closestDist = dist;
1045 _aggroTarget = *i;
1046 }
1047 }
1048 }
1049 }
1050 if (_aggroTarget)
1051 {
1052 return tally;
1053 }
1054
1055 return 0;
1056}
1057
1058/**
1059 * Selects the nearest known living Xcom unit.

Callers

nothing calls this directly

Calls 11

getSightOriginVoxelMethod · 0.80
getUnitsMethod · 0.80
visibleMethod · 0.80
distanceMethod · 0.80
canTargetUnitMethod · 0.80
getTimeUnitsMethod · 0.80
getDirectionToMethod · 0.80
validMeleeRangeMethod · 0.80
getTileEngineMethod · 0.45
getTileMethod · 0.45
getPositionMethod · 0.45

Tested by

no test coverage detected