2021-05-15 18:22:44 +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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2021-05-16 14:10:59 +02:00
|
|
|
// Check windows
|
|
|
|
#if _WIN32 || _WIN64
|
|
|
|
#if _WIN64
|
|
|
|
#define ENVIRONMENT64
|
|
|
|
#else
|
|
|
|
#define ENVIRONMENT32
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Check GCC
|
|
|
|
#if __GNUC__
|
|
|
|
#if __x86_64__ || __ppc64__
|
|
|
|
#define ENVIRONMENT64
|
|
|
|
#else
|
|
|
|
#define ENVIRONMENT32
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2021-05-16 14:10:35 +02:00
|
|
|
/*********************** TBB.h ********************/
|
|
|
|
|
|
|
|
#include "tbb/blocked_range.h"
|
|
|
|
#include "tbb/concurrent_hash_map.h"
|
|
|
|
#include "tbb/concurrent_unordered_map.h"
|
|
|
|
#include "tbb/concurrent_unordered_set.h"
|
|
|
|
#include "tbb/concurrent_vector.h"
|
|
|
|
#include "tbb/parallel_for.h"
|
|
|
|
#include "tbb/parallel_invoke.h"
|
|
|
|
|
|
|
|
/*********************** TBB.h ********************/
|
|
|
|
|
2021-05-15 18:22:44 +02:00
|
|
|
#include "../../lib/VCMI_Lib.h"
|
|
|
|
#include "../../lib/CCreatureHandler.h"
|
|
|
|
#include "../../lib/spells/CSpellHandler.h"
|
|
|
|
#include "../../lib/CStopWatch.h"
|
|
|
|
#include "../../lib/mapObjects/CGHeroInstance.h"
|
2022-09-26 20:01:07 +02:00
|
|
|
#include "../../CCallback.h"
|
2021-05-16 14:08:56 +02:00
|
|
|
|
2021-11-23 08:41:03 +02:00
|
|
|
#include <chrono>
|
|
|
|
|
2021-05-15 18:22:44 +02:00
|
|
|
|
2023-04-17 23:11:16 +02:00
|
|
|
using dwellingContent = std::pair<ui32, std::vector<CreatureID>>;
|
2021-05-15 18:22:44 +02:00
|
|
|
|
2022-09-26 20:01:07 +02:00
|
|
|
namespace NKAI
|
|
|
|
{
|
|
|
|
struct creInfo;
|
2023-07-31 16:00:37 +02:00
|
|
|
class AIGateway;
|
2022-09-26 20:01:07 +02:00
|
|
|
class Nullkiller;
|
|
|
|
|
2024-01-10 00:38:54 +02:00
|
|
|
const int GOLD_MINE_PRODUCTION = 1000;
|
|
|
|
const int WOOD_ORE_MINE_PRODUCTION = 2;
|
|
|
|
const int RESOURCE_MINE_PRODUCTION = 1;
|
2021-05-15 18:22:44 +02:00
|
|
|
const int ACTUAL_RESOURCE_COUNT = 7;
|
|
|
|
|
2023-07-31 16:00:37 +02:00
|
|
|
extern thread_local CCallback * cb;
|
2022-09-14 10:06:51 +02:00
|
|
|
|
2021-05-16 13:14:07 +02:00
|
|
|
enum HeroRole
|
|
|
|
{
|
2021-05-16 13:39:47 +02:00
|
|
|
SCOUT = 0,
|
2021-05-16 13:14:07 +02:00
|
|
|
|
2021-05-16 13:39:47 +02:00
|
|
|
MAIN = 1
|
2021-05-16 13:14:07 +02:00
|
|
|
};
|
|
|
|
|
2021-05-15 18:22:44 +02: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*
|
|
|
|
|
|
|
|
struct DLL_EXPORT HeroPtr
|
|
|
|
{
|
|
|
|
const CGHeroInstance * h;
|
|
|
|
ObjectInstanceID hid;
|
|
|
|
|
|
|
|
public:
|
2024-07-03 19:01:38 +02:00
|
|
|
std::string name() const;
|
2021-05-15 18:22:44 +02: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
|
|
|
|
bool operator==(const HeroPtr & rhs) const;
|
|
|
|
bool operator!=(const HeroPtr & rhs) const
|
|
|
|
{
|
|
|
|
return !(*this == rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
const CGHeroInstance * get(bool doWeExpectNull = false) const;
|
2024-04-14 14:23:44 +02:00
|
|
|
const CGHeroInstance * get(const CPlayerSpecificInfoCallback * cb, bool doWeExpectNull = false) const;
|
2021-05-15 18:22:44 +02:00
|
|
|
bool validAndSet() const;
|
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
operator bool() const;
|
|
|
|
|
|
|
|
ObjectIdRef(ObjectInstanceID _id);
|
|
|
|
ObjectIdRef(const CGObjectInstance * obj);
|
|
|
|
|
|
|
|
bool operator<(const ObjectIdRef & rhs) const;
|
|
|
|
};
|
|
|
|
|
2023-08-19 19:48:28 +02:00
|
|
|
template<Obj::Type id>
|
2021-05-15 18:22:44 +02:00
|
|
|
bool objWithID(const CGObjectInstance * obj)
|
|
|
|
{
|
|
|
|
return obj->ID == id;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct creInfo
|
|
|
|
{
|
|
|
|
int count;
|
|
|
|
CreatureID creID;
|
|
|
|
int level;
|
|
|
|
};
|
|
|
|
creInfo infoFromDC(const dwellingContent & dc);
|
|
|
|
|
2022-09-14 10:06:51 +02:00
|
|
|
template<class Func>
|
|
|
|
void foreach_tile_pos(const Func & foo)
|
|
|
|
{
|
|
|
|
// some micro-optimizations since this function gets called a LOT
|
|
|
|
// callback pointer is thread-specific and slow to retrieve -> read map size only once
|
|
|
|
int3 mapSize = cb->getMapSize();
|
2022-09-18 16:39:10 +02:00
|
|
|
for(int z = 0; z < mapSize.z; z++)
|
2022-09-14 10:06:51 +02:00
|
|
|
{
|
2022-09-18 16:39:10 +02:00
|
|
|
for(int x = 0; x < mapSize.x; x++)
|
2022-09-14 10:06:51 +02:00
|
|
|
{
|
2022-09-18 16:39:10 +02:00
|
|
|
for(int y = 0; y < mapSize.y; y++)
|
|
|
|
{
|
|
|
|
foo(int3(x, y, z));
|
|
|
|
}
|
2022-09-14 10:06:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-20 22:54:30 +02:00
|
|
|
template<class Func, class TCallback>
|
|
|
|
void foreach_tile_pos(TCallback * cbp, const Func & foo) // avoid costly retrieval of thread-specific pointer
|
2022-09-14 10:06:51 +02:00
|
|
|
{
|
|
|
|
int3 mapSize = cbp->getMapSize();
|
2022-09-18 16:39:10 +02:00
|
|
|
for(int z = 0; z < mapSize.z; z++)
|
2022-09-14 10:06:51 +02:00
|
|
|
{
|
2022-09-18 16:39:10 +02:00
|
|
|
for(int x = 0; x < mapSize.x; x++)
|
2022-09-14 10:06:51 +02:00
|
|
|
{
|
2022-09-18 16:39:10 +02:00
|
|
|
for(int y = 0; y < mapSize.y; y++)
|
|
|
|
{
|
|
|
|
foo(cbp, int3(x, y, z));
|
|
|
|
}
|
2022-09-14 10:06:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class Func>
|
|
|
|
void foreach_neighbour(const int3 & pos, const Func & foo)
|
|
|
|
{
|
2023-07-31 16:00:37 +02:00
|
|
|
CCallback * cbp = cb; // avoid costly retrieval of thread-specific pointer
|
2022-09-14 10:06:51 +02:00
|
|
|
for(const int3 & dir : int3::getDirs())
|
|
|
|
{
|
|
|
|
const int3 n = pos + dir;
|
|
|
|
if(cbp->isInTheMap(n))
|
|
|
|
foo(pos + dir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class Func>
|
|
|
|
void foreach_neighbour(CCallback * cbp, const int3 & pos, const Func & foo) // avoid costly retrieval of thread-specific pointer
|
|
|
|
{
|
|
|
|
for(const int3 & dir : int3::getDirs())
|
|
|
|
{
|
|
|
|
const int3 n = pos + dir;
|
|
|
|
if(cbp->isInTheMap(n))
|
|
|
|
foo(cbp, pos + dir);
|
|
|
|
}
|
|
|
|
}
|
2021-05-15 18:22:44 +02:00
|
|
|
|
|
|
|
bool canBeEmbarkmentPoint(const TerrainTile * t, bool fromWater);
|
2021-05-16 14:08:56 +02:00
|
|
|
bool isObjectPassable(const Nullkiller * ai, const CGObjectInstance * obj);
|
2023-08-19 20:43:50 +02:00
|
|
|
bool isObjectPassable(const CGObjectInstance * obj, PlayerColor playerColor, PlayerRelations objectRelations);
|
2021-05-15 18:22:44 +02:00
|
|
|
bool isBlockVisitObj(const int3 & pos);
|
|
|
|
|
2024-03-31 17:39:00 +02:00
|
|
|
bool isWeeklyRevisitable(const Nullkiller * ai, const CGObjectInstance * obj);
|
2021-05-15 18:22:44 +02:00
|
|
|
|
|
|
|
bool isObjectRemovable(const CGObjectInstance * obj); //FIXME FIXME: move logic to object property!
|
2024-11-28 13:53:51 +02:00
|
|
|
bool isSafeToVisit(const CGHeroInstance * h, uint64_t dangerStrength, float safeAttackRatio);
|
|
|
|
bool isSafeToVisit(const CGHeroInstance * h, const CCreatureSet *, uint64_t dangerStrength, float safeAttackRatio);
|
2021-05-15 18:22:44 +02:00
|
|
|
|
2024-03-29 20:39:03 +02:00
|
|
|
bool compareHeroStrength(const CGHeroInstance * h1, const CGHeroInstance * h2);
|
2021-05-15 18:22:44 +02:00
|
|
|
bool compareArmyStrength(const CArmedInstance * a1, const CArmedInstance * a2);
|
|
|
|
bool compareArtifacts(const CArtifactInstance * a1, const CArtifactInstance * a2);
|
2022-10-15 14:05:20 +02:00
|
|
|
bool townHasFreeTavern(const CGTownInstance * town);
|
2021-05-15 18:22:44 +02:00
|
|
|
|
2024-04-26 22:18:20 +02:00
|
|
|
uint64_t getHeroArmyStrengthWithCommander(const CGHeroInstance * hero, const CCreatureSet * heroArmy);
|
|
|
|
|
2021-11-23 10:56:06 +02:00
|
|
|
uint64_t timeElapsed(std::chrono::time_point<std::chrono::high_resolution_clock> start);
|
2021-05-16 13:56:13 +02:00
|
|
|
|
2021-05-16 13:56:27 +02:00
|
|
|
// todo: move to obj manager
|
2021-05-16 14:08:56 +02:00
|
|
|
bool shouldVisit(const Nullkiller * ai, const CGHeroInstance * h, const CGObjectInstance * obj);
|
2024-04-20 09:33:37 +02:00
|
|
|
int getDuplicatingSlots(const CArmedInstance * army);
|
2021-05-16 14:08:56 +02:00
|
|
|
|
|
|
|
template <class T>
|
|
|
|
class SharedPool
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
struct External_Deleter
|
|
|
|
{
|
|
|
|
explicit External_Deleter(std::weak_ptr<SharedPool<T>* > pool)
|
|
|
|
: pool(pool)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator()(T * ptr)
|
|
|
|
{
|
|
|
|
std::unique_ptr<T> uptr(ptr);
|
|
|
|
|
|
|
|
if(auto pool_ptr = pool.lock())
|
|
|
|
{
|
|
|
|
(*pool_ptr.get())->add(std::move(uptr));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::weak_ptr<SharedPool<T>* > pool;
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
using ptr_type = std::unique_ptr<T, External_Deleter>;
|
|
|
|
|
2023-04-17 23:11:16 +02:00
|
|
|
SharedPool(std::function<std::unique_ptr<T>()> elementFactory):
|
|
|
|
elementFactory(elementFactory), pool(), instance_tracker(new SharedPool<T> *(this))
|
2021-05-16 14:08:56 +02:00
|
|
|
{}
|
2023-04-17 23:11:16 +02:00
|
|
|
|
2021-05-16 14:08:56 +02:00
|
|
|
void add(std::unique_ptr<T> t)
|
|
|
|
{
|
2021-05-16 14:10:35 +02:00
|
|
|
boost::lock_guard<boost::mutex> lock(sync);
|
2021-05-16 14:08:56 +02:00
|
|
|
pool.push_back(std::move(t));
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr_type acquire()
|
|
|
|
{
|
2021-05-16 14:10:35 +02:00
|
|
|
boost::lock_guard<boost::mutex> lock(sync);
|
2021-05-16 14:08:56 +02:00
|
|
|
bool poolIsEmpty = pool.empty();
|
|
|
|
T * element = poolIsEmpty
|
|
|
|
? elementFactory().release()
|
|
|
|
: pool.back().release();
|
|
|
|
|
|
|
|
ptr_type tmp(
|
|
|
|
element,
|
|
|
|
External_Deleter(std::weak_ptr<SharedPool<T>*>(instance_tracker)));
|
|
|
|
|
|
|
|
if(!poolIsEmpty) pool.pop_back();
|
|
|
|
|
2022-12-07 18:05:47 +02:00
|
|
|
return tmp;
|
2021-05-16 14:08:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool empty() const
|
|
|
|
{
|
|
|
|
return pool.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t size() const
|
|
|
|
{
|
|
|
|
return pool.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<std::unique_ptr<T>> pool;
|
|
|
|
std::function<std::unique_ptr<T>()> elementFactory;
|
|
|
|
std::shared_ptr<SharedPool<T> *> instance_tracker;
|
2021-05-16 14:10:35 +02:00
|
|
|
boost::mutex sync;
|
2021-05-16 14:08:56 +02:00
|
|
|
};
|
2022-09-26 20:01:07 +02:00
|
|
|
|
|
|
|
}
|