IObjectInterface.cpp 4.1 KB

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