Index: binaries/data/mods/public/simulation/components/ProductionQueue.js =================================================================== --- binaries/data/mods/public/simulation/components/ProductionQueue.js +++ binaries/data/mods/public/simulation/components/ProductionQueue.js @@ -303,8 +303,11 @@ // Update entity count in the EntityLimits component if (template.TrainingRestrictions) { - var unitCategory = template.TrainingRestrictions.Category; - var cmpPlayerEntityLimits = QueryOwnerInterface(this.entity, IID_EntityLimits); + let unitCategory = template.TrainingRestrictions.Category; + let cmpPlayerEntityLimits = QueryOwnerInterface(this.entity, IID_EntityLimits); + // do not train more units than can + if (cmpPlayerEntityLimits.GetCounts()[unitCategory] + count > cmpPlayerEntityLimits.GetLimits()[unitCategory]) + return; cmpPlayerEntityLimits.ChangeCount(unitCategory, count); } @@ -572,17 +575,6 @@ { var ent = Engine.AddEntity(templateName); this.entityCache.push(ent); - - // Decrement entity count in the EntityLimits component - // since it will be increased by EntityLimits.OnGlobalOwnershipChanged function, - // i.e. we replace a 'trained' entity to an 'alive' one - var cmpTrainingRestrictions = Engine.QueryInterface(ent, IID_TrainingRestrictions); - if (cmpTrainingRestrictions) - { - var unitCategory = cmpTrainingRestrictions.GetCategory(); - var cmpPlayerEntityLimits = QueryOwnerInterface(this.entity, IID_EntityLimits); - cmpPlayerEntityLimits.ChangeCount(unitCategory, -1); - } } } @@ -619,7 +611,7 @@ let pos = cmpFootprint.PickSpawnPoint(ent); if (pos.y < 0) break; - + let cmpNewPosition = Engine.QueryInterface(ent, IID_Position); cmpNewPosition.JumpTo(pos.x, pos.z); @@ -630,6 +622,19 @@ spawnedEnts.push(ent); } + // Decrement entity count in the EntityLimits component + // since it will be increased by EntityLimits.OnGlobalOwnershipChanged function, + // i.e. we replace a 'trained' entity to an 'alive' one + // Do not move it before spawn check + + let cmpTrainingRestrictions = Engine.QueryInterface(ent, IID_TrainingRestrictions); + if (cmpTrainingRestrictions) + { + let unitCategory = cmpTrainingRestrictions.GetCategory(); + let cmpPlayerEntityLimits = QueryOwnerInterface(this.entity, IID_EntityLimits); + cmpPlayerEntityLimits.ChangeCount(unitCategory, -1); + } + cmpNewOwnership.SetOwner(cmpOwnership.GetOwner()); let cmpPlayerStatisticsTracker = QueryOwnerInterface(this.entity, IID_StatisticsTracker);