diff --git a/binaries/data/mods/public/simulation/components/GarrisonHolder.js b/binaries/data/mods/public/simulation/components/GarrisonHolder.js index b1078654af..260d4cbf01 100644 --- a/binaries/data/mods/public/simulation/components/GarrisonHolder.js +++ b/binaries/data/mods/public/simulation/components/GarrisonHolder.js @@ -53,6 +53,9 @@ GarrisonHolder.prototype.Schema = /** * Initialize GarrisonHolder Component + * + * Garrisoning when loading a map is set in the script of the map, by setting initGarrison + * which should contain the array of garrisoned entities */ GarrisonHolder.prototype.Init = function() { @@ -296,7 +299,6 @@ GarrisonHolder.prototype.PerformGarrison = function(entity) */ GarrisonHolder.prototype.Eject = function(entity, forced) { - var entityIndex = this.entities.indexOf(entity); // Error: invalid entity ID, usually it's already been ejected if (entityIndex == -1) @@ -632,6 +634,23 @@ GarrisonHolder.prototype.OnGlobalEntityRenamed = function(msg) this.Eject(msg.entity); this.Garrison(msg.newentity); } + + if (!this.initGarrison) + return; + + // update the pre-game garrison because of SkirmishReplacement + if (msg.entity == this.entity) + { + let cmpGarrisonHolder = Engine.QueryInterface(msg.newentity, IID_GarrisonHolder); + if (cmpGarrisonHolder) + cmpGarrisonHolder.initGarrison = this.initGarrison; + } + else + { + let entityIndex = this.initGarrison.indexOf(msg.entity); + if (entityIndex != -1) + this.initGarrison[entityIndex] = msg.newentity; + } }; @@ -697,5 +716,23 @@ GarrisonHolder.prototype.IsEjectable = function(entity) return false; }; +/** + * Initialise the garrisoned units + */ +GarrisonHolder.prototype.OnGlobalInitGame = function(msg) +{ + if (!this.initGarrison) + return; + + for (let ent of this.initGarrison) + { + let cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI); + if (!cmpUnitAI || !cmpUnitAI.CanGarrison(this.entity)) + continue; + this.Garrison(ent); + } + this.initGarrison = undefined; +}; + Engine.RegisterComponentType(IID_GarrisonHolder, "GarrisonHolder", GarrisonHolder); diff --git a/binaries/data/mods/public/simulation/components/interfaces/Messages.js b/binaries/data/mods/public/simulation/components/interfaces/Messages.js index 3fddeb934a..c38450f80c 100644 --- a/binaries/data/mods/public/simulation/components/interfaces/Messages.js +++ b/binaries/data/mods/public/simulation/components/interfaces/Messages.js @@ -13,3 +13,6 @@ Engine.RegisterMessageType("DiplomacyChanged"); // Message of the form { "to": receiver, "from": sender, "amounts": amounts }. // sent whenever a tribute is sent Engine.RegisterMessageType("TributeExchanged"); + +// Message sent by InitGame for component map-dependent initialization +Engine.RegisterMessageType("InitGame"); diff --git a/binaries/data/mods/public/simulation/helpers/InitGame.js b/binaries/data/mods/public/simulation/helpers/InitGame.js index 9f984b172d..a88f0b7af1 100644 --- a/binaries/data/mods/public/simulation/helpers/InitGame.js +++ b/binaries/data/mods/public/simulation/helpers/InitGame.js @@ -27,6 +27,9 @@ function PreInitGame() function InitGame(settings) { + // Map dependent initialisations of components (i.e. garrisoned units) + Engine.BroadcastMessage(MT_InitGame, {}); + // No settings when loading a map in Atlas, so do nothing if (!settings) return; diff --git a/binaries/data/mods/public/simulation/templates/skirmish/units/default_ship_trireme.xml b/binaries/data/mods/public/simulation/templates/skirmish/units/default_ship_trireme.xml new file mode 100644 index 0000000000..5855d9e204 --- /dev/null +++ b/binaries/data/mods/public/simulation/templates/skirmish/units/default_ship_trireme.xml @@ -0,0 +1,12 @@ + + + + skirm + + + units/{civ}_ship_trireme + + + structures/athenians/trireme.xml + +