2
0

IBonusBearer.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * IBonusBearer.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CBonusSystemNode.h"
  12. #include "BonusList.h"
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. int IBonusBearer::valOfBonuses(BonusType type, int subtype) const
  15. {
  16. //This part is performance-critical
  17. std::string cachingStr = "type_" + std::to_string(static_cast<int>(type)) + "_" + std::to_string(subtype);
  18. CSelector s = Selector::type()(type);
  19. if(subtype != -1)
  20. s = s.And(Selector::subtype()(subtype));
  21. return valOfBonuses(s, cachingStr);
  22. }
  23. int IBonusBearer::valOfBonuses(const CSelector &selector, const std::string &cachingStr) const
  24. {
  25. CSelector limit = nullptr;
  26. TConstBonusListPtr hlp = getAllBonuses(selector, limit, nullptr, cachingStr);
  27. return hlp->totalValue();
  28. }
  29. bool IBonusBearer::hasBonus(const CSelector &selector, const std::string &cachingStr) const
  30. {
  31. //TODO: We don't need to count all bonuses and could break on first matching
  32. return getBonuses(selector, cachingStr)->size() > 0;
  33. }
  34. bool IBonusBearer::hasBonus(const CSelector &selector, const CSelector &limit, const std::string &cachingStr) const
  35. {
  36. return getBonuses(selector, limit, cachingStr)->size() > 0;
  37. }
  38. bool IBonusBearer::hasBonusOfType(BonusType type, int subtype) const
  39. {
  40. //This part is performance-ciritcal
  41. std::string cachingStr = "type_" + std::to_string(static_cast<int>(type)) + "_" + std::to_string(subtype);
  42. CSelector s = Selector::type()(type);
  43. if(subtype != -1)
  44. s = s.And(Selector::subtype()(subtype));
  45. return hasBonus(s, cachingStr);
  46. }
  47. TConstBonusListPtr IBonusBearer::getBonuses(const CSelector &selector, const std::string &cachingStr) const
  48. {
  49. return getAllBonuses(selector, nullptr, nullptr, cachingStr);
  50. }
  51. TConstBonusListPtr IBonusBearer::getBonuses(const CSelector &selector, const CSelector &limit, const std::string &cachingStr) const
  52. {
  53. return getAllBonuses(selector, limit, nullptr, cachingStr);
  54. }
  55. bool IBonusBearer::hasBonusFrom(BonusSource source, ui32 sourceID) const
  56. {
  57. boost::format fmt("source_%did_%d");
  58. fmt % static_cast<int>(source) % sourceID;
  59. return hasBonus(Selector::source(source,sourceID), fmt.str());
  60. }
  61. std::shared_ptr<const Bonus> IBonusBearer::getBonus(const CSelector &selector) const
  62. {
  63. auto bonuses = getAllBonuses(selector, Selector::all);
  64. return bonuses->getFirst(Selector::all);
  65. }
  66. VCMI_LIB_NAMESPACE_END