mirror of
https://gitea.wildfiregames.com/0ad/0ad
synced 2026-06-19 23:03:56 -07:00
Speed up the guard component by removing OnGlobal calls, based on a patch from wraitii, fixes #4333
This was SVN commit r19026.
This commit is contained in:
parent
f01e932ce3
commit
82faae8637
4 changed files with 97 additions and 62 deletions
|
|
@ -10,11 +10,11 @@ Guard.prototype.Init = function()
|
|||
|
||||
Guard.prototype.GetRange = function(entity)
|
||||
{
|
||||
var range = 8;
|
||||
var cmpFootprint = Engine.QueryInterface(entity, IID_Footprint);
|
||||
let range = 8;
|
||||
let cmpFootprint = Engine.QueryInterface(entity, IID_Footprint);
|
||||
if (cmpFootprint)
|
||||
{
|
||||
var shape = cmpFootprint.GetShape();
|
||||
let shape = cmpFootprint.GetShape();
|
||||
if (shape.type == "square")
|
||||
range += Math.sqrt(shape.depth*shape.depth + shape.width*shape.width)*2/3;
|
||||
else if (shape.type == "circle")
|
||||
|
|
@ -42,64 +42,54 @@ Guard.prototype.AddGuard = function(ent)
|
|||
|
||||
Guard.prototype.RemoveGuard = function(ent)
|
||||
{
|
||||
var index = this.entities.indexOf(ent);
|
||||
let index = this.entities.indexOf(ent);
|
||||
if (index != -1)
|
||||
this.entities.splice(index, 1);
|
||||
};
|
||||
|
||||
Guard.prototype.RenameGuard = function(oldent, newent)
|
||||
{
|
||||
let index = this.entities.indexOf(oldent);
|
||||
if (index != -1)
|
||||
this.entities[index] = newent;
|
||||
};
|
||||
|
||||
Guard.prototype.OnAttacked = function(msg)
|
||||
{
|
||||
for each (var ent in this.entities)
|
||||
for (let ent of this.entities)
|
||||
Engine.PostMessage(ent, MT_GuardedAttacked, { "guarded": this.entity, "data": msg });
|
||||
};
|
||||
|
||||
/**
|
||||
* Update list of guarding/escorting entities if one gets renamed (e.g. by promotion)
|
||||
* If an entity is captured, or about to be killed (so its owner
|
||||
* changes to '-1') or if diplomacy changed, update the guards list
|
||||
*/
|
||||
Guard.prototype.OnGlobalEntityRenamed = function(msg)
|
||||
Guard.prototype.OnOwnershipChanged = function(msg)
|
||||
{
|
||||
var entityIndex = this.entities.indexOf(msg.entity);
|
||||
if (entityIndex != -1)
|
||||
this.entities[entityIndex] = msg.newentity;
|
||||
if (!this.entities.length)
|
||||
return;
|
||||
this.CheckGuards(msg.to == -1);
|
||||
};
|
||||
|
||||
/**
|
||||
* If an entity is captured, or about to be killed (so its owner
|
||||
* changes to '-1'), update the guards list
|
||||
*/
|
||||
Guard.prototype.OnGlobalOwnershipChanged = function(msg)
|
||||
Guard.prototype.OnDiplomacyChanged = function(msg)
|
||||
{
|
||||
// the ownership change may be on the guarded
|
||||
if (this.entity == msg.entity)
|
||||
{
|
||||
var entities = this.GetEntities();
|
||||
for each (var entity in entities)
|
||||
{
|
||||
if (msg.to == -1 || !IsOwnedByMutualAllyOfEntity(this.entity, entity))
|
||||
{
|
||||
var cmpUnitAI = Engine.QueryInterface(entity, IID_UnitAI);
|
||||
if (cmpUnitAI && cmpUnitAI.IsGuardOf() && cmpUnitAI.IsGuardOf() == this.entity)
|
||||
cmpUnitAI.RemoveGuard();
|
||||
else
|
||||
this.RemoveGuard(entity);
|
||||
}
|
||||
}
|
||||
this.entities = entities;
|
||||
if (!this.entities.length)
|
||||
return;
|
||||
}
|
||||
this.CheckGuards();
|
||||
};
|
||||
|
||||
// or on some of its guards
|
||||
if (this.entities.indexOf(msg.entity) != -1)
|
||||
Guard.prototype.CheckGuards = function(force = false)
|
||||
{
|
||||
let entities = this.GetEntities();
|
||||
for (let ent of entities)
|
||||
{
|
||||
if (msg.to == -1)
|
||||
this.RemoveGuard(msg.entity);
|
||||
else if(!IsOwnedByMutualAllyOfEntity(this.entity, msg.entity))
|
||||
if (force || !IsOwnedByMutualAllyOfEntity(this.entity, ent))
|
||||
{
|
||||
var cmpUnitAI = Engine.QueryInterface(msg.entity, IID_UnitAI);
|
||||
if (cmpUnitAI)
|
||||
let cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI);
|
||||
if (cmpUnitAI && cmpUnitAI.IsGuardOf() && cmpUnitAI.IsGuardOf() == this.entity)
|
||||
cmpUnitAI.RemoveGuard();
|
||||
else
|
||||
this.RemoveGuard(msg.entity);
|
||||
this.RemoveGuard(ent);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -91,12 +91,34 @@ Promotion.prototype.Promote = function(promotedTemplateName)
|
|||
|
||||
var workOrders = cmpCurrentUnitAI.GetWorkOrders();
|
||||
cmpPromotedUnitAI.SetWorkOrders(workOrders);
|
||||
cmpPromotedUnitAI.SetGuardOf(cmpCurrentUnitAI.IsGuardOf());
|
||||
|
||||
var cmpCurrentUnitGuard = Engine.QueryInterface(this.entity, IID_Guard);
|
||||
var cmpPromotedUnitGuard = Engine.QueryInterface(promotedUnitEntity, IID_Guard);
|
||||
if (cmpCurrentUnitAI.IsGuardOf())
|
||||
{
|
||||
let guarded = cmpCurrentUnitAI.IsGuardOf();
|
||||
let cmpGuard = Engine.QueryInterface(guarded, IID_UnitAI)
|
||||
if (cmpGuard)
|
||||
{
|
||||
cmpGuard.RenameGuard(this.entity, promotedUnitEntity);
|
||||
cmpPromotedUnitAI.SetGuardOf(guarded);
|
||||
}
|
||||
}
|
||||
|
||||
let cmpCurrentUnitGuard = Engine.QueryInterface(this.entity, IID_Guard);
|
||||
let cmpPromotedUnitGuard = Engine.QueryInterface(promotedUnitEntity, IID_Guard);
|
||||
if (cmpCurrentUnitGuard && cmpPromotedUnitGuard)
|
||||
cmpPromotedUnitGuard.SetEntities(cmpCurrentUnitGuard.GetEntities());
|
||||
{
|
||||
let entities = cmpCurrentUnitGuard.GetEntities();
|
||||
if (entities.length)
|
||||
{
|
||||
cmpPromotedUnitGuard.SetEntities(entities);
|
||||
for (let ent of entities)
|
||||
{
|
||||
let cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI);
|
||||
if (cmpUnitAI)
|
||||
cmpUnitAI.SetGuardOf(promotedUnitEntity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Engine.BroadcastMessage(MT_EntityRenamed, { entity: this.entity, newentity: promotedUnitEntity });
|
||||
|
||||
|
|
|
|||
|
|
@ -3468,15 +3468,21 @@ UnitAI.prototype.OnCreate = function()
|
|||
|
||||
UnitAI.prototype.OnDiplomacyChanged = function(msg)
|
||||
{
|
||||
var cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership);
|
||||
let cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership);
|
||||
if (cmpOwnership && cmpOwnership.GetOwner() == msg.player)
|
||||
this.SetupRangeQueries();
|
||||
|
||||
if (this.isGuardOf && !IsOwnedByMutualAllyOfEntity(this.entity, this.isGuardOf))
|
||||
this.RemoveGuard();
|
||||
};
|
||||
|
||||
UnitAI.prototype.OnOwnershipChanged = function(msg)
|
||||
{
|
||||
this.SetupRangeQueries();
|
||||
|
||||
if (this.isGuardOf && (msg.to == -1 || !IsOwnedByMutualAllyOfEntity(this.entity, this.isGuardOf)))
|
||||
this.RemoveGuard();
|
||||
|
||||
// If the unit isn't being created or dying, reset stance and clear orders
|
||||
if (msg.to != -1 && msg.from != -1)
|
||||
{
|
||||
|
|
@ -4038,9 +4044,6 @@ UnitAI.prototype.OnGlobalEntityRenamed = function(msg)
|
|||
}
|
||||
if (changed)
|
||||
Engine.PostMessage(this.entity, MT_UnitAIOrderDataChanged, { "to": this.GetOrderData() });
|
||||
|
||||
if (this.isGuardOf && this.isGuardOf == msg.entity)
|
||||
this.isGuardOf = msg.newentity;
|
||||
};
|
||||
|
||||
UnitAI.prototype.OnAttacked = function(msg)
|
||||
|
|
@ -4995,14 +4998,14 @@ UnitAI.prototype.AddGuard = function(target)
|
|||
|
||||
UnitAI.prototype.RemoveGuard = function()
|
||||
{
|
||||
if (this.isGuardOf)
|
||||
{
|
||||
var cmpGuard = Engine.QueryInterface(this.isGuardOf, IID_Guard);
|
||||
if (cmpGuard)
|
||||
cmpGuard.RemoveGuard(this.entity);
|
||||
this.guardRange = undefined;
|
||||
this.isGuardOf = undefined;
|
||||
}
|
||||
if (!this.isGuardOf)
|
||||
return;
|
||||
|
||||
let cmpGuard = Engine.QueryInterface(this.isGuardOf, IID_Guard);
|
||||
if (cmpGuard)
|
||||
cmpGuard.RemoveGuard(this.entity);
|
||||
this.guardRange = undefined;
|
||||
this.isGuardOf = undefined;
|
||||
|
||||
if (!this.order)
|
||||
return;
|
||||
|
|
@ -5010,10 +5013,9 @@ UnitAI.prototype.RemoveGuard = function()
|
|||
if (this.order.type == "Guard")
|
||||
this.UnitFsm.ProcessMessage(this, {"type": "RemoveGuard"});
|
||||
else
|
||||
for (var i = 1; i < this.orderQueue.length; ++i)
|
||||
for (let i = 1; i < this.orderQueue.length; ++i)
|
||||
if (this.orderQueue[i].type == "Guard")
|
||||
this.orderQueue.splice(i, 1);
|
||||
|
||||
Engine.PostMessage(this.entity, MT_UnitAIOrderDataChanged, { "to": this.GetOrderData() });
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -63,14 +63,35 @@ function ChangeEntityTemplate(oldEnt, newTemplate)
|
|||
if (cmpUnitAI.GetStanceName())
|
||||
cmpNewUnitAI.SwitchToStance(cmpUnitAI.GetStanceName());
|
||||
cmpNewUnitAI.AddOrders(cmpUnitAI.GetOrders());
|
||||
cmpNewUnitAI.SetGuardOf(cmpUnitAI.IsGuardOf());
|
||||
if (cmpUnitAI.IsGuardOf())
|
||||
{
|
||||
let guarded = cmpUnitAI.IsGuardOf();
|
||||
let cmpGuard = Engine.QueryInterface(guarded, IID_UnitAI)
|
||||
if (cmpGuard)
|
||||
{
|
||||
cmpGuard.RenameGuard(oldEnt, newEnt);
|
||||
cmpNewUnitAI.SetGuardOf(guarded);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Maintain the list of guards
|
||||
var cmpGuard = Engine.QueryInterface(oldEnt, IID_Guard);
|
||||
var cmpNewGuard = Engine.QueryInterface(newEnt, IID_Guard);
|
||||
let cmpGuard = Engine.QueryInterface(oldEnt, IID_Guard);
|
||||
let cmpNewGuard = Engine.QueryInterface(newEnt, IID_Guard);
|
||||
if (cmpGuard && cmpNewGuard)
|
||||
cmpNewGuard.SetEntities(cmpGuard.GetEntities());
|
||||
{
|
||||
let entities = cmpGuard.GetEntities();
|
||||
if (entities.length)
|
||||
{
|
||||
cmpNewGuard.SetEntities(entities);
|
||||
for (let ent of entities)
|
||||
{
|
||||
let cmpEntUnitAI = Engine.QueryInterface(ent, IID_UnitAI);
|
||||
if (cmpEntUnitAI)
|
||||
cmpEntUnitAI.SetGuardOf(NewEnt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TransferGarrisonedUnits(oldEnt, newEnt);
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue