Enabled multiple timed upgrades from single source.

This makes the UpgradeManager support requests to grant a timed
upgrade multiple times from a single source.
GrantUpgradeWarhead modified to take advantage of this.
This commit is contained in:
Matija Hustić
2015-10-09 20:56:59 +02:00
parent 03c80fb9c6
commit c55141c91c
2 changed files with 12 additions and 12 deletions

View File

@@ -85,13 +85,13 @@ namespace OpenRA.Mods.Common.Traits
}); });
} }
/// <summary>Upgrade level increments are limited to one per source, i.e., if a single source /// <summary>Upgrade level increments are limited to dupesAllowed per source, i.e., if a single
/// attempts granting multiple upgrades, they will not accumulate. They will replace each other /// source attempts granting more upgrades than dupesAllowed, they will not accumulate. They will
/// instead, leaving only the last granted upgrade active. An upgrade from each new distinct /// replace each other instead, leaving only the most recently granted upgrade active. Each new
/// source will increment the upgrade's level until AcceptsUpgrade starts returning false. Then, /// upgrade granting request will increment the upgrade's level until AcceptsUpgrade starts
/// when no new levels are accepted, the upgrade source with the shortest remaining upgrade /// returning false. Then, when no new levels are accepted, the upgrade source with the shortest
/// duration will be replaced by the new source.</summary> /// remaining upgrade duration will be replaced by the new source.</summary>
public void GrantTimedUpgrade(Actor self, string upgrade, int duration, object source = null) public void GrantTimedUpgrade(Actor self, string upgrade, int duration, object source = null, int dupesAllowed = 1)
{ {
var timed = timedUpgrades.FirstOrDefault(u => u.Upgrade == upgrade); var timed = timedUpgrades.FirstOrDefault(u => u.Upgrade == upgrade);
if (timed == null) if (timed == null)
@@ -102,17 +102,17 @@ namespace OpenRA.Mods.Common.Traits
return; return;
} }
var src = timed.Sources.FirstOrDefault(s => s.Source == source); var srcs = timed.Sources.Where(s => s.Source == source);
if (src == null) if (srcs.Count() < dupesAllowed)
{ {
timed.Sources.Add(new TimedUpgrade.UpgradeSource(duration, source)); timed.Sources.Add(new TimedUpgrade.UpgradeSource(duration, source));
if (AcceptsUpgrade(self, upgrade)) if (AcceptsUpgrade(self, upgrade))
GrantUpgrade(self, upgrade, timed); GrantUpgrade(self, upgrade, timed);
else else
timed.Sources.Remove(timed.Sources.OrderByDescending(s => s.Remaining).Last()); timed.Sources.Remove(timed.Sources.MinBy(s => s.Remaining));
} }
else else
src.Remaining = duration; srcs.MinBy(s => s.Remaining).Remaining = duration;
timed.Remaining = Math.Max(duration, timed.Remaining); timed.Remaining = Math.Max(duration, timed.Remaining);
} }

View File

@@ -48,7 +48,7 @@ namespace OpenRA.Mods.Common.Warheads
if (Duration > 0) if (Duration > 0)
{ {
if (um.AcknowledgesUpgrade(a, u)) if (um.AcknowledgesUpgrade(a, u))
um.GrantTimedUpgrade(a, u, Duration, firedBy); um.GrantTimedUpgrade(a, u, Duration, firedBy, Upgrades.Count(upg => upg == u));
} }
else else
{ {