PrisonHeroPlacer.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * PrisonHeroPlacer.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 "PrisonHeroPlacer.h"
  12. #include "../CMapGenerator.h"
  13. #include "../RmgMap.h"
  14. #include "TreasurePlacer.h"
  15. #include "../CZonePlacer.h"
  16. #include "../../GameLibrary.h"
  17. #include "../../mapObjectConstructors/AObjectTypeHandler.h"
  18. #include "../../mapObjectConstructors/CObjectClassesHandler.h"
  19. #include "../../mapObjects/MapObjects.h"
  20. #include <vstd/RNG.h>
  21. VCMI_LIB_NAMESPACE_BEGIN
  22. void PrisonHeroPlacer::process()
  23. {
  24. getAllowedHeroes();
  25. }
  26. void PrisonHeroPlacer::init()
  27. {
  28. // Reserve at least 16 heroes for each player
  29. reservedHeroes = 16 * generator.getMapGenOptions().getHumanOrCpuPlayerCount();
  30. }
  31. void PrisonHeroPlacer::getAllowedHeroes()
  32. {
  33. // TODO: Give each zone unique HeroPlacer with private hero list?
  34. // Call that only once
  35. if (allowedHeroes.empty())
  36. {
  37. allowedHeroes = generator.getAllPossibleHeroes();
  38. }
  39. }
  40. int PrisonHeroPlacer::getPrisonsRemaining() const
  41. {
  42. return std::max<int>(allowedHeroes.size() - reservedHeroes, 0);
  43. }
  44. HeroTypeID PrisonHeroPlacer::drawRandomHero()
  45. {
  46. RecursiveLock lock(externalAccessMutex);
  47. if (getPrisonsRemaining() > 0)
  48. {
  49. RandomGeneratorUtil::randomShuffle(allowedHeroes, zone.getRand());
  50. HeroTypeID ret = allowedHeroes.back();
  51. allowedHeroes.pop_back();
  52. return ret;
  53. }
  54. else
  55. {
  56. throw rmgException("No unused heroes left for prisons!");
  57. }
  58. }
  59. void PrisonHeroPlacer::restoreDrawnHero(const HeroTypeID & hid)
  60. {
  61. RecursiveLock lock(externalAccessMutex);
  62. allowedHeroes.push_back(hid);
  63. }
  64. VCMI_LIB_NAMESPACE_END