5419 | | return this.RespondToTargetedEntities( |
5420 | | ents.filter(function (v, i, a) { return cmpAttack.CanAttack(v) && animalfilter(v); }) |
5421 | | .sort(function (a, b) { return cmpAttack.CompareEntitiesByPreference(a, b); }) |
5422 | | ); |
| 5419 | // Use filter() to remove non-dangerous animals from ents. |
| 5420 | var filteredEnts = ents.filter(function (v, i, a) { return cmpAttack.CanAttack(v) && filter(v); }); |
| 5421 | |
| 5422 | // If it was only non-dangerous animals, do nothing. |
| 5423 | if (!filteredEnts.length) |
| 5424 | return false; |
| 5425 | |
| 5426 | while (filteredEnts.length) |
| 5427 | { |
| 5428 | // Otherwise prefer unit for best attack bonus. |
| 5429 | var preferredEntity = this.GetAttackBonusPreference(filteredEnts); |
| 5430 | if (this.RespondToTargetedEntities([preferredEntity])) |
| 5431 | return true; |
| 5432 | else |
| 5433 | filteredEnts = filteredEnts.filter(function (v, i, a) { return v != preferredEntity; }); |
| 5434 | } |
| 5435 | |
| 5436 | return false; |
| 5437 | |
| 5440 | UnitAI.prototype.GetAttackBonusPreference = function(ents) |
| 5441 | { |
| 5442 | var cmpAttack = Engine.QueryInterface(this.entity, IID_Attack); |
| 5443 | |
| 5444 | if (!cmpAttack || !ents.length) |
| 5445 | return false; |
| 5446 | |
| 5447 | var preferredUnit = ents[0]; |
| 5448 | var bestBonus = 0; |
| 5449 | var bestPref = 0; |
| 5450 | |
| 5451 | for each (var target in ents) |
| 5452 | { |
| 5453 | var thisBonus = cmpAttack.GetAttackBonus(cmpAttack.GetBestAttackAgainst(target),target); |
| 5454 | var thisPref = cmpAttack.GetPreference(target); |
| 5455 | |
| 5456 | if (thisPref) |
| 5457 | { |
| 5458 | if ( thisPref > bestPref ) |
| 5459 | { |
| 5460 | preferredUnit = target; |
| 5461 | bestPref = thisPref; |
| 5462 | } |
| 5463 | else if (thisPref == bestPref && thisBonus > bestBonus) |
| 5464 | { |
| 5465 | preferredUnit = target; |
| 5466 | bestBonus = thisBonus; |
| 5467 | } |
| 5468 | } else if (bestPref == 0) |
| 5469 | { |
| 5470 | if ( thisBonus > bestBonus ) |
| 5471 | { |
| 5472 | preferredUnit = target; |
| 5473 | bestBonus = thisBonus; |
| 5474 | } |
| 5475 | } |
| 5476 | } |
| 5477 | |
| 5478 | return preferredUnit; |
| 5479 | }; |
| 5480 | |