HeroBonus.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #define VCMI_DLL
  2. #include "HeroBonus.h"
  3. int BonusList::valOfBonuses( HeroBonus::BonusType type, int subtype /*= -1*/ ) const /*subtype -> subtype of bonus, if -1 then any */
  4. {
  5. if(!this) //to avoid null-checking in maany places -> no bonus list means 0 bonus value
  6. return 0;
  7. int ret = 0;
  8. if(subtype == -1)
  9. {
  10. for(const_iterator i = begin(); i != end(); i++)
  11. if(i->type == type)
  12. ret += i->val;
  13. }
  14. else
  15. {
  16. for(const_iterator i = begin(); i != end(); i++)
  17. if(i->type == type && i->subtype == subtype)
  18. ret += i->val;
  19. }
  20. return ret;
  21. }
  22. bool BonusList::hasBonusOfType( HeroBonus::BonusType type, int subtype /*= -1*/ ) const
  23. {
  24. if(!this) //to avoid null-checking in maany places -> no bonus list means there is no searched bonus
  25. return 0;
  26. if(subtype == -1) //any subtype
  27. {
  28. for(const_iterator i = begin(); i != end(); i++)
  29. if(i->type == type)
  30. return true;
  31. }
  32. else //given subtype
  33. {
  34. for(const_iterator i = begin(); i != end(); i++)
  35. if(i->type == type && i->subtype == subtype)
  36. return true;
  37. }
  38. return false;
  39. }
  40. const HeroBonus * BonusList::getBonus( int from, int id ) const
  41. {
  42. if(!this) //to avoid null-checking in maany places -> no bonus list means bonus cannot be retreived
  43. return NULL;
  44. for (const_iterator i = begin(); i != end(); i++)
  45. if(i->source == from && i->id == id)
  46. return &*i;
  47. return NULL;
  48. }
  49. void BonusList::getModifiersWDescr( std::vector<std::pair<int,std::string> > &out, HeroBonus::BonusType type, int subtype /*= -1 */ ) const
  50. {
  51. if(!this) //to avoid null-checking in maany places -> no bonus list means nothing has to be done here
  52. return;
  53. if(subtype == -1)
  54. {
  55. for(const_iterator i = begin(); i != end(); i++)
  56. if(i->type == type)
  57. out.push_back(std::make_pair(i->val, i->description));
  58. }
  59. else
  60. {
  61. for(const_iterator i = begin(); i != end(); i++)
  62. if(i->type == type && i->subtype == subtype)
  63. out.push_back(std::make_pair(i->val, i->description));
  64. }
  65. }