2010-12-12 00:19:17 +01:00
function BuildLimits ( ) { }
BuildLimits . prototype . Schema =
2011-08-06 10:11:05 +02:00
"<a:help>Specifies per category limits on number of buildings that can be constructed for each player.</a:help>" +
"<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>" +
2011-08-06 10:11:05 +02:00
"<Special>" +
"<LimitPerCivCentre>1</LimitPerCivCentre>" +
"</Special>" +
"</Limits>" +
"</a:example>" +
2010-12-12 00:19:17 +01:00
"<element name='LimitMultiplier'>" +
"<ref name='positiveDecimal'/>" +
"</element>" +
"<element name='Limits'>" +
"<zeroOrMore>" +
2011-08-06 10:11:05 +02:00
"<element a:help='Specifies a category of building on which to apply this limit. See BuildRestrictions 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
* /
2010-12-12 00:19:17 +01:00
BuildLimits . prototype . Init = function ( )
{
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
}
} ;
BuildLimits . prototype . IncrementCount = function ( category )
{
2011-08-06 10:11:05 +02:00
if ( this . count [ category ] !== undefined )
{
this . count [ category ] ++ ;
}
2010-12-12 00:19:17 +01:00
} ;
BuildLimits . prototype . DecrementCount = function ( category )
{
2011-08-06 10:11:05 +02:00
if ( this . count [ category ] !== undefined )
{
this . count [ category ] -- ;
}
2010-12-12 00:19:17 +01:00
} ;
2011-10-21 01:35:52 +02:00
BuildLimits . prototype . GetLimits = function ( )
{
2011-11-14 01:02:54 +01:00
return this . limit ;
2011-10-21 01:35:52 +02:00
} ;
BuildLimits . prototype . GetCounts = function ( )
{
2011-11-14 01:02:54 +01:00
return this . count ;
2011-10-21 01:35:52 +02:00
} ;
2010-12-12 00:19:17 +01:00
BuildLimits . prototype . AllowedToBuild = function ( category )
{
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 )
2010-12-12 00:19:17 +01:00
{
2011-08-06 10:11:05 +02:00
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
{
var cmpPlayer = Engine . QueryInterface ( this . entity , IID _Player ) ;
2011-08-06 10:11:05 +02:00
var notification = { "player" : cmpPlayer . GetPlayerID ( ) , "message" : category + " build 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 ;
}
}
2011-08-06 10:11:05 +02:00
else if ( this . count [ category ] >= 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 ) ;
2011-08-06 10:11:05 +02:00
var notification = { "player" : cmpPlayer . GetPlayerID ( ) , "message" : category + " build limit of " + this . limit [ category ] + " reached" } ;
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 ;
2010-12-12 00:19:17 +01:00
} ;
2011-08-16 06:44:15 +02:00
BuildLimits . prototype . OnGlobalOwnershipChanged = function ( msg )
{
// This automatically updates build counts
var cmpBuildRestrictions = Engine . QueryInterface ( msg . entity , IID _BuildRestrictions ) ;
if ( cmpBuildRestrictions )
{
var playerID = ( Engine . QueryInterface ( this . entity , IID _Player ) ) . GetPlayerID ( ) ;
if ( msg . from == playerID )
{
this . DecrementCount ( cmpBuildRestrictions . GetCategory ( ) ) ;
}
if ( msg . to == playerID )
{
this . IncrementCount ( cmpBuildRestrictions . GetCategory ( ) ) ;
}
}
} ;
2011-08-06 10:11:05 +02:00
Engine . RegisterComponentType ( IID _BuildLimits , "BuildLimits" , BuildLimits ) ;