IObjectInterface.cpp 3.6 KB

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