* 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. */
| 1010 | * @return viable targets. |
| 1011 | */ |
| 1012 | int 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. |
nothing calls this directly
no test coverage detected