1 #region Copyright & License Information
2 /*
3  * Copyright 2007-2020 The OpenRA Developers (see AUTHORS)
4  * This file is part of OpenRA, which is free software. It is made
5  * available to you under the terms of the GNU General Public License
6  * as published by the Free Software Foundation, either version 3 of
7  * the License, or (at your option) any later version. For more
8  * information, see COPYING.
9  */
10 #endregion
11 
12 using System.Collections.Generic;
13 using System.Linq;
14 
15 namespace OpenRA.Mods.Common.Traits
16 {
17 	[Desc("Can be used to make a unit partly uncontrollable by the player.")]
18 	public class RejectsOrdersInfo : ConditionalTraitInfo
19 	{
20 		[Desc("Explicit list of rejected orders. Leave empty to reject all minus those listed under Except.")]
21 		public readonly HashSet<string> Reject = new HashSet<string>();
22 
23 		[Desc("List of orders that should *not* be rejected.",
24 			"Also overrides other instances of this trait's Reject fields.")]
25 		public readonly HashSet<string> Except = new HashSet<string>();
26 
Create(ActorInitializer init)27 		public override object Create(ActorInitializer init) { return new RejectsOrders(this); }
28 	}
29 
30 	public class RejectsOrders : ConditionalTrait<RejectsOrdersInfo>
31 	{
32 		public HashSet<string> Reject { get { return Info.Reject; } }
33 		public HashSet<string> Except { get { return Info.Except; } }
34 
RejectsOrders(RejectsOrdersInfo info)35 		public RejectsOrders(RejectsOrdersInfo info)
36 			: base(info) { }
37 	}
38 
39 	public static class RejectsOrdersExts
40 	{
AcceptsOrder(this Actor self, string orderString)41 		public static bool AcceptsOrder(this Actor self, string orderString)
42 		{
43 			var rejectsOrdersTraits = self.TraitsImplementing<RejectsOrders>().Where(Exts.IsTraitEnabled).ToArray();
44 			if (!rejectsOrdersTraits.Any())
45 				return true;
46 
47 			var reject = rejectsOrdersTraits.SelectMany(t => t.Reject);
48 			var except = rejectsOrdersTraits.SelectMany(t => t.Except);
49 
50 			return except.Contains(orderString) || (reject.Any() && !reject.Contains(orderString));
51 		}
52 	}
53 }
54