Ticket #1985: restrict-and-prefer-building-attacks-1985.diff
File restrict-and-prefer-building-attacks-1985.diff, 3.6 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..8f45502
- + 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 if (index >= this._elements.length) 23 return; 24 25 this._totalWeight -= this._elements[index].weight; 26 27 for (var i = index; i < this._elements.length; i++) 28 this._elements[i] = this._elements[i + 1]; 29 30 this._elements.pop(); // discard last (now undefined) element 31 }; 32 33 WeightedList.prototype.itemAt = function(index) 34 { 35 var element = this._elements[index]; 36 return element ? element.item : null; 37 }; 38 39 WeightedList.prototype.randomIndex = function() { 40 var element, 41 targetWeight = Math.random() * this._totalWeight, 42 cumulativeWeight = 0; 43 for (var index = 0; index < this._elements.length; index++) 44 { 45 element = this._elements[index]; 46 cumulativeWeight += element.weight; 47 if (cumulativeWeight >= targetWeight) 48 return index; 49 } 50 return -1; 51 }; 52 53 Engine.RegisterGlobal("WeightedList", WeightedList);