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(CRandomGenerator & rand) const
  34. {}
  35. void IObjectInterface::initObj(CRandomGenerator & rand)
  36. {}
  37. void IObjectInterface::pickRandomObject(CRandomGenerator & 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, ui32 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->terType->isWater())
  72. continue;
  73. if (tile->blocked)
  74. {
  75. bool hasBoat = false;
  76. for (auto const * object : tile->blockingObjects)
  77. if (object->ID == Obj::BOAT || object->ID == Obj::HERO)
  78. hasBoat = true;
  79. if (!hasBoat)
  80. continue; // tile is blocked, but not by boat -> check next potential position
  81. }
  82. return targetTile;
  83. }
  84. return int3 (-1,-1,-1);
  85. }
  86. IBoatGenerator::EGeneratorState IBoatGenerator::shipyardStatus() const
  87. {
  88. int3 tile = bestLocation();
  89. if(!tile.valid())
  90. return TILE_BLOCKED; //no available water
  91. const TerrainTile *t = getObject()->cb->getTile(tile);
  92. if(!t)
  93. return TILE_BLOCKED; //no available water
  94. if(t->blockingObjects.empty())
  95. return GOOD; //OK
  96. if(t->blockingObjects.front()->ID == Obj::BOAT || t->blockingObjects.front()->ID == Obj::HERO)
  97. return BOAT_ALREADY_BUILT; //blocked with boat
  98. return TILE_BLOCKED; //blocked
  99. }
  100. void IBoatGenerator::getProblemText(MetaString &out, const CGHeroInstance *visitor) const
  101. {
  102. switch(shipyardStatus())
  103. {
  104. case BOAT_ALREADY_BUILT:
  105. out.appendLocalString(EMetaText::GENERAL_TXT, 51);
  106. break;
  107. case TILE_BLOCKED:
  108. if(visitor)
  109. {
  110. out.appendLocalString(EMetaText::GENERAL_TXT, 134);
  111. out.replaceRawString(visitor->getNameTranslated());
  112. }
  113. else
  114. out.appendLocalString(EMetaText::ADVOB_TXT, 189);
  115. break;
  116. case NO_WATER:
  117. logGlobal->error("Shipyard without water at tile %s! ", getObject()->getPosition().toString());
  118. return;
  119. }
  120. }
  121. void IShipyard::getBoatCost(TResources & cost) const
  122. {
  123. cost[EGameResID::WOOD] = 10;
  124. cost[EGameResID::GOLD] = 1000;
  125. }
  126. const IShipyard * IShipyard::castFrom( const CGObjectInstance *obj )
  127. {
  128. return dynamic_cast<const IShipyard *>(obj);
  129. }
  130. VCMI_LIB_NAMESPACE_END