cp => CapturePoints

Reviewed By: Angen
Differential Revision: D3248
This was SVN commit r24440.
This commit is contained in:
bb committed 2020-12-23 17:26:36 +00:00
1 parent 75b8d3194e
commit 6205f687e4
4 files changed
+69 -69

No files matched your search

@@ -163,7 +163,7 @@ PETRA.allowCapture = function(gameState, ent, target)
return false;
if (target.isInvulnerable())
return true;
// always try to recapture cp from an allied, except if it's decaying
// always try to recapture capture points from an allied, except if it's decaying
if (gameState.isPlayerAlly(target.owner()))
return !target.decaying();
@@ -13,10 +13,10 @@ Capturable.prototype.Schema =
Capturable.prototype.Init = function()
{
this.maxCp = +this.template.CapturePoints;
this.maxCapturePoints = +this.template.CapturePoints;
this.garrisonRegenRate = +this.template.GarrisonRegenRate;
this.regenRate = +this.template.RegenRate;
this.cp = [];
this.capturePoints = [];
};
//// Interface functions ////
@@ -26,12 +26,12 @@ Capturable.prototype.Init = function()
*/
Capturable.prototype.GetCapturePoints = function()
{
return this.cp;
return this.capturePoints;
};
Capturable.prototype.GetMaxCapturePoints = function()
{
return this.maxCp;
return this.maxCapturePoints;
};
Capturable.prototype.GetGarrisonRegenRate = function()
@@ -47,7 +47,7 @@ Capturable.prototype.GetGarrisonRegenRate = function()
*/
Capturable.prototype.SetCapturePoints = function(capturePointsArray)
{
this.cp = capturePointsArray;
this.capturePoints = capturePointsArray;
};
/**
@@ -92,7 +92,7 @@ Capturable.prototype.Reduce = function(amount, playerID)
if (cmpFogging)
cmpFogging.Activate();
let numberOfEnemies = this.cp.filter((v, i) => v > 0 && cmpPlayerSource.IsEnemy(i)).length;
let numberOfEnemies = this.capturePoints.filter((v, i) => v > 0 && cmpPlayerSource.IsEnemy(i)).length;
if (numberOfEnemies == 0)
return 0;
@@ -103,32 +103,32 @@ Capturable.prototype.Reduce = function(amount, playerID)
while (distributedAmount > 0.0001)
{
numberOfEnemies = 0;
for (let i in this.cp)
for (let i in this.capturePoints)
{
if (!this.cp[i] || !cmpPlayerSource.IsEnemy(i))
if (!this.capturePoints[i] || !cmpPlayerSource.IsEnemy(i))
continue;
if (this.cp[i] > distributedAmount)
if (this.capturePoints[i] > distributedAmount)
{
removedAmount += distributedAmount;
this.cp[i] -= distributedAmount;
this.capturePoints[i] -= distributedAmount;
++numberOfEnemies;
}
else
{
removedAmount += this.cp[i];
this.cp[i] = 0;
removedAmount += this.capturePoints[i];
this.capturePoints[i] = 0;
}
}
distributedAmount = numberOfEnemies ? (amount - removedAmount) / numberOfEnemies : 0;
}
// Give all cp taken to the player.
let takenCp = this.maxCp - this.cp.reduce((a, b) => a + b);
this.cp[playerID] += takenCp;
// Give all capture points taken to the player.
let takenCapturePoints = this.maxCapturePoints - this.capturePoints.reduce((a, b) => a + b);
this.capturePoints[playerID] += takenCapturePoints;
this.CheckTimer();
this.RegisterCapturePointsChanged();
return takenCp;
return takenCapturePoints;
};
/**
@@ -142,12 +142,12 @@ Capturable.prototype.CanCapture = function(playerID)
if (!cmpPlayerSource)
warn(playerID + " has no player component defined on its id.");
let cp = this.GetCapturePoints();
let sourceEnemyCp = 0;
let capturePoints = this.GetCapturePoints();
let sourceEnemyCapturePoints = 0;
for (let i in this.GetCapturePoints())
if (cmpPlayerSource.IsEnemy(i))
sourceEnemyCp += cp[i];
return sourceEnemyCp > 0;
sourceEnemyCapturePoints += capturePoints[i];
return sourceEnemyCapturePoints > 0;
};
//// Private functions ////
@@ -162,18 +162,18 @@ Capturable.prototype.RegisterCapturePointsChanged = function()
if (!cmpOwnership)
return;
Engine.PostMessage(this.entity, MT_CapturePointsChanged, { "capturePoints": this.cp });
Engine.PostMessage(this.entity, MT_CapturePointsChanged, { "capturePoints": this.capturePoints });
let owner = cmpOwnership.GetOwner();
if (owner == INVALID_PLAYER || this.cp[owner] > 0)
if (owner == INVALID_PLAYER || this.capturePoints[owner] > 0)
return;
// If all cp has been taken from the owner, convert it to player with the most capture points.
// If all capture points have been taken from the owner, convert it to player with the most capture points.
let cmpLostPlayerStatisticsTracker = QueryOwnerInterface(this.entity, IID_StatisticsTracker);
if (cmpLostPlayerStatisticsTracker)
cmpLostPlayerStatisticsTracker.LostEntity(this.entity);
cmpOwnership.SetOwner(this.cp.reduce((bestPlayer, playerCp, player, cp) => playerCp > cp[bestPlayer] ? player : bestPlayer, 0));
cmpOwnership.SetOwner(this.capturePoints.reduce((bestPlayer, playerCapturePoints, player, capturePoints) => playerCapturePoints > capturePoints[bestPlayer] ? player : bestPlayer, 0));
let cmpCapturedPlayerStatisticsTracker = QueryOwnerInterface(this.entity, IID_StatisticsTracker);
if (cmpCapturedPlayerStatisticsTracker)
@@ -196,36 +196,36 @@ Capturable.prototype.TimerTick = function()
return;
let owner = cmpOwnership.GetOwner();
let modifiedCp = 0;
let modifiedCapturePoints = 0;
// Special handle for the territory decay.
// Reduce cp from the owner in favour of all neighbours (also allies).
// Reduce capture points from the owner in favour of all neighbours (also allies).
let cmpTerritoryDecay = Engine.QueryInterface(this.entity, IID_TerritoryDecay);
if (cmpTerritoryDecay && cmpTerritoryDecay.IsDecaying())
{
let neighbours = cmpTerritoryDecay.GetConnectedNeighbours();
let totalNeighbours = neighbours.reduce((a, b) => a + b);
let decay = Math.min(cmpTerritoryDecay.GetDecayRate(), this.cp[owner]);
this.cp[owner] -= decay;
let decay = Math.min(cmpTerritoryDecay.GetDecayRate(), this.capturePoints[owner]);
this.capturePoints[owner] -= decay;
if (totalNeighbours)
for (let p in neighbours)
this.cp[p] += decay * neighbours[p] / totalNeighbours;
this.capturePoints[p] += decay * neighbours[p] / totalNeighbours;
// Decay to gaia as default.
else
this.cp[0] += decay;
this.capturePoints[0] += decay;
modifiedCp += decay;
modifiedCapturePoints += decay;
this.RegisterCapturePointsChanged();
}
let regenRate = this.GetRegenRate();
if (regenRate < 0)
modifiedCp += this.Reduce(-regenRate, 0);
modifiedCapturePoints += this.Reduce(-regenRate, 0);
else if (regenRate > 0)
modifiedCp += this.Reduce(regenRate, owner);
modifiedCapturePoints += this.Reduce(regenRate, owner);
if (modifiedCp)
if (modifiedCapturePoints)
return;
// Nothing changed, stop the timer.
@@ -263,29 +263,29 @@ Capturable.prototype.UpdateCachedValues = function()
{
this.garrisonRegenRate = ApplyValueModificationsToEntity("Capturable/GarrisonRegenRate", +this.template.GarrisonRegenRate, 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.
* 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} message - Whether not to send a CapturePointsChanged message. When false, caller should take care of sending that message.
*/
Capturable.prototype.UpdateCachedValuesAndNotify = function(dontSendCpChanged = false)
Capturable.prototype.UpdateCachedValuesAndNotify = function(sendMessage = true)
{
let oldMaxCp = this.maxCp;
let oldMaxCapturePoints = this.maxCapturePoints;
let oldGarrisonRegenRate = this.garrisonRegenRate;
let oldRegenRate = this.regenRate;
this.UpdateCachedValues();
if (oldMaxCp != this.maxCp)
if (oldMaxCapturePoints != this.maxCapturePoints)
{
let scale = this.maxCp / oldMaxCp;
for (let i in this.cp)
this.cp[i] *= scale;
if (!dontSendCpChanged)
Engine.PostMessage(this.entity, MT_CapturePointsChanged, { "capturePoints": this.cp });
let scale = this.maxCapturePoints / oldMaxCapturePoints;
for (let i in this.capturePoints)
this.capturePoints[i] *= scale;
if (sendMessage)
Engine.PostMessage(this.entity, MT_CapturePointsChanged, { "capturePoints": this.capturePoints });
}
if (oldGarrisonRegenRate != this.garrisonRegenRate || oldRegenRate != this.regenRate)
@@ -322,16 +322,16 @@ Capturable.prototype.OnOwnershipChanged = function(msg)
return;
// Initialise the capture points when created.
if (!this.cp.length)
if (!this.capturePoints.length)
{
this.UpdateCachedValues();
let numPlayers = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager).GetNumPlayers();
for (let i = 0; i < numPlayers; ++i)
{
if (i == msg.to)
this.cp[i] = this.maxCp;
this.capturePoints[i] = this.maxCapturePoints;
else
this.cp[i] = 0;
this.capturePoints[i] = 0;
}
this.CheckTimer();
return;
@@ -339,11 +339,11 @@ Capturable.prototype.OnOwnershipChanged = function(msg)
// When already initialised, this happens on defeat or wololo,
// transfer the points of the old owner to the new one.
if (this.cp[msg.from])
if (this.capturePoints[msg.from])
{
this.cp[msg.to] += this.cp[msg.from];
this.cp[msg.from] = 0;
this.UpdateCachedValuesAndNotify(true);
this.capturePoints[msg.to] += this.capturePoints[msg.from];
this.capturePoints[msg.from] = 0;
this.UpdateCachedValuesAndNotify(false);
this.RegisterCapturePointsChanged();
return;
}
@@ -352,19 +352,19 @@ Capturable.prototype.OnOwnershipChanged = function(msg)
};
/**
* When a player is defeated, reassign the cp of non-owned entities to gaia.
* When a player is defeated, reassign the capture points of non-owned entities to gaia.
* Those owned by the defeated player are dealt with onOwnershipChanged.
*/
Capturable.prototype.OnGlobalPlayerDefeated = function(msg)
{
if (!this.cp[msg.playerId])
if (!this.capturePoints[msg.playerId])
return;
let cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership);
if (cmpOwnership && (cmpOwnership.GetOwner() == INVALID_PLAYER ||
cmpOwnership.GetOwner() == msg.playerId))
return;
this.cp[0] += this.cp[msg.playerId];
this.cp[msg.playerId] = 0;
this.capturePoints[0] += this.capturePoints[msg.playerId];
this.capturePoints[msg.playerId] = 0;
this.RegisterCapturePointsChanged();
this.CheckTimer();
};
@@ -257,7 +257,7 @@ StatusBars.prototype.AddCaptureBar = function(cmpOverlayRenderer, yoffset)
return 0;
this.usedPlayerColors = true;
let cp = cmpCapturable.GetCapturePoints();
let capturePoints = cmpCapturable.GetCapturePoints();
// Size of health bar (in world-space units)
let width = +this.template.BarWidth;
@@ -270,7 +270,7 @@ StatusBars.prototype.AddCaptureBar = function(cmpOverlayRenderer, yoffset)
{
let c = QueryPlayerIDInterface(playerID).GetDisplayedColor();
let strColor = (c.r * 255) + " " + (c.g * 255) + " " + (c.b * 255) + " 255";
let size = width * cp[playerID] / cmpCapturable.GetMaxCapturePoints();
let size = width * capturePoints[playerID] / cmpCapturable.GetMaxCapturePoints();
cmpOverlayRenderer.AddSprite(
"art/textures/ui/session/icons/capture_bar.png",
@@ -285,8 +285,8 @@ StatusBars.prototype.AddCaptureBar = function(cmpOverlayRenderer, yoffset)
// First handle the owner's points, to keep those points on the left for clarity
let size = setCaptureBarPart(owner, -width / 2);
for (let i in cp)
if (i != owner && cp[i] > 0)
for (let i in capturePoints)
if (i != owner && capturePoints[i] > 0)
size = setCaptureBarPart(i, size);
return height * 1.2;
@@ -17,7 +17,7 @@ var testData = {
"garrisonRegenRate": 5,
"decay": false,
"decayRate": 30,
"maxCp": 3000,
"maxCapturePoints": 3000,
"neighbours": [20, 0, 20, 10]
};
@@ -70,7 +70,7 @@ function testCapturable(testData, test_function)
});
let cmpCapturable = ConstructComponent(testData.structure, "Capturable", {
"CapturePoints": testData.maxCp,
"CapturePoints": testData.maxCapturePoints,
"RegenRate": testData.regenRate,
"GarrisonRegenRate": testData.garrisonRegenRate
});
@@ -128,17 +128,17 @@ testCapturable(testData, cmpCapturable => {
TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.GetCapturePoints(), [0, 0, 0, 3000]);
});
function testRegen(testData, cpIn, cpOut, regenerating)
function testRegen(testData, capturePointsIn, capturePointsOut, regenerating)
{
testCapturable(testData, cmpCapturable => {
cmpCapturable.SetCapturePoints(cpIn);
cmpCapturable.SetCapturePoints(capturePointsIn);
cmpCapturable.CheckTimer();
Engine.PostMessage = function(ent, iid, message) {
if (iid == MT_CaptureRegenStateChanged)
TS_ASSERT_UNEVAL_EQUALS(message.regenerating, regenerating);
};
cmpCapturable.TimerTick(cpIn);
TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.GetCapturePoints(), cpOut);
cmpCapturable.TimerTick(capturePointsIn);
TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.GetCapturePoints(), capturePointsOut);
});
}
@@ -153,17 +153,17 @@ testData.regenRate = -32;
testRegen(testData, [100, 2800, 50, 50], [112, 2796, 46, 46], true);
testData.regenRate = 2;
function testDecay(testData, cpIn, cpOut)
function testDecay(testData, capturePointsIn, capturePointsOut)
{
testCapturable(testData, cmpCapturable => {
cmpCapturable.SetCapturePoints(cpIn);
cmpCapturable.SetCapturePoints(capturePointsIn);
cmpCapturable.CheckTimer();
Engine.PostMessage = function(ent, iid, message) {
if (iid == MT_CaptureRegenStateChanged)
TS_ASSERT_UNEVAL_EQUALS(message.territoryDecay, testData.decayRate);
};
cmpCapturable.TimerTick();
TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.GetCapturePoints(), cpOut);
TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.GetCapturePoints(), capturePointsOut);
});
}
testData.decay = true;