HeroPoolProcessor.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * HeroPoolProcessor.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "HeroPoolProcessor.h"
  12. #include "CGameHandler.h"
  13. #include "../lib/CHeroHandler.h"
  14. #include "../lib/CPlayerState.h"
  15. #include "../lib/GameSettings.h"
  16. #include "../lib/NetPacks.h"
  17. #include "../lib/StartInfo.h"
  18. #include "../lib/mapObjects/CGTownInstance.h"
  19. #include "../lib/gameState/CGameState.h"
  20. #include "../lib/gameState/TavernHeroesPool.h"
  21. HeroPoolProcessor::HeroPoolProcessor() = default;
  22. HeroPoolProcessor::HeroPoolProcessor(CGameHandler * gameHandler):
  23. gameHandler(gameHandler)
  24. {
  25. }
  26. void HeroPoolProcessor::onHeroSurrendered(const PlayerColor & color, const CGHeroInstance * hero)
  27. {
  28. SetAvailableHero sah;
  29. sah.slotID = 0;
  30. sah.player = color;
  31. sah.hid = hero->subID;
  32. sah.army.clear();
  33. sah.army.setCreature(SlotID(0), hero->type->initialArmy.at(0).creature, 1);
  34. gameHandler->sendAndApply(&sah);
  35. }
  36. void HeroPoolProcessor::onHeroEscaped(const PlayerColor & color, const CGHeroInstance * hero)
  37. {
  38. SetAvailableHero sah;
  39. sah.slotID = 0;
  40. sah.player = color;
  41. sah.hid = hero->subID;
  42. gameHandler->sendAndApply(&sah);
  43. }
  44. void HeroPoolProcessor::clearHeroFromSlot(const PlayerColor & color, TavernHeroSlot slot)
  45. {
  46. SetAvailableHero sah;
  47. sah.player = color;
  48. sah.slotID = static_cast<int>(slot);
  49. sah.hid = HeroTypeID::NONE;
  50. gameHandler->sendAndApply(&sah);
  51. }
  52. void HeroPoolProcessor::selectNewHeroForSlot(const PlayerColor & color, TavernHeroSlot slot, bool needNativeHero, bool giveArmy)
  53. {
  54. SetAvailableHero sah;
  55. sah.player = color;
  56. sah.slotID = static_cast<int>(slot);
  57. //first hero - native if possible, second hero -> any other class
  58. CGHeroInstance *h = pickHeroFor(needNativeHero, color, gameHandler->getPlayerSettings(color)->castle, gameHandler->getRandomGenerator(), nullptr);
  59. if (h)
  60. {
  61. sah.hid = h->subID;
  62. if (giveArmy)
  63. {
  64. h->initArmy(gameHandler->getRandomGenerator(), &sah.army);
  65. }
  66. else
  67. {
  68. sah.army.clear();
  69. sah.army.setCreature(SlotID(0), h->type->initialArmy[0].creature, 1);
  70. }
  71. }
  72. else
  73. {
  74. sah.hid = -1;
  75. }
  76. gameHandler->sendAndApply(&sah);
  77. }
  78. void HeroPoolProcessor::onNewWeek(const PlayerColor & color)
  79. {
  80. clearHeroFromSlot(color, TavernHeroSlot::NATIVE);
  81. clearHeroFromSlot(color, TavernHeroSlot::RANDOM);
  82. selectNewHeroForSlot(color, TavernHeroSlot::NATIVE, true, true);
  83. selectNewHeroForSlot(color, TavernHeroSlot::RANDOM, false, true);
  84. }
  85. bool HeroPoolProcessor::hireHero(const CGObjectInstance *obj, const HeroTypeID & heroToRecruit, const PlayerColor & player)
  86. {
  87. const PlayerState * playerState = gameHandler->getPlayerState(player);
  88. const CGTownInstance * town = gameHandler->getTown(obj->id);
  89. if (playerState->resources[EGameResID::GOLD] < GameConstants::HERO_GOLD_COST && gameHandler->complain("Not enough gold for buying hero!"))
  90. return false;
  91. if (gameHandler->getHeroCount(player, false) >= VLC->settings()->getInteger(EGameSettings::HEROES_PER_PLAYER_ON_MAP_CAP) && gameHandler->complain("Cannot hire hero, too many wandering heroes already!"))
  92. return false;
  93. if (gameHandler->getHeroCount(player, true) >= VLC->settings()->getInteger(EGameSettings::HEROES_PER_PLAYER_TOTAL_CAP) && gameHandler->complain("Cannot hire hero, too many heroes garrizoned and wandering already!"))
  94. return false;
  95. if(town) //tavern in town
  96. {
  97. if(!town->hasBuilt(BuildingID::TAVERN) && gameHandler->complain("No tavern!"))
  98. return false;
  99. if(town->visitingHero && gameHandler->complain("There is visiting hero - no place!"))
  100. return false;
  101. }
  102. if(obj->ID == Obj::TAVERN)
  103. {
  104. if(gameHandler->getTile(obj->visitablePos())->visitableObjects.back() != obj && gameHandler->complain("Tavern entry must be unoccupied!"))
  105. return false;
  106. }
  107. auto recruitableHeroes = gameHandler->gameState()->hpool->getHeroesFor(player);
  108. const CGHeroInstance * recruitedHero = nullptr;
  109. for(const auto & hero : recruitableHeroes)
  110. {
  111. if(hero->subID == heroToRecruit)
  112. recruitedHero = hero;
  113. }
  114. if(!recruitedHero)
  115. {
  116. gameHandler->complain("Hero is not available for hiring!");
  117. return false;
  118. }
  119. HeroRecruited hr;
  120. hr.tid = obj->id;
  121. hr.hid = recruitedHero->subID;
  122. hr.player = player;
  123. hr.tile = recruitedHero->convertFromVisitablePos(obj->visitablePos());
  124. if(gameHandler->getTile(hr.tile)->isWater())
  125. {
  126. //Create a new boat for hero
  127. gameHandler->createObject(obj->visitablePos(), Obj::BOAT, recruitedHero->getBoatType().getNum());
  128. hr.boatId = gameHandler->getTopObj(hr.tile)->id;
  129. }
  130. // apply netpack -> this will remove hired hero from tavern slot
  131. gameHandler->sendAndApply(&hr);
  132. if(recruitableHeroes[0] == recruitedHero)
  133. selectNewHeroForSlot(player, TavernHeroSlot::NATIVE, false, false);
  134. else
  135. selectNewHeroForSlot(player, TavernHeroSlot::RANDOM, false, false);
  136. gameHandler->giveResource(player, EGameResID::GOLD, -GameConstants::HERO_GOLD_COST);
  137. if(town)
  138. {
  139. gameHandler->visitCastleObjects(town, recruitedHero);
  140. gameHandler->giveSpells(town, recruitedHero);
  141. }
  142. return true;
  143. }
  144. CGHeroInstance * HeroPoolProcessor::pickHeroFor(bool isNative,
  145. const PlayerColor & player,
  146. const FactionID & factionID,
  147. CRandomGenerator & rand,
  148. const CHeroClass * bannedClass) const
  149. {
  150. if(player >= PlayerColor::PLAYER_LIMIT)
  151. {
  152. logGlobal->error("Cannot pick hero for player %d. Wrong owner!", player.getStr());
  153. return nullptr;
  154. }
  155. const auto & hpool = gameHandler->gameState()->hpool;
  156. if(isNative)
  157. {
  158. std::vector<CGHeroInstance *> pool;
  159. for(auto & elem : hpool->unusedHeroesFromPool())
  160. {
  161. //get all available heroes
  162. bool heroAvailable = hpool->isHeroAvailableFor(elem.first, player);
  163. bool heroClassNative = elem.second->type->heroClass->faction == factionID;
  164. if(heroAvailable && heroClassNative)
  165. pool.push_back(elem.second);
  166. }
  167. if(!pool.empty())
  168. return *RandomGeneratorUtil::nextItem(pool, rand);
  169. logGlobal->error("Cannot pick native hero for %s. Picking any...", player.getStr());
  170. }
  171. std::vector<CGHeroInstance *> pool;
  172. int totalWeight = 0;
  173. for(auto & elem : hpool->unusedHeroesFromPool())
  174. {
  175. bool heroAvailable = hpool->isHeroAvailableFor(elem.first, player);
  176. bool heroClassBanned = bannedClass && elem.second->type->heroClass == bannedClass;
  177. if(heroAvailable && !heroClassBanned)
  178. {
  179. pool.push_back(elem.second);
  180. totalWeight += elem.second->type->heroClass->selectionProbability[factionID]; //total weight
  181. }
  182. }
  183. if(pool.empty() || totalWeight == 0)
  184. {
  185. logGlobal->error("There are no heroes available for player %s!", player.getStr());
  186. return nullptr;
  187. }
  188. int roll = rand.nextInt(totalWeight - 1);
  189. for(auto & elem : pool)
  190. {
  191. roll -= elem->type->heroClass->selectionProbability[factionID];
  192. if(roll < 0)
  193. {
  194. return elem;
  195. }
  196. }
  197. return pool.back();
  198. }