2014-04-27 15:19:23 +03:00
/*
2014-06-05 20:26:50 +03:00
* CRewardableConstructor . cpp , part of VCMI engine
2014-04-27 15:19:23 +03:00
*
* 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
*
*/
2017-07-13 10:26:03 +02:00
# include "StdInc.h"
# include "CRewardableConstructor.h"
2023-06-02 20:47:37 +02:00
2024-08-14 15:39:40 +02:00
# include "../json/JsonUtils.h"
2023-06-02 20:47:37 +02:00
# include "../mapObjects/CRewardableObject.h"
2024-07-20 14:55:17 +02:00
# include "../texts/CGeneralTextHandler.h"
2023-04-30 02:39:31 +02:00
# include "../IGameCallback.h"
2024-10-06 21:21:18 +02:00
# include "../CConfigHandler.h"
2014-04-27 20:37:53 +03:00
2022-07-26 15:07:42 +02:00
VCMI_LIB_NAMESPACE_BEGIN
2014-06-14 18:42:13 +03:00
void CRewardableConstructor : : initTypeData ( const JsonNode & config )
2014-05-16 23:50:02 +03:00
{
2023-07-19 21:25:52 +02:00
objectInfo . init ( config , getBaseTextID ( ) ) ;
2023-04-29 22:59:02 +02:00
blockVisit = config [ " blockedVisitable " ] . Bool ( ) ;
2023-03-30 21:29:58 +02:00
if ( ! config [ " name " ] . isNull ( ) )
2024-09-30 12:21:10 +02:00
VLC - > generaltexth - > registerString ( config . getModScope ( ) , getNameTextID ( ) , config [ " name " ] ) ;
2024-08-14 15:39:40 +02:00
2024-10-06 21:21:18 +02:00
if ( settings [ " mods " ] [ " validation " ] . String ( ) ! = " off " )
JsonUtils : : validate ( config , " vcmi:rewardable " , getJsonKey ( ) ) ;
2023-04-29 22:59:02 +02:00
2023-03-30 21:29:58 +02:00
}
bool CRewardableConstructor : : hasNameTextID ( ) const
{
return ! objectInfo . getParameters ( ) [ " name " ] . isNull ( ) ;
2014-05-16 23:50:02 +03:00
}
2024-01-01 16:37:48 +02:00
CGObjectInstance * CRewardableConstructor : : create ( IGameCallback * cb , std : : shared_ptr < const ObjectTemplate > tmpl ) const
2014-05-16 23:50:02 +03:00
{
2024-01-01 16:37:48 +02:00
auto * ret = new CRewardableObject ( cb ) ;
2015-11-14 15:50:29 +02:00
preInitObject ( ret ) ;
2014-05-16 23:50:02 +03:00
ret - > appearance = tmpl ;
2023-04-29 22:59:02 +02:00
ret - > blockVisit = blockVisit ;
2014-05-16 23:50:02 +03:00
return ret ;
2014-04-27 20:37:53 +03:00
}
2024-10-03 14:35:15 +02:00
Rewardable : : Configuration CRewardableConstructor : : generateConfiguration ( IGameCallback * cb , vstd : : RNG & rand , MapObjectID objectID , const std : : map < std : : string , JsonNode > & presetVariables ) const
2014-04-27 20:37:53 +03:00
{
2024-07-12 17:10:03 +02:00
Rewardable : : Configuration result ;
2024-10-03 14:35:15 +02:00
result . variables . preset = presetVariables ;
2024-07-12 17:10:03 +02:00
objectInfo . configureObject ( result , rand , cb ) ;
for ( auto & rewardInfo : result . info )
2023-04-29 22:59:02 +02:00
{
2024-07-12 17:10:03 +02:00
for ( auto & bonus : rewardInfo . reward . bonuses )
2023-04-29 22:59:02 +02:00
{
2024-07-12 17:10:03 +02:00
bonus . source = BonusSource : : OBJECT_TYPE ;
bonus . sid = BonusSourceID ( objectID ) ;
2023-04-29 22:59:02 +02:00
}
2024-07-12 17:10:03 +02:00
}
return result ;
}
void CRewardableConstructor : : configureObject ( CGObjectInstance * object , vstd : : RNG & rng ) const
{
2024-08-30 16:21:44 +02:00
auto * rewardableObject = dynamic_cast < CRewardableObject * > ( object ) ;
2024-07-12 17:10:03 +02:00
2024-08-30 16:21:44 +02:00
if ( ! rewardableObject )
throw std : : runtime_error ( " Object " + std : : to_string ( object - > getObjGroupIndex ( ) ) + " , " + std : : to_string ( object - > getObjTypeIndex ( ) ) + " is not a rewardable object! " ) ;
2024-10-03 14:35:15 +02:00
rewardableObject - > configuration = generateConfiguration ( object - > cb , rng , object - > ID , rewardableObject - > configuration . variables . preset ) ;
2024-08-30 16:21:44 +02:00
rewardableObject - > initializeGuards ( ) ;
if ( rewardableObject - > configuration . info . empty ( ) )
{
if ( objectInfo . getParameters ( ) [ " rewards " ] . isNull ( ) )
logMod - > error ( " Object %s has invalid configuration! No defined rewards found! " , getJsonKey ( ) ) ;
else
logMod - > error ( " Object %s has invalid configuration! Make sure that defined appear chances are continuous! " , getJsonKey ( ) ) ;
2023-04-29 22:59:02 +02:00
}
2014-04-27 20:37:53 +03:00
}
2022-09-11 15:12:35 +02:00
std : : unique_ptr < IObjectInfo > CRewardableConstructor : : getObjectInfo ( std : : shared_ptr < const ObjectTemplate > tmpl ) const
2014-04-27 20:37:53 +03:00
{
2023-04-30 15:13:07 +02:00
return std : : unique_ptr < IObjectInfo > ( new Rewardable : : Info ( objectInfo ) ) ;
2014-04-27 20:37:53 +03:00
}
2022-07-26 15:07:42 +02:00
VCMI_LIB_NAMESPACE_END