2024-01-20 22:54:30 +02:00
|
|
|
/*
|
|
|
|
* ObjectGraph.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 "AINodeStorage.h"
|
|
|
|
#include "../AIUtility.h"
|
|
|
|
|
|
|
|
namespace NKAI
|
|
|
|
{
|
|
|
|
|
|
|
|
class Nullkiller;
|
|
|
|
|
|
|
|
struct ObjectLink
|
|
|
|
{
|
|
|
|
float cost = 100000; // some big number
|
|
|
|
uint64_t danger = 0;
|
2024-03-28 13:39:15 +02:00
|
|
|
std::shared_ptr<ISpecialActionFactory> specialAction;
|
2024-01-20 22:54:30 +02:00
|
|
|
|
2024-02-03 12:20:59 +02:00
|
|
|
bool update(float newCost, uint64_t newDanger)
|
2024-01-20 22:54:30 +02:00
|
|
|
{
|
|
|
|
if(cost > newCost)
|
|
|
|
{
|
|
|
|
cost = newCost;
|
|
|
|
danger = newDanger;
|
2024-02-03 12:20:59 +02:00
|
|
|
|
|
|
|
return true;
|
2024-01-20 22:54:30 +02:00
|
|
|
}
|
2024-02-03 12:20:59 +02:00
|
|
|
|
|
|
|
return false;
|
2024-01-20 22:54:30 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ObjectNode
|
|
|
|
{
|
|
|
|
ObjectInstanceID objID;
|
2024-02-03 12:20:59 +02:00
|
|
|
MapObjectID objTypeID;
|
2024-01-20 22:54:30 +02:00
|
|
|
bool objectExists;
|
|
|
|
std::unordered_map<int3, ObjectLink> connections;
|
|
|
|
|
|
|
|
void init(const CGObjectInstance * obj)
|
|
|
|
{
|
|
|
|
objectExists = true;
|
|
|
|
objID = obj->id;
|
2024-02-03 12:20:59 +02:00
|
|
|
objTypeID = obj->ID;
|
2024-01-20 22:54:30 +02:00
|
|
|
}
|
2024-03-17 09:34:54 +02:00
|
|
|
|
|
|
|
void initJunction()
|
|
|
|
{
|
|
|
|
objectExists = false;
|
|
|
|
objID = ObjectInstanceID();
|
|
|
|
objTypeID = Obj();
|
|
|
|
}
|
2024-01-20 22:54:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class ObjectGraph
|
|
|
|
{
|
|
|
|
std::unordered_map<int3, ObjectNode> nodes;
|
2024-03-28 13:39:15 +02:00
|
|
|
std::unordered_map<int3, ObjectInstanceID> virtualBoats;
|
2024-01-20 22:54:30 +02:00
|
|
|
|
|
|
|
public:
|
2024-03-28 13:39:15 +02:00
|
|
|
ObjectGraph()
|
|
|
|
:nodes(), virtualBoats()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-01-20 22:54:30 +02:00
|
|
|
void updateGraph(const Nullkiller * ai);
|
|
|
|
void addObject(const CGObjectInstance * obj);
|
2024-03-17 09:34:54 +02:00
|
|
|
void registerJunction(const int3 & pos);
|
2024-03-28 13:39:15 +02:00
|
|
|
void addVirtualBoat(const int3 & pos, const CGObjectInstance * shipyard);
|
2024-01-20 22:54:30 +02:00
|
|
|
void connectHeroes(const Nullkiller * ai);
|
2024-02-11 15:27:56 +02:00
|
|
|
void removeObject(const CGObjectInstance * obj);
|
2024-03-17 09:34:54 +02:00
|
|
|
bool tryAddConnection(const int3 & from, const int3 & to, float cost, uint64_t danger);
|
|
|
|
void removeConnection(const int3 & from, const int3 & to);
|
2024-02-03 12:20:59 +02:00
|
|
|
void dumpToLog(std::string visualKey) const;
|
2024-01-20 22:54:30 +02:00
|
|
|
|
2024-03-28 13:39:15 +02:00
|
|
|
bool isVirtualBoat(const int3 & tile) const
|
|
|
|
{
|
|
|
|
return vstd::contains(virtualBoats, tile);
|
|
|
|
}
|
|
|
|
|
2024-03-24 09:32:29 +02:00
|
|
|
void copyFrom(const ObjectGraph & other)
|
|
|
|
{
|
|
|
|
nodes = other.nodes;
|
2024-03-28 13:39:15 +02:00
|
|
|
virtualBoats = other.virtualBoats;
|
2024-03-24 09:32:29 +02:00
|
|
|
}
|
|
|
|
|
2024-01-20 22:54:30 +02:00
|
|
|
template<typename Func>
|
|
|
|
void iterateConnections(const int3 & pos, Func fn)
|
|
|
|
{
|
2024-02-03 12:20:59 +02:00
|
|
|
for(auto & connection : nodes.at(pos).connections)
|
2024-01-20 22:54:30 +02:00
|
|
|
{
|
|
|
|
fn(connection.first, connection.second);
|
|
|
|
}
|
|
|
|
}
|
2024-02-03 12:20:59 +02:00
|
|
|
|
|
|
|
const ObjectNode & getNode(int3 tile) const
|
|
|
|
{
|
|
|
|
return nodes.at(tile);
|
|
|
|
}
|
2024-03-09 16:20:00 +02:00
|
|
|
|
2024-03-17 09:34:54 +02:00
|
|
|
bool hasNodeAt(const int3 & tile) const
|
|
|
|
{
|
|
|
|
return vstd::contains(nodes, tile);
|
|
|
|
}
|
2024-01-20 22:54:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|