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;
13 using System.Linq;
14 using OpenRA.Mods.Common.Traits;
15 using OpenRA.Traits;
16 
17 namespace OpenRA.Mods.Common.Lint
18 {
19 	class CheckDefaultVisibility : ILintRulesPass
20 	{
Run(Action<string> emitError, Action<string> emitWarning, Ruleset rules)21 		public void Run(Action<string> emitError, Action<string> emitWarning, Ruleset rules)
22 		{
23 			foreach (var actorInfo in rules.Actors)
24 			{
25 				var count = actorInfo.Value.TraitInfos<IDefaultVisibilityInfo>().Count();
26 
27 				if (count == 0)
28 					emitError("Actor type `{0}` does not define a default visibility type!".F(actorInfo.Key));
29 				else if (count > 1)
30 					emitError("Actor type `{0}` defines multiple default visibility types!".F(actorInfo.Key));
31 				else
32 				{
33 					var vis = actorInfo.Value.TraitInfoOrDefault<HiddenUnderShroudInfo>();
34 					if (vis != null && vis.Type == VisibilityType.Footprint)
35 					{
36 						var ios = actorInfo.Value.TraitInfoOrDefault<IOccupySpaceInfo>();
37 						if (ios == null)
38 							emitError("Actor type `{0}` defines VisibilityType.Footprint in `{1}` but has no IOccupySpace traits!".F(actorInfo.Key, vis.GetType()));
39 						else if (!ios.OccupiedCells(actorInfo.Value, CPos.Zero).Any())
40 							emitError("Actor type `{0}` defines VisibilityType.Footprint in `{1}` but does not have any footprint cells!".F(actorInfo.Key, vis.GetType()));
41 					}
42 				}
43 			}
44 		}
45 	}
46 }
47