From f4c9305eee9e60f97d631ab148f85836c1bbc1fc Mon Sep 17 00:00:00 2001 From: Freagarach Date: Sun, 28 Feb 2021 06:37:05 +0000 Subject: [PATCH] Move some functionality from GarrisonHolder to Garrisonable. Take some responsibility from GarrisonHolder to where it belongs (Garrisonable). Differential revision: D3223 Comments by: @Stan, @wraitii This was SVN commit r24957. --- .../simulation/components/GarrisonHolder.js | 73 ++++++------------- .../simulation/components/Garrisonable.js | 50 ++++++++++++- .../components/tests/test_GarrisonHolder.js | 9 +-- .../components/tests/test_Garrisonable.js | 3 + 4 files changed, 77 insertions(+), 58 deletions(-) diff --git a/binaries/data/mods/public/simulation/components/GarrisonHolder.js b/binaries/data/mods/public/simulation/components/GarrisonHolder.js index c356881ddf..cb290e48a9 100644 --- a/binaries/data/mods/public/simulation/components/GarrisonHolder.js +++ b/binaries/data/mods/public/simulation/components/GarrisonHolder.js @@ -179,17 +179,6 @@ GarrisonHolder.prototype.Garrison = function(entity, renamed = false) this.entities.push(entity); this.UpdateGarrisonFlag(); - let cmpProductionQueue = Engine.QueryInterface(entity, IID_ProductionQueue); - if (cmpProductionQueue) - cmpProductionQueue.PauseProduction(); - - let cmpAura = Engine.QueryInterface(entity, IID_Auras); - if (cmpAura && cmpAura.HasGarrisonAura()) - cmpAura.ApplyGarrisonAura(this.entity); - - let cmpPosition = Engine.QueryInterface(entity, IID_Position); - if (cmpPosition) - cmpPosition.MoveOutOfWorld(); Engine.PostMessage(this.entity, MT_GarrisonedUnitsChanged, { "added": [entity], @@ -215,7 +204,27 @@ GarrisonHolder.prototype.Eject = function(entity, forced, renamed = false) if (entityIndex == -1) return false; - // Find spawning location + let cmpGarrisonable = Engine.QueryInterface(entity, IID_Garrisonable); + if (!cmpGarrisonable || !cmpGarrisonable.UnGarrison(forced)) + return false; + + this.entities.splice(entityIndex, 1); + Engine.PostMessage(this.entity, MT_GarrisonedUnitsChanged, { + "added": [], + "removed": [entity], + "renamed": renamed + }); + + return true; +}; + +/** + * @param {number} entity - EntityID to find the spawn position for. + * @param {boolean} forced - Optionally whether the spawning is forced. + * @return {Vector3D} - An appropriate spawning position. + */ +GarrisonHolder.prototype.GetSpawnPosition = function(entity, forced) +{ let cmpFootprint = Engine.QueryInterface(this.entity, IID_Footprint); let cmpHealth = Engine.QueryInterface(this.entity, IID_Health); let cmpIdentity = Engine.QueryInterface(this.entity, IID_Identity); @@ -232,49 +241,13 @@ GarrisonHolder.prototype.Eject = function(entity, forced, renamed = false) { // Error: couldn't find space satisfying the unit's passability criteria if (!forced) - return false; + return null; // If ejection is forced, we need to continue, so use center of the building let cmpPosition = Engine.QueryInterface(this.entity, IID_Position); pos = cmpPosition.GetPosition(); } - - this.entities.splice(entityIndex, 1); - - let cmpEntUnitAI = Engine.QueryInterface(entity, IID_UnitAI); - if (cmpEntUnitAI) - cmpEntUnitAI.Ungarrison(); - - let cmpEntProductionQueue = Engine.QueryInterface(entity, IID_ProductionQueue); - if (cmpEntProductionQueue) - cmpEntProductionQueue.UnpauseProduction(); - - let cmpEntAura = Engine.QueryInterface(entity, IID_Auras); - if (cmpEntAura && cmpEntAura.HasGarrisonAura()) - cmpEntAura.RemoveGarrisonAura(this.entity); - - let cmpEntPosition = Engine.QueryInterface(entity, IID_Position); - if (cmpEntPosition) - { - cmpEntPosition.JumpTo(pos.x, pos.z); - cmpEntPosition.SetHeightOffset(0); - - let cmpPosition = Engine.QueryInterface(this.entity, IID_Position); - if (cmpPosition) - cmpEntPosition.SetYRotation(cmpPosition.GetPosition().horizAngleTo(pos)); - } - - let cmpGarrisonable = Engine.QueryInterface(entity, IID_Garrisonable); - if (cmpGarrisonable) - cmpGarrisonable.UnGarrison(); - - Engine.PostMessage(this.entity, MT_GarrisonedUnitsChanged, { - "added": [], - "removed": [entity], - "renamed": renamed - }); - - return true; + return pos; }; /** diff --git a/binaries/data/mods/public/simulation/components/Garrisonable.js b/binaries/data/mods/public/simulation/components/Garrisonable.js index 4961a15040..9ea64cb304 100644 --- a/binaries/data/mods/public/simulation/components/Garrisonable.js +++ b/binaries/data/mods/public/simulation/components/Garrisonable.js @@ -24,15 +24,61 @@ Garrisonable.prototype.Garrison = function(entity) return false; this.holder = entity; + + let cmpProductionQueue = Engine.QueryInterface(this.entity, IID_ProductionQueue); + if (cmpProductionQueue) + cmpProductionQueue.PauseProduction(); + + let cmpAura = Engine.QueryInterface(this.entity, IID_Auras); + if (cmpAura && cmpAura.HasGarrisonAura()) + cmpAura.ApplyGarrisonAura(entity); + + let cmpPosition = Engine.QueryInterface(this.entity, IID_Position); + if (cmpPosition) + cmpPosition.MoveOutOfWorld(); + return true; }; /** - * Resets the garrisonHolder. + * @param {boolean} forced - Optionally whether the spawning is forced. + * @return {boolean} - Whether the ungarrisoning succeeded. */ -Garrisonable.prototype.UnGarrison = function() +Garrisonable.prototype.UnGarrison = function(forced) { + let cmpPosition = Engine.QueryInterface(this.entity, IID_Position); + if (cmpPosition) + { + let pos; + let cmpGarrisonHolder = Engine.QueryInterface(this.holder, IID_GarrisonHolder); + if (cmpGarrisonHolder) + pos = cmpGarrisonHolder.GetSpawnPosition(this.entity, forced); + + if (!pos) + return false; + + cmpPosition.JumpTo(pos.x, pos.z); + cmpPosition.SetHeightOffset(0); + + let cmpHolderPosition = Engine.QueryInterface(this.holder, IID_Position); + if (cmpHolderPosition) + cmpPosition.SetYRotation(cmpHolderPosition.GetPosition().horizAngleTo(pos)); + } + + let cmpUnitAI = Engine.QueryInterface(this.entity, IID_UnitAI); + if (cmpUnitAI) + cmpUnitAI.Ungarrison(); + + let cmpProductionQueue = Engine.QueryInterface(this.entity, IID_ProductionQueue); + if (cmpProductionQueue) + cmpProductionQueue.UnpauseProduction(); + + let cmpAura = Engine.QueryInterface(this.entity, IID_Auras); + if (cmpAura && cmpAura.HasGarrisonAura()) + cmpAura.RemoveGarrisonAura(this.holder); + delete this.holder; + return true; }; Engine.RegisterComponentType(IID_Garrisonable, "Garrisonable", Garrisonable); diff --git a/binaries/data/mods/public/simulation/components/tests/test_GarrisonHolder.js b/binaries/data/mods/public/simulation/components/tests/test_GarrisonHolder.js index 0c45789fb7..12d9508bec 100644 --- a/binaries/data/mods/public/simulation/components/tests/test_GarrisonHolder.js +++ b/binaries/data/mods/public/simulation/components/tests/test_GarrisonHolder.js @@ -4,12 +4,9 @@ Engine.LoadComponentScript("interfaces/GarrisonHolder.js"); Engine.LoadComponentScript("interfaces/Garrisonable.js"); Engine.LoadComponentScript("interfaces/TurretHolder.js"); Engine.LoadComponentScript("GarrisonHolder.js"); -Engine.LoadComponentScript("interfaces/Auras.js"); Engine.LoadComponentScript("interfaces/Health.js"); -Engine.LoadComponentScript("interfaces/ProductionQueue.js"); Engine.LoadComponentScript("interfaces/ModifiersManager.js"); Engine.LoadComponentScript("interfaces/Timer.js"); -Engine.LoadComponentScript("interfaces/UnitAI.js"); const garrisonedEntitiesList = [25, 26, 27, 28, 29, 30, 31, 32, 33]; const garrisonHolderId = 15; @@ -70,7 +67,7 @@ for (let i = 24; i <= 34; ++i) AddMock(i, IID_Garrisonable, { "Garrison": entity => true, - "UnGarrison": () => {} + "UnGarrison": () => true }); AddMock(i, IID_Position, { @@ -217,7 +214,7 @@ AddMock(siegeEngineId, IID_Ownership, { }); AddMock(siegeEngineId, IID_Garrisonable, { "Garrison": entity => true, - "UnGarrison": () => {} + "UnGarrison": () => true }); let cavalryId = 46; AddMock(cavalryId, IID_Identity, { @@ -238,7 +235,7 @@ AddMock(cavalryId, IID_Ownership, { }); AddMock(cavalryId, IID_Garrisonable, { "Garrison": entity => true, - "UnGarrison": () => {} + "UnGarrison": () => true }); TS_ASSERT(cmpGarrisonHolder.Garrison(siegeEngineId)); TS_ASSERT_EQUALS(cmpGarrisonHolder.GetGarrisonedEntitiesCount(), 1); diff --git a/binaries/data/mods/public/simulation/components/tests/test_Garrisonable.js b/binaries/data/mods/public/simulation/components/tests/test_Garrisonable.js index 597d00ec48..c5d94ab2c0 100644 --- a/binaries/data/mods/public/simulation/components/tests/test_Garrisonable.js +++ b/binaries/data/mods/public/simulation/components/tests/test_Garrisonable.js @@ -1,4 +1,7 @@ +Engine.LoadComponentScript("interfaces/Auras.js"); Engine.LoadComponentScript("interfaces/Garrisonable.js"); +Engine.LoadComponentScript("interfaces/ProductionQueue.js"); +Engine.LoadComponentScript("interfaces/UnitAI.js"); Engine.LoadComponentScript("Garrisonable.js"); const garrisonHolderID = 1;