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

Method checkAmmo

src/Savegame/BattleUnit.cpp:1794–1829  ·  view source on GitHub ↗

* Check if we have ammo and reload if needed (used for AI). * @return Do we have ammo? */

Source from the content-addressed store, hash-verified

1792 * @return Do we have ammo?
1793 */
1794bool 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.

Callers 2

findItemMethod · 0.80
btnReloadClickMethod · 0.80

Calls 7

getBattleTypeMethod · 0.80
getCompatibleAmmoMethod · 0.80
setAmmoItemMethod · 0.80
moveToOwnerMethod · 0.80
getAmmoItemMethod · 0.45
getRulesMethod · 0.45
getTypeMethod · 0.45

Tested by

no test coverage detected