AbstractGoal.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 "../ResourceManager.h"
  16. #include "../BuildingManager.h"
  17. #include "../../../lib/mapping/CMap.h" //for victory conditions
  18. #include "../../../lib/CPathfinder.h"
  19. #include "../../../lib/StringConstants.h"
  20. extern boost::thread_specific_ptr<CCallback> cb;
  21. extern boost::thread_specific_ptr<VCAI> ai;
  22. extern FuzzyHelper * fh;
  23. using namespace Goals;
  24. TSubgoal Goals::sptr(const AbstractGoal & tmp)
  25. {
  26. TSubgoal ptr;
  27. ptr.reset(tmp.clone());
  28. return ptr;
  29. }
  30. std::string AbstractGoal::name() const //TODO: virtualize
  31. {
  32. std::string desc;
  33. switch(goalType)
  34. {
  35. case INVALID:
  36. return "INVALID";
  37. case WIN:
  38. return "WIN";
  39. case CONQUER:
  40. return "CONQUER";
  41. case BUILD:
  42. return "BUILD";
  43. case EXPLORE:
  44. desc = "EXPLORE";
  45. break;
  46. case GATHER_ARMY:
  47. desc = "GATHER ARMY";
  48. break;
  49. case BUY_ARMY:
  50. return "BUY ARMY";
  51. break;
  52. case BOOST_HERO:
  53. desc = "BOOST_HERO (unsupported)";
  54. break;
  55. case RECRUIT_HERO:
  56. return "RECRUIT HERO";
  57. case BUILD_STRUCTURE:
  58. return "BUILD STRUCTURE";
  59. case COLLECT_RES:
  60. desc = "COLLECT RESOURCE " + GameConstants::RESOURCE_NAMES[resID] + " (" + boost::lexical_cast<std::string>(value) + ")";
  61. break;
  62. case TRADE:
  63. {
  64. auto obj = cb->getObjInstance(ObjectInstanceID(objid));
  65. if (obj)
  66. desc = (boost::format("TRADE %d of %s at %s") % value % GameConstants::RESOURCE_NAMES[resID] % obj->getObjectName()).str();
  67. }
  68. break;
  69. case GATHER_TROOPS:
  70. desc = "GATHER TROOPS";
  71. break;
  72. case VISIT_OBJ:
  73. {
  74. auto obj = cb->getObjInstance(ObjectInstanceID(objid));
  75. if(obj)
  76. desc = "VISIT OBJ " + obj->getObjectName();
  77. }
  78. break;
  79. case FIND_OBJ:
  80. desc = "FIND OBJ " + boost::lexical_cast<std::string>(objid);
  81. break;
  82. case VISIT_HERO:
  83. {
  84. auto obj = cb->getObjInstance(ObjectInstanceID(objid));
  85. if(obj)
  86. desc = "VISIT HERO " + obj->getObjectName();
  87. }
  88. break;
  89. case GET_ART_TYPE:
  90. desc = "GET ARTIFACT OF TYPE " + VLC->artifacts()->getByIndex(aid)->getName();
  91. break;
  92. case VISIT_TILE:
  93. desc = "VISIT TILE " + tile.toString();
  94. break;
  95. case CLEAR_WAY_TO:
  96. desc = "CLEAR WAY TO " + tile.toString();
  97. break;
  98. case DIG_AT_TILE:
  99. desc = "DIG AT TILE " + tile.toString();
  100. break;
  101. default:
  102. return boost::lexical_cast<std::string>(goalType);
  103. }
  104. if(hero.get(true)) //FIXME: used to crash when we lost hero and failed goal
  105. desc += " (" + hero->name + ")";
  106. return desc;
  107. }
  108. bool AbstractGoal::operator==(const AbstractGoal & g) const
  109. {
  110. return false;
  111. }
  112. bool AbstractGoal::operator<(AbstractGoal & g) //for std::unique
  113. {
  114. //TODO: make sure it gets goals consistent with == operator
  115. if (goalType < g.goalType)
  116. return true;
  117. if (goalType > g.goalType)
  118. return false;
  119. if (hero < g.hero)
  120. return true;
  121. if (hero > g.hero)
  122. return false;
  123. if (tile < g.tile)
  124. return true;
  125. if (g.tile < tile)
  126. return false;
  127. if (objid < g.objid)
  128. return true;
  129. if (objid > g.objid)
  130. return false;
  131. if (town < g.town)
  132. return true;
  133. if (town > g.town)
  134. return false;
  135. if (value < g.value)
  136. return true;
  137. if (value > g.value)
  138. return false;
  139. if (priority < g.priority)
  140. return true;
  141. if (priority > g.priority)
  142. return false;
  143. if (resID < g.resID)
  144. return true;
  145. if (resID > g.resID)
  146. return false;
  147. if (bid < g.bid)
  148. return true;
  149. if (bid > g.bid)
  150. return false;
  151. if (aid < g.aid)
  152. return true;
  153. if (aid > g.aid)
  154. return false;
  155. return false;
  156. }
  157. //TODO: find out why the following are not generated automatically on MVS?
  158. bool TSubgoal::operator==(const TSubgoal & rhs) const
  159. {
  160. return *get() == *rhs.get(); //comparison for Goals is overloaded, so they don't need to be identical to match
  161. }
  162. bool TSubgoal::operator<(const TSubgoal & rhs) const
  163. {
  164. return get() < rhs.get(); //compae by value
  165. }
  166. bool AbstractGoal::invalid() const
  167. {
  168. return goalType == EGoals::INVALID;
  169. }
  170. void AbstractGoal::accept(VCAI * ai)
  171. {
  172. ai->tryRealize(*this);
  173. }
  174. float AbstractGoal::accept(FuzzyHelper * f)
  175. {
  176. return f->evaluate(*this);
  177. }