2021-05-16 13:45:12 +02:00
|
|
|
/*
|
|
|
|
* DangerHitMapAnalyzer.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
|
|
|
|
|
|
|
|
#include "../Pathfinding/AINodeStorage.h"
|
2024-03-24 09:32:29 +02:00
|
|
|
#include "../Engine/PriorityEvaluator.h"
|
2021-05-16 13:45:12 +02:00
|
|
|
|
2022-09-26 20:01:07 +02:00
|
|
|
namespace NKAI
|
|
|
|
{
|
|
|
|
|
2021-05-16 13:55:57 +02:00
|
|
|
struct ClusterObjectInfo
|
2021-05-16 13:45:12 +02:00
|
|
|
{
|
|
|
|
float priority;
|
|
|
|
float movementCost;
|
|
|
|
uint64_t danger;
|
|
|
|
uint8_t turn;
|
|
|
|
};
|
|
|
|
|
2024-04-14 14:23:44 +02:00
|
|
|
struct ObjectInstanceIDHash
|
|
|
|
{
|
|
|
|
ObjectInstanceID::hash hash;
|
|
|
|
bool equal(ObjectInstanceID o1, ObjectInstanceID o2) const
|
|
|
|
{
|
|
|
|
return o1 == o2;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
using ClusterObjects = tbb::concurrent_hash_map<ObjectInstanceID, ClusterObjectInfo, ObjectInstanceIDHash>;
|
2021-05-16 14:08:56 +02:00
|
|
|
|
2021-05-16 13:45:12 +02:00
|
|
|
struct ObjectCluster
|
|
|
|
{
|
|
|
|
public:
|
2021-05-16 14:08:56 +02:00
|
|
|
ClusterObjects objects;
|
2021-05-16 13:45:12 +02:00
|
|
|
const CGObjectInstance * blocker;
|
|
|
|
|
|
|
|
void reset()
|
|
|
|
{
|
|
|
|
objects.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void addObject(const CGObjectInstance * object, const AIPath & path, float priority);
|
2023-04-17 23:11:16 +02:00
|
|
|
|
|
|
|
ObjectCluster(const CGObjectInstance * blocker): blocker(blocker) {}
|
2021-05-16 13:45:12 +02:00
|
|
|
|
|
|
|
ObjectCluster() : ObjectCluster(nullptr)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-04-14 14:23:44 +02:00
|
|
|
std::vector<const CGObjectInstance *> getObjects(const CPlayerSpecificInfoCallback * cb) const;
|
|
|
|
const CGObjectInstance * calculateCenter(const CPlayerSpecificInfoCallback * cb) const;
|
2021-05-16 13:45:12 +02:00
|
|
|
};
|
|
|
|
|
2024-04-14 14:23:44 +02:00
|
|
|
using ClusterMap = tbb::concurrent_hash_map<ObjectInstanceID, std::shared_ptr<ObjectCluster>, ObjectInstanceIDHash>;
|
2021-05-16 14:08:56 +02:00
|
|
|
|
2021-05-16 13:45:12 +02:00
|
|
|
class ObjectClusterizer
|
|
|
|
{
|
|
|
|
private:
|
2024-03-09 16:20:00 +02:00
|
|
|
static Obj IgnoredObjectTypes[];
|
|
|
|
|
2021-05-16 13:45:12 +02:00
|
|
|
ObjectCluster nearObjects;
|
|
|
|
ObjectCluster farObjects;
|
2021-05-16 14:08:56 +02:00
|
|
|
ClusterMap blockedObjects;
|
2020-05-04 17:58:43 +02:00
|
|
|
const Nullkiller * ai;
|
2024-03-24 09:32:29 +02:00
|
|
|
RewardEvaluator valueEvaluator;
|
2024-04-14 14:23:44 +02:00
|
|
|
bool isUpToDate;
|
|
|
|
std::vector<ObjectInstanceID> invalidated;
|
2021-05-16 13:45:12 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
void clusterize();
|
|
|
|
std::vector<const CGObjectInstance *> getNearbyObjects() const;
|
2021-05-16 13:45:45 +02:00
|
|
|
std::vector<const CGObjectInstance *> getFarObjects() const;
|
2021-05-16 13:45:12 +02:00
|
|
|
std::vector<std::shared_ptr<ObjectCluster>> getLockedClusters() const;
|
|
|
|
const CGObjectInstance * getBlocker(const AIPath & path) const;
|
2024-03-24 09:32:29 +02:00
|
|
|
std::optional<const CGObjectInstance *> getBlocker(const AIPathNodeInfo & node) const;
|
2021-05-16 13:45:12 +02:00
|
|
|
|
2024-04-14 14:23:44 +02:00
|
|
|
ObjectClusterizer(const Nullkiller * ai): ai(ai), valueEvaluator(ai), isUpToDate(false){}
|
|
|
|
|
|
|
|
void validateObjects();
|
|
|
|
void onObjectRemoved(ObjectInstanceID id);
|
|
|
|
void invalidate(ObjectInstanceID id);
|
|
|
|
|
|
|
|
void reset() {
|
|
|
|
isUpToDate = false;
|
|
|
|
invalidated.clear();
|
|
|
|
}
|
2020-05-04 17:58:43 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
bool shouldVisitObject(const CGObjectInstance * obj) const;
|
2024-03-24 09:32:29 +02:00
|
|
|
void clusterizeObject(
|
|
|
|
const CGObjectInstance * obj,
|
|
|
|
PriorityEvaluator * priorityEvaluator,
|
|
|
|
std::vector<AIPath> & pathCache,
|
|
|
|
std::vector<const CGHeroInstance *> & heroes);
|
2021-05-16 13:45:12 +02:00
|
|
|
};
|
2022-09-26 20:01:07 +02:00
|
|
|
|
|
|
|
}
|