IObjectInterface.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * IObjectInterface.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 "IObjectInterface.h"
  12. #include "CGTownInstance.h"
  13. #include "MiscObjects.h"
  14. #include "../IGameCallback.h"
  15. #include "../TerrainHandler.h"
  16. #include "../mapObjects/CGHeroInstance.h"
  17. #include "../networkPacks/PacksForClient.h"
  18. VCMI_LIB_NAMESPACE_BEGIN
  19. void IObjectInterface::showInfoDialog(const ui32 txtID, const ui16 soundID, EInfoWindowMode mode) const
  20. {
  21. InfoWindow iw;
  22. iw.soundID = soundID;
  23. iw.player = getOwner();
  24. iw.type = mode;
  25. iw.text.appendLocalString(EMetaText::ADVOB_TXT,txtID);
  26. cb->sendAndApply(iw);
  27. }
  28. ///IObjectInterface
  29. void IObjectInterface::onHeroVisit(const CGHeroInstance * h) const
  30. {}
  31. void IObjectInterface::onHeroLeave(const CGHeroInstance * h) const
  32. {}
  33. void IObjectInterface::newTurn(vstd::RNG & rand) const
  34. {}
  35. void IObjectInterface::initObj(vstd::RNG & rand)
  36. {}
  37. void IObjectInterface::pickRandomObject(vstd::RNG & rand)
  38. {}
  39. void IObjectInterface::setProperty(ObjProperty what, ObjPropertyID identifier)
  40. {}
  41. bool IObjectInterface::wasVisited (PlayerColor player) const
  42. {
  43. return false;
  44. }
  45. bool IObjectInterface::wasVisited (const CGHeroInstance * h) const
  46. {
  47. return false;
  48. }
  49. void IObjectInterface::postInit()
  50. {}
  51. void IObjectInterface::preInit()
  52. {}
  53. void IObjectInterface::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
  54. {}
  55. void IObjectInterface::blockingDialogAnswered(const CGHeroInstance *hero, int32_t answer) const
  56. {}
  57. void IObjectInterface::garrisonDialogClosed(const CGHeroInstance *hero) const
  58. {}
  59. void IObjectInterface::heroLevelUpDone(const CGHeroInstance *hero) const
  60. {}
  61. int3 IBoatGenerator::bestLocation() const
  62. {
  63. std::vector<int3> offsets;
  64. getOutOffsets(offsets);
  65. for (auto & offset : offsets)
  66. {
  67. int3 targetTile = getObject()->visitablePos() + offset;
  68. const TerrainTile *tile = getObject()->cb->getTile(targetTile, false);
  69. if(!tile)
  70. continue; // tile not visible / outside the map
  71. if(!tile->isWater())
  72. continue;
  73. if (tile->blocked())
  74. {
  75. bool hasBoat = false;
  76. for (auto const & objectID : tile->blockingObjects)
  77. {
  78. const auto * object = getObject()->cb->getObj(objectID);
  79. if (object->ID == Obj::BOAT || object->ID == Obj::HERO)
  80. hasBoat = true;
  81. }
  82. if (!hasBoat)
  83. continue; // tile is blocked, but not by boat -> check next potential position
  84. }
  85. return targetTile;
  86. }
  87. return int3 (-1,-1,-1);
  88. }
  89. IBoatGenerator::EGeneratorState IBoatGenerator::shipyardStatus() const
  90. {
  91. int3 tile = bestLocation();
  92. if(!tile.isValid())
  93. return TILE_BLOCKED; //no available water
  94. const TerrainTile *t = getObject()->cb->getTile(tile);
  95. if(!t)
  96. return TILE_BLOCKED; //no available water
  97. if(t->blockingObjects.empty())
  98. return GOOD; //OK
  99. auto blockerObject = getObject()->cb->getObjInstance(t->blockingObjects.front());
  100. if(blockerObject->ID == Obj::BOAT || blockerObject->ID == Obj::HERO)
  101. return BOAT_ALREADY_BUILT; //blocked with boat
  102. return TILE_BLOCKED; //blocked
  103. }
  104. void IBoatGenerator::getProblemText(MetaString &out, const CGHeroInstance *visitor) const
  105. {
  106. switch(shipyardStatus())
  107. {
  108. case BOAT_ALREADY_BUILT:
  109. out.appendLocalString(EMetaText::GENERAL_TXT, 51);
  110. break;
  111. case TILE_BLOCKED:
  112. if(visitor)
  113. {
  114. out.appendLocalString(EMetaText::GENERAL_TXT, 134);
  115. out.replaceRawString(visitor->getNameTranslated());
  116. }
  117. else
  118. out.appendLocalString(EMetaText::ADVOB_TXT, 189);
  119. break;
  120. case NO_WATER:
  121. logGlobal->error("Shipyard without water at tile %s! ", getObject()->anchorPos().toString());
  122. return;
  123. }
  124. }
  125. void IShipyard::getBoatCost(TResources & cost) const
  126. {
  127. cost[EGameResID::WOOD] = 10;
  128. cost[EGameResID::GOLD] = 1000;
  129. }
  130. VCMI_LIB_NAMESPACE_END