MapInfoCallback.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * MapInfoCallback.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 "MapInfoCallback.h"
  12. #include "../constants/EntityIdentifiers.h"
  13. #include "../mapObjects/CGObjectInstance.h"
  14. #include "../mapObjects/CGHeroInstance.h"
  15. #include "../mapObjects/CGTownInstance.h"
  16. #include "../mapObjects/MiscObjects.h"
  17. #include "../StartInfo.h"
  18. #include "../mapping/CMap.h"
  19. #include "../spells/CSpellHandler.h"
  20. VCMI_LIB_NAMESPACE_BEGIN
  21. MapInfoCallback::~MapInfoCallback() = default;
  22. const CGObjectInstance * MapInfoCallback::getObj(ObjectInstanceID objid, bool verbose) const
  23. {
  24. if(!objid.hasValue())
  25. {
  26. if(verbose)
  27. logGlobal->error("Cannot get object with id %d. No such object", objid.getNum());
  28. return nullptr;
  29. }
  30. const CGObjectInstance * ret = getMapConstPtr()->getObject(objid);
  31. if(!ret && verbose)
  32. {
  33. logGlobal->error("Cannot get object with id %d. Object was removed", objid.getNum());
  34. return nullptr;
  35. }
  36. return ret;
  37. }
  38. const CGHeroInstance * MapInfoCallback::getHero(ObjectInstanceID objid) const
  39. {
  40. const CGObjectInstance * obj = getObj(objid, false);
  41. return dynamic_cast<const CGHeroInstance *>(obj);
  42. }
  43. const CGTownInstance * MapInfoCallback::getTown(ObjectInstanceID objid) const
  44. {
  45. const CGObjectInstance * obj = getObj(objid, false);
  46. return dynamic_cast<const CGTownInstance*>(obj);
  47. }
  48. PlayerColor MapInfoCallback::getOwner(ObjectInstanceID heroID) const
  49. {
  50. const CGObjectInstance * obj = getObj(heroID);
  51. if(!obj)
  52. {
  53. logGlobal->error("MapInfoCallback::getOwner(heroID): No such object for heroID: %d", heroID.num);
  54. return PlayerColor::CANNOT_DETERMINE;
  55. }
  56. return obj->tempOwner;
  57. }
  58. const CArtifactInstance * MapInfoCallback::getArtInstance(ArtifactInstanceID aid) const
  59. {
  60. return getMapConstPtr()->getArtifactInstance(aid);
  61. }
  62. const CGObjectInstance * MapInfoCallback::getObjInstance(ObjectInstanceID oid) const
  63. {
  64. return getMapConstPtr()->getObject((oid));
  65. }
  66. bool MapInfoCallback::isInTheMap(const int3 & pos) const
  67. {
  68. return getMapConstPtr()->isInTheMap(pos);
  69. }
  70. bool MapInfoCallback::isAllowed(SpellID id) const
  71. {
  72. return getMapConstPtr()->allowedSpells.count(id) != 0;
  73. }
  74. bool MapInfoCallback::isAllowed(ArtifactID id) const
  75. {
  76. return getMapConstPtr()->allowedArtifact.count(id) != 0;
  77. }
  78. bool MapInfoCallback::isAllowed(SecondarySkill id) const
  79. {
  80. return getMapConstPtr()->allowedAbilities.count(id) != 0;
  81. }
  82. int3 MapInfoCallback::getMapSize() const
  83. {
  84. return int3(getMapConstPtr()->width, getMapConstPtr()->height, getMapConstPtr()->levels());
  85. }
  86. void MapInfoCallback::getAllowedSpells(std::vector<SpellID> & out, std::optional<ui16> level)
  87. {
  88. for (auto const & spellID : getMapConstPtr()->allowedSpells)
  89. {
  90. const auto * spell = spellID.toEntity(LIBRARY);
  91. if (!isAllowed(spellID))
  92. continue;
  93. if (level.has_value() && spell->getLevel() != level)
  94. continue;
  95. out.push_back(spellID);
  96. }
  97. }
  98. const IGameSettings & MapInfoCallback::getSettings() const
  99. {
  100. return getMapConstPtr()->getSettings();
  101. }
  102. const CMapHeader * MapInfoCallback::getMapHeader() const
  103. {
  104. return getMapConstPtr();
  105. }
  106. VCMI_LIB_NAMESPACE_END