VisitQueries.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * VisitQueries.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 "VisitQueries.h"
  12. #include "../../lib/mapObjects/CGHeroInstance.h"
  13. #include "../../lib/mapObjects/CGTownInstance.h"
  14. #include "../../lib/mapObjects/TownBuildingInstance.h"
  15. #include "../CGameHandler.h"
  16. #include "QueriesProcessor.h"
  17. VisitQuery::VisitQuery(CGameHandler * owner, const CGObjectInstance * Obj, const CGHeroInstance * Hero)
  18. : CQuery(owner)
  19. , visitedObject(Obj)
  20. , visitingHero(Hero)
  21. {
  22. addPlayer(Hero->tempOwner);
  23. }
  24. bool VisitQuery::blocksPack(const CPack * pack) const
  25. {
  26. //During the visit itself ALL actions are blocked.
  27. //(However, the visit may trigger a query above that'll pass some.)
  28. return true;
  29. }
  30. void MapObjectVisitQuery::onExposure(QueryPtr topQuery)
  31. {
  32. //Object may have been removed and deleted.
  33. if(gh->isValidObject(visitedObject))
  34. topQuery->notifyObjectAboutRemoval(visitedObject, visitingHero);
  35. owner->popIfTop(*this);
  36. }
  37. MapObjectVisitQuery::MapObjectVisitQuery(CGameHandler * owner, const CGObjectInstance * Obj, const CGHeroInstance * Hero)
  38. : VisitQuery(owner, Obj, Hero)
  39. , removeObjectAfterVisit(false)
  40. {
  41. }
  42. void MapObjectVisitQuery::onRemoval(PlayerColor color)
  43. {
  44. gh->objectVisitEnded(visitingHero, players.front());
  45. //TODO or should it be destructor?
  46. //Can object visit affect 2 players and what would be desired behavior?
  47. if(removeObjectAfterVisit)
  48. gh->removeObject(visitedObject, color);
  49. }
  50. TownBuildingVisitQuery::TownBuildingVisitQuery(CGameHandler * owner, const CGTownInstance * Obj, std::vector<const CGHeroInstance *> heroes, std::vector<BuildingID> buildingToVisit)
  51. : VisitQuery(owner, Obj, heroes.front())
  52. , visitedTown(Obj)
  53. {
  54. // generate in reverse order - first building-hero pair to handle must be in the end of vector
  55. for (auto const * hero : boost::adaptors::reverse(heroes))
  56. for (auto const & building : boost::adaptors::reverse(buildingToVisit))
  57. visitedBuilding.push_back({ hero, building});
  58. }
  59. void TownBuildingVisitQuery::onExposure(QueryPtr topQuery)
  60. {
  61. topQuery->notifyObjectAboutRemoval(visitedObject, visitingHero);
  62. onAdded(players.front());
  63. }
  64. void TownBuildingVisitQuery::onAdded(PlayerColor color)
  65. {
  66. while (!visitedBuilding.empty() && owner->topQuery(color).get() == this)
  67. {
  68. visitingHero = visitedBuilding.back().hero;
  69. auto * building = visitedTown->rewardableBuildings.at(visitedBuilding.back().building);
  70. building->onHeroVisit(visitingHero);
  71. visitedBuilding.pop_back();
  72. }
  73. if (visitedBuilding.empty() && owner->topQuery(color).get() == this)
  74. owner->popIfTop(*this);
  75. }