AbstractGoal.cpp 3.9 KB

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