Merge pull request #3531 from pchote/world-targets

Convert more things to world coordinates
This commit is contained in:
Matthias Mailänder
2013-07-08 10:33:25 -07:00
77 changed files with 486 additions and 341 deletions

View File

@@ -21,8 +21,6 @@ namespace OpenRA
public readonly int X, Y;
public CPos(int x, int y) { X = x; Y = y; }
public CPos(WPos a) { X = a.X / 1024; Y = a.Y / 1024; }
public static readonly CPos Zero = new CPos(0, 0);
public static explicit operator CPos(int2 a) { return new CPos(a.X, a.Y); }
@@ -71,4 +69,9 @@ namespace OpenRA
public static CPos TopLeftAsCPos(this Rectangle r) { return new CPos(r.Left, r.Top); }
public static CPos BottomRightAsCPos(this Rectangle r) { return new CPos(r.Right, r.Bottom); }
}
}
public static class WPosExtensions
{
public static CPos ToCPos(this WPos a) { return new CPos(a.X / 1024, a.Y / 1024); }
}
}

View File

@@ -137,5 +137,37 @@ namespace OpenRA.GameRules
return ret;
}
public bool IsValidAgainst(Actor a)
{
var targetable = a.TraitOrDefault<ITargetable>();
if (targetable == null || !ValidTargets.Intersect(targetable.TargetTypes).Any())
return false;
if (Warheads.All(w => w.EffectivenessAgainst(a) <= 0))
return false;
return true;
}
public bool IsValidAgainst(Target target, World world)
{
if (!target.IsValid)
return false;
if (target.IsActor)
return IsValidAgainst(target.Actor);
else
{
var cell = target.CenterPosition.ToCPos();
if (ValidTargets.Contains("Ground") && world.GetTerrainType(cell) != "Water")
return true;
if (ValidTargets.Contains("Water") && world.GetTerrainType(cell) == "Water")
return true;
return false;
}
}
}
}

View File

@@ -60,13 +60,13 @@ namespace OpenRA.Graphics
// Start of the first line segment is the tail of the list - don't smooth it.
var curPos = trail[idx(next - skip - 1)];
var curCell = new CPos(curPos);
var curCell = curPos.ToCPos();
var curColor = color;
for (var i = 0; i < length - skip - 4; i++)
{
var j = next - skip - i - 2;
var nextPos = WPos.Average(trail[idx(j)], trail[idx(j-1)], trail[idx(j-2)], trail[idx(j-3)]);
var nextCell = new CPos(nextPos);
var nextCell = nextPos.ToCPos();
var nextColor = Exts.ColorLerp(i * 1f / (length - 4), color, Color.Transparent);
if (!world.FogObscures(curCell) && !world.FogObscures(nextCell))

View File

@@ -80,6 +80,31 @@ namespace OpenRA
il.EmitCall(OpCodes.Call, ((Func<PSubVec, int>)hash_PSubVec).Method, null);
il.Emit(OpCodes.Xor);
}
else if (type == typeof(WRange))
{
il.EmitCall(OpCodes.Call, ((Func<WRange, int>)(a => a.GetHashCode())).Method, null);
il.Emit(OpCodes.Xor);
}
else if (type == typeof(WPos))
{
il.EmitCall(OpCodes.Call, ((Func<WPos, int>)(a => a.GetHashCode())).Method, null);
il.Emit(OpCodes.Xor);
}
else if (type == typeof(WVec))
{
il.EmitCall(OpCodes.Call, ((Func<WVec, int>)(a => a.GetHashCode())).Method, null);
il.Emit(OpCodes.Xor);
}
else if (type == typeof(WAngle))
{
il.EmitCall(OpCodes.Call, ((Func<WAngle, int>)(a => a.GetHashCode())).Method, null);
il.Emit(OpCodes.Xor);
}
else if (type == typeof(WRot))
{
il.EmitCall(OpCodes.Call, ((Func<WRot, int>)(a => a.GetHashCode())).Method, null);
il.Emit(OpCodes.Xor);
}
else if (type == typeof(TypeDictionary))
{
il.EmitCall(OpCodes.Call, ((Func<TypeDictionary, int>)hash_tdict).Method, null);

View File

@@ -52,8 +52,6 @@ namespace OpenRA.Traits
public void RenderAfterWorld(WorldRenderer wr)
{
//if (self.IsIdle) return;
var force = Game.GetModifierKeys().HasModifier(Modifiers.Alt);
if ((lifetime <= 0 || --lifetime <= 0) && !force)
return;
@@ -61,9 +59,7 @@ namespace OpenRA.Traits
if (targets == null || targets.Count == 0)
return;
var move = self.TraitOrDefault<IMove>();
var origin = (move != null ? self.CenterLocation - new PVecInt(0, move.Altitude) : self.CenterLocation).ToFloat2();
var from = wr.ScreenPxPosition(self.CenterPosition);
var wlr = Game.Renderer.WorldLineRenderer;
foreach (var target in targets)
@@ -71,9 +67,10 @@ namespace OpenRA.Traits
if (!target.IsValid)
continue;
wlr.DrawLine(origin, target.CenterLocation.ToFloat2(), c, c);
DrawTargetMarker(wlr, target.CenterLocation.ToFloat2());
DrawTargetMarker(wlr, origin);
var to = wr.ScreenPxPosition(target.CenterPosition);
wlr.DrawLine(from, to, c, c);
DrawTargetMarker(wlr, from);
DrawTargetMarker(wlr, to);
}
}

