1
0
forked from 0ad/0ad

Fix NaNs when gathering from unsupported resource types.

Fall back to walking to the target when unable to perform the specified
command on it.

This was SVN commit r7723.
This commit is contained in:
Ykkrosh 2010-07-08 20:08:08 +00:00
parent 56e1802b81
commit 778af99416
2 changed files with 57 additions and 6 deletions

View File

@ -53,15 +53,13 @@ ResourceGatherer.prototype.GetRange = function()
*/
ResourceGatherer.prototype.PerformGather = function(target)
{
var rate = this.GetTargetGatherRate(target);
if (!rate)
return { "exhausted": true };
var cmpResourceSupply = Engine.QueryInterface(target, IID_ResourceSupply);
var type = cmpResourceSupply.GetType();
var rate;
if (type.specific && this.template.Rates[type.generic+"."+type.specific])
rate = this.template.Rates[type.generic+"."+type.specific] * this.template.BaseSpeed;
else
rate = this.template.Rates[type.generic] * this.template.BaseSpeed;
var status = cmpResourceSupply.TakeResources(rate);
// Give the gathered resources to the player
@ -73,5 +71,26 @@ ResourceGatherer.prototype.PerformGather = function(target)
return status;
};
/**
* Compute the amount of resources collected per second from the target.
* Returns 0 if resources cannot be collected (e.g. the target doesn't
* exist, or is the wrong type).
*/
ResourceGatherer.prototype.GetTargetGatherRate = function(target)
{
var cmpResourceSupply = Engine.QueryInterface(target, IID_ResourceSupply);
if (!cmpResourceSupply)
return 0;
var type = cmpResourceSupply.GetType();
var rate;
if (type.specific && this.template.Rates[type.generic+"."+type.specific])
rate = this.template.Rates[type.generic+"."+type.specific];
else
rate = this.template.Rates[type.generic];
return (rate || 0) * this.template.BaseSpeed;
}
Engine.RegisterComponentType(IID_ResourceGatherer, "ResourceGatherer", ResourceGatherer);

View File

@ -91,18 +91,37 @@ UnitAI.prototype.Walk = function(x, z)
}
};
UnitAI.prototype.WalkToTarget = function(target)
{
var cmpPosition = Engine.QueryInterface(target, IID_Position);
if (!cmpPosition)
return;
if (!cmpPosition.IsInWorld())
return;
var pos = cmpPosition.GetPosition();
this.Walk(pos.x, pos.z);
}
UnitAI.prototype.Attack = function(target)
{
// Verify that we're able to respond to Attack commands
var cmpAttack = Engine.QueryInterface(this.entity, IID_Attack);
if (!cmpAttack)
{
this.WalkToTarget(target);
return;
}
// TODO: verify that this is a valid target
var type = cmpAttack.GetBestAttack();
if (!type)
{
this.WalkToTarget(target);
return;
}
// Stop any previous action timers
this.CancelTimers();
@ -125,7 +144,10 @@ UnitAI.prototype.Repair = function(target)
// Verify that we're able to respond to Repair commands
var cmpBuilder = Engine.QueryInterface(this.entity, IID_Builder);
if (!cmpBuilder)
{
this.WalkToTarget(target);
return;
}
// TODO: verify that this is a valid target
@ -149,7 +171,17 @@ UnitAI.prototype.Gather = function(target)
// Verify that we're able to respond to Gather commands
var cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer);
if (!cmpResourceGatherer)
{
this.WalkToTarget(target);
return;
}
// Verify that we can gather from this target
if (!cmpResourceGatherer.GetTargetGatherRate(target))
{
this.WalkToTarget(target);
return;
}
// TODO: verify that this is a valid target