2010-01-24 18:24:35 +01:00
function Builder ( ) { }
2010-04-09 21:02:39 +02:00
Builder . prototype . Schema =
2010-04-23 18:09:03 +02:00
"<a:help>Allows the unit to construct and repair buildings.</a:help>" +
"<a:example>" +
"<Rate>1.0</Rate>" +
2010-07-03 03:23:23 +02:00
"<Entities datatype='tokens'>" +
2010-04-23 18:09:03 +02:00
"\n structures/{civ}_barracks\n structures/{civ}_civil_centre\n structures/celt_sb1\n " +
"</Entities>" +
"</a:example>" +
"<element name='Rate' a:help='Construction speed multiplier (1.0 is normal speed, higher values are faster)'>" +
2010-04-09 21:02:39 +02:00
"<ref name='positiveDecimal'/>" +
2010-04-23 18:09:03 +02:00
"</element>" +
2012-07-03 05:15:39 +02:00
"<element name='Entities' a:help='Space-separated list of entity template names that this unit can build. The special string \"{civ}\" will be automatically replaced by the unit's four-character civ code. This element can also be empty, in which case no new foundations may be placed by the unit, but they can still repair existing buildings'>" +
2010-07-03 03:23:23 +02:00
"<attribute name='datatype'>" +
"<value>tokens</value>" +
"</attribute>" +
2010-04-23 18:09:03 +02:00
"<text/>" +
2010-04-09 21:02:39 +02:00
"</element>" ;
2010-01-24 18:24:35 +01:00
Builder . prototype . Init = function ( )
{
} ;
2011-03-05 23:30:32 +01:00
Builder . prototype . Serialize = null ; // we have no dynamic state to save
2010-01-24 18:24:35 +01:00
Builder . prototype . GetEntitiesList = function ( )
{
2012-07-03 05:15:39 +02:00
var entities = [ ] ;
2010-07-03 03:23:23 +02:00
var string = this . template . Entities . _string ;
2012-07-03 05:15:39 +02:00
if ( string )
{
// Replace the "{civ}" codes with this entity's civ ID
var cmpIdentity = Engine . QueryInterface ( this . entity , IID _Identity ) ;
if ( cmpIdentity )
string = string . replace ( /\{civ\}/g , cmpIdentity . GetCiv ( ) ) ;
entities = string . split ( /\s+/ ) ;
}
return entities ;
2010-01-24 18:24:35 +01:00
} ;
2010-03-12 22:41:40 +01:00
Builder . prototype . GetRange = function ( )
{
2013-08-20 11:39:01 +02:00
var cmpObstruction = Engine . QueryInterface ( this . entity , IID _Obstruction ) ;
var max = 2 ;
if ( cmpObstruction )
max += cmpObstruction . GetUnitRadius ( ) ;
return { "max" : max , "min" : 0 } ;
2012-04-26 23:58:05 +02:00
} ;
2010-03-12 22:41:40 +01:00
/ * *
* Build / repair the target entity . This should only be called after a successful range check .
* It should be called at a rate of once per second .
2010-07-21 18:09:58 +02:00
* Returns obj with obj . finished == true if this is a repair and it ' s fully repaired .
2010-03-12 22:41:40 +01:00
* /
Builder . prototype . PerformBuilding = function ( target )
{
2013-01-08 01:00:21 +01:00
var rate = ApplyTechModificationsToEntity ( "Builder/Rate" , + this . template . Rate , this . entity ) ;
2010-03-12 22:41:40 +01:00
// If it's a foundation, then build it
var cmpFoundation = Engine . QueryInterface ( target , IID _Foundation ) ;
if ( cmpFoundation )
{
2010-07-21 18:09:58 +02:00
cmpFoundation . Build ( this . entity , rate ) ;
2010-08-21 23:53:51 +02:00
return ;
2010-03-12 22:41:40 +01:00
}
2010-08-21 23:53:51 +02:00
// Otherwise try to repair it
var cmpHealth = Engine . QueryInterface ( target , IID _Health ) ;
if ( cmpHealth )
{
cmpHealth . Repair ( this . entity , rate ) ;
return ;
2010-03-12 22:41:40 +01:00
}
} ;
2010-01-24 18:24:35 +01:00
Engine . RegisterComponentType ( IID _Builder , "Builder" , Builder ) ;