* Check if we have ammo and reload if needed (used for AI). * @return Do we have ammo? */
| 1792 | * @return Do we have ammo? |
| 1793 | */ |
| 1794 | bool BattleUnit::checkAmmo() |
| 1795 | { |
| 1796 | BattleItem *weapon = getItem("STR_RIGHT_HAND"); |
| 1797 | if (!weapon || weapon->getAmmoItem() != 0 || weapon->getRules()->getBattleType() == BT_MELEE || getTimeUnits() < 15) |
| 1798 | { |
| 1799 | weapon = getItem("STR_LEFT_HAND"); |
| 1800 | if (!weapon || weapon->getAmmoItem() != 0 || weapon->getRules()->getBattleType() == BT_MELEE || getTimeUnits() < 15) |
| 1801 | { |
| 1802 | return false; |
| 1803 | } |
| 1804 | } |
| 1805 | // we have a non-melee weapon with no ammo and 15 or more TUs - we might need to look for ammo then |
| 1806 | BattleItem *ammo = 0; |
| 1807 | bool wrong = true; |
| 1808 | for (std::vector<BattleItem*>::iterator i = getInventory()->begin(); i != getInventory()->end(); ++i) |
| 1809 | { |
| 1810 | ammo = (*i); |
| 1811 | for (std::vector<std::string>::iterator c = weapon->getRules()->getCompatibleAmmo()->begin(); c != weapon->getRules()->getCompatibleAmmo()->end(); ++c) |
| 1812 | { |
| 1813 | if ((*c) == ammo->getRules()->getType()) |
| 1814 | { |
| 1815 | wrong = false; |
| 1816 | break; |
| 1817 | } |
| 1818 | } |
| 1819 | if (!wrong) break; |
| 1820 | } |
| 1821 | |
| 1822 | if (wrong) return false; // didn't find any compatible ammo in inventory |
| 1823 | |
| 1824 | spendTimeUnits(15); |
| 1825 | weapon->setAmmoItem(ammo); |
| 1826 | ammo->moveToOwner(0); |
| 1827 | |
| 1828 | return true; |
| 1829 | } |
| 1830 | |
| 1831 | /** |
| 1832 | * Check if this unit is in the exit area. |
no test coverage detected