AbstractGoal.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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/StringConstants.h"
  14. namespace NKAI
  15. {
  16. extern boost::thread_specific_ptr<CCallback> cb;
  17. extern boost::thread_specific_ptr<AIGateway> ai;
  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(dynamic_cast<ITask *>(tmp.clone()));
  31. return ptr;
  32. }
  33. std::string AbstractGoal::toString() const //TODO: virtualize
  34. {
  35. std::string desc;
  36. switch(goalType)
  37. {
  38. case COLLECT_RES:
  39. desc = "COLLECT RESOURCE " + GameConstants::RESOURCE_NAMES[resID] + " (" + 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 % GameConstants::RESOURCE_NAMES[resID] % 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 " + VLC->artifacts()->getByIndex(aid)->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.get(true)) //FIXME: used to crash when we lost hero and failed goal
  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. }