Changed things to do with Shroud to WRange. Updated Utility.

This commit is contained in:
Chicken man
2014-02-20 17:37:18 -05:00
parent 9e1f15448d
commit cf3cc43a28
11 changed files with 44 additions and 37 deletions

View File

@@ -16,7 +16,7 @@ namespace OpenRA
/// <summary> /// <summary>
/// 1d world distance - 1024 units = 1 cell. /// 1d world distance - 1024 units = 1 cell.
/// </summary> /// </summary>
public struct WRange : IComparable public struct WRange : IComparable, IComparable<WRange>
{ {
public readonly int Range; public readonly int Range;
@@ -92,6 +92,8 @@ namespace OpenRA
return Range.CompareTo(o.Value.Range); return Range.CompareTo(o.Value.Range);
} }
public int CompareTo(WRange other) { return Range.CompareTo(other.Range); }
public override string ToString() { return "{0}".F(Range); } public override string ToString() { return "{0}".F(Range); }
} }
} }

View File

@@ -14,7 +14,7 @@ namespace OpenRA.Traits
{ {
public class CreatesShroudInfo : ITraitInfo public class CreatesShroudInfo : ITraitInfo
{ {
public readonly int Range = 0; public readonly WRange Range = WRange.Zero;
public object Create(ActorInitializer init) { return new CreatesShroud(this); } public object Create(ActorInitializer init) { return new CreatesShroud(this); }
} }
@@ -43,6 +43,6 @@ namespace OpenRA.Traits
} }
} }
public int Range { get { return cachedDisabled ? 0 : Info.Range; } } public WRange Range { get { return cachedDisabled ? WRange.Zero : Info.Range; } }
} }
} }

View File

@@ -14,7 +14,7 @@ namespace OpenRA.Traits
{ {
public class RevealsShroudInfo : ITraitInfo public class RevealsShroudInfo : ITraitInfo
{ {
public readonly int Range = 0; public readonly WRange Range = WRange.Zero;
public object Create(ActorInitializer init) { return new RevealsShroud(this); } public object Create(ActorInitializer init) { return new RevealsShroud(this); }
} }
@@ -39,6 +39,6 @@ namespace OpenRA.Traits
} }
} }
public int Range { get { return Info.Range; } } public WRange Range { get { return Info.Range; } }
} }
} }

View File

