2012-11-07 18:56:14 +01:00
function EntityLimits ( ) { }
2010-12-12 00:19:17 +01:00
2012-11-07 18:56:14 +01:00
EntityLimits . prototype . Schema =
2013-10-06 05:13:46 +02:00
"<a:help>Specifies per category limits on number of entities (buildings or units) that can be created for each player</a:help>" +
2011-08-06 10:11:05 +02:00
"<a:example>" +
"<Limits>" +
2013-12-27 11:58:48 +01:00
"<DefenseTower>25</DefenseTower>" +
"<Fortress>10</Fortress>" +
"<Wonder>1</Wonder>" +
"<Hero>1</Hero>" +
"<Apadana>1</Apadana>" +
"<Monument>5</Monument>" +
2011-08-06 10:11:05 +02:00
"</Limits>" +
2013-12-27 11:58:48 +01:00
"<LimitChangers>" +
"<Monument>" +
"<CivCentre>2</CivCentre>" +
"</Monument>" +
"</LimitChangers>" +
2011-08-06 10:11:05 +02:00
"</a:example>" +
2010-12-12 00:19:17 +01:00
"<element name='Limits'>" +
"<zeroOrMore>" +
2013-10-06 05:13:46 +02:00
"<element a:help='Specifies a category of building/unit on which to apply this limit. See BuildRestrictions/TrainingRestrictions for possible categories'>" +
2010-12-12 00:19:17 +01:00
"<anyName />" +
2013-09-15 10:19:50 +02:00
"<data type='integer'/>" +
2010-12-12 00:19:17 +01:00
"</element>" +
"</zeroOrMore>" +
2013-12-27 11:58:48 +01:00
"</element>" +
"<element name='LimitChangers'>" +
"<zeroOrMore>" +
"<element a:help='Specifies a category of building/unit on which to apply this limit. See BuildRestrictions/TrainingRestrictions for possible categories'>" +
"<anyName />" +
"<zeroOrMore>" +
"<element a:help='Specifies the class that changes the entity limit'>" +
"<anyName />" +
"<data type='integer'/>" +
"</element>" +
"</zeroOrMore>" +
"</element>" +
"</zeroOrMore>" +
2010-12-12 00:19:17 +01:00
"</element>" ;
2013-12-27 11:58:48 +01:00
2011-08-06 10:11:05 +02:00
/ *
* TODO : Use an inheriting player _ { civ } . xml template for civ - specific limits
* /
2012-11-07 18:56:14 +01:00
const TRAINING = "training" ;
const BUILD = "build" ;
EntityLimits . prototype . Init = function ( )
2010-12-12 00:19:17 +01:00
{
2011-11-14 01:02:54 +01:00
this . limit = { } ;
this . count = { } ;
2013-12-27 11:58:48 +01:00
this . changers = { } ;
2010-12-12 00:19:17 +01:00
for ( var category in this . template . Limits )
{
2013-09-15 10:19:50 +02:00
this . limit [ category ] = + this . template . Limits [ category ] ;
2011-08-06 10:11:05 +02:00
this . count [ category ] = 0 ;
2013-12-27 11:58:48 +01:00
if ( ! ( category in this . template . LimitChangers ) )
continue ;
this . changers [ category ] = { } ;
for ( var c in this . template . LimitChangers [ category ] )
this . changers [ category ] [ c ] = + this . template . LimitChangers [ category ] [ c ] ;
2010-12-12 00:19:17 +01:00
}
} ;
2013-12-27 11:58:48 +01:00
EntityLimits . prototype . ChangeLimit = function ( category , value )
2013-09-15 10:19:50 +02:00
{
this . limit [ category ] += value ;
} ;
2013-12-27 11:58:48 +01:00
EntityLimits . prototype . ChangeCount = function ( category , value )
2010-12-12 00:19:17 +01:00
{
2011-08-06 10:11:05 +02:00
if ( this . count [ category ] !== undefined )
2012-11-07 18:56:14 +01:00
this . count [ category ] += value ;
2010-12-12 00:19:17 +01:00
} ;
2012-11-07 18:56:14 +01:00
EntityLimits . prototype . GetLimits = function ( )
2011-10-21 01:35:52 +02:00
{
2011-11-14 01:02:54 +01:00
return this . limit ;
2011-10-21 01:35:52 +02:00
} ;
2012-11-07 18:56:14 +01:00
EntityLimits . prototype . GetCounts = function ( )
2011-10-21 01:35:52 +02:00
{
2011-11-14 01:02:54 +01:00
return this . count ;
2011-10-21 01:35:52 +02:00
} ;
2013-12-27 11:58:48 +01:00
EntityLimits . prototype . GetLimitChangers = function ( )
2010-12-12 00:19:17 +01:00
{
2013-12-27 11:58:48 +01:00
return this . changers ;
} ;
2011-08-06 10:11:05 +02:00
2013-12-27 11:58:48 +01:00
EntityLimits . prototype . AllowedToCreate = function ( limitType , category , count )
{
2011-08-06 10:11:05 +02:00
// Allow unspecified categories and those with no limit
if ( this . count [ category ] === undefined || this . limit [ category ] === undefined )
return true ;
2013-09-15 10:19:50 +02:00
if ( this . count [ category ] + count > this . limit [ category ] )
2010-12-12 00:19:17 +01:00
{
2011-08-16 06:44:15 +02:00
var cmpPlayer = Engine . QueryInterface ( this . entity , IID _Player ) ;
2012-11-07 18:56:14 +01:00
var notification = {
"player" : cmpPlayer . GetPlayerID ( ) ,
2014-05-19 09:35:05 +02:00
"translateMessage" : true ,
"translateParameters" : [ "category" ] ,
"parameters" : { "category" : category , "limit" : this . limit [ category ] } ,
} ;
if ( limitType == BUILD )
notification . message = markForTranslation ( "%(category)s build limit of %(limit)s reached" ) ;
else if ( limitType == TRAINING )
notification . message = markForTranslation ( "%(category)s training limit of %(limit)s reached" ) ;
else
{
warn ( "EntityLimits.js: Unknown LimitType " + limitType )
notification . message = markForTranslation ( "%(category)s limit of %(limit)s reached" ) ;
}
2011-08-06 10:11:05 +02:00
var cmpGUIInterface = Engine . QueryInterface ( SYSTEM _ENTITY , IID _GuiInterface ) ;
cmpGUIInterface . PushNotification ( notification ) ;
return false ;
2010-12-12 00:19:17 +01:00
}
2011-08-06 10:11:05 +02:00
return true ;
2012-11-07 18:56:14 +01:00
}
EntityLimits . prototype . AllowedToBuild = function ( category )
{
2013-04-29 02:36:21 +02:00
// We pass count 0 as the creation of the building has already taken place and
// the ownership has been set (triggering OnGlobalOwnershipChanged)
return this . AllowedToCreate ( BUILD , category , 0 ) ;
2012-11-07 18:56:14 +01:00
} ;
EntityLimits . prototype . AllowedToTrain = function ( category , count )
{
return this . AllowedToCreate ( TRAINING , category , count ) ;
2010-12-12 00:19:17 +01:00
} ;
2012-11-07 18:56:14 +01:00
EntityLimits . prototype . OnGlobalOwnershipChanged = function ( msg )
2013-12-27 11:58:48 +01:00
{
// check if we are adding or removing an entity from this player
var cmpPlayer = Engine . QueryInterface ( this . entity , IID _Player ) ;
if ( ! cmpPlayer )
{
error ( "EntityLimits component is defined on a non-player entity" ) ;
return ;
}
if ( msg . from == cmpPlayer . GetPlayerID ( ) )
var modifier = - 1 ;
else if ( msg . to == cmpPlayer . GetPlayerID ( ) )
var modifier = 1 ;
else
return ;
// Update entity counts
2012-11-07 18:56:14 +01:00
var category = null ;
2011-08-16 06:44:15 +02:00
var cmpBuildRestrictions = Engine . QueryInterface ( msg . entity , IID _BuildRestrictions ) ;
if ( cmpBuildRestrictions )
2012-11-07 18:56:14 +01:00
category = cmpBuildRestrictions . GetCategory ( ) ;
var cmpTrainingRestrictions = Engine . QueryInterface ( msg . entity , IID _TrainingRestrictions ) ;
if ( cmpTrainingRestrictions )
category = cmpTrainingRestrictions . GetCategory ( ) ;
if ( category )
2013-12-27 11:58:48 +01:00
this . ChangeCount ( category , modifier ) ;
// Update entity limits
var cmpIdentity = Engine . QueryInterface ( msg . entity , IID _Identity ) ;
if ( ! cmpIdentity )
return ;
2014-05-07 10:24:31 +02:00
// foundations shouldn't change the entity limits until they're completed
var cmpFoundation = Engine . QueryInterface ( msg . entity , IID _Foundation ) ;
if ( cmpFoundation )
return ;
2013-12-27 11:58:48 +01:00
var classes = cmpIdentity . GetClassesList ( ) ;
for ( var category in this . changers )
for ( var c in this . changers [ category ] )
if ( classes . indexOf ( c ) >= 0 )
this . ChangeLimit ( category , modifier * this . changers [ category ] [ c ] ) ;
2011-08-16 06:44:15 +02:00
} ;
2012-11-07 18:56:14 +01:00
Engine . RegisterComponentType ( IID _EntityLimits , "EntityLimits" , EntityLimits ) ;