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 OpenRA.Graphics;
14 using OpenRA.Traits;
15 
16 namespace OpenRA.Mods.Common.Traits
17 {
18 	public class ElevatedBridgeLayerInfo : ITraitInfo, Requires<DomainIndexInfo>, ILobbyCustomRulesIgnore
19 	{
20 		[Desc("Terrain type used by cells outside any elevated bridge footprint.")]
21 		public readonly string ImpassableTerrainType = "Impassable";
22 
Create(ActorInitializer init)23 		public object Create(ActorInitializer init) { return new ElevatedBridgeLayer(init.Self, this); }
24 	}
25 
26 	// For now this is mostly copies TerrainTunnelLayer. This will change once bridge destruction is implemented
27 	public class ElevatedBridgeLayer : ICustomMovementLayer, IWorldLoaded
28 	{
29 		readonly Map map;
30 		readonly CellLayer<WPos> cellCenters;
31 		readonly CellLayer<byte> terrainIndices;
32 		readonly HashSet<CPos> ends = new HashSet<CPos>();
33 		bool enabled;
34 
ElevatedBridgeLayer(Actor self, ElevatedBridgeLayerInfo info)35 		public ElevatedBridgeLayer(Actor self, ElevatedBridgeLayerInfo info)
36 		{
37 			map = self.World.Map;
38 			cellCenters = new CellLayer<WPos>(map);
39 			terrainIndices = new CellLayer<byte>(map);
40 			terrainIndices.Clear(map.Rules.TileSet.GetTerrainIndex(info.ImpassableTerrainType));
41 		}
42 
WorldLoaded(World world, WorldRenderer wr)43 		public void WorldLoaded(World world, WorldRenderer wr)
44 		{
45 			var domainIndex = world.WorldActor.Trait<DomainIndex>();
46 			var cellHeight = world.Map.CellHeightStep.Length;
47 			foreach (var tti in world.WorldActor.Info.TraitInfos<ElevatedBridgePlaceholderInfo>())
48 			{
49 				enabled = true;
50 
51 				var terrain = map.Rules.TileSet.GetTerrainIndex(tti.TerrainType);
52 				foreach (var c in tti.BridgeCells())
53 				{
54 					var uv = c.ToMPos(map);
55 					terrainIndices[uv] = terrain;
56 
57 					var pos = map.CenterOfCell(c);
58 					cellCenters[uv] = pos - new WVec(0, 0, pos.Z - cellHeight * tti.Height);
59 				}
60 
61 				var end = tti.EndCells();
62 				domainIndex.AddFixedConnection(end);
63 				foreach (var c in end)
64 				{
65 					// Need to explicitly set both default and tunnel layers, otherwise the .Contains check will fail
66 					ends.Add(new CPos(c.X, c.Y, 0));
67 					ends.Add(new CPos(c.X, c.Y, CustomMovementLayerType.ElevatedBridge));
68 				}
69 			}
70 		}
71 
ICustomMovementLayer.EnabledForActor(ActorInfo a, LocomotorInfo li)72 		bool ICustomMovementLayer.EnabledForActor(ActorInfo a, LocomotorInfo li) { return enabled; }
73 		byte ICustomMovementLayer.Index { get { return CustomMovementLayerType.ElevatedBridge; } }
74 		bool ICustomMovementLayer.InteractsWithDefaultLayer { get { return true; } }
75 		bool ICustomMovementLayer.ReturnToGroundLayerOnIdle { get { return false; } }
76 
ICustomMovementLayer.CenterOfCell(CPos cell)77 		WPos ICustomMovementLayer.CenterOfCell(CPos cell)
78 		{
79 			return cellCenters[cell];
80 		}
81 
ICustomMovementLayer.EntryMovementCost(ActorInfo a, LocomotorInfo li, CPos cell)82 		int ICustomMovementLayer.EntryMovementCost(ActorInfo a, LocomotorInfo li, CPos cell)
83 		{
84 			return ends.Contains(cell) ? 0 : int.MaxValue;
85 		}
86 
ICustomMovementLayer.ExitMovementCost(ActorInfo a, LocomotorInfo li, CPos cell)87 		int ICustomMovementLayer.ExitMovementCost(ActorInfo a, LocomotorInfo li, CPos cell)
88 		{
89 			return ends.Contains(cell) ? 0 : int.MaxValue;
90 		}
91 
ICustomMovementLayer.GetTerrainIndex(CPos cell)92 		byte ICustomMovementLayer.GetTerrainIndex(CPos cell)
93 		{
94 			return terrainIndices[cell];
95 		}
96 	}
97 }
98