MapObjectsEvaluator.cpp 4.8 KB

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