AbstractGoal.cpp 2.2 KB

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