AbstractGoal.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * AbstractGoal.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 "AbstractGoal.h"
  12. #include "../AIGateway.h"
  13. #include "../../../lib/constants/StringConstants.h"
  14. #include "../../../lib/entities/artifact/CArtifact.h"
  15. #include "../../../lib/entities/ResourceTypeHandler.h"
  16. namespace NKAI
  17. {
  18. using namespace Goals;
  19. TSubgoal Goals::sptr(const AbstractGoal & tmp)
  20. {
  21. TSubgoal ptr;
  22. ptr.reset(tmp.clone());
  23. return ptr;
  24. }
  25. TTask Goals::taskptr(const AbstractGoal & tmp)
  26. {
  27. TTask ptr;
  28. if(!tmp.isElementar())
  29. throw cannotFulfillGoalException(tmp.toString() + " is not elementar");
  30. ptr.reset(tmp.clone()->asTask());
  31. return ptr;
  32. }
  33. std::string AbstractGoal::toString() const
  34. {
  35. std::string desc;
  36. switch(goalType)
  37. {
  38. case COLLECT_RES:
  39. desc = "COLLECT RESOURCE " + GameResID(resID).toResource()->getJsonKey() + " (" + std::to_string(value) + ")";
  40. break;
  41. case TRADE:
  42. {
  43. auto obj = cb->getObjInstance(ObjectInstanceID(objid));
  44. if (obj)
  45. desc = (boost::format("TRADE %d of %s at %s") % value % GameResID(resID).toResource()->getJsonKey() % obj->getObjectName()).str();
  46. }
  47. break;
  48. case GATHER_TROOPS:
  49. desc = "GATHER TROOPS";
  50. break;
  51. case GET_ART_TYPE:
  52. desc = "GET ARTIFACT OF TYPE " + ArtifactID(aid).toEntity(LIBRARY)->getNameTranslated();
  53. break;
  54. case DIG_AT_TILE:
  55. desc = "DIG AT TILE " + tile.toString();
  56. break;
  57. default:
  58. return std::to_string(goalType);
  59. }
  60. if(hero)
  61. desc += " (" + hero->getNameTranslated() + ")";
  62. return desc;
  63. }
  64. bool AbstractGoal::operator==(const AbstractGoal & g) const
  65. {
  66. return false;
  67. }
  68. //TODO: find out why the following are not generated automatically on MVS?
  69. bool TSubgoal::operator==(const TSubgoal & rhs) const
  70. {
  71. return *get() == *rhs.get(); //comparison for Goals is overloaded, so they don't need to be identical to match
  72. }
  73. bool AbstractGoal::invalid() const
  74. {
  75. return goalType == EGoals::INVALID;
  76. }
  77. }