Line data Source code
1 : function Guard() {} 2 : 3 0 : Guard.prototype.Schema = 4 : "<empty/>"; 5 : 6 0 : Guard.prototype.Init = function() 7 : { 8 0 : this.entities = []; 9 : }; 10 : 11 0 : Guard.prototype.GetRange = function(entity) 12 : { 13 0 : let range = 8; 14 0 : let cmpFootprint = Engine.QueryInterface(entity, IID_Footprint); 15 0 : if (cmpFootprint) 16 : { 17 0 : let shape = cmpFootprint.GetShape(); 18 0 : if (shape.type == "square") 19 0 : range += Math.sqrt(shape.depth*shape.depth + shape.width*shape.width)*2/3; 20 0 : else if (shape.type == "circle") 21 0 : range += shape.radius*3/2; 22 : } 23 0 : return range; 24 : }; 25 : 26 0 : Guard.prototype.GetEntities = function() 27 : { 28 0 : return this.entities.slice(); 29 : }; 30 : 31 0 : Guard.prototype.SetEntities = function(entities) 32 : { 33 0 : this.entities = entities; 34 : }; 35 : 36 0 : Guard.prototype.AddGuard = function(ent) 37 : { 38 0 : if (this.entities.indexOf(ent) != -1) 39 0 : return; 40 0 : this.entities.push(ent); 41 : }; 42 : 43 0 : Guard.prototype.RemoveGuard = function(ent) 44 : { 45 0 : let index = this.entities.indexOf(ent); 46 0 : if (index != -1) 47 0 : this.entities.splice(index, 1); 48 : }; 49 : 50 0 : Guard.prototype.RenameGuard = function(oldent, newent) 51 : { 52 0 : let index = this.entities.indexOf(oldent); 53 0 : if (index != -1) 54 0 : this.entities[index] = newent; 55 : }; 56 : 57 0 : Guard.prototype.OnAttacked = function(msg) 58 : { 59 0 : for (let ent of this.entities) 60 0 : Engine.PostMessage(ent, MT_GuardedAttacked, { "guarded": this.entity, "data": msg }); 61 : }; 62 : 63 : /** 64 : * If an entity is captured, or about to be killed (so its owner 65 : * changes to '-1') or if diplomacy changed, update the guards list 66 : */ 67 0 : Guard.prototype.OnOwnershipChanged = function(msg) 68 : { 69 0 : if (!this.entities.length) 70 0 : return; 71 0 : this.CheckGuards(msg.to == INVALID_PLAYER); 72 : }; 73 : 74 0 : Guard.prototype.OnDiplomacyChanged = function(msg) 75 : { 76 0 : if (!this.entities.length) 77 0 : return; 78 0 : this.CheckGuards(); 79 : }; 80 : 81 0 : Guard.prototype.CheckGuards = function(force = false) 82 : { 83 0 : let entities = this.GetEntities(); 84 0 : for (let ent of entities) 85 : { 86 0 : if (force || !IsOwnedByMutualAllyOfEntity(this.entity, ent)) 87 : { 88 0 : let cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI); 89 0 : if (cmpUnitAI && cmpUnitAI.IsGuardOf() && cmpUnitAI.IsGuardOf() == this.entity) 90 0 : cmpUnitAI.RemoveGuard(); 91 : else 92 0 : this.RemoveGuard(ent); 93 : } 94 : } 95 : }; 96 : 97 0 : Engine.RegisterComponentType(IID_Guard, "Guard", Guard);