forked from mirrors/0ad
Reduce UnitAI duplication in range checks.
By querying the range and passing a target along. Differential revision: D3748 Comments by: @wraitii This was SVN commit r25162.
This commit is contained in:
@@ -56,7 +56,7 @@ GarrisonHolder.prototype.IsGarrisoned = function(entity)
|
||||
/**
|
||||
* @return {Object} max and min range at which entities can garrison the holder.
|
||||
*/
|
||||
GarrisonHolder.prototype.GetLoadingRange = function()
|
||||
GarrisonHolder.prototype.LoadingRange = function()
|
||||
{
|
||||
return { "max": +this.template.LoadingRange, "min": 0 };
|
||||
};
|
||||
|
||||
@@ -13,6 +13,17 @@ Garrisonable.prototype.Init = function()
|
||||
{
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {string} type - Unused.
|
||||
* @param {number} target - The entity ID of the target to check.
|
||||
* @return {Object} - Min and max ranges this entity needs to be in in order to garrison the target.
|
||||
*/
|
||||
Garrisonable.prototype.GetRange = function(type, target)
|
||||
{
|
||||
let cmpGarrisonHolder = Engine.QueryInterface(target, IID_GarrisonHolder);
|
||||
return cmpGarrisonHolder ? cmpGarrisonHolder.LoadingRange() : { "min": 0, "max": 1 };
|
||||
};
|
||||
|
||||
/**
|
||||
* @return {number} - The number of slots this unit takes in a garrisonHolder.
|
||||
*/
|
||||
|
||||
@@ -215,7 +215,7 @@ class TurretHolder
|
||||
/**
|
||||
* @return {Object} - Max and min ranges at which entities can occupy any turret.
|
||||
*/
|
||||
GetLoadingRange()
|
||||
LoadingRange()
|
||||
{
|
||||
return { "min": 0, "max": +(this.template.LoadingRange || 2) };
|
||||
}
|
||||
|
||||
@@ -7,6 +7,17 @@ Turretable.prototype.Init = function()
|
||||
{
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {string} type - Unused.
|
||||
* @param {number} target - The entity ID of the target to check.
|
||||
* @return {Object} - The range this entity needs to be in in order to occupy a turret point on the target.
|
||||
*/
|
||||
Turretable.prototype.GetRange = function(type, target)
|
||||
{
|
||||
let cmpTurretHolder = Engine.QueryInterface(target, IID_TurretHolder);
|
||||
return cmpTurretHolder ? cmpTurretHolder.LoadingRange() : { "min": 0, "max": 1 };
|
||||
};
|
||||
|
||||
/**
|
||||
* @return {number} - The entity ID of the entity this entity is turreted on.
|
||||
*/
|
||||
|
||||
@@ -592,8 +592,7 @@ UnitAI.prototype.UnitFsmSpec = {
|
||||
return ACCEPT_ORDER;
|
||||
}
|
||||
|
||||
if (msg.data.garrison ? this.CheckGarrisonRange(msg.data.target) :
|
||||
this.CheckOccupyTurretRange(msg.data.target))
|
||||
if (this.CheckTargetRange(msg.data.target, msg.data.garrison ? IID_Garrisonable : IID_Turretable))
|
||||
this.SetNextState("INDIVIDUAL.GARRISON.GARRISONING");
|
||||
else
|
||||
this.SetNextState("INDIVIDUAL.GARRISON.APPROACHING");
|
||||
@@ -748,8 +747,7 @@ UnitAI.prototype.UnitFsmSpec = {
|
||||
if (!Engine.QueryInterface(msg.data.target,
|
||||
msg.data.garrison ? IID_GarrisonHolder : IID_TurretHolder))
|
||||
return this.FinishOrder();
|
||||
if (!(msg.data.garrison ? this.CheckGarrisonRange(msg.data.target) :
|
||||
this.CheckOccupyTurretRange(msg.data.target)))
|
||||
if (this.CheckTargetRange(msg.data.target, msg.data.garrison ? IID_Garrisonable : IID_Turretable))
|
||||
{
|
||||
if (!this.CheckTargetVisible(msg.data.target))
|
||||
return this.FinishOrder();
|
||||
@@ -1103,8 +1101,7 @@ UnitAI.prototype.UnitFsmSpec = {
|
||||
"GARRISON": {
|
||||
"APPROACHING": {
|
||||
"enter": function() {
|
||||
if (!(this.order.data.garrison ? this.MoveToGarrisonRange(this.order.data.target) :
|
||||
this.MoveToOccupyTurretRange(this.order.data.target)))
|
||||
if (!this.MoveToTargetRange(this.order.data.target, this.order.data.garrison ? IID_Garrisonable : IID_Turretable))
|
||||
{
|
||||
this.FinishOrder();
|
||||
return true;
|
||||
@@ -3220,8 +3217,7 @@ UnitAI.prototype.UnitFsmSpec = {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (this.order.data.garrison ? !this.MoveToGarrisonRange(this.order.data.target) :
|
||||
!this.MoveToOccupyTurretRange(this.order.data.target))
|
||||
if (!this.MoveToTargetRange(this.order.data.target, this.order.data.garrison ? IID_Garrisonable : IID_Turretable))
|
||||
{
|
||||
this.FinishOrder();
|
||||
return true;
|
||||
@@ -3252,8 +3248,7 @@ UnitAI.prototype.UnitFsmSpec = {
|
||||
if (!msg.likelyFailure && !msg.likelySuccess)
|
||||
return;
|
||||
|
||||
if (this.order.data.garrison ? this.CheckGarrisonRange(this.order.data.target) :
|
||||
this.CheckOccupyTurretRange(this.order.data.target))
|
||||
if (this.CheckTargetRange(this.order.data.target, this.order.data.garrison ? IID_Garrisonable : IID_Turretable))
|
||||
this.SetNextState("GARRISONING");
|
||||
else
|
||||
{
|
||||
@@ -4708,7 +4703,7 @@ UnitAI.prototype.MoveToTargetRange = function(target, iid, type)
|
||||
if (!this.CheckTargetVisible(target))
|
||||
return false;
|
||||
|
||||
let range = this.GetRange(iid, type);
|
||||
let range = this.GetRange(iid, type, target);
|
||||
if (!range)
|
||||
return false;
|
||||
|
||||
@@ -4745,7 +4740,7 @@ UnitAI.prototype.MoveToTargetAttackRange = function(target, type)
|
||||
if (!this.CheckTargetVisible(target))
|
||||
return false;
|
||||
|
||||
let range = this.GetRange(IID_Attack, type);
|
||||
let range = this.GetRange(IID_Attack, type, target);
|
||||
if (!range)
|
||||
return false;
|
||||
|
||||
@@ -4808,34 +4803,6 @@ UnitAI.prototype.MoveFormationToTargetAttackRange = function(target)
|
||||
return this.AbleToMove(cmpUnitMotion) && cmpUnitMotion.MoveToTargetRange(target, range.min, range.max);
|
||||
};
|
||||
|
||||
UnitAI.prototype.MoveToGarrisonRange = function(target)
|
||||
{
|
||||
if (!this.CheckTargetVisible(target))
|
||||
return false;
|
||||
|
||||
var cmpGarrisonHolder = Engine.QueryInterface(target, IID_GarrisonHolder);
|
||||
if (!cmpGarrisonHolder)
|
||||
return false;
|
||||
var range = cmpGarrisonHolder.GetLoadingRange();
|
||||
|
||||
let cmpUnitMotion = Engine.QueryInterface(this.entity, IID_UnitMotion);
|
||||
return this.AbleToMove(cmpUnitMotion) && cmpUnitMotion.MoveToTargetRange(target, range.min, range.max);
|
||||
};
|
||||
|
||||
UnitAI.prototype.MoveToOccupyTurretRange = function(target)
|
||||
{
|
||||
if (!this.CheckTargetVisible(target))
|
||||
return false;
|
||||
|
||||
let cmpTurretHolder = Engine.QueryInterface(target, IID_TurretHolder);
|
||||
if (!cmpTurretHolder)
|
||||
return false;
|
||||
let range = cmpTurretHolder.GetLoadingRange();
|
||||
|
||||
let cmpUnitMotion = Engine.QueryInterface(this.entity, IID_UnitMotion);
|
||||
return this.AbleToMove(cmpUnitMotion) && cmpUnitMotion.MoveToTargetRange(target, range.min, range.max);
|
||||
};
|
||||
|
||||
/**
|
||||
* Generic dispatcher for other Check...Range functions.
|
||||
* @param iid - Interface ID (optional) implementing GetRange
|
||||
@@ -4866,7 +4833,7 @@ UnitAI.prototype.CheckPointRangeExplicit = function(x, z, min, max)
|
||||
|
||||
UnitAI.prototype.CheckTargetRange = function(target, iid, type)
|
||||
{
|
||||
let range = this.GetRange(iid, type);
|
||||
let range = this.GetRange(iid, type, target);
|
||||
if (!range)
|
||||
return false;
|
||||
|
||||
@@ -4902,7 +4869,7 @@ UnitAI.prototype.CheckTargetAttackRange = function(target, type)
|
||||
if (!targetCmpPosition || !targetCmpPosition.IsInWorld())
|
||||
return false;
|
||||
|
||||
let range = this.GetRange(IID_Attack, type);
|
||||
let range = this.GetRange(IID_Attack, type, target);
|
||||
if (!range)
|
||||
return false;
|
||||
|
||||
@@ -4951,26 +4918,6 @@ UnitAI.prototype.CheckFormationTargetAttackRange = function(target)
|
||||
return cmpObstructionManager.IsInTargetRange(this.entity, target, range.min, range.max, false);
|
||||
};
|
||||
|
||||
UnitAI.prototype.CheckGarrisonRange = function(target)
|
||||
{
|
||||
let cmpGarrisonHolder = Engine.QueryInterface(target, IID_GarrisonHolder);
|
||||
if (!cmpGarrisonHolder)
|
||||
return false;
|
||||
|
||||
let range = cmpGarrisonHolder.GetLoadingRange();
|
||||
return this.CheckTargetRangeExplicit(target, range.min, range.max);
|
||||
};
|
||||
|
||||
UnitAI.prototype.CheckOccupyTurretRange = function(target)
|
||||
{
|
||||
let cmpTurretHolder = Engine.QueryInterface(target, IID_TurretHolder);
|
||||
if (!cmpTurretHolder)
|
||||
return false;
|
||||
|
||||
let range = cmpTurretHolder.GetLoadingRange();
|
||||
return this.CheckTargetRangeExplicit(target, range.min, range.max);
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns true if the target entity is visible through the FoW/SoD.
|
||||
*/
|
||||
@@ -5059,7 +5006,7 @@ UnitAI.prototype.FaceTowardsTarget = function(target)
|
||||
|
||||
UnitAI.prototype.CheckTargetDistanceFromHeldPosition = function(target, iid, type)
|
||||
{
|
||||
let range = this.GetRange(iid, type);
|
||||
let range = this.GetRange(iid, type, target);
|
||||
if (!range)
|
||||
return false;
|
||||
|
||||
@@ -6349,19 +6296,20 @@ UnitAI.prototype.WalkToHeldPosition = function()
|
||||
* General getter for ranges.
|
||||
*
|
||||
* @param {number} iid
|
||||
* @param {number} target - [Optional]
|
||||
* @param {string} type - [Optional]
|
||||
* @return {Object | undefined} - The range in the form
|
||||
* { "min": number, "max": number }
|
||||
* Object."elevationBonus": number may be present when iid == IID_Attack.
|
||||
* Returns undefined when the entity does not have the requested component.
|
||||
*/
|
||||
UnitAI.prototype.GetRange = function(iid, type)
|
||||
UnitAI.prototype.GetRange = function(iid, type, target)
|
||||
{
|
||||
let component = Engine.QueryInterface(this.entity, iid);
|
||||
if (!component)
|
||||
return undefined;
|
||||
|
||||
return component.GetRange(type);
|
||||
return component.GetRange(type, target);
|
||||
};
|
||||
|
||||
UnitAI.prototype.CanAttack = function(target)
|
||||
|
||||
@@ -152,7 +152,7 @@ TS_ASSERT_EQUALS(cmpGarrisonHolder.IsGarrisoningAllowed(), false);
|
||||
cmpGarrisonHolder.AllowGarrisoning(true, 5);
|
||||
TS_ASSERT_EQUALS(cmpGarrisonHolder.IsGarrisoningAllowed(), true);
|
||||
|
||||
TS_ASSERT_UNEVAL_EQUALS(cmpGarrisonHolder.GetLoadingRange(), { "max": 2.1, "min": 0 });
|
||||
TS_ASSERT_UNEVAL_EQUALS(cmpGarrisonHolder.LoadingRange(), { "max": 2.1, "min": 0 });
|
||||
TS_ASSERT_UNEVAL_EQUALS(cmpGarrisonHolder.GetEntities(), []);
|
||||
TS_ASSERT_UNEVAL_EQUALS(cmpGarrisonHolder.GetHealRate(), 1);
|
||||
TS_ASSERT_UNEVAL_EQUALS(cmpGarrisonHolder.GetAllowedClasses(), "Infantry+Cavalry");
|
||||
|
||||
@@ -6,6 +6,7 @@ Engine.LoadComponentScript("interfaces/Auras.js");
|
||||
Engine.LoadComponentScript("interfaces/Builder.js");
|
||||
Engine.LoadComponentScript("interfaces/BuildingAI.js");
|
||||
Engine.LoadComponentScript("interfaces/Capturable.js");
|
||||
Engine.LoadComponentScript("interfaces/Garrisonable.js");
|
||||
Engine.LoadComponentScript("interfaces/Resistance.js");
|
||||
Engine.LoadComponentScript("interfaces/Formation.js");
|
||||
Engine.LoadComponentScript("interfaces/Heal.js");
|
||||
@@ -59,14 +60,9 @@ TestTargetEntityRenaming(
|
||||
"INDIVIDUAL.GARRISON.APPROACHING", "INDIVIDUAL.IDLE",
|
||||
(unitAI, player_ent, target_ent) => {
|
||||
unitAI.CanGarrison = (target) => target == target_ent;
|
||||
unitAI.MoveToGarrisonRange = (target) => target == target_ent;
|
||||
unitAI.MoveToTargetRange = (target) => target == target_ent;
|
||||
unitAI.AbleToMove = () => true;
|
||||
|
||||
AddMock(target_ent, IID_GarrisonHolder, {
|
||||
"GetLoadingRange": () => ({ "max": 100, "min": 0 }),
|
||||
"CanPickup": () => false
|
||||
});
|
||||
|
||||
unitAI.Garrison(target_ent, false);
|
||||
}
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user