From 7d8ee64ce5d964884fe8f215e20f2754edf58135 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Sat, 13 Jun 2015 20:53:39 +0100 Subject: [PATCH] Reorganize shroud cell queries. This is in preparation for a future PR that will remove GetVisOrigins. --- OpenRA.Game/Traits/World/Shroud.cs | 29 ++++++++++--------- OpenRA.Mods.Common/Traits/RevealsShroud.cs | 3 +- .../Traits/World/MPStartLocations.cs | 22 ++++++++++---- 3 files changed, 34 insertions(+), 20 deletions(-) diff --git a/OpenRA.Game/Traits/World/Shroud.cs b/OpenRA.Game/Traits/World/Shroud.cs index 4ced59c1ca..473a1731b4 100644 --- a/OpenRA.Game/Traits/World/Shroud.cs +++ b/OpenRA.Game/Traits/World/Shroud.cs @@ -72,21 +72,20 @@ namespace OpenRA.Traits Hash += 1; } - static CPos[] FindVisibleTiles(Actor actor, WRange range) + public static IEnumerable CellsInRange(Map map, WPos pos, WRange range) { - return GetVisOrigins(actor).SelectMany(o => FindVisibleTiles(actor.World, o, range)).Distinct().ToArray(); + var r = (range.Range + 1023) / 1024; + var limit = range.RangeSquared; + var cell = map.CellContaining(pos); + + foreach (var c in map.FindTilesInCircle(cell, r, true)) + if ((map.CenterOfCell(c) - pos).HorizontalLengthSquared <= limit) + yield return c; } - public static IEnumerable FindVisibleTiles(World world, CPos position, WRange radius) + public static IEnumerable CellsInRange(Map map, CPos cell, WRange range) { - var map = world.Map; - var r = (radius.Range + 1023) / 1024; - var limit = radius.RangeSquared; - var pos = map.CenterOfCell(position); - - foreach (var cell in map.FindTilesInCircle(position, r, true)) - if ((map.CenterOfCell(cell) - pos).HorizontalLengthSquared <= limit) - yield return cell; + return CellsInRange(map, map.CenterOfCell(cell), range); } public void AddVisibility(Actor a, CPos[] visible) @@ -184,6 +183,8 @@ namespace OpenRA.Traits } } + // TODO: Actor vis will be split into separate cases for + // "cells that I reveal from" and "cells that reveal me" public static IEnumerable GetVisOrigins(Actor a) { var ios = a.OccupiesSpace; @@ -197,10 +198,10 @@ namespace OpenRA.Traits return new[] { a.World.Map.CellContaining(a.CenterPosition) }; } - public void Explore(World world, CPos center, WRange range) + public void Explore(World world, IEnumerable cells) { - var changed = new List(); - foreach (var c in FindVisibleTiles(world, center, range)) + var changed = new HashSet(); + foreach (var c in cells) { if (!explored[c]) { diff --git a/OpenRA.Mods.Common/Traits/RevealsShroud.cs b/OpenRA.Mods.Common/Traits/RevealsShroud.cs index d51a22c533..0c5ba61cf0 100644 --- a/OpenRA.Mods.Common/Traits/RevealsShroud.cs +++ b/OpenRA.Mods.Common/Traits/RevealsShroud.cs @@ -46,12 +46,13 @@ namespace OpenRA.Mods.Common.Traits CPos[] Cells(Actor self) { + var map = self.World.Map; var range = Range; if (range == WRange.Zero) return NoCells; return Shroud.GetVisOrigins(self) - .SelectMany(o => Shroud.FindVisibleTiles(self.World, o, range)) + .SelectMany(c => Shroud.CellsInRange(map, c, range)) .Distinct().ToArray(); } diff --git a/OpenRA.Mods.Common/Traits/World/MPStartLocations.cs b/OpenRA.Mods.Common/Traits/World/MPStartLocations.cs index db50067aae..3cd90e25ad 100644 --- a/OpenRA.Mods.Common/Traits/World/MPStartLocations.cs +++ b/OpenRA.Mods.Common/Traits/World/MPStartLocations.cs @@ -17,14 +17,23 @@ using OpenRA.Traits; namespace OpenRA.Mods.Common.Traits { - public class MPStartLocationsInfo : TraitInfo + public class MPStartLocationsInfo : ITraitInfo { public readonly WRange InitialExploreRange = WRange.FromCells(5); + + public virtual object Create(ActorInitializer init) { return new MPStartLocations(this); } } public class MPStartLocations : IWorldLoaded { - public Dictionary Start = new Dictionary(); + readonly MPStartLocationsInfo info; + + public readonly Dictionary Start = new Dictionary(); + + public MPStartLocations(MPStartLocationsInfo info) + { + this.info = info; + } public void WorldLoaded(World world, WorldRenderer wr) { @@ -52,15 +61,18 @@ namespace OpenRA.Mods.Common.Traits } // Explore allied shroud - var explore = world.WorldActor.Info.Traits.Get().InitialExploreRange; + var map = world.Map; foreach (var p in Start.Keys) + { + var cells = Shroud.CellsInRange(map, Start[p], info.InitialExploreRange); foreach (var q in world.Players) if (p.IsAlliedWith(q)) - q.Shroud.Explore(world, Start[p], explore); + q.Shroud.Explore(world, cells); + } // Set viewport if (world.LocalPlayer != null && Start.ContainsKey(world.LocalPlayer)) - wr.Viewport.Center(world.Map.CenterOfCell(Start[world.LocalPlayer])); + wr.Viewport.Center(map.CenterOfCell(Start[world.LocalPlayer])); } static Player FindPlayerInSlot(World world, string pr)