2017-07-13 10:26:03 +02:00
|
|
|
/*
|
|
|
|
* AIUtility.h, 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
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "../../lib/VCMI_Lib.h"
|
|
|
|
#include "../../lib/CBuildingHandler.h"
|
|
|
|
#include "../../lib/CCreatureHandler.h"
|
|
|
|
#include "../../lib/CTownHandler.h"
|
2015-02-02 10:25:26 +02:00
|
|
|
#include "../../lib/spells/CSpellHandler.h"
|
2013-10-18 23:17:25 +03:00
|
|
|
#include "../../lib/CStopWatch.h"
|
2016-09-10 02:32:40 +02:00
|
|
|
#include "../../lib/mapObjects/CObjectHandler.h"
|
|
|
|
#include "../../lib/mapObjects/CGHeroInstance.h"
|
2013-10-18 23:17:25 +03:00
|
|
|
|
2015-12-02 21:39:53 +02:00
|
|
|
class CCallback;
|
|
|
|
|
2013-10-18 23:17:25 +03:00
|
|
|
typedef const int3& crint3;
|
|
|
|
typedef const std::string& crstring;
|
|
|
|
|
|
|
|
const int GOLD_MINE_PRODUCTION = 1000, WOOD_ORE_MINE_PRODUCTION = 2, RESOURCE_MINE_PRODUCTION = 1;
|
|
|
|
const int ACTUAL_RESOURCE_COUNT = 7;
|
|
|
|
const int ALLOWED_ROAMING_HEROES = 8;
|
|
|
|
|
|
|
|
//implementation-dependent
|
|
|
|
extern const double SAFE_ATTACK_CONSTANT;
|
|
|
|
extern const int GOLD_RESERVE;
|
|
|
|
|
2014-02-15 11:10:06 +03:00
|
|
|
//provisional class for AI to store a reference to an owned hero object
|
|
|
|
//checks if it's valid on access, should be used in place of const CGHeroInstance*
|
|
|
|
|
2013-10-18 23:17:25 +03:00
|
|
|
struct HeroPtr
|
|
|
|
{
|
|
|
|
const CGHeroInstance *h;
|
|
|
|
ObjectInstanceID hid;
|
|
|
|
|
|
|
|
public:
|
|
|
|
std::string name;
|
|
|
|
|
2016-08-13 16:44:37 +02:00
|
|
|
|
2013-10-18 23:17:25 +03:00
|
|
|
HeroPtr();
|
|
|
|
HeroPtr(const CGHeroInstance *H);
|
|
|
|
~HeroPtr();
|
|
|
|
|
|
|
|
operator bool() const
|
|
|
|
{
|
|
|
|
return validAndSet();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<(const HeroPtr &rhs) const;
|
|
|
|
const CGHeroInstance *operator->() const;
|
|
|
|
const CGHeroInstance *operator*() const; //not that consistent with -> but all interfaces use CGHeroInstance*, so it's convenient
|
|
|
|
|
|
|
|
const CGHeroInstance *get(bool doWeExpectNull = false) const;
|
|
|
|
bool validAndSet() const;
|
|
|
|
|
|
|
|
|
|
|
|
template <typename Handler> void serialize(Handler &h, const int version)
|
|
|
|
{
|
2017-07-31 15:35:42 +02:00
|
|
|
h & this->h;
|
|
|
|
h & hid;
|
|
|
|
h & name;
|
2013-10-18 23:17:25 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
enum BattleState
|
|
|
|
{
|
|
|
|
NO_BATTLE,
|
|
|
|
UPCOMING_BATTLE,
|
|
|
|
ONGOING_BATTLE,
|
|
|
|
ENDING_BATTLE
|
|
|
|
};
|
|
|
|
|
|
|
|
// AI lives in a dangerous world. CGObjectInstances under pointer may got deleted/hidden.
|
|
|
|
// This class stores object id, so we can detect when we lose access to the underlying object.
|
|
|
|
struct ObjectIdRef
|
|
|
|
{
|
|
|
|
ObjectInstanceID id;
|
|
|
|
|
|
|
|
const CGObjectInstance *operator->() const;
|
|
|
|
operator const CGObjectInstance *() const;
|
|
|
|
|
|
|
|
ObjectIdRef(ObjectInstanceID _id);
|
|
|
|
ObjectIdRef(const CGObjectInstance *obj);
|
|
|
|
|
|
|
|
bool operator<(const ObjectIdRef &rhs) const;
|
|
|
|
|
|
|
|
|
|
|
|
template <typename Handler> void serialize(Handler &h, const int version)
|
|
|
|
{
|
|
|
|
h & id;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TimeCheck
|
|
|
|
{
|
|
|
|
CStopWatch time;
|
|
|
|
std::string txt;
|
|
|
|
TimeCheck(crstring TXT) : txt(TXT)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~TimeCheck()
|
|
|
|
{
|
2016-08-13 16:44:37 +02:00
|
|
|
logAi->trace("Time of %s was %d ms.",txt,time.getDiff());
|
2013-10-18 23:17:25 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-12-12 22:38:12 +02:00
|
|
|
//TODO: replace with vstd::
|
2013-10-18 23:17:25 +03:00
|
|
|
struct AtScopeExit
|
|
|
|
{
|
|
|
|
std::function<void()> foo;
|
|
|
|
AtScopeExit(const std::function<void()> &FOO) : foo(FOO)
|
|
|
|
{}
|
|
|
|
~AtScopeExit()
|
|
|
|
{
|
|
|
|
foo();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class ObjsVector : public std::vector<ObjectIdRef>
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
};
|
|
|
|
|
|
|
|
template<int id>
|
|
|
|
bool objWithID(const CGObjectInstance *obj)
|
|
|
|
{
|
2016-03-12 03:41:27 +02:00
|
|
|
return obj->ID == id;
|
2013-10-18 23:17:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void foreach_tile_pos(std::function<void(const int3& pos)> foo);
|
2014-04-01 14:53:28 +03:00
|
|
|
void foreach_tile_pos(CCallback * cbp, std::function<void(CCallback * cbp, const int3& pos)> foo); // avoid costly retrieval of thread-specific pointer
|
2013-10-18 23:17:25 +03:00
|
|
|
void foreach_neighbour(const int3 &pos, std::function<void(const int3& pos)> foo);
|
2014-04-01 14:53:28 +03:00
|
|
|
void foreach_neighbour(CCallback * cbp, const int3 &pos, std::function<void(CCallback * cbp, const int3& pos)> foo); // avoid costly retrieval of thread-specific pointer
|
2013-10-18 23:17:25 +03:00
|
|
|
|
2014-04-01 14:53:28 +03:00
|
|
|
int howManyTilesWillBeDiscovered(const int3 &pos, int radious, CCallback * cbp);
|
2013-10-18 23:17:25 +03:00
|
|
|
int howManyTilesWillBeDiscovered(int radious, int3 pos, crint3 dir);
|
|
|
|
void getVisibleNeighbours(const std::vector<int3> &tiles, std::vector<int3> &out);
|
|
|
|
|
2014-05-18 14:13:31 +03:00
|
|
|
bool canBeEmbarkmentPoint(const TerrainTile *t, bool fromWater);
|
2013-10-18 23:17:25 +03:00
|
|
|
bool isBlockedBorderGate(int3 tileToHit);
|
2016-11-28 20:29:11 +02:00
|
|
|
bool isBlockVisitObj(const int3 &pos);
|
2013-10-18 23:17:25 +03:00
|
|
|
|
|
|
|
bool isWeeklyRevisitable (const CGObjectInstance * obj);
|
|
|
|
bool shouldVisit (HeroPtr h, const CGObjectInstance * obj);
|
|
|
|
|
|
|
|
ui64 evaluateDanger(const CGObjectInstance *obj);
|
|
|
|
ui64 evaluateDanger(crint3 tile, const CGHeroInstance *visitor);
|
|
|
|
bool isSafeToVisit(HeroPtr h, crint3 tile);
|
2014-04-01 14:53:28 +03:00
|
|
|
bool boundaryBetweenTwoPoints (int3 pos1, int3 pos2, CCallback * cbp);
|
2013-10-18 23:17:25 +03:00
|
|
|
|
|
|
|
bool compareMovement(HeroPtr lhs, HeroPtr rhs);
|
|
|
|
bool compareHeroStrength(HeroPtr h1, HeroPtr h2);
|
|
|
|
bool compareArmyStrength(const CArmedInstance *a1, const CArmedInstance *a2);
|
2015-04-07 22:48:35 +02:00
|
|
|
bool compareArtifacts(const CArtifactInstance *a1, const CArtifactInstance *a2);
|
2013-10-18 23:17:25 +03:00
|
|
|
ui64 howManyReinforcementsCanGet(HeroPtr h, const CGTownInstance *t);
|
2014-06-05 19:52:14 +03:00
|
|
|
int3 whereToExplore(HeroPtr h);
|
2014-09-21 16:42:08 +03:00
|
|
|
|
|
|
|
class CDistanceSorter
|
|
|
|
{
|
|
|
|
const CGHeroInstance * hero;
|
|
|
|
public:
|
|
|
|
CDistanceSorter(const CGHeroInstance * hero): hero(hero) {}
|
|
|
|
|
|
|
|
bool operator ()(const CGObjectInstance *lhs, const CGObjectInstance *rhs);
|
|
|
|
};
|