MapObjectsEvaluator.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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/CRandomGenerator.h"
  8. MapObjectsEvaluator & MapObjectsEvaluator::getInstance()
  9. {
  10. static std::unique_ptr<MapObjectsEvaluator> singletonInstance;
  11. if(singletonInstance == nullptr)
  12. singletonInstance.reset(new MapObjectsEvaluator());
  13. return *(singletonInstance.get());
  14. }
  15. MapObjectsEvaluator::MapObjectsEvaluator()
  16. {
  17. for(auto primaryID : VLC->objtypeh->knownObjects())
  18. {
  19. for(auto secondaryID : VLC->objtypeh->knownSubObjects(primaryID))
  20. {
  21. auto handler = VLC->objtypeh->getHandlerFor(primaryID, secondaryID);
  22. if(!handler->isStaticObject())
  23. {
  24. if(handler->getAiValue() != boost::none)
  25. {
  26. objectDatabase[CompoundMapObjectID(primaryID, secondaryID)] = handler->getAiValue().get();
  27. }
  28. else if(VLC->objtypeh->getObjGroupAiValue(primaryID) != boost::none) //if value is not initialized - fallback to default value for this object family if it exists
  29. {
  30. objectDatabase[CompoundMapObjectID(primaryID, secondaryID)] = VLC->objtypeh->getObjGroupAiValue(primaryID).get();
  31. }
  32. else //some default handling when aiValue not found
  33. {
  34. objectDatabase[CompoundMapObjectID(primaryID, secondaryID)] = 0;
  35. }
  36. }
  37. }
  38. }
  39. }
  40. boost::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 boost::optional<int>();
  48. }
  49. boost::optional<int> MapObjectsEvaluator::getObjectValue(const CGObjectInstance * obj) const
  50. {
  51. if(obj->ID == Obj::CREATURE_GENERATOR1 || obj->ID == Obj::CREATURE_GENERATOR4)
  52. {
  53. auto dwelling = dynamic_cast<const CGDwelling *>(obj);
  54. int aiValue = 0;
  55. for(auto & creLevel : dwelling->creatures)
  56. {
  57. for(auto & creatureID : creLevel.second)
  58. {
  59. auto creature = VLC->creh->creatures[creatureID];
  60. aiValue += (creature->AIValue * creature->growth);
  61. }
  62. }
  63. return aiValue;
  64. }
  65. else
  66. return getObjectValue(obj->ID, obj->subID);
  67. }
  68. void MapObjectsEvaluator::addObjectData(int primaryID, int secondaryID, int value) //by current design it updates value if already in AI database
  69. {
  70. CompoundMapObjectID internalIdentifier = CompoundMapObjectID(primaryID, secondaryID);
  71. objectDatabase[internalIdentifier] = value;
  72. }
  73. void MapObjectsEvaluator::removeObjectData(int primaryID, int secondaryID)
  74. {
  75. CompoundMapObjectID internalIdentifier = CompoundMapObjectID(primaryID, secondaryID);
  76. vstd::erase_if_present(objectDatabase, internalIdentifier);
  77. }