| 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 structure = 20; |
| 14 | let playerID = 1; |
| 15 | let regenRate = 2; |
| 16 | let garrisonedEntities = [30,31,32,33]; |
| 17 | let decay = false; |
| 18 | let decayRate = 30; |
| 19 | let maxCp = 3000; |
| 20 | |
| 21 | function testCapturable(test_function) |
| 22 | { |
| 23 | ResetState(); |
| 24 | |
| 25 | AddMock(SYSTEM_ENTITY, IID_Timer, { |
| 26 | "SetInterval": (ent, iid, funcname, time, repeattime, data) => {}, |
| 27 | "CancelTimer": timer => {} |
| 28 | }); |
| 29 | |
| 30 | AddMock(structure, IID_Ownership, { |
| 31 | "GetOwner": () => playerID, |
| 32 | "SetOwner": id => {} |
| 33 | }); |
| 34 | |
| 35 | AddMock(structure, IID_GarrisonHolder, { |
| 36 | "GetEntities": () => garrisonedEntities |
| 37 | }); |
| 38 | |
| 39 | AddMock(structure, IID_Fogging, { |
| 40 | "Activate": () => {} |
| 41 | }); |
| 42 | |
| 43 | AddMock(10, IID_Player, { |
| 44 | "IsEnemy": id => id != 0 |
| 45 | }); |
| 46 | |
| 47 | AddMock(11, IID_Player, { |
| 48 | "IsEnemy": id => id != 1 && id != 2 |
| 49 | }); |
| 50 | |
| 51 | AddMock(12, IID_Player, { |
| 52 | "IsEnemy": id => id != 1 && id != 2 |
| 53 | }); |
| 54 | |
| 55 | AddMock(13, IID_Player, { |
| 56 | "IsEnemy": id => id != 3 |
| 57 | }); |
| 58 | |
| 59 | AddMock(SYSTEM_ENTITY, IID_PlayerManager, { |
| 60 | "GetNumPlayers": () => 4, |
| 61 | "GetPlayerByID": id => 10 + id |
| 62 | }); |
| 63 | |
| 64 | AddMock(structure, IID_StatisticsTracker, { |
| 65 | "LostEntity" : () => {}, |
| 66 | "CapturedBuilding": () => {} |
| 67 | }); |
| 68 | |
| 69 | let cmpCapturable = ConstructComponent(structure, "Capturable", { |
| 70 | "CapturePoints" : maxCp, |
| 71 | "RegenRate" : regenRate, |
| 72 | "GarrisonRegenRate" : 5 |
| 73 | }); |
| 74 | |
| 75 | AddMock(structure, IID_TerritoryDecay, { |
| 76 | "IsDecaying": () => decay, |
| 77 | "GetDecayRate": () => decayRate, |
| 78 | "GetConnectedNeighbours": () => [20, 0, 20, 10] |
| 79 | }); |
| 80 | |
| 81 | test_function(cmpCapturable); |
| 82 | Engine.PostMessage = (ent, iid, message) => {}; |
| 83 | } |
| 84 | |
| 85 | // Tests initialisation of the capture points when the entity is created |
| 86 | testCapturable( cmpCapturable => { |
| 87 | Engine.PostMessage = function(ent, iid, message) |
| 88 | { |
| 89 | TS_ASSERT_UNEVAL_EQUALS(message, { "regenerating": true, "regenRate": 22 , "territoryDecay": 0 }); |
| 90 | }; |
| 91 | cmpCapturable.OnOwnershipChanged({ "from": -1, "to": playerID }); |
| 92 | TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.GetCapturePoints(), [0, maxCp, 0, 0]) |
| 93 | }); |
| 94 | |
| 95 | // Tests if the message is sent when capture points change |
| 96 | testCapturable( cmpCapturable => { |
| 97 | cmpCapturable.SetCapturePoints([0, 2000, 0 , 1000]); |
| 98 | TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.GetCapturePoints(), [0, 2000, 0 , 1000]); |
| 99 | Engine.PostMessage = function(ent, iid, message) |
| 100 | { |
| 101 | TS_ASSERT_UNEVAL_EQUALS(message, { "capturePoints": [0, 2000, 0 , 1000] }); |
| 102 | }; |
| 103 | cmpCapturable.RegisterCapturePointsChanged(); |
| 104 | }); |
| 105 | |
| 106 | // Tests reducing capture points (after a capture attack or a decay) |
| 107 | testCapturable( cmpCapturable => { |
| 108 | cmpCapturable.SetCapturePoints([0, 2000, 0 , 1000]); |
| 109 | cmpCapturable.CheckTimer(); |
| 110 | Engine.PostMessage = function(ent, iid, message) |
| 111 | { |
| 112 | if (iid == MT_CapturePointsChanged) |
| 113 | TS_ASSERT_UNEVAL_EQUALS(message, { "capturePoints": [0, 2000 - 100, 0, 1000 + 100] }); |
| 114 | if (iid == MT_CaptureRegenStateChanged) |
| 115 | TS_ASSERT_UNEVAL_EQUALS(message, { "regenerating": true, "regenRate": 22, "territoryDecay": 0 }); |
| 116 | }; |
| 117 | TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.Reduce(100, 3), 100); |
| 118 | TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.GetCapturePoints(), [0, 2000 - 100, 0, 1000 + 100]); |
| 119 | }); |
| 120 | |
| 121 | // Tests reducing capture points (after a capture attack or a decay) |
| 122 | testCapturable( cmpCapturable => { |
| 123 | cmpCapturable.SetCapturePoints([0, 2000, 0 , 1000]); |
| 124 | cmpCapturable.CheckTimer(); |
| 125 | Engine.PostMessage = function(ent, iid, message) |
| 126 | { |
| 127 | if (iid == MT_CapturePointsChanged) |
| 128 | TS_ASSERT_UNEVAL_EQUALS(message, { "capturePoints": [0, 0, 0, 3000] }); |
| 129 | if (iid == MT_CaptureRegenStateChanged) |
| 130 | TS_ASSERT_UNEVAL_EQUALS(message, { "regenerating": true, "regenRate": 22, "territoryDecay": 0 }); |
| 131 | }; |
| 132 | cmpCapturable.Reduce(2500, 3); |
| 133 | TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.GetCapturePoints(), [0, 0, 0, 3000]); |
| 134 | }); |
| 135 | |
| 136 | // Tests regeneration |
| 137 | testCapturable( cmpCapturable => { |
| 138 | cmpCapturable.SetCapturePoints([12, 2950, 2 , 36]); |
| 139 | cmpCapturable.CheckTimer(); |
| 140 | |
| 141 | Engine.PostMessage = function(ent, iid, message) |
| 142 | { |
| 143 | if (iid == MT_CapturePointsChanged) |
| 144 | TS_ASSERT_UNEVAL_EQUALS(message, { "capturePoints": [1, 2972, 2, 25] }); |
| 145 | if (iid == MT_CaptureRegenStateChanged) |
| 146 | TS_ASSERT_UNEVAL_EQUALS(message, { "regenerating": true, "regenRate": 22, "territoryDecay": 0 }); |
| 147 | }; |
| 148 | cmpCapturable.TimerTick(); |
| 149 | TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.GetCapturePoints(), [1, 2972, 2, 25]); |
| 150 | |
| 151 | Engine.PostMessage = function(ent, iid, message) |
| 152 | { |
| 153 | if (iid == MT_CapturePointsChanged) |
| 154 | TS_ASSERT_UNEVAL_EQUALS(message, { "capturePoints": [0, 2994, 2, 4] }); |
| 155 | if (iid == MT_CaptureRegenStateChanged) |
| 156 | TS_ASSERT_UNEVAL_EQUALS(message, { "regenerating": true, "regenRate": 22, "territoryDecay": 0 }); |
| 157 | }; |
| 158 | cmpCapturable.TimerTick(); |
| 159 | TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.GetCapturePoints(), [0, 2994, 2, 4 ]); |
| 160 | |
| 161 | Engine.PostMessage = function(ent, iid, message) |
| 162 | { |
| 163 | if (iid == MT_CapturePointsChanged) |
| 164 | TS_ASSERT_UNEVAL_EQUALS(message, { "capturePoints": [0, 2998, 2, 0] }); |
| 165 | if (iid == MT_CaptureRegenStateChanged) |
| 166 | TS_ASSERT_UNEVAL_EQUALS(message, { "regenerating": true, "regenRate": 22, "territoryDecay": 0 }); |
| 167 | }; |
| 168 | cmpCapturable.TimerTick(); |
| 169 | TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.GetCapturePoints(), [0, 2998, 2, 0]); |
| 170 | |
| 171 | Engine.PostMessage = function(ent, iid, message) |
| 172 | { |
| 173 | if (iid == MT_CapturePointsChanged) |
| 174 | TS_ASSERT_UNEVAL_EQUALS(message, { "capturePoints": [0, 2998, 2, 0] }); |
| 175 | if (iid == MT_CaptureRegenStateChanged) |
| 176 | TS_ASSERT_UNEVAL_EQUALS(message, { "regenerating": false, "regenRate": 0, "territoryDecay": 0 }); |
| 177 | }; |
| 178 | cmpCapturable.TimerTick(); |
| 179 | TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.GetCapturePoints(), [0, 2998, 2, 0]); |
| 180 | }); |
| 181 | |
| 182 | // If the regeneration rate becomes negative, capture points are given in favour of gaia. |
| 183 | regenRate = -32; |
| 184 | testCapturable( cmpCapturable => { |
| 185 | cmpCapturable.SetCapturePoints([100, 2800, 50, 50]); |
| 186 | cmpCapturable.CheckTimer(); |
| 187 | TS_ASSERT_EQUALS(cmpCapturable.GetRegenRate(), -12); |
| 188 | |
| 189 | Engine.PostMessage = function(ent, iid, message) |
| 190 | { |
| 191 | if (iid == MT_CapturePointsChanged) |
| 192 | TS_ASSERT_UNEVAL_EQUALS(message, { "capturePoints": [112, 2796, 46, 46] }); |
| 193 | if (iid == MT_CaptureRegenStateChanged) |
| 194 | TS_ASSERT_UNEVAL_EQUALS(message, { "regenerating": true, "regenRate": -12, "territoryDecay": 0 }); |
| 195 | }; |
| 196 | cmpCapturable.TimerTick(); |
| 197 | TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.GetCapturePoints(), [112, 2796, 46, 46]); |
| 198 | }); |
| 199 | regenRate=2; |
| 200 | |
| 201 | // Tests decay |
| 202 | testCapturable( cmpCapturable => { |
| 203 | decay = true; |
| 204 | cmpCapturable.SetCapturePoints([2900, 35, 10, 55]); |
| 205 | cmpCapturable.CheckTimer(); |
| 206 | |
| 207 | let call = 1; |
| 208 | Engine.PostMessage = function(ent, iid, message) |
| 209 | { |
| 210 | if (iid == MT_CapturePointsChanged && call == 1) |
| 211 | { |
| 212 | TS_ASSERT_UNEVAL_EQUALS(message, { "capturePoints": [2912, 5, 22, 61] }); |
| 213 | ++call; |
| 214 | } |
| 215 | else if (iid == MT_CapturePointsChanged && call == 2) |
| 216 | TS_ASSERT_UNEVAL_EQUALS(message, { "capturePoints": [2901, 27, 22, 50] }); |
| 217 | else if (iid == MT_CaptureRegenStateChanged) |
| 218 | TS_ASSERT_UNEVAL_EQUALS(message, { "regenerating": true, "regenRate": 22 , "territoryDecay": 30 }); |
| 219 | }; |
| 220 | cmpCapturable.TimerTick(); |
| 221 | TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.GetCapturePoints(), [2901, 27, 22, 50]); |
| 222 | decay = false; |
| 223 | }); |
| 224 | |
| 225 | // Tests Reduce |
| 226 | function testReduce(amount, player, taken) |
| 227 | { |
| 228 | testCapturable(cmpCapturable => { |
| 229 | cmpCapturable.SetCapturePoints([0, 2000, 0 , 1000]); |
| 230 | cmpCapturable.CheckTimer(); |
| 231 | TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.Reduce(amount, player), taken); |
| 232 | }); |
| 233 | } |
| 234 | |
| 235 | testReduce(50, 3, 50); |
| 236 | testReduce(50, 2, 50); |
| 237 | testReduce(50, 1, 50); |
| 238 | testReduce(-50, 3, 0); |
| 239 | testReduce(50, 0, 50); |
| 240 | testReduce(0, 3, 0); |
| 241 | testReduce(1500, 3, 1500); |
| 242 | testReduce(2000, 3, 2000); |
| 243 | testReduce(3000, 3, 2000); |