2
0

CollectRes.cpp 5.4 KB

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