From 092206b12e5083802b609ee49396fce60cbd03cc Mon Sep 17 00:00:00 2001 From: mimo Date: Fri, 13 Nov 2015 17:47:31 +0000 Subject: [PATCH] Finding new targets can be really slow when lot of units are involved. In Combat demo huge for example, it could represent more than 30% of the simulation time. Part of it is because we loop several times on all identity classes of all enemy units, and most of the time for nothing. This patch avoids the useless loops, and adds some cleanup. In addition, when testing if we can't attack a units, Capture and Slaughter were used even if not applicable. This was SVN commit r17250. --- .../public/simulation/components/Attack.js | 36 +++++++++++-------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/binaries/data/mods/public/simulation/components/Attack.js b/binaries/data/mods/public/simulation/components/Attack.js index 962d27225d..db2efee006 100644 --- a/binaries/data/mods/public/simulation/components/Attack.js +++ b/binaries/data/mods/public/simulation/components/Attack.js @@ -258,26 +258,30 @@ Attack.prototype.CanAttack = function(target) const targetClasses = cmpIdentity.GetClassesList(); - for each (var type in this.GetAttackTypes()) + for (let type of this.GetAttackTypes()) { + if (type == "Capture" && !QueryMiragedInterface(target, IID_Capturable)) + continue; + if (type == "Slaughter" && targetClasses.indexOf("Domestic") == -1) + continue; + if (heightDiff > this.GetRange(type).max) continue; - var canAttack = true; - var restrictedClasses = this.GetRestrictedClasses(type); + let restrictedClasses = this.GetRestrictedClasses(type); + if (!restrictedClasses.length) + return true; - for each (var targetClass in targetClasses) + let canAttack = true; + for (let targetClass of targetClasses) { - if (restrictedClasses.indexOf(targetClass) != -1) - { - canAttack = false; - break; - } + if (restrictedClasses.indexOf(targetClass) == -1) + continue; + canAttack = false; + break; } if (canAttack) - { return true; - } } return false; @@ -295,12 +299,14 @@ Attack.prototype.GetPreference = function(target) const targetClasses = cmpIdentity.GetClassesList(); var minPref = null; - for (var type of this.GetAttackTypes()) + for (let type of this.GetAttackTypes()) { - var preferredClasses = this.GetPreferredClasses(type); - for (var targetClass of targetClasses) + let preferredClasses = this.GetPreferredClasses(type); + for (let targetClass of targetClasses) { - var pref = preferredClasses.indexOf(targetClass); + let pref = preferredClasses.indexOf(targetClass); + if (pref === 0) + return pref; if (pref != -1 && (minPref === null || minPref > pref)) minPref = pref; }