DeepDecomposer.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Nullkiller.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 "DeepDecomposer.h"
  12. #include "../AIGateway.h"
  13. #include "../Behaviors/CaptureObjectsBehavior.h"
  14. #include "../Behaviors/RecruitHeroBehavior.h"
  15. #include "../Behaviors/BuyArmyBehavior.h"
  16. #include "../Behaviors/StartupBehavior.h"
  17. #include "../Behaviors/DefenceBehavior.h"
  18. #include "../Behaviors/BuildingBehavior.h"
  19. #include "../Behaviors/GatherArmyBehavior.h"
  20. #include "../Behaviors/ClusterBehavior.h"
  21. #include "../Goals/Invalid.h"
  22. #include "../Goals/Composition.h"
  23. namespace NKAI
  24. {
  25. using namespace Goals;
  26. DeepDecomposer::DeepDecomposer(const Nullkiller * ai)
  27. :ai(ai), depth(0)
  28. {
  29. }
  30. void DeepDecomposer::reset()
  31. {
  32. decompositionCache.clear();
  33. goals.clear();
  34. }
  35. void DeepDecomposer::decompose(TGoalVec & result, TSubgoal behavior, int depthLimit)
  36. {
  37. goals.clear();
  38. goals.resize(depthLimit);
  39. decompositionCache.resize(depthLimit);
  40. depth = 0;
  41. goals[0] = {behavior};
  42. while(goals[0].size())
  43. {
  44. bool fromCache;
  45. TSubgoal current = goals[depth].back();
  46. TGoalVec subgoals = decomposeCached(unwrapComposition(current), fromCache);
  47. #if NKAI_TRACE_LEVEL >= 1
  48. logAi->trace("Decomposition level %d returned %d goals", depth, subgoals.size());
  49. #endif
  50. if(depth < depthLimit - 1)
  51. {
  52. goals[depth + 1].clear();
  53. }
  54. for(TSubgoal subgoal : subgoals)
  55. {
  56. if(subgoal->invalid())
  57. continue;
  58. if(subgoal->isElementar())
  59. {
  60. // need to get rid of priority control in behaviors like Startup to avoid this check.
  61. // 0 - goals directly from behavior
  62. Goals::TSubgoal task = depth >= 1 ? aggregateGoals(0, subgoal) : subgoal;
  63. // Issue with CGameHandler::spawnWanderingMonsters, see getFreeTiles(tiles, true);
  64. // danger not linked GraphPaths::addChainInfo, so spawning only with nearby unblocked
  65. #if NKAI_TRACE_LEVEL >= 1
  66. logAi->trace("Found task %s", task->toString());
  67. #endif
  68. if(!isCompositionLoop(subgoal))
  69. {
  70. result.push_back(task);
  71. if(!fromCache)
  72. {
  73. addToCache(subgoal);
  74. }
  75. }
  76. }
  77. else if(depth < depthLimit - 1)
  78. {
  79. #if NKAI_TRACE_LEVEL >= 1
  80. logAi->trace("Found abstract goal %s", subgoal->toString());
  81. #endif
  82. if(!isCompositionLoop(subgoal))
  83. {
  84. // depth 0 gives reward, deepest goal - task to execute, all the rest is not important
  85. // so avoid details to reduce decomposition complexity but they can give some negative effect so
  86. auto goalToAdd = depth >= 1 ? unwrapComposition(subgoal) : subgoal;
  87. if(!vstd::contains(goals[depth + 1], goalToAdd))
  88. {
  89. goals[depth + 1].push_back(subgoal);
  90. }
  91. }
  92. }
  93. }
  94. if(depth < depthLimit - 1 && goals[depth + 1].size())
  95. {
  96. depth++;
  97. }
  98. else
  99. {
  100. goals[depth].pop_back();
  101. while(depth > 0 && goals[depth].empty())
  102. {
  103. depth--;
  104. goals[depth].pop_back();
  105. }
  106. }
  107. }
  108. }
  109. Goals::TSubgoal DeepDecomposer::aggregateGoals(int startDepth, TSubgoal last)
  110. {
  111. Goals::Composition composition;
  112. for(int i = 0; i <= depth; i++)
  113. {
  114. composition.addNext(goals[i].back());
  115. }
  116. composition.addNext(last);
  117. return sptr(composition);
  118. }
  119. Goals::TSubgoal DeepDecomposer::unwrapComposition(Goals::TSubgoal goal)
  120. {
  121. return goal->goalType == Goals::COMPOSITION ? goal->decompose(ai).back() : goal;
  122. }
  123. bool isEquivalentGoals(TSubgoal goal1, TSubgoal goal2)
  124. {
  125. if(goal1 == goal2) return true;
  126. if(goal1->goalType == Goals::CAPTURE_OBJECT && goal2->goalType == Goals::CAPTURE_OBJECT)
  127. {
  128. auto o1 = cb->getObj(ObjectInstanceID(goal1->objid));
  129. auto o2 = cb->getObj(ObjectInstanceID(goal2->objid));
  130. return o1->ID == Obj::SHIPYARD && o1->ID == o2->ID;
  131. }
  132. return false;
  133. }
  134. bool DeepDecomposer::isCompositionLoop(TSubgoal goal)
  135. {
  136. auto goalsToTest = goal->goalType == Goals::COMPOSITION ? goal->decompose(ai) : TGoalVec{goal};
  137. for(auto goalToTest : goalsToTest)
  138. {
  139. for(int i = depth; i >= 0; i--)
  140. {
  141. auto parent = unwrapComposition(goals[i].back());
  142. if(isEquivalentGoals(parent, goalToTest))
  143. {
  144. return true;
  145. }
  146. }
  147. }
  148. return false;
  149. }
  150. TGoalVec DeepDecomposer::decomposeCached(TSubgoal goal, bool & fromCache)
  151. {
  152. #if NKAI_TRACE_LEVEL >= 1
  153. logAi->trace("Decomposing %s, level %s", goal->toString(), depth);
  154. #endif
  155. if(goal->hasHash())
  156. {
  157. for(int i = 0; i <= depth; i++)
  158. {
  159. auto cached = decompositionCache[i].find(goal);
  160. if(cached != decompositionCache[i].end())
  161. {
  162. #if NKAI_TRACE_LEVEL >= 1
  163. logAi->trace("Use decomposition cache for %s, level: %d", goal->toString(), depth);
  164. #endif
  165. fromCache = true;
  166. return cached->second;
  167. }
  168. }
  169. decompositionCache[depth][goal] = {}; // if goal decomposition yields no goals we still need it in cache to not decompose again
  170. }
  171. #if NKAI_TRACE_LEVEL >= 2
  172. logAi->trace("Calling decompose on %s, level %s", goal->toString(), depth);
  173. #endif
  174. fromCache = false;
  175. return goal->decompose(ai);
  176. }
  177. void DeepDecomposer::addToCache(TSubgoal goal)
  178. {
  179. bool trusted = true;
  180. for(int parentDepth = 1; parentDepth <= depth; parentDepth++)
  181. {
  182. TSubgoal parent = unwrapComposition(goals[parentDepth].back());
  183. if(parent->hasHash())
  184. {
  185. auto solution = parentDepth < depth ? aggregateGoals(parentDepth + 1, goal) : goal;
  186. #if NKAI_TRACE_LEVEL >= 2
  187. logAi->trace("Adding %s to decomosition cache of %s at level %d", solution->toString(), parent->toString(), parentDepth);
  188. #endif
  189. decompositionCache[parentDepth][parent].push_back(solution);
  190. if(trusted && parentDepth != 0)
  191. {
  192. decompositionCache[0][parent].push_back(solution);
  193. trusted = false;
  194. }
  195. }
  196. }
  197. }
  198. }