MapObjectsEvaluator.cpp 4.5 KB

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