|
@@ -10,19 +10,26 @@
|
|
|
#include "StdInc.h"
|
|
|
#include "TavernHeroesPool.h"
|
|
|
|
|
|
+#include "CGameState.h"
|
|
|
+
|
|
|
#include "../mapObjects/CGHeroInstance.h"
|
|
|
+#include "../mapping/CMap.h"
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
|
+TavernHeroesPool::TavernHeroesPool(CGameState * owner)
|
|
|
+ : owner(owner)
|
|
|
+{}
|
|
|
+
|
|
|
std::map<HeroTypeID, CGHeroInstance*> TavernHeroesPool::unusedHeroesFromPool() const
|
|
|
{
|
|
|
std::map<HeroTypeID, CGHeroInstance*> pool;
|
|
|
|
|
|
for (const auto & hero : heroesPool)
|
|
|
- pool[hero.first] = hero.second.get();
|
|
|
+ pool[hero] = owner->getMap().tryGetFromHeroPool(hero);
|
|
|
|
|
|
for(const auto & slot : currentTavern)
|
|
|
- pool.erase(slot.hero->getHeroTypeID());
|
|
|
+ pool.erase(slot.hero);
|
|
|
|
|
|
return pool;
|
|
|
}
|
|
@@ -31,7 +38,7 @@ TavernSlotRole TavernHeroesPool::getSlotRole(HeroTypeID hero) const
|
|
|
{
|
|
|
for (auto const & slot : currentTavern)
|
|
|
{
|
|
|
- if (slot.hero->getHeroTypeID() == hero)
|
|
|
+ if (slot.hero == hero)
|
|
|
return slot.role;
|
|
|
}
|
|
|
return TavernSlotRole::NONE;
|
|
@@ -46,7 +53,7 @@ void TavernHeroesPool::setHeroForPlayer(PlayerColor player, TavernHeroSlot slot,
|
|
|
if (hero == HeroTypeID::NONE)
|
|
|
return;
|
|
|
|
|
|
- auto h = heroesPool[hero];
|
|
|
+ auto h = owner->getMap().tryGetFromHeroPool(hero);
|
|
|
|
|
|
if (h && army)
|
|
|
h->setToArmy(army);
|
|
@@ -58,7 +65,7 @@ void TavernHeroesPool::setHeroForPlayer(PlayerColor player, TavernHeroSlot slot,
|
|
|
}
|
|
|
|
|
|
TavernSlot newSlot;
|
|
|
- newSlot.hero = h.get();
|
|
|
+ newSlot.hero = hero;
|
|
|
newSlot.player = player;
|
|
|
newSlot.role = role;
|
|
|
newSlot.slot = slot;
|
|
@@ -89,7 +96,7 @@ std::vector<const CGHeroInstance *> TavernHeroesPool::getHeroesFor(PlayerColor c
|
|
|
for(const auto & slot : currentTavern)
|
|
|
{
|
|
|
if (slot.player == color)
|
|
|
- result.push_back(slot.hero);
|
|
|
+ result.push_back(owner->getMap().tryGetFromHeroPool(slot.hero));
|
|
|
}
|
|
|
|
|
|
return result;
|
|
@@ -97,45 +104,41 @@ std::vector<const CGHeroInstance *> TavernHeroesPool::getHeroesFor(PlayerColor c
|
|
|
|
|
|
std::shared_ptr<CGHeroInstance> TavernHeroesPool::takeHeroFromPool(HeroTypeID hero)
|
|
|
{
|
|
|
- assert(heroesPool.count(hero));
|
|
|
-
|
|
|
- auto result = heroesPool[hero];
|
|
|
- heroesPool.erase(hero);
|
|
|
-
|
|
|
+ assert(vstd::contains(heroesPool, hero));
|
|
|
+ vstd::erase(heroesPool, hero);
|
|
|
vstd::erase_if(currentTavern, [&](const TavernSlot & entry){
|
|
|
- return entry.hero->getHeroTypeID() == hero;
|
|
|
+ return entry.hero == hero;
|
|
|
});
|
|
|
|
|
|
- assert(result);
|
|
|
- return result;
|
|
|
+ return owner->getMap().tryTakeFromHeroPool(hero);;
|
|
|
}
|
|
|
|
|
|
void TavernHeroesPool::onNewDay()
|
|
|
{
|
|
|
auto unusedHeroes = unusedHeroesFromPool();
|
|
|
|
|
|
- for(auto & hero : heroesPool)
|
|
|
+ for(auto & heroID : heroesPool)
|
|
|
{
|
|
|
- assert(hero.second);
|
|
|
- if(!hero.second)
|
|
|
- continue;
|
|
|
+ auto heroPtr = owner->getMap().tryGetFromHeroPool(heroID);
|
|
|
+ assert(heroPtr);
|
|
|
|
|
|
- hero.second->removeBonusesRecursive(Bonus::OneDay);
|
|
|
- hero.second->reduceBonusDurations(Bonus::NDays);
|
|
|
- hero.second->reduceBonusDurations(Bonus::OneWeek);
|
|
|
+ heroPtr->removeBonusesRecursive(Bonus::OneDay);
|
|
|
+ heroPtr->reduceBonusDurations(Bonus::NDays);
|
|
|
+ heroPtr->reduceBonusDurations(Bonus::OneWeek);
|
|
|
|
|
|
// do not access heroes who are not present in tavern of any players
|
|
|
- if (vstd::contains(unusedHeroes, hero.first))
|
|
|
+ if (vstd::contains(unusedHeroes, heroID))
|
|
|
continue;
|
|
|
|
|
|
- hero.second->setMovementPoints(hero.second->movementPointsLimit(true));
|
|
|
- hero.second->mana = hero.second->getManaNewTurn();
|
|
|
+ heroPtr->setMovementPoints(heroPtr->movementPointsLimit(true));
|
|
|
+ heroPtr->mana = heroPtr->getManaNewTurn();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-void TavernHeroesPool::addHeroToPool(std::shared_ptr<CGHeroInstance> hero)
|
|
|
+void TavernHeroesPool::addHeroToPool(HeroTypeID hero)
|
|
|
{
|
|
|
- heroesPool[hero->getHeroTypeID()] = hero;
|
|
|
+ assert(!vstd::contains(heroesPool, hero));
|
|
|
+ heroesPool.push_back(hero);
|
|
|
}
|
|
|
|
|
|
void TavernHeroesPool::setAvailability(HeroTypeID hero, std::set<PlayerColor> mask)
|