Ticket #1985: restrict-and-prefer-building-attacks-1985_using_splice_no_underscores.diff
File restrict-and-prefer-building-attacks-1985_using_splice_no_underscores.diff, 3.4 KB (added by , 11 years ago) |
---|
-
binaries/data/mods/public/simulation/components/BuildingAI.js
diff --git a/binaries/data/mods/public/simulation/components/BuildingAI.js b/binaries/data/mods/public/simulation/components/BuildingAI.js index 8405880..dd50f9b 100644
a b BuildingAI.prototype.OnRangeUpdate = function(msg) 169 169 else if (msg.tag != this.enemyUnitsQuery) 170 170 return; 171 171 172 const restrictedClasses = cmpAttack.GetRestrictedClasses(attackType);173 174 172 if (msg.added.length > 0) 175 173 { 176 174 for each (var entity in msg.added) 177 175 { 178 var cmpIdentity = Engine.QueryInterface(entity, IID_Identity); 179 var targetClasses = cmpIdentity.GetClassesList(); 180 181 if (!targetClasses.some(function(c){return restrictedClasses.indexOf(c) > -1;})) 176 if (cmpAttack.CanAttack(entity)) 182 177 this.targetUnits.push(entity); 183 178 } 184 179 } … … BuildingAI.prototype.FireArrows = function() 285 280 this.arrowsLeft 286 281 ); 287 282 } 288 var clonedTargets = this.targetUnits.slice(); 283 if (arrowsToFire <= 0) 284 { 285 this.currentRound++; 286 return; 287 } 288 var targets = new WeightedList(); 289 for (var i = 0; i < this.targetUnits.length; i++) 290 { 291 var target = this.targetUnits[i], 292 weight = (cmpAttack.GetPreference(target) || 0) + 1 293 targets.add(target, weight); 294 } 289 295 for (var i = 0;i < arrowsToFire;i++) 290 296 { 291 var target = clonedTargets[Math.floor(Math.random() * this.targetUnits.length)]; 292 if (target && this.CheckTargetVisible(target)) 297 var selectedIndex = targets.randomIndex(), 298 selectedTarget = targets.itemAt(selectedIndex); 299 if (selectedTarget && this.CheckTargetVisible(selectedTarget)) 293 300 { 294 cmpAttack.PerformAttack(attackType, target);301 cmpAttack.PerformAttack(attackType, selectedTarget); 295 302 PlaySound("attack", this.entity); 296 303 } 297 304 else 298 305 { 299 clonedTargets.splice(clonedTargets.indexOf(target),1);306 targets.removeAt(selectedIndex); 300 307 i--; // one extra arrow left to fire 301 if( clonedTargets.length < 1)308 if(targets.length() < 1) 302 309 { 303 310 this.arrowsLeft += arrowsToFire; 304 311 // no targets found in this round, save arrows and go to next round -
new file inaries/data/mods/public/simulation/helpers/WeightedList.js
diff --git a/binaries/data/mods/public/simulation/helpers/WeightedList.js b/binaries/data/mods/public/simulation/helpers/WeightedList.js new file mode 100644 index 0000000..5a4d3e8
- + 1 var WeightedList = function() 2 { 3 this.elements = [ ]; 4 this.totalWeight = 0; 5 }; 6 7 WeightedList.prototype.length = function() 8 { 9 return this.elements.length; 10 }; 11 12 WeightedList.prototype.add = function(item, weight) 13 { 14 if (weight === undefined) 15 weight = 1; 16 this.totalWeight += weight; 17 this.elements.push({ "item": item, "weight": weight }); 18 }; 19 20 WeightedList.prototype.removeAt = function(index) 21 { 22 var element = this.elements.splice(index, 1); 23 if (element.length) 24 this.totalWeight -= element[0].weight; 25 }; 26 27 WeightedList.prototype.itemAt = function(index) 28 { 29 var element = this.elements[index]; 30 return element ? element.item : null; 31 }; 32 33 WeightedList.prototype.randomIndex = function() { 34 var element, 35 targetWeight = Math.random() * this.totalWeight, 36 cumulativeWeight = 0; 37 for (var index = 0; index < this.elements.length; index++) 38 { 39 element = this.elements[index]; 40 cumulativeWeight += element.weight; 41 if (cumulativeWeight >= targetWeight) 42 return index; 43 } 44 return -1; 45 }; 46 47 Engine.RegisterGlobal("WeightedList", WeightedList);