Move ChooseRandomEdgeCell from WorldUtils to Map

This commit is contained in:
Pavlos Touboulidis
2014-06-13 14:31:01 +03:00
parent 060d5326ed
commit 86febed0ce
7 changed files with 19 additions and 21 deletions

View File

@@ -558,11 +558,21 @@ namespace OpenRA
return xy.Clamp(new Rectangle(r.X, r.Y, r.Width - 1, r.Height - 1)); return xy.Clamp(new Rectangle(r.X, r.Y, r.Width - 1, r.Height - 1));
} }
public CPos ChooseRandomCell(MersenneTwister r) public CPos ChooseRandomCell(MersenneTwister rand)
{ {
return new CPos( return new CPos(
r.Next(Bounds.Left, Bounds.Right), rand.Next(Bounds.Left, Bounds.Right),
r.Next(Bounds.Top, Bounds.Bottom)); rand.Next(Bounds.Top, Bounds.Bottom));
}
public CPos ChooseRandomEdgeCell(MersenneTwister rand)
{
var isX = rand.Next(2) == 0;
var edge = rand.Next(2) == 0;
return new CPos(
isX ? rand.Next(Bounds.Left, Bounds.Right) : (edge ? Bounds.Left : Bounds.Right),
!isX ? rand.Next(Bounds.Top, Bounds.Bottom) : (edge ? Bounds.Top : Bounds.Bottom));
} }
public WRange DistanceToEdge(WPos pos, WVec dir) public WRange DistanceToEdge(WPos pos, WVec dir)

View File

@@ -55,18 +55,6 @@ namespace OpenRA
} }
} }
public static CPos ChooseRandomEdgeCell(this World w)
{
var isX = w.SharedRandom.Next(2) == 0;
var edge = w.SharedRandom.Next(2) == 0;
return new CPos(
isX ? w.SharedRandom.Next(w.Map.Bounds.Left, w.Map.Bounds.Right)
: (edge ? w.Map.Bounds.Left : w.Map.Bounds.Right),
!isX ? w.SharedRandom.Next(w.Map.Bounds.Top, w.Map.Bounds.Bottom)
: (edge ? w.Map.Bounds.Top : w.Map.Bounds.Bottom));
}
public static bool HasVoices(this Actor a) public static bool HasVoices(this Actor a)
{ {
var selectable = a.Info.Traits.GetOrDefault<SelectableInfo>(); var selectable = a.Info.Traits.GetOrDefault<SelectableInfo>();

View File

@@ -89,8 +89,8 @@ namespace OpenRA.Mods.RA
if (info.DeliveryAircraft != null) if (info.DeliveryAircraft != null)
{ {
var crate = w.CreateActor(false, crateActor, new TypeDictionary { new OwnerInit(w.WorldActor.Owner) }); var crate = w.CreateActor(false, crateActor, new TypeDictionary { new OwnerInit(w.WorldActor.Owner) });
var startPos = w.ChooseRandomEdgeCell(); var startPos = w.Map.ChooseRandomEdgeCell(w.SharedRandom);
var altitude = self.World.Map.Rules.Actors[info.DeliveryAircraft].Traits.Get<PlaneInfo>().CruiseAltitude; var altitude = w.Map.Rules.Actors[info.DeliveryAircraft].Traits.Get<PlaneInfo>().CruiseAltitude;
var plane = w.CreateActor(info.DeliveryAircraft, new TypeDictionary var plane = w.CreateActor(info.DeliveryAircraft, new TypeDictionary
{ {
new CenterPositionInit(startPos.CenterPosition + new WVec(WRange.Zero, WRange.Zero, altitude)), new CenterPositionInit(startPos.CenterPosition + new WVec(WRange.Zero, WRange.Zero, altitude)),

View File

@@ -52,7 +52,7 @@ namespace OpenRA.Mods.RA.Scripting
[Desc("Returns a random cell on the visible border of the map.")] [Desc("Returns a random cell on the visible border of the map.")]
public CPos RandomEdgeCell() public CPos RandomEdgeCell()
{ {
return context.World.ChooseRandomEdgeCell(); return context.World.Map.ChooseRandomEdgeCell(context.World.SharedRandom);
} }
[Desc("Returns true if there is only one human player.")] [Desc("Returns true if there is only one human player.")]

View File

@@ -345,7 +345,7 @@ namespace OpenRA.Mods.RA.Scripting
[LuaGlobal] [LuaGlobal]
public CPos GetRandomEdgeCell() public CPos GetRandomEdgeCell()
{ {
return world.ChooseRandomEdgeCell(); return world.Map.ChooseRandomEdgeCell(world.SharedRandom);
} }
[LuaGlobal] [LuaGlobal]

View File

@@ -39,7 +39,7 @@ namespace OpenRA.Mods.RA
var info = (ParatroopersPowerInfo)Info; var info = (ParatroopersPowerInfo)Info;
var items = info.DropItems; var items = info.DropItems;
var startPos = self.World.ChooseRandomEdgeCell(); var startPos = self.World.Map.ChooseRandomEdgeCell(self.World.SharedRandom);
self.World.AddFrameEndTask(w => self.World.AddFrameEndTask(w =>
{ {

View File

@@ -29,7 +29,7 @@ namespace OpenRA.Mods.RA
{ {
base.Activate(self, order, manager); base.Activate(self, order, manager);
var enterCell = self.World.ChooseRandomEdgeCell(); var enterCell = self.World.Map.ChooseRandomEdgeCell(self.World.SharedRandom);
var altitude = self.World.Map.Rules.Actors["u2"].Traits.Get<PlaneInfo>().CruiseAltitude; var altitude = self.World.Map.Rules.Actors["u2"].Traits.Get<PlaneInfo>().CruiseAltitude;
var plane = self.World.CreateActor("u2", new TypeDictionary var plane = self.World.CreateActor("u2", new TypeDictionary