MapObjectsEvaluator.cpp 4.5 KB

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