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