@@ -64,32 +64,24 @@ namespace OpenRA.Traits
Hash = Sync.hash_player(self.Owner) + self.World.FrameNumber * 3; Hash = Sync.hash_player(self.Owner) + self.World.FrameNumber * 3;
} }
static IEnumerable<CPos> FindVisibleTiles(World world, CPos a, int r) static IEnumerable<CPos> FindVisibleTiles(World world, CPos position, WRange radius)
{ {
var min = a - new CVec(r, r); var r = (radius.Range + 1023) / 1024;
var max = a + new CVec(r, r); var min = (position - new CVec(r, r)).Clamp(world.Map.Bounds);
if (min.X < world.Map.Bounds.Left - 1) var max = (position + new CVec(r, r)).Clamp(world.Map.Bounds);
min = new CPos(world.Map.Bounds.Left - 1, min.Y);
if (min.Y < world.Map.Bounds.Top - 1)
min = new CPos(min.X, world.Map.Bounds.Top - 1);
if (max.X > world.Map.Bounds.Right)
max = new CPos(world.Map.Bounds.Right, max.Y);
if (max.Y > world.Map.Bounds.Bottom)
max = new CPos(max.X, world.Map.Bounds.Bottom);
var circleArea = radius.Range * radius.Range;
var pos = position.CenterPosition;
for (var j = min.Y; j <= max.Y; j++) for (var j = min.Y; j <= max.Y; j++)
for (var i = min.X; i <= max.X; i++) for (var i = min.X; i <= max.X; i++)
if (r*r >= (new CPos(i, j) - a).LengthSquared) if (circleArea >= (new CPos(i, j).CenterPosition - pos).LengthSquared)
yield return new CPos(i, j); yield return new CPos(i, j);
} }
void AddVisibility(Actor a) void AddVisibility(Actor a)
{ {
var rs = a.TraitOrDefault<RevealsShroud>(); var rs = a.TraitOrDefault<RevealsShroud>();
if (rs == null || !a.Owner.IsAlliedWith(self.Owner) || rs.Range == 0) if (rs == null || !a.Owner.IsAlliedWith(self.Owner) || rs.Range == WRange.Zero)
return; return;
var origins = GetVisOrigins(a); var origins = GetVisOrigins(a);
@@ -97,9 +89,11 @@ namespace OpenRA.Traits
.Distinct().ToArray(); .Distinct().ToArray();
// Update bounding rect // Update bounding rect
var r = (rs.Range.Range + 1023) / 1024;
foreach (var o in origins) foreach (var o in origins)
{ {
var box = new Rectangle(o.X - rs.Range, o.Y - rs.Range, 2*rs.Range + 1, 2*rs.Range + 1); var box = new Rectangle(o.X - r, o.Y - r, 2 * r + 1, 2 * r + 1);
ExploredBounds = Rectangle.Union(ExploredBounds, box); ExploredBounds = Rectangle.Union(ExploredBounds, box);
} }
@@ -143,7 +137,7 @@ namespace OpenRA.Traits
void AddShroudGeneration(Actor a) void AddShroudGeneration(Actor a)
{ {
var cs = a.TraitOrDefault<CreatesShroud>(); var cs = a.TraitOrDefault<CreatesShroud>();
if (cs == null || a.Owner.IsAlliedWith(self.Owner) || cs.Range == 0) if (cs == null || a.Owner.IsAlliedWith(self.Owner) || cs.Range == WRange.Zero)
return; return;
var shrouded = GetVisOrigins(a).SelectMany(o => FindVisibleTiles(a.World, o, cs.Range)) var shrouded = GetVisOrigins(a).SelectMany(o => FindVisibleTiles(a.World, o, cs.Range))
@@ -202,12 +196,13 @@ namespace OpenRA.Traits
return new[] { a.CenterPosition.ToCPos() }; return new[] { a.CenterPosition.ToCPos() };
} }
public void Explore(World world, CPos center, int range) public void Explore(World world, CPos center, WRange range)
{ {
foreach (var q in FindVisibleTiles(world, center, range)) foreach (var q in FindVisibleTiles(world, center, range))
explored[q.X, q.Y] = true; explored[q.X, q.Y] = true;
var box = new Rectangle(center.X - range, center.Y - range, 2*range + 1, 2*range + 1); var r = (range.Range + 1023) / 1024;
var box = new Rectangle(center.X - r, center.Y - r, 2 * r + 1, 2 * r + 1);
ExploredBounds = Rectangle.Union(ExploredBounds, box); ExploredBounds = Rectangle.Union(ExploredBounds, box);
Invalidate(); Invalidate();

View File

@@ -75,9 +75,9 @@ namespace OpenRA.Mods.RA
{ {
// Move within sight range of the frozen actor // Move within sight range of the frozen actor
var sight = self.TraitOrDefault<RevealsShroud>(); var sight = self.TraitOrDefault<RevealsShroud>();
var range = sight != null ? sight.Range : 2; var range = sight != null ? sight.Range : WRange.FromCells(2);
self.QueueActivity(move.MoveWithinRange(Target.FromPos(frozen.CenterPosition), WRange.FromCells(range))); self.QueueActivity(move.MoveWithinRange(Target.FromPos(frozen.CenterPosition), range));
} }
return Target.Invalid; return Target.Invalid;

View File

@@ -19,7 +19,7 @@ namespace OpenRA.Mods.RA
{ {
public class MPStartLocationsInfo : TraitInfo<MPStartLocations> public class MPStartLocationsInfo : TraitInfo<MPStartLocations>
{ {
public readonly int InitialExploreRange = 5; public readonly WRange InitialExploreRange = WRange.FromCells(5);
} }
public class MPStartLocations : IWorldLoaded public class MPStartLocations : IWorldLoaded

View File

@@ -462,10 +462,10 @@ namespace OpenRA.Mods.RA.Missions
SetupAlliedBase(); SetupAlliedBase();
var shroud = allies1.Shroud; var shroud = allies1.Shroud;
shroud.Explore(w, sam1.Location, 2); shroud.Explore(w, sam1.Location, WRange.FromCells(2));
shroud.Explore(w, sam2.Location, 2); shroud.Explore(w, sam2.Location, WRange.FromCells(2));
shroud.Explore(w, sam3.Location, 2); shroud.Explore(w, sam3.Location, WRange.FromCells(2));
shroud.Explore(w, sam4.Location, 2); shroud.Explore(w, sam4.Location, WRange.FromCells(2));
if (w.LocalPlayer == null || w.LocalPlayer == allies1) if (w.LocalPlayer == null || w.LocalPlayer == allies1)
wr.Viewport.Center(chinookHusk.CenterPosition); wr.Viewport.Center(chinookHusk.CenterPosition);

View File

@@ -333,8 +333,8 @@ namespace OpenRA.Mods.RA.Missions
sam2 = actors["Sam2"]; sam2 = actors["Sam2"];
var shroud = allies.PlayerActor.Trait<Shroud>(); var shroud = allies.PlayerActor.Trait<Shroud>();
shroud.Explore(w, sam1.Location, 4); shroud.Explore(w, sam1.Location, WRange.FromCells(4));
shroud.Explore(w, sam2.Location, 4); shroud.Explore(w, sam2.Location, WRange.FromCells(4));
wr.Viewport.Center(alliesbase.CenterPosition); wr.Viewport.Center(alliesbase.CenterPosition);
StartCountDownTimer(); StartCountDownTimer();

View File

@@ -58,7 +58,7 @@ namespace OpenRA.Mods.RA
var total = (double)world.Map.Bounds.Width * world.Map.Bounds.Height; var total = (double)world.Map.Bounds.Width * world.Map.Bounds.Height;
MapControl = world.Actors MapControl = world.Actors
.Where(a => !a.IsDead() && a.IsInWorld && a.Owner == player && a.HasTrait<RevealsShroud>()) .Where(a => !a.IsDead() && a.IsInWorld && a.Owner == player && a.HasTrait<RevealsShroud>())
.SelectMany(a => world.FindTilesInCircle(a.Location, a.Trait<RevealsShroud>().Range.Clamp(0, 50))) .SelectMany(a => world.FindTilesInCircle(a.Location, a.Trait<RevealsShroud>().Range.Clamp(WRange.Zero, WRange.FromCells(50)).Range / 1024))
.Distinct() .Distinct()
.Count() / total; .Count() / total;
} }

View File

@@ -20,7 +20,7 @@ namespace OpenRA.Mods.RA
{ {
wr.DrawRangeCircleWithContrast( wr.DrawRangeCircleWithContrast(
centerPosition, centerPosition,
WRange.FromCells(ai.Traits.Get<CreatesShroudInfo>().Range), ai.Traits.Get<CreatesShroudInfo>().Range,
Color.FromArgb(128, Color.Cyan), Color.FromArgb(128, Color.Cyan),
Color.FromArgb(96, Color.Black) Color.FromArgb(96, Color.Black)
); );
@@ -46,7 +46,7 @@ namespace OpenRA.Mods.RA
wr.DrawRangeCircleWithContrast( wr.DrawRangeCircleWithContrast(
self.CenterPosition, self.CenterPosition,
WRange.FromCells(self.Info.Traits.Get<CreatesShroudInfo>().Range), self.Info.Traits.Get<CreatesShroudInfo>().Range,
Color.FromArgb(128, Color.Cyan), Color.FromArgb(128, Color.Cyan),
Color.FromArgb(96, Color.Black) Color.FromArgb(96, Color.Black)
); );

View File

@@ -120,6 +120,16 @@ namespace OpenRA.Utility
node.Value.Nodes.RemoveAll(n => n.Key == "UnloadFacing"); node.Value.Nodes.RemoveAll(n => n.Key == "UnloadFacing");
} }
// RevealShroud was updated to use world units.
if (engineVersion < 20140220)
{
if (depth == 2 && parentKey == "RevealsShroud" && node.Key == "Range")
ConvertFloatToRange(ref node.Value.Value);
if (depth == 2 && parentKey == "CreatesShroud" && node.Key == "Range")
ConvertFloatToRange(ref node.Value.Value);
}
UpgradeActorRules(engineVersion, ref node.Value.Nodes, node, depth + 1); UpgradeActorRules(engineVersion, ref node.Value.Nodes, node, depth + 1);
} }
} }