Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/components/Capturable.js
Show All 27 Lines | Capturable.prototype.GetCapturePoints = function() | ||||
return this.cp; | return this.cp; | ||||
}; | }; | ||||
Capturable.prototype.GetMaxCapturePoints = function() | Capturable.prototype.GetMaxCapturePoints = function() | ||||
{ | { | ||||
return this.maxCp; | return this.maxCp; | ||||
}; | }; | ||||
//TODO get rid of this | |||||
Capturable.prototype.GetMax = Capturable.prototype.GetMaxCapturePoints; | |||||
Capturable.prototype.GetGarrisonRegenRate = function() | Capturable.prototype.GetGarrisonRegenRate = function() | ||||
{ | { | ||||
return ApplyValueModificationsToEntity("Capturable/GarrisonRegenRate", +this.template.GarrisonRegenRate, this.entity); | return ApplyValueModificationsToEntity("Capturable/GarrisonRegenRate", +this.template.GarrisonRegenRate, this.entity); | ||||
}; | }; | ||||
/** | /** | ||||
* 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. | ||||
*/ | */ | ||||
Capturable.prototype.SetCapturePoints = function(capturePointsArray) | Capturable.prototype.SetCapturePoints = function(capturePointsArray) | ||||
{ | { | ||||
this.cp = capturePointsArray; | this.cp = capturePointsArray; | ||||
}; | }; | ||||
/** | /** | ||||
* Reduces the amount of capture points of an entity, | * Reduces the amount of capture points of an entity, | ||||
* in favour of the player of the source | * in favour of the player of the source | ||||
* Returns the number of capture points actually taken | * Returns the number of capture points actually taken | ||||
*/ | */ | ||||
Capturable.prototype.Reduce = function(amount, playerID) | Capturable.prototype.Reduce = function(amount, playerID) | ||||
{ | { | ||||
if (amount <= 0) | if (amount <= 0) | ||||
return 0; | return { "captured": false, "captureChange": 0 }; | ||||
var cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership); | var cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership); | ||||
if (!cmpOwnership || cmpOwnership.GetOwner() == INVALID_PLAYER) | if (!cmpOwnership || cmpOwnership.GetOwner() == INVALID_PLAYER) | ||||
return 0; | return { "captured": false, "captureChange": 0 }; | ||||
var cmpPlayerSource = QueryPlayerIDInterface(playerID); | var cmpPlayerSource = QueryPlayerIDInterface(playerID); | ||||
if (!cmpPlayerSource) | if (!cmpPlayerSource) | ||||
return 0; | return { "captured": false, "captureChange": 0 }; | ||||
// Before changing the value, activate Fogging if necessary to hide changes | // Before changing the value, activate Fogging if necessary to hide changes | ||||
var cmpFogging = Engine.QueryInterface(this.entity, IID_Fogging); | var cmpFogging = Engine.QueryInterface(this.entity, IID_Fogging); | ||||
if (cmpFogging) | if (cmpFogging) | ||||
cmpFogging.Activate(); | cmpFogging.Activate(); | ||||
var numberOfEnemies = this.cp.filter((v, i) => v > 0 && cmpPlayerSource.IsEnemy(i)).length; | var numberOfEnemies = this.cp.filter((v, i) => v > 0 && cmpPlayerSource.IsEnemy(i)).length; | ||||
if (numberOfEnemies == 0) | if (numberOfEnemies == 0) | ||||
return 0; | return { "captured": false, "captureChange": 0 }; | ||||
// Distribute the capture points over all enemies. | // Distribute the capture points over all enemies. | ||||
let distributedAmount = amount / numberOfEnemies; | let distributedAmount = amount / numberOfEnemies; | ||||
let removedAmount = 0; | let removedAmount = 0; | ||||
while (distributedAmount > 0.0001) | while (distributedAmount > 0.0001) | ||||
{ | { | ||||
numberOfEnemies = 0; | numberOfEnemies = 0; | ||||
for (let i in this.cp) | for (let i in this.cp) | ||||
Show All 15 Lines | while (distributedAmount > 0.0001) | ||||
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 | ||||
var takenCp = this.maxCp - this.cp.reduce((a, b) => a + b); | var takenCp = this.maxCp - this.cp.reduce((a, b) => a + b); | ||||
this.cp[playerID] += takenCp; | this.cp[playerID] += takenCp; | ||||
this.CheckTimer(); | this.CheckTimer(); | ||||
this.RegisterCapturePointsChanged(); | return { "captured": this.RegisterCapturePointsChanged(), "captureChange": 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 | ||||
*/ | */ | ||||
Capturable.prototype.CanCapture = function(playerID) | Capturable.prototype.CanCapture = function(playerID) | ||||
{ | { | ||||
var cmpPlayerSource = QueryPlayerIDInterface(playerID); | var 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"); | ||||
var cp = this.GetCapturePoints(); | var cp = this.GetCapturePoints(); | ||||
var sourceEnemyCp = 0; | var sourceEnemyCp = 0; | ||||
for (let i in this.GetCapturePoints()) | for (let i in this.GetCapturePoints()) | ||||
if (cmpPlayerSource.IsEnemy(i)) | if (cmpPlayerSource.IsEnemy(i)) | ||||
sourceEnemyCp += cp[i]; | sourceEnemyCp += cp[i]; | ||||
return sourceEnemyCp > 0; | return sourceEnemyCp > 0; | ||||
}; | }; | ||||
// TODO stop this duplication | |||||
Capturable.prototype.CanAttack = function(playerID) | |||||
{ | |||||
var cmpPlayerSource = QueryPlayerIDInterface(playerID); | |||||
if (!cmpPlayerSource) | |||||
warn(playerID + " has no player component defined on its id"); | |||||
var cp = this.GetCapturePoints(); | |||||
var sourceEnemyCp = 0; | |||||
for (let i in this.GetCapturePoints()) | |||||
if (cmpPlayerSource.IsEnemy(i)) | |||||
sourceEnemyCp += cp[i]; | |||||
return sourceEnemyCp > 0; | |||||
}; | |||||
//// Private functions //// | //// Private functions //// | ||||
/** | /** | ||||
* This has to be called whenever the capture points are changed. | * This has to be called whenever the capture points are changed. | ||||
* It notifies other components of the change, and switches ownership when needed. | * It notifies other components of the change, and switches ownership when needed. | ||||
* return {boolean} - Whether the entity has changed owner. | |||||
*/ | */ | ||||
Capturable.prototype.RegisterCapturePointsChanged = function() | Capturable.prototype.RegisterCapturePointsChanged = function() | ||||
{ | { | ||||
var cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership); | var cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership); | ||||
if (!cmpOwnership) | if (!cmpOwnership) | ||||
return; | return false; | ||||
Engine.PostMessage(this.entity, MT_CapturePointsChanged, { "capturePoints": this.cp }); | Engine.PostMessage(this.entity, MT_CapturePointsChanged, { "capturePoints": this.cp }); | ||||
var owner = cmpOwnership.GetOwner(); | var owner = cmpOwnership.GetOwner(); | ||||
if (owner == INVALID_PLAYER || this.cp[owner] > 0) | if (owner == INVALID_PLAYER || this.cp[owner] > 0) | ||||
return; | return false; | ||||
// If all cp has been taken from the owner, convert it to the best player. | // If all cp has been taken from the owner, convert it to the best player. | ||||
var bestPlayer = 0; | var bestPlayer = 0; | ||||
for (let i in this.cp) | for (let i in this.cp) | ||||
if (this.cp[i] >= this.cp[bestPlayer]) | if (this.cp[i] >= this.cp[bestPlayer]) | ||||
bestPlayer = +i; | bestPlayer = +i; | ||||
let cmpLostPlayerStatisticsTracker = QueryOwnerInterface(this.entity, IID_StatisticsTracker); | let cmpLostPlayerStatisticsTracker = QueryOwnerInterface(this.entity, IID_StatisticsTracker); | ||||
if (cmpLostPlayerStatisticsTracker) | if (cmpLostPlayerStatisticsTracker) | ||||
cmpLostPlayerStatisticsTracker.LostEntity(this.entity); | cmpLostPlayerStatisticsTracker.LostEntity(this.entity); | ||||
cmpOwnership.SetOwner(bestPlayer); | cmpOwnership.SetOwner(bestPlayer); | ||||
let cmpCapturedPlayerStatisticsTracker = QueryOwnerInterface(this.entity, IID_StatisticsTracker); | let cmpCapturedPlayerStatisticsTracker = QueryOwnerInterface(this.entity, IID_StatisticsTracker); | ||||
if (cmpCapturedPlayerStatisticsTracker) | if (cmpCapturedPlayerStatisticsTracker) | ||||
cmpCapturedPlayerStatisticsTracker.CapturedEntity(this.entity); | cmpCapturedPlayerStatisticsTracker.CapturedEntity(this.entity); | ||||
return true; | |||||
}; | }; | ||||
Capturable.prototype.GetRegenRate = function() | Capturable.prototype.GetRegenRate = function() | ||||
{ | { | ||||
var regenRate = +this.template.RegenRate; | var regenRate = +this.template.RegenRate; | ||||
regenRate = ApplyValueModificationsToEntity("Capturable/RegenRate", regenRate, this.entity); | regenRate = ApplyValueModificationsToEntity("Capturable/RegenRate", regenRate, this.entity); | ||||
var cmpGarrisonHolder = Engine.QueryInterface(this.entity, IID_GarrisonHolder); | var cmpGarrisonHolder = Engine.QueryInterface(this.entity, IID_GarrisonHolder); | ||||
Show All 31 Lines | else // decay to gaia as default | ||||
this.cp[0] += decay; | this.cp[0] += decay; | ||||
modifiedCp += decay; | modifiedCp += decay; | ||||
this.RegisterCapturePointsChanged(); | this.RegisterCapturePointsChanged(); | ||||
} | } | ||||
var regenRate = this.GetRegenRate(); | var regenRate = this.GetRegenRate(); | ||||
if (regenRate < 0) | if (regenRate < 0) | ||||
modifiedCp += this.Reduce(-regenRate, 0); | modifiedCp += this.Reduce(-regenRate, 0).captureChange; | ||||
else if (regenRate > 0) | else if (regenRate > 0) | ||||
modifiedCp += this.Reduce(regenRate, owner); | modifiedCp += this.Reduce(regenRate, owner).captureChange; | ||||
if (modifiedCp) | if (modifiedCp) | ||||
return; | return; | ||||
// Nothing changed, stop the timer. | // Nothing changed, stop the timer. | ||||
var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); | var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); | ||||
cmpTimer.CancelTimer(this.timer); | cmpTimer.CancelTimer(this.timer); | ||||
this.timer = 0; | this.timer = 0; | ||||
▲ Show 20 Lines • Show All 106 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator