AIMemory.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 NKAI
  13. {
  14. void AIMemory::removeFromMemory(const CGObjectInstance * obj)
  15. {
  16. vstd::erase_if_present(visitableObjs, obj);
  17. vstd::erase_if_present(alreadyVisited, obj);
  18. //TODO: Find better way to handle hero boat removal
  19. if(auto hero = dynamic_cast<const CGHeroInstance *>(obj))
  20. {
  21. if(hero->inBoat())
  22. {
  23. vstd::erase_if_present(visitableObjs, hero->getBoat());
  24. vstd::erase_if_present(alreadyVisited, hero->getBoat());
  25. }
  26. }
  27. }
  28. void AIMemory::removeFromMemory(ObjectIdRef obj)
  29. {
  30. auto matchesId = [&](const CGObjectInstance * hlpObj) -> bool
  31. {
  32. return hlpObj->id == 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(
  42. "Found a pair of subterranean gates between %s and %s!",
  43. entrance->visitablePos().toString(),
  44. exit->visitablePos().toString());
  45. }
  46. void AIMemory::addVisitableObject(const CGObjectInstance * obj)
  47. {
  48. visitableObjs.insert(obj);
  49. // All teleport objects seen automatically assigned to appropriate channels
  50. auto teleportObj = dynamic_cast<const CGTeleport *>(obj);
  51. if(teleportObj)
  52. {
  53. CGTeleport::addToChannel(knownTeleportChannels, teleportObj);
  54. }
  55. }
  56. void AIMemory::markObjectVisited(const CGObjectInstance * obj)
  57. {
  58. if(!obj)
  59. return;
  60. // TODO: maybe this logic belongs to CaptureObjects::shouldVisit
  61. if(const auto * rewardable = dynamic_cast<const CRewardableObject *>(obj))
  62. {
  63. if (rewardable->configuration.getVisitMode() == Rewardable::VISIT_HERO) //we may want to visit it with another hero
  64. return;
  65. if (rewardable->configuration.getVisitMode() == Rewardable::VISIT_BONUS) //or another time
  66. return;
  67. }
  68. if(obj->ID == Obj::MONSTER)
  69. return;
  70. alreadyVisited.insert(obj);
  71. }
  72. void AIMemory::markObjectUnvisited(const CGObjectInstance * obj)
  73. {
  74. vstd::erase_if_present(alreadyVisited, obj);
  75. }
  76. bool AIMemory::wasVisited(const CGObjectInstance * obj) const
  77. {
  78. return vstd::contains(alreadyVisited, obj);
  79. }
  80. void AIMemory::removeInvisibleObjects(CCallback * cb)
  81. {
  82. auto shouldBeErased = [&](const CGObjectInstance * obj) -> bool
  83. {
  84. if(obj)
  85. return !cb->getObj(obj->id, false); // no verbose output needed as we check object visibility
  86. else
  87. return true;
  88. };
  89. vstd::erase_if(visitableObjs, shouldBeErased);
  90. vstd::erase_if(alreadyVisited, shouldBeErased);
  91. }
  92. }