1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

Polish object database, add data operations

This commit is contained in:
Dydzio 2018-07-21 14:30:38 +02:00
parent 36f501ebf3
commit 82da82fbeb
2 changed files with 49 additions and 15 deletions

View File

@ -12,35 +12,43 @@ MapObjectsEvaluator & MapObjectsEvaluator::getInstance()
return *(singletonInstance.get());
}
MapObjectsEvaluator::MapObjectsEvaluator() : objectDatabase(std::map<int, std::map<int, int>>())
MapObjectsEvaluator::MapObjectsEvaluator() : objectDatabase(std::map<AiMapObjectID, int>())
{
for(auto primaryID : VLC->objtypeh->knownObjects())
{
auto newObject = std::pair<int, std::map<int, int>>(primaryID, std::map<int, int>());
for(auto secondaryID : VLC->objtypeh->knownSubObjects(primaryID))
{
auto handler = VLC->objtypeh->getHandlerFor(primaryID, secondaryID);
if(!handler->isStaticObject() && handler->getRMGInfo().value)
{
newObject.second.insert(std::pair<int,int>(secondaryID, handler->getRMGInfo().value));
}
}
AiMapObjectID newObjectType = AiMapObjectID(primaryID, secondaryID);
std::pair<AiMapObjectID, int> newObject = { newObjectType, handler->getRMGInfo().value };
objectDatabase.insert(newObject);
}
}
}
}
boost::optional<int> MapObjectsEvaluator::getObjectValue(int primaryID, int secondaryID)
{
auto object = objectDatabase.find(primaryID);
AiMapObjectID internalIdentifier = AiMapObjectID(primaryID, secondaryID);
auto object = objectDatabase.find(internalIdentifier);
if(object != objectDatabase.end())
{
auto subobjects = (*object).second;
auto desiredObject = subobjects.find(secondaryID);
if(desiredObject != subobjects.end())
{
return (*desiredObject).second;
}
}
return object->second;
logGlobal->trace("Unknown object for AI, ID: " + std::to_string(primaryID) + ", SubID: " + std::to_string(secondaryID));
return boost::optional<int>();
}
void MapObjectsEvaluator::addObjectData(int primaryID, int secondaryID, int value) //by current design it updates value if already in AI database
{
AiMapObjectID internalIdentifier = AiMapObjectID(primaryID, secondaryID);
objectDatabase.insert_or_assign(internalIdentifier, value);
}
void MapObjectsEvaluator::removeObjectData(int primaryID, int secondaryID, int value)
{
AiMapObjectID internalIdentifier = AiMapObjectID(primaryID, secondaryID);
vstd::erase_if_present(objectDatabase, internalIdentifier);
}

View File

@ -9,15 +9,41 @@
*/
#pragma once
struct AiMapObjectID
{
int primaryID;
int secondaryID;
AiMapObjectID(int primID, int secID) : primaryID(primID), secondaryID(secID) {};
};
inline bool operator<(const AiMapObjectID& obj1, const AiMapObjectID& obj2)
{
if(obj1.primaryID != obj2.primaryID)
return obj1.primaryID < obj2.primaryID;
else
return obj1.secondaryID < obj2.secondaryID;
}
inline bool operator==(const AiMapObjectID& obj1, const AiMapObjectID& obj2)
{
if(obj1.primaryID == obj2.primaryID)
return obj1.secondaryID == obj2.secondaryID;
return false;
}
class MapObjectsEvaluator
{
private:
std::map<int, std::map<int, int>> objectDatabase; //each object contains map of subobjects with their values (std::map<ObjID, std::map<SubObjID, Value>>)
std::map<AiMapObjectID, int> objectDatabase; //value for each object type
static std::unique_ptr<MapObjectsEvaluator> singletonInstance;
public:
MapObjectsEvaluator();
static MapObjectsEvaluator & getInstance();
boost::optional<int> getObjectValue(int primaryID, int secondaryID);
void addObjectData(int primaryID, int secondaryID, int value);
void removeObjectData(int primaryID, int secondaryID, int value);
};