AIUtility.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /*
  2. * AIUtility.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 "AIUtility.h"
  12. #include "AIGateway.h"
  13. #include "Goals/Goals.h"
  14. #include "../../lib/UnlockGuard.h"
  15. #include "../../lib/CConfigHandler.h"
  16. #include "../../lib/CHeroHandler.h"
  17. #include "../../lib/mapObjects/MapObjects.h"
  18. #include "../../lib/mapping/CMapDefines.h"
  19. #include "../../lib/gameState/QuestInfo.h"
  20. #include "../../lib/GameSettings.h"
  21. #include <vcmi/CreatureService.h>
  22. namespace NKAI
  23. {
  24. const CGObjectInstance * ObjectIdRef::operator->() const
  25. {
  26. return cb->getObj(id, false);
  27. }
  28. ObjectIdRef::operator const CGObjectInstance *() const
  29. {
  30. return cb->getObj(id, false);
  31. }
  32. ObjectIdRef::operator bool() const
  33. {
  34. return cb->getObj(id, false);
  35. }
  36. ObjectIdRef::ObjectIdRef(ObjectInstanceID _id)
  37. : id(_id)
  38. {
  39. }
  40. ObjectIdRef::ObjectIdRef(const CGObjectInstance * obj)
  41. : id(obj->id)
  42. {
  43. }
  44. bool ObjectIdRef::operator<(const ObjectIdRef & rhs) const
  45. {
  46. return id < rhs.id;
  47. }
  48. HeroPtr::HeroPtr(const CGHeroInstance * H)
  49. {
  50. if(!H)
  51. {
  52. //init from nullptr should equal to default init
  53. *this = HeroPtr();
  54. return;
  55. }
  56. h = H;
  57. name = h->getNameTranslated();
  58. hid = H->id;
  59. // infosCount[ai->playerID][hid]++;
  60. }
  61. HeroPtr::HeroPtr()
  62. {
  63. h = nullptr;
  64. hid = ObjectInstanceID();
  65. }
  66. HeroPtr::~HeroPtr()
  67. {
  68. // if(hid >= 0)
  69. // infosCount[ai->playerID][hid]--;
  70. }
  71. bool HeroPtr::operator<(const HeroPtr & rhs) const
  72. {
  73. return hid < rhs.hid;
  74. }
  75. const CGHeroInstance * HeroPtr::get(bool doWeExpectNull) const
  76. {
  77. return get(cb, doWeExpectNull);
  78. }
  79. const CGHeroInstance * HeroPtr::get(const CPlayerSpecificInfoCallback * cb, bool doWeExpectNull) const
  80. {
  81. //TODO? check if these all assertions every time we get info about hero affect efficiency
  82. //
  83. //behave terribly when attempting unauthorized access to hero that is not ours (or was lost)
  84. assert(doWeExpectNull || h);
  85. if(h)
  86. {
  87. auto obj = cb->getObj(hid);
  88. //const bool owned = obj && obj->tempOwner == ai->playerID;
  89. if(doWeExpectNull && !obj)
  90. {
  91. return nullptr;
  92. }
  93. else
  94. {
  95. assert(obj);
  96. //assert(owned);
  97. }
  98. }
  99. return h;
  100. }
  101. const CGHeroInstance * HeroPtr::operator->() const
  102. {
  103. return get();
  104. }
  105. bool HeroPtr::validAndSet() const
  106. {
  107. return get(true);
  108. }
  109. const CGHeroInstance * HeroPtr::operator*() const
  110. {
  111. return get();
  112. }
  113. bool HeroPtr::operator==(const HeroPtr & rhs) const
  114. {
  115. return h == rhs.get(true);
  116. }
  117. bool isSafeToVisit(const CGHeroInstance * h, const CCreatureSet * heroArmy, uint64_t dangerStrength)
  118. {
  119. const ui64 heroStrength = h->getFightingStrength() * heroArmy->getArmyStrength();
  120. if(dangerStrength)
  121. {
  122. return heroStrength / SAFE_ATTACK_CONSTANT > dangerStrength;
  123. }
  124. return true; //there's no danger
  125. }
  126. bool isSafeToVisit(const CGHeroInstance * h, uint64_t dangerStrength)
  127. {
  128. return isSafeToVisit(h, h, dangerStrength);
  129. }
  130. bool isObjectRemovable(const CGObjectInstance * obj)
  131. {
  132. //FIXME: move logic to object property!
  133. switch (obj->ID)
  134. {
  135. case Obj::MONSTER:
  136. case Obj::RESOURCE:
  137. case Obj::CAMPFIRE:
  138. case Obj::TREASURE_CHEST:
  139. case Obj::ARTIFACT:
  140. case Obj::BORDERGUARD:
  141. case Obj::FLOTSAM:
  142. case Obj::PANDORAS_BOX:
  143. case Obj::OCEAN_BOTTLE:
  144. case Obj::SEA_CHEST:
  145. case Obj::SHIPWRECK_SURVIVOR:
  146. case Obj::SPELL_SCROLL:
  147. return true;
  148. break;
  149. default:
  150. return false;
  151. break;
  152. }
  153. }
  154. bool canBeEmbarkmentPoint(const TerrainTile * t, bool fromWater)
  155. {
  156. // TODO: Such information should be provided by pathfinder
  157. // Tile must be free or with unoccupied boat
  158. if(!t->blocked)
  159. {
  160. return true;
  161. }
  162. else if(!fromWater) // do not try to board when in water sector
  163. {
  164. if(t->visitableObjects.size() == 1 && t->topVisitableId() == Obj::BOAT)
  165. return true;
  166. }
  167. return false;
  168. }
  169. bool isObjectPassable(const Nullkiller * ai, const CGObjectInstance * obj)
  170. {
  171. return isObjectPassable(obj, ai->playerID, ai->cb->getPlayerRelations(obj->tempOwner, ai->playerID));
  172. }
  173. // Pathfinder internal helper
  174. bool isObjectPassable(const CGObjectInstance * obj, PlayerColor playerColor, PlayerRelations objectRelations)
  175. {
  176. if((obj->ID == Obj::GARRISON || obj->ID == Obj::GARRISON2)
  177. && objectRelations != PlayerRelations::ENEMIES)
  178. return true;
  179. if(obj->ID == Obj::BORDER_GATE)
  180. {
  181. auto quest = dynamic_cast<const CGKeys *>(obj);
  182. if(quest->wasMyColorVisited(playerColor))
  183. return true;
  184. }
  185. return false;
  186. }
  187. bool isBlockVisitObj(const int3 & pos)
  188. {
  189. if(auto obj = cb->getTopObj(pos))
  190. {
  191. if(obj->isBlockedVisitable()) //we can't stand on that object
  192. return true;
  193. }
  194. return false;
  195. }
  196. creInfo infoFromDC(const dwellingContent & dc)
  197. {
  198. creInfo ci;
  199. ci.count = dc.first;
  200. ci.creID = dc.second.size() ? dc.second.back() : CreatureID(-1); //should never be accessed
  201. if (ci.creID != CreatureID::NONE)
  202. {
  203. ci.level = ci.creID.toCreature()->getLevel(); //this is creature tier, while tryRealize expects dwelling level. Ignore.
  204. }
  205. else
  206. {
  207. ci.level = 0;
  208. }
  209. return ci;
  210. }
  211. bool compareHeroStrength(const CGHeroInstance * h1, const CGHeroInstance * h2)
  212. {
  213. return h1->getTotalStrength() < h2->getTotalStrength();
  214. }
  215. bool compareArmyStrength(const CArmedInstance * a1, const CArmedInstance * a2)
  216. {
  217. return a1->getArmyStrength() < a2->getArmyStrength();
  218. }
  219. bool compareArtifacts(const CArtifactInstance * a1, const CArtifactInstance * a2)
  220. {
  221. auto art1 = a1->artType;
  222. auto art2 = a2->artType;
  223. if(art1->getPrice() == art2->getPrice())
  224. return art1->valOfBonuses(BonusType::PRIMARY_SKILL) > art2->valOfBonuses(BonusType::PRIMARY_SKILL);
  225. else
  226. return art1->getPrice() > art2->getPrice();
  227. }
  228. bool isWeeklyRevisitable(const Nullkiller * ai, const CGObjectInstance * obj)
  229. {
  230. if(!obj)
  231. return false;
  232. //TODO: allow polling of remaining creatures in dwelling
  233. if(const auto * rewardable = dynamic_cast<const CRewardableObject *>(obj))
  234. return rewardable->configuration.getResetDuration() == 7;
  235. if(dynamic_cast<const CGDwelling *>(obj))
  236. return true;
  237. switch(obj->ID)
  238. {
  239. case Obj::HILL_FORT:
  240. return true;
  241. case Obj::BORDER_GATE:
  242. case Obj::BORDERGUARD:
  243. return (dynamic_cast<const CGKeys *>(obj))->wasMyColorVisited(ai->playerID); //FIXME: they could be revisited sooner than in a week
  244. }
  245. return false;
  246. }
  247. uint64_t timeElapsed(std::chrono::time_point<std::chrono::high_resolution_clock> start)
  248. {
  249. auto end = std::chrono::high_resolution_clock::now();
  250. return std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
  251. }
  252. int getDuplicatingSlots(const CArmedInstance * army)
  253. {
  254. int duplicatingSlots = 0;
  255. for(auto stack : army->Slots())
  256. {
  257. if(stack.second->type && army->getSlotFor(stack.second->type) != stack.first)
  258. duplicatingSlots++;
  259. }
  260. return duplicatingSlots;
  261. }
  262. // todo: move to obj manager
  263. bool shouldVisit(const Nullkiller * ai, const CGHeroInstance * h, const CGObjectInstance * obj)
  264. {
  265. auto relations = ai->cb->getPlayerRelations(obj->tempOwner, h->tempOwner);
  266. switch(obj->ID)
  267. {
  268. case Obj::TOWN:
  269. case Obj::HERO: //never visit our heroes at random
  270. return relations == PlayerRelations::ENEMIES; //do not visit our towns at random
  271. case Obj::BORDER_GATE:
  272. {
  273. for(auto q : ai->cb->getMyQuests())
  274. {
  275. if(q.obj == obj)
  276. {
  277. return false; // do not visit guards or gates when wandering
  278. }
  279. }
  280. return true; //we don't have this quest yet
  281. }
  282. case Obj::BORDERGUARD: //open borderguard if possible
  283. return (dynamic_cast<const CGKeys *>(obj))->wasMyColorVisited(ai->playerID);
  284. case Obj::SEER_HUT:
  285. {
  286. for(auto q : ai->cb->getMyQuests())
  287. {
  288. if(q.obj == obj)
  289. {
  290. if(q.quest->checkQuest(h))
  291. return true; //we completed the quest
  292. else
  293. return false; //we can't complete this quest
  294. }
  295. }
  296. return true; //we don't have this quest yet
  297. }
  298. case Obj::CREATURE_GENERATOR1:
  299. {
  300. if(relations == PlayerRelations::ENEMIES)
  301. return true; //flag just in case
  302. if(relations == PlayerRelations::ALLIES)
  303. return false;
  304. const CGDwelling * d = dynamic_cast<const CGDwelling *>(obj);
  305. auto duplicatingSlotsCount = getDuplicatingSlots(h);
  306. for(auto level : d->creatures)
  307. {
  308. for(auto c : level.second)
  309. {
  310. if(level.first
  311. && (h->getSlotFor(CreatureID(c)) != SlotID() || duplicatingSlotsCount > 0)
  312. && ai->cb->getResourceAmount().canAfford(c.toCreature()->getFullRecruitCost()))
  313. {
  314. return true;
  315. }
  316. }
  317. }
  318. return false;
  319. }
  320. case Obj::HILL_FORT:
  321. {
  322. for(auto slot : h->Slots())
  323. {
  324. if(slot.second->type->hasUpgrades())
  325. return true; //TODO: check price?
  326. }
  327. return false;
  328. }
  329. case Obj::MONOLITH_ONE_WAY_ENTRANCE:
  330. case Obj::MONOLITH_ONE_WAY_EXIT:
  331. case Obj::MONOLITH_TWO_WAY:
  332. case Obj::WHIRLPOOL:
  333. return false;
  334. case Obj::SCHOOL_OF_MAGIC:
  335. case Obj::SCHOOL_OF_WAR:
  336. {
  337. if(ai->getFreeGold() < 1000)
  338. return false;
  339. break;
  340. }
  341. case Obj::LIBRARY_OF_ENLIGHTENMENT:
  342. if(h->level < 10)
  343. return false;
  344. break;
  345. case Obj::TREE_OF_KNOWLEDGE:
  346. {
  347. if(ai->heroManager->getHeroRole(h) == HeroRole::SCOUT)
  348. return false;
  349. TResources myRes = ai->getFreeResources();
  350. if(myRes[EGameResID::GOLD] < 2000 || myRes[EGameResID::GEMS] < 10)
  351. return false;
  352. break;
  353. }
  354. case Obj::MAGIC_WELL:
  355. return h->mana < h->manaLimit();
  356. case Obj::PRISON:
  357. return !ai->heroManager->heroCapReached();
  358. case Obj::TAVERN:
  359. case Obj::EYE_OF_MAGI:
  360. case Obj::BOAT:
  361. case Obj::SIGN:
  362. return false;
  363. }
  364. if(obj->wasVisited(h)) //it must pointer to hero instance, heroPtr calls function wasVisited(ui8 player);
  365. return false;
  366. return true;
  367. }
  368. bool townHasFreeTavern(const CGTownInstance * town)
  369. {
  370. if(!town->hasBuilt(BuildingID::TAVERN)) return false;
  371. if(!town->visitingHero) return true;
  372. bool canMoveVisitingHeroToGarrison = !town->getUpperArmy()->stacksCount();
  373. return canMoveVisitingHeroToGarrison;
  374. }
  375. uint64_t getHeroArmyStrengthWithCommander(const CGHeroInstance * hero, const CCreatureSet * heroArmy)
  376. {
  377. auto armyStrength = heroArmy->getArmyStrength();
  378. if(hero && hero->commander && hero->commander->alive)
  379. {
  380. armyStrength += 100 * hero->commander->level;
  381. }
  382. return armyStrength;
  383. }
  384. }