GatherTroops.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * GatherTroops.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 "../../../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<VCAI> ai;
  21. extern FuzzyHelper * fh;
  22. using namespace Goals;
  23. bool GatherTroops::operator==(const GatherTroops & other) const
  24. {
  25. return objid == other.objid;
  26. }
  27. int GatherTroops::getCreaturesCount(const CArmedInstance * army)
  28. {
  29. int count = 0;
  30. for(auto stack : army->Slots())
  31. {
  32. if(objid == stack.second->getCreatureID().num)
  33. {
  34. count += stack.second->count;
  35. }
  36. }
  37. return count;
  38. }
  39. TSubgoal GatherTroops::whatToDoToAchieve()
  40. {
  41. logAi->trace("Entering GatherTroops::whatToDoToAchieve");
  42. auto heroes = cb->getHeroesInfo(true);
  43. for(auto hero : heroes)
  44. {
  45. if(getCreaturesCount(hero) >= this->value)
  46. {
  47. logAi->trace("Completing GATHER_TROOPS by hero %s", hero->name);
  48. throw goalFulfilledException(sptr(*this));
  49. }
  50. }
  51. return sptr(Invalid());
  52. }
  53. TGoalVec GatherTroops::getAllPossibleSubgoals()
  54. {
  55. TGoalVec solutions;
  56. //for(const CGTownInstance * t : cb->getTownsInfo())
  57. //{
  58. // int count = getCreaturesCount(t->getUpperArmy());
  59. // if(count >= this->value)
  60. // {
  61. // if(t->visitingHero)
  62. // {
  63. // solutions.push_back(sptr(VisitObj(t->id.getNum()).sethero(t->visitingHero.get())));
  64. // }
  65. // else
  66. // {
  67. // vstd::concatenate(solutions, ai->ah->howToVisitObj(t));
  68. // }
  69. // continue;
  70. // }
  71. // auto creature = VLC->creh->creatures[objid];
  72. // if(t->subID == creature->faction) //TODO: how to force AI to build unupgraded creatures? :O
  73. // {
  74. // auto creatures = vstd::tryAt(t->town->creatures, creature->level - 1);
  75. // if(!creatures)
  76. // continue;
  77. // int upgradeNumber = vstd::find_pos(*creatures, creature->idNumber);
  78. // if(upgradeNumber < 0)
  79. // continue;
  80. // BuildingID bid(BuildingID::DWELL_FIRST + creature->level - 1 + upgradeNumber * GameConstants::CREATURES_PER_TOWN);
  81. // if(t->hasBuilt(bid) && ai->ah->freeResources().canAfford(creature->cost)) //this assumes only creatures with dwellings are assigned to faction
  82. // {
  83. // solutions.push_back(sptr(BuyArmy(t, creature->AIValue * this->value).setobjid(objid)));
  84. // }
  85. // /*else //disable random building requests for now - this code needs to know a lot of town/resource context to do more good than harm
  86. // {
  87. // return sptr(BuildThis(bid, t).setpriority(priority));
  88. // }*/
  89. // }
  90. //}
  91. //for(auto obj : ai->visitableObjs)
  92. //{
  93. // auto d = dynamic_cast<const CGDwelling *>(obj);
  94. // if(!d || obj->ID == Obj::TOWN)
  95. // continue;
  96. // for(auto creature : d->creatures)
  97. // {
  98. // if(creature.first) //there are more than 0 creatures avaliabe
  99. // {
  100. // for(auto type : creature.second)
  101. // {
  102. // if(type == objid && ai->ah->freeResources().canAfford(VLC->creh->creatures[type]->cost))
  103. // vstd::concatenate(solutions, ai->ah->howToVisitObj(obj));
  104. // }
  105. // }
  106. // }
  107. //}
  108. //CreatureID creID = CreatureID(objid);
  109. //vstd::erase_if(solutions, [&](TSubgoal goal)->bool
  110. //{
  111. // return goal->hero && !goal->hero->getSlotFor(creID).validSlot() && !goal->hero->getFreeSlot().validSlot();
  112. //});
  113. return solutions;
  114. //TODO: exchange troops between heroes
  115. }