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 =
"<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>" +
"<CivilCentre/>" +
2012-02-05 00:42:55 +01:00
"<DefenseTower>25</DefenseTower>" +
2011-08-31 04:07:35 +02:00
"<Fortress>10</Fortress>" +
2013-04-29 00:51:08 +02:00
"<Wonder>1</Wonder>" +
2012-11-07 18:56:14 +01:00
"<Hero>1</Hero>" +
2011-08-06 10:11:05 +02:00
"<Special>" +
"<LimitPerCivCentre>1</LimitPerCivCentre>" +
"</Special>" +
2013-05-19 10:19:16 +02:00
"<Apadana>1</Apadana>" +
2013-08-26 00:20:09 +02:00
"<Monument>5</Monument>" +
2011-08-06 10:11:05 +02:00
"</Limits>" +
"</a:example>" +
2010-12-12 00:19:17 +01:00
"<element name='LimitMultiplier'>" +
"<ref name='positiveDecimal'/>" +
"</element>" +
"<element name='Limits'>" +
"<zeroOrMore>" +
2012-11-07 18:56:14 +01:00
"<element a:help='Specifies a category of building/unit on which to apply this limit. See BuildRestrictions/TrainingRestrictions for list of categories.'>" +
2010-12-12 00:19:17 +01:00
"<anyName />" +
2011-08-06 10:11:05 +02:00
"<choice>" +
"<text />" +
"<element name='LimitPerCivCentre' a:help='Specifies that this limit is per number of civil centres.'>" +
"<data type='nonNegativeInteger'/>" +
"</element>" +
"</choice>" +
2010-12-12 00:19:17 +01:00
"</element>" +
"</zeroOrMore>" +
"</element>" ;
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 = { } ;
2010-12-12 00:19:17 +01:00
for ( var category in this . template . Limits )
{
2011-08-06 10:11:05 +02:00
this . limit [ category ] = this . template . Limits [ category ] ;
this . count [ category ] = 0 ;
2010-12-12 00:19:17 +01:00
}
} ;
2012-11-07 18:56:14 +01:00
EntityLimits . prototype . IncreaseCount = 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 . DecreaseCount = 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 . IncrementCount = function ( category )
{
this . IncreaseCount ( category , 1 ) ;
} ;
EntityLimits . prototype . DecrementCount = function ( category )
{
this . DecreaseCount ( category , 1 ) ;
} ;
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
} ;
2012-11-07 18:56:14 +01:00
EntityLimits . prototype . AllowedToCreate = function ( limitType , category , count )
2010-12-12 00:19:17 +01:00
{
2011-08-06 10:11:05 +02:00
// TODO: The UI should reflect this before the user tries to place the building,
// since the limits are independent of placement location
// Allow unspecified categories and those with no limit
if ( this . count [ category ] === undefined || this . limit [ category ] === undefined )
return true ;
// Rather than complicating the schema unecessarily, just handle special cases here
if ( this . limit [ category ] . LimitPerCivCentre !== undefined )
{
if ( this . count [ category ] >= this . count [ "CivilCentre" ] * this . limit [ category ] . LimitPerCivCentre )
2010-12-12 00:19:17 +01:00
{
2012-11-07 18:56:14 +01:00
var cmpPlayer = Engine . QueryInterface ( this . entity , IID _Player ) ;
var notification = {
"player" : cmpPlayer . GetPlayerID ( ) ,
"message" : category + " " + limitType + " limit of " +
this . limit [ category ] . LimitPerCivCentre + " per civil centre reached"
} ;
2010-12-12 00:19:17 +01:00
var cmpGUIInterface = Engine . QueryInterface ( SYSTEM _ENTITY , IID _GuiInterface ) ;
cmpGUIInterface . PushNotification ( notification ) ;
2011-08-06 10:11:05 +02:00
2010-12-12 00:19:17 +01:00
return false ;
}
}
2012-11-07 18:56:14 +01:00
else 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 ( ) ,
"message" : category + " " + limitType + " limit of " + this . limit [ category ] + " 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 )
{
// TODO: The UI should reflect this before the user tries to place the building,
// since the limits are independent of placement location
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 )
2011-08-16 06:44:15 +02:00
{
2012-11-07 18:56:14 +01:00
// This automatically updates entity counts
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 )
2011-08-16 06:44:15 +02:00
{
var playerID = ( Engine . QueryInterface ( this . entity , IID _Player ) ) . GetPlayerID ( ) ;
if ( msg . from == playerID )
2012-11-07 18:56:14 +01:00
this . DecrementCount ( category ) ;
2011-08-16 06:44:15 +02:00
if ( msg . to == playerID )
2012-11-07 18:56:14 +01:00
this . IncrementCount ( category ) ;
2011-08-16 06:44:15 +02:00
}
} ;
2012-11-07 18:56:14 +01:00
Engine . RegisterComponentType ( IID _EntityLimits , "EntityLimits" , EntityLimits ) ;