AIMemory.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * AIMemory.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "../StdInc.h"
  11. #include "AIMemory.h"
  12. namespace NK2AI
  13. {
  14. void AIMemory::removeFromMemory(const CGObjectInstance * obj)
  15. {
  16. vstd::erase_if_present(visitableObjs, obj->id);
  17. vstd::erase_if_present(alreadyVisited, obj->id);
  18. //TODO: Find better way to handle hero boat removal
  19. if(const auto * hero = dynamic_cast<const CGHeroInstance *>(obj))
  20. {
  21. if(hero->inBoat())
  22. {
  23. vstd::erase_if_present(visitableObjs, hero->getBoat()->id);
  24. vstd::erase_if_present(alreadyVisited, hero->getBoat()->id);
  25. }
  26. }
  27. }
  28. void AIMemory::removeFromMemory(const ObjectIdRef obj)
  29. {
  30. auto matchesId = [&](const ObjectInstanceID & objId) -> bool
  31. {
  32. return objId == obj.id;
  33. };
  34. vstd::erase_if(visitableObjs, matchesId);
  35. vstd::erase_if(alreadyVisited, matchesId);
  36. }
  37. void AIMemory::addSubterraneanGate(const CGObjectInstance * entrance, const CGObjectInstance * exit)
  38. {
  39. knownSubterraneanGates[entrance] = exit;
  40. knownSubterraneanGates[exit] = entrance;
  41. logAi->trace("Found a pair of subterranean gates between %s and %s!", entrance->visitablePos().toString(), exit->visitablePos().toString());
  42. }
  43. void AIMemory::addVisitableObject(const CGObjectInstance * obj)
  44. {
  45. visitableObjs.insert(obj->id);
  46. // All teleport objects seen automatically assigned to appropriate channels
  47. if(const auto * const teleportObj = dynamic_cast<const CGTeleport *>(obj))
  48. {
  49. CGTeleport::addToChannel(knownTeleportChannels, teleportObj);
  50. }
  51. }
  52. void AIMemory::markObjectVisited(const CGObjectInstance * obj)
  53. {
  54. if(!obj)
  55. return;
  56. // TODO: maybe this logic belongs to CaptureObjects::shouldVisit
  57. if(const auto * rewardable = dynamic_cast<const CRewardableObject *>(obj))
  58. {
  59. if(rewardable->configuration.getVisitMode() == Rewardable::VISIT_HERO) //we may want to visit it with another hero
  60. return;
  61. if(rewardable->configuration.getVisitMode() == Rewardable::VISIT_BONUS) //or another time
  62. return;
  63. }
  64. if(obj->ID == Obj::MONSTER)
  65. return;
  66. alreadyVisited.insert(obj->id);
  67. }
  68. void AIMemory::markObjectUnvisited(const CGObjectInstance * obj)
  69. {
  70. vstd::erase_if_present(alreadyVisited, obj->id);
  71. }
  72. bool AIMemory::wasVisited(const CGObjectInstance * obj) const
  73. {
  74. return vstd::contains(alreadyVisited, obj->id);
  75. }
  76. void AIMemory::removeInvisibleOrDeletedObjects(const CCallback & cc)
  77. {
  78. auto shouldBeErased = [&](const ObjectInstanceID objId) -> bool
  79. {
  80. return !cc.getObj(objId, false);
  81. };
  82. vstd::erase_if(visitableObjs, shouldBeErased);
  83. vstd::erase_if(alreadyVisited, shouldBeErased);
  84. }
  85. std::vector<const CGObjectInstance *> AIMemory::visitableIdsToObjsVector(const CCallback & cc) const
  86. {
  87. auto objs = std::vector<const CGObjectInstance *>();
  88. for(const ObjectInstanceID objId : visitableObjs)
  89. {
  90. if(const auto * obj = cc.getObjInstance(objId))
  91. objs.push_back(obj);
  92. }
  93. return objs;
  94. }
  95. std::set<const CGObjectInstance *> AIMemory::visitableIdsToObjsSet(const CCallback & cc) const
  96. {
  97. auto objs = std::set<const CGObjectInstance *>();
  98. for(const ObjectInstanceID objId : visitableObjs)
  99. {
  100. if(const auto * obj = cc.getObjInstance(objId))
  101. objs.insert(obj);
  102. }
  103. return objs;
  104. }
  105. }