AIMemory.cpp 2.5 KB

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