Reorganize shroud cell queries.

This is in preparation for a future PR that will
remove GetVisOrigins.
This commit is contained in:
Paul Chote
2015-06-13 20:53:39 +01:00
parent 01dc7edbe9
commit 7d8ee64ce5
3 changed files with 34 additions and 20 deletions

View File

@@ -72,21 +72,20 @@ namespace OpenRA.Traits
Hash += 1; Hash += 1;
} }
static CPos[] FindVisibleTiles(Actor actor, WRange range) public static IEnumerable<CPos> 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<CPos> FindVisibleTiles(World world, CPos position, WRange radius) public static IEnumerable<CPos> CellsInRange(Map map, CPos cell, WRange range)
{ {
var map = world.Map; return CellsInRange(map, map.CenterOfCell(cell), range);
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;
} }
public void AddVisibility(Actor a, CPos[] visible) 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<CPos> GetVisOrigins(Actor a) public static IEnumerable<CPos> GetVisOrigins(Actor a)
{ {
var ios = a.OccupiesSpace; var ios = a.OccupiesSpace;
@@ -197,10 +198,10 @@ namespace OpenRA.Traits
return new[] { a.World.Map.CellContaining(a.CenterPosition) }; return new[] { a.World.Map.CellContaining(a.CenterPosition) };
} }
public void Explore(World world, CPos center, WRange range) public void Explore(World world, IEnumerable<CPos> cells)
{ {
var changed = new List<CPos>(); var changed = new HashSet<CPos>();
foreach (var c in FindVisibleTiles(world, center, range)) foreach (var c in cells)
{ {
if (!explored[c]) if (!explored[c])
{ {

View File

@@ -46,12 +46,13 @@ namespace OpenRA.Mods.Common.Traits
CPos[] Cells(Actor self) CPos[] Cells(Actor self)
{ {
var map = self.World.Map;
var range = Range; var range = Range;
if (range == WRange.Zero) if (range == WRange.Zero)
return NoCells; return NoCells;
return Shroud.GetVisOrigins(self) return Shroud.GetVisOrigins(self)
.SelectMany(o => Shroud.FindVisibleTiles(self.World, o, range)) .SelectMany(c => Shroud.CellsInRange(map, c, range))
.Distinct().ToArray(); .Distinct().ToArray();
} }

View File

@@ -17,14 +17,23 @@ using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits namespace OpenRA.Mods.Common.Traits
{ {
public class MPStartLocationsInfo : TraitInfo<MPStartLocations> public class MPStartLocationsInfo : ITraitInfo
{ {
public readonly WRange InitialExploreRange = WRange.FromCells(5); public readonly WRange InitialExploreRange = WRange.FromCells(5);
public virtual object Create(ActorInitializer init) { return new MPStartLocations(this); }
} }
public class MPStartLocations : IWorldLoaded public class MPStartLocations : IWorldLoaded
{ {
public Dictionary<Player, CPos> Start = new Dictionary<Player, CPos>(); readonly MPStartLocationsInfo info;
public readonly Dictionary<Player, CPos> Start = new Dictionary<Player, CPos>();
public MPStartLocations(MPStartLocationsInfo info)
{
this.info = info;
}
public void WorldLoaded(World world, WorldRenderer wr) public void WorldLoaded(World world, WorldRenderer wr)
{ {
@@ -52,15 +61,18 @@ namespace OpenRA.Mods.Common.Traits
} }
// Explore allied shroud // Explore allied shroud
var explore = world.WorldActor.Info.Traits.Get<MPStartLocationsInfo>().InitialExploreRange; var map = world.Map;
foreach (var p in Start.Keys) foreach (var p in Start.Keys)
{
var cells = Shroud.CellsInRange(map, Start[p], info.InitialExploreRange);
foreach (var q in world.Players) foreach (var q in world.Players)
if (p.IsAlliedWith(q)) if (p.IsAlliedWith(q))
q.Shroud.Explore(world, Start[p], explore); q.Shroud.Explore(world, cells);
}
// Set viewport // Set viewport
if (world.LocalPlayer != null && Start.ContainsKey(world.LocalPlayer)) 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) static Player FindPlayerInSlot(World world, string pr)