CGResource.cpp 3.7 KB

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