MapObjectsEvaluator.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * MapObjectsEvaluator.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 "MapObjectsEvaluator.h"
  12. #include "../../lib/GameConstants.h"
  13. #include "../../lib/VCMI_Lib.h"
  14. #include "../../lib/CCreatureHandler.h"
  15. #include "../../lib/mapObjects/CompoundMapObjectID.h"
  16. #include "../../lib/mapObjectConstructors/AObjectTypeHandler.h"
  17. #include "../../lib/mapObjects/CGHeroInstance.h"
  18. #include "../../lib/mapObjects/CGTownInstance.h"
  19. #include "../../lib/mapObjects/MiscObjects.h"
  20. #include "../../lib/CRandomGenerator.h"
  21. #include "../../lib/spells/CSpellHandler.h"
  22. MapObjectsEvaluator & MapObjectsEvaluator::getInstance()
  23. {
  24. static std::unique_ptr<MapObjectsEvaluator> singletonInstance;
  25. if(singletonInstance == nullptr)
  26. singletonInstance.reset(new MapObjectsEvaluator());
  27. return *(singletonInstance.get());
  28. }
  29. MapObjectsEvaluator::MapObjectsEvaluator()
  30. {
  31. for(auto primaryID : VLC->objtypeh->knownObjects())
  32. {
  33. for(auto secondaryID : VLC->objtypeh->knownSubObjects(primaryID))
  34. {
  35. auto handler = VLC->objtypeh->getHandlerFor(primaryID, secondaryID);
  36. if(handler && !handler->isStaticObject())
  37. {
  38. if(handler->getAiValue() != std::nullopt)
  39. {
  40. objectDatabase[CompoundMapObjectID(primaryID, secondaryID)] = handler->getAiValue().value();
  41. }
  42. else //some default handling when aiValue not found, objects that require advanced properties (unavailable from handler) get their value calculated in getObjectValue
  43. {
  44. objectDatabase[CompoundMapObjectID(primaryID, secondaryID)] = 0;
  45. }
  46. }
  47. }
  48. }
  49. }
  50. std::optional<int> MapObjectsEvaluator::getObjectValue(int primaryID, int secondaryID) const
  51. {
  52. CompoundMapObjectID internalIdentifier = CompoundMapObjectID(primaryID, secondaryID);
  53. auto object = objectDatabase.find(internalIdentifier);
  54. if(object != objectDatabase.end())
  55. return object->second;
  56. logGlobal->trace("Unknown object for AI, ID: " + std::to_string(primaryID) + ", SubID: " + std::to_string(secondaryID));
  57. return std::optional<int>();
  58. }
  59. std::optional<int> MapObjectsEvaluator::getObjectValue(const CGObjectInstance * obj) const
  60. {
  61. if(obj->ID == Obj::HERO)
  62. {
  63. //special case handling: in-game heroes have hero ID as object subID, but when reading configs available hero object subID's are hero classes
  64. auto hero = dynamic_cast<const CGHeroInstance*>(obj);
  65. return getObjectValue(obj->ID, hero->getHeroClassID());
  66. }
  67. else if(obj->ID == Obj::PRISON)
  68. {
  69. //special case: in-game prison subID is captured hero ID, but config has one subID with index 0 for normal prison - use that one
  70. return getObjectValue(obj->ID, 0);
  71. }
  72. else if(obj->ID == Obj::CREATURE_GENERATOR1 || obj->ID == Obj::CREATURE_GENERATOR4)
  73. {
  74. auto dwelling = dynamic_cast<const CGDwelling *>(obj);
  75. int aiValue = 0;
  76. for(auto & creLevel : dwelling->creatures)
  77. {
  78. for(auto & creatureID : creLevel.second)
  79. {
  80. auto creature = VLC->creatures()->getById(creatureID);
  81. aiValue += (creature->getAIValue() * creature->getGrowth());
  82. }
  83. }
  84. return aiValue;
  85. }
  86. else if(obj->ID == Obj::ARTIFACT)
  87. {
  88. auto artifactObject = dynamic_cast<const CGArtifact *>(obj);
  89. switch(artifactObject->storedArtifact->artType->aClass)
  90. {
  91. case CArtifact::EartClass::ART_TREASURE:
  92. return 2000;
  93. case CArtifact::EartClass::ART_MINOR:
  94. return 5000;
  95. case CArtifact::EartClass::ART_MAJOR:
  96. return 10000;
  97. case CArtifact::EartClass::ART_RELIC:
  98. return 20000;
  99. case CArtifact::EartClass::ART_SPECIAL:
  100. return 20000;
  101. default:
  102. return 0; //invalid artifact class
  103. }
  104. }
  105. else if(obj->ID == Obj::SPELL_SCROLL)
  106. {
  107. auto scrollObject = dynamic_cast<const CGArtifact *>(obj);
  108. auto spell = scrollObject->storedArtifact->getScrollSpellID().toSpell();
  109. if(spell)
  110. {
  111. switch(spell->getLevel())
  112. {
  113. case 0: return 0; //scroll with creature ability? Let's assume it is useless
  114. case 1: return 1000;
  115. case 2: return 2000;
  116. case 3: return 5000;
  117. case 4: return 10000;
  118. case 5: return 20000;
  119. default: logAi->warn("AI detected spell scroll with spell level %s", spell->getLevel());
  120. }
  121. }
  122. else
  123. logAi->warn("AI found spell scroll with invalid spell ID: %s", scrollObject->storedArtifact->getScrollSpellID());
  124. }
  125. return getObjectValue(obj->ID, obj->subID);
  126. }
  127. void MapObjectsEvaluator::addObjectData(int primaryID, int secondaryID, int value) //by current design it updates value if already in AI database
  128. {
  129. CompoundMapObjectID internalIdentifier = CompoundMapObjectID(primaryID, secondaryID);
  130. objectDatabase[internalIdentifier] = value;
  131. }
  132. void MapObjectsEvaluator::removeObjectData(int primaryID, int secondaryID)
  133. {
  134. CompoundMapObjectID internalIdentifier = CompoundMapObjectID(primaryID, secondaryID);
  135. vstd::erase_if_present(objectDatabase, internalIdentifier);
  136. }