2
0

AbstractGoal.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. namespace NKAI
  15. {
  16. using namespace Goals;
  17. TSubgoal Goals::sptr(const AbstractGoal & tmp)
  18. {
  19. TSubgoal ptr;
  20. ptr.reset(tmp.clone());
  21. return ptr;
  22. }
  23. TTask Goals::taskptr(const AbstractGoal & tmp)
  24. {
  25. TTask ptr;
  26. if(!tmp.isElementar())
  27. throw cannotFulfillGoalException(tmp.toString() + " is not elementar");
  28. ptr.reset(tmp.clone()->asTask());
  29. return ptr;
  30. }
  31. std::string AbstractGoal::toString() const
  32. {
  33. std::string desc;
  34. switch(goalType)
  35. {
  36. case COLLECT_RES:
  37. desc = "COLLECT RESOURCE " + GameConstants::RESOURCE_NAMES[resID] + " (" + std::to_string(value) + ")";
  38. break;
  39. case TRADE:
  40. {
  41. auto obj = cb->getObjInstance(ObjectInstanceID(objid));
  42. if (obj)
  43. desc = (boost::format("TRADE %d of %s at %s") % value % GameConstants::RESOURCE_NAMES[resID] % obj->getObjectName()).str();
  44. }
  45. break;
  46. case GATHER_TROOPS:
  47. desc = "GATHER TROOPS";
  48. break;
  49. case GET_ART_TYPE:
  50. desc = "GET ARTIFACT OF TYPE " + LIBRARY->artifacts()->getByIndex(aid)->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. bool AbstractGoal::operator==(const AbstractGoal & g) const
  63. {
  64. return false;
  65. }
  66. //TODO: find out why the following are not generated automatically on MVS?
  67. bool TSubgoal::operator==(const TSubgoal & rhs) const
  68. {
  69. return *get() == *rhs.get(); //comparison for Goals is overloaded, so they don't need to be identical to match
  70. }
  71. bool AbstractGoal::invalid() const
  72. {
  73. return goalType == EGoals::INVALID;
  74. }
  75. }