PageRenderTime 90ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/OpenRA.Mods.RA/Traits/SupportPowers/ChronoshiftPower.cs

http://github.com/OpenRA/OpenRA
C# | 313 lines | 241 code | 53 blank | 19 comment | 26 complexity | e480c2c94f163c1255cea0675cdd36ff MD5 | raw file
Possible License(s): GPL-3.0
  1. #region Copyright & License Information
  2. /*
  3. * Copyright 2007-2016 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. using System.Collections.Generic;
  12. using System.Drawing;
  13. using System.Linq;
  14. using OpenRA.Graphics;
  15. using OpenRA.Mods.Common.Graphics;
  16. using OpenRA.Mods.Common.Traits;
  17. using OpenRA.Mods.RA.Activities;
  18. using OpenRA.Traits;
  19. namespace OpenRA.Mods.RA.Traits
  20. {
  21. class ChronoshiftPowerInfo : SupportPowerInfo
  22. {
  23. [Desc("Target actor selection radius in cells.")]
  24. public readonly int Range = 1;
  25. [Desc("Seconds until returning after teleportation.")]
  26. public readonly int Duration = 30;
  27. [PaletteReference] public readonly string TargetOverlayPalette = TileSet.TerrainPaletteInternalName;
  28. public readonly string OverlaySpriteGroup = "overlay";
  29. [SequenceReference("OverlaySpriteGroup", true)] public readonly string ValidTileSequencePrefix = "target-valid-";
  30. [SequenceReference("OverlaySpriteGroup")] public readonly string InvalidTileSequence = "target-invalid";
  31. [SequenceReference("OverlaySpriteGroup")] public readonly string SourceTileSequence = "target-select";
  32. public readonly bool KillCargo = true;
  33. [Desc("Cursor sequence to use when selecting targets for the chronoshift.")]
  34. public readonly string SelectionCursor = "chrono-select";
  35. [Desc("Cursor sequence to use when targeting an area for the chronoshift.")]
  36. public readonly string TargetCursor = "chrono-target";
  37. [Desc("Cursor sequence to use when the targeted area is blocked.")]
  38. public readonly string TargetBlockedCursor = "move-blocked";
  39. public override object Create(ActorInitializer init) { return new ChronoshiftPower(init.Self, this); }
  40. }
  41. class ChronoshiftPower : SupportPower
  42. {
  43. public ChronoshiftPower(Actor self, ChronoshiftPowerInfo info) : base(self, info) { }
  44. public override void SelectTarget(Actor self, string order, SupportPowerManager manager)
  45. {
  46. Game.Sound.PlayToPlayer(manager.Self.Owner, Info.SelectTargetSound);
  47. self.World.OrderGenerator = new SelectChronoshiftTarget(Self.World, order, manager, this);
  48. }
  49. public override void Activate(Actor self, Order order, SupportPowerManager manager)
  50. {
  51. base.Activate(self, order, manager);
  52. foreach (var target in UnitsInRange(order.ExtraLocation))
  53. {
  54. var cs = target.Trait<Chronoshiftable>();
  55. var targetCell = target.Location + (order.TargetLocation - order.ExtraLocation);
  56. var cpi = Info as ChronoshiftPowerInfo;
  57. if (self.Owner.Shroud.IsExplored(targetCell) && cs.CanChronoshiftTo(target, targetCell))
  58. cs.Teleport(target, targetCell, cpi.Duration * 25, cpi.KillCargo, self);
  59. }
  60. }
  61. public IEnumerable<Actor> UnitsInRange(CPos xy)
  62. {
  63. var range = ((ChronoshiftPowerInfo)Info).Range;
  64. var tiles = Self.World.Map.FindTilesInCircle(xy, range);
  65. var units = new HashSet<Actor>();
  66. foreach (var t in tiles)
  67. units.UnionWith(Self.World.ActorMap.GetActorsAt(t));
  68. return units.Where(a => a.Info.HasTraitInfo<ChronoshiftableInfo>() &&
  69. !a.TraitsImplementing<IPreventsTeleport>().Any(condition => condition.PreventsTeleport(a)));
  70. }
  71. public bool SimilarTerrain(CPos xy, CPos sourceLocation)
  72. {
  73. if (!Self.Owner.Shroud.IsExplored(xy))
  74. return false;
  75. var range = ((ChronoshiftPowerInfo)Info).Range;
  76. var sourceTiles = Self.World.Map.FindTilesInCircle(xy, range);
  77. var destTiles = Self.World.Map.FindTilesInCircle(sourceLocation, range);
  78. using (var se = sourceTiles.GetEnumerator())
  79. using (var de = destTiles.GetEnumerator())
  80. while (se.MoveNext() && de.MoveNext())
  81. {
  82. var a = se.Current;
  83. var b = de.Current;
  84. if (!Self.Owner.Shroud.IsExplored(a) || !Self.Owner.Shroud.IsExplored(b))
  85. return false;
  86. if (Self.World.Map.GetTerrainIndex(a) != Self.World.Map.GetTerrainIndex(b))
  87. return false;
  88. }
  89. return true;
  90. }
  91. class SelectChronoshiftTarget : IOrderGenerator
  92. {
  93. readonly ChronoshiftPower power;
  94. readonly int range;
  95. readonly Sprite tile;
  96. readonly SupportPowerManager manager;
  97. readonly string order;
  98. public SelectChronoshiftTarget(World world, string order, SupportPowerManager manager, ChronoshiftPower power)
  99. {
  100. // Clear selection if using Left-Click Orders
  101. if (Game.Settings.Game.UseClassicMouseStyle)
  102. manager.Self.World.Selection.Clear();
  103. this.manager = manager;
  104. this.order = order;
  105. this.power = power;
  106. var info = (ChronoshiftPowerInfo)power.Info;
  107. range = info.Range;
  108. tile = world.Map.Rules.Sequences.GetSequence(info.OverlaySpriteGroup, info.SourceTileSequence).GetSprite(0);
  109. }
  110. public IEnumerable<Order> Order(World world, CPos cell, int2 worldPixel, MouseInput mi)
  111. {
  112. world.CancelInputMode();
  113. if (mi.Button == MouseButton.Left)
  114. world.OrderGenerator = new SelectDestination(world, order, manager, power, cell);
  115. yield break;
  116. }
  117. public void Tick(World world)
  118. {
  119. // Cancel the OG if we can't use the power
  120. if (!manager.Powers.ContainsKey(order))
  121. world.CancelInputMode();
  122. }
  123. public IEnumerable<IRenderable> RenderAfterWorld(WorldRenderer wr, World world)
  124. {
  125. var xy = wr.Viewport.ViewToWorld(Viewport.LastMousePos);
  126. var targetUnits = power.UnitsInRange(xy).Where(a => !world.FogObscures(a));
  127. foreach (var unit in targetUnits)
  128. if (manager.Self.Owner.CanTargetActor(unit))
  129. yield return new SelectionBoxRenderable(unit, Color.Red);
  130. }
  131. public IEnumerable<IRenderable> Render(WorldRenderer wr, World world)
  132. {
  133. var xy = wr.Viewport.ViewToWorld(Viewport.LastMousePos);
  134. var tiles = world.Map.FindTilesInCircle(xy, range);
  135. var palette = wr.Palette(((ChronoshiftPowerInfo)power.Info).TargetOverlayPalette);
  136. foreach (var t in tiles)
  137. yield return new SpriteRenderable(tile, wr.World.Map.CenterOfCell(t), WVec.Zero, -511, palette, 1f, true);
  138. }
  139. public string GetCursor(World world, CPos cell, int2 worldPixel, MouseInput mi)
  140. {
  141. return ((ChronoshiftPowerInfo)power.Info).SelectionCursor;
  142. }
  143. }
  144. class SelectDestination : IOrderGenerator
  145. {
  146. readonly ChronoshiftPower power;
  147. readonly CPos sourceLocation;
  148. readonly int range;
  149. readonly Sprite validTile, invalidTile, sourceTile;
  150. readonly SupportPowerManager manager;
  151. readonly string order;
  152. public SelectDestination(World world, string order, SupportPowerManager manager, ChronoshiftPower power, CPos sourceLocation)
  153. {
  154. this.manager = manager;
  155. this.order = order;
  156. this.power = power;
  157. this.sourceLocation = sourceLocation;
  158. var info = (ChronoshiftPowerInfo)power.Info;
  159. range = info.Range;
  160. var tileset = world.Map.Tileset.ToLowerInvariant();
  161. validTile = world.Map.Rules.Sequences.GetSequence(info.OverlaySpriteGroup, info.ValidTileSequencePrefix + tileset).GetSprite(0);
  162. invalidTile = world.Map.Rules.Sequences.GetSequence(info.OverlaySpriteGroup, info.InvalidTileSequence).GetSprite(0);
  163. sourceTile = world.Map.Rules.Sequences.GetSequence(info.OverlaySpriteGroup, info.SourceTileSequence).GetSprite(0);
  164. }
  165. public IEnumerable<Order> Order(World world, CPos cell, int2 worldPixel, MouseInput mi)
  166. {
  167. if (mi.Button == MouseButton.Right)
  168. {
  169. world.CancelInputMode();
  170. yield break;
  171. }
  172. var ret = OrderInner(cell).FirstOrDefault();
  173. if (ret == null)
  174. yield break;
  175. world.CancelInputMode();
  176. yield return ret;
  177. }
  178. IEnumerable<Order> OrderInner(CPos xy)
  179. {
  180. // Cannot chronoshift into unexplored location
  181. if (IsValidTarget(xy))
  182. yield return new Order(order, manager.Self, false)
  183. {
  184. TargetLocation = xy,
  185. ExtraLocation = sourceLocation,
  186. SuppressVisualFeedback = true
  187. };
  188. }
  189. public void Tick(World world)
  190. {
  191. // Cancel the OG if we can't use the power
  192. if (!manager.Powers.ContainsKey(order))
  193. world.CancelInputMode();
  194. }
  195. public IEnumerable<IRenderable> RenderAfterWorld(WorldRenderer wr, World world)
  196. {
  197. foreach (var unit in power.UnitsInRange(sourceLocation))
  198. if (manager.Self.Owner.CanTargetActor(unit))
  199. yield return new SelectionBoxRenderable(unit, Color.Red);
  200. }
  201. public IEnumerable<IRenderable> Render(WorldRenderer wr, World world)
  202. {
  203. var xy = wr.Viewport.ViewToWorld(Viewport.LastMousePos);
  204. var palette = wr.Palette(power.Info.IconPalette);
  205. // Source tiles
  206. foreach (var t in world.Map.FindTilesInCircle(sourceLocation, range))
  207. yield return new SpriteRenderable(sourceTile, wr.World.Map.CenterOfCell(t), WVec.Zero, -511, palette, 1f, true);
  208. // Destination tiles
  209. foreach (var t in world.Map.FindTilesInCircle(xy, range))
  210. yield return new SpriteRenderable(sourceTile, wr.World.Map.CenterOfCell(t), WVec.Zero, -511, palette, 1f, true);
  211. // Unit previews
  212. foreach (var unit in power.UnitsInRange(sourceLocation))
  213. {
  214. var offset = world.Map.CenterOfCell(xy) - world.Map.CenterOfCell(sourceLocation);
  215. if (manager.Self.Owner.CanTargetActor(unit))
  216. foreach (var r in unit.Render(wr))
  217. yield return r.OffsetBy(offset);
  218. }
  219. // Unit tiles
  220. foreach (var unit in power.UnitsInRange(sourceLocation))
  221. {
  222. if (manager.Self.Owner.CanTargetActor(unit))
  223. {
  224. var targetCell = unit.Location + (xy - sourceLocation);
  225. var canEnter = manager.Self.Owner.Shroud.IsExplored(targetCell) &&
  226. unit.Trait<Chronoshiftable>().CanChronoshiftTo(unit, targetCell);
  227. var tile = canEnter ? validTile : invalidTile;
  228. yield return new SpriteRenderable(tile, wr.World.Map.CenterOfCell(targetCell), WVec.Zero, -511, palette, 1f, true);
  229. }
  230. }
  231. }
  232. bool IsValidTarget(CPos xy)
  233. {
  234. var canTeleport = false;
  235. foreach (var unit in power.UnitsInRange(sourceLocation))
  236. {
  237. var targetCell = unit.Location + (xy - sourceLocation);
  238. if (manager.Self.Owner.Shroud.IsExplored(targetCell) && unit.Trait<Chronoshiftable>().CanChronoshiftTo(unit, targetCell))
  239. {
  240. canTeleport = true;
  241. break;
  242. }
  243. }
  244. if (!canTeleport)
  245. {
  246. // Check the terrain types. This will allow chronoshifts to occur on empty terrain to terrain of
  247. // a similar type. This also keeps the cursor from changing in non-visible property, alerting the
  248. // chronoshifter of enemy unit presence
  249. canTeleport = power.SimilarTerrain(sourceLocation, xy);
  250. }
  251. return canTeleport;
  252. }
  253. public string GetCursor(World world, CPos cell, int2 worldPixel, MouseInput mi)
  254. {
  255. var powerInfo = (ChronoshiftPowerInfo)power.Info;
  256. return IsValidTarget(cell) ? powerInfo.TargetCursor : powerInfo.TargetBlockedCursor;
  257. }
  258. }
  259. }
  260. }