View File

@@ -46,9 +46,9 @@ namespace OpenRA.Traits
var Xy = new float2(bounds.Right, bounds.Top);
wr.DrawSelectionBox(self, Color.White);
DrawHealthBar(self, xy, Xy);
DrawExtraBars(self, xy, Xy);
DrawUnitPath(self);
DrawHealthBar(wr, self, xy, Xy);
DrawExtraBars(wr, self, xy, Xy);
DrawUnitPath(wr, self);
}
public void DrawRollover(WorldRenderer wr, Actor self)
@@ -61,11 +61,11 @@ namespace OpenRA.Traits
var xy = new float2(bounds.Left, bounds.Top);
var Xy = new float2(bounds.Right, bounds.Top);
DrawHealthBar(self, xy, Xy);
DrawExtraBars(self, xy, Xy);
DrawHealthBar(wr, self, xy, Xy);
DrawExtraBars(wr, self, xy, Xy);
}
void DrawExtraBars(Actor self, float2 xy, float2 Xy)
void DrawExtraBars(WorldRenderer wr, Actor self, float2 xy, float2 Xy)
{
foreach (var extraBar in self.TraitsImplementing<ISelectionBar>())
{
@@ -74,12 +74,12 @@ namespace OpenRA.Traits
{
xy.Y += 4;
Xy.Y += 4;
DrawSelectionBar(self, xy, Xy, extraBar.GetValue(), extraBar.GetColor());
DrawSelectionBar(wr, self, xy, Xy, extraBar.GetValue(), extraBar.GetColor());
}
}
}
void DrawSelectionBar(Actor self, float2 xy, float2 Xy, float value, Color barColor)
void DrawSelectionBar(WorldRenderer wr, Actor self, float2 xy, float2 Xy, float value, Color barColor)
{
if (!self.IsInWorld) return;
@@ -102,7 +102,7 @@ namespace OpenRA.Traits
wlr.DrawLine(xy + new float2(0, -4), z + new float2(0, -4), barColor2, barColor2);
}
void DrawHealthBar(Actor self, float2 xy, float2 Xy)
void DrawHealthBar(WorldRenderer wr, Actor self, float2 xy, float2 Xy)
{
if (!self.IsInWorld) return;
@@ -148,24 +148,21 @@ namespace OpenRA.Traits
}
}
void DrawUnitPath(Actor self)
void DrawUnitPath(WorldRenderer wr, Actor self)
{
if (self.World.LocalPlayer == null ||!self.World.LocalPlayer.PlayerActor.Trait<DeveloperMode>().PathDebug) return;
var activity = self.GetCurrentActivity();
var mobile = self.TraitOrDefault<IMove>();
if (activity != null && mobile != null)
if (activity != null)
{
var alt = new float2(0, -mobile.Altitude);
var targets = activity.GetTargets(self);
var start = self.CenterLocation.ToFloat2() + alt;
var start = wr.ScreenPxPosition(self.CenterPosition);
var c = Color.Green;
var wlr = Game.Renderer.WorldLineRenderer;
foreach (var step in targets.Select(p => p.CenterLocation.ToFloat2()))
foreach (var stp in targets.Where(t => t.IsValid).Select(p => wr.ScreenPxPosition(p.CenterPosition)))
{
var stp = step + alt;
wlr.DrawLine(stp + new float2(-1, -1), stp + new float2(-1, 1), c, c);
wlr.DrawLine(stp + new float2(-1, 1), stp + new float2(1, 1), c, c);
wlr.DrawLine(stp + new float2(1, 1), stp + new float2(1, -1), c, c);

View File

@@ -8,17 +8,32 @@
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
namespace OpenRA.Traits
{
public struct Target // a target: either an actor, or a fixed location.
public struct Target
{
public static Target[] NoTargets = {};
public static readonly Target[] NoTargets = {};
public static readonly Target None = new Target();
Actor actor;
PPos pos;
WPos pos;
bool valid;
int generation;
public static Target FromPos(WPos p) { return new Target { pos = p, valid = true }; }
public static Target FromPos(PPos p) { return new Target { pos = p.ToWPos(0), valid = true }; }
public static Target FromCell(CPos c) { return new Target { pos = c.CenterPosition, valid = true }; }
public static Target FromOrder(Order o)
{
return o.TargetActor != null
? Target.FromActor(o.TargetActor)
: Target.FromCell(o.TargetLocation);
}
public static Target FromActor(Actor a)
{
return new Target
@@ -28,22 +43,56 @@ namespace OpenRA.Traits
generation = a.Generation,
};
}
public static Target FromPos(PPos p) { return new Target { pos = p, valid = true }; }
public static Target FromCell(CPos c) { return new Target { pos = Util.CenterOfCell(c), valid = true }; }
public static Target FromOrder(Order o)
{
return o.TargetActor != null
? Target.FromActor(o.TargetActor)
: Target.FromCell(o.TargetLocation);
}
public static readonly Target None = new Target();
public bool IsValid { get { return valid && (actor == null || (actor.IsInWorld && !actor.IsDead() && actor.Generation == generation)); } }
public PPos PxPosition { get { return IsActor ? actor.Trait<IHasLocation>().PxPosition : pos; } }
public PPos PxPosition { get { return IsActor ? actor.Trait<IHasLocation>().PxPosition : PPos.FromWPos(pos); } }
public PPos CenterLocation { get { return PxPosition; } }
public Actor Actor { get { return IsActor ? actor : null; } }
// TODO: This should return true even if the actor is destroyed
public bool IsActor { get { return actor != null && !actor.Destroyed; } }
// Representative position - see Positions for the full set of targetable positions.
public WPos CenterPosition
{
get
{
if (!IsValid)
throw new InvalidOperationException("Attempting to query the position of an invalid Target");
return actor != null ? actor.CenterPosition : pos;
}
}
// Positions available to target for range checks
static readonly WPos[] NoPositions = {};
public IEnumerable<WPos> Positions
{
get
{
if (!IsValid)
return NoPositions;
if (actor == null)
return new []{pos};
var targetable = actor.TraitOrDefault<ITargetable>();
if (targetable == null)
return new []{actor.CenterPosition};
return targetable.TargetableCells(actor).Select(c => c.CenterPosition);
}
}
public bool IsInRange(WPos origin, WRange range)
{
if (!IsValid)
return false;
// Target ranges are calculated in 2D, so ignore height differences
return Positions.Any(t => (t.X - origin.X)*(t.X - origin.X) +
(t.Y - origin.Y)*(t.Y - origin.Y) <= range.Range*range.Range);
}
}
}

View File

@@ -30,6 +30,11 @@ namespace OpenRA.Traits
return ( facing - rot ) & 0xFF;
}
public static int GetFacing(WVec d, int currentFacing)
{
return GetFacing(new int2(d.X, d.Y), currentFacing);
}
public static int GetFacing(PVecInt d, int currentFacing)
{
return GetFacing(d.ToInt2(), currentFacing);
@@ -40,7 +45,7 @@ namespace OpenRA.Traits
return GetFacing(d.ToInt2(), currentFacing);
}
public static int GetFacing( int2 d, int currentFacing )
public static int GetFacing(int2 d, int currentFacing)
{
if (d == int2.Zero)
return currentFacing;
@@ -177,7 +182,7 @@ namespace OpenRA.Traits
: new CPos[] {};
if (cells.Length == 0)
cells = new CPos[] { target.CenterLocation.ToCPos() };
cells = new CPos[] { target.CenterPosition.ToCPos() };
return Util.ExpandFootprint(cells, true);
}

View File

@@ -202,7 +202,7 @@ namespace OpenRA.Traits
return cells.Select(c => c.First);
}
return new[] { a.CenterLocation.ToCPos() };
return new[] { a.CenterPosition.ToCPos() };
}
public void Explore(World world, CPos center, int range)

View File

@@ -39,6 +39,11 @@ namespace OpenRA
return actors.OrderBy( a => (a.CenterLocation - px).LengthSquared ).FirstOrDefault();
}
public static IEnumerable<Actor> FindUnitsInCircle(this World world, WPos a, WRange r)
{
return world.FindUnitsInCircle(PPos.FromWPos(a), r.Range * Game.CellSize / 1024);
}
public static IEnumerable<Actor> FindUnitsInCircle(this World world, PPos a, int r)
{
using (new PerfSample("FindUnitsInCircle"))