AIUtility.cpp 9.6 KB

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