VisitQueries.cpp 2.9 KB

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