TavernHeroesPool.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * TavernHeroesPool.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 "TavernHeroesPool.h"
  12. #include "../mapObjects/CGHeroInstance.h"
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. TavernHeroesPool::~TavernHeroesPool()
  15. {
  16. for(auto ptr : heroesPool) // clean hero pool
  17. delete ptr.second;
  18. }
  19. std::map<HeroTypeID, CGHeroInstance*> TavernHeroesPool::unusedHeroesFromPool() const
  20. {
  21. std::map<HeroTypeID, CGHeroInstance*> pool = heroesPool;
  22. for(const auto & player : currentTavern)
  23. for(auto availableHero : player.second)
  24. if(availableHero.second)
  25. pool.erase(HeroTypeID(availableHero.second->subID));
  26. return pool;
  27. }
  28. void TavernHeroesPool::setHeroForPlayer(PlayerColor player, TavernHeroSlot slot, HeroTypeID hero, CSimpleArmy & army)
  29. {
  30. currentTavern[player].erase(slot);
  31. if (hero == HeroTypeID::NONE)
  32. return;
  33. CGHeroInstance * h = heroesPool[hero];
  34. if (h && army)
  35. h->setToArmy(army);
  36. currentTavern[player][slot] = h;
  37. }
  38. bool TavernHeroesPool::isHeroAvailableFor(HeroTypeID hero, PlayerColor color) const
  39. {
  40. if (pavailable.count(hero))
  41. return pavailable.at(hero) & (1 << color.getNum());
  42. return true;
  43. }
  44. std::vector<const CGHeroInstance *> TavernHeroesPool::getHeroesFor(PlayerColor color) const
  45. {
  46. std::vector<const CGHeroInstance *> result;
  47. if(!currentTavern.count(color))
  48. return result;
  49. for(const auto & hero : currentTavern.at(color))
  50. result.push_back(hero.second);
  51. return result;
  52. }
  53. CGHeroInstance * TavernHeroesPool::takeHero(HeroTypeID hero)
  54. {
  55. assert(heroesPool.count(hero));
  56. CGHeroInstance * result = heroesPool[hero];
  57. heroesPool.erase(hero);
  58. assert(result);
  59. return result;
  60. }
  61. void TavernHeroesPool::onNewDay()
  62. {
  63. for(auto & hero : heroesPool)
  64. {
  65. assert(hero.second);
  66. if(!hero.second)
  67. continue;
  68. hero.second->setMovementPoints(hero.second->movementPointsLimit(true));
  69. hero.second->mana = hero.second->manaLimit();
  70. }
  71. }
  72. void TavernHeroesPool::addHeroToPool(CGHeroInstance * hero)
  73. {
  74. heroesPool[HeroTypeID(hero->subID)] = hero;
  75. }
  76. void TavernHeroesPool::setAvailability(HeroTypeID hero, PlayerColor::Mask mask)
  77. {
  78. pavailable[hero] = mask;
  79. }
  80. VCMI_LIB_NAMESPACE_END