* Selects the next player unit in a certain direction. * @param dir Direction to select, eg. -1 for previous and 1 for next. * @param checkReselect Whether to check if we should reselect a unit. * @param setReselect Don't reselect a unit. * @param checkInventory Whether to check if the unit has an inventory. * @return Pointer to new selected BattleUnit, NULL if none can be selected. */
| 626 | * @return Pointer to new selected BattleUnit, NULL if none can be selected. |
| 627 | */ |
| 628 | BattleUnit *SavedBattleGame::selectPlayerUnit(int dir, bool checkReselect, bool setReselect, bool checkInventory) |
| 629 | { |
| 630 | if (_selectedUnit != 0 && setReselect) |
| 631 | { |
| 632 | _selectedUnit->dontReselect(); |
| 633 | } |
| 634 | if (_units.empty()) |
| 635 | { |
| 636 | return 0; |
| 637 | } |
| 638 | |
| 639 | std::vector<BattleUnit*>::iterator begin, end; |
| 640 | if (dir > 0) |
| 641 | { |
| 642 | begin = _units.begin(); |
| 643 | end = _units.end()-1; |
| 644 | } |
| 645 | else if (dir < 0) |
| 646 | { |
| 647 | begin = _units.end()-1; |
| 648 | end = _units.begin(); |
| 649 | } |
| 650 | |
| 651 | std::vector<BattleUnit*>::iterator i = std::find(_units.begin(), _units.end(), _selectedUnit); |
| 652 | do |
| 653 | { |
| 654 | // no unit selected |
| 655 | if (i == _units.end()) |
| 656 | { |
| 657 | i = begin; |
| 658 | continue; |
| 659 | } |
| 660 | if (i != end) |
| 661 | { |
| 662 | i += dir; |
| 663 | } |
| 664 | // reached the end, wrap-around |
| 665 | else |
| 666 | { |
| 667 | i = begin; |
| 668 | } |
| 669 | // back to where we started... no more units found |
| 670 | if (*i == _selectedUnit) |
| 671 | { |
| 672 | if (checkReselect && !_selectedUnit->reselectAllowed()) |
| 673 | _selectedUnit = 0; |
| 674 | return _selectedUnit; |
| 675 | } |
| 676 | else if (_selectedUnit == 0 && i == begin) |
| 677 | { |
| 678 | return _selectedUnit; |
| 679 | } |
| 680 | } |
| 681 | while (!(*i)->isSelectable(_side, checkReselect, checkInventory)); |
| 682 | |
| 683 | _selectedUnit = (*i); |
| 684 | return _selectedUnit; |
| 685 | } |
nothing calls this directly
no test coverage detected