CollectRes.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. if(const IMarket * m = IMarket::castFrom(obj, false); m && m->allowsTrade(EMarketMode::RESOURCE_RESOURCE))
  115. {
  116. if(obj->ID == Obj::TOWN)
  117. {
  118. if(obj->tempOwner == ai->playerID)
  119. markets.push_back(m);
  120. }
  121. else
  122. markets.push_back(m);
  123. }
  124. }
  125. boost::sort(markets, [](const IMarket * m1, const IMarket * m2) -> bool
  126. {
  127. return m1->getMarketEfficiency() < m2->getMarketEfficiency();
  128. });
  129. markets.erase(boost::remove_if(markets, [](const IMarket * market) -> bool
  130. {
  131. auto * o = dynamic_cast<const CGObjectInstance *>(market);
  132. if(o && !(o->ID == Obj::TOWN && o->tempOwner == ai->playerID))
  133. {
  134. if(!ai->isAccessible(o->visitablePos()))
  135. return true;
  136. }
  137. return false;
  138. }), markets.end());
  139. if (!markets.size())
  140. {
  141. for (const CGTownInstance * t : cb->getTownsInfo())
  142. {
  143. if (cb->canBuildStructure(t, BuildingID::MARKETPLACE) == EBuildingState::ALLOWED)
  144. return sptr(BuildThis(BuildingID::MARKETPLACE, t).setpriority(2));
  145. }
  146. }
  147. else
  148. {
  149. const IMarket * m = markets.back();
  150. //attempt trade at back (best prices)
  151. int howManyCanWeBuy = 0;
  152. for (GameResID i = EGameResID::WOOD; i <= EGameResID::GOLD; ++i)
  153. {
  154. if (i.getNum() == resID)
  155. continue;
  156. int toGive = -1;
  157. int toReceive = -1;
  158. m->getOffer(i, resID, toGive, toReceive, EMarketMode::RESOURCE_RESOURCE);
  159. assert(toGive > 0 && toReceive > 0);
  160. howManyCanWeBuy += toReceive * (ai->ah->freeResources()[i] / toGive);
  161. }
  162. if (howManyCanWeBuy >= value)
  163. {
  164. auto * o = dynamic_cast<const CGObjectInstance *>(m);
  165. auto backObj = cb->getTopObj(o->visitablePos()); //it'll be a hero if we have one there; otherwise marketplace
  166. assert(backObj);
  167. auto objid = o->id.getNum();
  168. if (backObj->tempOwner != ai->playerID) //top object not owned
  169. {
  170. return sptr(VisitObj(objid)); //just go there
  171. }
  172. else //either it's our town, or we have hero there
  173. {
  174. return sptr(Trade(static_cast<EGameResID>(resID), value, objid).setisElementar(true)); //we can do this immediately
  175. }
  176. }
  177. }
  178. return sptr(Invalid()); //cannot trade
  179. }
  180. bool CollectRes::fulfillsMe(TSubgoal goal)
  181. {
  182. if (goal->resID == resID)
  183. if (goal->value >= value)
  184. return true;
  185. return false;
  186. }