2
0

Win.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Win.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 "../AIGateway.h"
  13. #include "../AIUtility.h"
  14. #include "../AIhelper.h"
  15. #include "../FuzzyHelper.h"
  16. #include "../../../lib/mapping/CMap.h" //for victory conditions
  17. #include "../../../lib/CPathfinder.h"
  18. #include "../../../lib/StringConstants.h"
  19. extern boost::thread_specific_ptr<CCallback> cb;
  20. extern boost::thread_specific_ptr<AIGateway> ai;
  21. extern FuzzyHelper * fh;
  22. using namespace Goals;
  23. TSubgoal Win::whatToDoToAchieve()
  24. {
  25. auto toBool = [=](const EventCondition &)
  26. {
  27. // TODO: proper implementation
  28. // Right now even already fulfilled goals will be included into generated list
  29. // Proper check should test if event condition is already fulfilled
  30. // Easiest way to do this is to call CGameState::checkForVictory but this function should not be
  31. // used on client side or in AI code
  32. return false;
  33. };
  34. std::vector<EventCondition> goals;
  35. for(const TriggeredEvent & event : cb->getMapHeader()->triggeredEvents)
  36. {
  37. //TODO: try to eliminate human player(s) using loss conditions that have isHuman element
  38. if(event.effect.type == EventEffect::VICTORY)
  39. {
  40. boost::range::copy(event.trigger.getFulfillmentCandidates(toBool), std::back_inserter(goals));
  41. }
  42. }
  43. //TODO: instead of returning first encountered goal AI should generate list of possible subgoals
  44. for(const EventCondition & goal : goals)
  45. {
  46. switch(goal.condition)
  47. {
  48. case EventCondition::HAVE_ARTIFACT:
  49. return sptr(GetArtOfType(goal.objectType));
  50. case EventCondition::DESTROY:
  51. {
  52. if(goal.object)
  53. {
  54. auto obj = cb->getObj(goal.object->id);
  55. /*if(obj)
  56. if(obj->getOwner() == ai->playerID) //we can't capture our own object
  57. return sptr(Conquer());*/
  58. return sptr(VisitObj(goal.object->id.getNum()));
  59. }
  60. else
  61. {
  62. // TODO: destroy all objects of type goal.objectType
  63. // This situation represents "kill all creatures" condition from H3
  64. break;
  65. }
  66. }
  67. case EventCondition::HAVE_BUILDING:
  68. {
  69. // TODO build other buildings apart from Grail
  70. // goal.objectType = buidingID to build
  71. // goal.object = optional, town in which building should be built
  72. // Represents "Improve town" condition from H3 (but unlike H3 it consists from 2 separate conditions)
  73. if(goal.objectType == BuildingID::GRAIL)
  74. {
  75. if(auto h = ai->getHeroWithGrail())
  76. {
  77. //hero is in a town that can host Grail
  78. if(h->visitedTown && !vstd::contains(h->visitedTown->forbiddenBuildings, BuildingID::GRAIL))
  79. {
  80. const CGTownInstance * t = h->visitedTown;
  81. return sptr(BuildThis(BuildingID::GRAIL, t).setpriority(10));
  82. }
  83. else
  84. {
  85. auto towns = cb->getTownsInfo();
  86. towns.erase(boost::remove_if(towns,
  87. [](const CGTownInstance * t) -> bool
  88. {
  89. return vstd::contains(t->forbiddenBuildings, BuildingID::GRAIL);
  90. }),
  91. towns.end());
  92. boost::sort(towns, CDistanceSorter(h.get()));
  93. if(towns.size())
  94. {
  95. return sptr(VisitTile(towns.front()->visitablePos()).sethero(h));
  96. }
  97. }
  98. }
  99. double ratio = 0;
  100. // maybe make this check a bit more complex? For example:
  101. // 0.75 -> dig randomly within 3 tiles radius
  102. // 0.85 -> radius now 2 tiles
  103. // 0.95 -> 1 tile radius, position is fully known
  104. // AFAIK H3 AI does something like this
  105. int3 grailPos = cb->getGrailPos(&ratio);
  106. if(ratio > 0.99)
  107. {
  108. return sptr(DigAtTile(grailPos));
  109. } //TODO: use FIND_OBJ
  110. else if(const CGObjectInstance * obj = ai->getUnvisitedObj(objWithID<Obj::OBELISK>)) //there are unvisited Obelisks
  111. return sptr(VisitObj(obj->id.getNum()));
  112. /*else
  113. return sptr(Explore());*/
  114. }
  115. break;
  116. }
  117. case EventCondition::CONTROL:
  118. {
  119. if(goal.object)
  120. {
  121. auto objRelations = cb->getPlayerRelations(ai->playerID, goal.object->tempOwner);
  122. if(objRelations == PlayerRelations::ENEMIES)
  123. {
  124. return sptr(VisitObj(goal.object->id.getNum()));
  125. }
  126. else
  127. {
  128. // TODO: Defance
  129. break;
  130. }
  131. }
  132. else
  133. {
  134. //TODO: control all objects of type "goal.objectType"
  135. // Represents H3 condition "Flag all mines"
  136. break;
  137. }
  138. }
  139. case EventCondition::HAVE_RESOURCES:
  140. //TODO mines? piles? marketplace?
  141. //save?
  142. return sptr(CollectRes(static_cast<Res::ERes>(goal.objectType), goal.value));
  143. case EventCondition::HAVE_CREATURES:
  144. return sptr(GatherTroops(goal.objectType, goal.value));
  145. case EventCondition::TRANSPORT:
  146. {
  147. //TODO. merge with bring Grail to town? So AI will first dig grail, then transport it using this goal and builds it
  148. // Represents "transport artifact" condition:
  149. // goal.objectType = type of artifact
  150. // goal.object = destination-town where artifact should be transported
  151. break;
  152. }
  153. case EventCondition::STANDARD_WIN:
  154. return sptr(Invalid());
  155. // Conditions that likely don't need any implementation
  156. case EventCondition::DAYS_PASSED:
  157. break; // goal.value = number of days for condition to trigger
  158. case EventCondition::DAYS_WITHOUT_TOWN:
  159. break; // goal.value = number of days to trigger this
  160. case EventCondition::IS_HUMAN:
  161. break; // Should be only used in calculation of candidates (see toBool lambda)
  162. case EventCondition::CONST_VALUE:
  163. break;
  164. case EventCondition::HAVE_0:
  165. case EventCondition::HAVE_BUILDING_0:
  166. case EventCondition::DESTROY_0:
  167. //TODO: support new condition format
  168. return sptr(Invalid());
  169. default:
  170. assert(0);
  171. }
  172. }
  173. return sptr(Invalid());
  174. }