MapObjectsEvaluator.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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() != 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->getIndex());
  60. }
  61. else if(obj->ID == Obj::PRISON)
  62. {
  63. //special case: in-game prison subID is captured hero ID, but config has one subID with index 0 for normal prison - use that one
  64. return getObjectValue(obj->ID, 0);
  65. }
  66. else if(obj->ID == Obj::CREATURE_GENERATOR1 || obj->ID == Obj::CREATURE_GENERATOR4)
  67. {
  68. auto dwelling = dynamic_cast<const CGDwelling *>(obj);
  69. int aiValue = 0;
  70. for(auto & creLevel : dwelling->creatures)
  71. {
  72. for(auto & creatureID : creLevel.second)
  73. {
  74. auto creature = VLC->creh->objects[creatureID];
  75. aiValue += (creature->AIValue * creature->growth);
  76. }
  77. }
  78. return aiValue;
  79. }
  80. else if(obj->ID == Obj::ARTIFACT)
  81. {
  82. auto artifactObject = dynamic_cast<const CGArtifact *>(obj);
  83. switch(artifactObject->storedArtifact->artType->aClass)
  84. {
  85. case CArtifact::EartClass::ART_TREASURE:
  86. return 2000;
  87. case CArtifact::EartClass::ART_MINOR:
  88. return 5000;
  89. case CArtifact::EartClass::ART_MAJOR:
  90. return 10000;
  91. case CArtifact::EartClass::ART_RELIC:
  92. return 20000;
  93. case CArtifact::EartClass::ART_SPECIAL:
  94. return 20000;
  95. default:
  96. return 0; //invalid artifact class
  97. }
  98. }
  99. else if(obj->ID == Obj::SPELL_SCROLL)
  100. {
  101. auto scrollObject = dynamic_cast<const CGArtifact *>(obj);
  102. auto spell = scrollObject->storedArtifact->getGivenSpellID().toSpell();
  103. if(spell)
  104. {
  105. switch(spell->getLevel())
  106. {
  107. case 0: return 0; //scroll with creature ability? Let's assume it is useless
  108. case 1: return 1000;
  109. case 2: return 2000;
  110. case 3: return 5000;
  111. case 4: return 10000;
  112. case 5: return 20000;
  113. default: logAi->warn("AI detected spell scroll with spell level %s", spell->getLevel());
  114. }
  115. }
  116. else
  117. logAi->warn("AI found spell scroll with invalid spell ID: %s", scrollObject->storedArtifact->getGivenSpellID());
  118. }
  119. return getObjectValue(obj->ID, obj->subID);
  120. }
  121. void MapObjectsEvaluator::addObjectData(int primaryID, int secondaryID, int value) //by current design it updates value if already in AI database
  122. {
  123. CompoundMapObjectID internalIdentifier = CompoundMapObjectID(primaryID, secondaryID);
  124. objectDatabase[internalIdentifier] = value;
  125. }
  126. void MapObjectsEvaluator::removeObjectData(int primaryID, int secondaryID)
  127. {
  128. CompoundMapObjectID internalIdentifier = CompoundMapObjectID(primaryID, secondaryID);
  129. vstd::erase_if_present(objectDatabase, internalIdentifier);
  130. }