MapObjectsEvaluator.cpp 4.2 KB

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