From 2146dd29bbf0ce7152017ca701127afc96cec118 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Sat, 5 Oct 2019 16:51:10 +0100 Subject: [PATCH] Add priority levels to Exit. --- OpenRA.Mods.Common/Traits/Buildings/Exit.cs | 20 ++++++++++++++++--- .../Traits/Buildings/RallyPoint.cs | 8 +++++++- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/OpenRA.Mods.Common/Traits/Buildings/Exit.cs b/OpenRA.Mods.Common/Traits/Buildings/Exit.cs index fd32e4f00f..c2bd96ceb2 100644 --- a/OpenRA.Mods.Common/Traits/Buildings/Exit.cs +++ b/OpenRA.Mods.Common/Traits/Buildings/Exit.cs @@ -32,6 +32,9 @@ namespace OpenRA.Mods.Common.Traits [Desc("Number of ticks to wait before moving into the world.")] public readonly int ExitDelay = 0; + [Desc("Exits with larger priorities will be used before lower priorities.")] + public readonly int Priority = 1; + public override object Create(ActorInitializer init) { return new Exit(init, this); } } @@ -46,7 +49,8 @@ namespace OpenRA.Mods.Common.Traits public static Exit FirstExitOrDefault(this Actor actor, string productionType = null) { var all = actor.TraitsImplementing() - .Where(Exts.IsTraitEnabled); + .Where(Exts.IsTraitEnabled) + .OrderBy(e => e.Info.Priority); if (string.IsNullOrEmpty(productionType)) return all.FirstOrDefault(e => e.Info.ProductionTypes.Count == 0); @@ -71,8 +75,18 @@ namespace OpenRA.Mods.Common.Traits if (!allOfType.Any()) return null; - var shuffled = allOfType.Shuffle(world.SharedRandom); - return p != null ? shuffled.FirstOrDefault(p) : shuffled.First(); + foreach (var g in allOfType.GroupBy(e => e.Info.Priority)) + { + var shuffled = g.Shuffle(world.SharedRandom); + if (p == null) + return shuffled.First(); + + var valid = shuffled.FirstOrDefault(p); + if (valid != null) + return valid; + } + + return null; } } } diff --git a/OpenRA.Mods.Common/Traits/Buildings/RallyPoint.cs b/OpenRA.Mods.Common/Traits/Buildings/RallyPoint.cs index 1e6f62c699..f66ea71018 100644 --- a/OpenRA.Mods.Common/Traits/Buildings/RallyPoint.cs +++ b/OpenRA.Mods.Common/Traits/Buildings/RallyPoint.cs @@ -70,7 +70,13 @@ namespace OpenRA.Mods.Common.Traits void INotifyCreated.Created(Actor self) { - self.World.Add(new RallyPointIndicator(self, this, self.Info.TraitInfos().ToArray())); + // Display only the first level of priority + var priorityExits = self.Info.TraitInfos() + .GroupBy(e => e.Priority) + .FirstOrDefault(); + + var exits = priorityExits != null ? priorityExits.ToArray() : new ExitInfo[0]; + self.World.Add(new RallyPointIndicator(self, this, exits)); } public void OnOwnerChanged(Actor self, Player oldOwner, Player newOwner)