1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00
This commit is contained in:
Dydzio 2018-07-22 19:12:11 +02:00
parent 2c1d91e2ff
commit 21c1f47a78
4 changed files with 16 additions and 19 deletions

View File

@ -12,7 +12,7 @@ MapObjectsEvaluator & MapObjectsEvaluator::getInstance()
return *(singletonInstance.get());
}
MapObjectsEvaluator::MapObjectsEvaluator() : objectDatabase(std::map<AiMapObjectID, int>())
MapObjectsEvaluator::MapObjectsEvaluator() : objectDatabase(std::map<CompoundMapObjectID, int>())
{
for(auto primaryID : VLC->objtypeh->knownObjects())
{
@ -21,17 +21,17 @@ MapObjectsEvaluator::MapObjectsEvaluator() : objectDatabase(std::map<AiMapObject
auto handler = VLC->objtypeh->getHandlerFor(primaryID, secondaryID);
if(!handler->isStaticObject() && handler->getRMGInfo().value)
{
AiMapObjectID newObjectType = AiMapObjectID(primaryID, secondaryID);
std::pair<AiMapObjectID, int> newObject = { newObjectType, handler->getRMGInfo().value };
CompoundMapObjectID newObjectType = CompoundMapObjectID(primaryID, secondaryID);
std::pair<CompoundMapObjectID, int> newObject = { newObjectType, handler->getRMGInfo().value };
objectDatabase.insert(newObject);
}
}
}
}
boost::optional<int> MapObjectsEvaluator::getObjectValue(int primaryID, int secondaryID)
boost::optional<int> MapObjectsEvaluator::getObjectValue(int primaryID, int secondaryID) const
{
AiMapObjectID internalIdentifier = AiMapObjectID(primaryID, secondaryID);
CompoundMapObjectID internalIdentifier = CompoundMapObjectID(primaryID, secondaryID);
auto object = objectDatabase.find(internalIdentifier);
if(object != objectDatabase.end())
return object->second;
@ -42,13 +42,13 @@ boost::optional<int> MapObjectsEvaluator::getObjectValue(int primaryID, int seco
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);
CompoundMapObjectID internalIdentifier = CompoundMapObjectID(primaryID, secondaryID);
objectDatabase.insert_or_assign(internalIdentifier, value);
}
void MapObjectsEvaluator::removeObjectData(int primaryID, int secondaryID, int value)
{
AiMapObjectID internalIdentifier = AiMapObjectID(primaryID, secondaryID);
CompoundMapObjectID internalIdentifier = CompoundMapObjectID(primaryID, secondaryID);
vstd::erase_if_present(objectDatabase, internalIdentifier);
}

View File

@ -13,12 +13,12 @@
class MapObjectsEvaluator
{
private:
std::map<AiMapObjectID, int> objectDatabase; //value for each object type
std::map<CompoundMapObjectID, int> objectDatabase; //value for each object type
public:
MapObjectsEvaluator();
static MapObjectsEvaluator & getInstance();
boost::optional<int> getObjectValue(int primaryID, int secondaryID);
boost::optional<int> getObjectValue(int primaryID, int secondaryID) const;
void addObjectData(int primaryID, int secondaryID, int value);
void removeObjectData(int primaryID, int secondaryID, int value);
};

View File

@ -285,7 +285,7 @@ TObjectTypeHandler CObjectClassesHandler::getHandlerFor(std::string type, std::s
throw std::runtime_error("Object type handler not found");
}
TObjectTypeHandler CObjectClassesHandler::getHandlerFor(AiMapObjectID compoundIdentifier) const
TObjectTypeHandler CObjectClassesHandler::getHandlerFor(CompoundMapObjectID compoundIdentifier) const
{
return getHandlerFor(compoundIdentifier.primaryID, compoundIdentifier.secondaryID);
}

View File

@ -65,22 +65,19 @@ struct DLL_LINKAGE RandomMapInfo
}
};
struct DLL_LINKAGE AiMapObjectID
struct DLL_LINKAGE CompoundMapObjectID
{
si32 primaryID;
si32 secondaryID;
AiMapObjectID(si32 primID, si32 secID) : primaryID(primID), secondaryID(secID) {};
CompoundMapObjectID(si32 primID, si32 secID) : primaryID(primID), secondaryID(secID) {};
bool operator<(const AiMapObjectID& other)
bool operator<(const CompoundMapObjectID& other) const
{
if(this->primaryID != other.primaryID)
return this->primaryID < other.primaryID;
else
return this->secondaryID < other.secondaryID;
return (this->primaryID == other.primaryID) && (this->secondaryID == other.secondaryID);
}
bool operator==(const AiMapObjectID& other)
bool operator==(const CompoundMapObjectID& other) const
{
if(this->primaryID == other.primaryID)
return this->secondaryID == other.secondaryID;
@ -298,7 +295,7 @@ public:
/// returns handler for specified object (ID-based). ObjectHandler keeps ownership
TObjectTypeHandler getHandlerFor(si32 type, si32 subtype) const;
TObjectTypeHandler getHandlerFor(std::string type, std::string subtype) const;
TObjectTypeHandler getHandlerFor(AiMapObjectID compoundIdentifier) const;
TObjectTypeHandler getHandlerFor(CompoundMapObjectID compoundIdentifier) const;
std::string getObjectName(si32 type) const;
std::string getObjectName(si32 type, si32 subtype) const;