Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added HpPerStep to Repairable trait and using this value in Repair activity #15111

Merged
merged 1 commit into from May 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion OpenRA.Mods.Common/Activities/Repair.cs
Expand Up @@ -23,6 +23,7 @@ public class Repair : Activity
readonly RepairsUnits[] allRepairsUnits;
readonly Target host;
readonly WDist closeEnough;
readonly Repairable repairable;

int remainingTicks;
bool played = false;
Expand All @@ -33,6 +34,7 @@ public Repair(Actor self, Actor host, WDist closeEnough)
this.closeEnough = closeEnough;
allRepairsUnits = host.TraitsImplementing<RepairsUnits>().ToArray();
health = self.TraitOrDefault<Health>();
repairable = self.TraitOrDefault<Repairable>();
}

protected override void OnFirstRun(Actor self)
Expand Down Expand Up @@ -97,7 +99,7 @@ public override Activity Tick(Actor self)
if (remainingTicks == 0)
{
var unitCost = self.Info.TraitInfo<ValuedInfo>().Cost;
var hpToRepair = repairsUnits.Info.HpPerStep;
var hpToRepair = repairable != null && repairable.Info.HpPerStep > 0 ? repairable.Info.HpPerStep : repairsUnits.Info.HpPerStep;

// Cast to long to avoid overflow when multiplying by the health
var cost = Math.Max(1, (int)(((long)hpToRepair * unitCost * repairsUnits.Info.ValuePercentage) / (health.MaxHP * 100L)));
Expand Down
15 changes: 9 additions & 6 deletions OpenRA.Mods.Common/Traits/Repairable.cs
Expand Up @@ -26,19 +26,22 @@ class RepairableInfo : ITraitInfo, Requires<HealthInfo>, Requires<IMoveInfo>

[VoiceReference] public readonly string Voice = "Action";

[Desc("The amount the unit will be repaired at each step. Use -1 for fallback behavior where HpPerStep from RepairUnit trait will be used.")]
public readonly int HpPerStep = -1;

public virtual object Create(ActorInitializer init) { return new Repairable(init.Self, this); }
}

class Repairable : IIssueOrder, IResolveOrder, IOrderVoice
{
readonly RepairableInfo info;
public readonly RepairableInfo Info;
readonly Health health;
readonly IMove movement;
readonly AmmoPool[] ammoPools;

public Repairable(Actor self, RepairableInfo info)
{
this.info = info;
Info = info;
health = self.Trait<Health>();
movement = self.Trait<IMove>();
ammoPools = self.TraitsImplementing<AmmoPool>().ToArray();
Expand All @@ -62,12 +65,12 @@ public Order IssueOrder(Actor self, IOrderTargeter order, Target target, bool qu

bool CanRepairAt(Actor target)
{
return info.RepairBuildings.Contains(target.Info.Name);
return Info.RepairBuildings.Contains(target.Info.Name);
}

bool CanRearmAt(Actor target)
{
return info.RepairBuildings.Contains(target.Info.Name);
return Info.RepairBuildings.Contains(target.Info.Name);
}

bool CanRepair()
Expand All @@ -82,7 +85,7 @@ bool CanRearm()

public string VoicePhraseForOrder(Actor self, Order order)
{
return (order.OrderString == "Repair" && CanRepair()) ? info.Voice : null;
return (order.OrderString == "Repair" && CanRepair()) ? Info.Voice : null;
}

public void ResolveOrder(Actor self, Order order)
Expand Down Expand Up @@ -133,7 +136,7 @@ public Actor FindRepairBuilding(Actor self)
var repairBuilding = self.World.ActorsWithTrait<RepairsUnits>()
.Where(a => !a.Actor.IsDead && a.Actor.IsInWorld
&& a.Actor.Owner.IsAlliedWith(self.Owner) &&
info.RepairBuildings.Contains(a.Actor.Info.Name))
Info.RepairBuildings.Contains(a.Actor.Info.Name))
.OrderBy(p => (self.Location - p.Actor.Location).LengthSquared);

// Worst case FirstOrDefault() will return a TraitPair<null, null>, which is OK.
Expand Down