Differential D4679 Diff 20553 binaries/data/mods/public/simulation/components/tests/test_Capturable.js
Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/components/tests/test_Capturable.js
Engine.LoadHelperScript("Player.js"); | Engine.LoadHelperScript("Player.js"); | ||||
Engine.LoadHelperScript("ValueModification.js"); | Engine.LoadHelperScript("ValueModification.js"); | ||||
Engine.LoadComponentScript("interfaces/Auras.js"); | Engine.LoadComponentScript("interfaces/Auras.js"); | ||||
Engine.LoadComponentScript("interfaces/Capturable.js"); | Engine.LoadComponentScript("interfaces/Capturable.js"); | ||||
Engine.LoadComponentScript("interfaces/GarrisonHolder.js"); | Engine.LoadComponentScript("interfaces/GarrisonHolder.js"); | ||||
Engine.LoadComponentScript("interfaces/StatisticsTracker.js"); | Engine.LoadComponentScript("interfaces/StatisticsTracker.js"); | ||||
Engine.LoadComponentScript("interfaces/ModifiersManager.js"); | Engine.LoadComponentScript("interfaces/ModifiersManager.js"); | ||||
Engine.LoadComponentScript("interfaces/TerritoryDecay.js"); | Engine.LoadComponentScript("interfaces/TerritoryDecay.js"); | ||||
Engine.LoadComponentScript("interfaces/Timer.js"); | Engine.LoadComponentScript("interfaces/Timer.js"); | ||||
Engine.LoadComponentScript("Capturable.js"); | Engine.LoadComponentScript("Capturable.js"); | ||||
var testData = { | var testData = { | ||||
"structure": 20, | "structure": 20, | ||||
"playerID": 1, | "playerID": 1, | ||||
"regenRate": 2, | "regenRate": 2, | ||||
"garrisonedEntities": [30, 31, 32, 33], | "garrisonedEntities": [30, 31, 32, 33], | ||||
"garrisonRegenRate": 5, | "garrisonRegenRate": 2, | ||||
"decay": false, | "decay": false, | ||||
"decayRate": 30, | "decayRate": 30, | ||||
"maxCapturePoints": 3000, | "maxCapturePoints": 3000, | ||||
"neighbours": [20, 0, 20, 10] | "neighbours": [20, 0, 20, 10] | ||||
}; | }; | ||||
const testCaptureAttacks = { | |||||
"30": 1.5, | |||||
"32": 2.0 | |||||
}; | |||||
function testCapturable(testData, test_function) | function testCapturable(testData, test_function) | ||||
{ | { | ||||
ResetState(); | ResetState(); | ||||
AddMock(SYSTEM_ENTITY, IID_Timer, { | AddMock(SYSTEM_ENTITY, IID_Timer, { | ||||
"SetInterval": (ent, iid, funcname, time, repeattime, data) => {}, | "SetInterval": (ent, iid, funcname, time, repeattime, data) => {}, | ||||
"CancelTimer": timer => {} | "CancelTimer": timer => {} | ||||
}); | }); | ||||
▲ Show 20 Lines • Show All 44 Lines • ▼ Show 20 Lines | function testCapturable(testData, test_function) | ||||
}); | }); | ||||
AddMock(testData.structure, IID_TerritoryDecay, { | AddMock(testData.structure, IID_TerritoryDecay, { | ||||
"IsDecaying": () => testData.decay, | "IsDecaying": () => testData.decay, | ||||
"GetDecayRate": () => testData.decayRate, | "GetDecayRate": () => testData.decayRate, | ||||
"GetConnectedNeighbours": () => testData.neighbours | "GetConnectedNeighbours": () => testData.neighbours | ||||
}); | }); | ||||
TS_ASSERT_EQUALS(cmpCapturable.GetRegenRate(), testData.regenRate + testData.garrisonRegenRate * testData.garrisonedEntities.length); | let regenRate = testData.regenRate; | ||||
for (const entity of testData.garrisonedEntities) | |||||
{ | |||||
if (testCaptureAttacks[entity] === undefined) | |||||
continue; | |||||
AddMock(entity, IID_Attack, { | |||||
"GetAttackEffectsData": (type) => { | |||||
return type === "Capture" ? { "Capture": testCaptureAttacks[entity] } : undefined; | |||||
}, | |||||
}); | |||||
regenRate += testCaptureAttacks[entity] * testData.garrisonRegenRate; | |||||
} | |||||
TS_ASSERT_EQUALS(cmpCapturable.GetRegenRate(), regenRate); | |||||
test_function(cmpCapturable); | test_function(cmpCapturable); | ||||
Engine.PostMessage = (ent, iid, message) => {}; | Engine.PostMessage = (ent, iid, message) => {}; | ||||
} | } | ||||
// Tests initialisation of the capture points when the entity is created | // Tests initialisation of the capture points when the entity is created | ||||
testCapturable(testData, cmpCapturable => { | testCapturable(testData, cmpCapturable => { | ||||
Engine.PostMessage = function(ent, iid, message) { | Engine.PostMessage = function(ent, iid, message) { | ||||
TS_ASSERT_UNEVAL_EQUALS(message, { "regenerating": true, "regenRate": cmpCapturable.GetRegenRate(), "territoryDecay": 0 }); | TS_ASSERT_UNEVAL_EQUALS(message, { "regenerating": true, "regenRate": cmpCapturable.GetRegenRate(), "territoryDecay": 0 }); | ||||
▲ Show 20 Lines • Show All 44 Lines • ▼ Show 20 Lines | Engine.PostMessage = function(ent, iid, message) { | ||||
if (iid == MT_CaptureRegenStateChanged) | if (iid == MT_CaptureRegenStateChanged) | ||||
TS_ASSERT_UNEVAL_EQUALS(message.regenerating, regenerating); | TS_ASSERT_UNEVAL_EQUALS(message.regenerating, regenerating); | ||||
}; | }; | ||||
cmpCapturable.TimerTick(capturePointsIn); | cmpCapturable.TimerTick(capturePointsIn); | ||||
TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.GetCapturePoints(), capturePointsOut); | TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.GetCapturePoints(), capturePointsOut); | ||||
}); | }); | ||||
} | } | ||||
// With our testData, the total regen rate is 22. That should be taken from the ennemies | // With our testData, the total regen rate is 9. That should be taken from the enemies | ||||
testRegen(testData, [12, 2950, 2, 36], [1, 2972, 2, 25], true); | testRegen(testData, [12, 2950, 2, 36], [7.5, 2959, 2, 31.5], true); | ||||
testRegen(testData, [0, 2994, 2, 4], [0, 2998, 2, 0], true); | testRegen(testData, [0, 2994, 2, 4], [0, 2998, 2, 0], true); | ||||
testRegen(testData, [0, 2998, 2, 0], [0, 2998, 2, 0], false); | 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 | // If the regeneration rate becomes negative, capture points are given in favour of gaia | ||||
testData.regenRate = -32; | testData.regenRate = -19; | ||||
// With our testData, the total regen rate is -12. That should be taken from all players to gaia | // 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); | testRegen(testData, [100, 2800, 50, 50], [112, 2796, 46, 46], true); | ||||
testData.regenRate = 2; | testData.regenRate = 2; | ||||
function testDecay(testData, capturePointsIn, capturePointsOut) | function testDecay(testData, capturePointsIn, capturePointsOut) | ||||
{ | { | ||||
testCapturable(testData, cmpCapturable => { | testCapturable(testData, cmpCapturable => { | ||||
cmpCapturable.SetCapturePoints(capturePointsIn); | cmpCapturable.SetCapturePoints(capturePointsIn); | ||||
cmpCapturable.CheckTimer(); | cmpCapturable.CheckTimer(); | ||||
Engine.PostMessage = function(ent, iid, message) { | Engine.PostMessage = function(ent, iid, message) { | ||||
if (iid == MT_CaptureRegenStateChanged) | if (iid == MT_CaptureRegenStateChanged) | ||||
TS_ASSERT_UNEVAL_EQUALS(message.territoryDecay, testData.decayRate); | TS_ASSERT_UNEVAL_EQUALS(message.territoryDecay, testData.decayRate); | ||||
}; | }; | ||||
cmpCapturable.TimerTick(); | cmpCapturable.TimerTick(); | ||||
TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.GetCapturePoints(), capturePointsOut); | TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.GetCapturePoints(), capturePointsOut); | ||||
}); | }); | ||||
} | } | ||||
testData.decay = true; | testData.decay = true; | ||||
testData.regenRate = 8; | |||||
testData.garrisonRegenRate = 4; | |||||
// 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. | // 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]); | testDecay(testData, [2900, 35, 10, 55], [2901, 27, 22, 50]); | ||||
testData.decay = false; | testData.decay = false; | ||||
// Tests Reduce | // Tests Reduce | ||||
function testReduce(testData, amount, player, taken) | function testReduce(testData, amount, player, taken) | ||||
{ | { | ||||
testCapturable(testData, cmpCapturable => { | testCapturable(testData, cmpCapturable => { | ||||
Show All 22 Lines |
Wildfire Games · Phabricator