2010-01-22 21:03:14 +01:00
function Player ( ) { }
2010-04-23 18:09:03 +02:00
Player . prototype . Schema =
"<a:component type='system'/><empty/>" ;
2010-01-22 21:03:14 +01:00
Player . prototype . Init = function ( )
{
this . playerID = undefined ;
2010-10-30 06:02:42 +02:00
this . name = undefined ; // define defaults elsewhere (supporting other languages)
this . civ = undefined ;
2010-06-30 23:41:04 +02:00
this . colour = { "r" : 0.0 , "g" : 0.0 , "b" : 0.0 , "a" : 1.0 } ;
2010-08-21 22:43:55 +02:00
this . popUsed = 0 ; // population of units owned by this player
this . popReserved = 0 ; // population of units currently being trained
2010-09-06 20:34:25 +02:00
this . popLimit = 0 ; // maximum population
2010-09-04 15:24:52 +02:00
this . trainingQueueBlocked = false ; // indicates whether any training queue is currently blocked
2010-02-12 23:46:53 +01:00
this . resourceCount = {
2010-09-06 20:34:25 +02:00
"food" : 1000 ,
"wood" : 1000 ,
2010-03-12 22:41:40 +01:00
"metal" : 500 ,
"stone" : 1000
2010-02-12 23:46:53 +01:00
} ;
2010-10-30 06:02:42 +02:00
this . team = - 1 ; // team number of the player, players on the same team will always have ally diplomatic status - also this is useful for team emblems, scoring, etc.
2010-10-01 22:51:21 +02:00
this . state = "active" ; // game state - one of "active", "defeated", "won"
2010-10-30 06:02:42 +02:00
this . diplomacy = [ ] ; // array of diplomatic stances for this player with respect to other players (including self)
2010-10-01 22:51:21 +02:00
this . conquestCriticalEntitiesCount = 0 ; // number of owned units with ConquestCritical class
2010-10-30 06:02:42 +02:00
this . phase = "village" ;
2010-01-22 21:03:14 +01:00
} ;
2010-06-30 23:41:04 +02:00
Player . prototype . SetPlayerID = function ( id )
2010-04-23 20:36:48 +02:00
{
2010-06-30 23:41:04 +02:00
this . playerID = id ;
2010-04-23 20:36:48 +02:00
} ;
2010-08-21 22:43:55 +02:00
Player . prototype . GetPlayerID = function ( )
{
return this . playerID ;
} ;
2010-06-30 23:41:04 +02:00
Player . prototype . SetName = function ( name )
2010-01-22 21:03:14 +01:00
{
2010-06-30 23:41:04 +02:00
this . name = name ;
} ;
2010-07-02 04:44:25 +02:00
Player . prototype . GetName = function ( )
{
return this . name ;
} ;
2010-06-30 23:41:04 +02:00
Player . prototype . SetCiv = function ( civcode )
{
this . civ = civcode ;
2010-01-22 21:03:14 +01:00
} ;
2010-07-02 04:44:25 +02:00
Player . prototype . GetCiv = function ( )
{
return this . civ ;
} ;
2010-06-30 23:41:04 +02:00
Player . prototype . SetColour = function ( r , g , b )
{
this . colour = { "r" : r / 255.0 , "g" : g / 255.0 , "b" : b / 255.0 , "a" : 1.0 } ;
} ;
Player . prototype . GetColour = function ( )
{
return this . colour ;
} ;
2010-08-21 22:43:55 +02:00
Player . prototype . TryReservePopulationSlots = function ( num )
{
if ( num > this . GetPopulationLimit ( ) - this . GetPopulationCount ( ) )
return false ;
this . popReserved += num ;
return true ;
} ;
Player . prototype . UnReservePopulationSlots = function ( num )
{
this . popReserved -= num ;
} ;
2010-01-22 21:03:14 +01:00
Player . prototype . GetPopulationCount = function ( )
{
2010-08-21 22:43:55 +02:00
return this . popUsed + this . popReserved ;
2010-01-22 21:03:14 +01:00
} ;
2010-10-30 06:02:42 +02:00
Player . prototype . SetPopulationLimit = function ( limit )
{
this . popLimit = limit ;
} ;
2010-01-22 21:03:14 +01:00
Player . prototype . GetPopulationLimit = function ( )
{
return this . popLimit ;
} ;
2010-09-04 15:24:52 +02:00
Player . prototype . IsTrainingQueueBlocked = function ( )
{
return this . trainingQueueBlocked ;
} ;
Player . prototype . BlockTrainingQueue = function ( )
{
this . trainingQueueBlocked = true ;
} ;
Player . prototype . UnBlockTrainingQueue = function ( )
{
this . trainingQueueBlocked = false ;
} ;
2010-10-30 06:02:42 +02:00
Player . prototype . SetResourceCounts = function ( resources )
{
this . resourceCount = resources ;
} ;
2010-02-12 23:46:53 +01:00
Player . prototype . GetResourceCounts = function ( )
{
return this . resourceCount ;
} ;
2010-03-12 22:41:40 +01:00
Player . prototype . AddResource = function ( type , amount )
2010-02-12 23:46:53 +01:00
{
this . resourceCount [ type ] += ( + amount ) ;
} ;
2010-04-19 21:47:23 +02:00
Player . prototype . AddResources = function ( amounts )
{
for ( var type in amounts )
this . resourceCount [ type ] += ( + amounts [ type ] ) ;
} ;
2010-03-12 22:41:40 +01:00
Player . prototype . TrySubtractResources = function ( amounts )
{
2010-08-14 00:02:27 +02:00
// Check if we can afford it all
var amountsNeeded = { } ;
2010-03-12 22:41:40 +01:00
for ( var type in amounts )
if ( amounts [ type ] > this . resourceCount [ type ] )
2010-08-14 00:02:27 +02:00
amountsNeeded [ type ] = amounts [ type ] - this . resourceCount [ type ] ;
var formattedAmountsNeeded = [ ] ;
for ( var type in amountsNeeded )
formattedAmountsNeeded . push ( type + ": " + amountsNeeded [ type ] ) ;
// If we don't have enough resources, send a notification to the player
if ( formattedAmountsNeeded . length )
{
2010-08-14 17:34:36 +02:00
var notification = { "player" : this . playerID , "message" : "Insufficient resources - " + formattedAmountsNeeded . join ( ", " ) } ;
2010-08-14 00:02:27 +02:00
var cmpGUIInterface = Engine . QueryInterface ( SYSTEM _ENTITY , IID _GuiInterface ) ;
2010-08-14 06:34:49 +02:00
cmpGUIInterface . PushNotification ( notification ) ;
2010-08-14 00:02:27 +02:00
return false ;
}
else
{
// Subtract the resources
for ( var type in amounts )
2010-08-14 17:34:36 +02:00
this . resourceCount [ type ] -= amounts [ type ] ;
2010-08-14 00:02:27 +02:00
}
2010-03-12 22:41:40 +01:00
return true ;
} ;
2010-10-01 22:51:21 +02:00
Player . prototype . GetState = function ( )
{
return this . state ;
} ;
Player . prototype . SetState = function ( newState )
{
this . state = newState ;
} ;
Player . prototype . GetConquestCriticalEntitiesCount = function ( )
{
return this . conquestCriticalEntitiesCount ;
} ;
2010-10-30 06:02:42 +02:00
Player . prototype . GetTeam = function ( )
{
return this . team ;
} ;
Player . prototype . SetTeam = function ( team )
{
this . team = team ;
} ;
Player . prototype . GetDiplomacy = function ( )
{
return this . diplomacy ;
} ;
Player . prototype . SetDiplomacy = function ( dipl )
{
this . diplomacy = dipl ;
} ;
Player . prototype . GetPhase = function ( )
{
return this . phase ;
} ;
Player . prototype . SetPhase = function ( p )
{
this . phase = p ;
} ;
2010-08-21 22:43:55 +02:00
// Keep track of population effects of all entities that
// become owned or unowned by this player
2010-01-22 21:03:14 +01:00
Player . prototype . OnGlobalOwnershipChanged = function ( msg )
{
2010-10-01 22:51:21 +02:00
var classes = [ ] ;
// Load class list only if we're going to need it
if ( msg . from == this . playerID || msg . to == this . playerID )
{
var cmpIdentity = Engine . QueryInterface ( msg . entity , IID _Identity ) ;
if ( cmpIdentity )
classes = cmpIdentity . GetClassesList ( ) ;
}
2010-01-22 21:03:14 +01:00
if ( msg . from == this . playerID )
{
2010-10-01 22:51:21 +02:00
if ( classes . indexOf ( "ConquestCritical" ) != - 1 )
this . conquestCriticalEntitiesCount -- ;
2010-01-22 21:03:14 +01:00
var cost = Engine . QueryInterface ( msg . entity , IID _Cost ) ;
if ( cost )
2010-01-24 18:24:35 +01:00
{
2010-08-21 22:43:55 +02:00
this . popUsed -= cost . GetPopCost ( ) ;
2010-04-04 22:52:21 +02:00
this . popLimit -= cost . GetPopBonus ( ) ;
2010-01-24 18:24:35 +01:00
}
2010-01-22 21:03:14 +01:00
}
if ( msg . to == this . playerID )
{
2010-10-01 22:51:21 +02:00
if ( classes . indexOf ( "ConquestCritical" ) != - 1 )
this . conquestCriticalEntitiesCount ++ ;
2010-01-22 21:03:14 +01:00
var cost = Engine . QueryInterface ( msg . entity , IID _Cost ) ;
if ( cost )
2010-01-24 18:24:35 +01:00
{
2010-08-21 22:43:55 +02:00
this . popUsed += cost . GetPopCost ( ) ;
2010-04-04 22:52:21 +02:00
this . popLimit += cost . GetPopBonus ( ) ;
2010-01-24 18:24:35 +01:00
}
2010-01-22 21:03:14 +01:00
}
} ;
2010-10-01 22:51:21 +02:00
Player . prototype . OnPlayerDefeated = function ( )
{
this . state = "defeated" ;
// Reassign all player's entities to Gaia
var cmpRangeManager = Engine . QueryInterface ( SYSTEM _ENTITY , IID _RangeManager ) ;
var entities = cmpRangeManager . GetEntitiesByPlayer ( this . playerID ) ;
for each ( var entity in entities )
{
// Note: maybe we need to reassign units and buildings only?
var cmpOwnership = Engine . QueryInterface ( entity , IID _Ownership ) ;
cmpOwnership . SetOwner ( 0 ) ;
}
}
2010-01-22 21:03:14 +01:00
Engine . RegisterComponentType ( IID _Player , "Player" , Player ) ;