2017-07-13 10:26:03 +02:00
|
|
|
/*
|
|
|
|
* AIUtility.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
2013-10-18 23:17:25 +03:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "AIUtility.h"
|
|
|
|
#include "VCAI.h"
|
2018-08-10 18:27:57 +02:00
|
|
|
#include "FuzzyHelper.h"
|
2018-12-01 10:30:37 +02:00
|
|
|
#include "Goals/Goals.h"
|
2013-10-18 23:17:25 +03:00
|
|
|
|
|
|
|
#include "../../lib/UnlockGuard.h"
|
|
|
|
#include "../../lib/CConfigHandler.h"
|
2013-10-20 16:51:05 +03:00
|
|
|
#include "../../lib/CHeroHandler.h"
|
2014-06-22 13:39:40 +03:00
|
|
|
#include "../../lib/mapObjects/CBank.h"
|
2015-12-02 21:05:10 +02:00
|
|
|
#include "../../lib/mapObjects/CGTownInstance.h"
|
|
|
|
#include "../../lib/mapObjects/CQuest.h"
|
2015-12-02 22:10:46 +02:00
|
|
|
#include "../../lib/mapping/CMapDefines.h"
|
2013-10-18 23:17:25 +03:00
|
|
|
|
|
|
|
extern boost::thread_specific_ptr<CCallback> cb;
|
|
|
|
extern boost::thread_specific_ptr<VCAI> ai;
|
2018-04-07 10:44:14 +02:00
|
|
|
extern FuzzyHelper * fh;
|
2013-10-18 23:17:25 +03:00
|
|
|
|
|
|
|
//extern static const int3 dirs[8];
|
|
|
|
|
2014-02-15 11:10:06 +03:00
|
|
|
const CGObjectInstance * ObjectIdRef::operator->() const
|
|
|
|
{
|
|
|
|
return cb->getObj(id, false);
|
|
|
|
}
|
|
|
|
|
2018-04-07 10:44:14 +02:00
|
|
|
ObjectIdRef::operator const CGObjectInstance *() const
|
2014-02-15 11:10:06 +03:00
|
|
|
{
|
|
|
|
return cb->getObj(id, false);
|
|
|
|
}
|
|
|
|
|
2018-08-12 13:31:31 +02:00
|
|
|
ObjectIdRef::operator bool() const
|
|
|
|
{
|
|
|
|
return cb->getObj(id, false);
|
|
|
|
}
|
|
|
|
|
2018-04-07 10:44:14 +02:00
|
|
|
ObjectIdRef::ObjectIdRef(ObjectInstanceID _id)
|
|
|
|
: id(_id)
|
2014-02-15 11:10:06 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-04-07 10:44:14 +02:00
|
|
|
ObjectIdRef::ObjectIdRef(const CGObjectInstance * obj)
|
|
|
|
: id(obj->id)
|
2014-02-15 11:10:06 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-04-07 10:44:14 +02:00
|
|
|
bool ObjectIdRef::operator<(const ObjectIdRef & rhs) const
|
2014-02-15 11:10:06 +03:00
|
|
|
{
|
|
|
|
return id < rhs.id;
|
|
|
|
}
|
|
|
|
|
2018-04-07 10:44:14 +02:00
|
|
|
HeroPtr::HeroPtr(const CGHeroInstance * H)
|
2014-02-15 11:10:06 +03:00
|
|
|
{
|
|
|
|
if(!H)
|
|
|
|
{
|
|
|
|
//init from nullptr should equal to default init
|
|
|
|
*this = HeroPtr();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
h = H;
|
2023-01-02 13:27:03 +02:00
|
|
|
name = h->getNameTranslated();
|
2014-02-15 11:10:06 +03:00
|
|
|
hid = H->id;
|
|
|
|
// infosCount[ai->playerID][hid]++;
|
|
|
|
}
|
|
|
|
|
|
|
|
HeroPtr::HeroPtr()
|
|
|
|
{
|
|
|
|
h = nullptr;
|
|
|
|
hid = ObjectInstanceID();
|
|
|
|
}
|
|
|
|
|
|
|
|
HeroPtr::~HeroPtr()
|
|
|
|
{
|
2018-04-07 10:44:14 +02:00
|
|
|
// if(hid >= 0)
|
|
|
|
// infosCount[ai->playerID][hid]--;
|
2014-02-15 11:10:06 +03:00
|
|
|
}
|
|
|
|
|
2018-04-07 10:44:14 +02:00
|
|
|
bool HeroPtr::operator<(const HeroPtr & rhs) const
|
2014-02-15 11:10:06 +03:00
|
|
|
{
|
|
|
|
return hid < rhs.hid;
|
|
|
|
}
|
|
|
|
|
2017-07-15 13:08:20 +02:00
|
|
|
const CGHeroInstance * HeroPtr::get(bool doWeExpectNull) const
|
2014-02-15 11:10:06 +03:00
|
|
|
{
|
|
|
|
//TODO? check if these all assertions every time we get info about hero affect efficiency
|
|
|
|
//
|
|
|
|
//behave terribly when attempting unauthorized access to hero that is not ours (or was lost)
|
|
|
|
assert(doWeExpectNull || h);
|
|
|
|
|
|
|
|
if(h)
|
|
|
|
{
|
|
|
|
auto obj = cb->getObj(hid);
|
|
|
|
const bool owned = obj && obj->tempOwner == ai->playerID;
|
|
|
|
|
|
|
|
if(doWeExpectNull && !owned)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
assert(obj);
|
|
|
|
assert(owned);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
const CGHeroInstance * HeroPtr::operator->() const
|
|
|
|
{
|
|
|
|
return get();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HeroPtr::validAndSet() const
|
|
|
|
{
|
|
|
|
return get(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
const CGHeroInstance * HeroPtr::operator*() const
|
|
|
|
{
|
|
|
|
return get();
|
|
|
|
}
|
|
|
|
|
2018-07-26 12:06:55 +02:00
|
|
|
bool HeroPtr::operator==(const HeroPtr & rhs) const
|
|
|
|
{
|
|
|
|
return h == rhs.get(true);
|
|
|
|
}
|
|
|
|
|
2018-08-27 21:19:11 +02:00
|
|
|
bool CDistanceSorter::operator()(const CGObjectInstance * lhs, const CGObjectInstance * rhs) const
|
2013-10-18 23:17:25 +03:00
|
|
|
{
|
2018-04-07 10:44:14 +02:00
|
|
|
const CGPathNode * ln = ai->myCb->getPathsInfo(hero)->getPathInfo(lhs->visitablePos());
|
|
|
|
const CGPathNode * rn = ai->myCb->getPathsInfo(hero)->getPathInfo(rhs->visitablePos());
|
2014-09-21 16:42:08 +03:00
|
|
|
|
2020-09-21 23:19:40 +02:00
|
|
|
return ln->getCost() < rn->getCost();
|
2013-10-18 23:17:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool isSafeToVisit(HeroPtr h, crint3 tile)
|
2018-08-12 13:31:31 +02:00
|
|
|
{
|
2019-02-10 15:25:17 +02:00
|
|
|
return isSafeToVisit(h, fh->evaluateDanger(tile, h.get()));
|
2018-08-12 13:31:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool isSafeToVisit(HeroPtr h, uint64_t dangerStrength)
|
2013-10-18 23:17:25 +03:00
|
|
|
{
|
2018-04-07 10:44:14 +02:00
|
|
|
const ui64 heroStrength = h->getTotalStrength();
|
2018-08-12 13:31:31 +02:00
|
|
|
|
2013-10-18 23:17:25 +03:00
|
|
|
if(dangerStrength)
|
|
|
|
{
|
2019-03-20 21:58:15 +02:00
|
|
|
return heroStrength / SAFE_ATTACK_CONSTANT > dangerStrength;
|
2013-10-18 23:17:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return true; //there's no danger
|
|
|
|
}
|
|
|
|
|
2018-08-08 10:52:52 +02:00
|
|
|
bool isObjectRemovable(const CGObjectInstance * obj)
|
|
|
|
{
|
|
|
|
//FIXME: move logic to object property!
|
|
|
|
switch (obj->ID)
|
|
|
|
{
|
|
|
|
case Obj::MONSTER:
|
|
|
|
case Obj::RESOURCE:
|
|
|
|
case Obj::CAMPFIRE:
|
|
|
|
case Obj::TREASURE_CHEST:
|
|
|
|
case Obj::ARTIFACT:
|
|
|
|
case Obj::BORDERGUARD:
|
2019-01-26 22:38:57 +02:00
|
|
|
case Obj::FLOTSAM:
|
|
|
|
case Obj::PANDORAS_BOX:
|
|
|
|
case Obj::OCEAN_BOTTLE:
|
|
|
|
case Obj::SEA_CHEST:
|
|
|
|
case Obj::SHIPWRECK_SURVIVOR:
|
|
|
|
case Obj::SPELL_SCROLL:
|
2018-08-08 10:52:52 +02:00
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-04-07 10:44:14 +02:00
|
|
|
bool canBeEmbarkmentPoint(const TerrainTile * t, bool fromWater)
|
2013-10-18 23:17:25 +03:00
|
|
|
{
|
2018-04-07 10:44:14 +02:00
|
|
|
// TODO: Such information should be provided by pathfinder
|
|
|
|
// Tile must be free or with unoccupied boat
|
|
|
|
if(!t->blocked)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if(!fromWater) // do not try to board when in water sector
|
|
|
|
{
|
|
|
|
if(t->visitableObjects.size() == 1 && t->topVisitableId() == Obj::BOAT)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2013-10-18 23:17:25 +03:00
|
|
|
}
|
|
|
|
|
2016-11-28 20:29:11 +02:00
|
|
|
bool isBlockedBorderGate(int3 tileToHit) //TODO: is that function needed? should be handled by pathfinder
|
2013-10-18 23:17:25 +03:00
|
|
|
{
|
2018-04-07 10:44:14 +02:00
|
|
|
if(cb->getTile(tileToHit)->topVisitableId() != Obj::BORDER_GATE)
|
|
|
|
return false;
|
|
|
|
auto gate = dynamic_cast<const CGKeys *>(cb->getTile(tileToHit)->topVisitableObj());
|
2018-08-08 10:52:52 +02:00
|
|
|
return !gate->passableFor(ai->playerID);
|
2013-10-18 23:17:25 +03:00
|
|
|
}
|
2019-01-15 07:52:55 +02:00
|
|
|
|
2018-04-07 10:44:14 +02:00
|
|
|
bool isBlockVisitObj(const int3 & pos)
|
2016-11-28 20:29:11 +02:00
|
|
|
{
|
2018-04-07 10:44:14 +02:00
|
|
|
if(auto obj = cb->getTopObj(pos))
|
|
|
|
{
|
|
|
|
if(obj->blockVisit) //we can't stand on that object
|
2016-11-28 20:29:11 +02:00
|
|
|
return true;
|
2018-04-07 10:44:14 +02:00
|
|
|
}
|
2016-11-28 20:29:11 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2013-10-18 23:17:25 +03:00
|
|
|
|
2018-07-26 12:06:55 +02:00
|
|
|
creInfo infoFromDC(const dwellingContent & dc)
|
|
|
|
{
|
|
|
|
creInfo ci;
|
|
|
|
ci.count = dc.first;
|
|
|
|
ci.creID = dc.second.size() ? dc.second.back() : CreatureID(-1); //should never be accessed
|
|
|
|
if (ci.creID != -1)
|
|
|
|
{
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
ci.cre = VLC->creh->objects[ci.creID];
|
2018-07-26 12:06:55 +02:00
|
|
|
ci.level = ci.cre->level; //this is cretaure tier, while tryRealize expects dwelling level. Ignore.
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ci.cre = nullptr;
|
|
|
|
ci.level = 0;
|
|
|
|
}
|
|
|
|
return ci;
|
|
|
|
}
|
|
|
|
|
2013-10-18 23:17:25 +03:00
|
|
|
bool compareHeroStrength(HeroPtr h1, HeroPtr h2)
|
|
|
|
{
|
|
|
|
return h1->getTotalStrength() < h2->getTotalStrength();
|
|
|
|
}
|
|
|
|
|
2018-04-07 10:44:14 +02:00
|
|
|
bool compareArmyStrength(const CArmedInstance * a1, const CArmedInstance * a2)
|
2013-10-18 23:17:25 +03:00
|
|
|
{
|
|
|
|
return a1->getArmyStrength() < a2->getArmyStrength();
|
2013-10-20 16:51:05 +03:00
|
|
|
}
|
2015-04-07 22:48:35 +02:00
|
|
|
|
2018-04-07 10:44:14 +02:00
|
|
|
bool compareArtifacts(const CArtifactInstance * a1, const CArtifactInstance * a2)
|
2015-04-07 22:48:35 +02:00
|
|
|
{
|
|
|
|
auto art1 = a1->artType;
|
|
|
|
auto art2 = a2->artType;
|
|
|
|
|
2016-09-08 03:26:01 +02:00
|
|
|
if(art1->price == art2->price)
|
|
|
|
return art1->valOfBonuses(Bonus::PRIMARY_SKILL) > art2->valOfBonuses(Bonus::PRIMARY_SKILL);
|
2015-04-07 22:48:35 +02:00
|
|
|
else
|
2019-03-20 21:58:15 +02:00
|
|
|
return art1->price > art2->price;
|
2015-10-12 16:08:18 +02:00
|
|
|
}
|