| 1 | Engine.LoadHelperScript("Player.js"); |
| 2 | Engine.LoadHelperScript("ValueModification.js"); |
| 3 | Engine.LoadComponentScript("interfaces/AuraManager.js"); |
| 4 | Engine.LoadComponentScript("interfaces/Auras.js"); |
| 5 | Engine.LoadComponentScript("interfaces/Capturable.js"); |
| 6 | Engine.LoadComponentScript("interfaces/GarrisonHolder.js"); |
| 7 | Engine.LoadComponentScript("interfaces/StatisticsTracker.js"); |
| 8 | Engine.LoadComponentScript("interfaces/TechnologyManager.js"); |
| 9 | Engine.LoadComponentScript("interfaces/TerritoryDecay.js"); |
| 10 | Engine.LoadComponentScript("interfaces/Timer.js"); |
| 11 | Engine.LoadComponentScript("Capturable.js"); |
| 12 | |
| 13 | let testData = { |
| 14 | "structure": 20, |
| 15 | "playerID": 1, |
| 16 | "regenRate": 2, |
| 17 | "garrisonedEntities": [30,31,32,33], |
| 18 | "garrisonRegenRate": 5, |
| 19 | "decay": false, |
| 20 | "decayRate": 30, |
| 21 | "maxCp": 3000, |
| 22 | "neighbours": [20, 0, 20, 10] |
| 23 | }; |
| 24 | |
| 25 | function testCapturable(testData, test_function) |
| 26 | { |
| 27 | ResetState(); |
| 28 | |
| 29 | AddMock(SYSTEM_ENTITY, IID_Timer, { |
| 30 | "SetInterval": (ent, iid, funcname, time, repeattime, data) => {}, |
| 31 | "CancelTimer": timer => {} |
| 32 | }); |
| 33 | |
| 34 | AddMock(testData.structure, IID_Ownership, { |
| 35 | "GetOwner": () => testData.playerID, |
| 36 | "SetOwner": id => {} |
| 37 | }); |
| 38 | |
| 39 | AddMock(testData.structure, IID_GarrisonHolder, { |
| 40 | "GetEntities": () => testData.garrisonedEntities |
| 41 | }); |
| 42 | |
| 43 | AddMock(testData.structure, IID_Fogging, { |
| 44 | "Activate": () => {} |
| 45 | }); |
| 46 | |
| 47 | AddMock(10, IID_Player, { |
| 48 | "IsEnemy": id => id != 0 |
| 49 | }); |
| 50 | |
| 51 | AddMock(11, IID_Player, { |
| 52 | "IsEnemy": id => id != 1 && id != 2 |
| 53 | }); |
| 54 | |
| 55 | AddMock(12, IID_Player, { |
| 56 | "IsEnemy": id => id != 1 && id != 2 |
| 57 | }); |
| 58 | |
| 59 | AddMock(13, IID_Player, { |
| 60 | "IsEnemy": id => id != 3 |
| 61 | }); |
| 62 | |
| 63 | AddMock(SYSTEM_ENTITY, IID_PlayerManager, { |
| 64 | "GetNumPlayers": () => 4, |
| 65 | "GetPlayerByID": id => 10 + id |
| 66 | }); |
| 67 | |
| 68 | AddMock(testData.structure, IID_StatisticsTracker, { |
| 69 | "LostEntity" : () => {}, |
| 70 | "CapturedBuilding": () => {} |
| 71 | }); |
| 72 | |
| 73 | let cmpCapturable = ConstructComponent(testData.structure, "Capturable", { |
| 74 | "CapturePoints" : testData.maxCp, |
| 75 | "RegenRate" : testData.regenRate, |
| 76 | "GarrisonRegenRate" : testData.garrisonRegenRate |
| 77 | }); |
| 78 | |
| 79 | AddMock(testData.structure, IID_TerritoryDecay, { |
| 80 | "IsDecaying": () => testData.decay, |
| 81 | "GetDecayRate": () => testData.decayRate, |
| 82 | "GetConnectedNeighbours": () => testData.neighbours |
| 83 | }); |
| 84 | |
| 85 | TS_ASSERT_EQUALS(cmpCapturable.GetRegenRate(), testData.regenRate + testData.garrisonRegenRate * testData.garrisonedEntities.length); |
| 86 | test_function(cmpCapturable); |
| 87 | Engine.PostMessage = (ent, iid, message) => {}; |
| 88 | } |
| 89 | |
| 90 | // Tests initialisation of the capture points when the entity is created |
| 91 | testCapturable(testData, cmpCapturable => { |
| 92 | Engine.PostMessage = function(ent, iid, message) { |
| 93 | TS_ASSERT_UNEVAL_EQUALS(message, { "regenerating": true, "regenRate": cmpCapturable.GetRegenRate() , "territoryDecay": 0 }); |
| 94 | }; |
| 95 | cmpCapturable.OnOwnershipChanged({ "from": -1, "to": testData.playerID }); |
| 96 | TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.GetCapturePoints(), [0, 3000, 0, 0]) |
| 97 | }); |
| 98 | |
| 99 | // Tests if the message is sent when capture points change |
| 100 | testCapturable(testData, cmpCapturable => { |
| 101 | cmpCapturable.SetCapturePoints([0, 2000, 0 , 1000]); |
| 102 | TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.GetCapturePoints(), [0, 2000, 0 , 1000]); |
| 103 | Engine.PostMessage = function(ent, iid, message) |
| 104 | { |
| 105 | TS_ASSERT_UNEVAL_EQUALS(message, { "capturePoints": [0, 2000, 0 , 1000] }); |
| 106 | }; |
| 107 | cmpCapturable.RegisterCapturePointsChanged(); |
| 108 | }); |
| 109 | |
| 110 | // Tests reducing capture points (after a capture attack or a decay) |
| 111 | testCapturable(testData, cmpCapturable => { |
| 112 | cmpCapturable.SetCapturePoints([0, 2000, 0 , 1000]); |
| 113 | cmpCapturable.CheckTimer(); |
| 114 | Engine.PostMessage = function(ent, iid, message) { |
| 115 | if (iid == MT_CapturePointsChanged) |
| 116 | TS_ASSERT_UNEVAL_EQUALS(message, { "capturePoints": [0, 2000 - 100, 0, 1000 + 100] }); |
| 117 | if (iid == MT_CaptureRegenStateChanged) |
| 118 | TS_ASSERT_UNEVAL_EQUALS(message, { "regenerating": true, "regenRate": cmpCapturable.GetRegenRate(), "territoryDecay": 0 }); |
| 119 | }; |
| 120 | TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.Reduce(100, 3), 100); |
| 121 | TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.GetCapturePoints(), [0, 2000 - 100, 0, 1000 + 100]); |
| 122 | }); |
| 123 | |
| 124 | // Tests reducing capture points (after a capture attack or a decay) |
| 125 | testCapturable(testData, cmpCapturable => { |
| 126 | cmpCapturable.SetCapturePoints([0, 2000, 0 , 1000]); |
| 127 | cmpCapturable.CheckTimer(); |
| 128 | TS_ASSERT_EQUALS(cmpCapturable.Reduce(2500, 3),2000); |
| 129 | TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.GetCapturePoints(), [0, 0, 0, 3000]); |
| 130 | }); |
| 131 | |
| 132 | function testRegen(testData, cpIn, cpOut, regenerating) |
| 133 | { |
| 134 | testCapturable(testData, cmpCapturable => { |
| 135 | cmpCapturable.SetCapturePoints(cpIn); |
| 136 | cmpCapturable.CheckTimer(); |
| 137 | Engine.PostMessage = function(ent, iid, message) { |
| 138 | if (iid == MT_CaptureRegenStateChanged) |
| 139 | TS_ASSERT_UNEVAL_EQUALS(message.regenerating, regenerating); |
| 140 | }; |
| 141 | cmpCapturable.TimerTick(cpIn); |
| 142 | TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.GetCapturePoints(), cpOut); |
| 143 | }); |
| 144 | } |
| 145 | |
| 146 | // With our testData, the total regen rate is 22. That should be taken from the ennemies |
| 147 | testRegen(testData, [12, 2950, 2 , 36], [1, 2972, 2, 25], true); |
| 148 | testRegen(testData, [0, 2994, 2, 4], [0, 2998, 2, 0], true); |
| 149 | testRegen(testData, [0, 2998, 2, 0], [0, 2998, 2, 0], false); |
| 150 | |
| 151 | // If the regeneration rate becomes negative, capture points are given in favour of gaia |
| 152 | testData.regenRate = -32; |
| 153 | // With our testData, the total regen rate is -12. That should be taken from all players to gaia |
| 154 | testRegen(testData, [100, 2800, 50, 50], [112, 2796, 46, 46], true); |
| 155 | testData.regenRate = 2; |
| 156 | |
| 157 | function testDecay(testData, cpIn, cpOut) |
| 158 | { |
| 159 | testCapturable(testData, cmpCapturable => { |
| 160 | cmpCapturable.SetCapturePoints(cpIn); |
| 161 | cmpCapturable.CheckTimer(); |
| 162 | Engine.PostMessage = function(ent, iid, message) { |
| 163 | if (iid == MT_CaptureRegenStateChanged) |
| 164 | TS_ASSERT_UNEVAL_EQUALS(message.territoryDecay, testData.decayRate); |
| 165 | }; |
| 166 | cmpCapturable.TimerTick(); |
| 167 | TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.GetCapturePoints(), cpOut); |
| 168 | }); |
| 169 | } |
| 170 | testData.decay = true; |
| 171 | // 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. |
| 172 | testDecay(testData, [2900, 35, 10, 55], [2901, 27, 22, 50]); |
| 173 | testData.decay = false; |
| 174 | |
| 175 | // Tests Reduce |
| 176 | function testReduce(testData, amount, player, taken) |
| 177 | { |
| 178 | testCapturable(testData, cmpCapturable => { |
| 179 | cmpCapturable.SetCapturePoints([0, 2000, 0 , 1000]); |
| 180 | cmpCapturable.CheckTimer(); |
| 181 | TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.Reduce(amount, player), taken); |
| 182 | }); |
| 183 | } |
| 184 | |
| 185 | testReduce(testData, 50, 3, 50); |
| 186 | testReduce(testData, 50, 2, 50); |
| 187 | testReduce(testData, 50, 1, 50); |
| 188 | testReduce(testData, -50, 3, 0); |
| 189 | testReduce(testData, 50, 0, 50); |
| 190 | testReduce(testData, 0, 3, 0); |
| 191 | testReduce(testData, 1500, 3, 1500); |
| 192 | testReduce(testData, 2000, 3, 2000); |
| 193 | testReduce(testData, 3000, 3, 2000); |