IObjectInterface.cpp 4.2 KB

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