AbstractGoal.cpp 4.0 KB

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