AbstractGoal.cpp 4.0 KB

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