2024-03-29 07:48:52 +02:00
/*
* MapObjectsEvaluator . cpp , 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
*
*/
2018-07-20 21:14:57 +02:00
# include "StdInc.h"
# include "MapObjectsEvaluator.h"
# include "../../lib/GameConstants.h"
# include "../../lib/VCMI_Lib.h"
2018-08-10 16:36:45 +02:00
# include "../../lib/CCreatureHandler.h"
2018-11-03 13:46:02 +02:00
# include "../../lib/CHeroHandler.h"
2023-06-02 20:47:37 +02:00
# include "../../lib/mapObjectConstructors/AObjectTypeHandler.h"
2018-11-03 13:46:02 +02:00
# include "../../lib/mapObjects/CGHeroInstance.h"
2018-08-10 16:36:45 +02:00
# include "../../lib/mapObjects/CGTownInstance.h"
2018-08-23 19:39:42 +02:00
# include "../../lib/mapObjects/MiscObjects.h"
2018-08-10 16:36:45 +02:00
# include "../../lib/CRandomGenerator.h"
2018-08-23 19:39:42 +02:00
# include "../../lib/spells/CSpellHandler.h"
2018-07-20 21:14:57 +02:00
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 ) ;
2019-09-13 09:33:06 +02:00
if ( handler & & ! handler - > isStaticObject ( ) )
2018-07-20 21:14:57 +02:00
{
2023-04-16 19:42:56 +02:00
if ( handler - > getAiValue ( ) ! = std : : nullopt )
2018-07-28 12:58:18 +02:00
{
2023-04-16 19:42:56 +02:00
objectDatabase [ CompoundMapObjectID ( primaryID , secondaryID ) ] = handler - > getAiValue ( ) . value ( ) ;
2018-07-28 12:58:18 +02:00
}
2018-08-23 19:39:42 +02:00
else //some default handling when aiValue not found, objects that require advanced properties (unavailable from handler) get their value calculated in getObjectValue
2018-07-28 12:58:18 +02:00
{
2018-08-16 21:17:45 +02:00
objectDatabase [ CompoundMapObjectID ( primaryID , secondaryID ) ] = 0 ;
2018-07-28 12:58:18 +02:00
}
2018-07-20 21:14:57 +02:00
}
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
}
2018-07-20 21:14:57 +02:00
}
}
2023-04-16 19:42:56 +02:00
std : : 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 ) ) ;
2023-04-16 19:42:56 +02:00
return std : : optional < int > ( ) ;
2018-07-20 21:14:57 +02:00
}
2018-07-21 14:30:38 +02:00
2023-04-16 19:42:56 +02:00
std : : optional < int > MapObjectsEvaluator : : getObjectValue ( const CGObjectInstance * obj ) const
2018-08-16 21:17:45 +02:00
{
2018-11-03 13:46:02 +02:00
if ( obj - > ID = = Obj : : HERO )
{
//special case handling: in-game heroes have hero ID as object subID, but when reading configs available hero object subID's are hero classes
auto hero = dynamic_cast < const CGHeroInstance * > ( obj ) ;
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
return getObjectValue ( obj - > ID , hero - > type - > heroClass - > getIndex ( ) ) ;
2018-11-03 13:46:02 +02:00
}
2019-06-05 23:07:22 +02:00
else if ( obj - > ID = = Obj : : PRISON )
{
//special case: in-game prison subID is captured hero ID, but config has one subID with index 0 for normal prison - use that one
return getObjectValue ( obj - > ID , 0 ) ;
}
else if ( obj - > ID = = Obj : : CREATURE_GENERATOR1 | | obj - > ID = = Obj : : CREATURE_GENERATOR4 )
2018-08-16 21:17:45 +02:00
{
auto dwelling = dynamic_cast < const CGDwelling * > ( obj ) ;
int aiValue = 0 ;
for ( auto & creLevel : dwelling - > creatures )
{
for ( auto & creatureID : creLevel . second )
{
2023-04-05 02:26:29 +02:00
auto creature = VLC - > creatures ( ) - > getById ( creatureID ) ;
aiValue + = ( creature - > getAIValue ( ) * creature - > getGrowth ( ) ) ;
2018-08-16 21:17:45 +02:00
}
}
return aiValue ;
}
2018-08-23 19:39:42 +02:00
else if ( obj - > ID = = Obj : : ARTIFACT )
{
auto artifactObject = dynamic_cast < const CGArtifact * > ( obj ) ;
switch ( artifactObject - > storedArtifact - > artType - > aClass )
{
case CArtifact : : EartClass : : ART_TREASURE :
return 2000 ;
case CArtifact : : EartClass : : ART_MINOR :
return 5000 ;
case CArtifact : : EartClass : : ART_MAJOR :
return 10000 ;
case CArtifact : : EartClass : : ART_RELIC :
return 20000 ;
case CArtifact : : EartClass : : ART_SPECIAL :
return 20000 ;
default :
return 0 ; //invalid artifact class
}
}
else if ( obj - > ID = = Obj : : SPELL_SCROLL )
{
auto scrollObject = dynamic_cast < const CGArtifact * > ( obj ) ;
2023-04-13 19:40:36 +02:00
auto spell = scrollObject - > storedArtifact - > getScrollSpellID ( ) . toSpell ( ) ;
2018-08-23 19:39:42 +02:00
if ( spell )
{
switch ( spell - > getLevel ( ) )
{
case 0 : return 0 ; //scroll with creature ability? Let's assume it is useless
case 1 : return 1000 ;
case 2 : return 2000 ;
case 3 : return 5000 ;
case 4 : return 10000 ;
case 5 : return 20000 ;
default : logAi - > warn ( " AI detected spell scroll with spell level %s " , spell - > getLevel ( ) ) ;
}
}
else
2023-04-13 19:40:36 +02:00
logAi - > warn ( " AI found spell scroll with invalid spell ID: %s " , scrollObject - > storedArtifact - > getScrollSpellID ( ) ) ;
2018-08-23 19:39:42 +02:00
}
return getObjectValue ( obj - > ID , obj - > subID ) ;
2018-08-16 21:17:45 +02:00
}
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 ) ;
}