MapObjectsEvaluator.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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->isStaticObject())
  27. {
  28. if(handler->getAiValue() != boost::none)
  29. {
  30. objectDatabase[CompoundMapObjectID(primaryID, secondaryID)] = handler->getAiValue().get();
  31. }
  32. else if(VLC->objtypeh->getObjGroupAiValue(primaryID) != boost::none) //if value is not initialized - fallback to default value for this object family if it exists
  33. {
  34. objectDatabase[CompoundMapObjectID(primaryID, secondaryID)] = VLC->objtypeh->getObjGroupAiValue(primaryID).get();
  35. }
  36. else //some default handling when aiValue not found, objects that require advanced properties (unavailable from handler) get their value calculated in getObjectValue
  37. {
  38. objectDatabase[CompoundMapObjectID(primaryID, secondaryID)] = 0;
  39. }
  40. }
  41. }
  42. }
  43. }
  44. boost::optional<int> MapObjectsEvaluator::getObjectValue(int primaryID, int secondaryID) const
  45. {
  46. CompoundMapObjectID internalIdentifier = CompoundMapObjectID(primaryID, secondaryID);
  47. auto object = objectDatabase.find(internalIdentifier);
  48. if(object != objectDatabase.end())
  49. return object->second;
  50. logGlobal->trace("Unknown object for AI, ID: " + std::to_string(primaryID) + ", SubID: " + std::to_string(secondaryID));
  51. return boost::optional<int>();
  52. }
  53. boost::optional<int> MapObjectsEvaluator::getObjectValue(const CGObjectInstance * obj) const
  54. {
  55. if(obj->ID == Obj::HERO)
  56. {
  57. //special case handling: in-game heroes have hero ID as object subID, but when reading configs available hero object subID's are hero classes
  58. auto hero = dynamic_cast<const CGHeroInstance*>(obj);
  59. return getObjectValue(obj->ID, hero->type->heroClass->id);
  60. }
  61. if(obj->ID == Obj::CREATURE_GENERATOR1 || obj->ID == Obj::CREATURE_GENERATOR4)
  62. {
  63. auto dwelling = dynamic_cast<const CGDwelling *>(obj);
  64. int aiValue = 0;
  65. for(auto & creLevel : dwelling->creatures)
  66. {
  67. for(auto & creatureID : creLevel.second)
  68. {
  69. auto creature = VLC->creh->creatures[creatureID];
  70. aiValue += (creature->AIValue * creature->growth);
  71. }
  72. }
  73. return aiValue;
  74. }
  75. else if(obj->ID == Obj::ARTIFACT)
  76. {
  77. auto artifactObject = dynamic_cast<const CGArtifact *>(obj);
  78. switch(artifactObject->storedArtifact->artType->aClass)
  79. {
  80. case CArtifact::EartClass::ART_TREASURE:
  81. return 2000;
  82. case CArtifact::EartClass::ART_MINOR:
  83. return 5000;
  84. case CArtifact::EartClass::ART_MAJOR:
  85. return 10000;
  86. case CArtifact::EartClass::ART_RELIC:
  87. return 20000;
  88. case CArtifact::EartClass::ART_SPECIAL:
  89. return 20000;
  90. default:
  91. return 0; //invalid artifact class
  92. }
  93. }
  94. else if(obj->ID == Obj::SPELL_SCROLL)
  95. {
  96. auto scrollObject = dynamic_cast<const CGArtifact *>(obj);
  97. auto spell = scrollObject->storedArtifact->getGivenSpellID().toSpell();
  98. if(spell)
  99. {
  100. switch(spell->getLevel())
  101. {
  102. case 0: return 0; //scroll with creature ability? Let's assume it is useless
  103. case 1: return 1000;
  104. case 2: return 2000;
  105. case 3: return 5000;
  106. case 4: return 10000;
  107. case 5: return 20000;
  108. default: logAi->warn("AI detected spell scroll with spell level %s", spell->getLevel());
  109. }
  110. }
  111. else
  112. logAi->warn("AI found spell scroll with invalid spell ID: %s", scrollObject->storedArtifact->getGivenSpellID());
  113. }
  114. return getObjectValue(obj->ID, obj->subID);
  115. }
  116. void MapObjectsEvaluator::addObjectData(int primaryID, int secondaryID, int value) //by current design it updates value if already in AI database
  117. {
  118. CompoundMapObjectID internalIdentifier = CompoundMapObjectID(primaryID, secondaryID);
  119. objectDatabase[internalIdentifier] = value;
  120. }
  121. void MapObjectsEvaluator::removeObjectData(int primaryID, int secondaryID)
  122. {
  123. CompoundMapObjectID internalIdentifier = CompoundMapObjectID(primaryID, secondaryID);
  124. vstd::erase_if_present(objectDatabase, internalIdentifier);
  125. }