IBonusBearer.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. TConstBonusListPtr IBonusBearer::getBonusesFrom(BonusSource source) const
  37. {
  38. std::string cachingStr = "source_" + std::to_string(static_cast<int>(source));
  39. CSelector s = Selector::sourceTypeSel(source);
  40. return getBonuses(s, cachingStr);
  41. }
  42. TConstBonusListPtr IBonusBearer::getBonusesOfType(BonusType type) const
  43. {
  44. std::string cachingStr = "type_" + std::to_string(static_cast<int>(type));
  45. CSelector s = Selector::type()(type);
  46. return getBonuses(s, cachingStr);
  47. }
  48. TConstBonusListPtr IBonusBearer::getBonusesOfType(BonusType type, BonusSubtypeID subtype) const
  49. {
  50. std::string cachingStr = "type_" + std::to_string(static_cast<int>(type)) + "_" + subtype.toString();
  51. CSelector s = Selector::typeSubtype(type, subtype);
  52. return getBonuses(s, cachingStr);
  53. }
  54. int IBonusBearer::valOfBonuses(BonusType type) const
  55. {
  56. //This part is performance-critical
  57. std::string cachingStr = "type_" + std::to_string(static_cast<int>(type));
  58. CSelector s = Selector::type()(type);
  59. return valOfBonuses(s, cachingStr);
  60. }
  61. bool IBonusBearer::hasBonusOfType(BonusType type) const
  62. {
  63. //This part is performance-critical
  64. std::string cachingStr = "type_" + std::to_string(static_cast<int>(type));
  65. CSelector s = Selector::type()(type);
  66. return hasBonus(s, cachingStr);
  67. }
  68. int IBonusBearer::valOfBonuses(BonusType type, BonusSubtypeID subtype) const
  69. {
  70. //This part is performance-critical
  71. std::string cachingStr = "type_" + std::to_string(static_cast<int>(type)) + "_" + subtype.toString();
  72. CSelector s = Selector::typeSubtype(type, subtype);
  73. return valOfBonuses(s, cachingStr);
  74. }
  75. bool IBonusBearer::hasBonusOfType(BonusType type, BonusSubtypeID subtype) const
  76. {
  77. //This part is performance-critical
  78. std::string cachingStr = "type_" + std::to_string(static_cast<int>(type)) + "_" + subtype.toString();
  79. CSelector s = Selector::typeSubtype(type, subtype);
  80. return hasBonus(s, cachingStr);
  81. }
  82. bool IBonusBearer::hasBonusFrom(BonusSource source, BonusSourceID sourceID) const
  83. {
  84. std::string cachingStr = "source_" + std::to_string(static_cast<int>(source)) + "_" + std::to_string(sourceID.getNum());
  85. return hasBonus(Selector::source(source,sourceID), cachingStr);
  86. }
  87. bool IBonusBearer::hasBonusFrom(BonusSource source) const
  88. {
  89. std::string cachingStr = "source_" + std::to_string(static_cast<int>(source));
  90. return hasBonus((Selector::sourceTypeSel(source)), cachingStr);
  91. }
  92. std::shared_ptr<const Bonus> IBonusBearer::getBonus(const CSelector &selector) const
  93. {
  94. auto bonuses = getAllBonuses(selector, Selector::all);
  95. return bonuses->getFirst(Selector::all);
  96. }
  97. VCMI_LIB_NAMESPACE_END