mirror of
https://gitea.wildfiregames.com/0ad/0ad.git
synced 2026-09-21 20:06:40 +00:00
Adds Capturable component tests. Fixes a message. Thanks elexis and sanderd17 for the remarks. Fixes #4149.
This was SVN commit r18707.
This commit is contained in:
@@ -98,9 +98,9 @@ Capturable.prototype.Reduce = function(amount, playerID)
|
|||||||
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
|
||||||
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;
|
||||||
@@ -218,7 +218,7 @@ Capturable.prototype.TimerTick = function()
|
|||||||
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;
|
||||||
Engine.PostMessage(this.entity, MT_CaptureRegenStateChanged, {"regenerating": false, "regenRate": 0, "territoryDecay": 0});
|
Engine.PostMessage(this.entity, MT_CaptureRegenStateChanged, { "regenerating": false, "regenRate": 0, "territoryDecay": 0 });
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -239,7 +239,7 @@ Capturable.prototype.CheckTimer = function()
|
|||||||
|
|
||||||
var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
|
var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
|
||||||
this.timer = cmpTimer.SetInterval(this.entity, IID_Capturable, "TimerTick", 1000, 1000, null);
|
this.timer = cmpTimer.SetInterval(this.entity, IID_Capturable, "TimerTick", 1000, 1000, null);
|
||||||
Engine.PostMessage(this.entity, MT_CaptureRegenStateChanged, {"ticking": true, "regenRate": regenRate, "territoryDecay": decay});
|
Engine.PostMessage(this.entity, MT_CaptureRegenStateChanged, { "regenerating": true, "regenRate": regenRate, "territoryDecay": decay });
|
||||||
};
|
};
|
||||||
|
|
||||||
//// Message Listeners ////
|
//// Message Listeners ////
|
||||||
|
|||||||
@@ -8,10 +8,8 @@ Engine.RegisterInterface("Capturable");
|
|||||||
Engine.RegisterMessageType("CapturePointsChanged");
|
Engine.RegisterMessageType("CapturePointsChanged");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Message in the form of { "regenerating": boolean, "rate": number, "territoryDecay": number }
|
* Message in the form of { "regenerating": boolean, "regenRate": number, "territoryDecay": number }
|
||||||
* or in the form { "ticking": boolean, "rate": number, "territoryDecay": number }
|
|
||||||
* where "rate" value is always zero when not decaying,
|
* where "rate" value is always zero when not decaying,
|
||||||
* sent from Capturable component.
|
* sent from Capturable component.
|
||||||
*/
|
*/
|
||||||
Engine.RegisterMessageType("CaptureRegenStateChanged");
|
Engine.RegisterMessageType("CaptureRegenStateChanged");
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,193 @@
|
|||||||
|
Engine.LoadHelperScript("Player.js");
|
||||||
|
Engine.LoadHelperScript("ValueModification.js");
|
||||||
|
Engine.LoadComponentScript("interfaces/AuraManager.js");
|
||||||
|
Engine.LoadComponentScript("interfaces/Auras.js");
|
||||||
|
Engine.LoadComponentScript("interfaces/Capturable.js");
|
||||||
|
Engine.LoadComponentScript("interfaces/GarrisonHolder.js");
|
||||||
|
Engine.LoadComponentScript("interfaces/StatisticsTracker.js");
|
||||||
|
Engine.LoadComponentScript("interfaces/TechnologyManager.js");
|
||||||
|
Engine.LoadComponentScript("interfaces/TerritoryDecay.js");
|
||||||
|
Engine.LoadComponentScript("interfaces/Timer.js");
|
||||||
|
Engine.LoadComponentScript("Capturable.js");
|
||||||
|
|
||||||
|
let testData = {
|
||||||
|
"structure": 20,
|
||||||
|
"playerID": 1,
|
||||||
|
"regenRate": 2,
|
||||||
|
"garrisonedEntities": [30,31,32,33],
|
||||||
|
"garrisonRegenRate": 5,
|
||||||
|
"decay": false,
|
||||||
|
"decayRate": 30,
|
||||||
|
"maxCp": 3000,
|
||||||
|
"neighbours": [20, 0, 20, 10]
|
||||||
|
};
|
||||||
|
|
||||||
|
function testCapturable(testData, test_function)
|
||||||
|
{
|
||||||
|
ResetState();
|
||||||
|
|
||||||
|
AddMock(SYSTEM_ENTITY, IID_Timer, {
|
||||||
|
"SetInterval": (ent, iid, funcname, time, repeattime, data) => {},
|
||||||
|
"CancelTimer": timer => {}
|
||||||
|
});
|
||||||
|
|
||||||
|
AddMock(testData.structure, IID_Ownership, {
|
||||||
|
"GetOwner": () => testData.playerID,
|
||||||
|
"SetOwner": id => {}
|
||||||
|
});
|
||||||
|
|
||||||
|
AddMock(testData.structure, IID_GarrisonHolder, {
|
||||||
|
"GetEntities": () => testData.garrisonedEntities
|
||||||
|
});
|
||||||
|
|
||||||
|
AddMock(testData.structure, IID_Fogging, {
|
||||||
|
"Activate": () => {}
|
||||||
|
});
|
||||||
|
|
||||||
|
AddMock(10, IID_Player, {
|
||||||
|
"IsEnemy": id => id != 0
|
||||||
|
});
|
||||||
|
|
||||||
|
AddMock(11, IID_Player, {
|
||||||
|
"IsEnemy": id => id != 1 && id != 2
|
||||||
|
});
|
||||||
|
|
||||||
|
AddMock(12, IID_Player, {
|
||||||
|
"IsEnemy": id => id != 1 && id != 2
|
||||||
|
});
|
||||||
|
|
||||||
|
AddMock(13, IID_Player, {
|
||||||
|
"IsEnemy": id => id != 3
|
||||||
|
});
|
||||||
|
|
||||||
|
AddMock(SYSTEM_ENTITY, IID_PlayerManager, {
|
||||||
|
"GetNumPlayers": () => 4,
|
||||||
|
"GetPlayerByID": id => 10 + id
|
||||||
|
});
|
||||||
|
|
||||||
|
AddMock(testData.structure, IID_StatisticsTracker, {
|
||||||
|
"LostEntity" : () => {},
|
||||||
|
"CapturedBuilding": () => {}
|
||||||
|
});
|
||||||
|
|
||||||
|
let cmpCapturable = ConstructComponent(testData.structure, "Capturable", {
|
||||||
|
"CapturePoints" : testData.maxCp,
|
||||||
|
"RegenRate" : testData.regenRate,
|
||||||
|
"GarrisonRegenRate" : testData.garrisonRegenRate
|
||||||
|
});
|
||||||
|
|
||||||
|
AddMock(testData.structure, IID_TerritoryDecay, {
|
||||||
|
"IsDecaying": () => testData.decay,
|
||||||
|
"GetDecayRate": () => testData.decayRate,
|
||||||
|
"GetConnectedNeighbours": () => testData.neighbours
|
||||||
|
});
|
||||||
|
|
||||||
|
TS_ASSERT_EQUALS(cmpCapturable.GetRegenRate(), testData.regenRate + testData.garrisonRegenRate * testData.garrisonedEntities.length);
|
||||||
|
test_function(cmpCapturable);
|
||||||
|
Engine.PostMessage = (ent, iid, message) => {};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tests initialisation of the capture points when the entity is created
|
||||||
|
testCapturable(testData, cmpCapturable => {
|
||||||
|
Engine.PostMessage = function(ent, iid, message) {
|
||||||
|
TS_ASSERT_UNEVAL_EQUALS(message, { "regenerating": true, "regenRate": cmpCapturable.GetRegenRate() , "territoryDecay": 0 });
|
||||||
|
};
|
||||||
|
cmpCapturable.OnOwnershipChanged({ "from": -1, "to": testData.playerID });
|
||||||
|
TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.GetCapturePoints(), [0, 3000, 0, 0])
|
||||||
|
});
|
||||||
|
|
||||||
|
// Tests if the message is sent when capture points change
|
||||||
|
testCapturable(testData, cmpCapturable => {
|
||||||
|
cmpCapturable.SetCapturePoints([0, 2000, 0 , 1000]);
|
||||||
|
TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.GetCapturePoints(), [0, 2000, 0 , 1000]);
|
||||||
|
Engine.PostMessage = function(ent, iid, message)
|
||||||
|
{
|
||||||
|
TS_ASSERT_UNEVAL_EQUALS(message, { "capturePoints": [0, 2000, 0 , 1000] });
|
||||||
|
};
|
||||||
|
cmpCapturable.RegisterCapturePointsChanged();
|
||||||
|
});
|
||||||
|
|
||||||
|
// Tests reducing capture points (after a capture attack or a decay)
|
||||||
|
testCapturable(testData, cmpCapturable => {
|
||||||
|
cmpCapturable.SetCapturePoints([0, 2000, 0 , 1000]);
|
||||||
|
cmpCapturable.CheckTimer();
|
||||||
|
Engine.PostMessage = function(ent, iid, message) {
|
||||||
|
if (iid == MT_CapturePointsChanged)
|
||||||
|
TS_ASSERT_UNEVAL_EQUALS(message, { "capturePoints": [0, 2000 - 100, 0, 1000 + 100] });
|
||||||
|
if (iid == MT_CaptureRegenStateChanged)
|
||||||
|
TS_ASSERT_UNEVAL_EQUALS(message, { "regenerating": true, "regenRate": cmpCapturable.GetRegenRate(), "territoryDecay": 0 });
|
||||||
|
};
|
||||||
|
TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.Reduce(100, 3), 100);
|
||||||
|
TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.GetCapturePoints(), [0, 2000 - 100, 0, 1000 + 100]);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Tests reducing capture points (after a capture attack or a decay)
|
||||||
|
testCapturable(testData, cmpCapturable => {
|
||||||
|
cmpCapturable.SetCapturePoints([0, 2000, 0 , 1000]);
|
||||||
|
cmpCapturable.CheckTimer();
|
||||||
|
TS_ASSERT_EQUALS(cmpCapturable.Reduce(2500, 3),2000);
|
||||||
|
TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.GetCapturePoints(), [0, 0, 0, 3000]);
|
||||||
|
});
|
||||||
|
|
||||||
|
function testRegen(testData, cpIn, cpOut, regenerating)
|
||||||
|
{
|
||||||
|
testCapturable(testData, cmpCapturable => {
|
||||||
|
cmpCapturable.SetCapturePoints(cpIn);
|
||||||
|
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);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// With our testData, the total regen rate is 22. That should be taken from the ennemies
|
||||||
|
testRegen(testData, [12, 2950, 2 , 36], [1, 2972, 2, 25], true);
|
||||||
|
testRegen(testData, [0, 2994, 2, 4], [0, 2998, 2, 0], true);
|
||||||
|
testRegen(testData, [0, 2998, 2, 0], [0, 2998, 2, 0], false);
|
||||||
|
|
||||||
|
// If the regeneration rate becomes negative, capture points are given in favour of gaia
|
||||||
|
testData.regenRate = -32;
|
||||||
|
// With our testData, the total regen rate is -12. That should be taken from all players to gaia
|
||||||
|
testRegen(testData, [100, 2800, 50, 50], [112, 2796, 46, 46], true);
|
||||||
|
testData.regenRate = 2;
|
||||||
|
|
||||||
|
function testDecay(testData, cpIn, cpOut)
|
||||||
|
{
|
||||||
|
testCapturable(testData, cmpCapturable => {
|
||||||
|
cmpCapturable.SetCapturePoints(cpIn);
|
||||||
|
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);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
testData.decay = true;
|
||||||
|
// With our testData, the decay rate is 30, that should be given to all neighbours with weights [20/50, 0, 20/50, 10/50], then it regens.
|
||||||
|
testDecay(testData, [2900, 35, 10, 55], [2901, 27, 22, 50]);
|
||||||
|
testData.decay = false;
|
||||||
|
|
||||||
|
// Tests Reduce
|
||||||
|
function testReduce(testData, amount, player, taken)
|
||||||
|
{
|
||||||
|
testCapturable(testData, cmpCapturable => {
|
||||||
|
cmpCapturable.SetCapturePoints([0, 2000, 0 , 1000]);
|
||||||
|
cmpCapturable.CheckTimer();
|
||||||
|
TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.Reduce(amount, player), taken);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
testReduce(testData, 50, 3, 50);
|
||||||
|
testReduce(testData, 50, 2, 50);
|
||||||
|
testReduce(testData, 50, 1, 50);
|
||||||
|
testReduce(testData, -50, 3, 0);
|
||||||
|
testReduce(testData, 50, 0, 50);
|
||||||
|
testReduce(testData, 0, 3, 0);
|
||||||
|
testReduce(testData, 1500, 3, 1500);
|
||||||
|
testReduce(testData, 2000, 3, 2000);
|
||||||
|
testReduce(testData, 3000, 3, 2000);
|
||||||
Reference in New Issue
Block a user