IBonusBearer.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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, std::optional<int> subtype) const
  15. {
  16. //This part is performance-critical
  17. std::string cachingStr = "type_" + std::to_string(static_cast<int>(type)) + (subtype ? "_" + std::to_string(*subtype) : "");
  18. CSelector s = Selector::type()(type);
  19. if(subtype)
  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. TConstBonusListPtr hlp = getAllBonuses(selector, nullptr, nullptr, cachingStr);
  26. return hlp->totalValue();
  27. }
  28. bool IBonusBearer::hasBonus(const CSelector &selector, const std::string &cachingStr) const
  29. {
  30. //TODO: We don't need to count all bonuses and could break on first matching
  31. return getBonuses(selector, cachingStr)->size() > 0;
  32. }
  33. bool IBonusBearer::hasBonus(const CSelector &selector, const CSelector &limit, const std::string &cachingStr) const
  34. {
  35. return getBonuses(selector, limit, cachingStr)->size() > 0;
  36. }
  37. bool IBonusBearer::hasBonusOfType(BonusType type, std::optional<int> subtype) const
  38. {
  39. //This part is performance-ciritcal
  40. std::string cachingStr = "type_" + std::to_string(static_cast<int>(type)) + (subtype ? "_" + std::to_string(*subtype) : "");
  41. CSelector s = Selector::type()(type);
  42. if(subtype)
  43. s = s.And(Selector::subtype()(*subtype));
  44. return hasBonus(s, cachingStr);
  45. }
  46. TConstBonusListPtr IBonusBearer::getBonuses(const CSelector &selector, const std::string &cachingStr) const
  47. {
  48. return getAllBonuses(selector, nullptr, nullptr, cachingStr);
  49. }
  50. TConstBonusListPtr IBonusBearer::getBonuses(const CSelector &selector, const CSelector &limit, const std::string &cachingStr) const
  51. {
  52. return getAllBonuses(selector, limit, nullptr, cachingStr);
  53. }
  54. bool IBonusBearer::hasBonusFrom(BonusSource source, ui32 sourceID) const
  55. {
  56. boost::format fmt("source_%did_%d");
  57. fmt % static_cast<int>(source) % sourceID;
  58. return hasBonus(Selector::source(source,sourceID), fmt.str());
  59. }
  60. std::shared_ptr<const Bonus> IBonusBearer::getBonus(const CSelector &selector) const
  61. {
  62. auto bonuses = getAllBonuses(selector, Selector::all);
  63. return bonuses->getFirst(Selector::all);
  64. }
  65. VCMI_LIB_NAMESPACE_END