Avoid unnecessary lookups in the production logic

by improving the GetBuildTime method and moving lookups around.
This commit is contained in:
abcdefg30
2016-01-21 16:18:14 +01:00
parent 6272d79072
commit 17daac11a1
3 changed files with 16 additions and 7 deletions

View File

@@ -125,14 +125,19 @@ namespace OpenRA.Mods.Common.Traits
if (bi == null)
return 0;
return GetBuildTime(ai);
}
public override int GetBuildTime(ActorInfo unit)
{
if (self.World.AllowDevCommands && self.Owner.PlayerActor.Trait<DeveloperMode>().FastBuild)
return 0;
var time = (int)(ai.GetBuildTime() * Info.BuildSpeed);
var time = (int)(unit.GetBuildTime() * Info.BuildSpeed);
if (info.SpeedUp)
{
var type = bi.BuildAtProductionType ?? info.Type;
var type = unit.TraitInfo<BuildableInfo>().BuildAtProductionType ?? info.Type;
var selfsameProductionsCount = self.World.ActorsWithTrait<Production>()
.Count(p => p.Actor.Owner == self.Owner && p.Trait.Info.Produces.Contains(type));

View File

@@ -256,9 +256,6 @@ namespace OpenRA.Mods.Common.Traits
if (!bi.Queue.Contains(Info.Type))
return;
var cost = unit.HasTraitInfo<ValuedInfo>() ? unit.TraitInfo<ValuedInfo>().Cost : 0;
var time = GetBuildTime(order.TargetString);
// You can't build that
if (BuildableItems().All(b => b.Name != order.TargetString))
return;
@@ -275,6 +272,9 @@ namespace OpenRA.Mods.Common.Traits
return;
}
var valued = unit.TraitInfoOrDefault<ValuedInfo>();
var cost = valued != null ? valued.Cost : 0;
var time = GetBuildTime(unit);
var amountToBuild = Math.Min(fromLimit, order.ExtraData);
for (var n = 0; n < amountToBuild; n++)
{
@@ -313,11 +313,15 @@ namespace OpenRA.Mods.Common.Traits
if (unit == null || !unit.HasTraitInfo<BuildableInfo>())
return 0;
return GetBuildTime(unit);
}
public virtual int GetBuildTime(ActorInfo unit)
{
if (self.World.AllowDevCommands && self.Owner.PlayerActor.Trait<DeveloperMode>().FastBuild)
return 0;
var time = unit.GetBuildTime() * Info.BuildSpeed;
return (int)time;
}

View File

@@ -81,7 +81,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
powerIcon.IsVisible = () => power != 0;
var lowpower = pm.PowerState != PowerState.Normal;
var time = palette.CurrentQueue == null ? 0 : palette.CurrentQueue.GetBuildTime(actor.Name)
var time = palette.CurrentQueue == null ? 0 : palette.CurrentQueue.GetBuildTime(actor)
* (lowpower ? palette.CurrentQueue.Info.LowPowerSlowdown : 1);
var timeString = WidgetUtils.FormatTime(time, world.Timestep);
timeLabel.GetText = () => timeString;