Remove unnecessary lookups and checks
- We already return early if the unit doesn't have a BuildableInfo - World.Map.Rules.Actors[actorName] won't return null - Made BuildUnit use an ActorInfo instead of the name as parameter
This commit is contained in:
@@ -85,14 +85,13 @@ namespace OpenRA.Mods.Common.Traits
|
||||
.FirstOrDefault();
|
||||
}
|
||||
|
||||
protected override bool BuildUnit(string name)
|
||||
protected override bool BuildUnit(ActorInfo unit)
|
||||
{
|
||||
// Find a production structure to build this actor
|
||||
var ai = self.World.Map.Rules.Actors[name];
|
||||
var bi = ai.TraitInfoOrDefault<BuildableInfo>();
|
||||
var bi = unit.TraitInfo<BuildableInfo>();
|
||||
|
||||
// Some units may request a specific production type, which is ignored if the AllTech cheat is enabled
|
||||
var type = bi == null || developerMode.AllTech ? Info.Type : bi.BuildAtProductionType ?? Info.Type;
|
||||
var type = developerMode.AllTech ? Info.Type : bi.BuildAtProductionType ?? Info.Type;
|
||||
|
||||
var producers = self.World.ActorsWithTrait<Production>()
|
||||
.Where(x => x.Actor.Owner == self.Owner
|
||||
@@ -102,13 +101,13 @@ namespace OpenRA.Mods.Common.Traits
|
||||
|
||||
if (!producers.Any())
|
||||
{
|
||||
CancelProduction(name, 1);
|
||||
CancelProduction(unit.Name, 1);
|
||||
return true;
|
||||
}
|
||||
|
||||
foreach (var p in producers.Where(p => !p.Actor.IsDisabled()))
|
||||
{
|
||||
if (p.Trait.Produce(p.Actor, ai, p.Trait.Faction))
|
||||
if (p.Trait.Produce(p.Actor, unit, p.Trait.Faction))
|
||||
{
|
||||
FinishProduction();
|
||||
return true;
|
||||
@@ -120,12 +119,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
|
||||
public override int GetBuildTime(string unitString)
|
||||
{
|
||||
var ai = self.World.Map.Rules.Actors[unitString];
|
||||
var bi = ai.TraitInfoOrDefault<BuildableInfo>();
|
||||
if (bi == null)
|
||||
return 0;
|
||||
|
||||
return GetBuildTime(ai);
|
||||
return GetBuildTime(self.World.Map.Rules.Actors[unitString]);
|
||||
}
|
||||
|
||||
public override int GetBuildTime(ActorInfo unit)
|
||||
|
||||
@@ -232,7 +232,8 @@ namespace OpenRA.Mods.Common.Traits
|
||||
{
|
||||
while (queue.Count > 0 && BuildableItems().All(b => b.Name != queue[0].Item))
|
||||
{
|
||||
playerResources.GiveCash(queue[0].TotalCost - queue[0].RemainingCost); // refund what's been paid so far.
|
||||
// Refund what's been paid so far
|
||||
playerResources.GiveCash(queue[0].TotalCost - queue[0].RemainingCost);
|
||||
FinishProduction();
|
||||
}
|
||||
|
||||
@@ -287,7 +288,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
hasPlayedSound = Game.Sound.PlayNotification(rules, self.Owner, "Speech", Info.ReadyAudio, self.Owner.Faction.InternalName);
|
||||
else if (!isBuilding)
|
||||
{
|
||||
if (BuildUnit(order.TargetString))
|
||||
if (BuildUnit(unit))
|
||||
Game.Sound.PlayNotification(rules, self.Owner, "Speech", Info.ReadyAudio, self.Owner.Faction.InternalName);
|
||||
else if (!hasPlayedSound && time > 0)
|
||||
hasPlayedSound = Game.Sound.PlayNotification(rules, self.Owner, "Speech", Info.BlockedAudio, self.Owner.Faction.InternalName);
|
||||
@@ -309,11 +310,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
|
||||
public virtual int GetBuildTime(string unitString)
|
||||
{
|
||||
var unit = self.World.Map.Rules.Actors[unitString];
|
||||
if (unit == null || !unit.HasTraitInfo<BuildableInfo>())
|
||||
return 0;
|
||||
|
||||
return GetBuildTime(unit);
|
||||
return GetBuildTime(self.World.Map.Rules.Actors[unitString]);
|
||||
}
|
||||
|
||||
public virtual int GetBuildTime(ActorInfo unit)
|
||||
@@ -340,7 +337,9 @@ namespace OpenRA.Mods.Common.Traits
|
||||
else if (lastIndex == 0)
|
||||
{
|
||||
var item = queue[0];
|
||||
playerResources.GiveCash(item.TotalCost - item.RemainingCost); // refund what has been paid
|
||||
|
||||
// Refund what has been paid
|
||||
playerResources.GiveCash(item.TotalCost - item.RemainingCost);
|
||||
FinishProduction();
|
||||
}
|
||||
}
|
||||
@@ -365,17 +364,17 @@ namespace OpenRA.Mods.Common.Traits
|
||||
|
||||
// Builds a unit from the actor that holds this queue (1 queue per building)
|
||||
// Returns false if the unit can't be built
|
||||
protected virtual bool BuildUnit(string name)
|
||||
protected virtual bool BuildUnit(ActorInfo unit)
|
||||
{
|
||||
// Cannot produce if I'm dead
|
||||
if (!self.IsInWorld || self.IsDead)
|
||||
{
|
||||
CancelProduction(name, 1);
|
||||
CancelProduction(unit.Name, 1);
|
||||
return true;
|
||||
}
|
||||
|
||||
var sp = self.TraitsImplementing<Production>().FirstOrDefault(p => p.Info.Produces.Contains(Info.Type));
|
||||
if (sp != null && !self.IsDisabled() && sp.Produce(self, self.World.Map.Rules.Actors[name], Faction))
|
||||
if (sp != null && !self.IsDisabled() && sp.Produce(self, unit, Faction))
|
||||
{
|
||||
FinishProduction();
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user