2018-07-20 21:14:57 +02:00
# include "StdInc.h"
# include "MapObjectsEvaluator.h"
# include "../../lib/GameConstants.h"
# include "../../lib/VCMI_Lib.h"
2018-07-21 11:28:55 +02:00
MapObjectsEvaluator & MapObjectsEvaluator : : getInstance ( )
{
2018-07-21 15:42:17 +02:00
static std : : unique_ptr < MapObjectsEvaluator > singletonInstance ;
2018-07-21 11:28:55 +02:00
if ( singletonInstance = = nullptr )
singletonInstance . reset ( new MapObjectsEvaluator ( ) ) ;
return * ( singletonInstance . get ( ) ) ;
}
2018-07-20 21:14:57 +02:00
2018-07-22 19:53:06 +02:00
MapObjectsEvaluator : : MapObjectsEvaluator ( )
2018-07-20 21:14:57 +02:00
{
for ( auto primaryID : VLC - > objtypeh - > knownObjects ( ) )
{
for ( auto secondaryID : VLC - > objtypeh - > knownSubObjects ( primaryID ) )
{
auto handler = VLC - > objtypeh - > getHandlerFor ( primaryID , secondaryID ) ;
2018-07-27 02:28:12 +02:00
if ( ! handler - > isStaticObject ( ) )
2018-07-20 21:14:57 +02:00
{
2018-07-30 15:32:55 +02:00
if ( handler - > getAiValue ( ) ! = boost : : none )
2018-07-28 12:58:18 +02:00
{
2018-07-30 15:32:55 +02:00
objectDatabase [ CompoundMapObjectID ( primaryID , secondaryID ) ] = handler - > getAiValue ( ) . get ( ) ;
2018-07-28 12:58:18 +02:00
}
2018-07-30 15:32:55 +02:00
else if ( VLC - > objtypeh - > getObjGroupAiValue ( primaryID ) ! = boost : : none ) //if value is not initialized - fallback to default value for this object family if it exists
2018-07-28 12:58:18 +02:00
{
2018-07-30 15:32:55 +02:00
objectDatabase [ CompoundMapObjectID ( primaryID , secondaryID ) ] = VLC - > objtypeh - > getObjGroupAiValue ( primaryID ) . get ( ) ;
2018-07-28 12:58:18 +02:00
}
else
{
objectDatabase [ CompoundMapObjectID ( primaryID , secondaryID ) ] = 0 ; //some default handling when aiValue not found
}
2018-07-20 21:14:57 +02:00
}
2018-07-21 14:30:38 +02:00
}
2018-07-20 21:14:57 +02:00
}
}
2018-07-22 19:12:11 +02:00
boost : : optional < int > MapObjectsEvaluator : : getObjectValue ( int primaryID , int secondaryID ) const
2018-07-20 21:14:57 +02:00
{
2018-07-22 19:12:11 +02:00
CompoundMapObjectID internalIdentifier = CompoundMapObjectID ( primaryID , secondaryID ) ;
2018-07-21 14:30:38 +02:00
auto object = objectDatabase . find ( internalIdentifier ) ;
2018-07-20 21:14:57 +02:00
if ( object ! = objectDatabase . end ( ) )
2018-07-21 14:30:38 +02:00
return object - > second ;
2018-07-21 11:28:55 +02:00
logGlobal - > trace ( " Unknown object for AI, ID: " + std : : to_string ( primaryID ) + " , SubID: " + std : : to_string ( secondaryID ) ) ;
2018-07-20 21:14:57 +02:00
return boost : : optional < int > ( ) ;
}
2018-07-21 14:30:38 +02:00
void MapObjectsEvaluator : : addObjectData ( int primaryID , int secondaryID , int value ) //by current design it updates value if already in AI database
{
2018-07-22 19:12:11 +02:00
CompoundMapObjectID internalIdentifier = CompoundMapObjectID ( primaryID , secondaryID ) ;
2018-07-30 14:07:39 +02:00
objectDatabase [ internalIdentifier ] = value ;
2018-07-21 14:30:38 +02:00
}
2018-07-22 19:53:06 +02:00
void MapObjectsEvaluator : : removeObjectData ( int primaryID , int secondaryID )
2018-07-21 14:30:38 +02:00
{
2018-07-22 19:12:11 +02:00
CompoundMapObjectID internalIdentifier = CompoundMapObjectID ( primaryID , secondaryID ) ;
2018-07-21 14:30:38 +02:00
vstd : : erase_if_present ( objectDatabase , internalIdentifier ) ;
}