FlaggableMapObject.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * FlaggableMapObject.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 "FlaggableMapObject.h"
  12. #include "../IGameCallback.h"
  13. #include "CGHeroInstance.h"
  14. #include "../networkPacks/PacksForClient.h"
  15. #include "../mapObjectConstructors/FlaggableInstanceConstructor.h"
  16. VCMI_LIB_NAMESPACE_BEGIN
  17. const IOwnableObject * FlaggableMapObject::asOwnable() const
  18. {
  19. return this;
  20. }
  21. ResourceSet FlaggableMapObject::dailyIncome() const
  22. {
  23. return {};
  24. }
  25. std::vector<CreatureID> FlaggableMapObject::providedCreatures() const
  26. {
  27. return {};
  28. }
  29. void FlaggableMapObject::onHeroVisit( const CGHeroInstance * h ) const
  30. {
  31. if (cb->getPlayerRelations(h->getOwner(), getOwner()) != PlayerRelations::ENEMIES)
  32. return; // H3 behavior - revisiting owned Lighthouse is a no-op
  33. if (getOwner().isValidPlayer())
  34. takeBonusFrom(getOwner());
  35. cb->setOwner(this, h->getOwner()); //not ours? flag it!
  36. InfoWindow iw;
  37. iw.player = h->getOwner();
  38. iw.text.appendTextID(getFlaggableHandler()->getVisitMessageTextID());
  39. cb->showInfoDialog(&iw);
  40. giveBonusTo(h->getOwner());
  41. }
  42. void FlaggableMapObject::initObj(vstd::RNG & rand)
  43. {
  44. if(getOwner().isValidPlayer())
  45. {
  46. // FIXME: This is dirty hack
  47. giveBonusTo(getOwner(), true);
  48. }
  49. }
  50. std::shared_ptr<FlaggableInstanceConstructor> FlaggableMapObject::getFlaggableHandler() const
  51. {
  52. return std::dynamic_pointer_cast<FlaggableInstanceConstructor>(getObjectHandler());
  53. }
  54. void FlaggableMapObject::giveBonusTo(const PlayerColor & player, bool onInit) const
  55. {
  56. for (auto const & bonus : getFlaggableHandler()->getProvidedBonuses())
  57. {
  58. GiveBonus gb(GiveBonus::ETarget::PLAYER);
  59. gb.id = player;
  60. gb.bonus = *bonus;
  61. // FIXME: better place for this code?
  62. gb.bonus.duration = BonusDuration::PERMANENT;
  63. gb.bonus.source = BonusSource::OBJECT_INSTANCE;
  64. gb.bonus.sid = BonusSourceID(id);
  65. // FIXME: This is really dirty hack
  66. // Proper fix would be to make FlaggableMapObject into bonus system node
  67. // Unfortunately this will cause saves breakage
  68. if(onInit)
  69. gb.applyGs(cb->gameState());
  70. else
  71. cb->sendAndApply(gb);
  72. }
  73. }
  74. void FlaggableMapObject::takeBonusFrom(const PlayerColor & player) const
  75. {
  76. RemoveBonus rb(GiveBonus::ETarget::PLAYER);
  77. rb.whoID = player;
  78. rb.source = BonusSource::OBJECT_INSTANCE;
  79. rb.id = BonusSourceID(id);
  80. cb->sendAndApply(rb);
  81. }
  82. void FlaggableMapObject::serializeJsonOptions(JsonSerializeFormat& handler)
  83. {
  84. serializeJsonOwner(handler);
  85. }
  86. VCMI_LIB_NAMESPACE_END