| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- /*
- * TavernHeroesPool.cpp, part of VCMI engine
- *
- * Authors: listed in file AUTHORS in main folder
- *
- * License: GNU General Public License v2.0 or later
- * Full text of license available in license.txt file, in main folder
- *
- */
- #include "StdInc.h"
- #include "TavernHeroesPool.h"
- #include "../mapObjects/CGHeroInstance.h"
- #include "../CHeroHandler.h"
- VCMI_LIB_NAMESPACE_BEGIN
- TavernHeroesPool::~TavernHeroesPool()
- {
- for(const auto & ptr : heroesPool) // clean hero pool
- delete ptr.second;
- }
- std::map<HeroTypeID, CGHeroInstance*> TavernHeroesPool::unusedHeroesFromPool() const
- {
- std::map<HeroTypeID, CGHeroInstance*> pool = heroesPool;
- for(const auto & slot : currentTavern)
- pool.erase(slot.hero->getHeroType());
- return pool;
- }
- TavernSlotRole TavernHeroesPool::getSlotRole(HeroTypeID hero) const
- {
- for (auto const & slot : currentTavern)
- {
- if (slot.hero->getHeroType() == hero)
- return slot.role;
- }
- return TavernSlotRole::NONE;
- }
- void TavernHeroesPool::setHeroForPlayer(PlayerColor player, TavernHeroSlot slot, HeroTypeID hero, CSimpleArmy & army, TavernSlotRole role)
- {
- vstd::erase_if(currentTavern, [&](const TavernSlot & entry){
- return entry.player == player && entry.slot == slot;
- });
- if (hero == HeroTypeID::NONE)
- return;
- CGHeroInstance * h = heroesPool[hero];
- if (h && army)
- h->setToArmy(army);
- TavernSlot newSlot;
- newSlot.hero = h;
- newSlot.player = player;
- newSlot.role = role;
- newSlot.slot = slot;
- currentTavern.push_back(newSlot);
- boost::range::sort(currentTavern, [](const TavernSlot & left, const TavernSlot & right)
- {
- if (left.slot == right.slot)
- return left.player < right.player;
- else
- return left.slot < right.slot;
- });
- }
- bool TavernHeroesPool::isHeroAvailableFor(HeroTypeID hero, PlayerColor color) const
- {
- if (perPlayerAvailability.count(hero))
- return perPlayerAvailability.at(hero).count(color) != 0;
- return true;
- }
- std::vector<const CGHeroInstance *> TavernHeroesPool::getHeroesFor(PlayerColor color) const
- {
- std::vector<const CGHeroInstance *> result;
- for(const auto & slot : currentTavern)
- {
- if (slot.player == color)
- result.push_back(slot.hero);
- }
- return result;
- }
- CGHeroInstance * TavernHeroesPool::takeHeroFromPool(HeroTypeID hero)
- {
- assert(heroesPool.count(hero));
- CGHeroInstance * result = heroesPool[hero];
- heroesPool.erase(hero);
- vstd::erase_if(currentTavern, [&](const TavernSlot & entry){
- return entry.hero->type->getId() == hero;
- });
- assert(result);
- return result;
- }
- void TavernHeroesPool::onNewDay()
- {
- auto unusedHeroes = unusedHeroesFromPool();
- for(auto & hero : heroesPool)
- {
- assert(hero.second);
- if(!hero.second)
- continue;
- hero.second->removeBonusesRecursive(Bonus::OneDay);
- hero.second->reduceBonusDurations(Bonus::NDays);
- hero.second->reduceBonusDurations(Bonus::OneWeek);
- // do not access heroes who are not present in tavern of any players
- if (vstd::contains(unusedHeroes, hero.first))
- continue;
- hero.second->setMovementPoints(hero.second->movementPointsLimit(true));
- hero.second->mana = hero.second->manaLimit();
- }
- }
- void TavernHeroesPool::addHeroToPool(CGHeroInstance * hero)
- {
- heroesPool[hero->getHeroType()] = hero;
- }
- void TavernHeroesPool::setAvailability(HeroTypeID hero, std::set<PlayerColor> mask)
- {
- perPlayerAvailability[hero] = mask;
- }
- VCMI_LIB_NAMESPACE_END
|