CollectRes.cpp 5.4 KB

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