AIUtility.cpp 10.0 KB

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