MapObjectsEvaluator.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include "StdInc.h"
  2. #include "MapObjectsEvaluator.h"
  3. #include "../../lib/GameConstants.h"
  4. #include "../../lib/VCMI_Lib.h"
  5. MapObjectsEvaluator & MapObjectsEvaluator::getInstance()
  6. {
  7. static std::unique_ptr<MapObjectsEvaluator> singletonInstance;
  8. if(singletonInstance == nullptr)
  9. singletonInstance.reset(new MapObjectsEvaluator());
  10. return *(singletonInstance.get());
  11. }
  12. MapObjectsEvaluator::MapObjectsEvaluator()
  13. {
  14. for(auto primaryID : VLC->objtypeh->knownObjects())
  15. {
  16. for(auto secondaryID : VLC->objtypeh->knownSubObjects(primaryID))
  17. {
  18. auto handler = VLC->objtypeh->getHandlerFor(primaryID, secondaryID);
  19. if(!handler->isStaticObject())
  20. {
  21. if(handler->getAiValue() != boost::none)
  22. {
  23. objectDatabase[CompoundMapObjectID(primaryID, secondaryID)] = handler->getAiValue().get();
  24. }
  25. else if(VLC->objtypeh->getObjGroupAiValue(primaryID) != boost::none) //if value is not initialized - fallback to default value for this object family if it exists
  26. {
  27. objectDatabase[CompoundMapObjectID(primaryID, secondaryID)] = VLC->objtypeh->getObjGroupAiValue(primaryID).get();
  28. }
  29. else
  30. {
  31. objectDatabase[CompoundMapObjectID(primaryID, secondaryID)] = 0; //some default handling when aiValue not found
  32. }
  33. }
  34. }
  35. }
  36. }
  37. boost::optional<int> MapObjectsEvaluator::getObjectValue(int primaryID, int secondaryID) const
  38. {
  39. CompoundMapObjectID internalIdentifier = CompoundMapObjectID(primaryID, secondaryID);
  40. auto object = objectDatabase.find(internalIdentifier);
  41. if(object != objectDatabase.end())
  42. return object->second;
  43. logGlobal->trace("Unknown object for AI, ID: " + std::to_string(primaryID) + ", SubID: " + std::to_string(secondaryID));
  44. return boost::optional<int>();
  45. }
  46. void MapObjectsEvaluator::addObjectData(int primaryID, int secondaryID, int value) //by current design it updates value if already in AI database
  47. {
  48. CompoundMapObjectID internalIdentifier = CompoundMapObjectID(primaryID, secondaryID);
  49. objectDatabase[internalIdentifier] = value;
  50. }
  51. void MapObjectsEvaluator::removeObjectData(int primaryID, int secondaryID)
  52. {
  53. CompoundMapObjectID internalIdentifier = CompoundMapObjectID(primaryID, secondaryID);
  54. vstd::erase_if_present(objectDatabase, internalIdentifier);
  55. }