Ticket #4333: guard-4333.patch
File guard-4333.patch, 8.7 KB (added by , 7 years ago) |
---|
-
binaries/data/mods/public/simulation/components/Guard.js
10 10 11 11 Guard.prototype.GetRange = function(entity) 12 12 { 13 varrange = 8;14 varcmpFootprint = Engine.QueryInterface(entity, IID_Footprint);13 let range = 8; 14 let cmpFootprint = Engine.QueryInterface(entity, IID_Footprint); 15 15 if (cmpFootprint) 16 16 { 17 varshape = cmpFootprint.GetShape();17 let shape = cmpFootprint.GetShape(); 18 18 if (shape.type == "square") 19 19 range += Math.sqrt(shape.depth*shape.depth + shape.width*shape.width)*2/3; 20 20 else if (shape.type == "circle") … … 42 42 43 43 Guard.prototype.RemoveGuard = function(ent) 44 44 { 45 varindex = this.entities.indexOf(ent);45 let index = this.entities.indexOf(ent); 46 46 if (index != -1) 47 47 this.entities.splice(index, 1); 48 48 }; 49 49 50 Guard.prototype.RenameGuard = function(oldent, newent) 51 { 52 let index = this.entities.indexOf(oldent); 53 if (index != -1) 54 this.entities[index] = newent; 55 }; 56 50 57 Guard.prototype.OnAttacked = function(msg) 51 58 { 52 for each (var ent inthis.entities)59 for (let ent of this.entities) 53 60 Engine.PostMessage(ent, MT_GuardedAttacked, { "guarded": this.entity, "data": msg }); 54 61 }; 55 62 56 63 /** 57 * Update list of guarding/escorting entities if one gets renamed (e.g. by promotion) 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 58 66 */ 59 Guard.prototype.On GlobalEntityRenamed = function(msg)67 Guard.prototype.OnOwnershipChanged = function(msg) 60 68 { 61 var entityIndex = this.entities.indexOf(msg.entity);62 if (entityIndex != -1)63 this.entities[entityIndex] = msg.newentity;69 if (!this.entities.length) 70 return; 71 this.CheckGuards(msg.to == -1); 64 72 }; 65 73 66 /** 67 * If an entity is captured, or about to be killed (so its owner 68 * changes to '-1'), update the guards list 69 */ 70 Guard.prototype.OnGlobalOwnershipChanged = function(msg) 74 Guard.prototype.OnDiplomacyChanged = function(msg) 71 75 { 72 // the ownership change may be on the guarded 73 if (this.entity == msg.entity) 74 { 75 var entities = this.GetEntities(); 76 for each (var entity in entities) 77 { 78 if (msg.to == -1 || !IsOwnedByMutualAllyOfEntity(this.entity, entity)) 79 { 80 var cmpUnitAI = Engine.QueryInterface(entity, IID_UnitAI); 81 if (cmpUnitAI && cmpUnitAI.IsGuardOf() && cmpUnitAI.IsGuardOf() == this.entity) 82 cmpUnitAI.RemoveGuard(); 83 else 84 this.RemoveGuard(entity); 85 } 86 } 87 this.entities = entities; 76 if (!this.entities.length) 88 77 return; 89 } 78 this.CheckGuards(); 79 }; 90 80 91 // or on some of its guards 92 if (this.entities.indexOf(msg.entity) != -1) 81 Guard.prototype.CheckGuards = function(force = false) 82 { 83 let entities = this.GetEntities(); 84 for (let ent of entities) 93 85 { 94 if (msg.to == -1) 95 this.RemoveGuard(msg.entity); 96 else if(!IsOwnedByMutualAllyOfEntity(this.entity, msg.entity)) 86 if (force || !IsOwnedByMutualAllyOfEntity(this.entity, ent)) 97 87 { 98 var cmpUnitAI = Engine.QueryInterface(msg.entity, IID_UnitAI);99 if (cmpUnitAI )88 let cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI); 89 if (cmpUnitAI && cmpUnitAI.IsGuardOf() && cmpUnitAI.IsGuardOf() == this.entity) 100 90 cmpUnitAI.RemoveGuard(); 101 91 else 102 this.RemoveGuard( msg.entity);92 this.RemoveGuard(ent); 103 93 } 104 94 } 105 95 }; -
binaries/data/mods/public/simulation/components/Promotion.js
91 91 92 92 var workOrders = cmpCurrentUnitAI.GetWorkOrders(); 93 93 cmpPromotedUnitAI.SetWorkOrders(workOrders); 94 cmpPromotedUnitAI.SetGuardOf(cmpCurrentUnitAI.IsGuardOf());95 94 96 var cmpCurrentUnitGuard = Engine.QueryInterface(this.entity, IID_Guard); 97 var cmpPromotedUnitGuard = Engine.QueryInterface(promotedUnitEntity, IID_Guard); 95 if (cmpCurrentUnitAI.IsGuardOf()) 96 { 97 let guarded = cmpCurrentUnitAI.IsGuardOf(); 98 let cmpGuard = Engine.QueryInterface(guarded, IID_UnitAI) 99 if (cmpGuard) 100 { 101 cmpGuard.RenameGuard(this.entity, promotedUnitEntity); 102 cmpPromotedUnitAI.SetGuardOf(guarded); 103 } 104 } 105 106 let cmpCurrentUnitGuard = Engine.QueryInterface(this.entity, IID_Guard); 107 let cmpPromotedUnitGuard = Engine.QueryInterface(promotedUnitEntity, IID_Guard); 98 108 if (cmpCurrentUnitGuard && cmpPromotedUnitGuard) 99 cmpPromotedUnitGuard.SetEntities(cmpCurrentUnitGuard.GetEntities()); 109 { 110 let entities = cmpCurrentUnitGuard.GetEntities(); 111 if (entities.length) 112 { 113 cmpPromotedUnitGuard.SetEntities(entities); 114 for (let ent of entities) 115 { 116 let cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI); 117 if (cmpUnitAI) 118 cmpUnitAI.SetGuardOf(promotedUnitEntity); 119 } 120 } 121 } 100 122 101 123 Engine.BroadcastMessage(MT_EntityRenamed, { entity: this.entity, newentity: promotedUnitEntity }); 102 124 -
binaries/data/mods/public/simulation/components/UnitAI.js
3468 3468 3469 3469 UnitAI.prototype.OnDiplomacyChanged = function(msg) 3470 3470 { 3471 varcmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership);3471 let cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership); 3472 3472 if (cmpOwnership && cmpOwnership.GetOwner() == msg.player) 3473 3473 this.SetupRangeQueries(); 3474 3475 if (this.isGuardOf && !IsOwnedByMutualAllyOfEntity(this.entity, this.isGuardOf)) 3476 this.RemoveGuard(); 3474 3477 }; 3475 3478 3476 3479 UnitAI.prototype.OnOwnershipChanged = function(msg) … … 3477 3480 { 3478 3481 this.SetupRangeQueries(); 3479 3482 3483 if (this.isGuardOf && (msg.to == -1 || !IsOwnedByMutualAllyOfEntity(this.entity, this.isGuardOf))) 3484 this.RemoveGuard(); 3485 3480 3486 // If the unit isn't being created or dying, reset stance and clear orders 3481 3487 if (msg.to != -1 && msg.from != -1) 3482 3488 { … … 4038 4044 } 4039 4045 if (changed) 4040 4046 Engine.PostMessage(this.entity, MT_UnitAIOrderDataChanged, { "to": this.GetOrderData() }); 4041 4042 if (this.isGuardOf && this.isGuardOf == msg.entity)4043 this.isGuardOf = msg.newentity;4044 4047 }; 4045 4048 4046 4049 UnitAI.prototype.OnAttacked = function(msg) … … 4995 4998 4996 4999 UnitAI.prototype.RemoveGuard = function() 4997 5000 { 4998 if (this.isGuardOf) 4999 { 5000 var cmpGuard = Engine.QueryInterface(this.isGuardOf, IID_Guard); 5001 if (cmpGuard) 5002 cmpGuard.RemoveGuard(this.entity); 5003 this.guardRange = undefined; 5004 this.isGuardOf = undefined; 5005 } 5001 if (!this.isGuardOf) 5002 return; 5006 5003 5004 let cmpGuard = Engine.QueryInterface(this.isGuardOf, IID_Guard); 5005 if (cmpGuard) 5006 cmpGuard.RemoveGuard(this.entity); 5007 this.guardRange = undefined; 5008 this.isGuardOf = undefined; 5009 5007 5010 if (!this.order) 5008 5011 return; 5009 5012 … … 5010 5013 if (this.order.type == "Guard") 5011 5014 this.UnitFsm.ProcessMessage(this, {"type": "RemoveGuard"}); 5012 5015 else 5013 for ( vari = 1; i < this.orderQueue.length; ++i)5016 for (let i = 1; i < this.orderQueue.length; ++i) 5014 5017 if (this.orderQueue[i].type == "Guard") 5015 5018 this.orderQueue.splice(i, 1); 5016 5017 5019 Engine.PostMessage(this.entity, MT_UnitAIOrderDataChanged, { "to": this.GetOrderData() }); 5018 5020 }; 5019 5021 -
binaries/data/mods/public/simulation/helpers/Transform.js
63 63 if (cmpUnitAI.GetStanceName()) 64 64 cmpNewUnitAI.SwitchToStance(cmpUnitAI.GetStanceName()); 65 65 cmpNewUnitAI.AddOrders(cmpUnitAI.GetOrders()); 66 cmpNewUnitAI.SetGuardOf(cmpUnitAI.IsGuardOf()); 66 if (cmpUnitAI.IsGuardOf()) 67 { 68 let guarded = cmpUnitAI.IsGuardOf(); 69 let cmpGuard = Engine.QueryInterface(guarded, IID_UnitAI) 70 if (cmpGuard) 71 { 72 cmpGuard.RenameGuard(oldEnt, newEnt); 73 cmpNewUnitAI.SetGuardOf(guarded); 74 } 75 } 67 76 } 68 77 69 78 // Maintain the list of guards 70 varcmpGuard = Engine.QueryInterface(oldEnt, IID_Guard);71 varcmpNewGuard = Engine.QueryInterface(newEnt, IID_Guard);79 let cmpGuard = Engine.QueryInterface(oldEnt, IID_Guard); 80 let cmpNewGuard = Engine.QueryInterface(newEnt, IID_Guard); 72 81 if (cmpGuard && cmpNewGuard) 73 cmpNewGuard.SetEntities(cmpGuard.GetEntities()); 82 { 83 let entities = cmpGuard.GetEntities(); 84 if (entities.length) 85 { 86 cmpNewGuard.SetEntities(entities); 87 for (let ent of entities) 88 { 89 let cmpEntUnitAI = Engine.QueryInterface(ent, IID_UnitAI); 90 if (cmpEntUnitAI) 91 cmpEntUnitAI.SetGuardOf(NewEnt); 92 } 93 } 94 } 74 95 75 96 TransferGarrisonedUnits(oldEnt, newEnt); 76 97