AIMemory.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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(dynamic_cast<const CGVisitableOPH *>(obj)) //we may want to visit it with another hero
  63. return;
  64. if(dynamic_cast<const CGBonusingObject *>(obj)) //or another time
  65. return;
  66. if(obj->ID == Obj::MONSTER)
  67. return;
  68. alreadyVisited.insert(obj);
  69. }
  70. void AIMemory::markObjectUnvisited(const CGObjectInstance * obj)
  71. {
  72. vstd::erase_if_present(alreadyVisited, obj);
  73. }
  74. bool AIMemory::wasVisited(const CGObjectInstance * obj) const
  75. {
  76. return vstd::contains(alreadyVisited, obj);
  77. }
  78. void AIMemory::removeInvisibleObjects(CCallback * cb)
  79. {
  80. auto shouldBeErased = [&](const CGObjectInstance * obj) -> bool
  81. {
  82. if(obj)
  83. return !cb->getObj(obj->id, false); // no verbose output needed as we check object visibility
  84. else
  85. return true;
  86. };
  87. vstd::erase_if(visitableObjs, shouldBeErased);
  88. vstd::erase_if(alreadyVisited, shouldBeErased);
  89. }
  90. }