File: TavernHeroesPool.cpp

package info (click to toggle)
vcmi 1.6.5%2Bdfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 32,060 kB
  • sloc: cpp: 238,971; python: 265; sh: 224; xml: 157; ansic: 78; objc: 61; makefile: 49
file content (148 lines) | stat: -rw-r--r-- 3,461 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
 * 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"

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->getHeroTypeID());

	return pool;
}

TavernSlotRole TavernHeroesPool::getSlotRole(HeroTypeID hero) const
{
	for (auto const & slot : currentTavern)
	{
		if (slot.hero->getHeroTypeID() == hero)
			return slot.role;
	}
	return TavernSlotRole::NONE;
}

void TavernHeroesPool::setHeroForPlayer(PlayerColor player, TavernHeroSlot slot, HeroTypeID hero, CSimpleArmy & army, TavernSlotRole role, bool replenishPoints)
{
	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);

	if (h && replenishPoints)
	{
		h->setMovementPoints(h->movementPointsLimit(true));
		h->mana = h->manaLimit();
	}

	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->getHeroTypeID() == 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->getManaNewTurn();
	}
}

void TavernHeroesPool::addHeroToPool(CGHeroInstance * hero)
{
	heroesPool[hero->getHeroTypeID()] = hero;
}

void TavernHeroesPool::setAvailability(HeroTypeID hero, std::set<PlayerColor> mask)
{
	perPlayerAvailability[hero] = mask;
}

VCMI_LIB_NAMESPACE_END