AIMemory.cpp 2.7 KB

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