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