AbstractGoal.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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/constants/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 " + LIBRARY->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. // FIXME: unused code?
  108. //bool AbstractGoal::operator<(AbstractGoal & g) //for std::unique
  109. //{
  110. // //TODO: make sure it gets goals consistent with == operator
  111. // if (goalType < g.goalType)
  112. // return true;
  113. // if (goalType > g.goalType)
  114. // return false;
  115. // if (hero < g.hero)
  116. // return true;
  117. // if (hero > g.hero)
  118. // return false;
  119. // if (tile < g.tile)
  120. // return true;
  121. // if (g.tile < tile)
  122. // return false;
  123. // if (objid < g.objid)
  124. // return true;
  125. // if (objid > g.objid)
  126. // return false;
  127. // if (town < g.town)
  128. // return true;
  129. // if (town > g.town)
  130. // return false;
  131. // if (value < g.value)
  132. // return true;
  133. // if (value > g.value)
  134. // return false;
  135. // if (priority < g.priority)
  136. // return true;
  137. // if (priority > g.priority)
  138. // return false;
  139. // if (resID < g.resID)
  140. // return true;
  141. // if (resID > g.resID)
  142. // return false;
  143. // if (bid < g.bid)
  144. // return true;
  145. // if (bid > g.bid)
  146. // return false;
  147. // if (aid < g.aid)
  148. // return true;
  149. // if (aid > g.aid)
  150. // return false;
  151. // return false;
  152. //}
  153. //TODO: find out why the following are not generated automatically on MVS?
  154. bool TSubgoal::operator==(const TSubgoal & rhs) const
  155. {
  156. return *get() == *rhs.get(); //comparison for Goals is overloaded, so they don't need to be identical to match
  157. }
  158. bool TSubgoal::operator<(const TSubgoal & rhs) const
  159. {
  160. return get() < rhs.get(); //compae by value
  161. }
  162. bool AbstractGoal::invalid() const
  163. {
  164. return goalType == EGoals::INVALID;
  165. }
  166. void AbstractGoal::accept(VCAI * ai)
  167. {
  168. ai->tryRealize(*this);
  169. }
  170. float AbstractGoal::accept(FuzzyHelper * f)
  171. {
  172. return f->evaluate(*this);
  173. }