1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-03-25 21:38:59 +02:00

Resolved bugs #76 and #168 which always gave same heroes in tavern.

This commit is contained in:
DjWarmonger 2009-11-15 14:06:25 +00:00
parent d18b6c3d5a
commit adf3c9fd4b
3 changed files with 15 additions and 12 deletions

View File

@ -873,7 +873,7 @@ CGHeroInstance * CGameState::HeroesPool::pickHeroFor(bool native, int player, co
if(pavailable.find(i->first)->second & 1<<player
&& i->second->type->heroType/2 == town->typeID)
{
pool.push_back(i->second);
pool.push_back(i->second); //get all avaliable heroes
}
}
if(!pool.size())
@ -895,7 +895,7 @@ CGHeroInstance * CGameState::HeroesPool::pickHeroFor(bool native, int player, co
if(pavailable.find(i->first)->second & 1<<player)
{
pool.push_back(i->second);
sum += i->second->type->heroClass->selectionProbability[town->typeID];
sum += i->second->type->heroClass->selectionProbability[town->typeID]; //total weight
}
}
if(!pool.size())
@ -905,11 +905,14 @@ CGHeroInstance * CGameState::HeroesPool::pickHeroFor(bool native, int player, co
}
r = rand()%sum;
for(unsigned int i=0; i<pool.size(); i++)
for (unsigned int i=0; i<pool.size(); i++)
{
r -= pool[i]->type->heroClass->selectionProbability[town->typeID];
if(r<0)
if(r < 0)
{
ret = pool[i];
break;
}
}
if(!ret)
ret = pool.back();

View File

@ -323,7 +323,7 @@ public:
struct DLL_EXPORT HeroesPool
{
std::map<ui32,CGHeroInstance *> heroesPool; //[subID] - heroes available to buy; NULL if not available
std::map<ui32,ui8> pavailable; // [subid] -> which players can recruit hero
std::map<ui32,ui8> pavailable; // [subid] -> which players can recruit hero (binary flags)
CGHeroInstance * pickHeroFor(bool native, int player, const CTown *town, std::map<ui32,CGHeroInstance *> &available) const;

View File

@ -2439,22 +2439,22 @@ bool CGameHandler::hireHero( ui32 tid, ui8 hid )
hr.tile = t->pos - int3(1,0,0);
sendAndApply(&hr);
std::map<ui32,CGHeroInstance *> pool = gs->hpool.heroesPool;
for ( std::map<ui8, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
for(std::vector<CGHeroInstance *>::iterator j = i->second.availableHeroes.begin(); j != i->second.availableHeroes.end(); j++)
if(*j)
pool.erase((**j).subID);
SetAvailableHeroes sah;
CGHeroInstance *h1 = gs->hpool.pickHeroFor(false,t->tempOwner,t->town, pool),
CGHeroInstance *h1 = gs->hpool.pickHeroFor(false,t->tempOwner,t->town, pool), //new hero
*h2 = gs->getPlayer(t->tempOwner)->availableHeroes[!hid];
(hid ? sah.hid2 : sah.hid1) = h1 ? h1->subID : -1;
(hid ? sah.hid1 : sah.hid2) = h2 ? h2->subID : -1;
sah.player = t->tempOwner;
sah.flags = hid+1;
//sah.flags = hid+1; //without army - why?
sendAndApply(&sah);
for ( std::map<ui8, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
for(std::vector<CGHeroInstance *>::iterator j = i->second.availableHeroes.begin(); j != i->second.availableHeroes.end(); j++)
if(*j)
pool.erase((**j).subID); //hero is removed from available lists
SetResource sr;
sr.player = t->tempOwner;
sr.resid = 6;