IBonusBearer.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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, 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, nullptr, cachingStr);
  31. }
  32. TConstBonusListPtr IBonusBearer::getBonuses(const CSelector &selector, const CSelector &limit, const std::string &cachingStr) const
  33. {
  34. return getAllBonuses(selector, limit, nullptr, 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. boost::format fmt("source_%did_%s");
  67. fmt % static_cast<int>(source) % sourceID.toString();
  68. return hasBonus(Selector::source(source,sourceID), fmt.str());
  69. }
  70. std::shared_ptr<const Bonus> IBonusBearer::getBonus(const CSelector &selector) const
  71. {
  72. auto bonuses = getAllBonuses(selector, Selector::all);
  73. return bonuses->getFirst(Selector::all);
  74. }
  75. VCMI_LIB_NAMESPACE_END