CollectRes.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * CollectRes.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 "Goals.h"
  12. #include "../VCAI.h"
  13. #include "../AIUtility.h"
  14. #include "../AIhelper.h"
  15. #include "../FuzzyHelper.h"
  16. #include "../ResourceManager.h"
  17. #include "../BuildingManager.h"
  18. #include "../../../lib/mapObjects/CGMarket.h"
  19. #include "../../../lib/constants/StringConstants.h"
  20. using namespace Goals;
  21. bool CollectRes::operator==(const CollectRes & other) const
  22. {
  23. return resID == other.resID;
  24. }
  25. TGoalVec CollectRes::getAllPossibleSubgoals()
  26. {
  27. TGoalVec ret;
  28. auto givesResource = [this](const CGObjectInstance * obj) -> bool
  29. {
  30. //TODO: move this logic to object side
  31. //TODO: remember mithril exists
  32. //TODO: water objects
  33. //TODO: Creature banks
  34. //return false first from once-visitable, before checking if they were even visited
  35. switch (obj->ID.num)
  36. {
  37. case Obj::TREASURE_CHEST:
  38. return resID == GameResID(EGameResID::GOLD);
  39. break;
  40. case Obj::RESOURCE:
  41. return obj->subID == resID;
  42. break;
  43. case Obj::MINE:
  44. return (obj->subID == resID &&
  45. (cb->getPlayerRelations(obj->tempOwner, ai->playerID) == PlayerRelations::ENEMIES)); //don't capture our mines
  46. break;
  47. case Obj::CAMPFIRE:
  48. return true; //contains all resources
  49. break;
  50. case Obj::WINDMILL:
  51. switch (GameResID(resID).toEnum())
  52. {
  53. case EGameResID::GOLD:
  54. case EGameResID::WOOD:
  55. return false;
  56. }
  57. break;
  58. case Obj::WATER_WHEEL:
  59. if (resID != GameResID(EGameResID::GOLD))
  60. return false;
  61. break;
  62. case Obj::MYSTICAL_GARDEN:
  63. if ((resID != GameResID(EGameResID::GOLD)) && (resID != GameResID(EGameResID::GEMS)))
  64. return false;
  65. break;
  66. case Obj::LEAN_TO:
  67. case Obj::WAGON:
  68. if (resID != GameResID(EGameResID::GOLD))
  69. return false;
  70. break;
  71. default:
  72. return false;
  73. break;
  74. }
  75. return !vstd::contains(ai->alreadyVisited, obj); //for weekly / once visitable
  76. };
  77. std::vector<const CGObjectInstance *> objs;
  78. for (auto obj : ai->visitableObjs)
  79. {
  80. if (givesResource(obj))
  81. objs.push_back(obj);
  82. }
  83. for (auto h : cb->getHeroesInfo())
  84. {
  85. std::vector<const CGObjectInstance *> ourObjs(objs); //copy common objects
  86. for (auto obj : ai->reservedHeroesMap[h]) //add objects reserved by this hero
  87. {
  88. if (givesResource(obj))
  89. ourObjs.push_back(obj);
  90. }
  91. for (auto obj : ourObjs)
  92. {
  93. auto waysToGo = ai->ah->howToVisitObj(h, ObjectIdRef(obj));
  94. vstd::concatenate(ret, waysToGo);
  95. }
  96. }
  97. return ret;
  98. }
  99. TSubgoal CollectRes::whatToDoToAchieve()
  100. {
  101. auto goals = getAllPossibleSubgoals();
  102. auto trade = whatToDoToTrade();
  103. if (!trade->invalid())
  104. goals.push_back(trade);
  105. if (goals.empty())
  106. return sptr(Explore()); //we can always do that
  107. else
  108. return fh->chooseSolution(goals); //TODO: evaluate trading
  109. }
  110. TSubgoal CollectRes::whatToDoToTrade()
  111. {
  112. std::vector<const IMarket *> markets;
  113. std::vector<const CGObjectInstance *> visObjs;
  114. ai->retrieveVisitableObjs(visObjs, true);
  115. for(const CGObjectInstance * obj : visObjs)
  116. {
  117. if(const IMarket * m = IMarket::castFrom(obj, false); m && m->allowsTrade(EMarketMode::RESOURCE_RESOURCE))
  118. {
  119. if(obj->ID == Obj::TOWN)
  120. {
  121. if(obj->tempOwner == ai->playerID)
  122. markets.push_back(m);
  123. }
  124. else
  125. markets.push_back(m);
  126. }
  127. }
  128. boost::sort(markets, [](const IMarket * m1, const IMarket * m2) -> bool
  129. {
  130. return m1->getMarketEfficiency() < m2->getMarketEfficiency();
  131. });
  132. markets.erase(boost::remove_if(markets, [](const IMarket * market) -> bool
  133. {
  134. auto * o = dynamic_cast<const CGObjectInstance *>(market);
  135. if(o && !(o->ID == Obj::TOWN && o->tempOwner == ai->playerID))
  136. {
  137. if(!ai->isAccessible(o->visitablePos()))
  138. return true;
  139. }
  140. return false;
  141. }), markets.end());
  142. if (!markets.size())
  143. {
  144. for (const CGTownInstance * t : cb->getTownsInfo())
  145. {
  146. if (cb->canBuildStructure(t, BuildingID::MARKETPLACE) == EBuildingState::ALLOWED)
  147. return sptr(BuildThis(BuildingID::MARKETPLACE, t).setpriority(2));
  148. }
  149. }
  150. else
  151. {
  152. const IMarket * m = markets.back();
  153. //attempt trade at back (best prices)
  154. int howManyCanWeBuy = 0;
  155. for (GameResID i = EGameResID::WOOD; i <= EGameResID::GOLD; ++i)
  156. {
  157. if (i.getNum() == resID)
  158. continue;
  159. int toGive = -1, toReceive = -1;
  160. m->getOffer(i, resID, toGive, toReceive, EMarketMode::RESOURCE_RESOURCE);
  161. assert(toGive > 0 && toReceive > 0);
  162. howManyCanWeBuy += toReceive * (ai->ah->freeResources()[i] / toGive);
  163. }
  164. if (howManyCanWeBuy >= value)
  165. {
  166. auto * o = dynamic_cast<const CGObjectInstance *>(m);
  167. auto backObj = cb->getTopObj(o->visitablePos()); //it'll be a hero if we have one there; otherwise marketplace
  168. assert(backObj);
  169. auto objid = o->id.getNum();
  170. if (backObj->tempOwner != ai->playerID) //top object not owned
  171. {
  172. return sptr(VisitObj(objid)); //just go there
  173. }
  174. else //either it's our town, or we have hero there
  175. {
  176. return sptr(Trade(static_cast<EGameResID>(resID), value, objid).setisElementar(true)); //we can do this immediately
  177. }
  178. }
  179. }
  180. return sptr(Invalid()); //cannot trade
  181. }
  182. bool CollectRes::fulfillsMe(TSubgoal goal)
  183. {
  184. if (goal->resID == resID)
  185. if (goal->value >= value)
  186. return true;
  187. return false;
  188. }