Ticket #4121: foundation_xz_fix_v1.patch
File foundation_xz_fix_v1.patch, 10.4 KB (added by , 8 years ago) |
---|
-
binaries/data/mods/public/simulation/components/Foundation.js
251 251 } 252 252 253 253 var cmpFoundationPosition = Engine.QueryInterface(this.entity, IID_Position); 254 var pos = cmpFoundationPosition.GetPosition ();254 var pos = cmpFoundationPosition.GetPosition2D(); 255 255 var rot = cmpFoundationPosition.GetRotation(); 256 256 cmpPreviewPosition.SetYRotation(rot.y); 257 257 cmpPreviewPosition.SetXZRotation(rot.x, rot.z); 258 cmpPreviewPosition.JumpTo(pos.x, pos. z);258 cmpPreviewPosition.JumpTo(pos.x, pos.y); 259 259 } 260 260 261 261 this.committed = true; … … 333 333 if (cmpTerritoryDecay && cmpTerritoryDecay.HasTerritoryOwnership()) 334 334 { 335 335 let cmpTerritoryManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TerritoryManager); 336 owner = cmpTerritoryManager.GetOwner(pos.x, pos. z);336 owner = cmpTerritoryManager.GetOwner(pos.x, pos.y); 337 337 } 338 338 else 339 339 { -
binaries/data/mods/public/simulation/components/tests/test_Foundation.js
8 8 Engine.LoadComponentScript("interfaces/Trigger.js"); 9 9 Engine.LoadComponentScript("Foundation.js"); 10 10 11 AddMock(SYSTEM_ENTITY, IID_Trigger, {12 "CallEvent": () => {},13 });14 15 11 let player = 1; 16 12 let playerEnt = 3; 13 let foundationEnt = 20; 14 let previewEnt = 21; 15 let newEnt = 22; 17 16 18 AddMock(SYSTEM_ENTITY, IID_PlayerManager, { 19 "GetPlayerByID": () => playerEnt, 20 });17 function testFoundation(...mocks) 18 { 19 ResetState(); 21 20 22 AddMock(playerEnt, IID_StatisticsTracker, { 23 "IncreaseConstructedBuildingsCounter": ent => { 24 TS_ASSERT_EQUALS(ent, newEnt); 25 }, 26 }); 21 AddMock(SYSTEM_ENTITY, IID_Trigger, { 22 "CallEvent": () => {}, 23 }); 27 24 28 let foundationEnt = 20; 29 let newEnt = 21; 30 let finalTemplate = "structures/athen_civil_centre.xml"; 31 let foundationHP = 1; 32 let maxHP = 100; 25 AddMock(SYSTEM_ENTITY, IID_PlayerManager, { 26 "GetPlayerByID": () => playerEnt, 27 }); 33 28 34 AddMock(foundationEnt, IID_Cost, { 35 "GetBuildTime": () => 50, 36 "GetResourceCosts": () => ({ "wood": 100 }), 37 }); 29 AddMock(SYSTEM_ENTITY, IID_TerritoryManager, { 30 "GetOwner": (x, y) => { 31 TS_ASSERT_EQUALS(x, pos.x); 32 TS_ASSERT_EQUALS(y, pos.y); 33 return player; 34 }, 35 }); 38 36 39 AddMock(foundationEnt, IID_Health, { 40 "GetHitpoints": () => foundationHP, 41 "GetMaxHitpoints": () => maxHP, 42 "Increase": hp => { 43 foundationHP = Math.min(foundationHP + hp, maxHP); 44 cmpFoundation.OnHealthChanged(); 45 }, 46 }); 37 function PlaySound(name, source) 38 { 39 TS_ASSERT_EQUALS(name, "constructed"); 40 TS_ASSERT_EQUALS(source, newEnt); 41 }; 42 Engine.RegisterGlobal("PlaySound", PlaySound); 43 Engine.RegisterGlobal("MT_EntityRenamed", "entityRenamed"); 47 44 48 AddMock(foundationEnt, IID_Obstruction, { 49 "GetBlockMovementFlag": () => true,50 "GetUnitCollisions": () => [],51 "SetDisableBlockMovementPathfinding": () => {},52 });45 let finalTemplate = "structures/athen_civil_centre.xml"; 46 let foundationHP = 1; 47 let maxHP = 100; 48 let rot = new Vector3D(1, 2, 3); 49 let pos = new Vector2D(4, 5); 53 50 54 AddMock(foundationEnt, IID_Ownership, { 55 "GetOwner": () => player, 56 }); 51 AddMock(foundationEnt, IID_Cost, { 52 "GetBuildTime": () => 50, 53 "GetResourceCosts": () => ({ "wood": 100 }), 54 }); 57 55 58 AddMock(foundationEnt, IID_Position, { 59 "GetPosition2D": () => new Vector2D(1, 2), 60 "GetRotation": () => new Vector3D(1, 2, 3), 61 "IsInWorld": () => true, 62 }); 56 AddMock(foundationEnt, IID_Health, { 57 "GetHitpoints": () => foundationHP, 58 "GetMaxHitpoints": () => maxHP, 59 "Increase": hp => { 60 foundationHP = Math.min(foundationHP + hp, maxHP); 61 cmpFoundation.OnHealthChanged(); 62 }, 63 }); 63 64 64 Engine.AddEntity = function(template) 65 { 66 AddMock(newEnt, IID_Position, { 67 "JumpTo": (x, y) => { TS_ASSERT_EQUALS(x, 1); TS_ASSERT_EQUALS(y, 2); }, 68 "SetYRotation": r => { TS_ASSERT_EQUALS(r, 2); }, 65 AddMock(foundationEnt, IID_Obstruction, { 66 "GetBlockMovementFlag": () => true, 67 "GetUnitCollisions": () => [], 68 "SetDisableBlockMovementPathfinding": () => {}, 69 }); 70 71 AddMock(foundationEnt, IID_Ownership, { 72 "GetOwner": () => player, 73 }); 74 75 AddMock(foundationEnt, IID_Position, { 76 "GetPosition2D": () => pos, 77 "GetRotation": () => rot, 78 "SetConstructionProgress": () => {}, 79 "IsInWorld": () => true, 80 }); 81 82 AddMock(previewEnt, IID_Ownership, { 83 "SetOwner": owner => { TS_ASSERT_EQUALS(owner, player); }, 84 }); 85 86 AddMock(previewEnt, IID_Position, { 87 "JumpTo": (x, y) => { TS_ASSERT_EQUALS(x, pos.x); TS_ASSERT_EQUALS(y, pos.y); }, 88 "SetConstructionProgress": p => {}, 89 "SetYRotation": r => { TS_ASSERT_EQUALS(r, rot.y); }, 69 90 "SetXZRotation": (rx, rz) => { 70 TS_ASSERT_EQUALS(rx, 1);71 TS_ASSERT_EQUALS(rz, 3);91 TS_ASSERT_EQUALS(rx, rot.x); 92 TS_ASSERT_EQUALS(rz, rot.z); 72 93 }, 73 94 }); 95 74 96 AddMock(newEnt, IID_Ownership, { 75 97 "SetOwner": owner => { TS_ASSERT_EQUALS(owner, player); }, 76 98 }); 77 return newEnt;78 };79 99 80 function PlaySound(name, source) 81 { 82 TS_ASSERT_EQUALS(name, "constructed"); 83 TS_ASSERT_EQUALS(source, newEnt); 84 }; 85 Engine.RegisterGlobal("PlaySound", PlaySound); 86 Engine.RegisterGlobal("MT_EntityRenamed", "entityRenamed"); 100 AddMock(newEnt, IID_Position, { 101 "JumpTo": (x, y) => { TS_ASSERT_EQUALS(x, pos.x); TS_ASSERT_EQUALS(y, pos.y); }, 102 "SetYRotation": r => { TS_ASSERT_EQUALS(r, rot.y); }, 103 "SetXZRotation": (rx, rz) => { 104 TS_ASSERT_EQUALS(rx, rot.x); 105 TS_ASSERT_EQUALS(rz, rot.z); 106 }, 107 }); 87 108 88 let cmpFoundation = ConstructComponent(foundationEnt, "Foundation", {}); 109 // Add custom mocks 110 for (let mock of mocks) 111 AddMock(...mock); 89 112 90 // INITIALISE 91 cmpFoundation.InitialiseConstruction(player, finalTemplate); 113 // INITIALISE 114 Engine.AddEntity = function(template) { 115 TS_ASSERT_EQUALS(template, "construction|" + finalTemplate); 116 return previewEnt; 117 }; 118 let cmpFoundation = ConstructComponent(foundationEnt, "Foundation", {}); 119 cmpFoundation.InitialiseConstruction(player, finalTemplate); 92 120 93 TS_ASSERT_EQUALS(cmpFoundation.owner, player);94 TS_ASSERT_EQUALS(cmpFoundation.finalTemplateName, finalTemplate);95 TS_ASSERT_EQUALS(cmpFoundation.maxProgress, 0);96 TS_ASSERT_EQUALS(cmpFoundation.initialised, true);121 TS_ASSERT_EQUALS(cmpFoundation.owner, player); 122 TS_ASSERT_EQUALS(cmpFoundation.finalTemplateName, finalTemplate); 123 TS_ASSERT_EQUALS(cmpFoundation.maxProgress, 0); 124 TS_ASSERT_EQUALS(cmpFoundation.initialised, true); 97 125 98 // BUILDER COUNT 99 let twoBuilderMultiplier = Math.pow(2, cmpFoundation.buildTimePenalty) / 2; 100 TS_ASSERT_EQUALS(cmpFoundation.GetNumBuilders(), 0); 101 cmpFoundation.AddBuilder(10); 102 TS_ASSERT_EQUALS(cmpFoundation.GetNumBuilders(), 1); 103 TS_ASSERT_EQUALS(cmpFoundation.buildMultiplier, 1); 104 cmpFoundation.AddBuilder(11); 105 TS_ASSERT_EQUALS(cmpFoundation.GetNumBuilders(), 2); 106 TS_ASSERT_EQUALS(cmpFoundation.buildMultiplier, twoBuilderMultiplier); 107 cmpFoundation.AddBuilder(11); 108 TS_ASSERT_EQUALS(cmpFoundation.GetNumBuilders(), 2); 109 TS_ASSERT_EQUALS(cmpFoundation.buildMultiplier, twoBuilderMultiplier); 110 cmpFoundation.RemoveBuilder(11); 111 TS_ASSERT_EQUALS(cmpFoundation.GetNumBuilders(), 1); 112 TS_ASSERT_EQUALS(cmpFoundation.buildMultiplier, 1); 113 cmpFoundation.RemoveBuilder(11); 114 TS_ASSERT_EQUALS(cmpFoundation.GetNumBuilders(), 1); 115 TS_ASSERT_EQUALS(cmpFoundation.buildMultiplier, 1); 116 // with cmpVisual 117 AddMock(foundationEnt, IID_Visual, { 126 // BUILDER COUNT 127 let twoBuilderMultiplier = Math.pow(2, cmpFoundation.buildTimePenalty) / 2; 128 TS_ASSERT_EQUALS(cmpFoundation.GetNumBuilders(), 0); 129 cmpFoundation.AddBuilder(10); 130 TS_ASSERT_EQUALS(cmpFoundation.GetNumBuilders(), 1); 131 TS_ASSERT_EQUALS(cmpFoundation.buildMultiplier, 1); 132 cmpFoundation.AddBuilder(11); 133 TS_ASSERT_EQUALS(cmpFoundation.GetNumBuilders(), 2); 134 TS_ASSERT_EQUALS(cmpFoundation.buildMultiplier, twoBuilderMultiplier); 135 cmpFoundation.AddBuilder(11); 136 TS_ASSERT_EQUALS(cmpFoundation.GetNumBuilders(), 2); 137 TS_ASSERT_EQUALS(cmpFoundation.buildMultiplier, twoBuilderMultiplier); 138 cmpFoundation.RemoveBuilder(11); 139 TS_ASSERT_EQUALS(cmpFoundation.GetNumBuilders(), 1); 140 TS_ASSERT_EQUALS(cmpFoundation.buildMultiplier, 1); 141 cmpFoundation.RemoveBuilder(11); 142 TS_ASSERT_EQUALS(cmpFoundation.GetNumBuilders(), 1); 143 TS_ASSERT_EQUALS(cmpFoundation.buildMultiplier, 1); 144 145 // COMMIT FOUNDATION 146 TS_ASSERT_EQUALS(cmpFoundation.committed, false); 147 let work = 5; 148 cmpFoundation.Build(10, work); 149 TS_ASSERT_EQUALS(cmpFoundation.committed, true); 150 TS_ASSERT_EQUALS(foundationHP, 1 + work * cmpFoundation.GetBuildRate() * cmpFoundation.buildMultiplier); 151 TS_ASSERT_EQUALS(cmpFoundation.maxProgress, foundationHP / maxHP); 152 153 // FINISH CONSTRUCTION 154 Engine.AddEntity = function(template) 155 { 156 TS_ASSERT_EQUALS(template, finalTemplate); 157 return newEnt; 158 }; 159 cmpFoundation.Build(10, 1000); 160 TS_ASSERT_EQUALS(cmpFoundation.maxProgress, 1); 161 TS_ASSERT_EQUALS(foundationHP, maxHP); 162 } 163 164 testFoundation(); 165 166 testFoundation([foundationEnt, IID_Visual, { 118 167 "SetVariable": (key, num) => { 119 168 TS_ASSERT_EQUALS(key, "numbuilders"); 120 TS_ASSERT _EQUALS(num,2);169 TS_ASSERT(num == 1 || num == 2); 121 170 }, 122 }); 123 cmpFoundation.AddBuilder(11); 124 DeleteMock(foundationEnt, IID_Visual); 125 cmpFoundation.RemoveBuilder(11); 171 "SelectAnimation": () => {}, 172 "HasConstructionPreview": () => true, 173 }]); 126 174 127 // COMMIT FOUNDATION 128 TS_ASSERT_EQUALS(cmpFoundation.committed, false); 129 let work = 5; 130 cmpFoundation.Build(10, work); 131 TS_ASSERT_EQUALS(cmpFoundation.committed, true); 132 TS_ASSERT_EQUALS(foundationHP, 1 + work * cmpFoundation.GetBuildRate() * cmpFoundation.buildMultiplier); 133 TS_ASSERT_EQUALS(cmpFoundation.maxProgress, foundationHP / maxHP); 175 testFoundation([newEnt, IID_TerritoryDecay, { 176 "HasTerritoryOwnership": () => true, 177 }]); 134 178 135 // FINISH CONSTRUCTION 136 cmpFoundation.Build(10, 1000); 137 TS_ASSERT_EQUALS(cmpFoundation.maxProgress, 1); 138 TS_ASSERT_EQUALS(foundationHP, maxHP); 179 testFoundation([playerEnt, IID_StatisticsTracker, { 180 "IncreaseConstructedBuildingsCounter": ent => { 181 TS_ASSERT_EQUALS(ent, newEnt); 182 }, 183 }]); 139 184