VisitQueries.cpp 3.0 KB

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