2
0

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