Line data Source code
1 : function FormationAttack() {} 2 : 3 0 : FormationAttack.prototype.Schema = 4 : "<element name='CanAttackAsFormation'>" + 5 : "<text/>" + 6 : "</element>"; 7 : 8 0 : FormationAttack.prototype.Init = function() 9 : { 10 0 : this.canAttackAsFormation = this.template.CanAttackAsFormation == "true"; 11 : }; 12 : 13 0 : FormationAttack.prototype.CanAttackAsFormation = function() 14 : { 15 0 : return this.canAttackAsFormation; 16 : }; 17 : 18 : // Only called when making formation entities selectable for debugging 19 0 : FormationAttack.prototype.GetAttackTypes = function() 20 : { 21 0 : return []; 22 : }; 23 : 24 0 : FormationAttack.prototype.GetRange = function(target) 25 : { 26 0 : var result = { "min": 0, "max": this.canAttackAsFormation ? -1 : 0 }; 27 0 : var cmpFormation = Engine.QueryInterface(this.entity, IID_Formation); 28 0 : if (!cmpFormation) 29 : { 30 0 : warn("FormationAttack component used on a non-formation entity"); 31 0 : return result; 32 : } 33 0 : var members = cmpFormation.GetMembers(); 34 0 : for (var ent of members) 35 : { 36 0 : var cmpAttack = Engine.QueryInterface(ent, IID_Attack); 37 0 : if (!cmpAttack) 38 0 : continue; 39 : 40 0 : var type = cmpAttack.GetBestAttackAgainst(target); 41 0 : if (!type) 42 0 : continue; 43 : 44 : // if the formation can attack, take the minimum max range (so units are certainly in range), 45 : // If the formation can't attack, take the maximum max range as the point where the formation will be disbanded 46 : // Always take the minimum min range (to not get impossible situations) 47 0 : var range = cmpAttack.GetRange(type); 48 : 49 0 : if (this.canAttackAsFormation) 50 : { 51 0 : if (range.max < result.max || result.max < 0) 52 0 : result.max = range.max; 53 : } 54 : else 55 : { 56 0 : if (range.max > result.max || range.max < 0) 57 0 : result.max = range.max; 58 : } 59 0 : if (range.min < result.min) 60 0 : result.min = range.min; 61 : } 62 : // add half the formation size, so it counts as the range for the units on the first row 63 0 : var extraRange = cmpFormation.GetSize().depth/2; 64 : 65 0 : if (result.max >= 0) 66 0 : result.max += extraRange; 67 : 68 0 : return result; 69 : }; 70 : 71 0 : Engine.RegisterComponentType(IID_Attack, "FormationAttack", FormationAttack);