diff --git a/lib/CGameState.cpp b/lib/CGameState.cpp index a0fe53d7b..6bdbefa50 100644 --- a/lib/CGameState.cpp +++ b/lib/CGameState.cpp @@ -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(); diff --git a/lib/CGameState.h b/lib/CGameState.h index 373a8b3da..8b7ff10c7 100644 --- a/lib/CGameState.h +++ b/lib/CGameState.h @@ -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; diff --git a/server/CGameHandler.cpp b/server/CGameHandler.cpp index 471b7a25e..4c66aab41 100644 --- a/server/CGameHandler.cpp +++ b/server/CGameHandler.cpp @@ -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;