IObjectInterface.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. IGameCallback * IObjectInterface::cb = nullptr;
  20. void IObjectInterface::showInfoDialog(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. IObjectInterface::cb->sendAndApply(&iw);
  28. }
  29. ///IObjectInterface
  30. void IObjectInterface::onHeroVisit(const CGHeroInstance * h) const
  31. {}
  32. void IObjectInterface::onHeroLeave(const CGHeroInstance * h) const
  33. {}
  34. void IObjectInterface::newTurn(CRandomGenerator & rand) const
  35. {}
  36. void IObjectInterface::initObj(CRandomGenerator & rand)
  37. {}
  38. void IObjectInterface::setProperty( ui8 what, ui32 val )
  39. {}
  40. bool IObjectInterface::wasVisited (PlayerColor player) const
  41. {
  42. return false;
  43. }
  44. bool IObjectInterface::wasVisited (const CGHeroInstance * h) const
  45. {
  46. return false;
  47. }
  48. void IObjectInterface::postInit()
  49. {}
  50. void IObjectInterface::preInit()
  51. {}
  52. void IObjectInterface::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
  53. {}
  54. void IObjectInterface::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  55. {}
  56. void IObjectInterface::garrisonDialogClosed(const CGHeroInstance *hero) const
  57. {}
  58. void IObjectInterface::heroLevelUpDone(const CGHeroInstance *hero) const
  59. {}
  60. int3 IBoatGenerator::bestLocation() const
  61. {
  62. std::vector<int3> offsets;
  63. getOutOffsets(offsets);
  64. for (auto & offset : offsets)
  65. {
  66. int3 targetTile = getObject()->visitablePos() + offset;
  67. const TerrainTile *tile = getObject()->cb->getTile(targetTile, false);
  68. if(tile) //tile is in the map
  69. {
  70. if(tile->terType->isWater() && (!tile->blocked || tile->blockingObjects.front()->ID == Obj::BOAT)) //and is water and is not blocked or is blocked by boat
  71. return targetTile;
  72. }
  73. }
  74. return int3 (-1,-1,-1);
  75. }
  76. IBoatGenerator::EGeneratorState IBoatGenerator::shipyardStatus() const
  77. {
  78. int3 tile = bestLocation();
  79. if(!tile.valid())
  80. return TILE_BLOCKED; //no available water
  81. const TerrainTile *t = IObjectInterface::cb->getTile(tile);
  82. if(!t)
  83. return TILE_BLOCKED; //no available water
  84. if(t->blockingObjects.empty())
  85. return GOOD; //OK
  86. if(t->blockingObjects.front()->ID == Obj::BOAT)
  87. return BOAT_ALREADY_BUILT; //blocked with boat
  88. return TILE_BLOCKED; //blocked
  89. }
  90. void IBoatGenerator::getProblemText(MetaString &out, const CGHeroInstance *visitor) const
  91. {
  92. switch(shipyardStatus())
  93. {
  94. case BOAT_ALREADY_BUILT:
  95. out.appendLocalString(EMetaText::GENERAL_TXT, 51);
  96. break;
  97. case TILE_BLOCKED:
  98. if(visitor)
  99. {
  100. out.appendLocalString(EMetaText::GENERAL_TXT, 134);
  101. out.replaceRawString(visitor->getNameTranslated());
  102. }
  103. else
  104. out.appendLocalString(EMetaText::ADVOB_TXT, 189);
  105. break;
  106. case NO_WATER:
  107. logGlobal->error("Shipyard without water at tile %s! ", getObject()->getPosition().toString());
  108. return;
  109. }
  110. }
  111. void IShipyard::getBoatCost(TResources & cost) const
  112. {
  113. cost[EGameResID::WOOD] = 10;
  114. cost[EGameResID::GOLD] = 1000;
  115. }
  116. const IShipyard * IShipyard::castFrom( const CGObjectInstance *obj )
  117. {
  118. return dynamic_cast<const IShipyard *>(obj);
  119. }
  120. VCMI_LIB_NAMESPACE_END