Fix some 'undefined' problems reported by Lion.Kanzen + improve coding style.

This was SVN commit r14814.
This commit is contained in:
sanderd17 2014-03-07 08:27:13 +00:00
parent 29f94a759c
commit 32cf46f01c
3 changed files with 28 additions and 33 deletions

View File

@ -2101,7 +2101,12 @@ function findIdleUnit(classes)
for (var i = 0; i < classes.length; ++i) for (var i = 0; i < classes.length; ++i)
{ {
var data = { idleClass: classes[currIdleClass], prevUnit: lastIdleUnit, limit: 1 }; var data = {
"idleClass": classes[currIdleClass],
"prevUnit": lastIdleUnit,
"limit": 1,
"excludeUnits": []
};
if (append) if (append)
data.excludeUnits = g_Selection.toList(); data.excludeUnits = g_Selection.toList();
@ -2122,6 +2127,7 @@ function findIdleUnit(classes)
{ {
g_Selection.addList([lastIdleUnit]); g_Selection.addList([lastIdleUnit]);
var position = GetEntityState(lastIdleUnit).position; var position = GetEntityState(lastIdleUnit).position;
if (position)
Engine.CameraMoveTo(position.x, position.z); Engine.CameraMoveTo(position.x, position.z);
return; return;
} }

View File

@ -1738,40 +1738,29 @@ GuiInterface.prototype.PlaySound = function(player, data)
PlaySound(data.name, data.entity); PlaySound(data.name, data.entity);
}; };
function isIdleUnit(ent, idleClass)
{
var cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI);
var cmpIdentity = Engine.QueryInterface(ent, IID_Identity);
// TODO: Do something with garrisoned idle units
return (cmpUnitAI && cmpIdentity && cmpUnitAI.IsIdle() && !cmpUnitAI.IsGarrisoned() && idleClass && cmpIdentity.HasClass(idleClass));
};
GuiInterface.prototype.FindIdleUnits = function(player, data) GuiInterface.prototype.FindIdleUnits = function(player, data)
{ {
var rangeMan = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager); var cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
var playerEntities = rangeMan.GetEntitiesByPlayer(player).filter( function(e) { var playerEntities = cmpRangeManager.GetEntitiesByPlayer(player).filter( function(e) {
var cmpUnitAI = Engine.QueryInterface(e, IID_UnitAI); var cmpUnitAI = Engine.QueryInterface(e, IID_UnitAI);
if (cmpUnitAI) if (!cmpUnitAI || !cmpUnitAI.IsIdle() || cmpUnitAI.IsGarrisoned())
return true;
return false; return false;
var cmpIdentity = Engine.QueryInterface(e, IID_Identity);
if (!cmpIdentity || !cmpIdentity.HasClass(data.idleClass))
return false;
return true;
}); });
var idleUnits = []; var idleUnits = [];
var noFilter = (data.prevUnit == undefined && data.excludeUnits == undefined);
for (var j = 0; j < playerEntities.length; ++j) for (var j = 0; j < playerEntities.length; ++j)
{ {
var ent = playerEntities[j]; var ent = playerEntities[j];
if (!isIdleUnit(ent, data.idleClass))
continue;
if (noFilter || ((data.prevUnit == undefined || ent > data.prevUnit) && if (ent <= data.prevUnit|0 || data.excludeUnits.indexOf(ent) > -1)
(data.excludeUnits == undefined || data.excludeUnits.indexOf(ent) == -1))) continue;
{
idleUnits.push(ent); idleUnits.push(ent);
playerEntities.splice(j--, 1); playerEntities.splice(j--, 1);
}
if (data.limit && idleUnits.length >= data.limit) if (data.limit && idleUnits.length >= data.limit)
break; break;

View File

@ -1636,7 +1636,7 @@ var UnitFsmSpec = {
}, },
"Timer": function(msg) { "Timer": function(msg) {
if (this.ShouldAbandonChase(this.order.data.target, this.order.data.force, IID_Attack)) if (this.ShouldAbandonChase(this.order.data.target, this.order.data.force, IID_Attack, this.order.data.attackType))
{ {
this.StopMoving(); this.StopMoving();
this.FinishOrder(); this.FinishOrder();
@ -1932,7 +1932,7 @@ var UnitFsmSpec = {
}, },
"Timer": function(msg) { "Timer": function(msg) {
if (this.ShouldAbandonChase(this.order.data.target, this.order.data.force, IID_Attack)) if (this.ShouldAbandonChase(this.order.data.target, this.order.data.force, IID_Attack, this.order.data.attackType))
{ {
this.StopMoving(); this.StopMoving();
this.FinishOrder(); this.FinishOrder();
@ -2330,7 +2330,7 @@ var UnitFsmSpec = {
}, },
"Timer": function(msg) { "Timer": function(msg) {
if (this.ShouldAbandonChase(this.order.data.target, this.order.data.force, IID_Heal)) if (this.ShouldAbandonChase(this.order.data.target, this.order.data.force, IID_Heal, null))
{ {
this.StopMoving(); this.StopMoving();
this.FinishOrder(); this.FinishOrder();
@ -2430,7 +2430,7 @@ var UnitFsmSpec = {
this.StopTimer(); this.StopTimer();
}, },
"Timer": function(msg) { "Timer": function(msg) {
if (this.ShouldAbandonChase(this.order.data.target, this.order.data.force, IID_Heal)) if (this.ShouldAbandonChase(this.order.data.target, this.order.data.force, IID_Heal, null))
{ {
this.StopMoving(); this.StopMoving();
this.FinishOrder(); this.FinishOrder();
@ -4494,7 +4494,7 @@ UnitAI.prototype.RespondToHealableEntities = function(ents)
/** /**
* Returns true if we should stop following the target entity. * Returns true if we should stop following the target entity.
*/ */
UnitAI.prototype.ShouldAbandonChase = function(target, force, iid) UnitAI.prototype.ShouldAbandonChase = function(target, force, iid, type)
{ {
// Forced orders shouldn't be interrupted. // Forced orders shouldn't be interrupted.
if (force) if (force)
@ -4507,8 +4507,8 @@ UnitAI.prototype.ShouldAbandonChase = function(target, force, iid)
var cmpAttack = Engine.QueryInterface(target, IID_Attack); var cmpAttack = Engine.QueryInterface(target, IID_Attack);
if (cmpUnitAI && cmpAttack) if (cmpUnitAI && cmpAttack)
{ {
for each (var type in cmpAttack.GetAttackTypes()) for each (var targetType in cmpAttack.GetAttackTypes())
if (cmpUnitAI.CheckTargetAttackRange(this.isGuardOf, type)) if (cmpUnitAI.CheckTargetAttackRange(this.isGuardOf, targetType))
return false; return false;
} }
} }
@ -4516,7 +4516,7 @@ UnitAI.prototype.ShouldAbandonChase = function(target, force, iid)
// Stop if we're in hold-ground mode and it's too far from the holding point // Stop if we're in hold-ground mode and it's too far from the holding point
if (this.GetStance().respondHoldGround) if (this.GetStance().respondHoldGround)
{ {
if (!this.CheckTargetDistanceFromHeldPosition(target, iid, this.order.data.attackType)) if (!this.CheckTargetDistanceFromHeldPosition(target, iid, type))
return true; return true;
} }