CollectRes.cpp 5.3 KB

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