AbstractGoal.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 NK2AI
  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. desc = (boost::format("TRADE %d of %s at objid %d") % value % GameResID(resID).toResource()->getJsonKey() % objid).str();
  44. break;
  45. }
  46. case GATHER_TROOPS:
  47. desc = "GATHER TROOPS";
  48. break;
  49. case GET_ART_TYPE:
  50. desc = "GET ARTIFACT OF TYPE " + ArtifactID(aid).toEntity(LIBRARY)->getNameTranslated();
  51. break;
  52. case DIG_AT_TILE:
  53. desc = "DIG AT TILE " + tile.toString();
  54. break;
  55. default:
  56. return std::to_string(goalType);
  57. }
  58. if(hero)
  59. desc += " (" + hero->getNameTranslated() + ")";
  60. return desc;
  61. }
  62. //TODO: find out why the following are not generated automatically on MVS?
  63. bool TSubgoal::operator==(const TSubgoal & rhs) const
  64. {
  65. return *get() == *rhs.get(); //comparison for Goals is overloaded, so they don't need to be identical to match
  66. }
  67. bool TSubgoal::operator<(const TSubgoal & rhs) const
  68. {
  69. return false;
  70. }
  71. }