VisitQueries.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 CPackForServer * 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. //Can object visit affect 2 players and what would be desired behavior?
  46. if(removeObjectAfterVisit)
  47. gh->removeObject(visitedObject, color);
  48. }
  49. TownBuildingVisitQuery::TownBuildingVisitQuery(CGameHandler * owner, const CGTownInstance * Obj, std::vector<const CGHeroInstance *> heroes, std::vector<BuildingID> buildingToVisit)
  50. : VisitQuery(owner, Obj, heroes.front())
  51. , visitedTown(Obj)
  52. {
  53. // generate in reverse order - first building-hero pair to handle must be in the end of vector
  54. for (auto const * hero : boost::adaptors::reverse(heroes))
  55. for (auto const & building : boost::adaptors::reverse(buildingToVisit))
  56. visitedBuilding.push_back({ hero, building});
  57. }
  58. void TownBuildingVisitQuery::onExposure(QueryPtr topQuery)
  59. {
  60. topQuery->notifyObjectAboutRemoval(visitedObject, visitingHero);
  61. onAdded(players.front());
  62. }
  63. void TownBuildingVisitQuery::onAdded(PlayerColor color)
  64. {
  65. while (!visitedBuilding.empty() && owner->topQuery(color).get() == this)
  66. {
  67. visitingHero = visitedBuilding.back().hero;
  68. const auto * building = visitedTown->rewardableBuildings.at(visitedBuilding.back().building);
  69. building->onHeroVisit(visitingHero);
  70. visitedBuilding.pop_back();
  71. }
  72. if (visitedBuilding.empty() && owner->topQuery(color).get() == this)
  73. owner->popIfTop(*this);
  74. }