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 dynamic_cast<const CGResource*>(obj)->resourceID() == GameResID(resID);
  42. break;
  43. case Obj::MINE:
  44. return (dynamic_cast<const CGMine*>(obj)->producedResource == GameResID(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::MYSTICAL_GARDEN:
  59. if ((resID != GameResID(EGameResID::GOLD)) && (resID != GameResID(EGameResID::GEMS)))
  60. return false;
  61. break;
  62. case Obj::WATER_WHEEL:
  63. case Obj::LEAN_TO:
  64. case Obj::WAGON:
  65. if (resID != GameResID(EGameResID::GOLD))
  66. return false;
  67. break;
  68. default:
  69. return false;
  70. break;
  71. }
  72. return !vstd::contains(ai->alreadyVisited, obj); //for weekly / once visitable
  73. };
  74. std::vector<const CGObjectInstance *> objs;
  75. for (auto obj : ai->visitableObjs)
  76. {
  77. if (givesResource(obj))
  78. objs.push_back(obj);
  79. }
  80. for (auto h : cb->getHeroesInfo())
  81. {
  82. std::vector<const CGObjectInstance *> ourObjs(objs); //copy common objects
  83. for (auto obj : ai->reservedHeroesMap[h]) //add objects reserved by this hero
  84. {
  85. if (givesResource(obj))
  86. ourObjs.push_back(obj);
  87. }
  88. for (auto obj : ourObjs)
  89. {
  90. auto waysToGo = ai->ah->howToVisitObj(h, ObjectIdRef(obj));
  91. vstd::concatenate(ret, waysToGo);
  92. }
  93. }
  94. return ret;
  95. }
  96. TSubgoal CollectRes::whatToDoToAchieve()
  97. {
  98. auto goals = getAllPossibleSubgoals();
  99. auto trade = whatToDoToTrade();
  100. if (!trade->invalid())
  101. goals.push_back(trade);
  102. if (goals.empty())
  103. return sptr(Explore()); //we can always do that
  104. else
  105. return fh->chooseSolution(goals); //TODO: evaluate trading
  106. }
  107. TSubgoal CollectRes::whatToDoToTrade()
  108. {
  109. std::vector<const IMarket *> markets;
  110. std::vector<const CGObjectInstance *> visObjs;
  111. ai->retrieveVisitableObjs(visObjs, true);
  112. for(const CGObjectInstance * obj : visObjs)
  113. {
  114. const auto * m = dynamic_cast<const IMarket*>(obj);
  115. if(m && m->allowsTrade(EMarketMode::RESOURCE_RESOURCE))
  116. {
  117. if(obj->ID == Obj::TOWN)
  118. {
  119. if(obj->tempOwner == ai->playerID)
  120. markets.push_back(m);
  121. }
  122. else
  123. markets.push_back(m);
  124. }
  125. }
  126. boost::sort(markets, [](const IMarket * m1, const IMarket * m2) -> bool
  127. {
  128. return m1->getMarketEfficiency() < m2->getMarketEfficiency();
  129. });
  130. markets.erase(boost::remove_if(markets, [](const IMarket * market) -> bool
  131. {
  132. auto * o = dynamic_cast<const CGObjectInstance *>(market);
  133. if(o && !(o->ID == Obj::TOWN && o->tempOwner == ai->playerID))
  134. {
  135. if(!ai->isAccessible(o->visitablePos()))
  136. return true;
  137. }
  138. return false;
  139. }), markets.end());
  140. if (!markets.size())
  141. {
  142. for (const CGTownInstance * t : cb->getTownsInfo())
  143. {
  144. if (cb->canBuildStructure(t, BuildingID::MARKETPLACE) == EBuildingState::ALLOWED)
  145. return sptr(BuildThis(BuildingID::MARKETPLACE, t).setpriority(2));
  146. }
  147. }
  148. else
  149. {
  150. const IMarket * m = markets.back();
  151. //attempt trade at back (best prices)
  152. int howManyCanWeBuy = 0;
  153. for (GameResID i = EGameResID::WOOD; i <= EGameResID::GOLD; ++i)
  154. {
  155. if (i.getNum() == resID)
  156. continue;
  157. int toGive = -1;
  158. int toReceive = -1;
  159. m->getOffer(i, resID, toGive, toReceive, EMarketMode::RESOURCE_RESOURCE);
  160. assert(toGive > 0 && toReceive > 0);
  161. howManyCanWeBuy += toReceive * (ai->ah->freeResources()[i] / toGive);
  162. }
  163. if (howManyCanWeBuy >= value)
  164. {
  165. auto * o = dynamic_cast<const CGObjectInstance *>(m);
  166. auto backObj = cb->getTopObj(o->visitablePos()); //it'll be a hero if we have one there; otherwise marketplace
  167. assert(backObj);
  168. auto objid = o->id.getNum();
  169. if (backObj->tempOwner != ai->playerID) //top object not owned
  170. {
  171. return sptr(VisitObj(objid)); //just go there
  172. }
  173. else //either it's our town, or we have hero there
  174. {
  175. return sptr(Trade(static_cast<EGameResID>(resID), value, objid).setisElementar(true)); //we can do this immediately
  176. }
  177. }
  178. }
  179. return sptr(Invalid()); //cannot trade
  180. }
  181. bool CollectRes::fulfillsMe(TSubgoal goal)
  182. {
  183. if (goal->resID == resID)
  184. if (goal->value >= value)
  185. return true;
  186. return false;
  187. }