CGResource.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * MiscObjects.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 "CGResource.h"
  12. #include "../callback/IGameInfoCallback.h"
  13. #include "../callback/IGameEventCallback.h"
  14. #include "../callback/IGameRandomizer.h"
  15. #include "../mapObjectConstructors/CommonConstructors.h"
  16. #include "../texts/CGeneralTextHandler.h"
  17. #include "../networkPacks/PacksForClient.h"
  18. #include "../networkPacks/PacksForClientBattle.h"
  19. #include "../gameState/CGameState.h"
  20. #include "../serializer/JsonSerializeFormat.h"
  21. #include "../CSoundBase.h"
  22. #include <vstd/RNG.h>
  23. VCMI_LIB_NAMESPACE_BEGIN
  24. std::shared_ptr<ResourceInstanceConstructor> CGResource::getResourceHandler() const
  25. {
  26. const auto & baseHandler = getObjectHandler();
  27. const auto & ourHandler = std::dynamic_pointer_cast<ResourceInstanceConstructor>(baseHandler);
  28. return ourHandler;
  29. }
  30. int CGResource::getAmountMultiplier() const
  31. {
  32. return getResourceHandler()->getAmountMultiplier();
  33. }
  34. uint32_t CGResource::getAmount() const
  35. {
  36. return amount;
  37. }
  38. GameResID CGResource::resourceID() const
  39. {
  40. return getResourceHandler()->getResourceType();
  41. }
  42. std::string CGResource::getHoverText(PlayerColor player) const
  43. {
  44. return LIBRARY->generaltexth->restypes[resourceID().getNum()];
  45. }
  46. void CGResource::pickRandomObject(IGameRandomizer & gameRandomizer)
  47. {
  48. assert(ID == Obj::RESOURCE || ID == Obj::RANDOM_RESOURCE);
  49. if (ID == Obj::RANDOM_RESOURCE)
  50. {
  51. ID = Obj::RESOURCE;
  52. subID = gameRandomizer.getDefault().nextInt(EGameResID::WOOD, EGameResID::GOLD);
  53. setType(ID, subID);
  54. amount *= getAmountMultiplier();
  55. }
  56. }
  57. void CGResource::initObj(IGameRandomizer & gameRandomizer)
  58. {
  59. blockVisit = true;
  60. getResourceHandler()->randomizeObject(this, gameRandomizer);
  61. }
  62. void CGResource::onHeroVisit(IGameEventCallback & gameEvents, const CGHeroInstance * h) const
  63. {
  64. if(stacksCount())
  65. {
  66. if(!message.empty())
  67. {
  68. BlockingDialog ynd(true,false);
  69. ynd.player = h->getOwner();
  70. ynd.text = message;
  71. gameEvents.showBlockingDialog(this, &ynd);
  72. }
  73. else
  74. {
  75. blockingDialogAnswered(gameEvents, h, true); //behave as if player accepted battle
  76. }
  77. }
  78. else
  79. collectRes(gameEvents, h->getOwner());
  80. }
  81. void CGResource::collectRes(IGameEventCallback & gameEvents, const PlayerColor & player) const
  82. {
  83. gameEvents.giveResource(player, resourceID(), amount);
  84. InfoWindow sii;
  85. sii.player = player;
  86. if(!message.empty())
  87. {
  88. sii.type = EInfoWindowMode::AUTO;
  89. sii.text = message;
  90. }
  91. else
  92. {
  93. sii.type = EInfoWindowMode::INFO;
  94. sii.text.appendLocalString(EMetaText::ADVOB_TXT,113);
  95. sii.text.replaceName(resourceID());
  96. }
  97. sii.components.emplace_back(ComponentType::RESOURCE, resourceID(), amount);
  98. sii.soundID = soundBase::pickup01 + gameEvents.getRandomGenerator().nextInt(6);
  99. gameEvents.showInfoDialog(&sii);
  100. gameEvents.removeObject(this, player);
  101. }
  102. void CGResource::battleFinished(IGameEventCallback & gameEvents, const CGHeroInstance *hero, const BattleResult &result) const
  103. {
  104. if(result.winner == BattleSide::ATTACKER) //attacker won
  105. collectRes(gameEvents, hero->getOwner());
  106. }
  107. void CGResource::blockingDialogAnswered(IGameEventCallback & gameEvents, const CGHeroInstance *hero, int32_t answer) const
  108. {
  109. if(answer)
  110. gameEvents.startBattle(hero, this);
  111. }
  112. void CGResource::serializeJsonOptions(JsonSerializeFormat & handler)
  113. {
  114. CArmedInstance::serializeJsonOptions(handler);
  115. if(!handler.saving && !handler.getCurrent()["guards"].Vector().empty())
  116. CCreatureSet::serializeJson(handler, "guards", 7);
  117. handler.serializeInt("amount", amount, 0);
  118. handler.serializeStruct("guardMessage", message);
  119. }
  120. VCMI_LIB_NAMESPACE_END