AbstractGoal.cpp 4.1 KB

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