Moved RandomWalk to OpenRA.Traits.Util

This commit is contained in:
Christopher Grant
2015-07-04 13:51:45 -04:00
committed by Matthias Mailänder
parent 421b15cad1
commit a378abe7c1
2 changed files with 16 additions and 16 deletions

View File

@@ -149,5 +149,20 @@ namespace OpenRA.Traits
return (int)a; return (int)a;
} }
public static IEnumerable<CPos> RandomWalk(CPos p, MersenneTwister r)
{
for (;;)
{
var dx = r.Next(-1, 2);
var dy = r.Next(-1, 2);
if (dx == 0 && dy == 0)
continue;
p += new CVec(dx, dy);
yield return p;
}
}
} }
} }

View File

@@ -63,7 +63,7 @@ namespace OpenRA.Mods.Common.Traits
public void Seed(Actor self) public void Seed(Actor self)
{ {
var cell = RandomWalk(self.Location, self.World.SharedRandom) var cell = Util.RandomWalk(self.Location, self.World.SharedRandom)
.Take(info.MaxRange) .Take(info.MaxRange)
.SkipWhile(p => resLayer.GetResource(p) == resourceType && resLayer.IsFull(p)) .SkipWhile(p => resLayer.GetResource(p) == resourceType && resLayer.IsFull(p))
.Cast<CPos?>().FirstOrDefault(); .Cast<CPos?>().FirstOrDefault();
@@ -71,20 +71,5 @@ namespace OpenRA.Mods.Common.Traits
if (cell != null && resLayer.CanSpawnResourceAt(resourceType, cell.Value)) if (cell != null && resLayer.CanSpawnResourceAt(resourceType, cell.Value))
resLayer.AddResource(resourceType, cell.Value, 1); resLayer.AddResource(resourceType, cell.Value, 1);
} }
static IEnumerable<CPos> RandomWalk(CPos p, MersenneTwister r)
{
for (;;)
{
var dx = r.Next(-1, 2);
var dy = r.Next(-1, 2);
if (dx == 0 && dy == 0)
continue;
p += new CVec(dx, dy);
yield return p;
}
}
} }
} }