Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/components/Capturable.js
function Capturable() {} | function Capturable() {} | ||||
Capturable.prototype.Schema = | Capturable.prototype.Schema = | ||||
"<element name='CapturePoints' a:help='Maximum capture points.'>" + | "<element name='CapturePoints' a:help='Maximum capture points.'>" + | ||||
"<ref name='positiveDecimal'/>" + | "<ref name='positiveDecimal'/>" + | ||||
"</element>" + | "</element>" + | ||||
"<element name='RegenRate' a:help='Number of capture points that are regenerated per second in favour of the owner.'>" + | "<element name='RegenRate' a:help='Number of capture points that are regenerated per second in favour of the owner.'>" + | ||||
"<ref name='nonNegativeDecimal'/>" + | "<ref name='nonNegativeDecimal'/>" + | ||||
"</element>" + | "</element>" + | ||||
"<element name='GarrisonRegenRate' a:help='Number of capture points that are regenerated per second and per garrisoned unit in favour of the owner.'>" + | "<element name='GarrisonRegenRate' a:help='Number of capture points that are regenerated per second and per garrisoned unit in favour of the owner.'>" + | ||||
"<ref name='nonNegativeDecimal'/>" + | "<ref name='nonNegativeDecimal'/>" + | ||||
"</element>"; | "</element>"; | ||||
Capturable.prototype.Init = function() | Capturable.prototype.Init = function() | ||||
{ | { | ||||
this.maxCp = +this.template.CapturePoints; | this.maxCapturePoints = +this.template.CapturePoints; | ||||
this.garrisonRegenRate = +this.template.GarrisonRegenRate; | this.garrisonRegenRate = +this.template.GarrisonRegenRate; | ||||
this.regenRate = +this.template.RegenRate; | this.regenRate = +this.template.RegenRate; | ||||
this.cp = []; | this.cp = []; | ||||
}; | }; | ||||
//// Interface functions //// | //// Interface functions //// | ||||
/** | /** | ||||
* Returns the current capture points array. | * Returns the current capture points array. | ||||
*/ | */ | ||||
Capturable.prototype.GetCapturePoints = function() | Capturable.prototype.GetCapturePoints = function() | ||||
{ | { | ||||
return this.cp; | return this.cp; | ||||
}; | }; | ||||
Capturable.prototype.GetMaxCapturePoints = function() | Capturable.prototype.GetMaxCapturePoints = function() | ||||
{ | { | ||||
return this.maxCp; | return this.maxCapturePoints; | ||||
}; | }; | ||||
Capturable.prototype.GetGarrisonRegenRate = function() | Capturable.prototype.GetGarrisonRegenRate = function() | ||||
{ | { | ||||
return this.garrisonRegenRate; | return this.garrisonRegenRate; | ||||
}; | }; | ||||
Capturable.prototype.GetRelativeCapture = function(effectData, effectType, bonusMultiplier, attackerOwner) | |||||
{ | |||||
return this.CanCapture(attackerOwner) ? | |||||
Attacking.GetTotalAttackEffects( | |||||
this.entity, | |||||
effectData, | |||||
effectType, | |||||
bonusMultiplier, | |||||
QueryMiragedInterface(this.entity, IID_Resistance) | |||||
) / this.maxCapturePoints : | |||||
0; | |||||
}; | |||||
/** | /** | ||||
* Set the new capture points, used for cloning entities. | * Set the new capture points, used for cloning entities. | ||||
* The caller should assure that the sum of capture points | * The caller should assure that the sum of capture points | ||||
* matches the max. | * matches the max. | ||||
* @param {number[]} - Array with for all players the new value. | * @param {number[]} - Array with for all players the new value. | ||||
*/ | */ | ||||
Capturable.prototype.SetCapturePoints = function(capturePointsArray) | Capturable.prototype.SetCapturePoints = function(capturePointsArray) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 68 Lines • ▼ Show 20 Lines | for (let i in this.cp) | ||||
removedAmount += this.cp[i]; | removedAmount += this.cp[i]; | ||||
this.cp[i] = 0; | this.cp[i] = 0; | ||||
} | } | ||||
} | } | ||||
distributedAmount = numberOfEnemies ? (amount - removedAmount) / numberOfEnemies : 0; | distributedAmount = numberOfEnemies ? (amount - removedAmount) / numberOfEnemies : 0; | ||||
} | } | ||||
// Give all cp taken to the player. | // Give all cp taken to the player. | ||||
let takenCp = this.maxCp - this.cp.reduce((a, b) => a + b); | let takenCp = this.maxCapturePoints - this.cp.reduce((a, b) => a + b); | ||||
this.cp[playerID] += takenCp; | this.cp[playerID] += takenCp; | ||||
this.CheckTimer(); | this.CheckTimer(); | ||||
this.RegisterCapturePointsChanged(); | this.RegisterCapturePointsChanged(); | ||||
return takenCp; | return takenCp; | ||||
}; | }; | ||||
/** | /** | ||||
* Check if the source can (re)capture points from this building. | * Check if the source can (re)capture points from this building. | ||||
* @param {number} playerID - PlayerID of the source. | * @param {number} playerID - PlayerID of the source. | ||||
* @return {boolean} - Whether the source can (re)capture points from this building. | * @return {boolean} - Whether the source can (re)capture points from this building. | ||||
*/ | */ | ||||
Capturable.prototype.CanCapture = function(playerID) | Capturable.prototype.CanCapture = function(playerID) | ||||
{ | { | ||||
let cmpResistance = QueryMiragedInterface(this.entity, IID_Resistance); | |||||
if (cmpResistance && cmpResistance.IsInvulnerable()) | |||||
return false; | |||||
let cmpPlayerSource = QueryPlayerIDInterface(playerID); | let cmpPlayerSource = QueryPlayerIDInterface(playerID); | ||||
if (!cmpPlayerSource) | if (!cmpPlayerSource) | ||||
warn(playerID + " has no player component defined on its id."); | warn(playerID + " has no player component defined on its id."); | ||||
let cp = this.GetCapturePoints(); | let cp = this.GetCapturePoints(); | ||||
let sourceEnemyCp = 0; | let sourceEnemyCp = 0; | ||||
for (let i in this.GetCapturePoints()) | for (let i in this.GetCapturePoints()) | ||||
if (cmpPlayerSource.IsEnemy(i)) | if (cmpPlayerSource.IsEnemy(i)) | ||||
▲ Show 20 Lines • Show All 109 Lines • ▼ Show 20 Lines | |||||
/** | /** | ||||
* Update all chached values that could be affected by modifications. | * Update all chached values that could be affected by modifications. | ||||
*/ | */ | ||||
Capturable.prototype.UpdateCachedValues = function() | Capturable.prototype.UpdateCachedValues = function() | ||||
{ | { | ||||
this.garrisonRegenRate = ApplyValueModificationsToEntity("Capturable/GarrisonRegenRate", +this.template.GarrisonRegenRate, this.entity); | this.garrisonRegenRate = ApplyValueModificationsToEntity("Capturable/GarrisonRegenRate", +this.template.GarrisonRegenRate, this.entity); | ||||
this.regenRate = ApplyValueModificationsToEntity("Capturable/RegenRate", +this.template.RegenRate, this.entity); | this.regenRate = ApplyValueModificationsToEntity("Capturable/RegenRate", +this.template.RegenRate, this.entity); | ||||
this.maxCp = ApplyValueModificationsToEntity("Capturable/CapturePoints", +this.template.CapturePoints, this.entity); | this.maxCapturePoints = ApplyValueModificationsToEntity("Capturable/CapturePoints", +this.template.CapturePoints, this.entity); | ||||
}; | }; | ||||
/** | /** | ||||
* Update all chached values that could be affected by modifications. | * Update all chached values that could be affected by modifications. | ||||
* Check timer and send changed messages when required. | * Check timer and send changed messages when required. | ||||
* @param {boolean} dontSendCpChanged - Whether not to send a CapturePointsChanged message. When true, caller should take care of sending that message. | * @param {boolean} dontSendCpChanged - Whether not to send a CapturePointsChanged message. When true, caller should take care of sending that message. | ||||
*/ | */ | ||||
Capturable.prototype.UpdateCachedValuesAndNotify = function(dontSendCpChanged = false) | Capturable.prototype.UpdateCachedValuesAndNotify = function(dontSendCpChanged = false) | ||||
{ | { | ||||
let oldMaxCp = this.maxCp; | let oldMaxCapturePoints = this.maxCapturePoints; | ||||
let oldGarrisonRegenRate = this.garrisonRegenRate; | let oldGarrisonRegenRate = this.garrisonRegenRate; | ||||
let oldRegenRate = this.regenRate; | let oldRegenRate = this.regenRate; | ||||
this.UpdateCachedValues(); | this.UpdateCachedValues(); | ||||
if (oldMaxCp != this.maxCp) | if (oldMaxCapturePoints != this.maxCapturePoints) | ||||
{ | { | ||||
let scale = this.maxCp / oldMaxCp; | let scale = this.maxCapturePoints / oldMaxCapturePoints; | ||||
for (let i in this.cp) | for (let i in this.cp) | ||||
this.cp[i] *= scale; | this.cp[i] *= scale; | ||||
if (!dontSendCpChanged) | if (!dontSendCpChanged) | ||||
Engine.PostMessage(this.entity, MT_CapturePointsChanged, { "capturePoints": this.cp }); | Engine.PostMessage(this.entity, MT_CapturePointsChanged, { "capturePoints": this.cp }); | ||||
} | } | ||||
if (oldGarrisonRegenRate != this.garrisonRegenRate || oldRegenRate != this.regenRate) | if (oldGarrisonRegenRate != this.garrisonRegenRate || oldRegenRate != this.regenRate) | ||||
this.CheckTimer(); | this.CheckTimer(); | ||||
Show All 31 Lines | Capturable.prototype.OnOwnershipChanged = function(msg) | ||||
// Initialise the capture points when created. | // Initialise the capture points when created. | ||||
if (!this.cp.length) | if (!this.cp.length) | ||||
{ | { | ||||
this.UpdateCachedValues(); | this.UpdateCachedValues(); | ||||
let numPlayers = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager).GetNumPlayers(); | let numPlayers = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager).GetNumPlayers(); | ||||
for (let i = 0; i < numPlayers; ++i) | for (let i = 0; i < numPlayers; ++i) | ||||
{ | { | ||||
if (i == msg.to) | if (i == msg.to) | ||||
this.cp[i] = this.maxCp; | this.cp[i] = this.maxCapturePoints; | ||||
else | else | ||||
this.cp[i] = 0; | this.cp[i] = 0; | ||||
} | } | ||||
this.CheckTimer(); | this.CheckTimer(); | ||||
return; | return; | ||||
} | } | ||||
// When already initialised, this happens on defeat or wololo, | // When already initialised, this happens on defeat or wololo, | ||||
Show All 32 Lines |
Wildfire Games · Phabricator