Remove SpatialBins.

This commit is contained in:
Paul Chote
2013-09-21 16:31:07 +12:00
parent e03ec690ff
commit 3f8d75a1ac
8 changed files with 28 additions and 101 deletions

View File

@@ -184,7 +184,6 @@
<Compile Include="Traits\World\ResourceType.cs" />
<Compile Include="Traits\World\ScreenShaker.cs" />
<Compile Include="Traits\World\Shroud.cs" />
<Compile Include="Traits\World\SpatialBins.cs" />
<Compile Include="Widgets\BackgroundWidget.cs" />
<Compile Include="Widgets\ButtonWidget.cs" />
<Compile Include="Widgets\ChatDisplayWidget.cs" />

View File

@@ -8,6 +8,7 @@
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.FileFormats;
@@ -150,5 +151,30 @@ namespace OpenRA.Traits
RemovePosition(a, ios);
AddPosition(a, ios);
}
public IEnumerable<Actor> ActorsInBox(WPos a, WPos b)
{
var left = Math.Min(a.X, b.X);
var top = Math.Min(a.Y, b.Y);
var right = Math.Max(a.X, b.X);
var bottom = Math.Max(a.Y, b.Y);
var i1 = (left / info.BinSize).Clamp(0, cols - 1);
var i2 = (right / info.BinSize).Clamp(0, cols - 1);
var j1 = (top / info.BinSize).Clamp(0, rows - 1);
var j2 = (bottom / info.BinSize).Clamp(0, rows - 1);
for (var j = j1; j <= j2; j++)
{
for (var i = i1; i <= i2; i++)
{
foreach (var actor in actors[j*cols + i])
{
var c = actor.CenterPosition;
if (left <= c.X && c.X <= right && top <= c.Y && c.Y <= bottom)
yield return actor;
}
}
}
}
}
}

View File

@@ -1,87 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2011 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
namespace OpenRA.Traits
{
class SpatialBinsInfo : ITraitInfo
{
public readonly int BinSize = 8;
public object Create(ActorInitializer init) { return new SpatialBins( init.self, this ); }
}
class SpatialBins : ITick
{
List<Actor>[,] bins;
int scale;
public SpatialBins(Actor self, SpatialBinsInfo info)
{
bins = new List<Actor>[
self.World.Map.MapSize.X / info.BinSize,
self.World.Map.MapSize.Y / info.BinSize];
scale = Game.CellSize * info.BinSize;
for (var j = 0; j <= bins.GetUpperBound(1); j++)
for (var i = 0; i <= bins.GetUpperBound(0); i++)
bins[i, j] = new List<Actor>();
}
public void Tick(Actor self)
{
for (var j = 0; j <= bins.GetUpperBound(1); j++)
for (var i = 0; i <= bins.GetUpperBound(0); i++)
bins[i, j].Clear();
foreach (var a in self.World.ActorsWithTrait<IOccupySpace>())
{
var bounds = a.Actor.ExtendedBounds.Value;
if (bounds.Right <= Game.CellSize * self.World.Map.Bounds.Left) continue;
if (bounds.Bottom <= Game.CellSize * self.World.Map.Bounds.Top) continue;
if (bounds.Left >= Game.CellSize * self.World.Map.Bounds.Right) continue;
if (bounds.Top >= Game.CellSize * self.World.Map.Bounds.Bottom) continue;
var i1 = Math.Max(0, bounds.Left / scale);
var i2 = Math.Min(bins.GetUpperBound(0), bounds.Right / scale);
var j1 = Math.Max(0, bounds.Top / scale);
var j2 = Math.Min(bins.GetUpperBound(1), bounds.Bottom / scale);
for (var j = j1; j <= j2; j++)
for (var i = i1; i <= i2; i++)
bins[i, j].Add(a.Actor);
}
}
IEnumerable<Actor> ActorsInBins(int i1, int i2, int j1, int j2)
{
j1 = Math.Max(0, j1); j2 = Math.Min(j2, bins.GetUpperBound(1));
i1 = Math.Max(0, i1); i2 = Math.Min(i2, bins.GetUpperBound(0));
for (var j = j1; j <= j2; j++)
for (var i = i1; i <= i2; i++)
foreach (var a in bins[i, j])
yield return a;
}
public IEnumerable<Actor> ActorsInBox(PPos a, PPos b)
{
var r = Rectangle.FromLTRB(a.X, a.Y, b.X, b.Y);
return ActorsInBins(a.X / scale, b.X / scale, a.Y / scale, b.Y / scale)
.Distinct()
.Where(u => u.IsInWorld && u.ExtendedBounds.Value.IntersectsWith(r));
}
}
}

View File

@@ -32,16 +32,13 @@ namespace OpenRA
public static IEnumerable<Actor> FindActorsInBox(this World world, CPos tl, CPos br)
{
// TODO: Support diamond boxes for isometric maps?
return world.FindActorsInBox(tl.TopLeft, br.BottomRight);
}
public static IEnumerable<Actor> FindActorsInBox(this World world, WPos tl, WPos br)
{
var a = PPos.FromWPos(tl);
var b = PPos.FromWPos(br);
var u = PPos.Min(a, b);
var v = PPos.Max(a, b);
return world.WorldActor.Trait<SpatialBins>().ActorsInBox(u,v);
return world.ActorMap.ActorsInBox(tl, br);
}
public static Actor ClosestTo(this IEnumerable<Actor> actors, Actor a)

View File

@@ -365,8 +365,6 @@ World:
BaseActor: mcv
SupportActors: e1,e1,e1,e1,e1,e2,e2,e2,e3,e3,apc,mtnk
SpawnMPUnits:
SpatialBins:
BinSize: 4
CrateSpawner:
Minimum: 1
Maximum: 6

View File

@@ -476,8 +476,6 @@ World:
Races: ordos
BaseActor: mcvo
SpawnMPUnits:
SpatialBins:
BinSize: 4
PathFinder:
ValidateOrder:
DebugPauseState:

View File

@@ -682,8 +682,6 @@ World:
OuterSupportRadius: 5
MPStartLocations:
SpawnMPUnits:
SpatialBins:
BinSize: 4
PathFinder:
ValidateOrder:
DebugPauseState:

View File

@@ -124,8 +124,6 @@ World:
BaseActor: mcv
MPStartLocations:
SpawnMPUnits:
SpatialBins:
BinSize: 4
PathFinder:
ValidateOrder:
DebugPauseState: