IBonusBearer.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 "IBonusBearer.h"
  12. #include "BonusList.h"
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. int IBonusBearer::valOfBonuses(const CSelector &selector, const std::string &cachingStr) const
  15. {
  16. TConstBonusListPtr hlp = getAllBonuses(selector, nullptr, cachingStr);
  17. return hlp->totalValue();
  18. }
  19. bool IBonusBearer::hasBonus(const CSelector &selector, const std::string &cachingStr) const
  20. {
  21. //TODO: We don't need to count all bonuses and could break on first matching
  22. return !getBonuses(selector, cachingStr)->empty();
  23. }
  24. bool IBonusBearer::hasBonus(const CSelector &selector, const CSelector &limit, const std::string &cachingStr) const
  25. {
  26. return !getBonuses(selector, limit, cachingStr)->empty();
  27. }
  28. TConstBonusListPtr IBonusBearer::getBonuses(const CSelector &selector, const std::string &cachingStr) const
  29. {
  30. return getAllBonuses(selector, nullptr, cachingStr);
  31. }
  32. TConstBonusListPtr IBonusBearer::getBonuses(const CSelector &selector, const CSelector &limit, const std::string &cachingStr) const
  33. {
  34. return getAllBonuses(selector, limit, cachingStr);
  35. }
  36. int IBonusBearer::valOfBonuses(BonusType type) const
  37. {
  38. //This part is performance-critical
  39. std::string cachingStr = "type_" + std::to_string(static_cast<int>(type));
  40. CSelector s = Selector::type()(type);
  41. return valOfBonuses(s, cachingStr);
  42. }
  43. bool IBonusBearer::hasBonusOfType(BonusType type) const
  44. {
  45. //This part is performance-critical
  46. std::string cachingStr = "type_" + std::to_string(static_cast<int>(type));
  47. CSelector s = Selector::type()(type);
  48. return hasBonus(s, cachingStr);
  49. }
  50. int IBonusBearer::valOfBonuses(BonusType type, BonusSubtypeID subtype) const
  51. {
  52. //This part is performance-critical
  53. std::string cachingStr = "type_" + std::to_string(static_cast<int>(type)) + "_" + subtype.toString();
  54. CSelector s = Selector::typeSubtype(type, subtype);
  55. return valOfBonuses(s, cachingStr);
  56. }
  57. bool IBonusBearer::hasBonusOfType(BonusType type, BonusSubtypeID subtype) const
  58. {
  59. //This part is performance-critical
  60. std::string cachingStr = "type_" + std::to_string(static_cast<int>(type)) + "_" + subtype.toString();
  61. CSelector s = Selector::typeSubtype(type, subtype);
  62. return hasBonus(s, cachingStr);
  63. }
  64. bool IBonusBearer::hasBonusFrom(BonusSource source, BonusSourceID sourceID) const
  65. {
  66. return hasBonus(Selector::source(source,sourceID));
  67. }
  68. std::shared_ptr<const Bonus> IBonusBearer::getBonus(const CSelector &selector) const
  69. {
  70. auto bonuses = getAllBonuses(selector, Selector::all);
  71. return bonuses->getFirst(Selector::all);
  72. }
  73. VCMI_LIB_NAMESPACE_END