2010-02-12 23:46:53 +01:00
function ResourceSupply ( ) { }
2010-04-09 21:02:39 +02:00
ResourceSupply . prototype . Schema =
2010-04-23 18:09:03 +02:00
"<a:help>Provides a supply of one particular type of resource.</a:help>" +
"<a:example>" +
"<Amount>1000</Amount>" +
"<Type>food.meat</Type>" +
"</a:example>" +
2010-10-13 17:14:15 +02:00
"<element name='KillBeforeGather' a:help='Whether this entity must be killed (health reduced to 0) before its resources can be gathered'>" +
2013-08-26 05:06:08 +02:00
"<data type='boolean'/>" +
"</element>" +
"<element name='Amount' a:help='Amount of resources available from this entity'>" +
"<choice><data type='nonNegativeInteger'/><value>Infinity</value></choice>" +
"</element>" +
"<element name='Type' a:help='Type of resources'>" +
"<choice>" +
2010-07-21 20:51:27 +02:00
"<value>wood.tree</value>" +
2010-11-12 19:58:33 +01:00
"<value>wood.ruins</value>" +
2010-07-21 20:51:27 +02:00
"<value>stone.rock</value>" +
2010-11-12 19:58:33 +01:00
"<value>stone.ruins</value>" +
2010-07-21 20:51:27 +02:00
"<value>metal.ore</value>" +
2010-04-23 18:09:03 +02:00
"<value>food.fish</value>" +
"<value>food.fruit</value>" +
"<value>food.grain</value>" +
"<value>food.meat</value>" +
"<value>food.milk</value>" +
2010-11-20 22:37:31 +01:00
"<value>treasure.wood</value>" +
"<value>treasure.stone</value>" +
"<value>treasure.metal</value>" +
"<value>treasure.food</value>" +
2014-01-10 02:46:27 +01:00
"</choice>" +
"</element>" +
"<element name='MaxGatherers' a:help='Amount of gatherers who can gather resources from this entity at the same time'>" +
"<data type='nonNegativeInteger'/>" +
2013-08-26 05:06:08 +02:00
"</element>" +
"<optional>" +
"<element name='DiminishingReturns' a:help='The rate at which adding more gatherers decreases overall efficiency. Lower numbers = faster dropoff. Leave the element out for no diminishing returns.'>" +
"<ref name='positiveDecimal'/>" +
"</element>" +
"</optional>" ;
ResourceSupply . prototype . Init = function ( )
{
// Current resource amount (non-negative)
this . amount = this . GetMaxAmount ( ) ;
2014-01-10 02:46:27 +01:00
this . gatherers = [ [ ] , [ ] , [ ] , [ ] , [ ] , [ ] , [ ] , [ ] ] ; // list of IDs for each players
this . infinite = ! isFinite ( + this . template . Amount ) ;
} ;
2013-08-26 05:06:08 +02:00
ResourceSupply . prototype . IsInfinite = function ( )
{
return this . infinite ;
} ;
ResourceSupply . prototype . GetKillBeforeGather = function ( )
{
return ( this . template . KillBeforeGather == "true" ) ;
2010-10-13 17:14:15 +02:00
} ;
2010-02-12 23:46:53 +01:00
ResourceSupply . prototype . GetMaxAmount = function ( )
{
return + this . template . Amount ;
} ;
ResourceSupply . prototype . GetCurrentAmount = function ( )
{
return this . amount ;
} ;
2013-03-13 21:10:46 +01:00
ResourceSupply . prototype . GetMaxGatherers = function ( )
{
return + this . template . MaxGatherers ;
} ;
2014-01-10 02:46:27 +01:00
ResourceSupply . prototype . GetGatherers = function ( player )
2013-03-13 21:10:46 +01:00
{
2014-01-10 02:46:27 +01:00
if ( player === undefined )
return this . gatherers ;
return this . gatherers [ player - 1 ] ;
2013-08-26 05:06:08 +02:00
} ;
ResourceSupply . prototype . GetDiminishingReturns = function ( )
{
if ( "DiminishingReturns" in this . template )
2013-10-15 12:05:08 +02:00
return ApplyValueModificationsToEntity ( "ResourceSupply/DiminishingReturns" , + this . template . DiminishingReturns , this . entity ) ;
2013-08-26 05:06:08 +02:00
return null ;
} ;
ResourceSupply . prototype . TakeResources = function ( rate )
{
if ( this . infinite )
return { "amount" : rate , "exhausted" : false } ;
// 'rate' should be a non-negative integer
var old = this . amount ;
2010-04-23 18:09:03 +02:00
this . amount = Math . max ( 0 , old - rate ) ;
2012-04-28 21:25:44 +02:00
var change = old - this . amount ;
2014-01-10 02:46:27 +01:00
// Remove entities that have been exhausted
if ( this . amount == 0 )
Engine . DestroyEntity ( this . entity ) ;
Engine . PostMessage ( this . entity , MT _ResourceSupplyChanged , { "from" : old , "to" : this . amount } ) ;
return { "amount" : change , "exhausted" : ( this . amount == 0 ) } ;
} ;
2010-02-12 23:46:53 +01:00
ResourceSupply . prototype . GetType = function ( )
{
2010-07-31 23:21:42 +02:00
// All resources must have both type and subtype
var [ type , subtype ] = this . template . Type . split ( '.' ) ;
return { "generic" : type , "specific" : subtype } ;
2010-02-12 23:46:53 +01:00
} ;
2014-01-10 02:46:27 +01:00
ResourceSupply . prototype . IsAvailable = function ( player , gathererID )
2013-03-13 21:10:46 +01:00
{
2014-01-10 02:46:27 +01:00
if ( this . gatherers [ player - 1 ] . length < this . GetMaxGatherers ( ) || this . gatherers [ player - 1 ] . indexOf ( gathererID ) !== - 1 )
2013-03-13 21:10:46 +01:00
return true ;
return false ;
2014-01-10 02:46:27 +01:00
} ;
2013-03-13 21:10:46 +01:00
2014-01-10 02:46:27 +01:00
ResourceSupply . prototype . AddGatherer = function ( player , gathererID )
2013-03-13 21:10:46 +01:00
{
2014-01-10 02:46:27 +01:00
if ( ! this . IsAvailable ( player , gathererID ) )
return false ;
if ( this . gatherers [ player - 1 ] . indexOf ( gathererID ) === - 1 )
2013-03-13 21:10:46 +01:00
{
2014-01-10 02:46:27 +01:00
this . gatherers [ player - 1 ] . push ( gathererID ) ;
2013-03-13 21:10:46 +01:00
// broadcast message, mainly useful for the AIs.
2014-01-10 02:46:27 +01:00
Engine . PostMessage ( this . entity , MT _ResourceSupplyGatherersChanged , { "to" : this . gatherers } ) ;
}
return true ;
} ;
// should this return false if the gatherer didn't gather from said resource?
ResourceSupply . prototype . RemoveGatherer = function ( gathererID , player )
{
// this can happen if the unit is dead
if ( player === undefined || player === - 1 )
{
2014-01-10 21:04:37 +01:00
for ( var i = 0 ; i < 8 ; ++ i )
2014-01-10 02:46:27 +01:00
{
var index = this . gatherers [ i ] . indexOf ( gathererID ) ;
if ( index !== - 1 )
{
this . gatherers [ i ] . splice ( index , 1 ) ;
// broadcast message, mainly useful for the AIs.
Engine . PostMessage ( this . entity , MT _ResourceSupplyGatherersChanged , { "to" : this . gatherers } ) ;
return ;
}
}
2014-01-10 21:04:37 +01:00
}
else
{
2014-01-10 02:46:27 +01:00
var index = this . gatherers [ player - 1 ] . indexOf ( gathererID ) ;
if ( index !== - 1 )
{
this . gatherers [ player - 1 ] . splice ( index , 1 ) ;
// broadcast message, mainly useful for the AIs.
Engine . PostMessage ( this . entity , MT _ResourceSupplyGatherersChanged , { "to" : this . gatherers } ) ;
return ;
}
}
} ;
2010-02-12 23:46:53 +01:00
Engine . RegisterComponentType ( IID _ResourceSupply , "ResourceSupply" , ResourceSupply ) ;